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 | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 30 | #include <common/compat.h> |
| 31 | #include <common/config.h> |
Willy Tarreau | a4cd1f5 | 2006-12-16 19:57:26 +0100 | [diff] [blame] | 32 | #include <common/debug.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 33 | #include <common/memory.h> |
| 34 | #include <common/mini-clist.h> |
| 35 | #include <common/standard.h> |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 36 | #include <common/ticks.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 37 | #include <common/time.h> |
| 38 | #include <common/uri_auth.h> |
| 39 | #include <common/version.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 40 | |
| 41 | #include <types/capture.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 42 | #include <types/global.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 43 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 44 | #include <proto/acl.h> |
Willy Tarreau | 61612d4 | 2012-04-19 18:42:05 +0200 | [diff] [blame] | 45 | #include <proto/arg.h> |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 46 | #include <proto/auth.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 47 | #include <proto/backend.h> |
| 48 | #include <proto/buffers.h> |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 49 | #include <proto/checks.h> |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 50 | #include <proto/dumpstats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 51 | #include <proto/fd.h> |
Willy Tarreau | 03fa5df | 2010-05-24 21:02:37 +0200 | [diff] [blame] | 52 | #include <proto/frontend.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 53 | #include <proto/log.h> |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 54 | #include <proto/hdr_idx.h> |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 55 | #include <proto/proto_tcp.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 56 | #include <proto/proto_http.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 57 | #include <proto/proxy.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 58 | #include <proto/queue.h> |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 59 | #include <proto/sample.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 60 | #include <proto/server.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 61 | #include <proto/session.h> |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 62 | #include <proto/stream_interface.h> |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 63 | #include <proto/stream_sock.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 64 | #include <proto/task.h> |
| 65 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 66 | const char HTTP_100[] = |
| 67 | "HTTP/1.1 100 Continue\r\n\r\n"; |
| 68 | |
| 69 | const struct chunk http_100_chunk = { |
| 70 | .str = (char *)&HTTP_100, |
| 71 | .len = sizeof(HTTP_100)-1 |
| 72 | }; |
| 73 | |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 74 | /* Warning: no "connection" header is provided with the 3xx messages below */ |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 75 | const char *HTTP_301 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 76 | "HTTP/1.1 301 Moved Permanently\r\n" |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 77 | "Cache-Control: no-cache\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 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 94 | /* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */ |
| 95 | const char *HTTP_401_fmt = |
| 96 | "HTTP/1.0 401 Unauthorized\r\n" |
| 97 | "Cache-Control: no-cache\r\n" |
| 98 | "Connection: close\r\n" |
Willy Tarreau | 791d66d | 2006-07-08 16:53:38 +0200 | [diff] [blame] | 99 | "Content-Type: text/html\r\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 100 | "WWW-Authenticate: Basic realm=\"%s\"\r\n" |
| 101 | "\r\n" |
| 102 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 103 | |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 104 | const char *HTTP_407_fmt = |
| 105 | "HTTP/1.0 407 Unauthorized\r\n" |
| 106 | "Cache-Control: no-cache\r\n" |
| 107 | "Connection: close\r\n" |
| 108 | "Content-Type: text/html\r\n" |
| 109 | "Proxy-Authenticate: Basic realm=\"%s\"\r\n" |
| 110 | "\r\n" |
| 111 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 112 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 113 | |
| 114 | const int http_err_codes[HTTP_ERR_SIZE] = { |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 115 | [HTTP_ERR_200] = 200, /* used by "monitor-uri" */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 116 | [HTTP_ERR_400] = 400, |
| 117 | [HTTP_ERR_403] = 403, |
| 118 | [HTTP_ERR_408] = 408, |
| 119 | [HTTP_ERR_500] = 500, |
| 120 | [HTTP_ERR_502] = 502, |
| 121 | [HTTP_ERR_503] = 503, |
| 122 | [HTTP_ERR_504] = 504, |
| 123 | }; |
| 124 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 125 | static const char *http_err_msgs[HTTP_ERR_SIZE] = { |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 126 | [HTTP_ERR_200] = |
| 127 | "HTTP/1.0 200 OK\r\n" |
| 128 | "Cache-Control: no-cache\r\n" |
| 129 | "Connection: close\r\n" |
| 130 | "Content-Type: text/html\r\n" |
| 131 | "\r\n" |
| 132 | "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n", |
| 133 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 134 | [HTTP_ERR_400] = |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 135 | "HTTP/1.0 400 Bad request\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 136 | "Cache-Control: no-cache\r\n" |
| 137 | "Connection: close\r\n" |
| 138 | "Content-Type: text/html\r\n" |
| 139 | "\r\n" |
| 140 | "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n", |
| 141 | |
| 142 | [HTTP_ERR_403] = |
| 143 | "HTTP/1.0 403 Forbidden\r\n" |
| 144 | "Cache-Control: no-cache\r\n" |
| 145 | "Connection: close\r\n" |
| 146 | "Content-Type: text/html\r\n" |
| 147 | "\r\n" |
| 148 | "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n", |
| 149 | |
| 150 | [HTTP_ERR_408] = |
| 151 | "HTTP/1.0 408 Request Time-out\r\n" |
| 152 | "Cache-Control: no-cache\r\n" |
| 153 | "Connection: close\r\n" |
| 154 | "Content-Type: text/html\r\n" |
| 155 | "\r\n" |
| 156 | "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n", |
| 157 | |
| 158 | [HTTP_ERR_500] = |
| 159 | "HTTP/1.0 500 Server Error\r\n" |
| 160 | "Cache-Control: no-cache\r\n" |
| 161 | "Connection: close\r\n" |
| 162 | "Content-Type: text/html\r\n" |
| 163 | "\r\n" |
| 164 | "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n", |
| 165 | |
| 166 | [HTTP_ERR_502] = |
| 167 | "HTTP/1.0 502 Bad Gateway\r\n" |
| 168 | "Cache-Control: no-cache\r\n" |
| 169 | "Connection: close\r\n" |
| 170 | "Content-Type: text/html\r\n" |
| 171 | "\r\n" |
| 172 | "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n", |
| 173 | |
| 174 | [HTTP_ERR_503] = |
| 175 | "HTTP/1.0 503 Service Unavailable\r\n" |
| 176 | "Cache-Control: no-cache\r\n" |
| 177 | "Connection: close\r\n" |
| 178 | "Content-Type: text/html\r\n" |
| 179 | "\r\n" |
| 180 | "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n", |
| 181 | |
| 182 | [HTTP_ERR_504] = |
| 183 | "HTTP/1.0 504 Gateway Time-out\r\n" |
| 184 | "Cache-Control: no-cache\r\n" |
| 185 | "Connection: close\r\n" |
| 186 | "Content-Type: text/html\r\n" |
| 187 | "\r\n" |
| 188 | "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n", |
| 189 | |
| 190 | }; |
| 191 | |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 192 | /* status codes available for the stats admin page (strictly 4 chars length) */ |
| 193 | const char *stat_status_codes[STAT_STATUS_SIZE] = { |
| 194 | [STAT_STATUS_DENY] = "DENY", |
| 195 | [STAT_STATUS_DONE] = "DONE", |
| 196 | [STAT_STATUS_ERRP] = "ERRP", |
| 197 | [STAT_STATUS_EXCD] = "EXCD", |
| 198 | [STAT_STATUS_NONE] = "NONE", |
| 199 | [STAT_STATUS_PART] = "PART", |
| 200 | [STAT_STATUS_UNKN] = "UNKN", |
| 201 | }; |
| 202 | |
| 203 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 204 | /* We must put the messages here since GCC cannot initialize consts depending |
| 205 | * on strlen(). |
| 206 | */ |
| 207 | struct chunk http_err_chunks[HTTP_ERR_SIZE]; |
| 208 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 209 | #define FD_SETS_ARE_BITFIELDS |
| 210 | #ifdef FD_SETS_ARE_BITFIELDS |
| 211 | /* |
| 212 | * This map is used with all the FD_* macros to check whether a particular bit |
| 213 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 214 | * which should be encoded. When FD_ISSET() returns non-zero, it means that the |
| 215 | * byte should be encoded. Be careful to always pass bytes from 0 to 255 |
| 216 | * exclusively to the macros. |
| 217 | */ |
| 218 | fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 219 | fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 220 | |
| 221 | #else |
| 222 | #error "Check if your OS uses bitfields for fd_sets" |
| 223 | #endif |
| 224 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 225 | void init_proto_http() |
| 226 | { |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 227 | int i; |
| 228 | char *tmp; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 229 | int msg; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 230 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 231 | for (msg = 0; msg < HTTP_ERR_SIZE; msg++) { |
| 232 | if (!http_err_msgs[msg]) { |
| 233 | Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg); |
| 234 | abort(); |
| 235 | } |
| 236 | |
| 237 | http_err_chunks[msg].str = (char *)http_err_msgs[msg]; |
| 238 | http_err_chunks[msg].len = strlen(http_err_msgs[msg]); |
| 239 | } |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 240 | |
| 241 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 242 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 243 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 244 | * printable characters above. |
| 245 | */ |
| 246 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 247 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
| 248 | for (i = 0; i < 32; i++) { |
| 249 | FD_SET(i, hdr_encode_map); |
| 250 | FD_SET(i, url_encode_map); |
| 251 | } |
| 252 | for (i = 127; i < 256; i++) { |
| 253 | FD_SET(i, hdr_encode_map); |
| 254 | FD_SET(i, url_encode_map); |
| 255 | } |
| 256 | |
| 257 | tmp = "\"#{|}"; |
| 258 | while (*tmp) { |
| 259 | FD_SET(*tmp, hdr_encode_map); |
| 260 | tmp++; |
| 261 | } |
| 262 | |
| 263 | tmp = "\"#"; |
| 264 | while (*tmp) { |
| 265 | FD_SET(*tmp, url_encode_map); |
| 266 | tmp++; |
| 267 | } |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 268 | |
| 269 | /* memory allocations */ |
| 270 | pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED); |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 271 | pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 272 | pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 273 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 274 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 275 | /* |
| 276 | * We have 26 list of methods (1 per first letter), each of which can have |
| 277 | * up to 3 entries (2 valid, 1 null). |
| 278 | */ |
| 279 | struct http_method_desc { |
| 280 | http_meth_t meth; |
| 281 | int len; |
| 282 | const char text[8]; |
| 283 | }; |
| 284 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 285 | const struct http_method_desc http_methods[26][3] = { |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 286 | ['C' - 'A'] = { |
| 287 | [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" }, |
| 288 | }, |
| 289 | ['D' - 'A'] = { |
| 290 | [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" }, |
| 291 | }, |
| 292 | ['G' - 'A'] = { |
| 293 | [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" }, |
| 294 | }, |
| 295 | ['H' - 'A'] = { |
| 296 | [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" }, |
| 297 | }, |
| 298 | ['P' - 'A'] = { |
| 299 | [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" }, |
| 300 | [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" }, |
| 301 | }, |
| 302 | ['T' - 'A'] = { |
| 303 | [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" }, |
| 304 | }, |
| 305 | /* rest is empty like this : |
| 306 | * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" }, |
| 307 | */ |
| 308 | }; |
| 309 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 310 | /* 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] | 311 | * 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] | 312 | * RFC2616 for those chars. |
| 313 | */ |
| 314 | |
| 315 | const char http_is_spht[256] = { |
| 316 | [' '] = 1, ['\t'] = 1, |
| 317 | }; |
| 318 | |
| 319 | const char http_is_crlf[256] = { |
| 320 | ['\r'] = 1, ['\n'] = 1, |
| 321 | }; |
| 322 | |
| 323 | const char http_is_lws[256] = { |
| 324 | [' '] = 1, ['\t'] = 1, |
| 325 | ['\r'] = 1, ['\n'] = 1, |
| 326 | }; |
| 327 | |
| 328 | const char http_is_sep[256] = { |
| 329 | ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1, |
| 330 | ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1, |
| 331 | ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1, |
| 332 | ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1, |
| 333 | [' '] = 1, ['\t'] = 1, ['\\'] = 1, |
| 334 | }; |
| 335 | |
| 336 | const char http_is_ctl[256] = { |
| 337 | [0 ... 31] = 1, |
| 338 | [127] = 1, |
| 339 | }; |
| 340 | |
| 341 | /* |
| 342 | * A token is any ASCII char that is neither a separator nor a CTL char. |
| 343 | * Do not overwrite values in assignment since gcc-2.95 will not handle |
| 344 | * them correctly. Instead, define every non-CTL char's status. |
| 345 | */ |
| 346 | const char http_is_token[256] = { |
| 347 | [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1, |
| 348 | ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1, |
| 349 | ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1, |
| 350 | [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0, |
| 351 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, |
| 352 | ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1, |
| 353 | ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0, |
| 354 | ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0, |
| 355 | ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1, |
| 356 | ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1, |
| 357 | ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1, |
| 358 | ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1, |
| 359 | ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1, |
| 360 | ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1, |
| 361 | ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0, |
| 362 | ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1, |
| 363 | ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1, |
| 364 | ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1, |
| 365 | ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1, |
| 366 | ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1, |
| 367 | ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1, |
| 368 | ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1, |
| 369 | ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0, |
| 370 | ['|'] = 1, ['}'] = 0, ['~'] = 1, |
| 371 | }; |
| 372 | |
| 373 | |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 374 | /* |
| 375 | * An http ver_token is any ASCII which can be found in an HTTP version, |
| 376 | * which includes 'H', 'T', 'P', '/', '.' and any digit. |
| 377 | */ |
| 378 | const char http_is_ver_token[256] = { |
| 379 | ['.'] = 1, ['/'] = 1, |
| 380 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1, |
| 381 | ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1, |
| 382 | ['H'] = 1, ['P'] = 1, ['T'] = 1, |
| 383 | }; |
| 384 | |
| 385 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 386 | /* |
Willy Tarreau | e988a79 | 2010-01-04 21:13:14 +0100 | [diff] [blame] | 387 | * Silent debug that outputs only in strace, using fd #-1. Trash is modified. |
| 388 | */ |
| 389 | #if defined(DEBUG_FSM) |
| 390 | static void http_silent_debug(int line, struct session *s) |
| 391 | { |
| 392 | int size = 0; |
| 393 | size += snprintf(trash + size, sizeof(trash) - size, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 394 | "[%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", |
Willy Tarreau | e988a79 | 2010-01-04 21:13:14 +0100 | [diff] [blame] | 395 | line, |
| 396 | s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 397 | s->req->data, s->req->size, s->req->l, s->req->w, s->req->r, s->req->p, s->req->o, s->req->to_forward, s->txn.flags); |
Willy Tarreau | e988a79 | 2010-01-04 21:13:14 +0100 | [diff] [blame] | 398 | write(-1, trash, size); |
| 399 | size = 0; |
| 400 | size += snprintf(trash + size, sizeof(trash) - size, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 401 | " %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", |
Willy Tarreau | e988a79 | 2010-01-04 21:13:14 +0100 | [diff] [blame] | 402 | line, |
| 403 | s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 404 | s->rep->data, s->rep->size, s->rep->l, s->rep->w, s->rep->r, s->rep->p, s->rep->o, s->rep->to_forward); |
Willy Tarreau | e988a79 | 2010-01-04 21:13:14 +0100 | [diff] [blame] | 405 | |
| 406 | write(-1, trash, size); |
| 407 | } |
| 408 | #else |
| 409 | #define http_silent_debug(l,s) do { } while (0) |
| 410 | #endif |
| 411 | |
| 412 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 413 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 414 | * 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] | 415 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 416 | * of headers is automatically adjusted. The number of bytes added is returned |
| 417 | * on success, otherwise <0 is returned indicating an error. |
| 418 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 419 | 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] | 420 | { |
| 421 | int bytes, len; |
| 422 | |
| 423 | len = strlen(text); |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 424 | bytes = buffer_insert_line2(msg->buf, msg->buf->p + msg->eoh, text, len); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 425 | if (!bytes) |
| 426 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 427 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 428 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 429 | } |
| 430 | |
| 431 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 432 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 433 | * 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] | 434 | * the buffer is only opened and the space reserved, but nothing is copied. |
| 435 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 436 | * of headers is automatically adjusted. The number of bytes added is returned |
| 437 | * on success, otherwise <0 is returned indicating an error. |
| 438 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 439 | int http_header_add_tail2(struct http_msg *msg, |
| 440 | struct hdr_idx *hdr_idx, const char *text, int len) |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 441 | { |
| 442 | int bytes; |
| 443 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 444 | bytes = buffer_insert_line2(msg->buf, msg->buf->p + msg->eoh, text, len); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 445 | if (!bytes) |
| 446 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 447 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 448 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 449 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 450 | |
| 451 | /* |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 452 | * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| 453 | * If so, returns the position of the first non-space character relative to |
| 454 | * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| 455 | * to return a pointer to the place after the first space. Returns 0 if the |
| 456 | * header name does not match. Checks are case-insensitive. |
| 457 | */ |
| 458 | int http_header_match2(const char *hdr, const char *end, |
| 459 | const char *name, int len) |
| 460 | { |
| 461 | const char *val; |
| 462 | |
| 463 | if (hdr + len >= end) |
| 464 | return 0; |
| 465 | if (hdr[len] != ':') |
| 466 | return 0; |
| 467 | if (strncasecmp(hdr, name, len) != 0) |
| 468 | return 0; |
| 469 | val = hdr + len + 1; |
| 470 | while (val < end && HTTP_IS_SPHT(*val)) |
| 471 | val++; |
| 472 | if ((val >= end) && (len + 2 <= end - hdr)) |
| 473 | return len + 2; /* we may replace starting from second space */ |
| 474 | return val - hdr; |
| 475 | } |
| 476 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 477 | /* Find the end of the header value contained between <s> and <e>. See RFC2616, |
| 478 | * par 2.2 for more information. Note that it requires a valid header to return |
| 479 | * a valid result. This works for headers defined as comma-separated lists. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 480 | */ |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 481 | char *find_hdr_value_end(char *s, const char *e) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 482 | { |
| 483 | int quoted, qdpair; |
| 484 | |
| 485 | quoted = qdpair = 0; |
| 486 | for (; s < e; s++) { |
| 487 | if (qdpair) qdpair = 0; |
Willy Tarreau | 0f7f51f | 2010-08-30 11:06:34 +0200 | [diff] [blame] | 488 | else if (quoted) { |
| 489 | if (*s == '\\') qdpair = 1; |
| 490 | else if (*s == '"') quoted = 0; |
| 491 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 492 | else if (*s == '"') quoted = 1; |
| 493 | else if (*s == ',') return s; |
| 494 | } |
| 495 | return s; |
| 496 | } |
| 497 | |
| 498 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 499 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 500 | * structure holds everything necessary to use the header and find next |
| 501 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 502 | * beginning. Otherwise, the next occurrence is returned. The function returns |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 503 | * 1 when it finds a value, and 0 when there is no more. It is designed to work |
| 504 | * with headers defined as comma-separated lists. As a special case, if ctx->val |
| 505 | * is NULL when searching for a new values of a header, the current header is |
| 506 | * rescanned. This allows rescanning after a header deletion. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 507 | */ |
| 508 | int http_find_header2(const char *name, int len, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 509 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 510 | struct hdr_ctx *ctx) |
| 511 | { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 512 | char *eol, *sov; |
| 513 | int cur_idx, old_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 514 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 515 | cur_idx = ctx->idx; |
| 516 | if (cur_idx) { |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 517 | /* We have previously returned a value, let's search |
| 518 | * another one on the same line. |
| 519 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 520 | sol = ctx->line; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 521 | ctx->del = ctx->val + ctx->vlen + ctx->tws; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 522 | sov = sol + ctx->del; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 523 | eol = sol + idx->v[cur_idx].len; |
| 524 | |
| 525 | if (sov >= eol) |
| 526 | /* no more values in this header */ |
| 527 | goto next_hdr; |
| 528 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 529 | /* values remaining for this header, skip the comma but save it |
| 530 | * for later use (eg: for header deletion). |
| 531 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 532 | sov++; |
| 533 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 534 | sov++; |
| 535 | |
| 536 | goto return_hdr; |
| 537 | } |
| 538 | |
| 539 | /* first request for this header */ |
| 540 | sol += hdr_idx_first_pos(idx); |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 541 | old_idx = 0; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 542 | cur_idx = hdr_idx_first_idx(idx); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 543 | while (cur_idx) { |
| 544 | eol = sol + idx->v[cur_idx].len; |
| 545 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 546 | if (len == 0) { |
| 547 | /* No argument was passed, we want any header. |
| 548 | * To achieve this, we simply build a fake request. */ |
| 549 | while (sol + len < eol && sol[len] != ':') |
| 550 | len++; |
| 551 | name = sol; |
| 552 | } |
| 553 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 554 | if ((len < eol - sol) && |
| 555 | (sol[len] == ':') && |
| 556 | (strncasecmp(sol, name, len) == 0)) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 557 | ctx->del = len; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 558 | sov = sol + len + 1; |
| 559 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 560 | sov++; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 561 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 562 | ctx->line = sol; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 563 | ctx->prev = old_idx; |
| 564 | return_hdr: |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 565 | ctx->idx = cur_idx; |
| 566 | ctx->val = sov - sol; |
| 567 | |
| 568 | eol = find_hdr_value_end(sov, eol); |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 569 | ctx->tws = 0; |
Willy Tarreau | 275600b | 2011-09-16 08:11:26 +0200 | [diff] [blame] | 570 | while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) { |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 571 | eol--; |
| 572 | ctx->tws++; |
| 573 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 574 | ctx->vlen = eol - sov; |
| 575 | return 1; |
| 576 | } |
| 577 | next_hdr: |
| 578 | sol = eol + idx->v[cur_idx].cr + 1; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 579 | old_idx = cur_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 580 | cur_idx = idx->v[cur_idx].next; |
| 581 | } |
| 582 | return 0; |
| 583 | } |
| 584 | |
| 585 | int http_find_header(const char *name, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 586 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 587 | struct hdr_ctx *ctx) |
| 588 | { |
| 589 | return http_find_header2(name, strlen(name), sol, idx, ctx); |
| 590 | } |
| 591 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 592 | /* Remove one value of a header. This only works on a <ctx> returned by one of |
| 593 | * the http_find_header functions. The value is removed, as well as surrounding |
| 594 | * 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] | 595 | * 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] | 596 | * message <msg>. The new index is returned. If it is zero, it means there is |
| 597 | * no more header, so any processing may stop. The ctx is always left in a form |
| 598 | * that can be handled by http_find_header2() to find next occurrence. |
| 599 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 600 | 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] | 601 | { |
| 602 | int cur_idx = ctx->idx; |
| 603 | char *sol = ctx->line; |
| 604 | struct hdr_idx_elem *hdr; |
| 605 | int delta, skip_comma; |
| 606 | |
| 607 | if (!cur_idx) |
| 608 | return 0; |
| 609 | |
| 610 | hdr = &idx->v[cur_idx]; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 611 | if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 612 | /* This was the only value of the header, we must now remove it entirely. */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 613 | delta = buffer_replace2(msg->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0); |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 614 | http_msg_move_end(msg, delta); |
| 615 | idx->used--; |
| 616 | hdr->len = 0; /* unused entry */ |
| 617 | idx->v[ctx->prev].next = idx->v[ctx->idx].next; |
Willy Tarreau | 5c4784f | 2011-02-12 13:07:35 +0100 | [diff] [blame] | 618 | if (idx->tail == ctx->idx) |
| 619 | idx->tail = ctx->prev; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 620 | ctx->idx = ctx->prev; /* walk back to the end of previous header */ |
| 621 | ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1; |
| 622 | 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] | 623 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 624 | return ctx->idx; |
| 625 | } |
| 626 | |
| 627 | /* 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] | 628 | * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the |
| 629 | * last entry of the list, we remove the last separator. |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 630 | */ |
| 631 | |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 632 | skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 633 | delta = buffer_replace2(msg->buf, sol + ctx->del + skip_comma, |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 634 | sol + ctx->val + ctx->vlen + ctx->tws + skip_comma, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 635 | NULL, 0); |
| 636 | hdr->len += delta; |
| 637 | http_msg_move_end(msg, delta); |
| 638 | ctx->val = ctx->del; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 639 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 640 | return ctx->idx; |
| 641 | } |
| 642 | |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 643 | /* This function handles a server error at the stream interface level. The |
| 644 | * stream interface is assumed to be already in a closed state. An optional |
| 645 | * message is copied into the input buffer, and an HTTP status code stored. |
| 646 | * 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] | 647 | * in this buffer will be lost. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 648 | */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 649 | static void http_server_error(struct session *t, struct stream_interface *si, |
| 650 | int err, int finst, int status, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 651 | { |
Willy Tarreau | d5fd51c | 2010-01-22 14:17:47 +0100 | [diff] [blame] | 652 | buffer_auto_read(si->ob); |
| 653 | buffer_abort(si->ob); |
| 654 | buffer_auto_close(si->ob); |
| 655 | buffer_erase(si->ob); |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 656 | buffer_auto_close(si->ib); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 657 | buffer_auto_read(si->ib); |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 658 | if (status > 0 && msg) { |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 659 | t->txn.status = status; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 660 | bo_inject(si->ib, msg->str, msg->len); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 661 | } |
| 662 | if (!(t->flags & SN_ERR_MASK)) |
| 663 | t->flags |= err; |
| 664 | if (!(t->flags & SN_FINST_MASK)) |
| 665 | t->flags |= finst; |
| 666 | } |
| 667 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 668 | /* This function returns the appropriate error location for the given session |
| 669 | * and message. |
| 670 | */ |
| 671 | |
| 672 | struct chunk *error_message(struct session *s, int msgnum) |
| 673 | { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 674 | if (s->be->errmsg[msgnum].str) |
| 675 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 676 | else if (s->fe->errmsg[msgnum].str) |
| 677 | return &s->fe->errmsg[msgnum]; |
| 678 | else |
| 679 | return &http_err_chunks[msgnum]; |
| 680 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 681 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 682 | /* |
| 683 | * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text |
| 684 | * string), HTTP_METH_OTHER for unknown methods, or the identified method. |
| 685 | */ |
| 686 | static http_meth_t find_http_meth(const char *str, const int len) |
| 687 | { |
| 688 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 689 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 690 | |
| 691 | m = ((unsigned)*str - 'A'); |
| 692 | |
| 693 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 694 | for (h = http_methods[m]; h->len > 0; h++) { |
| 695 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 696 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 697 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 698 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 699 | }; |
| 700 | return HTTP_METH_OTHER; |
| 701 | } |
| 702 | return HTTP_METH_NONE; |
| 703 | |
| 704 | } |
| 705 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 706 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 707 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 708 | * It is returned otherwise. |
| 709 | */ |
| 710 | static char * |
| 711 | http_get_path(struct http_txn *txn) |
| 712 | { |
| 713 | char *ptr, *end; |
| 714 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 715 | ptr = txn->req.buf->p + txn->req.sol + txn->req.sl.rq.u; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 716 | end = ptr + txn->req.sl.rq.u_l; |
| 717 | |
| 718 | if (ptr >= end) |
| 719 | return NULL; |
| 720 | |
| 721 | /* RFC2616, par. 5.1.2 : |
| 722 | * Request-URI = "*" | absuri | abspath | authority |
| 723 | */ |
| 724 | |
| 725 | if (*ptr == '*') |
| 726 | return NULL; |
| 727 | |
| 728 | if (isalpha((unsigned char)*ptr)) { |
| 729 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 730 | ptr++; |
| 731 | while (ptr < end && |
| 732 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 733 | ptr++; |
| 734 | /* skip '://' */ |
| 735 | if (ptr == end || *ptr++ != ':') |
| 736 | return NULL; |
| 737 | if (ptr == end || *ptr++ != '/') |
| 738 | return NULL; |
| 739 | if (ptr == end || *ptr++ != '/') |
| 740 | return NULL; |
| 741 | } |
| 742 | /* skip [user[:passwd]@]host[:[port]] */ |
| 743 | |
| 744 | while (ptr < end && *ptr != '/') |
| 745 | ptr++; |
| 746 | |
| 747 | if (ptr == end) |
| 748 | return NULL; |
| 749 | |
| 750 | /* OK, we got the '/' ! */ |
| 751 | return ptr; |
| 752 | } |
| 753 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 754 | /* Returns a 302 for a redirectable request. This may only be called just after |
| 755 | * the stream interface has moved to SI_ST_ASS. Unprocessable requests are |
| 756 | * left unchanged and will follow normal proxy processing. |
| 757 | */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 758 | void perform_http_redirect(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 759 | { |
| 760 | struct http_txn *txn; |
| 761 | struct chunk rdr; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 762 | struct server *srv; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 763 | char *path; |
| 764 | int len; |
| 765 | |
| 766 | /* 1: create the response header */ |
| 767 | rdr.len = strlen(HTTP_302); |
| 768 | rdr.str = trash; |
Willy Tarreau | 59e0b0f | 2010-01-09 21:29:23 +0100 | [diff] [blame] | 769 | rdr.size = sizeof(trash); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 770 | memcpy(rdr.str, HTTP_302, rdr.len); |
| 771 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 772 | srv = target_srv(&s->target); |
| 773 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 774 | /* 2: add the server's prefix */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 775 | if (rdr.len + srv->rdr_len > rdr.size) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 776 | return; |
| 777 | |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 778 | /* special prefix "/" means don't change URL */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 779 | if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') { |
| 780 | memcpy(rdr.str + rdr.len, srv->rdr_pfx, srv->rdr_len); |
| 781 | rdr.len += srv->rdr_len; |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 782 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 783 | |
| 784 | /* 3: add the request URI */ |
| 785 | txn = &s->txn; |
| 786 | path = http_get_path(txn); |
| 787 | if (!path) |
| 788 | return; |
| 789 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 790 | len = txn->req.sl.rq.u_l + (s->req->p + txn->req.sol + txn->req.sl.rq.u) - path; |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 791 | if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 792 | return; |
| 793 | |
| 794 | memcpy(rdr.str + rdr.len, path, len); |
| 795 | rdr.len += len; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 796 | |
| 797 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 798 | memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 799 | rdr.len += 29; |
| 800 | } else { |
| 801 | memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23); |
| 802 | rdr.len += 23; |
| 803 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 804 | |
| 805 | /* prepare to return without error. */ |
Willy Tarreau | 060781f | 2012-05-07 16:50:03 +0200 | [diff] [blame^] | 806 | si->sock.shutr(si); |
| 807 | si->sock.shutw(si); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 808 | si->err_type = SI_ET_NONE; |
| 809 | si->err_loc = NULL; |
| 810 | si->state = SI_ST_CLO; |
| 811 | |
| 812 | /* send the message */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 813 | http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 814 | |
| 815 | /* FIXME: we should increase a counter of redirects per server and per backend. */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 816 | if (srv) |
| 817 | srv_inc_sess_ctr(srv); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 818 | } |
| 819 | |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 820 | /* Return the error message corresponding to si->err_type. It is assumed |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 821 | * that the server side is closed. Note that err_type is actually a |
| 822 | * bitmask, where almost only aborts may be cumulated with other |
| 823 | * values. We consider that aborted operations are more important |
| 824 | * than timeouts or errors due to the fact that nobody else in the |
| 825 | * logs might explain incomplete retries. All others should avoid |
| 826 | * being cumulated. It should normally not be possible to have multiple |
| 827 | * aborts at once, but just in case, the first one in sequence is reported. |
| 828 | */ |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 829 | void http_return_srv_error(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 830 | { |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 831 | int err_type = si->err_type; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 832 | |
| 833 | if (err_type & SI_ET_QUEUE_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 834 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q, |
| 835 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 836 | else if (err_type & SI_ET_CONN_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 837 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C, |
| 838 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 839 | else if (err_type & SI_ET_QUEUE_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 840 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q, |
| 841 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 842 | else if (err_type & SI_ET_QUEUE_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 843 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q, |
| 844 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 845 | else if (err_type & SI_ET_CONN_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 846 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C, |
| 847 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 848 | else if (err_type & SI_ET_CONN_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 849 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C, |
| 850 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 851 | else /* SI_ET_CONN_OTHER and others */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 852 | http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C, |
| 853 | 500, error_message(s, HTTP_ERR_500)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 854 | } |
| 855 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 856 | extern const char sess_term_cond[8]; |
| 857 | extern const char sess_fin_state[8]; |
| 858 | extern const char *monthname[12]; |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 859 | struct pool_head *pool2_requri; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 860 | struct pool_head *pool2_capture; |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 861 | struct pool_head *pool2_uniqueid; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 862 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 863 | /* |
| 864 | * Capture headers from message starting at <som> according to header list |
| 865 | * <cap_hdr>, and fill the <idx> structure appropriately. |
| 866 | */ |
| 867 | void capture_headers(char *som, struct hdr_idx *idx, |
| 868 | char **cap, struct cap_hdr *cap_hdr) |
| 869 | { |
| 870 | char *eol, *sol, *col, *sov; |
| 871 | int cur_idx; |
| 872 | struct cap_hdr *h; |
| 873 | int len; |
| 874 | |
| 875 | sol = som + hdr_idx_first_pos(idx); |
| 876 | cur_idx = hdr_idx_first_idx(idx); |
| 877 | |
| 878 | while (cur_idx) { |
| 879 | eol = sol + idx->v[cur_idx].len; |
| 880 | |
| 881 | col = sol; |
| 882 | while (col < eol && *col != ':') |
| 883 | col++; |
| 884 | |
| 885 | sov = col + 1; |
| 886 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 887 | sov++; |
| 888 | |
| 889 | for (h = cap_hdr; h; h = h->next) { |
| 890 | if ((h->namelen == col - sol) && |
| 891 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 892 | if (cap[h->index] == NULL) |
| 893 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 894 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 895 | |
| 896 | if (cap[h->index] == NULL) { |
| 897 | Alert("HTTP capture : out of memory.\n"); |
| 898 | continue; |
| 899 | } |
| 900 | |
| 901 | len = eol - sov; |
| 902 | if (len > h->len) |
| 903 | len = h->len; |
| 904 | |
| 905 | memcpy(cap[h->index], sov, len); |
| 906 | cap[h->index][len]=0; |
| 907 | } |
| 908 | } |
| 909 | sol = eol + idx->v[cur_idx].cr + 1; |
| 910 | cur_idx = idx->v[cur_idx].next; |
| 911 | } |
| 912 | } |
| 913 | |
| 914 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 915 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 916 | */ |
| 917 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 918 | |
| 919 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 920 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 921 | */ |
| 922 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 923 | ptr++; \ |
| 924 | if (likely(ptr < end)) \ |
| 925 | goto good; \ |
| 926 | else { \ |
| 927 | state = (st); \ |
| 928 | goto http_msg_ood; \ |
| 929 | } \ |
| 930 | } while (0) |
| 931 | |
| 932 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 933 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 934 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 935 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 936 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 937 | * will give undefined results. |
| 938 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 939 | * and that msg->sol points to the beginning of the response. |
| 940 | * If a complete line is found (which implies that at least one CR or LF is |
| 941 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 942 | * returned indicating an incomplete line (which does not mean that parts have |
| 943 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 944 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 945 | * upon next call. |
| 946 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 947 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 948 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 949 | * 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] | 950 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 951 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 952 | const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf, |
| 953 | unsigned int state, const char *ptr, const char *end, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 954 | unsigned int *ret_ptr, unsigned int *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 955 | { |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 956 | const char *msg_start = msg->buf->p; |
| 957 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 958 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 959 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 960 | http_msg_rpver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 961 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 962 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 963 | |
| 964 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 965 | msg->sl.st.v_l = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 966 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 967 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 968 | state = HTTP_MSG_ERROR; |
| 969 | break; |
| 970 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 971 | case HTTP_MSG_RPVER_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 972 | http_msg_rpver_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 973 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 974 | msg->sl.st.c = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 975 | goto http_msg_rpcode; |
| 976 | } |
| 977 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 978 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 979 | /* so it's a CR/LF, this is invalid */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 980 | state = HTTP_MSG_ERROR; |
| 981 | break; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 982 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 983 | case HTTP_MSG_RPCODE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 984 | http_msg_rpcode: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 985 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 986 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 987 | |
| 988 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 989 | msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 990 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 991 | } |
| 992 | |
| 993 | /* so it's a CR/LF, so there is no reason phrase */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 994 | msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 995 | http_msg_rsp_reason: |
| 996 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 997 | msg->sl.st.r = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 998 | msg->sl.st.r_l = 0; |
| 999 | goto http_msg_rpline_eol; |
| 1000 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1001 | case HTTP_MSG_RPCODE_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1002 | http_msg_rpcode_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1003 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1004 | msg->sl.st.r = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1005 | goto http_msg_rpreason; |
| 1006 | } |
| 1007 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1008 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1009 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1010 | goto http_msg_rsp_reason; |
| 1011 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1012 | case HTTP_MSG_RPREASON: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1013 | http_msg_rpreason: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1014 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1015 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1016 | msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1017 | http_msg_rpline_eol: |
| 1018 | /* We have seen the end of line. Note that we do not |
| 1019 | * necessarily have the \n yet, but at least we know that we |
| 1020 | * have EITHER \r OR \n, otherwise the response would not be |
| 1021 | * complete. We can then record the response length and return |
| 1022 | * to the caller which will be able to register it. |
| 1023 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1024 | msg->sl.st.l = ptr - msg_start - msg->sol; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1025 | return ptr; |
| 1026 | |
| 1027 | #ifdef DEBUG_FULL |
| 1028 | default: |
| 1029 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1030 | exit(1); |
| 1031 | #endif |
| 1032 | } |
| 1033 | |
| 1034 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1035 | /* out of valid data */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1036 | if (ret_state) |
| 1037 | *ret_state = state; |
| 1038 | if (ret_ptr) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1039 | *ret_ptr = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1040 | return NULL; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1041 | } |
| 1042 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1043 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1044 | * This function parses a request line between <ptr> and <end>, starting with |
| 1045 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1046 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1047 | * will give undefined results. |
| 1048 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1049 | * and that msg->sol points to the beginning of the request. |
| 1050 | * If a complete line is found (which implies that at least one CR or LF is |
| 1051 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1052 | * returned indicating an incomplete line (which does not mean that parts have |
| 1053 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1054 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1055 | * upon next call. |
| 1056 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1057 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1058 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1059 | * 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] | 1060 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1061 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1062 | const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf, |
| 1063 | unsigned int state, const char *ptr, const char *end, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1064 | unsigned int *ret_ptr, unsigned int *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1065 | { |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 1066 | const char *msg_start = msg->buf->p; |
| 1067 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1068 | switch (state) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1069 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1070 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1071 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1072 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1073 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1074 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1075 | msg->sl.rq.m_l = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1076 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1077 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1078 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1079 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1080 | /* HTTP 0.9 request */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1081 | msg->sl.rq.m_l = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1082 | http_msg_req09_uri: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1083 | msg->sl.rq.u = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1084 | http_msg_req09_uri_e: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1085 | msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1086 | http_msg_req09_ver: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1087 | msg->sl.rq.v = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1088 | msg->sl.rq.v_l = 0; |
| 1089 | goto http_msg_rqline_eol; |
| 1090 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1091 | state = HTTP_MSG_ERROR; |
| 1092 | break; |
| 1093 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1094 | case HTTP_MSG_RQMETH_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1095 | http_msg_rqmeth_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1096 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1097 | msg->sl.rq.u = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1098 | goto http_msg_rquri; |
| 1099 | } |
| 1100 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1101 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1102 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1103 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1104 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1105 | case HTTP_MSG_RQURI: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1106 | http_msg_rquri: |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1107 | if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1108 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1109 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1110 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1111 | msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1112 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1113 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1114 | |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1115 | if (likely((unsigned char)*ptr >= 128)) { |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1116 | /* non-ASCII chars are forbidden unless option |
| 1117 | * accept-invalid-http-request is enabled in the frontend. |
| 1118 | * In any case, we capture the faulty char. |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1119 | */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1120 | if (msg->err_pos < -1) |
| 1121 | goto invalid_char; |
| 1122 | if (msg->err_pos == -1) |
| 1123 | msg->err_pos = ptr - msg_buf; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1124 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
| 1125 | } |
| 1126 | |
| 1127 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1128 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1129 | goto http_msg_req09_uri_e; |
| 1130 | } |
| 1131 | |
| 1132 | /* OK forbidden chars, 0..31 or 127 */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1133 | invalid_char: |
| 1134 | msg->err_pos = ptr - msg_buf; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1135 | state = HTTP_MSG_ERROR; |
| 1136 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1137 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1138 | case HTTP_MSG_RQURI_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1139 | http_msg_rquri_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1140 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1141 | msg->sl.rq.v = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1142 | goto http_msg_rqver; |
| 1143 | } |
| 1144 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1145 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1146 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1147 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1148 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1149 | case HTTP_MSG_RQVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1150 | http_msg_rqver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1151 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1152 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1153 | |
| 1154 | if (likely(HTTP_IS_CRLF(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1155 | msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1156 | http_msg_rqline_eol: |
| 1157 | /* We have seen the end of line. Note that we do not |
| 1158 | * necessarily have the \n yet, but at least we know that we |
| 1159 | * have EITHER \r OR \n, otherwise the request would not be |
| 1160 | * complete. We can then record the request length and return |
| 1161 | * to the caller which will be able to register it. |
| 1162 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1163 | msg->sl.rq.l = ptr - msg_start - msg->sol; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1164 | return ptr; |
| 1165 | } |
| 1166 | |
| 1167 | /* neither an HTTP_VER token nor a CRLF */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1168 | state = HTTP_MSG_ERROR; |
| 1169 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1170 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1171 | #ifdef DEBUG_FULL |
| 1172 | default: |
| 1173 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1174 | exit(1); |
| 1175 | #endif |
| 1176 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1177 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1178 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1179 | /* out of valid data */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1180 | if (ret_state) |
| 1181 | *ret_state = state; |
| 1182 | if (ret_ptr) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1183 | *ret_ptr = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1184 | return NULL; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1185 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1186 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1187 | /* |
| 1188 | * Returns the data from Authorization header. Function may be called more |
| 1189 | * than once so data is stored in txn->auth_data. When no header is found |
| 1190 | * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid |
| 1191 | * searching again for something we are unable to find anyway. |
| 1192 | */ |
| 1193 | |
| 1194 | char get_http_auth_buff[BUFSIZE]; |
| 1195 | |
| 1196 | int |
| 1197 | get_http_auth(struct session *s) |
| 1198 | { |
| 1199 | |
| 1200 | struct http_txn *txn = &s->txn; |
| 1201 | struct chunk auth_method; |
| 1202 | struct hdr_ctx ctx; |
| 1203 | char *h, *p; |
| 1204 | int len; |
| 1205 | |
| 1206 | #ifdef DEBUG_AUTH |
| 1207 | printf("Auth for session %p: %d\n", s, txn->auth.method); |
| 1208 | #endif |
| 1209 | |
| 1210 | if (txn->auth.method == HTTP_AUTH_WRONG) |
| 1211 | return 0; |
| 1212 | |
| 1213 | if (txn->auth.method) |
| 1214 | return 1; |
| 1215 | |
| 1216 | txn->auth.method = HTTP_AUTH_WRONG; |
| 1217 | |
| 1218 | ctx.idx = 0; |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 1219 | |
| 1220 | if (txn->flags & TX_USE_PX_CONN) { |
| 1221 | h = "Proxy-Authorization"; |
| 1222 | len = strlen(h); |
| 1223 | } else { |
| 1224 | h = "Authorization"; |
| 1225 | len = strlen(h); |
| 1226 | } |
| 1227 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1228 | if (!http_find_header2(h, len, s->req->p + txn->req.sol, &txn->hdr_idx, &ctx)) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1229 | return 0; |
| 1230 | |
| 1231 | h = ctx.line + ctx.val; |
| 1232 | |
| 1233 | p = memchr(h, ' ', ctx.vlen); |
| 1234 | if (!p || p == h) |
| 1235 | return 0; |
| 1236 | |
| 1237 | chunk_initlen(&auth_method, h, 0, p-h); |
| 1238 | chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1); |
| 1239 | |
| 1240 | if (!strncasecmp("Basic", auth_method.str, auth_method.len)) { |
| 1241 | |
| 1242 | len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len, |
| 1243 | get_http_auth_buff, BUFSIZE - 1); |
| 1244 | |
| 1245 | if (len < 0) |
| 1246 | return 0; |
| 1247 | |
| 1248 | |
| 1249 | get_http_auth_buff[len] = '\0'; |
| 1250 | |
| 1251 | p = strchr(get_http_auth_buff, ':'); |
| 1252 | |
| 1253 | if (!p) |
| 1254 | return 0; |
| 1255 | |
| 1256 | txn->auth.user = get_http_auth_buff; |
| 1257 | *p = '\0'; |
| 1258 | txn->auth.pass = p+1; |
| 1259 | |
| 1260 | txn->auth.method = HTTP_AUTH_BASIC; |
| 1261 | return 1; |
| 1262 | } |
| 1263 | |
| 1264 | return 0; |
| 1265 | } |
| 1266 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1267 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1268 | /* |
| 1269 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | 8b1323e | 2012-03-09 14:46:19 +0100 | [diff] [blame] | 1270 | * depending on the initial msg->msg_state. The caller is responsible for |
| 1271 | * ensuring that the message does not wrap. The function can be preempted |
| 1272 | * everywhere when data are missing and recalled at the exact same location |
| 1273 | * with no information loss. The message may even be realigned between two |
| 1274 | * calls. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1275 | * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1276 | * fields. Note that msg->som and msg->sol will be initialized after completing |
| 1277 | * the first state, so that none of the msg pointers has to be initialized |
| 1278 | * prior to the first call. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1279 | */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 1280 | void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1281 | { |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1282 | unsigned int state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1283 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 1284 | struct buffer *buf = msg->buf; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1285 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1286 | state = msg->msg_state; |
Willy Tarreau | 8b1323e | 2012-03-09 14:46:19 +0100 | [diff] [blame] | 1287 | ptr = buf->p + msg->next; |
| 1288 | end = buf->p + buf->i; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1289 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1290 | if (unlikely(ptr >= end)) |
| 1291 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1292 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1293 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1294 | /* |
| 1295 | * First, states that are specific to the response only. |
| 1296 | * We check them first so that request and headers are |
| 1297 | * closer to each other (accessed more often). |
| 1298 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1299 | case HTTP_MSG_RPBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1300 | http_msg_rpbefore: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1301 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1302 | /* we have a start of message, but we have to check |
| 1303 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1304 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1305 | */ |
Willy Tarreau | 89fa706 | 2012-03-02 16:13:16 +0100 | [diff] [blame] | 1306 | char *beg = buf->p; |
| 1307 | |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1308 | if (unlikely(ptr != beg)) { |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1309 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1310 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1311 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 1312 | bi_fast_delete(buf, ptr - beg); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1313 | } |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1314 | msg->sol = msg->som = ptr - buf->p; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1315 | hdr_idx_init(idx); |
| 1316 | state = HTTP_MSG_RPVER; |
| 1317 | goto http_msg_rpver; |
| 1318 | } |
| 1319 | |
| 1320 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1321 | goto http_msg_invalid; |
| 1322 | |
| 1323 | if (unlikely(*ptr == '\n')) |
| 1324 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1325 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1326 | /* stop here */ |
| 1327 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1328 | case HTTP_MSG_RPBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1329 | http_msg_rpbefore_cr: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1330 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1331 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1332 | /* stop here */ |
| 1333 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1334 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1335 | http_msg_rpver: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1336 | case HTTP_MSG_RPVER_SP: |
| 1337 | case HTTP_MSG_RPCODE: |
| 1338 | case HTTP_MSG_RPCODE_SP: |
| 1339 | case HTTP_MSG_RPREASON: |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 1340 | ptr = (char *)http_parse_stsline(msg, buf->data, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1341 | state, ptr, end, |
| 1342 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1343 | if (unlikely(!ptr)) |
| 1344 | return; |
| 1345 | |
| 1346 | /* we have a full response and we know that we have either a CR |
| 1347 | * or an LF at <ptr>. |
| 1348 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1349 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1350 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1351 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1352 | if (likely(*ptr == '\r')) |
| 1353 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1354 | goto http_msg_rpline_end; |
| 1355 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1356 | case HTTP_MSG_RPLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1357 | http_msg_rpline_end: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1358 | /* msg->sol must point to the first of CR or LF. */ |
| 1359 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1360 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1361 | /* stop here */ |
| 1362 | |
| 1363 | /* |
| 1364 | * Second, states that are specific to the request only |
| 1365 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1366 | case HTTP_MSG_RQBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1367 | http_msg_rqbefore: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1368 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1369 | /* we have a start of message, but we have to check |
| 1370 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1371 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1372 | */ |
Willy Tarreau | 89fa706 | 2012-03-02 16:13:16 +0100 | [diff] [blame] | 1373 | char *beg = buf->p; |
| 1374 | |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1375 | if (likely(ptr != beg)) { |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1376 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1377 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1378 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 1379 | bi_fast_delete(buf, ptr - beg); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1380 | } |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1381 | msg->sol = msg->som = ptr - buf->p; |
Willy Tarreau | f0d058e | 2007-01-25 12:03:42 +0100 | [diff] [blame] | 1382 | /* we will need this when keep-alive will be supported |
| 1383 | hdr_idx_init(idx); |
| 1384 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1385 | state = HTTP_MSG_RQMETH; |
| 1386 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1387 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1388 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1389 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1390 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1391 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1392 | if (unlikely(*ptr == '\n')) |
| 1393 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1394 | 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] | 1395 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1396 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1397 | case HTTP_MSG_RQBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1398 | http_msg_rqbefore_cr: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1399 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1400 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1401 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1402 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1403 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1404 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1405 | case HTTP_MSG_RQMETH_SP: |
| 1406 | case HTTP_MSG_RQURI: |
| 1407 | case HTTP_MSG_RQURI_SP: |
| 1408 | case HTTP_MSG_RQVER: |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 1409 | ptr = (char *)http_parse_reqline(msg, buf->data, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1410 | state, ptr, end, |
| 1411 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1412 | if (unlikely(!ptr)) |
| 1413 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1414 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1415 | /* we have a full request and we know that we have either a CR |
| 1416 | * or an LF at <ptr>. |
| 1417 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1418 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1419 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1420 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1421 | if (likely(*ptr == '\r')) |
| 1422 | 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] | 1423 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1424 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1425 | case HTTP_MSG_RQLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1426 | http_msg_rqline_end: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1427 | /* check for HTTP/0.9 request : no version information available. |
| 1428 | * msg->sol must point to the first of CR or LF. |
| 1429 | */ |
| 1430 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1431 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1432 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1433 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1434 | 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] | 1435 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1436 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1437 | /* |
| 1438 | * Common states below |
| 1439 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1440 | case HTTP_MSG_HDR_FIRST: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1441 | http_msg_hdr_first: |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1442 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1443 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1444 | goto http_msg_hdr_name; |
| 1445 | } |
| 1446 | |
| 1447 | if (likely(*ptr == '\r')) |
| 1448 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1449 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1450 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1451 | case HTTP_MSG_HDR_NAME: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1452 | http_msg_hdr_name: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1453 | /* assumes msg->sol points to the first char */ |
| 1454 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1455 | 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] | 1456 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1457 | if (likely(*ptr == ':')) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1458 | 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] | 1459 | |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1460 | if (likely(msg->err_pos < -1) || *ptr == '\n') |
| 1461 | goto http_msg_invalid; |
| 1462 | |
| 1463 | if (msg->err_pos == -1) /* capture error pointer */ |
| 1464 | msg->err_pos = ptr - buf->data; /* >= 0 now */ |
| 1465 | |
| 1466 | /* and we still accept this non-token character */ |
| 1467 | 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] | 1468 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1469 | case HTTP_MSG_HDR_L1_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1470 | http_msg_hdr_l1_sp: |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1471 | /* assumes msg->sol points to the first char */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1472 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1473 | 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] | 1474 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1475 | /* header value can be basically anything except CR/LF */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1476 | msg->sov = ptr - buf->p; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1477 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1478 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1479 | goto http_msg_hdr_val; |
| 1480 | } |
| 1481 | |
| 1482 | if (likely(*ptr == '\r')) |
| 1483 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1484 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1485 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1486 | case HTTP_MSG_HDR_L1_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1487 | http_msg_hdr_l1_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1488 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1489 | 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] | 1490 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1491 | case HTTP_MSG_HDR_L1_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1492 | http_msg_hdr_l1_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1493 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1494 | /* replace HT,CR,LF with spaces */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1495 | for (; buf->p + msg->sov < ptr; msg->sov++) |
| 1496 | buf->p[msg->sov] = ' '; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1497 | goto http_msg_hdr_l1_sp; |
| 1498 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1499 | /* we had a header consisting only in spaces ! */ |
Willy Tarreau | 12e48b3 | 2012-03-05 16:57:34 +0100 | [diff] [blame] | 1500 | msg->eol = msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1501 | goto http_msg_complete_header; |
| 1502 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1503 | case HTTP_MSG_HDR_VAL: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1504 | http_msg_hdr_val: |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1505 | /* assumes msg->sol points to the first char, and msg->sov |
| 1506 | * points to the first character of the value. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1507 | */ |
| 1508 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1509 | 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] | 1510 | |
Willy Tarreau | 12e48b3 | 2012-03-05 16:57:34 +0100 | [diff] [blame] | 1511 | msg->eol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1512 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1513 | * real header end in case it ends with lots of LWS, but is this |
| 1514 | * really needed ? |
| 1515 | */ |
| 1516 | if (likely(*ptr == '\r')) |
| 1517 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1518 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1519 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1520 | case HTTP_MSG_HDR_L2_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1521 | http_msg_hdr_l2_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1522 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1523 | 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] | 1524 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1525 | case HTTP_MSG_HDR_L2_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1526 | http_msg_hdr_l2_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1527 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1528 | /* LWS: replace HT,CR,LF with spaces */ |
Willy Tarreau | 12e48b3 | 2012-03-05 16:57:34 +0100 | [diff] [blame] | 1529 | for (; buf->p + msg->eol < ptr; msg->eol++) |
| 1530 | buf->p[msg->eol] = ' '; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1531 | goto http_msg_hdr_val; |
| 1532 | } |
| 1533 | http_msg_complete_header: |
| 1534 | /* |
| 1535 | * It was a new header, so the last one is finished. |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1536 | * Assumes msg->sol points to the first char, msg->sov points |
| 1537 | * to the first character of the value and msg->eol to the |
| 1538 | * first CR or LF so we know how the line ends. We insert last |
| 1539 | * header into the index. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1540 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1541 | 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] | 1542 | idx, idx->tail) < 0)) |
| 1543 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1544 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1545 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1546 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1547 | goto http_msg_hdr_name; |
| 1548 | } |
| 1549 | |
| 1550 | if (likely(*ptr == '\r')) |
| 1551 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1552 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1553 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1554 | case HTTP_MSG_LAST_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1555 | http_msg_last_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1556 | /* Assumes msg->sol points to the first of either CR or LF */ |
| 1557 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1558 | ptr++; |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1559 | msg->sov = msg->next = ptr - buf->p; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1560 | msg->eoh = msg->sol; |
| 1561 | msg->sol = 0; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1562 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1563 | return; |
Willy Tarreau | b56928a | 2012-04-16 14:51:55 +0200 | [diff] [blame] | 1564 | |
| 1565 | case HTTP_MSG_ERROR: |
| 1566 | /* this may only happen if we call http_msg_analyser() twice with an error */ |
| 1567 | break; |
| 1568 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1569 | #ifdef DEBUG_FULL |
| 1570 | default: |
| 1571 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1572 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1573 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1574 | } |
| 1575 | http_msg_ood: |
| 1576 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1577 | msg->msg_state = state; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1578 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1579 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1580 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1581 | http_msg_invalid: |
| 1582 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1583 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1584 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1585 | return; |
| 1586 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1587 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1588 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 1589 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 1590 | * nothing is done and 1 is returned. |
| 1591 | */ |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1592 | static int http_upgrade_v09_to_v10(struct http_txn *txn) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1593 | { |
| 1594 | int delta; |
| 1595 | char *cur_end; |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1596 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1597 | |
| 1598 | if (msg->sl.rq.v_l != 0) |
| 1599 | return 1; |
| 1600 | |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1601 | cur_end = msg->buf->p + msg->sol + msg->sl.rq.l; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1602 | delta = 0; |
| 1603 | |
| 1604 | if (msg->sl.rq.u_l == 0) { |
| 1605 | /* if no URI was set, add "/" */ |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1606 | delta = buffer_replace2(msg->buf, cur_end, cur_end, " /", 2); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1607 | cur_end += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1608 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1609 | } |
| 1610 | /* add HTTP version */ |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1611 | delta = buffer_replace2(msg->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11); |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1612 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1613 | cur_end += delta; |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1614 | cur_end = (char *)http_parse_reqline(msg, msg->buf->data, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1615 | HTTP_MSG_RQMETH, |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1616 | msg->buf->p + msg->sol, cur_end + 1, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1617 | NULL, NULL); |
| 1618 | if (unlikely(!cur_end)) |
| 1619 | return 0; |
| 1620 | |
| 1621 | /* we have a full HTTP/1.0 request now and we know that |
| 1622 | * we have either a CR or an LF at <ptr>. |
| 1623 | */ |
| 1624 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 1625 | return 1; |
| 1626 | } |
| 1627 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1628 | /* Parse the Connection: header of an HTTP request, looking for both "close" |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1629 | * and "keep-alive" values. If we already know that some headers may safely |
| 1630 | * 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] | 1631 | * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses) |
| 1632 | * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1). |
| 1633 | * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was |
| 1634 | * found, and TX_CON_*_SET is adjusted depending on what is left so only |
| 1635 | * harmless combinations may be removed. Do not call that after changes have |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1636 | * been processed. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1637 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1638 | 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] | 1639 | { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1640 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1641 | const char *hdr_val = "Connection"; |
| 1642 | int hdr_len = 10; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1643 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1644 | if (txn->flags & TX_HDR_CONN_PRS) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1645 | return; |
| 1646 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1647 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1648 | hdr_val = "Proxy-Connection"; |
| 1649 | hdr_len = 16; |
| 1650 | } |
| 1651 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1652 | ctx.idx = 0; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1653 | txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET); |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1654 | while (http_find_header2(hdr_val, hdr_len, msg->buf->p + msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1655 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1656 | txn->flags |= TX_HDR_CONN_KAL; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1657 | if (to_del & 2) |
| 1658 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1659 | else |
| 1660 | txn->flags |= TX_CON_KAL_SET; |
| 1661 | } |
| 1662 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1663 | txn->flags |= TX_HDR_CONN_CLO; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1664 | if (to_del & 1) |
| 1665 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1666 | else |
| 1667 | txn->flags |= TX_CON_CLO_SET; |
| 1668 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1669 | } |
| 1670 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1671 | txn->flags |= TX_HDR_CONN_PRS; |
| 1672 | return; |
| 1673 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1674 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1675 | /* Apply desired changes on the Connection: header. Values may be removed and/or |
| 1676 | * added depending on the <wanted> flags, which are exclusively composed of |
| 1677 | * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The |
| 1678 | * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left. |
| 1679 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1680 | 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] | 1681 | { |
| 1682 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1683 | const char *hdr_val = "Connection"; |
| 1684 | int hdr_len = 10; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1685 | |
| 1686 | ctx.idx = 0; |
| 1687 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1688 | |
| 1689 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1690 | hdr_val = "Proxy-Connection"; |
| 1691 | hdr_len = 16; |
| 1692 | } |
| 1693 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1694 | txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET); |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1695 | while (http_find_header2(hdr_val, hdr_len, msg->buf->p + msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1696 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1697 | if (wanted & TX_CON_KAL_SET) |
| 1698 | txn->flags |= TX_CON_KAL_SET; |
| 1699 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1700 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1701 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1702 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1703 | if (wanted & TX_CON_CLO_SET) |
| 1704 | txn->flags |= TX_CON_CLO_SET; |
| 1705 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1706 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 1707 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1708 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1709 | |
| 1710 | if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 1711 | return; |
| 1712 | |
| 1713 | if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) { |
| 1714 | txn->flags |= TX_CON_CLO_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1715 | hdr_val = "Connection: close"; |
| 1716 | hdr_len = 17; |
| 1717 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1718 | hdr_val = "Proxy-Connection: close"; |
| 1719 | hdr_len = 23; |
| 1720 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1721 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1722 | } |
| 1723 | |
| 1724 | if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) { |
| 1725 | txn->flags |= TX_CON_KAL_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1726 | hdr_val = "Connection: keep-alive"; |
| 1727 | hdr_len = 22; |
| 1728 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1729 | hdr_val = "Proxy-Connection: keep-alive"; |
| 1730 | hdr_len = 28; |
| 1731 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1732 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1733 | } |
| 1734 | return; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1735 | } |
| 1736 | |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1737 | /* 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] | 1738 | * first byte of body, and increments msg->sov by the number of bytes parsed, |
| 1739 | * so that we know we can forward between ->som and ->sov. Note that due to |
| 1740 | * possible wrapping at the end of the buffer, it is possible that msg->sov is |
| 1741 | * lower than msg->som. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1742 | * 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] | 1743 | * 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] | 1744 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1745 | int http_parse_chunk_size(struct http_msg *msg) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1746 | { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1747 | const struct buffer *buf = msg->buf; |
Willy Tarreau | cc5cfcb | 2012-05-04 21:35:27 +0200 | [diff] [blame] | 1748 | const char *ptr = b_ptr(buf, msg->next); |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1749 | const char *ptr_old = ptr; |
| 1750 | const char *end = buf->data + buf->size; |
Willy Tarreau | cc5cfcb | 2012-05-04 21:35:27 +0200 | [diff] [blame] | 1751 | const char *stop = bi_end(buf); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1752 | unsigned int chunk = 0; |
| 1753 | |
| 1754 | /* The chunk size is in the following form, though we are only |
| 1755 | * interested in the size and CRLF : |
| 1756 | * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF |
| 1757 | */ |
| 1758 | while (1) { |
| 1759 | int c; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1760 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1761 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1762 | c = hex2i(*ptr); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1763 | if (c < 0) /* not a hex digit anymore */ |
| 1764 | break; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1765 | if (++ptr >= end) |
| 1766 | ptr = buf->data; |
Willy Tarreau | 431946e | 2012-02-24 19:20:12 +0100 | [diff] [blame] | 1767 | if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */ |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1768 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1769 | chunk = (chunk << 4) + c; |
| 1770 | } |
| 1771 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1772 | /* empty size not allowed */ |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1773 | if (ptr == ptr_old) |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1774 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1775 | |
| 1776 | while (http_is_spht[(unsigned char)*ptr]) { |
| 1777 | if (++ptr >= end) |
| 1778 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1779 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1780 | return 0; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1781 | } |
| 1782 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1783 | /* Up to there, we know that at least one byte is present at *ptr. Check |
| 1784 | * for the end of chunk size. |
| 1785 | */ |
| 1786 | while (1) { |
| 1787 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1788 | /* we now have a CR or an LF at ptr */ |
| 1789 | if (likely(*ptr == '\r')) { |
| 1790 | if (++ptr >= end) |
| 1791 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1792 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1793 | return 0; |
| 1794 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1795 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1796 | if (*ptr != '\n') |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1797 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1798 | if (++ptr >= end) |
| 1799 | ptr = buf->data; |
| 1800 | /* done */ |
| 1801 | break; |
| 1802 | } |
| 1803 | else if (*ptr == ';') { |
| 1804 | /* chunk extension, ends at next CRLF */ |
| 1805 | if (++ptr >= end) |
| 1806 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1807 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1808 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1809 | |
| 1810 | while (!HTTP_IS_CRLF(*ptr)) { |
| 1811 | if (++ptr >= end) |
| 1812 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1813 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1814 | return 0; |
| 1815 | } |
| 1816 | /* we have a CRLF now, loop above */ |
| 1817 | continue; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1818 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1819 | else |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1820 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1821 | } |
| 1822 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1823 | /* 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] | 1824 | * 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] | 1825 | * ->sov. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1826 | */ |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1827 | msg->sov += ptr - ptr_old; |
| 1828 | msg->next = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 1829 | msg->chunk_len = chunk; |
| 1830 | msg->body_len += chunk; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1831 | msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1832 | return 1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1833 | error: |
| 1834 | msg->err_pos = ptr - buf->data; |
| 1835 | return -1; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1836 | } |
| 1837 | |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1838 | /* This function skips trailers in the buffer associated with HTTP |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1839 | * 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] | 1840 | * the trailers is found, it is automatically scheduled to be forwarded, |
| 1841 | * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0. |
| 1842 | * If not enough data are available, the function does not change anything |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1843 | * 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] | 1844 | * 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] | 1845 | * 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] | 1846 | * must already be in HTTP_MSG_TRAILERS state before calling this function, |
| 1847 | * which implies that all non-trailers data have already been scheduled for |
| 1848 | * forwarding, and that the difference between msg->som and msg->sov exactly |
| 1849 | * matches the length of trailers already parsed and not forwarded. It is also |
| 1850 | * important to note that this function is designed to be able to parse wrapped |
| 1851 | * headers at end of buffer. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1852 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1853 | int http_forward_trailers(struct http_msg *msg) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1854 | { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1855 | const struct buffer *buf = msg->buf; |
| 1856 | |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1857 | /* we have msg->next which points to next line. Look for CRLF. */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1858 | while (1) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1859 | const char *p1 = NULL, *p2 = NULL; |
Willy Tarreau | cc5cfcb | 2012-05-04 21:35:27 +0200 | [diff] [blame] | 1860 | const char *ptr = b_ptr(buf, msg->next); |
| 1861 | const char *stop = bi_end(buf); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1862 | int bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1863 | |
| 1864 | /* scan current line and stop at LF or CRLF */ |
| 1865 | while (1) { |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1866 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1867 | return 0; |
| 1868 | |
| 1869 | if (*ptr == '\n') { |
| 1870 | if (!p1) |
| 1871 | p1 = ptr; |
| 1872 | p2 = ptr; |
| 1873 | break; |
| 1874 | } |
| 1875 | |
| 1876 | if (*ptr == '\r') { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1877 | if (p1) { |
| 1878 | msg->err_pos = ptr - buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1879 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1880 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1881 | p1 = ptr; |
| 1882 | } |
| 1883 | |
| 1884 | ptr++; |
| 1885 | if (ptr >= buf->data + buf->size) |
| 1886 | ptr = buf->data; |
| 1887 | } |
| 1888 | |
| 1889 | /* after LF; point to beginning of next line */ |
| 1890 | p2++; |
| 1891 | if (p2 >= buf->data + buf->size) |
| 1892 | p2 = buf->data; |
| 1893 | |
Willy Tarreau | cc5cfcb | 2012-05-04 21:35:27 +0200 | [diff] [blame] | 1894 | bytes = p2 - b_ptr(buf, msg->next); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1895 | if (bytes < 0) |
| 1896 | bytes += buf->size; |
| 1897 | |
| 1898 | /* schedule this line for forwarding */ |
| 1899 | msg->sov += bytes; |
| 1900 | if (msg->sov >= buf->size) |
| 1901 | msg->sov -= buf->size; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1902 | |
Willy Tarreau | cc5cfcb | 2012-05-04 21:35:27 +0200 | [diff] [blame] | 1903 | if (p1 == b_ptr(buf, msg->next)) { |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1904 | /* LF/CRLF at beginning of line => end of trailers at p2. |
| 1905 | * Everything was scheduled for forwarding, there's nothing |
| 1906 | * left from this message. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 1907 | */ |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1908 | msg->next = buffer_count(buf, buf->p, p2); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1909 | msg->msg_state = HTTP_MSG_DONE; |
| 1910 | return 1; |
| 1911 | } |
| 1912 | /* OK, next line then */ |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1913 | msg->next = buffer_count(buf, buf->p, p2); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1914 | } |
| 1915 | } |
| 1916 | |
| 1917 | /* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or |
| 1918 | * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1919 | * ->som, ->next in order to include this part into the next forwarding phase. |
| 1920 | * 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] | 1921 | * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If |
| 1922 | * not enough data are available, the function does not change anything and |
| 1923 | * returns zero. If a parse error is encountered, the function returns < 0 and |
| 1924 | * does not change anything. Note: this function is designed to parse wrapped |
| 1925 | * CRLF at the end of the buffer. |
| 1926 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1927 | int http_skip_chunk_crlf(struct http_msg *msg) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1928 | { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1929 | const struct buffer *buf = msg->buf; |
| 1930 | const char *ptr; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1931 | int bytes; |
| 1932 | |
| 1933 | /* NB: we'll check data availabilty at the end. It's not a |
| 1934 | * problem because whatever we match first will be checked |
| 1935 | * against the correct length. |
| 1936 | */ |
| 1937 | bytes = 1; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1938 | ptr = buf->p; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1939 | if (*ptr == '\r') { |
| 1940 | bytes++; |
| 1941 | ptr++; |
| 1942 | if (ptr >= buf->data + buf->size) |
| 1943 | ptr = buf->data; |
| 1944 | } |
| 1945 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 1946 | if (bytes > buf->i) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1947 | return 0; |
| 1948 | |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1949 | if (*ptr != '\n') { |
| 1950 | msg->err_pos = ptr - buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1951 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1952 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1953 | |
| 1954 | ptr++; |
| 1955 | if (ptr >= buf->data + buf->size) |
| 1956 | ptr = buf->data; |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1957 | /* prepare the CRLF to be forwarded (between ->som and ->sov) */ |
| 1958 | msg->som = 0; |
| 1959 | msg->sov = msg->next = bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1960 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 1961 | return 1; |
| 1962 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1963 | |
Willy Tarreau | 21710ff | 2012-03-09 13:58:04 +0100 | [diff] [blame] | 1964 | /* This function realigns a possibly wrapping http message at the beginning |
| 1965 | * of its buffer. The function may only be used when the buffer's tail is |
| 1966 | * empty. |
| 1967 | */ |
| 1968 | void http_message_realign(struct http_msg *msg) |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 1969 | { |
Willy Tarreau | 21710ff | 2012-03-09 13:58:04 +0100 | [diff] [blame] | 1970 | struct buffer *buf = msg->buf; |
Willy Tarreau | 89fa706 | 2012-03-02 16:13:16 +0100 | [diff] [blame] | 1971 | int off = buf->data + buf->size - buf->p; |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 1972 | |
| 1973 | /* two possible cases : |
| 1974 | * - the buffer is in one contiguous block, we move it in-place |
Willy Tarreau | 8096de9 | 2010-02-26 11:12:27 +0100 | [diff] [blame] | 1975 | * - the buffer is in two blocks, we move it via the swap_buffer |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 1976 | */ |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 1977 | if (buf->i) { |
| 1978 | int block1 = buf->i; |
Willy Tarreau | 8096de9 | 2010-02-26 11:12:27 +0100 | [diff] [blame] | 1979 | int block2 = 0; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1980 | if (buf->p + buf->i > buf->data + buf->size) { |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 1981 | /* non-contiguous block */ |
Willy Tarreau | 89fa706 | 2012-03-02 16:13:16 +0100 | [diff] [blame] | 1982 | block1 = buf->data + buf->size - buf->p; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1983 | block2 = buf->p + buf->i - (buf->data + buf->size); |
Willy Tarreau | 8096de9 | 2010-02-26 11:12:27 +0100 | [diff] [blame] | 1984 | } |
| 1985 | if (block2) |
| 1986 | memcpy(swap_buffer, buf->data, block2); |
Willy Tarreau | 89fa706 | 2012-03-02 16:13:16 +0100 | [diff] [blame] | 1987 | memmove(buf->data, buf->p, block1); |
Willy Tarreau | 8096de9 | 2010-02-26 11:12:27 +0100 | [diff] [blame] | 1988 | if (block2) |
| 1989 | memcpy(buf->data + block1, swap_buffer, block2); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 1990 | } |
| 1991 | |
| 1992 | /* adjust all known pointers */ |
Willy Tarreau | 89fa706 | 2012-03-02 16:13:16 +0100 | [diff] [blame] | 1993 | buf->p = buf->data; |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 1994 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 1995 | if (msg->err_pos >= 0) { |
| 1996 | msg->err_pos += off; |
| 1997 | if (msg->err_pos >= buf->size) |
| 1998 | msg->err_pos -= buf->size; |
| 1999 | } |
| 2000 | |
| 2001 | buf->flags &= ~BF_FULL; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 2002 | if (bi_full(buf)) |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2003 | buf->flags |= BF_FULL; |
| 2004 | } |
| 2005 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2006 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 2007 | * processing can continue on next analysers, or zero if it either needs more |
| 2008 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
| 2009 | * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers |
| 2010 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 2011 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2012 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2013 | int http_wait_for_request(struct session *s, struct buffer *req, int an_bit) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2014 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2015 | /* |
| 2016 | * We will parse the partial (or complete) lines. |
| 2017 | * We will check the request syntax, and also join multi-line |
| 2018 | * headers. An index of all the lines will be elaborated while |
| 2019 | * parsing. |
| 2020 | * |
| 2021 | * For the parsing, we use a 28 states FSM. |
| 2022 | * |
| 2023 | * Here is the information we currently have : |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 2024 | * req->p + msg->som = beginning of request |
| 2025 | * req->p + msg->eoh = end of processed headers / start of current one |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2026 | * msg->eol = end of current header or line (LF or CRLF) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 2027 | * msg->next = first non-visited byte |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2028 | * req->r = end of data |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2029 | * |
| 2030 | * At end of parsing, we may perform a capture of the error (if any), and |
| 2031 | * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE). |
| 2032 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 2033 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2034 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2035 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2036 | int cur_idx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2037 | int use_close_only; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2038 | struct http_txn *txn = &s->txn; |
| 2039 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2040 | struct hdr_ctx ctx; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2041 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 2042 | 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] | 2043 | now_ms, __FUNCTION__, |
| 2044 | s, |
| 2045 | req, |
| 2046 | req->rex, req->wex, |
| 2047 | req->flags, |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 2048 | req->i, |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 2049 | req->analysers); |
| 2050 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 2051 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 2052 | s->srv_error = http_return_srv_error; |
| 2053 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2054 | /* There's a protected area at the end of the buffer for rewriting |
| 2055 | * purposes. We don't want to start to parse the request if the |
| 2056 | * protected area is affected, because we may have to move processed |
| 2057 | * data later, which is much more complicated. |
| 2058 | */ |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 2059 | if (buffer_not_empty(req) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2060 | if ((txn->flags & TX_NOT_FIRST) && |
| 2061 | unlikely((req->flags & BF_FULL) || |
Willy Tarreau | cc5cfcb | 2012-05-04 21:35:27 +0200 | [diff] [blame] | 2062 | bi_end(req) < b_ptr(req, msg->next) || |
| 2063 | bi_end(req) > req->data + req->size - global.tune.maxrewrite)) { |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 2064 | if (req->o) { |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 2065 | if (req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) |
| 2066 | goto failed_keep_alive; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2067 | /* some data has still not left the buffer, wake us once that's done */ |
| 2068 | buffer_dont_connect(req); |
| 2069 | req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 2070 | return 0; |
| 2071 | } |
Willy Tarreau | cc5cfcb | 2012-05-04 21:35:27 +0200 | [diff] [blame] | 2072 | if (bi_end(req) < b_ptr(req, msg->next) || |
| 2073 | bi_end(req) > req->data + req->size - global.tune.maxrewrite) |
Willy Tarreau | 21710ff | 2012-03-09 13:58:04 +0100 | [diff] [blame] | 2074 | http_message_realign(msg); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2075 | } |
| 2076 | |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2077 | /* Note that we have the same problem with the response ; we |
| 2078 | * may want to send a redirect, error or anything which requires |
| 2079 | * some spare space. So we'll ensure that we have at least |
| 2080 | * maxrewrite bytes available in the response buffer before |
| 2081 | * processing that one. This will only affect pipelined |
| 2082 | * keep-alive requests. |
| 2083 | */ |
| 2084 | if ((txn->flags & TX_NOT_FIRST) && |
| 2085 | unlikely((s->rep->flags & BF_FULL) || |
Willy Tarreau | cc5cfcb | 2012-05-04 21:35:27 +0200 | [diff] [blame] | 2086 | bi_end(s->rep) < b_ptr(s->rep, txn->rsp.next) || |
| 2087 | bi_end(s->rep) > s->rep->data + s->rep->size - global.tune.maxrewrite)) { |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 2088 | if (s->rep->o) { |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 2089 | if (s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) |
| 2090 | goto failed_keep_alive; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2091 | /* don't let a connection request be initiated */ |
| 2092 | buffer_dont_connect(req); |
Willy Tarreau | ff7b588 | 2010-01-22 14:41:29 +0100 | [diff] [blame] | 2093 | s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 0499e35 | 2010-12-17 07:13:42 +0100 | [diff] [blame] | 2094 | s->rep->analysers |= an_bit; /* wake us up once it changes */ |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2095 | return 0; |
| 2096 | } |
| 2097 | } |
| 2098 | |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 2099 | if (likely(msg->next < req->i)) /* some unparsed data are available */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 2100 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2101 | } |
| 2102 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2103 | /* 1: we might have to print this header in debug mode */ |
| 2104 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2105 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | c3bfeeb | 2010-04-16 09:14:45 +0200 | [diff] [blame] | 2106 | msg->sol && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2107 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2108 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2109 | |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 2110 | sol = req->p + msg->som; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2111 | eol = sol + msg->sl.rq.l; |
| 2112 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2113 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2114 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2115 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2116 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2117 | while (cur_idx) { |
| 2118 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2119 | debug_hdr("clihdr", s, sol, eol); |
| 2120 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2121 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2122 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2123 | } |
| 2124 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2125 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2126 | /* |
| 2127 | * Now we quickly check if we have found a full valid request. |
| 2128 | * If not so, we check the FD and buffer states before leaving. |
| 2129 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2130 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2131 | * requests are checked first. When waiting for a second request |
| 2132 | * on a keep-alive session, if we encounter and error, close, t/o, |
| 2133 | * we note the error in the session flags but don't set any state. |
| 2134 | * Since the error will be noted there, it will not be counted by |
| 2135 | * process_session() as a frontend error. |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2136 | * Last, we may increase some tracked counters' http request errors on |
| 2137 | * the cases that are deliberately the client's fault. For instance, |
| 2138 | * a timeout or connection reset is not counted as an error. However |
| 2139 | * a bad request is. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2140 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2141 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2142 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2143 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2144 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2145 | */ |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2146 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2147 | session_inc_http_req_ctr(s); |
| 2148 | session_inc_http_err_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2149 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2150 | goto return_bad_req; |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2151 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2152 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2153 | /* 1: Since we are in header mode, if there's no space |
| 2154 | * left for headers, we won't be able to free more |
| 2155 | * later, so the session will never terminate. We |
| 2156 | * must terminate it now. |
| 2157 | */ |
| 2158 | if (unlikely(req->flags & BF_FULL)) { |
| 2159 | /* FIXME: check if URI is set and return Status |
| 2160 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2161 | */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2162 | session_inc_http_req_ctr(s); |
| 2163 | session_inc_http_err_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2164 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 2165 | if (msg->err_pos < 0) |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 2166 | msg->err_pos = req->i; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2167 | goto return_bad_req; |
| 2168 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2169 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2170 | /* 2: have we encountered a read error ? */ |
| 2171 | else if (req->flags & BF_READ_ERROR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2172 | if (!(s->flags & SN_ERR_MASK)) |
| 2173 | s->flags |= SN_ERR_CLICL; |
| 2174 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2175 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2176 | goto failed_keep_alive; |
| 2177 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2178 | /* we cannot return any message on error */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2179 | if (msg->err_pos >= 0) { |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2180 | 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] | 2181 | session_inc_http_err_ctr(s); |
| 2182 | } |
| 2183 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2184 | msg->msg_state = HTTP_MSG_ERROR; |
| 2185 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2186 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2187 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2188 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2189 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2190 | if (s->listener->counters) |
| 2191 | s->listener->counters->failed_req++; |
| 2192 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2193 | if (!(s->flags & SN_FINST_MASK)) |
| 2194 | s->flags |= SN_FINST_R; |
| 2195 | return 0; |
| 2196 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2197 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2198 | /* 3: has the read timeout expired ? */ |
| 2199 | else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2200 | if (!(s->flags & SN_ERR_MASK)) |
| 2201 | s->flags |= SN_ERR_CLITO; |
| 2202 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2203 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2204 | goto failed_keep_alive; |
| 2205 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2206 | /* read timeout : give up with an error message. */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2207 | if (msg->err_pos >= 0) { |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2208 | 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] | 2209 | session_inc_http_err_ctr(s); |
| 2210 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2211 | txn->status = 408; |
| 2212 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408)); |
| 2213 | msg->msg_state = HTTP_MSG_ERROR; |
| 2214 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2215 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2216 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2217 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2218 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2219 | if (s->listener->counters) |
| 2220 | s->listener->counters->failed_req++; |
| 2221 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2222 | if (!(s->flags & SN_FINST_MASK)) |
| 2223 | s->flags |= SN_FINST_R; |
| 2224 | return 0; |
| 2225 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2226 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2227 | /* 4: have we encountered a close ? */ |
| 2228 | else if (req->flags & BF_SHUTR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2229 | if (!(s->flags & SN_ERR_MASK)) |
| 2230 | s->flags |= SN_ERR_CLICL; |
| 2231 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2232 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2233 | goto failed_keep_alive; |
| 2234 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2235 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2236 | 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] | 2237 | txn->status = 400; |
| 2238 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
| 2239 | msg->msg_state = HTTP_MSG_ERROR; |
| 2240 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2241 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2242 | session_inc_http_err_ctr(s); |
| 2243 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2244 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2245 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2246 | if (s->listener->counters) |
| 2247 | s->listener->counters->failed_req++; |
| 2248 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2249 | if (!(s->flags & SN_FINST_MASK)) |
| 2250 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2251 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2252 | } |
| 2253 | |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2254 | buffer_dont_connect(req); |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2255 | req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | ff7b588 | 2010-01-22 14:41:29 +0100 | [diff] [blame] | 2256 | s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2257 | #ifdef TCP_QUICKACK |
| 2258 | if (s->listener->options & LI_O_NOQUICKACK) { |
| 2259 | /* We need more data, we have to re-enable quick-ack in case we |
| 2260 | * previously disabled it, otherwise we might cause the client |
| 2261 | * to delay next data. |
| 2262 | */ |
| 2263 | setsockopt(s->si[0].fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); |
| 2264 | } |
| 2265 | #endif |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2266 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2267 | if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) { |
| 2268 | /* If the client starts to talk, let's fall back to |
| 2269 | * request timeout processing. |
| 2270 | */ |
| 2271 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2272 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2273 | } |
| 2274 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2275 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2276 | if (!tick_isset(req->analyse_exp)) { |
| 2277 | if ((msg->msg_state == HTTP_MSG_RQBEFORE) && |
| 2278 | (txn->flags & TX_WAIT_NEXT_RQ) && |
| 2279 | tick_isset(s->be->timeout.httpka)) |
| 2280 | req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka); |
| 2281 | else |
| 2282 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
| 2283 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2284 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2285 | /* we're not ready yet */ |
| 2286 | return 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2287 | |
| 2288 | failed_keep_alive: |
| 2289 | /* Here we process low-level errors for keep-alive requests. In |
| 2290 | * short, if the request is not the first one and it experiences |
| 2291 | * a timeout, read error or shutdown, we just silently close so |
| 2292 | * that the client can try again. |
| 2293 | */ |
| 2294 | txn->status = 0; |
| 2295 | msg->msg_state = HTTP_MSG_RQBEFORE; |
| 2296 | req->analysers = 0; |
| 2297 | s->logs.logwait = 0; |
Willy Tarreau | ff7b588 | 2010-01-22 14:41:29 +0100 | [diff] [blame] | 2298 | s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 2299 | stream_int_retnclose(req->prod, NULL); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2300 | return 0; |
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 | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2303 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 2304 | * complete the request parsing by setting a few fields we will need |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 2305 | * later. At this point, we have the last CRLF at req->data + msg->eoh. |
| 2306 | * 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] | 2307 | * 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] | 2308 | * byte after the last LF. msg->sov points to the first byte of data. |
| 2309 | * msg->eol cannot be trusted because it may have been left uninitialized |
| 2310 | * (for instance in the absence of headers). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2311 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2312 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2313 | session_inc_http_req_ctr(s); |
Willy Tarreau | d9b587f | 2010-02-26 10:05:55 +0100 | [diff] [blame] | 2314 | proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */ |
| 2315 | |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2316 | if (txn->flags & TX_WAIT_NEXT_RQ) { |
| 2317 | /* kill the pending keep-alive timeout */ |
| 2318 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 2319 | req->analyse_exp = TICK_ETERNITY; |
| 2320 | } |
| 2321 | |
| 2322 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2323 | /* Maybe we found in invalid header name while we were configured not |
| 2324 | * to block on that, so we have to capture it now. |
| 2325 | */ |
| 2326 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2327 | 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] | 2328 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2329 | /* |
| 2330 | * 1: identify the method |
| 2331 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 2332 | txn->meth = find_http_meth(req->p + msg->sol, msg->sl.rq.m_l); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2333 | |
| 2334 | /* we can make use of server redirect on GET and HEAD */ |
| 2335 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 2336 | s->flags |= SN_REDIRECTABLE; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2337 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2338 | /* |
| 2339 | * 2: check if the URI matches the monitor_uri. |
| 2340 | * We have to do this for every request which gets in, because |
| 2341 | * the monitor-uri is defined by the frontend. |
| 2342 | */ |
| 2343 | if (unlikely((s->fe->monitor_uri_len != 0) && |
| 2344 | (s->fe->monitor_uri_len == msg->sl.rq.u_l) && |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 2345 | !memcmp(req->p + msg->sol + msg->sl.rq.u, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2346 | s->fe->monitor_uri, |
| 2347 | s->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2348 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2349 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2350 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2351 | struct acl_cond *cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2352 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2353 | s->flags |= SN_MONITOR; |
Willy Tarreau | eabea07 | 2011-09-10 23:29:44 +0200 | [diff] [blame] | 2354 | s->fe->fe_counters.intercepted_req++; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2355 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2356 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2357 | list_for_each_entry(cond, &s->fe->mon_fail_cond, list) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 2358 | 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] | 2359 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2360 | ret = acl_pass(ret); |
| 2361 | if (cond->pol == ACL_COND_UNLESS) |
| 2362 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2363 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2364 | if (ret) { |
| 2365 | /* we fail this request, let's return 503 service unavail */ |
| 2366 | txn->status = 503; |
| 2367 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503)); |
| 2368 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2369 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2370 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 2371 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2372 | /* nothing to fail, let's reply normaly */ |
| 2373 | txn->status = 200; |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 2374 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_200)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2375 | goto return_prx_cond; |
| 2376 | } |
| 2377 | |
| 2378 | /* |
| 2379 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 2380 | * Note: we cannot log anymore if the request has been |
| 2381 | * classified as invalid. |
| 2382 | */ |
| 2383 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 2384 | /* we have a complete HTTP request that we must log */ |
| 2385 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
| 2386 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2387 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2388 | if (urilen >= REQURI_LEN) |
| 2389 | urilen = REQURI_LEN - 1; |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 2390 | memcpy(txn->uri, &req->p[msg->som], urilen); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2391 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2392 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2393 | if (!(s->logs.logwait &= ~LW_REQ)) |
| 2394 | s->do_log(s); |
| 2395 | } else { |
| 2396 | Alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2397 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2398 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2399 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 2400 | if (!LIST_ISEMPTY(&s->fe->format_unique_id)) { |
| 2401 | s->unique_id = pool_alloc2(pool2_uniqueid); |
| 2402 | } |
| 2403 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2404 | /* 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] | 2405 | 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] | 2406 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2407 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2408 | /* ... and check if the request is HTTP/1.1 or above */ |
| 2409 | if ((msg->sl.rq.v_l == 8) && |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 2410 | ((req->p[msg->sol + msg->sl.rq.v + 5] > '1') || |
| 2411 | ((req->p[msg->sol + msg->sl.rq.v + 5] == '1') && |
| 2412 | (req->p[msg->sol + msg->sl.rq.v + 7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2413 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2414 | |
| 2415 | /* "connection" has not been parsed yet */ |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2416 | txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2417 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2418 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 2419 | * we have what looks like a proxied connection instead of a connection, |
| 2420 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 2421 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 2422 | * happen to do that despite being non-standard :-( |
| 2423 | * We consider that a request not beginning with either '/' or '*' is |
| 2424 | * a proxied connection, which covers both "scheme://location" and |
| 2425 | * CONNECT ip:port. |
| 2426 | */ |
| 2427 | if ((s->fe->options2 & PR_O2_USE_PXHDR) && |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 2428 | req->p[msg->sol + msg->sl.rq.u] != '/' && req->p[msg->sol + msg->sl.rq.u] != '*') |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2429 | txn->flags |= TX_USE_PX_CONN; |
| 2430 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2431 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2432 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2433 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2434 | /* 5: we may need to capture headers */ |
Willy Tarreau | 42f7d89 | 2012-03-24 08:28:09 +0100 | [diff] [blame] | 2435 | if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap)) |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 2436 | capture_headers(req->p + msg->sol, &txn->hdr_idx, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2437 | txn->req.cap, s->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2438 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2439 | /* 6: determine the transfer-length. |
| 2440 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 2441 | * the presence of a message-body in a REQUEST and its transfer length |
| 2442 | * must be determined that way (in order of precedence) : |
| 2443 | * 1. The presence of a message-body in a request is signaled by the |
| 2444 | * inclusion of a Content-Length or Transfer-Encoding header field |
| 2445 | * in the request's header fields. When a request message contains |
| 2446 | * both a message-body of non-zero length and a method that does |
| 2447 | * not define any semantics for that request message-body, then an |
| 2448 | * origin server SHOULD either ignore the message-body or respond |
| 2449 | * with an appropriate error message (e.g., 413). A proxy or |
| 2450 | * gateway, when presented the same request, SHOULD either forward |
| 2451 | * the request inbound with the message- body or ignore the |
| 2452 | * message-body when determining a response. |
| 2453 | * |
| 2454 | * 2. If a Transfer-Encoding header field (Section 9.7) is present |
| 2455 | * and the "chunked" transfer-coding (Section 6.2) is used, the |
| 2456 | * transfer-length is defined by the use of this transfer-coding. |
| 2457 | * If a Transfer-Encoding header field is present and the "chunked" |
| 2458 | * transfer-coding is not present, the transfer-length is defined |
| 2459 | * by the sender closing the connection. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2460 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2461 | * 3. If a Content-Length header field is present, its decimal value in |
| 2462 | * OCTETs represents both the entity-length and the transfer-length. |
| 2463 | * If a message is received with both a Transfer-Encoding header |
| 2464 | * field and a Content-Length header field, the latter MUST be ignored. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2465 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2466 | * 4. By the server closing the connection. (Closing the connection |
| 2467 | * cannot be used to indicate the end of a request body, since that |
| 2468 | * would leave no possibility for the server to send back a response.) |
| 2469 | * |
| 2470 | * Whenever a transfer-coding is applied to a message-body, the set of |
| 2471 | * transfer-codings MUST include "chunked", unless the message indicates |
| 2472 | * it is terminated by closing the connection. When the "chunked" |
| 2473 | * transfer-coding is used, it MUST be the last transfer-coding applied |
| 2474 | * to the message-body. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2475 | */ |
| 2476 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2477 | use_close_only = 0; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2478 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2479 | /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2480 | while ((msg->flags & HTTP_MSGF_VER_11) && |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 2481 | http_find_header2("Transfer-Encoding", 17, req->p + msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2482 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2483 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 2484 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2485 | /* bad transfer-encoding (chunked followed by something else) */ |
| 2486 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2487 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2488 | break; |
| 2489 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2490 | } |
| 2491 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2492 | ctx.idx = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2493 | while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only && |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 2494 | http_find_header2("Content-Length", 14, req->p + msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2495 | signed long long cl; |
| 2496 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2497 | if (!ctx.vlen) { |
| 2498 | msg->err_pos = ctx.line + ctx.val - req->data; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2499 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2500 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2501 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2502 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
| 2503 | msg->err_pos = ctx.line + ctx.val - req->data; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2504 | goto return_bad_req; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2505 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2506 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2507 | if (cl < 0) { |
| 2508 | msg->err_pos = ctx.line + ctx.val - req->data; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2509 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2510 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2511 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2512 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2513 | msg->err_pos = ctx.line + ctx.val - req->data; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2514 | goto return_bad_req; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2515 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2516 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2517 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2518 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2519 | } |
| 2520 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2521 | /* bodyless requests have a known length */ |
| 2522 | if (!use_close_only) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2523 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2524 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2525 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2526 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2527 | req->analyse_exp = TICK_ETERNITY; |
| 2528 | return 1; |
| 2529 | |
| 2530 | return_bad_req: |
| 2531 | /* We centralize bad requests processing here */ |
| 2532 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 2533 | /* we detected a parsing error. We want to archive this request |
| 2534 | * in the dedicated proxy area for later troubleshooting. |
| 2535 | */ |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2536 | 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] | 2537 | } |
| 2538 | |
| 2539 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 2540 | txn->status = 400; |
| 2541 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2542 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2543 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2544 | if (s->listener->counters) |
| 2545 | s->listener->counters->failed_req++; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2546 | |
| 2547 | return_prx_cond: |
| 2548 | if (!(s->flags & SN_ERR_MASK)) |
| 2549 | s->flags |= SN_ERR_PRXCOND; |
| 2550 | if (!(s->flags & SN_FINST_MASK)) |
| 2551 | s->flags |= SN_FINST_R; |
| 2552 | |
| 2553 | req->analysers = 0; |
| 2554 | req->analyse_exp = TICK_ETERNITY; |
| 2555 | return 0; |
| 2556 | } |
| 2557 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2558 | /* We reached the stats page through a POST request. |
| 2559 | * Parse the posted data and enable/disable servers if necessary. |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2560 | * 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] | 2561 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2562 | int http_process_req_stat_post(struct stream_interface *si, struct http_txn *txn, struct buffer *req) |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2563 | { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2564 | struct proxy *px = NULL; |
| 2565 | struct server *sv = NULL; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2566 | |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2567 | char key[LINESIZE]; |
| 2568 | int action = ST_ADM_ACTION_NONE; |
| 2569 | int reprocess = 0; |
| 2570 | |
| 2571 | int total_servers = 0; |
| 2572 | int altered_servers = 0; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2573 | |
| 2574 | char *first_param, *cur_param, *next_param, *end_params; |
Willy Tarreau | 46787ed | 2012-04-11 17:28:40 +0200 | [diff] [blame] | 2575 | char *st_cur_param = NULL; |
| 2576 | char *st_next_param = NULL; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2577 | |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 2578 | first_param = req->p + txn->req.eoh + 2; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2579 | end_params = first_param + txn->req.body_len; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2580 | |
| 2581 | cur_param = next_param = end_params; |
| 2582 | |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2583 | if (end_params >= req->data + req->size - global.tune.maxrewrite) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2584 | /* Prevent buffer overflow */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2585 | si->applet.ctx.stats.st_code = STAT_STATUS_EXCD; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2586 | return 1; |
| 2587 | } |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2588 | else if (end_params > req->p + req->i) { |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2589 | /* we need more data */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2590 | si->applet.ctx.stats.st_code = STAT_STATUS_NONE; |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2591 | return 0; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2592 | } |
| 2593 | |
| 2594 | *end_params = '\0'; |
| 2595 | |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2596 | si->applet.ctx.stats.st_code = STAT_STATUS_NONE; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2597 | |
| 2598 | /* |
| 2599 | * Parse the parameters in reverse order to only store the last value. |
| 2600 | * From the html form, the backend and the action are at the end. |
| 2601 | */ |
| 2602 | while (cur_param > first_param) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2603 | char *value; |
| 2604 | int poffset, plen; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2605 | |
| 2606 | cur_param--; |
| 2607 | if ((*cur_param == '&') || (cur_param == first_param)) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2608 | reprocess_servers: |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2609 | /* Parse the key */ |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2610 | poffset = (cur_param != first_param ? 1 : 0); |
| 2611 | plen = next_param - cur_param + (cur_param == first_param ? 1 : 0); |
| 2612 | if ((plen > 0) && (plen <= sizeof(key))) { |
| 2613 | strncpy(key, cur_param + poffset, plen); |
| 2614 | key[plen - 1] = '\0'; |
| 2615 | } else { |
| 2616 | si->applet.ctx.stats.st_code = STAT_STATUS_EXCD; |
| 2617 | goto out; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2618 | } |
| 2619 | |
| 2620 | /* Parse the value */ |
| 2621 | value = key; |
| 2622 | while (*value != '\0' && *value != '=') { |
| 2623 | value++; |
| 2624 | } |
| 2625 | if (*value == '=') { |
| 2626 | /* Ok, a value is found, we can mark the end of the key */ |
| 2627 | *value++ = '\0'; |
| 2628 | } |
| 2629 | |
Willy Tarreau | bf9c2fc | 2011-05-31 18:06:18 +0200 | [diff] [blame] | 2630 | if (!url_decode(key) || !url_decode(value)) |
| 2631 | break; |
| 2632 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2633 | /* Now we can check the key to see what to do */ |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2634 | if (!px && (strcmp(key, "b") == 0)) { |
| 2635 | if ((px = findproxy(value, PR_CAP_BE)) == NULL) { |
| 2636 | /* the backend name is unknown or ambiguous (duplicate names) */ |
| 2637 | si->applet.ctx.stats.st_code = STAT_STATUS_ERRP; |
| 2638 | goto out; |
| 2639 | } |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2640 | } |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2641 | else if (!action && (strcmp(key, "action") == 0)) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2642 | if (strcmp(value, "disable") == 0) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2643 | action = ST_ADM_ACTION_DISABLE; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2644 | } |
| 2645 | else if (strcmp(value, "enable") == 0) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2646 | action = ST_ADM_ACTION_ENABLE; |
| 2647 | } |
| 2648 | else { |
| 2649 | si->applet.ctx.stats.st_code = STAT_STATUS_ERRP; |
| 2650 | goto out; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2651 | } |
| 2652 | } |
| 2653 | else if (strcmp(key, "s") == 0) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2654 | if (!(px && action)) { |
| 2655 | /* |
| 2656 | * Indicates that we'll need to reprocess the parameters |
| 2657 | * as soon as backend and action are known |
| 2658 | */ |
| 2659 | if (!reprocess) { |
| 2660 | st_cur_param = cur_param; |
| 2661 | st_next_param = next_param; |
| 2662 | } |
| 2663 | reprocess = 1; |
| 2664 | } |
| 2665 | else if ((sv = findserver(px, value)) != NULL) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2666 | switch (action) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2667 | case ST_ADM_ACTION_DISABLE: |
Cyril Bonté | 1e2a170 | 2011-03-03 21:05:17 +0100 | [diff] [blame] | 2668 | if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2669 | /* Not already in maintenance, we can change the server state */ |
| 2670 | sv->state |= SRV_MAINTAIN; |
| 2671 | set_server_down(sv); |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2672 | altered_servers++; |
| 2673 | total_servers++; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2674 | } |
| 2675 | break; |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2676 | case ST_ADM_ACTION_ENABLE: |
Cyril Bonté | 1e2a170 | 2011-03-03 21:05:17 +0100 | [diff] [blame] | 2677 | if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2678 | /* Already in maintenance, we can change the server state */ |
| 2679 | set_server_up(sv); |
Willy Tarreau | 7046130 | 2010-10-22 14:39:02 +0200 | [diff] [blame] | 2680 | sv->health = sv->rise; /* up, but will fall down at first failure */ |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2681 | altered_servers++; |
| 2682 | total_servers++; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2683 | } |
| 2684 | break; |
| 2685 | } |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2686 | } else { |
| 2687 | /* the server name is unknown or ambiguous (duplicate names) */ |
| 2688 | total_servers++; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2689 | } |
| 2690 | } |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2691 | if (reprocess && px && action) { |
| 2692 | /* Now, we know the backend and the action chosen by the user. |
| 2693 | * We can safely restart from the first server parameter |
| 2694 | * to reprocess them |
| 2695 | */ |
| 2696 | cur_param = st_cur_param; |
| 2697 | next_param = st_next_param; |
| 2698 | reprocess = 0; |
| 2699 | goto reprocess_servers; |
| 2700 | } |
| 2701 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2702 | next_param = cur_param; |
| 2703 | } |
| 2704 | } |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2705 | |
| 2706 | if (total_servers == 0) { |
| 2707 | si->applet.ctx.stats.st_code = STAT_STATUS_NONE; |
| 2708 | } |
| 2709 | else if (altered_servers == 0) { |
| 2710 | si->applet.ctx.stats.st_code = STAT_STATUS_ERRP; |
| 2711 | } |
| 2712 | else if (altered_servers == total_servers) { |
| 2713 | si->applet.ctx.stats.st_code = STAT_STATUS_DONE; |
| 2714 | } |
| 2715 | else { |
| 2716 | si->applet.ctx.stats.st_code = STAT_STATUS_PART; |
| 2717 | } |
| 2718 | out: |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2719 | return 1; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2720 | } |
| 2721 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2722 | /* returns a pointer to the first rule which forbids access (deny or http_auth), |
| 2723 | * or NULL if everything's OK. |
| 2724 | */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2725 | static inline struct http_req_rule * |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2726 | http_check_access_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn) |
| 2727 | { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2728 | struct http_req_rule *rule; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2729 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2730 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2731 | int ret = 1; |
| 2732 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2733 | if (rule->action >= HTTP_REQ_ACT_MAX) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2734 | continue; |
| 2735 | |
| 2736 | /* check condition, but only if attached */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2737 | if (rule->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 2738 | 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] | 2739 | ret = acl_pass(ret); |
| 2740 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2741 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2742 | ret = !ret; |
| 2743 | } |
| 2744 | |
| 2745 | if (ret) { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2746 | if (rule->action == HTTP_REQ_ACT_ALLOW) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2747 | return NULL; /* no problem */ |
| 2748 | else |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2749 | return rule; /* most likely a deny or auth rule */ |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2750 | } |
| 2751 | } |
| 2752 | return NULL; |
| 2753 | } |
| 2754 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2755 | /* This stream analyser runs all HTTP request processing which is common to |
| 2756 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 2757 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2758 | * 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] | 2759 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 2760 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2761 | */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2762 | int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px) |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2763 | { |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2764 | struct http_txn *txn = &s->txn; |
| 2765 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2766 | struct acl_cond *cond; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2767 | struct http_req_rule *http_req_last_rule = NULL; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2768 | struct redirect_rule *rule; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 2769 | struct cond_wordlist *wl; |
Simon Horman | 70735c9 | 2011-06-07 11:07:50 +0900 | [diff] [blame] | 2770 | int do_stats; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2771 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2772 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2773 | /* we need more data */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2774 | buffer_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2775 | return 0; |
| 2776 | } |
| 2777 | |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2778 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2779 | req->analyse_exp = TICK_ETERNITY; |
| 2780 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 2781 | 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] | 2782 | now_ms, __FUNCTION__, |
| 2783 | s, |
| 2784 | req, |
| 2785 | req->rex, req->wex, |
| 2786 | req->flags, |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 2787 | req->i, |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2788 | req->analysers); |
| 2789 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2790 | /* first check whether we have some ACLs set to block this request */ |
| 2791 | list_for_each_entry(cond, &px->block_cond, list) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 2792 | 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] | 2793 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2794 | ret = acl_pass(ret); |
| 2795 | if (cond->pol == ACL_COND_UNLESS) |
| 2796 | ret = !ret; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 2797 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2798 | if (ret) { |
| 2799 | txn->status = 403; |
| 2800 | /* let's log the request time */ |
| 2801 | s->logs.tv_request = now; |
| 2802 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2803 | session_inc_http_err_ctr(s); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2804 | goto return_prx_cond; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2805 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2806 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2807 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2808 | /* evaluate http-request rules */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2809 | http_req_last_rule = http_check_access_rule(px, &px->http_req_rules, s, txn); |
Willy Tarreau | 5142594 | 2010-02-01 10:40:19 +0100 | [diff] [blame] | 2810 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2811 | /* evaluate stats http-request rules only if http-request is OK */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2812 | if (!http_req_last_rule) { |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2813 | do_stats = stats_check_uri(s->rep->prod, txn, px); |
| 2814 | if (do_stats) |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2815 | http_req_last_rule = http_check_access_rule(px, &px->uri_auth->http_req_rules, s, txn); |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 2816 | } |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2817 | else |
| 2818 | do_stats = 0; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 2819 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2820 | /* return a 403 if either rule has blocked */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2821 | if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_DENY) { |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 2822 | txn->status = 403; |
| 2823 | s->logs.tv_request = now; |
| 2824 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2825 | session_inc_http_err_ctr(s); |
Willy Tarreau | 6da0f6d | 2011-01-06 18:19:50 +0100 | [diff] [blame] | 2826 | s->fe->fe_counters.denied_req++; |
| 2827 | if (an_bit == AN_REQ_HTTP_PROCESS_BE) |
| 2828 | s->be->be_counters.denied_req++; |
| 2829 | if (s->listener->counters) |
| 2830 | s->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 2831 | goto return_prx_cond; |
| 2832 | } |
| 2833 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2834 | /* try headers filters */ |
| 2835 | if (px->req_exp != NULL) { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 2836 | if (apply_filters_to_request(s, req, px) < 0) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2837 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2838 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2839 | /* has the request been denied ? */ |
| 2840 | if (txn->flags & TX_CLDENY) { |
| 2841 | /* no need to go further */ |
| 2842 | txn->status = 403; |
| 2843 | /* let's log the request time */ |
| 2844 | s->logs.tv_request = now; |
| 2845 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2846 | session_inc_http_err_ctr(s); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2847 | goto return_prx_cond; |
| 2848 | } |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 2849 | |
| 2850 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 2851 | * which may be the same as the connect timeout if unspecified. |
| 2852 | * If unset, then set it to zero because we really want it to |
| 2853 | * eventually expire. We build the tarpit as an analyser. |
| 2854 | */ |
| 2855 | if (txn->flags & TX_CLTARPIT) { |
| 2856 | buffer_erase(s->req); |
| 2857 | /* wipe the request out so that we can drop the connection early |
| 2858 | * if the client closes first. |
| 2859 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2860 | buffer_dont_connect(req); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 2861 | req->analysers = 0; /* remove switching rules etc... */ |
| 2862 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 2863 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 2864 | if (!req->analyse_exp) |
| 2865 | req->analyse_exp = tick_add(now_ms, 0); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2866 | session_inc_http_err_ctr(s); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 2867 | return 1; |
| 2868 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2869 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2870 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2871 | /* Until set to anything else, the connection mode is set as TUNNEL. It will |
| 2872 | * only change if both the request and the config reference something else. |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2873 | * Option httpclose by itself does not set a mode, it remains a tunnel mode |
| 2874 | * 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] | 2875 | * affect it (eg: server-close/keep-alive + httpclose = close). Note that we |
| 2876 | * avoid to redo the same work if FE and BE have the same settings (common). |
| 2877 | * The method consists in checking if options changed between the two calls |
| 2878 | * (implying that either one is non-null, or one of them is non-null and we |
| 2879 | * are there for the first time. |
Willy Tarreau | 4273664 | 2009-10-18 21:04:35 +0200 | [diff] [blame] | 2880 | */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2881 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 2882 | if ((!(txn->flags & TX_HDR_CONN_PRS) && |
| 2883 | (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) || |
| 2884 | ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) != |
| 2885 | (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] | 2886 | int tmp = TX_CON_WANT_TUN; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2887 | |
Cyril Bonté | 9ea2b9a | 2010-12-29 09:36:56 +0100 | [diff] [blame] | 2888 | if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE || |
| 2889 | ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2890 | tmp = TX_CON_WANT_KAL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2891 | if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO) |
| 2892 | tmp = TX_CON_WANT_SCL; |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2893 | if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2894 | tmp = TX_CON_WANT_CLO; |
| 2895 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2896 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
| 2897 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2898 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2899 | if (!(txn->flags & TX_HDR_CONN_PRS)) { |
| 2900 | /* parse the Connection header and possibly clean it */ |
| 2901 | int to_del = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2902 | if ((msg->flags & HTTP_MSGF_VER_11) || |
Willy Tarreau | 8a8e1d9 | 2010-04-05 16:15:16 +0200 | [diff] [blame] | 2903 | ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL && |
| 2904 | !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2905 | to_del |= 2; /* remove "keep-alive" */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2906 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2907 | to_del |= 1; /* remove "close" */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2908 | http_parse_connection_header(txn, msg, to_del); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2909 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2910 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2911 | /* check if client or config asks for explicit close in KAL/SCL */ |
| 2912 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 2913 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) && |
| 2914 | ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2915 | (!(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] | 2916 | ((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] | 2917 | !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */ |
Willy Tarreau | c3e8b25 | 2010-01-28 15:01:20 +0100 | [diff] [blame] | 2918 | s->fe->state == PR_STSTOPPED)) /* frontend is stopping */ |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2919 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 2920 | } |
Willy Tarreau | 7859991 | 2009-10-17 20:12:21 +0200 | [diff] [blame] | 2921 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2922 | /* we can be blocked here because the request needs to be authenticated, |
| 2923 | * either to pass or to access stats. |
| 2924 | */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2925 | if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_HTTP_AUTH) { |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 2926 | struct chunk msg; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2927 | char *realm = http_req_last_rule->http_auth.realm; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 2928 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 2929 | if (!realm) |
| 2930 | realm = do_stats?STATS_DEFAULT_REALM:px->id; |
| 2931 | |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 2932 | sprintf(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] | 2933 | chunk_initlen(&msg, trash, sizeof(trash), strlen(trash)); |
| 2934 | txn->status = 401; |
| 2935 | stream_int_retnclose(req->prod, &msg); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2936 | /* on 401 we still count one error, because normal browsing |
| 2937 | * won't significantly increase the counter but brute force |
| 2938 | * attempts will. |
| 2939 | */ |
| 2940 | session_inc_http_err_ctr(s); |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 2941 | goto return_prx_cond; |
| 2942 | } |
| 2943 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2944 | /* add request headers from the rule sets in the same order */ |
| 2945 | list_for_each_entry(wl, &px->req_add, list) { |
| 2946 | if (wl->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 2947 | 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] | 2948 | ret = acl_pass(ret); |
| 2949 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 2950 | ret = !ret; |
| 2951 | if (!ret) |
| 2952 | continue; |
| 2953 | } |
| 2954 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2955 | 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] | 2956 | goto return_bad_req; |
| 2957 | } |
| 2958 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 2959 | if (do_stats) { |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 2960 | struct stats_admin_rule *stats_admin_rule; |
| 2961 | |
| 2962 | /* We need to provide stats for this request. |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2963 | * FIXME!!! that one is rather dangerous, we want to |
| 2964 | * make it follow standard rules (eg: clear req->analysers). |
| 2965 | */ |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 2966 | |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 2967 | /* now check whether we have some admin rules for this request */ |
| 2968 | list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) { |
| 2969 | int ret = 1; |
| 2970 | |
| 2971 | if (stats_admin_rule->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 2972 | ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 2973 | ret = acl_pass(ret); |
| 2974 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 2975 | ret = !ret; |
| 2976 | } |
| 2977 | |
| 2978 | if (ret) { |
| 2979 | /* no rule, or the rule matches */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2980 | s->rep->prod->applet.ctx.stats.flags |= STAT_ADMIN; |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 2981 | break; |
| 2982 | } |
| 2983 | } |
| 2984 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2985 | /* Was the status page requested with a POST ? */ |
| 2986 | if (txn->meth == HTTP_METH_POST) { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2987 | if (s->rep->prod->applet.ctx.stats.flags & STAT_ADMIN) { |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2988 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 2989 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 2990 | * send an HTTP/1.1 100 Continue intermediate response. |
| 2991 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2992 | if (msg->flags & HTTP_MSGF_VER_11) { |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2993 | struct hdr_ctx ctx; |
| 2994 | ctx.idx = 0; |
| 2995 | /* Expect is allowed in 1.1, look for it */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 2996 | if (http_find_header2("Expect", 6, req->p + msg->sol, &txn->hdr_idx, &ctx) && |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2997 | 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] | 2998 | bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len); |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2999 | } |
| 3000 | } |
| 3001 | msg->msg_state = HTTP_MSG_100_SENT; |
| 3002 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 3003 | } |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 3004 | if (!http_process_req_stat_post(s->rep->prod, txn, req)) { |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 3005 | /* we need more data */ |
| 3006 | req->analysers |= an_bit; |
| 3007 | buffer_dont_connect(req); |
| 3008 | return 0; |
| 3009 | } |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 3010 | } else { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 3011 | s->rep->prod->applet.ctx.stats.st_code = STAT_STATUS_DENY; |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 3012 | } |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 3013 | } |
| 3014 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3015 | s->logs.tv_request = now; |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3016 | s->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | b24281b | 2011-02-13 13:16:36 +0100 | [diff] [blame] | 3017 | stream_int_register_handler(s->rep->prod, &http_stats_applet); |
Willy Tarreau | 7b7a8e9 | 2011-03-27 19:53:06 +0200 | [diff] [blame] | 3018 | copy_target(&s->target, &s->rep->prod->target); // for logging only |
Willy Tarreau | bc4af05 | 2011-02-13 13:25:14 +0100 | [diff] [blame] | 3019 | s->rep->prod->applet.private = s; |
| 3020 | s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0; |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3021 | req->analysers = 0; |
Willy Tarreau | eabea07 | 2011-09-10 23:29:44 +0200 | [diff] [blame] | 3022 | if (s->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 3023 | s->fe->fe_counters.intercepted_req++; |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3024 | |
| 3025 | return 0; |
| 3026 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3027 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 3028 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3029 | /* check whether we have some ACLs set to redirect this request */ |
| 3030 | list_for_each_entry(rule, &px->redirect_rules, list) { |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3031 | int ret = ACL_PAT_PASS; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3032 | |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3033 | if (rule->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 3034 | 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] | 3035 | ret = acl_pass(ret); |
| 3036 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 3037 | ret = !ret; |
| 3038 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3039 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3040 | if (ret) { |
Willy Tarreau | 3bb9c23 | 2010-01-03 12:24:37 +0100 | [diff] [blame] | 3041 | struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 }; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3042 | const char *msg_fmt; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3043 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3044 | /* build redirect message */ |
| 3045 | switch(rule->code) { |
| 3046 | case 303: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3047 | msg_fmt = HTTP_303; |
| 3048 | break; |
| 3049 | case 301: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3050 | msg_fmt = HTTP_301; |
| 3051 | break; |
| 3052 | case 302: |
| 3053 | default: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3054 | msg_fmt = HTTP_302; |
| 3055 | break; |
| 3056 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3057 | |
Willy Tarreau | 3bb9c23 | 2010-01-03 12:24:37 +0100 | [diff] [blame] | 3058 | if (unlikely(!chunk_strcpy(&rdr, msg_fmt))) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3059 | goto return_bad_req; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3060 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3061 | switch(rule->type) { |
| 3062 | case REDIRECT_TYPE_PREFIX: { |
| 3063 | const char *path; |
| 3064 | int pathlen; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3065 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3066 | path = http_get_path(txn); |
| 3067 | /* build message using path */ |
| 3068 | if (path) { |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 3069 | pathlen = txn->req.sl.rq.u_l + (req->p + txn->req.sol + txn->req.sl.rq.u) - path; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3070 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3071 | int qs = 0; |
| 3072 | while (qs < pathlen) { |
| 3073 | if (path[qs] == '?') { |
| 3074 | pathlen = qs; |
| 3075 | break; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3076 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3077 | qs++; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3078 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3079 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3080 | } else { |
| 3081 | path = "/"; |
| 3082 | pathlen = 1; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3083 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3084 | |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 3085 | if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3086 | goto return_bad_req; |
| 3087 | |
| 3088 | /* add prefix. Note that if prefix == "/", we don't want to |
| 3089 | * add anything, otherwise it makes it hard for the user to |
| 3090 | * configure a self-redirection. |
| 3091 | */ |
| 3092 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3093 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 3094 | rdr.len += rule->rdr_len; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3095 | } |
| 3096 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3097 | /* add path */ |
| 3098 | memcpy(rdr.str + rdr.len, path, pathlen); |
| 3099 | rdr.len += pathlen; |
Willy Tarreau | 81e3b4f | 2010-01-10 00:42:19 +0100 | [diff] [blame] | 3100 | |
| 3101 | /* append a slash at the end of the location is needed and missing */ |
| 3102 | if (rdr.len && rdr.str[rdr.len - 1] != '/' && |
| 3103 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 3104 | if (rdr.len > rdr.size - 5) |
| 3105 | goto return_bad_req; |
| 3106 | rdr.str[rdr.len] = '/'; |
| 3107 | rdr.len++; |
| 3108 | } |
| 3109 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3110 | break; |
| 3111 | } |
| 3112 | case REDIRECT_TYPE_LOCATION: |
| 3113 | default: |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 3114 | if (rdr.len + rule->rdr_len > rdr.size - 4) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3115 | goto return_bad_req; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3116 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3117 | /* add location */ |
| 3118 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 3119 | rdr.len += rule->rdr_len; |
| 3120 | break; |
| 3121 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3122 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3123 | if (rule->cookie_len) { |
| 3124 | memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14); |
| 3125 | rdr.len += 14; |
| 3126 | memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len); |
| 3127 | rdr.len += rule->cookie_len; |
| 3128 | memcpy(rdr.str + rdr.len, "\r\n", 2); |
| 3129 | rdr.len += 2; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3130 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3131 | |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3132 | /* add end of headers and the keep-alive/close status. |
| 3133 | * We may choose to set keep-alive if the Location begins |
| 3134 | * with a slash, because the client will come back to the |
| 3135 | * same server. |
| 3136 | */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3137 | txn->status = rule->code; |
| 3138 | /* let's log the request time */ |
| 3139 | s->logs.tv_request = now; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3140 | |
| 3141 | if (rule->rdr_len >= 1 && *rule->rdr_str == '/' && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3142 | (msg->flags & HTTP_MSGF_XFER_LEN) && |
| 3143 | !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len && |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3144 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 3145 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 3146 | /* keep-alive possible */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3147 | if (!(msg->flags & HTTP_MSGF_VER_11)) { |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 3148 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 3149 | memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: keep-alive", 30); |
| 3150 | rdr.len += 30; |
| 3151 | } else { |
| 3152 | memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24); |
| 3153 | rdr.len += 24; |
| 3154 | } |
Willy Tarreau | 7566145 | 2010-01-10 10:35:01 +0100 | [diff] [blame] | 3155 | } |
| 3156 | memcpy(rdr.str + rdr.len, "\r\n\r\n", 4); |
| 3157 | rdr.len += 4; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 3158 | bo_inject(req->prod->ob, rdr.str, rdr.len); |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3159 | /* "eat" the request */ |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 3160 | bi_fast_delete(req, msg->sov - msg->som); |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3161 | msg->som = msg->sov; |
| 3162 | req->analysers = AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 9300fb2 | 2010-01-05 00:58:24 +0100 | [diff] [blame] | 3163 | s->rep->analysers = AN_RES_HTTP_XFER_BODY; |
| 3164 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3165 | txn->rsp.msg_state = HTTP_MSG_DONE; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3166 | break; |
| 3167 | } else { |
| 3168 | /* keep-alive not possible */ |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 3169 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 3170 | memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 3171 | rdr.len += 29; |
| 3172 | } else { |
| 3173 | memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23); |
| 3174 | rdr.len += 23; |
| 3175 | } |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 3176 | stream_int_retnclose(req->prod, &rdr); |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3177 | goto return_prx_cond; |
| 3178 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3179 | } |
| 3180 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3181 | |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 3182 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 3183 | * If this happens, then the data will not come immediately, so we must |
| 3184 | * send all what we have without waiting. Note that due to the small gain |
| 3185 | * in waiting for the body of the request, it's easier to simply put the |
| 3186 | * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove |
| 3187 | * itself once used. |
| 3188 | */ |
| 3189 | req->flags |= BF_SEND_DONTWAIT; |
| 3190 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3191 | /* that's OK for us now, let's move on to next analysers */ |
| 3192 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 3193 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3194 | return_bad_req: |
| 3195 | /* We centralize bad requests processing here */ |
| 3196 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 3197 | /* we detected a parsing error. We want to archive this request |
| 3198 | * in the dedicated proxy area for later troubleshooting. |
| 3199 | */ |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 3200 | 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] | 3201 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3202 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3203 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3204 | txn->status = 400; |
| 3205 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3206 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3207 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3208 | if (s->listener->counters) |
| 3209 | s->listener->counters->failed_req++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 3210 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3211 | return_prx_cond: |
| 3212 | if (!(s->flags & SN_ERR_MASK)) |
| 3213 | s->flags |= SN_ERR_PRXCOND; |
| 3214 | if (!(s->flags & SN_FINST_MASK)) |
| 3215 | s->flags |= SN_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 3216 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3217 | req->analysers = 0; |
| 3218 | req->analyse_exp = TICK_ETERNITY; |
| 3219 | return 0; |
| 3220 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3221 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3222 | /* This function performs all the processing enabled for the current request. |
| 3223 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 3224 | * needs more data, encounters an error, or wants to immediately abort the |
| 3225 | * request. It relies on buffers flags, and updates s->req->analysers. |
| 3226 | */ |
| 3227 | int http_process_request(struct session *s, struct buffer *req, int an_bit) |
| 3228 | { |
| 3229 | struct http_txn *txn = &s->txn; |
| 3230 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3231 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3232 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3233 | /* we need more data */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3234 | buffer_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3235 | return 0; |
| 3236 | } |
| 3237 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 3238 | 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] | 3239 | now_ms, __FUNCTION__, |
| 3240 | s, |
| 3241 | req, |
| 3242 | req->rex, req->wex, |
| 3243 | req->flags, |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 3244 | req->i, |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3245 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3246 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3247 | /* |
| 3248 | * Right now, we know that we have processed the entire headers |
| 3249 | * and that unwanted requests have been filtered out. We can do |
| 3250 | * whatever we want with the remaining request. Also, now we |
| 3251 | * may have separate values for ->fe, ->be. |
| 3252 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3253 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3254 | /* |
| 3255 | * If HTTP PROXY is set we simply get remote server address |
| 3256 | * parsing incoming request. |
| 3257 | */ |
| 3258 | if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) { |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 3259 | url2sa(req->p + msg->sol + msg->sl.rq.u, msg->sl.rq.u_l, &s->req->cons->addr.to); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3260 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3261 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3262 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3263 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3264 | * Note that doing so might move headers in the request, but |
| 3265 | * the fields will stay coherent and the URI will not move. |
| 3266 | * This should only be performed in the backend. |
| 3267 | */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 3268 | 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] | 3269 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
| 3270 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 3271 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3272 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3273 | * 8: the appsession cookie was looked up very early in 1.2, |
| 3274 | * so let's do the same now. |
| 3275 | */ |
| 3276 | |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 3277 | /* It needs to look into the URI unless persistence must be ignored */ |
| 3278 | if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) { |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 3279 | get_srv_from_appsession(s, req->p + msg->sol + msg->sl.rq.u, msg->sl.rq.u_l); |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3280 | } |
| 3281 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3282 | /* add unique-id if "header-unique-id" is specified */ |
| 3283 | |
| 3284 | if (!LIST_ISEMPTY(&s->fe->format_unique_id)) |
| 3285 | build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id); |
| 3286 | |
| 3287 | if (s->fe->header_unique_id && s->unique_id) { |
| 3288 | int ret = snprintf(trash, global.tune.bufsize, "%s: %s", s->fe->header_unique_id, s->unique_id); |
| 3289 | if (ret < 0 || ret > global.tune.bufsize) |
| 3290 | goto return_bad_req; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3291 | if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, trash) < 0)) |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3292 | goto return_bad_req; |
| 3293 | } |
| 3294 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3295 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3296 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 3297 | * asks for it. |
| 3298 | */ |
| 3299 | if ((s->fe->options | s->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 3300 | struct hdr_ctx ctx = { .idx = 0 }; |
| 3301 | |
| 3302 | if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) && |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 3303 | http_find_header2(s->be->fwdfor_hdr_name, s->be->fwdfor_hdr_len, req->p + txn->req.sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 3304 | /* The header is set to be added only if none is present |
| 3305 | * and we found it, so don't do anything. |
| 3306 | */ |
| 3307 | } |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3308 | else if (s->req->prod->addr.from.ss_family == AF_INET) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3309 | /* Add an X-Forwarded-For header unless the source IP is |
| 3310 | * in the 'except' network range. |
| 3311 | */ |
| 3312 | if ((!s->fe->except_mask.s_addr || |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3313 | (((struct sockaddr_in *)&s->req->prod->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3314 | != s->fe->except_net.s_addr) && |
| 3315 | (!s->be->except_mask.s_addr || |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3316 | (((struct sockaddr_in *)&s->req->prod->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3317 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3318 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3319 | unsigned char *pn; |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3320 | pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.from)->sin_addr; |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3321 | |
| 3322 | /* Note: we rely on the backend to get the header name to be used for |
| 3323 | * x-forwarded-for, because the header is really meant for the backends. |
| 3324 | * However, if the backend did not specify any option, we have to rely |
| 3325 | * on the frontend's header name. |
| 3326 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3327 | if (s->be->fwdfor_hdr_len) { |
| 3328 | len = s->be->fwdfor_hdr_len; |
| 3329 | memcpy(trash, s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3330 | } else { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3331 | len = s->fe->fwdfor_hdr_len; |
| 3332 | memcpy(trash, s->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3333 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3334 | len += sprintf(trash + 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] | 3335 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3336 | if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, len) < 0)) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3337 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3338 | } |
| 3339 | } |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3340 | else if (s->req->prod->addr.from.ss_family == AF_INET6) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3341 | /* FIXME: for the sake of completeness, we should also support |
| 3342 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3343 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3344 | int len; |
| 3345 | char pn[INET6_ADDRSTRLEN]; |
| 3346 | inet_ntop(AF_INET6, |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3347 | (const void *)&((struct sockaddr_in6 *)(&s->req->prod->addr.from))->sin6_addr, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3348 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3349 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3350 | /* Note: we rely on the backend to get the header name to be used for |
| 3351 | * x-forwarded-for, because the header is really meant for the backends. |
| 3352 | * However, if the backend did not specify any option, we have to rely |
| 3353 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3354 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3355 | if (s->be->fwdfor_hdr_len) { |
| 3356 | len = s->be->fwdfor_hdr_len; |
| 3357 | memcpy(trash, s->be->fwdfor_hdr_name, len); |
| 3358 | } else { |
| 3359 | len = s->fe->fwdfor_hdr_len; |
| 3360 | memcpy(trash, s->fe->fwdfor_hdr_name, len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3361 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3362 | len += sprintf(trash + len, ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3363 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3364 | if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, len) < 0)) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3365 | goto return_bad_req; |
| 3366 | } |
| 3367 | } |
| 3368 | |
| 3369 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3370 | * 10: add X-Original-To if either the frontend or the backend |
| 3371 | * asks for it. |
| 3372 | */ |
| 3373 | if ((s->fe->options | s->be->options) & PR_O_ORGTO) { |
| 3374 | |
| 3375 | /* FIXME: don't know if IPv6 can handle that case too. */ |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3376 | if (s->req->prod->addr.from.ss_family == AF_INET) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3377 | /* Add an X-Original-To header unless the destination IP is |
| 3378 | * in the 'except' network range. |
| 3379 | */ |
Willy Tarreau | 9b061e3 | 2012-04-07 18:03:52 +0200 | [diff] [blame] | 3380 | stream_sock_get_to_addr(s->req->prod); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3381 | |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3382 | if (s->req->prod->addr.to.ss_family == AF_INET && |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 3383 | ((!s->fe->except_mask_to.s_addr || |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3384 | (((struct sockaddr_in *)&s->req->prod->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] | 3385 | != s->fe->except_to.s_addr) && |
| 3386 | (!s->be->except_mask_to.s_addr || |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3387 | (((struct sockaddr_in *)&s->req->prod->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] | 3388 | != s->be->except_to.s_addr))) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3389 | int len; |
| 3390 | unsigned char *pn; |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3391 | pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.to)->sin_addr; |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3392 | |
| 3393 | /* Note: we rely on the backend to get the header name to be used for |
| 3394 | * x-original-to, because the header is really meant for the backends. |
| 3395 | * However, if the backend did not specify any option, we have to rely |
| 3396 | * on the frontend's header name. |
| 3397 | */ |
| 3398 | if (s->be->orgto_hdr_len) { |
| 3399 | len = s->be->orgto_hdr_len; |
| 3400 | memcpy(trash, s->be->orgto_hdr_name, len); |
| 3401 | } else { |
| 3402 | len = s->fe->orgto_hdr_len; |
| 3403 | memcpy(trash, s->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3404 | } |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3405 | len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]); |
| 3406 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3407 | if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, len) < 0)) |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3408 | goto return_bad_req; |
| 3409 | } |
| 3410 | } |
| 3411 | } |
| 3412 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3413 | /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */ |
| 3414 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Cyril Bonté | 9ea2b9a | 2010-12-29 09:36:56 +0100 | [diff] [blame] | 3415 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3416 | unsigned int want_flags = 0; |
| 3417 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3418 | if (msg->flags & HTTP_MSGF_VER_11) { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 3419 | if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL || |
| 3420 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) && |
| 3421 | !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3422 | want_flags |= TX_CON_CLO_SET; |
| 3423 | } else { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 3424 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
| 3425 | !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) || |
| 3426 | ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3427 | want_flags |= TX_CON_KAL_SET; |
| 3428 | } |
| 3429 | |
| 3430 | 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] | 3431 | http_change_connection_header(txn, msg, want_flags); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3432 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3433 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3434 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3435 | /* If we have no server assigned yet and we're balancing on url_param |
| 3436 | * with a POST request, we may be interested in checking the body for |
| 3437 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3438 | */ |
| 3439 | if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) && |
| 3440 | s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3441 | s->be->url_param_post_limit != 0 && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3442 | (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3443 | buffer_dont_connect(req); |
| 3444 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3445 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3446 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3447 | if (msg->flags & HTTP_MSGF_XFER_LEN) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3448 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 3449 | #ifdef TCP_QUICKACK |
| 3450 | /* We expect some data from the client. Unless we know for sure |
| 3451 | * we already have a full request, we have to re-enable quick-ack |
| 3452 | * in case we previously disabled it, otherwise we might cause |
| 3453 | * the client to delay further data. |
| 3454 | */ |
| 3455 | if ((s->listener->options & LI_O_NOQUICKACK) && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3456 | ((msg->flags & HTTP_MSGF_TE_CHNK) || |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 3457 | (msg->body_len > req->i - txn->req.eoh - 2))) |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 3458 | setsockopt(s->si[0].fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); |
| 3459 | #endif |
| 3460 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 3461 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3462 | /************************************************************* |
| 3463 | * OK, that's finished for the headers. We have done what we * |
| 3464 | * could. Let's switch to the DATA state. * |
| 3465 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3466 | req->analyse_exp = TICK_ETERNITY; |
| 3467 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3468 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3469 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3470 | /* OK let's go on with the BODY now */ |
| 3471 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3472 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3473 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3474 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 3475 | /* we detected a parsing error. We want to archive this request |
| 3476 | * in the dedicated proxy area for later troubleshooting. |
| 3477 | */ |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 3478 | 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] | 3479 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3480 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3481 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3482 | txn->status = 400; |
| 3483 | req->analysers = 0; |
| 3484 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3485 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3486 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3487 | if (s->listener->counters) |
| 3488 | s->listener->counters->failed_req++; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3489 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3490 | if (!(s->flags & SN_ERR_MASK)) |
| 3491 | s->flags |= SN_ERR_PRXCOND; |
| 3492 | if (!(s->flags & SN_FINST_MASK)) |
| 3493 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3494 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3495 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3496 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3497 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 3498 | * returns zero, at the beginning because it prevents any other processing |
| 3499 | * from occurring, and at the end because it terminates the request. |
| 3500 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3501 | int http_process_tarpit(struct session *s, struct buffer *req, int an_bit) |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3502 | { |
| 3503 | struct http_txn *txn = &s->txn; |
| 3504 | |
| 3505 | /* This connection is being tarpitted. The CLIENT side has |
| 3506 | * already set the connect expiration date to the right |
| 3507 | * timeout. We just have to check that the client is still |
| 3508 | * there and that the timeout has not expired. |
| 3509 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3510 | buffer_dont_connect(req); |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3511 | if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 && |
| 3512 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 3513 | return 0; |
| 3514 | |
| 3515 | /* We will set the queue timer to the time spent, just for |
| 3516 | * logging purposes. We fake a 500 server error, so that the |
| 3517 | * attacker will not suspect his connection has been tarpitted. |
| 3518 | * It will not cause trouble to the logs because we can exclude |
| 3519 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 3520 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3521 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 3522 | |
| 3523 | txn->status = 500; |
| 3524 | if (req->flags != BF_READ_ERROR) |
| 3525 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500)); |
| 3526 | |
| 3527 | req->analysers = 0; |
| 3528 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3529 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3530 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3531 | if (s->listener->counters) |
| 3532 | s->listener->counters->failed_req++; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3533 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3534 | if (!(s->flags & SN_ERR_MASK)) |
| 3535 | s->flags |= SN_ERR_PRXCOND; |
| 3536 | if (!(s->flags & SN_FINST_MASK)) |
| 3537 | s->flags |= SN_FINST_T; |
| 3538 | return 0; |
| 3539 | } |
| 3540 | |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3541 | /* This function is an analyser which processes the HTTP request body. It looks |
| 3542 | * for parameters to be used for the load balancing algorithm (url_param). It |
| 3543 | * must only be called after the standard HTTP request processing has occurred, |
| 3544 | * because it expects the request to be parsed. It returns zero if it needs to |
| 3545 | * read more data, or 1 once it has completed its analysis. |
| 3546 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3547 | int http_process_request_body(struct session *s, struct buffer *req, int an_bit) |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3548 | { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3549 | struct http_txn *txn = &s->txn; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3550 | struct http_msg *msg = &s->txn.req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3551 | long long limit = s->be->url_param_post_limit; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3552 | |
| 3553 | /* We have to parse the HTTP request body to find any required data. |
| 3554 | * "balance url_param check_post" should have been the only way to get |
| 3555 | * into this. We were brought here after HTTP header analysis, so all |
| 3556 | * related structures are ready. |
| 3557 | */ |
| 3558 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3559 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 3560 | goto missing_data; |
| 3561 | |
| 3562 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 3563 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 3564 | * send an HTTP/1.1 100 Continue intermediate response. |
| 3565 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3566 | if (msg->flags & HTTP_MSGF_VER_11) { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3567 | struct hdr_ctx ctx; |
| 3568 | ctx.idx = 0; |
| 3569 | /* Expect is allowed in 1.1, look for it */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 3570 | if (http_find_header2("Expect", 6, req->p + msg->sol, &txn->hdr_idx, &ctx) && |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3571 | 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] | 3572 | bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3573 | } |
| 3574 | } |
| 3575 | msg->msg_state = HTTP_MSG_100_SENT; |
| 3576 | } |
| 3577 | |
| 3578 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 3579 | /* we have msg->sov which points to the first byte of message body. |
| 3580 | * msg->som still points to the beginning of the message. We must |
| 3581 | * save the body in msg->next because it survives buffer re-alignments. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3582 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 3583 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 3584 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3585 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3586 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 3587 | else |
| 3588 | msg->msg_state = HTTP_MSG_DATA; |
| 3589 | } |
| 3590 | |
| 3591 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 3592 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 3593 | * 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] | 3594 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3595 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 3596 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3597 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3598 | if (!ret) |
| 3599 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3600 | else if (ret < 0) { |
| 3601 | session_inc_http_err_ctr(s); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3602 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3603 | } |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3604 | } |
| 3605 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3606 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 3607 | * We have the first data byte is in msg->sov. We're waiting for at |
| 3608 | * least <url_param_post_limit> bytes after msg->sov. |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3609 | */ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3610 | |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 3611 | if (msg->body_len < limit) |
| 3612 | limit = msg->body_len; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3613 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 3614 | if (req->i - (msg->sov - msg->som) >= limit) /* we have enough bytes now */ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3615 | goto http_end; |
| 3616 | |
| 3617 | missing_data: |
| 3618 | /* we get here if we need to wait for more data */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3619 | if (req->flags & BF_FULL) { |
| 3620 | session_inc_http_err_ctr(s); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3621 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3622 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3623 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3624 | if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) { |
| 3625 | txn->status = 408; |
| 3626 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408)); |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 3627 | |
| 3628 | if (!(s->flags & SN_ERR_MASK)) |
| 3629 | s->flags |= SN_ERR_CLITO; |
| 3630 | if (!(s->flags & SN_FINST_MASK)) |
| 3631 | s->flags |= SN_FINST_D; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3632 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3633 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3634 | |
| 3635 | /* we get here if we need to wait for more data */ |
| 3636 | if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) { |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3637 | /* Not enough data. We'll re-use the http-request |
| 3638 | * timeout here. Ideally, we should set the timeout |
| 3639 | * relative to the accept() date. We just set the |
| 3640 | * request timeout once at the beginning of the |
| 3641 | * request. |
| 3642 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3643 | buffer_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3644 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 3645 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3646 | return 0; |
| 3647 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3648 | |
| 3649 | http_end: |
| 3650 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 3651 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 3652 | req->analysers &= ~an_bit; |
| 3653 | req->analyse_exp = TICK_ETERNITY; |
| 3654 | return 1; |
| 3655 | |
| 3656 | return_bad_req: /* let's centralize all bad requests */ |
| 3657 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3658 | txn->status = 400; |
| 3659 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
| 3660 | |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 3661 | if (!(s->flags & SN_ERR_MASK)) |
| 3662 | s->flags |= SN_ERR_PRXCOND; |
| 3663 | if (!(s->flags & SN_FINST_MASK)) |
| 3664 | s->flags |= SN_FINST_R; |
| 3665 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3666 | return_err_msg: |
| 3667 | req->analysers = 0; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3668 | s->fe->fe_counters.failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3669 | if (s->listener->counters) |
| 3670 | s->listener->counters->failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3671 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3672 | } |
| 3673 | |
Willy Tarreau | 45c0d98 | 2012-03-09 12:11:57 +0100 | [diff] [blame] | 3674 | /* send a server's name with an outgoing request over an established connection */ |
| 3675 | 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] | 3676 | |
| 3677 | struct hdr_ctx ctx; |
| 3678 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3679 | char *hdr_name = be->server_id_hdr_name; |
| 3680 | int hdr_name_len = be->server_id_hdr_len; |
| 3681 | |
| 3682 | char *hdr_val; |
| 3683 | |
William Lallemand | d9e9066 | 2012-01-30 17:27:17 +0100 | [diff] [blame] | 3684 | ctx.idx = 0; |
| 3685 | |
Willy Tarreau | 45c0d98 | 2012-03-09 12:11:57 +0100 | [diff] [blame] | 3686 | while (http_find_header2(hdr_name, hdr_name_len, txn->req.buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) { |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3687 | /* remove any existing values from the header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3688 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3689 | } |
| 3690 | |
| 3691 | /* Add the new header requested with the server value */ |
| 3692 | hdr_val = trash; |
| 3693 | memcpy(hdr_val, hdr_name, hdr_name_len); |
| 3694 | hdr_val += hdr_name_len; |
| 3695 | *hdr_val++ = ':'; |
| 3696 | *hdr_val++ = ' '; |
| 3697 | hdr_val += strlcpy2(hdr_val, srv_name, trash + sizeof(trash) - hdr_val); |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3698 | http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, hdr_val - trash); |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3699 | |
| 3700 | return 0; |
| 3701 | } |
| 3702 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3703 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 3704 | * right now but it works. |
| 3705 | */ |
| 3706 | void http_end_txn_clean_session(struct session *s) |
| 3707 | { |
| 3708 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 3709 | * server's connections. We need a safer way to reinitialize buffer |
| 3710 | * flags. We also need a more accurate method for computing per-request |
| 3711 | * data. |
| 3712 | */ |
| 3713 | http_silent_debug(__LINE__, s); |
| 3714 | |
| 3715 | s->req->cons->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 060781f | 2012-05-07 16:50:03 +0200 | [diff] [blame^] | 3716 | s->req->cons->sock.shutr(s->req->cons); |
| 3717 | s->req->cons->sock.shutw(s->req->cons); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3718 | |
| 3719 | http_silent_debug(__LINE__, s); |
| 3720 | |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 3721 | if (s->flags & SN_BE_ASSIGNED) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3722 | s->be->beconn--; |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 3723 | if (unlikely(s->srv_conn)) |
| 3724 | sess_change_server(s, NULL); |
| 3725 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3726 | |
| 3727 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 3728 | session_process_counters(s); |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 3729 | session_stop_backend_counters(s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3730 | |
| 3731 | if (s->txn.status) { |
| 3732 | int n; |
| 3733 | |
| 3734 | n = s->txn.status / 100; |
| 3735 | if (n < 1 || n > 5) |
| 3736 | n = 0; |
| 3737 | |
| 3738 | if (s->fe->mode == PR_MODE_HTTP) |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3739 | s->fe->fe_counters.p.http.rsp[n]++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3740 | |
Willy Tarreau | 2465779 | 2010-02-26 10:30:28 +0100 | [diff] [blame] | 3741 | if ((s->flags & SN_BE_ASSIGNED) && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3742 | (s->be->mode == PR_MODE_HTTP)) |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3743 | s->be->be_counters.p.http.rsp[n]++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3744 | } |
| 3745 | |
| 3746 | /* don't count other requests' data */ |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 3747 | s->logs.bytes_in -= s->req->i; |
| 3748 | s->logs.bytes_out -= s->rep->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3749 | |
| 3750 | /* let's do a final log if we need it */ |
| 3751 | if (s->logs.logwait && |
| 3752 | !(s->flags & SN_MONITOR) && |
| 3753 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
| 3754 | s->do_log(s); |
| 3755 | } |
| 3756 | |
| 3757 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 3758 | s->logs.tv_accept = now; /* corrected date for internal use */ |
| 3759 | tv_zero(&s->logs.tv_request); |
| 3760 | s->logs.t_queue = -1; |
| 3761 | s->logs.t_connect = -1; |
| 3762 | s->logs.t_data = -1; |
| 3763 | s->logs.t_close = 0; |
| 3764 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 3765 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 3766 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 3767 | s->logs.bytes_in = s->req->total = s->req->i; |
| 3768 | s->logs.bytes_out = s->rep->total = s->rep->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3769 | |
| 3770 | if (s->pend_pos) |
| 3771 | pendconn_free(s->pend_pos); |
| 3772 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 3773 | if (target_srv(&s->target)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3774 | if (s->flags & SN_CURR_SESS) { |
| 3775 | s->flags &= ~SN_CURR_SESS; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 3776 | target_srv(&s->target)->cur_sess--; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3777 | } |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 3778 | if (may_dequeue_tasks(target_srv(&s->target), s->be)) |
| 3779 | process_srv_queue(target_srv(&s->target)); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3780 | } |
| 3781 | |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 3782 | clear_target(&s->target); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3783 | |
| 3784 | s->req->cons->state = s->req->cons->prev_state = SI_ST_INI; |
| 3785 | s->req->cons->fd = -1; /* just to help with debugging */ |
| 3786 | s->req->cons->err_type = SI_ET_NONE; |
Willy Tarreau | 0b3a411 | 2011-03-27 19:16:56 +0200 | [diff] [blame] | 3787 | s->req->cons->conn_retries = 0; /* used for logging too */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3788 | s->req->cons->err_loc = NULL; |
| 3789 | s->req->cons->exp = TICK_ETERNITY; |
| 3790 | s->req->cons->flags = SI_FL_NONE; |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 3791 | s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST|BF_NEVER_WAIT); |
| 3792 | s->rep->flags &= ~(BF_SHUTR|BF_SHUTR_NOW|BF_READ_ATTACHED|BF_READ_ERROR|BF_READ_NOEXP|BF_STREAMER|BF_STREAMER_FAST|BF_WRITE_PARTIAL|BF_NEVER_WAIT); |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 3793 | 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] | 3794 | s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE); |
| 3795 | s->txn.meth = 0; |
| 3796 | http_reset_txn(s); |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 3797 | s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ; |
Willy Tarreau | ee55dc0 | 2010-06-01 10:56:34 +0200 | [diff] [blame] | 3798 | if (s->fe->options2 & PR_O2_INDEPSTR) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3799 | s->req->cons->flags |= SI_FL_INDEP_STR; |
| 3800 | |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 3801 | if (s->fe->options2 & PR_O2_NODELAY) { |
| 3802 | s->req->flags |= BF_NEVER_WAIT; |
| 3803 | s->rep->flags |= BF_NEVER_WAIT; |
| 3804 | } |
| 3805 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3806 | /* if the request buffer is not empty, it means we're |
| 3807 | * about to process another request, so send pending |
| 3808 | * data with MSG_MORE to merge TCP packets when possible. |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 3809 | * Just don't do this if the buffer is close to be full, |
| 3810 | * because the request will wait for it to flush a little |
| 3811 | * bit before proceeding. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3812 | */ |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 3813 | if (s->req->i) { |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 3814 | if (s->rep->o && |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 3815 | !(s->rep->flags & BF_FULL) && |
Willy Tarreau | cc5cfcb | 2012-05-04 21:35:27 +0200 | [diff] [blame] | 3816 | bi_end(s->rep) <= s->rep->data + s->rep->size - global.tune.maxrewrite) |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 3817 | s->rep->flags |= BF_EXPECT_MORE; |
| 3818 | } |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3819 | |
| 3820 | /* we're removing the analysers, we MUST re-enable events detection */ |
| 3821 | buffer_auto_read(s->req); |
| 3822 | buffer_auto_close(s->req); |
| 3823 | buffer_auto_read(s->rep); |
| 3824 | buffer_auto_close(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3825 | |
Willy Tarreau | 342b11c | 2010-11-24 16:22:09 +0100 | [diff] [blame] | 3826 | s->req->analysers = s->listener->analysers; |
| 3827 | s->req->analysers &= ~AN_REQ_DECODE_PROXY; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3828 | s->rep->analysers = 0; |
| 3829 | |
| 3830 | http_silent_debug(__LINE__, s); |
| 3831 | } |
| 3832 | |
| 3833 | |
| 3834 | /* This function updates the request state machine according to the response |
| 3835 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 3836 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 3837 | * it is only used to find when a request/response couple is complete. Both |
| 3838 | * this function and its equivalent should loop until both return zero. It |
| 3839 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 3840 | */ |
| 3841 | int http_sync_req_state(struct session *s) |
| 3842 | { |
| 3843 | struct buffer *buf = s->req; |
| 3844 | struct http_txn *txn = &s->txn; |
| 3845 | unsigned int old_flags = buf->flags; |
| 3846 | unsigned int old_state = txn->req.msg_state; |
| 3847 | |
| 3848 | http_silent_debug(__LINE__, s); |
| 3849 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) |
| 3850 | return 0; |
| 3851 | |
| 3852 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3853 | /* No need to read anymore, the request was completely parsed. |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 3854 | * We can shut the read side unless we want to abort_on_close, |
| 3855 | * or we have a POST request. The issue with POST requests is |
| 3856 | * that some browsers still send a CRLF after the request, and |
| 3857 | * this CRLF must be read so that it does not remain in the kernel |
| 3858 | * buffers, otherwise a close could cause an RST on some systems |
| 3859 | * (eg: Linux). |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3860 | */ |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 3861 | if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST) |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3862 | buffer_dont_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3863 | |
| 3864 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 3865 | goto wait_other_side; |
| 3866 | |
| 3867 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 3868 | /* The server has not finished to respond, so we |
| 3869 | * don't want to move in order not to upset it. |
| 3870 | */ |
| 3871 | goto wait_other_side; |
| 3872 | } |
| 3873 | |
| 3874 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 3875 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3876 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3877 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
| 3878 | goto wait_other_side; |
| 3879 | } |
| 3880 | |
| 3881 | /* When we get here, it means that both the request and the |
| 3882 | * response have finished receiving. Depending on the connection |
| 3883 | * mode, we'll have to wait for the last bytes to leave in either |
| 3884 | * direction, and sometimes for a close to be effective. |
| 3885 | */ |
| 3886 | |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3887 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 3888 | /* Server-close mode : queue a connection close to the server */ |
| 3889 | if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3890 | buffer_shutw_now(buf); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3891 | } |
| 3892 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 3893 | /* Option forceclose is set, or either side wants to close, |
| 3894 | * let's enforce it now that we're not expecting any new |
| 3895 | * data to come. The caller knows the session is complete |
| 3896 | * once both states are CLOSED. |
| 3897 | */ |
| 3898 | if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3899 | buffer_shutr_now(buf); |
| 3900 | buffer_shutw_now(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3901 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3902 | } |
| 3903 | else { |
| 3904 | /* The last possible modes are keep-alive and tunnel. Since tunnel |
| 3905 | * mode does not set the body analyser, we can't reach this place |
| 3906 | * in tunnel mode, so we're left with keep-alive only. |
| 3907 | * This mode is currently not implemented, we switch to tunnel mode. |
| 3908 | */ |
| 3909 | buffer_auto_read(buf); |
| 3910 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3911 | } |
| 3912 | |
| 3913 | if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) { |
| 3914 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3915 | buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
| 3916 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3917 | if (!(buf->flags & BF_OUT_EMPTY)) { |
| 3918 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 3919 | goto http_msg_closing; |
| 3920 | } |
| 3921 | else { |
| 3922 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3923 | goto http_msg_closed; |
| 3924 | } |
| 3925 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3926 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3927 | } |
| 3928 | |
| 3929 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 3930 | http_msg_closing: |
| 3931 | /* nothing else to forward, just waiting for the output buffer |
| 3932 | * to be empty and for the shutw_now to take effect. |
| 3933 | */ |
| 3934 | if (buf->flags & BF_OUT_EMPTY) { |
| 3935 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3936 | goto http_msg_closed; |
| 3937 | } |
| 3938 | else if (buf->flags & BF_SHUTW) { |
| 3939 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3940 | goto wait_other_side; |
| 3941 | } |
| 3942 | } |
| 3943 | |
| 3944 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 3945 | http_msg_closed: |
| 3946 | goto wait_other_side; |
| 3947 | } |
| 3948 | |
| 3949 | wait_other_side: |
| 3950 | http_silent_debug(__LINE__, s); |
| 3951 | return txn->req.msg_state != old_state || buf->flags != old_flags; |
| 3952 | } |
| 3953 | |
| 3954 | |
| 3955 | /* This function updates the response state machine according to the request |
| 3956 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 3957 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 3958 | * it is only used to find when a request/response couple is complete. Both |
| 3959 | * this function and its equivalent should loop until both return zero. It |
| 3960 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 3961 | */ |
| 3962 | int http_sync_res_state(struct session *s) |
| 3963 | { |
| 3964 | struct buffer *buf = s->rep; |
| 3965 | struct http_txn *txn = &s->txn; |
| 3966 | unsigned int old_flags = buf->flags; |
| 3967 | unsigned int old_state = txn->rsp.msg_state; |
| 3968 | |
| 3969 | http_silent_debug(__LINE__, s); |
| 3970 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 3971 | return 0; |
| 3972 | |
| 3973 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 3974 | /* In theory, we don't need to read anymore, but we must |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3975 | * still monitor the server connection for a possible close |
| 3976 | * while the request is being uploaded, so we don't disable |
| 3977 | * reading. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3978 | */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3979 | /* buffer_dont_read(buf); */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3980 | |
| 3981 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 3982 | goto wait_other_side; |
| 3983 | |
| 3984 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 3985 | /* The client seems to still be sending data, probably |
| 3986 | * because we got an error response during an upload. |
| 3987 | * We have the choice of either breaking the connection |
| 3988 | * or letting it pass through. Let's do the later. |
| 3989 | */ |
| 3990 | goto wait_other_side; |
| 3991 | } |
| 3992 | |
| 3993 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 3994 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3995 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3996 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
| 3997 | goto wait_other_side; |
| 3998 | } |
| 3999 | |
| 4000 | /* When we get here, it means that both the request and the |
| 4001 | * response have finished receiving. Depending on the connection |
| 4002 | * mode, we'll have to wait for the last bytes to leave in either |
| 4003 | * direction, and sometimes for a close to be effective. |
| 4004 | */ |
| 4005 | |
| 4006 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 4007 | /* Server-close mode : shut read and wait for the request |
| 4008 | * side to close its output buffer. The caller will detect |
| 4009 | * when we're in DONE and the other is in CLOSED and will |
| 4010 | * catch that for the final cleanup. |
| 4011 | */ |
| 4012 | if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW))) |
| 4013 | buffer_shutr_now(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4014 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4015 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4016 | /* Option forceclose is set, or either side wants to close, |
| 4017 | * let's enforce it now that we're not expecting any new |
| 4018 | * data to come. The caller knows the session is complete |
| 4019 | * once both states are CLOSED. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4020 | */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4021 | if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) { |
| 4022 | buffer_shutr_now(buf); |
| 4023 | buffer_shutw_now(buf); |
| 4024 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4025 | } |
| 4026 | else { |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4027 | /* The last possible modes are keep-alive and tunnel. Since tunnel |
| 4028 | * mode does not set the body analyser, we can't reach this place |
| 4029 | * in tunnel mode, so we're left with keep-alive only. |
| 4030 | * This mode is currently not implemented, we switch to tunnel mode. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4031 | */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4032 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4033 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4034 | } |
| 4035 | |
| 4036 | if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) { |
| 4037 | /* if we've just closed an output, let's switch */ |
| 4038 | if (!(buf->flags & BF_OUT_EMPTY)) { |
| 4039 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 4040 | goto http_msg_closing; |
| 4041 | } |
| 4042 | else { |
| 4043 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4044 | goto http_msg_closed; |
| 4045 | } |
| 4046 | } |
| 4047 | goto wait_other_side; |
| 4048 | } |
| 4049 | |
| 4050 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 4051 | http_msg_closing: |
| 4052 | /* nothing else to forward, just waiting for the output buffer |
| 4053 | * to be empty and for the shutw_now to take effect. |
| 4054 | */ |
| 4055 | if (buf->flags & BF_OUT_EMPTY) { |
| 4056 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4057 | goto http_msg_closed; |
| 4058 | } |
| 4059 | else if (buf->flags & BF_SHUTW) { |
| 4060 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4061 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4062 | if (target_srv(&s->target)) |
| 4063 | target_srv(&s->target)->counters.cli_aborts++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4064 | goto wait_other_side; |
| 4065 | } |
| 4066 | } |
| 4067 | |
| 4068 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 4069 | http_msg_closed: |
| 4070 | /* drop any pending data */ |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 4071 | bi_erase(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4072 | buffer_auto_close(buf); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4073 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4074 | goto wait_other_side; |
| 4075 | } |
| 4076 | |
| 4077 | wait_other_side: |
| 4078 | http_silent_debug(__LINE__, s); |
| 4079 | return txn->rsp.msg_state != old_state || buf->flags != old_flags; |
| 4080 | } |
| 4081 | |
| 4082 | |
| 4083 | /* Resync the request and response state machines. Return 1 if either state |
| 4084 | * changes. |
| 4085 | */ |
| 4086 | int http_resync_states(struct session *s) |
| 4087 | { |
| 4088 | struct http_txn *txn = &s->txn; |
| 4089 | int old_req_state = txn->req.msg_state; |
| 4090 | int old_res_state = txn->rsp.msg_state; |
| 4091 | |
| 4092 | http_silent_debug(__LINE__, s); |
| 4093 | http_sync_req_state(s); |
| 4094 | while (1) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4095 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4096 | if (!http_sync_res_state(s)) |
| 4097 | break; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4098 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4099 | if (!http_sync_req_state(s)) |
| 4100 | break; |
| 4101 | } |
| 4102 | http_silent_debug(__LINE__, s); |
| 4103 | /* OK, both state machines agree on a compatible state. |
| 4104 | * There are a few cases we're interested in : |
| 4105 | * - HTTP_MSG_TUNNEL on either means we have to disable both analysers |
| 4106 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 4107 | * directions, so let's simply disable both analysers. |
| 4108 | * - HTTP_MSG_CLOSED on the response only means we must abort the |
| 4109 | * request. |
| 4110 | * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response |
| 4111 | * with server-close mode means we've completed one request and we |
| 4112 | * must re-initialize the server connection. |
| 4113 | */ |
| 4114 | |
| 4115 | if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 4116 | txn->rsp.msg_state == HTTP_MSG_TUNNEL || |
| 4117 | (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 4118 | txn->rsp.msg_state == HTTP_MSG_CLOSED)) { |
| 4119 | s->req->analysers = 0; |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4120 | buffer_auto_close(s->req); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4121 | buffer_auto_read(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4122 | s->rep->analysers = 0; |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4123 | buffer_auto_close(s->rep); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4124 | buffer_auto_read(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4125 | } |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4126 | else if (txn->rsp.msg_state == HTTP_MSG_CLOSED || |
| 4127 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4128 | txn->req.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4129 | (s->rep->flags & BF_SHUTW)) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4130 | s->rep->analysers = 0; |
| 4131 | buffer_auto_close(s->rep); |
| 4132 | buffer_auto_read(s->rep); |
| 4133 | s->req->analysers = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4134 | buffer_abort(s->req); |
| 4135 | buffer_auto_close(s->req); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4136 | buffer_auto_read(s->req); |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 4137 | bi_erase(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4138 | } |
| 4139 | else if (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 4140 | txn->rsp.msg_state == HTTP_MSG_DONE && |
| 4141 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) { |
| 4142 | /* server-close: terminate this server connection and |
| 4143 | * reinitialize a fresh-new transaction. |
| 4144 | */ |
| 4145 | http_end_txn_clean_session(s); |
| 4146 | } |
| 4147 | |
| 4148 | http_silent_debug(__LINE__, s); |
| 4149 | return txn->req.msg_state != old_req_state || |
| 4150 | txn->rsp.msg_state != old_res_state; |
| 4151 | } |
| 4152 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4153 | /* This function is an analyser which forwards request body (including chunk |
| 4154 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 4155 | * zero byte. The only situation where it must not be called is when we're in |
| 4156 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 4157 | * remaining data and to resync after end of body. It expects the msg_state to |
| 4158 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 4159 | * 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] | 4160 | * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4161 | * bytes of pending data + the headers if not already done (between som and sov). |
| 4162 | * It eventually adjusts som to match sov after the data in between have been sent. |
| 4163 | */ |
| 4164 | int http_request_forward_body(struct session *s, struct buffer *req, int an_bit) |
| 4165 | { |
| 4166 | struct http_txn *txn = &s->txn; |
| 4167 | struct http_msg *msg = &s->txn.req; |
| 4168 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4169 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4170 | return 0; |
| 4171 | |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 4172 | if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) || |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 4173 | ((req->flags & BF_SHUTW) && (req->to_forward || req->o))) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4174 | /* Output closed while we were sending data. We must abort and |
| 4175 | * wake the other side up. |
| 4176 | */ |
| 4177 | msg->msg_state = HTTP_MSG_ERROR; |
| 4178 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 4179 | return 1; |
| 4180 | } |
| 4181 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4182 | /* in most states, we should abort in case of early close */ |
| 4183 | buffer_auto_close(req); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4184 | |
| 4185 | /* Note that we don't have to send 100-continue back because we don't |
| 4186 | * need the data to complete our job, and it's up to the server to |
| 4187 | * decide whether to return 100, 417 or anything else in return of |
| 4188 | * an "Expect: 100-continue" header. |
| 4189 | */ |
| 4190 | |
| 4191 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 4192 | /* we have msg->sov which points to the first byte of message body. |
| 4193 | * msg->som still points to the beginning of the message. We must |
| 4194 | * save the body in msg->next because it survives buffer re-alignments. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4195 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 4196 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4197 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4198 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4199 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 4200 | else { |
| 4201 | msg->msg_state = HTTP_MSG_DATA; |
| 4202 | } |
| 4203 | } |
| 4204 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4205 | while (1) { |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 4206 | int bytes; |
| 4207 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4208 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 4209 | /* we may have some data pending */ |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 4210 | bytes = msg->sov - msg->som; |
| 4211 | if (msg->chunk_len || bytes) { |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 4212 | msg->som = msg->sov; |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 4213 | if (likely(bytes < 0)) /* sov may have wrapped at the end */ |
| 4214 | bytes += req->size; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4215 | msg->next -= bytes; /* will be forwarded */ |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 4216 | msg->chunk_len += (unsigned int)bytes; |
| 4217 | msg->chunk_len -= buffer_forward(req, msg->chunk_len); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 4218 | } |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 4219 | |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4220 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 4221 | /* must still forward */ |
| 4222 | if (req->to_forward) |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4223 | goto missing_data; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4224 | |
| 4225 | /* nothing left to forward */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4226 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4227 | msg->msg_state = HTTP_MSG_DATA_CRLF; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4228 | else |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4229 | msg->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4230 | } |
| 4231 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4232 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4233 | * 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] | 4234 | * TRAILERS state. |
| 4235 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 4236 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4237 | |
| 4238 | if (!ret) |
| 4239 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4240 | else if (ret < 0) { |
| 4241 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4242 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4243 | 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] | 4244 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4245 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4246 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4247 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4248 | else if (msg->msg_state == HTTP_MSG_DATA_CRLF) { |
| 4249 | /* we want the CRLF after the data */ |
| 4250 | int ret; |
| 4251 | |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 4252 | ret = http_skip_chunk_crlf(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4253 | |
| 4254 | if (ret == 0) |
| 4255 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4256 | else if (ret < 0) { |
| 4257 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4258 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4259 | http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_DATA_CRLF, s->be); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4260 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4261 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4262 | /* we're in MSG_CHUNK_SIZE now */ |
| 4263 | } |
| 4264 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 4265 | int ret = http_forward_trailers(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4266 | |
| 4267 | if (ret == 0) |
| 4268 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4269 | else if (ret < 0) { |
| 4270 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4271 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4272 | 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] | 4273 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4274 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4275 | /* we're in HTTP_MSG_DONE now */ |
| 4276 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4277 | else { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4278 | int old_state = msg->msg_state; |
| 4279 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4280 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4281 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4282 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 4283 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
| 4284 | buffer_dont_close(req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4285 | if (http_resync_states(s)) { |
| 4286 | /* some state changes occurred, maybe the analyser |
| 4287 | * was disabled too. |
Willy Tarreau | face839 | 2010-01-03 11:37:54 +0100 | [diff] [blame] | 4288 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 4289 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 4290 | if (req->flags & BF_SHUTW) { |
| 4291 | /* request errors are most likely due to |
| 4292 | * the server aborting the transfer. |
| 4293 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4294 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 4295 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4296 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4297 | 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] | 4298 | goto return_bad_req; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 4299 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4300 | return 1; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4301 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 4302 | |
| 4303 | /* If "option abortonclose" is set on the backend, we |
| 4304 | * want to monitor the client's connection and forward |
| 4305 | * any shutdown notification to the server, which will |
| 4306 | * decide whether to close or to go on processing the |
| 4307 | * request. |
| 4308 | */ |
| 4309 | if (s->be->options & PR_O_ABRT_CLOSE) { |
| 4310 | buffer_auto_read(req); |
| 4311 | buffer_auto_close(req); |
| 4312 | } |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4313 | else if (s->txn.meth == HTTP_METH_POST) { |
| 4314 | /* POST requests may require to read extra CRLF |
| 4315 | * sent by broken browsers and which could cause |
| 4316 | * an RST to be sent upon close on some systems |
| 4317 | * (eg: Linux). |
| 4318 | */ |
| 4319 | buffer_auto_read(req); |
| 4320 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 4321 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4322 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4323 | } |
| 4324 | } |
| 4325 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4326 | missing_data: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4327 | /* stop waiting for data if the input is closed before the end */ |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4328 | if (req->flags & BF_SHUTR) { |
| 4329 | if (!(s->flags & SN_ERR_MASK)) |
| 4330 | s->flags |= SN_ERR_CLICL; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4331 | if (!(s->flags & SN_FINST_MASK)) { |
| 4332 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 4333 | s->flags |= SN_FINST_H; |
| 4334 | else |
| 4335 | s->flags |= SN_FINST_D; |
| 4336 | } |
| 4337 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4338 | s->fe->fe_counters.cli_aborts++; |
| 4339 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4340 | if (target_srv(&s->target)) |
| 4341 | target_srv(&s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4342 | |
| 4343 | goto return_bad_req_stats_ok; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4344 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4345 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4346 | /* waiting for the last bits to leave the buffer */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4347 | if (req->flags & BF_SHUTW) |
| 4348 | goto aborted_xfer; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4349 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4350 | /* When TE: chunked is used, we need to get there again to parse remaining |
| 4351 | * chunks even if the client has closed, so we don't want to set BF_DONTCLOSE. |
| 4352 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4353 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4354 | buffer_dont_close(req); |
| 4355 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4356 | /* We know that more data are expected, but we couldn't send more that |
Willy Tarreau | 0729303 | 2011-05-30 18:29:28 +0200 | [diff] [blame] | 4357 | * what we did. So we always set the BF_EXPECT_MORE flag so that the |
| 4358 | * 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] | 4359 | * modes are already handled by the stream sock layer. We must not do |
| 4360 | * this in content-length mode because it could present the MSG_MORE |
| 4361 | * flag with the last block of forwarded data, which would cause an |
| 4362 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4363 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4364 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 869fc1e | 2012-03-05 08:29:20 +0100 | [diff] [blame] | 4365 | req->flags |= BF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4366 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4367 | http_silent_debug(__LINE__, s); |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4368 | return 0; |
| 4369 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4370 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4371 | s->fe->fe_counters.failed_req++; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4372 | if (s->listener->counters) |
| 4373 | s->listener->counters->failed_req++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4374 | return_bad_req_stats_ok: |
| 4375 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4376 | if (txn->status) { |
| 4377 | /* Note: we don't send any error if some data were already sent */ |
| 4378 | stream_int_retnclose(req->prod, NULL); |
| 4379 | } else { |
| 4380 | txn->status = 400; |
| 4381 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
| 4382 | } |
| 4383 | req->analysers = 0; |
| 4384 | 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] | 4385 | |
| 4386 | if (!(s->flags & SN_ERR_MASK)) |
| 4387 | s->flags |= SN_ERR_PRXCOND; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4388 | if (!(s->flags & SN_FINST_MASK)) { |
| 4389 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 4390 | s->flags |= SN_FINST_H; |
| 4391 | else |
| 4392 | s->flags |= SN_FINST_D; |
| 4393 | } |
| 4394 | return 0; |
| 4395 | |
| 4396 | aborted_xfer: |
| 4397 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4398 | if (txn->status) { |
| 4399 | /* Note: we don't send any error if some data were already sent */ |
| 4400 | stream_int_retnclose(req->prod, NULL); |
| 4401 | } else { |
| 4402 | txn->status = 502; |
| 4403 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_502)); |
| 4404 | } |
| 4405 | req->analysers = 0; |
| 4406 | s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */ |
| 4407 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4408 | s->fe->fe_counters.srv_aborts++; |
| 4409 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4410 | if (target_srv(&s->target)) |
| 4411 | target_srv(&s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4412 | |
| 4413 | if (!(s->flags & SN_ERR_MASK)) |
| 4414 | s->flags |= SN_ERR_SRVCL; |
| 4415 | if (!(s->flags & SN_FINST_MASK)) { |
| 4416 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 4417 | s->flags |= SN_FINST_H; |
| 4418 | else |
| 4419 | s->flags |= SN_FINST_D; |
| 4420 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4421 | return 0; |
| 4422 | } |
| 4423 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4424 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 4425 | * processing can continue on next analysers, or zero if it either needs more |
| 4426 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
| 4427 | * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers |
| 4428 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 4429 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4430 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4431 | int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit) |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4432 | { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4433 | struct http_txn *txn = &s->txn; |
| 4434 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4435 | struct hdr_ctx ctx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4436 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4437 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4438 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4439 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 4440 | 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] | 4441 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4442 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 4443 | rep, |
| 4444 | rep->rex, rep->wex, |
| 4445 | rep->flags, |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 4446 | rep->i, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 4447 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 4448 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4449 | /* |
| 4450 | * Now parse the partial (or complete) lines. |
| 4451 | * We will check the response syntax, and also join multi-line |
| 4452 | * headers. An index of all the lines will be elaborated while |
| 4453 | * parsing. |
| 4454 | * |
| 4455 | * For the parsing, we use a 28 states FSM. |
| 4456 | * |
| 4457 | * Here is the information we currently have : |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4458 | * rep->data + msg->som = beginning of response |
| 4459 | * rep->data + msg->eoh = end of processed headers / start of current one |
| 4460 | * msg->eol = end of current header or line (LF or CRLF) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4461 | * msg->next = first non-visited byte |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4462 | * rep->r = end of data |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4463 | * Once we reach MSG_BODY, rep->sol = rep->data + msg->som |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4464 | */ |
| 4465 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4466 | /* There's a protected area at the end of the buffer for rewriting |
| 4467 | * purposes. We don't want to start to parse the request if the |
| 4468 | * protected area is affected, because we may have to move processed |
| 4469 | * data later, which is much more complicated. |
| 4470 | */ |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 4471 | if (buffer_not_empty(rep) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 4472 | if (unlikely((rep->flags & BF_FULL) || |
Willy Tarreau | cc5cfcb | 2012-05-04 21:35:27 +0200 | [diff] [blame] | 4473 | bi_end(rep) < b_ptr(rep, msg->next) || |
| 4474 | bi_end(rep) > rep->data + rep->size - global.tune.maxrewrite)) { |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 4475 | if (rep->o) { |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 4476 | /* some data has still not left the buffer, wake us once that's done */ |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 4477 | if (rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) |
| 4478 | goto abort_response; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 4479 | buffer_dont_close(rep); |
| 4480 | rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 4481 | return 0; |
| 4482 | } |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 4483 | if (rep->i <= rep->size - global.tune.maxrewrite) |
Willy Tarreau | 21710ff | 2012-03-09 13:58:04 +0100 | [diff] [blame] | 4484 | http_message_realign(msg); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4485 | } |
| 4486 | |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4487 | if (likely(msg->next < rep->i)) |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 4488 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4489 | } |
| 4490 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4491 | /* 1: we might have to print this header in debug mode */ |
| 4492 | if (unlikely((global.mode & MODE_DEBUG) && |
| 4493 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | c3bfeeb | 2010-04-16 09:14:45 +0200 | [diff] [blame] | 4494 | msg->sol && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4495 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4496 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4497 | |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 4498 | sol = rep->p + msg->som; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 4499 | eol = sol + msg->sl.st.l; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4500 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4501 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4502 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 4503 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4504 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4505 | while (cur_idx) { |
| 4506 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 4507 | debug_hdr("srvhdr", s, sol, eol); |
| 4508 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 4509 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 4510 | } |
| 4511 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4512 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4513 | /* |
| 4514 | * Now we quickly check if we have found a full valid response. |
| 4515 | * If not so, we check the FD and buffer states before leaving. |
| 4516 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4517 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4518 | * responses are checked first. |
| 4519 | * |
| 4520 | * Depending on whether the client is still there or not, we |
| 4521 | * may send an error response back or not. Note that normally |
| 4522 | * we should only check for HTTP status there, and check I/O |
| 4523 | * errors somewhere else. |
| 4524 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4525 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4526 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4527 | /* Invalid response */ |
| 4528 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 4529 | /* we detected a parsing error. We want to archive this response |
| 4530 | * in the dedicated proxy area for later troubleshooting. |
| 4531 | */ |
| 4532 | hdr_response_bad: |
| 4533 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4534 | 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] | 4535 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4536 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4537 | if (target_srv(&s->target)) { |
| 4538 | target_srv(&s->target)->counters.failed_resp++; |
| 4539 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4540 | } |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 4541 | abort_response: |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4542 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4543 | rep->analysers = 0; |
| 4544 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4545 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 4546 | bi_erase(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4547 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
| 4548 | |
| 4549 | if (!(s->flags & SN_ERR_MASK)) |
| 4550 | s->flags |= SN_ERR_PRXCOND; |
| 4551 | if (!(s->flags & SN_FINST_MASK)) |
| 4552 | s->flags |= SN_FINST_H; |
| 4553 | |
| 4554 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4555 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4556 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4557 | /* too large response does not fit in buffer. */ |
| 4558 | else if (rep->flags & BF_FULL) { |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 4559 | if (msg->err_pos < 0) |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 4560 | msg->err_pos = rep->i; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4561 | goto hdr_response_bad; |
| 4562 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4563 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4564 | /* read error */ |
| 4565 | else if (rep->flags & BF_READ_ERROR) { |
| 4566 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4567 | 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] | 4568 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4569 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4570 | if (target_srv(&s->target)) { |
| 4571 | target_srv(&s->target)->counters.failed_resp++; |
| 4572 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_ERROR); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4573 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 4574 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4575 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4576 | rep->analysers = 0; |
| 4577 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4578 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 4579 | bi_erase(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4580 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 4581 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4582 | if (!(s->flags & SN_ERR_MASK)) |
| 4583 | s->flags |= SN_ERR_SRVCL; |
| 4584 | if (!(s->flags & SN_FINST_MASK)) |
| 4585 | s->flags |= SN_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 4586 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4587 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4588 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4589 | /* read timeout : return a 504 to the client. */ |
| 4590 | else if (rep->flags & BF_READ_TIMEOUT) { |
| 4591 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4592 | 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] | 4593 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4594 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4595 | if (target_srv(&s->target)) { |
| 4596 | target_srv(&s->target)->counters.failed_resp++; |
| 4597 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4598 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4599 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4600 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4601 | rep->analysers = 0; |
| 4602 | txn->status = 504; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4603 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 4604 | bi_erase(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4605 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504)); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4606 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4607 | if (!(s->flags & SN_ERR_MASK)) |
| 4608 | s->flags |= SN_ERR_SRVTO; |
| 4609 | if (!(s->flags & SN_FINST_MASK)) |
| 4610 | s->flags |= SN_FINST_H; |
| 4611 | return 0; |
| 4612 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 4613 | |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 4614 | /* close from server, capture the response if the server has started to respond */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4615 | else if (rep->flags & BF_SHUTR) { |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 4616 | if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4617 | 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] | 4618 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4619 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4620 | if (target_srv(&s->target)) { |
| 4621 | target_srv(&s->target)->counters.failed_resp++; |
| 4622 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4623 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4624 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4625 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4626 | rep->analysers = 0; |
| 4627 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4628 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 4629 | bi_erase(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4630 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4631 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4632 | if (!(s->flags & SN_ERR_MASK)) |
| 4633 | s->flags |= SN_ERR_SRVCL; |
| 4634 | if (!(s->flags & SN_FINST_MASK)) |
| 4635 | s->flags |= SN_FINST_H; |
| 4636 | return 0; |
| 4637 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4638 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4639 | /* write error to client (we don't send any message then) */ |
| 4640 | else if (rep->flags & BF_WRITE_ERROR) { |
| 4641 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4642 | 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] | 4643 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4644 | s->be->be_counters.failed_resp++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4645 | rep->analysers = 0; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4646 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4647 | |
| 4648 | if (!(s->flags & SN_ERR_MASK)) |
| 4649 | s->flags |= SN_ERR_CLICL; |
| 4650 | if (!(s->flags & SN_FINST_MASK)) |
| 4651 | s->flags |= SN_FINST_H; |
| 4652 | |
| 4653 | /* process_session() will take care of the error */ |
| 4654 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4655 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4656 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4657 | buffer_dont_close(rep); |
| 4658 | return 0; |
| 4659 | } |
| 4660 | |
| 4661 | /* More interesting part now : we know that we have a complete |
| 4662 | * response which at least looks like HTTP. We have an indicator |
| 4663 | * of each header's length, so we can parse them quickly. |
| 4664 | */ |
| 4665 | |
| 4666 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4667 | 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] | 4668 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4669 | /* |
| 4670 | * 1: get the status code |
| 4671 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 4672 | n = rep->p[msg->sol + msg->sl.st.c] - '0'; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4673 | if (n < 1 || n > 5) |
| 4674 | n = 0; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4675 | /* when the client triggers a 4xx from the server, it's most often due |
| 4676 | * to a missing object or permission. These events should be tracked |
| 4677 | * because if they happen often, it may indicate a brute force or a |
| 4678 | * vulnerability scan. |
| 4679 | */ |
| 4680 | if (n == 4) |
| 4681 | session_inc_http_err_ctr(s); |
| 4682 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4683 | if (target_srv(&s->target)) |
| 4684 | target_srv(&s->target)->counters.p.http.rsp[n]++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4685 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4686 | /* check if the response is HTTP/1.1 or above */ |
| 4687 | if ((msg->sl.st.v_l == 8) && |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 4688 | ((rep->p[msg->sol + 5] > '1') || |
| 4689 | ((rep->p[msg->sol + 5] == '1') && |
| 4690 | (rep->p[msg->sol + 7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4691 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4692 | |
| 4693 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4694 | txn->flags &= ~(TX_HDR_CONN_PRS|TX_HDR_CONN_CLO|TX_HDR_CONN_KAL|TX_CON_CLO_SET|TX_CON_KAL_SET); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4695 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4696 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4697 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4698 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 4699 | txn->status = strl2ui(rep->p + msg->sol + msg->sl.st.c, msg->sl.st.c_l); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4700 | |
Willy Tarreau | 3965040 | 2010-03-15 19:44:39 +0100 | [diff] [blame] | 4701 | /* Adjust server's health based on status code. Note: status codes 501 |
| 4702 | * and 505 are triggered on demand by client request, so we must not |
| 4703 | * count them as server failures. |
| 4704 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4705 | if (target_srv(&s->target)) { |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 4706 | if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505)) |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4707 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_OK); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 4708 | else |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4709 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_STS); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 4710 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4711 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4712 | /* |
| 4713 | * 2: check for cacheability. |
| 4714 | */ |
| 4715 | |
| 4716 | switch (txn->status) { |
| 4717 | case 200: |
| 4718 | case 203: |
| 4719 | case 206: |
| 4720 | case 300: |
| 4721 | case 301: |
| 4722 | case 410: |
| 4723 | /* RFC2616 @13.4: |
| 4724 | * "A response received with a status code of |
| 4725 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 4726 | * by a cache (...) unless a cache-control |
| 4727 | * directive prohibits caching." |
| 4728 | * |
| 4729 | * RFC2616 @9.5: POST method : |
| 4730 | * "Responses to this method are not cacheable, |
| 4731 | * unless the response includes appropriate |
| 4732 | * Cache-Control or Expires header fields." |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4733 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4734 | if (likely(txn->meth != HTTP_METH_POST) && |
| 4735 | (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC))) |
| 4736 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 4737 | break; |
| 4738 | default: |
| 4739 | break; |
| 4740 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4741 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4742 | /* |
| 4743 | * 3: we may need to capture headers |
| 4744 | */ |
| 4745 | s->logs.logwait &= ~LW_RESP; |
Willy Tarreau | 42f7d89 | 2012-03-24 08:28:09 +0100 | [diff] [blame] | 4746 | if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap)) |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 4747 | capture_headers(rep->p + msg->sol, &txn->hdr_idx, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4748 | txn->rsp.cap, s->fe->rsp_cap); |
| 4749 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4750 | /* 4: determine the transfer-length. |
| 4751 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 4752 | * the presence of a message-body in a RESPONSE and its transfer length |
| 4753 | * must be determined that way : |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4754 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4755 | * All responses to the HEAD request method MUST NOT include a |
| 4756 | * message-body, even though the presence of entity-header fields |
| 4757 | * might lead one to believe they do. All 1xx (informational), 204 |
| 4758 | * (No Content), and 304 (Not Modified) responses MUST NOT include a |
| 4759 | * message-body. All other responses do include a message-body, |
| 4760 | * although it MAY be of zero length. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4761 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4762 | * 1. Any response which "MUST NOT" include a message-body (such as the |
| 4763 | * 1xx, 204 and 304 responses and any response to a HEAD request) is |
| 4764 | * always terminated by the first empty line after the header fields, |
| 4765 | * regardless of the entity-header fields present in the message. |
| 4766 | * |
| 4767 | * 2. If a Transfer-Encoding header field (Section 9.7) is present and |
| 4768 | * the "chunked" transfer-coding (Section 6.2) is used, the |
| 4769 | * transfer-length is defined by the use of this transfer-coding. |
| 4770 | * If a Transfer-Encoding header field is present and the "chunked" |
| 4771 | * transfer-coding is not present, the transfer-length is defined by |
| 4772 | * the sender closing the connection. |
| 4773 | * |
| 4774 | * 3. If a Content-Length header field is present, its decimal value in |
| 4775 | * OCTETs represents both the entity-length and the transfer-length. |
| 4776 | * If a message is received with both a Transfer-Encoding header |
| 4777 | * field and a Content-Length header field, the latter MUST be ignored. |
| 4778 | * |
| 4779 | * 4. If the message uses the media type "multipart/byteranges", and |
| 4780 | * the transfer-length is not otherwise specified, then this self- |
| 4781 | * delimiting media type defines the transfer-length. This media |
| 4782 | * type MUST NOT be used unless the sender knows that the recipient |
| 4783 | * can parse it; the presence in a request of a Range header with |
| 4784 | * multiple byte-range specifiers from a 1.1 client implies that the |
| 4785 | * client can parse multipart/byteranges responses. |
| 4786 | * |
| 4787 | * 5. By the server closing the connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4788 | */ |
| 4789 | |
| 4790 | /* Skip parsing if no content length is possible. The response flags |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4791 | * 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] | 4792 | * 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] | 4793 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 4794 | */ |
| 4795 | if (txn->meth == HTTP_METH_HEAD || |
| 4796 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4797 | txn->status == 204 || txn->status == 304) { |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4798 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4799 | goto skip_content_length; |
| 4800 | } |
| 4801 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4802 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4803 | ctx.idx = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4804 | while ((msg->flags & HTTP_MSGF_VER_11) && |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 4805 | http_find_header2("Transfer-Encoding", 17, rep->p + msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4806 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4807 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 4808 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4809 | /* bad transfer-encoding (chunked followed by something else) */ |
| 4810 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4811 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4812 | break; |
| 4813 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4814 | } |
| 4815 | |
| 4816 | /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */ |
| 4817 | ctx.idx = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4818 | while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only && |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 4819 | http_find_header2("Content-Length", 14, rep->p + msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4820 | signed long long cl; |
| 4821 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4822 | if (!ctx.vlen) { |
| 4823 | msg->err_pos = ctx.line + ctx.val - rep->data; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4824 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4825 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4826 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4827 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
| 4828 | msg->err_pos = ctx.line + ctx.val - rep->data; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4829 | goto hdr_response_bad; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4830 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4831 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4832 | if (cl < 0) { |
| 4833 | msg->err_pos = ctx.line + ctx.val - rep->data; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4834 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4835 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4836 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4837 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4838 | msg->err_pos = ctx.line + ctx.val - rep->data; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4839 | goto hdr_response_bad; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4840 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4841 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4842 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4843 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4844 | } |
| 4845 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4846 | /* FIXME: we should also implement the multipart/byterange method. |
| 4847 | * For now on, we resort to close mode in this case (unknown length). |
| 4848 | */ |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4849 | skip_content_length: |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4850 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4851 | /* end of job, return OK */ |
| 4852 | rep->analysers &= ~an_bit; |
| 4853 | rep->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4854 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4855 | return 1; |
| 4856 | } |
| 4857 | |
| 4858 | /* This function performs all the processing enabled for the current response. |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 4859 | * It normally returns 1 unless it wants to break. It relies on buffers flags, |
| 4860 | * and updates t->rep->analysers. It might make sense to explode it into several |
| 4861 | * other functions. It works like process_request (see indications above). |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4862 | */ |
| 4863 | int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px) |
| 4864 | { |
| 4865 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4866 | struct http_msg *msg = &txn->rsp; |
| 4867 | struct proxy *cur_proxy; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 4868 | struct cond_wordlist *wl; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4869 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 4870 | 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] | 4871 | now_ms, __FUNCTION__, |
| 4872 | t, |
| 4873 | rep, |
| 4874 | rep->rex, rep->wex, |
| 4875 | rep->flags, |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 4876 | rep->i, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4877 | rep->analysers); |
| 4878 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4879 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4880 | return 0; |
| 4881 | |
| 4882 | rep->analysers &= ~an_bit; |
| 4883 | rep->analyse_exp = TICK_ETERNITY; |
| 4884 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4885 | /* Now we have to check if we need to modify the Connection header. |
| 4886 | * This is more difficult on the response than it is on the request, |
| 4887 | * because we can have two different HTTP versions and we don't know |
| 4888 | * how the client will interprete a response. For instance, let's say |
| 4889 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 4890 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 4891 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 4892 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 4893 | * the lack of header as a keep-alive. Thus we will use two flags |
| 4894 | * indicating how a request MAY be understood by the client. In case |
| 4895 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 4896 | * ambiguous cases such as both close and keepalive are seen, then we |
| 4897 | * will fall back to explicit close. Note that we won't take risks with |
| 4898 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4899 | * See doc/internals/connection-header.txt for the complete matrix. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4900 | */ |
| 4901 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 4902 | if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || |
| 4903 | txn->status == 101)) { |
| 4904 | /* Either we've established an explicit tunnel, or we're |
| 4905 | * switching the protocol. In both cases, we're very unlikely |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 4906 | * to understand the next protocols. We have to switch to tunnel |
| 4907 | * mode, so that we transfer the request and responses then let |
| 4908 | * this protocol pass unmodified. When we later implement specific |
| 4909 | * parsers for such protocols, we'll want to check the Upgrade |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 4910 | * header which contains information about that protocol for |
| 4911 | * responses with status 101 (eg: see RFC2817 about TLS). |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 4912 | */ |
| 4913 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN; |
| 4914 | } |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 4915 | else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) && |
| 4916 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN || |
| 4917 | ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4918 | int to_del = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4919 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4920 | /* on unknown transfer length, we must close */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4921 | if (!(msg->flags & HTTP_MSGF_XFER_LEN) && |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4922 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 4923 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4924 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4925 | /* now adjust header transformations depending on current state */ |
| 4926 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN || |
| 4927 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4928 | to_del |= 2; /* remove "keep-alive" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4929 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4930 | to_del |= 1; /* remove "close" for HTTP/1.0 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4931 | } |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4932 | else { /* SCL / KAL */ |
| 4933 | to_del |= 1; /* remove "close" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4934 | if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4935 | to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4936 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4937 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4938 | /* Parse and remove some headers from the connection header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 4939 | http_parse_connection_header(txn, msg, to_del); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4940 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4941 | /* Some keep-alive responses are converted to Server-close if |
| 4942 | * the server wants to close. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4943 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4944 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) { |
| 4945 | if ((txn->flags & TX_HDR_CONN_CLO) || |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4946 | (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11))) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4947 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4948 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4949 | } |
| 4950 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4951 | if (1) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4952 | /* |
| 4953 | * 3: we will have to evaluate the filters. |
| 4954 | * As opposed to version 1.2, now they will be evaluated in the |
| 4955 | * filters order and not in the header order. This means that |
| 4956 | * each filter has to be validated among all headers. |
| 4957 | * |
| 4958 | * Filters are tried with ->be first, then with ->fe if it is |
| 4959 | * different from ->be. |
| 4960 | */ |
| 4961 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4962 | cur_proxy = t->be; |
| 4963 | while (1) { |
| 4964 | struct proxy *rule_set = cur_proxy; |
| 4965 | |
| 4966 | /* try headers filters */ |
| 4967 | if (rule_set->rsp_exp != NULL) { |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 4968 | if (apply_filters_to_response(t, rep, rule_set) < 0) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4969 | return_bad_resp: |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4970 | if (target_srv(&t->target)) { |
| 4971 | target_srv(&t->target)->counters.failed_resp++; |
| 4972 | health_adjust(target_srv(&t->target), HANA_STATUS_HTTP_RSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4973 | } |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4974 | t->be->be_counters.failed_resp++; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4975 | return_srv_prx_502: |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 4976 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4977 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4978 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 4979 | bi_erase(rep); |
Willy Tarreau | 8e89b84 | 2009-10-18 23:56:35 +0200 | [diff] [blame] | 4980 | stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502)); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4981 | if (!(t->flags & SN_ERR_MASK)) |
| 4982 | t->flags |= SN_ERR_PRXCOND; |
| 4983 | if (!(t->flags & SN_FINST_MASK)) |
| 4984 | t->flags |= SN_FINST_H; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 4985 | return 0; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4986 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4987 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4988 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4989 | /* has the response been denied ? */ |
| 4990 | if (txn->flags & TX_SVDENY) { |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4991 | if (target_srv(&t->target)) |
| 4992 | target_srv(&t->target)->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4993 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4994 | t->be->be_counters.denied_resp++; |
| 4995 | t->fe->fe_counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4996 | if (t->listener->counters) |
| 4997 | t->listener->counters->denied_resp++; |
| 4998 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4999 | goto return_srv_prx_502; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 5000 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5001 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5002 | /* add response headers from the rule sets in the same order */ |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 5003 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 5004 | if (txn->status < 200) |
| 5005 | break; |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 5006 | if (wl->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 5007 | 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] | 5008 | ret = acl_pass(ret); |
| 5009 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 5010 | ret = !ret; |
| 5011 | if (!ret) |
| 5012 | continue; |
| 5013 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 5014 | 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] | 5015 | goto return_bad_resp; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 5016 | } |
| 5017 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5018 | /* check whether we're already working on the frontend */ |
| 5019 | if (cur_proxy == t->fe) |
| 5020 | break; |
| 5021 | cur_proxy = t->fe; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5022 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5023 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5024 | /* |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5025 | * We may be facing a 100-continue response, in which case this |
| 5026 | * is not the right response, and we're waiting for the next one. |
| 5027 | * 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] | 5028 | * next one. |
| 5029 | */ |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5030 | if (unlikely(txn->status == 100)) { |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5031 | hdr_idx_init(&txn->hdr_idx); |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 5032 | msg->next -= buffer_forward(rep, msg->next - msg->sol); |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5033 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 5034 | txn->status = 0; |
| 5035 | rep->analysers |= AN_RES_WAIT_HTTP | an_bit; |
| 5036 | return 1; |
| 5037 | } |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5038 | else if (unlikely(txn->status < 200)) |
| 5039 | goto skip_header_mangling; |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5040 | |
| 5041 | /* we don't have any 1xx status code now */ |
| 5042 | |
| 5043 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5044 | * 4: check for server cookie. |
| 5045 | */ |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5046 | if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name || |
| 5047 | (t->be->options & PR_O_CHK_CACHE)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5048 | manage_server_side_cookies(t, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5049 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5050 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5051 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5052 | * 5: check for cache-control or pragma headers if required. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5053 | */ |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5054 | if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5055 | check_response_for_cacheability(t, rep); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5056 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5057 | /* |
| 5058 | * 6: add server cookie in the response if needed |
| 5059 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5060 | if (target_srv(&t->target) && (t->be->options & PR_O_COOK_INS) && |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 5061 | !((txn->flags & TX_SCK_FOUND) && (t->be->options2 & PR_O2_COOK_PSV)) && |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5062 | (!(t->flags & SN_DIRECT) || |
| 5063 | ((t->be->cookie_maxidle || txn->cookie_last_date) && |
| 5064 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 5065 | (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 5066 | (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 5067 | (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST)) && |
| 5068 | !(t->flags & SN_IGNORE_PRST)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5069 | int len; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5070 | /* the server is known, it's not the one the client requested, or the |
| 5071 | * cookie's last seen date needs to be refreshed. We have to |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5072 | * insert a set-cookie here, except if we want to insert only on POST |
| 5073 | * requests and this one isn't. Note that servers which don't have cookies |
| 5074 | * (eg: some backup servers) will return a full cookie removal request. |
| 5075 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5076 | if (!target_srv(&t->target)->cookie) { |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5077 | len = sprintf(trash, |
| 5078 | "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
| 5079 | t->be->cookie_name); |
| 5080 | } |
| 5081 | else { |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5082 | len = sprintf(trash, "Set-Cookie: %s=%s", t->be->cookie_name, target_srv(&t->target)->cookie); |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5083 | |
| 5084 | if (t->be->cookie_maxidle || t->be->cookie_maxlife) { |
| 5085 | /* emit last_date, which is mandatory */ |
| 5086 | trash[len++] = COOKIE_DELIM_DATE; |
| 5087 | s30tob64((date.tv_sec+3) >> 2, trash + len); len += 5; |
| 5088 | if (t->be->cookie_maxlife) { |
| 5089 | /* emit first_date, which is either the original one or |
| 5090 | * the current date. |
| 5091 | */ |
| 5092 | trash[len++] = COOKIE_DELIM_DATE; |
| 5093 | s30tob64(txn->cookie_first_date ? |
| 5094 | txn->cookie_first_date >> 2 : |
| 5095 | (date.tv_sec+3) >> 2, trash + len); |
| 5096 | len += 5; |
| 5097 | } |
| 5098 | } |
| 5099 | len += sprintf(trash + len, "; path=/"); |
| 5100 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5101 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5102 | if (t->be->cookie_domain) |
| 5103 | len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5104 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 5105 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash, len) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5106 | goto return_bad_resp; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5107 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 5108 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5109 | if (target_srv(&t->target)->cookie && (t->flags & SN_DIRECT)) |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5110 | /* the server did not change, only the date was updated */ |
| 5111 | txn->flags |= TX_SCK_UPDATED; |
| 5112 | else |
| 5113 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5114 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5115 | /* Here, we will tell an eventual cache on the client side that we don't |
| 5116 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 5117 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 5118 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 5119 | */ |
| 5120 | if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5121 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5122 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 5123 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 5124 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 5125 | "Cache-control: private", 22) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5126 | goto return_bad_resp; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5127 | } |
| 5128 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5129 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5130 | /* |
| 5131 | * 7: check if result will be cacheable with a cookie. |
| 5132 | * We'll block the response if security checks have caught |
| 5133 | * nasty things such as a cacheable cookie. |
| 5134 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 5135 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 5136 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5137 | (t->be->options & PR_O_CHK_CACHE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5138 | |
| 5139 | /* we're in presence of a cacheable response containing |
| 5140 | * a set-cookie header. We'll block it as requested by |
| 5141 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5142 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5143 | if (target_srv(&t->target)) |
| 5144 | target_srv(&t->target)->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5145 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5146 | t->be->be_counters.denied_resp++; |
| 5147 | t->fe->fe_counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5148 | if (t->listener->counters) |
| 5149 | t->listener->counters->denied_resp++; |
| 5150 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5151 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5152 | t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>"); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5153 | send_log(t->be, LOG_ALERT, |
| 5154 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5155 | t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>"); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5156 | goto return_srv_prx_502; |
| 5157 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5158 | |
| 5159 | /* |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5160 | * 8: adjust "Connection: close" or "Connection: keep-alive" if needed. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5161 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5162 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
| 5163 | ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) { |
| 5164 | unsigned int want_flags = 0; |
| 5165 | |
| 5166 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5167 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5168 | /* we want a keep-alive response here. Keep-alive header |
| 5169 | * required if either side is not 1.1. |
| 5170 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5171 | if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5172 | want_flags |= TX_CON_KAL_SET; |
| 5173 | } |
| 5174 | else { |
| 5175 | /* we want a close response here. Close header required if |
| 5176 | * the server is 1.1, regardless of the client. |
| 5177 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5178 | if (msg->flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5179 | want_flags |= TX_CON_CLO_SET; |
| 5180 | } |
| 5181 | |
| 5182 | 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] | 5183 | http_change_connection_header(txn, msg, want_flags); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5184 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5185 | |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5186 | skip_header_mangling: |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5187 | if ((msg->flags & HTTP_MSGF_XFER_LEN) || |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5188 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5189 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 5190 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5191 | /************************************************************* |
| 5192 | * OK, that's finished for the headers. We have done what we * |
| 5193 | * could. Let's switch to the DATA state. * |
| 5194 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5195 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5196 | t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5197 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5198 | /* if the user wants to log as soon as possible, without counting |
| 5199 | * bytes from the server, then this is the right moment. We have |
| 5200 | * to temporarily assign bytes_out to log what we currently have. |
| 5201 | */ |
| 5202 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 5203 | t->logs.t_close = t->logs.t_data; /* to get a valid end date */ |
| 5204 | t->logs.bytes_out = txn->rsp.eoh; |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 5205 | t->do_log(t); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5206 | t->logs.bytes_out = 0; |
| 5207 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5208 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5209 | /* Note: we must not try to cheat by jumping directly to DATA, |
| 5210 | * otherwise we would not let the client side wake up. |
| 5211 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5212 | |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 5213 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5214 | } |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 5215 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5216 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5217 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5218 | /* This function is an analyser which forwards response body (including chunk |
| 5219 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 5220 | * zero byte. The only situation where it must not be called is when we're in |
| 5221 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 5222 | * remaining data and to resync after end of body. It expects the msg_state to |
| 5223 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 5224 | * 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] | 5225 | * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5226 | * bytes of pending data + the headers if not already done (between som and sov). |
| 5227 | * It eventually adjusts som to match sov after the data in between have been sent. |
| 5228 | */ |
| 5229 | int http_response_forward_body(struct session *s, struct buffer *res, int an_bit) |
| 5230 | { |
| 5231 | struct http_txn *txn = &s->txn; |
| 5232 | struct http_msg *msg = &s->txn.rsp; |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5233 | int bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5234 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5235 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 5236 | return 0; |
| 5237 | |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 5238 | if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) || |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 5239 | ((res->flags & BF_SHUTW) && (res->to_forward || res->o)) || |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 5240 | !s->req->analysers) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5241 | /* Output closed while we were sending data. We must abort and |
| 5242 | * wake the other side up. |
| 5243 | */ |
| 5244 | msg->msg_state = HTTP_MSG_ERROR; |
| 5245 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 5246 | return 1; |
| 5247 | } |
| 5248 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5249 | /* in most states, we should abort in case of early close */ |
| 5250 | buffer_auto_close(res); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5251 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5252 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 5253 | /* we have msg->sov which points to the first byte of message body. |
| 5254 | * msg->som still points to the beginning of the message. We must |
| 5255 | * save the body in msg->next because it survives buffer re-alignments. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5256 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 5257 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 5258 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5259 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5260 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 5261 | else { |
| 5262 | msg->msg_state = HTTP_MSG_DATA; |
| 5263 | } |
| 5264 | } |
| 5265 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5266 | while (1) { |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5267 | int bytes; |
| 5268 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5269 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 5270 | /* we may have some data pending */ |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5271 | bytes = msg->sov - msg->som; |
| 5272 | if (msg->chunk_len || bytes) { |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 5273 | msg->som = msg->sov; |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5274 | if (likely(bytes < 0)) /* sov may have wrapped at the end */ |
| 5275 | bytes += res->size; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 5276 | msg->next -= bytes; /* will be forwarded */ |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5277 | msg->chunk_len += (unsigned int)bytes; |
| 5278 | msg->chunk_len -= buffer_forward(res, msg->chunk_len); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 5279 | } |
| 5280 | |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5281 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 5282 | /* must still forward */ |
| 5283 | if (res->to_forward) |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5284 | goto missing_data; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5285 | |
| 5286 | /* nothing left to forward */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5287 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5288 | msg->msg_state = HTTP_MSG_DATA_CRLF; |
| 5289 | else |
| 5290 | msg->msg_state = HTTP_MSG_DONE; |
| 5291 | } |
| 5292 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5293 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 5294 | * set ->sov and ->next to point to the body and switch to DATA or |
| 5295 | * TRAILERS state. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5296 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 5297 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5298 | |
| 5299 | if (!ret) |
| 5300 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5301 | else if (ret < 0) { |
| 5302 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5303 | 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] | 5304 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5305 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5306 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5307 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5308 | else if (msg->msg_state == HTTP_MSG_DATA_CRLF) { |
| 5309 | /* we want the CRLF after the data */ |
| 5310 | int ret; |
| 5311 | |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 5312 | ret = http_skip_chunk_crlf(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5313 | |
| 5314 | if (!ret) |
| 5315 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5316 | else if (ret < 0) { |
| 5317 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5318 | http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_DATA_CRLF, s->fe); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5319 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5320 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5321 | /* we're in MSG_CHUNK_SIZE now */ |
| 5322 | } |
| 5323 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 5324 | int ret = http_forward_trailers(msg); |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5325 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5326 | if (ret == 0) |
| 5327 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5328 | else if (ret < 0) { |
| 5329 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5330 | 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] | 5331 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5332 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5333 | /* we're in HTTP_MSG_DONE now */ |
| 5334 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5335 | else { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5336 | int old_state = msg->msg_state; |
| 5337 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5338 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5339 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5340 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5341 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
| 5342 | buffer_dont_close(res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5343 | if (http_resync_states(s)) { |
| 5344 | http_silent_debug(__LINE__, s); |
| 5345 | /* some state changes occurred, maybe the analyser |
| 5346 | * was disabled too. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5347 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5348 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 5349 | if (res->flags & BF_SHUTW) { |
| 5350 | /* response errors are most likely due to |
| 5351 | * the client aborting the transfer. |
| 5352 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5353 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5354 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5355 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5356 | http_capture_bad_message(&s->be->invalid_rep, s, msg, old_state, s->fe); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5357 | goto return_bad_res; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5358 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5359 | return 1; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5360 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5361 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5362 | } |
| 5363 | } |
| 5364 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5365 | missing_data: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5366 | /* stop waiting for data if the input is closed before the end */ |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5367 | if (res->flags & BF_SHUTR) { |
| 5368 | if (!(s->flags & SN_ERR_MASK)) |
| 5369 | s->flags |= SN_ERR_SRVCL; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5370 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5371 | if (target_srv(&s->target)) |
| 5372 | target_srv(&s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5373 | goto return_bad_res_stats_ok; |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5374 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5375 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5376 | if (res->flags & BF_SHUTW) |
| 5377 | goto aborted_xfer; |
| 5378 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5379 | /* 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] | 5380 | if (!s->req->analysers) |
| 5381 | goto return_bad_res; |
| 5382 | |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5383 | /* forward any pending data */ |
| 5384 | bytes = msg->sov - msg->som; |
| 5385 | if (msg->chunk_len || bytes) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5386 | msg->som = msg->sov; |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5387 | if (likely(bytes < 0)) /* sov may have wrapped at the end */ |
| 5388 | bytes += res->size; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 5389 | msg->next -= bytes; /* will be forwarded */ |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5390 | msg->chunk_len += (unsigned int)bytes; |
| 5391 | msg->chunk_len -= buffer_forward(res, msg->chunk_len); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5392 | } |
| 5393 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5394 | /* When TE: chunked is used, we need to get there again to parse remaining |
| 5395 | * chunks even if the server has closed, so we don't want to set BF_DONTCLOSE. |
| 5396 | * Similarly, with keep-alive on the client side, we don't want to forward a |
| 5397 | * close. |
| 5398 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5399 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5400 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5401 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
| 5402 | buffer_dont_close(res); |
| 5403 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5404 | /* We know that more data are expected, but we couldn't send more that |
Willy Tarreau | 0729303 | 2011-05-30 18:29:28 +0200 | [diff] [blame] | 5405 | * what we did. So we always set the BF_EXPECT_MORE flag so that the |
| 5406 | * 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] | 5407 | * modes are already handled by the stream sock layer. We must not do |
| 5408 | * this in content-length mode because it could present the MSG_MORE |
| 5409 | * flag with the last block of forwarded data, which would cause an |
| 5410 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5411 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5412 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 869fc1e | 2012-03-05 08:29:20 +0100 | [diff] [blame] | 5413 | res->flags |= BF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5414 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5415 | /* the session handler will take care of timeouts and errors */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5416 | http_silent_debug(__LINE__, s); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5417 | return 0; |
| 5418 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5419 | return_bad_res: /* let's centralize all bad responses */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5420 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5421 | if (target_srv(&s->target)) |
| 5422 | target_srv(&s->target)->counters.failed_resp++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5423 | |
| 5424 | return_bad_res_stats_ok: |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5425 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 5426 | /* don't send any error message as we're in the body */ |
| 5427 | stream_int_retnclose(res->cons, NULL); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5428 | res->analysers = 0; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5429 | s->req->analysers = 0; /* we're in data phase, we want to abort both directions */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5430 | if (target_srv(&s->target)) |
| 5431 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5432 | |
| 5433 | if (!(s->flags & SN_ERR_MASK)) |
| 5434 | s->flags |= SN_ERR_PRXCOND; |
| 5435 | if (!(s->flags & SN_FINST_MASK)) |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5436 | s->flags |= SN_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5437 | return 0; |
| 5438 | |
| 5439 | aborted_xfer: |
| 5440 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 5441 | /* don't send any error message as we're in the body */ |
| 5442 | stream_int_retnclose(res->cons, NULL); |
| 5443 | res->analysers = 0; |
| 5444 | s->req->analysers = 0; /* we're in data phase, we want to abort both directions */ |
| 5445 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5446 | s->fe->fe_counters.cli_aborts++; |
| 5447 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5448 | if (target_srv(&s->target)) |
| 5449 | target_srv(&s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5450 | |
| 5451 | if (!(s->flags & SN_ERR_MASK)) |
| 5452 | s->flags |= SN_ERR_CLICL; |
| 5453 | if (!(s->flags & SN_FINST_MASK)) |
| 5454 | s->flags |= SN_FINST_D; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5455 | return 0; |
| 5456 | } |
| 5457 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5458 | /* Iterate the same filter through all request headers. |
| 5459 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5460 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 5461 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5462 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5463 | int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5464 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5465 | char term; |
| 5466 | char *cur_ptr, *cur_end, *cur_next; |
| 5467 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5468 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5469 | struct hdr_idx_elem *cur_hdr; |
| 5470 | int len, delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 5471 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5472 | last_hdr = 0; |
| 5473 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 5474 | cur_next = req->p + txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5475 | old_idx = 0; |
| 5476 | |
| 5477 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5478 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5479 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5480 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5481 | (exp->action == ACT_ALLOW || |
| 5482 | exp->action == ACT_DENY || |
| 5483 | exp->action == ACT_TARPIT)) |
| 5484 | return 0; |
| 5485 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5486 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5487 | if (!cur_idx) |
| 5488 | break; |
| 5489 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5490 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5491 | cur_ptr = cur_next; |
| 5492 | cur_end = cur_ptr + cur_hdr->len; |
| 5493 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5494 | |
| 5495 | /* Now we have one header between cur_ptr and cur_end, |
| 5496 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5497 | */ |
| 5498 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5499 | /* The annoying part is that pattern matching needs |
| 5500 | * that we modify the contents to null-terminate all |
| 5501 | * strings before testing them. |
| 5502 | */ |
| 5503 | |
| 5504 | term = *cur_end; |
| 5505 | *cur_end = '\0'; |
| 5506 | |
| 5507 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5508 | switch (exp->action) { |
| 5509 | case ACT_SETBE: |
| 5510 | /* It is not possible to jump a second time. |
| 5511 | * FIXME: should we return an HTTP/500 here so that |
| 5512 | * the admin knows there's a problem ? |
| 5513 | */ |
| 5514 | if (t->be != t->fe) |
| 5515 | break; |
| 5516 | |
| 5517 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 5518 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5519 | last_hdr = 1; |
| 5520 | break; |
| 5521 | |
| 5522 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5523 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5524 | last_hdr = 1; |
| 5525 | break; |
| 5526 | |
| 5527 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5528 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5529 | last_hdr = 1; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5530 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5531 | t->fe->fe_counters.denied_req++; |
| 5532 | if (t->fe != t->be) |
| 5533 | t->be->be_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5534 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 5535 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5536 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5537 | break; |
| 5538 | |
| 5539 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5540 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5541 | last_hdr = 1; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5542 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5543 | t->fe->fe_counters.denied_req++; |
| 5544 | if (t->fe != t->be) |
| 5545 | t->be->be_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5546 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 5547 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5548 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5549 | break; |
| 5550 | |
| 5551 | case ACT_REPLACE: |
| 5552 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 5553 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 5554 | /* FIXME: if the user adds a newline in the replacement, the |
| 5555 | * index will not be recalculated for now, and the new line |
| 5556 | * will not be counted as a new header. |
| 5557 | */ |
| 5558 | |
| 5559 | cur_end += delta; |
| 5560 | cur_next += delta; |
| 5561 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5562 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5563 | break; |
| 5564 | |
| 5565 | case ACT_REMOVE: |
| 5566 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
| 5567 | cur_next += delta; |
| 5568 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5569 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5570 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 5571 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5572 | cur_hdr->len = 0; |
| 5573 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 5574 | cur_idx = old_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5575 | break; |
| 5576 | |
| 5577 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5578 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5579 | if (cur_end) |
| 5580 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5581 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5582 | /* keep the link from this header to next one in case of later |
| 5583 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5584 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5585 | old_idx = cur_idx; |
| 5586 | } |
| 5587 | return 0; |
| 5588 | } |
| 5589 | |
| 5590 | |
| 5591 | /* Apply the filter to the request line. |
| 5592 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 5593 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5594 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 5595 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5596 | */ |
| 5597 | int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 5598 | { |
| 5599 | char term; |
| 5600 | char *cur_ptr, *cur_end; |
| 5601 | int done; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5602 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5603 | int len, delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5604 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5605 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5606 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5607 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5608 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5609 | (exp->action == ACT_ALLOW || |
| 5610 | exp->action == ACT_DENY || |
| 5611 | exp->action == ACT_TARPIT)) |
| 5612 | return 0; |
| 5613 | else if (exp->action == ACT_REMOVE) |
| 5614 | return 0; |
| 5615 | |
| 5616 | done = 0; |
| 5617 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 5618 | cur_ptr = req->p + txn->req.sol; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5619 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5620 | |
| 5621 | /* Now we have the request line between cur_ptr and cur_end */ |
| 5622 | |
| 5623 | /* The annoying part is that pattern matching needs |
| 5624 | * that we modify the contents to null-terminate all |
| 5625 | * strings before testing them. |
| 5626 | */ |
| 5627 | |
| 5628 | term = *cur_end; |
| 5629 | *cur_end = '\0'; |
| 5630 | |
| 5631 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5632 | switch (exp->action) { |
| 5633 | case ACT_SETBE: |
| 5634 | /* It is not possible to jump a second time. |
| 5635 | * FIXME: should we return an HTTP/500 here so that |
| 5636 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5637 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5638 | if (t->be != t->fe) |
| 5639 | break; |
| 5640 | |
| 5641 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 5642 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5643 | done = 1; |
| 5644 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5645 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5646 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5647 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5648 | done = 1; |
| 5649 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5650 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5651 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5652 | txn->flags |= TX_CLDENY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5653 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5654 | t->fe->fe_counters.denied_req++; |
| 5655 | if (t->fe != t->be) |
| 5656 | t->be->be_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5657 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 5658 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5659 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5660 | done = 1; |
| 5661 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5662 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5663 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5664 | txn->flags |= TX_CLTARPIT; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5665 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5666 | t->fe->fe_counters.denied_req++; |
| 5667 | if (t->fe != t->be) |
| 5668 | t->be->be_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5669 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 5670 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5671 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5672 | done = 1; |
| 5673 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5674 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5675 | case ACT_REPLACE: |
| 5676 | *cur_end = term; /* restore the string terminator */ |
| 5677 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 5678 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 5679 | /* FIXME: if the user adds a newline in the replacement, the |
| 5680 | * index will not be recalculated for now, and the new line |
| 5681 | * will not be counted as a new header. |
| 5682 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5683 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5684 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5685 | cur_end += delta; |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 5686 | cur_end = (char *)http_parse_reqline(&txn->req, req->data, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5687 | HTTP_MSG_RQMETH, |
| 5688 | cur_ptr, cur_end + 1, |
| 5689 | NULL, NULL); |
| 5690 | if (unlikely(!cur_end)) |
| 5691 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5692 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5693 | /* we have a full request and we know that we have either a CR |
| 5694 | * or an LF at <ptr>. |
| 5695 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5696 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 5697 | 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] | 5698 | /* there is no point trying this regex on headers */ |
| 5699 | return 1; |
| 5700 | } |
| 5701 | } |
| 5702 | *cur_end = term; /* restore the string terminator */ |
| 5703 | return done; |
| 5704 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 5705 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5706 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5707 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5708 | /* |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5709 | * 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] | 5710 | * 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] | 5711 | * unparsable request. Since it can manage the switch to another backend, it |
| 5712 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5713 | */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5714 | int apply_filters_to_request(struct session *s, struct buffer *req, struct proxy *px) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5715 | { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5716 | struct http_txn *txn = &s->txn; |
| 5717 | struct hdr_exp *exp; |
| 5718 | |
| 5719 | for (exp = px->req_exp; exp; exp = exp->next) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5720 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5721 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5722 | /* |
| 5723 | * The interleaving of transformations and verdicts |
| 5724 | * makes it difficult to decide to continue or stop |
| 5725 | * the evaluation. |
| 5726 | */ |
| 5727 | |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5728 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) |
| 5729 | break; |
| 5730 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5731 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5732 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5733 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5734 | continue; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5735 | |
| 5736 | /* if this filter had a condition, evaluate it now and skip to |
| 5737 | * next filter if the condition does not match. |
| 5738 | */ |
| 5739 | if (exp->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 5740 | 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] | 5741 | ret = acl_pass(ret); |
| 5742 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 5743 | ret = !ret; |
| 5744 | |
| 5745 | if (!ret) |
| 5746 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5747 | } |
| 5748 | |
| 5749 | /* Apply the filter to the request line. */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5750 | ret = apply_filter_to_req_line(s, req, exp); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5751 | if (unlikely(ret < 0)) |
| 5752 | return -1; |
| 5753 | |
| 5754 | if (likely(ret == 0)) { |
| 5755 | /* The filter did not match the request, it can be |
| 5756 | * iterated through all headers. |
| 5757 | */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5758 | apply_filter_to_req_headers(s, req, exp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5759 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5760 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5761 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5762 | } |
| 5763 | |
| 5764 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5765 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5766 | /* |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5767 | * Try to retrieve the server associated to the appsession. |
| 5768 | * If the server is found, it's assigned to the session. |
| 5769 | */ |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5770 | 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] | 5771 | struct http_txn *txn = &t->txn; |
| 5772 | appsess *asession = NULL; |
| 5773 | char *sessid_temp = NULL; |
| 5774 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5775 | if (len > t->be->appsession_len) { |
| 5776 | len = t->be->appsession_len; |
| 5777 | } |
| 5778 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5779 | if (t->be->options2 & PR_O2_AS_REQL) { |
| 5780 | /* 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] | 5781 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5782 | /* 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] | 5783 | pool_free2(apools.sessid, txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5784 | } |
| 5785 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 5786 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5787 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5788 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5789 | return; |
| 5790 | } |
| 5791 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 5792 | memcpy(txn->sessid, buf, len); |
| 5793 | txn->sessid[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5794 | } |
| 5795 | |
| 5796 | if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) { |
| 5797 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5798 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5799 | return; |
| 5800 | } |
| 5801 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5802 | memcpy(sessid_temp, buf, len); |
| 5803 | sessid_temp[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5804 | |
| 5805 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp); |
| 5806 | /* free previously allocated memory */ |
| 5807 | pool_free2(apools.sessid, sessid_temp); |
| 5808 | |
| 5809 | if (asession != NULL) { |
| 5810 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 5811 | if (!(t->be->options2 & PR_O2_AS_REQL)) |
| 5812 | asession->request_count++; |
| 5813 | |
| 5814 | if (asession->serverid != NULL) { |
| 5815 | struct server *srv = t->be->srv; |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 5816 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5817 | while (srv) { |
| 5818 | if (strcmp(srv->id, asession->serverid) == 0) { |
Willy Tarreau | 4de9149 | 2010-01-22 19:10:05 +0100 | [diff] [blame] | 5819 | if ((srv->state & SRV_RUNNING) || |
| 5820 | (t->be->options & PR_O_PERSIST) || |
| 5821 | (t->flags & SN_FORCE_PRST)) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5822 | /* we found the server and it's usable */ |
| 5823 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | 2a6d88d | 2010-01-24 13:10:43 +0100 | [diff] [blame] | 5824 | txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5825 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 5826 | set_target_server(&t->target, srv); |
Willy Tarreau | 664beb8 | 2011-03-10 11:38:29 +0100 | [diff] [blame] | 5827 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5828 | break; |
| 5829 | } else { |
| 5830 | txn->flags &= ~TX_CK_MASK; |
| 5831 | txn->flags |= TX_CK_DOWN; |
| 5832 | } |
| 5833 | } |
| 5834 | srv = srv->next; |
| 5835 | } |
| 5836 | } |
| 5837 | } |
| 5838 | } |
| 5839 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5840 | /* Find the end of a cookie value contained between <s> and <e>. It works the |
| 5841 | * same way as with headers above except that the semi-colon also ends a token. |
| 5842 | * See RFC2965 for more information. Note that it requires a valid header to |
| 5843 | * return a valid result. |
| 5844 | */ |
| 5845 | char *find_cookie_value_end(char *s, const char *e) |
| 5846 | { |
| 5847 | int quoted, qdpair; |
| 5848 | |
| 5849 | quoted = qdpair = 0; |
| 5850 | for (; s < e; s++) { |
| 5851 | if (qdpair) qdpair = 0; |
| 5852 | else if (quoted) { |
| 5853 | if (*s == '\\') qdpair = 1; |
| 5854 | else if (*s == '"') quoted = 0; |
| 5855 | } |
| 5856 | else if (*s == '"') quoted = 1; |
| 5857 | else if (*s == ',' || *s == ';') return s; |
| 5858 | } |
| 5859 | return s; |
| 5860 | } |
| 5861 | |
| 5862 | /* Delete a value in a header between delimiters <from> and <next> in buffer |
| 5863 | * <buf>. The number of characters displaced is returned, and the pointer to |
| 5864 | * the first delimiter is updated if required. The function tries as much as |
| 5865 | * possible to respect the following principles : |
| 5866 | * - replace <from> delimiter by the <next> one unless <from> points to a |
| 5867 | * colon, in which case <next> is simply removed |
| 5868 | * - set exactly one space character after the new first delimiter, unless |
| 5869 | * there are not enough characters in the block being moved to do so. |
| 5870 | * - remove unneeded spaces before the previous delimiter and after the new |
| 5871 | * one. |
| 5872 | * |
| 5873 | * It is the caller's responsibility to ensure that : |
| 5874 | * - <from> points to a valid delimiter or the colon ; |
| 5875 | * - <next> points to a valid delimiter or the final CR/LF ; |
| 5876 | * - there are non-space chars before <from> ; |
| 5877 | * - there is a CR/LF at or after <next>. |
| 5878 | */ |
| 5879 | int del_hdr_value(struct buffer *buf, char **from, char *next) |
| 5880 | { |
| 5881 | char *prev = *from; |
| 5882 | |
| 5883 | if (*prev == ':') { |
| 5884 | /* We're removing the first value, preserve the colon and add a |
| 5885 | * space if possible. |
| 5886 | */ |
| 5887 | if (!http_is_crlf[(unsigned char)*next]) |
| 5888 | next++; |
| 5889 | prev++; |
| 5890 | if (prev < next) |
| 5891 | *prev++ = ' '; |
| 5892 | |
| 5893 | while (http_is_spht[(unsigned char)*next]) |
| 5894 | next++; |
| 5895 | } else { |
| 5896 | /* Remove useless spaces before the old delimiter. */ |
| 5897 | while (http_is_spht[(unsigned char)*(prev-1)]) |
| 5898 | prev--; |
| 5899 | *from = prev; |
| 5900 | |
| 5901 | /* copy the delimiter and if possible a space if we're |
| 5902 | * not at the end of the line. |
| 5903 | */ |
| 5904 | if (!http_is_crlf[(unsigned char)*next]) { |
| 5905 | *prev++ = *next++; |
| 5906 | if (prev + 1 < next) |
| 5907 | *prev++ = ' '; |
| 5908 | while (http_is_spht[(unsigned char)*next]) |
| 5909 | next++; |
| 5910 | } |
| 5911 | } |
| 5912 | return buffer_replace2(buf, prev, next, NULL, 0); |
| 5913 | } |
| 5914 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5915 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 5916 | * 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] | 5917 | * desirable to call it only when needed. This code is quite complex because |
| 5918 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 5919 | * highly recommended not to touch this part without a good reason ! |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5920 | */ |
| 5921 | void manage_client_side_cookies(struct session *t, struct buffer *req) |
| 5922 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5923 | struct http_txn *txn = &t->txn; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5924 | int preserve_hdr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5925 | int cur_idx, old_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5926 | char *hdr_beg, *hdr_end, *hdr_next, *del_from; |
| 5927 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5928 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5929 | /* Iterate through the headers, we start with the start line. */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 5930 | old_idx = 0; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 5931 | hdr_next = req->p + txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5932 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5933 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5934 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5935 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5936 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5937 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5938 | hdr_beg = hdr_next; |
| 5939 | hdr_end = hdr_beg + cur_hdr->len; |
| 5940 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5941 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5942 | /* We have one full header between hdr_beg and hdr_end, and the |
| 5943 | * next header starts at hdr_next. We're only interested in |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5944 | * "Cookie:" headers. |
| 5945 | */ |
| 5946 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5947 | val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5948 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5949 | old_idx = cur_idx; |
| 5950 | continue; |
| 5951 | } |
| 5952 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5953 | del_from = NULL; /* nothing to be deleted */ |
| 5954 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 5955 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5956 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 5957 | * attributes whose name begin with a '$', and associate them with |
| 5958 | * the right cookie, if we want to delete this cookie. |
| 5959 | * So there are 3 cases for each cookie read : |
| 5960 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 5961 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 5962 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 5963 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 5964 | * "special" cookie. |
| 5965 | * At the end of loop, if a "special" cookie remains, we may have to |
| 5966 | * remove it. If no application cookie persists in the header, we |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5967 | * *MUST* delete it. |
| 5968 | * |
| 5969 | * Note: RFC2965 is unclear about the processing of spaces around |
| 5970 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 5971 | * the RFC explicitly allows spaces before it, and not within the |
| 5972 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 5973 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 5974 | * after the equal sign too, resulting in some (rare) buggy |
| 5975 | * implementations trying to do that. So let's do what servers do. |
| 5976 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 5977 | * allowed quoted strings in values, with any possible character |
| 5978 | * after a backslash, including control chars and delimitors, which |
| 5979 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 5980 | * within values even without quotes. |
| 5981 | * |
| 5982 | * We have to keep multiple pointers in order to support cookie |
| 5983 | * removal at the beginning, middle or end of header without |
| 5984 | * corrupting the header. All of these headers are valid : |
| 5985 | * |
| 5986 | * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n |
| 5987 | * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n |
| 5988 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 5989 | * | | | | | | | | | |
| 5990 | * | | | | | | | | hdr_end <--+ |
| 5991 | * | | | | | | | +--> next |
| 5992 | * | | | | | | +----> val_end |
| 5993 | * | | | | | +-----------> val_beg |
| 5994 | * | | | | +--------------> equal |
| 5995 | * | | | +----------------> att_end |
| 5996 | * | | +---------------------> att_beg |
| 5997 | * | +--------------------------> prev |
| 5998 | * +--------------------------------> hdr_beg |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5999 | */ |
| 6000 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6001 | for (prev = hdr_beg + 6; prev < hdr_end; prev = next) { |
| 6002 | /* Iterate through all cookies on this line */ |
| 6003 | |
| 6004 | /* find att_beg */ |
| 6005 | att_beg = prev + 1; |
| 6006 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 6007 | att_beg++; |
| 6008 | |
| 6009 | /* find att_end : this is the first character after the last non |
| 6010 | * space before the equal. It may be equal to hdr_end. |
| 6011 | */ |
| 6012 | equal = att_end = att_beg; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6013 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6014 | while (equal < hdr_end) { |
| 6015 | if (*equal == '=' || *equal == ',' || *equal == ';') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6016 | break; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6017 | if (http_is_spht[(unsigned char)*equal++]) |
| 6018 | continue; |
| 6019 | att_end = equal; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6020 | } |
| 6021 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6022 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 6023 | * is between <att_beg> and <equal>, both may be identical. |
| 6024 | */ |
| 6025 | |
| 6026 | /* look for end of cookie if there is an equal sign */ |
| 6027 | if (equal < hdr_end && *equal == '=') { |
| 6028 | /* look for the beginning of the value */ |
| 6029 | val_beg = equal + 1; |
| 6030 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 6031 | val_beg++; |
| 6032 | |
| 6033 | /* find the end of the value, respecting quotes */ |
| 6034 | next = find_cookie_value_end(val_beg, hdr_end); |
| 6035 | |
| 6036 | /* make val_end point to the first white space or delimitor after the value */ |
| 6037 | val_end = next; |
| 6038 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 6039 | val_end--; |
| 6040 | } else { |
| 6041 | val_beg = val_end = next = equal; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6042 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6043 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6044 | /* We have nothing to do with attributes beginning with '$'. However, |
| 6045 | * they will automatically be removed if a header before them is removed, |
| 6046 | * since they're supposed to be linked together. |
| 6047 | */ |
| 6048 | if (*att_beg == '$') |
| 6049 | continue; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6050 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6051 | /* Ignore cookies with no equal sign */ |
| 6052 | if (equal == next) { |
| 6053 | /* This is not our cookie, so we must preserve it. But if we already |
| 6054 | * scheduled another cookie for removal, we cannot remove the |
| 6055 | * complete header, but we can remove the previous block itself. |
| 6056 | */ |
| 6057 | preserve_hdr = 1; |
| 6058 | if (del_from != NULL) { |
| 6059 | int delta = del_hdr_value(req, &del_from, prev); |
| 6060 | val_end += delta; |
| 6061 | next += delta; |
| 6062 | hdr_end += delta; |
| 6063 | hdr_next += delta; |
| 6064 | cur_hdr->len += delta; |
| 6065 | http_msg_move_end(&txn->req, delta); |
| 6066 | prev = del_from; |
| 6067 | del_from = NULL; |
| 6068 | } |
| 6069 | continue; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6070 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6071 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6072 | /* if there are spaces around the equal sign, we need to |
| 6073 | * strip them otherwise we'll get trouble for cookie captures, |
| 6074 | * or even for rewrites. Since this happens extremely rarely, |
| 6075 | * it does not hurt performance. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6076 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6077 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 6078 | int stripped_before = 0; |
| 6079 | int stripped_after = 0; |
| 6080 | |
| 6081 | if (att_end != equal) { |
| 6082 | stripped_before = buffer_replace2(req, att_end, equal, NULL, 0); |
| 6083 | equal += stripped_before; |
| 6084 | val_beg += stripped_before; |
| 6085 | } |
| 6086 | |
| 6087 | if (val_beg > equal + 1) { |
| 6088 | stripped_after = buffer_replace2(req, equal + 1, val_beg, NULL, 0); |
| 6089 | val_beg += stripped_after; |
| 6090 | stripped_before += stripped_after; |
| 6091 | } |
| 6092 | |
| 6093 | val_end += stripped_before; |
| 6094 | next += stripped_before; |
| 6095 | hdr_end += stripped_before; |
| 6096 | hdr_next += stripped_before; |
| 6097 | cur_hdr->len += stripped_before; |
| 6098 | http_msg_move_end(&txn->req, stripped_before); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6099 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6100 | /* now everything is as on the diagram above */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6101 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6102 | /* First, let's see if we want to capture this cookie. We check |
| 6103 | * that we don't already have a client side cookie, because we |
| 6104 | * can only capture one. Also as an optimisation, we ignore |
| 6105 | * cookies shorter than the declared name. |
| 6106 | */ |
| 6107 | if (t->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 6108 | (val_end - att_beg >= t->fe->capture_namelen) && |
| 6109 | memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
| 6110 | int log_len = val_end - att_beg; |
| 6111 | |
| 6112 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
| 6113 | Alert("HTTP logging : out of memory.\n"); |
| 6114 | } else { |
| 6115 | if (log_len > t->fe->capture_len) |
| 6116 | log_len = t->fe->capture_len; |
| 6117 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 6118 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6119 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6120 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6121 | |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6122 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 6123 | * following form : |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6124 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6125 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6126 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6127 | * For cookies in prefix mode, the form is : |
| 6128 | * |
| 6129 | * Cookie: NAME=SRV~VALUE |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6130 | */ |
| 6131 | if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 6132 | (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) { |
| 6133 | struct server *srv = t->be->srv; |
| 6134 | char *delim; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6135 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6136 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 6137 | * have the server ID between val_beg and delim, and the original cookie between |
| 6138 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 6139 | * |
| 6140 | * Cookie: NAME=SRV; # in all but prefix modes |
| 6141 | * Cookie: NAME=SRV~OPAQUE ; # in prefix mode |
| 6142 | * | || || | |+-> next |
| 6143 | * | || || | +--> val_end |
| 6144 | * | || || +---------> delim |
| 6145 | * | || |+------------> val_beg |
| 6146 | * | || +-------------> att_end = equal |
| 6147 | * | |+-----------------> att_beg |
| 6148 | * | +------------------> prev |
| 6149 | * +-------------------------> hdr_beg |
| 6150 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6151 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6152 | if (t->be->options & PR_O_COOK_PFX) { |
| 6153 | for (delim = val_beg; delim < val_end; delim++) |
| 6154 | if (*delim == COOKIE_DELIM) |
| 6155 | break; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6156 | } else { |
| 6157 | char *vbar1; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6158 | delim = val_end; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6159 | /* Now check if the cookie contains a date field, which would |
| 6160 | * appear after a vertical bar ('|') just after the server name |
| 6161 | * and before the delimiter. |
| 6162 | */ |
| 6163 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 6164 | if (vbar1) { |
| 6165 | /* OK, so left of the bar is the server's cookie and |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6166 | * right is the last seen date. It is a base64 encoded |
| 6167 | * 30-bit value representing the UNIX date since the |
| 6168 | * epoch in 4-second quantities. |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6169 | */ |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6170 | int val; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6171 | delim = vbar1++; |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6172 | if (val_end - vbar1 >= 5) { |
| 6173 | val = b64tos30(vbar1); |
| 6174 | if (val > 0) |
| 6175 | txn->cookie_last_date = val << 2; |
| 6176 | } |
| 6177 | /* look for a second vertical bar */ |
| 6178 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 6179 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 6180 | val = b64tos30(vbar1 + 1); |
| 6181 | if (val > 0) |
| 6182 | txn->cookie_first_date = val << 2; |
| 6183 | } |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6184 | } |
| 6185 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6186 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6187 | /* if the cookie has an expiration date and the proxy wants to check |
| 6188 | * it, then we do that now. We first check if the cookie is too old, |
| 6189 | * then only if it has expired. We detect strict overflow because the |
| 6190 | * time resolution here is not great (4 seconds). Cookies with dates |
| 6191 | * in the future are ignored if their offset is beyond one day. This |
| 6192 | * allows an admin to fix timezone issues without expiring everyone |
| 6193 | * and at the same time avoids keeping unwanted side effects for too |
| 6194 | * long. |
| 6195 | */ |
| 6196 | if (txn->cookie_first_date && t->be->cookie_maxlife && |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6197 | (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) || |
| 6198 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6199 | txn->flags &= ~TX_CK_MASK; |
| 6200 | txn->flags |= TX_CK_OLD; |
| 6201 | delim = val_beg; // let's pretend we have not found the cookie |
| 6202 | txn->cookie_first_date = 0; |
| 6203 | txn->cookie_last_date = 0; |
| 6204 | } |
| 6205 | else if (txn->cookie_last_date && t->be->cookie_maxidle && |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6206 | (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) || |
| 6207 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6208 | txn->flags &= ~TX_CK_MASK; |
| 6209 | txn->flags |= TX_CK_EXPIRED; |
| 6210 | delim = val_beg; // let's pretend we have not found the cookie |
| 6211 | txn->cookie_first_date = 0; |
| 6212 | txn->cookie_last_date = 0; |
| 6213 | } |
| 6214 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6215 | /* Here, we'll look for the first running server which supports the cookie. |
| 6216 | * This allows to share a same cookie between several servers, for example |
| 6217 | * to dedicate backup servers to specific servers only. |
| 6218 | * However, to prevent clients from sticking to cookie-less backup server |
| 6219 | * when they have incidentely learned an empty cookie, we simply ignore |
| 6220 | * empty cookies and mark them as invalid. |
| 6221 | * The same behaviour is applied when persistence must be ignored. |
| 6222 | */ |
Willy Tarreau | 4a5cade | 2012-04-05 21:09:48 +0200 | [diff] [blame] | 6223 | if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6224 | srv = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6225 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6226 | while (srv) { |
| 6227 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 6228 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
| 6229 | if ((srv->state & SRV_RUNNING) || |
| 6230 | (t->be->options & PR_O_PERSIST) || |
| 6231 | (t->flags & SN_FORCE_PRST)) { |
| 6232 | /* we found the server and we can use it */ |
| 6233 | txn->flags &= ~TX_CK_MASK; |
| 6234 | txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN; |
| 6235 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 6236 | set_target_server(&t->target, srv); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6237 | break; |
| 6238 | } else { |
| 6239 | /* we found a server, but it's down, |
| 6240 | * mark it as such and go on in case |
| 6241 | * another one is available. |
| 6242 | */ |
| 6243 | txn->flags &= ~TX_CK_MASK; |
| 6244 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6245 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6246 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6247 | srv = srv->next; |
| 6248 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6249 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6250 | 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] | 6251 | /* no server matched this cookie or we deliberately skipped it */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6252 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | c89ccb6 | 2012-04-05 21:18:22 +0200 | [diff] [blame] | 6253 | if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED))) |
| 6254 | txn->flags |= TX_CK_UNUSED; |
| 6255 | else |
| 6256 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6257 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6258 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6259 | /* depending on the cookie mode, we may have to either : |
| 6260 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 6261 | * the server never sees it ; |
| 6262 | * - remove the server id from the cookie value, and tag the cookie as an |
| 6263 | * application cookie so that it does not get accidentely removed later, |
| 6264 | * if we're in cookie prefix mode |
| 6265 | */ |
| 6266 | if ((t->be->options & PR_O_COOK_PFX) && (delim != val_end)) { |
| 6267 | int delta; /* negative */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6268 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6269 | delta = buffer_replace2(req, val_beg, delim + 1, NULL, 0); |
| 6270 | val_end += delta; |
| 6271 | next += delta; |
| 6272 | hdr_end += delta; |
| 6273 | hdr_next += delta; |
| 6274 | cur_hdr->len += delta; |
| 6275 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6276 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6277 | del_from = NULL; |
| 6278 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 6279 | } |
| 6280 | else if (del_from == NULL && |
| 6281 | (t->be->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) { |
| 6282 | del_from = prev; |
| 6283 | } |
| 6284 | } else { |
| 6285 | /* This is not our cookie, so we must preserve it. But if we already |
| 6286 | * scheduled another cookie for removal, we cannot remove the |
| 6287 | * complete header, but we can remove the previous block itself. |
| 6288 | */ |
| 6289 | preserve_hdr = 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6290 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6291 | if (del_from != NULL) { |
| 6292 | int delta = del_hdr_value(req, &del_from, prev); |
Willy Tarreau | b810554 | 2010-11-24 18:31:28 +0100 | [diff] [blame] | 6293 | if (att_beg >= del_from) |
| 6294 | att_beg += delta; |
| 6295 | if (att_end >= del_from) |
| 6296 | att_end += delta; |
| 6297 | val_beg += delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6298 | val_end += delta; |
| 6299 | next += delta; |
| 6300 | hdr_end += delta; |
| 6301 | hdr_next += delta; |
| 6302 | cur_hdr->len += delta; |
| 6303 | http_msg_move_end(&txn->req, delta); |
| 6304 | prev = del_from; |
| 6305 | del_from = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6306 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6307 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6308 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6309 | /* Look for the appsession cookie unless persistence must be ignored */ |
| 6310 | if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) { |
| 6311 | int cmp_len, value_len; |
| 6312 | char *value_begin; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 6313 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6314 | if (t->be->options2 & PR_O2_AS_PFX) { |
| 6315 | cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len); |
| 6316 | value_begin = att_beg + t->be->appsession_name_len; |
| 6317 | value_len = val_end - att_beg - t->be->appsession_name_len; |
| 6318 | } else { |
| 6319 | cmp_len = att_end - att_beg; |
| 6320 | value_begin = val_beg; |
| 6321 | value_len = val_end - val_beg; |
| 6322 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6323 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6324 | /* let's see if the cookie is our appcookie */ |
| 6325 | if (cmp_len == t->be->appsession_name_len && |
| 6326 | memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) { |
| 6327 | manage_client_side_appsession(t, value_begin, value_len); |
| 6328 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6329 | } |
| 6330 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6331 | /* continue with next cookie on this header line */ |
| 6332 | att_beg = next; |
| 6333 | } /* for each cookie */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6334 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6335 | /* There are no more cookies on this line. |
| 6336 | * We may still have one (or several) marked for deletion at the |
| 6337 | * end of the line. We must do this now in two ways : |
| 6338 | * - if some cookies must be preserved, we only delete from the |
| 6339 | * mark to the end of line ; |
| 6340 | * - if nothing needs to be preserved, simply delete the whole header |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6341 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6342 | if (del_from) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6343 | int delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6344 | if (preserve_hdr) { |
| 6345 | delta = del_hdr_value(req, &del_from, hdr_end); |
| 6346 | hdr_end = del_from; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6347 | cur_hdr->len += delta; |
| 6348 | } else { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6349 | delta = buffer_replace2(req, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6350 | |
| 6351 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6352 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6353 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6354 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6355 | cur_idx = old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6356 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6357 | hdr_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6358 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6359 | } |
| 6360 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6361 | /* check next header */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6362 | old_idx = cur_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6363 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6364 | } |
| 6365 | |
| 6366 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6367 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 6368 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 6369 | */ |
| 6370 | int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 6371 | { |
| 6372 | char term; |
| 6373 | char *cur_ptr, *cur_end, *cur_next; |
| 6374 | int cur_idx, old_idx, last_hdr; |
| 6375 | struct http_txn *txn = &t->txn; |
| 6376 | struct hdr_idx_elem *cur_hdr; |
| 6377 | int len, delta; |
| 6378 | |
| 6379 | last_hdr = 0; |
| 6380 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 6381 | cur_next = rtr->p + txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6382 | old_idx = 0; |
| 6383 | |
| 6384 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6385 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6386 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6387 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6388 | (exp->action == ACT_ALLOW || |
| 6389 | exp->action == ACT_DENY)) |
| 6390 | return 0; |
| 6391 | |
| 6392 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 6393 | if (!cur_idx) |
| 6394 | break; |
| 6395 | |
| 6396 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 6397 | cur_ptr = cur_next; |
| 6398 | cur_end = cur_ptr + cur_hdr->len; |
| 6399 | cur_next = cur_end + cur_hdr->cr + 1; |
| 6400 | |
| 6401 | /* Now we have one header between cur_ptr and cur_end, |
| 6402 | * and the next header starts at cur_next. |
| 6403 | */ |
| 6404 | |
| 6405 | /* The annoying part is that pattern matching needs |
| 6406 | * that we modify the contents to null-terminate all |
| 6407 | * strings before testing them. |
| 6408 | */ |
| 6409 | |
| 6410 | term = *cur_end; |
| 6411 | *cur_end = '\0'; |
| 6412 | |
| 6413 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 6414 | switch (exp->action) { |
| 6415 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6416 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6417 | last_hdr = 1; |
| 6418 | break; |
| 6419 | |
| 6420 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6421 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6422 | last_hdr = 1; |
| 6423 | break; |
| 6424 | |
| 6425 | case ACT_REPLACE: |
| 6426 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 6427 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 6428 | /* FIXME: if the user adds a newline in the replacement, the |
| 6429 | * index will not be recalculated for now, and the new line |
| 6430 | * will not be counted as a new header. |
| 6431 | */ |
| 6432 | |
| 6433 | cur_end += delta; |
| 6434 | cur_next += delta; |
| 6435 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6436 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6437 | break; |
| 6438 | |
| 6439 | case ACT_REMOVE: |
| 6440 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 6441 | cur_next += delta; |
| 6442 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6443 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6444 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6445 | txn->hdr_idx.used--; |
| 6446 | cur_hdr->len = 0; |
| 6447 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6448 | cur_idx = old_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6449 | break; |
| 6450 | |
| 6451 | } |
| 6452 | } |
| 6453 | if (cur_end) |
| 6454 | *cur_end = term; /* restore the string terminator */ |
| 6455 | |
| 6456 | /* keep the link from this header to next one in case of later |
| 6457 | * removal of next header. |
| 6458 | */ |
| 6459 | old_idx = cur_idx; |
| 6460 | } |
| 6461 | return 0; |
| 6462 | } |
| 6463 | |
| 6464 | |
| 6465 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 6466 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 6467 | * or -1 if a replacement resulted in an invalid status line. |
| 6468 | */ |
| 6469 | int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 6470 | { |
| 6471 | char term; |
| 6472 | char *cur_ptr, *cur_end; |
| 6473 | int done; |
| 6474 | struct http_txn *txn = &t->txn; |
| 6475 | int len, delta; |
| 6476 | |
| 6477 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6478 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6479 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6480 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6481 | (exp->action == ACT_ALLOW || |
| 6482 | exp->action == ACT_DENY)) |
| 6483 | return 0; |
| 6484 | else if (exp->action == ACT_REMOVE) |
| 6485 | return 0; |
| 6486 | |
| 6487 | done = 0; |
| 6488 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 6489 | cur_ptr = rtr->p + txn->rsp.sol; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 6490 | cur_end = cur_ptr + txn->rsp.sl.st.l; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6491 | |
| 6492 | /* Now we have the status line between cur_ptr and cur_end */ |
| 6493 | |
| 6494 | /* The annoying part is that pattern matching needs |
| 6495 | * that we modify the contents to null-terminate all |
| 6496 | * strings before testing them. |
| 6497 | */ |
| 6498 | |
| 6499 | term = *cur_end; |
| 6500 | *cur_end = '\0'; |
| 6501 | |
| 6502 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 6503 | switch (exp->action) { |
| 6504 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6505 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6506 | done = 1; |
| 6507 | break; |
| 6508 | |
| 6509 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6510 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6511 | done = 1; |
| 6512 | break; |
| 6513 | |
| 6514 | case ACT_REPLACE: |
| 6515 | *cur_end = term; /* restore the string terminator */ |
| 6516 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 6517 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 6518 | /* FIXME: if the user adds a newline in the replacement, the |
| 6519 | * index will not be recalculated for now, and the new line |
| 6520 | * will not be counted as a new header. |
| 6521 | */ |
| 6522 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6523 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6524 | cur_end += delta; |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 6525 | cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 6526 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6527 | cur_ptr, cur_end + 1, |
| 6528 | NULL, NULL); |
| 6529 | if (unlikely(!cur_end)) |
| 6530 | return -1; |
| 6531 | |
| 6532 | /* we have a full respnse and we know that we have either a CR |
| 6533 | * or an LF at <ptr>. |
| 6534 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 6535 | txn->status = strl2ui(rtr->p + txn->rsp.sol + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l); |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 6536 | 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] | 6537 | /* there is no point trying this regex on headers */ |
| 6538 | return 1; |
| 6539 | } |
| 6540 | } |
| 6541 | *cur_end = term; /* restore the string terminator */ |
| 6542 | return done; |
| 6543 | } |
| 6544 | |
| 6545 | |
| 6546 | |
| 6547 | /* |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6548 | * 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] | 6549 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 6550 | * unparsable response. |
| 6551 | */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6552 | int apply_filters_to_response(struct session *s, struct buffer *rtr, struct proxy *px) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6553 | { |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6554 | struct http_txn *txn = &s->txn; |
| 6555 | struct hdr_exp *exp; |
| 6556 | |
| 6557 | for (exp = px->rsp_exp; exp; exp = exp->next) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6558 | int ret; |
| 6559 | |
| 6560 | /* |
| 6561 | * The interleaving of transformations and verdicts |
| 6562 | * makes it difficult to decide to continue or stop |
| 6563 | * the evaluation. |
| 6564 | */ |
| 6565 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6566 | if (txn->flags & TX_SVDENY) |
| 6567 | break; |
| 6568 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6569 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6570 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 6571 | exp->action == ACT_PASS)) { |
| 6572 | exp = exp->next; |
| 6573 | continue; |
| 6574 | } |
| 6575 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6576 | /* if this filter had a condition, evaluate it now and skip to |
| 6577 | * next filter if the condition does not match. |
| 6578 | */ |
| 6579 | if (exp->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 6580 | 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] | 6581 | ret = acl_pass(ret); |
| 6582 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 6583 | ret = !ret; |
| 6584 | if (!ret) |
| 6585 | continue; |
| 6586 | } |
| 6587 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6588 | /* Apply the filter to the status line. */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6589 | ret = apply_filter_to_sts_line(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6590 | if (unlikely(ret < 0)) |
| 6591 | return -1; |
| 6592 | |
| 6593 | if (likely(ret == 0)) { |
| 6594 | /* The filter did not match the response, it can be |
| 6595 | * iterated through all headers. |
| 6596 | */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6597 | apply_filter_to_resp_headers(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6598 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6599 | } |
| 6600 | return 0; |
| 6601 | } |
| 6602 | |
| 6603 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6604 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 6605 | * 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] | 6606 | * desirable to call it only when needed. This function is also used when we |
| 6607 | * 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] | 6608 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6609 | void manage_server_side_cookies(struct session *t, struct buffer *res) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6610 | { |
| 6611 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6612 | struct server *srv; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6613 | int is_cookie2; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6614 | int cur_idx, old_idx, delta; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6615 | char *hdr_beg, *hdr_end, *hdr_next; |
| 6616 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6617 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6618 | /* Iterate through the headers. |
| 6619 | * we start with the start line. |
| 6620 | */ |
| 6621 | old_idx = 0; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 6622 | hdr_next = res->p + txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6623 | |
| 6624 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 6625 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6626 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6627 | |
| 6628 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6629 | hdr_beg = hdr_next; |
| 6630 | hdr_end = hdr_beg + cur_hdr->len; |
| 6631 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6632 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6633 | /* We have one full header between hdr_beg and hdr_end, and the |
| 6634 | * next header starts at hdr_next. We're only interested in |
| 6635 | * "Set-Cookie" and "Set-Cookie2" headers. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6636 | */ |
| 6637 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6638 | is_cookie2 = 0; |
| 6639 | prev = hdr_beg + 10; |
| 6640 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6641 | if (!val) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6642 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11); |
| 6643 | if (!val) { |
| 6644 | old_idx = cur_idx; |
| 6645 | continue; |
| 6646 | } |
| 6647 | is_cookie2 = 1; |
| 6648 | prev = hdr_beg + 11; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6649 | } |
| 6650 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6651 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 6652 | * <prev> points to the colon. |
| 6653 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 6654 | txn->flags |= TX_SCK_PRESENT; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6655 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6656 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 6657 | * check-cache is enabled) and we are not interested in checking |
| 6658 | * them. Warning, the cookie capture is declared in the frontend. |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 6659 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6660 | if (t->be->cookie_name == NULL && |
| 6661 | t->be->appsession_name == NULL && |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 6662 | t->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6663 | return; |
| 6664 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6665 | /* OK so now we know we have to process this response cookie. |
| 6666 | * The format of the Set-Cookie header is slightly different |
| 6667 | * from the format of the Cookie header in that it does not |
| 6668 | * support the comma as a cookie delimiter (thus the header |
| 6669 | * cannot be folded) because the Expires attribute described in |
| 6670 | * the original Netscape's spec may contain an unquoted date |
| 6671 | * with a comma inside. We have to live with this because |
| 6672 | * many browsers don't support Max-Age and some browsers don't |
| 6673 | * support quoted strings. However the Set-Cookie2 header is |
| 6674 | * clean. |
| 6675 | * |
| 6676 | * We have to keep multiple pointers in order to support cookie |
| 6677 | * removal at the beginning, middle or end of header without |
| 6678 | * corrupting the header (in case of set-cookie2). A special |
| 6679 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 6680 | * fields after the first semi-colon. The <next> pointer points |
| 6681 | * either to the end of line (set-cookie) or next unquoted comma |
| 6682 | * (set-cookie2). All of these headers are valid : |
| 6683 | * |
| 6684 | * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n |
| 6685 | * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 6686 | * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 6687 | * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n |
| 6688 | * | | | | | | | | | | |
| 6689 | * | | | | | | | | +-> next hdr_end <--+ |
| 6690 | * | | | | | | | +------------> scav |
| 6691 | * | | | | | | +--------------> val_end |
| 6692 | * | | | | | +--------------------> val_beg |
| 6693 | * | | | | +----------------------> equal |
| 6694 | * | | | +------------------------> att_end |
| 6695 | * | | +----------------------------> att_beg |
| 6696 | * | +------------------------------> prev |
| 6697 | * +-----------------------------------------> hdr_beg |
| 6698 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6699 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6700 | for (; prev < hdr_end; prev = next) { |
| 6701 | /* Iterate through all cookies on this line */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6702 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6703 | /* find att_beg */ |
| 6704 | att_beg = prev + 1; |
| 6705 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 6706 | att_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6707 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6708 | /* find att_end : this is the first character after the last non |
| 6709 | * space before the equal. It may be equal to hdr_end. |
| 6710 | */ |
| 6711 | equal = att_end = att_beg; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6712 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6713 | while (equal < hdr_end) { |
| 6714 | if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ',')) |
| 6715 | break; |
| 6716 | if (http_is_spht[(unsigned char)*equal++]) |
| 6717 | continue; |
| 6718 | att_end = equal; |
| 6719 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6720 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6721 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 6722 | * is between <att_beg> and <equal>, both may be identical. |
| 6723 | */ |
| 6724 | |
| 6725 | /* look for end of cookie if there is an equal sign */ |
| 6726 | if (equal < hdr_end && *equal == '=') { |
| 6727 | /* look for the beginning of the value */ |
| 6728 | val_beg = equal + 1; |
| 6729 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 6730 | val_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6731 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6732 | /* find the end of the value, respecting quotes */ |
| 6733 | next = find_cookie_value_end(val_beg, hdr_end); |
| 6734 | |
| 6735 | /* make val_end point to the first white space or delimitor after the value */ |
| 6736 | val_end = next; |
| 6737 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 6738 | val_end--; |
| 6739 | } else { |
| 6740 | /* <equal> points to next comma, semi-colon or EOL */ |
| 6741 | val_beg = val_end = next = equal; |
| 6742 | } |
| 6743 | |
| 6744 | if (next < hdr_end) { |
| 6745 | /* Set-Cookie2 supports multiple cookies, and <next> points to |
| 6746 | * a colon or semi-colon before the end. So skip all attr-value |
| 6747 | * pairs and look for the next comma. For Set-Cookie, since |
| 6748 | * commas are permitted in values, skip to the end. |
| 6749 | */ |
| 6750 | if (is_cookie2) |
| 6751 | next = find_hdr_value_end(next, hdr_end); |
| 6752 | else |
| 6753 | next = hdr_end; |
| 6754 | } |
| 6755 | |
| 6756 | /* Now everything is as on the diagram above */ |
| 6757 | |
| 6758 | /* Ignore cookies with no equal sign */ |
| 6759 | if (equal == val_end) |
| 6760 | continue; |
| 6761 | |
| 6762 | /* If there are spaces around the equal sign, we need to |
| 6763 | * strip them otherwise we'll get trouble for cookie captures, |
| 6764 | * or even for rewrites. Since this happens extremely rarely, |
| 6765 | * it does not hurt performance. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6766 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6767 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 6768 | int stripped_before = 0; |
| 6769 | int stripped_after = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6770 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6771 | if (att_end != equal) { |
| 6772 | stripped_before = buffer_replace2(res, att_end, equal, NULL, 0); |
| 6773 | equal += stripped_before; |
| 6774 | val_beg += stripped_before; |
| 6775 | } |
| 6776 | |
| 6777 | if (val_beg > equal + 1) { |
| 6778 | stripped_after = buffer_replace2(res, equal + 1, val_beg, NULL, 0); |
| 6779 | val_beg += stripped_after; |
| 6780 | stripped_before += stripped_after; |
| 6781 | } |
| 6782 | |
| 6783 | val_end += stripped_before; |
| 6784 | next += stripped_before; |
| 6785 | hdr_end += stripped_before; |
| 6786 | hdr_next += stripped_before; |
| 6787 | cur_hdr->len += stripped_before; |
Willy Tarreau | 1fc1f45 | 2011-04-07 22:35:37 +0200 | [diff] [blame] | 6788 | http_msg_move_end(&txn->rsp, stripped_before); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6789 | } |
| 6790 | |
| 6791 | /* First, let's see if we want to capture this cookie. We check |
| 6792 | * that we don't already have a server side cookie, because we |
| 6793 | * can only capture one. Also as an optimisation, we ignore |
| 6794 | * cookies shorter than the declared name. |
| 6795 | */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 6796 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 6797 | txn->srv_cookie == NULL && |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6798 | (val_end - att_beg >= t->fe->capture_namelen) && |
| 6799 | memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
| 6800 | int log_len = val_end - att_beg; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 6801 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6802 | Alert("HTTP logging : out of memory.\n"); |
| 6803 | } |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 6804 | else { |
| 6805 | if (log_len > t->fe->capture_len) |
| 6806 | log_len = t->fe->capture_len; |
| 6807 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 6808 | txn->srv_cookie[log_len] = 0; |
| 6809 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6810 | } |
| 6811 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6812 | srv = target_srv(&t->target); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6813 | /* now check if we need to process it for persistence */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6814 | if (!(t->flags & SN_IGNORE_PRST) && |
| 6815 | (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 6816 | (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) { |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 6817 | /* assume passive cookie by default */ |
| 6818 | txn->flags &= ~TX_SCK_MASK; |
| 6819 | txn->flags |= TX_SCK_FOUND; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6820 | |
| 6821 | /* If the cookie is in insert mode on a known server, we'll delete |
| 6822 | * this occurrence because we'll insert another one later. |
| 6823 | * 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] | 6824 | * a direct access. |
| 6825 | */ |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 6826 | if (t->be->options2 & PR_O2_COOK_PSV) { |
| 6827 | /* The "preserve" flag was set, we don't want to touch the |
| 6828 | * server's cookie. |
| 6829 | */ |
| 6830 | } |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6831 | else if ((srv && (t->be->options & PR_O_COOK_INS)) || |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6832 | ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6833 | /* this cookie must be deleted */ |
| 6834 | if (*prev == ':' && next == hdr_end) { |
| 6835 | /* whole header */ |
| 6836 | delta = buffer_replace2(res, hdr_beg, hdr_next, NULL, 0); |
| 6837 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6838 | txn->hdr_idx.used--; |
| 6839 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6840 | cur_idx = old_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6841 | hdr_next += delta; |
| 6842 | http_msg_move_end(&txn->rsp, delta); |
| 6843 | /* note: while both invalid now, <next> and <hdr_end> |
| 6844 | * are still equal, so the for() will stop as expected. |
| 6845 | */ |
| 6846 | } else { |
| 6847 | /* just remove the value */ |
| 6848 | int delta = del_hdr_value(res, &prev, next); |
| 6849 | next = prev; |
| 6850 | hdr_end += delta; |
| 6851 | hdr_next += delta; |
| 6852 | cur_hdr->len += delta; |
| 6853 | http_msg_move_end(&txn->rsp, delta); |
| 6854 | } |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 6855 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6856 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6857 | /* and go on with next cookie */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6858 | } |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6859 | else if (srv && srv->cookie && (t->be->options & PR_O_COOK_RW)) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6860 | /* replace bytes val_beg->val_end with the cookie name associated |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6861 | * with this server since we know it. |
| 6862 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6863 | delta = buffer_replace2(res, val_beg, val_end, srv->cookie, srv->cklen); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6864 | next += delta; |
| 6865 | hdr_end += delta; |
| 6866 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6867 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6868 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6869 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 6870 | txn->flags &= ~TX_SCK_MASK; |
| 6871 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6872 | } |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6873 | else if (srv && srv && (t->be->options & PR_O_COOK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6874 | /* insert the cookie name associated with this server |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6875 | * before existing cookie, and insert a delimiter between them.. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6876 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6877 | delta = buffer_replace2(res, val_beg, val_beg, srv->cookie, srv->cklen + 1); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6878 | next += delta; |
| 6879 | hdr_end += delta; |
| 6880 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6881 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6882 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6883 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6884 | val_beg[srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 6885 | txn->flags &= ~TX_SCK_MASK; |
| 6886 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6887 | } |
| 6888 | } |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 6889 | /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */ |
| 6890 | else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6891 | int cmp_len, value_len; |
| 6892 | char *value_begin; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6893 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6894 | if (t->be->options2 & PR_O2_AS_PFX) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6895 | cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len); |
| 6896 | value_begin = att_beg + t->be->appsession_name_len; |
| 6897 | 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] | 6898 | } else { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6899 | cmp_len = att_end - att_beg; |
| 6900 | value_begin = val_beg; |
| 6901 | value_len = MIN(t->be->appsession_len, val_end - val_beg); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6902 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6903 | |
Cyril Bonté | 17530c3 | 2010-04-06 21:11:10 +0200 | [diff] [blame] | 6904 | if ((cmp_len == t->be->appsession_name_len) && |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6905 | (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) { |
| 6906 | /* free a possibly previously allocated memory */ |
| 6907 | pool_free2(apools.sessid, txn->sessid); |
| 6908 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6909 | /* Store the sessid in the session for future use */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6910 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6911 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 6912 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 6913 | return; |
| 6914 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6915 | memcpy(txn->sessid, value_begin, value_len); |
| 6916 | txn->sessid[value_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6917 | } |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6918 | } |
| 6919 | /* that's done for this cookie, check the next one on the same |
| 6920 | * line when next != hdr_end (only if is_cookie2). |
| 6921 | */ |
| 6922 | } |
| 6923 | /* check next header */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6924 | old_idx = cur_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6925 | } |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6926 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6927 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6928 | appsess *asession = NULL; |
| 6929 | /* only do insert, if lookup fails */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6930 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6931 | if (asession == NULL) { |
Willy Tarreau | 1fac753 | 2010-01-09 19:23:06 +0100 | [diff] [blame] | 6932 | size_t server_id_len; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6933 | if ((asession = pool_alloc2(pool2_appsess)) == NULL) { |
| 6934 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
| 6935 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n"); |
| 6936 | return; |
| 6937 | } |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 6938 | asession->serverid = NULL; /* to avoid a double free in case of allocation error */ |
| 6939 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6940 | if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) { |
| 6941 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 6942 | 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] | 6943 | t->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6944 | return; |
| 6945 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6946 | memcpy(asession->sessid, txn->sessid, t->be->appsession_len); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6947 | asession->sessid[t->be->appsession_len] = 0; |
| 6948 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6949 | server_id_len = strlen(target_srv(&t->target)->id) + 1; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6950 | if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) { |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 6951 | Alert("Not enough Memory process_srv():asession->serverid:malloc().\n"); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6952 | 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] | 6953 | t->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6954 | return; |
| 6955 | } |
| 6956 | asession->serverid[0] = '\0'; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6957 | memcpy(asession->serverid, target_srv(&t->target)->id, server_id_len); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6958 | |
| 6959 | asession->request_count = 0; |
| 6960 | appsession_hash_insert(&(t->be->htbl_proxy), asession); |
| 6961 | } |
| 6962 | |
| 6963 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 6964 | asession->request_count++; |
| 6965 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6966 | } |
| 6967 | |
| 6968 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6969 | /* |
| 6970 | * Check if response is cacheable or not. Updates t->flags. |
| 6971 | */ |
| 6972 | void check_response_for_cacheability(struct session *t, struct buffer *rtr) |
| 6973 | { |
| 6974 | struct http_txn *txn = &t->txn; |
| 6975 | char *p1, *p2; |
| 6976 | |
| 6977 | char *cur_ptr, *cur_end, *cur_next; |
| 6978 | int cur_idx; |
| 6979 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 6980 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6981 | return; |
| 6982 | |
| 6983 | /* Iterate through the headers. |
| 6984 | * we start with the start line. |
| 6985 | */ |
| 6986 | cur_idx = 0; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 6987 | cur_next = rtr->p + txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6988 | |
| 6989 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 6990 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6991 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6992 | |
| 6993 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 6994 | cur_ptr = cur_next; |
| 6995 | cur_end = cur_ptr + cur_hdr->len; |
| 6996 | cur_next = cur_end + cur_hdr->cr + 1; |
| 6997 | |
| 6998 | /* We have one full header between cur_ptr and cur_end, and the |
| 6999 | * next header starts at cur_next. We're only interested in |
| 7000 | * "Cookie:" headers. |
| 7001 | */ |
| 7002 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7003 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 7004 | if (val) { |
| 7005 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 7006 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 7007 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 7008 | return; |
| 7009 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7010 | } |
| 7011 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7012 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 7013 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7014 | continue; |
| 7015 | |
| 7016 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 7017 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7018 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7019 | |
| 7020 | if (p1 >= cur_end) /* no more info */ |
| 7021 | continue; |
| 7022 | |
| 7023 | /* p1 is at the beginning of the value */ |
| 7024 | p2 = p1; |
| 7025 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 7026 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7027 | p2++; |
| 7028 | |
| 7029 | /* we have a complete value between p1 and p2 */ |
| 7030 | if (p2 < cur_end && *p2 == '=') { |
| 7031 | /* we have something of the form no-cache="set-cookie" */ |
| 7032 | if ((cur_end - p1 >= 21) && |
| 7033 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 7034 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7035 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7036 | continue; |
| 7037 | } |
| 7038 | |
| 7039 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 7040 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
| 7041 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 7042 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 7043 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7044 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7045 | return; |
| 7046 | } |
| 7047 | |
| 7048 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7049 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7050 | continue; |
| 7051 | } |
| 7052 | } |
| 7053 | } |
| 7054 | |
| 7055 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7056 | /* |
| 7057 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 7058 | * If the server is found, it's assigned to the session. |
| 7059 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7060 | 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] | 7061 | { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7062 | char *end_params, *first_param, *cur_param, *next_param; |
| 7063 | char separator; |
| 7064 | int value_len; |
| 7065 | |
| 7066 | int mode = t->be->options2 & PR_O2_AS_M_ANY; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7067 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 7068 | if (t->be->appsession_name == NULL || |
Cyril Bonté | 17530c3 | 2010-04-06 21:11:10 +0200 | [diff] [blame] | 7069 | (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] | 7070 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7071 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7072 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7073 | first_param = NULL; |
| 7074 | switch (mode) { |
| 7075 | case PR_O2_AS_M_PP: |
| 7076 | first_param = memchr(begin, ';', len); |
| 7077 | break; |
| 7078 | case PR_O2_AS_M_QS: |
| 7079 | first_param = memchr(begin, '?', len); |
| 7080 | break; |
| 7081 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7082 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7083 | if (first_param == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7084 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7085 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7086 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7087 | switch (mode) { |
| 7088 | case PR_O2_AS_M_PP: |
| 7089 | if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) { |
| 7090 | end_params = (char *) begin + len; |
| 7091 | } |
| 7092 | separator = ';'; |
| 7093 | break; |
| 7094 | case PR_O2_AS_M_QS: |
| 7095 | end_params = (char *) begin + len; |
| 7096 | separator = '&'; |
| 7097 | break; |
| 7098 | default: |
| 7099 | /* unknown mode, shouldn't happen */ |
| 7100 | return; |
| 7101 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7102 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7103 | cur_param = next_param = end_params; |
| 7104 | while (cur_param > first_param) { |
| 7105 | cur_param--; |
| 7106 | if ((cur_param[0] == separator) || (cur_param == first_param)) { |
| 7107 | /* let's see if this is the appsession parameter */ |
| 7108 | if ((cur_param + t->be->appsession_name_len + 1 < next_param) && |
| 7109 | ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') && |
| 7110 | (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) { |
| 7111 | /* Cool... it's the right one */ |
| 7112 | cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2); |
| 7113 | value_len = MIN(t->be->appsession_len, next_param - cur_param); |
| 7114 | if (value_len > 0) { |
| 7115 | manage_client_side_appsession(t, cur_param, value_len); |
| 7116 | } |
| 7117 | break; |
| 7118 | } |
| 7119 | next_param = cur_param; |
| 7120 | } |
| 7121 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7122 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 7123 | Alert("get_srv_from_appsession\n"); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 7124 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7125 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7126 | } |
| 7127 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7128 | /* |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7129 | * 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] | 7130 | * for the current backend. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7131 | * |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7132 | * 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] | 7133 | * uri_auth field is valid. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7134 | * |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7135 | * Returns 1 if stats should be provided, otherwise 0. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7136 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7137 | 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] | 7138 | { |
| 7139 | struct uri_auth *uri_auth = backend->uri_auth; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7140 | struct http_msg *msg = &txn->req; |
| 7141 | const char *uri = msg->buf->p + msg->sol + msg->sl.rq.u; |
| 7142 | const char *h; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7143 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7144 | if (!uri_auth) |
| 7145 | return 0; |
| 7146 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7147 | 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] | 7148 | return 0; |
| 7149 | |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7150 | memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats)); |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 7151 | si->applet.ctx.stats.st_code = STAT_STATUS_INIT; |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 7152 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7153 | /* check URI size */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7154 | if (uri_auth->uri_len > msg->sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7155 | return 0; |
| 7156 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7157 | h = uri; |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 7158 | if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7159 | return 0; |
| 7160 | |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7161 | h += uri_auth->uri_len; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7162 | while (h <= uri + msg->sl.rq.u_l - 3) { |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7163 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7164 | si->applet.ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7165 | break; |
| 7166 | } |
| 7167 | h++; |
| 7168 | } |
| 7169 | |
| 7170 | if (uri_auth->refresh) { |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7171 | h = uri + uri_auth->uri_len; |
| 7172 | while (h <= uri + msg->sl.rq.u_l - 10) { |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7173 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7174 | si->applet.ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7175 | break; |
| 7176 | } |
| 7177 | h++; |
| 7178 | } |
| 7179 | } |
| 7180 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7181 | h = uri + uri_auth->uri_len; |
| 7182 | while (h <= uri + msg->sl.rq.u_l - 4) { |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 7183 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7184 | si->applet.ctx.stats.flags |= STAT_FMT_CSV; |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 7185 | break; |
| 7186 | } |
| 7187 | h++; |
| 7188 | } |
| 7189 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7190 | h = uri + uri_auth->uri_len; |
| 7191 | while (h <= uri + msg->sl.rq.u_l - 8) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7192 | if (memcmp(h, ";st=", 4) == 0) { |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 7193 | int i; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7194 | h += 4; |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 7195 | for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) { |
| 7196 | if (strncmp(stat_status_codes[i], h, 4) == 0) { |
| 7197 | si->applet.ctx.stats.st_code = i; |
| 7198 | break; |
| 7199 | } |
| 7200 | } |
| 7201 | si->applet.ctx.stats.st_code = STAT_STATUS_UNKN; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7202 | break; |
| 7203 | } |
| 7204 | h++; |
| 7205 | } |
| 7206 | |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7207 | si->applet.ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO; |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 7208 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7209 | return 1; |
| 7210 | } |
| 7211 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7212 | /* |
| 7213 | * Capture a bad request or response and archive it in the proxy's structure. |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7214 | * WARNING: it's unlikely that we've reached HTTP_MSG_BODY here so we must not |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 7215 | * assume that msg->sol = msg->buf->p + msg->som. Also, while HTTP requests |
| 7216 | * or response messages cannot wrap, this function may also be used with chunks |
| 7217 | * which may wrap. |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7218 | */ |
| 7219 | void http_capture_bad_message(struct error_snapshot *es, struct session *s, |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 7220 | struct http_msg *msg, |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 7221 | int state, struct proxy *other_end) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7222 | { |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 7223 | struct buffer *buf = msg->buf; |
| 7224 | |
Willy Tarreau | cc5cfcb | 2012-05-04 21:35:27 +0200 | [diff] [blame] | 7225 | if (bi_end(buf) <= (buf->p + msg->som)) { /* message wraps */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 7226 | int len1 = buf->size - msg->som - (buf->p - buf->data); |
Willy Tarreau | cc5cfcb | 2012-05-04 21:35:27 +0200 | [diff] [blame] | 7227 | es->len = bi_end(buf) - (buf->p + msg->som) + buf->size; |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 7228 | memcpy(es->buf, buf->p + msg->som, MIN(len1, sizeof(es->buf))); |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7229 | if (es->len > len1 && len1 < sizeof(es->buf)) |
| 7230 | memcpy(es->buf, buf->data, MIN(es->len, sizeof(es->buf)) - len1); |
| 7231 | } |
| 7232 | else { |
Willy Tarreau | cc5cfcb | 2012-05-04 21:35:27 +0200 | [diff] [blame] | 7233 | es->len = bi_end(buf) - (buf->p + msg->som); |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 7234 | memcpy(es->buf, buf->p + msg->som, MIN(es->len, sizeof(es->buf))); |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7235 | } |
| 7236 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7237 | if (msg->err_pos >= 0) |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 7238 | es->pos = msg->err_pos - msg->som - (buf->p - buf->data); |
Willy Tarreau | cc5cfcb | 2012-05-04 21:35:27 +0200 | [diff] [blame] | 7239 | else if (b_ptr(buf, msg->next) >= (buf->p + msg->som)) |
| 7240 | es->pos = b_ptr(buf, msg->next) - (buf->p + msg->som); |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7241 | else |
Willy Tarreau | cc5cfcb | 2012-05-04 21:35:27 +0200 | [diff] [blame] | 7242 | es->pos = b_ptr(buf, msg->next) - (buf->p + msg->som) + buf->size; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7243 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7244 | es->when = date; // user-visible date |
| 7245 | es->sid = s->uniq_id; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7246 | es->srv = target_srv(&s->target); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7247 | es->oe = other_end; |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 7248 | es->src = s->req->prod->addr.from; |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 7249 | es->state = state; |
| 7250 | es->flags = buf->flags; |
Willy Tarreau | 10479e4 | 2010-12-12 14:00:34 +0100 | [diff] [blame] | 7251 | es->ev_id = error_snapshot_id++; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7252 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7253 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7254 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 7255 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 7256 | * performed over the whole headers. Otherwise it must contain a valid header |
| 7257 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 7258 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 7259 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 7260 | * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is |
| 7261 | * -1. |
| 7262 | * 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] | 7263 | */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 7264 | 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] | 7265 | struct hdr_idx *idx, int occ, |
| 7266 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7267 | { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7268 | struct hdr_ctx local_ctx; |
| 7269 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 7270 | int len_hist[MAX_HDR_HISTORY]; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7271 | unsigned int hist_ptr; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7272 | int found; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7273 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7274 | if (!ctx) { |
| 7275 | local_ctx.idx = 0; |
| 7276 | ctx = &local_ctx; |
| 7277 | } |
| 7278 | |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7279 | if (occ >= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7280 | /* search from the beginning */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7281 | while (http_find_header2(hname, hlen, msg->buf->p + msg->sol, idx, ctx)) { |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7282 | occ--; |
| 7283 | if (occ <= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7284 | *vptr = ctx->line + ctx->val; |
| 7285 | *vlen = ctx->vlen; |
| 7286 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7287 | } |
| 7288 | } |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7289 | return 0; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7290 | } |
| 7291 | |
| 7292 | /* negative occurrence, we scan all the list then walk back */ |
| 7293 | if (-occ > MAX_HDR_HISTORY) |
| 7294 | return 0; |
| 7295 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7296 | found = hist_ptr = 0; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7297 | while (http_find_header2(hname, hlen, msg->buf->p + msg->sol, idx, ctx)) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7298 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 7299 | len_hist[hist_ptr] = ctx->vlen; |
| 7300 | if (++hist_ptr >= MAX_HDR_HISTORY) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7301 | hist_ptr = 0; |
| 7302 | found++; |
| 7303 | } |
| 7304 | if (-occ > found) |
| 7305 | return 0; |
| 7306 | /* OK now we have the last occurrence in [hist_ptr-1], and we need to |
| 7307 | * find occurrence -occ, so we have to check [hist_ptr+occ]. |
| 7308 | */ |
| 7309 | hist_ptr += occ; |
| 7310 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 7311 | hist_ptr -= MAX_HDR_HISTORY; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7312 | *vptr = ptr_hist[hist_ptr]; |
| 7313 | *vlen = len_hist[hist_ptr]; |
| 7314 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7315 | } |
| 7316 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 7317 | /* |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7318 | * Print a debug line with a header |
| 7319 | */ |
| 7320 | void debug_hdr(const char *dir, struct session *t, const char *start, const char *end) |
| 7321 | { |
| 7322 | int len, max; |
| 7323 | len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 7324 | dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7325 | max = end - start; |
| 7326 | UBOUND(max, sizeof(trash) - len - 1); |
| 7327 | len += strlcpy2(trash + len, start, max + 1); |
| 7328 | trash[len++] = '\n'; |
Willy Tarreau | 2133782 | 2012-04-29 14:11:38 +0200 | [diff] [blame] | 7329 | if (write(1, trash, len) < 0) /* shut gcc warning */; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7330 | } |
| 7331 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7332 | /* |
| 7333 | * Initialize a new HTTP transaction for session <s>. It is assumed that all |
| 7334 | * the required fields are properly allocated and that we only need to (re)init |
| 7335 | * them. This should be used before processing any new request. |
| 7336 | */ |
| 7337 | void http_init_txn(struct session *s) |
| 7338 | { |
| 7339 | struct http_txn *txn = &s->txn; |
| 7340 | struct proxy *fe = s->fe; |
| 7341 | |
| 7342 | txn->flags = 0; |
| 7343 | txn->status = -1; |
| 7344 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 7345 | global.req_count++; |
| 7346 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7347 | txn->cookie_first_date = 0; |
| 7348 | txn->cookie_last_date = 0; |
| 7349 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 7350 | txn->req.flags = 0; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7351 | txn->req.sol = txn->req.eol = txn->req.som = txn->req.eoh = 0; /* relative to the buffer */ |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 7352 | txn->req.next = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 7353 | txn->rsp.flags = 0; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7354 | txn->rsp.sol = txn->rsp.eol = txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */ |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 7355 | txn->rsp.next = 0; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 7356 | txn->req.chunk_len = 0LL; |
| 7357 | txn->req.body_len = 0LL; |
| 7358 | txn->rsp.chunk_len = 0LL; |
| 7359 | txn->rsp.body_len = 0LL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7360 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 7361 | txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */ |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 7362 | txn->req.buf = s->req; |
| 7363 | txn->rsp.buf = s->rep; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 7364 | |
| 7365 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7366 | |
| 7367 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 7368 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 7369 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 7370 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7371 | if (txn->req.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7372 | memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *)); |
| 7373 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7374 | if (txn->rsp.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7375 | memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
| 7376 | |
| 7377 | if (txn->hdr_idx.v) |
| 7378 | hdr_idx_init(&txn->hdr_idx); |
| 7379 | } |
| 7380 | |
| 7381 | /* to be used at the end of a transaction */ |
| 7382 | void http_end_txn(struct session *s) |
| 7383 | { |
| 7384 | struct http_txn *txn = &s->txn; |
| 7385 | |
| 7386 | /* these ones will have been dynamically allocated */ |
| 7387 | pool_free2(pool2_requri, txn->uri); |
| 7388 | pool_free2(pool2_capture, txn->cli_cookie); |
| 7389 | pool_free2(pool2_capture, txn->srv_cookie); |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7390 | pool_free2(apools.sessid, txn->sessid); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 7391 | pool_free2(pool2_uniqueid, s->unique_id); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 7392 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 7393 | s->unique_id = NULL; |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7394 | txn->sessid = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7395 | txn->uri = NULL; |
| 7396 | txn->srv_cookie = NULL; |
| 7397 | txn->cli_cookie = NULL; |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7398 | |
| 7399 | if (txn->req.cap) { |
| 7400 | struct cap_hdr *h; |
| 7401 | for (h = s->fe->req_cap; h; h = h->next) |
| 7402 | pool_free2(h->pool, txn->req.cap[h->index]); |
| 7403 | memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *)); |
| 7404 | } |
| 7405 | |
| 7406 | if (txn->rsp.cap) { |
| 7407 | struct cap_hdr *h; |
| 7408 | for (h = s->fe->rsp_cap; h; h = h->next) |
| 7409 | pool_free2(h->pool, txn->rsp.cap[h->index]); |
| 7410 | memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *)); |
| 7411 | } |
| 7412 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7413 | } |
| 7414 | |
| 7415 | /* to be used at the end of a transaction to prepare a new one */ |
| 7416 | void http_reset_txn(struct session *s) |
| 7417 | { |
| 7418 | http_end_txn(s); |
| 7419 | http_init_txn(s); |
| 7420 | |
| 7421 | s->be = s->fe; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7422 | s->logs.logwait = s->fe->to_log; |
Simon Horman | af51495 | 2011-06-21 14:34:57 +0900 | [diff] [blame] | 7423 | session_del_srv_conn(s); |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 7424 | clear_target(&s->target); |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 7425 | /* re-init store persistence */ |
| 7426 | s->store_count = 0; |
| 7427 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7428 | s->pend_pos = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7429 | |
| 7430 | s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */ |
| 7431 | |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 7432 | /* We must trim any excess data from the response buffer, because we |
| 7433 | * may have blocked an invalid response from a server that we don't |
| 7434 | * want to accidentely forward once we disable the analysers, nor do |
| 7435 | * we want those data to come along with next response. A typical |
| 7436 | * example of such data would be from a buggy server responding to |
| 7437 | * a HEAD with some data, or sending more than the advertised |
| 7438 | * content-length. |
| 7439 | */ |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 7440 | if (unlikely(s->rep->i)) |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 7441 | s->rep->i = 0; |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 7442 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7443 | s->req->rto = s->fe->timeout.client; |
Willy Tarreau | d04e858 | 2010-05-31 12:31:35 +0200 | [diff] [blame] | 7444 | s->req->wto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7445 | |
Willy Tarreau | d04e858 | 2010-05-31 12:31:35 +0200 | [diff] [blame] | 7446 | s->rep->rto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7447 | s->rep->wto = s->fe->timeout.client; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7448 | |
| 7449 | s->req->rex = TICK_ETERNITY; |
| 7450 | s->req->wex = TICK_ETERNITY; |
| 7451 | s->req->analyse_exp = TICK_ETERNITY; |
| 7452 | s->rep->rex = TICK_ETERNITY; |
| 7453 | s->rep->wex = TICK_ETERNITY; |
| 7454 | s->rep->analyse_exp = TICK_ETERNITY; |
| 7455 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7456 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 7457 | void free_http_req_rules(struct list *r) { |
| 7458 | struct http_req_rule *tr, *pr; |
| 7459 | |
| 7460 | list_for_each_entry_safe(pr, tr, r, list) { |
| 7461 | LIST_DEL(&pr->list); |
| 7462 | if (pr->action == HTTP_REQ_ACT_HTTP_AUTH) |
| 7463 | free(pr->http_auth.realm); |
| 7464 | |
| 7465 | free(pr); |
| 7466 | } |
| 7467 | } |
| 7468 | |
| 7469 | struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
| 7470 | { |
| 7471 | struct http_req_rule *rule; |
| 7472 | int cur_arg; |
| 7473 | |
| 7474 | rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule)); |
| 7475 | if (!rule) { |
| 7476 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
| 7477 | return NULL; |
| 7478 | } |
| 7479 | |
| 7480 | if (!*args[0]) { |
| 7481 | goto req_error_parsing; |
| 7482 | } else if (!strcmp(args[0], "allow")) { |
| 7483 | rule->action = HTTP_REQ_ACT_ALLOW; |
| 7484 | cur_arg = 1; |
| 7485 | } else if (!strcmp(args[0], "deny")) { |
| 7486 | rule->action = HTTP_REQ_ACT_DENY; |
| 7487 | cur_arg = 1; |
| 7488 | } else if (!strcmp(args[0], "auth")) { |
| 7489 | rule->action = HTTP_REQ_ACT_HTTP_AUTH; |
| 7490 | cur_arg = 1; |
| 7491 | |
| 7492 | while(*args[cur_arg]) { |
| 7493 | if (!strcmp(args[cur_arg], "realm")) { |
| 7494 | rule->http_auth.realm = strdup(args[cur_arg + 1]); |
| 7495 | cur_arg+=2; |
| 7496 | continue; |
| 7497 | } else |
| 7498 | break; |
| 7499 | } |
| 7500 | } else { |
| 7501 | req_error_parsing: |
| 7502 | Alert("parsing [%s:%d]: %s '%s', expects 'allow', 'deny', 'auth'.\n", |
| 7503 | file, linenum, *args[1]?"unknown parameter":"missing keyword in", args[*args[1]?1:0]); |
| 7504 | return NULL; |
| 7505 | } |
| 7506 | |
| 7507 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 7508 | struct acl_cond *cond; |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 7509 | char *errmsg = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 7510 | |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 7511 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 7512 | Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n", |
| 7513 | file, linenum, args[0], errmsg); |
| 7514 | free(errmsg); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 7515 | return NULL; |
| 7516 | } |
| 7517 | rule->cond = cond; |
| 7518 | } |
| 7519 | else if (*args[cur_arg]) { |
| 7520 | Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or" |
| 7521 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 7522 | file, linenum, args[0], args[cur_arg]); |
| 7523 | return NULL; |
| 7524 | } |
| 7525 | |
| 7526 | return rule; |
| 7527 | } |
| 7528 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7529 | /************************************************************************/ |
| 7530 | /* The code below is dedicated to ACL parsing and matching */ |
| 7531 | /************************************************************************/ |
| 7532 | |
| 7533 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7534 | /* This function ensures that the prerequisites for an L7 fetch are ready, |
| 7535 | * which means that a request or response is ready. If some data is missing, |
| 7536 | * 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] | 7537 | * to extract data from L7. If <req_vol> is non-null during a request prefetch, |
| 7538 | * another test is made to ensure the required information is not gone. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7539 | * |
| 7540 | * The function returns : |
| 7541 | * 0 if some data is missing or if the requested data cannot be fetched |
| 7542 | * -1 if it is certain that we'll never have any HTTP message there |
| 7543 | * 1 if an HTTP message is ready |
| 7544 | */ |
| 7545 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7546 | 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] | 7547 | const struct arg *args, struct sample *smp, int req_vol) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7548 | { |
| 7549 | struct http_txn *txn = l7; |
| 7550 | struct http_msg *msg = &txn->req; |
| 7551 | |
| 7552 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 7553 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 7554 | */ |
| 7555 | |
| 7556 | if (unlikely(!s || !txn)) |
| 7557 | return 0; |
| 7558 | |
| 7559 | /* Check for a dependency on a request */ |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 7560 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7561 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7562 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7563 | if (unlikely(!s->req)) |
| 7564 | return 0; |
| 7565 | |
| 7566 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) { |
| 7567 | if ((msg->msg_state == HTTP_MSG_ERROR) || (s->req->flags & BF_FULL)) { |
Willy Tarreau | 197e10a | 2012-04-23 19:18:42 +0200 | [diff] [blame] | 7568 | smp->data.uint = 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7569 | return -1; |
| 7570 | } |
| 7571 | |
| 7572 | /* Try to decode HTTP request */ |
| 7573 | if (likely(msg->next < s->req->i)) |
| 7574 | http_msg_analyzer(msg, &txn->hdr_idx); |
| 7575 | |
| 7576 | /* Still no valid request ? */ |
| 7577 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
| 7578 | if ((msg->msg_state == HTTP_MSG_ERROR) || (s->req->flags & BF_FULL)) { |
Willy Tarreau | 197e10a | 2012-04-23 19:18:42 +0200 | [diff] [blame] | 7579 | smp->data.uint = 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7580 | return -1; |
| 7581 | } |
| 7582 | /* wait for final state */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 7583 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7584 | return 0; |
| 7585 | } |
| 7586 | |
| 7587 | /* OK we just got a valid HTTP request. We have some minor |
| 7588 | * preparation to perform so that further checks can rely |
| 7589 | * on HTTP tests. |
| 7590 | */ |
| 7591 | txn->meth = find_http_meth(msg->buf->p + msg->sol, msg->sl.rq.m_l); |
| 7592 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 7593 | s->flags |= SN_REDIRECTABLE; |
| 7594 | |
| 7595 | 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] | 7596 | smp->data.uint = 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7597 | return -1; |
| 7598 | } |
| 7599 | } |
| 7600 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 7601 | if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7602 | return 0; /* data might have moved and indexes changed */ |
| 7603 | |
| 7604 | /* otherwise everything's ready for the request */ |
| 7605 | } |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 7606 | else { |
| 7607 | /* Check for a dependency on a response */ |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7608 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 7609 | return 0; |
| 7610 | } |
| 7611 | |
| 7612 | /* everything's OK */ |
| 7613 | return 1; |
| 7614 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7615 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 7616 | #define CHECK_HTTP_MESSAGE_FIRST() \ |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7617 | 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] | 7618 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 7619 | #define CHECK_HTTP_MESSAGE_FIRST_PERM() \ |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7620 | 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] | 7621 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7622 | |
| 7623 | /* 1. Check on METHOD |
| 7624 | * We use the pre-parsed method if it is known, and store its number as an |
| 7625 | * integer. If it is unknown, we use the pointer and the length. |
| 7626 | */ |
Willy Tarreau | 7dcb648 | 2012-04-27 17:52:25 +0200 | [diff] [blame] | 7627 | 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] | 7628 | { |
| 7629 | int len, meth; |
| 7630 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7631 | len = strlen(*text); |
| 7632 | meth = find_http_meth(*text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7633 | |
| 7634 | pattern->val.i = meth; |
| 7635 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7636 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 7dcb648 | 2012-04-27 17:52:25 +0200 | [diff] [blame] | 7637 | if (!pattern->ptr.str) { |
| 7638 | if (err) |
| 7639 | memprintf(err, "out of memory while loading pattern"); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7640 | return 0; |
Willy Tarreau | 7dcb648 | 2012-04-27 17:52:25 +0200 | [diff] [blame] | 7641 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7642 | pattern->len = len; |
| 7643 | } |
| 7644 | return 1; |
| 7645 | } |
| 7646 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7647 | /* This function fetches the method of current HTTP request and stores |
| 7648 | * it in the global pattern struct as a chunk. There are two possibilities : |
| 7649 | * - if the method is known (not HTTP_METH_OTHER), its identifier is stored |
| 7650 | * in <len> and <ptr> is NULL ; |
| 7651 | * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and |
| 7652 | * <len> to its length. |
| 7653 | * This is intended to be used with acl_match_meth() only. |
| 7654 | */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7655 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7656 | 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] | 7657 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7658 | { |
| 7659 | int meth; |
| 7660 | struct http_txn *txn = l7; |
| 7661 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 7662 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7663 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7664 | meth = txn->meth; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 7665 | smp->type = SMP_T_UINT; |
| 7666 | smp->data.uint = meth; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7667 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7668 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7669 | /* ensure the indexes are not affected */ |
| 7670 | return 0; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 7671 | smp->type = SMP_T_CSTR; |
| 7672 | smp->data.str.len = txn->req.sl.rq.m_l; |
| 7673 | smp->data.str.str = txn->req.buf->p + txn->req.sol; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7674 | } |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 7675 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7676 | return 1; |
| 7677 | } |
| 7678 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7679 | /* See above how the method is stored in the global pattern */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 7680 | static int acl_match_meth(struct sample *smp, struct acl_pattern *pattern) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7681 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 7682 | int icase; |
| 7683 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7684 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 7685 | if (smp->type == SMP_T_UINT) { |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7686 | /* well-known method */ |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 7687 | if (smp->data.uint == pattern->val.i) |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7688 | return ACL_PAT_PASS; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 7689 | return ACL_PAT_FAIL; |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7690 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7691 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7692 | /* Uncommon method, only HTTP_METH_OTHER is accepted now */ |
| 7693 | if (pattern->val.i != HTTP_METH_OTHER) |
| 7694 | return ACL_PAT_FAIL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7695 | |
| 7696 | /* Other method, we must compare the strings */ |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 7697 | if (pattern->len != smp->data.str.len) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 7698 | return ACL_PAT_FAIL; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 7699 | |
| 7700 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 7701 | if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0) || |
| 7702 | (!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] | 7703 | return ACL_PAT_FAIL; |
| 7704 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7705 | } |
| 7706 | |
| 7707 | /* 2. Check on Request/Status Version |
| 7708 | * We simply compare strings here. |
| 7709 | */ |
Willy Tarreau | 7dcb648 | 2012-04-27 17:52:25 +0200 | [diff] [blame] | 7710 | 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] | 7711 | { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7712 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 7dcb648 | 2012-04-27 17:52:25 +0200 | [diff] [blame] | 7713 | if (!pattern->ptr.str) { |
| 7714 | if (err) |
| 7715 | memprintf(err, "out of memory while loading pattern"); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7716 | return 0; |
Willy Tarreau | 7dcb648 | 2012-04-27 17:52:25 +0200 | [diff] [blame] | 7717 | } |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7718 | pattern->len = strlen(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7719 | return 1; |
| 7720 | } |
| 7721 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7722 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7723 | 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] | 7724 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7725 | { |
| 7726 | struct http_txn *txn = l7; |
| 7727 | char *ptr; |
| 7728 | int len; |
| 7729 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 7730 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7731 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7732 | len = txn->req.sl.rq.v_l; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7733 | ptr = txn->req.buf->p + txn->req.sol + txn->req.sl.rq.v; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7734 | |
| 7735 | while ((len-- > 0) && (*ptr++ != '/')); |
| 7736 | if (len <= 0) |
| 7737 | return 0; |
| 7738 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 7739 | smp->type = SMP_T_CSTR; |
| 7740 | smp->data.str.str = ptr; |
| 7741 | smp->data.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7742 | |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 7743 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7744 | return 1; |
| 7745 | } |
| 7746 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7747 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7748 | 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] | 7749 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7750 | { |
| 7751 | struct http_txn *txn = l7; |
| 7752 | char *ptr; |
| 7753 | int len; |
| 7754 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 7755 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7756 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7757 | len = txn->rsp.sl.st.v_l; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7758 | ptr = txn->rsp.buf->p + txn->rsp.sol; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7759 | |
| 7760 | while ((len-- > 0) && (*ptr++ != '/')); |
| 7761 | if (len <= 0) |
| 7762 | return 0; |
| 7763 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 7764 | smp->type = SMP_T_CSTR; |
| 7765 | smp->data.str.str = ptr; |
| 7766 | smp->data.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7767 | |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 7768 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7769 | return 1; |
| 7770 | } |
| 7771 | |
| 7772 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7773 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7774 | 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] | 7775 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7776 | { |
| 7777 | struct http_txn *txn = l7; |
| 7778 | char *ptr; |
| 7779 | int len; |
| 7780 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 7781 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7782 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7783 | len = txn->rsp.sl.st.c_l; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7784 | ptr = txn->rsp.buf->p + txn->rsp.sol + txn->rsp.sl.st.c; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7785 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 7786 | smp->type = SMP_T_UINT; |
| 7787 | smp->data.uint = __strl2ui(ptr, len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 7788 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7789 | return 1; |
| 7790 | } |
| 7791 | |
| 7792 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7793 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7794 | acl_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] | 7795 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7796 | { |
| 7797 | struct http_txn *txn = l7; |
| 7798 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 7799 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7800 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 7801 | smp->type = SMP_T_CSTR; |
| 7802 | smp->data.str.len = txn->req.sl.rq.u_l; |
| 7803 | smp->data.str.str = txn->req.buf->p + txn->req.sol + txn->req.sl.rq.u; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 7804 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7805 | return 1; |
| 7806 | } |
| 7807 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7808 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7809 | acl_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] | 7810 | const struct arg *args, struct sample *smp) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7811 | { |
| 7812 | struct http_txn *txn = l7; |
| 7813 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 7814 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7815 | |
| 7816 | /* Parse HTTP request */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7817 | url2sa(txn->req.buf->p + txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->addr.to); |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 7818 | if (((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_family != AF_INET) |
| 7819 | return 0; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 7820 | smp->type = SMP_T_IPV4; |
| 7821 | smp->data.ipv4 = ((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7822 | |
| 7823 | /* |
| 7824 | * If we are parsing url in frontend space, we prepare backend stage |
| 7825 | * to not parse again the same url ! optimization lazyness... |
| 7826 | */ |
| 7827 | if (px->options & PR_O_HTTP_PROXY) |
| 7828 | l4->flags |= SN_ADDR_SET; |
| 7829 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 7830 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7831 | return 1; |
| 7832 | } |
| 7833 | |
| 7834 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7835 | acl_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] | 7836 | const struct arg *args, struct sample *smp) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7837 | { |
| 7838 | struct http_txn *txn = l7; |
| 7839 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 7840 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7841 | |
| 7842 | /* Same optimization as url_ip */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7843 | url2sa(txn->req.buf->p + txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->addr.to); |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 7844 | smp->type = SMP_T_UINT; |
| 7845 | smp->data.uint = ntohs(((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_port); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7846 | |
| 7847 | if (px->options & PR_O_HTTP_PROXY) |
| 7848 | l4->flags |= SN_ADDR_SET; |
| 7849 | |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 7850 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7851 | return 1; |
| 7852 | } |
| 7853 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 7854 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 7855 | * Accepts an optional argument of type string containing the header field name, |
| 7856 | * and an optional argument of type signed or unsigned integer to request an |
| 7857 | * explicit occurrence of the header. Note that in the event of a missing name, |
| 7858 | * headers are considered from the first one. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7859 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7860 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 7861 | 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] | 7862 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7863 | { |
| 7864 | struct http_txn *txn = l7; |
| 7865 | struct hdr_idx *idx = &txn->hdr_idx; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 7866 | struct hdr_ctx *ctx = (struct hdr_ctx *)smp->ctx.a; |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7867 | 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] | 7868 | int occ = 0; |
| 7869 | const char *name_str = NULL; |
| 7870 | int name_len = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 7871 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 7872 | if (args) { |
| 7873 | if (args[0].type != ARGT_STR) |
| 7874 | return 0; |
| 7875 | name_str = args[0].data.str.str; |
| 7876 | name_len = args[0].data.str.len; |
| 7877 | |
| 7878 | if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT) |
| 7879 | occ = args[1].data.uint; |
| 7880 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 7881 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 7882 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7883 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 7884 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7885 | /* search for header from the beginning */ |
| 7886 | ctx->idx = 0; |
| 7887 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 7888 | if (!occ && !(opt & SMP_OPT_ITERATE)) |
| 7889 | /* no explicit occurrence and single fetch => last header by default */ |
| 7890 | occ = -1; |
| 7891 | |
| 7892 | if (!occ) |
| 7893 | /* prepare to report multiple occurrences for ACL fetches */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 7894 | smp->flags |= SMP_F_NOT_LAST; |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 7895 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 7896 | smp->type = SMP_T_CSTR; |
| 7897 | smp->flags |= SMP_F_VOL_HDR; |
| 7898 | 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] | 7899 | return 1; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7900 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 7901 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7902 | return 0; |
| 7903 | } |
| 7904 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7905 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 7906 | * Accepts exactly 1 argument of type string. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7907 | */ |
| 7908 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 7909 | 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] | 7910 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7911 | { |
| 7912 | struct http_txn *txn = l7; |
| 7913 | struct hdr_idx *idx = &txn->hdr_idx; |
| 7914 | struct hdr_ctx ctx; |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7915 | 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] | 7916 | int cnt; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7917 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 7918 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 7919 | return 0; |
| 7920 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 7921 | CHECK_HTTP_MESSAGE_FIRST(); |
| 7922 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7923 | ctx.idx = 0; |
| 7924 | cnt = 0; |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 7925 | while (http_find_header2(args->data.str.str, args->data.str.len, msg->buf->p + msg->sol, idx, &ctx)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7926 | cnt++; |
| 7927 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 7928 | smp->type = SMP_T_UINT; |
| 7929 | smp->data.uint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 7930 | smp->flags = SMP_F_VOL_HDR; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7931 | return 1; |
| 7932 | } |
| 7933 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 7934 | /* Fetch an HTTP header's integer value. The integer value is returned. It |
| 7935 | * takes a mandatory argument of type string and an optional one of type int |
| 7936 | * to designate a specific occurrence. It returns an unsigned integer, which |
| 7937 | * may or may not be appropriate for everything. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7938 | */ |
| 7939 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 7940 | 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] | 7941 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7942 | { |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 7943 | int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp); |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 7944 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 7945 | if (ret > 0) { |
| 7946 | smp->type = SMP_T_UINT; |
| 7947 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 7948 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7949 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 7950 | return ret; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7951 | } |
| 7952 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 7953 | /* Fetch an HTTP header's integer value. The integer value is returned. It |
| 7954 | * takes a mandatory argument of type string and an optional one of type int |
| 7955 | * to designate a specific occurrence. It returns an IPv4 address. |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 7956 | */ |
| 7957 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 7958 | 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] | 7959 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 7960 | { |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 7961 | int ret; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 7962 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 7963 | while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp)) > 0) { |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 7964 | smp->type = SMP_T_IPV4; |
| 7965 | if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 7966 | break; |
| 7967 | /* if the header doesn't match an IP address, fetch next one */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 7968 | if (!(smp->flags & SMP_F_NOT_LAST)) |
| 7969 | return 0; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 7970 | } |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 7971 | return ret; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 7972 | } |
| 7973 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 7974 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 7975 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 7976 | */ |
| 7977 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7978 | acl_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] | 7979 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 7980 | { |
| 7981 | struct http_txn *txn = l7; |
| 7982 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7983 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 7984 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7985 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7986 | end = txn->req.buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 7987 | ptr = http_get_path(txn); |
| 7988 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 7989 | return 0; |
| 7990 | |
| 7991 | /* OK, we got the '/' ! */ |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 7992 | smp->type = SMP_T_CSTR; |
| 7993 | smp->data.str.str = ptr; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 7994 | |
| 7995 | while (ptr < end && *ptr != '?') |
| 7996 | ptr++; |
| 7997 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 7998 | smp->data.str.len = ptr - smp->data.str.str; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 7999 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8000 | return 1; |
| 8001 | } |
| 8002 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8003 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8004 | 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] | 8005 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8006 | { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8007 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 8008 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 8009 | */ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8010 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8011 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8012 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8013 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 197e10a | 2012-04-23 19:18:42 +0200 | [diff] [blame] | 8014 | smp->data.uint = 1; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8015 | return 1; |
| 8016 | } |
| 8017 | |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 8018 | /* return a valid test if the current request is the first one on the connection */ |
| 8019 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8020 | 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] | 8021 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 8022 | { |
| 8023 | if (!s) |
| 8024 | return 0; |
| 8025 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8026 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 197e10a | 2012-04-23 19:18:42 +0200 | [diff] [blame] | 8027 | smp->data.uint = !(s->txn.flags & TX_NOT_FIRST); |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 8028 | return 1; |
| 8029 | } |
| 8030 | |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 8031 | /* Accepts exactly 1 argument of type userlist */ |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8032 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8033 | 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] | 8034 | const struct arg *args, struct sample *smp) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8035 | { |
| 8036 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8037 | if (!args || args->type != ARGT_USR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 8038 | return 0; |
| 8039 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 8040 | CHECK_HTTP_MESSAGE_FIRST(); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8041 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 8042 | if (!get_http_auth(l4)) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8043 | return 0; |
| 8044 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8045 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8046 | 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] | 8047 | return 1; |
| 8048 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8049 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8050 | /* Try to find the next occurrence of a cookie name in a cookie header value. |
| 8051 | * The lookup begins at <hdr>. The pointer and size of the next occurrence of |
| 8052 | * the cookie value is returned into *value and *value_l, and the function |
| 8053 | * returns a pointer to the next pointer to search from if the value was found. |
| 8054 | * Otherwise if the cookie was not found, NULL is returned and neither value |
| 8055 | * nor value_l are touched. The input <hdr> string should first point to the |
| 8056 | * header's value, and the <hdr_end> pointer must point to the first character |
| 8057 | * not part of the value. <list> must be non-zero if value may represent a list |
| 8058 | * of values (cookie headers). This makes it faster to abort parsing when no |
| 8059 | * list is expected. |
| 8060 | */ |
| 8061 | static char * |
| 8062 | extract_cookie_value(char *hdr, const char *hdr_end, |
| 8063 | char *cookie_name, size_t cookie_name_l, int list, |
Willy Tarreau | 3fb818c | 2012-04-11 17:21:08 +0200 | [diff] [blame] | 8064 | char **value, int *value_l) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8065 | { |
| 8066 | char *equal, *att_end, *att_beg, *val_beg, *val_end; |
| 8067 | char *next; |
| 8068 | |
| 8069 | /* we search at least a cookie name followed by an equal, and more |
| 8070 | * generally something like this : |
| 8071 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 8072 | */ |
| 8073 | for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) { |
| 8074 | /* Iterate through all cookies on this line */ |
| 8075 | |
| 8076 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 8077 | att_beg++; |
| 8078 | |
| 8079 | /* find att_end : this is the first character after the last non |
| 8080 | * space before the equal. It may be equal to hdr_end. |
| 8081 | */ |
| 8082 | equal = att_end = att_beg; |
| 8083 | |
| 8084 | while (equal < hdr_end) { |
| 8085 | if (*equal == '=' || *equal == ';' || (list && *equal == ',')) |
| 8086 | break; |
| 8087 | if (http_is_spht[(unsigned char)*equal++]) |
| 8088 | continue; |
| 8089 | att_end = equal; |
| 8090 | } |
| 8091 | |
| 8092 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 8093 | * is between <att_beg> and <equal>, both may be identical. |
| 8094 | */ |
| 8095 | |
| 8096 | /* look for end of cookie if there is an equal sign */ |
| 8097 | if (equal < hdr_end && *equal == '=') { |
| 8098 | /* look for the beginning of the value */ |
| 8099 | val_beg = equal + 1; |
| 8100 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 8101 | val_beg++; |
| 8102 | |
| 8103 | /* find the end of the value, respecting quotes */ |
| 8104 | next = find_cookie_value_end(val_beg, hdr_end); |
| 8105 | |
| 8106 | /* make val_end point to the first white space or delimitor after the value */ |
| 8107 | val_end = next; |
| 8108 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 8109 | val_end--; |
| 8110 | } else { |
| 8111 | val_beg = val_end = next = equal; |
| 8112 | } |
| 8113 | |
| 8114 | /* We have nothing to do with attributes beginning with '$'. However, |
| 8115 | * they will automatically be removed if a header before them is removed, |
| 8116 | * since they're supposed to be linked together. |
| 8117 | */ |
| 8118 | if (*att_beg == '$') |
| 8119 | continue; |
| 8120 | |
| 8121 | /* Ignore cookies with no equal sign */ |
| 8122 | if (equal == next) |
| 8123 | continue; |
| 8124 | |
| 8125 | /* Now we have the cookie name between att_beg and att_end, and |
| 8126 | * its value between val_beg and val_end. |
| 8127 | */ |
| 8128 | |
| 8129 | if (att_end - att_beg == cookie_name_l && |
| 8130 | memcmp(att_beg, cookie_name, cookie_name_l) == 0) { |
| 8131 | /* let's return this value and indicate where to go on from */ |
| 8132 | *value = val_beg; |
| 8133 | *value_l = val_end - val_beg; |
| 8134 | return next + 1; |
| 8135 | } |
| 8136 | |
| 8137 | /* Set-Cookie headers only have the name in the first attr=value part */ |
| 8138 | if (!list) |
| 8139 | break; |
| 8140 | } |
| 8141 | |
| 8142 | return NULL; |
| 8143 | } |
| 8144 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8145 | /* 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] | 8146 | * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the |
| 8147 | * end-of-header-value, and smp->ctx.a[2] for the hdr_idx. Depending on |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8148 | * 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] | 8149 | * 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] | 8150 | * Accepts exactly 1 argument of type string. If the input options indicate |
| 8151 | * 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] | 8152 | */ |
| 8153 | static int |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 8154 | smp_fetch_cookie_value(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8155 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8156 | { |
| 8157 | struct http_txn *txn = l7; |
| 8158 | struct hdr_idx *idx = &txn->hdr_idx; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8159 | struct hdr_ctx *ctx = (struct hdr_ctx *)&smp->ctx.a[2]; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8160 | const struct http_msg *msg; |
| 8161 | const char *hdr_name; |
| 8162 | int hdr_name_len; |
| 8163 | char *sol; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 8164 | int occ = 0; |
| 8165 | int found = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8166 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8167 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 8168 | return 0; |
| 8169 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8170 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8171 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8172 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8173 | msg = &txn->req; |
| 8174 | hdr_name = "Cookie"; |
| 8175 | hdr_name_len = 6; |
| 8176 | } else { |
| 8177 | msg = &txn->rsp; |
| 8178 | hdr_name = "Set-Cookie"; |
| 8179 | hdr_name_len = 10; |
| 8180 | } |
| 8181 | |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 8182 | if (!occ && !(opt & SMP_OPT_ITERATE)) |
| 8183 | /* no explicit occurrence and single fetch => last cookie by default */ |
| 8184 | occ = -1; |
| 8185 | |
| 8186 | /* OK so basically here, either we want only one value and it's the |
| 8187 | * last one, or we want to iterate over all of them and we fetch the |
| 8188 | * next one. |
| 8189 | */ |
| 8190 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8191 | sol = msg->buf->p + msg->sol; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8192 | if (!(smp->flags & SMP_F_NOT_LAST)) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8193 | /* search for the header from the beginning, we must first initialize |
| 8194 | * the search parameters. |
| 8195 | */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8196 | smp->ctx.a[0] = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8197 | ctx->idx = 0; |
| 8198 | } |
| 8199 | |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 8200 | smp->flags |= SMP_F_VOL_HDR; |
| 8201 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8202 | while (1) { |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8203 | /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */ |
| 8204 | if (!smp->ctx.a[0]) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8205 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx)) |
| 8206 | goto out; |
| 8207 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8208 | if (ctx->vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8209 | continue; |
| 8210 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8211 | smp->ctx.a[0] = ctx->line + ctx->val; |
| 8212 | smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8213 | } |
| 8214 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8215 | smp->type = SMP_T_CSTR; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8216 | 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] | 8217 | args->data.str.str, args->data.str.len, |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8218 | (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8219 | &smp->data.str.str, |
| 8220 | &smp->data.str.len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8221 | if (smp->ctx.a[0]) { |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 8222 | found = 1; |
| 8223 | if (occ >= 0) { |
| 8224 | /* one value was returned into smp->data.str.{str,len} */ |
| 8225 | smp->flags |= SMP_F_NOT_LAST; |
| 8226 | return 1; |
| 8227 | } |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8228 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 8229 | /* if we're looking for last occurrence, let's loop */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8230 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 8231 | /* all cookie headers and values were scanned. If we're looking for the |
| 8232 | * last occurrence, we may return it now. |
| 8233 | */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8234 | out: |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8235 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 8236 | return found; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8237 | } |
| 8238 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8239 | /* 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] | 8240 | * 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] | 8241 | * multiple cookies may be parsed on the same line. |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 8242 | * Accepts exactly 1 argument of type string. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8243 | */ |
| 8244 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8245 | 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] | 8246 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8247 | { |
| 8248 | struct http_txn *txn = l7; |
| 8249 | struct hdr_idx *idx = &txn->hdr_idx; |
| 8250 | struct hdr_ctx ctx; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8251 | const struct http_msg *msg; |
| 8252 | const char *hdr_name; |
| 8253 | int hdr_name_len; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8254 | int cnt; |
| 8255 | char *val_beg, *val_end; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8256 | char *sol; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8257 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8258 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 8259 | return 0; |
| 8260 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8261 | CHECK_HTTP_MESSAGE_FIRST(); |
| 8262 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8263 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8264 | msg = &txn->req; |
| 8265 | hdr_name = "Cookie"; |
| 8266 | hdr_name_len = 6; |
| 8267 | } else { |
| 8268 | msg = &txn->rsp; |
| 8269 | hdr_name = "Set-Cookie"; |
| 8270 | hdr_name_len = 10; |
| 8271 | } |
| 8272 | |
| 8273 | sol = msg->buf->p + msg->sol; |
Willy Tarreau | 46787ed | 2012-04-11 17:28:40 +0200 | [diff] [blame] | 8274 | val_end = val_beg = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8275 | ctx.idx = 0; |
| 8276 | cnt = 0; |
| 8277 | |
| 8278 | while (1) { |
| 8279 | /* Note: val_beg == NULL every time we need to fetch a new header */ |
| 8280 | if (!val_beg) { |
| 8281 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx)) |
| 8282 | break; |
| 8283 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8284 | if (ctx.vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8285 | continue; |
| 8286 | |
| 8287 | val_beg = ctx.line + ctx.val; |
| 8288 | val_end = val_beg + ctx.vlen; |
| 8289 | } |
| 8290 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8291 | smp->type = SMP_T_CSTR; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8292 | while ((val_beg = extract_cookie_value(val_beg, val_end, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8293 | args->data.str.str, args->data.str.len, |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8294 | (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8295 | &smp->data.str.str, |
| 8296 | &smp->data.str.len))) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8297 | cnt++; |
| 8298 | } |
| 8299 | } |
| 8300 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8301 | smp->data.uint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8302 | smp->flags |= SMP_F_VOL_HDR; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8303 | return 1; |
| 8304 | } |
| 8305 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8306 | /************************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 8307 | /* The code below is dedicated to sample fetches */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8308 | /************************************************************************/ |
| 8309 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8310 | /* |
| 8311 | * Given a path string and its length, find the position of beginning of the |
| 8312 | * query string. Returns NULL if no query string is found in the path. |
| 8313 | * |
| 8314 | * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22: |
| 8315 | * |
| 8316 | * find_query_string(path, n) points to "yo=mama;ye=daddy" string. |
| 8317 | */ |
| 8318 | static inline char *find_query_string(char *path, size_t path_l) |
| 8319 | { |
| 8320 | char *p; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 8321 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8322 | p = memchr(path, '?', path_l); |
| 8323 | return p ? p + 1 : NULL; |
| 8324 | } |
| 8325 | |
| 8326 | static inline int is_param_delimiter(char c) |
| 8327 | { |
| 8328 | return c == '&' || c == ';'; |
| 8329 | } |
| 8330 | |
| 8331 | /* |
| 8332 | * Given a url parameter, find the starting position of the first occurence, |
| 8333 | * or NULL if the parameter is not found. |
| 8334 | * |
| 8335 | * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye", |
| 8336 | * the function will return query_string+8. |
| 8337 | */ |
| 8338 | static char* |
| 8339 | find_url_param_pos(char* query_string, size_t query_string_l, |
| 8340 | char* url_param_name, size_t url_param_name_l) |
| 8341 | { |
| 8342 | char *pos, *last; |
| 8343 | |
| 8344 | pos = query_string; |
| 8345 | last = query_string + query_string_l - url_param_name_l - 1; |
| 8346 | |
| 8347 | while (pos <= last) { |
| 8348 | if (pos[url_param_name_l] == '=') { |
| 8349 | if (memcmp(pos, url_param_name, url_param_name_l) == 0) |
| 8350 | return pos; |
| 8351 | pos += url_param_name_l + 1; |
| 8352 | } |
| 8353 | while (pos <= last && !is_param_delimiter(*pos)) |
| 8354 | pos++; |
| 8355 | pos++; |
| 8356 | } |
| 8357 | return NULL; |
| 8358 | } |
| 8359 | |
| 8360 | /* |
| 8361 | * Given a url parameter name, returns its value and size into *value and |
| 8362 | * *value_l respectively, and returns non-zero. If the parameter is not found, |
| 8363 | * zero is returned and value/value_l are not touched. |
| 8364 | */ |
| 8365 | static int |
| 8366 | find_url_param_value(char* path, size_t path_l, |
| 8367 | char* url_param_name, size_t url_param_name_l, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 8368 | char** value, int* value_l) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8369 | { |
| 8370 | char *query_string, *qs_end; |
| 8371 | char *arg_start; |
| 8372 | char *value_start, *value_end; |
| 8373 | |
| 8374 | query_string = find_query_string(path, path_l); |
| 8375 | if (!query_string) |
| 8376 | return 0; |
| 8377 | |
| 8378 | qs_end = path + path_l; |
| 8379 | arg_start = find_url_param_pos(query_string, qs_end - query_string, |
| 8380 | url_param_name, url_param_name_l); |
| 8381 | if (!arg_start) |
| 8382 | return 0; |
| 8383 | |
| 8384 | value_start = arg_start + url_param_name_l + 1; |
| 8385 | value_end = value_start; |
| 8386 | |
| 8387 | while ((value_end < qs_end) && !is_param_delimiter(*value_end)) |
| 8388 | value_end++; |
| 8389 | |
| 8390 | *value = value_start; |
| 8391 | *value_l = value_end - value_start; |
Willy Tarreau | 0013433 | 2011-01-04 14:57:34 +0100 | [diff] [blame] | 8392 | return value_end != value_start; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8393 | } |
| 8394 | |
| 8395 | static int |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 8396 | smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 8397 | const struct arg *args, struct sample *smp) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8398 | { |
| 8399 | struct http_txn *txn = l7; |
| 8400 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 8401 | |
| 8402 | if (!args || args->type != ARGT_STR) |
| 8403 | return 0; |
| 8404 | |
| 8405 | CHECK_HTTP_MESSAGE_FIRST(); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8406 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 8407 | if (!find_url_param_value(msg->buf->p + msg->sol + msg->sl.rq.u, msg->sl.rq.u_l, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 8408 | args->data.str.str, args->data.str.len, |
| 8409 | &smp->data.str.str, &smp->data.str.len)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8410 | return 0; |
| 8411 | |
Willy Tarreau | b8c8f1f | 2012-04-23 22:38:26 +0200 | [diff] [blame] | 8412 | smp->type = SMP_T_CSTR; |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 8413 | smp->flags = SMP_F_VOL_1ST; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8414 | return 1; |
| 8415 | } |
| 8416 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8417 | /* This function is used to validate the arguments passed to any "hdr" fetch |
| 8418 | * keyword. These keywords support an optional positive or negative occurrence |
| 8419 | * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It |
| 8420 | * is assumed that the types are already the correct ones. Returns 0 on error, |
| 8421 | * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an |
| 8422 | * error message in case of error, that the caller is responsible for freeing. |
| 8423 | * The initial location must either be freeable or NULL. |
| 8424 | */ |
| 8425 | static int val_hdr(struct arg *arg, char **err_msg) |
| 8426 | { |
| 8427 | if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) { |
| 8428 | if (err_msg) |
| 8429 | memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY); |
| 8430 | return 0; |
| 8431 | } |
| 8432 | return 1; |
| 8433 | } |
| 8434 | |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8435 | /************************************************************************/ |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 8436 | /* All supported ACL keywords must be declared here. */ |
| 8437 | /************************************************************************/ |
| 8438 | |
| 8439 | /* Note: must not be declared <const> as its list will be overwritten. |
| 8440 | * Please take care of keeping this list alphabetically sorted. |
| 8441 | */ |
| 8442 | static struct acl_kw_list acl_kws = {{ },{ |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 8443 | { "cook", acl_parse_str, smp_fetch_cookie_value, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(0,STR) }, |
| 8444 | { "cook_beg", acl_parse_str, smp_fetch_cookie_value, acl_match_beg, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 8445 | { "cook_cnt", acl_parse_int, acl_fetch_cookie_cnt, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) }, |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 8446 | { "cook_dir", acl_parse_str, smp_fetch_cookie_value, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) }, |
| 8447 | { "cook_dom", acl_parse_str, smp_fetch_cookie_value, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) }, |
| 8448 | { "cook_end", acl_parse_str, smp_fetch_cookie_value, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) }, |
| 8449 | { "cook_len", acl_parse_int, smp_fetch_cookie_value, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) }, |
| 8450 | { "cook_reg", acl_parse_reg, smp_fetch_cookie_value, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) }, |
| 8451 | { "cook_sub", acl_parse_str, smp_fetch_cookie_value, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 8452 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8453 | { "hdr", acl_parse_str, smp_fetch_hdr, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr }, |
| 8454 | { "hdr_beg", acl_parse_str, smp_fetch_hdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 8455 | { "hdr_cnt", acl_parse_int, smp_fetch_hdr_cnt, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) }, |
| 8456 | { "hdr_dir", acl_parse_str, smp_fetch_hdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 8457 | { "hdr_dom", acl_parse_str, smp_fetch_hdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 8458 | { "hdr_end", acl_parse_str, smp_fetch_hdr, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 8459 | { "hdr_ip", acl_parse_ip, smp_fetch_hdr_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr }, |
| 8460 | { "hdr_len", acl_parse_int, smp_fetch_hdr, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 8461 | { "hdr_reg", acl_parse_reg, smp_fetch_hdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 8462 | { "hdr_sub", acl_parse_str, smp_fetch_hdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 8463 | { "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] | 8464 | |
| 8465 | { "http_auth", acl_parse_nothing, acl_fetch_http_auth, acl_match_nothing, ACL_USE_L7REQ_VOLATILE, ARG1(0,USR) }, |
| 8466 | { "http_auth_group", acl_parse_strcat, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_VOLATILE, ARG1(0,USR) }, |
| 8467 | { "http_first_req", acl_parse_nothing, acl_fetch_http_first_req, acl_match_nothing, ACL_USE_L7REQ_PERMANENT, 0 }, |
| 8468 | |
| 8469 | { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT, 0 }, |
| 8470 | |
| 8471 | { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 }, |
| 8472 | { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 8473 | { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 8474 | { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 8475 | { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 8476 | { "path_len", acl_parse_int, acl_fetch_path, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 8477 | { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 8478 | { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 8479 | |
| 8480 | { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT, 0 }, |
| 8481 | { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 }, |
| 8482 | { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, 0 }, |
| 8483 | |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 8484 | { "scook", acl_parse_str, smp_fetch_cookie_value, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, ARG1(0,STR) }, |
| 8485 | { "scook_beg", acl_parse_str, smp_fetch_cookie_value, acl_match_beg, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 8486 | { "scook_cnt", acl_parse_int, acl_fetch_cookie_cnt, acl_match_int, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) }, |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 8487 | { "scook_dir", acl_parse_str, smp_fetch_cookie_value, acl_match_dir, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) }, |
| 8488 | { "scook_dom", acl_parse_str, smp_fetch_cookie_value, acl_match_dom, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) }, |
| 8489 | { "scook_end", acl_parse_str, smp_fetch_cookie_value, acl_match_end, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) }, |
| 8490 | { "scook_len", acl_parse_int, smp_fetch_cookie_value, acl_match_len, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) }, |
| 8491 | { "scook_reg", acl_parse_reg, smp_fetch_cookie_value, acl_match_reg, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) }, |
| 8492 | { "scook_sub", acl_parse_str, smp_fetch_cookie_value, acl_match_sub, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 8493 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8494 | { "shdr", acl_parse_str, smp_fetch_hdr, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr }, |
| 8495 | { "shdr_beg", acl_parse_str, smp_fetch_hdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 8496 | { "shdr_cnt", acl_parse_int, smp_fetch_hdr_cnt, acl_match_int, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) }, |
| 8497 | { "shdr_dir", acl_parse_str, smp_fetch_hdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 8498 | { "shdr_dom", acl_parse_str, smp_fetch_hdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 8499 | { "shdr_end", acl_parse_str, smp_fetch_hdr, acl_match_end, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 8500 | { "shdr_ip", acl_parse_ip, smp_fetch_hdr_ip, acl_match_ip, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr }, |
| 8501 | { "shdr_len", acl_parse_int, smp_fetch_hdr, acl_match_len, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 8502 | { "shdr_reg", acl_parse_reg, smp_fetch_hdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 8503 | { "shdr_sub", acl_parse_str, smp_fetch_hdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 8504 | { "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] | 8505 | |
| 8506 | { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT, 0 }, |
| 8507 | |
| 8508 | { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 }, |
| 8509 | { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 8510 | { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 8511 | { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 8512 | { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 8513 | { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 }, |
| 8514 | { "url_len", acl_parse_int, acl_fetch_url, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 8515 | { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 8516 | { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 8517 | { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 8518 | |
| 8519 | { "urlp", acl_parse_str, smp_fetch_url_param, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(1,STR) }, |
| 8520 | { "urlp_beg", acl_parse_str, smp_fetch_url_param, acl_match_beg, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) }, |
| 8521 | { "urlp_dir", acl_parse_str, smp_fetch_url_param, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) }, |
| 8522 | { "urlp_dom", acl_parse_str, smp_fetch_url_param, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) }, |
| 8523 | { "urlp_end", acl_parse_str, smp_fetch_url_param, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) }, |
| 8524 | { "urlp_ip", acl_parse_ip, smp_fetch_url_param, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(1,STR) }, |
| 8525 | { "urlp_len", acl_parse_int, smp_fetch_url_param, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) }, |
| 8526 | { "urlp_reg", acl_parse_reg, smp_fetch_url_param, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) }, |
| 8527 | { "urlp_sub", acl_parse_str, smp_fetch_url_param, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) }, |
| 8528 | |
| 8529 | { NULL, NULL, NULL, NULL }, |
| 8530 | }}; |
| 8531 | |
| 8532 | /************************************************************************/ |
| 8533 | /* All supported pattern keywords must be declared here. */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8534 | /************************************************************************/ |
| 8535 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 8536 | static struct sample_fetch_kw_list sample_fetch_keywords = {{ },{ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8537 | { "hdr", smp_fetch_hdr, ARG2(1,STR,SINT), val_hdr, SMP_T_CSTR, SMP_CAP_REQ }, |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 8538 | { "url_param", smp_fetch_url_param, ARG1(1,STR), NULL, SMP_T_CSTR, SMP_CAP_REQ }, |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 8539 | { "cookie", smp_fetch_cookie_value, ARG1(1,STR), NULL, SMP_T_CSTR, SMP_CAP_REQ|SMP_CAP_RES }, |
| 8540 | { "set-cookie", smp_fetch_cookie_value, ARG1(1,STR), NULL, SMP_T_CSTR, SMP_CAP_RES }, /* deprecated */ |
Willy Tarreau | 9fcb984 | 2012-04-20 14:45:49 +0200 | [diff] [blame] | 8541 | { NULL, NULL, 0, 0, 0 }, |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8542 | }}; |
| 8543 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8544 | |
| 8545 | __attribute__((constructor)) |
| 8546 | static void __http_protocol_init(void) |
| 8547 | { |
| 8548 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 8549 | sample_register_fetches(&sample_fetch_keywords); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8550 | } |
| 8551 | |
| 8552 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8553 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 8554 | * Local variables: |
| 8555 | * c-indent-level: 8 |
| 8556 | * c-basic-offset: 8 |
| 8557 | * End: |
| 8558 | */ |