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> |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 57 | #include <proto/proto_tcp.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 58 | #include <proto/proto_http.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 59 | #include <proto/proxy.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 60 | #include <proto/queue.h> |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 61 | #include <proto/sample.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 62 | #include <proto/server.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 63 | #include <proto/session.h> |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 64 | #include <proto/stream_interface.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 65 | #include <proto/task.h> |
| 66 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 67 | const char HTTP_100[] = |
| 68 | "HTTP/1.1 100 Continue\r\n\r\n"; |
| 69 | |
| 70 | const struct chunk http_100_chunk = { |
| 71 | .str = (char *)&HTTP_100, |
| 72 | .len = sizeof(HTTP_100)-1 |
| 73 | }; |
| 74 | |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 75 | /* Warning: no "connection" header is provided with the 3xx messages below */ |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 76 | const char *HTTP_301 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 77 | "HTTP/1.1 301 Moved Permanently\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 78 | "Content-length: 0\r\n" |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 79 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 80 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 81 | const char *HTTP_302 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 82 | "HTTP/1.1 302 Found\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 83 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 84 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 85 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 86 | |
| 87 | /* same as 302 except that the browser MUST retry with the GET method */ |
| 88 | const char *HTTP_303 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 89 | "HTTP/1.1 303 See Other\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 90 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 91 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 92 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 93 | |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 94 | |
| 95 | /* same as 302 except that the browser MUST retry with the same method */ |
| 96 | const char *HTTP_307 = |
| 97 | "HTTP/1.1 307 Temporary Redirect\r\n" |
| 98 | "Cache-Control: no-cache\r\n" |
| 99 | "Content-length: 0\r\n" |
| 100 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 101 | |
| 102 | /* same as 301 except that the browser MUST retry with the same method */ |
| 103 | const char *HTTP_308 = |
| 104 | "HTTP/1.1 308 Permanent Redirect\r\n" |
| 105 | "Content-length: 0\r\n" |
| 106 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 107 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 108 | /* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */ |
| 109 | const char *HTTP_401_fmt = |
| 110 | "HTTP/1.0 401 Unauthorized\r\n" |
| 111 | "Cache-Control: no-cache\r\n" |
| 112 | "Connection: close\r\n" |
Willy Tarreau | 791d66d | 2006-07-08 16:53:38 +0200 | [diff] [blame] | 113 | "Content-Type: text/html\r\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 114 | "WWW-Authenticate: Basic realm=\"%s\"\r\n" |
| 115 | "\r\n" |
| 116 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 117 | |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 118 | const char *HTTP_407_fmt = |
| 119 | "HTTP/1.0 407 Unauthorized\r\n" |
| 120 | "Cache-Control: no-cache\r\n" |
| 121 | "Connection: close\r\n" |
| 122 | "Content-Type: text/html\r\n" |
| 123 | "Proxy-Authenticate: Basic realm=\"%s\"\r\n" |
| 124 | "\r\n" |
| 125 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 126 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 127 | |
| 128 | const int http_err_codes[HTTP_ERR_SIZE] = { |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 129 | [HTTP_ERR_200] = 200, /* used by "monitor-uri" */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 130 | [HTTP_ERR_400] = 400, |
| 131 | [HTTP_ERR_403] = 403, |
| 132 | [HTTP_ERR_408] = 408, |
| 133 | [HTTP_ERR_500] = 500, |
| 134 | [HTTP_ERR_502] = 502, |
| 135 | [HTTP_ERR_503] = 503, |
| 136 | [HTTP_ERR_504] = 504, |
| 137 | }; |
| 138 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 139 | static const char *http_err_msgs[HTTP_ERR_SIZE] = { |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 140 | [HTTP_ERR_200] = |
| 141 | "HTTP/1.0 200 OK\r\n" |
| 142 | "Cache-Control: no-cache\r\n" |
| 143 | "Connection: close\r\n" |
| 144 | "Content-Type: text/html\r\n" |
| 145 | "\r\n" |
| 146 | "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n", |
| 147 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 148 | [HTTP_ERR_400] = |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 149 | "HTTP/1.0 400 Bad request\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 150 | "Cache-Control: no-cache\r\n" |
| 151 | "Connection: close\r\n" |
| 152 | "Content-Type: text/html\r\n" |
| 153 | "\r\n" |
| 154 | "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n", |
| 155 | |
| 156 | [HTTP_ERR_403] = |
| 157 | "HTTP/1.0 403 Forbidden\r\n" |
| 158 | "Cache-Control: no-cache\r\n" |
| 159 | "Connection: close\r\n" |
| 160 | "Content-Type: text/html\r\n" |
| 161 | "\r\n" |
| 162 | "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n", |
| 163 | |
| 164 | [HTTP_ERR_408] = |
| 165 | "HTTP/1.0 408 Request Time-out\r\n" |
| 166 | "Cache-Control: no-cache\r\n" |
| 167 | "Connection: close\r\n" |
| 168 | "Content-Type: text/html\r\n" |
| 169 | "\r\n" |
| 170 | "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n", |
| 171 | |
| 172 | [HTTP_ERR_500] = |
| 173 | "HTTP/1.0 500 Server Error\r\n" |
| 174 | "Cache-Control: no-cache\r\n" |
| 175 | "Connection: close\r\n" |
| 176 | "Content-Type: text/html\r\n" |
| 177 | "\r\n" |
| 178 | "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n", |
| 179 | |
| 180 | [HTTP_ERR_502] = |
| 181 | "HTTP/1.0 502 Bad Gateway\r\n" |
| 182 | "Cache-Control: no-cache\r\n" |
| 183 | "Connection: close\r\n" |
| 184 | "Content-Type: text/html\r\n" |
| 185 | "\r\n" |
| 186 | "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n", |
| 187 | |
| 188 | [HTTP_ERR_503] = |
| 189 | "HTTP/1.0 503 Service Unavailable\r\n" |
| 190 | "Cache-Control: no-cache\r\n" |
| 191 | "Connection: close\r\n" |
| 192 | "Content-Type: text/html\r\n" |
| 193 | "\r\n" |
| 194 | "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n", |
| 195 | |
| 196 | [HTTP_ERR_504] = |
| 197 | "HTTP/1.0 504 Gateway Time-out\r\n" |
| 198 | "Cache-Control: no-cache\r\n" |
| 199 | "Connection: close\r\n" |
| 200 | "Content-Type: text/html\r\n" |
| 201 | "\r\n" |
| 202 | "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n", |
| 203 | |
| 204 | }; |
| 205 | |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 206 | /* status codes available for the stats admin page (strictly 4 chars length) */ |
| 207 | const char *stat_status_codes[STAT_STATUS_SIZE] = { |
| 208 | [STAT_STATUS_DENY] = "DENY", |
| 209 | [STAT_STATUS_DONE] = "DONE", |
| 210 | [STAT_STATUS_ERRP] = "ERRP", |
| 211 | [STAT_STATUS_EXCD] = "EXCD", |
| 212 | [STAT_STATUS_NONE] = "NONE", |
| 213 | [STAT_STATUS_PART] = "PART", |
| 214 | [STAT_STATUS_UNKN] = "UNKN", |
| 215 | }; |
| 216 | |
| 217 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 218 | /* We must put the messages here since GCC cannot initialize consts depending |
| 219 | * on strlen(). |
| 220 | */ |
| 221 | struct chunk http_err_chunks[HTTP_ERR_SIZE]; |
| 222 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 223 | /* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */ |
| 224 | static struct hdr_ctx static_hdr_ctx; |
| 225 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 226 | #define FD_SETS_ARE_BITFIELDS |
| 227 | #ifdef FD_SETS_ARE_BITFIELDS |
| 228 | /* |
| 229 | * This map is used with all the FD_* macros to check whether a particular bit |
| 230 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 231 | * which should be encoded. When FD_ISSET() returns non-zero, it means that the |
| 232 | * byte should be encoded. Be careful to always pass bytes from 0 to 255 |
| 233 | * exclusively to the macros. |
| 234 | */ |
| 235 | fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 236 | fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 237 | |
| 238 | #else |
| 239 | #error "Check if your OS uses bitfields for fd_sets" |
| 240 | #endif |
| 241 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 242 | void init_proto_http() |
| 243 | { |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 244 | int i; |
| 245 | char *tmp; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 246 | int msg; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 247 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 248 | for (msg = 0; msg < HTTP_ERR_SIZE; msg++) { |
| 249 | if (!http_err_msgs[msg]) { |
| 250 | Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg); |
| 251 | abort(); |
| 252 | } |
| 253 | |
| 254 | http_err_chunks[msg].str = (char *)http_err_msgs[msg]; |
| 255 | http_err_chunks[msg].len = strlen(http_err_msgs[msg]); |
| 256 | } |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 257 | |
| 258 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 259 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 260 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 261 | * printable characters above. |
| 262 | */ |
| 263 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 264 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
| 265 | for (i = 0; i < 32; i++) { |
| 266 | FD_SET(i, hdr_encode_map); |
| 267 | FD_SET(i, url_encode_map); |
| 268 | } |
| 269 | for (i = 127; i < 256; i++) { |
| 270 | FD_SET(i, hdr_encode_map); |
| 271 | FD_SET(i, url_encode_map); |
| 272 | } |
| 273 | |
| 274 | tmp = "\"#{|}"; |
| 275 | while (*tmp) { |
| 276 | FD_SET(*tmp, hdr_encode_map); |
| 277 | tmp++; |
| 278 | } |
| 279 | |
| 280 | tmp = "\"#"; |
| 281 | while (*tmp) { |
| 282 | FD_SET(*tmp, url_encode_map); |
| 283 | tmp++; |
| 284 | } |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 285 | |
| 286 | /* memory allocations */ |
| 287 | pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 288 | pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 289 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 290 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 291 | /* |
| 292 | * We have 26 list of methods (1 per first letter), each of which can have |
| 293 | * up to 3 entries (2 valid, 1 null). |
| 294 | */ |
| 295 | struct http_method_desc { |
| 296 | http_meth_t meth; |
| 297 | int len; |
| 298 | const char text[8]; |
| 299 | }; |
| 300 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 301 | const struct http_method_desc http_methods[26][3] = { |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 302 | ['C' - 'A'] = { |
| 303 | [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" }, |
| 304 | }, |
| 305 | ['D' - 'A'] = { |
| 306 | [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" }, |
| 307 | }, |
| 308 | ['G' - 'A'] = { |
| 309 | [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" }, |
| 310 | }, |
| 311 | ['H' - 'A'] = { |
| 312 | [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" }, |
| 313 | }, |
| 314 | ['P' - 'A'] = { |
| 315 | [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" }, |
| 316 | [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" }, |
| 317 | }, |
| 318 | ['T' - 'A'] = { |
| 319 | [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" }, |
| 320 | }, |
| 321 | /* rest is empty like this : |
| 322 | * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" }, |
| 323 | */ |
| 324 | }; |
| 325 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 326 | /* 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] | 327 | * 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] | 328 | * RFC2616 for those chars. |
| 329 | */ |
| 330 | |
| 331 | const char http_is_spht[256] = { |
| 332 | [' '] = 1, ['\t'] = 1, |
| 333 | }; |
| 334 | |
| 335 | const char http_is_crlf[256] = { |
| 336 | ['\r'] = 1, ['\n'] = 1, |
| 337 | }; |
| 338 | |
| 339 | const char http_is_lws[256] = { |
| 340 | [' '] = 1, ['\t'] = 1, |
| 341 | ['\r'] = 1, ['\n'] = 1, |
| 342 | }; |
| 343 | |
| 344 | const char http_is_sep[256] = { |
| 345 | ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1, |
| 346 | ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1, |
| 347 | ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1, |
| 348 | ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1, |
| 349 | [' '] = 1, ['\t'] = 1, ['\\'] = 1, |
| 350 | }; |
| 351 | |
| 352 | const char http_is_ctl[256] = { |
| 353 | [0 ... 31] = 1, |
| 354 | [127] = 1, |
| 355 | }; |
| 356 | |
| 357 | /* |
| 358 | * A token is any ASCII char that is neither a separator nor a CTL char. |
| 359 | * Do not overwrite values in assignment since gcc-2.95 will not handle |
| 360 | * them correctly. Instead, define every non-CTL char's status. |
| 361 | */ |
| 362 | const char http_is_token[256] = { |
| 363 | [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1, |
| 364 | ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1, |
| 365 | ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1, |
| 366 | [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0, |
| 367 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, |
| 368 | ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1, |
| 369 | ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0, |
| 370 | ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0, |
| 371 | ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1, |
| 372 | ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1, |
| 373 | ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1, |
| 374 | ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1, |
| 375 | ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1, |
| 376 | ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1, |
| 377 | ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0, |
| 378 | ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1, |
| 379 | ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1, |
| 380 | ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1, |
| 381 | ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1, |
| 382 | ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1, |
| 383 | ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1, |
| 384 | ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1, |
| 385 | ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0, |
| 386 | ['|'] = 1, ['}'] = 0, ['~'] = 1, |
| 387 | }; |
| 388 | |
| 389 | |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 390 | /* |
| 391 | * An http ver_token is any ASCII which can be found in an HTTP version, |
| 392 | * which includes 'H', 'T', 'P', '/', '.' and any digit. |
| 393 | */ |
| 394 | const char http_is_ver_token[256] = { |
| 395 | ['.'] = 1, ['/'] = 1, |
| 396 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1, |
| 397 | ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1, |
| 398 | ['H'] = 1, ['P'] = 1, ['T'] = 1, |
| 399 | }; |
| 400 | |
| 401 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 402 | /* |
Willy Tarreau | e988a79 | 2010-01-04 21:13:14 +0100 | [diff] [blame] | 403 | * Silent debug that outputs only in strace, using fd #-1. Trash is modified. |
| 404 | */ |
| 405 | #if defined(DEBUG_FSM) |
| 406 | static void http_silent_debug(int line, struct session *s) |
| 407 | { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 408 | chunk_printf(&trash, |
| 409 | "[%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", |
| 410 | line, |
| 411 | s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers, |
| 412 | 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); |
| 413 | write(-1, trash.str, trash.len); |
Willy Tarreau | e988a79 | 2010-01-04 21:13:14 +0100 | [diff] [blame] | 414 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 415 | chunk_printf(&trash, |
| 416 | " %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", |
| 417 | line, |
| 418 | s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers, |
| 419 | 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); |
| 420 | write(-1, trash.str, trash.len); |
Willy Tarreau | e988a79 | 2010-01-04 21:13:14 +0100 | [diff] [blame] | 421 | } |
| 422 | #else |
| 423 | #define http_silent_debug(l,s) do { } while (0) |
| 424 | #endif |
| 425 | |
| 426 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 427 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 428 | * 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] | 429 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 430 | * of headers is automatically adjusted. The number of bytes added is returned |
| 431 | * on success, otherwise <0 is returned indicating an error. |
| 432 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 433 | 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] | 434 | { |
| 435 | int bytes, len; |
| 436 | |
| 437 | len = strlen(text); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 438 | 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] | 439 | if (!bytes) |
| 440 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 441 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 442 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 443 | } |
| 444 | |
| 445 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 446 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 447 | * 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] | 448 | * the buffer is only opened and the space reserved, but nothing is copied. |
| 449 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 450 | * of headers is automatically adjusted. The number of bytes added is returned |
| 451 | * on success, otherwise <0 is returned indicating an error. |
| 452 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 453 | int http_header_add_tail2(struct http_msg *msg, |
| 454 | struct hdr_idx *hdr_idx, const char *text, int len) |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 455 | { |
| 456 | int bytes; |
| 457 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 458 | 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] | 459 | if (!bytes) |
| 460 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 461 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 462 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 463 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 464 | |
| 465 | /* |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 466 | * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| 467 | * If so, returns the position of the first non-space character relative to |
| 468 | * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| 469 | * to return a pointer to the place after the first space. Returns 0 if the |
| 470 | * header name does not match. Checks are case-insensitive. |
| 471 | */ |
| 472 | int http_header_match2(const char *hdr, const char *end, |
| 473 | const char *name, int len) |
| 474 | { |
| 475 | const char *val; |
| 476 | |
| 477 | if (hdr + len >= end) |
| 478 | return 0; |
| 479 | if (hdr[len] != ':') |
| 480 | return 0; |
| 481 | if (strncasecmp(hdr, name, len) != 0) |
| 482 | return 0; |
| 483 | val = hdr + len + 1; |
| 484 | while (val < end && HTTP_IS_SPHT(*val)) |
| 485 | val++; |
| 486 | if ((val >= end) && (len + 2 <= end - hdr)) |
| 487 | return len + 2; /* we may replace starting from second space */ |
| 488 | return val - hdr; |
| 489 | } |
| 490 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 491 | /* Find the end of the header value contained between <s> and <e>. See RFC2616, |
| 492 | * par 2.2 for more information. Note that it requires a valid header to return |
| 493 | * a valid result. This works for headers defined as comma-separated lists. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 494 | */ |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 495 | char *find_hdr_value_end(char *s, const char *e) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 496 | { |
| 497 | int quoted, qdpair; |
| 498 | |
| 499 | quoted = qdpair = 0; |
| 500 | for (; s < e; s++) { |
| 501 | if (qdpair) qdpair = 0; |
Willy Tarreau | 0f7f51f | 2010-08-30 11:06:34 +0200 | [diff] [blame] | 502 | else if (quoted) { |
| 503 | if (*s == '\\') qdpair = 1; |
| 504 | else if (*s == '"') quoted = 0; |
| 505 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 506 | else if (*s == '"') quoted = 1; |
| 507 | else if (*s == ',') return s; |
| 508 | } |
| 509 | return s; |
| 510 | } |
| 511 | |
| 512 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 513 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 514 | * structure holds everything necessary to use the header and find next |
| 515 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 516 | * beginning. Otherwise, the next occurrence is returned. The function returns |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 517 | * 1 when it finds a value, and 0 when there is no more. It is designed to work |
| 518 | * with headers defined as comma-separated lists. As a special case, if ctx->val |
| 519 | * is NULL when searching for a new values of a header, the current header is |
| 520 | * rescanned. This allows rescanning after a header deletion. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 521 | */ |
| 522 | int http_find_header2(const char *name, int len, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 523 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 524 | struct hdr_ctx *ctx) |
| 525 | { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 526 | char *eol, *sov; |
| 527 | int cur_idx, old_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 528 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 529 | cur_idx = ctx->idx; |
| 530 | if (cur_idx) { |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 531 | /* We have previously returned a value, let's search |
| 532 | * another one on the same line. |
| 533 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 534 | sol = ctx->line; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 535 | ctx->del = ctx->val + ctx->vlen + ctx->tws; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 536 | sov = sol + ctx->del; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 537 | eol = sol + idx->v[cur_idx].len; |
| 538 | |
| 539 | if (sov >= eol) |
| 540 | /* no more values in this header */ |
| 541 | goto next_hdr; |
| 542 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 543 | /* values remaining for this header, skip the comma but save it |
| 544 | * for later use (eg: for header deletion). |
| 545 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 546 | sov++; |
| 547 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 548 | sov++; |
| 549 | |
| 550 | goto return_hdr; |
| 551 | } |
| 552 | |
| 553 | /* first request for this header */ |
| 554 | sol += hdr_idx_first_pos(idx); |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 555 | old_idx = 0; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 556 | cur_idx = hdr_idx_first_idx(idx); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 557 | while (cur_idx) { |
| 558 | eol = sol + idx->v[cur_idx].len; |
| 559 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 560 | if (len == 0) { |
| 561 | /* No argument was passed, we want any header. |
| 562 | * To achieve this, we simply build a fake request. */ |
| 563 | while (sol + len < eol && sol[len] != ':') |
| 564 | len++; |
| 565 | name = sol; |
| 566 | } |
| 567 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 568 | if ((len < eol - sol) && |
| 569 | (sol[len] == ':') && |
| 570 | (strncasecmp(sol, name, len) == 0)) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 571 | ctx->del = len; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 572 | sov = sol + len + 1; |
| 573 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 574 | sov++; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 575 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 576 | ctx->line = sol; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 577 | ctx->prev = old_idx; |
| 578 | return_hdr: |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 579 | ctx->idx = cur_idx; |
| 580 | ctx->val = sov - sol; |
| 581 | |
| 582 | eol = find_hdr_value_end(sov, eol); |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 583 | ctx->tws = 0; |
Willy Tarreau | 275600b | 2011-09-16 08:11:26 +0200 | [diff] [blame] | 584 | while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) { |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 585 | eol--; |
| 586 | ctx->tws++; |
| 587 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 588 | ctx->vlen = eol - sov; |
| 589 | return 1; |
| 590 | } |
| 591 | next_hdr: |
| 592 | sol = eol + idx->v[cur_idx].cr + 1; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 593 | old_idx = cur_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 594 | cur_idx = idx->v[cur_idx].next; |
| 595 | } |
| 596 | return 0; |
| 597 | } |
| 598 | |
| 599 | int http_find_header(const char *name, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 600 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 601 | struct hdr_ctx *ctx) |
| 602 | { |
| 603 | return http_find_header2(name, strlen(name), sol, idx, ctx); |
| 604 | } |
| 605 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 606 | /* Remove one value of a header. This only works on a <ctx> returned by one of |
| 607 | * the http_find_header functions. The value is removed, as well as surrounding |
| 608 | * 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] | 609 | * 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] | 610 | * message <msg>. The new index is returned. If it is zero, it means there is |
| 611 | * no more header, so any processing may stop. The ctx is always left in a form |
| 612 | * that can be handled by http_find_header2() to find next occurrence. |
| 613 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 614 | 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] | 615 | { |
| 616 | int cur_idx = ctx->idx; |
| 617 | char *sol = ctx->line; |
| 618 | struct hdr_idx_elem *hdr; |
| 619 | int delta, skip_comma; |
| 620 | |
| 621 | if (!cur_idx) |
| 622 | return 0; |
| 623 | |
| 624 | hdr = &idx->v[cur_idx]; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 625 | if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 626 | /* 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] | 627 | 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] | 628 | http_msg_move_end(msg, delta); |
| 629 | idx->used--; |
| 630 | hdr->len = 0; /* unused entry */ |
| 631 | idx->v[ctx->prev].next = idx->v[ctx->idx].next; |
Willy Tarreau | 5c4784f | 2011-02-12 13:07:35 +0100 | [diff] [blame] | 632 | if (idx->tail == ctx->idx) |
| 633 | idx->tail = ctx->prev; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 634 | ctx->idx = ctx->prev; /* walk back to the end of previous header */ |
| 635 | ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1; |
| 636 | 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] | 637 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 638 | return ctx->idx; |
| 639 | } |
| 640 | |
| 641 | /* 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] | 642 | * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the |
| 643 | * last entry of the list, we remove the last separator. |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 644 | */ |
| 645 | |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 646 | skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 647 | delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma, |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 648 | sol + ctx->val + ctx->vlen + ctx->tws + skip_comma, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 649 | NULL, 0); |
| 650 | hdr->len += delta; |
| 651 | http_msg_move_end(msg, delta); |
| 652 | ctx->val = ctx->del; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 653 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 654 | return ctx->idx; |
| 655 | } |
| 656 | |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 657 | /* This function handles a server error at the stream interface level. The |
| 658 | * stream interface is assumed to be already in a closed state. An optional |
| 659 | * message is copied into the input buffer, and an HTTP status code stored. |
| 660 | * 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] | 661 | * in this buffer will be lost. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 662 | */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 663 | static void http_server_error(struct session *t, struct stream_interface *si, |
| 664 | int err, int finst, int status, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 665 | { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 666 | channel_auto_read(si->ob); |
| 667 | channel_abort(si->ob); |
| 668 | channel_auto_close(si->ob); |
| 669 | channel_erase(si->ob); |
| 670 | channel_auto_close(si->ib); |
| 671 | channel_auto_read(si->ib); |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 672 | if (status > 0 && msg) { |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 673 | t->txn.status = status; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 674 | bo_inject(si->ib, msg->str, msg->len); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 675 | } |
| 676 | if (!(t->flags & SN_ERR_MASK)) |
| 677 | t->flags |= err; |
| 678 | if (!(t->flags & SN_FINST_MASK)) |
| 679 | t->flags |= finst; |
| 680 | } |
| 681 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 682 | /* This function returns the appropriate error location for the given session |
| 683 | * and message. |
| 684 | */ |
| 685 | |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 686 | struct chunk *http_error_message(struct session *s, int msgnum) |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 687 | { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 688 | if (s->be->errmsg[msgnum].str) |
| 689 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 690 | else if (s->fe->errmsg[msgnum].str) |
| 691 | return &s->fe->errmsg[msgnum]; |
| 692 | else |
| 693 | return &http_err_chunks[msgnum]; |
| 694 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 695 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 696 | /* |
| 697 | * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text |
| 698 | * string), HTTP_METH_OTHER for unknown methods, or the identified method. |
| 699 | */ |
| 700 | static http_meth_t find_http_meth(const char *str, const int len) |
| 701 | { |
| 702 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 703 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 704 | |
| 705 | m = ((unsigned)*str - 'A'); |
| 706 | |
| 707 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 708 | for (h = http_methods[m]; h->len > 0; h++) { |
| 709 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 710 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 711 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 712 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 713 | }; |
| 714 | return HTTP_METH_OTHER; |
| 715 | } |
| 716 | return HTTP_METH_NONE; |
| 717 | |
| 718 | } |
| 719 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 720 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 721 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 722 | * It is returned otherwise. |
| 723 | */ |
| 724 | static char * |
| 725 | http_get_path(struct http_txn *txn) |
| 726 | { |
| 727 | char *ptr, *end; |
| 728 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 729 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.u; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 730 | end = ptr + txn->req.sl.rq.u_l; |
| 731 | |
| 732 | if (ptr >= end) |
| 733 | return NULL; |
| 734 | |
| 735 | /* RFC2616, par. 5.1.2 : |
| 736 | * Request-URI = "*" | absuri | abspath | authority |
| 737 | */ |
| 738 | |
| 739 | if (*ptr == '*') |
| 740 | return NULL; |
| 741 | |
| 742 | if (isalpha((unsigned char)*ptr)) { |
| 743 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 744 | ptr++; |
| 745 | while (ptr < end && |
| 746 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 747 | ptr++; |
| 748 | /* skip '://' */ |
| 749 | if (ptr == end || *ptr++ != ':') |
| 750 | return NULL; |
| 751 | if (ptr == end || *ptr++ != '/') |
| 752 | return NULL; |
| 753 | if (ptr == end || *ptr++ != '/') |
| 754 | return NULL; |
| 755 | } |
| 756 | /* skip [user[:passwd]@]host[:[port]] */ |
| 757 | |
| 758 | while (ptr < end && *ptr != '/') |
| 759 | ptr++; |
| 760 | |
| 761 | if (ptr == end) |
| 762 | return NULL; |
| 763 | |
| 764 | /* OK, we got the '/' ! */ |
| 765 | return ptr; |
| 766 | } |
| 767 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 768 | /* Returns a 302 for a redirectable request that reaches a server working in |
| 769 | * in redirect mode. This may only be called just after the stream interface |
| 770 | * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will |
| 771 | * follow normal proxy processing. NOTE: this function is designed to support |
| 772 | * being called once data are scheduled for forwarding. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 773 | */ |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 774 | void http_perform_server_redirect(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 775 | { |
| 776 | struct http_txn *txn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 777 | struct server *srv; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 778 | char *path; |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 779 | int len, rewind; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 780 | |
| 781 | /* 1: create the response header */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 782 | trash.len = strlen(HTTP_302); |
| 783 | memcpy(trash.str, HTTP_302, trash.len); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 784 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 785 | srv = objt_server(s->target); |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 786 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 787 | /* 2: add the server's prefix */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 788 | if (trash.len + srv->rdr_len > trash.size) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 789 | return; |
| 790 | |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 791 | /* special prefix "/" means don't change URL */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 792 | if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 793 | memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len); |
| 794 | trash.len += srv->rdr_len; |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 795 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 796 | |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 797 | /* 3: add the request URI. Since it was already forwarded, we need |
| 798 | * to temporarily rewind the buffer. |
| 799 | */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 800 | txn = &s->txn; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 801 | b_rew(s->req->buf, rewind = s->req->buf->o); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 802 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 803 | path = http_get_path(txn); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 804 | 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] | 805 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 806 | b_adv(s->req->buf, rewind); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 807 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 808 | if (!path) |
| 809 | return; |
| 810 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 811 | if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 812 | return; |
| 813 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 814 | memcpy(trash.str + trash.len, path, len); |
| 815 | trash.len += len; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 816 | |
| 817 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 818 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 819 | trash.len += 29; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 820 | } else { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 821 | memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23); |
| 822 | trash.len += 23; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 823 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 824 | |
| 825 | /* prepare to return without error. */ |
Willy Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 826 | si_shutr(si); |
| 827 | si_shutw(si); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 828 | si->err_type = SI_ET_NONE; |
| 829 | si->err_loc = NULL; |
| 830 | si->state = SI_ST_CLO; |
| 831 | |
| 832 | /* send the message */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 833 | http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &trash); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 834 | |
| 835 | /* 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] | 836 | srv_inc_sess_ctr(srv); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 837 | } |
| 838 | |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 839 | /* Return the error message corresponding to si->err_type. It is assumed |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 840 | * that the server side is closed. Note that err_type is actually a |
| 841 | * bitmask, where almost only aborts may be cumulated with other |
| 842 | * values. We consider that aborted operations are more important |
| 843 | * than timeouts or errors due to the fact that nobody else in the |
| 844 | * logs might explain incomplete retries. All others should avoid |
| 845 | * being cumulated. It should normally not be possible to have multiple |
| 846 | * aborts at once, but just in case, the first one in sequence is reported. |
| 847 | */ |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 848 | void http_return_srv_error(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 849 | { |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 850 | int err_type = si->err_type; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 851 | |
| 852 | if (err_type & SI_ET_QUEUE_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 853 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 854 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 855 | else if (err_type & SI_ET_CONN_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 856 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 857 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 858 | else if (err_type & SI_ET_QUEUE_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 859 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 860 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 861 | else if (err_type & SI_ET_QUEUE_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 862 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 863 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 864 | else if (err_type & SI_ET_CONN_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 865 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 866 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 867 | else if (err_type & SI_ET_CONN_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 868 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 869 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 870 | else /* SI_ET_CONN_OTHER and others */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 871 | http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 872 | 500, http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 873 | } |
| 874 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 875 | extern const char sess_term_cond[8]; |
| 876 | extern const char sess_fin_state[8]; |
| 877 | extern const char *monthname[12]; |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 878 | struct pool_head *pool2_requri; |
Willy Tarreau | 193b8c6 | 2012-11-22 00:17:38 +0100 | [diff] [blame] | 879 | struct pool_head *pool2_capture = NULL; |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 880 | struct pool_head *pool2_uniqueid; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 881 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 882 | /* |
| 883 | * Capture headers from message starting at <som> according to header list |
| 884 | * <cap_hdr>, and fill the <idx> structure appropriately. |
| 885 | */ |
| 886 | void capture_headers(char *som, struct hdr_idx *idx, |
| 887 | char **cap, struct cap_hdr *cap_hdr) |
| 888 | { |
| 889 | char *eol, *sol, *col, *sov; |
| 890 | int cur_idx; |
| 891 | struct cap_hdr *h; |
| 892 | int len; |
| 893 | |
| 894 | sol = som + hdr_idx_first_pos(idx); |
| 895 | cur_idx = hdr_idx_first_idx(idx); |
| 896 | |
| 897 | while (cur_idx) { |
| 898 | eol = sol + idx->v[cur_idx].len; |
| 899 | |
| 900 | col = sol; |
| 901 | while (col < eol && *col != ':') |
| 902 | col++; |
| 903 | |
| 904 | sov = col + 1; |
| 905 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 906 | sov++; |
| 907 | |
| 908 | for (h = cap_hdr; h; h = h->next) { |
| 909 | if ((h->namelen == col - sol) && |
| 910 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 911 | if (cap[h->index] == NULL) |
| 912 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 913 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 914 | |
| 915 | if (cap[h->index] == NULL) { |
| 916 | Alert("HTTP capture : out of memory.\n"); |
| 917 | continue; |
| 918 | } |
| 919 | |
| 920 | len = eol - sov; |
| 921 | if (len > h->len) |
| 922 | len = h->len; |
| 923 | |
| 924 | memcpy(cap[h->index], sov, len); |
| 925 | cap[h->index][len]=0; |
| 926 | } |
| 927 | } |
| 928 | sol = eol + idx->v[cur_idx].cr + 1; |
| 929 | cur_idx = idx->v[cur_idx].next; |
| 930 | } |
| 931 | } |
| 932 | |
| 933 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 934 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 935 | */ |
| 936 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 937 | |
| 938 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 939 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 940 | */ |
| 941 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 942 | ptr++; \ |
| 943 | if (likely(ptr < end)) \ |
| 944 | goto good; \ |
| 945 | else { \ |
| 946 | state = (st); \ |
| 947 | goto http_msg_ood; \ |
| 948 | } \ |
| 949 | } while (0) |
| 950 | |
| 951 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 952 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 953 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 954 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 955 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 956 | * will give undefined results. |
| 957 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 958 | * and that msg->sol points to the beginning of the response. |
| 959 | * If a complete line is found (which implies that at least one CR or LF is |
| 960 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 961 | * returned indicating an incomplete line (which does not mean that parts have |
| 962 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 963 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 964 | * upon next call. |
| 965 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 966 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 967 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 968 | * 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] | 969 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 970 | */ |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 971 | const char *http_parse_stsline(struct http_msg *msg, |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 972 | unsigned int state, const char *ptr, const char *end, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 973 | unsigned int *ret_ptr, unsigned int *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 974 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 975 | const char *msg_start = msg->chn->buf->p; |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 976 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 977 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 978 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 979 | http_msg_rpver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 980 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 981 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 982 | |
| 983 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 984 | msg->sl.st.v_l = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 985 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 986 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 987 | state = HTTP_MSG_ERROR; |
| 988 | break; |
| 989 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 990 | case HTTP_MSG_RPVER_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 991 | http_msg_rpver_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 992 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 993 | msg->sl.st.c = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 994 | goto http_msg_rpcode; |
| 995 | } |
| 996 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 997 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 998 | /* so it's a CR/LF, this is invalid */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 999 | state = HTTP_MSG_ERROR; |
| 1000 | break; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1001 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1002 | case HTTP_MSG_RPCODE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1003 | http_msg_rpcode: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1004 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1005 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 1006 | |
| 1007 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1008 | msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1009 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1010 | } |
| 1011 | |
| 1012 | /* so it's a CR/LF, so there is no reason phrase */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1013 | msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1014 | http_msg_rsp_reason: |
| 1015 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1016 | msg->sl.st.r = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1017 | msg->sl.st.r_l = 0; |
| 1018 | goto http_msg_rpline_eol; |
| 1019 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1020 | case HTTP_MSG_RPCODE_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1021 | http_msg_rpcode_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1022 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1023 | msg->sl.st.r = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1024 | goto http_msg_rpreason; |
| 1025 | } |
| 1026 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1027 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1028 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1029 | goto http_msg_rsp_reason; |
| 1030 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1031 | case HTTP_MSG_RPREASON: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1032 | http_msg_rpreason: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1033 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1034 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1035 | msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1036 | http_msg_rpline_eol: |
| 1037 | /* We have seen the end of line. Note that we do not |
| 1038 | * necessarily have the \n yet, but at least we know that we |
| 1039 | * have EITHER \r OR \n, otherwise the response would not be |
| 1040 | * complete. We can then record the response length and return |
| 1041 | * to the caller which will be able to register it. |
| 1042 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1043 | msg->sl.st.l = ptr - msg_start - msg->sol; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1044 | return ptr; |
| 1045 | |
| 1046 | #ifdef DEBUG_FULL |
| 1047 | default: |
| 1048 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1049 | exit(1); |
| 1050 | #endif |
| 1051 | } |
| 1052 | |
| 1053 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1054 | /* out of valid data */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1055 | if (ret_state) |
| 1056 | *ret_state = state; |
| 1057 | if (ret_ptr) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1058 | *ret_ptr = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1059 | return NULL; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1060 | } |
| 1061 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1062 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1063 | * This function parses a request line between <ptr> and <end>, starting with |
| 1064 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1065 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1066 | * will give undefined results. |
| 1067 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1068 | * and that msg->sol points to the beginning of the request. |
| 1069 | * If a complete line is found (which implies that at least one CR or LF is |
| 1070 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1071 | * returned indicating an incomplete line (which does not mean that parts have |
| 1072 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1073 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1074 | * upon next call. |
| 1075 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1076 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1077 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1078 | * 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] | 1079 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1080 | */ |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1081 | const char *http_parse_reqline(struct http_msg *msg, |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1082 | unsigned int state, const char *ptr, const char *end, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1083 | unsigned int *ret_ptr, unsigned int *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1084 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1085 | const char *msg_start = msg->chn->buf->p; |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 1086 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1087 | switch (state) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1088 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1089 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1090 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1091 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1092 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1093 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1094 | msg->sl.rq.m_l = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1095 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1096 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1097 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1098 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1099 | /* HTTP 0.9 request */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1100 | msg->sl.rq.m_l = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1101 | http_msg_req09_uri: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1102 | msg->sl.rq.u = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1103 | http_msg_req09_uri_e: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1104 | msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1105 | http_msg_req09_ver: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1106 | msg->sl.rq.v = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1107 | msg->sl.rq.v_l = 0; |
| 1108 | goto http_msg_rqline_eol; |
| 1109 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1110 | state = HTTP_MSG_ERROR; |
| 1111 | break; |
| 1112 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1113 | case HTTP_MSG_RQMETH_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1114 | http_msg_rqmeth_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1115 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1116 | msg->sl.rq.u = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1117 | goto http_msg_rquri; |
| 1118 | } |
| 1119 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1120 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1121 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1122 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1123 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1124 | case HTTP_MSG_RQURI: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1125 | http_msg_rquri: |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1126 | if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1127 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1128 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1129 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1130 | msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1131 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1132 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1133 | |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1134 | if (likely((unsigned char)*ptr >= 128)) { |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1135 | /* non-ASCII chars are forbidden unless option |
| 1136 | * accept-invalid-http-request is enabled in the frontend. |
| 1137 | * In any case, we capture the faulty char. |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1138 | */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1139 | if (msg->err_pos < -1) |
| 1140 | goto invalid_char; |
| 1141 | if (msg->err_pos == -1) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1142 | msg->err_pos = ptr - msg_start; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1143 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
| 1144 | } |
| 1145 | |
| 1146 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1147 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1148 | goto http_msg_req09_uri_e; |
| 1149 | } |
| 1150 | |
| 1151 | /* OK forbidden chars, 0..31 or 127 */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1152 | invalid_char: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1153 | msg->err_pos = ptr - msg_start; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1154 | state = HTTP_MSG_ERROR; |
| 1155 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1156 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1157 | case HTTP_MSG_RQURI_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1158 | http_msg_rquri_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1159 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1160 | msg->sl.rq.v = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1161 | goto http_msg_rqver; |
| 1162 | } |
| 1163 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1164 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1165 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1166 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1167 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1168 | case HTTP_MSG_RQVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1169 | http_msg_rqver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1170 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1171 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1172 | |
| 1173 | if (likely(HTTP_IS_CRLF(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1174 | msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1175 | http_msg_rqline_eol: |
| 1176 | /* We have seen the end of line. Note that we do not |
| 1177 | * necessarily have the \n yet, but at least we know that we |
| 1178 | * have EITHER \r OR \n, otherwise the request would not be |
| 1179 | * complete. We can then record the request length and return |
| 1180 | * to the caller which will be able to register it. |
| 1181 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1182 | msg->sl.rq.l = ptr - msg_start - msg->sol; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1183 | return ptr; |
| 1184 | } |
| 1185 | |
| 1186 | /* neither an HTTP_VER token nor a CRLF */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1187 | state = HTTP_MSG_ERROR; |
| 1188 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1189 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1190 | #ifdef DEBUG_FULL |
| 1191 | default: |
| 1192 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1193 | exit(1); |
| 1194 | #endif |
| 1195 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1196 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1197 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1198 | /* out of valid data */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1199 | if (ret_state) |
| 1200 | *ret_state = state; |
| 1201 | if (ret_ptr) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1202 | *ret_ptr = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1203 | return NULL; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1204 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1205 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1206 | /* |
| 1207 | * Returns the data from Authorization header. Function may be called more |
| 1208 | * than once so data is stored in txn->auth_data. When no header is found |
| 1209 | * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid |
| 1210 | * searching again for something we are unable to find anyway. |
| 1211 | */ |
| 1212 | |
Willy Tarreau | 7e2c647 | 2012-10-29 20:44:36 +0100 | [diff] [blame] | 1213 | char *get_http_auth_buff; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1214 | |
| 1215 | int |
| 1216 | get_http_auth(struct session *s) |
| 1217 | { |
| 1218 | |
| 1219 | struct http_txn *txn = &s->txn; |
| 1220 | struct chunk auth_method; |
| 1221 | struct hdr_ctx ctx; |
| 1222 | char *h, *p; |
| 1223 | int len; |
| 1224 | |
| 1225 | #ifdef DEBUG_AUTH |
| 1226 | printf("Auth for session %p: %d\n", s, txn->auth.method); |
| 1227 | #endif |
| 1228 | |
| 1229 | if (txn->auth.method == HTTP_AUTH_WRONG) |
| 1230 | return 0; |
| 1231 | |
| 1232 | if (txn->auth.method) |
| 1233 | return 1; |
| 1234 | |
| 1235 | txn->auth.method = HTTP_AUTH_WRONG; |
| 1236 | |
| 1237 | ctx.idx = 0; |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 1238 | |
| 1239 | if (txn->flags & TX_USE_PX_CONN) { |
| 1240 | h = "Proxy-Authorization"; |
| 1241 | len = strlen(h); |
| 1242 | } else { |
| 1243 | h = "Authorization"; |
| 1244 | len = strlen(h); |
| 1245 | } |
| 1246 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1247 | 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] | 1248 | return 0; |
| 1249 | |
| 1250 | h = ctx.line + ctx.val; |
| 1251 | |
| 1252 | p = memchr(h, ' ', ctx.vlen); |
| 1253 | if (!p || p == h) |
| 1254 | return 0; |
| 1255 | |
| 1256 | chunk_initlen(&auth_method, h, 0, p-h); |
| 1257 | chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1); |
| 1258 | |
| 1259 | if (!strncasecmp("Basic", auth_method.str, auth_method.len)) { |
| 1260 | |
| 1261 | len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len, |
Willy Tarreau | 7e2c647 | 2012-10-29 20:44:36 +0100 | [diff] [blame] | 1262 | get_http_auth_buff, global.tune.bufsize - 1); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1263 | |
| 1264 | if (len < 0) |
| 1265 | return 0; |
| 1266 | |
| 1267 | |
| 1268 | get_http_auth_buff[len] = '\0'; |
| 1269 | |
| 1270 | p = strchr(get_http_auth_buff, ':'); |
| 1271 | |
| 1272 | if (!p) |
| 1273 | return 0; |
| 1274 | |
| 1275 | txn->auth.user = get_http_auth_buff; |
| 1276 | *p = '\0'; |
| 1277 | txn->auth.pass = p+1; |
| 1278 | |
| 1279 | txn->auth.method = HTTP_AUTH_BASIC; |
| 1280 | return 1; |
| 1281 | } |
| 1282 | |
| 1283 | return 0; |
| 1284 | } |
| 1285 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1286 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1287 | /* |
| 1288 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | 8b1323e | 2012-03-09 14:46:19 +0100 | [diff] [blame] | 1289 | * depending on the initial msg->msg_state. The caller is responsible for |
| 1290 | * ensuring that the message does not wrap. The function can be preempted |
| 1291 | * everywhere when data are missing and recalled at the exact same location |
| 1292 | * with no information loss. The message may even be realigned between two |
| 1293 | * calls. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1294 | * 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] | 1295 | * fields. Note that msg->sol will be initialized after completing the first |
| 1296 | * state, so that none of the msg pointers has to be initialized prior to the |
| 1297 | * first call. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1298 | */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 1299 | void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1300 | { |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1301 | unsigned int state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1302 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1303 | struct buffer *buf; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1304 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1305 | state = msg->msg_state; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1306 | buf = msg->chn->buf; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1307 | ptr = buf->p + msg->next; |
| 1308 | end = buf->p + buf->i; |
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 | if (unlikely(ptr >= end)) |
| 1311 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1312 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1313 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1314 | /* |
| 1315 | * First, states that are specific to the response only. |
| 1316 | * We check them first so that request and headers are |
| 1317 | * closer to each other (accessed more often). |
| 1318 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1319 | case HTTP_MSG_RPBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1320 | http_msg_rpbefore: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1321 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1322 | /* we have a start of message, but we have to check |
| 1323 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1324 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1325 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1326 | if (unlikely(ptr != buf->p)) { |
| 1327 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1328 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1329 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1330 | bi_fast_delete(buf, ptr - buf->p); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1331 | } |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1332 | msg->sol = 0; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 1333 | msg->sl.st.l = 0; /* used in debug mode */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1334 | hdr_idx_init(idx); |
| 1335 | state = HTTP_MSG_RPVER; |
| 1336 | goto http_msg_rpver; |
| 1337 | } |
| 1338 | |
| 1339 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1340 | goto http_msg_invalid; |
| 1341 | |
| 1342 | if (unlikely(*ptr == '\n')) |
| 1343 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1344 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1345 | /* stop here */ |
| 1346 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1347 | case HTTP_MSG_RPBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1348 | http_msg_rpbefore_cr: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1349 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1350 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1351 | /* stop here */ |
| 1352 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1353 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1354 | http_msg_rpver: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1355 | case HTTP_MSG_RPVER_SP: |
| 1356 | case HTTP_MSG_RPCODE: |
| 1357 | case HTTP_MSG_RPCODE_SP: |
| 1358 | case HTTP_MSG_RPREASON: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1359 | ptr = (char *)http_parse_stsline(msg, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1360 | state, ptr, end, |
| 1361 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1362 | if (unlikely(!ptr)) |
| 1363 | return; |
| 1364 | |
| 1365 | /* we have a full response and we know that we have either a CR |
| 1366 | * or an LF at <ptr>. |
| 1367 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1368 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1369 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1370 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1371 | if (likely(*ptr == '\r')) |
| 1372 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1373 | goto http_msg_rpline_end; |
| 1374 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1375 | case HTTP_MSG_RPLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1376 | http_msg_rpline_end: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1377 | /* msg->sol must point to the first of CR or LF. */ |
| 1378 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1379 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1380 | /* stop here */ |
| 1381 | |
| 1382 | /* |
| 1383 | * Second, states that are specific to the request only |
| 1384 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1385 | case HTTP_MSG_RQBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1386 | http_msg_rqbefore: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1387 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1388 | /* we have a start of message, but we have to check |
| 1389 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1390 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1391 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1392 | if (likely(ptr != buf->p)) { |
| 1393 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1394 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1395 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1396 | bi_fast_delete(buf, ptr - buf->p); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1397 | } |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1398 | msg->sol = 0; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 1399 | msg->sl.rq.l = 0; /* used in debug mode */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1400 | state = HTTP_MSG_RQMETH; |
| 1401 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1402 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1403 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1404 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1405 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1406 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1407 | if (unlikely(*ptr == '\n')) |
| 1408 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1409 | 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] | 1410 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1411 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1412 | case HTTP_MSG_RQBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1413 | http_msg_rqbefore_cr: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1414 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1415 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1416 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1417 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1418 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1419 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1420 | case HTTP_MSG_RQMETH_SP: |
| 1421 | case HTTP_MSG_RQURI: |
| 1422 | case HTTP_MSG_RQURI_SP: |
| 1423 | case HTTP_MSG_RQVER: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1424 | ptr = (char *)http_parse_reqline(msg, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1425 | state, ptr, end, |
| 1426 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1427 | if (unlikely(!ptr)) |
| 1428 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1429 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1430 | /* we have a full request and we know that we have either a CR |
| 1431 | * or an LF at <ptr>. |
| 1432 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1433 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1434 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1435 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1436 | if (likely(*ptr == '\r')) |
| 1437 | 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] | 1438 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1439 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1440 | case HTTP_MSG_RQLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1441 | http_msg_rqline_end: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1442 | /* check for HTTP/0.9 request : no version information available. |
| 1443 | * msg->sol must point to the first of CR or LF. |
| 1444 | */ |
| 1445 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1446 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1447 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1448 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1449 | 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] | 1450 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1451 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1452 | /* |
| 1453 | * Common states below |
| 1454 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1455 | case HTTP_MSG_HDR_FIRST: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1456 | http_msg_hdr_first: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1457 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1458 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1459 | goto http_msg_hdr_name; |
| 1460 | } |
| 1461 | |
| 1462 | if (likely(*ptr == '\r')) |
| 1463 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1464 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1465 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1466 | case HTTP_MSG_HDR_NAME: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1467 | http_msg_hdr_name: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1468 | /* assumes msg->sol points to the first char */ |
| 1469 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1470 | 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] | 1471 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1472 | if (likely(*ptr == ':')) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1473 | 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] | 1474 | |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1475 | if (likely(msg->err_pos < -1) || *ptr == '\n') |
| 1476 | goto http_msg_invalid; |
| 1477 | |
| 1478 | if (msg->err_pos == -1) /* capture error pointer */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1479 | msg->err_pos = ptr - buf->p; /* >= 0 now */ |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1480 | |
| 1481 | /* and we still accept this non-token character */ |
| 1482 | 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] | 1483 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1484 | case HTTP_MSG_HDR_L1_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1485 | http_msg_hdr_l1_sp: |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1486 | /* assumes msg->sol points to the first char */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1487 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1488 | 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] | 1489 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1490 | /* header value can be basically anything except CR/LF */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1491 | msg->sov = ptr - buf->p; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1492 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1493 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1494 | goto http_msg_hdr_val; |
| 1495 | } |
| 1496 | |
| 1497 | if (likely(*ptr == '\r')) |
| 1498 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1499 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1500 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1501 | case HTTP_MSG_HDR_L1_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1502 | http_msg_hdr_l1_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1503 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1504 | 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] | 1505 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1506 | case HTTP_MSG_HDR_L1_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1507 | http_msg_hdr_l1_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1508 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1509 | /* replace HT,CR,LF with spaces */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1510 | for (; buf->p + msg->sov < ptr; msg->sov++) |
| 1511 | buf->p[msg->sov] = ' '; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1512 | goto http_msg_hdr_l1_sp; |
| 1513 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1514 | /* we had a header consisting only in spaces ! */ |
Willy Tarreau | 12e48b3 | 2012-03-05 16:57:34 +0100 | [diff] [blame] | 1515 | msg->eol = msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1516 | goto http_msg_complete_header; |
| 1517 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1518 | case HTTP_MSG_HDR_VAL: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1519 | http_msg_hdr_val: |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1520 | /* assumes msg->sol points to the first char, and msg->sov |
| 1521 | * points to the first character of the value. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1522 | */ |
| 1523 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1524 | 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] | 1525 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1526 | msg->eol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1527 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1528 | * real header end in case it ends with lots of LWS, but is this |
| 1529 | * really needed ? |
| 1530 | */ |
| 1531 | if (likely(*ptr == '\r')) |
| 1532 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1533 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1534 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1535 | case HTTP_MSG_HDR_L2_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1536 | http_msg_hdr_l2_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1537 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1538 | 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] | 1539 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1540 | case HTTP_MSG_HDR_L2_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1541 | http_msg_hdr_l2_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1542 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1543 | /* LWS: replace HT,CR,LF with spaces */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1544 | for (; buf->p + msg->eol < ptr; msg->eol++) |
| 1545 | buf->p[msg->eol] = ' '; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1546 | goto http_msg_hdr_val; |
| 1547 | } |
| 1548 | http_msg_complete_header: |
| 1549 | /* |
| 1550 | * It was a new header, so the last one is finished. |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1551 | * Assumes msg->sol points to the first char, msg->sov points |
| 1552 | * to the first character of the value and msg->eol to the |
| 1553 | * first CR or LF so we know how the line ends. We insert last |
| 1554 | * header into the index. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1555 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1556 | 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] | 1557 | idx, idx->tail) < 0)) |
| 1558 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1559 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1560 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1561 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1562 | goto http_msg_hdr_name; |
| 1563 | } |
| 1564 | |
| 1565 | if (likely(*ptr == '\r')) |
| 1566 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1567 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1568 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1569 | case HTTP_MSG_LAST_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1570 | http_msg_last_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1571 | /* Assumes msg->sol points to the first of either CR or LF */ |
| 1572 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1573 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1574 | msg->sov = msg->next = ptr - buf->p; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1575 | msg->eoh = msg->sol; |
| 1576 | msg->sol = 0; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1577 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1578 | return; |
Willy Tarreau | b56928a | 2012-04-16 14:51:55 +0200 | [diff] [blame] | 1579 | |
| 1580 | case HTTP_MSG_ERROR: |
| 1581 | /* this may only happen if we call http_msg_analyser() twice with an error */ |
| 1582 | break; |
| 1583 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1584 | #ifdef DEBUG_FULL |
| 1585 | default: |
| 1586 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1587 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1588 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1589 | } |
| 1590 | http_msg_ood: |
| 1591 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1592 | msg->msg_state = state; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1593 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1594 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1595 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1596 | http_msg_invalid: |
| 1597 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1598 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1599 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1600 | return; |
| 1601 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1602 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1603 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 1604 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 1605 | * nothing is done and 1 is returned. |
| 1606 | */ |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1607 | static int http_upgrade_v09_to_v10(struct http_txn *txn) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1608 | { |
| 1609 | int delta; |
| 1610 | char *cur_end; |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1611 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1612 | |
| 1613 | if (msg->sl.rq.v_l != 0) |
| 1614 | return 1; |
| 1615 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1616 | cur_end = msg->chn->buf->p + msg->sl.rq.l; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1617 | delta = 0; |
| 1618 | |
| 1619 | if (msg->sl.rq.u_l == 0) { |
| 1620 | /* if no URI was set, add "/" */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1621 | delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " /", 2); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1622 | cur_end += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1623 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1624 | } |
| 1625 | /* add HTTP version */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1626 | 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] | 1627 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1628 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1629 | cur_end = (char *)http_parse_reqline(msg, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1630 | HTTP_MSG_RQMETH, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1631 | msg->chn->buf->p, cur_end + 1, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1632 | NULL, NULL); |
| 1633 | if (unlikely(!cur_end)) |
| 1634 | return 0; |
| 1635 | |
| 1636 | /* we have a full HTTP/1.0 request now and we know that |
| 1637 | * we have either a CR or an LF at <ptr>. |
| 1638 | */ |
| 1639 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 1640 | return 1; |
| 1641 | } |
| 1642 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1643 | /* Parse the Connection: header of an HTTP request, looking for both "close" |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1644 | * and "keep-alive" values. If we already know that some headers may safely |
| 1645 | * 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] | 1646 | * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses) |
| 1647 | * - 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] | 1648 | * Presence of the "Upgrade" token is also checked and reported. |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1649 | * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was |
| 1650 | * found, and TX_CON_*_SET is adjusted depending on what is left so only |
| 1651 | * harmless combinations may be removed. Do not call that after changes have |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1652 | * been processed. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1653 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1654 | 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] | 1655 | { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1656 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1657 | const char *hdr_val = "Connection"; |
| 1658 | int hdr_len = 10; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1659 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1660 | if (txn->flags & TX_HDR_CONN_PRS) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1661 | return; |
| 1662 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1663 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1664 | hdr_val = "Proxy-Connection"; |
| 1665 | hdr_len = 16; |
| 1666 | } |
| 1667 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1668 | ctx.idx = 0; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1669 | txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1670 | 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] | 1671 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1672 | txn->flags |= TX_HDR_CONN_KAL; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1673 | if (to_del & 2) |
| 1674 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1675 | else |
| 1676 | txn->flags |= TX_CON_KAL_SET; |
| 1677 | } |
| 1678 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1679 | txn->flags |= TX_HDR_CONN_CLO; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1680 | if (to_del & 1) |
| 1681 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1682 | else |
| 1683 | txn->flags |= TX_CON_CLO_SET; |
| 1684 | } |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 1685 | else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) { |
| 1686 | txn->flags |= TX_HDR_CONN_UPG; |
| 1687 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1688 | } |
| 1689 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1690 | txn->flags |= TX_HDR_CONN_PRS; |
| 1691 | return; |
| 1692 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1693 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1694 | /* Apply desired changes on the Connection: header. Values may be removed and/or |
| 1695 | * added depending on the <wanted> flags, which are exclusively composed of |
| 1696 | * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The |
| 1697 | * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left. |
| 1698 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1699 | 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] | 1700 | { |
| 1701 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1702 | const char *hdr_val = "Connection"; |
| 1703 | int hdr_len = 10; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1704 | |
| 1705 | ctx.idx = 0; |
| 1706 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1707 | |
| 1708 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1709 | hdr_val = "Proxy-Connection"; |
| 1710 | hdr_len = 16; |
| 1711 | } |
| 1712 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1713 | txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1714 | 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] | 1715 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1716 | if (wanted & TX_CON_KAL_SET) |
| 1717 | txn->flags |= TX_CON_KAL_SET; |
| 1718 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1719 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1720 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1721 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1722 | if (wanted & TX_CON_CLO_SET) |
| 1723 | txn->flags |= TX_CON_CLO_SET; |
| 1724 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1725 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 1726 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1727 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1728 | |
| 1729 | if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 1730 | return; |
| 1731 | |
| 1732 | if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) { |
| 1733 | txn->flags |= TX_CON_CLO_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1734 | hdr_val = "Connection: close"; |
| 1735 | hdr_len = 17; |
| 1736 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1737 | hdr_val = "Proxy-Connection: close"; |
| 1738 | hdr_len = 23; |
| 1739 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1740 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1741 | } |
| 1742 | |
| 1743 | if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) { |
| 1744 | txn->flags |= TX_CON_KAL_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1745 | hdr_val = "Connection: keep-alive"; |
| 1746 | hdr_len = 22; |
| 1747 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1748 | hdr_val = "Proxy-Connection: keep-alive"; |
| 1749 | hdr_len = 28; |
| 1750 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1751 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1752 | } |
| 1753 | return; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1754 | } |
| 1755 | |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1756 | /* 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] | 1757 | * 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] | 1758 | * so that we know we can forward between ->sol and ->sov. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1759 | * 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] | 1760 | * 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] | 1761 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 1762 | static inline int http_parse_chunk_size(struct http_msg *msg) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1763 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1764 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1765 | const char *ptr = b_ptr(buf, msg->next); |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1766 | const char *ptr_old = ptr; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1767 | const char *end = buf->data + buf->size; |
| 1768 | const char *stop = bi_end(buf); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1769 | unsigned int chunk = 0; |
| 1770 | |
| 1771 | /* The chunk size is in the following form, though we are only |
| 1772 | * interested in the size and CRLF : |
| 1773 | * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF |
| 1774 | */ |
| 1775 | while (1) { |
| 1776 | int c; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1777 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1778 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1779 | c = hex2i(*ptr); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1780 | if (c < 0) /* not a hex digit anymore */ |
| 1781 | break; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 1782 | if (unlikely(++ptr >= end)) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1783 | ptr = buf->data; |
Willy Tarreau | 431946e | 2012-02-24 19:20:12 +0100 | [diff] [blame] | 1784 | if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */ |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1785 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1786 | chunk = (chunk << 4) + c; |
| 1787 | } |
| 1788 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1789 | /* empty size not allowed */ |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 1790 | if (unlikely(ptr == ptr_old)) |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1791 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1792 | |
| 1793 | while (http_is_spht[(unsigned char)*ptr]) { |
| 1794 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1795 | ptr = buf->data; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 1796 | if (unlikely(ptr == stop)) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1797 | return 0; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1798 | } |
| 1799 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1800 | /* Up to there, we know that at least one byte is present at *ptr. Check |
| 1801 | * for the end of chunk size. |
| 1802 | */ |
| 1803 | while (1) { |
| 1804 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1805 | /* we now have a CR or an LF at ptr */ |
| 1806 | if (likely(*ptr == '\r')) { |
| 1807 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1808 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1809 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1810 | return 0; |
| 1811 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1812 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1813 | if (*ptr != '\n') |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1814 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1815 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1816 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1817 | /* done */ |
| 1818 | break; |
| 1819 | } |
| 1820 | else if (*ptr == ';') { |
| 1821 | /* chunk extension, ends at next CRLF */ |
| 1822 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1823 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1824 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1825 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1826 | |
| 1827 | while (!HTTP_IS_CRLF(*ptr)) { |
| 1828 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1829 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1830 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1831 | return 0; |
| 1832 | } |
| 1833 | /* we have a CRLF now, loop above */ |
| 1834 | continue; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1835 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1836 | else |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1837 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1838 | } |
| 1839 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1840 | /* 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] | 1841 | * 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] | 1842 | * ->sov. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1843 | */ |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 1844 | if (unlikely(ptr < ptr_old)) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1845 | msg->sov += buf->size; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1846 | msg->sov += ptr - ptr_old; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1847 | msg->next = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 1848 | msg->chunk_len = chunk; |
| 1849 | msg->body_len += chunk; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1850 | msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1851 | return 1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1852 | error: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1853 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1854 | return -1; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1855 | } |
| 1856 | |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1857 | /* This function skips trailers in the buffer associated with HTTP |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1858 | * 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] | 1859 | * the trailers is found, it is automatically scheduled to be forwarded, |
| 1860 | * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0. |
| 1861 | * If not enough data are available, the function does not change anything |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1862 | * 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] | 1863 | * 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] | 1864 | * 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] | 1865 | * must already be in HTTP_MSG_TRAILERS state before calling this function, |
| 1866 | * which implies that all non-trailers data have already been scheduled for |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1867 | * forwarding, and that the difference between msg->sol and msg->sov exactly |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1868 | * matches the length of trailers already parsed and not forwarded. It is also |
| 1869 | * important to note that this function is designed to be able to parse wrapped |
| 1870 | * headers at end of buffer. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1871 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 1872 | static int http_forward_trailers(struct http_msg *msg) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1873 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1874 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1875 | |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1876 | /* we have msg->next which points to next line. Look for CRLF. */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1877 | while (1) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1878 | const char *p1 = NULL, *p2 = NULL; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1879 | const char *ptr = b_ptr(buf, msg->next); |
| 1880 | const char *stop = bi_end(buf); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1881 | int bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1882 | |
| 1883 | /* scan current line and stop at LF or CRLF */ |
| 1884 | while (1) { |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1885 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1886 | return 0; |
| 1887 | |
| 1888 | if (*ptr == '\n') { |
| 1889 | if (!p1) |
| 1890 | p1 = ptr; |
| 1891 | p2 = ptr; |
| 1892 | break; |
| 1893 | } |
| 1894 | |
| 1895 | if (*ptr == '\r') { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1896 | if (p1) { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1897 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1898 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1899 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1900 | p1 = ptr; |
| 1901 | } |
| 1902 | |
| 1903 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1904 | if (ptr >= buf->data + buf->size) |
| 1905 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1906 | } |
| 1907 | |
| 1908 | /* after LF; point to beginning of next line */ |
| 1909 | p2++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1910 | if (p2 >= buf->data + buf->size) |
| 1911 | p2 = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1912 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1913 | bytes = p2 - b_ptr(buf, msg->next); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1914 | if (bytes < 0) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1915 | bytes += buf->size; |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1916 | |
| 1917 | /* schedule this line for forwarding */ |
| 1918 | msg->sov += bytes; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1919 | if (msg->sov >= buf->size) |
| 1920 | msg->sov -= buf->size; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1921 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1922 | if (p1 == b_ptr(buf, msg->next)) { |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1923 | /* LF/CRLF at beginning of line => end of trailers at p2. |
| 1924 | * Everything was scheduled for forwarding, there's nothing |
| 1925 | * left from this message. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 1926 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1927 | msg->next = buffer_count(buf, buf->p, p2); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1928 | msg->msg_state = HTTP_MSG_DONE; |
| 1929 | return 1; |
| 1930 | } |
| 1931 | /* OK, next line then */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1932 | msg->next = buffer_count(buf, buf->p, p2); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1933 | } |
| 1934 | } |
| 1935 | |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 1936 | /* 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] | 1937 | * 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] | 1938 | * ->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] | 1939 | * 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] | 1940 | * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If |
| 1941 | * not enough data are available, the function does not change anything and |
| 1942 | * returns zero. If a parse error is encountered, the function returns < 0 and |
| 1943 | * does not change anything. Note: this function is designed to parse wrapped |
| 1944 | * CRLF at the end of the buffer. |
| 1945 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 1946 | static inline int http_skip_chunk_crlf(struct http_msg *msg) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1947 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1948 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1949 | const char *ptr; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1950 | int bytes; |
| 1951 | |
| 1952 | /* NB: we'll check data availabilty at the end. It's not a |
| 1953 | * problem because whatever we match first will be checked |
| 1954 | * against the correct length. |
| 1955 | */ |
| 1956 | bytes = 1; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1957 | ptr = buf->p; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1958 | if (*ptr == '\r') { |
| 1959 | bytes++; |
| 1960 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1961 | if (ptr >= buf->data + buf->size) |
| 1962 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1963 | } |
| 1964 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1965 | if (bytes > buf->i) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1966 | return 0; |
| 1967 | |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1968 | if (*ptr != '\n') { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1969 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1970 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1971 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1972 | |
| 1973 | ptr++; |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 1974 | if (unlikely(ptr >= buf->data + buf->size)) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1975 | ptr = buf->data; |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1976 | /* prepare the CRLF to be forwarded (between ->sol and ->sov) */ |
| 1977 | msg->sol = 0; |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1978 | msg->sov = msg->next = bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1979 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 1980 | return 1; |
| 1981 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1982 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 1983 | |
| 1984 | /* |
| 1985 | * Selects a compression algorithm depending on the client request. |
Willy Tarreau | 05d8460 | 2012-10-26 02:11:25 +0200 | [diff] [blame] | 1986 | */ |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 1987 | int select_compression_request_header(struct session *s, struct buffer *req) |
| 1988 | { |
| 1989 | struct http_txn *txn = &s->txn; |
Willy Tarreau | 70737d1 | 2012-10-27 00:34:28 +0200 | [diff] [blame] | 1990 | struct http_msg *msg = &txn->req; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 1991 | struct hdr_ctx ctx; |
| 1992 | struct comp_algo *comp_algo = NULL; |
Willy Tarreau | 3c7b97b | 2012-10-26 14:50:26 +0200 | [diff] [blame] | 1993 | struct comp_algo *comp_algo_back = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 1994 | |
Finn Arne Gangstad | cbb9a4b | 2012-10-29 21:43:01 +0100 | [diff] [blame] | 1995 | /* Disable compression for older user agents announcing themselves as "Mozilla/4" |
| 1996 | * 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] | 1997 | * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details. |
| 1998 | */ |
| 1999 | ctx.idx = 0; |
| 2000 | if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) && |
| 2001 | ctx.vlen >= 9 && |
Finn Arne Gangstad | cbb9a4b | 2012-10-29 21:43:01 +0100 | [diff] [blame] | 2002 | memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 && |
| 2003 | (ctx.vlen < 31 || |
| 2004 | memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 || |
| 2005 | ctx.line[ctx.val + 30] < '6' || |
| 2006 | (ctx.line[ctx.val + 30] == '6' && |
| 2007 | (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) { |
| 2008 | s->comp_algo = NULL; |
| 2009 | return 0; |
Willy Tarreau | 05d8460 | 2012-10-26 02:11:25 +0200 | [diff] [blame] | 2010 | } |
| 2011 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2012 | /* search for the algo in the backend in priority or the frontend */ |
Willy Tarreau | 3c7b97b | 2012-10-26 14:50:26 +0200 | [diff] [blame] | 2013 | 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] | 2014 | ctx.idx = 0; |
| 2015 | 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] | 2016 | 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] | 2017 | if (word_match(ctx.line + ctx.val, ctx.vlen, comp_algo->name, comp_algo->name_len)) { |
| 2018 | s->comp_algo = comp_algo; |
Willy Tarreau | 70737d1 | 2012-10-27 00:34:28 +0200 | [diff] [blame] | 2019 | |
| 2020 | /* remove all occurrences of the header when "compression offload" is set */ |
| 2021 | |
| 2022 | if ((s->be->comp && s->be->comp->offload) || |
| 2023 | (s->fe->comp && s->fe->comp->offload)) { |
| 2024 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 2025 | ctx.idx = 0; |
| 2026 | while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) { |
| 2027 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 2028 | } |
| 2029 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2030 | return 1; |
| 2031 | } |
| 2032 | } |
| 2033 | } |
| 2034 | } |
| 2035 | |
| 2036 | /* identity is implicit does not require headers */ |
Willy Tarreau | 3c7b97b | 2012-10-26 14:50:26 +0200 | [diff] [blame] | 2037 | if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) { |
| 2038 | 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] | 2039 | if (comp_algo->add_data == identity_add_data) { |
| 2040 | s->comp_algo = comp_algo; |
| 2041 | return 1; |
| 2042 | } |
| 2043 | } |
| 2044 | } |
| 2045 | |
| 2046 | s->comp_algo = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2047 | return 0; |
| 2048 | } |
| 2049 | |
| 2050 | /* |
| 2051 | * Selects a comression algorithm depending of the server response. |
| 2052 | */ |
| 2053 | int select_compression_response_header(struct session *s, struct buffer *res) |
| 2054 | { |
| 2055 | struct http_txn *txn = &s->txn; |
| 2056 | struct http_msg *msg = &txn->rsp; |
| 2057 | struct hdr_ctx ctx; |
| 2058 | struct comp_type *comp_type; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2059 | |
| 2060 | /* no common compression algorithm was found in request header */ |
| 2061 | if (s->comp_algo == NULL) |
| 2062 | goto fail; |
| 2063 | |
| 2064 | /* HTTP < 1.1 should not be compressed */ |
| 2065 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
| 2066 | goto fail; |
| 2067 | |
William Lallemand | d300261 | 2012-11-26 14:34:47 +0100 | [diff] [blame] | 2068 | /* 200 only */ |
| 2069 | if (txn->status != 200) |
| 2070 | goto fail; |
| 2071 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2072 | /* Content-Length is null */ |
| 2073 | if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0) |
| 2074 | goto fail; |
| 2075 | |
| 2076 | /* content is already compressed */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2077 | ctx.idx = 0; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2078 | if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx)) |
| 2079 | goto fail; |
| 2080 | |
Willy Tarreau | 56e9ffa | 2013-01-05 16:20:35 +0100 | [diff] [blame] | 2081 | /* no compression when Cache-Control: no-transform is present in the message */ |
| 2082 | ctx.idx = 0; |
| 2083 | while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) { |
| 2084 | if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12)) |
| 2085 | goto fail; |
| 2086 | } |
| 2087 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2088 | comp_type = NULL; |
| 2089 | |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2090 | /* we don't want to compress multipart content-types, nor content-types that are |
| 2091 | * not listed in the "compression type" directive if any. If no content-type was |
| 2092 | * found but configuration requires one, we don't compress either. Backend has |
| 2093 | * the priority. |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2094 | */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2095 | ctx.idx = 0; |
| 2096 | if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) { |
| 2097 | if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0) |
| 2098 | goto fail; |
| 2099 | |
| 2100 | if ((s->be->comp && (comp_type = s->be->comp->types)) || |
| 2101 | (s->fe->comp && (comp_type = s->fe->comp->types))) { |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2102 | for (; comp_type; comp_type = comp_type->next) { |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2103 | if (ctx.vlen >= comp_type->name_len && |
| 2104 | 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] | 2105 | /* this Content-Type should be compressed */ |
| 2106 | break; |
| 2107 | } |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2108 | /* this Content-Type should not be compressed */ |
| 2109 | if (comp_type == NULL) |
| 2110 | goto fail; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2111 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2112 | } |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2113 | else { /* no content-type header */ |
| 2114 | if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types)) |
| 2115 | goto fail; /* a content-type was required */ |
William Lallemand | d300261 | 2012-11-26 14:34:47 +0100 | [diff] [blame] | 2116 | } |
| 2117 | |
William Lallemand | d85f917 | 2012-11-09 17:05:39 +0100 | [diff] [blame] | 2118 | /* limit compression rate */ |
| 2119 | if (global.comp_rate_lim > 0) |
| 2120 | if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim) |
| 2121 | goto fail; |
| 2122 | |
William Lallemand | 072a2bf | 2012-11-20 17:01:01 +0100 | [diff] [blame] | 2123 | /* limit cpu usage */ |
| 2124 | if (idle_pct < compress_min_idle) |
| 2125 | goto fail; |
| 2126 | |
William Lallemand | 4c49fae | 2012-11-07 15:00:23 +0100 | [diff] [blame] | 2127 | /* initialize compression */ |
William Lallemand | f374783 | 2012-11-09 12:33:10 +0100 | [diff] [blame] | 2128 | 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] | 2129 | goto fail; |
| 2130 | |
William Lallemand | ec3e389 | 2012-11-12 17:02:18 +0100 | [diff] [blame] | 2131 | s->flags |= SN_COMP_READY; |
| 2132 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2133 | /* remove Content-Length header */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2134 | ctx.idx = 0; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2135 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx)) |
| 2136 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 2137 | |
| 2138 | /* add Transfer-Encoding header */ |
| 2139 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) |
| 2140 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26); |
| 2141 | |
| 2142 | /* |
| 2143 | * Add Content-Encoding header when it's not identity encoding. |
| 2144 | * RFC 2616 : Identity encoding: This content-coding is used only in the |
| 2145 | * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding |
| 2146 | * header. |
| 2147 | */ |
| 2148 | if (s->comp_algo->add_data != identity_add_data) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2149 | trash.len = 18; |
| 2150 | memcpy(trash.str, "Content-Encoding: ", trash.len); |
| 2151 | memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len); |
| 2152 | trash.len += s->comp_algo->name_len; |
| 2153 | trash.str[trash.len] = '\0'; |
| 2154 | 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] | 2155 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2156 | return 1; |
| 2157 | |
| 2158 | fail: |
Willy Tarreau | b97b619 | 2012-11-19 14:55:02 +0100 | [diff] [blame] | 2159 | s->comp_algo = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2160 | return 0; |
| 2161 | } |
| 2162 | |
| 2163 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2164 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 2165 | * processing can continue on next analysers, or zero if it either needs more |
| 2166 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
| 2167 | * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers |
| 2168 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 2169 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2170 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 2171 | 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] | 2172 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2173 | /* |
| 2174 | * We will parse the partial (or complete) lines. |
| 2175 | * We will check the request syntax, and also join multi-line |
| 2176 | * headers. An index of all the lines will be elaborated while |
| 2177 | * parsing. |
| 2178 | * |
| 2179 | * For the parsing, we use a 28 states FSM. |
| 2180 | * |
| 2181 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2182 | * req->buf->p = beginning of request |
| 2183 | * req->buf->p + msg->eoh = end of processed headers / start of current one |
| 2184 | * req->buf->p + req->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 2185 | * msg->eol = end of current header or line (LF or CRLF) |
| 2186 | * msg->next = first non-visited byte |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2187 | * |
| 2188 | * At end of parsing, we may perform a capture of the error (if any), and |
| 2189 | * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE). |
| 2190 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 2191 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2192 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2193 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2194 | int cur_idx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2195 | int use_close_only; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2196 | struct http_txn *txn = &s->txn; |
| 2197 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2198 | struct hdr_ctx ctx; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2199 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 2200 | 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] | 2201 | now_ms, __FUNCTION__, |
| 2202 | s, |
| 2203 | req, |
| 2204 | req->rex, req->wex, |
| 2205 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2206 | req->buf->i, |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 2207 | req->analysers); |
| 2208 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 2209 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 2210 | s->srv_error = http_return_srv_error; |
| 2211 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2212 | /* There's a protected area at the end of the buffer for rewriting |
| 2213 | * purposes. We don't want to start to parse the request if the |
| 2214 | * protected area is affected, because we may have to move processed |
| 2215 | * data later, which is much more complicated. |
| 2216 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2217 | if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2218 | if ((txn->flags & TX_NOT_FIRST) && |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 2219 | unlikely(channel_full(req) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2220 | bi_end(req->buf) < b_ptr(req->buf, msg->next) || |
| 2221 | bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite)) { |
| 2222 | if (req->buf->o) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2223 | 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] | 2224 | goto failed_keep_alive; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2225 | /* 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] | 2226 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2227 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2228 | return 0; |
| 2229 | } |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2230 | if (bi_end(req->buf) < b_ptr(req->buf, msg->next) || |
| 2231 | bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite) |
| 2232 | buffer_slow_realign(msg->chn->buf); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2233 | } |
| 2234 | |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2235 | /* Note that we have the same problem with the response ; we |
| 2236 | * may want to send a redirect, error or anything which requires |
| 2237 | * some spare space. So we'll ensure that we have at least |
| 2238 | * maxrewrite bytes available in the response buffer before |
| 2239 | * processing that one. This will only affect pipelined |
| 2240 | * keep-alive requests. |
| 2241 | */ |
| 2242 | if ((txn->flags & TX_NOT_FIRST) && |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 2243 | unlikely(channel_full(s->rep) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2244 | bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) || |
| 2245 | bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) { |
| 2246 | if (s->rep->buf->o) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2247 | 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] | 2248 | goto failed_keep_alive; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2249 | /* don't let a connection request be initiated */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2250 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2251 | s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 0499e35 | 2010-12-17 07:13:42 +0100 | [diff] [blame] | 2252 | s->rep->analysers |= an_bit; /* wake us up once it changes */ |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2253 | return 0; |
| 2254 | } |
| 2255 | } |
| 2256 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2257 | if (likely(msg->next < req->buf->i)) /* some unparsed data are available */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 2258 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2259 | } |
| 2260 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2261 | /* 1: we might have to print this header in debug mode */ |
| 2262 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2263 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2264 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2265 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2266 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2267 | sol = req->buf->p; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 2268 | /* this is a bit complex : in case of error on the request line, |
| 2269 | * we know that rq.l is still zero, so we display only the part |
| 2270 | * up to the end of the line (truncated by debug_hdr). |
| 2271 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2272 | 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] | 2273 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2274 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2275 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2276 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2277 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2278 | while (cur_idx) { |
| 2279 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2280 | debug_hdr("clihdr", s, sol, eol); |
| 2281 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2282 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2283 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2284 | } |
| 2285 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2286 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2287 | /* |
| 2288 | * Now we quickly check if we have found a full valid request. |
| 2289 | * If not so, we check the FD and buffer states before leaving. |
| 2290 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2291 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2292 | * requests are checked first. When waiting for a second request |
| 2293 | * on a keep-alive session, if we encounter and error, close, t/o, |
| 2294 | * we note the error in the session flags but don't set any state. |
| 2295 | * Since the error will be noted there, it will not be counted by |
| 2296 | * process_session() as a frontend error. |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2297 | * Last, we may increase some tracked counters' http request errors on |
| 2298 | * the cases that are deliberately the client's fault. For instance, |
| 2299 | * a timeout or connection reset is not counted as an error. However |
| 2300 | * a bad request is. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2301 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2302 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2303 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2304 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2305 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2306 | */ |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2307 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2308 | session_inc_http_req_ctr(s); |
| 2309 | session_inc_http_err_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2310 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2311 | goto return_bad_req; |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2312 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2313 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2314 | /* 1: Since we are in header mode, if there's no space |
| 2315 | * left for headers, we won't be able to free more |
| 2316 | * later, so the session will never terminate. We |
| 2317 | * must terminate it now. |
| 2318 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2319 | if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2320 | /* FIXME: check if URI is set and return Status |
| 2321 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2322 | */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2323 | session_inc_http_req_ctr(s); |
| 2324 | session_inc_http_err_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2325 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 2326 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2327 | msg->err_pos = req->buf->i; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2328 | goto return_bad_req; |
| 2329 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2330 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2331 | /* 2: have we encountered a read error ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2332 | else if (req->flags & CF_READ_ERROR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2333 | if (!(s->flags & SN_ERR_MASK)) |
| 2334 | s->flags |= SN_ERR_CLICL; |
| 2335 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2336 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2337 | goto failed_keep_alive; |
| 2338 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2339 | /* we cannot return any message on error */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2340 | if (msg->err_pos >= 0) { |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2341 | 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] | 2342 | session_inc_http_err_ctr(s); |
| 2343 | } |
| 2344 | |
Willy Tarreau | dc979f2 | 2012-12-04 10:39:01 +0100 | [diff] [blame] | 2345 | txn->status = 400; |
| 2346 | stream_int_retnclose(req->prod, NULL); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2347 | msg->msg_state = HTTP_MSG_ERROR; |
| 2348 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2349 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2350 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2351 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2352 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2353 | if (s->listener->counters) |
| 2354 | s->listener->counters->failed_req++; |
| 2355 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2356 | if (!(s->flags & SN_FINST_MASK)) |
| 2357 | s->flags |= SN_FINST_R; |
| 2358 | return 0; |
| 2359 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2360 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2361 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2362 | 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] | 2363 | if (!(s->flags & SN_ERR_MASK)) |
| 2364 | s->flags |= SN_ERR_CLITO; |
| 2365 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2366 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2367 | goto failed_keep_alive; |
| 2368 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2369 | /* read timeout : give up with an error message. */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2370 | if (msg->err_pos >= 0) { |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2371 | 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] | 2372 | session_inc_http_err_ctr(s); |
| 2373 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2374 | txn->status = 408; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2375 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2376 | msg->msg_state = HTTP_MSG_ERROR; |
| 2377 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2378 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2379 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2380 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2381 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2382 | if (s->listener->counters) |
| 2383 | s->listener->counters->failed_req++; |
| 2384 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2385 | if (!(s->flags & SN_FINST_MASK)) |
| 2386 | s->flags |= SN_FINST_R; |
| 2387 | return 0; |
| 2388 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2389 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2390 | /* 4: have we encountered a close ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2391 | else if (req->flags & CF_SHUTR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2392 | if (!(s->flags & SN_ERR_MASK)) |
| 2393 | s->flags |= SN_ERR_CLICL; |
| 2394 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2395 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2396 | goto failed_keep_alive; |
| 2397 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2398 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2399 | 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] | 2400 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2401 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2402 | msg->msg_state = HTTP_MSG_ERROR; |
| 2403 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2404 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2405 | session_inc_http_err_ctr(s); |
| 2406 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2407 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2408 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2409 | if (s->listener->counters) |
| 2410 | s->listener->counters->failed_req++; |
| 2411 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2412 | if (!(s->flags & SN_FINST_MASK)) |
| 2413 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2414 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2415 | } |
| 2416 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2417 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2418 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 2419 | s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2420 | #ifdef TCP_QUICKACK |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2421 | if (s->listener->options & LI_O_NOQUICKACK && req->buf->i) { |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2422 | /* We need more data, we have to re-enable quick-ack in case we |
| 2423 | * previously disabled it, otherwise we might cause the client |
| 2424 | * to delay next data. |
| 2425 | */ |
Willy Tarreau | 7f7ad91 | 2012-11-11 19:27:15 +0100 | [diff] [blame] | 2426 | setsockopt(s->si[0].conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2427 | } |
| 2428 | #endif |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2429 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2430 | if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) { |
| 2431 | /* If the client starts to talk, let's fall back to |
| 2432 | * request timeout processing. |
| 2433 | */ |
| 2434 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2435 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2436 | } |
| 2437 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2438 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2439 | if (!tick_isset(req->analyse_exp)) { |
| 2440 | if ((msg->msg_state == HTTP_MSG_RQBEFORE) && |
| 2441 | (txn->flags & TX_WAIT_NEXT_RQ) && |
| 2442 | tick_isset(s->be->timeout.httpka)) |
| 2443 | req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka); |
| 2444 | else |
| 2445 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
| 2446 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2447 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2448 | /* we're not ready yet */ |
| 2449 | return 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2450 | |
| 2451 | failed_keep_alive: |
| 2452 | /* Here we process low-level errors for keep-alive requests. In |
| 2453 | * short, if the request is not the first one and it experiences |
| 2454 | * a timeout, read error or shutdown, we just silently close so |
| 2455 | * that the client can try again. |
| 2456 | */ |
| 2457 | txn->status = 0; |
| 2458 | msg->msg_state = HTTP_MSG_RQBEFORE; |
| 2459 | req->analysers = 0; |
| 2460 | s->logs.logwait = 0; |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2461 | s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 2462 | stream_int_retnclose(req->prod, NULL); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2463 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2464 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2465 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2466 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 2467 | * complete the request parsing by setting a few fields we will need |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2468 | * 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] | 2469 | * 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] | 2470 | * 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] | 2471 | * byte after the last LF. msg->sov points to the first byte of data. |
| 2472 | * msg->eol cannot be trusted because it may have been left uninitialized |
| 2473 | * (for instance in the absence of headers). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2474 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2475 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2476 | session_inc_http_req_ctr(s); |
Willy Tarreau | d9b587f | 2010-02-26 10:05:55 +0100 | [diff] [blame] | 2477 | proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */ |
| 2478 | |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2479 | if (txn->flags & TX_WAIT_NEXT_RQ) { |
| 2480 | /* kill the pending keep-alive timeout */ |
| 2481 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 2482 | req->analyse_exp = TICK_ETERNITY; |
| 2483 | } |
| 2484 | |
| 2485 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2486 | /* Maybe we found in invalid header name while we were configured not |
| 2487 | * to block on that, so we have to capture it now. |
| 2488 | */ |
| 2489 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2490 | 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] | 2491 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2492 | /* |
| 2493 | * 1: identify the method |
| 2494 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2495 | 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] | 2496 | |
| 2497 | /* we can make use of server redirect on GET and HEAD */ |
| 2498 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 2499 | s->flags |= SN_REDIRECTABLE; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2500 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2501 | /* |
| 2502 | * 2: check if the URI matches the monitor_uri. |
| 2503 | * We have to do this for every request which gets in, because |
| 2504 | * the monitor-uri is defined by the frontend. |
| 2505 | */ |
| 2506 | if (unlikely((s->fe->monitor_uri_len != 0) && |
| 2507 | (s->fe->monitor_uri_len == msg->sl.rq.u_l) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2508 | !memcmp(req->buf->p + msg->sl.rq.u, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2509 | s->fe->monitor_uri, |
| 2510 | s->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2511 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2512 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2513 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2514 | struct acl_cond *cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2515 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2516 | s->flags |= SN_MONITOR; |
Willy Tarreau | eabea07 | 2011-09-10 23:29:44 +0200 | [diff] [blame] | 2517 | s->fe->fe_counters.intercepted_req++; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2518 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2519 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2520 | list_for_each_entry(cond, &s->fe->mon_fail_cond, list) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 2521 | 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] | 2522 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2523 | ret = acl_pass(ret); |
| 2524 | if (cond->pol == ACL_COND_UNLESS) |
| 2525 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2526 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2527 | if (ret) { |
| 2528 | /* we fail this request, let's return 503 service unavail */ |
| 2529 | txn->status = 503; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2530 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2531 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2532 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2533 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 2534 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2535 | /* nothing to fail, let's reply normaly */ |
| 2536 | txn->status = 200; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2537 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2538 | goto return_prx_cond; |
| 2539 | } |
| 2540 | |
| 2541 | /* |
| 2542 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 2543 | * Note: we cannot log anymore if the request has been |
| 2544 | * classified as invalid. |
| 2545 | */ |
| 2546 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 2547 | /* we have a complete HTTP request that we must log */ |
| 2548 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
| 2549 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2550 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2551 | if (urilen >= REQURI_LEN) |
| 2552 | urilen = REQURI_LEN - 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2553 | memcpy(txn->uri, req->buf->p, urilen); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2554 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2555 | |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 2556 | if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT))) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2557 | s->do_log(s); |
| 2558 | } else { |
| 2559 | Alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2560 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2561 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2562 | |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 2563 | if (!LIST_ISEMPTY(&s->fe->format_unique_id)) |
| 2564 | s->unique_id = pool_alloc2(pool2_uniqueid); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 2565 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2566 | /* 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] | 2567 | 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] | 2568 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2569 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2570 | /* ... and check if the request is HTTP/1.1 or above */ |
| 2571 | if ((msg->sl.rq.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2572 | ((req->buf->p[msg->sl.rq.v + 5] > '1') || |
| 2573 | ((req->buf->p[msg->sl.rq.v + 5] == '1') && |
| 2574 | (req->buf->p[msg->sl.rq.v + 7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2575 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2576 | |
| 2577 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 2578 | 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] | 2579 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2580 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 2581 | * we have what looks like a proxied connection instead of a connection, |
| 2582 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 2583 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 2584 | * happen to do that despite being non-standard :-( |
| 2585 | * We consider that a request not beginning with either '/' or '*' is |
| 2586 | * a proxied connection, which covers both "scheme://location" and |
| 2587 | * CONNECT ip:port. |
| 2588 | */ |
| 2589 | if ((s->fe->options2 & PR_O2_USE_PXHDR) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2590 | 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] | 2591 | txn->flags |= TX_USE_PX_CONN; |
| 2592 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2593 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2594 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2595 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2596 | /* 5: we may need to capture headers */ |
Willy Tarreau | 42f7d89 | 2012-03-24 08:28:09 +0100 | [diff] [blame] | 2597 | if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2598 | capture_headers(req->buf->p, &txn->hdr_idx, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2599 | txn->req.cap, s->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2600 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2601 | /* 6: determine the transfer-length. |
| 2602 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 2603 | * the presence of a message-body in a REQUEST and its transfer length |
| 2604 | * must be determined that way (in order of precedence) : |
| 2605 | * 1. The presence of a message-body in a request is signaled by the |
| 2606 | * inclusion of a Content-Length or Transfer-Encoding header field |
| 2607 | * in the request's header fields. When a request message contains |
| 2608 | * both a message-body of non-zero length and a method that does |
| 2609 | * not define any semantics for that request message-body, then an |
| 2610 | * origin server SHOULD either ignore the message-body or respond |
| 2611 | * with an appropriate error message (e.g., 413). A proxy or |
| 2612 | * gateway, when presented the same request, SHOULD either forward |
| 2613 | * the request inbound with the message- body or ignore the |
| 2614 | * message-body when determining a response. |
| 2615 | * |
| 2616 | * 2. If a Transfer-Encoding header field (Section 9.7) is present |
| 2617 | * and the "chunked" transfer-coding (Section 6.2) is used, the |
| 2618 | * transfer-length is defined by the use of this transfer-coding. |
| 2619 | * If a Transfer-Encoding header field is present and the "chunked" |
| 2620 | * transfer-coding is not present, the transfer-length is defined |
| 2621 | * by the sender closing the connection. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2622 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2623 | * 3. If a Content-Length header field is present, its decimal value in |
| 2624 | * OCTETs represents both the entity-length and the transfer-length. |
| 2625 | * If a message is received with both a Transfer-Encoding header |
| 2626 | * field and a Content-Length header field, the latter MUST be ignored. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2627 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2628 | * 4. By the server closing the connection. (Closing the connection |
| 2629 | * cannot be used to indicate the end of a request body, since that |
| 2630 | * would leave no possibility for the server to send back a response.) |
| 2631 | * |
| 2632 | * Whenever a transfer-coding is applied to a message-body, the set of |
| 2633 | * transfer-codings MUST include "chunked", unless the message indicates |
| 2634 | * it is terminated by closing the connection. When the "chunked" |
| 2635 | * transfer-coding is used, it MUST be the last transfer-coding applied |
| 2636 | * to the message-body. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2637 | */ |
| 2638 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2639 | use_close_only = 0; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2640 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2641 | /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2642 | while ((msg->flags & HTTP_MSGF_VER_11) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2643 | 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] | 2644 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2645 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 2646 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2647 | /* bad transfer-encoding (chunked followed by something else) */ |
| 2648 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2649 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2650 | break; |
| 2651 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2652 | } |
| 2653 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2654 | ctx.idx = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2655 | while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2656 | 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] | 2657 | signed long long cl; |
| 2658 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2659 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2660 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2661 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2662 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2663 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2664 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2665 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2666 | goto return_bad_req; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2667 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2668 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2669 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2670 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2671 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2672 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2673 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2674 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2675 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2676 | goto return_bad_req; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2677 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2678 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2679 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2680 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2681 | } |
| 2682 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2683 | /* bodyless requests have a known length */ |
| 2684 | if (!use_close_only) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2685 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2686 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2687 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2688 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2689 | req->analyse_exp = TICK_ETERNITY; |
| 2690 | return 1; |
| 2691 | |
| 2692 | return_bad_req: |
| 2693 | /* We centralize bad requests processing here */ |
| 2694 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 2695 | /* we detected a parsing error. We want to archive this request |
| 2696 | * in the dedicated proxy area for later troubleshooting. |
| 2697 | */ |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2698 | 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] | 2699 | } |
| 2700 | |
| 2701 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 2702 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2703 | 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] | 2704 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2705 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2706 | if (s->listener->counters) |
| 2707 | s->listener->counters->failed_req++; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2708 | |
| 2709 | return_prx_cond: |
| 2710 | if (!(s->flags & SN_ERR_MASK)) |
| 2711 | s->flags |= SN_ERR_PRXCOND; |
| 2712 | if (!(s->flags & SN_FINST_MASK)) |
| 2713 | s->flags |= SN_FINST_R; |
| 2714 | |
| 2715 | req->analysers = 0; |
| 2716 | req->analyse_exp = TICK_ETERNITY; |
| 2717 | return 0; |
| 2718 | } |
| 2719 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2720 | /* We reached the stats page through a POST request. |
| 2721 | * Parse the posted data and enable/disable servers if necessary. |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2722 | * Returns 1 if request was parsed or zero if it needs more data. |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2723 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 2724 | int http_process_req_stat_post(struct stream_interface *si, struct http_txn *txn, struct channel *req) |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2725 | { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2726 | struct proxy *px = NULL; |
| 2727 | struct server *sv = NULL; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2728 | |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2729 | char key[LINESIZE]; |
| 2730 | int action = ST_ADM_ACTION_NONE; |
| 2731 | int reprocess = 0; |
| 2732 | |
| 2733 | int total_servers = 0; |
| 2734 | int altered_servers = 0; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2735 | |
| 2736 | char *first_param, *cur_param, *next_param, *end_params; |
Willy Tarreau | 46787ed | 2012-04-11 17:28:40 +0200 | [diff] [blame] | 2737 | char *st_cur_param = NULL; |
| 2738 | char *st_next_param = NULL; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2739 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2740 | first_param = req->buf->p + txn->req.eoh + 2; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2741 | end_params = first_param + txn->req.body_len; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2742 | |
| 2743 | cur_param = next_param = end_params; |
| 2744 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2745 | if (end_params >= req->buf->data + req->buf->size - global.tune.maxrewrite) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2746 | /* Prevent buffer overflow */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2747 | si->applet.ctx.stats.st_code = STAT_STATUS_EXCD; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2748 | return 1; |
| 2749 | } |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2750 | else if (end_params > req->buf->p + req->buf->i) { |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2751 | /* we need more data */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2752 | si->applet.ctx.stats.st_code = STAT_STATUS_NONE; |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2753 | return 0; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2754 | } |
| 2755 | |
| 2756 | *end_params = '\0'; |
| 2757 | |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2758 | si->applet.ctx.stats.st_code = STAT_STATUS_NONE; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2759 | |
| 2760 | /* |
| 2761 | * Parse the parameters in reverse order to only store the last value. |
| 2762 | * From the html form, the backend and the action are at the end. |
| 2763 | */ |
| 2764 | while (cur_param > first_param) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2765 | char *value; |
| 2766 | int poffset, plen; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2767 | |
| 2768 | cur_param--; |
| 2769 | if ((*cur_param == '&') || (cur_param == first_param)) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2770 | reprocess_servers: |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2771 | /* Parse the key */ |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2772 | poffset = (cur_param != first_param ? 1 : 0); |
| 2773 | plen = next_param - cur_param + (cur_param == first_param ? 1 : 0); |
| 2774 | if ((plen > 0) && (plen <= sizeof(key))) { |
| 2775 | strncpy(key, cur_param + poffset, plen); |
| 2776 | key[plen - 1] = '\0'; |
| 2777 | } else { |
| 2778 | si->applet.ctx.stats.st_code = STAT_STATUS_EXCD; |
| 2779 | goto out; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2780 | } |
| 2781 | |
| 2782 | /* Parse the value */ |
| 2783 | value = key; |
| 2784 | while (*value != '\0' && *value != '=') { |
| 2785 | value++; |
| 2786 | } |
| 2787 | if (*value == '=') { |
| 2788 | /* Ok, a value is found, we can mark the end of the key */ |
| 2789 | *value++ = '\0'; |
| 2790 | } |
| 2791 | |
Willy Tarreau | bf9c2fc | 2011-05-31 18:06:18 +0200 | [diff] [blame] | 2792 | if (!url_decode(key) || !url_decode(value)) |
| 2793 | break; |
| 2794 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2795 | /* Now we can check the key to see what to do */ |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2796 | if (!px && (strcmp(key, "b") == 0)) { |
| 2797 | if ((px = findproxy(value, PR_CAP_BE)) == NULL) { |
| 2798 | /* the backend name is unknown or ambiguous (duplicate names) */ |
| 2799 | si->applet.ctx.stats.st_code = STAT_STATUS_ERRP; |
| 2800 | goto out; |
| 2801 | } |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2802 | } |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2803 | else if (!action && (strcmp(key, "action") == 0)) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2804 | if (strcmp(value, "disable") == 0) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2805 | action = ST_ADM_ACTION_DISABLE; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2806 | } |
| 2807 | else if (strcmp(value, "enable") == 0) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2808 | action = ST_ADM_ACTION_ENABLE; |
| 2809 | } |
Willy Tarreau | d728224 | 2012-06-04 00:22:44 +0200 | [diff] [blame] | 2810 | else if (strcmp(value, "stop") == 0) { |
| 2811 | action = ST_ADM_ACTION_STOP; |
| 2812 | } |
| 2813 | else if (strcmp(value, "start") == 0) { |
| 2814 | action = ST_ADM_ACTION_START; |
| 2815 | } |
Willy Tarreau | 4f8a83c | 2012-06-04 00:26:23 +0200 | [diff] [blame] | 2816 | else if (strcmp(value, "shutdown") == 0) { |
| 2817 | action = ST_ADM_ACTION_SHUTDOWN; |
| 2818 | } |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2819 | else { |
| 2820 | si->applet.ctx.stats.st_code = STAT_STATUS_ERRP; |
| 2821 | goto out; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2822 | } |
| 2823 | } |
| 2824 | else if (strcmp(key, "s") == 0) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2825 | if (!(px && action)) { |
| 2826 | /* |
| 2827 | * Indicates that we'll need to reprocess the parameters |
| 2828 | * as soon as backend and action are known |
| 2829 | */ |
| 2830 | if (!reprocess) { |
| 2831 | st_cur_param = cur_param; |
| 2832 | st_next_param = next_param; |
| 2833 | } |
| 2834 | reprocess = 1; |
| 2835 | } |
| 2836 | else if ((sv = findserver(px, value)) != NULL) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2837 | switch (action) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2838 | case ST_ADM_ACTION_DISABLE: |
Cyril Bonté | 1e2a170 | 2011-03-03 21:05:17 +0100 | [diff] [blame] | 2839 | if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2840 | /* Not already in maintenance, we can change the server state */ |
| 2841 | sv->state |= SRV_MAINTAIN; |
| 2842 | set_server_down(sv); |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2843 | altered_servers++; |
| 2844 | total_servers++; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2845 | } |
| 2846 | break; |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2847 | case ST_ADM_ACTION_ENABLE: |
Cyril Bonté | 1e2a170 | 2011-03-03 21:05:17 +0100 | [diff] [blame] | 2848 | if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2849 | /* Already in maintenance, we can change the server state */ |
| 2850 | set_server_up(sv); |
Willy Tarreau | 7046130 | 2010-10-22 14:39:02 +0200 | [diff] [blame] | 2851 | sv->health = sv->rise; /* up, but will fall down at first failure */ |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2852 | altered_servers++; |
| 2853 | total_servers++; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2854 | } |
Willy Tarreau | d728224 | 2012-06-04 00:22:44 +0200 | [diff] [blame] | 2855 | break; |
| 2856 | case ST_ADM_ACTION_STOP: |
| 2857 | case ST_ADM_ACTION_START: |
| 2858 | if (action == ST_ADM_ACTION_START) |
| 2859 | sv->uweight = sv->iweight; |
| 2860 | else |
| 2861 | sv->uweight = 0; |
| 2862 | |
| 2863 | if (px->lbprm.algo & BE_LB_PROP_DYN) { |
| 2864 | /* we must take care of not pushing the server to full throttle during slow starts */ |
| 2865 | if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN)) |
| 2866 | sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart; |
| 2867 | else |
| 2868 | sv->eweight = BE_WEIGHT_SCALE; |
| 2869 | sv->eweight *= sv->uweight; |
| 2870 | } else { |
| 2871 | sv->eweight = sv->uweight; |
| 2872 | } |
| 2873 | |
| 2874 | /* static LB algorithms are a bit harder to update */ |
| 2875 | if (px->lbprm.update_server_eweight) |
| 2876 | px->lbprm.update_server_eweight(sv); |
| 2877 | else if (sv->eweight) { |
| 2878 | if (px->lbprm.set_server_status_up) |
| 2879 | px->lbprm.set_server_status_up(sv); |
| 2880 | } |
| 2881 | else { |
| 2882 | if (px->lbprm.set_server_status_down) |
| 2883 | px->lbprm.set_server_status_down(sv); |
| 2884 | } |
| 2885 | altered_servers++; |
| 2886 | total_servers++; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2887 | break; |
Willy Tarreau | 4f8a83c | 2012-06-04 00:26:23 +0200 | [diff] [blame] | 2888 | case ST_ADM_ACTION_SHUTDOWN: |
| 2889 | if (px->state != PR_STSTOPPED) { |
| 2890 | struct session *sess, *sess_bck; |
| 2891 | |
| 2892 | list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv) |
| 2893 | if (sess->srv_conn == sv) |
| 2894 | session_shutdown(sess, SN_ERR_KILLED); |
| 2895 | |
| 2896 | altered_servers++; |
| 2897 | total_servers++; |
| 2898 | } |
| 2899 | break; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2900 | } |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2901 | } else { |
| 2902 | /* the server name is unknown or ambiguous (duplicate names) */ |
| 2903 | total_servers++; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2904 | } |
| 2905 | } |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2906 | if (reprocess && px && action) { |
| 2907 | /* Now, we know the backend and the action chosen by the user. |
| 2908 | * We can safely restart from the first server parameter |
| 2909 | * to reprocess them |
| 2910 | */ |
| 2911 | cur_param = st_cur_param; |
| 2912 | next_param = st_next_param; |
| 2913 | reprocess = 0; |
| 2914 | goto reprocess_servers; |
| 2915 | } |
| 2916 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2917 | next_param = cur_param; |
| 2918 | } |
| 2919 | } |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2920 | |
| 2921 | if (total_servers == 0) { |
| 2922 | si->applet.ctx.stats.st_code = STAT_STATUS_NONE; |
| 2923 | } |
| 2924 | else if (altered_servers == 0) { |
| 2925 | si->applet.ctx.stats.st_code = STAT_STATUS_ERRP; |
| 2926 | } |
| 2927 | else if (altered_servers == total_servers) { |
| 2928 | si->applet.ctx.stats.st_code = STAT_STATUS_DONE; |
| 2929 | } |
| 2930 | else { |
| 2931 | si->applet.ctx.stats.st_code = STAT_STATUS_PART; |
| 2932 | } |
| 2933 | out: |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2934 | return 1; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2935 | } |
| 2936 | |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2937 | /* This function checks whether we need to enable a POST analyser to parse a |
| 2938 | * stats request, and also registers the stats I/O handler. It returns zero |
Willy Tarreau | cbc743e | 2012-12-28 08:36:50 +0100 | [diff] [blame] | 2939 | * if it needs to come back again, otherwise non-zero if it finishes. In the |
| 2940 | * latter case, it also clears the request analysers. |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2941 | */ |
| 2942 | int http_handle_stats(struct session *s, struct channel *req) |
| 2943 | { |
| 2944 | struct stats_admin_rule *stats_admin_rule; |
| 2945 | struct stream_interface *si = s->rep->prod; |
| 2946 | struct http_txn *txn = &s->txn; |
| 2947 | struct http_msg *msg = &txn->req; |
| 2948 | struct uri_auth *uri = s->be->uri_auth; |
| 2949 | |
| 2950 | /* now check whether we have some admin rules for this request */ |
| 2951 | list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) { |
| 2952 | int ret = 1; |
| 2953 | |
| 2954 | if (stats_admin_rule->cond) { |
| 2955 | ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
| 2956 | ret = acl_pass(ret); |
| 2957 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 2958 | ret = !ret; |
| 2959 | } |
| 2960 | |
| 2961 | if (ret) { |
| 2962 | /* no rule, or the rule matches */ |
| 2963 | s->rep->prod->applet.ctx.stats.flags |= STAT_ADMIN; |
| 2964 | break; |
| 2965 | } |
| 2966 | } |
| 2967 | |
| 2968 | /* Was the status page requested with a POST ? */ |
| 2969 | if (unlikely(txn->meth == HTTP_METH_POST)) { |
| 2970 | if (si->applet.ctx.stats.flags & STAT_ADMIN) { |
| 2971 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 2972 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 2973 | * send an HTTP/1.1 100 Continue intermediate response. |
| 2974 | */ |
| 2975 | if (msg->flags & HTTP_MSGF_VER_11) { |
| 2976 | struct hdr_ctx ctx; |
| 2977 | ctx.idx = 0; |
| 2978 | /* Expect is allowed in 1.1, look for it */ |
| 2979 | if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) && |
| 2980 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) { |
| 2981 | bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len); |
| 2982 | } |
| 2983 | } |
| 2984 | msg->msg_state = HTTP_MSG_100_SENT; |
| 2985 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 2986 | } |
| 2987 | if (!http_process_req_stat_post(si, txn, req)) |
| 2988 | return 0; /* we need more data */ |
| 2989 | } |
| 2990 | else |
| 2991 | si->applet.ctx.stats.st_code = STAT_STATUS_DENY; |
| 2992 | |
| 2993 | /* We don't want to land on the posted stats page because a refresh will |
| 2994 | * repost the data. We don't want this to happen on accident so we redirect |
| 2995 | * the browse to the stats page with a GET. |
| 2996 | */ |
| 2997 | chunk_printf(&trash, |
Yves Lafon | 4e8ec50 | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 2998 | "HTTP/1.1 303 See Other\r\n" |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2999 | "Cache-Control: no-cache\r\n" |
| 3000 | "Content-Type: text/plain\r\n" |
| 3001 | "Connection: close\r\n" |
| 3002 | "Location: %s;st=%s\r\n" |
| 3003 | "\r\n", |
| 3004 | uri->uri_prefix, |
| 3005 | ((si->applet.ctx.stats.st_code > STAT_STATUS_INIT) && |
| 3006 | (si->applet.ctx.stats.st_code < STAT_STATUS_SIZE) && |
| 3007 | stat_status_codes[si->applet.ctx.stats.st_code]) ? |
| 3008 | stat_status_codes[si->applet.ctx.stats.st_code] : |
| 3009 | stat_status_codes[STAT_STATUS_UNKN]); |
| 3010 | |
| 3011 | s->txn.status = 303; |
| 3012 | s->logs.tv_request = now; |
| 3013 | stream_int_retnclose(req->prod, &trash); |
| 3014 | s->target = &http_stats_applet.obj_type; /* just for logging the applet name */ |
| 3015 | |
| 3016 | if (s->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 3017 | s->fe->fe_counters.intercepted_req++; |
| 3018 | |
| 3019 | if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is |
| 3020 | s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy |
| 3021 | if (!(s->flags & SN_FINST_MASK)) |
| 3022 | s->flags |= SN_FINST_R; |
Willy Tarreau | cbc743e | 2012-12-28 08:36:50 +0100 | [diff] [blame] | 3023 | req->analysers = 0; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3024 | return 1; |
| 3025 | } |
| 3026 | |
| 3027 | /* OK, let's go on now */ |
| 3028 | |
| 3029 | chunk_printf(&trash, |
| 3030 | "HTTP/1.0 200 OK\r\n" |
| 3031 | "Cache-Control: no-cache\r\n" |
| 3032 | "Connection: close\r\n" |
| 3033 | "Content-Type: %s\r\n", |
Willy Tarreau | 354898b | 2012-12-23 18:15:23 +0100 | [diff] [blame] | 3034 | (si->applet.ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain"); |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3035 | |
| 3036 | if (uri->refresh > 0 && !(si->applet.ctx.stats.flags & STAT_NO_REFRESH)) |
| 3037 | chunk_appendf(&trash, "Refresh: %d\r\n", |
| 3038 | uri->refresh); |
| 3039 | |
| 3040 | chunk_appendf(&trash, "\r\n"); |
| 3041 | |
| 3042 | s->txn.status = 200; |
| 3043 | s->logs.tv_request = now; |
| 3044 | |
| 3045 | if (s->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 3046 | s->fe->fe_counters.intercepted_req++; |
| 3047 | |
| 3048 | if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is |
| 3049 | s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy |
| 3050 | if (!(s->flags & SN_FINST_MASK)) |
| 3051 | s->flags |= SN_FINST_R; |
| 3052 | |
| 3053 | if (s->txn.meth == HTTP_METH_HEAD) { |
| 3054 | /* that's all we return in case of HEAD request, so let's immediately close. */ |
| 3055 | stream_int_retnclose(req->prod, &trash); |
| 3056 | s->target = &http_stats_applet.obj_type; /* just for logging the applet name */ |
Willy Tarreau | cbc743e | 2012-12-28 08:36:50 +0100 | [diff] [blame] | 3057 | req->analysers = 0; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3058 | return 1; |
| 3059 | } |
| 3060 | |
| 3061 | /* OK, push the response and hand over to the stats I/O handler */ |
| 3062 | bi_putchk(s->rep, &trash); |
| 3063 | |
| 3064 | s->task->nice = -32; /* small boost for HTTP statistics */ |
| 3065 | stream_int_register_handler(s->rep->prod, &http_stats_applet); |
| 3066 | s->target = s->rep->prod->conn->target; // for logging only |
| 3067 | s->rep->prod->conn->xprt_ctx = s; |
| 3068 | s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0; |
| 3069 | req->analysers = 0; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3070 | return 1; |
| 3071 | } |
| 3072 | |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3073 | /* Executes the http-request rules <rules> for session <s>, proxy <px> and |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3074 | * transaction <txn>. Returns the first rule that prevents further processing |
| 3075 | * of the request (auth, deny, ...) or NULL if it executed all rules or stopped |
| 3076 | * on an allow. It may set the TX_CLDENY on txn->flags if it encounters a deny |
| 3077 | * rule. |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3078 | */ |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3079 | static struct http_req_rule * |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3080 | 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] | 3081 | { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3082 | struct http_req_rule *rule; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3083 | struct hdr_ctx ctx; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3084 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3085 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3086 | if (rule->action >= HTTP_REQ_ACT_MAX) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3087 | continue; |
| 3088 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3089 | /* check optional condition */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3090 | if (rule->cond) { |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3091 | int ret; |
| 3092 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 3093 | 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] | 3094 | ret = acl_pass(ret); |
| 3095 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3096 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3097 | ret = !ret; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3098 | |
| 3099 | if (!ret) /* condition not matched */ |
| 3100 | continue; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3101 | } |
| 3102 | |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3103 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3104 | switch (rule->action) { |
| 3105 | case HTTP_REQ_ACT_ALLOW: |
| 3106 | return NULL; /* "allow" rules are OK */ |
| 3107 | |
| 3108 | case HTTP_REQ_ACT_DENY: |
| 3109 | txn->flags |= TX_CLDENY; |
| 3110 | return rule; |
| 3111 | |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 3112 | case HTTP_REQ_ACT_TARPIT: |
| 3113 | txn->flags |= TX_CLTARPIT; |
| 3114 | return rule; |
| 3115 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3116 | case HTTP_REQ_ACT_AUTH: |
| 3117 | return rule; |
| 3118 | |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 3119 | case HTTP_REQ_ACT_REDIR: |
| 3120 | return rule; |
| 3121 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3122 | case HTTP_REQ_ACT_SET_HDR: |
| 3123 | ctx.idx = 0; |
| 3124 | /* remove all occurrences of the header */ |
| 3125 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
| 3126 | txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) { |
| 3127 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3128 | } |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3129 | /* now fall through to header addition */ |
| 3130 | |
| 3131 | case HTTP_REQ_ACT_ADD_HDR: |
| 3132 | chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name); |
| 3133 | memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len); |
| 3134 | trash.len = rule->arg.hdr_add.name_len; |
| 3135 | trash.str[trash.len++] = ':'; |
| 3136 | trash.str[trash.len++] = ' '; |
| 3137 | trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt); |
| 3138 | http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len); |
| 3139 | break; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3140 | } |
| 3141 | } |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3142 | |
| 3143 | /* we reached the end of the rules, nothing to report */ |
Willy Tarreau | 418c1a0 | 2012-12-25 20:52:58 +0100 | [diff] [blame] | 3144 | return NULL; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3145 | } |
| 3146 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3147 | |
| 3148 | /* Perform an HTTP redirect based on the information in <rule>. The function |
| 3149 | * returns non-zero on success, or zero in case of a, irrecoverable error such |
| 3150 | * as too large a request to build a valid response. |
| 3151 | */ |
| 3152 | static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn) |
| 3153 | { |
| 3154 | struct http_msg *msg = &txn->req; |
| 3155 | const char *msg_fmt; |
| 3156 | |
| 3157 | /* build redirect message */ |
| 3158 | switch(rule->code) { |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 3159 | case 308: |
| 3160 | msg_fmt = HTTP_308; |
| 3161 | break; |
| 3162 | case 307: |
| 3163 | msg_fmt = HTTP_307; |
| 3164 | break; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3165 | case 303: |
| 3166 | msg_fmt = HTTP_303; |
| 3167 | break; |
| 3168 | case 301: |
| 3169 | msg_fmt = HTTP_301; |
| 3170 | break; |
| 3171 | case 302: |
| 3172 | default: |
| 3173 | msg_fmt = HTTP_302; |
| 3174 | break; |
| 3175 | } |
| 3176 | |
| 3177 | if (unlikely(!chunk_strcpy(&trash, msg_fmt))) |
| 3178 | return 0; |
| 3179 | |
| 3180 | switch(rule->type) { |
| 3181 | case REDIRECT_TYPE_SCHEME: { |
| 3182 | const char *path; |
| 3183 | const char *host; |
| 3184 | struct hdr_ctx ctx; |
| 3185 | int pathlen; |
| 3186 | int hostlen; |
| 3187 | |
| 3188 | host = ""; |
| 3189 | hostlen = 0; |
| 3190 | ctx.idx = 0; |
| 3191 | if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) { |
| 3192 | host = ctx.line + ctx.val; |
| 3193 | hostlen = ctx.vlen; |
| 3194 | } |
| 3195 | |
| 3196 | path = http_get_path(txn); |
| 3197 | /* build message using path */ |
| 3198 | if (path) { |
| 3199 | pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path; |
| 3200 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3201 | int qs = 0; |
| 3202 | while (qs < pathlen) { |
| 3203 | if (path[qs] == '?') { |
| 3204 | pathlen = qs; |
| 3205 | break; |
| 3206 | } |
| 3207 | qs++; |
| 3208 | } |
| 3209 | } |
| 3210 | } else { |
| 3211 | path = "/"; |
| 3212 | pathlen = 1; |
| 3213 | } |
| 3214 | |
| 3215 | /* check if we can add scheme + "://" + host + path */ |
| 3216 | if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4) |
| 3217 | return 0; |
| 3218 | |
| 3219 | /* add scheme */ |
| 3220 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 3221 | trash.len += rule->rdr_len; |
| 3222 | |
| 3223 | /* add "://" */ |
| 3224 | memcpy(trash.str + trash.len, "://", 3); |
| 3225 | trash.len += 3; |
| 3226 | |
| 3227 | /* add host */ |
| 3228 | memcpy(trash.str + trash.len, host, hostlen); |
| 3229 | trash.len += hostlen; |
| 3230 | |
| 3231 | /* add path */ |
| 3232 | memcpy(trash.str + trash.len, path, pathlen); |
| 3233 | trash.len += pathlen; |
| 3234 | |
| 3235 | /* append a slash at the end of the location is needed and missing */ |
| 3236 | if (trash.len && trash.str[trash.len - 1] != '/' && |
| 3237 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 3238 | if (trash.len > trash.size - 5) |
| 3239 | return 0; |
| 3240 | trash.str[trash.len] = '/'; |
| 3241 | trash.len++; |
| 3242 | } |
| 3243 | |
| 3244 | break; |
| 3245 | } |
| 3246 | case REDIRECT_TYPE_PREFIX: { |
| 3247 | const char *path; |
| 3248 | int pathlen; |
| 3249 | |
| 3250 | path = http_get_path(txn); |
| 3251 | /* build message using path */ |
| 3252 | if (path) { |
| 3253 | pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path; |
| 3254 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3255 | int qs = 0; |
| 3256 | while (qs < pathlen) { |
| 3257 | if (path[qs] == '?') { |
| 3258 | pathlen = qs; |
| 3259 | break; |
| 3260 | } |
| 3261 | qs++; |
| 3262 | } |
| 3263 | } |
| 3264 | } else { |
| 3265 | path = "/"; |
| 3266 | pathlen = 1; |
| 3267 | } |
| 3268 | |
| 3269 | if (trash.len + rule->rdr_len + pathlen > trash.size - 4) |
| 3270 | return 0; |
| 3271 | |
| 3272 | /* add prefix. Note that if prefix == "/", we don't want to |
| 3273 | * add anything, otherwise it makes it hard for the user to |
| 3274 | * configure a self-redirection. |
| 3275 | */ |
| 3276 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
| 3277 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 3278 | trash.len += rule->rdr_len; |
| 3279 | } |
| 3280 | |
| 3281 | /* add path */ |
| 3282 | memcpy(trash.str + trash.len, path, pathlen); |
| 3283 | trash.len += pathlen; |
| 3284 | |
| 3285 | /* append a slash at the end of the location is needed and missing */ |
| 3286 | if (trash.len && trash.str[trash.len - 1] != '/' && |
| 3287 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 3288 | if (trash.len > trash.size - 5) |
| 3289 | return 0; |
| 3290 | trash.str[trash.len] = '/'; |
| 3291 | trash.len++; |
| 3292 | } |
| 3293 | |
| 3294 | break; |
| 3295 | } |
| 3296 | case REDIRECT_TYPE_LOCATION: |
| 3297 | default: |
| 3298 | if (trash.len + rule->rdr_len > trash.size - 4) |
| 3299 | return 0; |
| 3300 | |
| 3301 | /* add location */ |
| 3302 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 3303 | trash.len += rule->rdr_len; |
| 3304 | break; |
| 3305 | } |
| 3306 | |
| 3307 | if (rule->cookie_len) { |
| 3308 | memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14); |
| 3309 | trash.len += 14; |
| 3310 | memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len); |
| 3311 | trash.len += rule->cookie_len; |
| 3312 | memcpy(trash.str + trash.len, "\r\n", 2); |
| 3313 | trash.len += 2; |
| 3314 | } |
| 3315 | |
| 3316 | /* add end of headers and the keep-alive/close status. |
| 3317 | * We may choose to set keep-alive if the Location begins |
| 3318 | * with a slash, because the client will come back to the |
| 3319 | * same server. |
| 3320 | */ |
| 3321 | txn->status = rule->code; |
| 3322 | /* let's log the request time */ |
| 3323 | s->logs.tv_request = now; |
| 3324 | |
| 3325 | if (rule->rdr_len >= 1 && *rule->rdr_str == '/' && |
| 3326 | (msg->flags & HTTP_MSGF_XFER_LEN) && |
| 3327 | !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len && |
| 3328 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 3329 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 3330 | /* keep-alive possible */ |
| 3331 | if (!(msg->flags & HTTP_MSGF_VER_11)) { |
| 3332 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 3333 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30); |
| 3334 | trash.len += 30; |
| 3335 | } else { |
| 3336 | memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24); |
| 3337 | trash.len += 24; |
| 3338 | } |
| 3339 | } |
| 3340 | memcpy(trash.str + trash.len, "\r\n\r\n", 4); |
| 3341 | trash.len += 4; |
| 3342 | bo_inject(txn->rsp.chn, trash.str, trash.len); |
| 3343 | /* "eat" the request */ |
| 3344 | bi_fast_delete(txn->req.chn->buf, msg->sov); |
| 3345 | msg->sov = 0; |
| 3346 | txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY; |
| 3347 | s->rep->analysers = AN_RES_HTTP_XFER_BODY; |
| 3348 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3349 | txn->rsp.msg_state = HTTP_MSG_DONE; |
| 3350 | } else { |
| 3351 | /* keep-alive not possible */ |
| 3352 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 3353 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 3354 | trash.len += 29; |
| 3355 | } else { |
| 3356 | memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23); |
| 3357 | trash.len += 23; |
| 3358 | } |
| 3359 | stream_int_retnclose(txn->req.chn->prod, &trash); |
| 3360 | txn->req.chn->analysers = 0; |
| 3361 | } |
| 3362 | |
| 3363 | if (!(s->flags & SN_ERR_MASK)) |
| 3364 | s->flags |= SN_ERR_PRXCOND; |
| 3365 | if (!(s->flags & SN_FINST_MASK)) |
| 3366 | s->flags |= SN_FINST_R; |
| 3367 | |
| 3368 | return 1; |
| 3369 | } |
| 3370 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3371 | /* This stream analyser runs all HTTP request processing which is common to |
| 3372 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 3373 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3374 | * 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] | 3375 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 3376 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3377 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 3378 | 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] | 3379 | { |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3380 | struct http_txn *txn = &s->txn; |
| 3381 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3382 | struct acl_cond *cond; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3383 | struct http_req_rule *http_req_last_rule = NULL; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3384 | struct redirect_rule *rule; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 3385 | struct cond_wordlist *wl; |
Simon Horman | 70735c9 | 2011-06-07 11:07:50 +0900 | [diff] [blame] | 3386 | int do_stats; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3387 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3388 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3389 | /* we need more data */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3390 | channel_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3391 | return 0; |
| 3392 | } |
| 3393 | |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3394 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3395 | req->analyse_exp = TICK_ETERNITY; |
| 3396 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 3397 | 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] | 3398 | now_ms, __FUNCTION__, |
| 3399 | s, |
| 3400 | req, |
| 3401 | req->rex, req->wex, |
| 3402 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3403 | req->buf->i, |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3404 | req->analysers); |
| 3405 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3406 | /* first check whether we have some ACLs set to block this request */ |
| 3407 | list_for_each_entry(cond, &px->block_cond, list) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 3408 | 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] | 3409 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3410 | ret = acl_pass(ret); |
| 3411 | if (cond->pol == ACL_COND_UNLESS) |
| 3412 | ret = !ret; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 3413 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3414 | if (ret) { |
| 3415 | txn->status = 403; |
| 3416 | /* let's log the request time */ |
| 3417 | s->logs.tv_request = now; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 3418 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403)); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3419 | session_inc_http_err_ctr(s); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3420 | goto return_prx_cond; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3421 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3422 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3423 | |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 3424 | /* just in case we have some per-backend tracking */ |
| 3425 | session_inc_be_http_req_ctr(s); |
| 3426 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3427 | /* evaluate http-request rules */ |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3428 | 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] | 3429 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3430 | /* evaluate stats http-request rules only if http-request is OK */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3431 | if (!http_req_last_rule) { |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3432 | do_stats = stats_check_uri(s->rep->prod, txn, px); |
| 3433 | if (do_stats) |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 3434 | http_req_last_rule = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, txn); |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3435 | } |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3436 | else |
| 3437 | do_stats = 0; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3438 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3439 | /* return a 403 if either rule has blocked */ |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 3440 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) { |
| 3441 | if (txn->flags & TX_CLDENY) { |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3442 | txn->status = 403; |
| 3443 | s->logs.tv_request = now; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 3444 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403)); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3445 | session_inc_http_err_ctr(s); |
Willy Tarreau | 6da0f6d | 2011-01-06 18:19:50 +0100 | [diff] [blame] | 3446 | s->fe->fe_counters.denied_req++; |
| 3447 | if (an_bit == AN_REQ_HTTP_PROCESS_BE) |
| 3448 | s->be->be_counters.denied_req++; |
| 3449 | if (s->listener->counters) |
| 3450 | s->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3451 | goto return_prx_cond; |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 3452 | } |
| 3453 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 3454 | * which may be the same as the connect timeout if unspecified. |
| 3455 | * If unset, then set it to zero because we really want it to |
| 3456 | * eventually expire. We build the tarpit as an analyser. |
| 3457 | */ |
| 3458 | if (txn->flags & TX_CLTARPIT) { |
| 3459 | channel_erase(s->req); |
| 3460 | /* wipe the request out so that we can drop the connection early |
| 3461 | * if the client closes first. |
| 3462 | */ |
| 3463 | channel_dont_connect(req); |
| 3464 | req->analysers = 0; /* remove switching rules etc... */ |
| 3465 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 3466 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 3467 | if (!req->analyse_exp) |
| 3468 | req->analyse_exp = tick_add(now_ms, 0); |
| 3469 | session_inc_http_err_ctr(s); |
| 3470 | s->fe->fe_counters.denied_req++; |
| 3471 | if (s->fe != s->be) |
| 3472 | s->be->be_counters.denied_req++; |
| 3473 | if (s->listener->counters) |
| 3474 | s->listener->counters->denied_req++; |
| 3475 | return 1; |
| 3476 | } |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3477 | } |
| 3478 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3479 | /* try headers filters */ |
| 3480 | if (px->req_exp != NULL) { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 3481 | if (apply_filters_to_request(s, req, px) < 0) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3482 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3483 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3484 | /* has the request been denied ? */ |
| 3485 | if (txn->flags & TX_CLDENY) { |
| 3486 | /* no need to go further */ |
| 3487 | txn->status = 403; |
| 3488 | /* let's log the request time */ |
| 3489 | s->logs.tv_request = now; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 3490 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403)); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3491 | session_inc_http_err_ctr(s); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3492 | goto return_prx_cond; |
| 3493 | } |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 3494 | |
| 3495 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 3496 | * which may be the same as the connect timeout if unspecified. |
| 3497 | * If unset, then set it to zero because we really want it to |
| 3498 | * eventually expire. We build the tarpit as an analyser. |
| 3499 | */ |
| 3500 | if (txn->flags & TX_CLTARPIT) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3501 | channel_erase(s->req); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 3502 | /* wipe the request out so that we can drop the connection early |
| 3503 | * if the client closes first. |
| 3504 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3505 | channel_dont_connect(req); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 3506 | req->analysers = 0; /* remove switching rules etc... */ |
| 3507 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 3508 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 3509 | if (!req->analyse_exp) |
| 3510 | req->analyse_exp = tick_add(now_ms, 0); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3511 | session_inc_http_err_ctr(s); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 3512 | return 1; |
| 3513 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3514 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3515 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3516 | /* Until set to anything else, the connection mode is set as TUNNEL. It will |
| 3517 | * only change if both the request and the config reference something else. |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 3518 | * Option httpclose by itself does not set a mode, it remains a tunnel mode |
| 3519 | * in which headers are mangled. However, if another mode is set, it will |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3520 | * affect it (eg: server-close/keep-alive + httpclose = close). Note that we |
| 3521 | * avoid to redo the same work if FE and BE have the same settings (common). |
| 3522 | * The method consists in checking if options changed between the two calls |
| 3523 | * (implying that either one is non-null, or one of them is non-null and we |
| 3524 | * are there for the first time. |
Willy Tarreau | 4273664 | 2009-10-18 21:04:35 +0200 | [diff] [blame] | 3525 | */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3526 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 3527 | if ((!(txn->flags & TX_HDR_CONN_PRS) && |
| 3528 | (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) || |
| 3529 | ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) != |
| 3530 | (s->be->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)))) { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3531 | int tmp = TX_CON_WANT_TUN; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3532 | |
Cyril Bonté | 9ea2b9a | 2010-12-29 09:36:56 +0100 | [diff] [blame] | 3533 | if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE || |
| 3534 | ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3535 | tmp = TX_CON_WANT_KAL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 3536 | if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO) |
| 3537 | tmp = TX_CON_WANT_SCL; |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 3538 | if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3539 | tmp = TX_CON_WANT_CLO; |
| 3540 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3541 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
| 3542 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 3543 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3544 | if (!(txn->flags & TX_HDR_CONN_PRS)) { |
| 3545 | /* parse the Connection header and possibly clean it */ |
| 3546 | int to_del = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3547 | if ((msg->flags & HTTP_MSGF_VER_11) || |
Willy Tarreau | 8a8e1d9 | 2010-04-05 16:15:16 +0200 | [diff] [blame] | 3548 | ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL && |
| 3549 | !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3550 | to_del |= 2; /* remove "keep-alive" */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3551 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3552 | to_del |= 1; /* remove "close" */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3553 | http_parse_connection_header(txn, msg, to_del); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 3554 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3555 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3556 | /* check if client or config asks for explicit close in KAL/SCL */ |
| 3557 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 3558 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) && |
| 3559 | ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3560 | (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */ |
Cyril Bonté | 9ea2b9a | 2010-12-29 09:36:56 +0100 | [diff] [blame] | 3561 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3562 | !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */ |
Willy Tarreau | c3e8b25 | 2010-01-28 15:01:20 +0100 | [diff] [blame] | 3563 | s->fe->state == PR_STSTOPPED)) /* frontend is stopping */ |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3564 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 3565 | } |
Willy Tarreau | 7859991 | 2009-10-17 20:12:21 +0200 | [diff] [blame] | 3566 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3567 | /* we can be blocked here because the request needs to be authenticated, |
| 3568 | * either to pass or to access stats. |
| 3569 | */ |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 3570 | 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] | 3571 | char *realm = http_req_last_rule->arg.auth.realm; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3572 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3573 | if (!realm) |
| 3574 | realm = do_stats?STATS_DEFAULT_REALM:px->id; |
| 3575 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3576 | 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] | 3577 | txn->status = 401; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3578 | stream_int_retnclose(req->prod, &trash); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3579 | /* on 401 we still count one error, because normal browsing |
| 3580 | * won't significantly increase the counter but brute force |
| 3581 | * attempts will. |
| 3582 | */ |
| 3583 | session_inc_http_err_ctr(s); |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3584 | goto return_prx_cond; |
| 3585 | } |
| 3586 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3587 | /* add request headers from the rule sets in the same order */ |
| 3588 | list_for_each_entry(wl, &px->req_add, list) { |
| 3589 | if (wl->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 3590 | 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] | 3591 | ret = acl_pass(ret); |
| 3592 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 3593 | ret = !ret; |
| 3594 | if (!ret) |
| 3595 | continue; |
| 3596 | } |
| 3597 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3598 | 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] | 3599 | goto return_bad_req; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 3600 | } |
| 3601 | |
| 3602 | if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_REDIR) { |
| 3603 | if (!http_apply_redirect_rule(http_req_last_rule->arg.redir, s, txn)) |
| 3604 | goto return_bad_req; |
| 3605 | req->analyse_exp = TICK_ETERNITY; |
| 3606 | return 1; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3607 | } |
| 3608 | |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3609 | if (unlikely(do_stats)) { |
| 3610 | /* process the stats request now */ |
| 3611 | if (!http_handle_stats(s, req)) { |
| 3612 | /* we need more data, let's come back here later */ |
| 3613 | req->analysers |= an_bit; |
| 3614 | channel_dont_connect(req); |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 3615 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 3616 | return 1; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3617 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 3618 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3619 | /* check whether we have some ACLs set to redirect this request */ |
| 3620 | list_for_each_entry(rule, &px->redirect_rules, list) { |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3621 | if (rule->cond) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3622 | int ret; |
| 3623 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 3624 | 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] | 3625 | ret = acl_pass(ret); |
| 3626 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 3627 | ret = !ret; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3628 | if (!ret) |
| 3629 | continue; |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3630 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3631 | if (!http_apply_redirect_rule(rule, s, txn)) |
| 3632 | goto return_bad_req; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3633 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3634 | req->analyse_exp = TICK_ETERNITY; |
| 3635 | return 1; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3636 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3637 | |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 3638 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 3639 | * If this happens, then the data will not come immediately, so we must |
| 3640 | * send all what we have without waiting. Note that due to the small gain |
| 3641 | * 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] | 3642 | * 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] | 3643 | * itself once used. |
| 3644 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 3645 | req->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 3646 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3647 | /* that's OK for us now, let's move on to next analysers */ |
| 3648 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 3649 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3650 | return_bad_req: |
| 3651 | /* We centralize bad requests processing here */ |
| 3652 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 3653 | /* we detected a parsing error. We want to archive this request |
| 3654 | * in the dedicated proxy area for later troubleshooting. |
| 3655 | */ |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 3656 | 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] | 3657 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3658 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3659 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3660 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 3661 | 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] | 3662 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3663 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3664 | if (s->listener->counters) |
| 3665 | s->listener->counters->failed_req++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 3666 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3667 | return_prx_cond: |
| 3668 | if (!(s->flags & SN_ERR_MASK)) |
| 3669 | s->flags |= SN_ERR_PRXCOND; |
| 3670 | if (!(s->flags & SN_FINST_MASK)) |
| 3671 | s->flags |= SN_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 3672 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3673 | req->analysers = 0; |
| 3674 | req->analyse_exp = TICK_ETERNITY; |
| 3675 | return 0; |
| 3676 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3677 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3678 | /* This function performs all the processing enabled for the current request. |
| 3679 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 3680 | * needs more data, encounters an error, or wants to immediately abort the |
| 3681 | * request. It relies on buffers flags, and updates s->req->analysers. |
| 3682 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 3683 | 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] | 3684 | { |
| 3685 | struct http_txn *txn = &s->txn; |
| 3686 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3687 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3688 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3689 | /* we need more data */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3690 | channel_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3691 | return 0; |
| 3692 | } |
| 3693 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 3694 | 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] | 3695 | now_ms, __FUNCTION__, |
| 3696 | s, |
| 3697 | req, |
| 3698 | req->rex, req->wex, |
| 3699 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3700 | req->buf->i, |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3701 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3702 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 3703 | if (s->fe->comp || s->be->comp) |
| 3704 | select_compression_request_header(s, req->buf); |
| 3705 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3706 | /* |
| 3707 | * Right now, we know that we have processed the entire headers |
| 3708 | * and that unwanted requests have been filtered out. We can do |
| 3709 | * whatever we want with the remaining request. Also, now we |
| 3710 | * may have separate values for ->fe, ->be. |
| 3711 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3712 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3713 | /* |
| 3714 | * If HTTP PROXY is set we simply get remote server address |
| 3715 | * parsing incoming request. |
| 3716 | */ |
| 3717 | if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) { |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 3718 | url2sa(req->buf->p + msg->sl.rq.u, msg->sl.rq.u_l, &s->req->cons->conn->addr.to); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3719 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3720 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3721 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3722 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3723 | * Note that doing so might move headers in the request, but |
| 3724 | * the fields will stay coherent and the URI will not move. |
| 3725 | * This should only be performed in the backend. |
| 3726 | */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 3727 | 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] | 3728 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
| 3729 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 3730 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3731 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3732 | * 8: the appsession cookie was looked up very early in 1.2, |
| 3733 | * so let's do the same now. |
| 3734 | */ |
| 3735 | |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 3736 | /* It needs to look into the URI unless persistence must be ignored */ |
| 3737 | 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] | 3738 | 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] | 3739 | } |
| 3740 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3741 | /* add unique-id if "header-unique-id" is specified */ |
| 3742 | |
| 3743 | if (!LIST_ISEMPTY(&s->fe->format_unique_id)) |
| 3744 | build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id); |
| 3745 | |
| 3746 | if (s->fe->header_unique_id && s->unique_id) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3747 | chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id); |
| 3748 | if (trash.len < 0) |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3749 | goto return_bad_req; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3750 | 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] | 3751 | goto return_bad_req; |
| 3752 | } |
| 3753 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3754 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3755 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 3756 | * asks for it. |
| 3757 | */ |
| 3758 | if ((s->fe->options | s->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 3759 | struct hdr_ctx ctx = { .idx = 0 }; |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 3760 | if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) && |
Cyril Bonté | a32d275 | 2012-05-29 23:27:41 +0200 | [diff] [blame] | 3761 | http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name, |
| 3762 | 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] | 3763 | req->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 3764 | /* The header is set to be added only if none is present |
| 3765 | * and we found it, so don't do anything. |
| 3766 | */ |
| 3767 | } |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 3768 | else if (s->req->prod->conn->addr.from.ss_family == AF_INET) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3769 | /* Add an X-Forwarded-For header unless the source IP is |
| 3770 | * in the 'except' network range. |
| 3771 | */ |
| 3772 | if ((!s->fe->except_mask.s_addr || |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 3773 | (((struct sockaddr_in *)&s->req->prod->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] | 3774 | != s->fe->except_net.s_addr) && |
| 3775 | (!s->be->except_mask.s_addr || |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 3776 | (((struct sockaddr_in *)&s->req->prod->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] | 3777 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3778 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3779 | unsigned char *pn; |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 3780 | pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->conn->addr.from)->sin_addr; |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3781 | |
| 3782 | /* Note: we rely on the backend to get the header name to be used for |
| 3783 | * x-forwarded-for, because the header is really meant for the backends. |
| 3784 | * However, if the backend did not specify any option, we have to rely |
| 3785 | * on the frontend's header name. |
| 3786 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3787 | if (s->be->fwdfor_hdr_len) { |
| 3788 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3789 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3790 | } else { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3791 | len = s->fe->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3792 | memcpy(trash.str, s->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3793 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3794 | 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] | 3795 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3796 | 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] | 3797 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3798 | } |
| 3799 | } |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 3800 | else if (s->req->prod->conn->addr.from.ss_family == AF_INET6) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3801 | /* FIXME: for the sake of completeness, we should also support |
| 3802 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3803 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3804 | int len; |
| 3805 | char pn[INET6_ADDRSTRLEN]; |
| 3806 | inet_ntop(AF_INET6, |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 3807 | (const void *)&((struct sockaddr_in6 *)(&s->req->prod->conn->addr.from))->sin6_addr, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3808 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3809 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3810 | /* Note: we rely on the backend to get the header name to be used for |
| 3811 | * x-forwarded-for, because the header is really meant for the backends. |
| 3812 | * However, if the backend did not specify any option, we have to rely |
| 3813 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3814 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3815 | if (s->be->fwdfor_hdr_len) { |
| 3816 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3817 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3818 | } else { |
| 3819 | len = s->fe->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3820 | memcpy(trash.str, s->fe->fwdfor_hdr_name, len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3821 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3822 | len += sprintf(trash.str + len, ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3823 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3824 | 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] | 3825 | goto return_bad_req; |
| 3826 | } |
| 3827 | } |
| 3828 | |
| 3829 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3830 | * 10: add X-Original-To if either the frontend or the backend |
| 3831 | * asks for it. |
| 3832 | */ |
| 3833 | if ((s->fe->options | s->be->options) & PR_O_ORGTO) { |
| 3834 | |
| 3835 | /* FIXME: don't know if IPv6 can handle that case too. */ |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 3836 | if (s->req->prod->conn->addr.from.ss_family == AF_INET) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3837 | /* Add an X-Original-To header unless the destination IP is |
| 3838 | * in the 'except' network range. |
| 3839 | */ |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 3840 | conn_get_to_addr(s->req->prod->conn); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3841 | |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 3842 | if (s->req->prod->conn->addr.to.ss_family == AF_INET && |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 3843 | ((!s->fe->except_mask_to.s_addr || |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 3844 | (((struct sockaddr_in *)&s->req->prod->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] | 3845 | != s->fe->except_to.s_addr) && |
| 3846 | (!s->be->except_mask_to.s_addr || |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 3847 | (((struct sockaddr_in *)&s->req->prod->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] | 3848 | != s->be->except_to.s_addr))) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3849 | int len; |
| 3850 | unsigned char *pn; |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 3851 | pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->conn->addr.to)->sin_addr; |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3852 | |
| 3853 | /* Note: we rely on the backend to get the header name to be used for |
| 3854 | * x-original-to, because the header is really meant for the backends. |
| 3855 | * However, if the backend did not specify any option, we have to rely |
| 3856 | * on the frontend's header name. |
| 3857 | */ |
| 3858 | if (s->be->orgto_hdr_len) { |
| 3859 | len = s->be->orgto_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3860 | memcpy(trash.str, s->be->orgto_hdr_name, len); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3861 | } else { |
| 3862 | len = s->fe->orgto_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3863 | memcpy(trash.str, s->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3864 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3865 | 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] | 3866 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3867 | 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] | 3868 | goto return_bad_req; |
| 3869 | } |
| 3870 | } |
| 3871 | } |
| 3872 | |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 3873 | /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. |
| 3874 | * If an "Upgrade" token is found, the header is left untouched in order not to have |
| 3875 | * to deal with some servers bugs : some of them fail an Upgrade if anything but |
| 3876 | * "Upgrade" is present in the Connection header. |
| 3877 | */ |
| 3878 | if (!(txn->flags & TX_HDR_CONN_UPG) && |
| 3879 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
| 3880 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3881 | unsigned int want_flags = 0; |
| 3882 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3883 | if (msg->flags & HTTP_MSGF_VER_11) { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 3884 | if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL || |
| 3885 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) && |
| 3886 | !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3887 | want_flags |= TX_CON_CLO_SET; |
| 3888 | } else { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 3889 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
| 3890 | !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) || |
| 3891 | ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3892 | want_flags |= TX_CON_KAL_SET; |
| 3893 | } |
| 3894 | |
| 3895 | 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] | 3896 | http_change_connection_header(txn, msg, want_flags); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3897 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3898 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3899 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3900 | /* If we have no server assigned yet and we're balancing on url_param |
| 3901 | * with a POST request, we may be interested in checking the body for |
| 3902 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3903 | */ |
| 3904 | if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) && |
| 3905 | s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3906 | s->be->url_param_post_limit != 0 && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3907 | (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3908 | channel_dont_connect(req); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3909 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3910 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3911 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3912 | if (msg->flags & HTTP_MSGF_XFER_LEN) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3913 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 3914 | #ifdef TCP_QUICKACK |
| 3915 | /* We expect some data from the client. Unless we know for sure |
| 3916 | * we already have a full request, we have to re-enable quick-ack |
| 3917 | * in case we previously disabled it, otherwise we might cause |
| 3918 | * the client to delay further data. |
| 3919 | */ |
| 3920 | if ((s->listener->options & LI_O_NOQUICKACK) && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3921 | ((msg->flags & HTTP_MSGF_TE_CHNK) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3922 | (msg->body_len > req->buf->i - txn->req.eoh - 2))) |
Willy Tarreau | 7f7ad91 | 2012-11-11 19:27:15 +0100 | [diff] [blame] | 3923 | setsockopt(s->si[0].conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 3924 | #endif |
| 3925 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 3926 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3927 | /************************************************************* |
| 3928 | * OK, that's finished for the headers. We have done what we * |
| 3929 | * could. Let's switch to the DATA state. * |
| 3930 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3931 | req->analyse_exp = TICK_ETERNITY; |
| 3932 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3933 | |
Willy Tarreau | 7bb68ab | 2012-05-13 14:48:59 +0200 | [diff] [blame] | 3934 | /* if the server closes the connection, we want to immediately react |
| 3935 | * and close the socket to save packets and syscalls. |
| 3936 | */ |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 3937 | if (!(req->analysers & AN_REQ_HTTP_XFER_BODY)) |
| 3938 | req->cons->flags |= SI_FL_NOHALF; |
Willy Tarreau | 7bb68ab | 2012-05-13 14:48:59 +0200 | [diff] [blame] | 3939 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3940 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3941 | /* OK let's go on with the BODY now */ |
| 3942 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3943 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3944 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3945 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 3946 | /* we detected a parsing error. We want to archive this request |
| 3947 | * in the dedicated proxy area for later troubleshooting. |
| 3948 | */ |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 3949 | 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] | 3950 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3951 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3952 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3953 | txn->status = 400; |
| 3954 | req->analysers = 0; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 3955 | 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] | 3956 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3957 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3958 | if (s->listener->counters) |
| 3959 | s->listener->counters->failed_req++; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3960 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3961 | if (!(s->flags & SN_ERR_MASK)) |
| 3962 | s->flags |= SN_ERR_PRXCOND; |
| 3963 | if (!(s->flags & SN_FINST_MASK)) |
| 3964 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3965 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3966 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3967 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3968 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 3969 | * returns zero, at the beginning because it prevents any other processing |
| 3970 | * from occurring, and at the end because it terminates the request. |
| 3971 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 3972 | 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] | 3973 | { |
| 3974 | struct http_txn *txn = &s->txn; |
| 3975 | |
| 3976 | /* This connection is being tarpitted. The CLIENT side has |
| 3977 | * already set the connect expiration date to the right |
| 3978 | * timeout. We just have to check that the client is still |
| 3979 | * there and that the timeout has not expired. |
| 3980 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3981 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 3982 | if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 && |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3983 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 3984 | return 0; |
| 3985 | |
| 3986 | /* We will set the queue timer to the time spent, just for |
| 3987 | * logging purposes. We fake a 500 server error, so that the |
| 3988 | * attacker will not suspect his connection has been tarpitted. |
| 3989 | * It will not cause trouble to the logs because we can exclude |
| 3990 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 3991 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3992 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 3993 | |
| 3994 | txn->status = 500; |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 3995 | if (!(req->flags & CF_READ_ERROR)) |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 3996 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3997 | |
| 3998 | req->analysers = 0; |
| 3999 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4000 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4001 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4002 | if (s->listener->counters) |
| 4003 | s->listener->counters->failed_req++; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4004 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 4005 | if (!(s->flags & SN_ERR_MASK)) |
| 4006 | s->flags |= SN_ERR_PRXCOND; |
| 4007 | if (!(s->flags & SN_FINST_MASK)) |
| 4008 | s->flags |= SN_FINST_T; |
| 4009 | return 0; |
| 4010 | } |
| 4011 | |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4012 | /* This function is an analyser which processes the HTTP request body. It looks |
| 4013 | * for parameters to be used for the load balancing algorithm (url_param). It |
| 4014 | * must only be called after the standard HTTP request processing has occurred, |
| 4015 | * because it expects the request to be parsed. It returns zero if it needs to |
| 4016 | * read more data, or 1 once it has completed its analysis. |
| 4017 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 4018 | 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] | 4019 | { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4020 | struct http_txn *txn = &s->txn; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4021 | struct http_msg *msg = &s->txn.req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4022 | long long limit = s->be->url_param_post_limit; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4023 | |
| 4024 | /* We have to parse the HTTP request body to find any required data. |
| 4025 | * "balance url_param check_post" should have been the only way to get |
| 4026 | * into this. We were brought here after HTTP header analysis, so all |
| 4027 | * related structures are ready. |
| 4028 | */ |
| 4029 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4030 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4031 | goto missing_data; |
| 4032 | |
| 4033 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 4034 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 4035 | * send an HTTP/1.1 100 Continue intermediate response. |
| 4036 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4037 | if (msg->flags & HTTP_MSGF_VER_11) { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4038 | struct hdr_ctx ctx; |
| 4039 | ctx.idx = 0; |
| 4040 | /* Expect is allowed in 1.1, look for it */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4041 | 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] | 4042 | 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] | 4043 | bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4044 | } |
| 4045 | } |
| 4046 | msg->msg_state = HTTP_MSG_100_SENT; |
| 4047 | } |
| 4048 | |
| 4049 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 4050 | /* 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] | 4051 | * 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] | 4052 | * is still null. We must save the body in msg->next because it |
| 4053 | * survives buffer re-alignments. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4054 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 4055 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4056 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4057 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4058 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 4059 | else |
| 4060 | msg->msg_state = HTTP_MSG_DATA; |
| 4061 | } |
| 4062 | |
| 4063 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4064 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4065 | * 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] | 4066 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4067 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 4068 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4069 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4070 | if (!ret) |
| 4071 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4072 | else if (ret < 0) { |
| 4073 | session_inc_http_err_ctr(s); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4074 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4075 | } |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4076 | } |
| 4077 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4078 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 4079 | * We have the first data byte is in msg->sov. We're waiting for at |
| 4080 | * least <url_param_post_limit> bytes after msg->sov. |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4081 | */ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4082 | |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4083 | if (msg->body_len < limit) |
| 4084 | limit = msg->body_len; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4085 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4086 | if (req->buf->i - msg->sov >= limit) /* we have enough bytes now */ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4087 | goto http_end; |
| 4088 | |
| 4089 | missing_data: |
| 4090 | /* we get here if we need to wait for more data */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4091 | if (buffer_full(req->buf, global.tune.maxrewrite)) { |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4092 | session_inc_http_err_ctr(s); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4093 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4094 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 4095 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4096 | 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] | 4097 | txn->status = 408; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4098 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408)); |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4099 | |
| 4100 | if (!(s->flags & SN_ERR_MASK)) |
| 4101 | s->flags |= SN_ERR_CLITO; |
| 4102 | if (!(s->flags & SN_FINST_MASK)) |
| 4103 | s->flags |= SN_FINST_D; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4104 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4105 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4106 | |
| 4107 | /* we get here if we need to wait for more data */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4108 | 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] | 4109 | /* Not enough data. We'll re-use the http-request |
| 4110 | * timeout here. Ideally, we should set the timeout |
| 4111 | * relative to the accept() date. We just set the |
| 4112 | * request timeout once at the beginning of the |
| 4113 | * request. |
| 4114 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4115 | channel_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4116 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 4117 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4118 | return 0; |
| 4119 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4120 | |
| 4121 | http_end: |
| 4122 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 4123 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 4124 | req->analysers &= ~an_bit; |
| 4125 | req->analyse_exp = TICK_ETERNITY; |
| 4126 | return 1; |
| 4127 | |
| 4128 | return_bad_req: /* let's centralize all bad requests */ |
| 4129 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4130 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4131 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4132 | |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4133 | if (!(s->flags & SN_ERR_MASK)) |
| 4134 | s->flags |= SN_ERR_PRXCOND; |
| 4135 | if (!(s->flags & SN_FINST_MASK)) |
| 4136 | s->flags |= SN_FINST_R; |
| 4137 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4138 | return_err_msg: |
| 4139 | req->analysers = 0; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4140 | s->fe->fe_counters.failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4141 | if (s->listener->counters) |
| 4142 | s->listener->counters->failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 4143 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 4144 | } |
| 4145 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4146 | /* send a server's name with an outgoing request over an established connection. |
| 4147 | * Note: this function is designed to be called once the request has been scheduled |
| 4148 | * for being forwarded. This is the reason why it rewinds the buffer before |
| 4149 | * proceeding. |
| 4150 | */ |
Willy Tarreau | 45c0d98 | 2012-03-09 12:11:57 +0100 | [diff] [blame] | 4151 | 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] | 4152 | |
| 4153 | struct hdr_ctx ctx; |
| 4154 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4155 | char *hdr_name = be->server_id_hdr_name; |
| 4156 | int hdr_name_len = be->server_id_hdr_len; |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 4157 | struct channel *chn = txn->req.chn; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4158 | char *hdr_val; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4159 | unsigned int old_o, old_i; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4160 | |
William Lallemand | d9e9066 | 2012-01-30 17:27:17 +0100 | [diff] [blame] | 4161 | ctx.idx = 0; |
| 4162 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4163 | old_o = chn->buf->o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4164 | if (old_o) { |
| 4165 | /* The request was already skipped, let's restore it */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4166 | b_rew(chn->buf, old_o); |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4167 | } |
| 4168 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4169 | old_i = chn->buf->i; |
| 4170 | 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] | 4171 | /* remove any existing values from the header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 4172 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4173 | } |
| 4174 | |
| 4175 | /* Add the new header requested with the server value */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4176 | hdr_val = trash.str; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4177 | memcpy(hdr_val, hdr_name, hdr_name_len); |
| 4178 | hdr_val += hdr_name_len; |
| 4179 | *hdr_val++ = ':'; |
| 4180 | *hdr_val++ = ' '; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 4181 | hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val); |
| 4182 | 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] | 4183 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4184 | if (old_o) { |
| 4185 | /* If this was a forwarded request, we must readjust the amount of |
| 4186 | * data to be forwarded in order to take into account the size |
Willy Tarreau | 2fef9b1 | 2013-03-26 01:08:21 +0100 | [diff] [blame] | 4187 | * variations. Note that if the request was already scheduled for |
| 4188 | * forwarding, it had its req->sol pointing to the body, which |
| 4189 | * must then be updated too. |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4190 | */ |
Willy Tarreau | 2fef9b1 | 2013-03-26 01:08:21 +0100 | [diff] [blame] | 4191 | txn->req.sol += chn->buf->i - old_i; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4192 | b_adv(chn->buf, old_o + chn->buf->i - old_i); |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4193 | } |
| 4194 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4195 | return 0; |
| 4196 | } |
| 4197 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4198 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 4199 | * right now but it works. |
| 4200 | */ |
| 4201 | void http_end_txn_clean_session(struct session *s) |
| 4202 | { |
| 4203 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 4204 | * server's connections. We need a safer way to reinitialize buffer |
| 4205 | * flags. We also need a more accurate method for computing per-request |
| 4206 | * data. |
| 4207 | */ |
| 4208 | http_silent_debug(__LINE__, s); |
| 4209 | |
Willy Tarreau | 7bb68ab | 2012-05-13 14:48:59 +0200 | [diff] [blame] | 4210 | s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF; |
Willy Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 4211 | si_shutr(s->req->cons); |
| 4212 | si_shutw(s->req->cons); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4213 | |
| 4214 | http_silent_debug(__LINE__, s); |
| 4215 | |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 4216 | if (s->flags & SN_BE_ASSIGNED) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4217 | s->be->beconn--; |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 4218 | if (unlikely(s->srv_conn)) |
| 4219 | sess_change_server(s, NULL); |
| 4220 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4221 | |
| 4222 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 4223 | session_process_counters(s); |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 4224 | session_stop_backend_counters(s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4225 | |
| 4226 | if (s->txn.status) { |
| 4227 | int n; |
| 4228 | |
| 4229 | n = s->txn.status / 100; |
| 4230 | if (n < 1 || n > 5) |
| 4231 | n = 0; |
| 4232 | |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4233 | if (s->fe->mode == PR_MODE_HTTP) { |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4234 | s->fe->fe_counters.p.http.rsp[n]++; |
Willy Tarreau | 8139b99 | 2012-11-27 07:35:31 +0100 | [diff] [blame] | 4235 | if (s->comp_algo && (s->flags & SN_COMP_READY)) |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4236 | s->fe->fe_counters.p.http.comp_rsp++; |
| 4237 | } |
Willy Tarreau | 2465779 | 2010-02-26 10:30:28 +0100 | [diff] [blame] | 4238 | if ((s->flags & SN_BE_ASSIGNED) && |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4239 | (s->be->mode == PR_MODE_HTTP)) { |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4240 | s->be->be_counters.p.http.rsp[n]++; |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4241 | s->be->be_counters.p.http.cum_req++; |
Willy Tarreau | 8139b99 | 2012-11-27 07:35:31 +0100 | [diff] [blame] | 4242 | if (s->comp_algo && (s->flags & SN_COMP_READY)) |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4243 | s->be->be_counters.p.http.comp_rsp++; |
| 4244 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4245 | } |
| 4246 | |
| 4247 | /* don't count other requests' data */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4248 | s->logs.bytes_in -= s->req->buf->i; |
| 4249 | s->logs.bytes_out -= s->rep->buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4250 | |
| 4251 | /* let's do a final log if we need it */ |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 4252 | if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4253 | !(s->flags & SN_MONITOR) && |
| 4254 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
| 4255 | s->do_log(s); |
| 4256 | } |
| 4257 | |
| 4258 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 4259 | s->logs.tv_accept = now; /* corrected date for internal use */ |
| 4260 | tv_zero(&s->logs.tv_request); |
| 4261 | s->logs.t_queue = -1; |
| 4262 | s->logs.t_connect = -1; |
| 4263 | s->logs.t_data = -1; |
| 4264 | s->logs.t_close = 0; |
| 4265 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 4266 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 4267 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4268 | s->logs.bytes_in = s->req->total = s->req->buf->i; |
| 4269 | s->logs.bytes_out = s->rep->total = s->rep->buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4270 | |
| 4271 | if (s->pend_pos) |
| 4272 | pendconn_free(s->pend_pos); |
| 4273 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4274 | if (objt_server(s->target)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4275 | if (s->flags & SN_CURR_SESS) { |
| 4276 | s->flags &= ~SN_CURR_SESS; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4277 | objt_server(s->target)->cur_sess--; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4278 | } |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4279 | if (may_dequeue_tasks(objt_server(s->target), s->be)) |
| 4280 | process_srv_queue(objt_server(s->target)); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4281 | } |
| 4282 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4283 | s->target = NULL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4284 | |
| 4285 | s->req->cons->state = s->req->cons->prev_state = SI_ST_INI; |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 4286 | s->req->cons->conn->t.sock.fd = -1; /* just to help with debugging */ |
| 4287 | s->req->cons->conn->flags = CO_FL_NONE; |
Willy Tarreau | 14cba4b | 2012-11-30 17:33:05 +0100 | [diff] [blame] | 4288 | s->req->cons->conn->err_code = CO_ER_NONE; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4289 | s->req->cons->err_type = SI_ET_NONE; |
Willy Tarreau | 0b3a411 | 2011-03-27 19:16:56 +0200 | [diff] [blame] | 4290 | s->req->cons->conn_retries = 0; /* used for logging too */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4291 | s->req->cons->err_loc = NULL; |
| 4292 | s->req->cons->exp = TICK_ETERNITY; |
| 4293 | s->req->cons->flags = SI_FL_NONE; |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4294 | s->req->flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT); |
| 4295 | 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] | 4296 | s->flags &= ~(SN_DIRECT|SN_ASSIGNED|SN_ADDR_SET|SN_BE_ASSIGNED|SN_FORCE_PRST|SN_IGNORE_PRST); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4297 | s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE); |
Willy Tarreau | 543db62 | 2012-11-15 16:41:22 +0100 | [diff] [blame] | 4298 | |
| 4299 | if (s->flags & SN_COMP_READY) |
| 4300 | s->comp_algo->end(&s->comp_ctx); |
| 4301 | s->comp_algo = NULL; |
| 4302 | s->flags &= ~SN_COMP_READY; |
| 4303 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4304 | s->txn.meth = 0; |
| 4305 | http_reset_txn(s); |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 4306 | s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ; |
Willy Tarreau | ee55dc0 | 2010-06-01 10:56:34 +0200 | [diff] [blame] | 4307 | if (s->fe->options2 & PR_O2_INDEPSTR) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4308 | s->req->cons->flags |= SI_FL_INDEP_STR; |
| 4309 | |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 4310 | if (s->fe->options2 & PR_O2_NODELAY) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4311 | s->req->flags |= CF_NEVER_WAIT; |
| 4312 | s->rep->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 4313 | } |
| 4314 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4315 | /* if the request buffer is not empty, it means we're |
| 4316 | * about to process another request, so send pending |
| 4317 | * data with MSG_MORE to merge TCP packets when possible. |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 4318 | * Just don't do this if the buffer is close to be full, |
| 4319 | * because the request will wait for it to flush a little |
| 4320 | * bit before proceeding. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4321 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4322 | if (s->req->buf->i) { |
| 4323 | if (s->rep->buf->o && |
| 4324 | !buffer_full(s->rep->buf, global.tune.maxrewrite) && |
| 4325 | 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] | 4326 | s->rep->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 4327 | } |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4328 | |
| 4329 | /* we're removing the analysers, we MUST re-enable events detection */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4330 | channel_auto_read(s->req); |
| 4331 | channel_auto_close(s->req); |
| 4332 | channel_auto_read(s->rep); |
| 4333 | channel_auto_close(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4334 | |
Willy Tarreau | 342b11c | 2010-11-24 16:22:09 +0100 | [diff] [blame] | 4335 | s->req->analysers = s->listener->analysers; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4336 | s->rep->analysers = 0; |
| 4337 | |
| 4338 | http_silent_debug(__LINE__, s); |
| 4339 | } |
| 4340 | |
| 4341 | |
| 4342 | /* This function updates the request state machine according to the response |
| 4343 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 4344 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 4345 | * it is only used to find when a request/response couple is complete. Both |
| 4346 | * this function and its equivalent should loop until both return zero. It |
| 4347 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 4348 | */ |
| 4349 | int http_sync_req_state(struct session *s) |
| 4350 | { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4351 | struct channel *chn = s->req; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4352 | struct http_txn *txn = &s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4353 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4354 | unsigned int old_state = txn->req.msg_state; |
| 4355 | |
| 4356 | http_silent_debug(__LINE__, s); |
| 4357 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) |
| 4358 | return 0; |
| 4359 | |
| 4360 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4361 | /* No need to read anymore, the request was completely parsed. |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4362 | * We can shut the read side unless we want to abort_on_close, |
| 4363 | * or we have a POST request. The issue with POST requests is |
| 4364 | * that some browsers still send a CRLF after the request, and |
| 4365 | * this CRLF must be read so that it does not remain in the kernel |
| 4366 | * buffers, otherwise a close could cause an RST on some systems |
| 4367 | * (eg: Linux). |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4368 | */ |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4369 | if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4370 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4371 | |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4372 | /* if the server closes the connection, we want to immediately react |
| 4373 | * and close the socket to save packets and syscalls. |
| 4374 | */ |
| 4375 | chn->cons->flags |= SI_FL_NOHALF; |
| 4376 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4377 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 4378 | goto wait_other_side; |
| 4379 | |
| 4380 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 4381 | /* The server has not finished to respond, so we |
| 4382 | * don't want to move in order not to upset it. |
| 4383 | */ |
| 4384 | goto wait_other_side; |
| 4385 | } |
| 4386 | |
| 4387 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 4388 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4389 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4390 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 4391 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4392 | goto wait_other_side; |
| 4393 | } |
| 4394 | |
| 4395 | /* When we get here, it means that both the request and the |
| 4396 | * response have finished receiving. Depending on the connection |
| 4397 | * mode, we'll have to wait for the last bytes to leave in either |
| 4398 | * direction, and sometimes for a close to be effective. |
| 4399 | */ |
| 4400 | |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4401 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 4402 | /* Server-close mode : queue a connection close to the server */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4403 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) |
| 4404 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4405 | } |
| 4406 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4407 | /* Option forceclose is set, or either side wants to close, |
| 4408 | * let's enforce it now that we're not expecting any new |
| 4409 | * data to come. The caller knows the session is complete |
| 4410 | * once both states are CLOSED. |
| 4411 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4412 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 4413 | channel_shutr_now(chn); |
| 4414 | channel_shutw_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4415 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4416 | } |
| 4417 | else { |
| 4418 | /* The last possible modes are keep-alive and tunnel. Since tunnel |
| 4419 | * mode does not set the body analyser, we can't reach this place |
| 4420 | * in tunnel mode, so we're left with keep-alive only. |
| 4421 | * This mode is currently not implemented, we switch to tunnel mode. |
| 4422 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4423 | channel_auto_read(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4424 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 4425 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4426 | } |
| 4427 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4428 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4429 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4430 | chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4431 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4432 | if (!channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4433 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 4434 | goto http_msg_closing; |
| 4435 | } |
| 4436 | else { |
| 4437 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 4438 | goto http_msg_closed; |
| 4439 | } |
| 4440 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4441 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4442 | } |
| 4443 | |
| 4444 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 4445 | http_msg_closing: |
| 4446 | /* nothing else to forward, just waiting for the output buffer |
| 4447 | * to be empty and for the shutw_now to take effect. |
| 4448 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4449 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4450 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 4451 | goto http_msg_closed; |
| 4452 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4453 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4454 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4455 | goto wait_other_side; |
| 4456 | } |
| 4457 | } |
| 4458 | |
| 4459 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 4460 | http_msg_closed: |
| 4461 | goto wait_other_side; |
| 4462 | } |
| 4463 | |
| 4464 | wait_other_side: |
| 4465 | http_silent_debug(__LINE__, s); |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4466 | return txn->req.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4467 | } |
| 4468 | |
| 4469 | |
| 4470 | /* This function updates the response state machine according to the request |
| 4471 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 4472 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 4473 | * it is only used to find when a request/response couple is complete. Both |
| 4474 | * this function and its equivalent should loop until both return zero. It |
| 4475 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 4476 | */ |
| 4477 | int http_sync_res_state(struct session *s) |
| 4478 | { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4479 | struct channel *chn = s->rep; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4480 | struct http_txn *txn = &s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4481 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4482 | unsigned int old_state = txn->rsp.msg_state; |
| 4483 | |
| 4484 | http_silent_debug(__LINE__, s); |
| 4485 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 4486 | return 0; |
| 4487 | |
| 4488 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 4489 | /* In theory, we don't need to read anymore, but we must |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4490 | * still monitor the server connection for a possible close |
| 4491 | * while the request is being uploaded, so we don't disable |
| 4492 | * reading. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4493 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4494 | /* channel_dont_read(chn); */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4495 | |
| 4496 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 4497 | goto wait_other_side; |
| 4498 | |
| 4499 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 4500 | /* The client seems to still be sending data, probably |
| 4501 | * because we got an error response during an upload. |
| 4502 | * We have the choice of either breaking the connection |
| 4503 | * or letting it pass through. Let's do the later. |
| 4504 | */ |
| 4505 | goto wait_other_side; |
| 4506 | } |
| 4507 | |
| 4508 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 4509 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4510 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4511 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 4512 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4513 | goto wait_other_side; |
| 4514 | } |
| 4515 | |
| 4516 | /* When we get here, it means that both the request and the |
| 4517 | * response have finished receiving. Depending on the connection |
| 4518 | * mode, we'll have to wait for the last bytes to leave in either |
| 4519 | * direction, and sometimes for a close to be effective. |
| 4520 | */ |
| 4521 | |
| 4522 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 4523 | /* Server-close mode : shut read and wait for the request |
| 4524 | * side to close its output buffer. The caller will detect |
| 4525 | * when we're in DONE and the other is in CLOSED and will |
| 4526 | * catch that for the final cleanup. |
| 4527 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4528 | if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW))) |
| 4529 | channel_shutr_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4530 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4531 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4532 | /* Option forceclose is set, or either side wants to close, |
| 4533 | * let's enforce it now that we're not expecting any new |
| 4534 | * data to come. The caller knows the session is complete |
| 4535 | * once both states are CLOSED. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4536 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4537 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 4538 | channel_shutr_now(chn); |
| 4539 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4540 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4541 | } |
| 4542 | else { |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4543 | /* The last possible modes are keep-alive and tunnel. Since tunnel |
| 4544 | * mode does not set the body analyser, we can't reach this place |
| 4545 | * in tunnel mode, so we're left with keep-alive only. |
| 4546 | * This mode is currently not implemented, we switch to tunnel mode. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4547 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4548 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4549 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 4550 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4551 | } |
| 4552 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4553 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4554 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4555 | if (!channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4556 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 4557 | goto http_msg_closing; |
| 4558 | } |
| 4559 | else { |
| 4560 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4561 | goto http_msg_closed; |
| 4562 | } |
| 4563 | } |
| 4564 | goto wait_other_side; |
| 4565 | } |
| 4566 | |
| 4567 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 4568 | http_msg_closing: |
| 4569 | /* nothing else to forward, just waiting for the output buffer |
| 4570 | * to be empty and for the shutw_now to take effect. |
| 4571 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4572 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4573 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4574 | goto http_msg_closed; |
| 4575 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4576 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4577 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4578 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4579 | if (objt_server(s->target)) |
| 4580 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4581 | goto wait_other_side; |
| 4582 | } |
| 4583 | } |
| 4584 | |
| 4585 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 4586 | http_msg_closed: |
| 4587 | /* drop any pending data */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4588 | bi_erase(chn); |
| 4589 | channel_auto_close(chn); |
| 4590 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4591 | goto wait_other_side; |
| 4592 | } |
| 4593 | |
| 4594 | wait_other_side: |
| 4595 | http_silent_debug(__LINE__, s); |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 4596 | /* We force the response to leave immediately if we're waiting for the |
| 4597 | * other side, since there is no pending shutdown to push it out. |
| 4598 | */ |
| 4599 | if (!channel_is_empty(chn)) |
| 4600 | chn->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4601 | return txn->rsp.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4602 | } |
| 4603 | |
| 4604 | |
| 4605 | /* Resync the request and response state machines. Return 1 if either state |
| 4606 | * changes. |
| 4607 | */ |
| 4608 | int http_resync_states(struct session *s) |
| 4609 | { |
| 4610 | struct http_txn *txn = &s->txn; |
| 4611 | int old_req_state = txn->req.msg_state; |
| 4612 | int old_res_state = txn->rsp.msg_state; |
| 4613 | |
| 4614 | http_silent_debug(__LINE__, s); |
| 4615 | http_sync_req_state(s); |
| 4616 | while (1) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4617 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4618 | if (!http_sync_res_state(s)) |
| 4619 | break; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4620 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4621 | if (!http_sync_req_state(s)) |
| 4622 | break; |
| 4623 | } |
| 4624 | http_silent_debug(__LINE__, s); |
| 4625 | /* OK, both state machines agree on a compatible state. |
| 4626 | * There are a few cases we're interested in : |
| 4627 | * - HTTP_MSG_TUNNEL on either means we have to disable both analysers |
| 4628 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 4629 | * directions, so let's simply disable both analysers. |
| 4630 | * - HTTP_MSG_CLOSED on the response only means we must abort the |
| 4631 | * request. |
| 4632 | * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response |
| 4633 | * with server-close mode means we've completed one request and we |
| 4634 | * must re-initialize the server connection. |
| 4635 | */ |
| 4636 | |
| 4637 | if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 4638 | txn->rsp.msg_state == HTTP_MSG_TUNNEL || |
| 4639 | (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 4640 | txn->rsp.msg_state == HTTP_MSG_CLOSED)) { |
| 4641 | s->req->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4642 | channel_auto_close(s->req); |
| 4643 | channel_auto_read(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4644 | s->rep->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4645 | channel_auto_close(s->rep); |
| 4646 | channel_auto_read(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4647 | } |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4648 | else if ((txn->req.msg_state >= HTTP_MSG_DONE && |
| 4649 | (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) || |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4650 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4651 | txn->req.msg_state == HTTP_MSG_ERROR) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4652 | s->rep->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4653 | channel_auto_close(s->rep); |
| 4654 | channel_auto_read(s->rep); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4655 | s->req->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4656 | channel_abort(s->req); |
| 4657 | channel_auto_close(s->req); |
| 4658 | channel_auto_read(s->req); |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 4659 | bi_erase(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4660 | } |
| 4661 | else if (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 4662 | txn->rsp.msg_state == HTTP_MSG_DONE && |
| 4663 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) { |
| 4664 | /* server-close: terminate this server connection and |
| 4665 | * reinitialize a fresh-new transaction. |
| 4666 | */ |
| 4667 | http_end_txn_clean_session(s); |
| 4668 | } |
| 4669 | |
| 4670 | http_silent_debug(__LINE__, s); |
| 4671 | return txn->req.msg_state != old_req_state || |
| 4672 | txn->rsp.msg_state != old_res_state; |
| 4673 | } |
| 4674 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4675 | /* This function is an analyser which forwards request body (including chunk |
| 4676 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 4677 | * zero byte. The only situation where it must not be called is when we're in |
| 4678 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 4679 | * remaining data and to resync after end of body. It expects the msg_state to |
| 4680 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 4681 | * 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] | 4682 | * 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] | 4683 | * bytes of pending data + the headers if not already done (between sol and sov). |
| 4684 | * 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] | 4685 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 4686 | 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] | 4687 | { |
| 4688 | struct http_txn *txn = &s->txn; |
| 4689 | struct http_msg *msg = &s->txn.req; |
| 4690 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4691 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4692 | return 0; |
| 4693 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4694 | 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] | 4695 | ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4696 | /* Output closed while we were sending data. We must abort and |
| 4697 | * wake the other side up. |
| 4698 | */ |
| 4699 | msg->msg_state = HTTP_MSG_ERROR; |
| 4700 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 4701 | return 1; |
| 4702 | } |
| 4703 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4704 | /* in most states, we should abort in case of early close */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4705 | channel_auto_close(req); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4706 | |
| 4707 | /* Note that we don't have to send 100-continue back because we don't |
| 4708 | * need the data to complete our job, and it's up to the server to |
| 4709 | * decide whether to return 100, 417 or anything else in return of |
| 4710 | * an "Expect: 100-continue" header. |
| 4711 | */ |
| 4712 | |
| 4713 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 4714 | /* 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] | 4715 | * 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] | 4716 | * is still null. We must save the body in msg->next because it |
| 4717 | * survives buffer re-alignments. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4718 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 4719 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4720 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4721 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4722 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 4723 | else |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4724 | msg->msg_state = HTTP_MSG_DATA; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4725 | } |
| 4726 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4727 | while (1) { |
Willy Tarreau | ea95316 | 2012-05-18 23:41:28 +0200 | [diff] [blame] | 4728 | unsigned int bytes; |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 4729 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4730 | http_silent_debug(__LINE__, s); |
Willy Tarreau | ea95316 | 2012-05-18 23:41:28 +0200 | [diff] [blame] | 4731 | /* we may have some data pending between sol and sov */ |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 4732 | bytes = msg->sov - msg->sol; |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 4733 | if (msg->chunk_len || bytes) { |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 4734 | msg->sol = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4735 | msg->next -= bytes; /* will be forwarded */ |
Willy Tarreau | ea95316 | 2012-05-18 23:41:28 +0200 | [diff] [blame] | 4736 | msg->chunk_len += bytes; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4737 | msg->chunk_len -= channel_forward(req, msg->chunk_len); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 4738 | } |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 4739 | |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4740 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 4741 | /* must still forward */ |
| 4742 | if (req->to_forward) |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4743 | goto missing_data; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4744 | |
| 4745 | /* nothing left to forward */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4746 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 4747 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4748 | else |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4749 | msg->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4750 | } |
| 4751 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4752 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4753 | * 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] | 4754 | * TRAILERS state. |
| 4755 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 4756 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4757 | |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 4758 | if (ret == 0) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4759 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4760 | else if (ret < 0) { |
| 4761 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4762 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4763 | 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] | 4764 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4765 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4766 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4767 | } |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 4768 | else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4769 | /* we want the CRLF after the data */ |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 4770 | int ret = http_skip_chunk_crlf(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4771 | |
| 4772 | if (ret == 0) |
| 4773 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4774 | else if (ret < 0) { |
| 4775 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4776 | if (msg->err_pos >= 0) |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 4777 | 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] | 4778 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4779 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4780 | /* we're in MSG_CHUNK_SIZE now */ |
| 4781 | } |
| 4782 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 4783 | int ret = http_forward_trailers(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4784 | |
| 4785 | if (ret == 0) |
| 4786 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4787 | else if (ret < 0) { |
| 4788 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4789 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4790 | 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] | 4791 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4792 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4793 | /* we're in HTTP_MSG_DONE now */ |
| 4794 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4795 | else { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4796 | int old_state = msg->msg_state; |
| 4797 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4798 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4799 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4800 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 4801 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4802 | channel_dont_close(req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4803 | if (http_resync_states(s)) { |
| 4804 | /* some state changes occurred, maybe the analyser |
| 4805 | * was disabled too. |
Willy Tarreau | face839 | 2010-01-03 11:37:54 +0100 | [diff] [blame] | 4806 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 4807 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4808 | if (req->flags & CF_SHUTW) { |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 4809 | /* request errors are most likely due to |
| 4810 | * the server aborting the transfer. |
| 4811 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4812 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 4813 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4814 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4815 | 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] | 4816 | goto return_bad_req; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 4817 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4818 | return 1; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4819 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 4820 | |
| 4821 | /* If "option abortonclose" is set on the backend, we |
| 4822 | * want to monitor the client's connection and forward |
| 4823 | * any shutdown notification to the server, which will |
| 4824 | * decide whether to close or to go on processing the |
| 4825 | * request. |
| 4826 | */ |
| 4827 | if (s->be->options & PR_O_ABRT_CLOSE) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4828 | channel_auto_read(req); |
| 4829 | channel_auto_close(req); |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 4830 | } |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4831 | else if (s->txn.meth == HTTP_METH_POST) { |
| 4832 | /* POST requests may require to read extra CRLF |
| 4833 | * sent by broken browsers and which could cause |
| 4834 | * an RST to be sent upon close on some systems |
| 4835 | * (eg: Linux). |
| 4836 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4837 | channel_auto_read(req); |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4838 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 4839 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4840 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4841 | } |
| 4842 | } |
| 4843 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4844 | missing_data: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4845 | /* stop waiting for data if the input is closed before the end */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4846 | if (req->flags & CF_SHUTR) { |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4847 | if (!(s->flags & SN_ERR_MASK)) |
| 4848 | s->flags |= SN_ERR_CLICL; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4849 | if (!(s->flags & SN_FINST_MASK)) { |
| 4850 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 4851 | s->flags |= SN_FINST_H; |
| 4852 | else |
| 4853 | s->flags |= SN_FINST_D; |
| 4854 | } |
| 4855 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4856 | s->fe->fe_counters.cli_aborts++; |
| 4857 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4858 | if (objt_server(s->target)) |
| 4859 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4860 | |
| 4861 | goto return_bad_req_stats_ok; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4862 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4863 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4864 | /* waiting for the last bits to leave the buffer */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4865 | if (req->flags & CF_SHUTW) |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4866 | goto aborted_xfer; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4867 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4868 | /* 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] | 4869 | * 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] | 4870 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4871 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4872 | channel_dont_close(req); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4873 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4874 | /* 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] | 4875 | * 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] | 4876 | * 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] | 4877 | * modes are already handled by the stream sock layer. We must not do |
| 4878 | * this in content-length mode because it could present the MSG_MORE |
| 4879 | * flag with the last block of forwarded data, which would cause an |
| 4880 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4881 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4882 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4883 | req->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4884 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4885 | http_silent_debug(__LINE__, s); |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4886 | return 0; |
| 4887 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4888 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4889 | s->fe->fe_counters.failed_req++; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4890 | if (s->listener->counters) |
| 4891 | s->listener->counters->failed_req++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4892 | return_bad_req_stats_ok: |
| 4893 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4894 | if (txn->status) { |
| 4895 | /* Note: we don't send any error if some data were already sent */ |
| 4896 | stream_int_retnclose(req->prod, NULL); |
| 4897 | } else { |
| 4898 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4899 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4900 | } |
| 4901 | req->analysers = 0; |
| 4902 | 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] | 4903 | |
| 4904 | if (!(s->flags & SN_ERR_MASK)) |
| 4905 | s->flags |= SN_ERR_PRXCOND; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4906 | if (!(s->flags & SN_FINST_MASK)) { |
| 4907 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 4908 | s->flags |= SN_FINST_H; |
| 4909 | else |
| 4910 | s->flags |= SN_FINST_D; |
| 4911 | } |
| 4912 | return 0; |
| 4913 | |
| 4914 | aborted_xfer: |
| 4915 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4916 | if (txn->status) { |
| 4917 | /* Note: we don't send any error if some data were already sent */ |
| 4918 | stream_int_retnclose(req->prod, NULL); |
| 4919 | } else { |
| 4920 | txn->status = 502; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4921 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4922 | } |
| 4923 | req->analysers = 0; |
| 4924 | s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */ |
| 4925 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4926 | s->fe->fe_counters.srv_aborts++; |
| 4927 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4928 | if (objt_server(s->target)) |
| 4929 | objt_server(s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4930 | |
| 4931 | if (!(s->flags & SN_ERR_MASK)) |
| 4932 | s->flags |= SN_ERR_SRVCL; |
| 4933 | if (!(s->flags & SN_FINST_MASK)) { |
| 4934 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 4935 | s->flags |= SN_FINST_H; |
| 4936 | else |
| 4937 | s->flags |= SN_FINST_D; |
| 4938 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4939 | return 0; |
| 4940 | } |
| 4941 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4942 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 4943 | * processing can continue on next analysers, or zero if it either needs more |
| 4944 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
| 4945 | * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers |
| 4946 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 4947 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4948 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 4949 | 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] | 4950 | { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4951 | struct http_txn *txn = &s->txn; |
| 4952 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4953 | struct hdr_ctx ctx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4954 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4955 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4956 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4957 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 4958 | 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] | 4959 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4960 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 4961 | rep, |
| 4962 | rep->rex, rep->wex, |
| 4963 | rep->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4964 | rep->buf->i, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 4965 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 4966 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4967 | /* |
| 4968 | * Now parse the partial (or complete) lines. |
| 4969 | * We will check the response syntax, and also join multi-line |
| 4970 | * headers. An index of all the lines will be elaborated while |
| 4971 | * parsing. |
| 4972 | * |
| 4973 | * For the parsing, we use a 28 states FSM. |
| 4974 | * |
| 4975 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4976 | * rep->buf->p = beginning of response |
| 4977 | * rep->buf->p + msg->eoh = end of processed headers / start of current one |
| 4978 | * rep->buf->p + rep->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 4979 | * msg->eol = end of current header or line (LF or CRLF) |
| 4980 | * msg->next = first non-visited byte |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4981 | */ |
| 4982 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4983 | /* There's a protected area at the end of the buffer for rewriting |
| 4984 | * purposes. We don't want to start to parse the request if the |
| 4985 | * protected area is affected, because we may have to move processed |
| 4986 | * data later, which is much more complicated. |
| 4987 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4988 | if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 4989 | if (unlikely(channel_full(rep) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4990 | bi_end(rep->buf) < b_ptr(rep->buf, msg->next) || |
| 4991 | bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite)) { |
| 4992 | if (rep->buf->o) { |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 4993 | /* some data has still not left the buffer, wake us once that's done */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4994 | if (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] | 4995 | goto abort_response; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4996 | channel_dont_close(rep); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4997 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 4998 | return 0; |
| 4999 | } |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5000 | if (rep->buf->i <= rep->buf->size - global.tune.maxrewrite) |
| 5001 | buffer_slow_realign(msg->chn->buf); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5002 | } |
| 5003 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5004 | if (likely(msg->next < rep->buf->i)) |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 5005 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 5006 | } |
| 5007 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5008 | /* 1: we might have to print this header in debug mode */ |
| 5009 | if (unlikely((global.mode & MODE_DEBUG) && |
| 5010 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5011 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5012 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5013 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5014 | sol = rep->buf->p; |
| 5015 | 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] | 5016 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5017 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5018 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 5019 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5020 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5021 | while (cur_idx) { |
| 5022 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 5023 | debug_hdr("srvhdr", s, sol, eol); |
| 5024 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 5025 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 5026 | } |
| 5027 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5028 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5029 | /* |
| 5030 | * Now we quickly check if we have found a full valid response. |
| 5031 | * If not so, we check the FD and buffer states before leaving. |
| 5032 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5033 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5034 | * responses are checked first. |
| 5035 | * |
| 5036 | * Depending on whether the client is still there or not, we |
| 5037 | * may send an error response back or not. Note that normally |
| 5038 | * we should only check for HTTP status there, and check I/O |
| 5039 | * errors somewhere else. |
| 5040 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5041 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5042 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5043 | /* Invalid response */ |
| 5044 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 5045 | /* we detected a parsing error. We want to archive this response |
| 5046 | * in the dedicated proxy area for later troubleshooting. |
| 5047 | */ |
| 5048 | hdr_response_bad: |
| 5049 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5050 | 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] | 5051 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5052 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5053 | if (objt_server(s->target)) { |
| 5054 | objt_server(s->target)->counters.failed_resp++; |
| 5055 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5056 | } |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 5057 | abort_response: |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5058 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5059 | rep->analysers = 0; |
| 5060 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5061 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 5062 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5063 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5064 | |
| 5065 | if (!(s->flags & SN_ERR_MASK)) |
| 5066 | s->flags |= SN_ERR_PRXCOND; |
| 5067 | if (!(s->flags & SN_FINST_MASK)) |
| 5068 | s->flags |= SN_FINST_H; |
| 5069 | |
| 5070 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5071 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5072 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5073 | /* too large response does not fit in buffer. */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5074 | else if (buffer_full(rep->buf, global.tune.maxrewrite)) { |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 5075 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5076 | msg->err_pos = rep->buf->i; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5077 | goto hdr_response_bad; |
| 5078 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5079 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5080 | /* read error */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5081 | else if (rep->flags & CF_READ_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5082 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5083 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 5084 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5085 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5086 | if (objt_server(s->target)) { |
| 5087 | objt_server(s->target)->counters.failed_resp++; |
| 5088 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5089 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 5090 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5091 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5092 | rep->analysers = 0; |
| 5093 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5094 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 5095 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5096 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 5097 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5098 | if (!(s->flags & SN_ERR_MASK)) |
| 5099 | s->flags |= SN_ERR_SRVCL; |
| 5100 | if (!(s->flags & SN_FINST_MASK)) |
| 5101 | s->flags |= SN_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 5102 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5103 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5104 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5105 | /* read timeout : return a 504 to the client. */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5106 | else if (rep->flags & CF_READ_TIMEOUT) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5107 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5108 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5109 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5110 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5111 | if (objt_server(s->target)) { |
| 5112 | objt_server(s->target)->counters.failed_resp++; |
| 5113 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5114 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5115 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5116 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5117 | rep->analysers = 0; |
| 5118 | txn->status = 504; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5119 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 5120 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5121 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504)); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 5122 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5123 | if (!(s->flags & SN_ERR_MASK)) |
| 5124 | s->flags |= SN_ERR_SRVTO; |
| 5125 | if (!(s->flags & SN_FINST_MASK)) |
| 5126 | s->flags |= SN_FINST_H; |
| 5127 | return 0; |
| 5128 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 5129 | |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5130 | /* client abort with an abortonclose */ |
| 5131 | else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) { |
| 5132 | s->fe->fe_counters.cli_aborts++; |
| 5133 | s->be->be_counters.cli_aborts++; |
| 5134 | if (objt_server(s->target)) |
| 5135 | objt_server(s->target)->counters.cli_aborts++; |
| 5136 | |
| 5137 | rep->analysers = 0; |
| 5138 | channel_auto_close(rep); |
| 5139 | |
| 5140 | txn->status = 400; |
| 5141 | bi_erase(rep); |
| 5142 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400)); |
| 5143 | |
| 5144 | if (!(s->flags & SN_ERR_MASK)) |
| 5145 | s->flags |= SN_ERR_CLICL; |
| 5146 | if (!(s->flags & SN_FINST_MASK)) |
| 5147 | s->flags |= SN_FINST_H; |
| 5148 | |
| 5149 | /* process_session() will take care of the error */ |
| 5150 | return 0; |
| 5151 | } |
| 5152 | |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 5153 | /* 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] | 5154 | else if (rep->flags & CF_SHUTR) { |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 5155 | if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5156 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5157 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5158 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5159 | if (objt_server(s->target)) { |
| 5160 | objt_server(s->target)->counters.failed_resp++; |
| 5161 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5162 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5163 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5164 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5165 | rep->analysers = 0; |
| 5166 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5167 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 5168 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5169 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5170 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5171 | if (!(s->flags & SN_ERR_MASK)) |
| 5172 | s->flags |= SN_ERR_SRVCL; |
| 5173 | if (!(s->flags & SN_FINST_MASK)) |
| 5174 | s->flags |= SN_FINST_H; |
| 5175 | return 0; |
| 5176 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5177 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5178 | /* write error to client (we don't send any message then) */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5179 | else if (rep->flags & CF_WRITE_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5180 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5181 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe); |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 5182 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5183 | s->be->be_counters.failed_resp++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5184 | rep->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5185 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5186 | |
| 5187 | if (!(s->flags & SN_ERR_MASK)) |
| 5188 | s->flags |= SN_ERR_CLICL; |
| 5189 | if (!(s->flags & SN_FINST_MASK)) |
| 5190 | s->flags |= SN_FINST_H; |
| 5191 | |
| 5192 | /* process_session() will take care of the error */ |
| 5193 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5194 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5195 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5196 | channel_dont_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5197 | return 0; |
| 5198 | } |
| 5199 | |
| 5200 | /* More interesting part now : we know that we have a complete |
| 5201 | * response which at least looks like HTTP. We have an indicator |
| 5202 | * of each header's length, so we can parse them quickly. |
| 5203 | */ |
| 5204 | |
| 5205 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5206 | 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] | 5207 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5208 | /* |
| 5209 | * 1: get the status code |
| 5210 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5211 | n = rep->buf->p[msg->sl.st.c] - '0'; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5212 | if (n < 1 || n > 5) |
| 5213 | n = 0; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5214 | /* when the client triggers a 4xx from the server, it's most often due |
| 5215 | * to a missing object or permission. These events should be tracked |
| 5216 | * because if they happen often, it may indicate a brute force or a |
| 5217 | * vulnerability scan. |
| 5218 | */ |
| 5219 | if (n == 4) |
| 5220 | session_inc_http_err_ctr(s); |
| 5221 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5222 | if (objt_server(s->target)) |
| 5223 | objt_server(s->target)->counters.p.http.rsp[n]++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5224 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5225 | /* check if the response is HTTP/1.1 or above */ |
| 5226 | if ((msg->sl.st.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5227 | ((rep->buf->p[5] > '1') || |
| 5228 | ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5229 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5230 | |
| 5231 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 5232 | 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] | 5233 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5234 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5235 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5236 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5237 | 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] | 5238 | |
Willy Tarreau | 3965040 | 2010-03-15 19:44:39 +0100 | [diff] [blame] | 5239 | /* Adjust server's health based on status code. Note: status codes 501 |
| 5240 | * and 505 are triggered on demand by client request, so we must not |
| 5241 | * count them as server failures. |
| 5242 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5243 | if (objt_server(s->target)) { |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5244 | 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] | 5245 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5246 | else |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5247 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5248 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5249 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5250 | /* |
| 5251 | * 2: check for cacheability. |
| 5252 | */ |
| 5253 | |
| 5254 | switch (txn->status) { |
| 5255 | case 200: |
| 5256 | case 203: |
| 5257 | case 206: |
| 5258 | case 300: |
| 5259 | case 301: |
| 5260 | case 410: |
| 5261 | /* RFC2616 @13.4: |
| 5262 | * "A response received with a status code of |
| 5263 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 5264 | * by a cache (...) unless a cache-control |
| 5265 | * directive prohibits caching." |
| 5266 | * |
| 5267 | * RFC2616 @9.5: POST method : |
| 5268 | * "Responses to this method are not cacheable, |
| 5269 | * unless the response includes appropriate |
| 5270 | * Cache-Control or Expires header fields." |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5271 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5272 | if (likely(txn->meth != HTTP_METH_POST) && |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 5273 | ((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] | 5274 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 5275 | break; |
| 5276 | default: |
| 5277 | break; |
| 5278 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5279 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5280 | /* |
| 5281 | * 3: we may need to capture headers |
| 5282 | */ |
| 5283 | s->logs.logwait &= ~LW_RESP; |
Willy Tarreau | 42f7d89 | 2012-03-24 08:28:09 +0100 | [diff] [blame] | 5284 | if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5285 | capture_headers(rep->buf->p, &txn->hdr_idx, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5286 | txn->rsp.cap, s->fe->rsp_cap); |
| 5287 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5288 | /* 4: determine the transfer-length. |
| 5289 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 5290 | * the presence of a message-body in a RESPONSE and its transfer length |
| 5291 | * must be determined that way : |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5292 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5293 | * All responses to the HEAD request method MUST NOT include a |
| 5294 | * message-body, even though the presence of entity-header fields |
| 5295 | * might lead one to believe they do. All 1xx (informational), 204 |
| 5296 | * (No Content), and 304 (Not Modified) responses MUST NOT include a |
| 5297 | * message-body. All other responses do include a message-body, |
| 5298 | * although it MAY be of zero length. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5299 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5300 | * 1. Any response which "MUST NOT" include a message-body (such as the |
| 5301 | * 1xx, 204 and 304 responses and any response to a HEAD request) is |
| 5302 | * always terminated by the first empty line after the header fields, |
| 5303 | * regardless of the entity-header fields present in the message. |
| 5304 | * |
| 5305 | * 2. If a Transfer-Encoding header field (Section 9.7) is present and |
| 5306 | * the "chunked" transfer-coding (Section 6.2) is used, the |
| 5307 | * transfer-length is defined by the use of this transfer-coding. |
| 5308 | * If a Transfer-Encoding header field is present and the "chunked" |
| 5309 | * transfer-coding is not present, the transfer-length is defined by |
| 5310 | * the sender closing the connection. |
| 5311 | * |
| 5312 | * 3. If a Content-Length header field is present, its decimal value in |
| 5313 | * OCTETs represents both the entity-length and the transfer-length. |
| 5314 | * If a message is received with both a Transfer-Encoding header |
| 5315 | * field and a Content-Length header field, the latter MUST be ignored. |
| 5316 | * |
| 5317 | * 4. If the message uses the media type "multipart/byteranges", and |
| 5318 | * the transfer-length is not otherwise specified, then this self- |
| 5319 | * delimiting media type defines the transfer-length. This media |
| 5320 | * type MUST NOT be used unless the sender knows that the recipient |
| 5321 | * can parse it; the presence in a request of a Range header with |
| 5322 | * multiple byte-range specifiers from a 1.1 client implies that the |
| 5323 | * client can parse multipart/byteranges responses. |
| 5324 | * |
| 5325 | * 5. By the server closing the connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5326 | */ |
| 5327 | |
| 5328 | /* Skip parsing if no content length is possible. The response flags |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5329 | * 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] | 5330 | * 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] | 5331 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 5332 | */ |
| 5333 | if (txn->meth == HTTP_METH_HEAD || |
| 5334 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5335 | txn->status == 204 || txn->status == 304) { |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5336 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 9101535 | 2012-11-27 07:31:33 +0100 | [diff] [blame] | 5337 | s->comp_algo = NULL; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5338 | goto skip_content_length; |
| 5339 | } |
| 5340 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5341 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5342 | ctx.idx = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5343 | while ((msg->flags & HTTP_MSGF_VER_11) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5344 | 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] | 5345 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5346 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 5347 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5348 | /* bad transfer-encoding (chunked followed by something else) */ |
| 5349 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5350 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5351 | break; |
| 5352 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5353 | } |
| 5354 | |
| 5355 | /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */ |
| 5356 | ctx.idx = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5357 | while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5358 | 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] | 5359 | signed long long cl; |
| 5360 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5361 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5362 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5363 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5364 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5365 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5366 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5367 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5368 | goto hdr_response_bad; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5369 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5370 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5371 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5372 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5373 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5374 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5375 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5376 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5377 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5378 | goto hdr_response_bad; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5379 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5380 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5381 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5382 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5383 | } |
| 5384 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 5385 | if (s->fe->comp || s->be->comp) |
| 5386 | select_compression_response_header(s, rep->buf); |
| 5387 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5388 | /* FIXME: we should also implement the multipart/byterange method. |
| 5389 | * For now on, we resort to close mode in this case (unknown length). |
| 5390 | */ |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5391 | skip_content_length: |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5392 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5393 | /* end of job, return OK */ |
| 5394 | rep->analysers &= ~an_bit; |
| 5395 | rep->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5396 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5397 | return 1; |
| 5398 | } |
| 5399 | |
| 5400 | /* This function performs all the processing enabled for the current response. |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 5401 | * It normally returns 1 unless it wants to break. It relies on buffers flags, |
| 5402 | * and updates t->rep->analysers. It might make sense to explode it into several |
| 5403 | * other functions. It works like process_request (see indications above). |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5404 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 5405 | 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] | 5406 | { |
| 5407 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5408 | struct http_msg *msg = &txn->rsp; |
| 5409 | struct proxy *cur_proxy; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 5410 | struct cond_wordlist *wl; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5411 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 5412 | 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] | 5413 | now_ms, __FUNCTION__, |
| 5414 | t, |
| 5415 | rep, |
| 5416 | rep->rex, rep->wex, |
| 5417 | rep->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5418 | rep->buf->i, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5419 | rep->analysers); |
| 5420 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5421 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5422 | return 0; |
| 5423 | |
| 5424 | rep->analysers &= ~an_bit; |
| 5425 | rep->analyse_exp = TICK_ETERNITY; |
| 5426 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5427 | /* Now we have to check if we need to modify the Connection header. |
| 5428 | * This is more difficult on the response than it is on the request, |
| 5429 | * because we can have two different HTTP versions and we don't know |
| 5430 | * how the client will interprete a response. For instance, let's say |
| 5431 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 5432 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 5433 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 5434 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 5435 | * the lack of header as a keep-alive. Thus we will use two flags |
| 5436 | * indicating how a request MAY be understood by the client. In case |
| 5437 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 5438 | * ambiguous cases such as both close and keepalive are seen, then we |
| 5439 | * will fall back to explicit close. Note that we won't take risks with |
| 5440 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5441 | * See doc/internals/connection-header.txt for the complete matrix. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5442 | */ |
| 5443 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5444 | if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || |
| 5445 | txn->status == 101)) { |
| 5446 | /* Either we've established an explicit tunnel, or we're |
| 5447 | * switching the protocol. In both cases, we're very unlikely |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5448 | * to understand the next protocols. We have to switch to tunnel |
| 5449 | * mode, so that we transfer the request and responses then let |
| 5450 | * this protocol pass unmodified. When we later implement specific |
| 5451 | * parsers for such protocols, we'll want to check the Upgrade |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5452 | * header which contains information about that protocol for |
| 5453 | * responses with status 101 (eg: see RFC2817 about TLS). |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5454 | */ |
| 5455 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN; |
| 5456 | } |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5457 | else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) && |
| 5458 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN || |
| 5459 | ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5460 | int to_del = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5461 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5462 | /* on unknown transfer length, we must close */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5463 | if (!(msg->flags & HTTP_MSGF_XFER_LEN) && |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5464 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 5465 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5466 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5467 | /* now adjust header transformations depending on current state */ |
| 5468 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN || |
| 5469 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5470 | to_del |= 2; /* remove "keep-alive" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5471 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5472 | to_del |= 1; /* remove "close" for HTTP/1.0 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5473 | } |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5474 | else { /* SCL / KAL */ |
| 5475 | to_del |= 1; /* remove "close" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5476 | if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5477 | to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5478 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5479 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5480 | /* Parse and remove some headers from the connection header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 5481 | http_parse_connection_header(txn, msg, to_del); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5482 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5483 | /* Some keep-alive responses are converted to Server-close if |
| 5484 | * the server wants to close. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5485 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5486 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) { |
| 5487 | if ((txn->flags & TX_HDR_CONN_CLO) || |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5488 | (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11))) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5489 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5490 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5491 | } |
| 5492 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5493 | if (1) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5494 | /* |
| 5495 | * 3: we will have to evaluate the filters. |
| 5496 | * As opposed to version 1.2, now they will be evaluated in the |
| 5497 | * filters order and not in the header order. This means that |
| 5498 | * each filter has to be validated among all headers. |
| 5499 | * |
| 5500 | * Filters are tried with ->be first, then with ->fe if it is |
| 5501 | * different from ->be. |
| 5502 | */ |
| 5503 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5504 | cur_proxy = t->be; |
| 5505 | while (1) { |
| 5506 | struct proxy *rule_set = cur_proxy; |
| 5507 | |
| 5508 | /* try headers filters */ |
| 5509 | if (rule_set->rsp_exp != NULL) { |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 5510 | if (apply_filters_to_response(t, rep, rule_set) < 0) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5511 | return_bad_resp: |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5512 | if (objt_server(t->target)) { |
| 5513 | objt_server(t->target)->counters.failed_resp++; |
| 5514 | health_adjust(objt_server(t->target), HANA_STATUS_HTTP_RSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5515 | } |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5516 | t->be->be_counters.failed_resp++; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5517 | return_srv_prx_502: |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 5518 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5519 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5520 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 5521 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5522 | stream_int_retnclose(rep->cons, http_error_message(t, HTTP_ERR_502)); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5523 | if (!(t->flags & SN_ERR_MASK)) |
| 5524 | t->flags |= SN_ERR_PRXCOND; |
| 5525 | if (!(t->flags & SN_FINST_MASK)) |
| 5526 | t->flags |= SN_FINST_H; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 5527 | return 0; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5528 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5529 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5530 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5531 | /* has the response been denied ? */ |
| 5532 | if (txn->flags & TX_SVDENY) { |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5533 | if (objt_server(t->target)) |
| 5534 | objt_server(t->target)->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5535 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5536 | t->be->be_counters.denied_resp++; |
| 5537 | t->fe->fe_counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5538 | if (t->listener->counters) |
| 5539 | t->listener->counters->denied_resp++; |
| 5540 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5541 | goto return_srv_prx_502; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 5542 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5543 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5544 | /* add response headers from the rule sets in the same order */ |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 5545 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 5546 | if (txn->status < 200) |
| 5547 | break; |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 5548 | if (wl->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 5549 | 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] | 5550 | ret = acl_pass(ret); |
| 5551 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 5552 | ret = !ret; |
| 5553 | if (!ret) |
| 5554 | continue; |
| 5555 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 5556 | 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] | 5557 | goto return_bad_resp; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 5558 | } |
| 5559 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5560 | /* check whether we're already working on the frontend */ |
| 5561 | if (cur_proxy == t->fe) |
| 5562 | break; |
| 5563 | cur_proxy = t->fe; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5564 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5565 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5566 | /* |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5567 | * We may be facing a 100-continue response, in which case this |
| 5568 | * is not the right response, and we're waiting for the next one. |
| 5569 | * 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] | 5570 | * next one. |
| 5571 | */ |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5572 | if (unlikely(txn->status == 100)) { |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5573 | hdr_idx_init(&txn->hdr_idx); |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5574 | msg->next -= channel_forward(rep, msg->next); |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5575 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 5576 | txn->status = 0; |
| 5577 | rep->analysers |= AN_RES_WAIT_HTTP | an_bit; |
| 5578 | return 1; |
| 5579 | } |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5580 | else if (unlikely(txn->status < 200)) |
| 5581 | goto skip_header_mangling; |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5582 | |
| 5583 | /* we don't have any 1xx status code now */ |
| 5584 | |
| 5585 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5586 | * 4: check for server cookie. |
| 5587 | */ |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5588 | if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name || |
| 5589 | (t->be->options & PR_O_CHK_CACHE)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5590 | manage_server_side_cookies(t, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5591 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5592 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5593 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5594 | * 5: check for cache-control or pragma headers if required. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5595 | */ |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 5596 | 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] | 5597 | check_response_for_cacheability(t, rep); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5598 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5599 | /* |
| 5600 | * 6: add server cookie in the response if needed |
| 5601 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5602 | if (objt_server(t->target) && (t->be->ck_opts & PR_CK_INS) && |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 5603 | !((txn->flags & TX_SCK_FOUND) && (t->be->ck_opts & PR_CK_PSV)) && |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5604 | (!(t->flags & SN_DIRECT) || |
| 5605 | ((t->be->cookie_maxidle || txn->cookie_last_date) && |
| 5606 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 5607 | (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 5608 | (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 5609 | (!(t->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) && |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 5610 | !(t->flags & SN_IGNORE_PRST)) { |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5611 | /* the server is known, it's not the one the client requested, or the |
| 5612 | * cookie's last seen date needs to be refreshed. We have to |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5613 | * insert a set-cookie here, except if we want to insert only on POST |
| 5614 | * requests and this one isn't. Note that servers which don't have cookies |
| 5615 | * (eg: some backup servers) will return a full cookie removal request. |
| 5616 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5617 | if (!objt_server(t->target)->cookie) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5618 | chunk_printf(&trash, |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5619 | "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
| 5620 | t->be->cookie_name); |
| 5621 | } |
| 5622 | else { |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5623 | 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] | 5624 | |
| 5625 | if (t->be->cookie_maxidle || t->be->cookie_maxlife) { |
| 5626 | /* emit last_date, which is mandatory */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5627 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
| 5628 | s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len); |
| 5629 | trash.len += 5; |
| 5630 | |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5631 | if (t->be->cookie_maxlife) { |
| 5632 | /* emit first_date, which is either the original one or |
| 5633 | * the current date. |
| 5634 | */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5635 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5636 | s30tob64(txn->cookie_first_date ? |
| 5637 | txn->cookie_first_date >> 2 : |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5638 | (date.tv_sec+3) >> 2, trash.str + trash.len); |
| 5639 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5640 | } |
| 5641 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5642 | chunk_appendf(&trash, "; path=/"); |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5643 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5644 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5645 | if (t->be->cookie_domain) |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5646 | chunk_appendf(&trash, "; domain=%s", t->be->cookie_domain); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5647 | |
Willy Tarreau | 4992dd2 | 2012-05-31 21:02:17 +0200 | [diff] [blame] | 5648 | if (t->be->ck_opts & PR_CK_HTTPONLY) |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5649 | chunk_appendf(&trash, "; HttpOnly"); |
Willy Tarreau | 4992dd2 | 2012-05-31 21:02:17 +0200 | [diff] [blame] | 5650 | |
| 5651 | if (t->be->ck_opts & PR_CK_SECURE) |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5652 | chunk_appendf(&trash, "; Secure"); |
Willy Tarreau | 4992dd2 | 2012-05-31 21:02:17 +0200 | [diff] [blame] | 5653 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5654 | 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] | 5655 | goto return_bad_resp; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5656 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 5657 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5658 | if (objt_server(t->target)->cookie && (t->flags & SN_DIRECT)) |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5659 | /* the server did not change, only the date was updated */ |
| 5660 | txn->flags |= TX_SCK_UPDATED; |
| 5661 | else |
| 5662 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5663 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5664 | /* Here, we will tell an eventual cache on the client side that we don't |
| 5665 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 5666 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 5667 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 5668 | */ |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 5669 | if ((t->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5670 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5671 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 5672 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 5673 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 5674 | "Cache-control: private", 22) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5675 | goto return_bad_resp; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5676 | } |
| 5677 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5678 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5679 | /* |
| 5680 | * 7: check if result will be cacheable with a cookie. |
| 5681 | * We'll block the response if security checks have caught |
| 5682 | * nasty things such as a cacheable cookie. |
| 5683 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 5684 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 5685 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5686 | (t->be->options & PR_O_CHK_CACHE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5687 | |
| 5688 | /* we're in presence of a cacheable response containing |
| 5689 | * a set-cookie header. We'll block it as requested by |
| 5690 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5691 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5692 | if (objt_server(t->target)) |
| 5693 | objt_server(t->target)->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5694 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5695 | t->be->be_counters.denied_resp++; |
| 5696 | t->fe->fe_counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5697 | if (t->listener->counters) |
| 5698 | t->listener->counters->denied_resp++; |
| 5699 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5700 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5701 | 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] | 5702 | send_log(t->be, LOG_ALERT, |
| 5703 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5704 | 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] | 5705 | goto return_srv_prx_502; |
| 5706 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5707 | |
| 5708 | /* |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5709 | * 8: adjust "Connection: close" or "Connection: keep-alive" if needed. |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 5710 | * If an "Upgrade" token is found, the header is left untouched in order |
| 5711 | * not to have to deal with some client bugs : some of them fail an upgrade |
| 5712 | * if anything but "Upgrade" is present in the Connection header. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5713 | */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 5714 | if (!(txn->flags & TX_HDR_CONN_UPG) && |
| 5715 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
| 5716 | ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5717 | unsigned int want_flags = 0; |
| 5718 | |
| 5719 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5720 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5721 | /* we want a keep-alive response here. Keep-alive header |
| 5722 | * required if either side is not 1.1. |
| 5723 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5724 | if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5725 | want_flags |= TX_CON_KAL_SET; |
| 5726 | } |
| 5727 | else { |
| 5728 | /* we want a close response here. Close header required if |
| 5729 | * the server is 1.1, regardless of the client. |
| 5730 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5731 | if (msg->flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5732 | want_flags |= TX_CON_CLO_SET; |
| 5733 | } |
| 5734 | |
| 5735 | 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] | 5736 | http_change_connection_header(txn, msg, want_flags); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5737 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5738 | |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5739 | skip_header_mangling: |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5740 | if ((msg->flags & HTTP_MSGF_XFER_LEN) || |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5741 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5742 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 5743 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5744 | /************************************************************* |
| 5745 | * OK, that's finished for the headers. We have done what we * |
| 5746 | * could. Let's switch to the DATA state. * |
| 5747 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5748 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5749 | t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5750 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5751 | /* if the user wants to log as soon as possible, without counting |
| 5752 | * bytes from the server, then this is the right moment. We have |
| 5753 | * to temporarily assign bytes_out to log what we currently have. |
| 5754 | */ |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 5755 | if (!LIST_ISEMPTY(&t->fe->logformat) && !(t->logs.logwait & LW_BYTES)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5756 | t->logs.t_close = t->logs.t_data; /* to get a valid end date */ |
| 5757 | t->logs.bytes_out = txn->rsp.eoh; |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 5758 | t->do_log(t); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5759 | t->logs.bytes_out = 0; |
| 5760 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5761 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5762 | /* Note: we must not try to cheat by jumping directly to DATA, |
| 5763 | * otherwise we would not let the client side wake up. |
| 5764 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5765 | |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 5766 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5767 | } |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 5768 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5769 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5770 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5771 | /* This function is an analyser which forwards response body (including chunk |
| 5772 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 5773 | * zero byte. The only situation where it must not be called is when we're in |
| 5774 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 5775 | * remaining data and to resync after end of body. It expects the msg_state to |
| 5776 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 5777 | * 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] | 5778 | * 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] | 5779 | * bytes of pending data + the headers if not already done (between sol and sov). |
| 5780 | * 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] | 5781 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 5782 | 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] | 5783 | { |
| 5784 | struct http_txn *txn = &s->txn; |
| 5785 | struct http_msg *msg = &s->txn.rsp; |
Willy Tarreau | ea95316 | 2012-05-18 23:41:28 +0200 | [diff] [blame] | 5786 | unsigned int bytes; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 5787 | static struct buffer *tmpbuf = NULL; |
| 5788 | int compressing = 0; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 5789 | int consumed_data = 0; |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 5790 | int ret; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5791 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5792 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 5793 | return 0; |
| 5794 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5795 | 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] | 5796 | ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) || |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 5797 | !s->req->analysers) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5798 | /* Output closed while we were sending data. We must abort and |
| 5799 | * wake the other side up. |
| 5800 | */ |
| 5801 | msg->msg_state = HTTP_MSG_ERROR; |
| 5802 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 5803 | return 1; |
| 5804 | } |
| 5805 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5806 | /* in most states, we should abort in case of early close */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5807 | channel_auto_close(res); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5808 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 5809 | /* this is the first time we need the compression buffer */ |
| 5810 | if (s->comp_algo != NULL && tmpbuf == NULL) { |
| 5811 | if ((tmpbuf = pool_alloc2(pool2_buffer)) == NULL) |
| 5812 | goto aborted_xfer; /* no memory */ |
| 5813 | } |
| 5814 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5815 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 5816 | /* 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] | 5817 | * rep->buf.p still points to the beginning of the message and msg->sol |
| 5818 | * is still null. We forward the headers, we don't need them. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5819 | */ |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 5820 | channel_forward(res, msg->sov); |
| 5821 | msg->next = 0; |
| 5822 | msg->sov = 0; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 5823 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5824 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5825 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5826 | else |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5827 | msg->msg_state = HTTP_MSG_DATA; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5828 | } |
| 5829 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 5830 | if (s->comp_algo != NULL) { |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 5831 | ret = http_compression_buffer_init(s, res->buf, tmpbuf); /* init a buffer with headers */ |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 5832 | if (ret < 0) |
| 5833 | goto missing_data; /* not enough spaces in buffers */ |
| 5834 | compressing = 1; |
| 5835 | } |
| 5836 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5837 | while (1) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5838 | http_silent_debug(__LINE__, s); |
Willy Tarreau | ea95316 | 2012-05-18 23:41:28 +0200 | [diff] [blame] | 5839 | /* we may have some data pending between sol and sov */ |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 5840 | if (s->comp_algo == NULL) { |
| 5841 | bytes = msg->sov - msg->sol; |
| 5842 | if (msg->chunk_len || bytes) { |
| 5843 | msg->sol = msg->sov; |
| 5844 | msg->next -= bytes; /* will be forwarded */ |
| 5845 | msg->chunk_len += bytes; |
| 5846 | msg->chunk_len -= channel_forward(res, msg->chunk_len); |
| 5847 | } |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 5848 | } |
| 5849 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 5850 | switch (msg->msg_state - HTTP_MSG_DATA) { |
| 5851 | case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */ |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 5852 | if (compressing) { |
| 5853 | consumed_data += ret = http_compression_buffer_add_data(s, res->buf, tmpbuf); |
| 5854 | if (ret < 0) |
| 5855 | goto aborted_xfer; |
| 5856 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 5857 | |
| 5858 | if (res->to_forward || msg->chunk_len) |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5859 | goto missing_data; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5860 | |
| 5861 | /* nothing left to forward */ |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 5862 | if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5863 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 5864 | } else { |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5865 | msg->msg_state = HTTP_MSG_DONE; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 5866 | if (compressing && consumed_data) { |
| 5867 | http_compression_buffer_end(s, &res->buf, &tmpbuf, 1); |
| 5868 | compressing = 0; |
| 5869 | } |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 5870 | break; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 5871 | } |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 5872 | /* fall through for HTTP_MSG_CHUNK_CRLF */ |
| 5873 | |
| 5874 | case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA: |
| 5875 | /* we want the CRLF after the data */ |
| 5876 | |
| 5877 | ret = http_skip_chunk_crlf(msg); |
| 5878 | if (ret == 0) |
| 5879 | goto missing_data; |
| 5880 | else if (ret < 0) { |
| 5881 | if (msg->err_pos >= 0) |
| 5882 | http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe); |
| 5883 | goto return_bad_res; |
| 5884 | } |
| 5885 | /* skipping data in buffer for compression */ |
| 5886 | if (compressing) { |
| 5887 | b_adv(res->buf, msg->next); |
| 5888 | msg->next = 0; |
| 5889 | msg->sov = 0; |
| 5890 | msg->sol = 0; |
| 5891 | } |
| 5892 | /* we're in MSG_CHUNK_SIZE now, fall through */ |
| 5893 | |
| 5894 | case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA: |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5895 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 5896 | * set ->sov and ->next to point to the body and switch to DATA or |
| 5897 | * TRAILERS state. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5898 | */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5899 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 5900 | ret = http_parse_chunk_size(msg); |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5901 | if (ret == 0) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5902 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5903 | else if (ret < 0) { |
| 5904 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5905 | 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] | 5906 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5907 | } |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 5908 | if (compressing) { |
| 5909 | if (likely(msg->chunk_len > 0)) { |
| 5910 | /* skipping data if we are in compression mode */ |
| 5911 | b_adv(res->buf, msg->next); |
| 5912 | msg->next = 0; |
| 5913 | msg->sov = 0; |
| 5914 | msg->sol = 0; |
| 5915 | } else { |
| 5916 | if (consumed_data) { |
| 5917 | http_compression_buffer_end(s, &res->buf, &tmpbuf, 1); |
| 5918 | compressing = 0; |
| 5919 | } |
| 5920 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 5921 | } |
Willy Tarreau | 0161d62 | 2013-04-02 01:26:55 +0200 | [diff] [blame] | 5922 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 5923 | break; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5924 | |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 5925 | case HTTP_MSG_TRAILERS - HTTP_MSG_DATA: |
| 5926 | ret = http_forward_trailers(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5927 | if (ret == 0) |
| 5928 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5929 | else if (ret < 0) { |
| 5930 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5931 | 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] | 5932 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5933 | } |
William Lallemand | 00bf1de | 2012-11-22 17:55:14 +0100 | [diff] [blame] | 5934 | if (s->comp_algo != NULL) { |
| 5935 | /* forwarding trailers */ |
| 5936 | channel_forward(res, msg->next); |
| 5937 | msg->next = 0; |
| 5938 | } |
Willy Tarreau | 2d43e18 | 2013-04-03 00:22:25 +0200 | [diff] [blame] | 5939 | /* we're in HTTP_MSG_DONE now, but we might still have |
| 5940 | * some data pending, so let's loop over once. |
| 5941 | */ |
| 5942 | break; |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 5943 | |
| 5944 | default: |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5945 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 5946 | |
| 5947 | ret = msg->msg_state; |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5948 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5949 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5950 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5951 | channel_dont_close(res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5952 | if (http_resync_states(s)) { |
| 5953 | http_silent_debug(__LINE__, s); |
| 5954 | /* some state changes occurred, maybe the analyser |
| 5955 | * was disabled too. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5956 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5957 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5958 | if (res->flags & CF_SHUTW) { |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5959 | /* response errors are most likely due to |
| 5960 | * the client aborting the transfer. |
| 5961 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5962 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5963 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5964 | if (msg->err_pos >= 0) |
Willy Tarreau | d655ffe | 2013-04-02 01:48:58 +0200 | [diff] [blame] | 5965 | 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] | 5966 | goto return_bad_res; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5967 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5968 | return 1; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5969 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5970 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5971 | } |
| 5972 | } |
| 5973 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5974 | missing_data: |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 5975 | if (compressing && consumed_data) { |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 5976 | http_compression_buffer_end(s, &res->buf, &tmpbuf, 0); |
| 5977 | compressing = 0; |
| 5978 | } |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5979 | |
| 5980 | if (res->flags & CF_SHUTW) |
| 5981 | goto aborted_xfer; |
| 5982 | |
| 5983 | /* stop waiting for data if the input is closed before the end. If the |
| 5984 | * client side was already closed, it means that the client has aborted, |
| 5985 | * so we don't want to count this as a server abort. Otherwise it's a |
| 5986 | * server abort. |
| 5987 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5988 | if (res->flags & CF_SHUTR) { |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5989 | if ((res->flags & CF_SHUTW_NOW) || (s->req->flags & CF_SHUTR)) |
| 5990 | goto aborted_xfer; |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5991 | if (!(s->flags & SN_ERR_MASK)) |
| 5992 | s->flags |= SN_ERR_SRVCL; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5993 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5994 | if (objt_server(s->target)) |
| 5995 | objt_server(s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5996 | goto return_bad_res_stats_ok; |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5997 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5998 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5999 | /* 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] | 6000 | if (!s->req->analysers) |
| 6001 | goto return_bad_res; |
| 6002 | |
Willy Tarreau | ea95316 | 2012-05-18 23:41:28 +0200 | [diff] [blame] | 6003 | /* forward any data pending between sol and sov */ |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 6004 | if (s->comp_algo == NULL) { |
| 6005 | bytes = msg->sov - msg->sol; |
| 6006 | if (msg->chunk_len || bytes) { |
| 6007 | msg->sol = msg->sov; |
| 6008 | msg->next -= bytes; /* will be forwarded */ |
| 6009 | msg->chunk_len += bytes; |
| 6010 | msg->chunk_len -= channel_forward(res, msg->chunk_len); |
| 6011 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6012 | } |
| 6013 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6014 | /* 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] | 6015 | * 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] | 6016 | * Similarly, with keep-alive on the client side, we don't want to forward a |
| 6017 | * close. |
| 6018 | */ |
Willy Tarreau | 08b4d79 | 2012-10-27 01:36:34 +0200 | [diff] [blame] | 6019 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo || |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6020 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 6021 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 6022 | channel_dont_close(res); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 6023 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6024 | /* 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] | 6025 | * 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] | 6026 | * 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] | 6027 | * modes are already handled by the stream sock layer. We must not do |
| 6028 | * this in content-length mode because it could present the MSG_MORE |
| 6029 | * flag with the last block of forwarded data, which would cause an |
| 6030 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6031 | */ |
Willy Tarreau | 08b4d79 | 2012-10-27 01:36:34 +0200 | [diff] [blame] | 6032 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 6033 | res->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 6034 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6035 | /* the session handler will take care of timeouts and errors */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 6036 | http_silent_debug(__LINE__, s); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6037 | return 0; |
| 6038 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6039 | return_bad_res: /* let's centralize all bad responses */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6040 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6041 | if (objt_server(s->target)) |
| 6042 | objt_server(s->target)->counters.failed_resp++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6043 | |
| 6044 | return_bad_res_stats_ok: |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6045 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 6046 | /* don't send any error message as we're in the body */ |
| 6047 | stream_int_retnclose(res->cons, NULL); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6048 | res->analysers = 0; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6049 | 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] | 6050 | if (objt_server(s->target)) |
| 6051 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6052 | |
| 6053 | if (!(s->flags & SN_ERR_MASK)) |
| 6054 | s->flags |= SN_ERR_PRXCOND; |
| 6055 | if (!(s->flags & SN_FINST_MASK)) |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 6056 | s->flags |= SN_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6057 | return 0; |
| 6058 | |
| 6059 | aborted_xfer: |
| 6060 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 6061 | /* don't send any error message as we're in the body */ |
| 6062 | stream_int_retnclose(res->cons, NULL); |
| 6063 | res->analysers = 0; |
| 6064 | s->req->analysers = 0; /* we're in data phase, we want to abort both directions */ |
| 6065 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6066 | s->fe->fe_counters.cli_aborts++; |
| 6067 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6068 | if (objt_server(s->target)) |
| 6069 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 6070 | |
| 6071 | if (!(s->flags & SN_ERR_MASK)) |
| 6072 | s->flags |= SN_ERR_CLICL; |
| 6073 | if (!(s->flags & SN_FINST_MASK)) |
| 6074 | s->flags |= SN_FINST_D; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 6075 | return 0; |
| 6076 | } |
| 6077 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6078 | /* Iterate the same filter through all request headers. |
| 6079 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6080 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 6081 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6082 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 6083 | 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] | 6084 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6085 | char term; |
| 6086 | char *cur_ptr, *cur_end, *cur_next; |
| 6087 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6088 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6089 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6090 | int delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 6091 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6092 | last_hdr = 0; |
| 6093 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6094 | cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6095 | old_idx = 0; |
| 6096 | |
| 6097 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6098 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6099 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6100 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6101 | (exp->action == ACT_ALLOW || |
| 6102 | exp->action == ACT_DENY || |
| 6103 | exp->action == ACT_TARPIT)) |
| 6104 | return 0; |
| 6105 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6106 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6107 | if (!cur_idx) |
| 6108 | break; |
| 6109 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6110 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6111 | cur_ptr = cur_next; |
| 6112 | cur_end = cur_ptr + cur_hdr->len; |
| 6113 | cur_next = cur_end + cur_hdr->cr + 1; |
| 6114 | |
| 6115 | /* Now we have one header between cur_ptr and cur_end, |
| 6116 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6117 | */ |
| 6118 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6119 | /* The annoying part is that pattern matching needs |
| 6120 | * that we modify the contents to null-terminate all |
| 6121 | * strings before testing them. |
| 6122 | */ |
| 6123 | |
| 6124 | term = *cur_end; |
| 6125 | *cur_end = '\0'; |
| 6126 | |
| 6127 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 6128 | switch (exp->action) { |
| 6129 | case ACT_SETBE: |
| 6130 | /* It is not possible to jump a second time. |
| 6131 | * FIXME: should we return an HTTP/500 here so that |
| 6132 | * the admin knows there's a problem ? |
| 6133 | */ |
| 6134 | if (t->be != t->fe) |
| 6135 | break; |
| 6136 | |
| 6137 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 6138 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6139 | last_hdr = 1; |
| 6140 | break; |
| 6141 | |
| 6142 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6143 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6144 | last_hdr = 1; |
| 6145 | break; |
| 6146 | |
| 6147 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6148 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6149 | last_hdr = 1; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6150 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6151 | t->fe->fe_counters.denied_req++; |
| 6152 | if (t->fe != t->be) |
| 6153 | t->be->be_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6154 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 6155 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6156 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6157 | break; |
| 6158 | |
| 6159 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6160 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6161 | last_hdr = 1; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6162 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6163 | t->fe->fe_counters.denied_req++; |
| 6164 | if (t->fe != t->be) |
| 6165 | t->be->be_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6166 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 6167 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6168 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6169 | break; |
| 6170 | |
| 6171 | case ACT_REPLACE: |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6172 | trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch); |
| 6173 | 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] | 6174 | /* FIXME: if the user adds a newline in the replacement, the |
| 6175 | * index will not be recalculated for now, and the new line |
| 6176 | * will not be counted as a new header. |
| 6177 | */ |
| 6178 | |
| 6179 | cur_end += delta; |
| 6180 | cur_next += delta; |
| 6181 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6182 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6183 | break; |
| 6184 | |
| 6185 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6186 | delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6187 | cur_next += delta; |
| 6188 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6189 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6190 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6191 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6192 | cur_hdr->len = 0; |
| 6193 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6194 | cur_idx = old_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6195 | break; |
| 6196 | |
| 6197 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6198 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6199 | if (cur_end) |
| 6200 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6201 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6202 | /* keep the link from this header to next one in case of later |
| 6203 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6204 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6205 | old_idx = cur_idx; |
| 6206 | } |
| 6207 | return 0; |
| 6208 | } |
| 6209 | |
| 6210 | |
| 6211 | /* Apply the filter to the request line. |
| 6212 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 6213 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6214 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 6215 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6216 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 6217 | 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] | 6218 | { |
| 6219 | char term; |
| 6220 | char *cur_ptr, *cur_end; |
| 6221 | int done; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6222 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6223 | int delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6224 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6225 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6226 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6227 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6228 | (exp->action == ACT_ALLOW || |
| 6229 | exp->action == ACT_DENY || |
| 6230 | exp->action == ACT_TARPIT)) |
| 6231 | return 0; |
| 6232 | else if (exp->action == ACT_REMOVE) |
| 6233 | return 0; |
| 6234 | |
| 6235 | done = 0; |
| 6236 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6237 | cur_ptr = req->buf->p; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6238 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6239 | |
| 6240 | /* Now we have the request line between cur_ptr and cur_end */ |
| 6241 | |
| 6242 | /* The annoying part is that pattern matching needs |
| 6243 | * that we modify the contents to null-terminate all |
| 6244 | * strings before testing them. |
| 6245 | */ |
| 6246 | |
| 6247 | term = *cur_end; |
| 6248 | *cur_end = '\0'; |
| 6249 | |
| 6250 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 6251 | switch (exp->action) { |
| 6252 | case ACT_SETBE: |
| 6253 | /* It is not possible to jump a second time. |
| 6254 | * FIXME: should we return an HTTP/500 here so that |
| 6255 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6256 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6257 | if (t->be != t->fe) |
| 6258 | break; |
| 6259 | |
| 6260 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 6261 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6262 | done = 1; |
| 6263 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6264 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6265 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6266 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6267 | done = 1; |
| 6268 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6269 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6270 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6271 | txn->flags |= TX_CLDENY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6272 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6273 | t->fe->fe_counters.denied_req++; |
| 6274 | if (t->fe != t->be) |
| 6275 | t->be->be_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6276 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 6277 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6278 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6279 | done = 1; |
| 6280 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6281 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6282 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6283 | txn->flags |= TX_CLTARPIT; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6284 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6285 | t->fe->fe_counters.denied_req++; |
| 6286 | if (t->fe != t->be) |
| 6287 | t->be->be_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6288 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 6289 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6290 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6291 | done = 1; |
| 6292 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6293 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6294 | case ACT_REPLACE: |
| 6295 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6296 | trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch); |
| 6297 | 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] | 6298 | /* FIXME: if the user adds a newline in the replacement, the |
| 6299 | * index will not be recalculated for now, and the new line |
| 6300 | * will not be counted as a new header. |
| 6301 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6302 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6303 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6304 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 6305 | cur_end = (char *)http_parse_reqline(&txn->req, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6306 | HTTP_MSG_RQMETH, |
| 6307 | cur_ptr, cur_end + 1, |
| 6308 | NULL, NULL); |
| 6309 | if (unlikely(!cur_end)) |
| 6310 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6311 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6312 | /* we have a full request and we know that we have either a CR |
| 6313 | * or an LF at <ptr>. |
| 6314 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6315 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 6316 | 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] | 6317 | /* there is no point trying this regex on headers */ |
| 6318 | return 1; |
| 6319 | } |
| 6320 | } |
| 6321 | *cur_end = term; /* restore the string terminator */ |
| 6322 | return done; |
| 6323 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 6324 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6325 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6326 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6327 | /* |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6328 | * 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] | 6329 | * 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] | 6330 | * unparsable request. Since it can manage the switch to another backend, it |
| 6331 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6332 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 6333 | 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] | 6334 | { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6335 | struct http_txn *txn = &s->txn; |
| 6336 | struct hdr_exp *exp; |
| 6337 | |
| 6338 | for (exp = px->req_exp; exp; exp = exp->next) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6339 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6340 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6341 | /* |
| 6342 | * The interleaving of transformations and verdicts |
| 6343 | * makes it difficult to decide to continue or stop |
| 6344 | * the evaluation. |
| 6345 | */ |
| 6346 | |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6347 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) |
| 6348 | break; |
| 6349 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6350 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6351 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6352 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6353 | continue; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6354 | |
| 6355 | /* if this filter had a condition, evaluate it now and skip to |
| 6356 | * next filter if the condition does not match. |
| 6357 | */ |
| 6358 | if (exp->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 6359 | 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] | 6360 | ret = acl_pass(ret); |
| 6361 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 6362 | ret = !ret; |
| 6363 | |
| 6364 | if (!ret) |
| 6365 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6366 | } |
| 6367 | |
| 6368 | /* Apply the filter to the request line. */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6369 | ret = apply_filter_to_req_line(s, req, exp); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6370 | if (unlikely(ret < 0)) |
| 6371 | return -1; |
| 6372 | |
| 6373 | if (likely(ret == 0)) { |
| 6374 | /* The filter did not match the request, it can be |
| 6375 | * iterated through all headers. |
| 6376 | */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6377 | apply_filter_to_req_headers(s, req, exp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6378 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6379 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6380 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6381 | } |
| 6382 | |
| 6383 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6384 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6385 | /* |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6386 | * Try to retrieve the server associated to the appsession. |
| 6387 | * If the server is found, it's assigned to the session. |
| 6388 | */ |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6389 | 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] | 6390 | struct http_txn *txn = &t->txn; |
| 6391 | appsess *asession = NULL; |
| 6392 | char *sessid_temp = NULL; |
| 6393 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6394 | if (len > t->be->appsession_len) { |
| 6395 | len = t->be->appsession_len; |
| 6396 | } |
| 6397 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6398 | if (t->be->options2 & PR_O2_AS_REQL) { |
| 6399 | /* 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] | 6400 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6401 | /* 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] | 6402 | pool_free2(apools.sessid, txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6403 | } |
| 6404 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6405 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6406 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 6407 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 6408 | return; |
| 6409 | } |
| 6410 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6411 | memcpy(txn->sessid, buf, len); |
| 6412 | txn->sessid[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6413 | } |
| 6414 | |
| 6415 | if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) { |
| 6416 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 6417 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 6418 | return; |
| 6419 | } |
| 6420 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6421 | memcpy(sessid_temp, buf, len); |
| 6422 | sessid_temp[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6423 | |
| 6424 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp); |
| 6425 | /* free previously allocated memory */ |
| 6426 | pool_free2(apools.sessid, sessid_temp); |
| 6427 | |
| 6428 | if (asession != NULL) { |
| 6429 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 6430 | if (!(t->be->options2 & PR_O2_AS_REQL)) |
| 6431 | asession->request_count++; |
| 6432 | |
| 6433 | if (asession->serverid != NULL) { |
| 6434 | struct server *srv = t->be->srv; |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 6435 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6436 | while (srv) { |
| 6437 | if (strcmp(srv->id, asession->serverid) == 0) { |
Willy Tarreau | 4de9149 | 2010-01-22 19:10:05 +0100 | [diff] [blame] | 6438 | if ((srv->state & SRV_RUNNING) || |
| 6439 | (t->be->options & PR_O_PERSIST) || |
| 6440 | (t->flags & SN_FORCE_PRST)) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6441 | /* we found the server and it's usable */ |
| 6442 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | 2a6d88d | 2010-01-24 13:10:43 +0100 | [diff] [blame] | 6443 | txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6444 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6445 | t->target = &srv->obj_type; |
Willy Tarreau | 664beb8 | 2011-03-10 11:38:29 +0100 | [diff] [blame] | 6446 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6447 | break; |
| 6448 | } else { |
| 6449 | txn->flags &= ~TX_CK_MASK; |
| 6450 | txn->flags |= TX_CK_DOWN; |
| 6451 | } |
| 6452 | } |
| 6453 | srv = srv->next; |
| 6454 | } |
| 6455 | } |
| 6456 | } |
| 6457 | } |
| 6458 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6459 | /* Find the end of a cookie value contained between <s> and <e>. It works the |
| 6460 | * same way as with headers above except that the semi-colon also ends a token. |
| 6461 | * See RFC2965 for more information. Note that it requires a valid header to |
| 6462 | * return a valid result. |
| 6463 | */ |
| 6464 | char *find_cookie_value_end(char *s, const char *e) |
| 6465 | { |
| 6466 | int quoted, qdpair; |
| 6467 | |
| 6468 | quoted = qdpair = 0; |
| 6469 | for (; s < e; s++) { |
| 6470 | if (qdpair) qdpair = 0; |
| 6471 | else if (quoted) { |
| 6472 | if (*s == '\\') qdpair = 1; |
| 6473 | else if (*s == '"') quoted = 0; |
| 6474 | } |
| 6475 | else if (*s == '"') quoted = 1; |
| 6476 | else if (*s == ',' || *s == ';') return s; |
| 6477 | } |
| 6478 | return s; |
| 6479 | } |
| 6480 | |
| 6481 | /* Delete a value in a header between delimiters <from> and <next> in buffer |
| 6482 | * <buf>. The number of characters displaced is returned, and the pointer to |
| 6483 | * the first delimiter is updated if required. The function tries as much as |
| 6484 | * possible to respect the following principles : |
| 6485 | * - replace <from> delimiter by the <next> one unless <from> points to a |
| 6486 | * colon, in which case <next> is simply removed |
| 6487 | * - set exactly one space character after the new first delimiter, unless |
| 6488 | * there are not enough characters in the block being moved to do so. |
| 6489 | * - remove unneeded spaces before the previous delimiter and after the new |
| 6490 | * one. |
| 6491 | * |
| 6492 | * It is the caller's responsibility to ensure that : |
| 6493 | * - <from> points to a valid delimiter or the colon ; |
| 6494 | * - <next> points to a valid delimiter or the final CR/LF ; |
| 6495 | * - there are non-space chars before <from> ; |
| 6496 | * - there is a CR/LF at or after <next>. |
| 6497 | */ |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 6498 | int del_hdr_value(struct buffer *buf, char **from, char *next) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6499 | { |
| 6500 | char *prev = *from; |
| 6501 | |
| 6502 | if (*prev == ':') { |
| 6503 | /* We're removing the first value, preserve the colon and add a |
| 6504 | * space if possible. |
| 6505 | */ |
| 6506 | if (!http_is_crlf[(unsigned char)*next]) |
| 6507 | next++; |
| 6508 | prev++; |
| 6509 | if (prev < next) |
| 6510 | *prev++ = ' '; |
| 6511 | |
| 6512 | while (http_is_spht[(unsigned char)*next]) |
| 6513 | next++; |
| 6514 | } else { |
| 6515 | /* Remove useless spaces before the old delimiter. */ |
| 6516 | while (http_is_spht[(unsigned char)*(prev-1)]) |
| 6517 | prev--; |
| 6518 | *from = prev; |
| 6519 | |
| 6520 | /* copy the delimiter and if possible a space if we're |
| 6521 | * not at the end of the line. |
| 6522 | */ |
| 6523 | if (!http_is_crlf[(unsigned char)*next]) { |
| 6524 | *prev++ = *next++; |
| 6525 | if (prev + 1 < next) |
| 6526 | *prev++ = ' '; |
| 6527 | while (http_is_spht[(unsigned char)*next]) |
| 6528 | next++; |
| 6529 | } |
| 6530 | } |
| 6531 | return buffer_replace2(buf, prev, next, NULL, 0); |
| 6532 | } |
| 6533 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6534 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 6535 | * 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] | 6536 | * desirable to call it only when needed. This code is quite complex because |
| 6537 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 6538 | * highly recommended not to touch this part without a good reason ! |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6539 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 6540 | void manage_client_side_cookies(struct session *t, struct channel *req) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6541 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6542 | struct http_txn *txn = &t->txn; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6543 | int preserve_hdr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6544 | int cur_idx, old_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6545 | char *hdr_beg, *hdr_end, *hdr_next, *del_from; |
| 6546 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6547 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6548 | /* Iterate through the headers, we start with the start line. */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 6549 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6550 | hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6551 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6552 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6553 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6554 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6555 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6556 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6557 | hdr_beg = hdr_next; |
| 6558 | hdr_end = hdr_beg + cur_hdr->len; |
| 6559 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6560 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6561 | /* We have one full header between hdr_beg and hdr_end, and the |
| 6562 | * next header starts at hdr_next. We're only interested in |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6563 | * "Cookie:" headers. |
| 6564 | */ |
| 6565 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6566 | val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6567 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6568 | old_idx = cur_idx; |
| 6569 | continue; |
| 6570 | } |
| 6571 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6572 | del_from = NULL; /* nothing to be deleted */ |
| 6573 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 6574 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6575 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 6576 | * attributes whose name begin with a '$', and associate them with |
| 6577 | * the right cookie, if we want to delete this cookie. |
| 6578 | * So there are 3 cases for each cookie read : |
| 6579 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 6580 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 6581 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 6582 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 6583 | * "special" cookie. |
| 6584 | * At the end of loop, if a "special" cookie remains, we may have to |
| 6585 | * remove it. If no application cookie persists in the header, we |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6586 | * *MUST* delete it. |
| 6587 | * |
| 6588 | * Note: RFC2965 is unclear about the processing of spaces around |
| 6589 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 6590 | * the RFC explicitly allows spaces before it, and not within the |
| 6591 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 6592 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 6593 | * after the equal sign too, resulting in some (rare) buggy |
| 6594 | * implementations trying to do that. So let's do what servers do. |
| 6595 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 6596 | * allowed quoted strings in values, with any possible character |
| 6597 | * after a backslash, including control chars and delimitors, which |
| 6598 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 6599 | * within values even without quotes. |
| 6600 | * |
| 6601 | * We have to keep multiple pointers in order to support cookie |
| 6602 | * removal at the beginning, middle or end of header without |
| 6603 | * corrupting the header. All of these headers are valid : |
| 6604 | * |
| 6605 | * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n |
| 6606 | * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n |
| 6607 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 6608 | * | | | | | | | | | |
| 6609 | * | | | | | | | | hdr_end <--+ |
| 6610 | * | | | | | | | +--> next |
| 6611 | * | | | | | | +----> val_end |
| 6612 | * | | | | | +-----------> val_beg |
| 6613 | * | | | | +--------------> equal |
| 6614 | * | | | +----------------> att_end |
| 6615 | * | | +---------------------> att_beg |
| 6616 | * | +--------------------------> prev |
| 6617 | * +--------------------------------> hdr_beg |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6618 | */ |
| 6619 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6620 | for (prev = hdr_beg + 6; prev < hdr_end; prev = next) { |
| 6621 | /* Iterate through all cookies on this line */ |
| 6622 | |
| 6623 | /* find att_beg */ |
| 6624 | att_beg = prev + 1; |
| 6625 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 6626 | att_beg++; |
| 6627 | |
| 6628 | /* find att_end : this is the first character after the last non |
| 6629 | * space before the equal. It may be equal to hdr_end. |
| 6630 | */ |
| 6631 | equal = att_end = att_beg; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6632 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6633 | while (equal < hdr_end) { |
| 6634 | if (*equal == '=' || *equal == ',' || *equal == ';') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6635 | break; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6636 | if (http_is_spht[(unsigned char)*equal++]) |
| 6637 | continue; |
| 6638 | att_end = equal; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6639 | } |
| 6640 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6641 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 6642 | * is between <att_beg> and <equal>, both may be identical. |
| 6643 | */ |
| 6644 | |
| 6645 | /* look for end of cookie if there is an equal sign */ |
| 6646 | if (equal < hdr_end && *equal == '=') { |
| 6647 | /* look for the beginning of the value */ |
| 6648 | val_beg = equal + 1; |
| 6649 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 6650 | val_beg++; |
| 6651 | |
| 6652 | /* find the end of the value, respecting quotes */ |
| 6653 | next = find_cookie_value_end(val_beg, hdr_end); |
| 6654 | |
| 6655 | /* make val_end point to the first white space or delimitor after the value */ |
| 6656 | val_end = next; |
| 6657 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 6658 | val_end--; |
| 6659 | } else { |
| 6660 | val_beg = val_end = next = equal; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6661 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6662 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6663 | /* We have nothing to do with attributes beginning with '$'. However, |
| 6664 | * they will automatically be removed if a header before them is removed, |
| 6665 | * since they're supposed to be linked together. |
| 6666 | */ |
| 6667 | if (*att_beg == '$') |
| 6668 | continue; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6669 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6670 | /* Ignore cookies with no equal sign */ |
| 6671 | if (equal == next) { |
| 6672 | /* This is not our cookie, so we must preserve it. But if we already |
| 6673 | * scheduled another cookie for removal, we cannot remove the |
| 6674 | * complete header, but we can remove the previous block itself. |
| 6675 | */ |
| 6676 | preserve_hdr = 1; |
| 6677 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6678 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6679 | val_end += delta; |
| 6680 | next += delta; |
| 6681 | hdr_end += delta; |
| 6682 | hdr_next += delta; |
| 6683 | cur_hdr->len += delta; |
| 6684 | http_msg_move_end(&txn->req, delta); |
| 6685 | prev = del_from; |
| 6686 | del_from = NULL; |
| 6687 | } |
| 6688 | continue; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6689 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6690 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6691 | /* if there are spaces around the equal sign, we need to |
| 6692 | * strip them otherwise we'll get trouble for cookie captures, |
| 6693 | * or even for rewrites. Since this happens extremely rarely, |
| 6694 | * it does not hurt performance. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6695 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6696 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 6697 | int stripped_before = 0; |
| 6698 | int stripped_after = 0; |
| 6699 | |
| 6700 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6701 | stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6702 | equal += stripped_before; |
| 6703 | val_beg += stripped_before; |
| 6704 | } |
| 6705 | |
| 6706 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6707 | stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6708 | val_beg += stripped_after; |
| 6709 | stripped_before += stripped_after; |
| 6710 | } |
| 6711 | |
| 6712 | val_end += stripped_before; |
| 6713 | next += stripped_before; |
| 6714 | hdr_end += stripped_before; |
| 6715 | hdr_next += stripped_before; |
| 6716 | cur_hdr->len += stripped_before; |
| 6717 | http_msg_move_end(&txn->req, stripped_before); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6718 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6719 | /* now everything is as on the diagram above */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6720 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6721 | /* First, let's see if we want to capture this cookie. We check |
| 6722 | * that we don't already have a client side cookie, because we |
| 6723 | * can only capture one. Also as an optimisation, we ignore |
| 6724 | * cookies shorter than the declared name. |
| 6725 | */ |
| 6726 | if (t->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 6727 | (val_end - att_beg >= t->fe->capture_namelen) && |
| 6728 | memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
| 6729 | int log_len = val_end - att_beg; |
| 6730 | |
| 6731 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
| 6732 | Alert("HTTP logging : out of memory.\n"); |
| 6733 | } else { |
| 6734 | if (log_len > t->fe->capture_len) |
| 6735 | log_len = t->fe->capture_len; |
| 6736 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 6737 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6738 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6739 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6740 | |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6741 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 6742 | * following form : |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6743 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6744 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6745 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6746 | * For cookies in prefix mode, the form is : |
| 6747 | * |
| 6748 | * Cookie: NAME=SRV~VALUE |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6749 | */ |
| 6750 | if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 6751 | (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) { |
| 6752 | struct server *srv = t->be->srv; |
| 6753 | char *delim; |
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 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 6756 | * have the server ID between val_beg and delim, and the original cookie between |
| 6757 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 6758 | * |
| 6759 | * Cookie: NAME=SRV; # in all but prefix modes |
| 6760 | * Cookie: NAME=SRV~OPAQUE ; # in prefix mode |
| 6761 | * | || || | |+-> next |
| 6762 | * | || || | +--> val_end |
| 6763 | * | || || +---------> delim |
| 6764 | * | || |+------------> val_beg |
| 6765 | * | || +-------------> att_end = equal |
| 6766 | * | |+-----------------> att_beg |
| 6767 | * | +------------------> prev |
| 6768 | * +-------------------------> hdr_beg |
| 6769 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6770 | |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 6771 | if (t->be->ck_opts & PR_CK_PFX) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6772 | for (delim = val_beg; delim < val_end; delim++) |
| 6773 | if (*delim == COOKIE_DELIM) |
| 6774 | break; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6775 | } else { |
| 6776 | char *vbar1; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6777 | delim = val_end; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6778 | /* Now check if the cookie contains a date field, which would |
| 6779 | * appear after a vertical bar ('|') just after the server name |
| 6780 | * and before the delimiter. |
| 6781 | */ |
| 6782 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 6783 | if (vbar1) { |
| 6784 | /* OK, so left of the bar is the server's cookie and |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6785 | * right is the last seen date. It is a base64 encoded |
| 6786 | * 30-bit value representing the UNIX date since the |
| 6787 | * epoch in 4-second quantities. |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6788 | */ |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6789 | int val; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6790 | delim = vbar1++; |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6791 | if (val_end - vbar1 >= 5) { |
| 6792 | val = b64tos30(vbar1); |
| 6793 | if (val > 0) |
| 6794 | txn->cookie_last_date = val << 2; |
| 6795 | } |
| 6796 | /* look for a second vertical bar */ |
| 6797 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 6798 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 6799 | val = b64tos30(vbar1 + 1); |
| 6800 | if (val > 0) |
| 6801 | txn->cookie_first_date = val << 2; |
| 6802 | } |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6803 | } |
| 6804 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6805 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6806 | /* if the cookie has an expiration date and the proxy wants to check |
| 6807 | * it, then we do that now. We first check if the cookie is too old, |
| 6808 | * then only if it has expired. We detect strict overflow because the |
| 6809 | * time resolution here is not great (4 seconds). Cookies with dates |
| 6810 | * in the future are ignored if their offset is beyond one day. This |
| 6811 | * allows an admin to fix timezone issues without expiring everyone |
| 6812 | * and at the same time avoids keeping unwanted side effects for too |
| 6813 | * long. |
| 6814 | */ |
| 6815 | if (txn->cookie_first_date && t->be->cookie_maxlife && |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6816 | (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) || |
| 6817 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6818 | txn->flags &= ~TX_CK_MASK; |
| 6819 | txn->flags |= TX_CK_OLD; |
| 6820 | delim = val_beg; // let's pretend we have not found the cookie |
| 6821 | txn->cookie_first_date = 0; |
| 6822 | txn->cookie_last_date = 0; |
| 6823 | } |
| 6824 | else if (txn->cookie_last_date && t->be->cookie_maxidle && |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6825 | (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) || |
| 6826 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6827 | txn->flags &= ~TX_CK_MASK; |
| 6828 | txn->flags |= TX_CK_EXPIRED; |
| 6829 | delim = val_beg; // let's pretend we have not found the cookie |
| 6830 | txn->cookie_first_date = 0; |
| 6831 | txn->cookie_last_date = 0; |
| 6832 | } |
| 6833 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6834 | /* Here, we'll look for the first running server which supports the cookie. |
| 6835 | * This allows to share a same cookie between several servers, for example |
| 6836 | * to dedicate backup servers to specific servers only. |
| 6837 | * However, to prevent clients from sticking to cookie-less backup server |
| 6838 | * when they have incidentely learned an empty cookie, we simply ignore |
| 6839 | * empty cookies and mark them as invalid. |
| 6840 | * The same behaviour is applied when persistence must be ignored. |
| 6841 | */ |
Willy Tarreau | 4a5cade | 2012-04-05 21:09:48 +0200 | [diff] [blame] | 6842 | if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6843 | srv = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6844 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6845 | while (srv) { |
| 6846 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 6847 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
| 6848 | if ((srv->state & SRV_RUNNING) || |
| 6849 | (t->be->options & PR_O_PERSIST) || |
| 6850 | (t->flags & SN_FORCE_PRST)) { |
| 6851 | /* we found the server and we can use it */ |
| 6852 | txn->flags &= ~TX_CK_MASK; |
| 6853 | txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN; |
| 6854 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6855 | t->target = &srv->obj_type; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6856 | break; |
| 6857 | } else { |
| 6858 | /* we found a server, but it's down, |
| 6859 | * mark it as such and go on in case |
| 6860 | * another one is available. |
| 6861 | */ |
| 6862 | txn->flags &= ~TX_CK_MASK; |
| 6863 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6864 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6865 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6866 | srv = srv->next; |
| 6867 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6868 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6869 | 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] | 6870 | /* no server matched this cookie or we deliberately skipped it */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6871 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | c89ccb6 | 2012-04-05 21:18:22 +0200 | [diff] [blame] | 6872 | if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED))) |
| 6873 | txn->flags |= TX_CK_UNUSED; |
| 6874 | else |
| 6875 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6876 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6877 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6878 | /* depending on the cookie mode, we may have to either : |
| 6879 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 6880 | * the server never sees it ; |
| 6881 | * - remove the server id from the cookie value, and tag the cookie as an |
| 6882 | * application cookie so that it does not get accidentely removed later, |
| 6883 | * if we're in cookie prefix mode |
| 6884 | */ |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 6885 | if ((t->be->ck_opts & PR_CK_PFX) && (delim != val_end)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6886 | int delta; /* negative */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6887 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6888 | delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6889 | val_end += delta; |
| 6890 | next += delta; |
| 6891 | hdr_end += delta; |
| 6892 | hdr_next += delta; |
| 6893 | cur_hdr->len += delta; |
| 6894 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6895 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6896 | del_from = NULL; |
| 6897 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 6898 | } |
| 6899 | else if (del_from == NULL && |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 6900 | (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] | 6901 | del_from = prev; |
| 6902 | } |
| 6903 | } else { |
| 6904 | /* This is not our cookie, so we must preserve it. But if we already |
| 6905 | * scheduled another cookie for removal, we cannot remove the |
| 6906 | * complete header, but we can remove the previous block itself. |
| 6907 | */ |
| 6908 | preserve_hdr = 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6909 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6910 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6911 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | b810554 | 2010-11-24 18:31:28 +0100 | [diff] [blame] | 6912 | if (att_beg >= del_from) |
| 6913 | att_beg += delta; |
| 6914 | if (att_end >= del_from) |
| 6915 | att_end += delta; |
| 6916 | val_beg += delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6917 | val_end += delta; |
| 6918 | next += delta; |
| 6919 | hdr_end += delta; |
| 6920 | hdr_next += delta; |
| 6921 | cur_hdr->len += delta; |
| 6922 | http_msg_move_end(&txn->req, delta); |
| 6923 | prev = del_from; |
| 6924 | del_from = NULL; |
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 | } |
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 | /* Look for the appsession cookie unless persistence must be ignored */ |
| 6929 | if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) { |
| 6930 | int cmp_len, value_len; |
| 6931 | char *value_begin; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 6932 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6933 | if (t->be->options2 & PR_O2_AS_PFX) { |
| 6934 | cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len); |
| 6935 | value_begin = att_beg + t->be->appsession_name_len; |
| 6936 | value_len = val_end - att_beg - t->be->appsession_name_len; |
| 6937 | } else { |
| 6938 | cmp_len = att_end - att_beg; |
| 6939 | value_begin = val_beg; |
| 6940 | value_len = val_end - val_beg; |
| 6941 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6942 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6943 | /* let's see if the cookie is our appcookie */ |
| 6944 | if (cmp_len == t->be->appsession_name_len && |
| 6945 | memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) { |
| 6946 | manage_client_side_appsession(t, value_begin, value_len); |
| 6947 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6948 | } |
| 6949 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6950 | /* continue with next cookie on this header line */ |
| 6951 | att_beg = next; |
| 6952 | } /* for each cookie */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6953 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6954 | /* There are no more cookies on this line. |
| 6955 | * We may still have one (or several) marked for deletion at the |
| 6956 | * end of the line. We must do this now in two ways : |
| 6957 | * - if some cookies must be preserved, we only delete from the |
| 6958 | * mark to the end of line ; |
| 6959 | * - if nothing needs to be preserved, simply delete the whole header |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6960 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6961 | if (del_from) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6962 | int delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6963 | if (preserve_hdr) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6964 | delta = del_hdr_value(req->buf, &del_from, hdr_end); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6965 | hdr_end = del_from; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6966 | cur_hdr->len += delta; |
| 6967 | } else { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6968 | delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6969 | |
| 6970 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6971 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6972 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6973 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6974 | cur_idx = old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6975 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6976 | hdr_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6977 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6978 | } |
| 6979 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6980 | /* check next header */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6981 | old_idx = cur_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6982 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6983 | } |
| 6984 | |
| 6985 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6986 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 6987 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 6988 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 6989 | 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] | 6990 | { |
| 6991 | char term; |
| 6992 | char *cur_ptr, *cur_end, *cur_next; |
| 6993 | int cur_idx, old_idx, last_hdr; |
| 6994 | struct http_txn *txn = &t->txn; |
| 6995 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6996 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6997 | |
| 6998 | last_hdr = 0; |
| 6999 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7000 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7001 | old_idx = 0; |
| 7002 | |
| 7003 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7004 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7005 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7006 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7007 | (exp->action == ACT_ALLOW || |
| 7008 | exp->action == ACT_DENY)) |
| 7009 | return 0; |
| 7010 | |
| 7011 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 7012 | if (!cur_idx) |
| 7013 | break; |
| 7014 | |
| 7015 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 7016 | cur_ptr = cur_next; |
| 7017 | cur_end = cur_ptr + cur_hdr->len; |
| 7018 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7019 | |
| 7020 | /* Now we have one header between cur_ptr and cur_end, |
| 7021 | * and the next header starts at cur_next. |
| 7022 | */ |
| 7023 | |
| 7024 | /* The annoying part is that pattern matching needs |
| 7025 | * that we modify the contents to null-terminate all |
| 7026 | * strings before testing them. |
| 7027 | */ |
| 7028 | |
| 7029 | term = *cur_end; |
| 7030 | *cur_end = '\0'; |
| 7031 | |
| 7032 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 7033 | switch (exp->action) { |
| 7034 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7035 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7036 | last_hdr = 1; |
| 7037 | break; |
| 7038 | |
| 7039 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7040 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7041 | last_hdr = 1; |
| 7042 | break; |
| 7043 | |
| 7044 | case ACT_REPLACE: |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7045 | trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch); |
| 7046 | 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] | 7047 | /* FIXME: if the user adds a newline in the replacement, the |
| 7048 | * index will not be recalculated for now, and the new line |
| 7049 | * will not be counted as a new header. |
| 7050 | */ |
| 7051 | |
| 7052 | cur_end += delta; |
| 7053 | cur_next += delta; |
| 7054 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7055 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7056 | break; |
| 7057 | |
| 7058 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7059 | delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7060 | cur_next += delta; |
| 7061 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7062 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7063 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7064 | txn->hdr_idx.used--; |
| 7065 | cur_hdr->len = 0; |
| 7066 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7067 | cur_idx = old_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7068 | break; |
| 7069 | |
| 7070 | } |
| 7071 | } |
| 7072 | if (cur_end) |
| 7073 | *cur_end = term; /* restore the string terminator */ |
| 7074 | |
| 7075 | /* keep the link from this header to next one in case of later |
| 7076 | * removal of next header. |
| 7077 | */ |
| 7078 | old_idx = cur_idx; |
| 7079 | } |
| 7080 | return 0; |
| 7081 | } |
| 7082 | |
| 7083 | |
| 7084 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 7085 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 7086 | * or -1 if a replacement resulted in an invalid status line. |
| 7087 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 7088 | 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] | 7089 | { |
| 7090 | char term; |
| 7091 | char *cur_ptr, *cur_end; |
| 7092 | int done; |
| 7093 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7094 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7095 | |
| 7096 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7097 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7098 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7099 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7100 | (exp->action == ACT_ALLOW || |
| 7101 | exp->action == ACT_DENY)) |
| 7102 | return 0; |
| 7103 | else if (exp->action == ACT_REMOVE) |
| 7104 | return 0; |
| 7105 | |
| 7106 | done = 0; |
| 7107 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7108 | cur_ptr = rtr->buf->p; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 7109 | cur_end = cur_ptr + txn->rsp.sl.st.l; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7110 | |
| 7111 | /* Now we have the status line between cur_ptr and cur_end */ |
| 7112 | |
| 7113 | /* The annoying part is that pattern matching needs |
| 7114 | * that we modify the contents to null-terminate all |
| 7115 | * strings before testing them. |
| 7116 | */ |
| 7117 | |
| 7118 | term = *cur_end; |
| 7119 | *cur_end = '\0'; |
| 7120 | |
| 7121 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 7122 | switch (exp->action) { |
| 7123 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7124 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7125 | done = 1; |
| 7126 | break; |
| 7127 | |
| 7128 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7129 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7130 | done = 1; |
| 7131 | break; |
| 7132 | |
| 7133 | case ACT_REPLACE: |
| 7134 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7135 | trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch); |
| 7136 | 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] | 7137 | /* FIXME: if the user adds a newline in the replacement, the |
| 7138 | * index will not be recalculated for now, and the new line |
| 7139 | * will not be counted as a new header. |
| 7140 | */ |
| 7141 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7142 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7143 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7144 | cur_end = (char *)http_parse_stsline(&txn->rsp, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 7145 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7146 | cur_ptr, cur_end + 1, |
| 7147 | NULL, NULL); |
| 7148 | if (unlikely(!cur_end)) |
| 7149 | return -1; |
| 7150 | |
| 7151 | /* we have a full respnse and we know that we have either a CR |
| 7152 | * or an LF at <ptr>. |
| 7153 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7154 | 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] | 7155 | 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] | 7156 | /* there is no point trying this regex on headers */ |
| 7157 | return 1; |
| 7158 | } |
| 7159 | } |
| 7160 | *cur_end = term; /* restore the string terminator */ |
| 7161 | return done; |
| 7162 | } |
| 7163 | |
| 7164 | |
| 7165 | |
| 7166 | /* |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7167 | * 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] | 7168 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 7169 | * unparsable response. |
| 7170 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 7171 | 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] | 7172 | { |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7173 | struct http_txn *txn = &s->txn; |
| 7174 | struct hdr_exp *exp; |
| 7175 | |
| 7176 | for (exp = px->rsp_exp; exp; exp = exp->next) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7177 | int ret; |
| 7178 | |
| 7179 | /* |
| 7180 | * The interleaving of transformations and verdicts |
| 7181 | * makes it difficult to decide to continue or stop |
| 7182 | * the evaluation. |
| 7183 | */ |
| 7184 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7185 | if (txn->flags & TX_SVDENY) |
| 7186 | break; |
| 7187 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7188 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7189 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 7190 | exp->action == ACT_PASS)) { |
| 7191 | exp = exp->next; |
| 7192 | continue; |
| 7193 | } |
| 7194 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7195 | /* if this filter had a condition, evaluate it now and skip to |
| 7196 | * next filter if the condition does not match. |
| 7197 | */ |
| 7198 | if (exp->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7199 | 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] | 7200 | ret = acl_pass(ret); |
| 7201 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 7202 | ret = !ret; |
| 7203 | if (!ret) |
| 7204 | continue; |
| 7205 | } |
| 7206 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7207 | /* Apply the filter to the status line. */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7208 | ret = apply_filter_to_sts_line(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7209 | if (unlikely(ret < 0)) |
| 7210 | return -1; |
| 7211 | |
| 7212 | if (likely(ret == 0)) { |
| 7213 | /* The filter did not match the response, it can be |
| 7214 | * iterated through all headers. |
| 7215 | */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7216 | apply_filter_to_resp_headers(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7217 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7218 | } |
| 7219 | return 0; |
| 7220 | } |
| 7221 | |
| 7222 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7223 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 7224 | * 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] | 7225 | * desirable to call it only when needed. This function is also used when we |
| 7226 | * 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] | 7227 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 7228 | void manage_server_side_cookies(struct session *t, struct channel *res) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7229 | { |
| 7230 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7231 | struct server *srv; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7232 | int is_cookie2; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7233 | int cur_idx, old_idx, delta; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7234 | char *hdr_beg, *hdr_end, *hdr_next; |
| 7235 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7236 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7237 | /* Iterate through the headers. |
| 7238 | * we start with the start line. |
| 7239 | */ |
| 7240 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7241 | hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7242 | |
| 7243 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 7244 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7245 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7246 | |
| 7247 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7248 | hdr_beg = hdr_next; |
| 7249 | hdr_end = hdr_beg + cur_hdr->len; |
| 7250 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7251 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7252 | /* We have one full header between hdr_beg and hdr_end, and the |
| 7253 | * next header starts at hdr_next. We're only interested in |
| 7254 | * "Set-Cookie" and "Set-Cookie2" headers. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7255 | */ |
| 7256 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7257 | is_cookie2 = 0; |
| 7258 | prev = hdr_beg + 10; |
| 7259 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7260 | if (!val) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7261 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11); |
| 7262 | if (!val) { |
| 7263 | old_idx = cur_idx; |
| 7264 | continue; |
| 7265 | } |
| 7266 | is_cookie2 = 1; |
| 7267 | prev = hdr_beg + 11; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7268 | } |
| 7269 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7270 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 7271 | * <prev> points to the colon. |
| 7272 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7273 | txn->flags |= TX_SCK_PRESENT; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7274 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7275 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 7276 | * check-cache is enabled) and we are not interested in checking |
| 7277 | * them. Warning, the cookie capture is declared in the frontend. |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 7278 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 7279 | if (t->be->cookie_name == NULL && |
| 7280 | t->be->appsession_name == NULL && |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 7281 | t->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7282 | return; |
| 7283 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7284 | /* OK so now we know we have to process this response cookie. |
| 7285 | * The format of the Set-Cookie header is slightly different |
| 7286 | * from the format of the Cookie header in that it does not |
| 7287 | * support the comma as a cookie delimiter (thus the header |
| 7288 | * cannot be folded) because the Expires attribute described in |
| 7289 | * the original Netscape's spec may contain an unquoted date |
| 7290 | * with a comma inside. We have to live with this because |
| 7291 | * many browsers don't support Max-Age and some browsers don't |
| 7292 | * support quoted strings. However the Set-Cookie2 header is |
| 7293 | * clean. |
| 7294 | * |
| 7295 | * We have to keep multiple pointers in order to support cookie |
| 7296 | * removal at the beginning, middle or end of header without |
| 7297 | * corrupting the header (in case of set-cookie2). A special |
| 7298 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 7299 | * fields after the first semi-colon. The <next> pointer points |
| 7300 | * either to the end of line (set-cookie) or next unquoted comma |
| 7301 | * (set-cookie2). All of these headers are valid : |
| 7302 | * |
| 7303 | * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n |
| 7304 | * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 7305 | * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 7306 | * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n |
| 7307 | * | | | | | | | | | | |
| 7308 | * | | | | | | | | +-> next hdr_end <--+ |
| 7309 | * | | | | | | | +------------> scav |
| 7310 | * | | | | | | +--------------> val_end |
| 7311 | * | | | | | +--------------------> val_beg |
| 7312 | * | | | | +----------------------> equal |
| 7313 | * | | | +------------------------> att_end |
| 7314 | * | | +----------------------------> att_beg |
| 7315 | * | +------------------------------> prev |
| 7316 | * +-----------------------------------------> hdr_beg |
| 7317 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7318 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7319 | for (; prev < hdr_end; prev = next) { |
| 7320 | /* Iterate through all cookies on this line */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7321 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7322 | /* find att_beg */ |
| 7323 | att_beg = prev + 1; |
| 7324 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 7325 | att_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7326 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7327 | /* find att_end : this is the first character after the last non |
| 7328 | * space before the equal. It may be equal to hdr_end. |
| 7329 | */ |
| 7330 | equal = att_end = att_beg; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7331 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7332 | while (equal < hdr_end) { |
| 7333 | if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ',')) |
| 7334 | break; |
| 7335 | if (http_is_spht[(unsigned char)*equal++]) |
| 7336 | continue; |
| 7337 | att_end = equal; |
| 7338 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7339 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7340 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 7341 | * is between <att_beg> and <equal>, both may be identical. |
| 7342 | */ |
| 7343 | |
| 7344 | /* look for end of cookie if there is an equal sign */ |
| 7345 | if (equal < hdr_end && *equal == '=') { |
| 7346 | /* look for the beginning of the value */ |
| 7347 | val_beg = equal + 1; |
| 7348 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 7349 | val_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7350 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7351 | /* find the end of the value, respecting quotes */ |
| 7352 | next = find_cookie_value_end(val_beg, hdr_end); |
| 7353 | |
| 7354 | /* make val_end point to the first white space or delimitor after the value */ |
| 7355 | val_end = next; |
| 7356 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 7357 | val_end--; |
| 7358 | } else { |
| 7359 | /* <equal> points to next comma, semi-colon or EOL */ |
| 7360 | val_beg = val_end = next = equal; |
| 7361 | } |
| 7362 | |
| 7363 | if (next < hdr_end) { |
| 7364 | /* Set-Cookie2 supports multiple cookies, and <next> points to |
| 7365 | * a colon or semi-colon before the end. So skip all attr-value |
| 7366 | * pairs and look for the next comma. For Set-Cookie, since |
| 7367 | * commas are permitted in values, skip to the end. |
| 7368 | */ |
| 7369 | if (is_cookie2) |
| 7370 | next = find_hdr_value_end(next, hdr_end); |
| 7371 | else |
| 7372 | next = hdr_end; |
| 7373 | } |
| 7374 | |
| 7375 | /* Now everything is as on the diagram above */ |
| 7376 | |
| 7377 | /* Ignore cookies with no equal sign */ |
| 7378 | if (equal == val_end) |
| 7379 | continue; |
| 7380 | |
| 7381 | /* If there are spaces around the equal sign, we need to |
| 7382 | * strip them otherwise we'll get trouble for cookie captures, |
| 7383 | * or even for rewrites. Since this happens extremely rarely, |
| 7384 | * it does not hurt performance. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7385 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7386 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 7387 | int stripped_before = 0; |
| 7388 | int stripped_after = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7389 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7390 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7391 | stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7392 | equal += stripped_before; |
| 7393 | val_beg += stripped_before; |
| 7394 | } |
| 7395 | |
| 7396 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7397 | stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7398 | val_beg += stripped_after; |
| 7399 | stripped_before += stripped_after; |
| 7400 | } |
| 7401 | |
| 7402 | val_end += stripped_before; |
| 7403 | next += stripped_before; |
| 7404 | hdr_end += stripped_before; |
| 7405 | hdr_next += stripped_before; |
| 7406 | cur_hdr->len += stripped_before; |
Willy Tarreau | 1fc1f45 | 2011-04-07 22:35:37 +0200 | [diff] [blame] | 7407 | http_msg_move_end(&txn->rsp, stripped_before); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7408 | } |
| 7409 | |
| 7410 | /* First, let's see if we want to capture this cookie. We check |
| 7411 | * that we don't already have a server side cookie, because we |
| 7412 | * can only capture one. Also as an optimisation, we ignore |
| 7413 | * cookies shorter than the declared name. |
| 7414 | */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 7415 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 7416 | txn->srv_cookie == NULL && |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7417 | (val_end - att_beg >= t->fe->capture_namelen) && |
| 7418 | memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
| 7419 | int log_len = val_end - att_beg; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 7420 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7421 | Alert("HTTP logging : out of memory.\n"); |
| 7422 | } |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 7423 | else { |
| 7424 | if (log_len > t->fe->capture_len) |
| 7425 | log_len = t->fe->capture_len; |
| 7426 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 7427 | txn->srv_cookie[log_len] = 0; |
| 7428 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7429 | } |
| 7430 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7431 | srv = objt_server(t->target); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7432 | /* now check if we need to process it for persistence */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7433 | if (!(t->flags & SN_IGNORE_PRST) && |
| 7434 | (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 7435 | (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) { |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7436 | /* assume passive cookie by default */ |
| 7437 | txn->flags &= ~TX_SCK_MASK; |
| 7438 | txn->flags |= TX_SCK_FOUND; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7439 | |
| 7440 | /* If the cookie is in insert mode on a known server, we'll delete |
| 7441 | * this occurrence because we'll insert another one later. |
| 7442 | * 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] | 7443 | * a direct access. |
| 7444 | */ |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 7445 | if (t->be->ck_opts & PR_CK_PSV) { |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 7446 | /* The "preserve" flag was set, we don't want to touch the |
| 7447 | * server's cookie. |
| 7448 | */ |
| 7449 | } |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 7450 | else if ((srv && (t->be->ck_opts & PR_CK_INS)) || |
| 7451 | ((t->flags & SN_DIRECT) && (t->be->ck_opts & PR_CK_IND))) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7452 | /* this cookie must be deleted */ |
| 7453 | if (*prev == ':' && next == hdr_end) { |
| 7454 | /* whole header */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7455 | delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7456 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7457 | txn->hdr_idx.used--; |
| 7458 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7459 | cur_idx = old_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7460 | hdr_next += delta; |
| 7461 | http_msg_move_end(&txn->rsp, delta); |
| 7462 | /* note: while both invalid now, <next> and <hdr_end> |
| 7463 | * are still equal, so the for() will stop as expected. |
| 7464 | */ |
| 7465 | } else { |
| 7466 | /* just remove the value */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7467 | int delta = del_hdr_value(res->buf, &prev, next); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7468 | next = prev; |
| 7469 | hdr_end += delta; |
| 7470 | hdr_next += delta; |
| 7471 | cur_hdr->len += delta; |
| 7472 | http_msg_move_end(&txn->rsp, delta); |
| 7473 | } |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7474 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7475 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7476 | /* and go on with next cookie */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7477 | } |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 7478 | else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_RW)) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7479 | /* replace bytes val_beg->val_end with the cookie name associated |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7480 | * with this server since we know it. |
| 7481 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7482 | 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] | 7483 | next += delta; |
| 7484 | hdr_end += delta; |
| 7485 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7486 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7487 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7488 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7489 | txn->flags &= ~TX_SCK_MASK; |
| 7490 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7491 | } |
Willy Tarreau | a059031 | 2012-06-06 16:07:00 +0200 | [diff] [blame] | 7492 | else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7493 | /* insert the cookie name associated with this server |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7494 | * before existing cookie, and insert a delimiter between them.. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7495 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7496 | 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] | 7497 | next += delta; |
| 7498 | hdr_end += delta; |
| 7499 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7500 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7501 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7502 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7503 | val_beg[srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7504 | txn->flags &= ~TX_SCK_MASK; |
| 7505 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7506 | } |
| 7507 | } |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 7508 | /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */ |
| 7509 | else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7510 | int cmp_len, value_len; |
| 7511 | char *value_begin; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7512 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7513 | if (t->be->options2 & PR_O2_AS_PFX) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7514 | cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len); |
| 7515 | value_begin = att_beg + t->be->appsession_name_len; |
| 7516 | 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] | 7517 | } else { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7518 | cmp_len = att_end - att_beg; |
| 7519 | value_begin = val_beg; |
| 7520 | value_len = MIN(t->be->appsession_len, val_end - val_beg); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7521 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7522 | |
Cyril Bonté | 17530c3 | 2010-04-06 21:11:10 +0200 | [diff] [blame] | 7523 | if ((cmp_len == t->be->appsession_name_len) && |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7524 | (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) { |
| 7525 | /* free a possibly previously allocated memory */ |
| 7526 | pool_free2(apools.sessid, txn->sessid); |
| 7527 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7528 | /* Store the sessid in the session for future use */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7529 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7530 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 7531 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 7532 | return; |
| 7533 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7534 | memcpy(txn->sessid, value_begin, value_len); |
| 7535 | txn->sessid[value_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7536 | } |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7537 | } |
| 7538 | /* that's done for this cookie, check the next one on the same |
| 7539 | * line when next != hdr_end (only if is_cookie2). |
| 7540 | */ |
| 7541 | } |
| 7542 | /* check next header */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7543 | old_idx = cur_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7544 | } |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7545 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7546 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7547 | appsess *asession = NULL; |
| 7548 | /* only do insert, if lookup fails */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7549 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7550 | if (asession == NULL) { |
Willy Tarreau | 1fac753 | 2010-01-09 19:23:06 +0100 | [diff] [blame] | 7551 | size_t server_id_len; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7552 | if ((asession = pool_alloc2(pool2_appsess)) == NULL) { |
| 7553 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
| 7554 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n"); |
| 7555 | return; |
| 7556 | } |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 7557 | asession->serverid = NULL; /* to avoid a double free in case of allocation error */ |
| 7558 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7559 | if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) { |
| 7560 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 7561 | 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] | 7562 | t->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7563 | return; |
| 7564 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7565 | memcpy(asession->sessid, txn->sessid, t->be->appsession_len); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7566 | asession->sessid[t->be->appsession_len] = 0; |
| 7567 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7568 | server_id_len = strlen(objt_server(t->target)->id) + 1; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7569 | if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) { |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 7570 | Alert("Not enough Memory process_srv():asession->serverid:malloc().\n"); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7571 | 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] | 7572 | t->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7573 | return; |
| 7574 | } |
| 7575 | asession->serverid[0] = '\0'; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7576 | memcpy(asession->serverid, objt_server(t->target)->id, server_id_len); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7577 | |
| 7578 | asession->request_count = 0; |
| 7579 | appsession_hash_insert(&(t->be->htbl_proxy), asession); |
| 7580 | } |
| 7581 | |
| 7582 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 7583 | asession->request_count++; |
| 7584 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7585 | } |
| 7586 | |
| 7587 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7588 | /* |
| 7589 | * Check if response is cacheable or not. Updates t->flags. |
| 7590 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 7591 | void check_response_for_cacheability(struct session *t, struct channel *rtr) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7592 | { |
| 7593 | struct http_txn *txn = &t->txn; |
| 7594 | char *p1, *p2; |
| 7595 | |
| 7596 | char *cur_ptr, *cur_end, *cur_next; |
| 7597 | int cur_idx; |
| 7598 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 7599 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7600 | return; |
| 7601 | |
| 7602 | /* Iterate through the headers. |
| 7603 | * we start with the start line. |
| 7604 | */ |
| 7605 | cur_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7606 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7607 | |
| 7608 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 7609 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7610 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7611 | |
| 7612 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 7613 | cur_ptr = cur_next; |
| 7614 | cur_end = cur_ptr + cur_hdr->len; |
| 7615 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7616 | |
| 7617 | /* We have one full header between cur_ptr and cur_end, and the |
| 7618 | * next header starts at cur_next. We're only interested in |
| 7619 | * "Cookie:" headers. |
| 7620 | */ |
| 7621 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7622 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 7623 | if (val) { |
| 7624 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 7625 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 7626 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 7627 | return; |
| 7628 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7629 | } |
| 7630 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7631 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 7632 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7633 | continue; |
| 7634 | |
| 7635 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 7636 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7637 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7638 | |
| 7639 | if (p1 >= cur_end) /* no more info */ |
| 7640 | continue; |
| 7641 | |
| 7642 | /* p1 is at the beginning of the value */ |
| 7643 | p2 = p1; |
| 7644 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 7645 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7646 | p2++; |
| 7647 | |
| 7648 | /* we have a complete value between p1 and p2 */ |
| 7649 | if (p2 < cur_end && *p2 == '=') { |
| 7650 | /* we have something of the form no-cache="set-cookie" */ |
| 7651 | if ((cur_end - p1 >= 21) && |
| 7652 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 7653 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7654 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7655 | continue; |
| 7656 | } |
| 7657 | |
| 7658 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 7659 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
| 7660 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 7661 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 7662 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7663 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7664 | return; |
| 7665 | } |
| 7666 | |
| 7667 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7668 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7669 | continue; |
| 7670 | } |
| 7671 | } |
| 7672 | } |
| 7673 | |
| 7674 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7675 | /* |
| 7676 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 7677 | * If the server is found, it's assigned to the session. |
| 7678 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7679 | 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] | 7680 | { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7681 | char *end_params, *first_param, *cur_param, *next_param; |
| 7682 | char separator; |
| 7683 | int value_len; |
| 7684 | |
| 7685 | int mode = t->be->options2 & PR_O2_AS_M_ANY; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7686 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 7687 | if (t->be->appsession_name == NULL || |
Cyril Bonté | 17530c3 | 2010-04-06 21:11:10 +0200 | [diff] [blame] | 7688 | (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] | 7689 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7690 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7691 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7692 | first_param = NULL; |
| 7693 | switch (mode) { |
| 7694 | case PR_O2_AS_M_PP: |
| 7695 | first_param = memchr(begin, ';', len); |
| 7696 | break; |
| 7697 | case PR_O2_AS_M_QS: |
| 7698 | first_param = memchr(begin, '?', len); |
| 7699 | break; |
| 7700 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7701 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7702 | if (first_param == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7703 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7704 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7705 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7706 | switch (mode) { |
| 7707 | case PR_O2_AS_M_PP: |
| 7708 | if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) { |
| 7709 | end_params = (char *) begin + len; |
| 7710 | } |
| 7711 | separator = ';'; |
| 7712 | break; |
| 7713 | case PR_O2_AS_M_QS: |
| 7714 | end_params = (char *) begin + len; |
| 7715 | separator = '&'; |
| 7716 | break; |
| 7717 | default: |
| 7718 | /* unknown mode, shouldn't happen */ |
| 7719 | return; |
| 7720 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7721 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7722 | cur_param = next_param = end_params; |
| 7723 | while (cur_param > first_param) { |
| 7724 | cur_param--; |
| 7725 | if ((cur_param[0] == separator) || (cur_param == first_param)) { |
| 7726 | /* let's see if this is the appsession parameter */ |
| 7727 | if ((cur_param + t->be->appsession_name_len + 1 < next_param) && |
| 7728 | ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') && |
| 7729 | (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) { |
| 7730 | /* Cool... it's the right one */ |
| 7731 | cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2); |
| 7732 | value_len = MIN(t->be->appsession_len, next_param - cur_param); |
| 7733 | if (value_len > 0) { |
| 7734 | manage_client_side_appsession(t, cur_param, value_len); |
| 7735 | } |
| 7736 | break; |
| 7737 | } |
| 7738 | next_param = cur_param; |
| 7739 | } |
| 7740 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7741 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 7742 | Alert("get_srv_from_appsession\n"); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 7743 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7744 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7745 | } |
| 7746 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7747 | /* |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7748 | * 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] | 7749 | * for the current backend. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7750 | * |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7751 | * 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] | 7752 | * uri_auth field is valid. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7753 | * |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7754 | * Returns 1 if stats should be provided, otherwise 0. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7755 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7756 | 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] | 7757 | { |
| 7758 | struct uri_auth *uri_auth = backend->uri_auth; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7759 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7760 | const char *uri = msg->chn->buf->p+ msg->sl.rq.u; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7761 | const char *h; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7762 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7763 | if (!uri_auth) |
| 7764 | return 0; |
| 7765 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7766 | 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] | 7767 | return 0; |
| 7768 | |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7769 | memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats)); |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 7770 | si->applet.ctx.stats.st_code = STAT_STATUS_INIT; |
Willy Tarreau | 354898b | 2012-12-23 18:15:23 +0100 | [diff] [blame] | 7771 | si->applet.ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */ |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 7772 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7773 | /* check URI size */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7774 | if (uri_auth->uri_len > msg->sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7775 | return 0; |
| 7776 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7777 | h = uri; |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 7778 | if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7779 | return 0; |
| 7780 | |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7781 | h += uri_auth->uri_len; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7782 | while (h <= uri + msg->sl.rq.u_l - 3) { |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7783 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7784 | si->applet.ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7785 | break; |
| 7786 | } |
| 7787 | h++; |
| 7788 | } |
| 7789 | |
| 7790 | if (uri_auth->refresh) { |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7791 | h = uri + uri_auth->uri_len; |
| 7792 | while (h <= uri + msg->sl.rq.u_l - 10) { |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7793 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7794 | si->applet.ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7795 | break; |
| 7796 | } |
| 7797 | h++; |
| 7798 | } |
| 7799 | } |
| 7800 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7801 | h = uri + uri_auth->uri_len; |
| 7802 | while (h <= uri + msg->sl.rq.u_l - 4) { |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 7803 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 354898b | 2012-12-23 18:15:23 +0100 | [diff] [blame] | 7804 | si->applet.ctx.stats.flags &= ~STAT_FMT_HTML; |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 7805 | break; |
| 7806 | } |
| 7807 | h++; |
| 7808 | } |
| 7809 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7810 | h = uri + uri_auth->uri_len; |
| 7811 | while (h <= uri + msg->sl.rq.u_l - 8) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7812 | if (memcmp(h, ";st=", 4) == 0) { |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 7813 | int i; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7814 | h += 4; |
Cyril Bonté | 20a804a | 2012-05-10 19:42:52 +0200 | [diff] [blame] | 7815 | si->applet.ctx.stats.st_code = STAT_STATUS_UNKN; |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 7816 | for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) { |
| 7817 | if (strncmp(stat_status_codes[i], h, 4) == 0) { |
| 7818 | si->applet.ctx.stats.st_code = i; |
| 7819 | break; |
| 7820 | } |
| 7821 | } |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7822 | break; |
| 7823 | } |
| 7824 | h++; |
| 7825 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7826 | return 1; |
| 7827 | } |
| 7828 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7829 | /* |
| 7830 | * 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] | 7831 | * By default it tries to report the error position as msg->err_pos. However if |
| 7832 | * this one is not set, it will then report msg->next, which is the last known |
| 7833 | * 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] | 7834 | * displays buffers as a contiguous area starting at buf->p. |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7835 | */ |
| 7836 | void http_capture_bad_message(struct error_snapshot *es, struct session *s, |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 7837 | struct http_msg *msg, |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 7838 | int state, struct proxy *other_end) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7839 | { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 7840 | struct channel *chn = msg->chn; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7841 | int len1, len2; |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 7842 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7843 | es->len = MIN(chn->buf->i, sizeof(es->buf)); |
| 7844 | len1 = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7845 | len1 = MIN(len1, es->len); |
| 7846 | len2 = es->len - len1; /* remaining data if buffer wraps */ |
| 7847 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7848 | memcpy(es->buf, chn->buf->p, len1); |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7849 | if (len2) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7850 | memcpy(es->buf + len1, chn->buf->data, len2); |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7851 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7852 | if (msg->err_pos >= 0) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7853 | es->pos = msg->err_pos; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7854 | else |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7855 | es->pos = msg->next; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7856 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7857 | es->when = date; // user-visible date |
| 7858 | es->sid = s->uniq_id; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7859 | es->srv = objt_server(s->target); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7860 | es->oe = other_end; |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 7861 | es->src = s->req->prod->conn->addr.from; |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 7862 | es->state = state; |
Willy Tarreau | 10479e4 | 2010-12-12 14:00:34 +0100 | [diff] [blame] | 7863 | es->ev_id = error_snapshot_id++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 7864 | es->b_flags = chn->flags; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 7865 | es->s_flags = s->flags; |
| 7866 | es->t_flags = s->txn.flags; |
| 7867 | es->m_flags = msg->flags; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7868 | es->b_out = chn->buf->o; |
| 7869 | es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 7870 | es->b_tot = chn->total; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 7871 | es->m_clen = msg->chunk_len; |
| 7872 | es->m_blen = msg->body_len; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7873 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7874 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7875 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 7876 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 7877 | * performed over the whole headers. Otherwise it must contain a valid header |
| 7878 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 7879 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 7880 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 7881 | * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is |
| 7882 | * -1. |
| 7883 | * 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] | 7884 | */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 7885 | 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] | 7886 | struct hdr_idx *idx, int occ, |
| 7887 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7888 | { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7889 | struct hdr_ctx local_ctx; |
| 7890 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 7891 | int len_hist[MAX_HDR_HISTORY]; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7892 | unsigned int hist_ptr; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7893 | int found; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7894 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7895 | if (!ctx) { |
| 7896 | local_ctx.idx = 0; |
| 7897 | ctx = &local_ctx; |
| 7898 | } |
| 7899 | |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7900 | if (occ >= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7901 | /* search from the beginning */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7902 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7903 | occ--; |
| 7904 | if (occ <= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7905 | *vptr = ctx->line + ctx->val; |
| 7906 | *vlen = ctx->vlen; |
| 7907 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7908 | } |
| 7909 | } |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7910 | return 0; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7911 | } |
| 7912 | |
| 7913 | /* negative occurrence, we scan all the list then walk back */ |
| 7914 | if (-occ > MAX_HDR_HISTORY) |
| 7915 | return 0; |
| 7916 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7917 | found = hist_ptr = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7918 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7919 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 7920 | len_hist[hist_ptr] = ctx->vlen; |
| 7921 | if (++hist_ptr >= MAX_HDR_HISTORY) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7922 | hist_ptr = 0; |
| 7923 | found++; |
| 7924 | } |
| 7925 | if (-occ > found) |
| 7926 | return 0; |
| 7927 | /* OK now we have the last occurrence in [hist_ptr-1], and we need to |
| 7928 | * find occurrence -occ, so we have to check [hist_ptr+occ]. |
| 7929 | */ |
| 7930 | hist_ptr += occ; |
| 7931 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 7932 | hist_ptr -= MAX_HDR_HISTORY; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7933 | *vptr = ptr_hist[hist_ptr]; |
| 7934 | *vlen = len_hist[hist_ptr]; |
| 7935 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7936 | } |
| 7937 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 7938 | /* |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 7939 | * Print a debug line with a header. Always stop at the first CR or LF char, |
| 7940 | * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an |
| 7941 | * arrow is printed after the line which contains the pointer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7942 | */ |
| 7943 | void debug_hdr(const char *dir, struct session *t, const char *start, const char *end) |
| 7944 | { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7945 | int max; |
| 7946 | chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id, |
Willy Tarreau | 7f7ad91 | 2012-11-11 19:27:15 +0100 | [diff] [blame] | 7947 | dir, (unsigned short)t->req->prod->conn->t.sock.fd, |
| 7948 | (unsigned short)t->req->cons->conn->t.sock.fd); |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 7949 | |
| 7950 | for (max = 0; start + max < end; max++) |
| 7951 | if (start[max] == '\r' || start[max] == '\n') |
| 7952 | break; |
| 7953 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7954 | UBOUND(max, trash.size - trash.len - 3); |
| 7955 | trash.len += strlcpy2(trash.str + trash.len, start, max + 1); |
| 7956 | trash.str[trash.len++] = '\n'; |
| 7957 | if (write(1, trash.str, trash.len) < 0) /* shut gcc warning */; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7958 | } |
| 7959 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7960 | /* |
| 7961 | * Initialize a new HTTP transaction for session <s>. It is assumed that all |
| 7962 | * the required fields are properly allocated and that we only need to (re)init |
| 7963 | * them. This should be used before processing any new request. |
| 7964 | */ |
| 7965 | void http_init_txn(struct session *s) |
| 7966 | { |
| 7967 | struct http_txn *txn = &s->txn; |
| 7968 | struct proxy *fe = s->fe; |
| 7969 | |
| 7970 | txn->flags = 0; |
| 7971 | txn->status = -1; |
| 7972 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 7973 | global.req_count++; |
| 7974 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7975 | txn->cookie_first_date = 0; |
| 7976 | txn->cookie_last_date = 0; |
| 7977 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 7978 | txn->req.flags = 0; |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 7979 | 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] | 7980 | txn->req.next = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 7981 | txn->rsp.flags = 0; |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 7982 | 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] | 7983 | txn->rsp.next = 0; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 7984 | txn->req.chunk_len = 0LL; |
| 7985 | txn->req.body_len = 0LL; |
| 7986 | txn->rsp.chunk_len = 0LL; |
| 7987 | txn->rsp.body_len = 0LL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7988 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 7989 | 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] | 7990 | txn->req.chn = s->req; |
| 7991 | txn->rsp.chn = s->rep; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 7992 | |
| 7993 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7994 | |
| 7995 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 7996 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 7997 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 7998 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7999 | if (txn->req.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8000 | memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *)); |
| 8001 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8002 | if (txn->rsp.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8003 | memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
| 8004 | |
| 8005 | if (txn->hdr_idx.v) |
| 8006 | hdr_idx_init(&txn->hdr_idx); |
| 8007 | } |
| 8008 | |
| 8009 | /* to be used at the end of a transaction */ |
| 8010 | void http_end_txn(struct session *s) |
| 8011 | { |
| 8012 | struct http_txn *txn = &s->txn; |
| 8013 | |
| 8014 | /* these ones will have been dynamically allocated */ |
| 8015 | pool_free2(pool2_requri, txn->uri); |
| 8016 | pool_free2(pool2_capture, txn->cli_cookie); |
| 8017 | pool_free2(pool2_capture, txn->srv_cookie); |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8018 | pool_free2(apools.sessid, txn->sessid); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 8019 | pool_free2(pool2_uniqueid, s->unique_id); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8020 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 8021 | s->unique_id = NULL; |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 8022 | txn->sessid = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8023 | txn->uri = NULL; |
| 8024 | txn->srv_cookie = NULL; |
| 8025 | txn->cli_cookie = NULL; |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 8026 | |
| 8027 | if (txn->req.cap) { |
| 8028 | struct cap_hdr *h; |
| 8029 | for (h = s->fe->req_cap; h; h = h->next) |
| 8030 | pool_free2(h->pool, txn->req.cap[h->index]); |
| 8031 | memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *)); |
| 8032 | } |
| 8033 | |
| 8034 | if (txn->rsp.cap) { |
| 8035 | struct cap_hdr *h; |
| 8036 | for (h = s->fe->rsp_cap; h; h = h->next) |
| 8037 | pool_free2(h->pool, txn->rsp.cap[h->index]); |
| 8038 | memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *)); |
| 8039 | } |
| 8040 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8041 | } |
| 8042 | |
| 8043 | /* to be used at the end of a transaction to prepare a new one */ |
| 8044 | void http_reset_txn(struct session *s) |
| 8045 | { |
| 8046 | http_end_txn(s); |
| 8047 | http_init_txn(s); |
| 8048 | |
| 8049 | s->be = s->fe; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8050 | s->logs.logwait = s->fe->to_log; |
Simon Horman | af51495 | 2011-06-21 14:34:57 +0900 | [diff] [blame] | 8051 | session_del_srv_conn(s); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 8052 | s->target = NULL; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 8053 | /* re-init store persistence */ |
| 8054 | s->store_count = 0; |
| 8055 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8056 | s->pend_pos = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8057 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 8058 | s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */ |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8059 | |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 8060 | /* We must trim any excess data from the response buffer, because we |
| 8061 | * may have blocked an invalid response from a server that we don't |
| 8062 | * want to accidentely forward once we disable the analysers, nor do |
| 8063 | * we want those data to come along with next response. A typical |
| 8064 | * example of such data would be from a buggy server responding to |
| 8065 | * a HEAD with some data, or sending more than the advertised |
| 8066 | * content-length. |
| 8067 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8068 | if (unlikely(s->rep->buf->i)) |
| 8069 | s->rep->buf->i = 0; |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 8070 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8071 | s->req->rto = s->fe->timeout.client; |
Willy Tarreau | d04e858 | 2010-05-31 12:31:35 +0200 | [diff] [blame] | 8072 | s->req->wto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8073 | |
Willy Tarreau | d04e858 | 2010-05-31 12:31:35 +0200 | [diff] [blame] | 8074 | s->rep->rto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8075 | s->rep->wto = s->fe->timeout.client; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 8076 | |
| 8077 | s->req->rex = TICK_ETERNITY; |
| 8078 | s->req->wex = TICK_ETERNITY; |
| 8079 | s->req->analyse_exp = TICK_ETERNITY; |
| 8080 | s->rep->rex = TICK_ETERNITY; |
| 8081 | s->rep->wex = TICK_ETERNITY; |
| 8082 | s->rep->analyse_exp = TICK_ETERNITY; |
| 8083 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8084 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8085 | void free_http_req_rules(struct list *r) { |
| 8086 | struct http_req_rule *tr, *pr; |
| 8087 | |
| 8088 | list_for_each_entry_safe(pr, tr, r, list) { |
| 8089 | LIST_DEL(&pr->list); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8090 | if (pr->action == HTTP_REQ_ACT_AUTH) |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8091 | free(pr->arg.auth.realm); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8092 | |
| 8093 | free(pr); |
| 8094 | } |
| 8095 | } |
| 8096 | |
| 8097 | struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
| 8098 | { |
| 8099 | struct http_req_rule *rule; |
| 8100 | int cur_arg; |
| 8101 | |
| 8102 | rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule)); |
| 8103 | if (!rule) { |
| 8104 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8105 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8106 | } |
| 8107 | |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8108 | if (!strcmp(args[0], "allow")) { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8109 | rule->action = HTTP_REQ_ACT_ALLOW; |
| 8110 | cur_arg = 1; |
| 8111 | } else if (!strcmp(args[0], "deny")) { |
| 8112 | rule->action = HTTP_REQ_ACT_DENY; |
| 8113 | cur_arg = 1; |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 8114 | } else if (!strcmp(args[0], "tarpit")) { |
| 8115 | rule->action = HTTP_REQ_ACT_TARPIT; |
| 8116 | cur_arg = 1; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8117 | } else if (!strcmp(args[0], "auth")) { |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8118 | rule->action = HTTP_REQ_ACT_AUTH; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8119 | cur_arg = 1; |
| 8120 | |
| 8121 | while(*args[cur_arg]) { |
| 8122 | if (!strcmp(args[cur_arg], "realm")) { |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8123 | rule->arg.auth.realm = strdup(args[cur_arg + 1]); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8124 | cur_arg+=2; |
| 8125 | continue; |
| 8126 | } else |
| 8127 | break; |
| 8128 | } |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8129 | } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) { |
| 8130 | rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR; |
| 8131 | cur_arg = 1; |
| 8132 | |
| 8133 | if (!*args[cur_arg] || !*args[cur_arg+1] || *args[cur_arg+2]) { |
| 8134 | Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n", |
| 8135 | file, linenum, args[0]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8136 | goto out_err; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8137 | } |
| 8138 | |
| 8139 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 8140 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 8141 | LIST_INIT(&rule->arg.hdr_add.fmt); |
Willy Tarreau | 434c57c | 2013-01-08 01:10:24 +0100 | [diff] [blame^] | 8142 | parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, 0, |
| 8143 | (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 8144 | cur_arg += 2; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8145 | } else if (strcmp(args[0], "redirect") == 0) { |
| 8146 | struct redirect_rule *redir; |
| 8147 | char *errmsg; |
| 8148 | |
| 8149 | if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg)) == NULL) { |
| 8150 | Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 8151 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 8152 | goto out_err; |
| 8153 | } |
| 8154 | |
| 8155 | /* this redirect rule might already contain a parsed condition which |
| 8156 | * we'll pass to the http-request rule. |
| 8157 | */ |
| 8158 | rule->action = HTTP_REQ_ACT_REDIR; |
| 8159 | rule->arg.redir = redir; |
| 8160 | rule->cond = redir->cond; |
| 8161 | redir->cond = NULL; |
| 8162 | cur_arg = 2; |
| 8163 | return rule; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8164 | } else { |
Baptiste Assmann | 116eefe | 2013-01-05 16:02:07 +0100 | [diff] [blame] | 8165 | Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', 'tarpit', 'add-header', 'set-header', but got '%s'%s.\n", |
Willy Tarreau | 5c2e198 | 2012-12-24 12:00:25 +0100 | [diff] [blame] | 8166 | file, linenum, args[0], *args[0] ? "" : " (missing argument)"); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8167 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8168 | } |
| 8169 | |
| 8170 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 8171 | struct acl_cond *cond; |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 8172 | char *errmsg = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8173 | |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 8174 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 8175 | Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n", |
| 8176 | file, linenum, args[0], errmsg); |
| 8177 | free(errmsg); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8178 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8179 | } |
| 8180 | rule->cond = cond; |
| 8181 | } |
| 8182 | else if (*args[cur_arg]) { |
| 8183 | Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or" |
| 8184 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 8185 | file, linenum, args[0], args[cur_arg]); |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8186 | goto out_err; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8187 | } |
| 8188 | |
| 8189 | return rule; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 8190 | out_err: |
| 8191 | free(rule); |
| 8192 | return NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 8193 | } |
| 8194 | |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 8195 | /* Parses a redirect rule. Returns the redirect rule on success or NULL on error, |
| 8196 | * with <err> filled with the error message. |
| 8197 | */ |
| 8198 | struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy, |
| 8199 | const char **args, char **errmsg) |
| 8200 | { |
| 8201 | struct redirect_rule *rule; |
| 8202 | int cur_arg; |
| 8203 | int type = REDIRECT_TYPE_NONE; |
| 8204 | int code = 302; |
| 8205 | const char *destination = NULL; |
| 8206 | const char *cookie = NULL; |
| 8207 | int cookie_set = 0; |
| 8208 | unsigned int flags = REDIRECT_FLAG_NONE; |
| 8209 | struct acl_cond *cond = NULL; |
| 8210 | |
| 8211 | cur_arg = 0; |
| 8212 | while (*(args[cur_arg])) { |
| 8213 | if (strcmp(args[cur_arg], "location") == 0) { |
| 8214 | if (!*args[cur_arg + 1]) |
| 8215 | goto missing_arg; |
| 8216 | |
| 8217 | type = REDIRECT_TYPE_LOCATION; |
| 8218 | cur_arg++; |
| 8219 | destination = args[cur_arg]; |
| 8220 | } |
| 8221 | else if (strcmp(args[cur_arg], "prefix") == 0) { |
| 8222 | if (!*args[cur_arg + 1]) |
| 8223 | goto missing_arg; |
| 8224 | |
| 8225 | type = REDIRECT_TYPE_PREFIX; |
| 8226 | cur_arg++; |
| 8227 | destination = args[cur_arg]; |
| 8228 | } |
| 8229 | else if (strcmp(args[cur_arg], "scheme") == 0) { |
| 8230 | if (!*args[cur_arg + 1]) |
| 8231 | goto missing_arg; |
| 8232 | |
| 8233 | type = REDIRECT_TYPE_SCHEME; |
| 8234 | cur_arg++; |
| 8235 | destination = args[cur_arg]; |
| 8236 | } |
| 8237 | else if (strcmp(args[cur_arg], "set-cookie") == 0) { |
| 8238 | if (!*args[cur_arg + 1]) |
| 8239 | goto missing_arg; |
| 8240 | |
| 8241 | cur_arg++; |
| 8242 | cookie = args[cur_arg]; |
| 8243 | cookie_set = 1; |
| 8244 | } |
| 8245 | else if (strcmp(args[cur_arg], "clear-cookie") == 0) { |
| 8246 | if (!*args[cur_arg + 1]) |
| 8247 | goto missing_arg; |
| 8248 | |
| 8249 | cur_arg++; |
| 8250 | cookie = args[cur_arg]; |
| 8251 | cookie_set = 0; |
| 8252 | } |
| 8253 | else if (strcmp(args[cur_arg], "code") == 0) { |
| 8254 | if (!*args[cur_arg + 1]) |
| 8255 | goto missing_arg; |
| 8256 | |
| 8257 | cur_arg++; |
| 8258 | code = atol(args[cur_arg]); |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 8259 | if (code < 301 || code > 308 || (code > 303 && code < 307)) { |
Willy Tarreau | 4baae24 | 2012-12-27 12:00:31 +0100 | [diff] [blame] | 8260 | memprintf(errmsg, |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 8261 | "'%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] | 8262 | args[cur_arg - 1], args[cur_arg]); |
| 8263 | return NULL; |
| 8264 | } |
| 8265 | } |
| 8266 | else if (!strcmp(args[cur_arg],"drop-query")) { |
| 8267 | flags |= REDIRECT_FLAG_DROP_QS; |
| 8268 | } |
| 8269 | else if (!strcmp(args[cur_arg],"append-slash")) { |
| 8270 | flags |= REDIRECT_FLAG_APPEND_SLASH; |
| 8271 | } |
| 8272 | else if (strcmp(args[cur_arg], "if") == 0 || |
| 8273 | strcmp(args[cur_arg], "unless") == 0) { |
| 8274 | cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg); |
| 8275 | if (!cond) { |
| 8276 | memprintf(errmsg, "error in condition: %s", *errmsg); |
| 8277 | return NULL; |
| 8278 | } |
| 8279 | break; |
| 8280 | } |
| 8281 | else { |
| 8282 | memprintf(errmsg, |
| 8283 | "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')", |
| 8284 | args[cur_arg]); |
| 8285 | return NULL; |
| 8286 | } |
| 8287 | cur_arg++; |
| 8288 | } |
| 8289 | |
| 8290 | if (type == REDIRECT_TYPE_NONE) { |
| 8291 | memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')"); |
| 8292 | return NULL; |
| 8293 | } |
| 8294 | |
| 8295 | rule = (struct redirect_rule *)calloc(1, sizeof(*rule)); |
| 8296 | rule->cond = cond; |
| 8297 | rule->rdr_str = strdup(destination); |
| 8298 | rule->rdr_len = strlen(destination); |
| 8299 | if (cookie) { |
| 8300 | /* depending on cookie_set, either we want to set the cookie, or to clear it. |
| 8301 | * a clear consists in appending "; path=/; Max-Age=0;" at the end. |
| 8302 | */ |
| 8303 | rule->cookie_len = strlen(cookie); |
| 8304 | if (cookie_set) { |
| 8305 | rule->cookie_str = malloc(rule->cookie_len + 10); |
| 8306 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 8307 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10); |
| 8308 | rule->cookie_len += 9; |
| 8309 | } else { |
| 8310 | rule->cookie_str = malloc(rule->cookie_len + 21); |
| 8311 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 8312 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21); |
| 8313 | rule->cookie_len += 20; |
| 8314 | } |
| 8315 | } |
| 8316 | rule->type = type; |
| 8317 | rule->code = code; |
| 8318 | rule->flags = flags; |
| 8319 | LIST_INIT(&rule->list); |
| 8320 | return rule; |
| 8321 | |
| 8322 | missing_arg: |
| 8323 | memprintf(errmsg, "missing argument for '%s'", args[cur_arg]); |
| 8324 | return NULL; |
| 8325 | } |
| 8326 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8327 | /************************************************************************/ |
| 8328 | /* The code below is dedicated to ACL parsing and matching */ |
| 8329 | /************************************************************************/ |
| 8330 | |
| 8331 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8332 | /* This function ensures that the prerequisites for an L7 fetch are ready, |
| 8333 | * which means that a request or response is ready. If some data is missing, |
| 8334 | * 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] | 8335 | * to extract data from L7. If <req_vol> is non-null during a request prefetch, |
| 8336 | * another test is made to ensure the required information is not gone. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8337 | * |
| 8338 | * The function returns : |
| 8339 | * 0 if some data is missing or if the requested data cannot be fetched |
| 8340 | * -1 if it is certain that we'll never have any HTTP message there |
| 8341 | * 1 if an HTTP message is ready |
| 8342 | */ |
| 8343 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8344 | acl_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] | 8345 | const struct arg *args, struct sample *smp, int req_vol) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8346 | { |
| 8347 | struct http_txn *txn = l7; |
| 8348 | struct http_msg *msg = &txn->req; |
| 8349 | |
| 8350 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 8351 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 8352 | */ |
| 8353 | |
| 8354 | if (unlikely(!s || !txn)) |
| 8355 | return 0; |
| 8356 | |
| 8357 | /* Check for a dependency on a request */ |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8358 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8359 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8360 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8361 | if (unlikely(!s->req)) |
| 8362 | return 0; |
| 8363 | |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 8364 | /* If the buffer does not leave enough free space at the end, |
| 8365 | * we must first realign it. |
| 8366 | */ |
| 8367 | if (s->req->buf->p > s->req->buf->data && |
| 8368 | s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite) |
| 8369 | buffer_slow_realign(s->req->buf); |
| 8370 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8371 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 8372 | if ((msg->msg_state == HTTP_MSG_ERROR) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8373 | buffer_full(s->req->buf, global.tune.maxrewrite)) { |
Willy Tarreau | 197e10a | 2012-04-23 19:18:42 +0200 | [diff] [blame] | 8374 | smp->data.uint = 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8375 | return -1; |
| 8376 | } |
| 8377 | |
| 8378 | /* Try to decode HTTP request */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8379 | if (likely(msg->next < s->req->buf->i)) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8380 | http_msg_analyzer(msg, &txn->hdr_idx); |
| 8381 | |
| 8382 | /* Still no valid request ? */ |
| 8383 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 8384 | if ((msg->msg_state == HTTP_MSG_ERROR) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8385 | buffer_full(s->req->buf, global.tune.maxrewrite)) { |
Willy Tarreau | 197e10a | 2012-04-23 19:18:42 +0200 | [diff] [blame] | 8386 | smp->data.uint = 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8387 | return -1; |
| 8388 | } |
| 8389 | /* wait for final state */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8390 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8391 | return 0; |
| 8392 | } |
| 8393 | |
| 8394 | /* OK we just got a valid HTTP request. We have some minor |
| 8395 | * preparation to perform so that further checks can rely |
| 8396 | * on HTTP tests. |
| 8397 | */ |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 8398 | |
| 8399 | /* If the request was parsed but was too large, we must absolutely |
| 8400 | * return an error so that it is not processed. At the moment this |
| 8401 | * cannot happen, but if the parsers are to change in the future, |
| 8402 | * we want this check to be maintained. |
| 8403 | */ |
| 8404 | if (unlikely(s->req->buf->i + s->req->buf->p > |
| 8405 | s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) { |
| 8406 | msg->msg_state = HTTP_MSG_ERROR; |
| 8407 | return 1; |
| 8408 | } |
| 8409 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8410 | 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] | 8411 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 8412 | s->flags |= SN_REDIRECTABLE; |
| 8413 | |
| 8414 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) { |
Willy Tarreau | 197e10a | 2012-04-23 19:18:42 +0200 | [diff] [blame] | 8415 | smp->data.uint = 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8416 | return -1; |
| 8417 | } |
| 8418 | } |
| 8419 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8420 | if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8421 | return 0; /* data might have moved and indexes changed */ |
| 8422 | |
| 8423 | /* otherwise everything's ready for the request */ |
| 8424 | } |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8425 | else { |
| 8426 | /* Check for a dependency on a response */ |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8427 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 8428 | return 0; |
| 8429 | } |
| 8430 | |
| 8431 | /* everything's OK */ |
| 8432 | return 1; |
| 8433 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8434 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 8435 | #define CHECK_HTTP_MESSAGE_FIRST() \ |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8436 | do { int r = acl_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] | 8437 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8438 | #define CHECK_HTTP_MESSAGE_FIRST_PERM() \ |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8439 | do { int r = acl_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] | 8440 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8441 | |
| 8442 | /* 1. Check on METHOD |
| 8443 | * We use the pre-parsed method if it is known, and store its number as an |
| 8444 | * integer. If it is unknown, we use the pointer and the length. |
| 8445 | */ |
Willy Tarreau | 7dcb648 | 2012-04-27 17:52:25 +0200 | [diff] [blame] | 8446 | static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque, char **err) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8447 | { |
| 8448 | int len, meth; |
| 8449 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 8450 | len = strlen(*text); |
| 8451 | meth = find_http_meth(*text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8452 | |
| 8453 | pattern->val.i = meth; |
| 8454 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 8455 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 7dcb648 | 2012-04-27 17:52:25 +0200 | [diff] [blame] | 8456 | if (!pattern->ptr.str) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 8457 | memprintf(err, "out of memory while loading pattern"); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8458 | return 0; |
Willy Tarreau | 7dcb648 | 2012-04-27 17:52:25 +0200 | [diff] [blame] | 8459 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8460 | pattern->len = len; |
| 8461 | } |
| 8462 | return 1; |
| 8463 | } |
| 8464 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 8465 | /* This function fetches the method of current HTTP request and stores |
| 8466 | * it in the global pattern struct as a chunk. There are two possibilities : |
| 8467 | * - if the method is known (not HTTP_METH_OTHER), its identifier is stored |
| 8468 | * in <len> and <ptr> is NULL ; |
| 8469 | * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and |
| 8470 | * <len> to its length. |
| 8471 | * This is intended to be used with acl_match_meth() only. |
| 8472 | */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 8473 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8474 | acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8475 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8476 | { |
| 8477 | int meth; |
| 8478 | struct http_txn *txn = l7; |
| 8479 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8480 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8481 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8482 | meth = txn->meth; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8483 | smp->type = SMP_T_UINT; |
| 8484 | smp->data.uint = meth; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8485 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8486 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 8487 | /* ensure the indexes are not affected */ |
| 8488 | return 0; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8489 | smp->type = SMP_T_CSTR; |
| 8490 | smp->data.str.len = txn->req.sl.rq.m_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8491 | smp->data.str.str = txn->req.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8492 | } |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 8493 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8494 | return 1; |
| 8495 | } |
| 8496 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 8497 | /* See above how the method is stored in the global pattern */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8498 | static int acl_match_meth(struct sample *smp, struct acl_pattern *pattern) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8499 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 8500 | int icase; |
| 8501 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 8502 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8503 | if (smp->type == SMP_T_UINT) { |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 8504 | /* well-known method */ |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8505 | if (smp->data.uint == pattern->val.i) |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 8506 | return ACL_PAT_PASS; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 8507 | return ACL_PAT_FAIL; |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 8508 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8509 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 8510 | /* Uncommon method, only HTTP_METH_OTHER is accepted now */ |
| 8511 | if (pattern->val.i != HTTP_METH_OTHER) |
| 8512 | return ACL_PAT_FAIL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8513 | |
| 8514 | /* Other method, we must compare the strings */ |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8515 | if (pattern->len != smp->data.str.len) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 8516 | return ACL_PAT_FAIL; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 8517 | |
| 8518 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8519 | if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0) || |
| 8520 | (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0)) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 8521 | return ACL_PAT_FAIL; |
| 8522 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8523 | } |
| 8524 | |
| 8525 | /* 2. Check on Request/Status Version |
| 8526 | * We simply compare strings here. |
| 8527 | */ |
Willy Tarreau | 7dcb648 | 2012-04-27 17:52:25 +0200 | [diff] [blame] | 8528 | static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque, char **err) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8529 | { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 8530 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 7dcb648 | 2012-04-27 17:52:25 +0200 | [diff] [blame] | 8531 | if (!pattern->ptr.str) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 8532 | memprintf(err, "out of memory while loading pattern"); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8533 | return 0; |
Willy Tarreau | 7dcb648 | 2012-04-27 17:52:25 +0200 | [diff] [blame] | 8534 | } |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 8535 | pattern->len = strlen(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8536 | return 1; |
| 8537 | } |
| 8538 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 8539 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8540 | acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8541 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8542 | { |
| 8543 | struct http_txn *txn = l7; |
| 8544 | char *ptr; |
| 8545 | int len; |
| 8546 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 8547 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8548 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8549 | len = txn->req.sl.rq.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8550 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.v; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8551 | |
| 8552 | while ((len-- > 0) && (*ptr++ != '/')); |
| 8553 | if (len <= 0) |
| 8554 | return 0; |
| 8555 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8556 | smp->type = SMP_T_CSTR; |
| 8557 | smp->data.str.str = ptr; |
| 8558 | smp->data.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8559 | |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 8560 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8561 | return 1; |
| 8562 | } |
| 8563 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 8564 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8565 | acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8566 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8567 | { |
| 8568 | struct http_txn *txn = l7; |
| 8569 | char *ptr; |
| 8570 | int len; |
| 8571 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 8572 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8573 | |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 8574 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 8575 | return 0; |
| 8576 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8577 | len = txn->rsp.sl.st.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8578 | ptr = txn->rsp.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8579 | |
| 8580 | while ((len-- > 0) && (*ptr++ != '/')); |
| 8581 | if (len <= 0) |
| 8582 | return 0; |
| 8583 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8584 | smp->type = SMP_T_CSTR; |
| 8585 | smp->data.str.str = ptr; |
| 8586 | smp->data.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8587 | |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 8588 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8589 | return 1; |
| 8590 | } |
| 8591 | |
| 8592 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 8593 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8594 | acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8595 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8596 | { |
| 8597 | struct http_txn *txn = l7; |
| 8598 | char *ptr; |
| 8599 | int len; |
| 8600 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 8601 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8602 | |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 8603 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 8604 | return 0; |
| 8605 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8606 | len = txn->rsp.sl.st.c_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8607 | ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8608 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8609 | smp->type = SMP_T_UINT; |
| 8610 | smp->data.uint = __strl2ui(ptr, len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8611 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8612 | return 1; |
| 8613 | } |
| 8614 | |
| 8615 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 8616 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 8617 | smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8618 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8619 | { |
| 8620 | struct http_txn *txn = l7; |
| 8621 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 8622 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8623 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8624 | smp->type = SMP_T_CSTR; |
| 8625 | smp->data.str.len = txn->req.sl.rq.u_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8626 | 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] | 8627 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8628 | return 1; |
| 8629 | } |
| 8630 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 8631 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 8632 | smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8633 | const struct arg *args, struct sample *smp) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 8634 | { |
| 8635 | struct http_txn *txn = l7; |
| 8636 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 8637 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 8638 | |
| 8639 | /* Parse HTTP request */ |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 8640 | url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->conn->addr.to); |
| 8641 | if (((struct sockaddr_in *)&l4->req->cons->conn->addr.to)->sin_family != AF_INET) |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 8642 | return 0; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8643 | smp->type = SMP_T_IPV4; |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 8644 | smp->data.ipv4 = ((struct sockaddr_in *)&l4->req->cons->conn->addr.to)->sin_addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 8645 | |
| 8646 | /* |
| 8647 | * If we are parsing url in frontend space, we prepare backend stage |
| 8648 | * to not parse again the same url ! optimization lazyness... |
| 8649 | */ |
| 8650 | if (px->options & PR_O_HTTP_PROXY) |
| 8651 | l4->flags |= SN_ADDR_SET; |
| 8652 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8653 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 8654 | return 1; |
| 8655 | } |
| 8656 | |
| 8657 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 8658 | smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8659 | const struct arg *args, struct sample *smp) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 8660 | { |
| 8661 | struct http_txn *txn = l7; |
| 8662 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 8663 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 8664 | |
| 8665 | /* Same optimization as url_ip */ |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 8666 | url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->conn->addr.to); |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8667 | smp->type = SMP_T_UINT; |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 8668 | smp->data.uint = ntohs(((struct sockaddr_in *)&l4->req->cons->conn->addr.to)->sin_port); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 8669 | |
| 8670 | if (px->options & PR_O_HTTP_PROXY) |
| 8671 | l4->flags |= SN_ADDR_SET; |
| 8672 | |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 8673 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 8674 | return 1; |
| 8675 | } |
| 8676 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8677 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 8678 | * Accepts an optional argument of type string containing the header field name, |
| 8679 | * and an optional argument of type signed or unsigned integer to request an |
| 8680 | * explicit occurrence of the header. Note that in the event of a missing name, |
| 8681 | * headers are considered from the first one. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8682 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8683 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8684 | smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8685 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8686 | { |
| 8687 | struct http_txn *txn = l7; |
| 8688 | struct hdr_idx *idx = &txn->hdr_idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 8689 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8690 | 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] | 8691 | int occ = 0; |
| 8692 | const char *name_str = NULL; |
| 8693 | int name_len = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8694 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 8695 | if (!ctx) { |
| 8696 | /* first call */ |
| 8697 | ctx = &static_hdr_ctx; |
| 8698 | ctx->idx = 0; |
Willy Tarreau | ffb6f08 | 2013-04-02 23:16:53 +0200 | [diff] [blame] | 8699 | smp->ctx.a[0] = ctx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 8700 | } |
| 8701 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8702 | if (args) { |
| 8703 | if (args[0].type != ARGT_STR) |
| 8704 | return 0; |
| 8705 | name_str = args[0].data.str.str; |
| 8706 | name_len = args[0].data.str.len; |
| 8707 | |
| 8708 | if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT) |
| 8709 | occ = args[1].data.uint; |
| 8710 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 8711 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8712 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8713 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8714 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8715 | /* search for header from the beginning */ |
| 8716 | ctx->idx = 0; |
| 8717 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8718 | if (!occ && !(opt & SMP_OPT_ITERATE)) |
| 8719 | /* no explicit occurrence and single fetch => last header by default */ |
| 8720 | occ = -1; |
| 8721 | |
| 8722 | if (!occ) |
| 8723 | /* prepare to report multiple occurrences for ACL fetches */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8724 | smp->flags |= SMP_F_NOT_LAST; |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 8725 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8726 | smp->type = SMP_T_CSTR; |
| 8727 | smp->flags |= SMP_F_VOL_HDR; |
| 8728 | 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] | 8729 | return 1; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8730 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8731 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8732 | return 0; |
| 8733 | } |
| 8734 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8735 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 8736 | * Accepts exactly 1 argument of type string. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8737 | */ |
| 8738 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8739 | smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8740 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8741 | { |
| 8742 | struct http_txn *txn = l7; |
| 8743 | struct hdr_idx *idx = &txn->hdr_idx; |
| 8744 | struct hdr_ctx ctx; |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8745 | 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] | 8746 | int cnt; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8747 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8748 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 8749 | return 0; |
| 8750 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8751 | CHECK_HTTP_MESSAGE_FIRST(); |
| 8752 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8753 | ctx.idx = 0; |
| 8754 | cnt = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8755 | 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] | 8756 | cnt++; |
| 8757 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8758 | smp->type = SMP_T_UINT; |
| 8759 | smp->data.uint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8760 | smp->flags = SMP_F_VOL_HDR; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8761 | return 1; |
| 8762 | } |
| 8763 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8764 | /* Fetch an HTTP header's integer value. The integer value is returned. It |
| 8765 | * takes a mandatory argument of type string and an optional one of type int |
| 8766 | * to designate a specific occurrence. It returns an unsigned integer, which |
| 8767 | * may or may not be appropriate for everything. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8768 | */ |
| 8769 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8770 | smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8771 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8772 | { |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8773 | int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp); |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8774 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8775 | if (ret > 0) { |
| 8776 | smp->type = SMP_T_UINT; |
| 8777 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 8778 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8779 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 8780 | return ret; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8781 | } |
| 8782 | |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 8783 | /* Fetch an HTTP header's IP value. takes a mandatory argument of type string |
| 8784 | * and an optional one of type int to designate a specific occurrence. |
| 8785 | * It returns an IPv4 or IPv6 address. |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 8786 | */ |
| 8787 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8788 | smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8789 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 8790 | { |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 8791 | int ret; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8792 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8793 | while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp)) > 0) { |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 8794 | if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) { |
| 8795 | smp->type = SMP_T_IPV4; |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 8796 | break; |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 8797 | } else { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 8798 | struct chunk *temp = get_trash_chunk(); |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 8799 | if (smp->data.str.len < temp->size - 1) { |
| 8800 | memcpy(temp->str, smp->data.str.str, smp->data.str.len); |
| 8801 | temp->str[smp->data.str.len] = '\0'; |
| 8802 | if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) { |
| 8803 | smp->type = SMP_T_IPV6; |
| 8804 | break; |
| 8805 | } |
| 8806 | } |
| 8807 | } |
| 8808 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 8809 | /* if the header doesn't match an IP address, fetch next one */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8810 | if (!(smp->flags & SMP_F_NOT_LAST)) |
| 8811 | return 0; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 8812 | } |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 8813 | return ret; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 8814 | } |
| 8815 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8816 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 8817 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 8818 | */ |
| 8819 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 8820 | smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8821 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8822 | { |
| 8823 | struct http_txn *txn = l7; |
| 8824 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8825 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 8826 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8827 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8828 | 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] | 8829 | ptr = http_get_path(txn); |
| 8830 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8831 | return 0; |
| 8832 | |
| 8833 | /* OK, we got the '/' ! */ |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8834 | smp->type = SMP_T_CSTR; |
| 8835 | smp->data.str.str = ptr; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8836 | |
| 8837 | while (ptr < end && *ptr != '?') |
| 8838 | ptr++; |
| 8839 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8840 | smp->data.str.len = ptr - smp->data.str.str; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8841 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8842 | return 1; |
| 8843 | } |
| 8844 | |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 8845 | /* This produces a concatenation of the first occurrence of the Host header |
| 8846 | * followed by the path component if it begins with a slash ('/'). This means |
| 8847 | * that '*' will not be added, resulting in exactly the first Host entry. |
| 8848 | * If no Host header is found, then the path is returned as-is. The returned |
| 8849 | * value is stored in the trash so it does not need to be marked constant. |
| 8850 | */ |
| 8851 | static int |
| 8852 | smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 8853 | const struct arg *args, struct sample *smp) |
| 8854 | { |
| 8855 | struct http_txn *txn = l7; |
| 8856 | char *ptr, *end, *beg; |
| 8857 | struct hdr_ctx ctx; |
| 8858 | |
| 8859 | CHECK_HTTP_MESSAGE_FIRST(); |
| 8860 | |
| 8861 | ctx.idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8862 | 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] | 8863 | !ctx.vlen) |
| 8864 | return smp_fetch_path(px, l4, l7, opt, args, smp); |
| 8865 | |
| 8866 | /* 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] | 8867 | memcpy(trash.str, ctx.line + ctx.val, ctx.vlen); |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 8868 | smp->type = SMP_T_STR; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8869 | smp->data.str.str = trash.str; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 8870 | smp->data.str.len = ctx.vlen; |
| 8871 | |
| 8872 | /* now retrieve the path */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8873 | 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] | 8874 | beg = http_get_path(txn); |
| 8875 | if (!beg) |
| 8876 | beg = end; |
| 8877 | |
| 8878 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 8879 | |
| 8880 | if (beg < ptr && *beg == '/') { |
| 8881 | memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg); |
| 8882 | smp->data.str.len += ptr - beg; |
| 8883 | } |
| 8884 | |
| 8885 | smp->flags = SMP_F_VOL_1ST; |
| 8886 | return 1; |
| 8887 | } |
| 8888 | |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 8889 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 8890 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 8891 | * This means that '*' will not be added, resulting in exactly the first Host |
| 8892 | * entry. If no Host header is found, then the path is used. The resulting value |
| 8893 | * is hashed using the url hash followed by a full avalanche hash and provides a |
| 8894 | * 32-bit integer value. This fetch is useful for tracking per-URL activity on |
| 8895 | * high-traffic sites without having to store whole paths. |
| 8896 | */ |
| 8897 | static int |
| 8898 | smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 8899 | const struct arg *args, struct sample *smp) |
| 8900 | { |
| 8901 | struct http_txn *txn = l7; |
| 8902 | struct hdr_ctx ctx; |
| 8903 | unsigned int hash = 0; |
| 8904 | char *ptr, *beg, *end; |
| 8905 | int len; |
| 8906 | |
| 8907 | CHECK_HTTP_MESSAGE_FIRST(); |
| 8908 | |
| 8909 | ctx.idx = 0; |
| 8910 | if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) { |
| 8911 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 8912 | ptr = ctx.line + ctx.val; |
| 8913 | len = ctx.vlen; |
| 8914 | while (len--) |
| 8915 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 8916 | } |
| 8917 | |
| 8918 | /* now retrieve the path */ |
| 8919 | end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
| 8920 | beg = http_get_path(txn); |
| 8921 | if (!beg) |
| 8922 | beg = end; |
| 8923 | |
| 8924 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 8925 | |
| 8926 | if (beg < ptr && *beg == '/') { |
| 8927 | while (beg < ptr) |
| 8928 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 8929 | } |
| 8930 | hash = full_hash(hash); |
| 8931 | |
| 8932 | smp->type = SMP_T_UINT; |
| 8933 | smp->data.uint = hash; |
| 8934 | smp->flags = SMP_F_VOL_1ST; |
| 8935 | return 1; |
| 8936 | } |
| 8937 | |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 8938 | /* This concatenates the source address with the 32-bit hash of the Host and |
| 8939 | * URL as returned by smp_fetch_base32(). The idea is to have per-source and |
| 8940 | * per-url counters. The result is a binary block from 8 to 20 bytes depending |
| 8941 | * on the source address length. The URL hash is stored before the address so |
| 8942 | * that in environments where IPv6 is insignificant, truncating the output to |
| 8943 | * 8 bytes would still work. |
| 8944 | */ |
| 8945 | static int |
| 8946 | smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 8947 | const struct arg *args, struct sample *smp) |
| 8948 | { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 8949 | struct chunk *temp; |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 8950 | |
| 8951 | if (!smp_fetch_base32(px, l4, l7, opt, args, smp)) |
| 8952 | return 0; |
| 8953 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 8954 | temp = get_trash_chunk(); |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 8955 | memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint)); |
| 8956 | temp->len += sizeof(smp->data.uint); |
| 8957 | |
| 8958 | switch (l4->si[0].conn->addr.from.ss_family) { |
| 8959 | case AF_INET: |
| 8960 | memcpy(temp->str + temp->len, &((struct sockaddr_in *)&l4->si[0].conn->addr.from)->sin_addr, 4); |
| 8961 | temp->len += 4; |
| 8962 | break; |
| 8963 | case AF_INET6: |
| 8964 | memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)(&l4->si[0].conn->addr.from))->sin6_addr, 16); |
| 8965 | temp->len += 16; |
| 8966 | break; |
| 8967 | default: |
| 8968 | return 0; |
| 8969 | } |
| 8970 | |
| 8971 | smp->data.str = *temp; |
| 8972 | smp->type = SMP_T_BIN; |
| 8973 | return 1; |
| 8974 | } |
| 8975 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8976 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8977 | acl_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8978 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8979 | { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8980 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 8981 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 8982 | */ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8983 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8984 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8985 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8986 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 197e10a | 2012-04-23 19:18:42 +0200 | [diff] [blame] | 8987 | smp->data.uint = 1; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8988 | return 1; |
| 8989 | } |
| 8990 | |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 8991 | /* return a valid test if the current request is the first one on the connection */ |
| 8992 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8993 | acl_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8994 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 8995 | { |
| 8996 | if (!s) |
| 8997 | return 0; |
| 8998 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8999 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 197e10a | 2012-04-23 19:18:42 +0200 | [diff] [blame] | 9000 | smp->data.uint = !(s->txn.flags & TX_NOT_FIRST); |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 9001 | return 1; |
| 9002 | } |
| 9003 | |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 9004 | /* Accepts exactly 1 argument of type userlist */ |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 9005 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 9006 | acl_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9007 | const struct arg *args, struct sample *smp) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 9008 | { |
| 9009 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9010 | if (!args || args->type != ARGT_USR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 9011 | return 0; |
| 9012 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9013 | CHECK_HTTP_MESSAGE_FIRST(); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 9014 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 9015 | if (!get_http_auth(l4)) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 9016 | return 0; |
| 9017 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9018 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9019 | 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] | 9020 | return 1; |
| 9021 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9022 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 9023 | /* Accepts exactly 1 argument of type userlist */ |
| 9024 | static int |
| 9025 | acl_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 9026 | const struct arg *args, struct sample *smp) |
| 9027 | { |
| 9028 | |
| 9029 | if (!args || args->type != ARGT_USR) |
| 9030 | return 0; |
| 9031 | |
| 9032 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9033 | |
| 9034 | if (!get_http_auth(l4)) |
| 9035 | return 0; |
| 9036 | |
| 9037 | /* acl_match_auth() will need several information at once */ |
| 9038 | smp->ctx.a[0] = args->data.usr; /* user list */ |
| 9039 | smp->ctx.a[1] = l4->txn.auth.user; /* user name */ |
| 9040 | smp->ctx.a[2] = l4->txn.auth.pass; /* password */ |
| 9041 | |
| 9042 | /* if the user does not belong to the userlist or has a wrong password, |
| 9043 | * report that it unconditionally does not match. Otherwise we return |
| 9044 | * a non-zero integer which will be ignored anyway since all the params |
| 9045 | * that acl_match_auth() will use are in test->ctx.a[0,1,2]. |
| 9046 | */ |
| 9047 | smp->type = SMP_T_BOOL; |
| 9048 | smp->data.uint = check_user(args->data.usr, 0, l4->txn.auth.user, l4->txn.auth.pass); |
| 9049 | if (smp->data.uint) |
| 9050 | smp->type = SMP_T_UINT; |
| 9051 | |
| 9052 | return 1; |
| 9053 | } |
| 9054 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9055 | /* Try to find the next occurrence of a cookie name in a cookie header value. |
| 9056 | * The lookup begins at <hdr>. The pointer and size of the next occurrence of |
| 9057 | * the cookie value is returned into *value and *value_l, and the function |
| 9058 | * returns a pointer to the next pointer to search from if the value was found. |
| 9059 | * Otherwise if the cookie was not found, NULL is returned and neither value |
| 9060 | * nor value_l are touched. The input <hdr> string should first point to the |
| 9061 | * header's value, and the <hdr_end> pointer must point to the first character |
| 9062 | * not part of the value. <list> must be non-zero if value may represent a list |
| 9063 | * of values (cookie headers). This makes it faster to abort parsing when no |
| 9064 | * list is expected. |
| 9065 | */ |
| 9066 | static char * |
| 9067 | extract_cookie_value(char *hdr, const char *hdr_end, |
| 9068 | char *cookie_name, size_t cookie_name_l, int list, |
Willy Tarreau | 3fb818c | 2012-04-11 17:21:08 +0200 | [diff] [blame] | 9069 | char **value, int *value_l) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9070 | { |
| 9071 | char *equal, *att_end, *att_beg, *val_beg, *val_end; |
| 9072 | char *next; |
| 9073 | |
| 9074 | /* we search at least a cookie name followed by an equal, and more |
| 9075 | * generally something like this : |
| 9076 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 9077 | */ |
| 9078 | for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) { |
| 9079 | /* Iterate through all cookies on this line */ |
| 9080 | |
| 9081 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 9082 | att_beg++; |
| 9083 | |
| 9084 | /* find att_end : this is the first character after the last non |
| 9085 | * space before the equal. It may be equal to hdr_end. |
| 9086 | */ |
| 9087 | equal = att_end = att_beg; |
| 9088 | |
| 9089 | while (equal < hdr_end) { |
| 9090 | if (*equal == '=' || *equal == ';' || (list && *equal == ',')) |
| 9091 | break; |
| 9092 | if (http_is_spht[(unsigned char)*equal++]) |
| 9093 | continue; |
| 9094 | att_end = equal; |
| 9095 | } |
| 9096 | |
| 9097 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 9098 | * is between <att_beg> and <equal>, both may be identical. |
| 9099 | */ |
| 9100 | |
| 9101 | /* look for end of cookie if there is an equal sign */ |
| 9102 | if (equal < hdr_end && *equal == '=') { |
| 9103 | /* look for the beginning of the value */ |
| 9104 | val_beg = equal + 1; |
| 9105 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 9106 | val_beg++; |
| 9107 | |
| 9108 | /* find the end of the value, respecting quotes */ |
| 9109 | next = find_cookie_value_end(val_beg, hdr_end); |
| 9110 | |
| 9111 | /* make val_end point to the first white space or delimitor after the value */ |
| 9112 | val_end = next; |
| 9113 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 9114 | val_end--; |
| 9115 | } else { |
| 9116 | val_beg = val_end = next = equal; |
| 9117 | } |
| 9118 | |
| 9119 | /* We have nothing to do with attributes beginning with '$'. However, |
| 9120 | * they will automatically be removed if a header before them is removed, |
| 9121 | * since they're supposed to be linked together. |
| 9122 | */ |
| 9123 | if (*att_beg == '$') |
| 9124 | continue; |
| 9125 | |
| 9126 | /* Ignore cookies with no equal sign */ |
| 9127 | if (equal == next) |
| 9128 | continue; |
| 9129 | |
| 9130 | /* Now we have the cookie name between att_beg and att_end, and |
| 9131 | * its value between val_beg and val_end. |
| 9132 | */ |
| 9133 | |
| 9134 | if (att_end - att_beg == cookie_name_l && |
| 9135 | memcmp(att_beg, cookie_name, cookie_name_l) == 0) { |
| 9136 | /* let's return this value and indicate where to go on from */ |
| 9137 | *value = val_beg; |
| 9138 | *value_l = val_end - val_beg; |
| 9139 | return next + 1; |
| 9140 | } |
| 9141 | |
| 9142 | /* Set-Cookie headers only have the name in the first attr=value part */ |
| 9143 | if (!list) |
| 9144 | break; |
| 9145 | } |
| 9146 | |
| 9147 | return NULL; |
| 9148 | } |
| 9149 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9150 | /* 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] | 9151 | * 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] | 9152 | * 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] | 9153 | * 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] | 9154 | * 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] | 9155 | * Accepts exactly 1 argument of type string. If the input options indicate |
| 9156 | * that no iterating is desired, then only last value is fetched if any. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9157 | */ |
| 9158 | static int |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 9159 | smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 9160 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9161 | { |
| 9162 | struct http_txn *txn = l7; |
| 9163 | struct hdr_idx *idx = &txn->hdr_idx; |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 9164 | struct hdr_ctx *ctx = smp->ctx.a[2]; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9165 | const struct http_msg *msg; |
| 9166 | const char *hdr_name; |
| 9167 | int hdr_name_len; |
| 9168 | char *sol; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 9169 | int occ = 0; |
| 9170 | int found = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9171 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9172 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 9173 | return 0; |
| 9174 | |
Willy Tarreau | a890d07 | 2013-04-02 12:01:06 +0200 | [diff] [blame] | 9175 | if (!ctx) { |
| 9176 | /* first call */ |
| 9177 | ctx = &static_hdr_ctx; |
| 9178 | ctx->idx = 0; |
| 9179 | smp->ctx.a[2] = ctx; |
| 9180 | } |
| 9181 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9182 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9183 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 9184 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9185 | msg = &txn->req; |
| 9186 | hdr_name = "Cookie"; |
| 9187 | hdr_name_len = 6; |
| 9188 | } else { |
| 9189 | msg = &txn->rsp; |
| 9190 | hdr_name = "Set-Cookie"; |
| 9191 | hdr_name_len = 10; |
| 9192 | } |
| 9193 | |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 9194 | if (!occ && !(opt & SMP_OPT_ITERATE)) |
| 9195 | /* no explicit occurrence and single fetch => last cookie by default */ |
| 9196 | occ = -1; |
| 9197 | |
| 9198 | /* OK so basically here, either we want only one value and it's the |
| 9199 | * last one, or we want to iterate over all of them and we fetch the |
| 9200 | * next one. |
| 9201 | */ |
| 9202 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9203 | sol = msg->chn->buf->p; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9204 | if (!(smp->flags & SMP_F_NOT_LAST)) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9205 | /* search for the header from the beginning, we must first initialize |
| 9206 | * the search parameters. |
| 9207 | */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9208 | smp->ctx.a[0] = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9209 | ctx->idx = 0; |
| 9210 | } |
| 9211 | |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 9212 | smp->flags |= SMP_F_VOL_HDR; |
| 9213 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9214 | while (1) { |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9215 | /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */ |
| 9216 | if (!smp->ctx.a[0]) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9217 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx)) |
| 9218 | goto out; |
| 9219 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9220 | if (ctx->vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9221 | continue; |
| 9222 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9223 | smp->ctx.a[0] = ctx->line + ctx->val; |
| 9224 | smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9225 | } |
| 9226 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9227 | smp->type = SMP_T_CSTR; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9228 | 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] | 9229 | args->data.str.str, args->data.str.len, |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 9230 | (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9231 | &smp->data.str.str, |
| 9232 | &smp->data.str.len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9233 | if (smp->ctx.a[0]) { |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 9234 | found = 1; |
| 9235 | if (occ >= 0) { |
| 9236 | /* one value was returned into smp->data.str.{str,len} */ |
| 9237 | smp->flags |= SMP_F_NOT_LAST; |
| 9238 | return 1; |
| 9239 | } |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9240 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 9241 | /* if we're looking for last occurrence, let's loop */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9242 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 9243 | /* all cookie headers and values were scanned. If we're looking for the |
| 9244 | * last occurrence, we may return it now. |
| 9245 | */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9246 | out: |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9247 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 9248 | return found; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9249 | } |
| 9250 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9251 | /* 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] | 9252 | * match the name in args and args->data.str.len. If <multi> is non-null, then |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9253 | * multiple cookies may be parsed on the same line. |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 9254 | * Accepts exactly 1 argument of type string. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9255 | */ |
| 9256 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 9257 | acl_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9258 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9259 | { |
| 9260 | struct http_txn *txn = l7; |
| 9261 | struct hdr_idx *idx = &txn->hdr_idx; |
| 9262 | struct hdr_ctx ctx; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9263 | const struct http_msg *msg; |
| 9264 | const char *hdr_name; |
| 9265 | int hdr_name_len; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9266 | int cnt; |
| 9267 | char *val_beg, *val_end; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9268 | char *sol; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9269 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9270 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 9271 | return 0; |
| 9272 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9273 | CHECK_HTTP_MESSAGE_FIRST(); |
| 9274 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 9275 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 9276 | msg = &txn->req; |
| 9277 | hdr_name = "Cookie"; |
| 9278 | hdr_name_len = 6; |
| 9279 | } else { |
| 9280 | msg = &txn->rsp; |
| 9281 | hdr_name = "Set-Cookie"; |
| 9282 | hdr_name_len = 10; |
| 9283 | } |
| 9284 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9285 | sol = msg->chn->buf->p; |
Willy Tarreau | 46787ed | 2012-04-11 17:28:40 +0200 | [diff] [blame] | 9286 | val_end = val_beg = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9287 | ctx.idx = 0; |
| 9288 | cnt = 0; |
| 9289 | |
| 9290 | while (1) { |
| 9291 | /* Note: val_beg == NULL every time we need to fetch a new header */ |
| 9292 | if (!val_beg) { |
| 9293 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx)) |
| 9294 | break; |
| 9295 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9296 | if (ctx.vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9297 | continue; |
| 9298 | |
| 9299 | val_beg = ctx.line + ctx.val; |
| 9300 | val_end = val_beg + ctx.vlen; |
| 9301 | } |
| 9302 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9303 | smp->type = SMP_T_CSTR; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9304 | while ((val_beg = extract_cookie_value(val_beg, val_end, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 9305 | args->data.str.str, args->data.str.len, |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 9306 | (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9307 | &smp->data.str.str, |
| 9308 | &smp->data.str.len))) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9309 | cnt++; |
| 9310 | } |
| 9311 | } |
| 9312 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 9313 | smp->data.uint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 9314 | smp->flags |= SMP_F_VOL_HDR; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 9315 | return 1; |
| 9316 | } |
| 9317 | |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 9318 | /* Fetch an cookie's integer value. The integer value is returned. It |
| 9319 | * takes a mandatory argument of type string. It relies on smp_fetch_cookie(). |
| 9320 | */ |
| 9321 | static int |
| 9322 | smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 9323 | const struct arg *args, struct sample *smp) |
| 9324 | { |
| 9325 | int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp); |
| 9326 | |
| 9327 | if (ret > 0) { |
| 9328 | smp->type = SMP_T_UINT; |
| 9329 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 9330 | } |
| 9331 | |
| 9332 | return ret; |
| 9333 | } |
| 9334 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9335 | /************************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 9336 | /* The code below is dedicated to sample fetches */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 9337 | /************************************************************************/ |
| 9338 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9339 | /* |
| 9340 | * Given a path string and its length, find the position of beginning of the |
| 9341 | * query string. Returns NULL if no query string is found in the path. |
| 9342 | * |
| 9343 | * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22: |
| 9344 | * |
| 9345 | * find_query_string(path, n) points to "yo=mama;ye=daddy" string. |
| 9346 | */ |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9347 | 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] | 9348 | { |
| 9349 | char *p; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 9350 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9351 | p = memchr(path, delim, path_l); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9352 | return p ? p + 1 : NULL; |
| 9353 | } |
| 9354 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9355 | static inline int is_param_delimiter(char c, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9356 | { |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9357 | return c == '&' || c == ';' || c == delim; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9358 | } |
| 9359 | |
| 9360 | /* |
| 9361 | * Given a url parameter, find the starting position of the first occurence, |
| 9362 | * or NULL if the parameter is not found. |
| 9363 | * |
| 9364 | * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye", |
| 9365 | * the function will return query_string+8. |
| 9366 | */ |
| 9367 | static char* |
| 9368 | find_url_param_pos(char* query_string, size_t query_string_l, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9369 | char* url_param_name, size_t url_param_name_l, |
| 9370 | char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9371 | { |
| 9372 | char *pos, *last; |
| 9373 | |
| 9374 | pos = query_string; |
| 9375 | last = query_string + query_string_l - url_param_name_l - 1; |
| 9376 | |
| 9377 | while (pos <= last) { |
| 9378 | if (pos[url_param_name_l] == '=') { |
| 9379 | if (memcmp(pos, url_param_name, url_param_name_l) == 0) |
| 9380 | return pos; |
| 9381 | pos += url_param_name_l + 1; |
| 9382 | } |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9383 | while (pos <= last && !is_param_delimiter(*pos, delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9384 | pos++; |
| 9385 | pos++; |
| 9386 | } |
| 9387 | return NULL; |
| 9388 | } |
| 9389 | |
| 9390 | /* |
| 9391 | * Given a url parameter name, returns its value and size into *value and |
| 9392 | * *value_l respectively, and returns non-zero. If the parameter is not found, |
| 9393 | * zero is returned and value/value_l are not touched. |
| 9394 | */ |
| 9395 | static int |
| 9396 | find_url_param_value(char* path, size_t path_l, |
| 9397 | char* url_param_name, size_t url_param_name_l, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9398 | char** value, int* value_l, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9399 | { |
| 9400 | char *query_string, *qs_end; |
| 9401 | char *arg_start; |
| 9402 | char *value_start, *value_end; |
| 9403 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9404 | query_string = find_param_list(path, path_l, delim); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9405 | if (!query_string) |
| 9406 | return 0; |
| 9407 | |
| 9408 | qs_end = path + path_l; |
| 9409 | arg_start = find_url_param_pos(query_string, qs_end - query_string, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9410 | url_param_name, url_param_name_l, |
| 9411 | delim); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9412 | if (!arg_start) |
| 9413 | return 0; |
| 9414 | |
| 9415 | value_start = arg_start + url_param_name_l + 1; |
| 9416 | value_end = value_start; |
| 9417 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9418 | while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9419 | value_end++; |
| 9420 | |
| 9421 | *value = value_start; |
| 9422 | *value_l = value_end - value_start; |
Willy Tarreau | 0013433 | 2011-01-04 14:57:34 +0100 | [diff] [blame] | 9423 | return value_end != value_start; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9424 | } |
| 9425 | |
| 9426 | static int |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 9427 | smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 9428 | const struct arg *args, struct sample *smp) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9429 | { |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9430 | char delim = '?'; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9431 | struct http_txn *txn = l7; |
| 9432 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 9433 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9434 | if (!args || args[0].type != ARGT_STR || |
| 9435 | (args[1].type && args[1].type != ARGT_STR)) |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 9436 | return 0; |
| 9437 | |
| 9438 | CHECK_HTTP_MESSAGE_FIRST(); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9439 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9440 | if (args[1].type) |
| 9441 | delim = *args[1].data.str.str; |
| 9442 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9443 | 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] | 9444 | args->data.str.str, args->data.str.len, |
| 9445 | &smp->data.str.str, &smp->data.str.len, |
| 9446 | delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9447 | return 0; |
| 9448 | |
Willy Tarreau | b8c8f1f | 2012-04-23 22:38:26 +0200 | [diff] [blame] | 9449 | smp->type = SMP_T_CSTR; |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 9450 | smp->flags = SMP_F_VOL_1ST; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9451 | return 1; |
| 9452 | } |
| 9453 | |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 9454 | /* Return the signed integer value for the specified url parameter (see url_param |
| 9455 | * above). |
| 9456 | */ |
| 9457 | static int |
| 9458 | smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 9459 | const struct arg *args, struct sample *smp) |
| 9460 | { |
| 9461 | int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp); |
| 9462 | |
| 9463 | if (ret > 0) { |
| 9464 | smp->type = SMP_T_UINT; |
| 9465 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 9466 | } |
| 9467 | |
| 9468 | return ret; |
| 9469 | } |
| 9470 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9471 | /* This function is used to validate the arguments passed to any "hdr" fetch |
| 9472 | * keyword. These keywords support an optional positive or negative occurrence |
| 9473 | * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It |
| 9474 | * is assumed that the types are already the correct ones. Returns 0 on error, |
| 9475 | * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an |
| 9476 | * error message in case of error, that the caller is responsible for freeing. |
| 9477 | * The initial location must either be freeable or NULL. |
| 9478 | */ |
| 9479 | static int val_hdr(struct arg *arg, char **err_msg) |
| 9480 | { |
| 9481 | 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] | 9482 | memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY); |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9483 | return 0; |
| 9484 | } |
| 9485 | return 1; |
| 9486 | } |
| 9487 | |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 9488 | /************************************************************************/ |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 9489 | /* All supported ACL keywords must be declared here. */ |
| 9490 | /************************************************************************/ |
| 9491 | |
| 9492 | /* Note: must not be declared <const> as its list will be overwritten. |
| 9493 | * Please take care of keeping this list alphabetically sorted. |
| 9494 | */ |
| 9495 | static struct acl_kw_list acl_kws = {{ },{ |
Willy Tarreau | e0db1e8 | 2013-01-04 16:31:47 +0100 | [diff] [blame] | 9496 | { "base", acl_parse_str, smp_fetch_base, acl_match_str, ACL_USE_L7REQ_VOLATILE, 0 }, |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 9497 | { "base_beg", acl_parse_str, smp_fetch_base, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9498 | { "base_dir", acl_parse_str, smp_fetch_base, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9499 | { "base_dom", acl_parse_str, smp_fetch_base, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9500 | { "base_end", acl_parse_str, smp_fetch_base, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9501 | { "base_len", acl_parse_int, smp_fetch_base, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9502 | { "base_reg", acl_parse_reg, smp_fetch_base, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9503 | { "base_sub", acl_parse_str, smp_fetch_base, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9504 | |
Willy Tarreau | e0db1e8 | 2013-01-04 16:31:47 +0100 | [diff] [blame] | 9505 | { "cook", acl_parse_str, smp_fetch_cookie, acl_match_str, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) }, |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 9506 | { "cook_beg", acl_parse_str, smp_fetch_cookie, acl_match_beg, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 9507 | { "cook_cnt", acl_parse_int, acl_fetch_cookie_cnt, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) }, |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 9508 | { "cook_dir", acl_parse_str, smp_fetch_cookie, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) }, |
| 9509 | { "cook_dom", acl_parse_str, smp_fetch_cookie, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) }, |
| 9510 | { "cook_end", acl_parse_str, smp_fetch_cookie, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) }, |
| 9511 | { "cook_len", acl_parse_int, smp_fetch_cookie, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) }, |
| 9512 | { "cook_reg", acl_parse_reg, smp_fetch_cookie, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) }, |
| 9513 | { "cook_sub", acl_parse_str, smp_fetch_cookie, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) }, |
| 9514 | { "cook_val", acl_parse_int, smp_fetch_cookie_val, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 9515 | |
Willy Tarreau | e0db1e8 | 2013-01-04 16:31:47 +0100 | [diff] [blame] | 9516 | { "hdr", acl_parse_str, smp_fetch_hdr, acl_match_str, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9517 | { "hdr_beg", acl_parse_str, smp_fetch_hdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 9518 | { "hdr_cnt", acl_parse_int, smp_fetch_hdr_cnt, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) }, |
| 9519 | { "hdr_dir", acl_parse_str, smp_fetch_hdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 9520 | { "hdr_dom", acl_parse_str, smp_fetch_hdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 9521 | { "hdr_end", acl_parse_str, smp_fetch_hdr, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
Willy Tarreau | e0db1e8 | 2013-01-04 16:31:47 +0100 | [diff] [blame] | 9522 | { "hdr_ip", acl_parse_ip, smp_fetch_hdr_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9523 | { "hdr_len", acl_parse_int, smp_fetch_hdr, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 9524 | { "hdr_reg", acl_parse_reg, smp_fetch_hdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 9525 | { "hdr_sub", acl_parse_str, smp_fetch_hdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 9526 | { "hdr_val", acl_parse_int, smp_fetch_hdr_val, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 9527 | |
| 9528 | { "http_auth", acl_parse_nothing, acl_fetch_http_auth, acl_match_nothing, ACL_USE_L7REQ_VOLATILE, ARG1(0,USR) }, |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 9529 | { "http_auth_group", acl_parse_strcat, acl_fetch_http_auth_grp, acl_match_auth, ACL_USE_L7REQ_VOLATILE, ARG1(0,USR) }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 9530 | { "http_first_req", acl_parse_nothing, acl_fetch_http_first_req, acl_match_nothing, ACL_USE_L7REQ_PERMANENT, 0 }, |
| 9531 | |
| 9532 | { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT, 0 }, |
| 9533 | |
Willy Tarreau | e0db1e8 | 2013-01-04 16:31:47 +0100 | [diff] [blame] | 9534 | { "path", acl_parse_str, smp_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE, 0 }, |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 9535 | { "path_beg", acl_parse_str, smp_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9536 | { "path_dir", acl_parse_str, smp_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9537 | { "path_dom", acl_parse_str, smp_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9538 | { "path_end", acl_parse_str, smp_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9539 | { "path_len", acl_parse_int, smp_fetch_path, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9540 | { "path_reg", acl_parse_reg, smp_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9541 | { "path_sub", acl_parse_str, smp_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 9542 | |
| 9543 | { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT, 0 }, |
Willy Tarreau | e0db1e8 | 2013-01-04 16:31:47 +0100 | [diff] [blame] | 9544 | { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9545 | { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE, 0 }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 9546 | |
Willy Tarreau | e0db1e8 | 2013-01-04 16:31:47 +0100 | [diff] [blame] | 9547 | { "scook", acl_parse_str, smp_fetch_cookie, acl_match_str, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) }, |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 9548 | { "scook_beg", acl_parse_str, smp_fetch_cookie, acl_match_beg, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 9549 | { "scook_cnt", acl_parse_int, acl_fetch_cookie_cnt, acl_match_int, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) }, |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 9550 | { "scook_dir", acl_parse_str, smp_fetch_cookie, acl_match_dir, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) }, |
| 9551 | { "scook_dom", acl_parse_str, smp_fetch_cookie, acl_match_dom, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) }, |
| 9552 | { "scook_end", acl_parse_str, smp_fetch_cookie, acl_match_end, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) }, |
| 9553 | { "scook_len", acl_parse_int, smp_fetch_cookie, acl_match_len, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) }, |
| 9554 | { "scook_reg", acl_parse_reg, smp_fetch_cookie, acl_match_reg, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) }, |
| 9555 | { "scook_sub", acl_parse_str, smp_fetch_cookie, acl_match_sub, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) }, |
| 9556 | { "scook_val", acl_parse_int, smp_fetch_cookie_val, acl_match_int, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 9557 | |
Willy Tarreau | e0db1e8 | 2013-01-04 16:31:47 +0100 | [diff] [blame] | 9558 | { "shdr", acl_parse_str, smp_fetch_hdr, acl_match_str, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9559 | { "shdr_beg", acl_parse_str, smp_fetch_hdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 9560 | { "shdr_cnt", acl_parse_int, smp_fetch_hdr_cnt, acl_match_int, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) }, |
| 9561 | { "shdr_dir", acl_parse_str, smp_fetch_hdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 9562 | { "shdr_dom", acl_parse_str, smp_fetch_hdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 9563 | { "shdr_end", acl_parse_str, smp_fetch_hdr, acl_match_end, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
Willy Tarreau | e0db1e8 | 2013-01-04 16:31:47 +0100 | [diff] [blame] | 9564 | { "shdr_ip", acl_parse_ip, smp_fetch_hdr_ip, acl_match_ip, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9565 | { "shdr_len", acl_parse_int, smp_fetch_hdr, acl_match_len, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 9566 | { "shdr_reg", acl_parse_reg, smp_fetch_hdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 9567 | { "shdr_sub", acl_parse_str, smp_fetch_hdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 9568 | { "shdr_val", acl_parse_int, smp_fetch_hdr_val, acl_match_int, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 9569 | |
| 9570 | { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT, 0 }, |
| 9571 | |
Willy Tarreau | e0db1e8 | 2013-01-04 16:31:47 +0100 | [diff] [blame] | 9572 | { "url", acl_parse_str, smp_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE, 0 }, |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 9573 | { "url_beg", acl_parse_str, smp_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9574 | { "url_dir", acl_parse_str, smp_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9575 | { "url_dom", acl_parse_str, smp_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9576 | { "url_end", acl_parse_str, smp_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 }, |
Willy Tarreau | e0db1e8 | 2013-01-04 16:31:47 +0100 | [diff] [blame] | 9577 | { "url_ip", acl_parse_ip, smp_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE, 0 }, |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 9578 | { "url_len", acl_parse_int, smp_fetch_url, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9579 | { "url_port", acl_parse_int, smp_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9580 | { "url_reg", acl_parse_reg, smp_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9581 | { "url_sub", acl_parse_str, smp_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 9582 | |
Willy Tarreau | e0db1e8 | 2013-01-04 16:31:47 +0100 | [diff] [blame] | 9583 | { "urlp", acl_parse_str, smp_fetch_url_param, acl_match_str, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 9584 | { "urlp_beg", acl_parse_str, smp_fetch_url_param, acl_match_beg, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) }, |
| 9585 | { "urlp_dir", acl_parse_str, smp_fetch_url_param, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) }, |
| 9586 | { "urlp_dom", acl_parse_str, smp_fetch_url_param, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) }, |
| 9587 | { "urlp_end", acl_parse_str, smp_fetch_url_param, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) }, |
Willy Tarreau | e0db1e8 | 2013-01-04 16:31:47 +0100 | [diff] [blame] | 9588 | { "urlp_ip", acl_parse_ip, smp_fetch_url_param, acl_match_ip, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 9589 | { "urlp_len", acl_parse_int, smp_fetch_url_param, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) }, |
| 9590 | { "urlp_reg", acl_parse_reg, smp_fetch_url_param, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) }, |
| 9591 | { "urlp_sub", acl_parse_str, smp_fetch_url_param, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) }, |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 9592 | { "urlp_val", acl_parse_int, smp_fetch_url_param_val, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 9593 | |
| 9594 | { NULL, NULL, NULL, NULL }, |
| 9595 | }}; |
| 9596 | |
| 9597 | /************************************************************************/ |
| 9598 | /* All supported pattern keywords must be declared here. */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 9599 | /************************************************************************/ |
| 9600 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 9601 | static struct sample_fetch_kw_list sample_fetch_keywords = {{ },{ |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 9602 | { "hdr", smp_fetch_hdr, ARG2(1,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV|SMP_USE_HRSHV }, |
| 9603 | { "base", smp_fetch_base, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV }, |
| 9604 | { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 9605 | { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 9606 | { "path", smp_fetch_path, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV }, |
| 9607 | { "url", smp_fetch_url, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV }, |
| 9608 | { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV }, |
| 9609 | { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV }, |
| 9610 | { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV }, |
| 9611 | { "cookie", smp_fetch_cookie, ARG1(1,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV|SMP_USE_HRSHV }, |
| 9612 | { "set-cookie", smp_fetch_cookie, ARG1(1,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV }, /* deprecated */ |
Willy Tarreau | 9fcb984 | 2012-04-20 14:45:49 +0200 | [diff] [blame] | 9613 | { NULL, NULL, 0, 0, 0 }, |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 9614 | }}; |
| 9615 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9616 | |
| 9617 | __attribute__((constructor)) |
| 9618 | static void __http_protocol_init(void) |
| 9619 | { |
| 9620 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 9621 | sample_register_fetches(&sample_fetch_keywords); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9622 | } |
| 9623 | |
| 9624 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 9625 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 9626 | * Local variables: |
| 9627 | * c-indent-level: 8 |
| 9628 | * c-basic-offset: 8 |
| 9629 | * End: |
| 9630 | */ |