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> |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 45 | #include <proto/auth.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 46 | #include <proto/backend.h> |
| 47 | #include <proto/buffers.h> |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 48 | #include <proto/checks.h> |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 49 | #include <proto/dumpstats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 50 | #include <proto/fd.h> |
Willy Tarreau | 03fa5df | 2010-05-24 21:02:37 +0200 | [diff] [blame] | 51 | #include <proto/frontend.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 52 | #include <proto/log.h> |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 53 | #include <proto/hdr_idx.h> |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 54 | #include <proto/pattern.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 | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 59 | #include <proto/server.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 60 | #include <proto/session.h> |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 61 | #include <proto/stream_interface.h> |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 62 | #include <proto/stream_sock.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 63 | #include <proto/task.h> |
| 64 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 65 | const char HTTP_100[] = |
| 66 | "HTTP/1.1 100 Continue\r\n\r\n"; |
| 67 | |
| 68 | const struct chunk http_100_chunk = { |
| 69 | .str = (char *)&HTTP_100, |
| 70 | .len = sizeof(HTTP_100)-1 |
| 71 | }; |
| 72 | |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 73 | /* Warning: no "connection" header is provided with the 3xx messages below */ |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 74 | const char *HTTP_301 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 75 | "HTTP/1.1 301 Moved Permanently\r\n" |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 76 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 77 | "Content-length: 0\r\n" |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 78 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 79 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 80 | const char *HTTP_302 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 81 | "HTTP/1.1 302 Found\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 82 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 83 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 84 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 85 | |
| 86 | /* same as 302 except that the browser MUST retry with the GET method */ |
| 87 | const char *HTTP_303 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 88 | "HTTP/1.1 303 See Other\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 89 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 90 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 91 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 92 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 93 | /* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */ |
| 94 | const char *HTTP_401_fmt = |
| 95 | "HTTP/1.0 401 Unauthorized\r\n" |
| 96 | "Cache-Control: no-cache\r\n" |
| 97 | "Connection: close\r\n" |
Willy Tarreau | 791d66d | 2006-07-08 16:53:38 +0200 | [diff] [blame] | 98 | "Content-Type: text/html\r\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 99 | "WWW-Authenticate: Basic realm=\"%s\"\r\n" |
| 100 | "\r\n" |
| 101 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 102 | |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 103 | const char *HTTP_407_fmt = |
| 104 | "HTTP/1.0 407 Unauthorized\r\n" |
| 105 | "Cache-Control: no-cache\r\n" |
| 106 | "Connection: close\r\n" |
| 107 | "Content-Type: text/html\r\n" |
| 108 | "Proxy-Authenticate: Basic realm=\"%s\"\r\n" |
| 109 | "\r\n" |
| 110 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 111 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 112 | |
| 113 | const int http_err_codes[HTTP_ERR_SIZE] = { |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 114 | [HTTP_ERR_200] = 200, /* used by "monitor-uri" */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 115 | [HTTP_ERR_400] = 400, |
| 116 | [HTTP_ERR_403] = 403, |
| 117 | [HTTP_ERR_408] = 408, |
| 118 | [HTTP_ERR_500] = 500, |
| 119 | [HTTP_ERR_502] = 502, |
| 120 | [HTTP_ERR_503] = 503, |
| 121 | [HTTP_ERR_504] = 504, |
| 122 | }; |
| 123 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 124 | static const char *http_err_msgs[HTTP_ERR_SIZE] = { |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 125 | [HTTP_ERR_200] = |
| 126 | "HTTP/1.0 200 OK\r\n" |
| 127 | "Cache-Control: no-cache\r\n" |
| 128 | "Connection: close\r\n" |
| 129 | "Content-Type: text/html\r\n" |
| 130 | "\r\n" |
| 131 | "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n", |
| 132 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 133 | [HTTP_ERR_400] = |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 134 | "HTTP/1.0 400 Bad request\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 135 | "Cache-Control: no-cache\r\n" |
| 136 | "Connection: close\r\n" |
| 137 | "Content-Type: text/html\r\n" |
| 138 | "\r\n" |
| 139 | "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n", |
| 140 | |
| 141 | [HTTP_ERR_403] = |
| 142 | "HTTP/1.0 403 Forbidden\r\n" |
| 143 | "Cache-Control: no-cache\r\n" |
| 144 | "Connection: close\r\n" |
| 145 | "Content-Type: text/html\r\n" |
| 146 | "\r\n" |
| 147 | "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n", |
| 148 | |
| 149 | [HTTP_ERR_408] = |
| 150 | "HTTP/1.0 408 Request Time-out\r\n" |
| 151 | "Cache-Control: no-cache\r\n" |
| 152 | "Connection: close\r\n" |
| 153 | "Content-Type: text/html\r\n" |
| 154 | "\r\n" |
| 155 | "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n", |
| 156 | |
| 157 | [HTTP_ERR_500] = |
| 158 | "HTTP/1.0 500 Server Error\r\n" |
| 159 | "Cache-Control: no-cache\r\n" |
| 160 | "Connection: close\r\n" |
| 161 | "Content-Type: text/html\r\n" |
| 162 | "\r\n" |
| 163 | "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n", |
| 164 | |
| 165 | [HTTP_ERR_502] = |
| 166 | "HTTP/1.0 502 Bad Gateway\r\n" |
| 167 | "Cache-Control: no-cache\r\n" |
| 168 | "Connection: close\r\n" |
| 169 | "Content-Type: text/html\r\n" |
| 170 | "\r\n" |
| 171 | "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n", |
| 172 | |
| 173 | [HTTP_ERR_503] = |
| 174 | "HTTP/1.0 503 Service Unavailable\r\n" |
| 175 | "Cache-Control: no-cache\r\n" |
| 176 | "Connection: close\r\n" |
| 177 | "Content-Type: text/html\r\n" |
| 178 | "\r\n" |
| 179 | "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n", |
| 180 | |
| 181 | [HTTP_ERR_504] = |
| 182 | "HTTP/1.0 504 Gateway Time-out\r\n" |
| 183 | "Cache-Control: no-cache\r\n" |
| 184 | "Connection: close\r\n" |
| 185 | "Content-Type: text/html\r\n" |
| 186 | "\r\n" |
| 187 | "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n", |
| 188 | |
| 189 | }; |
| 190 | |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 191 | /* status codes available for the stats admin page (strictly 4 chars length) */ |
| 192 | const char *stat_status_codes[STAT_STATUS_SIZE] = { |
| 193 | [STAT_STATUS_DENY] = "DENY", |
| 194 | [STAT_STATUS_DONE] = "DONE", |
| 195 | [STAT_STATUS_ERRP] = "ERRP", |
| 196 | [STAT_STATUS_EXCD] = "EXCD", |
| 197 | [STAT_STATUS_NONE] = "NONE", |
| 198 | [STAT_STATUS_PART] = "PART", |
| 199 | [STAT_STATUS_UNKN] = "UNKN", |
| 200 | }; |
| 201 | |
| 202 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 203 | /* We must put the messages here since GCC cannot initialize consts depending |
| 204 | * on strlen(). |
| 205 | */ |
| 206 | struct chunk http_err_chunks[HTTP_ERR_SIZE]; |
| 207 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 208 | #define FD_SETS_ARE_BITFIELDS |
| 209 | #ifdef FD_SETS_ARE_BITFIELDS |
| 210 | /* |
| 211 | * This map is used with all the FD_* macros to check whether a particular bit |
| 212 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 213 | * which should be encoded. When FD_ISSET() returns non-zero, it means that the |
| 214 | * byte should be encoded. Be careful to always pass bytes from 0 to 255 |
| 215 | * exclusively to the macros. |
| 216 | */ |
| 217 | fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 218 | fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 219 | |
| 220 | #else |
| 221 | #error "Check if your OS uses bitfields for fd_sets" |
| 222 | #endif |
| 223 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 224 | void init_proto_http() |
| 225 | { |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 226 | int i; |
| 227 | char *tmp; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 228 | int msg; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 229 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 230 | for (msg = 0; msg < HTTP_ERR_SIZE; msg++) { |
| 231 | if (!http_err_msgs[msg]) { |
| 232 | Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg); |
| 233 | abort(); |
| 234 | } |
| 235 | |
| 236 | http_err_chunks[msg].str = (char *)http_err_msgs[msg]; |
| 237 | http_err_chunks[msg].len = strlen(http_err_msgs[msg]); |
| 238 | } |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 239 | |
| 240 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 241 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 242 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 243 | * printable characters above. |
| 244 | */ |
| 245 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 246 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
| 247 | for (i = 0; i < 32; i++) { |
| 248 | FD_SET(i, hdr_encode_map); |
| 249 | FD_SET(i, url_encode_map); |
| 250 | } |
| 251 | for (i = 127; i < 256; i++) { |
| 252 | FD_SET(i, hdr_encode_map); |
| 253 | FD_SET(i, url_encode_map); |
| 254 | } |
| 255 | |
| 256 | tmp = "\"#{|}"; |
| 257 | while (*tmp) { |
| 258 | FD_SET(*tmp, hdr_encode_map); |
| 259 | tmp++; |
| 260 | } |
| 261 | |
| 262 | tmp = "\"#"; |
| 263 | while (*tmp) { |
| 264 | FD_SET(*tmp, url_encode_map); |
| 265 | tmp++; |
| 266 | } |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 267 | |
| 268 | /* memory allocations */ |
| 269 | pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED); |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 270 | pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 271 | pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 272 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 273 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 274 | /* |
| 275 | * We have 26 list of methods (1 per first letter), each of which can have |
| 276 | * up to 3 entries (2 valid, 1 null). |
| 277 | */ |
| 278 | struct http_method_desc { |
| 279 | http_meth_t meth; |
| 280 | int len; |
| 281 | const char text[8]; |
| 282 | }; |
| 283 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 284 | const struct http_method_desc http_methods[26][3] = { |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 285 | ['C' - 'A'] = { |
| 286 | [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" }, |
| 287 | }, |
| 288 | ['D' - 'A'] = { |
| 289 | [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" }, |
| 290 | }, |
| 291 | ['G' - 'A'] = { |
| 292 | [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" }, |
| 293 | }, |
| 294 | ['H' - 'A'] = { |
| 295 | [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" }, |
| 296 | }, |
| 297 | ['P' - 'A'] = { |
| 298 | [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" }, |
| 299 | [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" }, |
| 300 | }, |
| 301 | ['T' - 'A'] = { |
| 302 | [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" }, |
| 303 | }, |
| 304 | /* rest is empty like this : |
| 305 | * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" }, |
| 306 | */ |
| 307 | }; |
| 308 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 309 | /* 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] | 310 | * 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] | 311 | * RFC2616 for those chars. |
| 312 | */ |
| 313 | |
| 314 | const char http_is_spht[256] = { |
| 315 | [' '] = 1, ['\t'] = 1, |
| 316 | }; |
| 317 | |
| 318 | const char http_is_crlf[256] = { |
| 319 | ['\r'] = 1, ['\n'] = 1, |
| 320 | }; |
| 321 | |
| 322 | const char http_is_lws[256] = { |
| 323 | [' '] = 1, ['\t'] = 1, |
| 324 | ['\r'] = 1, ['\n'] = 1, |
| 325 | }; |
| 326 | |
| 327 | const char http_is_sep[256] = { |
| 328 | ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1, |
| 329 | ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1, |
| 330 | ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1, |
| 331 | ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1, |
| 332 | [' '] = 1, ['\t'] = 1, ['\\'] = 1, |
| 333 | }; |
| 334 | |
| 335 | const char http_is_ctl[256] = { |
| 336 | [0 ... 31] = 1, |
| 337 | [127] = 1, |
| 338 | }; |
| 339 | |
| 340 | /* |
| 341 | * A token is any ASCII char that is neither a separator nor a CTL char. |
| 342 | * Do not overwrite values in assignment since gcc-2.95 will not handle |
| 343 | * them correctly. Instead, define every non-CTL char's status. |
| 344 | */ |
| 345 | const char http_is_token[256] = { |
| 346 | [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1, |
| 347 | ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1, |
| 348 | ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1, |
| 349 | [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0, |
| 350 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, |
| 351 | ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1, |
| 352 | ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0, |
| 353 | ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0, |
| 354 | ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1, |
| 355 | ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1, |
| 356 | ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1, |
| 357 | ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1, |
| 358 | ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1, |
| 359 | ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1, |
| 360 | ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0, |
| 361 | ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1, |
| 362 | ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1, |
| 363 | ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1, |
| 364 | ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1, |
| 365 | ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1, |
| 366 | ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1, |
| 367 | ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1, |
| 368 | ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0, |
| 369 | ['|'] = 1, ['}'] = 0, ['~'] = 1, |
| 370 | }; |
| 371 | |
| 372 | |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 373 | /* |
| 374 | * An http ver_token is any ASCII which can be found in an HTTP version, |
| 375 | * which includes 'H', 'T', 'P', '/', '.' and any digit. |
| 376 | */ |
| 377 | const char http_is_ver_token[256] = { |
| 378 | ['.'] = 1, ['/'] = 1, |
| 379 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1, |
| 380 | ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1, |
| 381 | ['H'] = 1, ['P'] = 1, ['T'] = 1, |
| 382 | }; |
| 383 | |
| 384 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 385 | /* |
Willy Tarreau | e988a79 | 2010-01-04 21:13:14 +0100 | [diff] [blame] | 386 | * Silent debug that outputs only in strace, using fd #-1. Trash is modified. |
| 387 | */ |
| 388 | #if defined(DEBUG_FSM) |
| 389 | static void http_silent_debug(int line, struct session *s) |
| 390 | { |
| 391 | int size = 0; |
| 392 | size += snprintf(trash + size, sizeof(trash) - size, |
| 393 | "[%04d] req: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p lr=%p sm=%d fw=%ld tf=%08x\n", |
| 394 | line, |
| 395 | s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers, |
| 396 | s->req->data, s->req->size, s->req->l, s->req->w, s->req->r, s->req->lr, s->req->send_max, s->req->to_forward, s->txn.flags); |
| 397 | write(-1, trash, size); |
| 398 | size = 0; |
| 399 | size += snprintf(trash + size, sizeof(trash) - size, |
| 400 | " %04d rep: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p lr=%p sm=%d fw=%ld\n", |
| 401 | line, |
| 402 | s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers, |
| 403 | s->rep->data, s->rep->size, s->rep->l, s->rep->w, s->rep->r, s->rep->lr, s->rep->send_max, s->rep->to_forward); |
| 404 | |
| 405 | write(-1, trash, size); |
| 406 | } |
| 407 | #else |
| 408 | #define http_silent_debug(l,s) do { } while (0) |
| 409 | #endif |
| 410 | |
| 411 | /* |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 412 | * Adds a header and its CRLF at the tail of buffer <b>, just before the last |
| 413 | * CRLF. Text length is measured first, so it cannot be NULL. |
| 414 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 415 | * of headers is automatically adjusted. The number of bytes added is returned |
| 416 | * on success, otherwise <0 is returned indicating an error. |
| 417 | */ |
| 418 | int http_header_add_tail(struct buffer *b, struct http_msg *msg, |
| 419 | struct hdr_idx *hdr_idx, const char *text) |
| 420 | { |
| 421 | int bytes, len; |
| 422 | |
| 423 | len = strlen(text); |
| 424 | bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len); |
| 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 | /* |
| 432 | * Adds a header and its CRLF at the tail of buffer <b>, just before the last |
| 433 | * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then |
| 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 | */ |
| 439 | int http_header_add_tail2(struct buffer *b, struct http_msg *msg, |
| 440 | struct hdr_idx *hdr_idx, const char *text, int len) |
| 441 | { |
| 442 | int bytes; |
| 443 | |
| 444 | bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len); |
| 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. |
| 595 | * The ctx is always updated accordingly, as well as buffer <buf> and HTTP |
| 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 | */ |
| 600 | int http_remove_header2(struct http_msg *msg, struct buffer *buf, |
| 601 | struct hdr_idx *idx, struct hdr_ctx *ctx) |
| 602 | { |
| 603 | int cur_idx = ctx->idx; |
| 604 | char *sol = ctx->line; |
| 605 | struct hdr_idx_elem *hdr; |
| 606 | int delta, skip_comma; |
| 607 | |
| 608 | if (!cur_idx) |
| 609 | return 0; |
| 610 | |
| 611 | hdr = &idx->v[cur_idx]; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 612 | if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 613 | /* This was the only value of the header, we must now remove it entirely. */ |
| 614 | delta = buffer_replace2(buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0); |
| 615 | http_msg_move_end(msg, delta); |
| 616 | idx->used--; |
| 617 | hdr->len = 0; /* unused entry */ |
| 618 | idx->v[ctx->prev].next = idx->v[ctx->idx].next; |
Willy Tarreau | 5c4784f | 2011-02-12 13:07:35 +0100 | [diff] [blame] | 619 | if (idx->tail == ctx->idx) |
| 620 | idx->tail = ctx->prev; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 621 | ctx->idx = ctx->prev; /* walk back to the end of previous header */ |
| 622 | ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1; |
| 623 | 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] | 624 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 625 | return ctx->idx; |
| 626 | } |
| 627 | |
| 628 | /* 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] | 629 | * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the |
| 630 | * last entry of the list, we remove the last separator. |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 631 | */ |
| 632 | |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 633 | skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 634 | delta = buffer_replace2(buf, sol + ctx->del + skip_comma, |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 635 | sol + ctx->val + ctx->vlen + ctx->tws + skip_comma, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 636 | NULL, 0); |
| 637 | hdr->len += delta; |
| 638 | http_msg_move_end(msg, delta); |
| 639 | ctx->val = ctx->del; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 640 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 641 | return ctx->idx; |
| 642 | } |
| 643 | |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 644 | /* This function handles a server error at the stream interface level. The |
| 645 | * stream interface is assumed to be already in a closed state. An optional |
| 646 | * message is copied into the input buffer, and an HTTP status code stored. |
| 647 | * 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] | 648 | * in this buffer will be lost. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 649 | */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 650 | static void http_server_error(struct session *t, struct stream_interface *si, |
| 651 | int err, int finst, int status, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 652 | { |
Willy Tarreau | d5fd51c | 2010-01-22 14:17:47 +0100 | [diff] [blame] | 653 | buffer_auto_read(si->ob); |
| 654 | buffer_abort(si->ob); |
| 655 | buffer_auto_close(si->ob); |
| 656 | buffer_erase(si->ob); |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 657 | buffer_auto_close(si->ib); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 658 | buffer_auto_read(si->ib); |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 659 | if (status > 0 && msg) { |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 660 | t->txn.status = status; |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 661 | buffer_write(si->ib, msg->str, msg->len); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 662 | } |
| 663 | if (!(t->flags & SN_ERR_MASK)) |
| 664 | t->flags |= err; |
| 665 | if (!(t->flags & SN_FINST_MASK)) |
| 666 | t->flags |= finst; |
| 667 | } |
| 668 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 669 | /* This function returns the appropriate error location for the given session |
| 670 | * and message. |
| 671 | */ |
| 672 | |
| 673 | struct chunk *error_message(struct session *s, int msgnum) |
| 674 | { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 675 | if (s->be->errmsg[msgnum].str) |
| 676 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 677 | else if (s->fe->errmsg[msgnum].str) |
| 678 | return &s->fe->errmsg[msgnum]; |
| 679 | else |
| 680 | return &http_err_chunks[msgnum]; |
| 681 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 682 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 683 | /* |
| 684 | * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text |
| 685 | * string), HTTP_METH_OTHER for unknown methods, or the identified method. |
| 686 | */ |
| 687 | static http_meth_t find_http_meth(const char *str, const int len) |
| 688 | { |
| 689 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 690 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 691 | |
| 692 | m = ((unsigned)*str - 'A'); |
| 693 | |
| 694 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 695 | for (h = http_methods[m]; h->len > 0; h++) { |
| 696 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 697 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 698 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 699 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 700 | }; |
| 701 | return HTTP_METH_OTHER; |
| 702 | } |
| 703 | return HTTP_METH_NONE; |
| 704 | |
| 705 | } |
| 706 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 707 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 708 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 709 | * It is returned otherwise. |
| 710 | */ |
| 711 | static char * |
| 712 | http_get_path(struct http_txn *txn) |
| 713 | { |
| 714 | char *ptr, *end; |
| 715 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 716 | ptr = txn->req.sol + txn->req.sl.rq.u; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 717 | end = ptr + txn->req.sl.rq.u_l; |
| 718 | |
| 719 | if (ptr >= end) |
| 720 | return NULL; |
| 721 | |
| 722 | /* RFC2616, par. 5.1.2 : |
| 723 | * Request-URI = "*" | absuri | abspath | authority |
| 724 | */ |
| 725 | |
| 726 | if (*ptr == '*') |
| 727 | return NULL; |
| 728 | |
| 729 | if (isalpha((unsigned char)*ptr)) { |
| 730 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 731 | ptr++; |
| 732 | while (ptr < end && |
| 733 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 734 | ptr++; |
| 735 | /* skip '://' */ |
| 736 | if (ptr == end || *ptr++ != ':') |
| 737 | return NULL; |
| 738 | if (ptr == end || *ptr++ != '/') |
| 739 | return NULL; |
| 740 | if (ptr == end || *ptr++ != '/') |
| 741 | return NULL; |
| 742 | } |
| 743 | /* skip [user[:passwd]@]host[:[port]] */ |
| 744 | |
| 745 | while (ptr < end && *ptr != '/') |
| 746 | ptr++; |
| 747 | |
| 748 | if (ptr == end) |
| 749 | return NULL; |
| 750 | |
| 751 | /* OK, we got the '/' ! */ |
| 752 | return ptr; |
| 753 | } |
| 754 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 755 | /* Returns a 302 for a redirectable request. This may only be called just after |
| 756 | * the stream interface has moved to SI_ST_ASS. Unprocessable requests are |
| 757 | * left unchanged and will follow normal proxy processing. |
| 758 | */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 759 | void perform_http_redirect(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 760 | { |
| 761 | struct http_txn *txn; |
| 762 | struct chunk rdr; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 763 | struct server *srv; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 764 | char *path; |
| 765 | int len; |
| 766 | |
| 767 | /* 1: create the response header */ |
| 768 | rdr.len = strlen(HTTP_302); |
| 769 | rdr.str = trash; |
Willy Tarreau | 59e0b0f | 2010-01-09 21:29:23 +0100 | [diff] [blame] | 770 | rdr.size = sizeof(trash); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 771 | memcpy(rdr.str, HTTP_302, rdr.len); |
| 772 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 773 | srv = target_srv(&s->target); |
| 774 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 775 | /* 2: add the server's prefix */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 776 | if (rdr.len + srv->rdr_len > rdr.size) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 777 | return; |
| 778 | |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 779 | /* special prefix "/" means don't change URL */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 780 | if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') { |
| 781 | memcpy(rdr.str + rdr.len, srv->rdr_pfx, srv->rdr_len); |
| 782 | rdr.len += srv->rdr_len; |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 783 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 784 | |
| 785 | /* 3: add the request URI */ |
| 786 | txn = &s->txn; |
| 787 | path = http_get_path(txn); |
| 788 | if (!path) |
| 789 | return; |
| 790 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 791 | len = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path; |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 792 | if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 793 | return; |
| 794 | |
| 795 | memcpy(rdr.str + rdr.len, path, len); |
| 796 | rdr.len += len; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 797 | |
| 798 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 799 | memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 800 | rdr.len += 29; |
| 801 | } else { |
| 802 | memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23); |
| 803 | rdr.len += 23; |
| 804 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 805 | |
| 806 | /* prepare to return without error. */ |
Willy Tarreau | 99126c3 | 2008-11-27 10:30:51 +0100 | [diff] [blame] | 807 | si->shutr(si); |
| 808 | si->shutw(si); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 809 | si->err_type = SI_ET_NONE; |
| 810 | si->err_loc = NULL; |
| 811 | si->state = SI_ST_CLO; |
| 812 | |
| 813 | /* send the message */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 814 | 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] | 815 | |
| 816 | /* 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] | 817 | if (srv) |
| 818 | srv_inc_sess_ctr(srv); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 819 | } |
| 820 | |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 821 | /* Return the error message corresponding to si->err_type. It is assumed |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 822 | * that the server side is closed. Note that err_type is actually a |
| 823 | * bitmask, where almost only aborts may be cumulated with other |
| 824 | * values. We consider that aborted operations are more important |
| 825 | * than timeouts or errors due to the fact that nobody else in the |
| 826 | * logs might explain incomplete retries. All others should avoid |
| 827 | * being cumulated. It should normally not be possible to have multiple |
| 828 | * aborts at once, but just in case, the first one in sequence is reported. |
| 829 | */ |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 830 | void http_return_srv_error(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 831 | { |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 832 | int err_type = si->err_type; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 833 | |
| 834 | if (err_type & SI_ET_QUEUE_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 835 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q, |
| 836 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 837 | else if (err_type & SI_ET_CONN_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 838 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C, |
| 839 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 840 | else if (err_type & SI_ET_QUEUE_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 841 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q, |
| 842 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 843 | else if (err_type & SI_ET_QUEUE_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 844 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q, |
| 845 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 846 | else if (err_type & SI_ET_CONN_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 847 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C, |
| 848 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 849 | else if (err_type & SI_ET_CONN_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 850 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C, |
| 851 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 852 | else /* SI_ET_CONN_OTHER and others */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 853 | http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C, |
| 854 | 500, error_message(s, HTTP_ERR_500)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 855 | } |
| 856 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 857 | extern const char sess_term_cond[8]; |
| 858 | extern const char sess_fin_state[8]; |
| 859 | extern const char *monthname[12]; |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 860 | struct pool_head *pool2_requri; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 861 | struct pool_head *pool2_capture; |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 862 | struct pool_head *pool2_uniqueid; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 863 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 864 | /* |
| 865 | * Capture headers from message starting at <som> according to header list |
| 866 | * <cap_hdr>, and fill the <idx> structure appropriately. |
| 867 | */ |
| 868 | void capture_headers(char *som, struct hdr_idx *idx, |
| 869 | char **cap, struct cap_hdr *cap_hdr) |
| 870 | { |
| 871 | char *eol, *sol, *col, *sov; |
| 872 | int cur_idx; |
| 873 | struct cap_hdr *h; |
| 874 | int len; |
| 875 | |
| 876 | sol = som + hdr_idx_first_pos(idx); |
| 877 | cur_idx = hdr_idx_first_idx(idx); |
| 878 | |
| 879 | while (cur_idx) { |
| 880 | eol = sol + idx->v[cur_idx].len; |
| 881 | |
| 882 | col = sol; |
| 883 | while (col < eol && *col != ':') |
| 884 | col++; |
| 885 | |
| 886 | sov = col + 1; |
| 887 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 888 | sov++; |
| 889 | |
| 890 | for (h = cap_hdr; h; h = h->next) { |
| 891 | if ((h->namelen == col - sol) && |
| 892 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 893 | if (cap[h->index] == NULL) |
| 894 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 895 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 896 | |
| 897 | if (cap[h->index] == NULL) { |
| 898 | Alert("HTTP capture : out of memory.\n"); |
| 899 | continue; |
| 900 | } |
| 901 | |
| 902 | len = eol - sov; |
| 903 | if (len > h->len) |
| 904 | len = h->len; |
| 905 | |
| 906 | memcpy(cap[h->index], sov, len); |
| 907 | cap[h->index][len]=0; |
| 908 | } |
| 909 | } |
| 910 | sol = eol + idx->v[cur_idx].cr + 1; |
| 911 | cur_idx = idx->v[cur_idx].next; |
| 912 | } |
| 913 | } |
| 914 | |
| 915 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 916 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 917 | */ |
| 918 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 919 | |
| 920 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 921 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 922 | */ |
| 923 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 924 | ptr++; \ |
| 925 | if (likely(ptr < end)) \ |
| 926 | goto good; \ |
| 927 | else { \ |
| 928 | state = (st); \ |
| 929 | goto http_msg_ood; \ |
| 930 | } \ |
| 931 | } while (0) |
| 932 | |
| 933 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 934 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 935 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 936 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 937 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 938 | * will give undefined results. |
| 939 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 940 | * and that msg->sol points to the beginning of the response. |
| 941 | * If a complete line is found (which implies that at least one CR or LF is |
| 942 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 943 | * returned indicating an incomplete line (which does not mean that parts have |
| 944 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 945 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 946 | * upon next call. |
| 947 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 948 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 949 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 950 | * 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] | 951 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 952 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 953 | const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf, |
| 954 | unsigned int state, const char *ptr, const char *end, |
| 955 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 956 | { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 957 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 958 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 959 | http_msg_rpver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 960 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 961 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 962 | |
| 963 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 964 | msg->sl.st.v_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 965 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 966 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 967 | state = HTTP_MSG_ERROR; |
| 968 | break; |
| 969 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 970 | case HTTP_MSG_RPVER_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 971 | http_msg_rpver_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 972 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 973 | msg->sl.st.c = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 974 | goto http_msg_rpcode; |
| 975 | } |
| 976 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 977 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 978 | /* so it's a CR/LF, this is invalid */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 979 | state = HTTP_MSG_ERROR; |
| 980 | break; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 981 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 982 | case HTTP_MSG_RPCODE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 983 | http_msg_rpcode: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 984 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 985 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 986 | |
| 987 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 988 | msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 989 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 990 | } |
| 991 | |
| 992 | /* so it's a CR/LF, so there is no reason phrase */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 993 | msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 994 | http_msg_rsp_reason: |
| 995 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 996 | msg->sl.st.r = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 997 | msg->sl.st.r_l = 0; |
| 998 | goto http_msg_rpline_eol; |
| 999 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1000 | case HTTP_MSG_RPCODE_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1001 | http_msg_rpcode_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1002 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1003 | msg->sl.st.r = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1004 | goto http_msg_rpreason; |
| 1005 | } |
| 1006 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1007 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1008 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1009 | goto http_msg_rsp_reason; |
| 1010 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1011 | case HTTP_MSG_RPREASON: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1012 | http_msg_rpreason: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1013 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1014 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1015 | msg->sl.st.r_l = (ptr - msg_buf) - msg->som - msg->sl.st.r; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1016 | http_msg_rpline_eol: |
| 1017 | /* We have seen the end of line. Note that we do not |
| 1018 | * necessarily have the \n yet, but at least we know that we |
| 1019 | * have EITHER \r OR \n, otherwise the response would not be |
| 1020 | * complete. We can then record the response length and return |
| 1021 | * to the caller which will be able to register it. |
| 1022 | */ |
| 1023 | msg->sl.st.l = ptr - msg->sol; |
| 1024 | return ptr; |
| 1025 | |
| 1026 | #ifdef DEBUG_FULL |
| 1027 | default: |
| 1028 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1029 | exit(1); |
| 1030 | #endif |
| 1031 | } |
| 1032 | |
| 1033 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1034 | /* out of valid data */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1035 | if (ret_state) |
| 1036 | *ret_state = state; |
| 1037 | if (ret_ptr) |
| 1038 | *ret_ptr = (char *)ptr; |
| 1039 | return NULL; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1040 | } |
| 1041 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1042 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1043 | * This function parses a request line between <ptr> and <end>, starting with |
| 1044 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1045 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1046 | * will give undefined results. |
| 1047 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1048 | * and that msg->sol points to the beginning of the request. |
| 1049 | * If a complete line is found (which implies that at least one CR or LF is |
| 1050 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1051 | * returned indicating an incomplete line (which does not mean that parts have |
| 1052 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1053 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1054 | * upon next call. |
| 1055 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1056 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1057 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1058 | * 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] | 1059 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1060 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1061 | const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf, |
| 1062 | unsigned int state, const char *ptr, const char *end, |
| 1063 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1064 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1065 | switch (state) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1066 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1067 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1068 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1069 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1070 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1071 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1072 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1073 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1074 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1075 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1076 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1077 | /* HTTP 0.9 request */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1078 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1079 | http_msg_req09_uri: |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1080 | msg->sl.rq.u = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1081 | http_msg_req09_uri_e: |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1082 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1083 | http_msg_req09_ver: |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1084 | msg->sl.rq.v = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1085 | msg->sl.rq.v_l = 0; |
| 1086 | goto http_msg_rqline_eol; |
| 1087 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1088 | state = HTTP_MSG_ERROR; |
| 1089 | break; |
| 1090 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1091 | case HTTP_MSG_RQMETH_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1092 | http_msg_rqmeth_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1093 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1094 | msg->sl.rq.u = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1095 | goto http_msg_rquri; |
| 1096 | } |
| 1097 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1098 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1099 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1100 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1101 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1102 | case HTTP_MSG_RQURI: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1103 | http_msg_rquri: |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1104 | if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1105 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1106 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1107 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1108 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1109 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1110 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1111 | |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1112 | if (likely((unsigned char)*ptr >= 128)) { |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1113 | /* non-ASCII chars are forbidden unless option |
| 1114 | * accept-invalid-http-request is enabled in the frontend. |
| 1115 | * In any case, we capture the faulty char. |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1116 | */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1117 | if (msg->err_pos < -1) |
| 1118 | goto invalid_char; |
| 1119 | if (msg->err_pos == -1) |
| 1120 | msg->err_pos = ptr - msg_buf; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1121 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
| 1122 | } |
| 1123 | |
| 1124 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1125 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1126 | goto http_msg_req09_uri_e; |
| 1127 | } |
| 1128 | |
| 1129 | /* OK forbidden chars, 0..31 or 127 */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1130 | invalid_char: |
| 1131 | msg->err_pos = ptr - msg_buf; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1132 | state = HTTP_MSG_ERROR; |
| 1133 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1134 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1135 | case HTTP_MSG_RQURI_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1136 | http_msg_rquri_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1137 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1138 | msg->sl.rq.v = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1139 | goto http_msg_rqver; |
| 1140 | } |
| 1141 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1142 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1143 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1144 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1145 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1146 | case HTTP_MSG_RQVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1147 | http_msg_rqver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1148 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1149 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1150 | |
| 1151 | if (likely(HTTP_IS_CRLF(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1152 | msg->sl.rq.v_l = (ptr - msg_buf) - msg->som - msg->sl.rq.v; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1153 | http_msg_rqline_eol: |
| 1154 | /* We have seen the end of line. Note that we do not |
| 1155 | * necessarily have the \n yet, but at least we know that we |
| 1156 | * have EITHER \r OR \n, otherwise the request would not be |
| 1157 | * complete. We can then record the request length and return |
| 1158 | * to the caller which will be able to register it. |
| 1159 | */ |
| 1160 | msg->sl.rq.l = ptr - msg->sol; |
| 1161 | return ptr; |
| 1162 | } |
| 1163 | |
| 1164 | /* neither an HTTP_VER token nor a CRLF */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1165 | state = HTTP_MSG_ERROR; |
| 1166 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1167 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1168 | #ifdef DEBUG_FULL |
| 1169 | default: |
| 1170 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1171 | exit(1); |
| 1172 | #endif |
| 1173 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1174 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1175 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1176 | /* out of valid data */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1177 | if (ret_state) |
| 1178 | *ret_state = state; |
| 1179 | if (ret_ptr) |
| 1180 | *ret_ptr = (char *)ptr; |
| 1181 | return NULL; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1182 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1183 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1184 | /* |
| 1185 | * Returns the data from Authorization header. Function may be called more |
| 1186 | * than once so data is stored in txn->auth_data. When no header is found |
| 1187 | * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid |
| 1188 | * searching again for something we are unable to find anyway. |
| 1189 | */ |
| 1190 | |
| 1191 | char get_http_auth_buff[BUFSIZE]; |
| 1192 | |
| 1193 | int |
| 1194 | get_http_auth(struct session *s) |
| 1195 | { |
| 1196 | |
| 1197 | struct http_txn *txn = &s->txn; |
| 1198 | struct chunk auth_method; |
| 1199 | struct hdr_ctx ctx; |
| 1200 | char *h, *p; |
| 1201 | int len; |
| 1202 | |
| 1203 | #ifdef DEBUG_AUTH |
| 1204 | printf("Auth for session %p: %d\n", s, txn->auth.method); |
| 1205 | #endif |
| 1206 | |
| 1207 | if (txn->auth.method == HTTP_AUTH_WRONG) |
| 1208 | return 0; |
| 1209 | |
| 1210 | if (txn->auth.method) |
| 1211 | return 1; |
| 1212 | |
| 1213 | txn->auth.method = HTTP_AUTH_WRONG; |
| 1214 | |
| 1215 | ctx.idx = 0; |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 1216 | |
| 1217 | if (txn->flags & TX_USE_PX_CONN) { |
| 1218 | h = "Proxy-Authorization"; |
| 1219 | len = strlen(h); |
| 1220 | } else { |
| 1221 | h = "Authorization"; |
| 1222 | len = strlen(h); |
| 1223 | } |
| 1224 | |
| 1225 | if (!http_find_header2(h, len, txn->req.sol, &txn->hdr_idx, &ctx)) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1226 | return 0; |
| 1227 | |
| 1228 | h = ctx.line + ctx.val; |
| 1229 | |
| 1230 | p = memchr(h, ' ', ctx.vlen); |
| 1231 | if (!p || p == h) |
| 1232 | return 0; |
| 1233 | |
| 1234 | chunk_initlen(&auth_method, h, 0, p-h); |
| 1235 | chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1); |
| 1236 | |
| 1237 | if (!strncasecmp("Basic", auth_method.str, auth_method.len)) { |
| 1238 | |
| 1239 | len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len, |
| 1240 | get_http_auth_buff, BUFSIZE - 1); |
| 1241 | |
| 1242 | if (len < 0) |
| 1243 | return 0; |
| 1244 | |
| 1245 | |
| 1246 | get_http_auth_buff[len] = '\0'; |
| 1247 | |
| 1248 | p = strchr(get_http_auth_buff, ':'); |
| 1249 | |
| 1250 | if (!p) |
| 1251 | return 0; |
| 1252 | |
| 1253 | txn->auth.user = get_http_auth_buff; |
| 1254 | *p = '\0'; |
| 1255 | txn->auth.pass = p+1; |
| 1256 | |
| 1257 | txn->auth.method = HTTP_AUTH_BASIC; |
| 1258 | return 1; |
| 1259 | } |
| 1260 | |
| 1261 | return 0; |
| 1262 | } |
| 1263 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1264 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1265 | /* |
| 1266 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1267 | * depending on the initial msg->msg_state. It can be preempted everywhere |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1268 | * when data are missing and recalled at the exact same location with no |
| 1269 | * information loss. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1270 | * 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] | 1271 | * fields. Note that msg->som and msg->sol will be initialized after completing |
| 1272 | * the first state, so that none of the msg pointers has to be initialized |
| 1273 | * prior to the first call. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1274 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1275 | void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx) |
| 1276 | { |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1277 | unsigned int state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1278 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1279 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1280 | state = msg->msg_state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1281 | ptr = buf->lr; |
| 1282 | end = buf->r; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1283 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1284 | if (unlikely(ptr >= end)) |
| 1285 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1286 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1287 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1288 | /* |
| 1289 | * First, states that are specific to the response only. |
| 1290 | * We check them first so that request and headers are |
| 1291 | * closer to each other (accessed more often). |
| 1292 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1293 | case HTTP_MSG_RPBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1294 | http_msg_rpbefore: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1295 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1296 | /* we have a start of message, but we have to check |
| 1297 | * first if we need to remove some CRLF. We can only |
| 1298 | * do this when send_max=0. |
| 1299 | */ |
| 1300 | char *beg = buf->w + buf->send_max; |
| 1301 | if (beg >= buf->data + buf->size) |
| 1302 | beg -= buf->size; |
| 1303 | if (unlikely(ptr != beg)) { |
| 1304 | if (buf->send_max) |
| 1305 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1306 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1307 | buffer_ignore(buf, ptr - beg); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1308 | } |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1309 | msg->som = ptr - buf->data; |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 1310 | msg->sol = ptr; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1311 | hdr_idx_init(idx); |
| 1312 | state = HTTP_MSG_RPVER; |
| 1313 | goto http_msg_rpver; |
| 1314 | } |
| 1315 | |
| 1316 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1317 | goto http_msg_invalid; |
| 1318 | |
| 1319 | if (unlikely(*ptr == '\n')) |
| 1320 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1321 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1322 | /* stop here */ |
| 1323 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1324 | case HTTP_MSG_RPBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1325 | http_msg_rpbefore_cr: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1326 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1327 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1328 | /* stop here */ |
| 1329 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1330 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1331 | http_msg_rpver: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1332 | case HTTP_MSG_RPVER_SP: |
| 1333 | case HTTP_MSG_RPCODE: |
| 1334 | case HTTP_MSG_RPCODE_SP: |
| 1335 | case HTTP_MSG_RPREASON: |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1336 | ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1337 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1338 | if (unlikely(!ptr)) |
| 1339 | return; |
| 1340 | |
| 1341 | /* we have a full response and we know that we have either a CR |
| 1342 | * or an LF at <ptr>. |
| 1343 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1344 | //fprintf(stderr,"som=%d rq.l=%d *ptr=0x%02x\n", msg->som, msg->sl.st.l, *ptr); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1345 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1346 | |
| 1347 | msg->sol = ptr; |
| 1348 | if (likely(*ptr == '\r')) |
| 1349 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1350 | goto http_msg_rpline_end; |
| 1351 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1352 | case HTTP_MSG_RPLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1353 | http_msg_rpline_end: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1354 | /* msg->sol must point to the first of CR or LF. */ |
| 1355 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1356 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1357 | /* stop here */ |
| 1358 | |
| 1359 | /* |
| 1360 | * Second, states that are specific to the request only |
| 1361 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1362 | case HTTP_MSG_RQBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1363 | http_msg_rqbefore: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1364 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1365 | /* we have a start of message, but we have to check |
| 1366 | * first if we need to remove some CRLF. We can only |
| 1367 | * do this when send_max=0. |
| 1368 | */ |
| 1369 | char *beg = buf->w + buf->send_max; |
| 1370 | if (beg >= buf->data + buf->size) |
| 1371 | beg -= buf->size; |
| 1372 | if (likely(ptr != beg)) { |
| 1373 | if (buf->send_max) |
| 1374 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1375 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1376 | buffer_ignore(buf, ptr - beg); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1377 | } |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1378 | msg->som = ptr - buf->data; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1379 | msg->sol = ptr; |
Willy Tarreau | f0d058e | 2007-01-25 12:03:42 +0100 | [diff] [blame] | 1380 | /* we will need this when keep-alive will be supported |
| 1381 | hdr_idx_init(idx); |
| 1382 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1383 | state = HTTP_MSG_RQMETH; |
| 1384 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1385 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1386 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1387 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1388 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1389 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1390 | if (unlikely(*ptr == '\n')) |
| 1391 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1392 | 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] | 1393 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1394 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1395 | case HTTP_MSG_RQBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1396 | http_msg_rqbefore_cr: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1397 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1398 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1399 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1400 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1401 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1402 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1403 | case HTTP_MSG_RQMETH_SP: |
| 1404 | case HTTP_MSG_RQURI: |
| 1405 | case HTTP_MSG_RQURI_SP: |
| 1406 | case HTTP_MSG_RQVER: |
| 1407 | ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1408 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1409 | if (unlikely(!ptr)) |
| 1410 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1411 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1412 | /* we have a full request and we know that we have either a CR |
| 1413 | * or an LF at <ptr>. |
| 1414 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1415 | //fprintf(stderr,"som=%d rq.l=%d *ptr=0x%02x\n", msg->som, msg->sl.rq.l, *ptr); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1416 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1417 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1418 | msg->sol = ptr; |
| 1419 | if (likely(*ptr == '\r')) |
| 1420 | 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] | 1421 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1422 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1423 | case HTTP_MSG_RQLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1424 | http_msg_rqline_end: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1425 | /* check for HTTP/0.9 request : no version information available. |
| 1426 | * msg->sol must point to the first of CR or LF. |
| 1427 | */ |
| 1428 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1429 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1430 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1431 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1432 | 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] | 1433 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1434 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1435 | /* |
| 1436 | * Common states below |
| 1437 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1438 | case HTTP_MSG_HDR_FIRST: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1439 | http_msg_hdr_first: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1440 | msg->sol = ptr; |
| 1441 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1442 | goto http_msg_hdr_name; |
| 1443 | } |
| 1444 | |
| 1445 | if (likely(*ptr == '\r')) |
| 1446 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1447 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1448 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1449 | case HTTP_MSG_HDR_NAME: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1450 | http_msg_hdr_name: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1451 | /* assumes msg->sol points to the first char */ |
| 1452 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1453 | 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] | 1454 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1455 | if (likely(*ptr == ':')) { |
| 1456 | msg->col = ptr - buf->data; |
| 1457 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP); |
| 1458 | } |
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 | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1471 | /* assumes msg->sol points to the first char and msg->col to the colon */ |
| 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 */ |
| 1476 | msg->sov = ptr - buf->data; |
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 */ |
| 1495 | for (; buf->data+msg->sov < ptr; msg->sov++) |
| 1496 | buf->data[msg->sov] = ' '; |
| 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 ! */ |
| 1500 | msg->eol = buf->data + 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 | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1505 | /* assumes msg->sol points to the first char, msg->col to the |
| 1506 | * colon, and msg->sov points to the first character of the |
| 1507 | * value. |
| 1508 | */ |
| 1509 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1510 | 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] | 1511 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1512 | msg->eol = ptr; |
| 1513 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1514 | * real header end in case it ends with lots of LWS, but is this |
| 1515 | * really needed ? |
| 1516 | */ |
| 1517 | if (likely(*ptr == '\r')) |
| 1518 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1519 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1520 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1521 | case HTTP_MSG_HDR_L2_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1522 | http_msg_hdr_l2_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1523 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1524 | 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] | 1525 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1526 | case HTTP_MSG_HDR_L2_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1527 | http_msg_hdr_l2_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1528 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1529 | /* LWS: replace HT,CR,LF with spaces */ |
| 1530 | for (; msg->eol < ptr; msg->eol++) |
| 1531 | *msg->eol = ' '; |
| 1532 | goto http_msg_hdr_val; |
| 1533 | } |
| 1534 | http_msg_complete_header: |
| 1535 | /* |
| 1536 | * It was a new header, so the last one is finished. |
| 1537 | * Assumes msg->sol points to the first char, msg->col to the |
| 1538 | * colon, msg->sov points to the first character of the value |
| 1539 | * and msg->eol to the first CR or LF so we know how the line |
| 1540 | * ends. We insert last header into the index. |
| 1541 | */ |
| 1542 | /* |
| 1543 | fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol); |
| 1544 | write(2, msg->sol, msg->eol-msg->sol); |
| 1545 | fprintf(stderr,"\n"); |
| 1546 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1547 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1548 | if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r', |
| 1549 | idx, idx->tail) < 0)) |
| 1550 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1551 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1552 | msg->sol = ptr; |
| 1553 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1554 | goto http_msg_hdr_name; |
| 1555 | } |
| 1556 | |
| 1557 | if (likely(*ptr == '\r')) |
| 1558 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1559 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1560 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1561 | case HTTP_MSG_LAST_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1562 | http_msg_last_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1563 | /* Assumes msg->sol points to the first of either CR or LF */ |
| 1564 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1565 | ptr++; |
| 1566 | buf->lr = ptr; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1567 | msg->col = msg->sov = buf->lr - buf->data; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1568 | msg->eoh = msg->sol - buf->data; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1569 | msg->sol = buf->data + msg->som; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1570 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1571 | return; |
Willy Tarreau | b56928a | 2012-04-16 14:51:55 +0200 | [diff] [blame] | 1572 | |
| 1573 | case HTTP_MSG_ERROR: |
| 1574 | /* this may only happen if we call http_msg_analyser() twice with an error */ |
| 1575 | break; |
| 1576 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1577 | #ifdef DEBUG_FULL |
| 1578 | default: |
| 1579 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1580 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1581 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1582 | } |
| 1583 | http_msg_ood: |
| 1584 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1585 | msg->msg_state = state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1586 | buf->lr = ptr; |
| 1587 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1588 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1589 | http_msg_invalid: |
| 1590 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1591 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1592 | buf->lr = ptr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1593 | return; |
| 1594 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1595 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1596 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 1597 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 1598 | * nothing is done and 1 is returned. |
| 1599 | */ |
| 1600 | static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn) |
| 1601 | { |
| 1602 | int delta; |
| 1603 | char *cur_end; |
| 1604 | |
| 1605 | if (msg->sl.rq.v_l != 0) |
| 1606 | return 1; |
| 1607 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1608 | cur_end = msg->sol + msg->sl.rq.l; |
| 1609 | delta = 0; |
| 1610 | |
| 1611 | if (msg->sl.rq.u_l == 0) { |
| 1612 | /* if no URI was set, add "/" */ |
| 1613 | delta = buffer_replace2(req, cur_end, cur_end, " /", 2); |
| 1614 | cur_end += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1615 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1616 | } |
| 1617 | /* add HTTP version */ |
| 1618 | delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11); |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1619 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1620 | cur_end += delta; |
| 1621 | cur_end = (char *)http_parse_reqline(msg, req->data, |
| 1622 | HTTP_MSG_RQMETH, |
| 1623 | msg->sol, cur_end + 1, |
| 1624 | NULL, NULL); |
| 1625 | if (unlikely(!cur_end)) |
| 1626 | return 0; |
| 1627 | |
| 1628 | /* we have a full HTTP/1.0 request now and we know that |
| 1629 | * we have either a CR or an LF at <ptr>. |
| 1630 | */ |
| 1631 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 1632 | return 1; |
| 1633 | } |
| 1634 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1635 | /* Parse the Connection: header of an HTTP request, looking for both "close" |
| 1636 | * and "keep-alive" values. If a buffer is provided and we already know that |
| 1637 | * some headers may safely be removed, we remove them now. The <to_del> flags |
| 1638 | * are used for that : |
| 1639 | * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses) |
| 1640 | * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1). |
| 1641 | * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was |
| 1642 | * found, and TX_CON_*_SET is adjusted depending on what is left so only |
| 1643 | * harmless combinations may be removed. Do not call that after changes have |
| 1644 | * been processed. If unused, the buffer can be NULL, and no data will be |
| 1645 | * changed. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1646 | */ |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1647 | void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, int to_del) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1648 | { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1649 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1650 | const char *hdr_val = "Connection"; |
| 1651 | int hdr_len = 10; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1652 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1653 | if (txn->flags & TX_HDR_CONN_PRS) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1654 | return; |
| 1655 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1656 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1657 | hdr_val = "Proxy-Connection"; |
| 1658 | hdr_len = 16; |
| 1659 | } |
| 1660 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1661 | ctx.idx = 0; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1662 | txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET); |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1663 | while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1664 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1665 | txn->flags |= TX_HDR_CONN_KAL; |
| 1666 | if ((to_del & 2) && buf) |
| 1667 | http_remove_header2(msg, buf, &txn->hdr_idx, &ctx); |
| 1668 | else |
| 1669 | txn->flags |= TX_CON_KAL_SET; |
| 1670 | } |
| 1671 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1672 | txn->flags |= TX_HDR_CONN_CLO; |
| 1673 | if ((to_del & 1) && buf) |
| 1674 | http_remove_header2(msg, buf, &txn->hdr_idx, &ctx); |
| 1675 | else |
| 1676 | txn->flags |= TX_CON_CLO_SET; |
| 1677 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1678 | } |
| 1679 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1680 | txn->flags |= TX_HDR_CONN_PRS; |
| 1681 | return; |
| 1682 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1683 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1684 | /* Apply desired changes on the Connection: header. Values may be removed and/or |
| 1685 | * added depending on the <wanted> flags, which are exclusively composed of |
| 1686 | * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The |
| 1687 | * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left. |
| 1688 | */ |
| 1689 | void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, int wanted) |
| 1690 | { |
| 1691 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1692 | const char *hdr_val = "Connection"; |
| 1693 | int hdr_len = 10; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1694 | |
| 1695 | ctx.idx = 0; |
| 1696 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1697 | |
| 1698 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1699 | hdr_val = "Proxy-Connection"; |
| 1700 | hdr_len = 16; |
| 1701 | } |
| 1702 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1703 | txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET); |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1704 | while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1705 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1706 | if (wanted & TX_CON_KAL_SET) |
| 1707 | txn->flags |= TX_CON_KAL_SET; |
| 1708 | else |
| 1709 | http_remove_header2(msg, buf, &txn->hdr_idx, &ctx); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1710 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1711 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1712 | if (wanted & TX_CON_CLO_SET) |
| 1713 | txn->flags |= TX_CON_CLO_SET; |
| 1714 | else |
| 1715 | http_remove_header2(msg, buf, &txn->hdr_idx, &ctx); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 1716 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1717 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1718 | |
| 1719 | if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 1720 | return; |
| 1721 | |
| 1722 | if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) { |
| 1723 | txn->flags |= TX_CON_CLO_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1724 | hdr_val = "Connection: close"; |
| 1725 | hdr_len = 17; |
| 1726 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1727 | hdr_val = "Proxy-Connection: close"; |
| 1728 | hdr_len = 23; |
| 1729 | } |
| 1730 | http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1731 | } |
| 1732 | |
| 1733 | if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) { |
| 1734 | txn->flags |= TX_CON_KAL_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1735 | hdr_val = "Connection: keep-alive"; |
| 1736 | hdr_len = 22; |
| 1737 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1738 | hdr_val = "Proxy-Connection: keep-alive"; |
| 1739 | hdr_len = 28; |
| 1740 | } |
| 1741 | http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1742 | } |
| 1743 | return; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1744 | } |
| 1745 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1746 | /* Parse the chunk size at buf->lr. Once done, it adjusts ->lr to point to the |
| 1747 | * first byte of body, and increments msg->sov by the number of bytes parsed, |
| 1748 | * so that we know we can forward between ->som and ->sov. Note that due to |
| 1749 | * possible wrapping at the end of the buffer, it is possible that msg->sov is |
| 1750 | * lower than msg->som. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1751 | * 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] | 1752 | * 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] | 1753 | */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1754 | int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1755 | { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1756 | char *ptr = buf->lr; |
| 1757 | char *end = buf->data + buf->size; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1758 | unsigned int chunk = 0; |
| 1759 | |
| 1760 | /* The chunk size is in the following form, though we are only |
| 1761 | * interested in the size and CRLF : |
| 1762 | * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF |
| 1763 | */ |
| 1764 | while (1) { |
| 1765 | int c; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1766 | if (ptr == buf->r) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1767 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1768 | c = hex2i(*ptr); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1769 | if (c < 0) /* not a hex digit anymore */ |
| 1770 | break; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1771 | if (++ptr >= end) |
| 1772 | ptr = buf->data; |
Willy Tarreau | 431946e | 2012-02-24 19:20:12 +0100 | [diff] [blame] | 1773 | if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */ |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1774 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1775 | chunk = (chunk << 4) + c; |
| 1776 | } |
| 1777 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1778 | /* empty size not allowed */ |
| 1779 | if (ptr == buf->lr) |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1780 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1781 | |
| 1782 | while (http_is_spht[(unsigned char)*ptr]) { |
| 1783 | if (++ptr >= end) |
| 1784 | ptr = buf->data; |
| 1785 | if (ptr == buf->r) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1786 | return 0; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1787 | } |
| 1788 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1789 | /* Up to there, we know that at least one byte is present at *ptr. Check |
| 1790 | * for the end of chunk size. |
| 1791 | */ |
| 1792 | while (1) { |
| 1793 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1794 | /* we now have a CR or an LF at ptr */ |
| 1795 | if (likely(*ptr == '\r')) { |
| 1796 | if (++ptr >= end) |
| 1797 | ptr = buf->data; |
| 1798 | if (ptr == buf->r) |
| 1799 | return 0; |
| 1800 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1801 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1802 | if (*ptr != '\n') |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1803 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1804 | if (++ptr >= end) |
| 1805 | ptr = buf->data; |
| 1806 | /* done */ |
| 1807 | break; |
| 1808 | } |
| 1809 | else if (*ptr == ';') { |
| 1810 | /* chunk extension, ends at next CRLF */ |
| 1811 | if (++ptr >= end) |
| 1812 | ptr = buf->data; |
| 1813 | if (ptr == buf->r) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1814 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1815 | |
| 1816 | while (!HTTP_IS_CRLF(*ptr)) { |
| 1817 | if (++ptr >= end) |
| 1818 | ptr = buf->data; |
| 1819 | if (ptr == buf->r) |
| 1820 | return 0; |
| 1821 | } |
| 1822 | /* we have a CRLF now, loop above */ |
| 1823 | continue; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1824 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1825 | else |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1826 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1827 | } |
| 1828 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1829 | /* OK we found our CRLF and now <ptr> points to the next byte, |
| 1830 | * which may or may not be present. We save that into ->lr and |
| 1831 | * ->sov. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1832 | */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1833 | msg->sov += ptr - buf->lr; |
| 1834 | buf->lr = ptr; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 1835 | msg->chunk_len = chunk; |
| 1836 | msg->body_len += chunk; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1837 | msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1838 | return 1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1839 | error: |
| 1840 | msg->err_pos = ptr - buf->data; |
| 1841 | return -1; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1842 | } |
| 1843 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1844 | /* This function skips trailers in the buffer <buf> associated with HTTP |
| 1845 | * message <msg>. The first visited position is buf->lr. If the end of |
| 1846 | * the trailers is found, it is automatically scheduled to be forwarded, |
| 1847 | * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0. |
| 1848 | * If not enough data are available, the function does not change anything |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1849 | * except maybe buf->lr and msg->sov if it could parse some lines, and returns |
| 1850 | * zero. If a parse error is encountered, the function returns < 0 and does not |
| 1851 | * change anything except maybe buf->lr and msg->sov. Note that the message |
| 1852 | * must already be in HTTP_MSG_TRAILERS state before calling this function, |
| 1853 | * which implies that all non-trailers data have already been scheduled for |
| 1854 | * forwarding, and that the difference between msg->som and msg->sov exactly |
| 1855 | * matches the length of trailers already parsed and not forwarded. It is also |
| 1856 | * important to note that this function is designed to be able to parse wrapped |
| 1857 | * headers at end of buffer. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1858 | */ |
| 1859 | int http_forward_trailers(struct buffer *buf, struct http_msg *msg) |
| 1860 | { |
| 1861 | /* we have buf->lr which points to next line. Look for CRLF. */ |
| 1862 | while (1) { |
| 1863 | char *p1 = NULL, *p2 = NULL; |
| 1864 | char *ptr = buf->lr; |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1865 | int bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1866 | |
| 1867 | /* scan current line and stop at LF or CRLF */ |
| 1868 | while (1) { |
| 1869 | if (ptr == buf->r) |
| 1870 | return 0; |
| 1871 | |
| 1872 | if (*ptr == '\n') { |
| 1873 | if (!p1) |
| 1874 | p1 = ptr; |
| 1875 | p2 = ptr; |
| 1876 | break; |
| 1877 | } |
| 1878 | |
| 1879 | if (*ptr == '\r') { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1880 | if (p1) { |
| 1881 | msg->err_pos = ptr - buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1882 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1883 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1884 | p1 = ptr; |
| 1885 | } |
| 1886 | |
| 1887 | ptr++; |
| 1888 | if (ptr >= buf->data + buf->size) |
| 1889 | ptr = buf->data; |
| 1890 | } |
| 1891 | |
| 1892 | /* after LF; point to beginning of next line */ |
| 1893 | p2++; |
| 1894 | if (p2 >= buf->data + buf->size) |
| 1895 | p2 = buf->data; |
| 1896 | |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1897 | bytes = p2 - buf->lr; |
| 1898 | if (bytes < 0) |
| 1899 | bytes += buf->size; |
| 1900 | |
| 1901 | /* schedule this line for forwarding */ |
| 1902 | msg->sov += bytes; |
| 1903 | if (msg->sov >= buf->size) |
| 1904 | msg->sov -= buf->size; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1905 | |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1906 | if (p1 == buf->lr) { |
| 1907 | /* LF/CRLF at beginning of line => end of trailers at p2. |
| 1908 | * Everything was scheduled for forwarding, there's nothing |
| 1909 | * left from this message. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 1910 | */ |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1911 | buf->lr = p2; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1912 | msg->msg_state = HTTP_MSG_DONE; |
| 1913 | return 1; |
| 1914 | } |
| 1915 | /* OK, next line then */ |
| 1916 | buf->lr = p2; |
| 1917 | } |
| 1918 | } |
| 1919 | |
| 1920 | /* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or |
| 1921 | * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov, |
| 1922 | * ->som, buf->lr in order to include this part into the next forwarding phase. |
| 1923 | * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If |
| 1924 | * not enough data are available, the function does not change anything and |
| 1925 | * returns zero. If a parse error is encountered, the function returns < 0 and |
| 1926 | * does not change anything. Note: this function is designed to parse wrapped |
| 1927 | * CRLF at the end of the buffer. |
| 1928 | */ |
| 1929 | int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg) |
| 1930 | { |
| 1931 | char *ptr; |
| 1932 | int bytes; |
| 1933 | |
| 1934 | /* NB: we'll check data availabilty at the end. It's not a |
| 1935 | * problem because whatever we match first will be checked |
| 1936 | * against the correct length. |
| 1937 | */ |
| 1938 | bytes = 1; |
| 1939 | ptr = buf->lr; |
| 1940 | if (*ptr == '\r') { |
| 1941 | bytes++; |
| 1942 | ptr++; |
| 1943 | if (ptr >= buf->data + buf->size) |
| 1944 | ptr = buf->data; |
| 1945 | } |
| 1946 | |
Willy Tarreau | bf3f1de | 2010-03-17 15:54:24 +0100 | [diff] [blame] | 1947 | if (bytes > buf->l - buf->send_max) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1948 | return 0; |
| 1949 | |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1950 | if (*ptr != '\n') { |
| 1951 | msg->err_pos = ptr - buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1952 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1953 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1954 | |
| 1955 | ptr++; |
| 1956 | if (ptr >= buf->data + buf->size) |
| 1957 | ptr = buf->data; |
| 1958 | buf->lr = ptr; |
| 1959 | /* prepare the CRLF to be forwarded. msg->som may be before data but we don't care */ |
| 1960 | msg->sov = ptr - buf->data; |
| 1961 | msg->som = msg->sov - bytes; |
| 1962 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 1963 | return 1; |
| 1964 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1965 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 1966 | void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg) |
| 1967 | { |
| 1968 | char *end = buf->data + buf->size; |
| 1969 | int off = buf->data + buf->size - buf->w; |
| 1970 | |
| 1971 | /* two possible cases : |
| 1972 | * - the buffer is in one contiguous block, we move it in-place |
Willy Tarreau | 8096de9 | 2010-02-26 11:12:27 +0100 | [diff] [blame] | 1973 | * - 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] | 1974 | */ |
| 1975 | if (buf->l) { |
Willy Tarreau | 8096de9 | 2010-02-26 11:12:27 +0100 | [diff] [blame] | 1976 | int block1 = buf->l; |
| 1977 | int block2 = 0; |
| 1978 | if (buf->r <= buf->w) { |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 1979 | /* non-contiguous block */ |
Willy Tarreau | 8096de9 | 2010-02-26 11:12:27 +0100 | [diff] [blame] | 1980 | block1 = buf->data + buf->size - buf->w; |
| 1981 | block2 = buf->r - buf->data; |
| 1982 | } |
| 1983 | if (block2) |
| 1984 | memcpy(swap_buffer, buf->data, block2); |
| 1985 | memmove(buf->data, buf->w, block1); |
| 1986 | if (block2) |
| 1987 | memcpy(buf->data + block1, swap_buffer, block2); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 1988 | } |
| 1989 | |
| 1990 | /* adjust all known pointers */ |
| 1991 | buf->w = buf->data; |
| 1992 | buf->lr += off; if (buf->lr >= end) buf->lr -= buf->size; |
| 1993 | buf->r += off; if (buf->r >= end) buf->r -= buf->size; |
| 1994 | msg->sol += off; if (msg->sol >= end) msg->sol -= buf->size; |
| 1995 | msg->eol += off; if (msg->eol >= end) msg->eol -= buf->size; |
| 1996 | |
| 1997 | /* adjust relative pointers */ |
| 1998 | msg->som = 0; |
| 1999 | msg->eoh += off; if (msg->eoh >= buf->size) msg->eoh -= buf->size; |
| 2000 | msg->col += off; if (msg->col >= buf->size) msg->col -= buf->size; |
| 2001 | msg->sov += off; if (msg->sov >= buf->size) msg->sov -= buf->size; |
| 2002 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2003 | if (msg->err_pos >= 0) { |
| 2004 | msg->err_pos += off; |
| 2005 | if (msg->err_pos >= buf->size) |
| 2006 | msg->err_pos -= buf->size; |
| 2007 | } |
| 2008 | |
| 2009 | buf->flags &= ~BF_FULL; |
| 2010 | if (buf->l >= buffer_max_len(buf)) |
| 2011 | buf->flags |= BF_FULL; |
| 2012 | } |
| 2013 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2014 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 2015 | * processing can continue on next analysers, or zero if it either needs more |
| 2016 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
| 2017 | * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers |
| 2018 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 2019 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2020 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2021 | 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] | 2022 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2023 | /* |
| 2024 | * We will parse the partial (or complete) lines. |
| 2025 | * We will check the request syntax, and also join multi-line |
| 2026 | * headers. An index of all the lines will be elaborated while |
| 2027 | * parsing. |
| 2028 | * |
| 2029 | * For the parsing, we use a 28 states FSM. |
| 2030 | * |
| 2031 | * Here is the information we currently have : |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 2032 | * req->data + msg->som = beginning of request |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2033 | * req->data + msg->eoh = end of processed headers / start of current one |
| 2034 | * msg->eol = end of current header or line (LF or CRLF) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2035 | * req->lr = first non-visited byte |
| 2036 | * req->r = end of data |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2037 | * |
| 2038 | * At end of parsing, we may perform a capture of the error (if any), and |
| 2039 | * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE). |
| 2040 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 2041 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2042 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2043 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2044 | int cur_idx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2045 | int use_close_only; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2046 | struct http_txn *txn = &s->txn; |
| 2047 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2048 | struct hdr_ctx ctx; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2049 | |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 2050 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 2051 | now_ms, __FUNCTION__, |
| 2052 | s, |
| 2053 | req, |
| 2054 | req->rex, req->wex, |
| 2055 | req->flags, |
| 2056 | req->l, |
| 2057 | req->analysers); |
| 2058 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 2059 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 2060 | s->srv_error = http_return_srv_error; |
| 2061 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2062 | /* There's a protected area at the end of the buffer for rewriting |
| 2063 | * purposes. We don't want to start to parse the request if the |
| 2064 | * protected area is affected, because we may have to move processed |
| 2065 | * data later, which is much more complicated. |
| 2066 | */ |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2067 | if (req->l && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2068 | if ((txn->flags & TX_NOT_FIRST) && |
| 2069 | unlikely((req->flags & BF_FULL) || |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2070 | req->r < req->lr || |
| 2071 | req->r > req->data + req->size - global.tune.maxrewrite)) { |
| 2072 | if (req->send_max) { |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 2073 | if (req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) |
| 2074 | goto failed_keep_alive; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2075 | /* some data has still not left the buffer, wake us once that's done */ |
| 2076 | buffer_dont_connect(req); |
| 2077 | req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 2078 | return 0; |
| 2079 | } |
Willy Tarreau | 0499e35 | 2010-12-17 07:13:42 +0100 | [diff] [blame] | 2080 | if (req->r < req->lr || req->r > req->data + req->size - global.tune.maxrewrite) |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2081 | http_buffer_heavy_realign(req, msg); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2082 | } |
| 2083 | |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2084 | /* Note that we have the same problem with the response ; we |
| 2085 | * may want to send a redirect, error or anything which requires |
| 2086 | * some spare space. So we'll ensure that we have at least |
| 2087 | * maxrewrite bytes available in the response buffer before |
| 2088 | * processing that one. This will only affect pipelined |
| 2089 | * keep-alive requests. |
| 2090 | */ |
| 2091 | if ((txn->flags & TX_NOT_FIRST) && |
| 2092 | unlikely((s->rep->flags & BF_FULL) || |
| 2093 | s->rep->r < s->rep->lr || |
| 2094 | s->rep->r > s->rep->data + s->rep->size - global.tune.maxrewrite)) { |
| 2095 | if (s->rep->send_max) { |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 2096 | if (s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) |
| 2097 | goto failed_keep_alive; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2098 | /* don't let a connection request be initiated */ |
| 2099 | buffer_dont_connect(req); |
Willy Tarreau | ff7b588 | 2010-01-22 14:41:29 +0100 | [diff] [blame] | 2100 | s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 0499e35 | 2010-12-17 07:13:42 +0100 | [diff] [blame] | 2101 | s->rep->analysers |= an_bit; /* wake us up once it changes */ |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2102 | return 0; |
| 2103 | } |
| 2104 | } |
| 2105 | |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2106 | if (likely(req->lr < req->r)) |
| 2107 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2108 | } |
| 2109 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2110 | /* 1: we might have to print this header in debug mode */ |
| 2111 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2112 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | c3bfeeb | 2010-04-16 09:14:45 +0200 | [diff] [blame] | 2113 | msg->sol && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2114 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2115 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2116 | |
Willy Tarreau | 663308b | 2010-06-07 14:06:08 +0200 | [diff] [blame] | 2117 | sol = req->data + msg->som; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2118 | eol = sol + msg->sl.rq.l; |
| 2119 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2120 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2121 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2122 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2123 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2124 | while (cur_idx) { |
| 2125 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2126 | debug_hdr("clihdr", s, sol, eol); |
| 2127 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2128 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2129 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2130 | } |
| 2131 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2132 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2133 | /* |
| 2134 | * Now we quickly check if we have found a full valid request. |
| 2135 | * If not so, we check the FD and buffer states before leaving. |
| 2136 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2137 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2138 | * requests are checked first. When waiting for a second request |
| 2139 | * on a keep-alive session, if we encounter and error, close, t/o, |
| 2140 | * we note the error in the session flags but don't set any state. |
| 2141 | * Since the error will be noted there, it will not be counted by |
| 2142 | * process_session() as a frontend error. |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2143 | * Last, we may increase some tracked counters' http request errors on |
| 2144 | * the cases that are deliberately the client's fault. For instance, |
| 2145 | * a timeout or connection reset is not counted as an error. However |
| 2146 | * a bad request is. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2147 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2148 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2149 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2150 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2151 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2152 | */ |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2153 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2154 | session_inc_http_req_ctr(s); |
| 2155 | session_inc_http_err_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2156 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2157 | goto return_bad_req; |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2158 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2159 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2160 | /* 1: Since we are in header mode, if there's no space |
| 2161 | * left for headers, we won't be able to free more |
| 2162 | * later, so the session will never terminate. We |
| 2163 | * must terminate it now. |
| 2164 | */ |
| 2165 | if (unlikely(req->flags & BF_FULL)) { |
| 2166 | /* FIXME: check if URI is set and return Status |
| 2167 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2168 | */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2169 | session_inc_http_req_ctr(s); |
| 2170 | session_inc_http_err_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2171 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 2172 | if (msg->err_pos < 0) |
| 2173 | msg->err_pos = req->l; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2174 | goto return_bad_req; |
| 2175 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2176 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2177 | /* 2: have we encountered a read error ? */ |
| 2178 | else if (req->flags & BF_READ_ERROR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2179 | if (!(s->flags & SN_ERR_MASK)) |
| 2180 | s->flags |= SN_ERR_CLICL; |
| 2181 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2182 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2183 | goto failed_keep_alive; |
| 2184 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2185 | /* we cannot return any message on error */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2186 | if (msg->err_pos >= 0) { |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 2187 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2188 | session_inc_http_err_ctr(s); |
| 2189 | } |
| 2190 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2191 | msg->msg_state = HTTP_MSG_ERROR; |
| 2192 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2193 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2194 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2195 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2196 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2197 | if (s->listener->counters) |
| 2198 | s->listener->counters->failed_req++; |
| 2199 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2200 | if (!(s->flags & SN_FINST_MASK)) |
| 2201 | s->flags |= SN_FINST_R; |
| 2202 | return 0; |
| 2203 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2204 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2205 | /* 3: has the read timeout expired ? */ |
| 2206 | 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] | 2207 | if (!(s->flags & SN_ERR_MASK)) |
| 2208 | s->flags |= SN_ERR_CLITO; |
| 2209 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2210 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2211 | goto failed_keep_alive; |
| 2212 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2213 | /* read timeout : give up with an error message. */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2214 | if (msg->err_pos >= 0) { |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 2215 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2216 | session_inc_http_err_ctr(s); |
| 2217 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2218 | txn->status = 408; |
| 2219 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408)); |
| 2220 | msg->msg_state = HTTP_MSG_ERROR; |
| 2221 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2222 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2223 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2224 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2225 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2226 | if (s->listener->counters) |
| 2227 | s->listener->counters->failed_req++; |
| 2228 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2229 | if (!(s->flags & SN_FINST_MASK)) |
| 2230 | s->flags |= SN_FINST_R; |
| 2231 | return 0; |
| 2232 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2233 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2234 | /* 4: have we encountered a close ? */ |
| 2235 | else if (req->flags & BF_SHUTR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2236 | if (!(s->flags & SN_ERR_MASK)) |
| 2237 | s->flags |= SN_ERR_CLICL; |
| 2238 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2239 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2240 | goto failed_keep_alive; |
| 2241 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2242 | if (msg->err_pos >= 0) |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 2243 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2244 | txn->status = 400; |
| 2245 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
| 2246 | msg->msg_state = HTTP_MSG_ERROR; |
| 2247 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2248 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2249 | session_inc_http_err_ctr(s); |
| 2250 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2251 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2252 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2253 | if (s->listener->counters) |
| 2254 | s->listener->counters->failed_req++; |
| 2255 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2256 | if (!(s->flags & SN_FINST_MASK)) |
| 2257 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2258 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2259 | } |
| 2260 | |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2261 | buffer_dont_connect(req); |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2262 | req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | ff7b588 | 2010-01-22 14:41:29 +0100 | [diff] [blame] | 2263 | s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2264 | #ifdef TCP_QUICKACK |
| 2265 | if (s->listener->options & LI_O_NOQUICKACK) { |
| 2266 | /* We need more data, we have to re-enable quick-ack in case we |
| 2267 | * previously disabled it, otherwise we might cause the client |
| 2268 | * to delay next data. |
| 2269 | */ |
| 2270 | setsockopt(s->si[0].fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); |
| 2271 | } |
| 2272 | #endif |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2273 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2274 | if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) { |
| 2275 | /* If the client starts to talk, let's fall back to |
| 2276 | * request timeout processing. |
| 2277 | */ |
| 2278 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2279 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2280 | } |
| 2281 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2282 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2283 | if (!tick_isset(req->analyse_exp)) { |
| 2284 | if ((msg->msg_state == HTTP_MSG_RQBEFORE) && |
| 2285 | (txn->flags & TX_WAIT_NEXT_RQ) && |
| 2286 | tick_isset(s->be->timeout.httpka)) |
| 2287 | req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka); |
| 2288 | else |
| 2289 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
| 2290 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2291 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2292 | /* we're not ready yet */ |
| 2293 | return 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2294 | |
| 2295 | failed_keep_alive: |
| 2296 | /* Here we process low-level errors for keep-alive requests. In |
| 2297 | * short, if the request is not the first one and it experiences |
| 2298 | * a timeout, read error or shutdown, we just silently close so |
| 2299 | * that the client can try again. |
| 2300 | */ |
| 2301 | txn->status = 0; |
| 2302 | msg->msg_state = HTTP_MSG_RQBEFORE; |
| 2303 | req->analysers = 0; |
| 2304 | s->logs.logwait = 0; |
Willy Tarreau | ff7b588 | 2010-01-22 14:41:29 +0100 | [diff] [blame] | 2305 | s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 2306 | stream_int_retnclose(req->prod, NULL); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2307 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2308 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2309 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2310 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 2311 | * complete the request parsing by setting a few fields we will need |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 2312 | * later. At this point, we have the last CRLF at req->data + msg->eoh. |
| 2313 | * If the request is in HTTP/0.9 form, the rule is still true, and eoh |
| 2314 | * points to the CRLF of the request line. req->lr points to the first |
| 2315 | * byte after the last LF. msg->col and msg->sov point to the first |
| 2316 | * byte of data. msg->eol cannot be trusted because it may have been |
| 2317 | * left uninitialized (for instance in the absence of headers). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2318 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2319 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2320 | session_inc_http_req_ctr(s); |
Willy Tarreau | d9b587f | 2010-02-26 10:05:55 +0100 | [diff] [blame] | 2321 | proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */ |
| 2322 | |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2323 | if (txn->flags & TX_WAIT_NEXT_RQ) { |
| 2324 | /* kill the pending keep-alive timeout */ |
| 2325 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 2326 | req->analyse_exp = TICK_ETERNITY; |
| 2327 | } |
| 2328 | |
| 2329 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2330 | /* Maybe we found in invalid header name while we were configured not |
| 2331 | * to block on that, so we have to capture it now. |
| 2332 | */ |
| 2333 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 2334 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2335 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2336 | /* |
| 2337 | * 1: identify the method |
| 2338 | */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2339 | txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2340 | |
| 2341 | /* we can make use of server redirect on GET and HEAD */ |
| 2342 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 2343 | s->flags |= SN_REDIRECTABLE; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2344 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2345 | /* |
| 2346 | * 2: check if the URI matches the monitor_uri. |
| 2347 | * We have to do this for every request which gets in, because |
| 2348 | * the monitor-uri is defined by the frontend. |
| 2349 | */ |
| 2350 | if (unlikely((s->fe->monitor_uri_len != 0) && |
| 2351 | (s->fe->monitor_uri_len == msg->sl.rq.u_l) && |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2352 | !memcmp(msg->sol + msg->sl.rq.u, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2353 | s->fe->monitor_uri, |
| 2354 | s->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2355 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2356 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2357 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2358 | struct acl_cond *cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2359 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2360 | s->flags |= SN_MONITOR; |
Willy Tarreau | eabea07 | 2011-09-10 23:29:44 +0200 | [diff] [blame] | 2361 | s->fe->fe_counters.intercepted_req++; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2362 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2363 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2364 | list_for_each_entry(cond, &s->fe->mon_fail_cond, list) { |
| 2365 | int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2366 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2367 | ret = acl_pass(ret); |
| 2368 | if (cond->pol == ACL_COND_UNLESS) |
| 2369 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2370 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2371 | if (ret) { |
| 2372 | /* we fail this request, let's return 503 service unavail */ |
| 2373 | txn->status = 503; |
| 2374 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503)); |
| 2375 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2376 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2377 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 2378 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2379 | /* nothing to fail, let's reply normaly */ |
| 2380 | txn->status = 200; |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 2381 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_200)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2382 | goto return_prx_cond; |
| 2383 | } |
| 2384 | |
| 2385 | /* |
| 2386 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 2387 | * Note: we cannot log anymore if the request has been |
| 2388 | * classified as invalid. |
| 2389 | */ |
| 2390 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 2391 | /* we have a complete HTTP request that we must log */ |
| 2392 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
| 2393 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2394 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2395 | if (urilen >= REQURI_LEN) |
| 2396 | urilen = REQURI_LEN - 1; |
| 2397 | memcpy(txn->uri, &req->data[msg->som], urilen); |
| 2398 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2399 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2400 | if (!(s->logs.logwait &= ~LW_REQ)) |
| 2401 | s->do_log(s); |
| 2402 | } else { |
| 2403 | Alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2404 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2405 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2406 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 2407 | if (!LIST_ISEMPTY(&s->fe->format_unique_id)) { |
| 2408 | s->unique_id = pool_alloc2(pool2_uniqueid); |
| 2409 | } |
| 2410 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2411 | /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 2412 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) |
| 2413 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2414 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2415 | /* ... and check if the request is HTTP/1.1 or above */ |
| 2416 | if ((msg->sl.rq.v_l == 8) && |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2417 | ((msg->sol[msg->sl.rq.v + 5] > '1') || |
| 2418 | ((msg->sol[msg->sl.rq.v + 5] == '1') && |
| 2419 | (msg->sol[msg->sl.rq.v + 7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2420 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2421 | |
| 2422 | /* "connection" has not been parsed yet */ |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2423 | 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] | 2424 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2425 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 2426 | * we have what looks like a proxied connection instead of a connection, |
| 2427 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 2428 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 2429 | * happen to do that despite being non-standard :-( |
| 2430 | * We consider that a request not beginning with either '/' or '*' is |
| 2431 | * a proxied connection, which covers both "scheme://location" and |
| 2432 | * CONNECT ip:port. |
| 2433 | */ |
| 2434 | if ((s->fe->options2 & PR_O2_USE_PXHDR) && |
| 2435 | msg->sol[msg->sl.rq.u] != '/' && msg->sol[msg->sl.rq.u] != '*') |
| 2436 | txn->flags |= TX_USE_PX_CONN; |
| 2437 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2438 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2439 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2440 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2441 | /* 5: we may need to capture headers */ |
Willy Tarreau | 42f7d89 | 2012-03-24 08:28:09 +0100 | [diff] [blame] | 2442 | if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap)) |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2443 | capture_headers(msg->sol, &txn->hdr_idx, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2444 | txn->req.cap, s->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2445 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2446 | /* 6: determine the transfer-length. |
| 2447 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 2448 | * the presence of a message-body in a REQUEST and its transfer length |
| 2449 | * must be determined that way (in order of precedence) : |
| 2450 | * 1. The presence of a message-body in a request is signaled by the |
| 2451 | * inclusion of a Content-Length or Transfer-Encoding header field |
| 2452 | * in the request's header fields. When a request message contains |
| 2453 | * both a message-body of non-zero length and a method that does |
| 2454 | * not define any semantics for that request message-body, then an |
| 2455 | * origin server SHOULD either ignore the message-body or respond |
| 2456 | * with an appropriate error message (e.g., 413). A proxy or |
| 2457 | * gateway, when presented the same request, SHOULD either forward |
| 2458 | * the request inbound with the message- body or ignore the |
| 2459 | * message-body when determining a response. |
| 2460 | * |
| 2461 | * 2. If a Transfer-Encoding header field (Section 9.7) is present |
| 2462 | * and the "chunked" transfer-coding (Section 6.2) is used, the |
| 2463 | * transfer-length is defined by the use of this transfer-coding. |
| 2464 | * If a Transfer-Encoding header field is present and the "chunked" |
| 2465 | * transfer-coding is not present, the transfer-length is defined |
| 2466 | * by the sender closing the connection. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2467 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2468 | * 3. If a Content-Length header field is present, its decimal value in |
| 2469 | * OCTETs represents both the entity-length and the transfer-length. |
| 2470 | * If a message is received with both a Transfer-Encoding header |
| 2471 | * field and a Content-Length header field, the latter MUST be ignored. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2472 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2473 | * 4. By the server closing the connection. (Closing the connection |
| 2474 | * cannot be used to indicate the end of a request body, since that |
| 2475 | * would leave no possibility for the server to send back a response.) |
| 2476 | * |
| 2477 | * Whenever a transfer-coding is applied to a message-body, the set of |
| 2478 | * transfer-codings MUST include "chunked", unless the message indicates |
| 2479 | * it is terminated by closing the connection. When the "chunked" |
| 2480 | * transfer-coding is used, it MUST be the last transfer-coding applied |
| 2481 | * to the message-body. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2482 | */ |
| 2483 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2484 | use_close_only = 0; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2485 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2486 | /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2487 | while ((msg->flags & HTTP_MSGF_VER_11) && |
Willy Tarreau | 9e13c3c | 2009-12-22 09:59:58 +0100 | [diff] [blame] | 2488 | http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2489 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2490 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 2491 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2492 | /* bad transfer-encoding (chunked followed by something else) */ |
| 2493 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2494 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2495 | break; |
| 2496 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2497 | } |
| 2498 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2499 | ctx.idx = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2500 | while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only && |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2501 | http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) { |
| 2502 | signed long long cl; |
| 2503 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2504 | if (!ctx.vlen) { |
| 2505 | msg->err_pos = ctx.line + ctx.val - req->data; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2506 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2507 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2508 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2509 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
| 2510 | msg->err_pos = ctx.line + ctx.val - req->data; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2511 | goto return_bad_req; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2512 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2513 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2514 | if (cl < 0) { |
| 2515 | msg->err_pos = ctx.line + ctx.val - req->data; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2516 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2517 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2518 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2519 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2520 | msg->err_pos = ctx.line + ctx.val - req->data; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2521 | goto return_bad_req; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2522 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2523 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2524 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2525 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2526 | } |
| 2527 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2528 | /* bodyless requests have a known length */ |
| 2529 | if (!use_close_only) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2530 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2531 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2532 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2533 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2534 | req->analyse_exp = TICK_ETERNITY; |
| 2535 | return 1; |
| 2536 | |
| 2537 | return_bad_req: |
| 2538 | /* We centralize bad requests processing here */ |
| 2539 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 2540 | /* we detected a parsing error. We want to archive this request |
| 2541 | * in the dedicated proxy area for later troubleshooting. |
| 2542 | */ |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 2543 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe); |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2544 | } |
| 2545 | |
| 2546 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 2547 | txn->status = 400; |
| 2548 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2549 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2550 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2551 | if (s->listener->counters) |
| 2552 | s->listener->counters->failed_req++; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2553 | |
| 2554 | return_prx_cond: |
| 2555 | if (!(s->flags & SN_ERR_MASK)) |
| 2556 | s->flags |= SN_ERR_PRXCOND; |
| 2557 | if (!(s->flags & SN_FINST_MASK)) |
| 2558 | s->flags |= SN_FINST_R; |
| 2559 | |
| 2560 | req->analysers = 0; |
| 2561 | req->analyse_exp = TICK_ETERNITY; |
| 2562 | return 0; |
| 2563 | } |
| 2564 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2565 | /* We reached the stats page through a POST request. |
| 2566 | * Parse the posted data and enable/disable servers if necessary. |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2567 | * 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] | 2568 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2569 | 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] | 2570 | { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2571 | struct proxy *px = NULL; |
| 2572 | struct server *sv = NULL; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2573 | |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2574 | char key[LINESIZE]; |
| 2575 | int action = ST_ADM_ACTION_NONE; |
| 2576 | int reprocess = 0; |
| 2577 | |
| 2578 | int total_servers = 0; |
| 2579 | int altered_servers = 0; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2580 | |
| 2581 | char *first_param, *cur_param, *next_param, *end_params; |
Willy Tarreau | 46787ed | 2012-04-11 17:28:40 +0200 | [diff] [blame] | 2582 | char *st_cur_param = NULL; |
| 2583 | char *st_next_param = NULL; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2584 | |
| 2585 | first_param = req->data + txn->req.eoh + 2; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2586 | end_params = first_param + txn->req.body_len; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2587 | |
| 2588 | cur_param = next_param = end_params; |
| 2589 | |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2590 | if (end_params >= req->data + req->size - global.tune.maxrewrite) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2591 | /* Prevent buffer overflow */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2592 | si->applet.ctx.stats.st_code = STAT_STATUS_EXCD; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2593 | return 1; |
| 2594 | } |
| 2595 | else if (end_params > req->data + req->l) { |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2596 | /* we need more data */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2597 | si->applet.ctx.stats.st_code = STAT_STATUS_NONE; |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2598 | return 0; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2599 | } |
| 2600 | |
| 2601 | *end_params = '\0'; |
| 2602 | |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2603 | si->applet.ctx.stats.st_code = STAT_STATUS_NONE; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2604 | |
| 2605 | /* |
| 2606 | * Parse the parameters in reverse order to only store the last value. |
| 2607 | * From the html form, the backend and the action are at the end. |
| 2608 | */ |
| 2609 | while (cur_param > first_param) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2610 | char *value; |
| 2611 | int poffset, plen; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2612 | |
| 2613 | cur_param--; |
| 2614 | if ((*cur_param == '&') || (cur_param == first_param)) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2615 | reprocess_servers: |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2616 | /* Parse the key */ |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2617 | poffset = (cur_param != first_param ? 1 : 0); |
| 2618 | plen = next_param - cur_param + (cur_param == first_param ? 1 : 0); |
| 2619 | if ((plen > 0) && (plen <= sizeof(key))) { |
| 2620 | strncpy(key, cur_param + poffset, plen); |
| 2621 | key[plen - 1] = '\0'; |
| 2622 | } else { |
| 2623 | si->applet.ctx.stats.st_code = STAT_STATUS_EXCD; |
| 2624 | goto out; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2625 | } |
| 2626 | |
| 2627 | /* Parse the value */ |
| 2628 | value = key; |
| 2629 | while (*value != '\0' && *value != '=') { |
| 2630 | value++; |
| 2631 | } |
| 2632 | if (*value == '=') { |
| 2633 | /* Ok, a value is found, we can mark the end of the key */ |
| 2634 | *value++ = '\0'; |
| 2635 | } |
| 2636 | |
Willy Tarreau | bf9c2fc | 2011-05-31 18:06:18 +0200 | [diff] [blame] | 2637 | if (!url_decode(key) || !url_decode(value)) |
| 2638 | break; |
| 2639 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2640 | /* Now we can check the key to see what to do */ |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2641 | if (!px && (strcmp(key, "b") == 0)) { |
| 2642 | if ((px = findproxy(value, PR_CAP_BE)) == NULL) { |
| 2643 | /* the backend name is unknown or ambiguous (duplicate names) */ |
| 2644 | si->applet.ctx.stats.st_code = STAT_STATUS_ERRP; |
| 2645 | goto out; |
| 2646 | } |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2647 | } |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2648 | else if (!action && (strcmp(key, "action") == 0)) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2649 | if (strcmp(value, "disable") == 0) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2650 | action = ST_ADM_ACTION_DISABLE; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2651 | } |
| 2652 | else if (strcmp(value, "enable") == 0) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2653 | action = ST_ADM_ACTION_ENABLE; |
| 2654 | } |
| 2655 | else { |
| 2656 | si->applet.ctx.stats.st_code = STAT_STATUS_ERRP; |
| 2657 | goto out; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2658 | } |
| 2659 | } |
| 2660 | else if (strcmp(key, "s") == 0) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2661 | if (!(px && action)) { |
| 2662 | /* |
| 2663 | * Indicates that we'll need to reprocess the parameters |
| 2664 | * as soon as backend and action are known |
| 2665 | */ |
| 2666 | if (!reprocess) { |
| 2667 | st_cur_param = cur_param; |
| 2668 | st_next_param = next_param; |
| 2669 | } |
| 2670 | reprocess = 1; |
| 2671 | } |
| 2672 | else if ((sv = findserver(px, value)) != NULL) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2673 | switch (action) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2674 | case ST_ADM_ACTION_DISABLE: |
Cyril Bonté | 1e2a170 | 2011-03-03 21:05:17 +0100 | [diff] [blame] | 2675 | if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2676 | /* Not already in maintenance, we can change the server state */ |
| 2677 | sv->state |= SRV_MAINTAIN; |
| 2678 | set_server_down(sv); |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2679 | altered_servers++; |
| 2680 | total_servers++; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2681 | } |
| 2682 | break; |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2683 | case ST_ADM_ACTION_ENABLE: |
Cyril Bonté | 1e2a170 | 2011-03-03 21:05:17 +0100 | [diff] [blame] | 2684 | if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2685 | /* Already in maintenance, we can change the server state */ |
| 2686 | set_server_up(sv); |
Willy Tarreau | 7046130 | 2010-10-22 14:39:02 +0200 | [diff] [blame] | 2687 | sv->health = sv->rise; /* up, but will fall down at first failure */ |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2688 | altered_servers++; |
| 2689 | total_servers++; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2690 | } |
| 2691 | break; |
| 2692 | } |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2693 | } else { |
| 2694 | /* the server name is unknown or ambiguous (duplicate names) */ |
| 2695 | total_servers++; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2696 | } |
| 2697 | } |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2698 | if (reprocess && px && action) { |
| 2699 | /* Now, we know the backend and the action chosen by the user. |
| 2700 | * We can safely restart from the first server parameter |
| 2701 | * to reprocess them |
| 2702 | */ |
| 2703 | cur_param = st_cur_param; |
| 2704 | next_param = st_next_param; |
| 2705 | reprocess = 0; |
| 2706 | goto reprocess_servers; |
| 2707 | } |
| 2708 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2709 | next_param = cur_param; |
| 2710 | } |
| 2711 | } |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2712 | |
| 2713 | if (total_servers == 0) { |
| 2714 | si->applet.ctx.stats.st_code = STAT_STATUS_NONE; |
| 2715 | } |
| 2716 | else if (altered_servers == 0) { |
| 2717 | si->applet.ctx.stats.st_code = STAT_STATUS_ERRP; |
| 2718 | } |
| 2719 | else if (altered_servers == total_servers) { |
| 2720 | si->applet.ctx.stats.st_code = STAT_STATUS_DONE; |
| 2721 | } |
| 2722 | else { |
| 2723 | si->applet.ctx.stats.st_code = STAT_STATUS_PART; |
| 2724 | } |
| 2725 | out: |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2726 | return 1; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2727 | } |
| 2728 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2729 | /* returns a pointer to the first rule which forbids access (deny or http_auth), |
| 2730 | * or NULL if everything's OK. |
| 2731 | */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2732 | static inline struct http_req_rule * |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2733 | http_check_access_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn) |
| 2734 | { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2735 | struct http_req_rule *rule; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2736 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2737 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2738 | int ret = 1; |
| 2739 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2740 | if (rule->action >= HTTP_REQ_ACT_MAX) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2741 | continue; |
| 2742 | |
| 2743 | /* check condition, but only if attached */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2744 | if (rule->cond) { |
| 2745 | ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ); |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2746 | ret = acl_pass(ret); |
| 2747 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2748 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2749 | ret = !ret; |
| 2750 | } |
| 2751 | |
| 2752 | if (ret) { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2753 | if (rule->action == HTTP_REQ_ACT_ALLOW) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2754 | return NULL; /* no problem */ |
| 2755 | else |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2756 | return rule; /* most likely a deny or auth rule */ |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2757 | } |
| 2758 | } |
| 2759 | return NULL; |
| 2760 | } |
| 2761 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2762 | /* This stream analyser runs all HTTP request processing which is common to |
| 2763 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 2764 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2765 | * 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] | 2766 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 2767 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2768 | */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2769 | 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] | 2770 | { |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2771 | struct http_txn *txn = &s->txn; |
| 2772 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2773 | struct acl_cond *cond; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2774 | struct http_req_rule *http_req_last_rule = NULL; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2775 | struct redirect_rule *rule; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 2776 | struct cond_wordlist *wl; |
Simon Horman | 70735c9 | 2011-06-07 11:07:50 +0900 | [diff] [blame] | 2777 | int do_stats; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2778 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2779 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2780 | /* we need more data */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2781 | buffer_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2782 | return 0; |
| 2783 | } |
| 2784 | |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2785 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2786 | req->analyse_exp = TICK_ETERNITY; |
| 2787 | |
| 2788 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 2789 | now_ms, __FUNCTION__, |
| 2790 | s, |
| 2791 | req, |
| 2792 | req->rex, req->wex, |
| 2793 | req->flags, |
| 2794 | req->l, |
| 2795 | req->analysers); |
| 2796 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2797 | /* first check whether we have some ACLs set to block this request */ |
| 2798 | list_for_each_entry(cond, &px->block_cond, list) { |
| 2799 | int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ); |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2800 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2801 | ret = acl_pass(ret); |
| 2802 | if (cond->pol == ACL_COND_UNLESS) |
| 2803 | ret = !ret; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 2804 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2805 | if (ret) { |
| 2806 | txn->status = 403; |
| 2807 | /* let's log the request time */ |
| 2808 | s->logs.tv_request = now; |
| 2809 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2810 | session_inc_http_err_ctr(s); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2811 | goto return_prx_cond; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2812 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2813 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2814 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2815 | /* evaluate http-request rules */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2816 | 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] | 2817 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2818 | /* evaluate stats http-request rules only if http-request is OK */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2819 | if (!http_req_last_rule) { |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2820 | do_stats = stats_check_uri(s->rep->prod, txn, px); |
| 2821 | if (do_stats) |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2822 | 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] | 2823 | } |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2824 | else |
| 2825 | do_stats = 0; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 2826 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2827 | /* return a 403 if either rule has blocked */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2828 | 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] | 2829 | txn->status = 403; |
| 2830 | s->logs.tv_request = now; |
| 2831 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2832 | session_inc_http_err_ctr(s); |
Willy Tarreau | 6da0f6d | 2011-01-06 18:19:50 +0100 | [diff] [blame] | 2833 | s->fe->fe_counters.denied_req++; |
| 2834 | if (an_bit == AN_REQ_HTTP_PROCESS_BE) |
| 2835 | s->be->be_counters.denied_req++; |
| 2836 | if (s->listener->counters) |
| 2837 | s->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 2838 | goto return_prx_cond; |
| 2839 | } |
| 2840 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2841 | /* try headers filters */ |
| 2842 | if (px->req_exp != NULL) { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 2843 | if (apply_filters_to_request(s, req, px) < 0) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2844 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2845 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2846 | /* has the request been denied ? */ |
| 2847 | if (txn->flags & TX_CLDENY) { |
| 2848 | /* no need to go further */ |
| 2849 | txn->status = 403; |
| 2850 | /* let's log the request time */ |
| 2851 | s->logs.tv_request = now; |
| 2852 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2853 | session_inc_http_err_ctr(s); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2854 | goto return_prx_cond; |
| 2855 | } |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 2856 | |
| 2857 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 2858 | * which may be the same as the connect timeout if unspecified. |
| 2859 | * If unset, then set it to zero because we really want it to |
| 2860 | * eventually expire. We build the tarpit as an analyser. |
| 2861 | */ |
| 2862 | if (txn->flags & TX_CLTARPIT) { |
| 2863 | buffer_erase(s->req); |
| 2864 | /* wipe the request out so that we can drop the connection early |
| 2865 | * if the client closes first. |
| 2866 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2867 | buffer_dont_connect(req); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 2868 | req->analysers = 0; /* remove switching rules etc... */ |
| 2869 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 2870 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 2871 | if (!req->analyse_exp) |
| 2872 | req->analyse_exp = tick_add(now_ms, 0); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2873 | session_inc_http_err_ctr(s); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 2874 | return 1; |
| 2875 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2876 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2877 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2878 | /* Until set to anything else, the connection mode is set as TUNNEL. It will |
| 2879 | * only change if both the request and the config reference something else. |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2880 | * Option httpclose by itself does not set a mode, it remains a tunnel mode |
| 2881 | * 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] | 2882 | * affect it (eg: server-close/keep-alive + httpclose = close). Note that we |
| 2883 | * avoid to redo the same work if FE and BE have the same settings (common). |
| 2884 | * The method consists in checking if options changed between the two calls |
| 2885 | * (implying that either one is non-null, or one of them is non-null and we |
| 2886 | * are there for the first time. |
Willy Tarreau | 4273664 | 2009-10-18 21:04:35 +0200 | [diff] [blame] | 2887 | */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2888 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 2889 | if ((!(txn->flags & TX_HDR_CONN_PRS) && |
| 2890 | (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) || |
| 2891 | ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) != |
| 2892 | (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] | 2893 | int tmp = TX_CON_WANT_TUN; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2894 | |
Cyril Bonté | 9ea2b9a | 2010-12-29 09:36:56 +0100 | [diff] [blame] | 2895 | if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE || |
| 2896 | ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2897 | tmp = TX_CON_WANT_KAL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2898 | if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO) |
| 2899 | tmp = TX_CON_WANT_SCL; |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2900 | if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2901 | tmp = TX_CON_WANT_CLO; |
| 2902 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2903 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
| 2904 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2905 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2906 | if (!(txn->flags & TX_HDR_CONN_PRS)) { |
| 2907 | /* parse the Connection header and possibly clean it */ |
| 2908 | int to_del = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2909 | if ((msg->flags & HTTP_MSGF_VER_11) || |
Willy Tarreau | 8a8e1d9 | 2010-04-05 16:15:16 +0200 | [diff] [blame] | 2910 | ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL && |
| 2911 | !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2912 | to_del |= 2; /* remove "keep-alive" */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2913 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2914 | to_del |= 1; /* remove "close" */ |
| 2915 | http_parse_connection_header(txn, msg, req, to_del); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2916 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2917 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2918 | /* check if client or config asks for explicit close in KAL/SCL */ |
| 2919 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 2920 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) && |
| 2921 | ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2922 | (!(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] | 2923 | ((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] | 2924 | !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */ |
Willy Tarreau | c3e8b25 | 2010-01-28 15:01:20 +0100 | [diff] [blame] | 2925 | s->fe->state == PR_STSTOPPED)) /* frontend is stopping */ |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2926 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 2927 | } |
Willy Tarreau | 7859991 | 2009-10-17 20:12:21 +0200 | [diff] [blame] | 2928 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2929 | /* we can be blocked here because the request needs to be authenticated, |
| 2930 | * either to pass or to access stats. |
| 2931 | */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2932 | 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] | 2933 | struct chunk msg; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2934 | char *realm = http_req_last_rule->http_auth.realm; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 2935 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 2936 | if (!realm) |
| 2937 | realm = do_stats?STATS_DEFAULT_REALM:px->id; |
| 2938 | |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 2939 | 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] | 2940 | chunk_initlen(&msg, trash, sizeof(trash), strlen(trash)); |
| 2941 | txn->status = 401; |
| 2942 | stream_int_retnclose(req->prod, &msg); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2943 | /* on 401 we still count one error, because normal browsing |
| 2944 | * won't significantly increase the counter but brute force |
| 2945 | * attempts will. |
| 2946 | */ |
| 2947 | session_inc_http_err_ctr(s); |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 2948 | goto return_prx_cond; |
| 2949 | } |
| 2950 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2951 | /* add request headers from the rule sets in the same order */ |
| 2952 | list_for_each_entry(wl, &px->req_add, list) { |
| 2953 | if (wl->cond) { |
| 2954 | int ret = acl_exec_cond(wl->cond, px, s, txn, ACL_DIR_REQ); |
| 2955 | ret = acl_pass(ret); |
| 2956 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 2957 | ret = !ret; |
| 2958 | if (!ret) |
| 2959 | continue; |
| 2960 | } |
| 2961 | |
| 2962 | if (unlikely(http_header_add_tail(req, &txn->req, &txn->hdr_idx, wl->s) < 0)) |
| 2963 | goto return_bad_req; |
| 2964 | } |
| 2965 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 2966 | if (do_stats) { |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 2967 | struct stats_admin_rule *stats_admin_rule; |
| 2968 | |
| 2969 | /* We need to provide stats for this request. |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2970 | * FIXME!!! that one is rather dangerous, we want to |
| 2971 | * make it follow standard rules (eg: clear req->analysers). |
| 2972 | */ |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 2973 | |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 2974 | /* now check whether we have some admin rules for this request */ |
| 2975 | list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) { |
| 2976 | int ret = 1; |
| 2977 | |
| 2978 | if (stats_admin_rule->cond) { |
| 2979 | ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ); |
| 2980 | ret = acl_pass(ret); |
| 2981 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 2982 | ret = !ret; |
| 2983 | } |
| 2984 | |
| 2985 | if (ret) { |
| 2986 | /* no rule, or the rule matches */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2987 | s->rep->prod->applet.ctx.stats.flags |= STAT_ADMIN; |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 2988 | break; |
| 2989 | } |
| 2990 | } |
| 2991 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2992 | /* Was the status page requested with a POST ? */ |
| 2993 | if (txn->meth == HTTP_METH_POST) { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2994 | if (s->rep->prod->applet.ctx.stats.flags & STAT_ADMIN) { |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2995 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 2996 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 2997 | * send an HTTP/1.1 100 Continue intermediate response. |
| 2998 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2999 | if (msg->flags & HTTP_MSGF_VER_11) { |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 3000 | struct hdr_ctx ctx; |
| 3001 | ctx.idx = 0; |
| 3002 | /* Expect is allowed in 1.1, look for it */ |
| 3003 | if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) && |
| 3004 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) { |
| 3005 | buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len); |
| 3006 | } |
| 3007 | } |
| 3008 | msg->msg_state = HTTP_MSG_100_SENT; |
| 3009 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 3010 | } |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 3011 | if (!http_process_req_stat_post(s->rep->prod, txn, req)) { |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 3012 | /* we need more data */ |
| 3013 | req->analysers |= an_bit; |
| 3014 | buffer_dont_connect(req); |
| 3015 | return 0; |
| 3016 | } |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 3017 | } else { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 3018 | s->rep->prod->applet.ctx.stats.st_code = STAT_STATUS_DENY; |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 3019 | } |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 3020 | } |
| 3021 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3022 | s->logs.tv_request = now; |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3023 | s->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | b24281b | 2011-02-13 13:16:36 +0100 | [diff] [blame] | 3024 | stream_int_register_handler(s->rep->prod, &http_stats_applet); |
Willy Tarreau | 7b7a8e9 | 2011-03-27 19:53:06 +0200 | [diff] [blame] | 3025 | copy_target(&s->target, &s->rep->prod->target); // for logging only |
Willy Tarreau | bc4af05 | 2011-02-13 13:25:14 +0100 | [diff] [blame] | 3026 | s->rep->prod->applet.private = s; |
| 3027 | s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0; |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3028 | req->analysers = 0; |
Willy Tarreau | eabea07 | 2011-09-10 23:29:44 +0200 | [diff] [blame] | 3029 | if (s->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 3030 | s->fe->fe_counters.intercepted_req++; |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3031 | |
| 3032 | return 0; |
| 3033 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3034 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 3035 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3036 | /* check whether we have some ACLs set to redirect this request */ |
| 3037 | list_for_each_entry(rule, &px->redirect_rules, list) { |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3038 | int ret = ACL_PAT_PASS; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3039 | |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3040 | if (rule->cond) { |
| 3041 | ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ); |
| 3042 | ret = acl_pass(ret); |
| 3043 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 3044 | ret = !ret; |
| 3045 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3046 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3047 | if (ret) { |
Willy Tarreau | 3bb9c23 | 2010-01-03 12:24:37 +0100 | [diff] [blame] | 3048 | struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 }; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3049 | const char *msg_fmt; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3050 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3051 | /* build redirect message */ |
| 3052 | switch(rule->code) { |
| 3053 | case 303: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3054 | msg_fmt = HTTP_303; |
| 3055 | break; |
| 3056 | case 301: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3057 | msg_fmt = HTTP_301; |
| 3058 | break; |
| 3059 | case 302: |
| 3060 | default: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3061 | msg_fmt = HTTP_302; |
| 3062 | break; |
| 3063 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3064 | |
Willy Tarreau | 3bb9c23 | 2010-01-03 12:24:37 +0100 | [diff] [blame] | 3065 | if (unlikely(!chunk_strcpy(&rdr, msg_fmt))) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3066 | goto return_bad_req; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3067 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3068 | switch(rule->type) { |
| 3069 | case REDIRECT_TYPE_PREFIX: { |
| 3070 | const char *path; |
| 3071 | int pathlen; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3072 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3073 | path = http_get_path(txn); |
| 3074 | /* build message using path */ |
| 3075 | if (path) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 3076 | pathlen = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3077 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3078 | int qs = 0; |
| 3079 | while (qs < pathlen) { |
| 3080 | if (path[qs] == '?') { |
| 3081 | pathlen = qs; |
| 3082 | break; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3083 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3084 | qs++; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3085 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3086 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3087 | } else { |
| 3088 | path = "/"; |
| 3089 | pathlen = 1; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3090 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3091 | |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 3092 | if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3093 | goto return_bad_req; |
| 3094 | |
| 3095 | /* add prefix. Note that if prefix == "/", we don't want to |
| 3096 | * add anything, otherwise it makes it hard for the user to |
| 3097 | * configure a self-redirection. |
| 3098 | */ |
| 3099 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3100 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 3101 | rdr.len += rule->rdr_len; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3102 | } |
| 3103 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3104 | /* add path */ |
| 3105 | memcpy(rdr.str + rdr.len, path, pathlen); |
| 3106 | rdr.len += pathlen; |
Willy Tarreau | 81e3b4f | 2010-01-10 00:42:19 +0100 | [diff] [blame] | 3107 | |
| 3108 | /* append a slash at the end of the location is needed and missing */ |
| 3109 | if (rdr.len && rdr.str[rdr.len - 1] != '/' && |
| 3110 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 3111 | if (rdr.len > rdr.size - 5) |
| 3112 | goto return_bad_req; |
| 3113 | rdr.str[rdr.len] = '/'; |
| 3114 | rdr.len++; |
| 3115 | } |
| 3116 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3117 | break; |
| 3118 | } |
| 3119 | case REDIRECT_TYPE_LOCATION: |
| 3120 | default: |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 3121 | if (rdr.len + rule->rdr_len > rdr.size - 4) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3122 | goto return_bad_req; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3123 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3124 | /* add location */ |
| 3125 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 3126 | rdr.len += rule->rdr_len; |
| 3127 | break; |
| 3128 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3129 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3130 | if (rule->cookie_len) { |
| 3131 | memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14); |
| 3132 | rdr.len += 14; |
| 3133 | memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len); |
| 3134 | rdr.len += rule->cookie_len; |
| 3135 | memcpy(rdr.str + rdr.len, "\r\n", 2); |
| 3136 | rdr.len += 2; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3137 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3138 | |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3139 | /* add end of headers and the keep-alive/close status. |
| 3140 | * We may choose to set keep-alive if the Location begins |
| 3141 | * with a slash, because the client will come back to the |
| 3142 | * same server. |
| 3143 | */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3144 | txn->status = rule->code; |
| 3145 | /* let's log the request time */ |
| 3146 | s->logs.tv_request = now; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3147 | |
| 3148 | if (rule->rdr_len >= 1 && *rule->rdr_str == '/' && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3149 | (msg->flags & HTTP_MSGF_XFER_LEN) && |
| 3150 | !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len && |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3151 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 3152 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 3153 | /* keep-alive possible */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3154 | if (!(msg->flags & HTTP_MSGF_VER_11)) { |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 3155 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 3156 | memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: keep-alive", 30); |
| 3157 | rdr.len += 30; |
| 3158 | } else { |
| 3159 | memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24); |
| 3160 | rdr.len += 24; |
| 3161 | } |
Willy Tarreau | 7566145 | 2010-01-10 10:35:01 +0100 | [diff] [blame] | 3162 | } |
| 3163 | memcpy(rdr.str + rdr.len, "\r\n\r\n", 4); |
| 3164 | rdr.len += 4; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3165 | buffer_write(req->prod->ob, rdr.str, rdr.len); |
| 3166 | /* "eat" the request */ |
| 3167 | buffer_ignore(req, msg->sov - msg->som); |
| 3168 | msg->som = msg->sov; |
| 3169 | req->analysers = AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 9300fb2 | 2010-01-05 00:58:24 +0100 | [diff] [blame] | 3170 | s->rep->analysers = AN_RES_HTTP_XFER_BODY; |
| 3171 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3172 | txn->rsp.msg_state = HTTP_MSG_DONE; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3173 | break; |
| 3174 | } else { |
| 3175 | /* keep-alive not possible */ |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 3176 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 3177 | memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 3178 | rdr.len += 29; |
| 3179 | } else { |
| 3180 | memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23); |
| 3181 | rdr.len += 23; |
| 3182 | } |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 3183 | stream_int_retnclose(req->prod, &rdr); |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3184 | goto return_prx_cond; |
| 3185 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3186 | } |
| 3187 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3188 | |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 3189 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 3190 | * If this happens, then the data will not come immediately, so we must |
| 3191 | * send all what we have without waiting. Note that due to the small gain |
| 3192 | * in waiting for the body of the request, it's easier to simply put the |
| 3193 | * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove |
| 3194 | * itself once used. |
| 3195 | */ |
| 3196 | req->flags |= BF_SEND_DONTWAIT; |
| 3197 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3198 | /* that's OK for us now, let's move on to next analysers */ |
| 3199 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 3200 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3201 | return_bad_req: |
| 3202 | /* We centralize bad requests processing here */ |
| 3203 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 3204 | /* we detected a parsing error. We want to archive this request |
| 3205 | * in the dedicated proxy area for later troubleshooting. |
| 3206 | */ |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 3207 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3208 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3209 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3210 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3211 | txn->status = 400; |
| 3212 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3213 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3214 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3215 | if (s->listener->counters) |
| 3216 | s->listener->counters->failed_req++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 3217 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3218 | return_prx_cond: |
| 3219 | if (!(s->flags & SN_ERR_MASK)) |
| 3220 | s->flags |= SN_ERR_PRXCOND; |
| 3221 | if (!(s->flags & SN_FINST_MASK)) |
| 3222 | s->flags |= SN_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 3223 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3224 | req->analysers = 0; |
| 3225 | req->analyse_exp = TICK_ETERNITY; |
| 3226 | return 0; |
| 3227 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3228 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3229 | /* This function performs all the processing enabled for the current request. |
| 3230 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 3231 | * needs more data, encounters an error, or wants to immediately abort the |
| 3232 | * request. It relies on buffers flags, and updates s->req->analysers. |
| 3233 | */ |
| 3234 | int http_process_request(struct session *s, struct buffer *req, int an_bit) |
| 3235 | { |
| 3236 | struct http_txn *txn = &s->txn; |
| 3237 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3238 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3239 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3240 | /* we need more data */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3241 | buffer_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3242 | return 0; |
| 3243 | } |
| 3244 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3245 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 3246 | now_ms, __FUNCTION__, |
| 3247 | s, |
| 3248 | req, |
| 3249 | req->rex, req->wex, |
| 3250 | req->flags, |
| 3251 | req->l, |
| 3252 | req->analysers); |
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 | * Right now, we know that we have processed the entire headers |
| 3256 | * and that unwanted requests have been filtered out. We can do |
| 3257 | * whatever we want with the remaining request. Also, now we |
| 3258 | * may have separate values for ->fe, ->be. |
| 3259 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3260 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3261 | /* |
| 3262 | * If HTTP PROXY is set we simply get remote server address |
| 3263 | * parsing incoming request. |
| 3264 | */ |
| 3265 | if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) { |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3266 | url2sa(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] | 3267 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3268 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3269 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3270 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3271 | * Note that doing so might move headers in the request, but |
| 3272 | * the fields will stay coherent and the URI will not move. |
| 3273 | * This should only be performed in the backend. |
| 3274 | */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 3275 | 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] | 3276 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
| 3277 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 3278 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3279 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3280 | * 8: the appsession cookie was looked up very early in 1.2, |
| 3281 | * so let's do the same now. |
| 3282 | */ |
| 3283 | |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 3284 | /* It needs to look into the URI unless persistence must be ignored */ |
| 3285 | if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 3286 | get_srv_from_appsession(s, msg->sol + msg->sl.rq.u, msg->sl.rq.u_l); |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3287 | } |
| 3288 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3289 | /* add unique-id if "header-unique-id" is specified */ |
| 3290 | |
| 3291 | if (!LIST_ISEMPTY(&s->fe->format_unique_id)) |
| 3292 | build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id); |
| 3293 | |
| 3294 | if (s->fe->header_unique_id && s->unique_id) { |
| 3295 | int ret = snprintf(trash, global.tune.bufsize, "%s: %s", s->fe->header_unique_id, s->unique_id); |
| 3296 | if (ret < 0 || ret > global.tune.bufsize) |
| 3297 | goto return_bad_req; |
| 3298 | if(unlikely(http_header_add_tail(req, &txn->req, &txn->hdr_idx, trash) < 0)) |
| 3299 | goto return_bad_req; |
| 3300 | } |
| 3301 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3302 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3303 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 3304 | * asks for it. |
| 3305 | */ |
| 3306 | if ((s->fe->options | s->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 3307 | struct hdr_ctx ctx = { .idx = 0 }; |
| 3308 | |
| 3309 | if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) && |
Sagi Bashari | 1611e2d | 2011-10-08 22:48:48 +0200 | [diff] [blame] | 3310 | http_find_header2(s->be->fwdfor_hdr_name, s->be->fwdfor_hdr_len, txn->req.sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 3311 | /* The header is set to be added only if none is present |
| 3312 | * and we found it, so don't do anything. |
| 3313 | */ |
| 3314 | } |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3315 | else if (s->req->prod->addr.from.ss_family == AF_INET) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3316 | /* Add an X-Forwarded-For header unless the source IP is |
| 3317 | * in the 'except' network range. |
| 3318 | */ |
| 3319 | if ((!s->fe->except_mask.s_addr || |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3320 | (((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] | 3321 | != s->fe->except_net.s_addr) && |
| 3322 | (!s->be->except_mask.s_addr || |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3323 | (((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] | 3324 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3325 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3326 | unsigned char *pn; |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3327 | 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] | 3328 | |
| 3329 | /* Note: we rely on the backend to get the header name to be used for |
| 3330 | * x-forwarded-for, because the header is really meant for the backends. |
| 3331 | * However, if the backend did not specify any option, we have to rely |
| 3332 | * on the frontend's header name. |
| 3333 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3334 | if (s->be->fwdfor_hdr_len) { |
| 3335 | len = s->be->fwdfor_hdr_len; |
| 3336 | memcpy(trash, s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3337 | } else { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3338 | len = s->fe->fwdfor_hdr_len; |
| 3339 | memcpy(trash, s->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3340 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3341 | 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] | 3342 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 3343 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 3344 | &txn->hdr_idx, trash, len) < 0)) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3345 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3346 | } |
| 3347 | } |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3348 | else if (s->req->prod->addr.from.ss_family == AF_INET6) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3349 | /* FIXME: for the sake of completeness, we should also support |
| 3350 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3351 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3352 | int len; |
| 3353 | char pn[INET6_ADDRSTRLEN]; |
| 3354 | inet_ntop(AF_INET6, |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3355 | (const void *)&((struct sockaddr_in6 *)(&s->req->prod->addr.from))->sin6_addr, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3356 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3357 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3358 | /* Note: we rely on the backend to get the header name to be used for |
| 3359 | * x-forwarded-for, because the header is really meant for the backends. |
| 3360 | * However, if the backend did not specify any option, we have to rely |
| 3361 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3362 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3363 | if (s->be->fwdfor_hdr_len) { |
| 3364 | len = s->be->fwdfor_hdr_len; |
| 3365 | memcpy(trash, s->be->fwdfor_hdr_name, len); |
| 3366 | } else { |
| 3367 | len = s->fe->fwdfor_hdr_len; |
| 3368 | memcpy(trash, s->fe->fwdfor_hdr_name, len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3369 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3370 | len += sprintf(trash + len, ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3371 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3372 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 3373 | &txn->hdr_idx, trash, len) < 0)) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3374 | goto return_bad_req; |
| 3375 | } |
| 3376 | } |
| 3377 | |
| 3378 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3379 | * 10: add X-Original-To if either the frontend or the backend |
| 3380 | * asks for it. |
| 3381 | */ |
| 3382 | if ((s->fe->options | s->be->options) & PR_O_ORGTO) { |
| 3383 | |
| 3384 | /* FIXME: don't know if IPv6 can handle that case too. */ |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3385 | if (s->req->prod->addr.from.ss_family == AF_INET) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3386 | /* Add an X-Original-To header unless the destination IP is |
| 3387 | * in the 'except' network range. |
| 3388 | */ |
Willy Tarreau | 9b061e3 | 2012-04-07 18:03:52 +0200 | [diff] [blame] | 3389 | stream_sock_get_to_addr(s->req->prod); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3390 | |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3391 | if (s->req->prod->addr.to.ss_family == AF_INET && |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 3392 | ((!s->fe->except_mask_to.s_addr || |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3393 | (((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] | 3394 | != s->fe->except_to.s_addr) && |
| 3395 | (!s->be->except_mask_to.s_addr || |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3396 | (((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] | 3397 | != s->be->except_to.s_addr))) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3398 | int len; |
| 3399 | unsigned char *pn; |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3400 | 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] | 3401 | |
| 3402 | /* Note: we rely on the backend to get the header name to be used for |
| 3403 | * x-original-to, because the header is really meant for the backends. |
| 3404 | * However, if the backend did not specify any option, we have to rely |
| 3405 | * on the frontend's header name. |
| 3406 | */ |
| 3407 | if (s->be->orgto_hdr_len) { |
| 3408 | len = s->be->orgto_hdr_len; |
| 3409 | memcpy(trash, s->be->orgto_hdr_name, len); |
| 3410 | } else { |
| 3411 | len = s->fe->orgto_hdr_len; |
| 3412 | memcpy(trash, s->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3413 | } |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3414 | len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]); |
| 3415 | |
| 3416 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 3417 | &txn->hdr_idx, trash, len) < 0)) |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3418 | goto return_bad_req; |
| 3419 | } |
| 3420 | } |
| 3421 | } |
| 3422 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3423 | /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */ |
| 3424 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Cyril Bonté | 9ea2b9a | 2010-12-29 09:36:56 +0100 | [diff] [blame] | 3425 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3426 | unsigned int want_flags = 0; |
| 3427 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3428 | if (msg->flags & HTTP_MSGF_VER_11) { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 3429 | if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL || |
| 3430 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) && |
| 3431 | !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3432 | want_flags |= TX_CON_CLO_SET; |
| 3433 | } else { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 3434 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
| 3435 | !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) || |
| 3436 | ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3437 | want_flags |= TX_CON_KAL_SET; |
| 3438 | } |
| 3439 | |
| 3440 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 3441 | http_change_connection_header(txn, msg, req, want_flags); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3442 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3443 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3444 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3445 | /* If we have no server assigned yet and we're balancing on url_param |
| 3446 | * with a POST request, we may be interested in checking the body for |
| 3447 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3448 | */ |
| 3449 | if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) && |
| 3450 | s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3451 | s->be->url_param_post_limit != 0 && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3452 | (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3453 | buffer_dont_connect(req); |
| 3454 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3455 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3456 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3457 | if (msg->flags & HTTP_MSGF_XFER_LEN) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3458 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 3459 | #ifdef TCP_QUICKACK |
| 3460 | /* We expect some data from the client. Unless we know for sure |
| 3461 | * we already have a full request, we have to re-enable quick-ack |
| 3462 | * in case we previously disabled it, otherwise we might cause |
| 3463 | * the client to delay further data. |
| 3464 | */ |
| 3465 | if ((s->listener->options & LI_O_NOQUICKACK) && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3466 | ((msg->flags & HTTP_MSGF_TE_CHNK) || |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 3467 | (msg->body_len > req->l - txn->req.eoh - 2))) |
| 3468 | setsockopt(s->si[0].fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); |
| 3469 | #endif |
| 3470 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 3471 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3472 | /************************************************************* |
| 3473 | * OK, that's finished for the headers. We have done what we * |
| 3474 | * could. Let's switch to the DATA state. * |
| 3475 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3476 | req->analyse_exp = TICK_ETERNITY; |
| 3477 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3478 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3479 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3480 | /* OK let's go on with the BODY now */ |
| 3481 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3482 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3483 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3484 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 3485 | /* we detected a parsing error. We want to archive this request |
| 3486 | * in the dedicated proxy area for later troubleshooting. |
| 3487 | */ |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 3488 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe); |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 3489 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3490 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3491 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3492 | txn->status = 400; |
| 3493 | req->analysers = 0; |
| 3494 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3495 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3496 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3497 | if (s->listener->counters) |
| 3498 | s->listener->counters->failed_req++; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3499 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3500 | if (!(s->flags & SN_ERR_MASK)) |
| 3501 | s->flags |= SN_ERR_PRXCOND; |
| 3502 | if (!(s->flags & SN_FINST_MASK)) |
| 3503 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3504 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3505 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3506 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3507 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 3508 | * returns zero, at the beginning because it prevents any other processing |
| 3509 | * from occurring, and at the end because it terminates the request. |
| 3510 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3511 | 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] | 3512 | { |
| 3513 | struct http_txn *txn = &s->txn; |
| 3514 | |
| 3515 | /* This connection is being tarpitted. The CLIENT side has |
| 3516 | * already set the connect expiration date to the right |
| 3517 | * timeout. We just have to check that the client is still |
| 3518 | * there and that the timeout has not expired. |
| 3519 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3520 | buffer_dont_connect(req); |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3521 | if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 && |
| 3522 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 3523 | return 0; |
| 3524 | |
| 3525 | /* We will set the queue timer to the time spent, just for |
| 3526 | * logging purposes. We fake a 500 server error, so that the |
| 3527 | * attacker will not suspect his connection has been tarpitted. |
| 3528 | * It will not cause trouble to the logs because we can exclude |
| 3529 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 3530 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3531 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 3532 | |
| 3533 | txn->status = 500; |
| 3534 | if (req->flags != BF_READ_ERROR) |
| 3535 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500)); |
| 3536 | |
| 3537 | req->analysers = 0; |
| 3538 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3539 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3540 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3541 | if (s->listener->counters) |
| 3542 | s->listener->counters->failed_req++; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3543 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3544 | if (!(s->flags & SN_ERR_MASK)) |
| 3545 | s->flags |= SN_ERR_PRXCOND; |
| 3546 | if (!(s->flags & SN_FINST_MASK)) |
| 3547 | s->flags |= SN_FINST_T; |
| 3548 | return 0; |
| 3549 | } |
| 3550 | |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3551 | /* This function is an analyser which processes the HTTP request body. It looks |
| 3552 | * for parameters to be used for the load balancing algorithm (url_param). It |
| 3553 | * must only be called after the standard HTTP request processing has occurred, |
| 3554 | * because it expects the request to be parsed. It returns zero if it needs to |
| 3555 | * read more data, or 1 once it has completed its analysis. |
| 3556 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3557 | 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] | 3558 | { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3559 | struct http_txn *txn = &s->txn; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3560 | struct http_msg *msg = &s->txn.req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3561 | long long limit = s->be->url_param_post_limit; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3562 | |
| 3563 | /* We have to parse the HTTP request body to find any required data. |
| 3564 | * "balance url_param check_post" should have been the only way to get |
| 3565 | * into this. We were brought here after HTTP header analysis, so all |
| 3566 | * related structures are ready. |
| 3567 | */ |
| 3568 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3569 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 3570 | goto missing_data; |
| 3571 | |
| 3572 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 3573 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 3574 | * send an HTTP/1.1 100 Continue intermediate response. |
| 3575 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3576 | if (msg->flags & HTTP_MSGF_VER_11) { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3577 | struct hdr_ctx ctx; |
| 3578 | ctx.idx = 0; |
| 3579 | /* Expect is allowed in 1.1, look for it */ |
| 3580 | if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) && |
| 3581 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) { |
| 3582 | buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len); |
| 3583 | } |
| 3584 | } |
| 3585 | msg->msg_state = HTTP_MSG_100_SENT; |
| 3586 | } |
| 3587 | |
| 3588 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3589 | /* we have msg->col and msg->sov which both point to the first |
| 3590 | * byte of message body. msg->som still points to the beginning |
| 3591 | * of the message. We must save the body in req->lr because it |
| 3592 | * survives buffer re-alignments. |
| 3593 | */ |
| 3594 | req->lr = req->data + msg->sov; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3595 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3596 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 3597 | else |
| 3598 | msg->msg_state = HTTP_MSG_DATA; |
| 3599 | } |
| 3600 | |
| 3601 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 3602 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3603 | * set ->sov and ->lr to point to the body and switch to DATA or |
| 3604 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3605 | */ |
| 3606 | int ret = http_parse_chunk_size(req, msg); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3607 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3608 | if (!ret) |
| 3609 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3610 | else if (ret < 0) { |
| 3611 | session_inc_http_err_ctr(s); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3612 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3613 | } |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3614 | } |
| 3615 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3616 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3617 | * We have the first non-header byte in msg->col, which is either the |
| 3618 | * beginning of the chunk size or of the data. The first data byte is in |
| 3619 | * msg->sov, which is equal to msg->col when not using transfer-encoding. |
| 3620 | * We're waiting for at least <url_param_post_limit> bytes after msg->sov. |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3621 | */ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3622 | |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 3623 | if (msg->body_len < limit) |
| 3624 | limit = msg->body_len; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3625 | |
Willy Tarreau | 7c96f67 | 2009-12-27 22:47:25 +0100 | [diff] [blame] | 3626 | if (req->l - (msg->sov - msg->som) >= limit) /* we have enough bytes now */ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3627 | goto http_end; |
| 3628 | |
| 3629 | missing_data: |
| 3630 | /* we get here if we need to wait for more data */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3631 | if (req->flags & BF_FULL) { |
| 3632 | session_inc_http_err_ctr(s); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3633 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3634 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3635 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3636 | if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) { |
| 3637 | txn->status = 408; |
| 3638 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408)); |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 3639 | |
| 3640 | if (!(s->flags & SN_ERR_MASK)) |
| 3641 | s->flags |= SN_ERR_CLITO; |
| 3642 | if (!(s->flags & SN_FINST_MASK)) |
| 3643 | s->flags |= SN_FINST_D; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3644 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3645 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3646 | |
| 3647 | /* we get here if we need to wait for more data */ |
| 3648 | if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) { |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3649 | /* Not enough data. We'll re-use the http-request |
| 3650 | * timeout here. Ideally, we should set the timeout |
| 3651 | * relative to the accept() date. We just set the |
| 3652 | * request timeout once at the beginning of the |
| 3653 | * request. |
| 3654 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3655 | buffer_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3656 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 3657 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3658 | return 0; |
| 3659 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3660 | |
| 3661 | http_end: |
| 3662 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 3663 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 3664 | req->analysers &= ~an_bit; |
| 3665 | req->analyse_exp = TICK_ETERNITY; |
| 3666 | return 1; |
| 3667 | |
| 3668 | return_bad_req: /* let's centralize all bad requests */ |
| 3669 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3670 | txn->status = 400; |
| 3671 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
| 3672 | |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 3673 | if (!(s->flags & SN_ERR_MASK)) |
| 3674 | s->flags |= SN_ERR_PRXCOND; |
| 3675 | if (!(s->flags & SN_FINST_MASK)) |
| 3676 | s->flags |= SN_FINST_R; |
| 3677 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3678 | return_err_msg: |
| 3679 | req->analysers = 0; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3680 | s->fe->fe_counters.failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3681 | if (s->listener->counters) |
| 3682 | s->listener->counters->failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3683 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3684 | } |
| 3685 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3686 | int http_send_name_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, struct proxy* be, const char* srv_name) { |
| 3687 | |
| 3688 | struct hdr_ctx ctx; |
| 3689 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3690 | char *hdr_name = be->server_id_hdr_name; |
| 3691 | int hdr_name_len = be->server_id_hdr_len; |
| 3692 | |
| 3693 | char *hdr_val; |
| 3694 | |
William Lallemand | d9e9066 | 2012-01-30 17:27:17 +0100 | [diff] [blame] | 3695 | ctx.idx = 0; |
| 3696 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3697 | while (http_find_header2(hdr_name, hdr_name_len, msg->sol, &txn->hdr_idx, &ctx)) { |
| 3698 | /* remove any existing values from the header */ |
| 3699 | http_remove_header2(msg, buf, &txn->hdr_idx, &ctx); |
| 3700 | } |
| 3701 | |
| 3702 | /* Add the new header requested with the server value */ |
| 3703 | hdr_val = trash; |
| 3704 | memcpy(hdr_val, hdr_name, hdr_name_len); |
| 3705 | hdr_val += hdr_name_len; |
| 3706 | *hdr_val++ = ':'; |
| 3707 | *hdr_val++ = ' '; |
| 3708 | hdr_val += strlcpy2(hdr_val, srv_name, trash + sizeof(trash) - hdr_val); |
| 3709 | http_header_add_tail2(buf, msg, &txn->hdr_idx, trash, hdr_val - trash); |
| 3710 | |
| 3711 | return 0; |
| 3712 | } |
| 3713 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3714 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 3715 | * right now but it works. |
| 3716 | */ |
| 3717 | void http_end_txn_clean_session(struct session *s) |
| 3718 | { |
| 3719 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 3720 | * server's connections. We need a safer way to reinitialize buffer |
| 3721 | * flags. We also need a more accurate method for computing per-request |
| 3722 | * data. |
| 3723 | */ |
| 3724 | http_silent_debug(__LINE__, s); |
| 3725 | |
| 3726 | s->req->cons->flags |= SI_FL_NOLINGER; |
| 3727 | s->req->cons->shutr(s->req->cons); |
| 3728 | s->req->cons->shutw(s->req->cons); |
| 3729 | |
| 3730 | http_silent_debug(__LINE__, s); |
| 3731 | |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 3732 | if (s->flags & SN_BE_ASSIGNED) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3733 | s->be->beconn--; |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 3734 | if (unlikely(s->srv_conn)) |
| 3735 | sess_change_server(s, NULL); |
| 3736 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3737 | |
| 3738 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 3739 | session_process_counters(s); |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 3740 | session_stop_backend_counters(s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3741 | |
| 3742 | if (s->txn.status) { |
| 3743 | int n; |
| 3744 | |
| 3745 | n = s->txn.status / 100; |
| 3746 | if (n < 1 || n > 5) |
| 3747 | n = 0; |
| 3748 | |
| 3749 | if (s->fe->mode == PR_MODE_HTTP) |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3750 | s->fe->fe_counters.p.http.rsp[n]++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3751 | |
Willy Tarreau | 2465779 | 2010-02-26 10:30:28 +0100 | [diff] [blame] | 3752 | if ((s->flags & SN_BE_ASSIGNED) && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3753 | (s->be->mode == PR_MODE_HTTP)) |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3754 | s->be->be_counters.p.http.rsp[n]++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3755 | } |
| 3756 | |
| 3757 | /* don't count other requests' data */ |
| 3758 | s->logs.bytes_in -= s->req->l - s->req->send_max; |
| 3759 | s->logs.bytes_out -= s->rep->l - s->rep->send_max; |
| 3760 | |
| 3761 | /* let's do a final log if we need it */ |
| 3762 | if (s->logs.logwait && |
| 3763 | !(s->flags & SN_MONITOR) && |
| 3764 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
| 3765 | s->do_log(s); |
| 3766 | } |
| 3767 | |
| 3768 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 3769 | s->logs.tv_accept = now; /* corrected date for internal use */ |
| 3770 | tv_zero(&s->logs.tv_request); |
| 3771 | s->logs.t_queue = -1; |
| 3772 | s->logs.t_connect = -1; |
| 3773 | s->logs.t_data = -1; |
| 3774 | s->logs.t_close = 0; |
| 3775 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 3776 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 3777 | |
| 3778 | s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max; |
| 3779 | s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max; |
| 3780 | |
| 3781 | if (s->pend_pos) |
| 3782 | pendconn_free(s->pend_pos); |
| 3783 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 3784 | if (target_srv(&s->target)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3785 | if (s->flags & SN_CURR_SESS) { |
| 3786 | s->flags &= ~SN_CURR_SESS; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 3787 | target_srv(&s->target)->cur_sess--; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3788 | } |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 3789 | if (may_dequeue_tasks(target_srv(&s->target), s->be)) |
| 3790 | process_srv_queue(target_srv(&s->target)); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3791 | } |
| 3792 | |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 3793 | clear_target(&s->target); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3794 | |
| 3795 | s->req->cons->state = s->req->cons->prev_state = SI_ST_INI; |
| 3796 | s->req->cons->fd = -1; /* just to help with debugging */ |
| 3797 | s->req->cons->err_type = SI_ET_NONE; |
Willy Tarreau | 0b3a411 | 2011-03-27 19:16:56 +0200 | [diff] [blame] | 3798 | s->req->cons->conn_retries = 0; /* used for logging too */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3799 | s->req->cons->err_loc = NULL; |
| 3800 | s->req->cons->exp = TICK_ETERNITY; |
| 3801 | s->req->cons->flags = SI_FL_NONE; |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 3802 | s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST|BF_NEVER_WAIT); |
| 3803 | 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] | 3804 | 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] | 3805 | s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE); |
| 3806 | s->txn.meth = 0; |
| 3807 | http_reset_txn(s); |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 3808 | s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ; |
Willy Tarreau | ee55dc0 | 2010-06-01 10:56:34 +0200 | [diff] [blame] | 3809 | if (s->fe->options2 & PR_O2_INDEPSTR) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3810 | s->req->cons->flags |= SI_FL_INDEP_STR; |
| 3811 | |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 3812 | if (s->fe->options2 & PR_O2_NODELAY) { |
| 3813 | s->req->flags |= BF_NEVER_WAIT; |
| 3814 | s->rep->flags |= BF_NEVER_WAIT; |
| 3815 | } |
| 3816 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3817 | /* if the request buffer is not empty, it means we're |
| 3818 | * about to process another request, so send pending |
| 3819 | * data with MSG_MORE to merge TCP packets when possible. |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 3820 | * Just don't do this if the buffer is close to be full, |
| 3821 | * because the request will wait for it to flush a little |
| 3822 | * bit before proceeding. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3823 | */ |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 3824 | if (s->req->l > s->req->send_max) { |
| 3825 | if (s->rep->send_max && |
| 3826 | !(s->rep->flags & BF_FULL) && |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 3827 | s->rep->r <= s->rep->data + s->rep->size - global.tune.maxrewrite) |
| 3828 | s->rep->flags |= BF_EXPECT_MORE; |
| 3829 | } |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3830 | |
| 3831 | /* we're removing the analysers, we MUST re-enable events detection */ |
| 3832 | buffer_auto_read(s->req); |
| 3833 | buffer_auto_close(s->req); |
| 3834 | buffer_auto_read(s->rep); |
| 3835 | buffer_auto_close(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3836 | |
| 3837 | /* make ->lr point to the first non-forwarded byte */ |
| 3838 | s->req->lr = s->req->w + s->req->send_max; |
| 3839 | if (s->req->lr >= s->req->data + s->req->size) |
| 3840 | s->req->lr -= s->req->size; |
| 3841 | s->rep->lr = s->rep->w + s->rep->send_max; |
| 3842 | if (s->rep->lr >= s->rep->data + s->rep->size) |
| 3843 | s->rep->lr -= s->req->size; |
| 3844 | |
Willy Tarreau | 342b11c | 2010-11-24 16:22:09 +0100 | [diff] [blame] | 3845 | s->req->analysers = s->listener->analysers; |
| 3846 | s->req->analysers &= ~AN_REQ_DECODE_PROXY; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3847 | s->rep->analysers = 0; |
| 3848 | |
| 3849 | http_silent_debug(__LINE__, s); |
| 3850 | } |
| 3851 | |
| 3852 | |
| 3853 | /* This function updates the request state machine according to the response |
| 3854 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 3855 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 3856 | * it is only used to find when a request/response couple is complete. Both |
| 3857 | * this function and its equivalent should loop until both return zero. It |
| 3858 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 3859 | */ |
| 3860 | int http_sync_req_state(struct session *s) |
| 3861 | { |
| 3862 | struct buffer *buf = s->req; |
| 3863 | struct http_txn *txn = &s->txn; |
| 3864 | unsigned int old_flags = buf->flags; |
| 3865 | unsigned int old_state = txn->req.msg_state; |
| 3866 | |
| 3867 | http_silent_debug(__LINE__, s); |
| 3868 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) |
| 3869 | return 0; |
| 3870 | |
| 3871 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3872 | /* No need to read anymore, the request was completely parsed. |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 3873 | * We can shut the read side unless we want to abort_on_close, |
| 3874 | * or we have a POST request. The issue with POST requests is |
| 3875 | * that some browsers still send a CRLF after the request, and |
| 3876 | * this CRLF must be read so that it does not remain in the kernel |
| 3877 | * buffers, otherwise a close could cause an RST on some systems |
| 3878 | * (eg: Linux). |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3879 | */ |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 3880 | 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] | 3881 | buffer_dont_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3882 | |
| 3883 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 3884 | goto wait_other_side; |
| 3885 | |
| 3886 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 3887 | /* The server has not finished to respond, so we |
| 3888 | * don't want to move in order not to upset it. |
| 3889 | */ |
| 3890 | goto wait_other_side; |
| 3891 | } |
| 3892 | |
| 3893 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 3894 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3895 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3896 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
| 3897 | goto wait_other_side; |
| 3898 | } |
| 3899 | |
| 3900 | /* When we get here, it means that both the request and the |
| 3901 | * response have finished receiving. Depending on the connection |
| 3902 | * mode, we'll have to wait for the last bytes to leave in either |
| 3903 | * direction, and sometimes for a close to be effective. |
| 3904 | */ |
| 3905 | |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3906 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 3907 | /* Server-close mode : queue a connection close to the server */ |
| 3908 | if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3909 | buffer_shutw_now(buf); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3910 | } |
| 3911 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 3912 | /* Option forceclose is set, or either side wants to close, |
| 3913 | * let's enforce it now that we're not expecting any new |
| 3914 | * data to come. The caller knows the session is complete |
| 3915 | * once both states are CLOSED. |
| 3916 | */ |
| 3917 | if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3918 | buffer_shutr_now(buf); |
| 3919 | buffer_shutw_now(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3920 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3921 | } |
| 3922 | else { |
| 3923 | /* The last possible modes are keep-alive and tunnel. Since tunnel |
| 3924 | * mode does not set the body analyser, we can't reach this place |
| 3925 | * in tunnel mode, so we're left with keep-alive only. |
| 3926 | * This mode is currently not implemented, we switch to tunnel mode. |
| 3927 | */ |
| 3928 | buffer_auto_read(buf); |
| 3929 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3930 | } |
| 3931 | |
| 3932 | if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) { |
| 3933 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3934 | buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
| 3935 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3936 | if (!(buf->flags & BF_OUT_EMPTY)) { |
| 3937 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 3938 | goto http_msg_closing; |
| 3939 | } |
| 3940 | else { |
| 3941 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3942 | goto http_msg_closed; |
| 3943 | } |
| 3944 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3945 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3946 | } |
| 3947 | |
| 3948 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 3949 | http_msg_closing: |
| 3950 | /* nothing else to forward, just waiting for the output buffer |
| 3951 | * to be empty and for the shutw_now to take effect. |
| 3952 | */ |
| 3953 | if (buf->flags & BF_OUT_EMPTY) { |
| 3954 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3955 | goto http_msg_closed; |
| 3956 | } |
| 3957 | else if (buf->flags & BF_SHUTW) { |
| 3958 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3959 | goto wait_other_side; |
| 3960 | } |
| 3961 | } |
| 3962 | |
| 3963 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 3964 | http_msg_closed: |
| 3965 | goto wait_other_side; |
| 3966 | } |
| 3967 | |
| 3968 | wait_other_side: |
| 3969 | http_silent_debug(__LINE__, s); |
| 3970 | return txn->req.msg_state != old_state || buf->flags != old_flags; |
| 3971 | } |
| 3972 | |
| 3973 | |
| 3974 | /* This function updates the response state machine according to the request |
| 3975 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 3976 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 3977 | * it is only used to find when a request/response couple is complete. Both |
| 3978 | * this function and its equivalent should loop until both return zero. It |
| 3979 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 3980 | */ |
| 3981 | int http_sync_res_state(struct session *s) |
| 3982 | { |
| 3983 | struct buffer *buf = s->rep; |
| 3984 | struct http_txn *txn = &s->txn; |
| 3985 | unsigned int old_flags = buf->flags; |
| 3986 | unsigned int old_state = txn->rsp.msg_state; |
| 3987 | |
| 3988 | http_silent_debug(__LINE__, s); |
| 3989 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 3990 | return 0; |
| 3991 | |
| 3992 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 3993 | /* In theory, we don't need to read anymore, but we must |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3994 | * still monitor the server connection for a possible close |
| 3995 | * while the request is being uploaded, so we don't disable |
| 3996 | * reading. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3997 | */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3998 | /* buffer_dont_read(buf); */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3999 | |
| 4000 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 4001 | goto wait_other_side; |
| 4002 | |
| 4003 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 4004 | /* The client seems to still be sending data, probably |
| 4005 | * because we got an error response during an upload. |
| 4006 | * We have the choice of either breaking the connection |
| 4007 | * or letting it pass through. Let's do the later. |
| 4008 | */ |
| 4009 | goto wait_other_side; |
| 4010 | } |
| 4011 | |
| 4012 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 4013 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4014 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4015 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
| 4016 | goto wait_other_side; |
| 4017 | } |
| 4018 | |
| 4019 | /* When we get here, it means that both the request and the |
| 4020 | * response have finished receiving. Depending on the connection |
| 4021 | * mode, we'll have to wait for the last bytes to leave in either |
| 4022 | * direction, and sometimes for a close to be effective. |
| 4023 | */ |
| 4024 | |
| 4025 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 4026 | /* Server-close mode : shut read and wait for the request |
| 4027 | * side to close its output buffer. The caller will detect |
| 4028 | * when we're in DONE and the other is in CLOSED and will |
| 4029 | * catch that for the final cleanup. |
| 4030 | */ |
| 4031 | if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW))) |
| 4032 | buffer_shutr_now(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4033 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4034 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4035 | /* Option forceclose is set, or either side wants to close, |
| 4036 | * let's enforce it now that we're not expecting any new |
| 4037 | * data to come. The caller knows the session is complete |
| 4038 | * once both states are CLOSED. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4039 | */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4040 | if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) { |
| 4041 | buffer_shutr_now(buf); |
| 4042 | buffer_shutw_now(buf); |
| 4043 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4044 | } |
| 4045 | else { |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4046 | /* The last possible modes are keep-alive and tunnel. Since tunnel |
| 4047 | * mode does not set the body analyser, we can't reach this place |
| 4048 | * in tunnel mode, so we're left with keep-alive only. |
| 4049 | * This mode is currently not implemented, we switch to tunnel mode. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4050 | */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4051 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4052 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4053 | } |
| 4054 | |
| 4055 | if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) { |
| 4056 | /* if we've just closed an output, let's switch */ |
| 4057 | if (!(buf->flags & BF_OUT_EMPTY)) { |
| 4058 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 4059 | goto http_msg_closing; |
| 4060 | } |
| 4061 | else { |
| 4062 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4063 | goto http_msg_closed; |
| 4064 | } |
| 4065 | } |
| 4066 | goto wait_other_side; |
| 4067 | } |
| 4068 | |
| 4069 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 4070 | http_msg_closing: |
| 4071 | /* nothing else to forward, just waiting for the output buffer |
| 4072 | * to be empty and for the shutw_now to take effect. |
| 4073 | */ |
| 4074 | if (buf->flags & BF_OUT_EMPTY) { |
| 4075 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4076 | goto http_msg_closed; |
| 4077 | } |
| 4078 | else if (buf->flags & BF_SHUTW) { |
| 4079 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4080 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4081 | if (target_srv(&s->target)) |
| 4082 | target_srv(&s->target)->counters.cli_aborts++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4083 | goto wait_other_side; |
| 4084 | } |
| 4085 | } |
| 4086 | |
| 4087 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 4088 | http_msg_closed: |
| 4089 | /* drop any pending data */ |
| 4090 | buffer_ignore(buf, buf->l - buf->send_max); |
| 4091 | buffer_auto_close(buf); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4092 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4093 | goto wait_other_side; |
| 4094 | } |
| 4095 | |
| 4096 | wait_other_side: |
| 4097 | http_silent_debug(__LINE__, s); |
| 4098 | return txn->rsp.msg_state != old_state || buf->flags != old_flags; |
| 4099 | } |
| 4100 | |
| 4101 | |
| 4102 | /* Resync the request and response state machines. Return 1 if either state |
| 4103 | * changes. |
| 4104 | */ |
| 4105 | int http_resync_states(struct session *s) |
| 4106 | { |
| 4107 | struct http_txn *txn = &s->txn; |
| 4108 | int old_req_state = txn->req.msg_state; |
| 4109 | int old_res_state = txn->rsp.msg_state; |
| 4110 | |
| 4111 | http_silent_debug(__LINE__, s); |
| 4112 | http_sync_req_state(s); |
| 4113 | while (1) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4114 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4115 | if (!http_sync_res_state(s)) |
| 4116 | break; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4117 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4118 | if (!http_sync_req_state(s)) |
| 4119 | break; |
| 4120 | } |
| 4121 | http_silent_debug(__LINE__, s); |
| 4122 | /* OK, both state machines agree on a compatible state. |
| 4123 | * There are a few cases we're interested in : |
| 4124 | * - HTTP_MSG_TUNNEL on either means we have to disable both analysers |
| 4125 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 4126 | * directions, so let's simply disable both analysers. |
| 4127 | * - HTTP_MSG_CLOSED on the response only means we must abort the |
| 4128 | * request. |
| 4129 | * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response |
| 4130 | * with server-close mode means we've completed one request and we |
| 4131 | * must re-initialize the server connection. |
| 4132 | */ |
| 4133 | |
| 4134 | if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 4135 | txn->rsp.msg_state == HTTP_MSG_TUNNEL || |
| 4136 | (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 4137 | txn->rsp.msg_state == HTTP_MSG_CLOSED)) { |
| 4138 | s->req->analysers = 0; |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4139 | buffer_auto_close(s->req); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4140 | buffer_auto_read(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4141 | s->rep->analysers = 0; |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4142 | buffer_auto_close(s->rep); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4143 | buffer_auto_read(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4144 | } |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4145 | else if (txn->rsp.msg_state == HTTP_MSG_CLOSED || |
| 4146 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4147 | txn->req.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4148 | (s->rep->flags & BF_SHUTW)) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4149 | s->rep->analysers = 0; |
| 4150 | buffer_auto_close(s->rep); |
| 4151 | buffer_auto_read(s->rep); |
| 4152 | s->req->analysers = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4153 | buffer_abort(s->req); |
| 4154 | buffer_auto_close(s->req); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4155 | buffer_auto_read(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4156 | buffer_ignore(s->req, s->req->l - s->req->send_max); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4157 | } |
| 4158 | else if (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 4159 | txn->rsp.msg_state == HTTP_MSG_DONE && |
| 4160 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) { |
| 4161 | /* server-close: terminate this server connection and |
| 4162 | * reinitialize a fresh-new transaction. |
| 4163 | */ |
| 4164 | http_end_txn_clean_session(s); |
| 4165 | } |
| 4166 | |
| 4167 | http_silent_debug(__LINE__, s); |
| 4168 | return txn->req.msg_state != old_req_state || |
| 4169 | txn->rsp.msg_state != old_res_state; |
| 4170 | } |
| 4171 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4172 | /* This function is an analyser which forwards request body (including chunk |
| 4173 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 4174 | * zero byte. The only situation where it must not be called is when we're in |
| 4175 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 4176 | * remaining data and to resync after end of body. It expects the msg_state to |
| 4177 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 4178 | * 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] | 4179 | * 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] | 4180 | * bytes of pending data + the headers if not already done (between som and sov). |
| 4181 | * It eventually adjusts som to match sov after the data in between have been sent. |
| 4182 | */ |
| 4183 | int http_request_forward_body(struct session *s, struct buffer *req, int an_bit) |
| 4184 | { |
| 4185 | struct http_txn *txn = &s->txn; |
| 4186 | struct http_msg *msg = &s->txn.req; |
| 4187 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4188 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4189 | return 0; |
| 4190 | |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 4191 | if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) || |
| 4192 | ((req->flags & BF_SHUTW) && (req->to_forward || req->send_max))) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4193 | /* Output closed while we were sending data. We must abort and |
| 4194 | * wake the other side up. |
| 4195 | */ |
| 4196 | msg->msg_state = HTTP_MSG_ERROR; |
| 4197 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 4198 | return 1; |
| 4199 | } |
| 4200 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4201 | /* in most states, we should abort in case of early close */ |
| 4202 | buffer_auto_close(req); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4203 | |
| 4204 | /* Note that we don't have to send 100-continue back because we don't |
| 4205 | * need the data to complete our job, and it's up to the server to |
| 4206 | * decide whether to return 100, 417 or anything else in return of |
| 4207 | * an "Expect: 100-continue" header. |
| 4208 | */ |
| 4209 | |
| 4210 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 4211 | /* we have msg->col and msg->sov which both point to the first |
| 4212 | * byte of message body. msg->som still points to the beginning |
| 4213 | * of the message. We must save the body in req->lr because it |
| 4214 | * survives buffer re-alignments. |
| 4215 | */ |
| 4216 | req->lr = req->data + msg->sov; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4217 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4218 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 4219 | else { |
| 4220 | msg->msg_state = HTTP_MSG_DATA; |
| 4221 | } |
| 4222 | } |
| 4223 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4224 | while (1) { |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 4225 | int bytes; |
| 4226 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4227 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 4228 | /* we may have some data pending */ |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 4229 | bytes = msg->sov - msg->som; |
| 4230 | if (msg->chunk_len || bytes) { |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 4231 | msg->som = msg->sov; |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 4232 | if (likely(bytes < 0)) /* sov may have wrapped at the end */ |
| 4233 | bytes += req->size; |
| 4234 | msg->chunk_len += (unsigned int)bytes; |
| 4235 | msg->chunk_len -= buffer_forward(req, msg->chunk_len); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 4236 | } |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 4237 | |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4238 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 4239 | /* must still forward */ |
| 4240 | if (req->to_forward) |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4241 | goto missing_data; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4242 | |
| 4243 | /* nothing left to forward */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4244 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4245 | msg->msg_state = HTTP_MSG_DATA_CRLF; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4246 | else |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4247 | msg->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4248 | } |
| 4249 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4250 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4251 | * set ->sov and ->lr to point to the body and switch to DATA or |
| 4252 | * TRAILERS state. |
| 4253 | */ |
| 4254 | int ret = http_parse_chunk_size(req, msg); |
| 4255 | |
| 4256 | if (!ret) |
| 4257 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4258 | else if (ret < 0) { |
| 4259 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4260 | if (msg->err_pos >= 0) |
| 4261 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_CHUNK_SIZE, s->be); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4262 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4263 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4264 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4265 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4266 | else if (msg->msg_state == HTTP_MSG_DATA_CRLF) { |
| 4267 | /* we want the CRLF after the data */ |
| 4268 | int ret; |
| 4269 | |
Willy Tarreau | d3347ee | 2010-01-04 02:02:25 +0100 | [diff] [blame] | 4270 | req->lr = req->w + req->send_max; |
| 4271 | if (req->lr >= req->data + req->size) |
| 4272 | req->lr -= req->size; |
| 4273 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4274 | ret = http_skip_chunk_crlf(req, msg); |
| 4275 | |
| 4276 | if (ret == 0) |
| 4277 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4278 | else if (ret < 0) { |
| 4279 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4280 | if (msg->err_pos >= 0) |
| 4281 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_DATA_CRLF, s->be); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4282 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4283 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4284 | /* we're in MSG_CHUNK_SIZE now */ |
| 4285 | } |
| 4286 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
| 4287 | int ret = http_forward_trailers(req, msg); |
| 4288 | |
| 4289 | if (ret == 0) |
| 4290 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4291 | else if (ret < 0) { |
| 4292 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4293 | if (msg->err_pos >= 0) |
| 4294 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_TRAILERS, s->be); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4295 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4296 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4297 | /* we're in HTTP_MSG_DONE now */ |
| 4298 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4299 | else { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4300 | int old_state = msg->msg_state; |
| 4301 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4302 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4303 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4304 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 4305 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
| 4306 | buffer_dont_close(req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4307 | if (http_resync_states(s)) { |
| 4308 | /* some state changes occurred, maybe the analyser |
| 4309 | * was disabled too. |
Willy Tarreau | face839 | 2010-01-03 11:37:54 +0100 | [diff] [blame] | 4310 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 4311 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 4312 | if (req->flags & BF_SHUTW) { |
| 4313 | /* request errors are most likely due to |
| 4314 | * the server aborting the transfer. |
| 4315 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4316 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 4317 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4318 | if (msg->err_pos >= 0) |
| 4319 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, old_state, s->be); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4320 | goto return_bad_req; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 4321 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4322 | return 1; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4323 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 4324 | |
| 4325 | /* If "option abortonclose" is set on the backend, we |
| 4326 | * want to monitor the client's connection and forward |
| 4327 | * any shutdown notification to the server, which will |
| 4328 | * decide whether to close or to go on processing the |
| 4329 | * request. |
| 4330 | */ |
| 4331 | if (s->be->options & PR_O_ABRT_CLOSE) { |
| 4332 | buffer_auto_read(req); |
| 4333 | buffer_auto_close(req); |
| 4334 | } |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4335 | else if (s->txn.meth == HTTP_METH_POST) { |
| 4336 | /* POST requests may require to read extra CRLF |
| 4337 | * sent by broken browsers and which could cause |
| 4338 | * an RST to be sent upon close on some systems |
| 4339 | * (eg: Linux). |
| 4340 | */ |
| 4341 | buffer_auto_read(req); |
| 4342 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 4343 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4344 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4345 | } |
| 4346 | } |
| 4347 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4348 | missing_data: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4349 | /* stop waiting for data if the input is closed before the end */ |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4350 | if (req->flags & BF_SHUTR) { |
| 4351 | if (!(s->flags & SN_ERR_MASK)) |
| 4352 | s->flags |= SN_ERR_CLICL; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4353 | if (!(s->flags & SN_FINST_MASK)) { |
| 4354 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 4355 | s->flags |= SN_FINST_H; |
| 4356 | else |
| 4357 | s->flags |= SN_FINST_D; |
| 4358 | } |
| 4359 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4360 | s->fe->fe_counters.cli_aborts++; |
| 4361 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4362 | if (target_srv(&s->target)) |
| 4363 | target_srv(&s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4364 | |
| 4365 | goto return_bad_req_stats_ok; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4366 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4367 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4368 | /* waiting for the last bits to leave the buffer */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4369 | if (req->flags & BF_SHUTW) |
| 4370 | goto aborted_xfer; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4371 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4372 | /* When TE: chunked is used, we need to get there again to parse remaining |
| 4373 | * chunks even if the client has closed, so we don't want to set BF_DONTCLOSE. |
| 4374 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4375 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4376 | buffer_dont_close(req); |
| 4377 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4378 | /* 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] | 4379 | * what we did. So we always set the BF_EXPECT_MORE flag so that the |
| 4380 | * 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] | 4381 | * modes are already handled by the stream sock layer. We must not do |
| 4382 | * this in content-length mode because it could present the MSG_MORE |
| 4383 | * flag with the last block of forwarded data, which would cause an |
| 4384 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4385 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4386 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 869fc1e | 2012-03-05 08:29:20 +0100 | [diff] [blame] | 4387 | req->flags |= BF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4388 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4389 | http_silent_debug(__LINE__, s); |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4390 | return 0; |
| 4391 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4392 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4393 | s->fe->fe_counters.failed_req++; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4394 | if (s->listener->counters) |
| 4395 | s->listener->counters->failed_req++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4396 | return_bad_req_stats_ok: |
| 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 = 400; |
| 4403 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
| 4404 | } |
| 4405 | req->analysers = 0; |
| 4406 | 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] | 4407 | |
| 4408 | if (!(s->flags & SN_ERR_MASK)) |
| 4409 | s->flags |= SN_ERR_PRXCOND; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4410 | if (!(s->flags & SN_FINST_MASK)) { |
| 4411 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 4412 | s->flags |= SN_FINST_H; |
| 4413 | else |
| 4414 | s->flags |= SN_FINST_D; |
| 4415 | } |
| 4416 | return 0; |
| 4417 | |
| 4418 | aborted_xfer: |
| 4419 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4420 | if (txn->status) { |
| 4421 | /* Note: we don't send any error if some data were already sent */ |
| 4422 | stream_int_retnclose(req->prod, NULL); |
| 4423 | } else { |
| 4424 | txn->status = 502; |
| 4425 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_502)); |
| 4426 | } |
| 4427 | req->analysers = 0; |
| 4428 | s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */ |
| 4429 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4430 | s->fe->fe_counters.srv_aborts++; |
| 4431 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4432 | if (target_srv(&s->target)) |
| 4433 | target_srv(&s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4434 | |
| 4435 | if (!(s->flags & SN_ERR_MASK)) |
| 4436 | s->flags |= SN_ERR_SRVCL; |
| 4437 | if (!(s->flags & SN_FINST_MASK)) { |
| 4438 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 4439 | s->flags |= SN_FINST_H; |
| 4440 | else |
| 4441 | s->flags |= SN_FINST_D; |
| 4442 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4443 | return 0; |
| 4444 | } |
| 4445 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4446 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 4447 | * processing can continue on next analysers, or zero if it either needs more |
| 4448 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
| 4449 | * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers |
| 4450 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 4451 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4452 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4453 | 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] | 4454 | { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4455 | struct http_txn *txn = &s->txn; |
| 4456 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4457 | struct hdr_ctx ctx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4458 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4459 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4460 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4461 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 4462 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 4463 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4464 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 4465 | rep, |
| 4466 | rep->rex, rep->wex, |
| 4467 | rep->flags, |
| 4468 | rep->l, |
| 4469 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 4470 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4471 | /* |
| 4472 | * Now parse the partial (or complete) lines. |
| 4473 | * We will check the response syntax, and also join multi-line |
| 4474 | * headers. An index of all the lines will be elaborated while |
| 4475 | * parsing. |
| 4476 | * |
| 4477 | * For the parsing, we use a 28 states FSM. |
| 4478 | * |
| 4479 | * Here is the information we currently have : |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4480 | * rep->data + msg->som = beginning of response |
| 4481 | * rep->data + msg->eoh = end of processed headers / start of current one |
| 4482 | * msg->eol = end of current header or line (LF or CRLF) |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4483 | * rep->lr = first non-visited byte |
| 4484 | * rep->r = end of data |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4485 | * Once we reach MSG_BODY, rep->sol = rep->data + msg->som |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4486 | */ |
| 4487 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4488 | /* There's a protected area at the end of the buffer for rewriting |
| 4489 | * purposes. We don't want to start to parse the request if the |
| 4490 | * protected area is affected, because we may have to move processed |
| 4491 | * data later, which is much more complicated. |
| 4492 | */ |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 4493 | if (rep->l && msg->msg_state < HTTP_MSG_ERROR) { |
| 4494 | if (unlikely((rep->flags & BF_FULL) || |
| 4495 | rep->r < rep->lr || |
| 4496 | rep->r > rep->data + rep->size - global.tune.maxrewrite)) { |
| 4497 | if (rep->send_max) { |
| 4498 | /* 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] | 4499 | if (rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) |
| 4500 | goto abort_response; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 4501 | buffer_dont_close(rep); |
| 4502 | rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 4503 | return 0; |
| 4504 | } |
| 4505 | if (rep->l <= rep->size - global.tune.maxrewrite) |
| 4506 | http_buffer_heavy_realign(rep, msg); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4507 | } |
| 4508 | |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 4509 | if (likely(rep->lr < rep->r)) |
| 4510 | http_msg_analyzer(rep, msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4511 | } |
| 4512 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4513 | /* 1: we might have to print this header in debug mode */ |
| 4514 | if (unlikely((global.mode & MODE_DEBUG) && |
| 4515 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | c3bfeeb | 2010-04-16 09:14:45 +0200 | [diff] [blame] | 4516 | msg->sol && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4517 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4518 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4519 | |
Willy Tarreau | 663308b | 2010-06-07 14:06:08 +0200 | [diff] [blame] | 4520 | sol = rep->data + msg->som; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 4521 | eol = sol + msg->sl.st.l; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4522 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4523 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4524 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 4525 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4526 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4527 | while (cur_idx) { |
| 4528 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 4529 | debug_hdr("srvhdr", s, sol, eol); |
| 4530 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 4531 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 4532 | } |
| 4533 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4534 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4535 | /* |
| 4536 | * Now we quickly check if we have found a full valid response. |
| 4537 | * If not so, we check the FD and buffer states before leaving. |
| 4538 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4539 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4540 | * responses are checked first. |
| 4541 | * |
| 4542 | * Depending on whether the client is still there or not, we |
| 4543 | * may send an error response back or not. Note that normally |
| 4544 | * we should only check for HTTP status there, and check I/O |
| 4545 | * errors somewhere else. |
| 4546 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4547 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4548 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4549 | /* Invalid response */ |
| 4550 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 4551 | /* we detected a parsing error. We want to archive this response |
| 4552 | * in the dedicated proxy area for later troubleshooting. |
| 4553 | */ |
| 4554 | hdr_response_bad: |
| 4555 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 4556 | http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4557 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4558 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4559 | if (target_srv(&s->target)) { |
| 4560 | target_srv(&s->target)->counters.failed_resp++; |
| 4561 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4562 | } |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 4563 | abort_response: |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4564 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4565 | rep->analysers = 0; |
| 4566 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4567 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 0b89fbb | 2010-02-02 09:57:24 +0100 | [diff] [blame] | 4568 | buffer_ignore(rep, rep->l - rep->send_max); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4569 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
| 4570 | |
| 4571 | if (!(s->flags & SN_ERR_MASK)) |
| 4572 | s->flags |= SN_ERR_PRXCOND; |
| 4573 | if (!(s->flags & SN_FINST_MASK)) |
| 4574 | s->flags |= SN_FINST_H; |
| 4575 | |
| 4576 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4577 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4578 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4579 | /* too large response does not fit in buffer. */ |
| 4580 | else if (rep->flags & BF_FULL) { |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 4581 | if (msg->err_pos < 0) |
| 4582 | msg->err_pos = rep->l; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4583 | goto hdr_response_bad; |
| 4584 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4585 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4586 | /* read error */ |
| 4587 | else if (rep->flags & BF_READ_ERROR) { |
| 4588 | if (msg->err_pos >= 0) |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 4589 | http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4590 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4591 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4592 | if (target_srv(&s->target)) { |
| 4593 | target_srv(&s->target)->counters.failed_resp++; |
| 4594 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_ERROR); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4595 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 4596 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4597 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4598 | rep->analysers = 0; |
| 4599 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4600 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 0b89fbb | 2010-02-02 09:57:24 +0100 | [diff] [blame] | 4601 | buffer_ignore(rep, rep->l - rep->send_max); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4602 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 4603 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4604 | if (!(s->flags & SN_ERR_MASK)) |
| 4605 | s->flags |= SN_ERR_SRVCL; |
| 4606 | if (!(s->flags & SN_FINST_MASK)) |
| 4607 | s->flags |= SN_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 4608 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4609 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4610 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4611 | /* read timeout : return a 504 to the client. */ |
| 4612 | else if (rep->flags & BF_READ_TIMEOUT) { |
| 4613 | if (msg->err_pos >= 0) |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 4614 | http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4615 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4616 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4617 | if (target_srv(&s->target)) { |
| 4618 | target_srv(&s->target)->counters.failed_resp++; |
| 4619 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4620 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4621 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4622 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4623 | rep->analysers = 0; |
| 4624 | txn->status = 504; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4625 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 0b89fbb | 2010-02-02 09:57:24 +0100 | [diff] [blame] | 4626 | buffer_ignore(rep, rep->l - rep->send_max); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4627 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504)); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4628 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4629 | if (!(s->flags & SN_ERR_MASK)) |
| 4630 | s->flags |= SN_ERR_SRVTO; |
| 4631 | if (!(s->flags & SN_FINST_MASK)) |
| 4632 | s->flags |= SN_FINST_H; |
| 4633 | return 0; |
| 4634 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 4635 | |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 4636 | /* 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] | 4637 | else if (rep->flags & BF_SHUTR) { |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 4638 | if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0) |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 4639 | http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4640 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4641 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4642 | if (target_srv(&s->target)) { |
| 4643 | target_srv(&s->target)->counters.failed_resp++; |
| 4644 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4645 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4646 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4647 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4648 | rep->analysers = 0; |
| 4649 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4650 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 0b89fbb | 2010-02-02 09:57:24 +0100 | [diff] [blame] | 4651 | buffer_ignore(rep, rep->l - rep->send_max); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4652 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4653 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4654 | if (!(s->flags & SN_ERR_MASK)) |
| 4655 | s->flags |= SN_ERR_SRVCL; |
| 4656 | if (!(s->flags & SN_FINST_MASK)) |
| 4657 | s->flags |= SN_FINST_H; |
| 4658 | return 0; |
| 4659 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4660 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4661 | /* write error to client (we don't send any message then) */ |
| 4662 | else if (rep->flags & BF_WRITE_ERROR) { |
| 4663 | if (msg->err_pos >= 0) |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 4664 | http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe); |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4665 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4666 | s->be->be_counters.failed_resp++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4667 | rep->analysers = 0; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4668 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4669 | |
| 4670 | if (!(s->flags & SN_ERR_MASK)) |
| 4671 | s->flags |= SN_ERR_CLICL; |
| 4672 | if (!(s->flags & SN_FINST_MASK)) |
| 4673 | s->flags |= SN_FINST_H; |
| 4674 | |
| 4675 | /* process_session() will take care of the error */ |
| 4676 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4677 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4678 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4679 | buffer_dont_close(rep); |
| 4680 | return 0; |
| 4681 | } |
| 4682 | |
| 4683 | /* More interesting part now : we know that we have a complete |
| 4684 | * response which at least looks like HTTP. We have an indicator |
| 4685 | * of each header's length, so we can parse them quickly. |
| 4686 | */ |
| 4687 | |
| 4688 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 4689 | http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4690 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4691 | /* |
| 4692 | * 1: get the status code |
| 4693 | */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4694 | n = msg->sol[msg->sl.st.c] - '0'; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4695 | if (n < 1 || n > 5) |
| 4696 | n = 0; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4697 | /* when the client triggers a 4xx from the server, it's most often due |
| 4698 | * to a missing object or permission. These events should be tracked |
| 4699 | * because if they happen often, it may indicate a brute force or a |
| 4700 | * vulnerability scan. |
| 4701 | */ |
| 4702 | if (n == 4) |
| 4703 | session_inc_http_err_ctr(s); |
| 4704 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4705 | if (target_srv(&s->target)) |
| 4706 | target_srv(&s->target)->counters.p.http.rsp[n]++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4707 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4708 | /* check if the response is HTTP/1.1 or above */ |
| 4709 | if ((msg->sl.st.v_l == 8) && |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4710 | ((msg->sol[5] > '1') || |
| 4711 | ((msg->sol[5] == '1') && |
| 4712 | (msg->sol[7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4713 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4714 | |
| 4715 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4716 | 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] | 4717 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4718 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4719 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4720 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4721 | txn->status = strl2ui(msg->sol + msg->sl.st.c, msg->sl.st.c_l); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4722 | |
Willy Tarreau | 3965040 | 2010-03-15 19:44:39 +0100 | [diff] [blame] | 4723 | /* Adjust server's health based on status code. Note: status codes 501 |
| 4724 | * and 505 are triggered on demand by client request, so we must not |
| 4725 | * count them as server failures. |
| 4726 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4727 | if (target_srv(&s->target)) { |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 4728 | 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] | 4729 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_OK); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 4730 | else |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4731 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_STS); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 4732 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4733 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4734 | /* |
| 4735 | * 2: check for cacheability. |
| 4736 | */ |
| 4737 | |
| 4738 | switch (txn->status) { |
| 4739 | case 200: |
| 4740 | case 203: |
| 4741 | case 206: |
| 4742 | case 300: |
| 4743 | case 301: |
| 4744 | case 410: |
| 4745 | /* RFC2616 @13.4: |
| 4746 | * "A response received with a status code of |
| 4747 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 4748 | * by a cache (...) unless a cache-control |
| 4749 | * directive prohibits caching." |
| 4750 | * |
| 4751 | * RFC2616 @9.5: POST method : |
| 4752 | * "Responses to this method are not cacheable, |
| 4753 | * unless the response includes appropriate |
| 4754 | * Cache-Control or Expires header fields." |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4755 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4756 | if (likely(txn->meth != HTTP_METH_POST) && |
| 4757 | (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC))) |
| 4758 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 4759 | break; |
| 4760 | default: |
| 4761 | break; |
| 4762 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4763 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4764 | /* |
| 4765 | * 3: we may need to capture headers |
| 4766 | */ |
| 4767 | s->logs.logwait &= ~LW_RESP; |
Willy Tarreau | 42f7d89 | 2012-03-24 08:28:09 +0100 | [diff] [blame] | 4768 | if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap)) |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4769 | capture_headers(msg->sol, &txn->hdr_idx, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4770 | txn->rsp.cap, s->fe->rsp_cap); |
| 4771 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4772 | /* 4: determine the transfer-length. |
| 4773 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 4774 | * the presence of a message-body in a RESPONSE and its transfer length |
| 4775 | * must be determined that way : |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4776 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4777 | * All responses to the HEAD request method MUST NOT include a |
| 4778 | * message-body, even though the presence of entity-header fields |
| 4779 | * might lead one to believe they do. All 1xx (informational), 204 |
| 4780 | * (No Content), and 304 (Not Modified) responses MUST NOT include a |
| 4781 | * message-body. All other responses do include a message-body, |
| 4782 | * although it MAY be of zero length. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4783 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4784 | * 1. Any response which "MUST NOT" include a message-body (such as the |
| 4785 | * 1xx, 204 and 304 responses and any response to a HEAD request) is |
| 4786 | * always terminated by the first empty line after the header fields, |
| 4787 | * regardless of the entity-header fields present in the message. |
| 4788 | * |
| 4789 | * 2. If a Transfer-Encoding header field (Section 9.7) is present and |
| 4790 | * the "chunked" transfer-coding (Section 6.2) is used, the |
| 4791 | * transfer-length is defined by the use of this transfer-coding. |
| 4792 | * If a Transfer-Encoding header field is present and the "chunked" |
| 4793 | * transfer-coding is not present, the transfer-length is defined by |
| 4794 | * the sender closing the connection. |
| 4795 | * |
| 4796 | * 3. If a Content-Length header field is present, its decimal value in |
| 4797 | * OCTETs represents both the entity-length and the transfer-length. |
| 4798 | * If a message is received with both a Transfer-Encoding header |
| 4799 | * field and a Content-Length header field, the latter MUST be ignored. |
| 4800 | * |
| 4801 | * 4. If the message uses the media type "multipart/byteranges", and |
| 4802 | * the transfer-length is not otherwise specified, then this self- |
| 4803 | * delimiting media type defines the transfer-length. This media |
| 4804 | * type MUST NOT be used unless the sender knows that the recipient |
| 4805 | * can parse it; the presence in a request of a Range header with |
| 4806 | * multiple byte-range specifiers from a 1.1 client implies that the |
| 4807 | * client can parse multipart/byteranges responses. |
| 4808 | * |
| 4809 | * 5. By the server closing the connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4810 | */ |
| 4811 | |
| 4812 | /* Skip parsing if no content length is possible. The response flags |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4813 | * 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] | 4814 | * 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] | 4815 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 4816 | */ |
| 4817 | if (txn->meth == HTTP_METH_HEAD || |
| 4818 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4819 | txn->status == 204 || txn->status == 304) { |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4820 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4821 | goto skip_content_length; |
| 4822 | } |
| 4823 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4824 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4825 | ctx.idx = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4826 | while ((msg->flags & HTTP_MSGF_VER_11) && |
Willy Tarreau | 9e13c3c | 2009-12-22 09:59:58 +0100 | [diff] [blame] | 4827 | http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4828 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4829 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 4830 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4831 | /* bad transfer-encoding (chunked followed by something else) */ |
| 4832 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4833 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4834 | break; |
| 4835 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4836 | } |
| 4837 | |
| 4838 | /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */ |
| 4839 | ctx.idx = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4840 | while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only && |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4841 | http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) { |
| 4842 | signed long long cl; |
| 4843 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4844 | if (!ctx.vlen) { |
| 4845 | msg->err_pos = ctx.line + ctx.val - rep->data; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4846 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4847 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4848 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4849 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
| 4850 | msg->err_pos = ctx.line + ctx.val - rep->data; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4851 | goto hdr_response_bad; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4852 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4853 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4854 | if (cl < 0) { |
| 4855 | msg->err_pos = ctx.line + ctx.val - rep->data; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4856 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4857 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4858 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4859 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4860 | msg->err_pos = ctx.line + ctx.val - rep->data; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4861 | goto hdr_response_bad; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4862 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4863 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4864 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4865 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4866 | } |
| 4867 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4868 | /* FIXME: we should also implement the multipart/byterange method. |
| 4869 | * For now on, we resort to close mode in this case (unknown length). |
| 4870 | */ |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4871 | skip_content_length: |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4872 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4873 | /* end of job, return OK */ |
| 4874 | rep->analysers &= ~an_bit; |
| 4875 | rep->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4876 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4877 | return 1; |
| 4878 | } |
| 4879 | |
| 4880 | /* This function performs all the processing enabled for the current response. |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 4881 | * It normally returns 1 unless it wants to break. It relies on buffers flags, |
| 4882 | * and updates t->rep->analysers. It might make sense to explode it into several |
| 4883 | * other functions. It works like process_request (see indications above). |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4884 | */ |
| 4885 | int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px) |
| 4886 | { |
| 4887 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4888 | struct http_msg *msg = &txn->rsp; |
| 4889 | struct proxy *cur_proxy; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 4890 | struct cond_wordlist *wl; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4891 | |
| 4892 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 4893 | now_ms, __FUNCTION__, |
| 4894 | t, |
| 4895 | rep, |
| 4896 | rep->rex, rep->wex, |
| 4897 | rep->flags, |
| 4898 | rep->l, |
| 4899 | rep->analysers); |
| 4900 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4901 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4902 | return 0; |
| 4903 | |
| 4904 | rep->analysers &= ~an_bit; |
| 4905 | rep->analyse_exp = TICK_ETERNITY; |
| 4906 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4907 | /* Now we have to check if we need to modify the Connection header. |
| 4908 | * This is more difficult on the response than it is on the request, |
| 4909 | * because we can have two different HTTP versions and we don't know |
| 4910 | * how the client will interprete a response. For instance, let's say |
| 4911 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 4912 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 4913 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 4914 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 4915 | * the lack of header as a keep-alive. Thus we will use two flags |
| 4916 | * indicating how a request MAY be understood by the client. In case |
| 4917 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 4918 | * ambiguous cases such as both close and keepalive are seen, then we |
| 4919 | * will fall back to explicit close. Note that we won't take risks with |
| 4920 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4921 | * See doc/internals/connection-header.txt for the complete matrix. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4922 | */ |
| 4923 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 4924 | if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || |
| 4925 | txn->status == 101)) { |
| 4926 | /* Either we've established an explicit tunnel, or we're |
| 4927 | * switching the protocol. In both cases, we're very unlikely |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 4928 | * to understand the next protocols. We have to switch to tunnel |
| 4929 | * mode, so that we transfer the request and responses then let |
| 4930 | * this protocol pass unmodified. When we later implement specific |
| 4931 | * parsers for such protocols, we'll want to check the Upgrade |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 4932 | * header which contains information about that protocol for |
| 4933 | * responses with status 101 (eg: see RFC2817 about TLS). |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 4934 | */ |
| 4935 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN; |
| 4936 | } |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 4937 | else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) && |
| 4938 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN || |
| 4939 | ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4940 | int to_del = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4941 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4942 | /* on unknown transfer length, we must close */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4943 | if (!(msg->flags & HTTP_MSGF_XFER_LEN) && |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4944 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 4945 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4946 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4947 | /* now adjust header transformations depending on current state */ |
| 4948 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN || |
| 4949 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4950 | to_del |= 2; /* remove "keep-alive" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4951 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4952 | to_del |= 1; /* remove "close" for HTTP/1.0 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4953 | } |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4954 | else { /* SCL / KAL */ |
| 4955 | to_del |= 1; /* remove "close" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4956 | if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4957 | to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4958 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4959 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4960 | /* Parse and remove some headers from the connection header */ |
| 4961 | http_parse_connection_header(txn, msg, rep, to_del); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4962 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4963 | /* Some keep-alive responses are converted to Server-close if |
| 4964 | * the server wants to close. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4965 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4966 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) { |
| 4967 | if ((txn->flags & TX_HDR_CONN_CLO) || |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4968 | (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11))) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4969 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4970 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4971 | } |
| 4972 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4973 | if (1) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4974 | /* |
| 4975 | * 3: we will have to evaluate the filters. |
| 4976 | * As opposed to version 1.2, now they will be evaluated in the |
| 4977 | * filters order and not in the header order. This means that |
| 4978 | * each filter has to be validated among all headers. |
| 4979 | * |
| 4980 | * Filters are tried with ->be first, then with ->fe if it is |
| 4981 | * different from ->be. |
| 4982 | */ |
| 4983 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4984 | cur_proxy = t->be; |
| 4985 | while (1) { |
| 4986 | struct proxy *rule_set = cur_proxy; |
| 4987 | |
| 4988 | /* try headers filters */ |
| 4989 | if (rule_set->rsp_exp != NULL) { |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 4990 | if (apply_filters_to_response(t, rep, rule_set) < 0) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4991 | return_bad_resp: |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4992 | if (target_srv(&t->target)) { |
| 4993 | target_srv(&t->target)->counters.failed_resp++; |
| 4994 | health_adjust(target_srv(&t->target), HANA_STATUS_HTTP_RSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4995 | } |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4996 | t->be->be_counters.failed_resp++; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4997 | return_srv_prx_502: |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 4998 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4999 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5000 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 0b89fbb | 2010-02-02 09:57:24 +0100 | [diff] [blame] | 5001 | buffer_ignore(rep, rep->l - rep->send_max); |
Willy Tarreau | 8e89b84 | 2009-10-18 23:56:35 +0200 | [diff] [blame] | 5002 | stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502)); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5003 | if (!(t->flags & SN_ERR_MASK)) |
| 5004 | t->flags |= SN_ERR_PRXCOND; |
| 5005 | if (!(t->flags & SN_FINST_MASK)) |
| 5006 | t->flags |= SN_FINST_H; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 5007 | return 0; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5008 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5009 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5010 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5011 | /* has the response been denied ? */ |
| 5012 | if (txn->flags & TX_SVDENY) { |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5013 | if (target_srv(&t->target)) |
| 5014 | target_srv(&t->target)->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5015 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5016 | t->be->be_counters.denied_resp++; |
| 5017 | t->fe->fe_counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5018 | if (t->listener->counters) |
| 5019 | t->listener->counters->denied_resp++; |
| 5020 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5021 | goto return_srv_prx_502; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [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 | /* add response headers from the rule sets in the same order */ |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 5025 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 5026 | if (txn->status < 200) |
| 5027 | break; |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 5028 | if (wl->cond) { |
| 5029 | int ret = acl_exec_cond(wl->cond, px, t, txn, ACL_DIR_RTR); |
| 5030 | ret = acl_pass(ret); |
| 5031 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 5032 | ret = !ret; |
| 5033 | if (!ret) |
| 5034 | continue; |
| 5035 | } |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 5036 | if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, wl->s) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5037 | goto return_bad_resp; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 5038 | } |
| 5039 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5040 | /* check whether we're already working on the frontend */ |
| 5041 | if (cur_proxy == t->fe) |
| 5042 | break; |
| 5043 | cur_proxy = t->fe; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5044 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5045 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5046 | /* |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5047 | * We may be facing a 100-continue response, in which case this |
| 5048 | * is not the right response, and we're waiting for the next one. |
| 5049 | * 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] | 5050 | * next one. |
| 5051 | */ |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5052 | if (unlikely(txn->status == 100)) { |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5053 | hdr_idx_init(&txn->hdr_idx); |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5054 | buffer_forward(rep, rep->lr - msg->sol); |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5055 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 5056 | txn->status = 0; |
| 5057 | rep->analysers |= AN_RES_WAIT_HTTP | an_bit; |
| 5058 | return 1; |
| 5059 | } |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5060 | else if (unlikely(txn->status < 200)) |
| 5061 | goto skip_header_mangling; |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5062 | |
| 5063 | /* we don't have any 1xx status code now */ |
| 5064 | |
| 5065 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5066 | * 4: check for server cookie. |
| 5067 | */ |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5068 | if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name || |
| 5069 | (t->be->options & PR_O_CHK_CACHE)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5070 | manage_server_side_cookies(t, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5071 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5072 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5073 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5074 | * 5: check for cache-control or pragma headers if required. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5075 | */ |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5076 | 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] | 5077 | check_response_for_cacheability(t, rep); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5078 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5079 | /* |
| 5080 | * 6: add server cookie in the response if needed |
| 5081 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5082 | if (target_srv(&t->target) && (t->be->options & PR_O_COOK_INS) && |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 5083 | !((txn->flags & TX_SCK_FOUND) && (t->be->options2 & PR_O2_COOK_PSV)) && |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5084 | (!(t->flags & SN_DIRECT) || |
| 5085 | ((t->be->cookie_maxidle || txn->cookie_last_date) && |
| 5086 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 5087 | (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 5088 | (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 5089 | (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST)) && |
| 5090 | !(t->flags & SN_IGNORE_PRST)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5091 | int len; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5092 | /* the server is known, it's not the one the client requested, or the |
| 5093 | * cookie's last seen date needs to be refreshed. We have to |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5094 | * insert a set-cookie here, except if we want to insert only on POST |
| 5095 | * requests and this one isn't. Note that servers which don't have cookies |
| 5096 | * (eg: some backup servers) will return a full cookie removal request. |
| 5097 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5098 | if (!target_srv(&t->target)->cookie) { |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5099 | len = sprintf(trash, |
| 5100 | "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
| 5101 | t->be->cookie_name); |
| 5102 | } |
| 5103 | else { |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5104 | 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] | 5105 | |
| 5106 | if (t->be->cookie_maxidle || t->be->cookie_maxlife) { |
| 5107 | /* emit last_date, which is mandatory */ |
| 5108 | trash[len++] = COOKIE_DELIM_DATE; |
| 5109 | s30tob64((date.tv_sec+3) >> 2, trash + len); len += 5; |
| 5110 | if (t->be->cookie_maxlife) { |
| 5111 | /* emit first_date, which is either the original one or |
| 5112 | * the current date. |
| 5113 | */ |
| 5114 | trash[len++] = COOKIE_DELIM_DATE; |
| 5115 | s30tob64(txn->cookie_first_date ? |
| 5116 | txn->cookie_first_date >> 2 : |
| 5117 | (date.tv_sec+3) >> 2, trash + len); |
| 5118 | len += 5; |
| 5119 | } |
| 5120 | } |
| 5121 | len += sprintf(trash + len, "; path=/"); |
| 5122 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5123 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5124 | if (t->be->cookie_domain) |
| 5125 | len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5126 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5127 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 5128 | trash, len) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5129 | goto return_bad_resp; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5130 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 5131 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5132 | if (target_srv(&t->target)->cookie && (t->flags & SN_DIRECT)) |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5133 | /* the server did not change, only the date was updated */ |
| 5134 | txn->flags |= TX_SCK_UPDATED; |
| 5135 | else |
| 5136 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5137 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5138 | /* Here, we will tell an eventual cache on the client side that we don't |
| 5139 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 5140 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 5141 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 5142 | */ |
| 5143 | if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5144 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5145 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 5146 | |
| 5147 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 5148 | "Cache-control: private", 22) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5149 | goto return_bad_resp; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5150 | } |
| 5151 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5152 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5153 | /* |
| 5154 | * 7: check if result will be cacheable with a cookie. |
| 5155 | * We'll block the response if security checks have caught |
| 5156 | * nasty things such as a cacheable cookie. |
| 5157 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 5158 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 5159 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5160 | (t->be->options & PR_O_CHK_CACHE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5161 | |
| 5162 | /* we're in presence of a cacheable response containing |
| 5163 | * a set-cookie header. We'll block it as requested by |
| 5164 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5165 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5166 | if (target_srv(&t->target)) |
| 5167 | target_srv(&t->target)->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5168 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5169 | t->be->be_counters.denied_resp++; |
| 5170 | t->fe->fe_counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5171 | if (t->listener->counters) |
| 5172 | t->listener->counters->denied_resp++; |
| 5173 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5174 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5175 | 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] | 5176 | send_log(t->be, LOG_ALERT, |
| 5177 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5178 | 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] | 5179 | goto return_srv_prx_502; |
| 5180 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5181 | |
| 5182 | /* |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5183 | * 8: adjust "Connection: close" or "Connection: keep-alive" if needed. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5184 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5185 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
| 5186 | ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) { |
| 5187 | unsigned int want_flags = 0; |
| 5188 | |
| 5189 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5190 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5191 | /* we want a keep-alive response here. Keep-alive header |
| 5192 | * required if either side is not 1.1. |
| 5193 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5194 | if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5195 | want_flags |= TX_CON_KAL_SET; |
| 5196 | } |
| 5197 | else { |
| 5198 | /* we want a close response here. Close header required if |
| 5199 | * the server is 1.1, regardless of the client. |
| 5200 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5201 | if (msg->flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5202 | want_flags |= TX_CON_CLO_SET; |
| 5203 | } |
| 5204 | |
| 5205 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 5206 | http_change_connection_header(txn, msg, rep, want_flags); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5207 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5208 | |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5209 | skip_header_mangling: |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5210 | if ((msg->flags & HTTP_MSGF_XFER_LEN) || |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5211 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5212 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 5213 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5214 | /************************************************************* |
| 5215 | * OK, that's finished for the headers. We have done what we * |
| 5216 | * could. Let's switch to the DATA state. * |
| 5217 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5218 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5219 | t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5220 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5221 | /* if the user wants to log as soon as possible, without counting |
| 5222 | * bytes from the server, then this is the right moment. We have |
| 5223 | * to temporarily assign bytes_out to log what we currently have. |
| 5224 | */ |
| 5225 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 5226 | t->logs.t_close = t->logs.t_data; /* to get a valid end date */ |
| 5227 | t->logs.bytes_out = txn->rsp.eoh; |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 5228 | t->do_log(t); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5229 | t->logs.bytes_out = 0; |
| 5230 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5231 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5232 | /* Note: we must not try to cheat by jumping directly to DATA, |
| 5233 | * otherwise we would not let the client side wake up. |
| 5234 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5235 | |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 5236 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5237 | } |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 5238 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5239 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5240 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5241 | /* This function is an analyser which forwards response body (including chunk |
| 5242 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 5243 | * zero byte. The only situation where it must not be called is when we're in |
| 5244 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 5245 | * remaining data and to resync after end of body. It expects the msg_state to |
| 5246 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 5247 | * 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] | 5248 | * 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] | 5249 | * bytes of pending data + the headers if not already done (between som and sov). |
| 5250 | * It eventually adjusts som to match sov after the data in between have been sent. |
| 5251 | */ |
| 5252 | int http_response_forward_body(struct session *s, struct buffer *res, int an_bit) |
| 5253 | { |
| 5254 | struct http_txn *txn = &s->txn; |
| 5255 | struct http_msg *msg = &s->txn.rsp; |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5256 | int bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5257 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5258 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 5259 | return 0; |
| 5260 | |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 5261 | if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) || |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 5262 | ((res->flags & BF_SHUTW) && (res->to_forward || res->send_max)) || |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 5263 | !s->req->analysers) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5264 | /* Output closed while we were sending data. We must abort and |
| 5265 | * wake the other side up. |
| 5266 | */ |
| 5267 | msg->msg_state = HTTP_MSG_ERROR; |
| 5268 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 5269 | return 1; |
| 5270 | } |
| 5271 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5272 | /* in most states, we should abort in case of early close */ |
| 5273 | buffer_auto_close(res); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5274 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5275 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 5276 | /* we have msg->col and msg->sov which both point to the first |
| 5277 | * byte of message body. msg->som still points to the beginning |
| 5278 | * of the message. We must save the body in req->lr because it |
| 5279 | * survives buffer re-alignments. |
| 5280 | */ |
| 5281 | res->lr = res->data + msg->sov; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5282 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5283 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 5284 | else { |
| 5285 | msg->msg_state = HTTP_MSG_DATA; |
| 5286 | } |
| 5287 | } |
| 5288 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5289 | while (1) { |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5290 | int bytes; |
| 5291 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5292 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 5293 | /* we may have some data pending */ |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5294 | bytes = msg->sov - msg->som; |
| 5295 | if (msg->chunk_len || bytes) { |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 5296 | msg->som = msg->sov; |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5297 | if (likely(bytes < 0)) /* sov may have wrapped at the end */ |
| 5298 | bytes += res->size; |
| 5299 | msg->chunk_len += (unsigned int)bytes; |
| 5300 | msg->chunk_len -= buffer_forward(res, msg->chunk_len); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 5301 | } |
| 5302 | |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5303 | |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5304 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 5305 | /* must still forward */ |
| 5306 | if (res->to_forward) |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5307 | goto missing_data; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5308 | |
| 5309 | /* nothing left to forward */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5310 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5311 | msg->msg_state = HTTP_MSG_DATA_CRLF; |
| 5312 | else |
| 5313 | msg->msg_state = HTTP_MSG_DONE; |
| 5314 | } |
| 5315 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5316 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5317 | * set ->sov to point to the body and switch to DATA or TRAILERS state. |
| 5318 | */ |
| 5319 | int ret = http_parse_chunk_size(res, msg); |
| 5320 | |
| 5321 | if (!ret) |
| 5322 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5323 | else if (ret < 0) { |
| 5324 | if (msg->err_pos >= 0) |
| 5325 | http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_CHUNK_SIZE, s->fe); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5326 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5327 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5328 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5329 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5330 | else if (msg->msg_state == HTTP_MSG_DATA_CRLF) { |
| 5331 | /* we want the CRLF after the data */ |
| 5332 | int ret; |
| 5333 | |
Willy Tarreau | d3347ee | 2010-01-04 02:02:25 +0100 | [diff] [blame] | 5334 | res->lr = res->w + res->send_max; |
| 5335 | if (res->lr >= res->data + res->size) |
| 5336 | res->lr -= res->size; |
| 5337 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5338 | ret = http_skip_chunk_crlf(res, msg); |
| 5339 | |
| 5340 | if (!ret) |
| 5341 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5342 | else if (ret < 0) { |
| 5343 | if (msg->err_pos >= 0) |
| 5344 | http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_DATA_CRLF, s->fe); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5345 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5346 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5347 | /* we're in MSG_CHUNK_SIZE now */ |
| 5348 | } |
| 5349 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
| 5350 | int ret = http_forward_trailers(res, msg); |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5351 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5352 | if (ret == 0) |
| 5353 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5354 | else if (ret < 0) { |
| 5355 | if (msg->err_pos >= 0) |
| 5356 | http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_TRAILERS, s->fe); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5357 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5358 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5359 | /* we're in HTTP_MSG_DONE now */ |
| 5360 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5361 | else { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5362 | int old_state = msg->msg_state; |
| 5363 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5364 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5365 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5366 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5367 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
| 5368 | buffer_dont_close(res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5369 | if (http_resync_states(s)) { |
| 5370 | http_silent_debug(__LINE__, s); |
| 5371 | /* some state changes occurred, maybe the analyser |
| 5372 | * was disabled too. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5373 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5374 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 5375 | if (res->flags & BF_SHUTW) { |
| 5376 | /* response errors are most likely due to |
| 5377 | * the client aborting the transfer. |
| 5378 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5379 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5380 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5381 | if (msg->err_pos >= 0) |
| 5382 | http_capture_bad_message(&s->be->invalid_rep, s, res, msg, old_state, s->fe); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5383 | goto return_bad_res; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5384 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5385 | return 1; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5386 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5387 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5388 | } |
| 5389 | } |
| 5390 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5391 | missing_data: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5392 | /* stop waiting for data if the input is closed before the end */ |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5393 | if (res->flags & BF_SHUTR) { |
| 5394 | if (!(s->flags & SN_ERR_MASK)) |
| 5395 | s->flags |= SN_ERR_SRVCL; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5396 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5397 | if (target_srv(&s->target)) |
| 5398 | target_srv(&s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5399 | goto return_bad_res_stats_ok; |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5400 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5401 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5402 | if (res->flags & BF_SHUTW) |
| 5403 | goto aborted_xfer; |
| 5404 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5405 | /* 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] | 5406 | if (!s->req->analysers) |
| 5407 | goto return_bad_res; |
| 5408 | |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5409 | /* forward any pending data */ |
| 5410 | bytes = msg->sov - msg->som; |
| 5411 | if (msg->chunk_len || bytes) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5412 | msg->som = msg->sov; |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5413 | if (likely(bytes < 0)) /* sov may have wrapped at the end */ |
| 5414 | bytes += res->size; |
| 5415 | msg->chunk_len += (unsigned int)bytes; |
| 5416 | msg->chunk_len -= buffer_forward(res, msg->chunk_len); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5417 | } |
| 5418 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5419 | /* When TE: chunked is used, we need to get there again to parse remaining |
| 5420 | * chunks even if the server has closed, so we don't want to set BF_DONTCLOSE. |
| 5421 | * Similarly, with keep-alive on the client side, we don't want to forward a |
| 5422 | * close. |
| 5423 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5424 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5425 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5426 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
| 5427 | buffer_dont_close(res); |
| 5428 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5429 | /* 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] | 5430 | * what we did. So we always set the BF_EXPECT_MORE flag so that the |
| 5431 | * 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] | 5432 | * modes are already handled by the stream sock layer. We must not do |
| 5433 | * this in content-length mode because it could present the MSG_MORE |
| 5434 | * flag with the last block of forwarded data, which would cause an |
| 5435 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5436 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5437 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 869fc1e | 2012-03-05 08:29:20 +0100 | [diff] [blame] | 5438 | res->flags |= BF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5439 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5440 | /* the session handler will take care of timeouts and errors */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5441 | http_silent_debug(__LINE__, s); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5442 | return 0; |
| 5443 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5444 | return_bad_res: /* let's centralize all bad responses */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5445 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5446 | if (target_srv(&s->target)) |
| 5447 | target_srv(&s->target)->counters.failed_resp++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5448 | |
| 5449 | return_bad_res_stats_ok: |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5450 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 5451 | /* don't send any error message as we're in the body */ |
| 5452 | stream_int_retnclose(res->cons, NULL); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5453 | res->analysers = 0; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5454 | 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] | 5455 | if (target_srv(&s->target)) |
| 5456 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5457 | |
| 5458 | if (!(s->flags & SN_ERR_MASK)) |
| 5459 | s->flags |= SN_ERR_PRXCOND; |
| 5460 | if (!(s->flags & SN_FINST_MASK)) |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5461 | s->flags |= SN_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5462 | return 0; |
| 5463 | |
| 5464 | aborted_xfer: |
| 5465 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 5466 | /* don't send any error message as we're in the body */ |
| 5467 | stream_int_retnclose(res->cons, NULL); |
| 5468 | res->analysers = 0; |
| 5469 | s->req->analysers = 0; /* we're in data phase, we want to abort both directions */ |
| 5470 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5471 | s->fe->fe_counters.cli_aborts++; |
| 5472 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5473 | if (target_srv(&s->target)) |
| 5474 | target_srv(&s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5475 | |
| 5476 | if (!(s->flags & SN_ERR_MASK)) |
| 5477 | s->flags |= SN_ERR_CLICL; |
| 5478 | if (!(s->flags & SN_FINST_MASK)) |
| 5479 | s->flags |= SN_FINST_D; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5480 | return 0; |
| 5481 | } |
| 5482 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5483 | /* Iterate the same filter through all request headers. |
| 5484 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5485 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 5486 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5487 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5488 | 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] | 5489 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5490 | char term; |
| 5491 | char *cur_ptr, *cur_end, *cur_next; |
| 5492 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5493 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5494 | struct hdr_idx_elem *cur_hdr; |
| 5495 | int len, delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 5496 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5497 | last_hdr = 0; |
| 5498 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5499 | cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5500 | old_idx = 0; |
| 5501 | |
| 5502 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5503 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5504 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5505 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5506 | (exp->action == ACT_ALLOW || |
| 5507 | exp->action == ACT_DENY || |
| 5508 | exp->action == ACT_TARPIT)) |
| 5509 | return 0; |
| 5510 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5511 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5512 | if (!cur_idx) |
| 5513 | break; |
| 5514 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5515 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5516 | cur_ptr = cur_next; |
| 5517 | cur_end = cur_ptr + cur_hdr->len; |
| 5518 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5519 | |
| 5520 | /* Now we have one header between cur_ptr and cur_end, |
| 5521 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5522 | */ |
| 5523 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5524 | /* The annoying part is that pattern matching needs |
| 5525 | * that we modify the contents to null-terminate all |
| 5526 | * strings before testing them. |
| 5527 | */ |
| 5528 | |
| 5529 | term = *cur_end; |
| 5530 | *cur_end = '\0'; |
| 5531 | |
| 5532 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5533 | switch (exp->action) { |
| 5534 | case ACT_SETBE: |
| 5535 | /* It is not possible to jump a second time. |
| 5536 | * FIXME: should we return an HTTP/500 here so that |
| 5537 | * the admin knows there's a problem ? |
| 5538 | */ |
| 5539 | if (t->be != t->fe) |
| 5540 | break; |
| 5541 | |
| 5542 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 5543 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5544 | last_hdr = 1; |
| 5545 | break; |
| 5546 | |
| 5547 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5548 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5549 | last_hdr = 1; |
| 5550 | break; |
| 5551 | |
| 5552 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5553 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5554 | last_hdr = 1; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5555 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5556 | t->fe->fe_counters.denied_req++; |
| 5557 | if (t->fe != t->be) |
| 5558 | t->be->be_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5559 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 5560 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5561 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5562 | break; |
| 5563 | |
| 5564 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5565 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5566 | last_hdr = 1; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5567 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5568 | t->fe->fe_counters.denied_req++; |
| 5569 | if (t->fe != t->be) |
| 5570 | t->be->be_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5571 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 5572 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5573 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5574 | break; |
| 5575 | |
| 5576 | case ACT_REPLACE: |
| 5577 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 5578 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 5579 | /* FIXME: if the user adds a newline in the replacement, the |
| 5580 | * index will not be recalculated for now, and the new line |
| 5581 | * will not be counted as a new header. |
| 5582 | */ |
| 5583 | |
| 5584 | cur_end += delta; |
| 5585 | cur_next += delta; |
| 5586 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5587 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5588 | break; |
| 5589 | |
| 5590 | case ACT_REMOVE: |
| 5591 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
| 5592 | cur_next += delta; |
| 5593 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5594 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5595 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 5596 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5597 | cur_hdr->len = 0; |
| 5598 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 5599 | cur_idx = old_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5600 | break; |
| 5601 | |
| 5602 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5603 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5604 | if (cur_end) |
| 5605 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5606 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5607 | /* keep the link from this header to next one in case of later |
| 5608 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5609 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5610 | old_idx = cur_idx; |
| 5611 | } |
| 5612 | return 0; |
| 5613 | } |
| 5614 | |
| 5615 | |
| 5616 | /* Apply the filter to the request line. |
| 5617 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 5618 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5619 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 5620 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5621 | */ |
| 5622 | int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 5623 | { |
| 5624 | char term; |
| 5625 | char *cur_ptr, *cur_end; |
| 5626 | int done; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5627 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5628 | int len, delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5629 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5630 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5631 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5632 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5633 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5634 | (exp->action == ACT_ALLOW || |
| 5635 | exp->action == ACT_DENY || |
| 5636 | exp->action == ACT_TARPIT)) |
| 5637 | return 0; |
| 5638 | else if (exp->action == ACT_REMOVE) |
| 5639 | return 0; |
| 5640 | |
| 5641 | done = 0; |
| 5642 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5643 | cur_ptr = txn->req.sol; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5644 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5645 | |
| 5646 | /* Now we have the request line between cur_ptr and cur_end */ |
| 5647 | |
| 5648 | /* The annoying part is that pattern matching needs |
| 5649 | * that we modify the contents to null-terminate all |
| 5650 | * strings before testing them. |
| 5651 | */ |
| 5652 | |
| 5653 | term = *cur_end; |
| 5654 | *cur_end = '\0'; |
| 5655 | |
| 5656 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5657 | switch (exp->action) { |
| 5658 | case ACT_SETBE: |
| 5659 | /* It is not possible to jump a second time. |
| 5660 | * FIXME: should we return an HTTP/500 here so that |
| 5661 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5662 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5663 | if (t->be != t->fe) |
| 5664 | break; |
| 5665 | |
| 5666 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 5667 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5668 | done = 1; |
| 5669 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5670 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5671 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5672 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5673 | done = 1; |
| 5674 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5675 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5676 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5677 | txn->flags |= TX_CLDENY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5678 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5679 | t->fe->fe_counters.denied_req++; |
| 5680 | if (t->fe != t->be) |
| 5681 | t->be->be_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5682 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 5683 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5684 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5685 | done = 1; |
| 5686 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5687 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5688 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5689 | txn->flags |= TX_CLTARPIT; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5690 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5691 | t->fe->fe_counters.denied_req++; |
| 5692 | if (t->fe != t->be) |
| 5693 | t->be->be_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5694 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 5695 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5696 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5697 | done = 1; |
| 5698 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5699 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5700 | case ACT_REPLACE: |
| 5701 | *cur_end = term; /* restore the string terminator */ |
| 5702 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 5703 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 5704 | /* FIXME: if the user adds a newline in the replacement, the |
| 5705 | * index will not be recalculated for now, and the new line |
| 5706 | * will not be counted as a new header. |
| 5707 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5708 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5709 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5710 | cur_end += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5711 | cur_end = (char *)http_parse_reqline(&txn->req, req->data, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5712 | HTTP_MSG_RQMETH, |
| 5713 | cur_ptr, cur_end + 1, |
| 5714 | NULL, NULL); |
| 5715 | if (unlikely(!cur_end)) |
| 5716 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5717 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5718 | /* we have a full request and we know that we have either a CR |
| 5719 | * or an LF at <ptr>. |
| 5720 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5721 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 5722 | 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] | 5723 | /* there is no point trying this regex on headers */ |
| 5724 | return 1; |
| 5725 | } |
| 5726 | } |
| 5727 | *cur_end = term; /* restore the string terminator */ |
| 5728 | return done; |
| 5729 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 5730 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5731 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5732 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5733 | /* |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5734 | * 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] | 5735 | * 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] | 5736 | * unparsable request. Since it can manage the switch to another backend, it |
| 5737 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5738 | */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5739 | 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] | 5740 | { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5741 | struct http_txn *txn = &s->txn; |
| 5742 | struct hdr_exp *exp; |
| 5743 | |
| 5744 | for (exp = px->req_exp; exp; exp = exp->next) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5745 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5746 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5747 | /* |
| 5748 | * The interleaving of transformations and verdicts |
| 5749 | * makes it difficult to decide to continue or stop |
| 5750 | * the evaluation. |
| 5751 | */ |
| 5752 | |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5753 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) |
| 5754 | break; |
| 5755 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5756 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5757 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5758 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5759 | continue; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5760 | |
| 5761 | /* if this filter had a condition, evaluate it now and skip to |
| 5762 | * next filter if the condition does not match. |
| 5763 | */ |
| 5764 | if (exp->cond) { |
| 5765 | ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_REQ); |
| 5766 | ret = acl_pass(ret); |
| 5767 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 5768 | ret = !ret; |
| 5769 | |
| 5770 | if (!ret) |
| 5771 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5772 | } |
| 5773 | |
| 5774 | /* Apply the filter to the request line. */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5775 | ret = apply_filter_to_req_line(s, req, exp); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5776 | if (unlikely(ret < 0)) |
| 5777 | return -1; |
| 5778 | |
| 5779 | if (likely(ret == 0)) { |
| 5780 | /* The filter did not match the request, it can be |
| 5781 | * iterated through all headers. |
| 5782 | */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5783 | apply_filter_to_req_headers(s, req, exp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5784 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5785 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5786 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5787 | } |
| 5788 | |
| 5789 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5790 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5791 | /* |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5792 | * Try to retrieve the server associated to the appsession. |
| 5793 | * If the server is found, it's assigned to the session. |
| 5794 | */ |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5795 | 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] | 5796 | struct http_txn *txn = &t->txn; |
| 5797 | appsess *asession = NULL; |
| 5798 | char *sessid_temp = NULL; |
| 5799 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5800 | if (len > t->be->appsession_len) { |
| 5801 | len = t->be->appsession_len; |
| 5802 | } |
| 5803 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5804 | if (t->be->options2 & PR_O2_AS_REQL) { |
| 5805 | /* 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] | 5806 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5807 | /* 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] | 5808 | pool_free2(apools.sessid, txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5809 | } |
| 5810 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 5811 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5812 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5813 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5814 | return; |
| 5815 | } |
| 5816 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 5817 | memcpy(txn->sessid, buf, len); |
| 5818 | txn->sessid[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5819 | } |
| 5820 | |
| 5821 | if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) { |
| 5822 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5823 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5824 | return; |
| 5825 | } |
| 5826 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5827 | memcpy(sessid_temp, buf, len); |
| 5828 | sessid_temp[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5829 | |
| 5830 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp); |
| 5831 | /* free previously allocated memory */ |
| 5832 | pool_free2(apools.sessid, sessid_temp); |
| 5833 | |
| 5834 | if (asession != NULL) { |
| 5835 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 5836 | if (!(t->be->options2 & PR_O2_AS_REQL)) |
| 5837 | asession->request_count++; |
| 5838 | |
| 5839 | if (asession->serverid != NULL) { |
| 5840 | struct server *srv = t->be->srv; |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 5841 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5842 | while (srv) { |
| 5843 | if (strcmp(srv->id, asession->serverid) == 0) { |
Willy Tarreau | 4de9149 | 2010-01-22 19:10:05 +0100 | [diff] [blame] | 5844 | if ((srv->state & SRV_RUNNING) || |
| 5845 | (t->be->options & PR_O_PERSIST) || |
| 5846 | (t->flags & SN_FORCE_PRST)) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5847 | /* we found the server and it's usable */ |
| 5848 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | 2a6d88d | 2010-01-24 13:10:43 +0100 | [diff] [blame] | 5849 | txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5850 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 5851 | set_target_server(&t->target, srv); |
Willy Tarreau | 664beb8 | 2011-03-10 11:38:29 +0100 | [diff] [blame] | 5852 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5853 | break; |
| 5854 | } else { |
| 5855 | txn->flags &= ~TX_CK_MASK; |
| 5856 | txn->flags |= TX_CK_DOWN; |
| 5857 | } |
| 5858 | } |
| 5859 | srv = srv->next; |
| 5860 | } |
| 5861 | } |
| 5862 | } |
| 5863 | } |
| 5864 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5865 | /* Find the end of a cookie value contained between <s> and <e>. It works the |
| 5866 | * same way as with headers above except that the semi-colon also ends a token. |
| 5867 | * See RFC2965 for more information. Note that it requires a valid header to |
| 5868 | * return a valid result. |
| 5869 | */ |
| 5870 | char *find_cookie_value_end(char *s, const char *e) |
| 5871 | { |
| 5872 | int quoted, qdpair; |
| 5873 | |
| 5874 | quoted = qdpair = 0; |
| 5875 | for (; s < e; s++) { |
| 5876 | if (qdpair) qdpair = 0; |
| 5877 | else if (quoted) { |
| 5878 | if (*s == '\\') qdpair = 1; |
| 5879 | else if (*s == '"') quoted = 0; |
| 5880 | } |
| 5881 | else if (*s == '"') quoted = 1; |
| 5882 | else if (*s == ',' || *s == ';') return s; |
| 5883 | } |
| 5884 | return s; |
| 5885 | } |
| 5886 | |
| 5887 | /* Delete a value in a header between delimiters <from> and <next> in buffer |
| 5888 | * <buf>. The number of characters displaced is returned, and the pointer to |
| 5889 | * the first delimiter is updated if required. The function tries as much as |
| 5890 | * possible to respect the following principles : |
| 5891 | * - replace <from> delimiter by the <next> one unless <from> points to a |
| 5892 | * colon, in which case <next> is simply removed |
| 5893 | * - set exactly one space character after the new first delimiter, unless |
| 5894 | * there are not enough characters in the block being moved to do so. |
| 5895 | * - remove unneeded spaces before the previous delimiter and after the new |
| 5896 | * one. |
| 5897 | * |
| 5898 | * It is the caller's responsibility to ensure that : |
| 5899 | * - <from> points to a valid delimiter or the colon ; |
| 5900 | * - <next> points to a valid delimiter or the final CR/LF ; |
| 5901 | * - there are non-space chars before <from> ; |
| 5902 | * - there is a CR/LF at or after <next>. |
| 5903 | */ |
| 5904 | int del_hdr_value(struct buffer *buf, char **from, char *next) |
| 5905 | { |
| 5906 | char *prev = *from; |
| 5907 | |
| 5908 | if (*prev == ':') { |
| 5909 | /* We're removing the first value, preserve the colon and add a |
| 5910 | * space if possible. |
| 5911 | */ |
| 5912 | if (!http_is_crlf[(unsigned char)*next]) |
| 5913 | next++; |
| 5914 | prev++; |
| 5915 | if (prev < next) |
| 5916 | *prev++ = ' '; |
| 5917 | |
| 5918 | while (http_is_spht[(unsigned char)*next]) |
| 5919 | next++; |
| 5920 | } else { |
| 5921 | /* Remove useless spaces before the old delimiter. */ |
| 5922 | while (http_is_spht[(unsigned char)*(prev-1)]) |
| 5923 | prev--; |
| 5924 | *from = prev; |
| 5925 | |
| 5926 | /* copy the delimiter and if possible a space if we're |
| 5927 | * not at the end of the line. |
| 5928 | */ |
| 5929 | if (!http_is_crlf[(unsigned char)*next]) { |
| 5930 | *prev++ = *next++; |
| 5931 | if (prev + 1 < next) |
| 5932 | *prev++ = ' '; |
| 5933 | while (http_is_spht[(unsigned char)*next]) |
| 5934 | next++; |
| 5935 | } |
| 5936 | } |
| 5937 | return buffer_replace2(buf, prev, next, NULL, 0); |
| 5938 | } |
| 5939 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5940 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 5941 | * 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] | 5942 | * desirable to call it only when needed. This code is quite complex because |
| 5943 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 5944 | * highly recommended not to touch this part without a good reason ! |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5945 | */ |
| 5946 | void manage_client_side_cookies(struct session *t, struct buffer *req) |
| 5947 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5948 | struct http_txn *txn = &t->txn; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5949 | int preserve_hdr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5950 | int cur_idx, old_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5951 | char *hdr_beg, *hdr_end, *hdr_next, *del_from; |
| 5952 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5953 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5954 | /* Iterate through the headers, we start with the start line. */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 5955 | old_idx = 0; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5956 | hdr_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5957 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5958 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5959 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5960 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5961 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5962 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5963 | hdr_beg = hdr_next; |
| 5964 | hdr_end = hdr_beg + cur_hdr->len; |
| 5965 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5966 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5967 | /* We have one full header between hdr_beg and hdr_end, and the |
| 5968 | * next header starts at hdr_next. We're only interested in |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5969 | * "Cookie:" headers. |
| 5970 | */ |
| 5971 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5972 | val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5973 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5974 | old_idx = cur_idx; |
| 5975 | continue; |
| 5976 | } |
| 5977 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5978 | del_from = NULL; /* nothing to be deleted */ |
| 5979 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 5980 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5981 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 5982 | * attributes whose name begin with a '$', and associate them with |
| 5983 | * the right cookie, if we want to delete this cookie. |
| 5984 | * So there are 3 cases for each cookie read : |
| 5985 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 5986 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 5987 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 5988 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 5989 | * "special" cookie. |
| 5990 | * At the end of loop, if a "special" cookie remains, we may have to |
| 5991 | * remove it. If no application cookie persists in the header, we |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5992 | * *MUST* delete it. |
| 5993 | * |
| 5994 | * Note: RFC2965 is unclear about the processing of spaces around |
| 5995 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 5996 | * the RFC explicitly allows spaces before it, and not within the |
| 5997 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 5998 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 5999 | * after the equal sign too, resulting in some (rare) buggy |
| 6000 | * implementations trying to do that. So let's do what servers do. |
| 6001 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 6002 | * allowed quoted strings in values, with any possible character |
| 6003 | * after a backslash, including control chars and delimitors, which |
| 6004 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 6005 | * within values even without quotes. |
| 6006 | * |
| 6007 | * We have to keep multiple pointers in order to support cookie |
| 6008 | * removal at the beginning, middle or end of header without |
| 6009 | * corrupting the header. All of these headers are valid : |
| 6010 | * |
| 6011 | * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n |
| 6012 | * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n |
| 6013 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 6014 | * | | | | | | | | | |
| 6015 | * | | | | | | | | hdr_end <--+ |
| 6016 | * | | | | | | | +--> next |
| 6017 | * | | | | | | +----> val_end |
| 6018 | * | | | | | +-----------> val_beg |
| 6019 | * | | | | +--------------> equal |
| 6020 | * | | | +----------------> att_end |
| 6021 | * | | +---------------------> att_beg |
| 6022 | * | +--------------------------> prev |
| 6023 | * +--------------------------------> hdr_beg |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6024 | */ |
| 6025 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6026 | for (prev = hdr_beg + 6; prev < hdr_end; prev = next) { |
| 6027 | /* Iterate through all cookies on this line */ |
| 6028 | |
| 6029 | /* find att_beg */ |
| 6030 | att_beg = prev + 1; |
| 6031 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 6032 | att_beg++; |
| 6033 | |
| 6034 | /* find att_end : this is the first character after the last non |
| 6035 | * space before the equal. It may be equal to hdr_end. |
| 6036 | */ |
| 6037 | equal = att_end = att_beg; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6038 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6039 | while (equal < hdr_end) { |
| 6040 | if (*equal == '=' || *equal == ',' || *equal == ';') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6041 | break; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6042 | if (http_is_spht[(unsigned char)*equal++]) |
| 6043 | continue; |
| 6044 | att_end = equal; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6045 | } |
| 6046 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6047 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 6048 | * is between <att_beg> and <equal>, both may be identical. |
| 6049 | */ |
| 6050 | |
| 6051 | /* look for end of cookie if there is an equal sign */ |
| 6052 | if (equal < hdr_end && *equal == '=') { |
| 6053 | /* look for the beginning of the value */ |
| 6054 | val_beg = equal + 1; |
| 6055 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 6056 | val_beg++; |
| 6057 | |
| 6058 | /* find the end of the value, respecting quotes */ |
| 6059 | next = find_cookie_value_end(val_beg, hdr_end); |
| 6060 | |
| 6061 | /* make val_end point to the first white space or delimitor after the value */ |
| 6062 | val_end = next; |
| 6063 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 6064 | val_end--; |
| 6065 | } else { |
| 6066 | val_beg = val_end = next = equal; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6067 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6068 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6069 | /* We have nothing to do with attributes beginning with '$'. However, |
| 6070 | * they will automatically be removed if a header before them is removed, |
| 6071 | * since they're supposed to be linked together. |
| 6072 | */ |
| 6073 | if (*att_beg == '$') |
| 6074 | continue; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6075 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6076 | /* Ignore cookies with no equal sign */ |
| 6077 | if (equal == next) { |
| 6078 | /* This is not our cookie, so we must preserve it. But if we already |
| 6079 | * scheduled another cookie for removal, we cannot remove the |
| 6080 | * complete header, but we can remove the previous block itself. |
| 6081 | */ |
| 6082 | preserve_hdr = 1; |
| 6083 | if (del_from != NULL) { |
| 6084 | int delta = del_hdr_value(req, &del_from, prev); |
| 6085 | val_end += delta; |
| 6086 | next += delta; |
| 6087 | hdr_end += delta; |
| 6088 | hdr_next += delta; |
| 6089 | cur_hdr->len += delta; |
| 6090 | http_msg_move_end(&txn->req, delta); |
| 6091 | prev = del_from; |
| 6092 | del_from = NULL; |
| 6093 | } |
| 6094 | continue; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6095 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6096 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6097 | /* if there are spaces around the equal sign, we need to |
| 6098 | * strip them otherwise we'll get trouble for cookie captures, |
| 6099 | * or even for rewrites. Since this happens extremely rarely, |
| 6100 | * it does not hurt performance. |
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 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 6103 | int stripped_before = 0; |
| 6104 | int stripped_after = 0; |
| 6105 | |
| 6106 | if (att_end != equal) { |
| 6107 | stripped_before = buffer_replace2(req, att_end, equal, NULL, 0); |
| 6108 | equal += stripped_before; |
| 6109 | val_beg += stripped_before; |
| 6110 | } |
| 6111 | |
| 6112 | if (val_beg > equal + 1) { |
| 6113 | stripped_after = buffer_replace2(req, equal + 1, val_beg, NULL, 0); |
| 6114 | val_beg += stripped_after; |
| 6115 | stripped_before += stripped_after; |
| 6116 | } |
| 6117 | |
| 6118 | val_end += stripped_before; |
| 6119 | next += stripped_before; |
| 6120 | hdr_end += stripped_before; |
| 6121 | hdr_next += stripped_before; |
| 6122 | cur_hdr->len += stripped_before; |
| 6123 | http_msg_move_end(&txn->req, stripped_before); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6124 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6125 | /* now everything is as on the diagram above */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6126 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6127 | /* First, let's see if we want to capture this cookie. We check |
| 6128 | * that we don't already have a client side cookie, because we |
| 6129 | * can only capture one. Also as an optimisation, we ignore |
| 6130 | * cookies shorter than the declared name. |
| 6131 | */ |
| 6132 | if (t->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 6133 | (val_end - att_beg >= t->fe->capture_namelen) && |
| 6134 | memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
| 6135 | int log_len = val_end - att_beg; |
| 6136 | |
| 6137 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
| 6138 | Alert("HTTP logging : out of memory.\n"); |
| 6139 | } else { |
| 6140 | if (log_len > t->fe->capture_len) |
| 6141 | log_len = t->fe->capture_len; |
| 6142 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 6143 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6144 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6145 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6146 | |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6147 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 6148 | * following form : |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6149 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6150 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6151 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6152 | * For cookies in prefix mode, the form is : |
| 6153 | * |
| 6154 | * Cookie: NAME=SRV~VALUE |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6155 | */ |
| 6156 | if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 6157 | (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) { |
| 6158 | struct server *srv = t->be->srv; |
| 6159 | char *delim; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6160 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6161 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 6162 | * have the server ID between val_beg and delim, and the original cookie between |
| 6163 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 6164 | * |
| 6165 | * Cookie: NAME=SRV; # in all but prefix modes |
| 6166 | * Cookie: NAME=SRV~OPAQUE ; # in prefix mode |
| 6167 | * | || || | |+-> next |
| 6168 | * | || || | +--> val_end |
| 6169 | * | || || +---------> delim |
| 6170 | * | || |+------------> val_beg |
| 6171 | * | || +-------------> att_end = equal |
| 6172 | * | |+-----------------> att_beg |
| 6173 | * | +------------------> prev |
| 6174 | * +-------------------------> hdr_beg |
| 6175 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6176 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6177 | if (t->be->options & PR_O_COOK_PFX) { |
| 6178 | for (delim = val_beg; delim < val_end; delim++) |
| 6179 | if (*delim == COOKIE_DELIM) |
| 6180 | break; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6181 | } else { |
| 6182 | char *vbar1; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6183 | delim = val_end; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6184 | /* Now check if the cookie contains a date field, which would |
| 6185 | * appear after a vertical bar ('|') just after the server name |
| 6186 | * and before the delimiter. |
| 6187 | */ |
| 6188 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 6189 | if (vbar1) { |
| 6190 | /* OK, so left of the bar is the server's cookie and |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6191 | * right is the last seen date. It is a base64 encoded |
| 6192 | * 30-bit value representing the UNIX date since the |
| 6193 | * epoch in 4-second quantities. |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6194 | */ |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6195 | int val; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6196 | delim = vbar1++; |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6197 | if (val_end - vbar1 >= 5) { |
| 6198 | val = b64tos30(vbar1); |
| 6199 | if (val > 0) |
| 6200 | txn->cookie_last_date = val << 2; |
| 6201 | } |
| 6202 | /* look for a second vertical bar */ |
| 6203 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 6204 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 6205 | val = b64tos30(vbar1 + 1); |
| 6206 | if (val > 0) |
| 6207 | txn->cookie_first_date = val << 2; |
| 6208 | } |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6209 | } |
| 6210 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6211 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6212 | /* if the cookie has an expiration date and the proxy wants to check |
| 6213 | * it, then we do that now. We first check if the cookie is too old, |
| 6214 | * then only if it has expired. We detect strict overflow because the |
| 6215 | * time resolution here is not great (4 seconds). Cookies with dates |
| 6216 | * in the future are ignored if their offset is beyond one day. This |
| 6217 | * allows an admin to fix timezone issues without expiring everyone |
| 6218 | * and at the same time avoids keeping unwanted side effects for too |
| 6219 | * long. |
| 6220 | */ |
| 6221 | if (txn->cookie_first_date && t->be->cookie_maxlife && |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6222 | (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) || |
| 6223 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6224 | txn->flags &= ~TX_CK_MASK; |
| 6225 | txn->flags |= TX_CK_OLD; |
| 6226 | delim = val_beg; // let's pretend we have not found the cookie |
| 6227 | txn->cookie_first_date = 0; |
| 6228 | txn->cookie_last_date = 0; |
| 6229 | } |
| 6230 | else if (txn->cookie_last_date && t->be->cookie_maxidle && |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6231 | (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) || |
| 6232 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6233 | txn->flags &= ~TX_CK_MASK; |
| 6234 | txn->flags |= TX_CK_EXPIRED; |
| 6235 | delim = val_beg; // let's pretend we have not found the cookie |
| 6236 | txn->cookie_first_date = 0; |
| 6237 | txn->cookie_last_date = 0; |
| 6238 | } |
| 6239 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6240 | /* Here, we'll look for the first running server which supports the cookie. |
| 6241 | * This allows to share a same cookie between several servers, for example |
| 6242 | * to dedicate backup servers to specific servers only. |
| 6243 | * However, to prevent clients from sticking to cookie-less backup server |
| 6244 | * when they have incidentely learned an empty cookie, we simply ignore |
| 6245 | * empty cookies and mark them as invalid. |
| 6246 | * The same behaviour is applied when persistence must be ignored. |
| 6247 | */ |
Willy Tarreau | 4a5cade | 2012-04-05 21:09:48 +0200 | [diff] [blame] | 6248 | if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6249 | srv = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6250 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6251 | while (srv) { |
| 6252 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 6253 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
| 6254 | if ((srv->state & SRV_RUNNING) || |
| 6255 | (t->be->options & PR_O_PERSIST) || |
| 6256 | (t->flags & SN_FORCE_PRST)) { |
| 6257 | /* we found the server and we can use it */ |
| 6258 | txn->flags &= ~TX_CK_MASK; |
| 6259 | txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN; |
| 6260 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 6261 | set_target_server(&t->target, srv); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6262 | break; |
| 6263 | } else { |
| 6264 | /* we found a server, but it's down, |
| 6265 | * mark it as such and go on in case |
| 6266 | * another one is available. |
| 6267 | */ |
| 6268 | txn->flags &= ~TX_CK_MASK; |
| 6269 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6270 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6271 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6272 | srv = srv->next; |
| 6273 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6274 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6275 | 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] | 6276 | /* no server matched this cookie or we deliberately skipped it */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6277 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | c89ccb6 | 2012-04-05 21:18:22 +0200 | [diff] [blame] | 6278 | if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED))) |
| 6279 | txn->flags |= TX_CK_UNUSED; |
| 6280 | else |
| 6281 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6282 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6283 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6284 | /* depending on the cookie mode, we may have to either : |
| 6285 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 6286 | * the server never sees it ; |
| 6287 | * - remove the server id from the cookie value, and tag the cookie as an |
| 6288 | * application cookie so that it does not get accidentely removed later, |
| 6289 | * if we're in cookie prefix mode |
| 6290 | */ |
| 6291 | if ((t->be->options & PR_O_COOK_PFX) && (delim != val_end)) { |
| 6292 | int delta; /* negative */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6293 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6294 | delta = buffer_replace2(req, val_beg, delim + 1, NULL, 0); |
| 6295 | val_end += delta; |
| 6296 | next += delta; |
| 6297 | hdr_end += delta; |
| 6298 | hdr_next += delta; |
| 6299 | cur_hdr->len += delta; |
| 6300 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6301 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6302 | del_from = NULL; |
| 6303 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 6304 | } |
| 6305 | else if (del_from == NULL && |
| 6306 | (t->be->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) { |
| 6307 | del_from = prev; |
| 6308 | } |
| 6309 | } else { |
| 6310 | /* This is not our cookie, so we must preserve it. But if we already |
| 6311 | * scheduled another cookie for removal, we cannot remove the |
| 6312 | * complete header, but we can remove the previous block itself. |
| 6313 | */ |
| 6314 | preserve_hdr = 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6315 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6316 | if (del_from != NULL) { |
| 6317 | int delta = del_hdr_value(req, &del_from, prev); |
Willy Tarreau | b810554 | 2010-11-24 18:31:28 +0100 | [diff] [blame] | 6318 | if (att_beg >= del_from) |
| 6319 | att_beg += delta; |
| 6320 | if (att_end >= del_from) |
| 6321 | att_end += delta; |
| 6322 | val_beg += delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6323 | val_end += delta; |
| 6324 | next += delta; |
| 6325 | hdr_end += delta; |
| 6326 | hdr_next += delta; |
| 6327 | cur_hdr->len += delta; |
| 6328 | http_msg_move_end(&txn->req, delta); |
| 6329 | prev = del_from; |
| 6330 | del_from = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6331 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6332 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6333 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6334 | /* Look for the appsession cookie unless persistence must be ignored */ |
| 6335 | if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) { |
| 6336 | int cmp_len, value_len; |
| 6337 | char *value_begin; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 6338 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6339 | if (t->be->options2 & PR_O2_AS_PFX) { |
| 6340 | cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len); |
| 6341 | value_begin = att_beg + t->be->appsession_name_len; |
| 6342 | value_len = val_end - att_beg - t->be->appsession_name_len; |
| 6343 | } else { |
| 6344 | cmp_len = att_end - att_beg; |
| 6345 | value_begin = val_beg; |
| 6346 | value_len = val_end - val_beg; |
| 6347 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6348 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6349 | /* let's see if the cookie is our appcookie */ |
| 6350 | if (cmp_len == t->be->appsession_name_len && |
| 6351 | memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) { |
| 6352 | manage_client_side_appsession(t, value_begin, value_len); |
| 6353 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6354 | } |
| 6355 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6356 | /* continue with next cookie on this header line */ |
| 6357 | att_beg = next; |
| 6358 | } /* for each cookie */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6359 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6360 | /* There are no more cookies on this line. |
| 6361 | * We may still have one (or several) marked for deletion at the |
| 6362 | * end of the line. We must do this now in two ways : |
| 6363 | * - if some cookies must be preserved, we only delete from the |
| 6364 | * mark to the end of line ; |
| 6365 | * - if nothing needs to be preserved, simply delete the whole header |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6366 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6367 | if (del_from) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6368 | int delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6369 | if (preserve_hdr) { |
| 6370 | delta = del_hdr_value(req, &del_from, hdr_end); |
| 6371 | hdr_end = del_from; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6372 | cur_hdr->len += delta; |
| 6373 | } else { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6374 | delta = buffer_replace2(req, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6375 | |
| 6376 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6377 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6378 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6379 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6380 | cur_idx = old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6381 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6382 | hdr_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6383 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6384 | } |
| 6385 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6386 | /* check next header */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6387 | old_idx = cur_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6388 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6389 | } |
| 6390 | |
| 6391 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6392 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 6393 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 6394 | */ |
| 6395 | int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 6396 | { |
| 6397 | char term; |
| 6398 | char *cur_ptr, *cur_end, *cur_next; |
| 6399 | int cur_idx, old_idx, last_hdr; |
| 6400 | struct http_txn *txn = &t->txn; |
| 6401 | struct hdr_idx_elem *cur_hdr; |
| 6402 | int len, delta; |
| 6403 | |
| 6404 | last_hdr = 0; |
| 6405 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6406 | cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6407 | old_idx = 0; |
| 6408 | |
| 6409 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6410 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6411 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6412 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6413 | (exp->action == ACT_ALLOW || |
| 6414 | exp->action == ACT_DENY)) |
| 6415 | return 0; |
| 6416 | |
| 6417 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 6418 | if (!cur_idx) |
| 6419 | break; |
| 6420 | |
| 6421 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 6422 | cur_ptr = cur_next; |
| 6423 | cur_end = cur_ptr + cur_hdr->len; |
| 6424 | cur_next = cur_end + cur_hdr->cr + 1; |
| 6425 | |
| 6426 | /* Now we have one header between cur_ptr and cur_end, |
| 6427 | * and the next header starts at cur_next. |
| 6428 | */ |
| 6429 | |
| 6430 | /* The annoying part is that pattern matching needs |
| 6431 | * that we modify the contents to null-terminate all |
| 6432 | * strings before testing them. |
| 6433 | */ |
| 6434 | |
| 6435 | term = *cur_end; |
| 6436 | *cur_end = '\0'; |
| 6437 | |
| 6438 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 6439 | switch (exp->action) { |
| 6440 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6441 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6442 | last_hdr = 1; |
| 6443 | break; |
| 6444 | |
| 6445 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6446 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6447 | last_hdr = 1; |
| 6448 | break; |
| 6449 | |
| 6450 | case ACT_REPLACE: |
| 6451 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 6452 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 6453 | /* FIXME: if the user adds a newline in the replacement, the |
| 6454 | * index will not be recalculated for now, and the new line |
| 6455 | * will not be counted as a new header. |
| 6456 | */ |
| 6457 | |
| 6458 | cur_end += delta; |
| 6459 | cur_next += delta; |
| 6460 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6461 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6462 | break; |
| 6463 | |
| 6464 | case ACT_REMOVE: |
| 6465 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 6466 | cur_next += delta; |
| 6467 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6468 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6469 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6470 | txn->hdr_idx.used--; |
| 6471 | cur_hdr->len = 0; |
| 6472 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6473 | cur_idx = old_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6474 | break; |
| 6475 | |
| 6476 | } |
| 6477 | } |
| 6478 | if (cur_end) |
| 6479 | *cur_end = term; /* restore the string terminator */ |
| 6480 | |
| 6481 | /* keep the link from this header to next one in case of later |
| 6482 | * removal of next header. |
| 6483 | */ |
| 6484 | old_idx = cur_idx; |
| 6485 | } |
| 6486 | return 0; |
| 6487 | } |
| 6488 | |
| 6489 | |
| 6490 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 6491 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 6492 | * or -1 if a replacement resulted in an invalid status line. |
| 6493 | */ |
| 6494 | int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 6495 | { |
| 6496 | char term; |
| 6497 | char *cur_ptr, *cur_end; |
| 6498 | int done; |
| 6499 | struct http_txn *txn = &t->txn; |
| 6500 | int len, delta; |
| 6501 | |
| 6502 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6503 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6504 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6505 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6506 | (exp->action == ACT_ALLOW || |
| 6507 | exp->action == ACT_DENY)) |
| 6508 | return 0; |
| 6509 | else if (exp->action == ACT_REMOVE) |
| 6510 | return 0; |
| 6511 | |
| 6512 | done = 0; |
| 6513 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6514 | cur_ptr = txn->rsp.sol; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 6515 | cur_end = cur_ptr + txn->rsp.sl.st.l; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6516 | |
| 6517 | /* Now we have the status line between cur_ptr and cur_end */ |
| 6518 | |
| 6519 | /* The annoying part is that pattern matching needs |
| 6520 | * that we modify the contents to null-terminate all |
| 6521 | * strings before testing them. |
| 6522 | */ |
| 6523 | |
| 6524 | term = *cur_end; |
| 6525 | *cur_end = '\0'; |
| 6526 | |
| 6527 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 6528 | switch (exp->action) { |
| 6529 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6530 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6531 | done = 1; |
| 6532 | break; |
| 6533 | |
| 6534 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6535 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6536 | done = 1; |
| 6537 | break; |
| 6538 | |
| 6539 | case ACT_REPLACE: |
| 6540 | *cur_end = term; /* restore the string terminator */ |
| 6541 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 6542 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 6543 | /* FIXME: if the user adds a newline in the replacement, the |
| 6544 | * index will not be recalculated for now, and the new line |
| 6545 | * will not be counted as a new header. |
| 6546 | */ |
| 6547 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6548 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6549 | cur_end += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6550 | cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 6551 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6552 | cur_ptr, cur_end + 1, |
| 6553 | NULL, NULL); |
| 6554 | if (unlikely(!cur_end)) |
| 6555 | return -1; |
| 6556 | |
| 6557 | /* we have a full respnse and we know that we have either a CR |
| 6558 | * or an LF at <ptr>. |
| 6559 | */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6560 | txn->status = strl2ui(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] | 6561 | 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] | 6562 | /* there is no point trying this regex on headers */ |
| 6563 | return 1; |
| 6564 | } |
| 6565 | } |
| 6566 | *cur_end = term; /* restore the string terminator */ |
| 6567 | return done; |
| 6568 | } |
| 6569 | |
| 6570 | |
| 6571 | |
| 6572 | /* |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6573 | * 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] | 6574 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 6575 | * unparsable response. |
| 6576 | */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6577 | 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] | 6578 | { |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6579 | struct http_txn *txn = &s->txn; |
| 6580 | struct hdr_exp *exp; |
| 6581 | |
| 6582 | for (exp = px->rsp_exp; exp; exp = exp->next) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6583 | int ret; |
| 6584 | |
| 6585 | /* |
| 6586 | * The interleaving of transformations and verdicts |
| 6587 | * makes it difficult to decide to continue or stop |
| 6588 | * the evaluation. |
| 6589 | */ |
| 6590 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6591 | if (txn->flags & TX_SVDENY) |
| 6592 | break; |
| 6593 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6594 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6595 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 6596 | exp->action == ACT_PASS)) { |
| 6597 | exp = exp->next; |
| 6598 | continue; |
| 6599 | } |
| 6600 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6601 | /* if this filter had a condition, evaluate it now and skip to |
| 6602 | * next filter if the condition does not match. |
| 6603 | */ |
| 6604 | if (exp->cond) { |
| 6605 | ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_RTR); |
| 6606 | ret = acl_pass(ret); |
| 6607 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 6608 | ret = !ret; |
| 6609 | if (!ret) |
| 6610 | continue; |
| 6611 | } |
| 6612 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6613 | /* Apply the filter to the status line. */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6614 | ret = apply_filter_to_sts_line(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6615 | if (unlikely(ret < 0)) |
| 6616 | return -1; |
| 6617 | |
| 6618 | if (likely(ret == 0)) { |
| 6619 | /* The filter did not match the response, it can be |
| 6620 | * iterated through all headers. |
| 6621 | */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6622 | apply_filter_to_resp_headers(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6623 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6624 | } |
| 6625 | return 0; |
| 6626 | } |
| 6627 | |
| 6628 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6629 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 6630 | * 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] | 6631 | * desirable to call it only when needed. This function is also used when we |
| 6632 | * 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] | 6633 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6634 | void manage_server_side_cookies(struct session *t, struct buffer *res) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6635 | { |
| 6636 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6637 | struct server *srv; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6638 | int is_cookie2; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6639 | int cur_idx, old_idx, delta; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6640 | char *hdr_beg, *hdr_end, *hdr_next; |
| 6641 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6642 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6643 | /* Iterate through the headers. |
| 6644 | * we start with the start line. |
| 6645 | */ |
| 6646 | old_idx = 0; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6647 | hdr_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6648 | |
| 6649 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 6650 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6651 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6652 | |
| 6653 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6654 | hdr_beg = hdr_next; |
| 6655 | hdr_end = hdr_beg + cur_hdr->len; |
| 6656 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6657 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6658 | /* We have one full header between hdr_beg and hdr_end, and the |
| 6659 | * next header starts at hdr_next. We're only interested in |
| 6660 | * "Set-Cookie" and "Set-Cookie2" headers. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6661 | */ |
| 6662 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6663 | is_cookie2 = 0; |
| 6664 | prev = hdr_beg + 10; |
| 6665 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6666 | if (!val) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6667 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11); |
| 6668 | if (!val) { |
| 6669 | old_idx = cur_idx; |
| 6670 | continue; |
| 6671 | } |
| 6672 | is_cookie2 = 1; |
| 6673 | prev = hdr_beg + 11; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6674 | } |
| 6675 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6676 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 6677 | * <prev> points to the colon. |
| 6678 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 6679 | txn->flags |= TX_SCK_PRESENT; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6680 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6681 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 6682 | * check-cache is enabled) and we are not interested in checking |
| 6683 | * them. Warning, the cookie capture is declared in the frontend. |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 6684 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6685 | if (t->be->cookie_name == NULL && |
| 6686 | t->be->appsession_name == NULL && |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 6687 | t->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6688 | return; |
| 6689 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6690 | /* OK so now we know we have to process this response cookie. |
| 6691 | * The format of the Set-Cookie header is slightly different |
| 6692 | * from the format of the Cookie header in that it does not |
| 6693 | * support the comma as a cookie delimiter (thus the header |
| 6694 | * cannot be folded) because the Expires attribute described in |
| 6695 | * the original Netscape's spec may contain an unquoted date |
| 6696 | * with a comma inside. We have to live with this because |
| 6697 | * many browsers don't support Max-Age and some browsers don't |
| 6698 | * support quoted strings. However the Set-Cookie2 header is |
| 6699 | * clean. |
| 6700 | * |
| 6701 | * We have to keep multiple pointers in order to support cookie |
| 6702 | * removal at the beginning, middle or end of header without |
| 6703 | * corrupting the header (in case of set-cookie2). A special |
| 6704 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 6705 | * fields after the first semi-colon. The <next> pointer points |
| 6706 | * either to the end of line (set-cookie) or next unquoted comma |
| 6707 | * (set-cookie2). All of these headers are valid : |
| 6708 | * |
| 6709 | * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n |
| 6710 | * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 6711 | * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 6712 | * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n |
| 6713 | * | | | | | | | | | | |
| 6714 | * | | | | | | | | +-> next hdr_end <--+ |
| 6715 | * | | | | | | | +------------> scav |
| 6716 | * | | | | | | +--------------> val_end |
| 6717 | * | | | | | +--------------------> val_beg |
| 6718 | * | | | | +----------------------> equal |
| 6719 | * | | | +------------------------> att_end |
| 6720 | * | | +----------------------------> att_beg |
| 6721 | * | +------------------------------> prev |
| 6722 | * +-----------------------------------------> hdr_beg |
| 6723 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6724 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6725 | for (; prev < hdr_end; prev = next) { |
| 6726 | /* Iterate through all cookies on this line */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6727 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6728 | /* find att_beg */ |
| 6729 | att_beg = prev + 1; |
| 6730 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 6731 | att_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6732 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6733 | /* find att_end : this is the first character after the last non |
| 6734 | * space before the equal. It may be equal to hdr_end. |
| 6735 | */ |
| 6736 | equal = att_end = att_beg; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6737 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6738 | while (equal < hdr_end) { |
| 6739 | if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ',')) |
| 6740 | break; |
| 6741 | if (http_is_spht[(unsigned char)*equal++]) |
| 6742 | continue; |
| 6743 | att_end = equal; |
| 6744 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6745 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6746 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 6747 | * is between <att_beg> and <equal>, both may be identical. |
| 6748 | */ |
| 6749 | |
| 6750 | /* look for end of cookie if there is an equal sign */ |
| 6751 | if (equal < hdr_end && *equal == '=') { |
| 6752 | /* look for the beginning of the value */ |
| 6753 | val_beg = equal + 1; |
| 6754 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 6755 | val_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6756 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6757 | /* find the end of the value, respecting quotes */ |
| 6758 | next = find_cookie_value_end(val_beg, hdr_end); |
| 6759 | |
| 6760 | /* make val_end point to the first white space or delimitor after the value */ |
| 6761 | val_end = next; |
| 6762 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 6763 | val_end--; |
| 6764 | } else { |
| 6765 | /* <equal> points to next comma, semi-colon or EOL */ |
| 6766 | val_beg = val_end = next = equal; |
| 6767 | } |
| 6768 | |
| 6769 | if (next < hdr_end) { |
| 6770 | /* Set-Cookie2 supports multiple cookies, and <next> points to |
| 6771 | * a colon or semi-colon before the end. So skip all attr-value |
| 6772 | * pairs and look for the next comma. For Set-Cookie, since |
| 6773 | * commas are permitted in values, skip to the end. |
| 6774 | */ |
| 6775 | if (is_cookie2) |
| 6776 | next = find_hdr_value_end(next, hdr_end); |
| 6777 | else |
| 6778 | next = hdr_end; |
| 6779 | } |
| 6780 | |
| 6781 | /* Now everything is as on the diagram above */ |
| 6782 | |
| 6783 | /* Ignore cookies with no equal sign */ |
| 6784 | if (equal == val_end) |
| 6785 | continue; |
| 6786 | |
| 6787 | /* If there are spaces around the equal sign, we need to |
| 6788 | * strip them otherwise we'll get trouble for cookie captures, |
| 6789 | * or even for rewrites. Since this happens extremely rarely, |
| 6790 | * it does not hurt performance. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6791 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6792 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 6793 | int stripped_before = 0; |
| 6794 | int stripped_after = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6795 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6796 | if (att_end != equal) { |
| 6797 | stripped_before = buffer_replace2(res, att_end, equal, NULL, 0); |
| 6798 | equal += stripped_before; |
| 6799 | val_beg += stripped_before; |
| 6800 | } |
| 6801 | |
| 6802 | if (val_beg > equal + 1) { |
| 6803 | stripped_after = buffer_replace2(res, equal + 1, val_beg, NULL, 0); |
| 6804 | val_beg += stripped_after; |
| 6805 | stripped_before += stripped_after; |
| 6806 | } |
| 6807 | |
| 6808 | val_end += stripped_before; |
| 6809 | next += stripped_before; |
| 6810 | hdr_end += stripped_before; |
| 6811 | hdr_next += stripped_before; |
| 6812 | cur_hdr->len += stripped_before; |
Willy Tarreau | 1fc1f45 | 2011-04-07 22:35:37 +0200 | [diff] [blame] | 6813 | http_msg_move_end(&txn->rsp, stripped_before); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6814 | } |
| 6815 | |
| 6816 | /* First, let's see if we want to capture this cookie. We check |
| 6817 | * that we don't already have a server side cookie, because we |
| 6818 | * can only capture one. Also as an optimisation, we ignore |
| 6819 | * cookies shorter than the declared name. |
| 6820 | */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 6821 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 6822 | txn->srv_cookie == NULL && |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6823 | (val_end - att_beg >= t->fe->capture_namelen) && |
| 6824 | memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
| 6825 | int log_len = val_end - att_beg; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 6826 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6827 | Alert("HTTP logging : out of memory.\n"); |
| 6828 | } |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 6829 | else { |
| 6830 | if (log_len > t->fe->capture_len) |
| 6831 | log_len = t->fe->capture_len; |
| 6832 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 6833 | txn->srv_cookie[log_len] = 0; |
| 6834 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6835 | } |
| 6836 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6837 | srv = target_srv(&t->target); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6838 | /* now check if we need to process it for persistence */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6839 | if (!(t->flags & SN_IGNORE_PRST) && |
| 6840 | (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 6841 | (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) { |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 6842 | /* assume passive cookie by default */ |
| 6843 | txn->flags &= ~TX_SCK_MASK; |
| 6844 | txn->flags |= TX_SCK_FOUND; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6845 | |
| 6846 | /* If the cookie is in insert mode on a known server, we'll delete |
| 6847 | * this occurrence because we'll insert another one later. |
| 6848 | * 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] | 6849 | * a direct access. |
| 6850 | */ |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 6851 | if (t->be->options2 & PR_O2_COOK_PSV) { |
| 6852 | /* The "preserve" flag was set, we don't want to touch the |
| 6853 | * server's cookie. |
| 6854 | */ |
| 6855 | } |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6856 | else if ((srv && (t->be->options & PR_O_COOK_INS)) || |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6857 | ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6858 | /* this cookie must be deleted */ |
| 6859 | if (*prev == ':' && next == hdr_end) { |
| 6860 | /* whole header */ |
| 6861 | delta = buffer_replace2(res, hdr_beg, hdr_next, NULL, 0); |
| 6862 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6863 | txn->hdr_idx.used--; |
| 6864 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6865 | cur_idx = old_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6866 | hdr_next += delta; |
| 6867 | http_msg_move_end(&txn->rsp, delta); |
| 6868 | /* note: while both invalid now, <next> and <hdr_end> |
| 6869 | * are still equal, so the for() will stop as expected. |
| 6870 | */ |
| 6871 | } else { |
| 6872 | /* just remove the value */ |
| 6873 | int delta = del_hdr_value(res, &prev, next); |
| 6874 | next = prev; |
| 6875 | hdr_end += delta; |
| 6876 | hdr_next += delta; |
| 6877 | cur_hdr->len += delta; |
| 6878 | http_msg_move_end(&txn->rsp, delta); |
| 6879 | } |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 6880 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6881 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6882 | /* and go on with next cookie */ |
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 | else if (srv && srv->cookie && (t->be->options & PR_O_COOK_RW)) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6885 | /* replace bytes val_beg->val_end with the cookie name associated |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6886 | * with this server since we know it. |
| 6887 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6888 | delta = buffer_replace2(res, val_beg, val_end, srv->cookie, srv->cklen); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6889 | next += delta; |
| 6890 | hdr_end += delta; |
| 6891 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6892 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6893 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6894 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 6895 | txn->flags &= ~TX_SCK_MASK; |
| 6896 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6897 | } |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6898 | else if (srv && srv && (t->be->options & PR_O_COOK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6899 | /* insert the cookie name associated with this server |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6900 | * before existing cookie, and insert a delimiter between them.. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6901 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6902 | 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] | 6903 | next += delta; |
| 6904 | hdr_end += delta; |
| 6905 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6906 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6907 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6908 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6909 | val_beg[srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 6910 | txn->flags &= ~TX_SCK_MASK; |
| 6911 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6912 | } |
| 6913 | } |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 6914 | /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */ |
| 6915 | else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6916 | int cmp_len, value_len; |
| 6917 | char *value_begin; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6918 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6919 | if (t->be->options2 & PR_O2_AS_PFX) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6920 | cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len); |
| 6921 | value_begin = att_beg + t->be->appsession_name_len; |
| 6922 | 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] | 6923 | } else { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6924 | cmp_len = att_end - att_beg; |
| 6925 | value_begin = val_beg; |
| 6926 | value_len = MIN(t->be->appsession_len, val_end - val_beg); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6927 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6928 | |
Cyril Bonté | 17530c3 | 2010-04-06 21:11:10 +0200 | [diff] [blame] | 6929 | if ((cmp_len == t->be->appsession_name_len) && |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6930 | (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) { |
| 6931 | /* free a possibly previously allocated memory */ |
| 6932 | pool_free2(apools.sessid, txn->sessid); |
| 6933 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6934 | /* Store the sessid in the session for future use */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6935 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6936 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 6937 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 6938 | return; |
| 6939 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6940 | memcpy(txn->sessid, value_begin, value_len); |
| 6941 | txn->sessid[value_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6942 | } |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6943 | } |
| 6944 | /* that's done for this cookie, check the next one on the same |
| 6945 | * line when next != hdr_end (only if is_cookie2). |
| 6946 | */ |
| 6947 | } |
| 6948 | /* check next header */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6949 | old_idx = cur_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6950 | } |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6951 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6952 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6953 | appsess *asession = NULL; |
| 6954 | /* only do insert, if lookup fails */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6955 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6956 | if (asession == NULL) { |
Willy Tarreau | 1fac753 | 2010-01-09 19:23:06 +0100 | [diff] [blame] | 6957 | size_t server_id_len; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6958 | if ((asession = pool_alloc2(pool2_appsess)) == NULL) { |
| 6959 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
| 6960 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n"); |
| 6961 | return; |
| 6962 | } |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 6963 | asession->serverid = NULL; /* to avoid a double free in case of allocation error */ |
| 6964 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6965 | if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) { |
| 6966 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 6967 | 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] | 6968 | t->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6969 | return; |
| 6970 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6971 | memcpy(asession->sessid, txn->sessid, t->be->appsession_len); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6972 | asession->sessid[t->be->appsession_len] = 0; |
| 6973 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6974 | server_id_len = strlen(target_srv(&t->target)->id) + 1; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6975 | if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) { |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 6976 | Alert("Not enough Memory process_srv():asession->serverid:malloc().\n"); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6977 | 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] | 6978 | t->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6979 | return; |
| 6980 | } |
| 6981 | asession->serverid[0] = '\0'; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6982 | memcpy(asession->serverid, target_srv(&t->target)->id, server_id_len); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6983 | |
| 6984 | asession->request_count = 0; |
| 6985 | appsession_hash_insert(&(t->be->htbl_proxy), asession); |
| 6986 | } |
| 6987 | |
| 6988 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 6989 | asession->request_count++; |
| 6990 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6991 | } |
| 6992 | |
| 6993 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6994 | /* |
| 6995 | * Check if response is cacheable or not. Updates t->flags. |
| 6996 | */ |
| 6997 | void check_response_for_cacheability(struct session *t, struct buffer *rtr) |
| 6998 | { |
| 6999 | struct http_txn *txn = &t->txn; |
| 7000 | char *p1, *p2; |
| 7001 | |
| 7002 | char *cur_ptr, *cur_end, *cur_next; |
| 7003 | int cur_idx; |
| 7004 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 7005 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7006 | return; |
| 7007 | |
| 7008 | /* Iterate through the headers. |
| 7009 | * we start with the start line. |
| 7010 | */ |
| 7011 | cur_idx = 0; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7012 | cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7013 | |
| 7014 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 7015 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7016 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7017 | |
| 7018 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 7019 | cur_ptr = cur_next; |
| 7020 | cur_end = cur_ptr + cur_hdr->len; |
| 7021 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7022 | |
| 7023 | /* We have one full header between cur_ptr and cur_end, and the |
| 7024 | * next header starts at cur_next. We're only interested in |
| 7025 | * "Cookie:" headers. |
| 7026 | */ |
| 7027 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7028 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 7029 | if (val) { |
| 7030 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 7031 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 7032 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 7033 | return; |
| 7034 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7035 | } |
| 7036 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7037 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 7038 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7039 | continue; |
| 7040 | |
| 7041 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 7042 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7043 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7044 | |
| 7045 | if (p1 >= cur_end) /* no more info */ |
| 7046 | continue; |
| 7047 | |
| 7048 | /* p1 is at the beginning of the value */ |
| 7049 | p2 = p1; |
| 7050 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 7051 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7052 | p2++; |
| 7053 | |
| 7054 | /* we have a complete value between p1 and p2 */ |
| 7055 | if (p2 < cur_end && *p2 == '=') { |
| 7056 | /* we have something of the form no-cache="set-cookie" */ |
| 7057 | if ((cur_end - p1 >= 21) && |
| 7058 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 7059 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7060 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7061 | continue; |
| 7062 | } |
| 7063 | |
| 7064 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 7065 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
| 7066 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 7067 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 7068 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7069 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7070 | return; |
| 7071 | } |
| 7072 | |
| 7073 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7074 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7075 | continue; |
| 7076 | } |
| 7077 | } |
| 7078 | } |
| 7079 | |
| 7080 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7081 | /* |
| 7082 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 7083 | * If the server is found, it's assigned to the session. |
| 7084 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7085 | 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] | 7086 | { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7087 | char *end_params, *first_param, *cur_param, *next_param; |
| 7088 | char separator; |
| 7089 | int value_len; |
| 7090 | |
| 7091 | int mode = t->be->options2 & PR_O2_AS_M_ANY; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7092 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 7093 | if (t->be->appsession_name == NULL || |
Cyril Bonté | 17530c3 | 2010-04-06 21:11:10 +0200 | [diff] [blame] | 7094 | (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] | 7095 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7096 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7097 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7098 | first_param = NULL; |
| 7099 | switch (mode) { |
| 7100 | case PR_O2_AS_M_PP: |
| 7101 | first_param = memchr(begin, ';', len); |
| 7102 | break; |
| 7103 | case PR_O2_AS_M_QS: |
| 7104 | first_param = memchr(begin, '?', len); |
| 7105 | break; |
| 7106 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7107 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7108 | if (first_param == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7109 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7110 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7111 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7112 | switch (mode) { |
| 7113 | case PR_O2_AS_M_PP: |
| 7114 | if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) { |
| 7115 | end_params = (char *) begin + len; |
| 7116 | } |
| 7117 | separator = ';'; |
| 7118 | break; |
| 7119 | case PR_O2_AS_M_QS: |
| 7120 | end_params = (char *) begin + len; |
| 7121 | separator = '&'; |
| 7122 | break; |
| 7123 | default: |
| 7124 | /* unknown mode, shouldn't happen */ |
| 7125 | return; |
| 7126 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7127 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7128 | cur_param = next_param = end_params; |
| 7129 | while (cur_param > first_param) { |
| 7130 | cur_param--; |
| 7131 | if ((cur_param[0] == separator) || (cur_param == first_param)) { |
| 7132 | /* let's see if this is the appsession parameter */ |
| 7133 | if ((cur_param + t->be->appsession_name_len + 1 < next_param) && |
| 7134 | ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') && |
| 7135 | (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) { |
| 7136 | /* Cool... it's the right one */ |
| 7137 | cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2); |
| 7138 | value_len = MIN(t->be->appsession_len, next_param - cur_param); |
| 7139 | if (value_len > 0) { |
| 7140 | manage_client_side_appsession(t, cur_param, value_len); |
| 7141 | } |
| 7142 | break; |
| 7143 | } |
| 7144 | next_param = cur_param; |
| 7145 | } |
| 7146 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7147 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 7148 | Alert("get_srv_from_appsession\n"); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 7149 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7150 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7151 | } |
| 7152 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7153 | /* |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7154 | * 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] | 7155 | * for the current backend. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7156 | * |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7157 | * 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] | 7158 | * uri_auth field is valid. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7159 | * |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7160 | * Returns 1 if stats should be provided, otherwise 0. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7161 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7162 | 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] | 7163 | { |
| 7164 | struct uri_auth *uri_auth = backend->uri_auth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7165 | char *h; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7166 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7167 | if (!uri_auth) |
| 7168 | return 0; |
| 7169 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7170 | 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] | 7171 | return 0; |
| 7172 | |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7173 | memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats)); |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 7174 | si->applet.ctx.stats.st_code = STAT_STATUS_INIT; |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 7175 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7176 | /* check URI size */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7177 | if (uri_auth->uri_len > txn->req.sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7178 | return 0; |
| 7179 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7180 | h = txn->req.sol + txn->req.sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7181 | |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 7182 | /* the URI is in h */ |
| 7183 | if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7184 | return 0; |
| 7185 | |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7186 | h += uri_auth->uri_len; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7187 | while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) { |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7188 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7189 | si->applet.ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7190 | break; |
| 7191 | } |
| 7192 | h++; |
| 7193 | } |
| 7194 | |
| 7195 | if (uri_auth->refresh) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7196 | h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len; |
| 7197 | while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) { |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7198 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7199 | si->applet.ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7200 | break; |
| 7201 | } |
| 7202 | h++; |
| 7203 | } |
| 7204 | } |
| 7205 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7206 | h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len; |
| 7207 | while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) { |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 7208 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7209 | si->applet.ctx.stats.flags |= STAT_FMT_CSV; |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 7210 | break; |
| 7211 | } |
| 7212 | h++; |
| 7213 | } |
| 7214 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7215 | h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len; |
| 7216 | while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 8) { |
| 7217 | if (memcmp(h, ";st=", 4) == 0) { |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 7218 | int i; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7219 | h += 4; |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 7220 | for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) { |
| 7221 | if (strncmp(stat_status_codes[i], h, 4) == 0) { |
| 7222 | si->applet.ctx.stats.st_code = i; |
| 7223 | break; |
| 7224 | } |
| 7225 | } |
| 7226 | si->applet.ctx.stats.st_code = STAT_STATUS_UNKN; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7227 | break; |
| 7228 | } |
| 7229 | h++; |
| 7230 | } |
| 7231 | |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7232 | si->applet.ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO; |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 7233 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7234 | return 1; |
| 7235 | } |
| 7236 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7237 | /* |
| 7238 | * 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] | 7239 | * WARNING: it's unlikely that we've reached HTTP_MSG_BODY here so we must not |
| 7240 | * assume that msg->sol = buf->data + msg->som. |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7241 | */ |
| 7242 | void http_capture_bad_message(struct error_snapshot *es, struct session *s, |
| 7243 | struct buffer *buf, struct http_msg *msg, |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 7244 | int state, struct proxy *other_end) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7245 | { |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7246 | if (buf->r <= (buf->data + msg->som)) { /* message wraps */ |
| 7247 | int len1 = buf->size - msg->som; |
| 7248 | es->len = buf->r - (buf->data + msg->som) + buf->size; |
| 7249 | memcpy(es->buf, buf->data + msg->som, MIN(len1, sizeof(es->buf))); |
| 7250 | if (es->len > len1 && len1 < sizeof(es->buf)) |
| 7251 | memcpy(es->buf, buf->data, MIN(es->len, sizeof(es->buf)) - len1); |
| 7252 | } |
| 7253 | else { |
| 7254 | es->len = buf->r - (buf->data + msg->som); |
| 7255 | memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf))); |
| 7256 | } |
| 7257 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7258 | if (msg->err_pos >= 0) |
Willy Tarreau | 2df8d71 | 2009-05-01 11:33:17 +0200 | [diff] [blame] | 7259 | es->pos = msg->err_pos - msg->som; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7260 | else if (buf->lr >= (buf->data + msg->som)) |
Willy Tarreau | 2df8d71 | 2009-05-01 11:33:17 +0200 | [diff] [blame] | 7261 | es->pos = buf->lr - (buf->data + msg->som); |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7262 | else |
| 7263 | es->pos = buf->lr - (buf->data + msg->som) + buf->size; |
| 7264 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7265 | es->when = date; // user-visible date |
| 7266 | es->sid = s->uniq_id; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7267 | es->srv = target_srv(&s->target); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7268 | es->oe = other_end; |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 7269 | es->src = s->req->prod->addr.from; |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 7270 | es->state = state; |
| 7271 | es->flags = buf->flags; |
Willy Tarreau | 10479e4 | 2010-12-12 14:00:34 +0100 | [diff] [blame] | 7272 | es->ev_id = error_snapshot_id++; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7273 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7274 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7275 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 7276 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 7277 | * performed over the whole headers. Otherwise it must contain a valid header |
| 7278 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 7279 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 7280 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 7281 | * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is |
| 7282 | * -1. |
| 7283 | * 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] | 7284 | */ |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7285 | unsigned int http_get_hdr(struct http_msg *msg, const char *hname, int hlen, |
| 7286 | struct hdr_idx *idx, int occ, |
| 7287 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7288 | { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7289 | struct hdr_ctx local_ctx; |
| 7290 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 7291 | int len_hist[MAX_HDR_HISTORY]; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7292 | unsigned int hist_ptr; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7293 | int found; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7294 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7295 | if (!ctx) { |
| 7296 | local_ctx.idx = 0; |
| 7297 | ctx = &local_ctx; |
| 7298 | } |
| 7299 | |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7300 | if (occ >= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7301 | /* search from the beginning */ |
| 7302 | while (http_find_header2(hname, hlen, msg->sol, idx, ctx)) { |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7303 | occ--; |
| 7304 | if (occ <= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7305 | *vptr = ctx->line + ctx->val; |
| 7306 | *vlen = ctx->vlen; |
| 7307 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7308 | } |
| 7309 | } |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7310 | return 0; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7311 | } |
| 7312 | |
| 7313 | /* negative occurrence, we scan all the list then walk back */ |
| 7314 | if (-occ > MAX_HDR_HISTORY) |
| 7315 | return 0; |
| 7316 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7317 | found = hist_ptr = 0; |
| 7318 | while (http_find_header2(hname, hlen, msg->sol, idx, ctx)) { |
| 7319 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 7320 | len_hist[hist_ptr] = ctx->vlen; |
| 7321 | if (++hist_ptr >= MAX_HDR_HISTORY) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7322 | hist_ptr = 0; |
| 7323 | found++; |
| 7324 | } |
| 7325 | if (-occ > found) |
| 7326 | return 0; |
| 7327 | /* OK now we have the last occurrence in [hist_ptr-1], and we need to |
| 7328 | * find occurrence -occ, so we have to check [hist_ptr+occ]. |
| 7329 | */ |
| 7330 | hist_ptr += occ; |
| 7331 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 7332 | hist_ptr -= MAX_HDR_HISTORY; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7333 | *vptr = ptr_hist[hist_ptr]; |
| 7334 | *vlen = len_hist[hist_ptr]; |
| 7335 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7336 | } |
| 7337 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 7338 | /* |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7339 | * Print a debug line with a header |
| 7340 | */ |
| 7341 | void debug_hdr(const char *dir, struct session *t, const char *start, const char *end) |
| 7342 | { |
| 7343 | int len, max; |
| 7344 | 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] | 7345 | 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] | 7346 | max = end - start; |
| 7347 | UBOUND(max, sizeof(trash) - len - 1); |
| 7348 | len += strlcpy2(trash + len, start, max + 1); |
| 7349 | trash[len++] = '\n'; |
Willy Tarreau | 2133782 | 2012-04-29 14:11:38 +0200 | [diff] [blame] | 7350 | if (write(1, trash, len) < 0) /* shut gcc warning */; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7351 | } |
| 7352 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7353 | /* |
| 7354 | * Initialize a new HTTP transaction for session <s>. It is assumed that all |
| 7355 | * the required fields are properly allocated and that we only need to (re)init |
| 7356 | * them. This should be used before processing any new request. |
| 7357 | */ |
| 7358 | void http_init_txn(struct session *s) |
| 7359 | { |
| 7360 | struct http_txn *txn = &s->txn; |
| 7361 | struct proxy *fe = s->fe; |
| 7362 | |
| 7363 | txn->flags = 0; |
| 7364 | txn->status = -1; |
| 7365 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 7366 | global.req_count++; |
| 7367 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7368 | txn->cookie_first_date = 0; |
| 7369 | txn->cookie_last_date = 0; |
| 7370 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 7371 | txn->req.flags = 0; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7372 | txn->req.sol = txn->req.eol = NULL; |
| 7373 | txn->req.som = txn->req.eoh = 0; /* relative to the buffer */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 7374 | txn->rsp.flags = 0; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7375 | txn->rsp.sol = txn->rsp.eol = NULL; |
| 7376 | txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */ |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 7377 | txn->req.chunk_len = 0LL; |
| 7378 | txn->req.body_len = 0LL; |
| 7379 | txn->rsp.chunk_len = 0LL; |
| 7380 | txn->rsp.body_len = 0LL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7381 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 7382 | txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */ |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 7383 | |
| 7384 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7385 | |
| 7386 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 7387 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 7388 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 7389 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7390 | if (txn->req.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7391 | memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *)); |
| 7392 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7393 | if (txn->rsp.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7394 | memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
| 7395 | |
| 7396 | if (txn->hdr_idx.v) |
| 7397 | hdr_idx_init(&txn->hdr_idx); |
| 7398 | } |
| 7399 | |
| 7400 | /* to be used at the end of a transaction */ |
| 7401 | void http_end_txn(struct session *s) |
| 7402 | { |
| 7403 | struct http_txn *txn = &s->txn; |
| 7404 | |
| 7405 | /* these ones will have been dynamically allocated */ |
| 7406 | pool_free2(pool2_requri, txn->uri); |
| 7407 | pool_free2(pool2_capture, txn->cli_cookie); |
| 7408 | pool_free2(pool2_capture, txn->srv_cookie); |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7409 | pool_free2(apools.sessid, txn->sessid); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 7410 | pool_free2(pool2_uniqueid, s->unique_id); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 7411 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 7412 | s->unique_id = NULL; |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7413 | txn->sessid = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7414 | txn->uri = NULL; |
| 7415 | txn->srv_cookie = NULL; |
| 7416 | txn->cli_cookie = NULL; |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7417 | |
| 7418 | if (txn->req.cap) { |
| 7419 | struct cap_hdr *h; |
| 7420 | for (h = s->fe->req_cap; h; h = h->next) |
| 7421 | pool_free2(h->pool, txn->req.cap[h->index]); |
| 7422 | memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *)); |
| 7423 | } |
| 7424 | |
| 7425 | if (txn->rsp.cap) { |
| 7426 | struct cap_hdr *h; |
| 7427 | for (h = s->fe->rsp_cap; h; h = h->next) |
| 7428 | pool_free2(h->pool, txn->rsp.cap[h->index]); |
| 7429 | memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *)); |
| 7430 | } |
| 7431 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7432 | } |
| 7433 | |
| 7434 | /* to be used at the end of a transaction to prepare a new one */ |
| 7435 | void http_reset_txn(struct session *s) |
| 7436 | { |
| 7437 | http_end_txn(s); |
| 7438 | http_init_txn(s); |
| 7439 | |
| 7440 | s->be = s->fe; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7441 | s->logs.logwait = s->fe->to_log; |
Simon Horman | af51495 | 2011-06-21 14:34:57 +0900 | [diff] [blame] | 7442 | session_del_srv_conn(s); |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 7443 | clear_target(&s->target); |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 7444 | /* re-init store persistence */ |
| 7445 | s->store_count = 0; |
| 7446 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7447 | s->pend_pos = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7448 | |
| 7449 | s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */ |
| 7450 | |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 7451 | /* We must trim any excess data from the response buffer, because we |
| 7452 | * may have blocked an invalid response from a server that we don't |
| 7453 | * want to accidentely forward once we disable the analysers, nor do |
| 7454 | * we want those data to come along with next response. A typical |
| 7455 | * example of such data would be from a buggy server responding to |
| 7456 | * a HEAD with some data, or sending more than the advertised |
| 7457 | * content-length. |
| 7458 | */ |
| 7459 | if (unlikely(s->rep->l > s->rep->send_max)) { |
| 7460 | s->rep->l = s->rep->send_max; |
| 7461 | s->rep->r = s->rep->w + s->rep->l; |
| 7462 | if (s->rep->r >= s->rep->data + s->rep->size) |
| 7463 | s->rep->r -= s->rep->size; |
| 7464 | } |
| 7465 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7466 | s->req->rto = s->fe->timeout.client; |
Willy Tarreau | d04e858 | 2010-05-31 12:31:35 +0200 | [diff] [blame] | 7467 | s->req->wto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7468 | |
Willy Tarreau | d04e858 | 2010-05-31 12:31:35 +0200 | [diff] [blame] | 7469 | s->rep->rto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7470 | s->rep->wto = s->fe->timeout.client; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7471 | |
| 7472 | s->req->rex = TICK_ETERNITY; |
| 7473 | s->req->wex = TICK_ETERNITY; |
| 7474 | s->req->analyse_exp = TICK_ETERNITY; |
| 7475 | s->rep->rex = TICK_ETERNITY; |
| 7476 | s->rep->wex = TICK_ETERNITY; |
| 7477 | s->rep->analyse_exp = TICK_ETERNITY; |
| 7478 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7479 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 7480 | void free_http_req_rules(struct list *r) { |
| 7481 | struct http_req_rule *tr, *pr; |
| 7482 | |
| 7483 | list_for_each_entry_safe(pr, tr, r, list) { |
| 7484 | LIST_DEL(&pr->list); |
| 7485 | if (pr->action == HTTP_REQ_ACT_HTTP_AUTH) |
| 7486 | free(pr->http_auth.realm); |
| 7487 | |
| 7488 | free(pr); |
| 7489 | } |
| 7490 | } |
| 7491 | |
| 7492 | struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
| 7493 | { |
| 7494 | struct http_req_rule *rule; |
| 7495 | int cur_arg; |
| 7496 | |
| 7497 | rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule)); |
| 7498 | if (!rule) { |
| 7499 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
| 7500 | return NULL; |
| 7501 | } |
| 7502 | |
| 7503 | if (!*args[0]) { |
| 7504 | goto req_error_parsing; |
| 7505 | } else if (!strcmp(args[0], "allow")) { |
| 7506 | rule->action = HTTP_REQ_ACT_ALLOW; |
| 7507 | cur_arg = 1; |
| 7508 | } else if (!strcmp(args[0], "deny")) { |
| 7509 | rule->action = HTTP_REQ_ACT_DENY; |
| 7510 | cur_arg = 1; |
| 7511 | } else if (!strcmp(args[0], "auth")) { |
| 7512 | rule->action = HTTP_REQ_ACT_HTTP_AUTH; |
| 7513 | cur_arg = 1; |
| 7514 | |
| 7515 | while(*args[cur_arg]) { |
| 7516 | if (!strcmp(args[cur_arg], "realm")) { |
| 7517 | rule->http_auth.realm = strdup(args[cur_arg + 1]); |
| 7518 | cur_arg+=2; |
| 7519 | continue; |
| 7520 | } else |
| 7521 | break; |
| 7522 | } |
| 7523 | } else { |
| 7524 | req_error_parsing: |
| 7525 | Alert("parsing [%s:%d]: %s '%s', expects 'allow', 'deny', 'auth'.\n", |
| 7526 | file, linenum, *args[1]?"unknown parameter":"missing keyword in", args[*args[1]?1:0]); |
| 7527 | return NULL; |
| 7528 | } |
| 7529 | |
| 7530 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 7531 | struct acl_cond *cond; |
| 7532 | |
| 7533 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg)) == NULL) { |
| 7534 | Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition.\n", |
| 7535 | file, linenum, args[0]); |
| 7536 | return NULL; |
| 7537 | } |
| 7538 | rule->cond = cond; |
| 7539 | } |
| 7540 | else if (*args[cur_arg]) { |
| 7541 | Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or" |
| 7542 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 7543 | file, linenum, args[0], args[cur_arg]); |
| 7544 | return NULL; |
| 7545 | } |
| 7546 | |
| 7547 | return rule; |
| 7548 | } |
| 7549 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7550 | /************************************************************************/ |
| 7551 | /* The code below is dedicated to ACL parsing and matching */ |
| 7552 | /************************************************************************/ |
| 7553 | |
| 7554 | |
| 7555 | |
| 7556 | |
| 7557 | /* 1. Check on METHOD |
| 7558 | * We use the pre-parsed method if it is known, and store its number as an |
| 7559 | * integer. If it is unknown, we use the pointer and the length. |
| 7560 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7561 | static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7562 | { |
| 7563 | int len, meth; |
| 7564 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7565 | len = strlen(*text); |
| 7566 | meth = find_http_meth(*text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7567 | |
| 7568 | pattern->val.i = meth; |
| 7569 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7570 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7571 | if (!pattern->ptr.str) |
| 7572 | return 0; |
| 7573 | pattern->len = len; |
| 7574 | } |
| 7575 | return 1; |
| 7576 | } |
| 7577 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7578 | /* This function fetches the method of current HTTP request and stores |
| 7579 | * it in the global pattern struct as a chunk. There are two possibilities : |
| 7580 | * - if the method is known (not HTTP_METH_OTHER), its identifier is stored |
| 7581 | * in <len> and <ptr> is NULL ; |
| 7582 | * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and |
| 7583 | * <len> to its length. |
| 7584 | * This is intended to be used with acl_match_meth() only. |
| 7585 | */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7586 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 7587 | acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7588 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7589 | { |
| 7590 | int meth; |
| 7591 | struct http_txn *txn = l7; |
| 7592 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7593 | if (!txn) |
| 7594 | return 0; |
| 7595 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7596 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7597 | return 0; |
| 7598 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7599 | meth = txn->meth; |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7600 | temp_pattern.data.str.len = meth; |
| 7601 | temp_pattern.data.str.str = NULL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7602 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7603 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7604 | /* ensure the indexes are not affected */ |
| 7605 | return 0; |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7606 | temp_pattern.data.str.len = txn->req.sl.rq.m_l; |
| 7607 | temp_pattern.data.str.str = txn->req.sol; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7608 | } |
| 7609 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 7610 | return 1; |
| 7611 | } |
| 7612 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7613 | /* See above how the method is stored in the global pattern */ |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7614 | static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern) |
| 7615 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 7616 | int icase; |
| 7617 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7618 | |
| 7619 | if (temp_pattern.data.str.str == NULL) { |
| 7620 | /* well-known method */ |
| 7621 | if (temp_pattern.data.str.len == pattern->val.i) |
| 7622 | return ACL_PAT_PASS; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 7623 | return ACL_PAT_FAIL; |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7624 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7625 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7626 | /* Uncommon method, only HTTP_METH_OTHER is accepted now */ |
| 7627 | if (pattern->val.i != HTTP_METH_OTHER) |
| 7628 | return ACL_PAT_FAIL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7629 | |
| 7630 | /* Other method, we must compare the strings */ |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7631 | if (pattern->len != temp_pattern.data.str.len) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 7632 | return ACL_PAT_FAIL; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 7633 | |
| 7634 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7635 | if ((icase && strncasecmp(pattern->ptr.str, temp_pattern.data.str.str, temp_pattern.data.str.len) != 0) || |
| 7636 | (!icase && strncmp(pattern->ptr.str, temp_pattern.data.str.str, temp_pattern.data.str.len) != 0)) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 7637 | return ACL_PAT_FAIL; |
| 7638 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7639 | } |
| 7640 | |
| 7641 | /* 2. Check on Request/Status Version |
| 7642 | * We simply compare strings here. |
| 7643 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7644 | static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7645 | { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7646 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7647 | if (!pattern->ptr.str) |
| 7648 | return 0; |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7649 | pattern->len = strlen(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7650 | return 1; |
| 7651 | } |
| 7652 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7653 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 7654 | acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7655 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7656 | { |
| 7657 | struct http_txn *txn = l7; |
| 7658 | char *ptr; |
| 7659 | int len; |
| 7660 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7661 | if (!txn) |
| 7662 | return 0; |
| 7663 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7664 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7665 | return 0; |
| 7666 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7667 | len = txn->req.sl.rq.v_l; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7668 | ptr = txn->req.sol + txn->req.sl.rq.v; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7669 | |
| 7670 | while ((len-- > 0) && (*ptr++ != '/')); |
| 7671 | if (len <= 0) |
| 7672 | return 0; |
| 7673 | |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 7674 | temp_pattern.data.str.str = ptr; |
| 7675 | temp_pattern.data.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7676 | |
| 7677 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 7678 | return 1; |
| 7679 | } |
| 7680 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7681 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 7682 | acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7683 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7684 | { |
| 7685 | struct http_txn *txn = l7; |
| 7686 | char *ptr; |
| 7687 | int len; |
| 7688 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7689 | if (!txn) |
| 7690 | return 0; |
| 7691 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7692 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7693 | return 0; |
| 7694 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7695 | len = txn->rsp.sl.st.v_l; |
| 7696 | ptr = txn->rsp.sol; |
| 7697 | |
| 7698 | while ((len-- > 0) && (*ptr++ != '/')); |
| 7699 | if (len <= 0) |
| 7700 | return 0; |
| 7701 | |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 7702 | temp_pattern.data.str.str = ptr; |
| 7703 | temp_pattern.data.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7704 | |
| 7705 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 7706 | return 1; |
| 7707 | } |
| 7708 | |
| 7709 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7710 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 7711 | acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7712 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7713 | { |
| 7714 | struct http_txn *txn = l7; |
| 7715 | char *ptr; |
| 7716 | int len; |
| 7717 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7718 | if (!txn) |
| 7719 | return 0; |
| 7720 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7721 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7722 | return 0; |
| 7723 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7724 | len = txn->rsp.sl.st.c_l; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7725 | ptr = txn->rsp.sol + txn->rsp.sl.st.c; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7726 | |
Willy Tarreau | a5e3756 | 2011-12-16 17:06:15 +0100 | [diff] [blame] | 7727 | temp_pattern.data.integer = __strl2ui(ptr, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7728 | test->flags = ACL_TEST_F_VOL_1ST; |
| 7729 | return 1; |
| 7730 | } |
| 7731 | |
| 7732 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7733 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 7734 | acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7735 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7736 | { |
| 7737 | struct http_txn *txn = l7; |
| 7738 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7739 | if (!txn) |
| 7740 | return 0; |
| 7741 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7742 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7743 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7744 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7745 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7746 | /* ensure the indexes are not affected */ |
| 7747 | return 0; |
| 7748 | |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 7749 | temp_pattern.data.str.len = txn->req.sl.rq.u_l; |
| 7750 | temp_pattern.data.str.str = txn->req.sol + txn->req.sl.rq.u; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7751 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 7752 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 7753 | test->flags = ACL_TEST_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7754 | return 1; |
| 7755 | } |
| 7756 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7757 | static int |
| 7758 | acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7759 | struct acl_expr *expr, struct acl_test *test) |
| 7760 | { |
| 7761 | struct http_txn *txn = l7; |
| 7762 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7763 | if (!txn) |
| 7764 | return 0; |
| 7765 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7766 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7767 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7768 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7769 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7770 | /* ensure the indexes are not affected */ |
| 7771 | return 0; |
| 7772 | |
| 7773 | /* Parse HTTP request */ |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 7774 | url2sa(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] | 7775 | if (((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_family != AF_INET) |
| 7776 | return 0; |
| 7777 | temp_pattern.type = PATTERN_TYPE_IP; |
| 7778 | temp_pattern.data.ip = ((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7779 | |
| 7780 | /* |
| 7781 | * If we are parsing url in frontend space, we prepare backend stage |
| 7782 | * to not parse again the same url ! optimization lazyness... |
| 7783 | */ |
| 7784 | if (px->options & PR_O_HTTP_PROXY) |
| 7785 | l4->flags |= SN_ADDR_SET; |
| 7786 | |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 7787 | test->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7788 | return 1; |
| 7789 | } |
| 7790 | |
| 7791 | static int |
| 7792 | acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7793 | struct acl_expr *expr, struct acl_test *test) |
| 7794 | { |
| 7795 | struct http_txn *txn = l7; |
| 7796 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7797 | if (!txn) |
| 7798 | return 0; |
| 7799 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7800 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7801 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7802 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7803 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7804 | /* ensure the indexes are not affected */ |
| 7805 | return 0; |
| 7806 | |
| 7807 | /* Same optimization as url_ip */ |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 7808 | url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->addr.to); |
Willy Tarreau | a5e3756 | 2011-12-16 17:06:15 +0100 | [diff] [blame] | 7809 | temp_pattern.data.integer = ntohs(((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_port); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7810 | |
| 7811 | if (px->options & PR_O_HTTP_PROXY) |
| 7812 | l4->flags |= SN_ADDR_SET; |
| 7813 | |
| 7814 | test->flags = ACL_TEST_F_READ_ONLY; |
| 7815 | return 1; |
| 7816 | } |
| 7817 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7818 | /* 5. Check on HTTP header. A pointer to the beginning of the value is returned. |
| 7819 | * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr(). |
| 7820 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7821 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7822 | acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7823 | struct acl_expr *expr, struct acl_test *test) |
| 7824 | { |
| 7825 | struct http_txn *txn = l7; |
| 7826 | struct hdr_idx *idx = &txn->hdr_idx; |
| 7827 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7828 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7829 | if (!txn) |
| 7830 | return 0; |
| 7831 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7832 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 7833 | /* search for header from the beginning */ |
| 7834 | ctx->idx = 0; |
| 7835 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7836 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 7837 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 7838 | test->flags |= ACL_TEST_F_VOL_HDR; |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 7839 | temp_pattern.data.str.str = (char *)ctx->line + ctx->val; |
| 7840 | temp_pattern.data.str.len = ctx->vlen; |
| 7841 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7842 | return 1; |
| 7843 | } |
| 7844 | |
| 7845 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 7846 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 7847 | return 0; |
| 7848 | } |
| 7849 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7850 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7851 | acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7852 | struct acl_expr *expr, struct acl_test *test) |
| 7853 | { |
| 7854 | struct http_txn *txn = l7; |
| 7855 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7856 | if (!txn) |
| 7857 | return 0; |
| 7858 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7859 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7860 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7861 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7862 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7863 | /* ensure the indexes are not affected */ |
| 7864 | return 0; |
| 7865 | |
| 7866 | return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test); |
| 7867 | } |
| 7868 | |
| 7869 | static int |
| 7870 | acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7871 | struct acl_expr *expr, struct acl_test *test) |
| 7872 | { |
| 7873 | struct http_txn *txn = l7; |
| 7874 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7875 | if (!txn) |
| 7876 | return 0; |
| 7877 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7878 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7879 | return 0; |
| 7880 | |
| 7881 | return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test); |
| 7882 | } |
| 7883 | |
| 7884 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 7885 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 7886 | */ |
| 7887 | static int |
| 7888 | acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7889 | struct acl_expr *expr, struct acl_test *test) |
| 7890 | { |
| 7891 | struct http_txn *txn = l7; |
| 7892 | struct hdr_idx *idx = &txn->hdr_idx; |
| 7893 | struct hdr_ctx ctx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7894 | int cnt; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7895 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7896 | if (!txn) |
| 7897 | return 0; |
| 7898 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7899 | ctx.idx = 0; |
| 7900 | cnt = 0; |
| 7901 | while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx)) |
| 7902 | cnt++; |
| 7903 | |
Willy Tarreau | a5e3756 | 2011-12-16 17:06:15 +0100 | [diff] [blame] | 7904 | temp_pattern.data.integer = cnt; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7905 | test->flags = ACL_TEST_F_VOL_HDR; |
| 7906 | return 1; |
| 7907 | } |
| 7908 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7909 | static int |
| 7910 | acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7911 | struct acl_expr *expr, struct acl_test *test) |
| 7912 | { |
| 7913 | struct http_txn *txn = l7; |
| 7914 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7915 | if (!txn) |
| 7916 | return 0; |
| 7917 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7918 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7919 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7920 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7921 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7922 | /* ensure the indexes are not affected */ |
| 7923 | return 0; |
| 7924 | |
| 7925 | return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test); |
| 7926 | } |
| 7927 | |
| 7928 | static int |
| 7929 | acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7930 | struct acl_expr *expr, struct acl_test *test) |
| 7931 | { |
| 7932 | struct http_txn *txn = l7; |
| 7933 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7934 | if (!txn) |
| 7935 | return 0; |
| 7936 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7937 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7938 | return 0; |
| 7939 | |
| 7940 | return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test); |
| 7941 | } |
| 7942 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7943 | /* 7. Check on HTTP header's integer value. The integer value is returned. |
| 7944 | * FIXME: the type is 'int', it may not be appropriate for everything. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7945 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7946 | */ |
| 7947 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7948 | acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7949 | struct acl_expr *expr, struct acl_test *test) |
| 7950 | { |
| 7951 | struct http_txn *txn = l7; |
| 7952 | struct hdr_idx *idx = &txn->hdr_idx; |
| 7953 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7954 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7955 | if (!txn) |
| 7956 | return 0; |
| 7957 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7958 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 7959 | /* search for header from the beginning */ |
| 7960 | ctx->idx = 0; |
| 7961 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7962 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 7963 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 7964 | test->flags |= ACL_TEST_F_VOL_HDR; |
Willy Tarreau | a5e3756 | 2011-12-16 17:06:15 +0100 | [diff] [blame] | 7965 | temp_pattern.data.integer = strl2ic((char *)ctx->line + ctx->val, ctx->vlen); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7966 | return 1; |
| 7967 | } |
| 7968 | |
| 7969 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 7970 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 7971 | return 0; |
| 7972 | } |
| 7973 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7974 | static int |
| 7975 | acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7976 | struct acl_expr *expr, struct acl_test *test) |
| 7977 | { |
| 7978 | struct http_txn *txn = l7; |
| 7979 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7980 | if (!txn) |
| 7981 | return 0; |
| 7982 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7983 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7984 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7985 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7986 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7987 | /* ensure the indexes are not affected */ |
| 7988 | return 0; |
| 7989 | |
| 7990 | return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test); |
| 7991 | } |
| 7992 | |
| 7993 | static int |
| 7994 | acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7995 | struct acl_expr *expr, struct acl_test *test) |
| 7996 | { |
| 7997 | struct http_txn *txn = l7; |
| 7998 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7999 | if (!txn) |
| 8000 | return 0; |
| 8001 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8002 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8003 | return 0; |
| 8004 | |
| 8005 | return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test); |
| 8006 | } |
| 8007 | |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 8008 | /* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned. |
| 8009 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 8010 | */ |
| 8011 | static int |
| 8012 | acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol, |
| 8013 | struct acl_expr *expr, struct acl_test *test) |
| 8014 | { |
| 8015 | struct http_txn *txn = l7; |
| 8016 | struct hdr_idx *idx = &txn->hdr_idx; |
| 8017 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
| 8018 | |
| 8019 | if (!txn) |
| 8020 | return 0; |
| 8021 | |
| 8022 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 8023 | /* search for header from the beginning */ |
| 8024 | ctx->idx = 0; |
| 8025 | |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 8026 | while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 8027 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 8028 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 8029 | /* Same optimization as url_ip */ |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 8030 | temp_pattern.type = PATTERN_TYPE_IP; |
| 8031 | if (url2ipv4((char *)ctx->line + ctx->val, &temp_pattern.data.ip)) |
| 8032 | return 1; |
| 8033 | /* Dods not look like an IP address, let's fetch next one */ |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 8034 | } |
| 8035 | |
| 8036 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 8037 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 8038 | return 0; |
| 8039 | } |
| 8040 | |
| 8041 | static int |
| 8042 | acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8043 | struct acl_expr *expr, struct acl_test *test) |
| 8044 | { |
| 8045 | struct http_txn *txn = l7; |
| 8046 | |
| 8047 | if (!txn) |
| 8048 | return 0; |
| 8049 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8050 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 8051 | return 0; |
| 8052 | |
| 8053 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 8054 | /* ensure the indexes are not affected */ |
| 8055 | return 0; |
| 8056 | |
| 8057 | return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test); |
| 8058 | } |
| 8059 | |
| 8060 | static int |
| 8061 | acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8062 | struct acl_expr *expr, struct acl_test *test) |
| 8063 | { |
| 8064 | struct http_txn *txn = l7; |
| 8065 | |
| 8066 | if (!txn) |
| 8067 | return 0; |
| 8068 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8069 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 8070 | return 0; |
| 8071 | |
| 8072 | return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test); |
| 8073 | } |
| 8074 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8075 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 8076 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 8077 | */ |
| 8078 | static int |
| 8079 | acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8080 | struct acl_expr *expr, struct acl_test *test) |
| 8081 | { |
| 8082 | struct http_txn *txn = l7; |
| 8083 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8084 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8085 | if (!txn) |
| 8086 | return 0; |
| 8087 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8088 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8089 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8090 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8091 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 8092 | /* ensure the indexes are not affected */ |
| 8093 | return 0; |
| 8094 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 8095 | end = 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] | 8096 | ptr = http_get_path(txn); |
| 8097 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8098 | return 0; |
| 8099 | |
| 8100 | /* OK, we got the '/' ! */ |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 8101 | temp_pattern.data.str.str = ptr; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8102 | |
| 8103 | while (ptr < end && *ptr != '?') |
| 8104 | ptr++; |
| 8105 | |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 8106 | temp_pattern.data.str.len = ptr - temp_pattern.data.str.str; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8107 | |
| 8108 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 8109 | test->flags = ACL_TEST_F_VOL_1ST; |
| 8110 | return 1; |
| 8111 | } |
| 8112 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8113 | static int |
| 8114 | acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir, |
| 8115 | struct acl_expr *expr, struct acl_test *test) |
| 8116 | { |
| 8117 | struct buffer *req = s->req; |
| 8118 | struct http_txn *txn = &s->txn; |
| 8119 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8120 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8121 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 8122 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 8123 | */ |
| 8124 | |
| 8125 | if (!s || !req) |
| 8126 | return 0; |
| 8127 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8128 | if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8129 | /* Already decoded as OK */ |
| 8130 | test->flags |= ACL_TEST_F_SET_RES_PASS; |
| 8131 | return 1; |
| 8132 | } |
| 8133 | |
| 8134 | /* Try to decode HTTP request */ |
| 8135 | if (likely(req->lr < req->r)) |
| 8136 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
| 8137 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8138 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8139 | if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) { |
| 8140 | test->flags |= ACL_TEST_F_SET_RES_FAIL; |
| 8141 | return 1; |
| 8142 | } |
| 8143 | /* wait for final state */ |
| 8144 | test->flags |= ACL_TEST_F_MAY_CHANGE; |
| 8145 | return 0; |
| 8146 | } |
| 8147 | |
| 8148 | /* OK we got a valid HTTP request. We have some minor preparation to |
| 8149 | * perform so that further checks can rely on HTTP tests. |
| 8150 | */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 8151 | txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8152 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 8153 | s->flags |= SN_REDIRECTABLE; |
| 8154 | |
| 8155 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) { |
| 8156 | test->flags |= ACL_TEST_F_SET_RES_FAIL; |
| 8157 | return 1; |
| 8158 | } |
| 8159 | |
| 8160 | test->flags |= ACL_TEST_F_SET_RES_PASS; |
| 8161 | return 1; |
| 8162 | } |
| 8163 | |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 8164 | /* return a valid test if the current request is the first one on the connection */ |
| 8165 | static int |
| 8166 | acl_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, int dir, |
| 8167 | struct acl_expr *expr, struct acl_test *test) |
| 8168 | { |
| 8169 | if (!s) |
| 8170 | return 0; |
| 8171 | |
| 8172 | if (s->txn.flags & TX_NOT_FIRST) |
| 8173 | test->flags |= ACL_TEST_F_SET_RES_FAIL; |
| 8174 | else |
| 8175 | test->flags |= ACL_TEST_F_SET_RES_PASS; |
| 8176 | |
| 8177 | return 1; |
| 8178 | } |
| 8179 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8180 | static int |
| 8181 | acl_fetch_http_auth(struct proxy *px, struct session *s, void *l7, int dir, |
| 8182 | struct acl_expr *expr, struct acl_test *test) |
| 8183 | { |
| 8184 | |
| 8185 | if (!s) |
| 8186 | return 0; |
| 8187 | |
| 8188 | if (!get_http_auth(s)) |
| 8189 | return 0; |
| 8190 | |
| 8191 | test->ctx.a[0] = expr->arg.ul; |
| 8192 | test->ctx.a[1] = s->txn.auth.user; |
| 8193 | test->ctx.a[2] = s->txn.auth.pass; |
| 8194 | |
| 8195 | test->flags |= ACL_TEST_F_READ_ONLY | ACL_TEST_F_NULL_MATCH; |
| 8196 | |
| 8197 | return 1; |
| 8198 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8199 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8200 | /* Try to find the next occurrence of a cookie name in a cookie header value. |
| 8201 | * The lookup begins at <hdr>. The pointer and size of the next occurrence of |
| 8202 | * the cookie value is returned into *value and *value_l, and the function |
| 8203 | * returns a pointer to the next pointer to search from if the value was found. |
| 8204 | * Otherwise if the cookie was not found, NULL is returned and neither value |
| 8205 | * nor value_l are touched. The input <hdr> string should first point to the |
| 8206 | * header's value, and the <hdr_end> pointer must point to the first character |
| 8207 | * not part of the value. <list> must be non-zero if value may represent a list |
| 8208 | * of values (cookie headers). This makes it faster to abort parsing when no |
| 8209 | * list is expected. |
| 8210 | */ |
| 8211 | static char * |
| 8212 | extract_cookie_value(char *hdr, const char *hdr_end, |
| 8213 | char *cookie_name, size_t cookie_name_l, int list, |
Willy Tarreau | 3fb818c | 2012-04-11 17:21:08 +0200 | [diff] [blame] | 8214 | char **value, int *value_l) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8215 | { |
| 8216 | char *equal, *att_end, *att_beg, *val_beg, *val_end; |
| 8217 | char *next; |
| 8218 | |
| 8219 | /* we search at least a cookie name followed by an equal, and more |
| 8220 | * generally something like this : |
| 8221 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 8222 | */ |
| 8223 | for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) { |
| 8224 | /* Iterate through all cookies on this line */ |
| 8225 | |
| 8226 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 8227 | att_beg++; |
| 8228 | |
| 8229 | /* find att_end : this is the first character after the last non |
| 8230 | * space before the equal. It may be equal to hdr_end. |
| 8231 | */ |
| 8232 | equal = att_end = att_beg; |
| 8233 | |
| 8234 | while (equal < hdr_end) { |
| 8235 | if (*equal == '=' || *equal == ';' || (list && *equal == ',')) |
| 8236 | break; |
| 8237 | if (http_is_spht[(unsigned char)*equal++]) |
| 8238 | continue; |
| 8239 | att_end = equal; |
| 8240 | } |
| 8241 | |
| 8242 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 8243 | * is between <att_beg> and <equal>, both may be identical. |
| 8244 | */ |
| 8245 | |
| 8246 | /* look for end of cookie if there is an equal sign */ |
| 8247 | if (equal < hdr_end && *equal == '=') { |
| 8248 | /* look for the beginning of the value */ |
| 8249 | val_beg = equal + 1; |
| 8250 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 8251 | val_beg++; |
| 8252 | |
| 8253 | /* find the end of the value, respecting quotes */ |
| 8254 | next = find_cookie_value_end(val_beg, hdr_end); |
| 8255 | |
| 8256 | /* make val_end point to the first white space or delimitor after the value */ |
| 8257 | val_end = next; |
| 8258 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 8259 | val_end--; |
| 8260 | } else { |
| 8261 | val_beg = val_end = next = equal; |
| 8262 | } |
| 8263 | |
| 8264 | /* We have nothing to do with attributes beginning with '$'. However, |
| 8265 | * they will automatically be removed if a header before them is removed, |
| 8266 | * since they're supposed to be linked together. |
| 8267 | */ |
| 8268 | if (*att_beg == '$') |
| 8269 | continue; |
| 8270 | |
| 8271 | /* Ignore cookies with no equal sign */ |
| 8272 | if (equal == next) |
| 8273 | continue; |
| 8274 | |
| 8275 | /* Now we have the cookie name between att_beg and att_end, and |
| 8276 | * its value between val_beg and val_end. |
| 8277 | */ |
| 8278 | |
| 8279 | if (att_end - att_beg == cookie_name_l && |
| 8280 | memcmp(att_beg, cookie_name, cookie_name_l) == 0) { |
| 8281 | /* let's return this value and indicate where to go on from */ |
| 8282 | *value = val_beg; |
| 8283 | *value_l = val_end - val_beg; |
| 8284 | return next + 1; |
| 8285 | } |
| 8286 | |
| 8287 | /* Set-Cookie headers only have the name in the first attr=value part */ |
| 8288 | if (!list) |
| 8289 | break; |
| 8290 | } |
| 8291 | |
| 8292 | return NULL; |
| 8293 | } |
| 8294 | |
| 8295 | /* Iterate over all cookies present in a request. The context is stored in |
| 8296 | * test->ctx.a[0] for the in-header position, test->ctx.a[1] for the |
| 8297 | * end-of-header-value, and test->ctx.a[2] for the hdr_idx. If <multi> is |
| 8298 | * non-null, then multiple cookies may be parsed on the same line. |
| 8299 | * The cookie name is in expr->arg and the name length in expr->arg_len. |
| 8300 | */ |
| 8301 | static int |
| 8302 | acl_fetch_any_cookie_value(struct proxy *px, struct session *l4, void *l7, char *sol, |
| 8303 | const char *hdr_name, int hdr_name_len, int multi, |
| 8304 | struct acl_expr *expr, struct acl_test *test) |
| 8305 | { |
| 8306 | struct http_txn *txn = l7; |
| 8307 | struct hdr_idx *idx = &txn->hdr_idx; |
| 8308 | struct hdr_ctx *ctx = (struct hdr_ctx *)&test->ctx.a[2]; |
| 8309 | |
| 8310 | if (!txn) |
| 8311 | return 0; |
| 8312 | |
| 8313 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) { |
| 8314 | /* search for the header from the beginning, we must first initialize |
| 8315 | * the search parameters. |
| 8316 | */ |
| 8317 | test->ctx.a[0] = NULL; |
| 8318 | ctx->idx = 0; |
| 8319 | } |
| 8320 | |
| 8321 | while (1) { |
| 8322 | /* Note: test->ctx.a[0] == NULL every time we need to fetch a new header */ |
| 8323 | if (!test->ctx.a[0]) { |
| 8324 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx)) |
| 8325 | goto out; |
| 8326 | |
| 8327 | if (ctx->vlen < expr->arg_len + 1) |
| 8328 | continue; |
| 8329 | |
| 8330 | test->ctx.a[0] = ctx->line + ctx->val; |
| 8331 | test->ctx.a[1] = test->ctx.a[0] + ctx->vlen; |
| 8332 | } |
| 8333 | |
| 8334 | test->ctx.a[0] = extract_cookie_value(test->ctx.a[0], test->ctx.a[1], |
| 8335 | expr->arg.str, expr->arg_len, multi, |
| 8336 | &temp_pattern.data.str.str, |
| 8337 | &temp_pattern.data.str.len); |
| 8338 | if (test->ctx.a[0]) { |
| 8339 | /* one value was returned into temp_pattern.data.str.{str,len} */ |
| 8340 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 8341 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 8342 | return 1; |
| 8343 | } |
| 8344 | } |
| 8345 | |
| 8346 | out: |
| 8347 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 8348 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 8349 | return 0; |
| 8350 | } |
| 8351 | |
| 8352 | static int |
| 8353 | acl_fetch_cookie_value(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8354 | struct acl_expr *expr, struct acl_test *test) |
| 8355 | { |
| 8356 | struct http_txn *txn = l7; |
| 8357 | |
| 8358 | if (!txn) |
| 8359 | return 0; |
| 8360 | |
| 8361 | if (txn->req.msg_state < HTTP_MSG_BODY) |
| 8362 | return 0; |
| 8363 | |
| 8364 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 8365 | /* ensure the indexes are not affected */ |
| 8366 | return 0; |
| 8367 | |
| 8368 | /* The Cookie header allows multiple cookies on the same line */ |
| 8369 | return acl_fetch_any_cookie_value(px, l4, txn, txn->req.sol, "Cookie", 6, 1, expr, test); |
| 8370 | } |
| 8371 | |
| 8372 | static int |
| 8373 | acl_fetch_scookie_value(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8374 | struct acl_expr *expr, struct acl_test *test) |
| 8375 | { |
| 8376 | struct http_txn *txn = l7; |
| 8377 | |
| 8378 | if (!txn) |
| 8379 | return 0; |
| 8380 | |
| 8381 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 8382 | return 0; |
| 8383 | |
| 8384 | /* The Set-Cookie header allows only one cookie on the same line */ |
| 8385 | return acl_fetch_any_cookie_value(px, l4, txn, txn->rsp.sol, "Set-Cookie", 10, 0, expr, test); |
| 8386 | } |
| 8387 | |
| 8388 | /* Iterate over all cookies present in a request to count how many occurrences |
| 8389 | * match the name in expr->arg and expr->arg_len. If <multi> is non-null, then |
| 8390 | * multiple cookies may be parsed on the same line. |
| 8391 | */ |
| 8392 | static int |
| 8393 | acl_fetch_any_cookie_cnt(struct proxy *px, struct session *l4, void *l7, char *sol, |
| 8394 | const char *hdr_name, int hdr_name_len, int multi, |
| 8395 | struct acl_expr *expr, struct acl_test *test) |
| 8396 | { |
| 8397 | struct http_txn *txn = l7; |
| 8398 | struct hdr_idx *idx = &txn->hdr_idx; |
| 8399 | struct hdr_ctx ctx; |
| 8400 | int cnt; |
| 8401 | char *val_beg, *val_end; |
| 8402 | |
| 8403 | if (!txn) |
| 8404 | return 0; |
| 8405 | |
Willy Tarreau | 46787ed | 2012-04-11 17:28:40 +0200 | [diff] [blame] | 8406 | val_end = val_beg = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8407 | ctx.idx = 0; |
| 8408 | cnt = 0; |
| 8409 | |
| 8410 | while (1) { |
| 8411 | /* Note: val_beg == NULL every time we need to fetch a new header */ |
| 8412 | if (!val_beg) { |
| 8413 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx)) |
| 8414 | break; |
| 8415 | |
| 8416 | if (ctx.vlen < expr->arg_len + 1) |
| 8417 | continue; |
| 8418 | |
| 8419 | val_beg = ctx.line + ctx.val; |
| 8420 | val_end = val_beg + ctx.vlen; |
| 8421 | } |
| 8422 | |
| 8423 | while ((val_beg = extract_cookie_value(val_beg, val_end, |
| 8424 | expr->arg.str, expr->arg_len, multi, |
| 8425 | &temp_pattern.data.str.str, |
| 8426 | &temp_pattern.data.str.len))) { |
| 8427 | cnt++; |
| 8428 | } |
| 8429 | } |
| 8430 | |
| 8431 | temp_pattern.data.integer = cnt; |
| 8432 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 8433 | return 1; |
| 8434 | } |
| 8435 | |
| 8436 | static int |
| 8437 | acl_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8438 | struct acl_expr *expr, struct acl_test *test) |
| 8439 | { |
| 8440 | struct http_txn *txn = l7; |
| 8441 | |
| 8442 | if (!txn) |
| 8443 | return 0; |
| 8444 | |
| 8445 | if (txn->req.msg_state < HTTP_MSG_BODY) |
| 8446 | return 0; |
| 8447 | |
| 8448 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 8449 | /* ensure the indexes are not affected */ |
| 8450 | return 0; |
| 8451 | |
| 8452 | /* The Cookie header allows multiple cookies on the same line */ |
| 8453 | return acl_fetch_any_cookie_cnt(px, l4, txn, txn->req.sol, "Cookie", 6, 1, expr, test); |
| 8454 | } |
| 8455 | |
| 8456 | static int |
| 8457 | acl_fetch_scookie_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8458 | struct acl_expr *expr, struct acl_test *test) |
| 8459 | { |
| 8460 | struct http_txn *txn = l7; |
| 8461 | |
| 8462 | if (!txn) |
| 8463 | return 0; |
| 8464 | |
| 8465 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 8466 | return 0; |
| 8467 | |
| 8468 | /* The Set-Cookie header allows only one cookie on the same line */ |
| 8469 | return acl_fetch_any_cookie_cnt(px, l4, txn, txn->rsp.sol, "Set-Cookie", 10, 0, expr, test); |
| 8470 | } |
| 8471 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8472 | /************************************************************************/ |
| 8473 | /* All supported keywords must be declared here. */ |
| 8474 | /************************************************************************/ |
| 8475 | |
| 8476 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 8477 | static struct acl_kw_list acl_kws = {{ },{ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8478 | { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT }, |
| 8479 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 8480 | { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT }, |
Willy Tarreau | c426296 | 2010-05-10 23:42:40 +0200 | [diff] [blame] | 8481 | { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP }, |
| 8482 | { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP }, |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 8483 | { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT }, |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8484 | |
Willy Tarreau | c426296 | 2010-05-10 23:42:40 +0200 | [diff] [blame] | 8485 | { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP }, |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 8486 | { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 8487 | { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 8488 | { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 8489 | { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 8490 | { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 8491 | { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
Willy Tarreau | 0e69854 | 2011-09-16 08:32:32 +0200 | [diff] [blame] | 8492 | { "url_len", acl_parse_int, acl_fetch_url, acl_match_len, ACL_USE_L7REQ_VOLATILE }, |
Willy Tarreau | b337b53 | 2010-05-13 20:03:41 +0200 | [diff] [blame] | 8493 | { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP }, |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 8494 | { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE }, |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8495 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 8496 | /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */ |
Willy Tarreau | c426296 | 2010-05-10 23:42:40 +0200 | [diff] [blame] | 8497 | { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP }, |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 8498 | { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 8499 | { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 8500 | { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 8501 | { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 8502 | { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 8503 | { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 8504 | { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE }, |
Willy Tarreau | 0e69854 | 2011-09-16 08:32:32 +0200 | [diff] [blame] | 8505 | { "hdr_len", acl_parse_int, acl_fetch_chdr, acl_match_len, ACL_USE_L7REQ_VOLATILE }, |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 8506 | { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE }, |
Willy Tarreau | b337b53 | 2010-05-13 20:03:41 +0200 | [diff] [blame] | 8507 | { "hdr_ip", acl_parse_ip, acl_fetch_chdr_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP }, |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8508 | |
Willy Tarreau | c426296 | 2010-05-10 23:42:40 +0200 | [diff] [blame] | 8509 | { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP }, |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 8510 | { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE }, |
| 8511 | { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE }, |
| 8512 | { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE }, |
| 8513 | { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE }, |
| 8514 | { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE }, |
| 8515 | { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE }, |
| 8516 | { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE }, |
Willy Tarreau | 0e69854 | 2011-09-16 08:32:32 +0200 | [diff] [blame] | 8517 | { "shdr_len", acl_parse_int, acl_fetch_shdr, acl_match_len, ACL_USE_L7RTR_VOLATILE }, |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 8518 | { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE }, |
Willy Tarreau | b337b53 | 2010-05-13 20:03:41 +0200 | [diff] [blame] | 8519 | { "shdr_ip", acl_parse_ip, acl_fetch_shdr_ip, acl_match_ip, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP }, |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8520 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8521 | { "cook", acl_parse_str, acl_fetch_cookie_value, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP }, |
| 8522 | { "cook_reg", acl_parse_reg, acl_fetch_cookie_value, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 8523 | { "cook_beg", acl_parse_str, acl_fetch_cookie_value, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 8524 | { "cook_end", acl_parse_str, acl_fetch_cookie_value, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 8525 | { "cook_sub", acl_parse_str, acl_fetch_cookie_value, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 8526 | { "cook_dir", acl_parse_str, acl_fetch_cookie_value, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 8527 | { "cook_dom", acl_parse_str, acl_fetch_cookie_value, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 8528 | { "cook_len", acl_parse_int, acl_fetch_cookie_value, acl_match_len, ACL_USE_L7REQ_VOLATILE }, |
| 8529 | { "cook_cnt", acl_parse_int, acl_fetch_cookie_cnt, acl_match_int, ACL_USE_L7REQ_VOLATILE }, |
| 8530 | |
| 8531 | { "scook", acl_parse_str, acl_fetch_scookie_value, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP }, |
| 8532 | { "scook_reg", acl_parse_reg, acl_fetch_scookie_value, acl_match_reg, ACL_USE_L7RTR_VOLATILE }, |
| 8533 | { "scook_beg", acl_parse_str, acl_fetch_scookie_value, acl_match_beg, ACL_USE_L7RTR_VOLATILE }, |
| 8534 | { "scook_end", acl_parse_str, acl_fetch_scookie_value, acl_match_end, ACL_USE_L7RTR_VOLATILE }, |
| 8535 | { "scook_sub", acl_parse_str, acl_fetch_scookie_value, acl_match_sub, ACL_USE_L7RTR_VOLATILE }, |
| 8536 | { "scook_dir", acl_parse_str, acl_fetch_scookie_value, acl_match_dir, ACL_USE_L7RTR_VOLATILE }, |
| 8537 | { "scook_dom", acl_parse_str, acl_fetch_scookie_value, acl_match_dom, ACL_USE_L7RTR_VOLATILE }, |
| 8538 | { "scook_len", acl_parse_int, acl_fetch_scookie_value, acl_match_len, ACL_USE_L7RTR_VOLATILE }, |
| 8539 | { "scook_cnt", acl_parse_int, acl_fetch_scookie_cnt, acl_match_int, ACL_USE_L7RTR_VOLATILE }, |
| 8540 | |
Willy Tarreau | c426296 | 2010-05-10 23:42:40 +0200 | [diff] [blame] | 8541 | { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP }, |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 8542 | { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 8543 | { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 8544 | { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 8545 | { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 8546 | { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 8547 | { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
Willy Tarreau | 0e69854 | 2011-09-16 08:32:32 +0200 | [diff] [blame] | 8548 | { "path_len", acl_parse_int, acl_fetch_path, acl_match_len, ACL_USE_L7REQ_VOLATILE }, |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8549 | |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 8550 | { "http_auth", acl_parse_nothing, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT }, |
| 8551 | { "http_auth_group", acl_parse_strcat, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT }, |
| 8552 | { "http_first_req", acl_parse_nothing, acl_fetch_http_first_req, acl_match_nothing, ACL_USE_L7REQ_PERMANENT }, |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8553 | { NULL, NULL, NULL, NULL }, |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8554 | }}; |
| 8555 | |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8556 | /************************************************************************/ |
| 8557 | /* The code below is dedicated to pattern fetching and matching */ |
| 8558 | /************************************************************************/ |
| 8559 | |
Willy Tarreau | e428fb7 | 2011-12-16 21:50:30 +0100 | [diff] [blame] | 8560 | /* Returns the last occurrence of specified header. */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8561 | static int |
Willy Tarreau | e428fb7 | 2011-12-16 21:50:30 +0100 | [diff] [blame] | 8562 | pattern_fetch_hdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8563 | const struct pattern_arg *arg_p, int arg_i, union pattern_data *data) |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8564 | { |
| 8565 | struct http_txn *txn = l7; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8566 | |
Willy Tarreau | e428fb7 | 2011-12-16 21:50:30 +0100 | [diff] [blame] | 8567 | return http_get_hdr(&txn->req, arg_p->data.str.str, arg_p->data.str.len, &txn->hdr_idx, |
| 8568 | -1, NULL, &data->str.str, &data->str.len); |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8569 | } |
| 8570 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8571 | /* |
| 8572 | * Given a path string and its length, find the position of beginning of the |
| 8573 | * query string. Returns NULL if no query string is found in the path. |
| 8574 | * |
| 8575 | * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22: |
| 8576 | * |
| 8577 | * find_query_string(path, n) points to "yo=mama;ye=daddy" string. |
| 8578 | */ |
| 8579 | static inline char *find_query_string(char *path, size_t path_l) |
| 8580 | { |
| 8581 | char *p; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 8582 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8583 | p = memchr(path, '?', path_l); |
| 8584 | return p ? p + 1 : NULL; |
| 8585 | } |
| 8586 | |
| 8587 | static inline int is_param_delimiter(char c) |
| 8588 | { |
| 8589 | return c == '&' || c == ';'; |
| 8590 | } |
| 8591 | |
| 8592 | /* |
| 8593 | * Given a url parameter, find the starting position of the first occurence, |
| 8594 | * or NULL if the parameter is not found. |
| 8595 | * |
| 8596 | * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye", |
| 8597 | * the function will return query_string+8. |
| 8598 | */ |
| 8599 | static char* |
| 8600 | find_url_param_pos(char* query_string, size_t query_string_l, |
| 8601 | char* url_param_name, size_t url_param_name_l) |
| 8602 | { |
| 8603 | char *pos, *last; |
| 8604 | |
| 8605 | pos = query_string; |
| 8606 | last = query_string + query_string_l - url_param_name_l - 1; |
| 8607 | |
| 8608 | while (pos <= last) { |
| 8609 | if (pos[url_param_name_l] == '=') { |
| 8610 | if (memcmp(pos, url_param_name, url_param_name_l) == 0) |
| 8611 | return pos; |
| 8612 | pos += url_param_name_l + 1; |
| 8613 | } |
| 8614 | while (pos <= last && !is_param_delimiter(*pos)) |
| 8615 | pos++; |
| 8616 | pos++; |
| 8617 | } |
| 8618 | return NULL; |
| 8619 | } |
| 8620 | |
| 8621 | /* |
| 8622 | * Given a url parameter name, returns its value and size into *value and |
| 8623 | * *value_l respectively, and returns non-zero. If the parameter is not found, |
| 8624 | * zero is returned and value/value_l are not touched. |
| 8625 | */ |
| 8626 | static int |
| 8627 | find_url_param_value(char* path, size_t path_l, |
| 8628 | char* url_param_name, size_t url_param_name_l, |
| 8629 | char** value, size_t* value_l) |
| 8630 | { |
| 8631 | char *query_string, *qs_end; |
| 8632 | char *arg_start; |
| 8633 | char *value_start, *value_end; |
| 8634 | |
| 8635 | query_string = find_query_string(path, path_l); |
| 8636 | if (!query_string) |
| 8637 | return 0; |
| 8638 | |
| 8639 | qs_end = path + path_l; |
| 8640 | arg_start = find_url_param_pos(query_string, qs_end - query_string, |
| 8641 | url_param_name, url_param_name_l); |
| 8642 | if (!arg_start) |
| 8643 | return 0; |
| 8644 | |
| 8645 | value_start = arg_start + url_param_name_l + 1; |
| 8646 | value_end = value_start; |
| 8647 | |
| 8648 | while ((value_end < qs_end) && !is_param_delimiter(*value_end)) |
| 8649 | value_end++; |
| 8650 | |
| 8651 | *value = value_start; |
| 8652 | *value_l = value_end - value_start; |
Willy Tarreau | 0013433 | 2011-01-04 14:57:34 +0100 | [diff] [blame] | 8653 | return value_end != value_start; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8654 | } |
| 8655 | |
| 8656 | static int |
| 8657 | pattern_fetch_url_param(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8658 | const struct pattern_arg *arg_p, int arg_i, union pattern_data *data) |
| 8659 | { |
| 8660 | struct http_txn *txn = l7; |
| 8661 | struct http_msg *msg = &txn->req; |
| 8662 | char *url_param_value; |
| 8663 | size_t url_param_value_l; |
| 8664 | |
| 8665 | if (!find_url_param_value(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l, |
| 8666 | arg_p->data.str.str, arg_p->data.str.len, |
| 8667 | &url_param_value, &url_param_value_l)) |
| 8668 | return 0; |
| 8669 | |
| 8670 | data->str.str = url_param_value; |
| 8671 | data->str.len = url_param_value_l; |
| 8672 | return 1; |
| 8673 | } |
| 8674 | |
Willy Tarreau | b3eb221 | 2011-07-01 16:16:17 +0200 | [diff] [blame] | 8675 | /* Try to find in request or response message is in <msg> and whose transaction |
| 8676 | * is in <txn> the last occurrence of a cookie name in all cookie header values |
| 8677 | * whose header name is <hdr_name> with name of length <hdr_name_len>. The |
| 8678 | * pointer and size of the last occurrence of the cookie value is returned into |
| 8679 | * <value> and <value_l>, and the function returns non-zero if the value was |
| 8680 | * found. Otherwise if the cookie was not found, zero is returned and neither |
| 8681 | * value nor value_l are touched. The input hdr string should begin at the |
| 8682 | * header's value, and its size should be in hdr_l. <list> must be non-zero if |
| 8683 | * value may represent a list of values (cookie headers). |
| 8684 | */ |
| 8685 | |
| 8686 | static int |
| 8687 | find_cookie_value(struct http_msg *msg, struct http_txn *txn, |
| 8688 | const char *hdr_name, int hdr_name_len, |
| 8689 | char *cookie_name, size_t cookie_name_l, int list, |
Willy Tarreau | 3fb818c | 2012-04-11 17:21:08 +0200 | [diff] [blame] | 8690 | char **value, int *value_l) |
Willy Tarreau | b3eb221 | 2011-07-01 16:16:17 +0200 | [diff] [blame] | 8691 | { |
| 8692 | struct hdr_ctx ctx; |
| 8693 | int found = 0; |
| 8694 | |
| 8695 | ctx.idx = 0; |
| 8696 | while (http_find_header2(hdr_name, hdr_name_len, msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 4573af9 | 2012-04-06 18:20:06 +0200 | [diff] [blame] | 8697 | char *hdr, *end; |
| 8698 | |
Willy Tarreau | b3eb221 | 2011-07-01 16:16:17 +0200 | [diff] [blame] | 8699 | if (ctx.vlen < cookie_name_l + 1) |
| 8700 | continue; |
| 8701 | |
Willy Tarreau | 4573af9 | 2012-04-06 18:20:06 +0200 | [diff] [blame] | 8702 | hdr = ctx.line + ctx.val; |
| 8703 | end = hdr + ctx.vlen; |
| 8704 | while ((hdr = extract_cookie_value(hdr, end, cookie_name, cookie_name_l, 1, value, value_l))) |
| 8705 | found = 1; |
Willy Tarreau | b3eb221 | 2011-07-01 16:16:17 +0200 | [diff] [blame] | 8706 | } |
| 8707 | return found; |
| 8708 | } |
| 8709 | |
| 8710 | static int |
| 8711 | pattern_fetch_cookie(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8712 | const struct pattern_arg *arg_p, int arg_i, union pattern_data *data) |
| 8713 | { |
| 8714 | struct http_txn *txn = l7; |
| 8715 | struct http_msg *msg = &txn->req; |
| 8716 | char *cookie_value; |
Willy Tarreau | 3fb818c | 2012-04-11 17:21:08 +0200 | [diff] [blame] | 8717 | int cookie_value_l; |
Willy Tarreau | b3eb221 | 2011-07-01 16:16:17 +0200 | [diff] [blame] | 8718 | int found = 0; |
| 8719 | |
| 8720 | found = find_cookie_value(msg, txn, "Cookie", 6, |
| 8721 | arg_p->data.str.str, arg_p->data.str.len, 1, |
| 8722 | &cookie_value, &cookie_value_l); |
| 8723 | if (found) { |
| 8724 | data->str.str = cookie_value; |
| 8725 | data->str.len = cookie_value_l; |
| 8726 | } |
| 8727 | |
| 8728 | return found; |
| 8729 | } |
| 8730 | |
| 8731 | |
| 8732 | static int |
| 8733 | pattern_fetch_set_cookie(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8734 | const struct pattern_arg *arg_p, int arg_i, union pattern_data *data) |
| 8735 | { |
| 8736 | struct http_txn *txn = l7; |
| 8737 | struct http_msg *msg = &txn->rsp; |
| 8738 | char *cookie_value; |
Willy Tarreau | 3fb818c | 2012-04-11 17:21:08 +0200 | [diff] [blame] | 8739 | int cookie_value_l; |
Willy Tarreau | b3eb221 | 2011-07-01 16:16:17 +0200 | [diff] [blame] | 8740 | int found = 0; |
| 8741 | |
| 8742 | found = find_cookie_value(msg, txn, "Set-Cookie", 10, |
| 8743 | arg_p->data.str.str, arg_p->data.str.len, 1, |
| 8744 | &cookie_value, &cookie_value_l); |
| 8745 | if (found) { |
| 8746 | data->str.str = cookie_value; |
| 8747 | data->str.len = cookie_value_l; |
| 8748 | } |
| 8749 | |
| 8750 | return found; |
| 8751 | } |
| 8752 | |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 8753 | |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8754 | /************************************************************************/ |
| 8755 | /* All supported keywords must be declared here. */ |
| 8756 | /************************************************************************/ |
| 8757 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 8758 | static struct pattern_fetch_kw_list pattern_fetch_keywords = {{ },{ |
Willy Tarreau | e428fb7 | 2011-12-16 21:50:30 +0100 | [diff] [blame] | 8759 | { "hdr", pattern_fetch_hdr, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ }, |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8760 | { "url_param", pattern_fetch_url_param, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ }, |
Willy Tarreau | b3eb221 | 2011-07-01 16:16:17 +0200 | [diff] [blame] | 8761 | { "cookie", pattern_fetch_cookie, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ }, |
| 8762 | { "set-cookie", pattern_fetch_set_cookie, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_RTR }, |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 8763 | { NULL, NULL, NULL, 0, 0 }, |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8764 | }}; |
| 8765 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8766 | |
| 8767 | __attribute__((constructor)) |
| 8768 | static void __http_protocol_init(void) |
| 8769 | { |
| 8770 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8771 | pattern_register_fetches(&pattern_fetch_keywords); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8772 | } |
| 8773 | |
| 8774 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8775 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 8776 | * Local variables: |
| 8777 | * c-indent-level: 8 |
| 8778 | * c-basic-offset: 8 |
| 8779 | * End: |
| 8780 | */ |