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 | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 26 | #include <common/appsession.h> |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 27 | #include <common/base64.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 28 | #include <common/compat.h> |
| 29 | #include <common/config.h> |
Willy Tarreau | a4cd1f5 | 2006-12-16 19:57:26 +0100 | [diff] [blame] | 30 | #include <common/debug.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 31 | #include <common/memory.h> |
| 32 | #include <common/mini-clist.h> |
| 33 | #include <common/standard.h> |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 34 | #include <common/ticks.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 35 | #include <common/time.h> |
| 36 | #include <common/uri_auth.h> |
| 37 | #include <common/version.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 38 | |
| 39 | #include <types/capture.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 40 | #include <types/global.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 41 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 42 | #include <proto/acl.h> |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 43 | #include <proto/auth.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 44 | #include <proto/backend.h> |
| 45 | #include <proto/buffers.h> |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 46 | #include <proto/checks.h> |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 47 | #include <proto/dumpstats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 48 | #include <proto/fd.h> |
Willy Tarreau | 03fa5df | 2010-05-24 21:02:37 +0200 | [diff] [blame] | 49 | #include <proto/frontend.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 50 | #include <proto/log.h> |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 51 | #include <proto/hdr_idx.h> |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 52 | #include <proto/pattern.h> |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 53 | #include <proto/proto_tcp.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 54 | #include <proto/proto_http.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 55 | #include <proto/proxy.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 56 | #include <proto/queue.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 57 | #include <proto/server.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 58 | #include <proto/session.h> |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 59 | #include <proto/stream_interface.h> |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 60 | #include <proto/stream_sock.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 61 | #include <proto/task.h> |
| 62 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 63 | const char HTTP_100[] = |
| 64 | "HTTP/1.1 100 Continue\r\n\r\n"; |
| 65 | |
| 66 | const struct chunk http_100_chunk = { |
| 67 | .str = (char *)&HTTP_100, |
| 68 | .len = sizeof(HTTP_100)-1 |
| 69 | }; |
| 70 | |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 71 | /* Warning: no "connection" header is provided with the 3xx messages below */ |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 72 | const char *HTTP_301 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 73 | "HTTP/1.1 301 Moved Permanently\r\n" |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 74 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 75 | "Content-length: 0\r\n" |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 76 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 77 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 78 | const char *HTTP_302 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 79 | "HTTP/1.1 302 Found\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 80 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 81 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 82 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 83 | |
| 84 | /* same as 302 except that the browser MUST retry with the GET method */ |
| 85 | const char *HTTP_303 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 86 | "HTTP/1.1 303 See Other\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 87 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 88 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 89 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 90 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 91 | /* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */ |
| 92 | const char *HTTP_401_fmt = |
| 93 | "HTTP/1.0 401 Unauthorized\r\n" |
| 94 | "Cache-Control: no-cache\r\n" |
| 95 | "Connection: close\r\n" |
Willy Tarreau | 791d66d | 2006-07-08 16:53:38 +0200 | [diff] [blame] | 96 | "Content-Type: text/html\r\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 97 | "WWW-Authenticate: Basic realm=\"%s\"\r\n" |
| 98 | "\r\n" |
| 99 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 100 | |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 101 | const char *HTTP_407_fmt = |
| 102 | "HTTP/1.0 407 Unauthorized\r\n" |
| 103 | "Cache-Control: no-cache\r\n" |
| 104 | "Connection: close\r\n" |
| 105 | "Content-Type: text/html\r\n" |
| 106 | "Proxy-Authenticate: Basic realm=\"%s\"\r\n" |
| 107 | "\r\n" |
| 108 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 109 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 110 | |
| 111 | const int http_err_codes[HTTP_ERR_SIZE] = { |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 112 | [HTTP_ERR_200] = 200, /* used by "monitor-uri" */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 113 | [HTTP_ERR_400] = 400, |
| 114 | [HTTP_ERR_403] = 403, |
| 115 | [HTTP_ERR_408] = 408, |
| 116 | [HTTP_ERR_500] = 500, |
| 117 | [HTTP_ERR_502] = 502, |
| 118 | [HTTP_ERR_503] = 503, |
| 119 | [HTTP_ERR_504] = 504, |
| 120 | }; |
| 121 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 122 | static const char *http_err_msgs[HTTP_ERR_SIZE] = { |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 123 | [HTTP_ERR_200] = |
| 124 | "HTTP/1.0 200 OK\r\n" |
| 125 | "Cache-Control: no-cache\r\n" |
| 126 | "Connection: close\r\n" |
| 127 | "Content-Type: text/html\r\n" |
| 128 | "\r\n" |
| 129 | "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n", |
| 130 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 131 | [HTTP_ERR_400] = |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 132 | "HTTP/1.0 400 Bad request\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 133 | "Cache-Control: no-cache\r\n" |
| 134 | "Connection: close\r\n" |
| 135 | "Content-Type: text/html\r\n" |
| 136 | "\r\n" |
| 137 | "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n", |
| 138 | |
| 139 | [HTTP_ERR_403] = |
| 140 | "HTTP/1.0 403 Forbidden\r\n" |
| 141 | "Cache-Control: no-cache\r\n" |
| 142 | "Connection: close\r\n" |
| 143 | "Content-Type: text/html\r\n" |
| 144 | "\r\n" |
| 145 | "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n", |
| 146 | |
| 147 | [HTTP_ERR_408] = |
| 148 | "HTTP/1.0 408 Request Time-out\r\n" |
| 149 | "Cache-Control: no-cache\r\n" |
| 150 | "Connection: close\r\n" |
| 151 | "Content-Type: text/html\r\n" |
| 152 | "\r\n" |
| 153 | "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n", |
| 154 | |
| 155 | [HTTP_ERR_500] = |
| 156 | "HTTP/1.0 500 Server Error\r\n" |
| 157 | "Cache-Control: no-cache\r\n" |
| 158 | "Connection: close\r\n" |
| 159 | "Content-Type: text/html\r\n" |
| 160 | "\r\n" |
| 161 | "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n", |
| 162 | |
| 163 | [HTTP_ERR_502] = |
| 164 | "HTTP/1.0 502 Bad Gateway\r\n" |
| 165 | "Cache-Control: no-cache\r\n" |
| 166 | "Connection: close\r\n" |
| 167 | "Content-Type: text/html\r\n" |
| 168 | "\r\n" |
| 169 | "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n", |
| 170 | |
| 171 | [HTTP_ERR_503] = |
| 172 | "HTTP/1.0 503 Service Unavailable\r\n" |
| 173 | "Cache-Control: no-cache\r\n" |
| 174 | "Connection: close\r\n" |
| 175 | "Content-Type: text/html\r\n" |
| 176 | "\r\n" |
| 177 | "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n", |
| 178 | |
| 179 | [HTTP_ERR_504] = |
| 180 | "HTTP/1.0 504 Gateway Time-out\r\n" |
| 181 | "Cache-Control: no-cache\r\n" |
| 182 | "Connection: close\r\n" |
| 183 | "Content-Type: text/html\r\n" |
| 184 | "\r\n" |
| 185 | "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n", |
| 186 | |
| 187 | }; |
| 188 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 189 | /* We must put the messages here since GCC cannot initialize consts depending |
| 190 | * on strlen(). |
| 191 | */ |
| 192 | struct chunk http_err_chunks[HTTP_ERR_SIZE]; |
| 193 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 194 | #define FD_SETS_ARE_BITFIELDS |
| 195 | #ifdef FD_SETS_ARE_BITFIELDS |
| 196 | /* |
| 197 | * This map is used with all the FD_* macros to check whether a particular bit |
| 198 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 199 | * which should be encoded. When FD_ISSET() returns non-zero, it means that the |
| 200 | * byte should be encoded. Be careful to always pass bytes from 0 to 255 |
| 201 | * exclusively to the macros. |
| 202 | */ |
| 203 | fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 204 | fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 205 | |
| 206 | #else |
| 207 | #error "Check if your OS uses bitfields for fd_sets" |
| 208 | #endif |
| 209 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 210 | void init_proto_http() |
| 211 | { |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 212 | int i; |
| 213 | char *tmp; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 214 | int msg; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 215 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 216 | for (msg = 0; msg < HTTP_ERR_SIZE; msg++) { |
| 217 | if (!http_err_msgs[msg]) { |
| 218 | Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg); |
| 219 | abort(); |
| 220 | } |
| 221 | |
| 222 | http_err_chunks[msg].str = (char *)http_err_msgs[msg]; |
| 223 | http_err_chunks[msg].len = strlen(http_err_msgs[msg]); |
| 224 | } |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 225 | |
| 226 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 227 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 228 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 229 | * printable characters above. |
| 230 | */ |
| 231 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 232 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
| 233 | for (i = 0; i < 32; i++) { |
| 234 | FD_SET(i, hdr_encode_map); |
| 235 | FD_SET(i, url_encode_map); |
| 236 | } |
| 237 | for (i = 127; i < 256; i++) { |
| 238 | FD_SET(i, hdr_encode_map); |
| 239 | FD_SET(i, url_encode_map); |
| 240 | } |
| 241 | |
| 242 | tmp = "\"#{|}"; |
| 243 | while (*tmp) { |
| 244 | FD_SET(*tmp, hdr_encode_map); |
| 245 | tmp++; |
| 246 | } |
| 247 | |
| 248 | tmp = "\"#"; |
| 249 | while (*tmp) { |
| 250 | FD_SET(*tmp, url_encode_map); |
| 251 | tmp++; |
| 252 | } |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 253 | |
| 254 | /* memory allocations */ |
| 255 | pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED); |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 256 | pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 257 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 258 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 259 | /* |
| 260 | * We have 26 list of methods (1 per first letter), each of which can have |
| 261 | * up to 3 entries (2 valid, 1 null). |
| 262 | */ |
| 263 | struct http_method_desc { |
| 264 | http_meth_t meth; |
| 265 | int len; |
| 266 | const char text[8]; |
| 267 | }; |
| 268 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 269 | const struct http_method_desc http_methods[26][3] = { |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 270 | ['C' - 'A'] = { |
| 271 | [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" }, |
| 272 | }, |
| 273 | ['D' - 'A'] = { |
| 274 | [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" }, |
| 275 | }, |
| 276 | ['G' - 'A'] = { |
| 277 | [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" }, |
| 278 | }, |
| 279 | ['H' - 'A'] = { |
| 280 | [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" }, |
| 281 | }, |
| 282 | ['P' - 'A'] = { |
| 283 | [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" }, |
| 284 | [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" }, |
| 285 | }, |
| 286 | ['T' - 'A'] = { |
| 287 | [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" }, |
| 288 | }, |
| 289 | /* rest is empty like this : |
| 290 | * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" }, |
| 291 | */ |
| 292 | }; |
| 293 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 294 | /* 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] | 295 | * 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] | 296 | * RFC2616 for those chars. |
| 297 | */ |
| 298 | |
| 299 | const char http_is_spht[256] = { |
| 300 | [' '] = 1, ['\t'] = 1, |
| 301 | }; |
| 302 | |
| 303 | const char http_is_crlf[256] = { |
| 304 | ['\r'] = 1, ['\n'] = 1, |
| 305 | }; |
| 306 | |
| 307 | const char http_is_lws[256] = { |
| 308 | [' '] = 1, ['\t'] = 1, |
| 309 | ['\r'] = 1, ['\n'] = 1, |
| 310 | }; |
| 311 | |
| 312 | const char http_is_sep[256] = { |
| 313 | ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1, |
| 314 | ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1, |
| 315 | ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1, |
| 316 | ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1, |
| 317 | [' '] = 1, ['\t'] = 1, ['\\'] = 1, |
| 318 | }; |
| 319 | |
| 320 | const char http_is_ctl[256] = { |
| 321 | [0 ... 31] = 1, |
| 322 | [127] = 1, |
| 323 | }; |
| 324 | |
| 325 | /* |
| 326 | * A token is any ASCII char that is neither a separator nor a CTL char. |
| 327 | * Do not overwrite values in assignment since gcc-2.95 will not handle |
| 328 | * them correctly. Instead, define every non-CTL char's status. |
| 329 | */ |
| 330 | const char http_is_token[256] = { |
| 331 | [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1, |
| 332 | ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1, |
| 333 | ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1, |
| 334 | [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0, |
| 335 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, |
| 336 | ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1, |
| 337 | ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0, |
| 338 | ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0, |
| 339 | ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1, |
| 340 | ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1, |
| 341 | ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1, |
| 342 | ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1, |
| 343 | ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1, |
| 344 | ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1, |
| 345 | ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0, |
| 346 | ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1, |
| 347 | ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1, |
| 348 | ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1, |
| 349 | ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1, |
| 350 | ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1, |
| 351 | ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1, |
| 352 | ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1, |
| 353 | ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0, |
| 354 | ['|'] = 1, ['}'] = 0, ['~'] = 1, |
| 355 | }; |
| 356 | |
| 357 | |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 358 | /* |
| 359 | * An http ver_token is any ASCII which can be found in an HTTP version, |
| 360 | * which includes 'H', 'T', 'P', '/', '.' and any digit. |
| 361 | */ |
| 362 | const char http_is_ver_token[256] = { |
| 363 | ['.'] = 1, ['/'] = 1, |
| 364 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1, |
| 365 | ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1, |
| 366 | ['H'] = 1, ['P'] = 1, ['T'] = 1, |
| 367 | }; |
| 368 | |
| 369 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 370 | /* |
Willy Tarreau | e988a79 | 2010-01-04 21:13:14 +0100 | [diff] [blame] | 371 | * Silent debug that outputs only in strace, using fd #-1. Trash is modified. |
| 372 | */ |
| 373 | #if defined(DEBUG_FSM) |
| 374 | static void http_silent_debug(int line, struct session *s) |
| 375 | { |
| 376 | int size = 0; |
| 377 | size += snprintf(trash + size, sizeof(trash) - size, |
| 378 | "[%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", |
| 379 | line, |
| 380 | s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers, |
| 381 | 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); |
| 382 | write(-1, trash, size); |
| 383 | size = 0; |
| 384 | size += snprintf(trash + size, sizeof(trash) - size, |
| 385 | " %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", |
| 386 | line, |
| 387 | s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers, |
| 388 | 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); |
| 389 | |
| 390 | write(-1, trash, size); |
| 391 | } |
| 392 | #else |
| 393 | #define http_silent_debug(l,s) do { } while (0) |
| 394 | #endif |
| 395 | |
| 396 | /* |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 397 | * Adds a header and its CRLF at the tail of buffer <b>, just before the last |
| 398 | * CRLF. Text length is measured first, so it cannot be NULL. |
| 399 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 400 | * of headers is automatically adjusted. The number of bytes added is returned |
| 401 | * on success, otherwise <0 is returned indicating an error. |
| 402 | */ |
| 403 | int http_header_add_tail(struct buffer *b, struct http_msg *msg, |
| 404 | struct hdr_idx *hdr_idx, const char *text) |
| 405 | { |
| 406 | int bytes, len; |
| 407 | |
| 408 | len = strlen(text); |
| 409 | bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len); |
| 410 | if (!bytes) |
| 411 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 412 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 413 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 414 | } |
| 415 | |
| 416 | /* |
| 417 | * Adds a header and its CRLF at the tail of buffer <b>, just before the last |
| 418 | * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then |
| 419 | * the buffer is only opened and the space reserved, but nothing is copied. |
| 420 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 421 | * of headers is automatically adjusted. The number of bytes added is returned |
| 422 | * on success, otherwise <0 is returned indicating an error. |
| 423 | */ |
| 424 | int http_header_add_tail2(struct buffer *b, struct http_msg *msg, |
| 425 | struct hdr_idx *hdr_idx, const char *text, int len) |
| 426 | { |
| 427 | int bytes; |
| 428 | |
| 429 | bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len); |
| 430 | if (!bytes) |
| 431 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 432 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 433 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 434 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 435 | |
| 436 | /* |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 437 | * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| 438 | * If so, returns the position of the first non-space character relative to |
| 439 | * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| 440 | * to return a pointer to the place after the first space. Returns 0 if the |
| 441 | * header name does not match. Checks are case-insensitive. |
| 442 | */ |
| 443 | int http_header_match2(const char *hdr, const char *end, |
| 444 | const char *name, int len) |
| 445 | { |
| 446 | const char *val; |
| 447 | |
| 448 | if (hdr + len >= end) |
| 449 | return 0; |
| 450 | if (hdr[len] != ':') |
| 451 | return 0; |
| 452 | if (strncasecmp(hdr, name, len) != 0) |
| 453 | return 0; |
| 454 | val = hdr + len + 1; |
| 455 | while (val < end && HTTP_IS_SPHT(*val)) |
| 456 | val++; |
| 457 | if ((val >= end) && (len + 2 <= end - hdr)) |
| 458 | return len + 2; /* we may replace starting from second space */ |
| 459 | return val - hdr; |
| 460 | } |
| 461 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 462 | /* Find the end of the header value contained between <s> and <e>. See RFC2616, |
| 463 | * par 2.2 for more information. Note that it requires a valid header to return |
| 464 | * a valid result. This works for headers defined as comma-separated lists. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 465 | */ |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 466 | char *find_hdr_value_end(char *s, const char *e) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 467 | { |
| 468 | int quoted, qdpair; |
| 469 | |
| 470 | quoted = qdpair = 0; |
| 471 | for (; s < e; s++) { |
| 472 | if (qdpair) qdpair = 0; |
Willy Tarreau | 0f7f51f | 2010-08-30 11:06:34 +0200 | [diff] [blame] | 473 | else if (quoted) { |
| 474 | if (*s == '\\') qdpair = 1; |
| 475 | else if (*s == '"') quoted = 0; |
| 476 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 477 | else if (*s == '"') quoted = 1; |
| 478 | else if (*s == ',') return s; |
| 479 | } |
| 480 | return s; |
| 481 | } |
| 482 | |
| 483 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 484 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 485 | * structure holds everything necessary to use the header and find next |
| 486 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 487 | * beginning. Otherwise, the next occurrence is returned. The function returns |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 488 | * 1 when it finds a value, and 0 when there is no more. It is designed to work |
| 489 | * with headers defined as comma-separated lists. As a special case, if ctx->val |
| 490 | * is NULL when searching for a new values of a header, the current header is |
| 491 | * rescanned. This allows rescanning after a header deletion. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 492 | */ |
| 493 | int http_find_header2(const char *name, int len, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 494 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 495 | struct hdr_ctx *ctx) |
| 496 | { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 497 | char *eol, *sov; |
| 498 | int cur_idx, old_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 499 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 500 | cur_idx = ctx->idx; |
| 501 | if (cur_idx) { |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 502 | /* We have previously returned a value, let's search |
| 503 | * another one on the same line. |
| 504 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 505 | sol = ctx->line; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 506 | ctx->del = ctx->val + ctx->vlen; |
| 507 | sov = sol + ctx->del; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 508 | eol = sol + idx->v[cur_idx].len; |
| 509 | |
| 510 | if (sov >= eol) |
| 511 | /* no more values in this header */ |
| 512 | goto next_hdr; |
| 513 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 514 | /* values remaining for this header, skip the comma but save it |
| 515 | * for later use (eg: for header deletion). |
| 516 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 517 | sov++; |
| 518 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 519 | sov++; |
| 520 | |
| 521 | goto return_hdr; |
| 522 | } |
| 523 | |
| 524 | /* first request for this header */ |
| 525 | sol += hdr_idx_first_pos(idx); |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 526 | old_idx = 0; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 527 | cur_idx = hdr_idx_first_idx(idx); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 528 | while (cur_idx) { |
| 529 | eol = sol + idx->v[cur_idx].len; |
| 530 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 531 | if (len == 0) { |
| 532 | /* No argument was passed, we want any header. |
| 533 | * To achieve this, we simply build a fake request. */ |
| 534 | while (sol + len < eol && sol[len] != ':') |
| 535 | len++; |
| 536 | name = sol; |
| 537 | } |
| 538 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 539 | if ((len < eol - sol) && |
| 540 | (sol[len] == ':') && |
| 541 | (strncasecmp(sol, name, len) == 0)) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 542 | ctx->del = len; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 543 | sov = sol + len + 1; |
| 544 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 545 | sov++; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 546 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 547 | ctx->line = sol; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 548 | ctx->prev = old_idx; |
| 549 | return_hdr: |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 550 | ctx->idx = cur_idx; |
| 551 | ctx->val = sov - sol; |
| 552 | |
| 553 | eol = find_hdr_value_end(sov, eol); |
| 554 | ctx->vlen = eol - sov; |
| 555 | return 1; |
| 556 | } |
| 557 | next_hdr: |
| 558 | sol = eol + idx->v[cur_idx].cr + 1; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 559 | old_idx = cur_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 560 | cur_idx = idx->v[cur_idx].next; |
| 561 | } |
| 562 | return 0; |
| 563 | } |
| 564 | |
| 565 | int http_find_header(const char *name, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 566 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 567 | struct hdr_ctx *ctx) |
| 568 | { |
| 569 | return http_find_header2(name, strlen(name), sol, idx, ctx); |
| 570 | } |
| 571 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 572 | /* Remove one value of a header. This only works on a <ctx> returned by one of |
| 573 | * the http_find_header functions. The value is removed, as well as surrounding |
| 574 | * commas if any. If the removed value was alone, the whole header is removed. |
| 575 | * The ctx is always updated accordingly, as well as buffer <buf> and HTTP |
| 576 | * message <msg>. The new index is returned. If it is zero, it means there is |
| 577 | * no more header, so any processing may stop. The ctx is always left in a form |
| 578 | * that can be handled by http_find_header2() to find next occurrence. |
| 579 | */ |
| 580 | int http_remove_header2(struct http_msg *msg, struct buffer *buf, |
| 581 | struct hdr_idx *idx, struct hdr_ctx *ctx) |
| 582 | { |
| 583 | int cur_idx = ctx->idx; |
| 584 | char *sol = ctx->line; |
| 585 | struct hdr_idx_elem *hdr; |
| 586 | int delta, skip_comma; |
| 587 | |
| 588 | if (!cur_idx) |
| 589 | return 0; |
| 590 | |
| 591 | hdr = &idx->v[cur_idx]; |
| 592 | if (sol[ctx->del] == ':' && ctx->val + ctx->vlen == hdr->len) { |
| 593 | /* This was the only value of the header, we must now remove it entirely. */ |
| 594 | delta = buffer_replace2(buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0); |
| 595 | http_msg_move_end(msg, delta); |
| 596 | idx->used--; |
| 597 | hdr->len = 0; /* unused entry */ |
| 598 | idx->v[ctx->prev].next = idx->v[ctx->idx].next; |
Willy Tarreau | 5c4784f | 2011-02-12 13:07:35 +0100 | [diff] [blame] | 599 | if (idx->tail == ctx->idx) |
| 600 | idx->tail = ctx->prev; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 601 | ctx->idx = ctx->prev; /* walk back to the end of previous header */ |
| 602 | ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1; |
| 603 | ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */ |
| 604 | ctx->vlen = 0; |
| 605 | return ctx->idx; |
| 606 | } |
| 607 | |
| 608 | /* This was not the only value of this header. We have to remove between |
| 609 | * ctx->del+1 and ctx->val+ctx->vlen+1 included. If it is the last entry |
| 610 | * of the list, we remove the last separator. |
| 611 | */ |
| 612 | |
| 613 | skip_comma = (ctx->val + ctx->vlen == hdr->len) ? 0 : 1; |
| 614 | delta = buffer_replace2(buf, sol + ctx->del + skip_comma, |
| 615 | sol + ctx->val + ctx->vlen + skip_comma, |
| 616 | NULL, 0); |
| 617 | hdr->len += delta; |
| 618 | http_msg_move_end(msg, delta); |
| 619 | ctx->val = ctx->del; |
| 620 | ctx->vlen = 0; |
| 621 | return ctx->idx; |
| 622 | } |
| 623 | |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 624 | /* This function handles a server error at the stream interface level. The |
| 625 | * stream interface is assumed to be already in a closed state. An optional |
| 626 | * message is copied into the input buffer, and an HTTP status code stored. |
| 627 | * 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] | 628 | * in this buffer will be lost. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 629 | */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 630 | static void http_server_error(struct session *t, struct stream_interface *si, |
| 631 | int err, int finst, int status, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 632 | { |
Willy Tarreau | d5fd51c | 2010-01-22 14:17:47 +0100 | [diff] [blame] | 633 | buffer_auto_read(si->ob); |
| 634 | buffer_abort(si->ob); |
| 635 | buffer_auto_close(si->ob); |
| 636 | buffer_erase(si->ob); |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 637 | buffer_auto_close(si->ib); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 638 | buffer_auto_read(si->ib); |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 639 | if (status > 0 && msg) { |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 640 | t->txn.status = status; |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 641 | buffer_write(si->ib, msg->str, msg->len); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 642 | } |
| 643 | if (!(t->flags & SN_ERR_MASK)) |
| 644 | t->flags |= err; |
| 645 | if (!(t->flags & SN_FINST_MASK)) |
| 646 | t->flags |= finst; |
| 647 | } |
| 648 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 649 | /* This function returns the appropriate error location for the given session |
| 650 | * and message. |
| 651 | */ |
| 652 | |
| 653 | struct chunk *error_message(struct session *s, int msgnum) |
| 654 | { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 655 | if (s->be->errmsg[msgnum].str) |
| 656 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 657 | else if (s->fe->errmsg[msgnum].str) |
| 658 | return &s->fe->errmsg[msgnum]; |
| 659 | else |
| 660 | return &http_err_chunks[msgnum]; |
| 661 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 662 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 663 | /* |
| 664 | * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text |
| 665 | * string), HTTP_METH_OTHER for unknown methods, or the identified method. |
| 666 | */ |
| 667 | static http_meth_t find_http_meth(const char *str, const int len) |
| 668 | { |
| 669 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 670 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 671 | |
| 672 | m = ((unsigned)*str - 'A'); |
| 673 | |
| 674 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 675 | for (h = http_methods[m]; h->len > 0; h++) { |
| 676 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 677 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 678 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 679 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 680 | }; |
| 681 | return HTTP_METH_OTHER; |
| 682 | } |
| 683 | return HTTP_METH_NONE; |
| 684 | |
| 685 | } |
| 686 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 687 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 688 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 689 | * It is returned otherwise. |
| 690 | */ |
| 691 | static char * |
| 692 | http_get_path(struct http_txn *txn) |
| 693 | { |
| 694 | char *ptr, *end; |
| 695 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 696 | ptr = txn->req.sol + txn->req.sl.rq.u; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 697 | end = ptr + txn->req.sl.rq.u_l; |
| 698 | |
| 699 | if (ptr >= end) |
| 700 | return NULL; |
| 701 | |
| 702 | /* RFC2616, par. 5.1.2 : |
| 703 | * Request-URI = "*" | absuri | abspath | authority |
| 704 | */ |
| 705 | |
| 706 | if (*ptr == '*') |
| 707 | return NULL; |
| 708 | |
| 709 | if (isalpha((unsigned char)*ptr)) { |
| 710 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 711 | ptr++; |
| 712 | while (ptr < end && |
| 713 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 714 | ptr++; |
| 715 | /* skip '://' */ |
| 716 | if (ptr == end || *ptr++ != ':') |
| 717 | return NULL; |
| 718 | if (ptr == end || *ptr++ != '/') |
| 719 | return NULL; |
| 720 | if (ptr == end || *ptr++ != '/') |
| 721 | return NULL; |
| 722 | } |
| 723 | /* skip [user[:passwd]@]host[:[port]] */ |
| 724 | |
| 725 | while (ptr < end && *ptr != '/') |
| 726 | ptr++; |
| 727 | |
| 728 | if (ptr == end) |
| 729 | return NULL; |
| 730 | |
| 731 | /* OK, we got the '/' ! */ |
| 732 | return ptr; |
| 733 | } |
| 734 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 735 | /* Returns a 302 for a redirectable request. This may only be called just after |
| 736 | * the stream interface has moved to SI_ST_ASS. Unprocessable requests are |
| 737 | * left unchanged and will follow normal proxy processing. |
| 738 | */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 739 | void perform_http_redirect(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 740 | { |
| 741 | struct http_txn *txn; |
| 742 | struct chunk rdr; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 743 | struct server *srv; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 744 | char *path; |
| 745 | int len; |
| 746 | |
| 747 | /* 1: create the response header */ |
| 748 | rdr.len = strlen(HTTP_302); |
| 749 | rdr.str = trash; |
Willy Tarreau | 59e0b0f | 2010-01-09 21:29:23 +0100 | [diff] [blame] | 750 | rdr.size = sizeof(trash); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 751 | memcpy(rdr.str, HTTP_302, rdr.len); |
| 752 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 753 | srv = target_srv(&s->target); |
| 754 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 755 | /* 2: add the server's prefix */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 756 | if (rdr.len + srv->rdr_len > rdr.size) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 757 | return; |
| 758 | |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 759 | /* special prefix "/" means don't change URL */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 760 | if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') { |
| 761 | memcpy(rdr.str + rdr.len, srv->rdr_pfx, srv->rdr_len); |
| 762 | rdr.len += srv->rdr_len; |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 763 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 764 | |
| 765 | /* 3: add the request URI */ |
| 766 | txn = &s->txn; |
| 767 | path = http_get_path(txn); |
| 768 | if (!path) |
| 769 | return; |
| 770 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 771 | 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] | 772 | if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 773 | return; |
| 774 | |
| 775 | memcpy(rdr.str + rdr.len, path, len); |
| 776 | rdr.len += len; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 777 | |
| 778 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 779 | memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 780 | rdr.len += 29; |
| 781 | } else { |
| 782 | memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23); |
| 783 | rdr.len += 23; |
| 784 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 785 | |
| 786 | /* prepare to return without error. */ |
Willy Tarreau | 99126c3 | 2008-11-27 10:30:51 +0100 | [diff] [blame] | 787 | si->shutr(si); |
| 788 | si->shutw(si); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 789 | si->err_type = SI_ET_NONE; |
| 790 | si->err_loc = NULL; |
| 791 | si->state = SI_ST_CLO; |
| 792 | |
| 793 | /* send the message */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 794 | 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] | 795 | |
| 796 | /* 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] | 797 | if (srv) |
| 798 | srv_inc_sess_ctr(srv); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 799 | } |
| 800 | |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 801 | /* Return the error message corresponding to si->err_type. It is assumed |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 802 | * that the server side is closed. Note that err_type is actually a |
| 803 | * bitmask, where almost only aborts may be cumulated with other |
| 804 | * values. We consider that aborted operations are more important |
| 805 | * than timeouts or errors due to the fact that nobody else in the |
| 806 | * logs might explain incomplete retries. All others should avoid |
| 807 | * being cumulated. It should normally not be possible to have multiple |
| 808 | * aborts at once, but just in case, the first one in sequence is reported. |
| 809 | */ |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 810 | void http_return_srv_error(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 811 | { |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 812 | int err_type = si->err_type; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 813 | |
| 814 | if (err_type & SI_ET_QUEUE_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 815 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q, |
| 816 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 817 | else if (err_type & SI_ET_CONN_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 818 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C, |
| 819 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 820 | else if (err_type & SI_ET_QUEUE_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 821 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q, |
| 822 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 823 | else if (err_type & SI_ET_QUEUE_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 824 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q, |
| 825 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 826 | else if (err_type & SI_ET_CONN_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 827 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C, |
| 828 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 829 | else if (err_type & SI_ET_CONN_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 830 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C, |
| 831 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 832 | else /* SI_ET_CONN_OTHER and others */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 833 | http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C, |
| 834 | 500, error_message(s, HTTP_ERR_500)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 835 | } |
| 836 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 837 | extern const char sess_term_cond[8]; |
| 838 | extern const char sess_fin_state[8]; |
| 839 | extern const char *monthname[12]; |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 840 | const char sess_cookie[8] = "NIDVEO67"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie, Expired cookie, Old cookie, unknown */ |
| 841 | const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive), |
| 842 | Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten, |
| 843 | Set-cookie Updated, unknown, unknown */ |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 844 | struct pool_head *pool2_requri; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 845 | struct pool_head *pool2_capture; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 846 | |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 847 | void http_sess_clflog(struct session *s) |
| 848 | { |
Cyril Bonté | acd7d63 | 2010-11-01 19:26:02 +0100 | [diff] [blame] | 849 | char pn[INET6_ADDRSTRLEN]; |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 850 | struct proxy *fe = s->fe; |
| 851 | struct proxy *be = s->be; |
| 852 | struct proxy *prx_log; |
| 853 | struct http_txn *txn = &s->txn; |
| 854 | int tolog, level, err; |
| 855 | char *uri, *h; |
Willy Tarreau | 71904a4 | 2011-02-13 14:30:26 +0100 | [diff] [blame] | 856 | const char *svid; |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 857 | struct tm tm; |
| 858 | static char tmpline[MAX_SYSLOG_LEN]; |
| 859 | int hdr; |
| 860 | size_t w; |
| 861 | int t_request; |
| 862 | |
| 863 | prx_log = fe; |
| 864 | err = (s->flags & (SN_ERR_MASK | SN_REDISP)) || |
Willy Tarreau | ee28de0 | 2010-06-01 09:51:00 +0200 | [diff] [blame] | 865 | (s->req->cons->conn_retries != be->conn_retries) || |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 866 | txn->status >= 500; |
| 867 | |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 868 | if (s->req->prod->addr.c.from.ss_family == AF_INET) |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 869 | inet_ntop(AF_INET, |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 870 | (const void *)&((struct sockaddr_in *)&s->req->prod->addr.c.from)->sin_addr, |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 871 | pn, sizeof(pn)); |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 872 | else if (s->req->prod->addr.c.from.ss_family == AF_INET6) |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 873 | inet_ntop(AF_INET6, |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 874 | (const void *)&((struct sockaddr_in6 *)(&s->req->prod->addr.c.from))->sin6_addr, |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 875 | pn, sizeof(pn)); |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 876 | else |
| 877 | snprintf(pn, sizeof(pn), "unix:%d", s->listener->luid); |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 878 | |
| 879 | get_gmtime(s->logs.accept_date.tv_sec, &tm); |
| 880 | |
| 881 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
| 882 | tolog = fe->to_log; |
| 883 | |
| 884 | h = tmpline; |
| 885 | |
| 886 | w = snprintf(h, sizeof(tmpline), |
| 887 | "%s - - [%02d/%s/%04d:%02d:%02d:%02d +0000]", |
| 888 | pn, |
| 889 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
| 890 | tm.tm_hour, tm.tm_min, tm.tm_sec); |
| 891 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 892 | goto trunc; |
| 893 | h += w; |
| 894 | |
| 895 | if (h >= tmpline + sizeof(tmpline) - 4) |
| 896 | goto trunc; |
| 897 | |
| 898 | *(h++) = ' '; |
| 899 | *(h++) = '\"'; |
| 900 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
| 901 | h = encode_string(h, tmpline + sizeof(tmpline) - 1, |
| 902 | '#', url_encode_map, uri); |
| 903 | *(h++) = '\"'; |
| 904 | |
| 905 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), " %d %lld", txn->status, s->logs.bytes_out); |
| 906 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 907 | goto trunc; |
| 908 | h += w; |
| 909 | |
| 910 | if (h >= tmpline + sizeof(tmpline) - 9) |
| 911 | goto trunc; |
| 912 | memcpy(h, " \"-\" \"-\"", 8); |
| 913 | h += 8; |
| 914 | |
| 915 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), |
| 916 | " %d %03d", |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 917 | s->req->prod->addr.c.from.ss_family == AF_UNIX ? s->listener->luid : |
| 918 | ntohs((s->req->prod->addr.c.from.ss_family == AF_INET) ? |
| 919 | ((struct sockaddr_in *)&s->req->prod->addr.c.from)->sin_port : |
| 920 | ((struct sockaddr_in6 *)&s->req->prod->addr.c.from)->sin6_port), |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 921 | (int)s->logs.accept_date.tv_usec/1000); |
| 922 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 923 | goto trunc; |
| 924 | h += w; |
| 925 | |
| 926 | w = strlen(fe->id); |
| 927 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 928 | goto trunc; |
| 929 | *(h++) = ' '; |
| 930 | *(h++) = '\"'; |
| 931 | memcpy(h, fe->id, w); |
| 932 | h += w; |
| 933 | *(h++) = '\"'; |
| 934 | |
| 935 | w = strlen(be->id); |
| 936 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 937 | goto trunc; |
| 938 | *(h++) = ' '; |
| 939 | *(h++) = '\"'; |
| 940 | memcpy(h, be->id, w); |
| 941 | h += w; |
| 942 | *(h++) = '\"'; |
| 943 | |
Willy Tarreau | 71904a4 | 2011-02-13 14:30:26 +0100 | [diff] [blame] | 944 | if (!(tolog & LW_SVID)) |
| 945 | svid = "-"; |
Willy Tarreau | 7b7a8e9 | 2011-03-27 19:53:06 +0200 | [diff] [blame] | 946 | else switch (s->target.type) { |
Willy Tarreau | 71904a4 | 2011-02-13 14:30:26 +0100 | [diff] [blame] | 947 | case TARG_TYPE_SERVER: |
Willy Tarreau | 7b7a8e9 | 2011-03-27 19:53:06 +0200 | [diff] [blame] | 948 | svid = s->target.ptr.s->id; |
Willy Tarreau | 71904a4 | 2011-02-13 14:30:26 +0100 | [diff] [blame] | 949 | break; |
| 950 | case TARG_TYPE_APPLET: |
Willy Tarreau | 7b7a8e9 | 2011-03-27 19:53:06 +0200 | [diff] [blame] | 951 | svid = s->target.ptr.a->name; |
Willy Tarreau | 71904a4 | 2011-02-13 14:30:26 +0100 | [diff] [blame] | 952 | break; |
| 953 | default: |
| 954 | svid = "<NOSRV>"; |
| 955 | break; |
| 956 | } |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 957 | |
| 958 | w = strlen(svid); |
| 959 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 960 | goto trunc; |
| 961 | *(h++) = ' '; |
| 962 | *(h++) = '\"'; |
| 963 | memcpy(h, svid, w); |
| 964 | h += w; |
| 965 | *(h++) = '\"'; |
| 966 | |
| 967 | t_request = -1; |
| 968 | if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept)) |
| 969 | t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request); |
| 970 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), |
| 971 | " %d %ld %ld %ld %ld", |
| 972 | t_request, |
| 973 | (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1, |
| 974 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, |
| 975 | (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
| 976 | s->logs.t_close); |
| 977 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 978 | goto trunc; |
| 979 | h += w; |
| 980 | |
| 981 | if (h >= tmpline + sizeof(tmpline) - 8) |
| 982 | goto trunc; |
| 983 | *(h++) = ' '; |
| 984 | *(h++) = '\"'; |
| 985 | *(h++) = sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT]; |
| 986 | *(h++) = sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT]; |
| 987 | *(h++) = (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-', |
| 988 | *(h++) = (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-'; |
| 989 | *(h++) = '\"'; |
| 990 | |
| 991 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), |
| 992 | " %d %d %d %d %d %ld %ld", |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 993 | actconn, fe->feconn, be->beconn, target_srv(&s->target) ? target_srv(&s->target)->cur_sess : 0, |
Willy Tarreau | ee28de0 | 2010-06-01 09:51:00 +0200 | [diff] [blame] | 994 | (s->req->cons->conn_retries > 0) ? (be->conn_retries - s->req->cons->conn_retries) : be->conn_retries, |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 995 | s->logs.srv_queue_size, s->logs.prx_queue_size); |
| 996 | |
| 997 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 998 | goto trunc; |
| 999 | h += w; |
| 1000 | |
| 1001 | if (txn->cli_cookie) { |
| 1002 | w = strlen(txn->cli_cookie); |
| 1003 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 1004 | goto trunc; |
| 1005 | *(h++) = ' '; |
| 1006 | *(h++) = '\"'; |
| 1007 | memcpy(h, txn->cli_cookie, w); |
| 1008 | h += w; |
| 1009 | *(h++) = '\"'; |
| 1010 | } else { |
| 1011 | if (h >= tmpline + sizeof(tmpline) - 5) |
| 1012 | goto trunc; |
| 1013 | memcpy(h, " \"-\"", 4); |
| 1014 | h += 4; |
| 1015 | } |
| 1016 | |
| 1017 | if (txn->srv_cookie) { |
| 1018 | w = strlen(txn->srv_cookie); |
| 1019 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 1020 | goto trunc; |
| 1021 | *(h++) = ' '; |
| 1022 | *(h++) = '\"'; |
| 1023 | memcpy(h, txn->srv_cookie, w); |
| 1024 | h += w; |
| 1025 | *(h++) = '\"'; |
| 1026 | } else { |
| 1027 | if (h >= tmpline + sizeof(tmpline) - 5) |
| 1028 | goto trunc; |
| 1029 | memcpy(h, " \"-\"", 4); |
| 1030 | h += 4; |
| 1031 | } |
| 1032 | |
| 1033 | if ((fe->to_log & LW_REQHDR) && txn->req.cap) { |
| 1034 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 1035 | if (h >= sizeof (tmpline) + tmpline - 4) |
| 1036 | goto trunc; |
Cyril Bonté | 7f2c539 | 2010-03-13 15:15:07 +0100 | [diff] [blame] | 1037 | if (txn->req.cap[hdr] != NULL) { |
| 1038 | *(h++) = ' '; |
| 1039 | *(h++) = '\"'; |
| 1040 | h = encode_string(h, tmpline + sizeof(tmpline) - 2, |
| 1041 | '#', hdr_encode_map, txn->req.cap[hdr]); |
| 1042 | *(h++) = '\"'; |
| 1043 | } else { |
| 1044 | memcpy(h, " \"-\"", 4); |
| 1045 | h += 4; |
| 1046 | } |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 1047 | } |
| 1048 | } |
| 1049 | |
| 1050 | if ((fe->to_log & LW_RSPHDR) && txn->rsp.cap) { |
| 1051 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 1052 | if (h >= sizeof (tmpline) + tmpline - 4) |
| 1053 | goto trunc; |
Cyril Bonté | 7f2c539 | 2010-03-13 15:15:07 +0100 | [diff] [blame] | 1054 | if (txn->rsp.cap[hdr] != NULL) { |
| 1055 | *(h++) = ' '; |
| 1056 | *(h++) = '\"'; |
| 1057 | h = encode_string(h, tmpline + sizeof(tmpline) - 2, |
| 1058 | '#', hdr_encode_map, txn->rsp.cap[hdr]); |
| 1059 | *(h++) = '\"'; |
| 1060 | } else { |
| 1061 | memcpy(h, " \"-\"", 4); |
| 1062 | h += 4; |
| 1063 | } |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 1064 | } |
| 1065 | } |
| 1066 | |
| 1067 | trunc: |
| 1068 | *h = '\0'; |
| 1069 | |
| 1070 | level = LOG_INFO; |
| 1071 | if (err && (fe->options2 & PR_O2_LOGERRORS)) |
| 1072 | level = LOG_ERR; |
| 1073 | |
| 1074 | send_log(prx_log, level, "%s\n", tmpline); |
| 1075 | |
| 1076 | s->logs.logwait = 0; |
| 1077 | } |
| 1078 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1079 | /* |
| 1080 | * send a log for the session when we have enough info about it. |
| 1081 | * Will not log if the frontend has no log defined. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1082 | */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1083 | void http_sess_log(struct session *s) |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1084 | { |
Cyril Bonté | acd7d63 | 2010-11-01 19:26:02 +0100 | [diff] [blame] | 1085 | char pn[INET6_ADDRSTRLEN]; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1086 | struct proxy *fe = s->fe; |
| 1087 | struct proxy *be = s->be; |
| 1088 | struct proxy *prx_log; |
| 1089 | struct http_txn *txn = &s->txn; |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 1090 | int tolog, level, err; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1091 | char *uri, *h; |
Willy Tarreau | 71904a4 | 2011-02-13 14:30:26 +0100 | [diff] [blame] | 1092 | const char *svid; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 1093 | struct tm tm; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1094 | static char tmpline[MAX_SYSLOG_LEN]; |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1095 | int t_request; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1096 | int hdr; |
| 1097 | |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 1098 | /* if we don't want to log normal traffic, return now */ |
| 1099 | err = (s->flags & (SN_ERR_MASK | SN_REDISP)) || |
Willy Tarreau | ee28de0 | 2010-06-01 09:51:00 +0200 | [diff] [blame] | 1100 | (s->req->cons->conn_retries != be->conn_retries) || |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 1101 | txn->status >= 500; |
| 1102 | if (!err && (fe->options2 & PR_O2_NOLOGNORM)) |
| 1103 | return; |
| 1104 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1105 | if (fe->logfac1 < 0 && fe->logfac2 < 0) |
| 1106 | return; |
| 1107 | prx_log = fe; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1108 | |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 1109 | if (prx_log->options2 & PR_O2_CLFLOG) |
| 1110 | return http_sess_clflog(s); |
| 1111 | |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 1112 | if (s->req->prod->addr.c.from.ss_family == AF_INET) |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1113 | inet_ntop(AF_INET, |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 1114 | (const void *)&((struct sockaddr_in *)&s->req->prod->addr.c.from)->sin_addr, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1115 | pn, sizeof(pn)); |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 1116 | else if (s->req->prod->addr.c.from.ss_family == AF_INET6) |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1117 | inet_ntop(AF_INET6, |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 1118 | (const void *)&((struct sockaddr_in6 *)(&s->req->prod->addr.c.from))->sin6_addr, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1119 | pn, sizeof(pn)); |
| 1120 | |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 1121 | get_localtime(s->logs.accept_date.tv_sec, &tm); |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1122 | |
| 1123 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
| 1124 | tolog = fe->to_log; |
| 1125 | |
| 1126 | h = tmpline; |
| 1127 | if (fe->to_log & LW_REQHDR && |
| 1128 | txn->req.cap && |
| 1129 | (h < tmpline + sizeof(tmpline) - 10)) { |
| 1130 | *(h++) = ' '; |
| 1131 | *(h++) = '{'; |
| 1132 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 1133 | if (hdr) |
| 1134 | *(h++) = '|'; |
| 1135 | if (txn->req.cap[hdr] != NULL) |
| 1136 | h = encode_string(h, tmpline + sizeof(tmpline) - 7, |
| 1137 | '#', hdr_encode_map, txn->req.cap[hdr]); |
| 1138 | } |
| 1139 | *(h++) = '}'; |
| 1140 | } |
| 1141 | |
| 1142 | if (fe->to_log & LW_RSPHDR && |
| 1143 | txn->rsp.cap && |
| 1144 | (h < tmpline + sizeof(tmpline) - 7)) { |
| 1145 | *(h++) = ' '; |
| 1146 | *(h++) = '{'; |
| 1147 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 1148 | if (hdr) |
| 1149 | *(h++) = '|'; |
| 1150 | if (txn->rsp.cap[hdr] != NULL) |
| 1151 | h = encode_string(h, tmpline + sizeof(tmpline) - 4, |
| 1152 | '#', hdr_encode_map, txn->rsp.cap[hdr]); |
| 1153 | } |
| 1154 | *(h++) = '}'; |
| 1155 | } |
| 1156 | |
| 1157 | if (h < tmpline + sizeof(tmpline) - 4) { |
| 1158 | *(h++) = ' '; |
| 1159 | *(h++) = '"'; |
| 1160 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
| 1161 | h = encode_string(h, tmpline + sizeof(tmpline) - 1, |
| 1162 | '#', url_encode_map, uri); |
| 1163 | *(h++) = '"'; |
| 1164 | } |
| 1165 | *h = '\0'; |
| 1166 | |
Willy Tarreau | 71904a4 | 2011-02-13 14:30:26 +0100 | [diff] [blame] | 1167 | if (!(tolog & LW_SVID)) |
| 1168 | svid = "-"; |
Willy Tarreau | 7b7a8e9 | 2011-03-27 19:53:06 +0200 | [diff] [blame] | 1169 | else switch (s->target.type) { |
Willy Tarreau | 71904a4 | 2011-02-13 14:30:26 +0100 | [diff] [blame] | 1170 | case TARG_TYPE_SERVER: |
Willy Tarreau | 7b7a8e9 | 2011-03-27 19:53:06 +0200 | [diff] [blame] | 1171 | svid = s->target.ptr.s->id; |
Willy Tarreau | 71904a4 | 2011-02-13 14:30:26 +0100 | [diff] [blame] | 1172 | break; |
| 1173 | case TARG_TYPE_APPLET: |
Willy Tarreau | 7b7a8e9 | 2011-03-27 19:53:06 +0200 | [diff] [blame] | 1174 | svid = s->target.ptr.a->name; |
Willy Tarreau | 71904a4 | 2011-02-13 14:30:26 +0100 | [diff] [blame] | 1175 | break; |
| 1176 | default: |
| 1177 | svid = "<NOSRV>"; |
| 1178 | break; |
| 1179 | } |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1180 | |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1181 | t_request = -1; |
| 1182 | if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept)) |
| 1183 | t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request); |
| 1184 | |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 1185 | level = LOG_INFO; |
| 1186 | if (err && (fe->options2 & PR_O2_LOGERRORS)) |
| 1187 | level = LOG_ERR; |
| 1188 | |
| 1189 | send_log(prx_log, level, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1190 | "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]" |
Willy Tarreau | 1772ece | 2009-04-03 14:49:12 +0200 | [diff] [blame] | 1191 | " %s %s/%s %d/%ld/%ld/%ld/%s%ld %d %s%lld" |
| 1192 | " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %ld/%ld%s\n", |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 1193 | (s->req->prod->addr.c.from.ss_family == AF_UNIX) ? "unix" : pn, |
| 1194 | (s->req->prod->addr.c.from.ss_family == AF_UNIX) ? s->listener->luid : |
| 1195 | ntohs((s->req->prod->addr.c.from.ss_family == AF_INET) ? |
| 1196 | ((struct sockaddr_in *)&s->req->prod->addr.c.from)->sin_port : |
| 1197 | ((struct sockaddr_in6 *)&s->req->prod->addr.c.from)->sin6_port), |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 1198 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
Willy Tarreau | 1772ece | 2009-04-03 14:49:12 +0200 | [diff] [blame] | 1199 | tm.tm_hour, tm.tm_min, tm.tm_sec, (int)s->logs.accept_date.tv_usec/1000, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1200 | fe->id, be->id, svid, |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1201 | t_request, |
| 1202 | (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1203 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, |
| 1204 | (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
| 1205 | (tolog & LW_BYTES) ? "" : "+", s->logs.t_close, |
| 1206 | txn->status, |
Willy Tarreau | 8b3977f | 2008-01-18 11:16:32 +0100 | [diff] [blame] | 1207 | (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1208 | txn->cli_cookie ? txn->cli_cookie : "-", |
| 1209 | txn->srv_cookie ? txn->srv_cookie : "-", |
| 1210 | sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT], |
| 1211 | sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT], |
| 1212 | (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-', |
| 1213 | (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-', |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 1214 | actconn, fe->feconn, be->beconn, target_srv(&s->target) ? target_srv(&s->target)->cur_sess : 0, |
Krzysztof Piotr Oledzki | 25b501a | 2008-01-06 16:36:16 +0100 | [diff] [blame] | 1215 | (s->flags & SN_REDISP)?"+":"", |
Willy Tarreau | ee28de0 | 2010-06-01 09:51:00 +0200 | [diff] [blame] | 1216 | (s->req->cons->conn_retries>0)?(be->conn_retries - s->req->cons->conn_retries):be->conn_retries, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1217 | s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline); |
| 1218 | |
| 1219 | s->logs.logwait = 0; |
| 1220 | } |
| 1221 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1222 | |
| 1223 | /* |
| 1224 | * Capture headers from message starting at <som> according to header list |
| 1225 | * <cap_hdr>, and fill the <idx> structure appropriately. |
| 1226 | */ |
| 1227 | void capture_headers(char *som, struct hdr_idx *idx, |
| 1228 | char **cap, struct cap_hdr *cap_hdr) |
| 1229 | { |
| 1230 | char *eol, *sol, *col, *sov; |
| 1231 | int cur_idx; |
| 1232 | struct cap_hdr *h; |
| 1233 | int len; |
| 1234 | |
| 1235 | sol = som + hdr_idx_first_pos(idx); |
| 1236 | cur_idx = hdr_idx_first_idx(idx); |
| 1237 | |
| 1238 | while (cur_idx) { |
| 1239 | eol = sol + idx->v[cur_idx].len; |
| 1240 | |
| 1241 | col = sol; |
| 1242 | while (col < eol && *col != ':') |
| 1243 | col++; |
| 1244 | |
| 1245 | sov = col + 1; |
| 1246 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 1247 | sov++; |
| 1248 | |
| 1249 | for (h = cap_hdr; h; h = h->next) { |
| 1250 | if ((h->namelen == col - sol) && |
| 1251 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 1252 | if (cap[h->index] == NULL) |
| 1253 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 1254 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1255 | |
| 1256 | if (cap[h->index] == NULL) { |
| 1257 | Alert("HTTP capture : out of memory.\n"); |
| 1258 | continue; |
| 1259 | } |
| 1260 | |
| 1261 | len = eol - sov; |
| 1262 | if (len > h->len) |
| 1263 | len = h->len; |
| 1264 | |
| 1265 | memcpy(cap[h->index], sov, len); |
| 1266 | cap[h->index][len]=0; |
| 1267 | } |
| 1268 | } |
| 1269 | sol = eol + idx->v[cur_idx].cr + 1; |
| 1270 | cur_idx = idx->v[cur_idx].next; |
| 1271 | } |
| 1272 | } |
| 1273 | |
| 1274 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1275 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 1276 | */ |
| 1277 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 1278 | |
| 1279 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 1280 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 1281 | */ |
| 1282 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 1283 | ptr++; \ |
| 1284 | if (likely(ptr < end)) \ |
| 1285 | goto good; \ |
| 1286 | else { \ |
| 1287 | state = (st); \ |
| 1288 | goto http_msg_ood; \ |
| 1289 | } \ |
| 1290 | } while (0) |
| 1291 | |
| 1292 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1293 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1294 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1295 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 1296 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 1297 | * will give undefined results. |
| 1298 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1299 | * and that msg->sol points to the beginning of the response. |
| 1300 | * If a complete line is found (which implies that at least one CR or LF is |
| 1301 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1302 | * returned indicating an incomplete line (which does not mean that parts have |
| 1303 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1304 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1305 | * upon next call. |
| 1306 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1307 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1308 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1309 | * 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] | 1310 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1311 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1312 | const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf, |
| 1313 | unsigned int state, const char *ptr, const char *end, |
| 1314 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1315 | { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1316 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1317 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1318 | http_msg_rpver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1319 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1320 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 1321 | |
| 1322 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1323 | msg->sl.st.v_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1324 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1325 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1326 | state = HTTP_MSG_ERROR; |
| 1327 | break; |
| 1328 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1329 | case HTTP_MSG_RPVER_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1330 | http_msg_rpver_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1331 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1332 | msg->sl.st.c = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1333 | goto http_msg_rpcode; |
| 1334 | } |
| 1335 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1336 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1337 | /* so it's a CR/LF, this is invalid */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1338 | state = HTTP_MSG_ERROR; |
| 1339 | break; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1340 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1341 | case HTTP_MSG_RPCODE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1342 | http_msg_rpcode: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1343 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1344 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 1345 | |
| 1346 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1347 | 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] | 1348 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1349 | } |
| 1350 | |
| 1351 | /* so it's a CR/LF, so there is no reason phrase */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1352 | 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] | 1353 | http_msg_rsp_reason: |
| 1354 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1355 | msg->sl.st.r = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1356 | msg->sl.st.r_l = 0; |
| 1357 | goto http_msg_rpline_eol; |
| 1358 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1359 | case HTTP_MSG_RPCODE_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1360 | http_msg_rpcode_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1361 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1362 | msg->sl.st.r = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1363 | goto http_msg_rpreason; |
| 1364 | } |
| 1365 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1366 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1367 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1368 | goto http_msg_rsp_reason; |
| 1369 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1370 | case HTTP_MSG_RPREASON: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1371 | http_msg_rpreason: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1372 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1373 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1374 | 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] | 1375 | http_msg_rpline_eol: |
| 1376 | /* We have seen the end of line. Note that we do not |
| 1377 | * necessarily have the \n yet, but at least we know that we |
| 1378 | * have EITHER \r OR \n, otherwise the response would not be |
| 1379 | * complete. We can then record the response length and return |
| 1380 | * to the caller which will be able to register it. |
| 1381 | */ |
| 1382 | msg->sl.st.l = ptr - msg->sol; |
| 1383 | return ptr; |
| 1384 | |
| 1385 | #ifdef DEBUG_FULL |
| 1386 | default: |
| 1387 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1388 | exit(1); |
| 1389 | #endif |
| 1390 | } |
| 1391 | |
| 1392 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1393 | /* out of valid data */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1394 | if (ret_state) |
| 1395 | *ret_state = state; |
| 1396 | if (ret_ptr) |
| 1397 | *ret_ptr = (char *)ptr; |
| 1398 | return NULL; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1399 | } |
| 1400 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1401 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1402 | * This function parses a request line between <ptr> and <end>, starting with |
| 1403 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1404 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1405 | * will give undefined results. |
| 1406 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1407 | * and that msg->sol points to the beginning of the request. |
| 1408 | * If a complete line is found (which implies that at least one CR or LF is |
| 1409 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1410 | * returned indicating an incomplete line (which does not mean that parts have |
| 1411 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1412 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1413 | * upon next call. |
| 1414 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1415 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1416 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1417 | * 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] | 1418 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1419 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1420 | const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf, |
| 1421 | unsigned int state, const char *ptr, const char *end, |
| 1422 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1423 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1424 | switch (state) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1425 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1426 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1427 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1428 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1429 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1430 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1431 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1432 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1433 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1434 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1435 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1436 | /* HTTP 0.9 request */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1437 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1438 | http_msg_req09_uri: |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1439 | msg->sl.rq.u = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1440 | http_msg_req09_uri_e: |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1441 | 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] | 1442 | http_msg_req09_ver: |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1443 | msg->sl.rq.v = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1444 | msg->sl.rq.v_l = 0; |
| 1445 | goto http_msg_rqline_eol; |
| 1446 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1447 | state = HTTP_MSG_ERROR; |
| 1448 | break; |
| 1449 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1450 | case HTTP_MSG_RQMETH_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1451 | http_msg_rqmeth_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1452 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1453 | msg->sl.rq.u = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1454 | goto http_msg_rquri; |
| 1455 | } |
| 1456 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1457 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1458 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1459 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1460 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1461 | case HTTP_MSG_RQURI: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1462 | http_msg_rquri: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1463 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1464 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1465 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1466 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1467 | 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] | 1468 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1469 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1470 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1471 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1472 | goto http_msg_req09_uri_e; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1473 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1474 | case HTTP_MSG_RQURI_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1475 | http_msg_rquri_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1476 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1477 | msg->sl.rq.v = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1478 | goto http_msg_rqver; |
| 1479 | } |
| 1480 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1481 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1482 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1483 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1484 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1485 | case HTTP_MSG_RQVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1486 | http_msg_rqver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1487 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1488 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1489 | |
| 1490 | if (likely(HTTP_IS_CRLF(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1491 | 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] | 1492 | http_msg_rqline_eol: |
| 1493 | /* We have seen the end of line. Note that we do not |
| 1494 | * necessarily have the \n yet, but at least we know that we |
| 1495 | * have EITHER \r OR \n, otherwise the request would not be |
| 1496 | * complete. We can then record the request length and return |
| 1497 | * to the caller which will be able to register it. |
| 1498 | */ |
| 1499 | msg->sl.rq.l = ptr - msg->sol; |
| 1500 | return ptr; |
| 1501 | } |
| 1502 | |
| 1503 | /* neither an HTTP_VER token nor a CRLF */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1504 | state = HTTP_MSG_ERROR; |
| 1505 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1506 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1507 | #ifdef DEBUG_FULL |
| 1508 | default: |
| 1509 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1510 | exit(1); |
| 1511 | #endif |
| 1512 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1513 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1514 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1515 | /* out of valid data */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1516 | if (ret_state) |
| 1517 | *ret_state = state; |
| 1518 | if (ret_ptr) |
| 1519 | *ret_ptr = (char *)ptr; |
| 1520 | return NULL; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1521 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1522 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1523 | /* |
| 1524 | * Returns the data from Authorization header. Function may be called more |
| 1525 | * than once so data is stored in txn->auth_data. When no header is found |
| 1526 | * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid |
| 1527 | * searching again for something we are unable to find anyway. |
| 1528 | */ |
| 1529 | |
| 1530 | char get_http_auth_buff[BUFSIZE]; |
| 1531 | |
| 1532 | int |
| 1533 | get_http_auth(struct session *s) |
| 1534 | { |
| 1535 | |
| 1536 | struct http_txn *txn = &s->txn; |
| 1537 | struct chunk auth_method; |
| 1538 | struct hdr_ctx ctx; |
| 1539 | char *h, *p; |
| 1540 | int len; |
| 1541 | |
| 1542 | #ifdef DEBUG_AUTH |
| 1543 | printf("Auth for session %p: %d\n", s, txn->auth.method); |
| 1544 | #endif |
| 1545 | |
| 1546 | if (txn->auth.method == HTTP_AUTH_WRONG) |
| 1547 | return 0; |
| 1548 | |
| 1549 | if (txn->auth.method) |
| 1550 | return 1; |
| 1551 | |
| 1552 | txn->auth.method = HTTP_AUTH_WRONG; |
| 1553 | |
| 1554 | ctx.idx = 0; |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 1555 | |
| 1556 | if (txn->flags & TX_USE_PX_CONN) { |
| 1557 | h = "Proxy-Authorization"; |
| 1558 | len = strlen(h); |
| 1559 | } else { |
| 1560 | h = "Authorization"; |
| 1561 | len = strlen(h); |
| 1562 | } |
| 1563 | |
| 1564 | 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] | 1565 | return 0; |
| 1566 | |
| 1567 | h = ctx.line + ctx.val; |
| 1568 | |
| 1569 | p = memchr(h, ' ', ctx.vlen); |
| 1570 | if (!p || p == h) |
| 1571 | return 0; |
| 1572 | |
| 1573 | chunk_initlen(&auth_method, h, 0, p-h); |
| 1574 | chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1); |
| 1575 | |
| 1576 | if (!strncasecmp("Basic", auth_method.str, auth_method.len)) { |
| 1577 | |
| 1578 | len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len, |
| 1579 | get_http_auth_buff, BUFSIZE - 1); |
| 1580 | |
| 1581 | if (len < 0) |
| 1582 | return 0; |
| 1583 | |
| 1584 | |
| 1585 | get_http_auth_buff[len] = '\0'; |
| 1586 | |
| 1587 | p = strchr(get_http_auth_buff, ':'); |
| 1588 | |
| 1589 | if (!p) |
| 1590 | return 0; |
| 1591 | |
| 1592 | txn->auth.user = get_http_auth_buff; |
| 1593 | *p = '\0'; |
| 1594 | txn->auth.pass = p+1; |
| 1595 | |
| 1596 | txn->auth.method = HTTP_AUTH_BASIC; |
| 1597 | return 1; |
| 1598 | } |
| 1599 | |
| 1600 | return 0; |
| 1601 | } |
| 1602 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1603 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1604 | /* |
| 1605 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1606 | * depending on the initial msg->msg_state. It can be preempted everywhere |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1607 | * when data are missing and recalled at the exact same location with no |
| 1608 | * information loss. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1609 | * 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] | 1610 | * fields. Note that msg->som and msg->sol will be initialized after completing |
| 1611 | * the first state, so that none of the msg pointers has to be initialized |
| 1612 | * prior to the first call. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1613 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1614 | void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx) |
| 1615 | { |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1616 | unsigned int state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1617 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1618 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1619 | state = msg->msg_state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1620 | ptr = buf->lr; |
| 1621 | end = buf->r; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1622 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1623 | if (unlikely(ptr >= end)) |
| 1624 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1625 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1626 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1627 | /* |
| 1628 | * First, states that are specific to the response only. |
| 1629 | * We check them first so that request and headers are |
| 1630 | * closer to each other (accessed more often). |
| 1631 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1632 | case HTTP_MSG_RPBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1633 | http_msg_rpbefore: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1634 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1635 | /* we have a start of message, but we have to check |
| 1636 | * first if we need to remove some CRLF. We can only |
| 1637 | * do this when send_max=0. |
| 1638 | */ |
| 1639 | char *beg = buf->w + buf->send_max; |
| 1640 | if (beg >= buf->data + buf->size) |
| 1641 | beg -= buf->size; |
| 1642 | if (unlikely(ptr != beg)) { |
| 1643 | if (buf->send_max) |
| 1644 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1645 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1646 | buffer_ignore(buf, ptr - beg); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1647 | } |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1648 | msg->som = ptr - buf->data; |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 1649 | msg->sol = ptr; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1650 | hdr_idx_init(idx); |
| 1651 | state = HTTP_MSG_RPVER; |
| 1652 | goto http_msg_rpver; |
| 1653 | } |
| 1654 | |
| 1655 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1656 | goto http_msg_invalid; |
| 1657 | |
| 1658 | if (unlikely(*ptr == '\n')) |
| 1659 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1660 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1661 | /* stop here */ |
| 1662 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1663 | case HTTP_MSG_RPBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1664 | http_msg_rpbefore_cr: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1665 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1666 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1667 | /* stop here */ |
| 1668 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1669 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1670 | http_msg_rpver: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1671 | case HTTP_MSG_RPVER_SP: |
| 1672 | case HTTP_MSG_RPCODE: |
| 1673 | case HTTP_MSG_RPCODE_SP: |
| 1674 | case HTTP_MSG_RPREASON: |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1675 | ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1676 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1677 | if (unlikely(!ptr)) |
| 1678 | return; |
| 1679 | |
| 1680 | /* we have a full response and we know that we have either a CR |
| 1681 | * or an LF at <ptr>. |
| 1682 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1683 | //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] | 1684 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1685 | |
| 1686 | msg->sol = ptr; |
| 1687 | if (likely(*ptr == '\r')) |
| 1688 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1689 | goto http_msg_rpline_end; |
| 1690 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1691 | case HTTP_MSG_RPLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1692 | http_msg_rpline_end: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1693 | /* msg->sol must point to the first of CR or LF. */ |
| 1694 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1695 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1696 | /* stop here */ |
| 1697 | |
| 1698 | /* |
| 1699 | * Second, states that are specific to the request only |
| 1700 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1701 | case HTTP_MSG_RQBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1702 | http_msg_rqbefore: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1703 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1704 | /* we have a start of message, but we have to check |
| 1705 | * first if we need to remove some CRLF. We can only |
| 1706 | * do this when send_max=0. |
| 1707 | */ |
| 1708 | char *beg = buf->w + buf->send_max; |
| 1709 | if (beg >= buf->data + buf->size) |
| 1710 | beg -= buf->size; |
| 1711 | if (likely(ptr != beg)) { |
| 1712 | if (buf->send_max) |
| 1713 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1714 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1715 | buffer_ignore(buf, ptr - beg); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1716 | } |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1717 | msg->som = ptr - buf->data; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1718 | msg->sol = ptr; |
Willy Tarreau | f0d058e | 2007-01-25 12:03:42 +0100 | [diff] [blame] | 1719 | /* we will need this when keep-alive will be supported |
| 1720 | hdr_idx_init(idx); |
| 1721 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1722 | state = HTTP_MSG_RQMETH; |
| 1723 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1724 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1725 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1726 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1727 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1728 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1729 | if (unlikely(*ptr == '\n')) |
| 1730 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1731 | 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] | 1732 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1733 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1734 | case HTTP_MSG_RQBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1735 | http_msg_rqbefore_cr: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1736 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1737 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1738 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1739 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1740 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1741 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1742 | case HTTP_MSG_RQMETH_SP: |
| 1743 | case HTTP_MSG_RQURI: |
| 1744 | case HTTP_MSG_RQURI_SP: |
| 1745 | case HTTP_MSG_RQVER: |
| 1746 | ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1747 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1748 | if (unlikely(!ptr)) |
| 1749 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1750 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1751 | /* we have a full request and we know that we have either a CR |
| 1752 | * or an LF at <ptr>. |
| 1753 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1754 | //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] | 1755 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1756 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1757 | msg->sol = ptr; |
| 1758 | if (likely(*ptr == '\r')) |
| 1759 | 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] | 1760 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1761 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1762 | case HTTP_MSG_RQLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1763 | http_msg_rqline_end: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1764 | /* check for HTTP/0.9 request : no version information available. |
| 1765 | * msg->sol must point to the first of CR or LF. |
| 1766 | */ |
| 1767 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1768 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1769 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1770 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1771 | 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] | 1772 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1773 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1774 | /* |
| 1775 | * Common states below |
| 1776 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1777 | case HTTP_MSG_HDR_FIRST: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1778 | http_msg_hdr_first: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1779 | msg->sol = ptr; |
| 1780 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1781 | goto http_msg_hdr_name; |
| 1782 | } |
| 1783 | |
| 1784 | if (likely(*ptr == '\r')) |
| 1785 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1786 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1787 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1788 | case HTTP_MSG_HDR_NAME: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1789 | http_msg_hdr_name: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1790 | /* assumes msg->sol points to the first char */ |
| 1791 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1792 | 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] | 1793 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1794 | if (likely(*ptr == ':')) { |
| 1795 | msg->col = ptr - buf->data; |
| 1796 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP); |
| 1797 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1798 | |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1799 | if (likely(msg->err_pos < -1) || *ptr == '\n') |
| 1800 | goto http_msg_invalid; |
| 1801 | |
| 1802 | if (msg->err_pos == -1) /* capture error pointer */ |
| 1803 | msg->err_pos = ptr - buf->data; /* >= 0 now */ |
| 1804 | |
| 1805 | /* and we still accept this non-token character */ |
| 1806 | 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] | 1807 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1808 | case HTTP_MSG_HDR_L1_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1809 | http_msg_hdr_l1_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1810 | /* assumes msg->sol points to the first char and msg->col to the colon */ |
| 1811 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1812 | 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] | 1813 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1814 | /* header value can be basically anything except CR/LF */ |
| 1815 | msg->sov = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1816 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1817 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1818 | goto http_msg_hdr_val; |
| 1819 | } |
| 1820 | |
| 1821 | if (likely(*ptr == '\r')) |
| 1822 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1823 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1824 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1825 | case HTTP_MSG_HDR_L1_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1826 | http_msg_hdr_l1_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1827 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1828 | 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] | 1829 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1830 | case HTTP_MSG_HDR_L1_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1831 | http_msg_hdr_l1_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1832 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1833 | /* replace HT,CR,LF with spaces */ |
| 1834 | for (; buf->data+msg->sov < ptr; msg->sov++) |
| 1835 | buf->data[msg->sov] = ' '; |
| 1836 | goto http_msg_hdr_l1_sp; |
| 1837 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1838 | /* we had a header consisting only in spaces ! */ |
| 1839 | msg->eol = buf->data + msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1840 | goto http_msg_complete_header; |
| 1841 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1842 | case HTTP_MSG_HDR_VAL: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1843 | http_msg_hdr_val: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1844 | /* assumes msg->sol points to the first char, msg->col to the |
| 1845 | * colon, and msg->sov points to the first character of the |
| 1846 | * value. |
| 1847 | */ |
| 1848 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1849 | 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] | 1850 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1851 | msg->eol = ptr; |
| 1852 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1853 | * real header end in case it ends with lots of LWS, but is this |
| 1854 | * really needed ? |
| 1855 | */ |
| 1856 | if (likely(*ptr == '\r')) |
| 1857 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1858 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1859 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1860 | case HTTP_MSG_HDR_L2_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1861 | http_msg_hdr_l2_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1862 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1863 | 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] | 1864 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1865 | case HTTP_MSG_HDR_L2_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1866 | http_msg_hdr_l2_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1867 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1868 | /* LWS: replace HT,CR,LF with spaces */ |
| 1869 | for (; msg->eol < ptr; msg->eol++) |
| 1870 | *msg->eol = ' '; |
| 1871 | goto http_msg_hdr_val; |
| 1872 | } |
| 1873 | http_msg_complete_header: |
| 1874 | /* |
| 1875 | * It was a new header, so the last one is finished. |
| 1876 | * Assumes msg->sol points to the first char, msg->col to the |
| 1877 | * colon, msg->sov points to the first character of the value |
| 1878 | * and msg->eol to the first CR or LF so we know how the line |
| 1879 | * ends. We insert last header into the index. |
| 1880 | */ |
| 1881 | /* |
| 1882 | fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol); |
| 1883 | write(2, msg->sol, msg->eol-msg->sol); |
| 1884 | fprintf(stderr,"\n"); |
| 1885 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1886 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1887 | if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r', |
| 1888 | idx, idx->tail) < 0)) |
| 1889 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1890 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1891 | msg->sol = ptr; |
| 1892 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1893 | goto http_msg_hdr_name; |
| 1894 | } |
| 1895 | |
| 1896 | if (likely(*ptr == '\r')) |
| 1897 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1898 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1899 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1900 | case HTTP_MSG_LAST_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1901 | http_msg_last_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1902 | /* Assumes msg->sol points to the first of either CR or LF */ |
| 1903 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1904 | ptr++; |
| 1905 | buf->lr = ptr; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1906 | msg->col = msg->sov = buf->lr - buf->data; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1907 | msg->eoh = msg->sol - buf->data; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1908 | msg->sol = buf->data + msg->som; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1909 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1910 | return; |
| 1911 | #ifdef DEBUG_FULL |
| 1912 | default: |
| 1913 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1914 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1915 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1916 | } |
| 1917 | http_msg_ood: |
| 1918 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1919 | msg->msg_state = state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1920 | buf->lr = ptr; |
| 1921 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1922 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1923 | http_msg_invalid: |
| 1924 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1925 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1926 | buf->lr = ptr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1927 | return; |
| 1928 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1929 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1930 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 1931 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 1932 | * nothing is done and 1 is returned. |
| 1933 | */ |
| 1934 | static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn) |
| 1935 | { |
| 1936 | int delta; |
| 1937 | char *cur_end; |
| 1938 | |
| 1939 | if (msg->sl.rq.v_l != 0) |
| 1940 | return 1; |
| 1941 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1942 | cur_end = msg->sol + msg->sl.rq.l; |
| 1943 | delta = 0; |
| 1944 | |
| 1945 | if (msg->sl.rq.u_l == 0) { |
| 1946 | /* if no URI was set, add "/" */ |
| 1947 | delta = buffer_replace2(req, cur_end, cur_end, " /", 2); |
| 1948 | cur_end += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1949 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1950 | } |
| 1951 | /* add HTTP version */ |
| 1952 | 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] | 1953 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1954 | cur_end += delta; |
| 1955 | cur_end = (char *)http_parse_reqline(msg, req->data, |
| 1956 | HTTP_MSG_RQMETH, |
| 1957 | msg->sol, cur_end + 1, |
| 1958 | NULL, NULL); |
| 1959 | if (unlikely(!cur_end)) |
| 1960 | return 0; |
| 1961 | |
| 1962 | /* we have a full HTTP/1.0 request now and we know that |
| 1963 | * we have either a CR or an LF at <ptr>. |
| 1964 | */ |
| 1965 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 1966 | return 1; |
| 1967 | } |
| 1968 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1969 | /* Parse the Connection: header of an HTTP request, looking for both "close" |
| 1970 | * and "keep-alive" values. If a buffer is provided and we already know that |
| 1971 | * some headers may safely be removed, we remove them now. The <to_del> flags |
| 1972 | * are used for that : |
| 1973 | * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses) |
| 1974 | * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1). |
| 1975 | * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was |
| 1976 | * found, and TX_CON_*_SET is adjusted depending on what is left so only |
| 1977 | * harmless combinations may be removed. Do not call that after changes have |
| 1978 | * been processed. If unused, the buffer can be NULL, and no data will be |
| 1979 | * changed. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1980 | */ |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1981 | 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] | 1982 | { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1983 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1984 | const char *hdr_val = "Connection"; |
| 1985 | int hdr_len = 10; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1986 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1987 | if (txn->flags & TX_HDR_CONN_PRS) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1988 | return; |
| 1989 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1990 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1991 | hdr_val = "Proxy-Connection"; |
| 1992 | hdr_len = 16; |
| 1993 | } |
| 1994 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1995 | ctx.idx = 0; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1996 | txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET); |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1997 | 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] | 1998 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1999 | txn->flags |= TX_HDR_CONN_KAL; |
| 2000 | if ((to_del & 2) && buf) |
| 2001 | http_remove_header2(msg, buf, &txn->hdr_idx, &ctx); |
| 2002 | else |
| 2003 | txn->flags |= TX_CON_KAL_SET; |
| 2004 | } |
| 2005 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 2006 | txn->flags |= TX_HDR_CONN_CLO; |
| 2007 | if ((to_del & 1) && buf) |
| 2008 | http_remove_header2(msg, buf, &txn->hdr_idx, &ctx); |
| 2009 | else |
| 2010 | txn->flags |= TX_CON_CLO_SET; |
| 2011 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2012 | } |
| 2013 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2014 | txn->flags |= TX_HDR_CONN_PRS; |
| 2015 | return; |
| 2016 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2017 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2018 | /* Apply desired changes on the Connection: header. Values may be removed and/or |
| 2019 | * added depending on the <wanted> flags, which are exclusively composed of |
| 2020 | * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The |
| 2021 | * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left. |
| 2022 | */ |
| 2023 | void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, int wanted) |
| 2024 | { |
| 2025 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2026 | const char *hdr_val = "Connection"; |
| 2027 | int hdr_len = 10; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2028 | |
| 2029 | ctx.idx = 0; |
| 2030 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2031 | |
| 2032 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 2033 | hdr_val = "Proxy-Connection"; |
| 2034 | hdr_len = 16; |
| 2035 | } |
| 2036 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2037 | txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET); |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2038 | 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] | 2039 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 2040 | if (wanted & TX_CON_KAL_SET) |
| 2041 | txn->flags |= TX_CON_KAL_SET; |
| 2042 | else |
| 2043 | http_remove_header2(msg, buf, &txn->hdr_idx, &ctx); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2044 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2045 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 2046 | if (wanted & TX_CON_CLO_SET) |
| 2047 | txn->flags |= TX_CON_CLO_SET; |
| 2048 | else |
| 2049 | http_remove_header2(msg, buf, &txn->hdr_idx, &ctx); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2050 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2051 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2052 | |
| 2053 | if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 2054 | return; |
| 2055 | |
| 2056 | if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) { |
| 2057 | txn->flags |= TX_CON_CLO_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2058 | hdr_val = "Connection: close"; |
| 2059 | hdr_len = 17; |
| 2060 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 2061 | hdr_val = "Proxy-Connection: close"; |
| 2062 | hdr_len = 23; |
| 2063 | } |
| 2064 | 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] | 2065 | } |
| 2066 | |
| 2067 | if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) { |
| 2068 | txn->flags |= TX_CON_KAL_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2069 | hdr_val = "Connection: keep-alive"; |
| 2070 | hdr_len = 22; |
| 2071 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 2072 | hdr_val = "Proxy-Connection: keep-alive"; |
| 2073 | hdr_len = 28; |
| 2074 | } |
| 2075 | 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] | 2076 | } |
| 2077 | return; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2078 | } |
| 2079 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2080 | /* Parse the chunk size at buf->lr. Once done, it adjusts ->lr to point to the |
| 2081 | * first byte of body, and increments msg->sov by the number of bytes parsed, |
| 2082 | * so that we know we can forward between ->som and ->sov. Note that due to |
| 2083 | * possible wrapping at the end of the buffer, it is possible that msg->sov is |
| 2084 | * lower than msg->som. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2085 | * 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] | 2086 | * 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] | 2087 | */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2088 | int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2089 | { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2090 | char *ptr = buf->lr; |
| 2091 | char *end = buf->data + buf->size; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2092 | unsigned int chunk = 0; |
| 2093 | |
| 2094 | /* The chunk size is in the following form, though we are only |
| 2095 | * interested in the size and CRLF : |
| 2096 | * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF |
| 2097 | */ |
| 2098 | while (1) { |
| 2099 | int c; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2100 | if (ptr == buf->r) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2101 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2102 | c = hex2i(*ptr); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2103 | if (c < 0) /* not a hex digit anymore */ |
| 2104 | break; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2105 | if (++ptr >= end) |
| 2106 | ptr = buf->data; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2107 | if (chunk & 0xF000000) /* overflow will occur */ |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2108 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2109 | chunk = (chunk << 4) + c; |
| 2110 | } |
| 2111 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2112 | /* empty size not allowed */ |
| 2113 | if (ptr == buf->lr) |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2114 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2115 | |
| 2116 | while (http_is_spht[(unsigned char)*ptr]) { |
| 2117 | if (++ptr >= end) |
| 2118 | ptr = buf->data; |
| 2119 | if (ptr == buf->r) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2120 | return 0; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2121 | } |
| 2122 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2123 | /* Up to there, we know that at least one byte is present at *ptr. Check |
| 2124 | * for the end of chunk size. |
| 2125 | */ |
| 2126 | while (1) { |
| 2127 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 2128 | /* we now have a CR or an LF at ptr */ |
| 2129 | if (likely(*ptr == '\r')) { |
| 2130 | if (++ptr >= end) |
| 2131 | ptr = buf->data; |
| 2132 | if (ptr == buf->r) |
| 2133 | return 0; |
| 2134 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2135 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2136 | if (*ptr != '\n') |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2137 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2138 | if (++ptr >= end) |
| 2139 | ptr = buf->data; |
| 2140 | /* done */ |
| 2141 | break; |
| 2142 | } |
| 2143 | else if (*ptr == ';') { |
| 2144 | /* chunk extension, ends at next CRLF */ |
| 2145 | if (++ptr >= end) |
| 2146 | ptr = buf->data; |
| 2147 | if (ptr == buf->r) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2148 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2149 | |
| 2150 | while (!HTTP_IS_CRLF(*ptr)) { |
| 2151 | if (++ptr >= end) |
| 2152 | ptr = buf->data; |
| 2153 | if (ptr == buf->r) |
| 2154 | return 0; |
| 2155 | } |
| 2156 | /* we have a CRLF now, loop above */ |
| 2157 | continue; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2158 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2159 | else |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2160 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2161 | } |
| 2162 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2163 | /* OK we found our CRLF and now <ptr> points to the next byte, |
| 2164 | * which may or may not be present. We save that into ->lr and |
| 2165 | * ->sov. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2166 | */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2167 | msg->sov += ptr - buf->lr; |
| 2168 | buf->lr = ptr; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2169 | msg->chunk_len = chunk; |
| 2170 | msg->body_len += chunk; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2171 | msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2172 | return 1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2173 | error: |
| 2174 | msg->err_pos = ptr - buf->data; |
| 2175 | return -1; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2176 | } |
| 2177 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2178 | /* This function skips trailers in the buffer <buf> associated with HTTP |
| 2179 | * message <msg>. The first visited position is buf->lr. If the end of |
| 2180 | * the trailers is found, it is automatically scheduled to be forwarded, |
| 2181 | * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0. |
| 2182 | * If not enough data are available, the function does not change anything |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2183 | * except maybe buf->lr and msg->sov if it could parse some lines, and returns |
| 2184 | * zero. If a parse error is encountered, the function returns < 0 and does not |
| 2185 | * change anything except maybe buf->lr and msg->sov. Note that the message |
| 2186 | * must already be in HTTP_MSG_TRAILERS state before calling this function, |
| 2187 | * which implies that all non-trailers data have already been scheduled for |
| 2188 | * forwarding, and that the difference between msg->som and msg->sov exactly |
| 2189 | * matches the length of trailers already parsed and not forwarded. It is also |
| 2190 | * important to note that this function is designed to be able to parse wrapped |
| 2191 | * headers at end of buffer. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2192 | */ |
| 2193 | int http_forward_trailers(struct buffer *buf, struct http_msg *msg) |
| 2194 | { |
| 2195 | /* we have buf->lr which points to next line. Look for CRLF. */ |
| 2196 | while (1) { |
| 2197 | char *p1 = NULL, *p2 = NULL; |
| 2198 | char *ptr = buf->lr; |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2199 | int bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2200 | |
| 2201 | /* scan current line and stop at LF or CRLF */ |
| 2202 | while (1) { |
| 2203 | if (ptr == buf->r) |
| 2204 | return 0; |
| 2205 | |
| 2206 | if (*ptr == '\n') { |
| 2207 | if (!p1) |
| 2208 | p1 = ptr; |
| 2209 | p2 = ptr; |
| 2210 | break; |
| 2211 | } |
| 2212 | |
| 2213 | if (*ptr == '\r') { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2214 | if (p1) { |
| 2215 | msg->err_pos = ptr - buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2216 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2217 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2218 | p1 = ptr; |
| 2219 | } |
| 2220 | |
| 2221 | ptr++; |
| 2222 | if (ptr >= buf->data + buf->size) |
| 2223 | ptr = buf->data; |
| 2224 | } |
| 2225 | |
| 2226 | /* after LF; point to beginning of next line */ |
| 2227 | p2++; |
| 2228 | if (p2 >= buf->data + buf->size) |
| 2229 | p2 = buf->data; |
| 2230 | |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2231 | bytes = p2 - buf->lr; |
| 2232 | if (bytes < 0) |
| 2233 | bytes += buf->size; |
| 2234 | |
| 2235 | /* schedule this line for forwarding */ |
| 2236 | msg->sov += bytes; |
| 2237 | if (msg->sov >= buf->size) |
| 2238 | msg->sov -= buf->size; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2239 | |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2240 | if (p1 == buf->lr) { |
| 2241 | /* LF/CRLF at beginning of line => end of trailers at p2. |
| 2242 | * Everything was scheduled for forwarding, there's nothing |
| 2243 | * left from this message. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 2244 | */ |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2245 | buf->lr = p2; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2246 | msg->msg_state = HTTP_MSG_DONE; |
| 2247 | return 1; |
| 2248 | } |
| 2249 | /* OK, next line then */ |
| 2250 | buf->lr = p2; |
| 2251 | } |
| 2252 | } |
| 2253 | |
| 2254 | /* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or |
| 2255 | * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov, |
| 2256 | * ->som, buf->lr in order to include this part into the next forwarding phase. |
| 2257 | * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If |
| 2258 | * not enough data are available, the function does not change anything and |
| 2259 | * returns zero. If a parse error is encountered, the function returns < 0 and |
| 2260 | * does not change anything. Note: this function is designed to parse wrapped |
| 2261 | * CRLF at the end of the buffer. |
| 2262 | */ |
| 2263 | int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg) |
| 2264 | { |
| 2265 | char *ptr; |
| 2266 | int bytes; |
| 2267 | |
| 2268 | /* NB: we'll check data availabilty at the end. It's not a |
| 2269 | * problem because whatever we match first will be checked |
| 2270 | * against the correct length. |
| 2271 | */ |
| 2272 | bytes = 1; |
| 2273 | ptr = buf->lr; |
| 2274 | if (*ptr == '\r') { |
| 2275 | bytes++; |
| 2276 | ptr++; |
| 2277 | if (ptr >= buf->data + buf->size) |
| 2278 | ptr = buf->data; |
| 2279 | } |
| 2280 | |
Willy Tarreau | bf3f1de | 2010-03-17 15:54:24 +0100 | [diff] [blame] | 2281 | if (bytes > buf->l - buf->send_max) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2282 | return 0; |
| 2283 | |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2284 | if (*ptr != '\n') { |
| 2285 | msg->err_pos = ptr - buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2286 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2287 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2288 | |
| 2289 | ptr++; |
| 2290 | if (ptr >= buf->data + buf->size) |
| 2291 | ptr = buf->data; |
| 2292 | buf->lr = ptr; |
| 2293 | /* prepare the CRLF to be forwarded. msg->som may be before data but we don't care */ |
| 2294 | msg->sov = ptr - buf->data; |
| 2295 | msg->som = msg->sov - bytes; |
| 2296 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 2297 | return 1; |
| 2298 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2299 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2300 | void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg) |
| 2301 | { |
| 2302 | char *end = buf->data + buf->size; |
| 2303 | int off = buf->data + buf->size - buf->w; |
| 2304 | |
| 2305 | /* two possible cases : |
| 2306 | * - the buffer is in one contiguous block, we move it in-place |
Willy Tarreau | 8096de9 | 2010-02-26 11:12:27 +0100 | [diff] [blame] | 2307 | * - 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] | 2308 | */ |
| 2309 | if (buf->l) { |
Willy Tarreau | 8096de9 | 2010-02-26 11:12:27 +0100 | [diff] [blame] | 2310 | int block1 = buf->l; |
| 2311 | int block2 = 0; |
| 2312 | if (buf->r <= buf->w) { |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2313 | /* non-contiguous block */ |
Willy Tarreau | 8096de9 | 2010-02-26 11:12:27 +0100 | [diff] [blame] | 2314 | block1 = buf->data + buf->size - buf->w; |
| 2315 | block2 = buf->r - buf->data; |
| 2316 | } |
| 2317 | if (block2) |
| 2318 | memcpy(swap_buffer, buf->data, block2); |
| 2319 | memmove(buf->data, buf->w, block1); |
| 2320 | if (block2) |
| 2321 | memcpy(buf->data + block1, swap_buffer, block2); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2322 | } |
| 2323 | |
| 2324 | /* adjust all known pointers */ |
| 2325 | buf->w = buf->data; |
| 2326 | buf->lr += off; if (buf->lr >= end) buf->lr -= buf->size; |
| 2327 | buf->r += off; if (buf->r >= end) buf->r -= buf->size; |
| 2328 | msg->sol += off; if (msg->sol >= end) msg->sol -= buf->size; |
| 2329 | msg->eol += off; if (msg->eol >= end) msg->eol -= buf->size; |
| 2330 | |
| 2331 | /* adjust relative pointers */ |
| 2332 | msg->som = 0; |
| 2333 | msg->eoh += off; if (msg->eoh >= buf->size) msg->eoh -= buf->size; |
| 2334 | msg->col += off; if (msg->col >= buf->size) msg->col -= buf->size; |
| 2335 | msg->sov += off; if (msg->sov >= buf->size) msg->sov -= buf->size; |
| 2336 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2337 | if (msg->err_pos >= 0) { |
| 2338 | msg->err_pos += off; |
| 2339 | if (msg->err_pos >= buf->size) |
| 2340 | msg->err_pos -= buf->size; |
| 2341 | } |
| 2342 | |
| 2343 | buf->flags &= ~BF_FULL; |
| 2344 | if (buf->l >= buffer_max_len(buf)) |
| 2345 | buf->flags |= BF_FULL; |
| 2346 | } |
| 2347 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2348 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 2349 | * processing can continue on next analysers, or zero if it either needs more |
| 2350 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
| 2351 | * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers |
| 2352 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 2353 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2354 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2355 | 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] | 2356 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2357 | /* |
| 2358 | * We will parse the partial (or complete) lines. |
| 2359 | * We will check the request syntax, and also join multi-line |
| 2360 | * headers. An index of all the lines will be elaborated while |
| 2361 | * parsing. |
| 2362 | * |
| 2363 | * For the parsing, we use a 28 states FSM. |
| 2364 | * |
| 2365 | * Here is the information we currently have : |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 2366 | * req->data + msg->som = beginning of request |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2367 | * req->data + msg->eoh = end of processed headers / start of current one |
| 2368 | * msg->eol = end of current header or line (LF or CRLF) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2369 | * req->lr = first non-visited byte |
| 2370 | * req->r = end of data |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2371 | * |
| 2372 | * At end of parsing, we may perform a capture of the error (if any), and |
| 2373 | * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE). |
| 2374 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 2375 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2376 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2377 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2378 | int cur_idx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2379 | int use_close_only; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2380 | struct http_txn *txn = &s->txn; |
| 2381 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2382 | struct hdr_ctx ctx; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2383 | |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 2384 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 2385 | now_ms, __FUNCTION__, |
| 2386 | s, |
| 2387 | req, |
| 2388 | req->rex, req->wex, |
| 2389 | req->flags, |
| 2390 | req->l, |
| 2391 | req->analysers); |
| 2392 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 2393 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 2394 | s->srv_error = http_return_srv_error; |
| 2395 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2396 | /* There's a protected area at the end of the buffer for rewriting |
| 2397 | * purposes. We don't want to start to parse the request if the |
| 2398 | * protected area is affected, because we may have to move processed |
| 2399 | * data later, which is much more complicated. |
| 2400 | */ |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2401 | if (req->l && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2402 | if ((txn->flags & TX_NOT_FIRST) && |
| 2403 | unlikely((req->flags & BF_FULL) || |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2404 | req->r < req->lr || |
| 2405 | req->r > req->data + req->size - global.tune.maxrewrite)) { |
| 2406 | if (req->send_max) { |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 2407 | if (req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) |
| 2408 | goto failed_keep_alive; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2409 | /* some data has still not left the buffer, wake us once that's done */ |
| 2410 | buffer_dont_connect(req); |
| 2411 | req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 2412 | return 0; |
| 2413 | } |
Willy Tarreau | 0499e35 | 2010-12-17 07:13:42 +0100 | [diff] [blame] | 2414 | 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] | 2415 | http_buffer_heavy_realign(req, msg); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2416 | } |
| 2417 | |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2418 | /* Note that we have the same problem with the response ; we |
| 2419 | * may want to send a redirect, error or anything which requires |
| 2420 | * some spare space. So we'll ensure that we have at least |
| 2421 | * maxrewrite bytes available in the response buffer before |
| 2422 | * processing that one. This will only affect pipelined |
| 2423 | * keep-alive requests. |
| 2424 | */ |
| 2425 | if ((txn->flags & TX_NOT_FIRST) && |
| 2426 | unlikely((s->rep->flags & BF_FULL) || |
| 2427 | s->rep->r < s->rep->lr || |
| 2428 | s->rep->r > s->rep->data + s->rep->size - global.tune.maxrewrite)) { |
| 2429 | if (s->rep->send_max) { |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 2430 | if (s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) |
| 2431 | goto failed_keep_alive; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2432 | /* don't let a connection request be initiated */ |
| 2433 | buffer_dont_connect(req); |
Willy Tarreau | ff7b588 | 2010-01-22 14:41:29 +0100 | [diff] [blame] | 2434 | s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 0499e35 | 2010-12-17 07:13:42 +0100 | [diff] [blame] | 2435 | s->rep->analysers |= an_bit; /* wake us up once it changes */ |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2436 | return 0; |
| 2437 | } |
| 2438 | } |
| 2439 | |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2440 | if (likely(req->lr < req->r)) |
| 2441 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2442 | } |
| 2443 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2444 | /* 1: we might have to print this header in debug mode */ |
| 2445 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2446 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | c3bfeeb | 2010-04-16 09:14:45 +0200 | [diff] [blame] | 2447 | msg->sol && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2448 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2449 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2450 | |
Willy Tarreau | 663308b | 2010-06-07 14:06:08 +0200 | [diff] [blame] | 2451 | sol = req->data + msg->som; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2452 | eol = sol + msg->sl.rq.l; |
| 2453 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2454 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2455 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2456 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2457 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2458 | while (cur_idx) { |
| 2459 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2460 | debug_hdr("clihdr", s, sol, eol); |
| 2461 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2462 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2463 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2464 | } |
| 2465 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2466 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2467 | /* |
| 2468 | * Now we quickly check if we have found a full valid request. |
| 2469 | * If not so, we check the FD and buffer states before leaving. |
| 2470 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2471 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2472 | * requests are checked first. When waiting for a second request |
| 2473 | * on a keep-alive session, if we encounter and error, close, t/o, |
| 2474 | * we note the error in the session flags but don't set any state. |
| 2475 | * Since the error will be noted there, it will not be counted by |
| 2476 | * process_session() as a frontend error. |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2477 | * Last, we may increase some tracked counters' http request errors on |
| 2478 | * the cases that are deliberately the client's fault. For instance, |
| 2479 | * a timeout or connection reset is not counted as an error. However |
| 2480 | * a bad request is. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2481 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2482 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2483 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2484 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2485 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2486 | */ |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2487 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2488 | session_inc_http_req_ctr(s); |
| 2489 | session_inc_http_err_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2490 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2491 | goto return_bad_req; |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2492 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2493 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2494 | /* 1: Since we are in header mode, if there's no space |
| 2495 | * left for headers, we won't be able to free more |
| 2496 | * later, so the session will never terminate. We |
| 2497 | * must terminate it now. |
| 2498 | */ |
| 2499 | if (unlikely(req->flags & BF_FULL)) { |
| 2500 | /* FIXME: check if URI is set and return Status |
| 2501 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2502 | */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2503 | session_inc_http_req_ctr(s); |
| 2504 | session_inc_http_err_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2505 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2506 | goto return_bad_req; |
| 2507 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2508 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2509 | /* 2: have we encountered a read error ? */ |
| 2510 | else if (req->flags & BF_READ_ERROR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2511 | if (!(s->flags & SN_ERR_MASK)) |
| 2512 | s->flags |= SN_ERR_CLICL; |
| 2513 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2514 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2515 | goto failed_keep_alive; |
| 2516 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2517 | /* we cannot return any message on error */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2518 | if (msg->err_pos >= 0) { |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 2519 | 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] | 2520 | session_inc_http_err_ctr(s); |
| 2521 | } |
| 2522 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2523 | msg->msg_state = HTTP_MSG_ERROR; |
| 2524 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2525 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2526 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2527 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2528 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2529 | if (s->listener->counters) |
| 2530 | s->listener->counters->failed_req++; |
| 2531 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2532 | if (!(s->flags & SN_FINST_MASK)) |
| 2533 | s->flags |= SN_FINST_R; |
| 2534 | return 0; |
| 2535 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2536 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2537 | /* 3: has the read timeout expired ? */ |
| 2538 | 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] | 2539 | if (!(s->flags & SN_ERR_MASK)) |
| 2540 | s->flags |= SN_ERR_CLITO; |
| 2541 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2542 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2543 | goto failed_keep_alive; |
| 2544 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2545 | /* read timeout : give up with an error message. */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2546 | if (msg->err_pos >= 0) { |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 2547 | 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] | 2548 | session_inc_http_err_ctr(s); |
| 2549 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2550 | txn->status = 408; |
| 2551 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408)); |
| 2552 | msg->msg_state = HTTP_MSG_ERROR; |
| 2553 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2554 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2555 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2556 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2557 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2558 | if (s->listener->counters) |
| 2559 | s->listener->counters->failed_req++; |
| 2560 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2561 | if (!(s->flags & SN_FINST_MASK)) |
| 2562 | s->flags |= SN_FINST_R; |
| 2563 | return 0; |
| 2564 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2565 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2566 | /* 4: have we encountered a close ? */ |
| 2567 | else if (req->flags & BF_SHUTR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2568 | if (!(s->flags & SN_ERR_MASK)) |
| 2569 | s->flags |= SN_ERR_CLICL; |
| 2570 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2571 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2572 | goto failed_keep_alive; |
| 2573 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2574 | if (msg->err_pos >= 0) |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 2575 | 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] | 2576 | txn->status = 400; |
| 2577 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
| 2578 | msg->msg_state = HTTP_MSG_ERROR; |
| 2579 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2580 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2581 | session_inc_http_err_ctr(s); |
| 2582 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2583 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2584 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2585 | if (s->listener->counters) |
| 2586 | s->listener->counters->failed_req++; |
| 2587 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2588 | if (!(s->flags & SN_FINST_MASK)) |
| 2589 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2590 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2591 | } |
| 2592 | |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2593 | buffer_dont_connect(req); |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2594 | req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | ff7b588 | 2010-01-22 14:41:29 +0100 | [diff] [blame] | 2595 | s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2596 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2597 | if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) { |
| 2598 | /* If the client starts to talk, let's fall back to |
| 2599 | * request timeout processing. |
| 2600 | */ |
| 2601 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2602 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2603 | } |
| 2604 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2605 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2606 | if (!tick_isset(req->analyse_exp)) { |
| 2607 | if ((msg->msg_state == HTTP_MSG_RQBEFORE) && |
| 2608 | (txn->flags & TX_WAIT_NEXT_RQ) && |
| 2609 | tick_isset(s->be->timeout.httpka)) |
| 2610 | req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka); |
| 2611 | else |
| 2612 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
| 2613 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2614 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2615 | /* we're not ready yet */ |
| 2616 | return 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2617 | |
| 2618 | failed_keep_alive: |
| 2619 | /* Here we process low-level errors for keep-alive requests. In |
| 2620 | * short, if the request is not the first one and it experiences |
| 2621 | * a timeout, read error or shutdown, we just silently close so |
| 2622 | * that the client can try again. |
| 2623 | */ |
| 2624 | txn->status = 0; |
| 2625 | msg->msg_state = HTTP_MSG_RQBEFORE; |
| 2626 | req->analysers = 0; |
| 2627 | s->logs.logwait = 0; |
Willy Tarreau | ff7b588 | 2010-01-22 14:41:29 +0100 | [diff] [blame] | 2628 | s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 2629 | stream_int_retnclose(req->prod, NULL); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2630 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2631 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2632 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2633 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 2634 | * complete the request parsing by setting a few fields we will need |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 2635 | * later. At this point, we have the last CRLF at req->data + msg->eoh. |
| 2636 | * If the request is in HTTP/0.9 form, the rule is still true, and eoh |
| 2637 | * points to the CRLF of the request line. req->lr points to the first |
| 2638 | * byte after the last LF. msg->col and msg->sov point to the first |
| 2639 | * byte of data. msg->eol cannot be trusted because it may have been |
| 2640 | * left uninitialized (for instance in the absence of headers). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2641 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2642 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2643 | session_inc_http_req_ctr(s); |
Willy Tarreau | d9b587f | 2010-02-26 10:05:55 +0100 | [diff] [blame] | 2644 | proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */ |
| 2645 | |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2646 | if (txn->flags & TX_WAIT_NEXT_RQ) { |
| 2647 | /* kill the pending keep-alive timeout */ |
| 2648 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 2649 | req->analyse_exp = TICK_ETERNITY; |
| 2650 | } |
| 2651 | |
| 2652 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2653 | /* Maybe we found in invalid header name while we were configured not |
| 2654 | * to block on that, so we have to capture it now. |
| 2655 | */ |
| 2656 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 2657 | 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] | 2658 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2659 | /* |
| 2660 | * 1: identify the method |
| 2661 | */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2662 | txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2663 | |
| 2664 | /* we can make use of server redirect on GET and HEAD */ |
| 2665 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 2666 | s->flags |= SN_REDIRECTABLE; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2667 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2668 | /* |
| 2669 | * 2: check if the URI matches the monitor_uri. |
| 2670 | * We have to do this for every request which gets in, because |
| 2671 | * the monitor-uri is defined by the frontend. |
| 2672 | */ |
| 2673 | if (unlikely((s->fe->monitor_uri_len != 0) && |
| 2674 | (s->fe->monitor_uri_len == msg->sl.rq.u_l) && |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2675 | !memcmp(msg->sol + msg->sl.rq.u, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2676 | s->fe->monitor_uri, |
| 2677 | s->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2678 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2679 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2680 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2681 | struct acl_cond *cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2682 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2683 | s->flags |= SN_MONITOR; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2684 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2685 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2686 | list_for_each_entry(cond, &s->fe->mon_fail_cond, list) { |
| 2687 | 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] | 2688 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2689 | ret = acl_pass(ret); |
| 2690 | if (cond->pol == ACL_COND_UNLESS) |
| 2691 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2692 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2693 | if (ret) { |
| 2694 | /* we fail this request, let's return 503 service unavail */ |
| 2695 | txn->status = 503; |
| 2696 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503)); |
| 2697 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2698 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2699 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 2700 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2701 | /* nothing to fail, let's reply normaly */ |
| 2702 | txn->status = 200; |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 2703 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_200)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2704 | goto return_prx_cond; |
| 2705 | } |
| 2706 | |
| 2707 | /* |
| 2708 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 2709 | * Note: we cannot log anymore if the request has been |
| 2710 | * classified as invalid. |
| 2711 | */ |
| 2712 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 2713 | /* we have a complete HTTP request that we must log */ |
| 2714 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
| 2715 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2716 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2717 | if (urilen >= REQURI_LEN) |
| 2718 | urilen = REQURI_LEN - 1; |
| 2719 | memcpy(txn->uri, &req->data[msg->som], urilen); |
| 2720 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2721 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2722 | if (!(s->logs.logwait &= ~LW_REQ)) |
| 2723 | s->do_log(s); |
| 2724 | } else { |
| 2725 | Alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2726 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2727 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2728 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2729 | /* 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] | 2730 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) |
| 2731 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2732 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2733 | /* ... and check if the request is HTTP/1.1 or above */ |
| 2734 | if ((msg->sl.rq.v_l == 8) && |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2735 | ((msg->sol[msg->sl.rq.v + 5] > '1') || |
| 2736 | ((msg->sol[msg->sl.rq.v + 5] == '1') && |
| 2737 | (msg->sol[msg->sl.rq.v + 7] >= '1')))) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2738 | txn->flags |= TX_REQ_VER_11; |
| 2739 | |
| 2740 | /* "connection" has not been parsed yet */ |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2741 | 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] | 2742 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2743 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 2744 | * we have what looks like a proxied connection instead of a connection, |
| 2745 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 2746 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 2747 | * happen to do that despite being non-standard :-( |
| 2748 | * We consider that a request not beginning with either '/' or '*' is |
| 2749 | * a proxied connection, which covers both "scheme://location" and |
| 2750 | * CONNECT ip:port. |
| 2751 | */ |
| 2752 | if ((s->fe->options2 & PR_O2_USE_PXHDR) && |
| 2753 | msg->sol[msg->sl.rq.u] != '/' && msg->sol[msg->sl.rq.u] != '*') |
| 2754 | txn->flags |= TX_USE_PX_CONN; |
| 2755 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2756 | /* transfer length unknown*/ |
| 2757 | txn->flags &= ~TX_REQ_XFER_LEN; |
| 2758 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2759 | /* 5: we may need to capture headers */ |
| 2760 | if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap)) |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2761 | capture_headers(msg->sol, &txn->hdr_idx, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2762 | txn->req.cap, s->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2763 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2764 | /* 6: determine the transfer-length. |
| 2765 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 2766 | * the presence of a message-body in a REQUEST and its transfer length |
| 2767 | * must be determined that way (in order of precedence) : |
| 2768 | * 1. The presence of a message-body in a request is signaled by the |
| 2769 | * inclusion of a Content-Length or Transfer-Encoding header field |
| 2770 | * in the request's header fields. When a request message contains |
| 2771 | * both a message-body of non-zero length and a method that does |
| 2772 | * not define any semantics for that request message-body, then an |
| 2773 | * origin server SHOULD either ignore the message-body or respond |
| 2774 | * with an appropriate error message (e.g., 413). A proxy or |
| 2775 | * gateway, when presented the same request, SHOULD either forward |
| 2776 | * the request inbound with the message- body or ignore the |
| 2777 | * message-body when determining a response. |
| 2778 | * |
| 2779 | * 2. If a Transfer-Encoding header field (Section 9.7) is present |
| 2780 | * and the "chunked" transfer-coding (Section 6.2) is used, the |
| 2781 | * transfer-length is defined by the use of this transfer-coding. |
| 2782 | * If a Transfer-Encoding header field is present and the "chunked" |
| 2783 | * transfer-coding is not present, the transfer-length is defined |
| 2784 | * by the sender closing the connection. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2785 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2786 | * 3. If a Content-Length header field is present, its decimal value in |
| 2787 | * OCTETs represents both the entity-length and the transfer-length. |
| 2788 | * If a message is received with both a Transfer-Encoding header |
| 2789 | * field and a Content-Length header field, the latter MUST be ignored. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2790 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2791 | * 4. By the server closing the connection. (Closing the connection |
| 2792 | * cannot be used to indicate the end of a request body, since that |
| 2793 | * would leave no possibility for the server to send back a response.) |
| 2794 | * |
| 2795 | * Whenever a transfer-coding is applied to a message-body, the set of |
| 2796 | * transfer-codings MUST include "chunked", unless the message indicates |
| 2797 | * it is terminated by closing the connection. When the "chunked" |
| 2798 | * transfer-coding is used, it MUST be the last transfer-coding applied |
| 2799 | * to the message-body. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2800 | */ |
| 2801 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2802 | use_close_only = 0; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2803 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2804 | /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
Willy Tarreau | 9e13c3c | 2009-12-22 09:59:58 +0100 | [diff] [blame] | 2805 | while ((txn->flags & TX_REQ_VER_11) && |
| 2806 | http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2807 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
| 2808 | txn->flags |= (TX_REQ_TE_CHNK | TX_REQ_XFER_LEN); |
| 2809 | else if (txn->flags & TX_REQ_TE_CHNK) { |
| 2810 | /* bad transfer-encoding (chunked followed by something else) */ |
| 2811 | use_close_only = 1; |
| 2812 | txn->flags &= ~(TX_REQ_TE_CHNK | TX_REQ_XFER_LEN); |
| 2813 | break; |
| 2814 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2815 | } |
| 2816 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2817 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2818 | while (!(txn->flags & TX_REQ_TE_CHNK) && !use_close_only && |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2819 | http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) { |
| 2820 | signed long long cl; |
| 2821 | |
| 2822 | if (!ctx.vlen) |
| 2823 | goto return_bad_req; |
| 2824 | |
| 2825 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) |
| 2826 | goto return_bad_req; /* parse failure */ |
| 2827 | |
| 2828 | if (cl < 0) |
| 2829 | goto return_bad_req; |
| 2830 | |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2831 | if ((txn->flags & TX_REQ_CNT_LEN) && (msg->chunk_len != cl)) |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2832 | goto return_bad_req; /* already specified, was different */ |
| 2833 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2834 | txn->flags |= TX_REQ_CNT_LEN | TX_REQ_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2835 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2836 | } |
| 2837 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2838 | /* bodyless requests have a known length */ |
| 2839 | if (!use_close_only) |
| 2840 | txn->flags |= TX_REQ_XFER_LEN; |
| 2841 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2842 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2843 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2844 | req->analyse_exp = TICK_ETERNITY; |
| 2845 | return 1; |
| 2846 | |
| 2847 | return_bad_req: |
| 2848 | /* We centralize bad requests processing here */ |
| 2849 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 2850 | /* we detected a parsing error. We want to archive this request |
| 2851 | * in the dedicated proxy area for later troubleshooting. |
| 2852 | */ |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 2853 | 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] | 2854 | } |
| 2855 | |
| 2856 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 2857 | txn->status = 400; |
| 2858 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2859 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2860 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2861 | if (s->listener->counters) |
| 2862 | s->listener->counters->failed_req++; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2863 | |
| 2864 | return_prx_cond: |
| 2865 | if (!(s->flags & SN_ERR_MASK)) |
| 2866 | s->flags |= SN_ERR_PRXCOND; |
| 2867 | if (!(s->flags & SN_FINST_MASK)) |
| 2868 | s->flags |= SN_FINST_R; |
| 2869 | |
| 2870 | req->analysers = 0; |
| 2871 | req->analyse_exp = TICK_ETERNITY; |
| 2872 | return 0; |
| 2873 | } |
| 2874 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2875 | /* We reached the stats page through a POST request. |
| 2876 | * Parse the posted data and enable/disable servers if necessary. |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2877 | * 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] | 2878 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2879 | 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] | 2880 | { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2881 | struct proxy *px; |
| 2882 | struct server *sv; |
| 2883 | |
| 2884 | char *backend = NULL; |
| 2885 | int action = 0; |
| 2886 | |
| 2887 | char *first_param, *cur_param, *next_param, *end_params; |
| 2888 | |
| 2889 | first_param = req->data + txn->req.eoh + 2; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2890 | end_params = first_param + txn->req.body_len; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2891 | |
| 2892 | cur_param = next_param = end_params; |
| 2893 | |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2894 | if (end_params >= req->data + req->size - global.tune.maxrewrite) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2895 | /* Prevent buffer overflow */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2896 | si->applet.ctx.stats.st_code = STAT_STATUS_EXCD; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2897 | return 1; |
| 2898 | } |
| 2899 | else if (end_params > req->data + req->l) { |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2900 | /* we need more data */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2901 | si->applet.ctx.stats.st_code = STAT_STATUS_NONE; |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2902 | return 0; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2903 | } |
| 2904 | |
| 2905 | *end_params = '\0'; |
| 2906 | |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2907 | si->applet.ctx.stats.st_code = STAT_STATUS_NONE; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2908 | |
| 2909 | /* |
| 2910 | * Parse the parameters in reverse order to only store the last value. |
| 2911 | * From the html form, the backend and the action are at the end. |
| 2912 | */ |
| 2913 | while (cur_param > first_param) { |
| 2914 | char *key, *value; |
| 2915 | |
| 2916 | cur_param--; |
| 2917 | if ((*cur_param == '&') || (cur_param == first_param)) { |
| 2918 | /* Parse the key */ |
| 2919 | key = cur_param; |
| 2920 | if (cur_param != first_param) { |
| 2921 | /* delimit the string for the next loop */ |
| 2922 | *key++ = '\0'; |
| 2923 | } |
| 2924 | |
| 2925 | /* Parse the value */ |
| 2926 | value = key; |
| 2927 | while (*value != '\0' && *value != '=') { |
| 2928 | value++; |
| 2929 | } |
| 2930 | if (*value == '=') { |
| 2931 | /* Ok, a value is found, we can mark the end of the key */ |
| 2932 | *value++ = '\0'; |
| 2933 | } |
| 2934 | |
Willy Tarreau | bf9c2fc | 2011-05-31 18:06:18 +0200 | [diff] [blame] | 2935 | if (!url_decode(key) || !url_decode(value)) |
| 2936 | break; |
| 2937 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2938 | /* Now we can check the key to see what to do */ |
| 2939 | if (!backend && strcmp(key, "b") == 0) { |
| 2940 | backend = value; |
| 2941 | } |
| 2942 | else if (!action && strcmp(key, "action") == 0) { |
| 2943 | if (strcmp(value, "disable") == 0) { |
| 2944 | action = 1; |
| 2945 | } |
| 2946 | else if (strcmp(value, "enable") == 0) { |
| 2947 | action = 2; |
| 2948 | } else { |
| 2949 | /* unknown action, no need to continue */ |
| 2950 | break; |
| 2951 | } |
| 2952 | } |
| 2953 | else if (strcmp(key, "s") == 0) { |
| 2954 | if (backend && action && get_backend_server(backend, value, &px, &sv)) { |
| 2955 | switch (action) { |
| 2956 | case 1: |
Cyril Bonté | 1e2a170 | 2011-03-03 21:05:17 +0100 | [diff] [blame] | 2957 | if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2958 | /* Not already in maintenance, we can change the server state */ |
| 2959 | sv->state |= SRV_MAINTAIN; |
| 2960 | set_server_down(sv); |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2961 | si->applet.ctx.stats.st_code = STAT_STATUS_DONE; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2962 | } |
| 2963 | break; |
| 2964 | case 2: |
Cyril Bonté | 1e2a170 | 2011-03-03 21:05:17 +0100 | [diff] [blame] | 2965 | if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2966 | /* Already in maintenance, we can change the server state */ |
| 2967 | set_server_up(sv); |
Willy Tarreau | 7046130 | 2010-10-22 14:39:02 +0200 | [diff] [blame] | 2968 | sv->health = sv->rise; /* up, but will fall down at first failure */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2969 | si->applet.ctx.stats.st_code = STAT_STATUS_DONE; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2970 | } |
| 2971 | break; |
| 2972 | } |
| 2973 | } |
| 2974 | } |
| 2975 | next_param = cur_param; |
| 2976 | } |
| 2977 | } |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2978 | return 1; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2979 | } |
| 2980 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2981 | /* returns a pointer to the first rule which forbids access (deny or http_auth), |
| 2982 | * or NULL if everything's OK. |
| 2983 | */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2984 | static inline struct http_req_rule * |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2985 | http_check_access_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn) |
| 2986 | { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2987 | struct http_req_rule *rule; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2988 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2989 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2990 | int ret = 1; |
| 2991 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2992 | if (rule->action >= HTTP_REQ_ACT_MAX) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2993 | continue; |
| 2994 | |
| 2995 | /* check condition, but only if attached */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2996 | if (rule->cond) { |
| 2997 | ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ); |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2998 | ret = acl_pass(ret); |
| 2999 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3000 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3001 | ret = !ret; |
| 3002 | } |
| 3003 | |
| 3004 | if (ret) { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3005 | if (rule->action == HTTP_REQ_ACT_ALLOW) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3006 | return NULL; /* no problem */ |
| 3007 | else |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3008 | return rule; /* most likely a deny or auth rule */ |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3009 | } |
| 3010 | } |
| 3011 | return NULL; |
| 3012 | } |
| 3013 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3014 | /* This stream analyser runs all HTTP request processing which is common to |
| 3015 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 3016 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3017 | * 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] | 3018 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 3019 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3020 | */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3021 | 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] | 3022 | { |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3023 | struct http_txn *txn = &s->txn; |
| 3024 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3025 | struct acl_cond *cond; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3026 | struct http_req_rule *http_req_last_rule = NULL; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3027 | struct redirect_rule *rule; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 3028 | struct cond_wordlist *wl; |
Simon Horman | 70735c9 | 2011-06-07 11:07:50 +0900 | [diff] [blame] | 3029 | int do_stats; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3030 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3031 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3032 | /* we need more data */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3033 | buffer_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3034 | return 0; |
| 3035 | } |
| 3036 | |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3037 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3038 | req->analyse_exp = TICK_ETERNITY; |
| 3039 | |
| 3040 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 3041 | now_ms, __FUNCTION__, |
| 3042 | s, |
| 3043 | req, |
| 3044 | req->rex, req->wex, |
| 3045 | req->flags, |
| 3046 | req->l, |
| 3047 | req->analysers); |
| 3048 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3049 | /* first check whether we have some ACLs set to block this request */ |
| 3050 | list_for_each_entry(cond, &px->block_cond, list) { |
| 3051 | int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ); |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 3052 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3053 | ret = acl_pass(ret); |
| 3054 | if (cond->pol == ACL_COND_UNLESS) |
| 3055 | ret = !ret; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 3056 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3057 | if (ret) { |
| 3058 | txn->status = 403; |
| 3059 | /* let's log the request time */ |
| 3060 | s->logs.tv_request = now; |
| 3061 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3062 | session_inc_http_err_ctr(s); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3063 | goto return_prx_cond; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3064 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3065 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3066 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3067 | /* evaluate http-request rules */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3068 | 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] | 3069 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3070 | /* evaluate stats http-request rules only if http-request is OK */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3071 | if (!http_req_last_rule) { |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3072 | do_stats = stats_check_uri(s->rep->prod, txn, px); |
| 3073 | if (do_stats) |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3074 | 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] | 3075 | } |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3076 | else |
| 3077 | do_stats = 0; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3078 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3079 | /* return a 403 if either rule has blocked */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3080 | 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] | 3081 | txn->status = 403; |
| 3082 | s->logs.tv_request = now; |
| 3083 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3084 | session_inc_http_err_ctr(s); |
Willy Tarreau | 6da0f6d | 2011-01-06 18:19:50 +0100 | [diff] [blame] | 3085 | s->fe->fe_counters.denied_req++; |
| 3086 | if (an_bit == AN_REQ_HTTP_PROCESS_BE) |
| 3087 | s->be->be_counters.denied_req++; |
| 3088 | if (s->listener->counters) |
| 3089 | s->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3090 | goto return_prx_cond; |
| 3091 | } |
| 3092 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3093 | /* try headers filters */ |
| 3094 | if (px->req_exp != NULL) { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 3095 | if (apply_filters_to_request(s, req, px) < 0) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3096 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3097 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3098 | /* has the request been denied ? */ |
| 3099 | if (txn->flags & TX_CLDENY) { |
| 3100 | /* no need to go further */ |
| 3101 | txn->status = 403; |
| 3102 | /* let's log the request time */ |
| 3103 | s->logs.tv_request = now; |
| 3104 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3105 | session_inc_http_err_ctr(s); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3106 | goto return_prx_cond; |
| 3107 | } |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 3108 | |
| 3109 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 3110 | * which may be the same as the connect timeout if unspecified. |
| 3111 | * If unset, then set it to zero because we really want it to |
| 3112 | * eventually expire. We build the tarpit as an analyser. |
| 3113 | */ |
| 3114 | if (txn->flags & TX_CLTARPIT) { |
| 3115 | buffer_erase(s->req); |
| 3116 | /* wipe the request out so that we can drop the connection early |
| 3117 | * if the client closes first. |
| 3118 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3119 | buffer_dont_connect(req); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 3120 | req->analysers = 0; /* remove switching rules etc... */ |
| 3121 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 3122 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 3123 | if (!req->analyse_exp) |
| 3124 | req->analyse_exp = tick_add(now_ms, 0); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3125 | session_inc_http_err_ctr(s); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 3126 | return 1; |
| 3127 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3128 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3129 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3130 | /* Until set to anything else, the connection mode is set as TUNNEL. It will |
| 3131 | * only change if both the request and the config reference something else. |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 3132 | * Option httpclose by itself does not set a mode, it remains a tunnel mode |
| 3133 | * 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] | 3134 | * affect it (eg: server-close/keep-alive + httpclose = close). Note that we |
| 3135 | * avoid to redo the same work if FE and BE have the same settings (common). |
| 3136 | * The method consists in checking if options changed between the two calls |
| 3137 | * (implying that either one is non-null, or one of them is non-null and we |
| 3138 | * are there for the first time. |
Willy Tarreau | 4273664 | 2009-10-18 21:04:35 +0200 | [diff] [blame] | 3139 | */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3140 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 3141 | if ((!(txn->flags & TX_HDR_CONN_PRS) && |
| 3142 | (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) || |
| 3143 | ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) != |
| 3144 | (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] | 3145 | int tmp = TX_CON_WANT_TUN; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3146 | |
Cyril Bonté | 9ea2b9a | 2010-12-29 09:36:56 +0100 | [diff] [blame] | 3147 | if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE || |
| 3148 | ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3149 | tmp = TX_CON_WANT_KAL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 3150 | if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO) |
| 3151 | tmp = TX_CON_WANT_SCL; |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 3152 | if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3153 | tmp = TX_CON_WANT_CLO; |
| 3154 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3155 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
| 3156 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 3157 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3158 | if (!(txn->flags & TX_HDR_CONN_PRS)) { |
| 3159 | /* parse the Connection header and possibly clean it */ |
| 3160 | int to_del = 0; |
| 3161 | if ((txn->flags & TX_REQ_VER_11) || |
Willy Tarreau | 8a8e1d9 | 2010-04-05 16:15:16 +0200 | [diff] [blame] | 3162 | ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL && |
| 3163 | !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3164 | to_del |= 2; /* remove "keep-alive" */ |
| 3165 | if (!(txn->flags & TX_REQ_VER_11)) |
| 3166 | to_del |= 1; /* remove "close" */ |
| 3167 | http_parse_connection_header(txn, msg, req, to_del); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 3168 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3169 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3170 | /* check if client or config asks for explicit close in KAL/SCL */ |
| 3171 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 3172 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) && |
| 3173 | ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */ |
| 3174 | (txn->flags & (TX_REQ_VER_11|TX_HDR_CONN_KAL)) == 0 || /* no "connection: k-a" in 1.0 */ |
Cyril Bonté | 9ea2b9a | 2010-12-29 09:36:56 +0100 | [diff] [blame] | 3175 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */ |
Willy Tarreau | c3e8b25 | 2010-01-28 15:01:20 +0100 | [diff] [blame] | 3176 | !(txn->flags & TX_REQ_XFER_LEN) || /* no length known => close */ |
| 3177 | s->fe->state == PR_STSTOPPED)) /* frontend is stopping */ |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3178 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 3179 | } |
Willy Tarreau | 7859991 | 2009-10-17 20:12:21 +0200 | [diff] [blame] | 3180 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3181 | /* we can be blocked here because the request needs to be authenticated, |
| 3182 | * either to pass or to access stats. |
| 3183 | */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3184 | 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] | 3185 | struct chunk msg; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3186 | char *realm = http_req_last_rule->http_auth.realm; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3187 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3188 | if (!realm) |
| 3189 | realm = do_stats?STATS_DEFAULT_REALM:px->id; |
| 3190 | |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 3191 | 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] | 3192 | chunk_initlen(&msg, trash, sizeof(trash), strlen(trash)); |
| 3193 | txn->status = 401; |
| 3194 | stream_int_retnclose(req->prod, &msg); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3195 | /* on 401 we still count one error, because normal browsing |
| 3196 | * won't significantly increase the counter but brute force |
| 3197 | * attempts will. |
| 3198 | */ |
| 3199 | session_inc_http_err_ctr(s); |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3200 | goto return_prx_cond; |
| 3201 | } |
| 3202 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3203 | /* add request headers from the rule sets in the same order */ |
| 3204 | list_for_each_entry(wl, &px->req_add, list) { |
| 3205 | if (wl->cond) { |
| 3206 | int ret = acl_exec_cond(wl->cond, px, s, txn, ACL_DIR_REQ); |
| 3207 | ret = acl_pass(ret); |
| 3208 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 3209 | ret = !ret; |
| 3210 | if (!ret) |
| 3211 | continue; |
| 3212 | } |
| 3213 | |
| 3214 | if (unlikely(http_header_add_tail(req, &txn->req, &txn->hdr_idx, wl->s) < 0)) |
| 3215 | goto return_bad_req; |
| 3216 | } |
| 3217 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3218 | if (do_stats) { |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 3219 | struct stats_admin_rule *stats_admin_rule; |
| 3220 | |
| 3221 | /* We need to provide stats for this request. |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3222 | * FIXME!!! that one is rather dangerous, we want to |
| 3223 | * make it follow standard rules (eg: clear req->analysers). |
| 3224 | */ |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3225 | |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 3226 | /* now check whether we have some admin rules for this request */ |
| 3227 | list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) { |
| 3228 | int ret = 1; |
| 3229 | |
| 3230 | if (stats_admin_rule->cond) { |
| 3231 | ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ); |
| 3232 | ret = acl_pass(ret); |
| 3233 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 3234 | ret = !ret; |
| 3235 | } |
| 3236 | |
| 3237 | if (ret) { |
| 3238 | /* no rule, or the rule matches */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 3239 | s->rep->prod->applet.ctx.stats.flags |= STAT_ADMIN; |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 3240 | break; |
| 3241 | } |
| 3242 | } |
| 3243 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 3244 | /* Was the status page requested with a POST ? */ |
| 3245 | if (txn->meth == HTTP_METH_POST) { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 3246 | if (s->rep->prod->applet.ctx.stats.flags & STAT_ADMIN) { |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 3247 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 3248 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 3249 | * send an HTTP/1.1 100 Continue intermediate response. |
| 3250 | */ |
| 3251 | if (txn->flags & TX_REQ_VER_11) { |
| 3252 | struct hdr_ctx ctx; |
| 3253 | ctx.idx = 0; |
| 3254 | /* Expect is allowed in 1.1, look for it */ |
| 3255 | if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) && |
| 3256 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) { |
| 3257 | buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len); |
| 3258 | } |
| 3259 | } |
| 3260 | msg->msg_state = HTTP_MSG_100_SENT; |
| 3261 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 3262 | } |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 3263 | if (!http_process_req_stat_post(s->rep->prod, txn, req)) { |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 3264 | /* we need more data */ |
| 3265 | req->analysers |= an_bit; |
| 3266 | buffer_dont_connect(req); |
| 3267 | return 0; |
| 3268 | } |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 3269 | } else { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 3270 | s->rep->prod->applet.ctx.stats.st_code = STAT_STATUS_DENY; |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 3271 | } |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 3272 | } |
| 3273 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3274 | s->logs.tv_request = now; |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3275 | s->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | b24281b | 2011-02-13 13:16:36 +0100 | [diff] [blame] | 3276 | stream_int_register_handler(s->rep->prod, &http_stats_applet); |
Willy Tarreau | 7b7a8e9 | 2011-03-27 19:53:06 +0200 | [diff] [blame] | 3277 | copy_target(&s->target, &s->rep->prod->target); // for logging only |
Willy Tarreau | bc4af05 | 2011-02-13 13:25:14 +0100 | [diff] [blame] | 3278 | s->rep->prod->applet.private = s; |
| 3279 | s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0; |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3280 | req->analysers = 0; |
| 3281 | |
| 3282 | return 0; |
| 3283 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3284 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 3285 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3286 | /* check whether we have some ACLs set to redirect this request */ |
| 3287 | list_for_each_entry(rule, &px->redirect_rules, list) { |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3288 | int ret = ACL_PAT_PASS; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3289 | |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3290 | if (rule->cond) { |
| 3291 | ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ); |
| 3292 | ret = acl_pass(ret); |
| 3293 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 3294 | ret = !ret; |
| 3295 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3296 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3297 | if (ret) { |
Willy Tarreau | 3bb9c23 | 2010-01-03 12:24:37 +0100 | [diff] [blame] | 3298 | struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 }; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3299 | const char *msg_fmt; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3300 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3301 | /* build redirect message */ |
| 3302 | switch(rule->code) { |
| 3303 | case 303: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3304 | msg_fmt = HTTP_303; |
| 3305 | break; |
| 3306 | case 301: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3307 | msg_fmt = HTTP_301; |
| 3308 | break; |
| 3309 | case 302: |
| 3310 | default: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3311 | msg_fmt = HTTP_302; |
| 3312 | break; |
| 3313 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3314 | |
Willy Tarreau | 3bb9c23 | 2010-01-03 12:24:37 +0100 | [diff] [blame] | 3315 | if (unlikely(!chunk_strcpy(&rdr, msg_fmt))) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3316 | goto return_bad_req; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3317 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3318 | switch(rule->type) { |
| 3319 | case REDIRECT_TYPE_PREFIX: { |
| 3320 | const char *path; |
| 3321 | int pathlen; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3322 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3323 | path = http_get_path(txn); |
| 3324 | /* build message using path */ |
| 3325 | if (path) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 3326 | 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] | 3327 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3328 | int qs = 0; |
| 3329 | while (qs < pathlen) { |
| 3330 | if (path[qs] == '?') { |
| 3331 | pathlen = qs; |
| 3332 | break; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3333 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3334 | qs++; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3335 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3336 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3337 | } else { |
| 3338 | path = "/"; |
| 3339 | pathlen = 1; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3340 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3341 | |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 3342 | if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3343 | goto return_bad_req; |
| 3344 | |
| 3345 | /* add prefix. Note that if prefix == "/", we don't want to |
| 3346 | * add anything, otherwise it makes it hard for the user to |
| 3347 | * configure a self-redirection. |
| 3348 | */ |
| 3349 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3350 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 3351 | rdr.len += rule->rdr_len; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3352 | } |
| 3353 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3354 | /* add path */ |
| 3355 | memcpy(rdr.str + rdr.len, path, pathlen); |
| 3356 | rdr.len += pathlen; |
Willy Tarreau | 81e3b4f | 2010-01-10 00:42:19 +0100 | [diff] [blame] | 3357 | |
| 3358 | /* append a slash at the end of the location is needed and missing */ |
| 3359 | if (rdr.len && rdr.str[rdr.len - 1] != '/' && |
| 3360 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 3361 | if (rdr.len > rdr.size - 5) |
| 3362 | goto return_bad_req; |
| 3363 | rdr.str[rdr.len] = '/'; |
| 3364 | rdr.len++; |
| 3365 | } |
| 3366 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3367 | break; |
| 3368 | } |
| 3369 | case REDIRECT_TYPE_LOCATION: |
| 3370 | default: |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 3371 | if (rdr.len + rule->rdr_len > rdr.size - 4) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3372 | goto return_bad_req; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3373 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3374 | /* add location */ |
| 3375 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 3376 | rdr.len += rule->rdr_len; |
| 3377 | break; |
| 3378 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3379 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3380 | if (rule->cookie_len) { |
| 3381 | memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14); |
| 3382 | rdr.len += 14; |
| 3383 | memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len); |
| 3384 | rdr.len += rule->cookie_len; |
| 3385 | memcpy(rdr.str + rdr.len, "\r\n", 2); |
| 3386 | rdr.len += 2; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3387 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3388 | |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3389 | /* add end of headers and the keep-alive/close status. |
| 3390 | * We may choose to set keep-alive if the Location begins |
| 3391 | * with a slash, because the client will come back to the |
| 3392 | * same server. |
| 3393 | */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3394 | txn->status = rule->code; |
| 3395 | /* let's log the request time */ |
| 3396 | s->logs.tv_request = now; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3397 | |
| 3398 | if (rule->rdr_len >= 1 && *rule->rdr_str == '/' && |
| 3399 | (txn->flags & TX_REQ_XFER_LEN) && |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 3400 | !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.body_len && |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3401 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 3402 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 3403 | /* keep-alive possible */ |
Willy Tarreau | 7566145 | 2010-01-10 10:35:01 +0100 | [diff] [blame] | 3404 | if (!(txn->flags & TX_REQ_VER_11)) { |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 3405 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 3406 | memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: keep-alive", 30); |
| 3407 | rdr.len += 30; |
| 3408 | } else { |
| 3409 | memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24); |
| 3410 | rdr.len += 24; |
| 3411 | } |
Willy Tarreau | 7566145 | 2010-01-10 10:35:01 +0100 | [diff] [blame] | 3412 | } |
| 3413 | memcpy(rdr.str + rdr.len, "\r\n\r\n", 4); |
| 3414 | rdr.len += 4; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3415 | buffer_write(req->prod->ob, rdr.str, rdr.len); |
| 3416 | /* "eat" the request */ |
| 3417 | buffer_ignore(req, msg->sov - msg->som); |
| 3418 | msg->som = msg->sov; |
| 3419 | req->analysers = AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 9300fb2 | 2010-01-05 00:58:24 +0100 | [diff] [blame] | 3420 | s->rep->analysers = AN_RES_HTTP_XFER_BODY; |
| 3421 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3422 | txn->rsp.msg_state = HTTP_MSG_DONE; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3423 | break; |
| 3424 | } else { |
| 3425 | /* keep-alive not possible */ |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 3426 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 3427 | memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 3428 | rdr.len += 29; |
| 3429 | } else { |
| 3430 | memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23); |
| 3431 | rdr.len += 23; |
| 3432 | } |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 3433 | stream_int_retnclose(req->prod, &rdr); |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3434 | goto return_prx_cond; |
| 3435 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3436 | } |
| 3437 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3438 | |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 3439 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 3440 | * If this happens, then the data will not come immediately, so we must |
| 3441 | * send all what we have without waiting. Note that due to the small gain |
| 3442 | * in waiting for the body of the request, it's easier to simply put the |
| 3443 | * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove |
| 3444 | * itself once used. |
| 3445 | */ |
| 3446 | req->flags |= BF_SEND_DONTWAIT; |
| 3447 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3448 | /* that's OK for us now, let's move on to next analysers */ |
| 3449 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 3450 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3451 | return_bad_req: |
| 3452 | /* We centralize bad requests processing here */ |
| 3453 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 3454 | /* we detected a parsing error. We want to archive this request |
| 3455 | * in the dedicated proxy area for later troubleshooting. |
| 3456 | */ |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 3457 | 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] | 3458 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3459 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3460 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3461 | txn->status = 400; |
| 3462 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3463 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3464 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3465 | if (s->listener->counters) |
| 3466 | s->listener->counters->failed_req++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 3467 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3468 | return_prx_cond: |
| 3469 | if (!(s->flags & SN_ERR_MASK)) |
| 3470 | s->flags |= SN_ERR_PRXCOND; |
| 3471 | if (!(s->flags & SN_FINST_MASK)) |
| 3472 | s->flags |= SN_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 3473 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3474 | req->analysers = 0; |
| 3475 | req->analyse_exp = TICK_ETERNITY; |
| 3476 | return 0; |
| 3477 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3478 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3479 | /* This function performs all the processing enabled for the current request. |
| 3480 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 3481 | * needs more data, encounters an error, or wants to immediately abort the |
| 3482 | * request. It relies on buffers flags, and updates s->req->analysers. |
| 3483 | */ |
| 3484 | int http_process_request(struct session *s, struct buffer *req, int an_bit) |
| 3485 | { |
| 3486 | struct http_txn *txn = &s->txn; |
| 3487 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3488 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3489 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3490 | /* we need more data */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3491 | buffer_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3492 | return 0; |
| 3493 | } |
| 3494 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3495 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 3496 | now_ms, __FUNCTION__, |
| 3497 | s, |
| 3498 | req, |
| 3499 | req->rex, req->wex, |
| 3500 | req->flags, |
| 3501 | req->l, |
| 3502 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3503 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3504 | /* |
| 3505 | * Right now, we know that we have processed the entire headers |
| 3506 | * and that unwanted requests have been filtered out. We can do |
| 3507 | * whatever we want with the remaining request. Also, now we |
| 3508 | * may have separate values for ->fe, ->be. |
| 3509 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3510 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3511 | /* |
| 3512 | * If HTTP PROXY is set we simply get remote server address |
| 3513 | * parsing incoming request. |
| 3514 | */ |
| 3515 | if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) { |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 3516 | url2sa(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l, &s->req->cons->addr.s.to); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3517 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3518 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3519 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3520 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3521 | * Note that doing so might move headers in the request, but |
| 3522 | * the fields will stay coherent and the URI will not move. |
| 3523 | * This should only be performed in the backend. |
| 3524 | */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 3525 | 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] | 3526 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
| 3527 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 3528 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3529 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3530 | * 8: the appsession cookie was looked up very early in 1.2, |
| 3531 | * so let's do the same now. |
| 3532 | */ |
| 3533 | |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 3534 | /* It needs to look into the URI unless persistence must be ignored */ |
| 3535 | 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] | 3536 | 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] | 3537 | } |
| 3538 | |
| 3539 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3540 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 3541 | * asks for it. |
| 3542 | */ |
| 3543 | if ((s->fe->options | s->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 3544 | struct hdr_ctx ctx = { .idx = 0 }; |
| 3545 | |
| 3546 | if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) && |
| 3547 | http_find_header2("X-Forwarded-For", 15, txn->req.sol, &txn->hdr_idx, &ctx)) { |
| 3548 | /* The header is set to be added only if none is present |
| 3549 | * and we found it, so don't do anything. |
| 3550 | */ |
| 3551 | } |
| 3552 | else if (s->req->prod->addr.c.from.ss_family == AF_INET) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3553 | /* Add an X-Forwarded-For header unless the source IP is |
| 3554 | * in the 'except' network range. |
| 3555 | */ |
| 3556 | if ((!s->fe->except_mask.s_addr || |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 3557 | (((struct sockaddr_in *)&s->req->prod->addr.c.from)->sin_addr.s_addr & s->fe->except_mask.s_addr) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3558 | != s->fe->except_net.s_addr) && |
| 3559 | (!s->be->except_mask.s_addr || |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 3560 | (((struct sockaddr_in *)&s->req->prod->addr.c.from)->sin_addr.s_addr & s->be->except_mask.s_addr) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3561 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3562 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3563 | unsigned char *pn; |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 3564 | pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.c.from)->sin_addr; |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3565 | |
| 3566 | /* Note: we rely on the backend to get the header name to be used for |
| 3567 | * x-forwarded-for, because the header is really meant for the backends. |
| 3568 | * However, if the backend did not specify any option, we have to rely |
| 3569 | * on the frontend's header name. |
| 3570 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3571 | if (s->be->fwdfor_hdr_len) { |
| 3572 | len = s->be->fwdfor_hdr_len; |
| 3573 | memcpy(trash, s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3574 | } else { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3575 | len = s->fe->fwdfor_hdr_len; |
| 3576 | memcpy(trash, s->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3577 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3578 | 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] | 3579 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 3580 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 3581 | &txn->hdr_idx, trash, len) < 0)) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3582 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3583 | } |
| 3584 | } |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 3585 | else if (s->req->prod->addr.c.from.ss_family == AF_INET6) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3586 | /* FIXME: for the sake of completeness, we should also support |
| 3587 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3588 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3589 | int len; |
| 3590 | char pn[INET6_ADDRSTRLEN]; |
| 3591 | inet_ntop(AF_INET6, |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 3592 | (const void *)&((struct sockaddr_in6 *)(&s->req->prod->addr.c.from))->sin6_addr, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3593 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3594 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3595 | /* Note: we rely on the backend to get the header name to be used for |
| 3596 | * x-forwarded-for, because the header is really meant for the backends. |
| 3597 | * However, if the backend did not specify any option, we have to rely |
| 3598 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3599 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3600 | if (s->be->fwdfor_hdr_len) { |
| 3601 | len = s->be->fwdfor_hdr_len; |
| 3602 | memcpy(trash, s->be->fwdfor_hdr_name, len); |
| 3603 | } else { |
| 3604 | len = s->fe->fwdfor_hdr_len; |
| 3605 | memcpy(trash, s->fe->fwdfor_hdr_name, len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3606 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3607 | len += sprintf(trash + len, ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3608 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3609 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 3610 | &txn->hdr_idx, trash, len) < 0)) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3611 | goto return_bad_req; |
| 3612 | } |
| 3613 | } |
| 3614 | |
| 3615 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3616 | * 10: add X-Original-To if either the frontend or the backend |
| 3617 | * asks for it. |
| 3618 | */ |
| 3619 | if ((s->fe->options | s->be->options) & PR_O_ORGTO) { |
| 3620 | |
| 3621 | /* FIXME: don't know if IPv6 can handle that case too. */ |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 3622 | if (s->req->prod->addr.c.from.ss_family == AF_INET) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3623 | /* Add an X-Original-To header unless the destination IP is |
| 3624 | * in the 'except' network range. |
| 3625 | */ |
| 3626 | if (!(s->flags & SN_FRT_ADDR_SET)) |
| 3627 | get_frt_addr(s); |
| 3628 | |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 3629 | if (s->req->prod->addr.c.to.ss_family == AF_INET && |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 3630 | ((!s->fe->except_mask_to.s_addr || |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 3631 | (((struct sockaddr_in *)&s->req->prod->addr.c.to)->sin_addr.s_addr & s->fe->except_mask_to.s_addr) |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 3632 | != s->fe->except_to.s_addr) && |
| 3633 | (!s->be->except_mask_to.s_addr || |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 3634 | (((struct sockaddr_in *)&s->req->prod->addr.c.to)->sin_addr.s_addr & s->be->except_mask_to.s_addr) |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 3635 | != s->be->except_to.s_addr))) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3636 | int len; |
| 3637 | unsigned char *pn; |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 3638 | pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.c.to)->sin_addr; |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3639 | |
| 3640 | /* Note: we rely on the backend to get the header name to be used for |
| 3641 | * x-original-to, because the header is really meant for the backends. |
| 3642 | * However, if the backend did not specify any option, we have to rely |
| 3643 | * on the frontend's header name. |
| 3644 | */ |
| 3645 | if (s->be->orgto_hdr_len) { |
| 3646 | len = s->be->orgto_hdr_len; |
| 3647 | memcpy(trash, s->be->orgto_hdr_name, len); |
| 3648 | } else { |
| 3649 | len = s->fe->orgto_hdr_len; |
| 3650 | memcpy(trash, s->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3651 | } |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3652 | len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]); |
| 3653 | |
| 3654 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 3655 | &txn->hdr_idx, trash, len) < 0)) |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3656 | goto return_bad_req; |
| 3657 | } |
| 3658 | } |
| 3659 | } |
| 3660 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3661 | /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */ |
| 3662 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Cyril Bonté | 9ea2b9a | 2010-12-29 09:36:56 +0100 | [diff] [blame] | 3663 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3664 | unsigned int want_flags = 0; |
| 3665 | |
| 3666 | if (txn->flags & TX_REQ_VER_11) { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 3667 | if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL || |
| 3668 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) && |
| 3669 | !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3670 | want_flags |= TX_CON_CLO_SET; |
| 3671 | } else { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 3672 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
| 3673 | !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) || |
| 3674 | ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3675 | want_flags |= TX_CON_KAL_SET; |
| 3676 | } |
| 3677 | |
| 3678 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 3679 | http_change_connection_header(txn, msg, req, want_flags); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3680 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3681 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3682 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3683 | /* If we have no server assigned yet and we're balancing on url_param |
| 3684 | * with a POST request, we may be interested in checking the body for |
| 3685 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3686 | */ |
| 3687 | if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) && |
| 3688 | s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3689 | s->be->url_param_post_limit != 0 && |
Willy Tarreau | 61a21a3 | 2011-03-01 20:35:49 +0100 | [diff] [blame] | 3690 | (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK))) { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3691 | buffer_dont_connect(req); |
| 3692 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3693 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3694 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3695 | if (txn->flags & TX_REQ_XFER_LEN) |
| 3696 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 3697 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3698 | /************************************************************* |
| 3699 | * OK, that's finished for the headers. We have done what we * |
| 3700 | * could. Let's switch to the DATA state. * |
| 3701 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3702 | req->analyse_exp = TICK_ETERNITY; |
| 3703 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3704 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3705 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3706 | /* OK let's go on with the BODY now */ |
| 3707 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3708 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3709 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3710 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 3711 | /* we detected a parsing error. We want to archive this request |
| 3712 | * in the dedicated proxy area for later troubleshooting. |
| 3713 | */ |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 3714 | 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] | 3715 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3716 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3717 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3718 | txn->status = 400; |
| 3719 | req->analysers = 0; |
| 3720 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3721 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3722 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3723 | if (s->listener->counters) |
| 3724 | s->listener->counters->failed_req++; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3725 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3726 | if (!(s->flags & SN_ERR_MASK)) |
| 3727 | s->flags |= SN_ERR_PRXCOND; |
| 3728 | if (!(s->flags & SN_FINST_MASK)) |
| 3729 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3730 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3731 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3732 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3733 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 3734 | * returns zero, at the beginning because it prevents any other processing |
| 3735 | * from occurring, and at the end because it terminates the request. |
| 3736 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3737 | 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] | 3738 | { |
| 3739 | struct http_txn *txn = &s->txn; |
| 3740 | |
| 3741 | /* This connection is being tarpitted. The CLIENT side has |
| 3742 | * already set the connect expiration date to the right |
| 3743 | * timeout. We just have to check that the client is still |
| 3744 | * there and that the timeout has not expired. |
| 3745 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3746 | buffer_dont_connect(req); |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3747 | if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 && |
| 3748 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 3749 | return 0; |
| 3750 | |
| 3751 | /* We will set the queue timer to the time spent, just for |
| 3752 | * logging purposes. We fake a 500 server error, so that the |
| 3753 | * attacker will not suspect his connection has been tarpitted. |
| 3754 | * It will not cause trouble to the logs because we can exclude |
| 3755 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 3756 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3757 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 3758 | |
| 3759 | txn->status = 500; |
| 3760 | if (req->flags != BF_READ_ERROR) |
| 3761 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500)); |
| 3762 | |
| 3763 | req->analysers = 0; |
| 3764 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3765 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3766 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3767 | if (s->listener->counters) |
| 3768 | s->listener->counters->failed_req++; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3769 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3770 | if (!(s->flags & SN_ERR_MASK)) |
| 3771 | s->flags |= SN_ERR_PRXCOND; |
| 3772 | if (!(s->flags & SN_FINST_MASK)) |
| 3773 | s->flags |= SN_FINST_T; |
| 3774 | return 0; |
| 3775 | } |
| 3776 | |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3777 | /* This function is an analyser which processes the HTTP request body. It looks |
| 3778 | * for parameters to be used for the load balancing algorithm (url_param). It |
| 3779 | * must only be called after the standard HTTP request processing has occurred, |
| 3780 | * because it expects the request to be parsed. It returns zero if it needs to |
| 3781 | * read more data, or 1 once it has completed its analysis. |
| 3782 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3783 | 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] | 3784 | { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3785 | struct http_txn *txn = &s->txn; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3786 | struct http_msg *msg = &s->txn.req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3787 | long long limit = s->be->url_param_post_limit; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3788 | |
| 3789 | /* We have to parse the HTTP request body to find any required data. |
| 3790 | * "balance url_param check_post" should have been the only way to get |
| 3791 | * into this. We were brought here after HTTP header analysis, so all |
| 3792 | * related structures are ready. |
| 3793 | */ |
| 3794 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3795 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 3796 | goto missing_data; |
| 3797 | |
| 3798 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 3799 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 3800 | * send an HTTP/1.1 100 Continue intermediate response. |
| 3801 | */ |
Willy Tarreau | 9e13c3c | 2009-12-22 09:59:58 +0100 | [diff] [blame] | 3802 | if (txn->flags & TX_REQ_VER_11) { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3803 | struct hdr_ctx ctx; |
| 3804 | ctx.idx = 0; |
| 3805 | /* Expect is allowed in 1.1, look for it */ |
| 3806 | if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) && |
| 3807 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) { |
| 3808 | buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len); |
| 3809 | } |
| 3810 | } |
| 3811 | msg->msg_state = HTTP_MSG_100_SENT; |
| 3812 | } |
| 3813 | |
| 3814 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3815 | /* we have msg->col and msg->sov which both point to the first |
| 3816 | * byte of message body. msg->som still points to the beginning |
| 3817 | * of the message. We must save the body in req->lr because it |
| 3818 | * survives buffer re-alignments. |
| 3819 | */ |
| 3820 | req->lr = req->data + msg->sov; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3821 | if (txn->flags & TX_REQ_TE_CHNK) |
| 3822 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 3823 | else |
| 3824 | msg->msg_state = HTTP_MSG_DATA; |
| 3825 | } |
| 3826 | |
| 3827 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 3828 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3829 | * set ->sov and ->lr to point to the body and switch to DATA or |
| 3830 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3831 | */ |
| 3832 | int ret = http_parse_chunk_size(req, msg); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3833 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3834 | if (!ret) |
| 3835 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3836 | else if (ret < 0) { |
| 3837 | session_inc_http_err_ctr(s); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3838 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3839 | } |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3840 | } |
| 3841 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3842 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3843 | * We have the first non-header byte in msg->col, which is either the |
| 3844 | * beginning of the chunk size or of the data. The first data byte is in |
| 3845 | * msg->sov, which is equal to msg->col when not using transfer-encoding. |
| 3846 | * 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] | 3847 | */ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3848 | |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 3849 | if (msg->body_len < limit) |
| 3850 | limit = msg->body_len; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3851 | |
Willy Tarreau | 7c96f67 | 2009-12-27 22:47:25 +0100 | [diff] [blame] | 3852 | 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] | 3853 | goto http_end; |
| 3854 | |
| 3855 | missing_data: |
| 3856 | /* we get here if we need to wait for more data */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3857 | if (req->flags & BF_FULL) { |
| 3858 | session_inc_http_err_ctr(s); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3859 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3860 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3861 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3862 | if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) { |
| 3863 | txn->status = 408; |
| 3864 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408)); |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 3865 | |
| 3866 | if (!(s->flags & SN_ERR_MASK)) |
| 3867 | s->flags |= SN_ERR_CLITO; |
| 3868 | if (!(s->flags & SN_FINST_MASK)) |
| 3869 | s->flags |= SN_FINST_D; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3870 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3871 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3872 | |
| 3873 | /* we get here if we need to wait for more data */ |
| 3874 | if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) { |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3875 | /* Not enough data. We'll re-use the http-request |
| 3876 | * timeout here. Ideally, we should set the timeout |
| 3877 | * relative to the accept() date. We just set the |
| 3878 | * request timeout once at the beginning of the |
| 3879 | * request. |
| 3880 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3881 | buffer_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3882 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 3883 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3884 | return 0; |
| 3885 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3886 | |
| 3887 | http_end: |
| 3888 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 3889 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 3890 | req->analysers &= ~an_bit; |
| 3891 | req->analyse_exp = TICK_ETERNITY; |
| 3892 | return 1; |
| 3893 | |
| 3894 | return_bad_req: /* let's centralize all bad requests */ |
| 3895 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3896 | txn->status = 400; |
| 3897 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
| 3898 | |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 3899 | if (!(s->flags & SN_ERR_MASK)) |
| 3900 | s->flags |= SN_ERR_PRXCOND; |
| 3901 | if (!(s->flags & SN_FINST_MASK)) |
| 3902 | s->flags |= SN_FINST_R; |
| 3903 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3904 | return_err_msg: |
| 3905 | req->analysers = 0; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3906 | s->fe->fe_counters.failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3907 | if (s->listener->counters) |
| 3908 | s->listener->counters->failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3909 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3910 | } |
| 3911 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3912 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 3913 | * right now but it works. |
| 3914 | */ |
| 3915 | void http_end_txn_clean_session(struct session *s) |
| 3916 | { |
| 3917 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 3918 | * server's connections. We need a safer way to reinitialize buffer |
| 3919 | * flags. We also need a more accurate method for computing per-request |
| 3920 | * data. |
| 3921 | */ |
| 3922 | http_silent_debug(__LINE__, s); |
| 3923 | |
| 3924 | s->req->cons->flags |= SI_FL_NOLINGER; |
| 3925 | s->req->cons->shutr(s->req->cons); |
| 3926 | s->req->cons->shutw(s->req->cons); |
| 3927 | |
| 3928 | http_silent_debug(__LINE__, s); |
| 3929 | |
| 3930 | if (s->flags & SN_BE_ASSIGNED) |
| 3931 | s->be->beconn--; |
| 3932 | |
| 3933 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 3934 | session_process_counters(s); |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 3935 | session_stop_backend_counters(s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3936 | |
| 3937 | if (s->txn.status) { |
| 3938 | int n; |
| 3939 | |
| 3940 | n = s->txn.status / 100; |
| 3941 | if (n < 1 || n > 5) |
| 3942 | n = 0; |
| 3943 | |
| 3944 | if (s->fe->mode == PR_MODE_HTTP) |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3945 | s->fe->fe_counters.p.http.rsp[n]++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3946 | |
Willy Tarreau | 2465779 | 2010-02-26 10:30:28 +0100 | [diff] [blame] | 3947 | if ((s->flags & SN_BE_ASSIGNED) && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3948 | (s->be->mode == PR_MODE_HTTP)) |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3949 | s->be->be_counters.p.http.rsp[n]++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3950 | } |
| 3951 | |
| 3952 | /* don't count other requests' data */ |
| 3953 | s->logs.bytes_in -= s->req->l - s->req->send_max; |
| 3954 | s->logs.bytes_out -= s->rep->l - s->rep->send_max; |
| 3955 | |
| 3956 | /* let's do a final log if we need it */ |
| 3957 | if (s->logs.logwait && |
| 3958 | !(s->flags & SN_MONITOR) && |
| 3959 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
| 3960 | s->do_log(s); |
| 3961 | } |
| 3962 | |
| 3963 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 3964 | s->logs.tv_accept = now; /* corrected date for internal use */ |
| 3965 | tv_zero(&s->logs.tv_request); |
| 3966 | s->logs.t_queue = -1; |
| 3967 | s->logs.t_connect = -1; |
| 3968 | s->logs.t_data = -1; |
| 3969 | s->logs.t_close = 0; |
| 3970 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 3971 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 3972 | |
| 3973 | s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max; |
| 3974 | s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max; |
| 3975 | |
| 3976 | if (s->pend_pos) |
| 3977 | pendconn_free(s->pend_pos); |
| 3978 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 3979 | if (target_srv(&s->target)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3980 | if (s->flags & SN_CURR_SESS) { |
| 3981 | s->flags &= ~SN_CURR_SESS; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 3982 | target_srv(&s->target)->cur_sess--; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3983 | } |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 3984 | if (may_dequeue_tasks(target_srv(&s->target), s->be)) |
| 3985 | process_srv_queue(target_srv(&s->target)); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3986 | } |
| 3987 | |
| 3988 | if (unlikely(s->srv_conn)) |
| 3989 | sess_change_server(s, NULL); |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 3990 | clear_target(&s->target); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3991 | |
| 3992 | s->req->cons->state = s->req->cons->prev_state = SI_ST_INI; |
| 3993 | s->req->cons->fd = -1; /* just to help with debugging */ |
| 3994 | s->req->cons->err_type = SI_ET_NONE; |
Willy Tarreau | 0b3a411 | 2011-03-27 19:16:56 +0200 | [diff] [blame] | 3995 | s->req->cons->conn_retries = 0; /* used for logging too */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3996 | s->req->cons->err_loc = NULL; |
| 3997 | s->req->cons->exp = TICK_ETERNITY; |
| 3998 | s->req->cons->flags = SI_FL_NONE; |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 3999 | s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST|BF_NEVER_WAIT); |
| 4000 | 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] | 4001 | 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] | 4002 | s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE); |
| 4003 | s->txn.meth = 0; |
| 4004 | http_reset_txn(s); |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 4005 | s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ; |
Willy Tarreau | ee55dc0 | 2010-06-01 10:56:34 +0200 | [diff] [blame] | 4006 | if (s->fe->options2 & PR_O2_INDEPSTR) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4007 | s->req->cons->flags |= SI_FL_INDEP_STR; |
| 4008 | |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 4009 | if (s->fe->options2 & PR_O2_NODELAY) { |
| 4010 | s->req->flags |= BF_NEVER_WAIT; |
| 4011 | s->rep->flags |= BF_NEVER_WAIT; |
| 4012 | } |
| 4013 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4014 | /* if the request buffer is not empty, it means we're |
| 4015 | * about to process another request, so send pending |
| 4016 | * data with MSG_MORE to merge TCP packets when possible. |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 4017 | * Just don't do this if the buffer is close to be full, |
| 4018 | * because the request will wait for it to flush a little |
| 4019 | * bit before proceeding. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4020 | */ |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 4021 | if (s->req->l > s->req->send_max) { |
| 4022 | if (s->rep->send_max && |
| 4023 | !(s->rep->flags & BF_FULL) && |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 4024 | s->rep->r <= s->rep->data + s->rep->size - global.tune.maxrewrite) |
| 4025 | s->rep->flags |= BF_EXPECT_MORE; |
| 4026 | } |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4027 | |
| 4028 | /* we're removing the analysers, we MUST re-enable events detection */ |
| 4029 | buffer_auto_read(s->req); |
| 4030 | buffer_auto_close(s->req); |
| 4031 | buffer_auto_read(s->rep); |
| 4032 | buffer_auto_close(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4033 | |
| 4034 | /* make ->lr point to the first non-forwarded byte */ |
| 4035 | s->req->lr = s->req->w + s->req->send_max; |
| 4036 | if (s->req->lr >= s->req->data + s->req->size) |
| 4037 | s->req->lr -= s->req->size; |
| 4038 | s->rep->lr = s->rep->w + s->rep->send_max; |
| 4039 | if (s->rep->lr >= s->rep->data + s->rep->size) |
| 4040 | s->rep->lr -= s->req->size; |
| 4041 | |
Willy Tarreau | 342b11c | 2010-11-24 16:22:09 +0100 | [diff] [blame] | 4042 | s->req->analysers = s->listener->analysers; |
| 4043 | s->req->analysers &= ~AN_REQ_DECODE_PROXY; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4044 | s->rep->analysers = 0; |
| 4045 | |
| 4046 | http_silent_debug(__LINE__, s); |
| 4047 | } |
| 4048 | |
| 4049 | |
| 4050 | /* This function updates the request state machine according to the response |
| 4051 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 4052 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 4053 | * it is only used to find when a request/response couple is complete. Both |
| 4054 | * this function and its equivalent should loop until both return zero. It |
| 4055 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 4056 | */ |
| 4057 | int http_sync_req_state(struct session *s) |
| 4058 | { |
| 4059 | struct buffer *buf = s->req; |
| 4060 | struct http_txn *txn = &s->txn; |
| 4061 | unsigned int old_flags = buf->flags; |
| 4062 | unsigned int old_state = txn->req.msg_state; |
| 4063 | |
| 4064 | http_silent_debug(__LINE__, s); |
| 4065 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) |
| 4066 | return 0; |
| 4067 | |
| 4068 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4069 | /* No need to read anymore, the request was completely parsed. |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4070 | * We can shut the read side unless we want to abort_on_close, |
| 4071 | * or we have a POST request. The issue with POST requests is |
| 4072 | * that some browsers still send a CRLF after the request, and |
| 4073 | * this CRLF must be read so that it does not remain in the kernel |
| 4074 | * buffers, otherwise a close could cause an RST on some systems |
| 4075 | * (eg: Linux). |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4076 | */ |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4077 | 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] | 4078 | buffer_dont_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4079 | |
| 4080 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 4081 | goto wait_other_side; |
| 4082 | |
| 4083 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 4084 | /* The server has not finished to respond, so we |
| 4085 | * don't want to move in order not to upset it. |
| 4086 | */ |
| 4087 | goto wait_other_side; |
| 4088 | } |
| 4089 | |
| 4090 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 4091 | /* if any side switches to tunnel mode, the other one does too */ |
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 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
| 4094 | goto wait_other_side; |
| 4095 | } |
| 4096 | |
| 4097 | /* When we get here, it means that both the request and the |
| 4098 | * response have finished receiving. Depending on the connection |
| 4099 | * mode, we'll have to wait for the last bytes to leave in either |
| 4100 | * direction, and sometimes for a close to be effective. |
| 4101 | */ |
| 4102 | |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4103 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 4104 | /* Server-close mode : queue a connection close to the server */ |
| 4105 | if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4106 | buffer_shutw_now(buf); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4107 | } |
| 4108 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4109 | /* Option forceclose is set, or either side wants to close, |
| 4110 | * let's enforce it now that we're not expecting any new |
| 4111 | * data to come. The caller knows the session is complete |
| 4112 | * once both states are CLOSED. |
| 4113 | */ |
| 4114 | if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4115 | buffer_shutr_now(buf); |
| 4116 | buffer_shutw_now(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4117 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4118 | } |
| 4119 | else { |
| 4120 | /* The last possible modes are keep-alive and tunnel. Since tunnel |
| 4121 | * mode does not set the body analyser, we can't reach this place |
| 4122 | * in tunnel mode, so we're left with keep-alive only. |
| 4123 | * This mode is currently not implemented, we switch to tunnel mode. |
| 4124 | */ |
| 4125 | buffer_auto_read(buf); |
| 4126 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4127 | } |
| 4128 | |
| 4129 | if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) { |
| 4130 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4131 | buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
| 4132 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4133 | if (!(buf->flags & BF_OUT_EMPTY)) { |
| 4134 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 4135 | goto http_msg_closing; |
| 4136 | } |
| 4137 | else { |
| 4138 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 4139 | goto http_msg_closed; |
| 4140 | } |
| 4141 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4142 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4143 | } |
| 4144 | |
| 4145 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 4146 | http_msg_closing: |
| 4147 | /* nothing else to forward, just waiting for the output buffer |
| 4148 | * to be empty and for the shutw_now to take effect. |
| 4149 | */ |
| 4150 | if (buf->flags & BF_OUT_EMPTY) { |
| 4151 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 4152 | goto http_msg_closed; |
| 4153 | } |
| 4154 | else if (buf->flags & BF_SHUTW) { |
| 4155 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4156 | goto wait_other_side; |
| 4157 | } |
| 4158 | } |
| 4159 | |
| 4160 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 4161 | http_msg_closed: |
| 4162 | goto wait_other_side; |
| 4163 | } |
| 4164 | |
| 4165 | wait_other_side: |
| 4166 | http_silent_debug(__LINE__, s); |
| 4167 | return txn->req.msg_state != old_state || buf->flags != old_flags; |
| 4168 | } |
| 4169 | |
| 4170 | |
| 4171 | /* This function updates the response state machine according to the request |
| 4172 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 4173 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 4174 | * it is only used to find when a request/response couple is complete. Both |
| 4175 | * this function and its equivalent should loop until both return zero. It |
| 4176 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 4177 | */ |
| 4178 | int http_sync_res_state(struct session *s) |
| 4179 | { |
| 4180 | struct buffer *buf = s->rep; |
| 4181 | struct http_txn *txn = &s->txn; |
| 4182 | unsigned int old_flags = buf->flags; |
| 4183 | unsigned int old_state = txn->rsp.msg_state; |
| 4184 | |
| 4185 | http_silent_debug(__LINE__, s); |
| 4186 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 4187 | return 0; |
| 4188 | |
| 4189 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 4190 | /* In theory, we don't need to read anymore, but we must |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4191 | * still monitor the server connection for a possible close |
| 4192 | * while the request is being uploaded, so we don't disable |
| 4193 | * reading. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4194 | */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4195 | /* buffer_dont_read(buf); */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4196 | |
| 4197 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 4198 | goto wait_other_side; |
| 4199 | |
| 4200 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 4201 | /* The client seems to still be sending data, probably |
| 4202 | * because we got an error response during an upload. |
| 4203 | * We have the choice of either breaking the connection |
| 4204 | * or letting it pass through. Let's do the later. |
| 4205 | */ |
| 4206 | goto wait_other_side; |
| 4207 | } |
| 4208 | |
| 4209 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 4210 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4211 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4212 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
| 4213 | goto wait_other_side; |
| 4214 | } |
| 4215 | |
| 4216 | /* When we get here, it means that both the request and the |
| 4217 | * response have finished receiving. Depending on the connection |
| 4218 | * mode, we'll have to wait for the last bytes to leave in either |
| 4219 | * direction, and sometimes for a close to be effective. |
| 4220 | */ |
| 4221 | |
| 4222 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 4223 | /* Server-close mode : shut read and wait for the request |
| 4224 | * side to close its output buffer. The caller will detect |
| 4225 | * when we're in DONE and the other is in CLOSED and will |
| 4226 | * catch that for the final cleanup. |
| 4227 | */ |
| 4228 | if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW))) |
| 4229 | buffer_shutr_now(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4230 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4231 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4232 | /* Option forceclose is set, or either side wants to close, |
| 4233 | * let's enforce it now that we're not expecting any new |
| 4234 | * data to come. The caller knows the session is complete |
| 4235 | * once both states are CLOSED. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4236 | */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4237 | if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) { |
| 4238 | buffer_shutr_now(buf); |
| 4239 | buffer_shutw_now(buf); |
| 4240 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4241 | } |
| 4242 | else { |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4243 | /* The last possible modes are keep-alive and tunnel. Since tunnel |
| 4244 | * mode does not set the body analyser, we can't reach this place |
| 4245 | * in tunnel mode, so we're left with keep-alive only. |
| 4246 | * This mode is currently not implemented, we switch to tunnel mode. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4247 | */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4248 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4249 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4250 | } |
| 4251 | |
| 4252 | if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) { |
| 4253 | /* if we've just closed an output, let's switch */ |
| 4254 | if (!(buf->flags & BF_OUT_EMPTY)) { |
| 4255 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 4256 | goto http_msg_closing; |
| 4257 | } |
| 4258 | else { |
| 4259 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4260 | goto http_msg_closed; |
| 4261 | } |
| 4262 | } |
| 4263 | goto wait_other_side; |
| 4264 | } |
| 4265 | |
| 4266 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 4267 | http_msg_closing: |
| 4268 | /* nothing else to forward, just waiting for the output buffer |
| 4269 | * to be empty and for the shutw_now to take effect. |
| 4270 | */ |
| 4271 | if (buf->flags & BF_OUT_EMPTY) { |
| 4272 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4273 | goto http_msg_closed; |
| 4274 | } |
| 4275 | else if (buf->flags & BF_SHUTW) { |
| 4276 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4277 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4278 | if (target_srv(&s->target)) |
| 4279 | target_srv(&s->target)->counters.cli_aborts++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4280 | goto wait_other_side; |
| 4281 | } |
| 4282 | } |
| 4283 | |
| 4284 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 4285 | http_msg_closed: |
| 4286 | /* drop any pending data */ |
| 4287 | buffer_ignore(buf, buf->l - buf->send_max); |
| 4288 | buffer_auto_close(buf); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4289 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4290 | goto wait_other_side; |
| 4291 | } |
| 4292 | |
| 4293 | wait_other_side: |
| 4294 | http_silent_debug(__LINE__, s); |
| 4295 | return txn->rsp.msg_state != old_state || buf->flags != old_flags; |
| 4296 | } |
| 4297 | |
| 4298 | |
| 4299 | /* Resync the request and response state machines. Return 1 if either state |
| 4300 | * changes. |
| 4301 | */ |
| 4302 | int http_resync_states(struct session *s) |
| 4303 | { |
| 4304 | struct http_txn *txn = &s->txn; |
| 4305 | int old_req_state = txn->req.msg_state; |
| 4306 | int old_res_state = txn->rsp.msg_state; |
| 4307 | |
| 4308 | http_silent_debug(__LINE__, s); |
| 4309 | http_sync_req_state(s); |
| 4310 | while (1) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4311 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4312 | if (!http_sync_res_state(s)) |
| 4313 | break; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4314 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4315 | if (!http_sync_req_state(s)) |
| 4316 | break; |
| 4317 | } |
| 4318 | http_silent_debug(__LINE__, s); |
| 4319 | /* OK, both state machines agree on a compatible state. |
| 4320 | * There are a few cases we're interested in : |
| 4321 | * - HTTP_MSG_TUNNEL on either means we have to disable both analysers |
| 4322 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 4323 | * directions, so let's simply disable both analysers. |
| 4324 | * - HTTP_MSG_CLOSED on the response only means we must abort the |
| 4325 | * request. |
| 4326 | * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response |
| 4327 | * with server-close mode means we've completed one request and we |
| 4328 | * must re-initialize the server connection. |
| 4329 | */ |
| 4330 | |
| 4331 | if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 4332 | txn->rsp.msg_state == HTTP_MSG_TUNNEL || |
| 4333 | (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 4334 | txn->rsp.msg_state == HTTP_MSG_CLOSED)) { |
| 4335 | s->req->analysers = 0; |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4336 | buffer_auto_close(s->req); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4337 | buffer_auto_read(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4338 | s->rep->analysers = 0; |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4339 | buffer_auto_close(s->rep); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4340 | buffer_auto_read(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4341 | } |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4342 | else if (txn->rsp.msg_state == HTTP_MSG_CLOSED || |
| 4343 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4344 | txn->req.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4345 | (s->rep->flags & BF_SHUTW)) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4346 | s->rep->analysers = 0; |
| 4347 | buffer_auto_close(s->rep); |
| 4348 | buffer_auto_read(s->rep); |
| 4349 | s->req->analysers = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4350 | buffer_abort(s->req); |
| 4351 | buffer_auto_close(s->req); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4352 | buffer_auto_read(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4353 | buffer_ignore(s->req, s->req->l - s->req->send_max); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4354 | } |
| 4355 | else if (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 4356 | txn->rsp.msg_state == HTTP_MSG_DONE && |
| 4357 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) { |
| 4358 | /* server-close: terminate this server connection and |
| 4359 | * reinitialize a fresh-new transaction. |
| 4360 | */ |
| 4361 | http_end_txn_clean_session(s); |
| 4362 | } |
| 4363 | |
| 4364 | http_silent_debug(__LINE__, s); |
| 4365 | return txn->req.msg_state != old_req_state || |
| 4366 | txn->rsp.msg_state != old_res_state; |
| 4367 | } |
| 4368 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4369 | /* This function is an analyser which forwards request body (including chunk |
| 4370 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 4371 | * zero byte. The only situation where it must not be called is when we're in |
| 4372 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 4373 | * remaining data and to resync after end of body. It expects the msg_state to |
| 4374 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 4375 | * 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] | 4376 | * 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] | 4377 | * bytes of pending data + the headers if not already done (between som and sov). |
| 4378 | * It eventually adjusts som to match sov after the data in between have been sent. |
| 4379 | */ |
| 4380 | int http_request_forward_body(struct session *s, struct buffer *req, int an_bit) |
| 4381 | { |
| 4382 | struct http_txn *txn = &s->txn; |
| 4383 | struct http_msg *msg = &s->txn.req; |
| 4384 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4385 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4386 | return 0; |
| 4387 | |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 4388 | if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) || |
| 4389 | ((req->flags & BF_SHUTW) && (req->to_forward || req->send_max))) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4390 | /* Output closed while we were sending data. We must abort and |
| 4391 | * wake the other side up. |
| 4392 | */ |
| 4393 | msg->msg_state = HTTP_MSG_ERROR; |
| 4394 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 4395 | return 1; |
| 4396 | } |
| 4397 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4398 | /* in most states, we should abort in case of early close */ |
| 4399 | buffer_auto_close(req); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4400 | |
| 4401 | /* Note that we don't have to send 100-continue back because we don't |
| 4402 | * need the data to complete our job, and it's up to the server to |
| 4403 | * decide whether to return 100, 417 or anything else in return of |
| 4404 | * an "Expect: 100-continue" header. |
| 4405 | */ |
| 4406 | |
| 4407 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 4408 | /* we have msg->col and msg->sov which both point to the first |
| 4409 | * byte of message body. msg->som still points to the beginning |
| 4410 | * of the message. We must save the body in req->lr because it |
| 4411 | * survives buffer re-alignments. |
| 4412 | */ |
| 4413 | req->lr = req->data + msg->sov; |
| 4414 | if (txn->flags & TX_REQ_TE_CHNK) |
| 4415 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 4416 | else { |
| 4417 | msg->msg_state = HTTP_MSG_DATA; |
| 4418 | } |
| 4419 | } |
| 4420 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4421 | while (1) { |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 4422 | int bytes; |
| 4423 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4424 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 4425 | /* we may have some data pending */ |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 4426 | bytes = msg->sov - msg->som; |
| 4427 | if (msg->chunk_len || bytes) { |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 4428 | msg->som = msg->sov; |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 4429 | if (likely(bytes < 0)) /* sov may have wrapped at the end */ |
| 4430 | bytes += req->size; |
| 4431 | msg->chunk_len += (unsigned int)bytes; |
| 4432 | msg->chunk_len -= buffer_forward(req, msg->chunk_len); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 4433 | } |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 4434 | |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4435 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 4436 | /* must still forward */ |
| 4437 | if (req->to_forward) |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4438 | goto missing_data; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4439 | |
| 4440 | /* nothing left to forward */ |
| 4441 | if (txn->flags & TX_REQ_TE_CHNK) |
| 4442 | msg->msg_state = HTTP_MSG_DATA_CRLF; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4443 | else |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4444 | msg->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4445 | } |
| 4446 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4447 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4448 | * set ->sov and ->lr to point to the body and switch to DATA or |
| 4449 | * TRAILERS state. |
| 4450 | */ |
| 4451 | int ret = http_parse_chunk_size(req, msg); |
| 4452 | |
| 4453 | if (!ret) |
| 4454 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4455 | else if (ret < 0) { |
| 4456 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4457 | if (msg->err_pos >= 0) |
| 4458 | 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] | 4459 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4460 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4461 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4462 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4463 | else if (msg->msg_state == HTTP_MSG_DATA_CRLF) { |
| 4464 | /* we want the CRLF after the data */ |
| 4465 | int ret; |
| 4466 | |
Willy Tarreau | d3347ee | 2010-01-04 02:02:25 +0100 | [diff] [blame] | 4467 | req->lr = req->w + req->send_max; |
| 4468 | if (req->lr >= req->data + req->size) |
| 4469 | req->lr -= req->size; |
| 4470 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4471 | ret = http_skip_chunk_crlf(req, msg); |
| 4472 | |
| 4473 | if (ret == 0) |
| 4474 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4475 | else if (ret < 0) { |
| 4476 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4477 | if (msg->err_pos >= 0) |
| 4478 | 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] | 4479 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4480 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4481 | /* we're in MSG_CHUNK_SIZE now */ |
| 4482 | } |
| 4483 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
| 4484 | int ret = http_forward_trailers(req, msg); |
| 4485 | |
| 4486 | if (ret == 0) |
| 4487 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4488 | else if (ret < 0) { |
| 4489 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4490 | if (msg->err_pos >= 0) |
| 4491 | 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] | 4492 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4493 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4494 | /* we're in HTTP_MSG_DONE now */ |
| 4495 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4496 | else { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4497 | int old_state = msg->msg_state; |
| 4498 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4499 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4500 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4501 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 4502 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
| 4503 | buffer_dont_close(req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4504 | if (http_resync_states(s)) { |
| 4505 | /* some state changes occurred, maybe the analyser |
| 4506 | * was disabled too. |
Willy Tarreau | face839 | 2010-01-03 11:37:54 +0100 | [diff] [blame] | 4507 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 4508 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 4509 | if (req->flags & BF_SHUTW) { |
| 4510 | /* request errors are most likely due to |
| 4511 | * the server aborting the transfer. |
| 4512 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4513 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 4514 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4515 | if (msg->err_pos >= 0) |
| 4516 | 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] | 4517 | goto return_bad_req; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 4518 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4519 | return 1; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4520 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 4521 | |
| 4522 | /* If "option abortonclose" is set on the backend, we |
| 4523 | * want to monitor the client's connection and forward |
| 4524 | * any shutdown notification to the server, which will |
| 4525 | * decide whether to close or to go on processing the |
| 4526 | * request. |
| 4527 | */ |
| 4528 | if (s->be->options & PR_O_ABRT_CLOSE) { |
| 4529 | buffer_auto_read(req); |
| 4530 | buffer_auto_close(req); |
| 4531 | } |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4532 | else if (s->txn.meth == HTTP_METH_POST) { |
| 4533 | /* POST requests may require to read extra CRLF |
| 4534 | * sent by broken browsers and which could cause |
| 4535 | * an RST to be sent upon close on some systems |
| 4536 | * (eg: Linux). |
| 4537 | */ |
| 4538 | buffer_auto_read(req); |
| 4539 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 4540 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4541 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4542 | } |
| 4543 | } |
| 4544 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4545 | missing_data: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4546 | /* stop waiting for data if the input is closed before the end */ |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4547 | if (req->flags & BF_SHUTR) { |
| 4548 | if (!(s->flags & SN_ERR_MASK)) |
| 4549 | s->flags |= SN_ERR_CLICL; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4550 | if (!(s->flags & SN_FINST_MASK)) { |
| 4551 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 4552 | s->flags |= SN_FINST_H; |
| 4553 | else |
| 4554 | s->flags |= SN_FINST_D; |
| 4555 | } |
| 4556 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4557 | s->fe->fe_counters.cli_aborts++; |
| 4558 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4559 | if (target_srv(&s->target)) |
| 4560 | target_srv(&s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4561 | |
| 4562 | goto return_bad_req_stats_ok; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4563 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4564 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4565 | /* waiting for the last bits to leave the buffer */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4566 | if (req->flags & BF_SHUTW) |
| 4567 | goto aborted_xfer; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4568 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4569 | /* When TE: chunked is used, we need to get there again to parse remaining |
| 4570 | * chunks even if the client has closed, so we don't want to set BF_DONTCLOSE. |
| 4571 | */ |
| 4572 | if (txn->flags & TX_REQ_TE_CHNK) |
| 4573 | buffer_dont_close(req); |
| 4574 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4575 | /* 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] | 4576 | * what we did. So we always set the BF_EXPECT_MORE flag so that the |
| 4577 | * system knows it must not set a PUSH on this first part. Interactive |
| 4578 | * modes are already handled by the stream sock layer. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4579 | */ |
Willy Tarreau | 0729303 | 2011-05-30 18:29:28 +0200 | [diff] [blame] | 4580 | req->flags |= BF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4581 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4582 | http_silent_debug(__LINE__, s); |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4583 | return 0; |
| 4584 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4585 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4586 | s->fe->fe_counters.failed_req++; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4587 | if (s->listener->counters) |
| 4588 | s->listener->counters->failed_req++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4589 | return_bad_req_stats_ok: |
| 4590 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4591 | if (txn->status) { |
| 4592 | /* Note: we don't send any error if some data were already sent */ |
| 4593 | stream_int_retnclose(req->prod, NULL); |
| 4594 | } else { |
| 4595 | txn->status = 400; |
| 4596 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
| 4597 | } |
| 4598 | req->analysers = 0; |
| 4599 | 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] | 4600 | |
| 4601 | if (!(s->flags & SN_ERR_MASK)) |
| 4602 | s->flags |= SN_ERR_PRXCOND; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4603 | if (!(s->flags & SN_FINST_MASK)) { |
| 4604 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 4605 | s->flags |= SN_FINST_H; |
| 4606 | else |
| 4607 | s->flags |= SN_FINST_D; |
| 4608 | } |
| 4609 | return 0; |
| 4610 | |
| 4611 | aborted_xfer: |
| 4612 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4613 | if (txn->status) { |
| 4614 | /* Note: we don't send any error if some data were already sent */ |
| 4615 | stream_int_retnclose(req->prod, NULL); |
| 4616 | } else { |
| 4617 | txn->status = 502; |
| 4618 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_502)); |
| 4619 | } |
| 4620 | req->analysers = 0; |
| 4621 | s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */ |
| 4622 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4623 | s->fe->fe_counters.srv_aborts++; |
| 4624 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4625 | if (target_srv(&s->target)) |
| 4626 | target_srv(&s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4627 | |
| 4628 | if (!(s->flags & SN_ERR_MASK)) |
| 4629 | s->flags |= SN_ERR_SRVCL; |
| 4630 | if (!(s->flags & SN_FINST_MASK)) { |
| 4631 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 4632 | s->flags |= SN_FINST_H; |
| 4633 | else |
| 4634 | s->flags |= SN_FINST_D; |
| 4635 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4636 | return 0; |
| 4637 | } |
| 4638 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4639 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 4640 | * processing can continue on next analysers, or zero if it either needs more |
| 4641 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
| 4642 | * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers |
| 4643 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 4644 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4645 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4646 | 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] | 4647 | { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4648 | struct http_txn *txn = &s->txn; |
| 4649 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4650 | struct hdr_ctx ctx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4651 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4652 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4653 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4654 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 4655 | 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] | 4656 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4657 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 4658 | rep, |
| 4659 | rep->rex, rep->wex, |
| 4660 | rep->flags, |
| 4661 | rep->l, |
| 4662 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 4663 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4664 | /* |
| 4665 | * Now parse the partial (or complete) lines. |
| 4666 | * We will check the response syntax, and also join multi-line |
| 4667 | * headers. An index of all the lines will be elaborated while |
| 4668 | * parsing. |
| 4669 | * |
| 4670 | * For the parsing, we use a 28 states FSM. |
| 4671 | * |
| 4672 | * Here is the information we currently have : |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4673 | * rep->data + msg->som = beginning of response |
| 4674 | * rep->data + msg->eoh = end of processed headers / start of current one |
| 4675 | * msg->eol = end of current header or line (LF or CRLF) |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4676 | * rep->lr = first non-visited byte |
| 4677 | * rep->r = end of data |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4678 | * Once we reach MSG_BODY, rep->sol = rep->data + msg->som |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4679 | */ |
| 4680 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4681 | /* There's a protected area at the end of the buffer for rewriting |
| 4682 | * purposes. We don't want to start to parse the request if the |
| 4683 | * protected area is affected, because we may have to move processed |
| 4684 | * data later, which is much more complicated. |
| 4685 | */ |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 4686 | if (rep->l && msg->msg_state < HTTP_MSG_ERROR) { |
| 4687 | if (unlikely((rep->flags & BF_FULL) || |
| 4688 | rep->r < rep->lr || |
| 4689 | rep->r > rep->data + rep->size - global.tune.maxrewrite)) { |
| 4690 | if (rep->send_max) { |
| 4691 | /* 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] | 4692 | if (rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) |
| 4693 | goto abort_response; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 4694 | buffer_dont_close(rep); |
| 4695 | rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 4696 | return 0; |
| 4697 | } |
| 4698 | if (rep->l <= rep->size - global.tune.maxrewrite) |
| 4699 | http_buffer_heavy_realign(rep, msg); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4700 | } |
| 4701 | |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 4702 | if (likely(rep->lr < rep->r)) |
| 4703 | http_msg_analyzer(rep, msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4704 | } |
| 4705 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4706 | /* 1: we might have to print this header in debug mode */ |
| 4707 | if (unlikely((global.mode & MODE_DEBUG) && |
| 4708 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | c3bfeeb | 2010-04-16 09:14:45 +0200 | [diff] [blame] | 4709 | msg->sol && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4710 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4711 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4712 | |
Willy Tarreau | 663308b | 2010-06-07 14:06:08 +0200 | [diff] [blame] | 4713 | sol = rep->data + msg->som; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 4714 | eol = sol + msg->sl.st.l; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4715 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4716 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4717 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 4718 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4719 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4720 | while (cur_idx) { |
| 4721 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 4722 | debug_hdr("srvhdr", s, sol, eol); |
| 4723 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 4724 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 4725 | } |
| 4726 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4727 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4728 | /* |
| 4729 | * Now we quickly check if we have found a full valid response. |
| 4730 | * If not so, we check the FD and buffer states before leaving. |
| 4731 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4732 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4733 | * responses are checked first. |
| 4734 | * |
| 4735 | * Depending on whether the client is still there or not, we |
| 4736 | * may send an error response back or not. Note that normally |
| 4737 | * we should only check for HTTP status there, and check I/O |
| 4738 | * errors somewhere else. |
| 4739 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4740 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4741 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4742 | /* Invalid response */ |
| 4743 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 4744 | /* we detected a parsing error. We want to archive this response |
| 4745 | * in the dedicated proxy area for later troubleshooting. |
| 4746 | */ |
| 4747 | hdr_response_bad: |
| 4748 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 4749 | 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] | 4750 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4751 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4752 | if (target_srv(&s->target)) { |
| 4753 | target_srv(&s->target)->counters.failed_resp++; |
| 4754 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4755 | } |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 4756 | abort_response: |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4757 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4758 | rep->analysers = 0; |
| 4759 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4760 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 0b89fbb | 2010-02-02 09:57:24 +0100 | [diff] [blame] | 4761 | buffer_ignore(rep, rep->l - rep->send_max); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4762 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
| 4763 | |
| 4764 | if (!(s->flags & SN_ERR_MASK)) |
| 4765 | s->flags |= SN_ERR_PRXCOND; |
| 4766 | if (!(s->flags & SN_FINST_MASK)) |
| 4767 | s->flags |= SN_FINST_H; |
| 4768 | |
| 4769 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4770 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4771 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4772 | /* too large response does not fit in buffer. */ |
| 4773 | else if (rep->flags & BF_FULL) { |
| 4774 | goto hdr_response_bad; |
| 4775 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4776 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4777 | /* read error */ |
| 4778 | else if (rep->flags & BF_READ_ERROR) { |
| 4779 | if (msg->err_pos >= 0) |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 4780 | 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] | 4781 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4782 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4783 | if (target_srv(&s->target)) { |
| 4784 | target_srv(&s->target)->counters.failed_resp++; |
| 4785 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_ERROR); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4786 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 4787 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4788 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4789 | rep->analysers = 0; |
| 4790 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4791 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 0b89fbb | 2010-02-02 09:57:24 +0100 | [diff] [blame] | 4792 | buffer_ignore(rep, rep->l - rep->send_max); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4793 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 4794 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4795 | if (!(s->flags & SN_ERR_MASK)) |
| 4796 | s->flags |= SN_ERR_SRVCL; |
| 4797 | if (!(s->flags & SN_FINST_MASK)) |
| 4798 | s->flags |= SN_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 4799 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4800 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4801 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4802 | /* read timeout : return a 504 to the client. */ |
| 4803 | else if (rep->flags & BF_READ_TIMEOUT) { |
| 4804 | if (msg->err_pos >= 0) |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 4805 | 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] | 4806 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4807 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4808 | if (target_srv(&s->target)) { |
| 4809 | target_srv(&s->target)->counters.failed_resp++; |
| 4810 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4811 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4812 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4813 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4814 | rep->analysers = 0; |
| 4815 | txn->status = 504; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4816 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 0b89fbb | 2010-02-02 09:57:24 +0100 | [diff] [blame] | 4817 | buffer_ignore(rep, rep->l - rep->send_max); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4818 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504)); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4819 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4820 | if (!(s->flags & SN_ERR_MASK)) |
| 4821 | s->flags |= SN_ERR_SRVTO; |
| 4822 | if (!(s->flags & SN_FINST_MASK)) |
| 4823 | s->flags |= SN_FINST_H; |
| 4824 | return 0; |
| 4825 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 4826 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4827 | /* close from server */ |
| 4828 | else if (rep->flags & BF_SHUTR) { |
| 4829 | if (msg->err_pos >= 0) |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 4830 | 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] | 4831 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4832 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4833 | if (target_srv(&s->target)) { |
| 4834 | target_srv(&s->target)->counters.failed_resp++; |
| 4835 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4836 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4837 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4838 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4839 | rep->analysers = 0; |
| 4840 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4841 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 0b89fbb | 2010-02-02 09:57:24 +0100 | [diff] [blame] | 4842 | buffer_ignore(rep, rep->l - rep->send_max); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4843 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4844 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4845 | if (!(s->flags & SN_ERR_MASK)) |
| 4846 | s->flags |= SN_ERR_SRVCL; |
| 4847 | if (!(s->flags & SN_FINST_MASK)) |
| 4848 | s->flags |= SN_FINST_H; |
| 4849 | return 0; |
| 4850 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4851 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4852 | /* write error to client (we don't send any message then) */ |
| 4853 | else if (rep->flags & BF_WRITE_ERROR) { |
| 4854 | if (msg->err_pos >= 0) |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 4855 | 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] | 4856 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4857 | s->be->be_counters.failed_resp++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4858 | rep->analysers = 0; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4859 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4860 | |
| 4861 | if (!(s->flags & SN_ERR_MASK)) |
| 4862 | s->flags |= SN_ERR_CLICL; |
| 4863 | if (!(s->flags & SN_FINST_MASK)) |
| 4864 | s->flags |= SN_FINST_H; |
| 4865 | |
| 4866 | /* process_session() will take care of the error */ |
| 4867 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4868 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4869 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4870 | buffer_dont_close(rep); |
| 4871 | return 0; |
| 4872 | } |
| 4873 | |
| 4874 | /* More interesting part now : we know that we have a complete |
| 4875 | * response which at least looks like HTTP. We have an indicator |
| 4876 | * of each header's length, so we can parse them quickly. |
| 4877 | */ |
| 4878 | |
| 4879 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 4880 | 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] | 4881 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4882 | /* |
| 4883 | * 1: get the status code |
| 4884 | */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4885 | n = msg->sol[msg->sl.st.c] - '0'; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4886 | if (n < 1 || n > 5) |
| 4887 | n = 0; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4888 | /* when the client triggers a 4xx from the server, it's most often due |
| 4889 | * to a missing object or permission. These events should be tracked |
| 4890 | * because if they happen often, it may indicate a brute force or a |
| 4891 | * vulnerability scan. |
| 4892 | */ |
| 4893 | if (n == 4) |
| 4894 | session_inc_http_err_ctr(s); |
| 4895 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4896 | if (target_srv(&s->target)) |
| 4897 | target_srv(&s->target)->counters.p.http.rsp[n]++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4898 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4899 | /* check if the response is HTTP/1.1 or above */ |
| 4900 | if ((msg->sl.st.v_l == 8) && |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4901 | ((msg->sol[5] > '1') || |
| 4902 | ((msg->sol[5] == '1') && |
| 4903 | (msg->sol[7] >= '1')))) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4904 | txn->flags |= TX_RES_VER_11; |
| 4905 | |
| 4906 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4907 | 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] | 4908 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4909 | /* transfer length unknown*/ |
| 4910 | txn->flags &= ~TX_RES_XFER_LEN; |
| 4911 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4912 | 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] | 4913 | |
Willy Tarreau | 3965040 | 2010-03-15 19:44:39 +0100 | [diff] [blame] | 4914 | /* Adjust server's health based on status code. Note: status codes 501 |
| 4915 | * and 505 are triggered on demand by client request, so we must not |
| 4916 | * count them as server failures. |
| 4917 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4918 | if (target_srv(&s->target)) { |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 4919 | 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] | 4920 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_OK); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 4921 | else |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4922 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_STS); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 4923 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4924 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4925 | /* |
| 4926 | * 2: check for cacheability. |
| 4927 | */ |
| 4928 | |
| 4929 | switch (txn->status) { |
| 4930 | case 200: |
| 4931 | case 203: |
| 4932 | case 206: |
| 4933 | case 300: |
| 4934 | case 301: |
| 4935 | case 410: |
| 4936 | /* RFC2616 @13.4: |
| 4937 | * "A response received with a status code of |
| 4938 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 4939 | * by a cache (...) unless a cache-control |
| 4940 | * directive prohibits caching." |
| 4941 | * |
| 4942 | * RFC2616 @9.5: POST method : |
| 4943 | * "Responses to this method are not cacheable, |
| 4944 | * unless the response includes appropriate |
| 4945 | * Cache-Control or Expires header fields." |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4946 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4947 | if (likely(txn->meth != HTTP_METH_POST) && |
| 4948 | (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC))) |
| 4949 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 4950 | break; |
| 4951 | default: |
| 4952 | break; |
| 4953 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4954 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4955 | /* |
| 4956 | * 3: we may need to capture headers |
| 4957 | */ |
| 4958 | s->logs.logwait &= ~LW_RESP; |
| 4959 | if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap)) |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4960 | capture_headers(msg->sol, &txn->hdr_idx, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4961 | txn->rsp.cap, s->fe->rsp_cap); |
| 4962 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4963 | /* 4: determine the transfer-length. |
| 4964 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 4965 | * the presence of a message-body in a RESPONSE and its transfer length |
| 4966 | * must be determined that way : |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4967 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4968 | * All responses to the HEAD request method MUST NOT include a |
| 4969 | * message-body, even though the presence of entity-header fields |
| 4970 | * might lead one to believe they do. All 1xx (informational), 204 |
| 4971 | * (No Content), and 304 (Not Modified) responses MUST NOT include a |
| 4972 | * message-body. All other responses do include a message-body, |
| 4973 | * although it MAY be of zero length. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4974 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4975 | * 1. Any response which "MUST NOT" include a message-body (such as the |
| 4976 | * 1xx, 204 and 304 responses and any response to a HEAD request) is |
| 4977 | * always terminated by the first empty line after the header fields, |
| 4978 | * regardless of the entity-header fields present in the message. |
| 4979 | * |
| 4980 | * 2. If a Transfer-Encoding header field (Section 9.7) is present and |
| 4981 | * the "chunked" transfer-coding (Section 6.2) is used, the |
| 4982 | * transfer-length is defined by the use of this transfer-coding. |
| 4983 | * If a Transfer-Encoding header field is present and the "chunked" |
| 4984 | * transfer-coding is not present, the transfer-length is defined by |
| 4985 | * the sender closing the connection. |
| 4986 | * |
| 4987 | * 3. If a Content-Length header field is present, its decimal value in |
| 4988 | * OCTETs represents both the entity-length and the transfer-length. |
| 4989 | * If a message is received with both a Transfer-Encoding header |
| 4990 | * field and a Content-Length header field, the latter MUST be ignored. |
| 4991 | * |
| 4992 | * 4. If the message uses the media type "multipart/byteranges", and |
| 4993 | * the transfer-length is not otherwise specified, then this self- |
| 4994 | * delimiting media type defines the transfer-length. This media |
| 4995 | * type MUST NOT be used unless the sender knows that the recipient |
| 4996 | * can parse it; the presence in a request of a Range header with |
| 4997 | * multiple byte-range specifiers from a 1.1 client implies that the |
| 4998 | * client can parse multipart/byteranges responses. |
| 4999 | * |
| 5000 | * 5. By the server closing the connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5001 | */ |
| 5002 | |
| 5003 | /* Skip parsing if no content length is possible. The response flags |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5004 | * 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] | 5005 | * 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] | 5006 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 5007 | */ |
| 5008 | if (txn->meth == HTTP_METH_HEAD || |
| 5009 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5010 | txn->status == 204 || txn->status == 304) { |
| 5011 | txn->flags |= TX_RES_XFER_LEN; |
| 5012 | goto skip_content_length; |
| 5013 | } |
| 5014 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5015 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5016 | ctx.idx = 0; |
Willy Tarreau | 9e13c3c | 2009-12-22 09:59:58 +0100 | [diff] [blame] | 5017 | while ((txn->flags & TX_RES_VER_11) && |
| 5018 | http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5019 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
| 5020 | txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN); |
| 5021 | else if (txn->flags & TX_RES_TE_CHNK) { |
| 5022 | /* bad transfer-encoding (chunked followed by something else) */ |
| 5023 | use_close_only = 1; |
| 5024 | txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN); |
| 5025 | break; |
| 5026 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5027 | } |
| 5028 | |
| 5029 | /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */ |
| 5030 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5031 | while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only && |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5032 | http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) { |
| 5033 | signed long long cl; |
| 5034 | |
| 5035 | if (!ctx.vlen) |
| 5036 | goto hdr_response_bad; |
| 5037 | |
| 5038 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) |
| 5039 | goto hdr_response_bad; /* parse failure */ |
| 5040 | |
| 5041 | if (cl < 0) |
| 5042 | goto hdr_response_bad; |
| 5043 | |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5044 | if ((txn->flags & TX_RES_CNT_LEN) && (msg->chunk_len != cl)) |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5045 | goto hdr_response_bad; /* already specified, was different */ |
| 5046 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5047 | txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5048 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5049 | } |
| 5050 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5051 | /* FIXME: we should also implement the multipart/byterange method. |
| 5052 | * For now on, we resort to close mode in this case (unknown length). |
| 5053 | */ |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5054 | skip_content_length: |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5055 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5056 | /* end of job, return OK */ |
| 5057 | rep->analysers &= ~an_bit; |
| 5058 | rep->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5059 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5060 | return 1; |
| 5061 | } |
| 5062 | |
| 5063 | /* This function performs all the processing enabled for the current response. |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 5064 | * It normally returns 1 unless it wants to break. It relies on buffers flags, |
| 5065 | * and updates t->rep->analysers. It might make sense to explode it into several |
| 5066 | * other functions. It works like process_request (see indications above). |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5067 | */ |
| 5068 | int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px) |
| 5069 | { |
| 5070 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5071 | struct http_msg *msg = &txn->rsp; |
| 5072 | struct proxy *cur_proxy; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 5073 | struct cond_wordlist *wl; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5074 | |
| 5075 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 5076 | now_ms, __FUNCTION__, |
| 5077 | t, |
| 5078 | rep, |
| 5079 | rep->rex, rep->wex, |
| 5080 | rep->flags, |
| 5081 | rep->l, |
| 5082 | rep->analysers); |
| 5083 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5084 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5085 | return 0; |
| 5086 | |
| 5087 | rep->analysers &= ~an_bit; |
| 5088 | rep->analyse_exp = TICK_ETERNITY; |
| 5089 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5090 | /* Now we have to check if we need to modify the Connection header. |
| 5091 | * This is more difficult on the response than it is on the request, |
| 5092 | * because we can have two different HTTP versions and we don't know |
| 5093 | * how the client will interprete a response. For instance, let's say |
| 5094 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 5095 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 5096 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 5097 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 5098 | * the lack of header as a keep-alive. Thus we will use two flags |
| 5099 | * indicating how a request MAY be understood by the client. In case |
| 5100 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 5101 | * ambiguous cases such as both close and keepalive are seen, then we |
| 5102 | * will fall back to explicit close. Note that we won't take risks with |
| 5103 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5104 | * See doc/internals/connection-header.txt for the complete matrix. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5105 | */ |
| 5106 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5107 | if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || |
| 5108 | txn->status == 101)) { |
| 5109 | /* Either we've established an explicit tunnel, or we're |
| 5110 | * switching the protocol. In both cases, we're very unlikely |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5111 | * to understand the next protocols. We have to switch to tunnel |
| 5112 | * mode, so that we transfer the request and responses then let |
| 5113 | * this protocol pass unmodified. When we later implement specific |
| 5114 | * parsers for such protocols, we'll want to check the Upgrade |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5115 | * header which contains information about that protocol for |
| 5116 | * responses with status 101 (eg: see RFC2817 about TLS). |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5117 | */ |
| 5118 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN; |
| 5119 | } |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5120 | else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) && |
| 5121 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN || |
| 5122 | ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5123 | int to_del = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5124 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5125 | /* on unknown transfer length, we must close */ |
| 5126 | if (!(txn->flags & TX_RES_XFER_LEN) && |
| 5127 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 5128 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5129 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5130 | /* now adjust header transformations depending on current state */ |
| 5131 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN || |
| 5132 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5133 | to_del |= 2; /* remove "keep-alive" on any response */ |
| 5134 | if (!(txn->flags & TX_RES_VER_11)) |
| 5135 | to_del |= 1; /* remove "close" for HTTP/1.0 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5136 | } |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5137 | else { /* SCL / KAL */ |
| 5138 | to_del |= 1; /* remove "close" on any response */ |
| 5139 | if ((txn->flags & (TX_RES_VER_11|TX_REQ_VER_11)) == (TX_RES_VER_11|TX_REQ_VER_11)) |
| 5140 | to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5141 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5142 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5143 | /* Parse and remove some headers from the connection header */ |
| 5144 | http_parse_connection_header(txn, msg, rep, to_del); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5145 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5146 | /* Some keep-alive responses are converted to Server-close if |
| 5147 | * the server wants to close. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5148 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5149 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) { |
| 5150 | if ((txn->flags & TX_HDR_CONN_CLO) || |
| 5151 | (txn->flags & (TX_HDR_CONN_KAL|TX_RES_VER_11)) == 0) |
| 5152 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5153 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5154 | } |
| 5155 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5156 | if (1) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5157 | /* |
| 5158 | * 3: we will have to evaluate the filters. |
| 5159 | * As opposed to version 1.2, now they will be evaluated in the |
| 5160 | * filters order and not in the header order. This means that |
| 5161 | * each filter has to be validated among all headers. |
| 5162 | * |
| 5163 | * Filters are tried with ->be first, then with ->fe if it is |
| 5164 | * different from ->be. |
| 5165 | */ |
| 5166 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5167 | cur_proxy = t->be; |
| 5168 | while (1) { |
| 5169 | struct proxy *rule_set = cur_proxy; |
| 5170 | |
| 5171 | /* try headers filters */ |
| 5172 | if (rule_set->rsp_exp != NULL) { |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 5173 | if (apply_filters_to_response(t, rep, rule_set) < 0) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5174 | return_bad_resp: |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5175 | if (target_srv(&t->target)) { |
| 5176 | target_srv(&t->target)->counters.failed_resp++; |
| 5177 | health_adjust(target_srv(&t->target), HANA_STATUS_HTTP_RSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5178 | } |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5179 | t->be->be_counters.failed_resp++; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5180 | return_srv_prx_502: |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 5181 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5182 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5183 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 0b89fbb | 2010-02-02 09:57:24 +0100 | [diff] [blame] | 5184 | buffer_ignore(rep, rep->l - rep->send_max); |
Willy Tarreau | 8e89b84 | 2009-10-18 23:56:35 +0200 | [diff] [blame] | 5185 | stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502)); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5186 | if (!(t->flags & SN_ERR_MASK)) |
| 5187 | t->flags |= SN_ERR_PRXCOND; |
| 5188 | if (!(t->flags & SN_FINST_MASK)) |
| 5189 | t->flags |= SN_FINST_H; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 5190 | return 0; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5191 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5192 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5193 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5194 | /* has the response been denied ? */ |
| 5195 | if (txn->flags & TX_SVDENY) { |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5196 | if (target_srv(&t->target)) |
| 5197 | target_srv(&t->target)->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5198 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5199 | t->be->be_counters.denied_resp++; |
| 5200 | t->fe->fe_counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5201 | if (t->listener->counters) |
| 5202 | t->listener->counters->denied_resp++; |
| 5203 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5204 | goto return_srv_prx_502; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 5205 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5206 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5207 | /* add response headers from the rule sets in the same order */ |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 5208 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 5209 | if (txn->status < 200) |
| 5210 | break; |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 5211 | if (wl->cond) { |
| 5212 | int ret = acl_exec_cond(wl->cond, px, t, txn, ACL_DIR_RTR); |
| 5213 | ret = acl_pass(ret); |
| 5214 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 5215 | ret = !ret; |
| 5216 | if (!ret) |
| 5217 | continue; |
| 5218 | } |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 5219 | 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] | 5220 | goto return_bad_resp; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 5221 | } |
| 5222 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5223 | /* check whether we're already working on the frontend */ |
| 5224 | if (cur_proxy == t->fe) |
| 5225 | break; |
| 5226 | cur_proxy = t->fe; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5227 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5228 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5229 | /* |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5230 | * We may be facing a 100-continue response, in which case this |
| 5231 | * is not the right response, and we're waiting for the next one. |
| 5232 | * 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] | 5233 | * next one. |
| 5234 | */ |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5235 | if (unlikely(txn->status == 100)) { |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5236 | hdr_idx_init(&txn->hdr_idx); |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5237 | buffer_forward(rep, rep->lr - msg->sol); |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5238 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 5239 | txn->status = 0; |
| 5240 | rep->analysers |= AN_RES_WAIT_HTTP | an_bit; |
| 5241 | return 1; |
| 5242 | } |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5243 | else if (unlikely(txn->status < 200)) |
| 5244 | goto skip_header_mangling; |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5245 | |
| 5246 | /* we don't have any 1xx status code now */ |
| 5247 | |
| 5248 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5249 | * 4: check for server cookie. |
| 5250 | */ |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5251 | if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name || |
| 5252 | (t->be->options & PR_O_CHK_CACHE)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5253 | manage_server_side_cookies(t, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5254 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5255 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5256 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5257 | * 5: check for cache-control or pragma headers if required. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5258 | */ |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5259 | 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] | 5260 | check_response_for_cacheability(t, rep); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5261 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5262 | /* |
| 5263 | * 6: add server cookie in the response if needed |
| 5264 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5265 | if (target_srv(&t->target) && (t->be->options & PR_O_COOK_INS) && |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 5266 | !((txn->flags & TX_SCK_FOUND) && (t->be->options2 & PR_O2_COOK_PSV)) && |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5267 | (!(t->flags & SN_DIRECT) || |
| 5268 | ((t->be->cookie_maxidle || txn->cookie_last_date) && |
| 5269 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 5270 | (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 5271 | (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 5272 | (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST)) && |
| 5273 | !(t->flags & SN_IGNORE_PRST)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5274 | int len; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5275 | /* the server is known, it's not the one the client requested, or the |
| 5276 | * cookie's last seen date needs to be refreshed. We have to |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5277 | * insert a set-cookie here, except if we want to insert only on POST |
| 5278 | * requests and this one isn't. Note that servers which don't have cookies |
| 5279 | * (eg: some backup servers) will return a full cookie removal request. |
| 5280 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5281 | if (!target_srv(&t->target)->cookie) { |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5282 | len = sprintf(trash, |
| 5283 | "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
| 5284 | t->be->cookie_name); |
| 5285 | } |
| 5286 | else { |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5287 | 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] | 5288 | |
| 5289 | if (t->be->cookie_maxidle || t->be->cookie_maxlife) { |
| 5290 | /* emit last_date, which is mandatory */ |
| 5291 | trash[len++] = COOKIE_DELIM_DATE; |
| 5292 | s30tob64((date.tv_sec+3) >> 2, trash + len); len += 5; |
| 5293 | if (t->be->cookie_maxlife) { |
| 5294 | /* emit first_date, which is either the original one or |
| 5295 | * the current date. |
| 5296 | */ |
| 5297 | trash[len++] = COOKIE_DELIM_DATE; |
| 5298 | s30tob64(txn->cookie_first_date ? |
| 5299 | txn->cookie_first_date >> 2 : |
| 5300 | (date.tv_sec+3) >> 2, trash + len); |
| 5301 | len += 5; |
| 5302 | } |
| 5303 | } |
| 5304 | len += sprintf(trash + len, "; path=/"); |
| 5305 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5306 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5307 | if (t->be->cookie_domain) |
| 5308 | len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5309 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5310 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 5311 | trash, len) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5312 | goto return_bad_resp; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5313 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 5314 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5315 | if (target_srv(&t->target)->cookie && (t->flags & SN_DIRECT)) |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5316 | /* the server did not change, only the date was updated */ |
| 5317 | txn->flags |= TX_SCK_UPDATED; |
| 5318 | else |
| 5319 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5320 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5321 | /* Here, we will tell an eventual cache on the client side that we don't |
| 5322 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 5323 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 5324 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 5325 | */ |
| 5326 | if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5327 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5328 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 5329 | |
| 5330 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 5331 | "Cache-control: private", 22) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5332 | goto return_bad_resp; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5333 | } |
| 5334 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5335 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5336 | /* |
| 5337 | * 7: check if result will be cacheable with a cookie. |
| 5338 | * We'll block the response if security checks have caught |
| 5339 | * nasty things such as a cacheable cookie. |
| 5340 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 5341 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 5342 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5343 | (t->be->options & PR_O_CHK_CACHE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5344 | |
| 5345 | /* we're in presence of a cacheable response containing |
| 5346 | * a set-cookie header. We'll block it as requested by |
| 5347 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5348 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5349 | if (target_srv(&t->target)) |
| 5350 | target_srv(&t->target)->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5351 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5352 | t->be->be_counters.denied_resp++; |
| 5353 | t->fe->fe_counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5354 | if (t->listener->counters) |
| 5355 | t->listener->counters->denied_resp++; |
| 5356 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5357 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5358 | 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] | 5359 | send_log(t->be, LOG_ALERT, |
| 5360 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5361 | 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] | 5362 | goto return_srv_prx_502; |
| 5363 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5364 | |
| 5365 | /* |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5366 | * 8: adjust "Connection: close" or "Connection: keep-alive" if needed. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5367 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5368 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
| 5369 | ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) { |
| 5370 | unsigned int want_flags = 0; |
| 5371 | |
| 5372 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5373 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5374 | /* we want a keep-alive response here. Keep-alive header |
| 5375 | * required if either side is not 1.1. |
| 5376 | */ |
| 5377 | if ((txn->flags & (TX_REQ_VER_11|TX_RES_VER_11)) != (TX_REQ_VER_11|TX_RES_VER_11)) |
| 5378 | want_flags |= TX_CON_KAL_SET; |
| 5379 | } |
| 5380 | else { |
| 5381 | /* we want a close response here. Close header required if |
| 5382 | * the server is 1.1, regardless of the client. |
| 5383 | */ |
| 5384 | if (txn->flags & TX_RES_VER_11) |
| 5385 | want_flags |= TX_CON_CLO_SET; |
| 5386 | } |
| 5387 | |
| 5388 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 5389 | http_change_connection_header(txn, msg, rep, want_flags); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5390 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5391 | |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5392 | skip_header_mangling: |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5393 | if ((txn->flags & TX_RES_XFER_LEN) || |
| 5394 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5395 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 5396 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5397 | /************************************************************* |
| 5398 | * OK, that's finished for the headers. We have done what we * |
| 5399 | * could. Let's switch to the DATA state. * |
| 5400 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5401 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5402 | t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5403 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5404 | /* if the user wants to log as soon as possible, without counting |
| 5405 | * bytes from the server, then this is the right moment. We have |
| 5406 | * to temporarily assign bytes_out to log what we currently have. |
| 5407 | */ |
| 5408 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 5409 | t->logs.t_close = t->logs.t_data; /* to get a valid end date */ |
| 5410 | t->logs.bytes_out = txn->rsp.eoh; |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 5411 | t->do_log(t); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5412 | t->logs.bytes_out = 0; |
| 5413 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5414 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5415 | /* Note: we must not try to cheat by jumping directly to DATA, |
| 5416 | * otherwise we would not let the client side wake up. |
| 5417 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5418 | |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 5419 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5420 | } |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 5421 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5422 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5423 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5424 | /* This function is an analyser which forwards response body (including chunk |
| 5425 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 5426 | * zero byte. The only situation where it must not be called is when we're in |
| 5427 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 5428 | * remaining data and to resync after end of body. It expects the msg_state to |
| 5429 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 5430 | * 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] | 5431 | * 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] | 5432 | * bytes of pending data + the headers if not already done (between som and sov). |
| 5433 | * It eventually adjusts som to match sov after the data in between have been sent. |
| 5434 | */ |
| 5435 | int http_response_forward_body(struct session *s, struct buffer *res, int an_bit) |
| 5436 | { |
| 5437 | struct http_txn *txn = &s->txn; |
| 5438 | struct http_msg *msg = &s->txn.rsp; |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5439 | int bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5440 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5441 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 5442 | return 0; |
| 5443 | |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 5444 | 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] | 5445 | ((res->flags & BF_SHUTW) && (res->to_forward || res->send_max)) || |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 5446 | !s->req->analysers) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5447 | /* Output closed while we were sending data. We must abort and |
| 5448 | * wake the other side up. |
| 5449 | */ |
| 5450 | msg->msg_state = HTTP_MSG_ERROR; |
| 5451 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 5452 | return 1; |
| 5453 | } |
| 5454 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5455 | /* in most states, we should abort in case of early close */ |
| 5456 | buffer_auto_close(res); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5457 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5458 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 5459 | /* we have msg->col and msg->sov which both point to the first |
| 5460 | * byte of message body. msg->som still points to the beginning |
| 5461 | * of the message. We must save the body in req->lr because it |
| 5462 | * survives buffer re-alignments. |
| 5463 | */ |
| 5464 | res->lr = res->data + msg->sov; |
| 5465 | if (txn->flags & TX_RES_TE_CHNK) |
| 5466 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 5467 | else { |
| 5468 | msg->msg_state = HTTP_MSG_DATA; |
| 5469 | } |
| 5470 | } |
| 5471 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5472 | while (1) { |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5473 | int bytes; |
| 5474 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5475 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 5476 | /* we may have some data pending */ |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5477 | bytes = msg->sov - msg->som; |
| 5478 | if (msg->chunk_len || bytes) { |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 5479 | msg->som = msg->sov; |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5480 | if (likely(bytes < 0)) /* sov may have wrapped at the end */ |
| 5481 | bytes += res->size; |
| 5482 | msg->chunk_len += (unsigned int)bytes; |
| 5483 | msg->chunk_len -= buffer_forward(res, msg->chunk_len); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 5484 | } |
| 5485 | |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5486 | |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5487 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 5488 | /* must still forward */ |
| 5489 | if (res->to_forward) |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5490 | goto missing_data; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5491 | |
| 5492 | /* nothing left to forward */ |
| 5493 | if (txn->flags & TX_RES_TE_CHNK) |
| 5494 | msg->msg_state = HTTP_MSG_DATA_CRLF; |
| 5495 | else |
| 5496 | msg->msg_state = HTTP_MSG_DONE; |
| 5497 | } |
| 5498 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5499 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5500 | * set ->sov to point to the body and switch to DATA or TRAILERS state. |
| 5501 | */ |
| 5502 | int ret = http_parse_chunk_size(res, msg); |
| 5503 | |
| 5504 | if (!ret) |
| 5505 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5506 | else if (ret < 0) { |
| 5507 | if (msg->err_pos >= 0) |
| 5508 | 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] | 5509 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5510 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5511 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5512 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5513 | else if (msg->msg_state == HTTP_MSG_DATA_CRLF) { |
| 5514 | /* we want the CRLF after the data */ |
| 5515 | int ret; |
| 5516 | |
Willy Tarreau | d3347ee | 2010-01-04 02:02:25 +0100 | [diff] [blame] | 5517 | res->lr = res->w + res->send_max; |
| 5518 | if (res->lr >= res->data + res->size) |
| 5519 | res->lr -= res->size; |
| 5520 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5521 | ret = http_skip_chunk_crlf(res, msg); |
| 5522 | |
| 5523 | if (!ret) |
| 5524 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5525 | else if (ret < 0) { |
| 5526 | if (msg->err_pos >= 0) |
| 5527 | 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] | 5528 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5529 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5530 | /* we're in MSG_CHUNK_SIZE now */ |
| 5531 | } |
| 5532 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
| 5533 | int ret = http_forward_trailers(res, msg); |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5534 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5535 | if (ret == 0) |
| 5536 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5537 | else if (ret < 0) { |
| 5538 | if (msg->err_pos >= 0) |
| 5539 | 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] | 5540 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5541 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5542 | /* we're in HTTP_MSG_DONE now */ |
| 5543 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5544 | else { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5545 | int old_state = msg->msg_state; |
| 5546 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5547 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5548 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5549 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5550 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
| 5551 | buffer_dont_close(res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5552 | if (http_resync_states(s)) { |
| 5553 | http_silent_debug(__LINE__, s); |
| 5554 | /* some state changes occurred, maybe the analyser |
| 5555 | * was disabled too. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5556 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5557 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 5558 | if (res->flags & BF_SHUTW) { |
| 5559 | /* response errors are most likely due to |
| 5560 | * the client aborting the transfer. |
| 5561 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5562 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5563 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5564 | if (msg->err_pos >= 0) |
| 5565 | 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] | 5566 | goto return_bad_res; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5567 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5568 | return 1; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5569 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5570 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5571 | } |
| 5572 | } |
| 5573 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5574 | missing_data: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5575 | /* stop waiting for data if the input is closed before the end */ |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5576 | if (res->flags & BF_SHUTR) { |
| 5577 | if (!(s->flags & SN_ERR_MASK)) |
| 5578 | s->flags |= SN_ERR_SRVCL; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5579 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5580 | if (target_srv(&s->target)) |
| 5581 | target_srv(&s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5582 | goto return_bad_res_stats_ok; |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5583 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5584 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5585 | if (res->flags & BF_SHUTW) |
| 5586 | goto aborted_xfer; |
| 5587 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5588 | /* 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] | 5589 | if (!s->req->analysers) |
| 5590 | goto return_bad_res; |
| 5591 | |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5592 | /* forward any pending data */ |
| 5593 | bytes = msg->sov - msg->som; |
| 5594 | if (msg->chunk_len || bytes) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5595 | msg->som = msg->sov; |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5596 | if (likely(bytes < 0)) /* sov may have wrapped at the end */ |
| 5597 | bytes += res->size; |
| 5598 | msg->chunk_len += (unsigned int)bytes; |
| 5599 | msg->chunk_len -= buffer_forward(res, msg->chunk_len); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5600 | } |
| 5601 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5602 | /* When TE: chunked is used, we need to get there again to parse remaining |
| 5603 | * chunks even if the server has closed, so we don't want to set BF_DONTCLOSE. |
| 5604 | * Similarly, with keep-alive on the client side, we don't want to forward a |
| 5605 | * close. |
| 5606 | */ |
| 5607 | if ((txn->flags & TX_RES_TE_CHNK) || |
| 5608 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5609 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
| 5610 | buffer_dont_close(res); |
| 5611 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5612 | /* 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] | 5613 | * what we did. So we always set the BF_EXPECT_MORE flag so that the |
| 5614 | * system knows it must not set a PUSH on this first part. Interactive |
| 5615 | * modes are already handled by the stream sock layer. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5616 | */ |
Willy Tarreau | 0729303 | 2011-05-30 18:29:28 +0200 | [diff] [blame] | 5617 | res->flags |= BF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5618 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5619 | /* the session handler will take care of timeouts and errors */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5620 | http_silent_debug(__LINE__, s); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5621 | return 0; |
| 5622 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5623 | return_bad_res: /* let's centralize all bad responses */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5624 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5625 | if (target_srv(&s->target)) |
| 5626 | target_srv(&s->target)->counters.failed_resp++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5627 | |
| 5628 | return_bad_res_stats_ok: |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5629 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 5630 | /* don't send any error message as we're in the body */ |
| 5631 | stream_int_retnclose(res->cons, NULL); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5632 | res->analysers = 0; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5633 | 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] | 5634 | if (target_srv(&s->target)) |
| 5635 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5636 | |
| 5637 | if (!(s->flags & SN_ERR_MASK)) |
| 5638 | s->flags |= SN_ERR_PRXCOND; |
| 5639 | if (!(s->flags & SN_FINST_MASK)) |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5640 | s->flags |= SN_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5641 | return 0; |
| 5642 | |
| 5643 | aborted_xfer: |
| 5644 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 5645 | /* don't send any error message as we're in the body */ |
| 5646 | stream_int_retnclose(res->cons, NULL); |
| 5647 | res->analysers = 0; |
| 5648 | s->req->analysers = 0; /* we're in data phase, we want to abort both directions */ |
| 5649 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5650 | s->fe->fe_counters.cli_aborts++; |
| 5651 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5652 | if (target_srv(&s->target)) |
| 5653 | target_srv(&s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5654 | |
| 5655 | if (!(s->flags & SN_ERR_MASK)) |
| 5656 | s->flags |= SN_ERR_CLICL; |
| 5657 | if (!(s->flags & SN_FINST_MASK)) |
| 5658 | s->flags |= SN_FINST_D; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5659 | return 0; |
| 5660 | } |
| 5661 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5662 | /* Iterate the same filter through all request headers. |
| 5663 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5664 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 5665 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5666 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5667 | 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] | 5668 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5669 | char term; |
| 5670 | char *cur_ptr, *cur_end, *cur_next; |
| 5671 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5672 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5673 | struct hdr_idx_elem *cur_hdr; |
| 5674 | int len, delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 5675 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5676 | last_hdr = 0; |
| 5677 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5678 | cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5679 | old_idx = 0; |
| 5680 | |
| 5681 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5682 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5683 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5684 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5685 | (exp->action == ACT_ALLOW || |
| 5686 | exp->action == ACT_DENY || |
| 5687 | exp->action == ACT_TARPIT)) |
| 5688 | return 0; |
| 5689 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5690 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5691 | if (!cur_idx) |
| 5692 | break; |
| 5693 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5694 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5695 | cur_ptr = cur_next; |
| 5696 | cur_end = cur_ptr + cur_hdr->len; |
| 5697 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5698 | |
| 5699 | /* Now we have one header between cur_ptr and cur_end, |
| 5700 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5701 | */ |
| 5702 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5703 | /* The annoying part is that pattern matching needs |
| 5704 | * that we modify the contents to null-terminate all |
| 5705 | * strings before testing them. |
| 5706 | */ |
| 5707 | |
| 5708 | term = *cur_end; |
| 5709 | *cur_end = '\0'; |
| 5710 | |
| 5711 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5712 | switch (exp->action) { |
| 5713 | case ACT_SETBE: |
| 5714 | /* It is not possible to jump a second time. |
| 5715 | * FIXME: should we return an HTTP/500 here so that |
| 5716 | * the admin knows there's a problem ? |
| 5717 | */ |
| 5718 | if (t->be != t->fe) |
| 5719 | break; |
| 5720 | |
| 5721 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 5722 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5723 | last_hdr = 1; |
| 5724 | break; |
| 5725 | |
| 5726 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5727 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5728 | last_hdr = 1; |
| 5729 | break; |
| 5730 | |
| 5731 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5732 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5733 | last_hdr = 1; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5734 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5735 | t->fe->fe_counters.denied_req++; |
| 5736 | if (t->fe != t->be) |
| 5737 | t->be->be_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5738 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 5739 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5740 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5741 | break; |
| 5742 | |
| 5743 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5744 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5745 | last_hdr = 1; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5746 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5747 | t->fe->fe_counters.denied_req++; |
| 5748 | if (t->fe != t->be) |
| 5749 | t->be->be_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5750 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 5751 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5752 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5753 | break; |
| 5754 | |
| 5755 | case ACT_REPLACE: |
| 5756 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 5757 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 5758 | /* FIXME: if the user adds a newline in the replacement, the |
| 5759 | * index will not be recalculated for now, and the new line |
| 5760 | * will not be counted as a new header. |
| 5761 | */ |
| 5762 | |
| 5763 | cur_end += delta; |
| 5764 | cur_next += delta; |
| 5765 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5766 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5767 | break; |
| 5768 | |
| 5769 | case ACT_REMOVE: |
| 5770 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
| 5771 | cur_next += delta; |
| 5772 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5773 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5774 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 5775 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5776 | cur_hdr->len = 0; |
| 5777 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 5778 | cur_idx = old_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5779 | break; |
| 5780 | |
| 5781 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5782 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5783 | if (cur_end) |
| 5784 | *cur_end = term; /* restore the string terminator */ |
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 | /* keep the link from this header to next one in case of later |
| 5787 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5788 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5789 | old_idx = cur_idx; |
| 5790 | } |
| 5791 | return 0; |
| 5792 | } |
| 5793 | |
| 5794 | |
| 5795 | /* Apply the filter to the request line. |
| 5796 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 5797 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5798 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 5799 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5800 | */ |
| 5801 | int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 5802 | { |
| 5803 | char term; |
| 5804 | char *cur_ptr, *cur_end; |
| 5805 | int done; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5806 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5807 | int len, delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5808 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5809 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5810 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5811 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5812 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5813 | (exp->action == ACT_ALLOW || |
| 5814 | exp->action == ACT_DENY || |
| 5815 | exp->action == ACT_TARPIT)) |
| 5816 | return 0; |
| 5817 | else if (exp->action == ACT_REMOVE) |
| 5818 | return 0; |
| 5819 | |
| 5820 | done = 0; |
| 5821 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5822 | cur_ptr = txn->req.sol; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5823 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5824 | |
| 5825 | /* Now we have the request line between cur_ptr and cur_end */ |
| 5826 | |
| 5827 | /* The annoying part is that pattern matching needs |
| 5828 | * that we modify the contents to null-terminate all |
| 5829 | * strings before testing them. |
| 5830 | */ |
| 5831 | |
| 5832 | term = *cur_end; |
| 5833 | *cur_end = '\0'; |
| 5834 | |
| 5835 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5836 | switch (exp->action) { |
| 5837 | case ACT_SETBE: |
| 5838 | /* It is not possible to jump a second time. |
| 5839 | * FIXME: should we return an HTTP/500 here so that |
| 5840 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5841 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5842 | if (t->be != t->fe) |
| 5843 | break; |
| 5844 | |
| 5845 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 5846 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5847 | done = 1; |
| 5848 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5849 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5850 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5851 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5852 | done = 1; |
| 5853 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5854 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5855 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5856 | txn->flags |= TX_CLDENY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5857 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5858 | t->fe->fe_counters.denied_req++; |
| 5859 | if (t->fe != t->be) |
| 5860 | t->be->be_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5861 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 5862 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5863 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5864 | done = 1; |
| 5865 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5866 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5867 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5868 | txn->flags |= TX_CLTARPIT; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5869 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5870 | t->fe->fe_counters.denied_req++; |
| 5871 | if (t->fe != t->be) |
| 5872 | t->be->be_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5873 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 5874 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5875 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5876 | done = 1; |
| 5877 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5878 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5879 | case ACT_REPLACE: |
| 5880 | *cur_end = term; /* restore the string terminator */ |
| 5881 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 5882 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 5883 | /* FIXME: if the user adds a newline in the replacement, the |
| 5884 | * index will not be recalculated for now, and the new line |
| 5885 | * will not be counted as a new header. |
| 5886 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5887 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5888 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5889 | cur_end += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5890 | cur_end = (char *)http_parse_reqline(&txn->req, req->data, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5891 | HTTP_MSG_RQMETH, |
| 5892 | cur_ptr, cur_end + 1, |
| 5893 | NULL, NULL); |
| 5894 | if (unlikely(!cur_end)) |
| 5895 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5896 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5897 | /* we have a full request and we know that we have either a CR |
| 5898 | * or an LF at <ptr>. |
| 5899 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5900 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 5901 | 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] | 5902 | /* there is no point trying this regex on headers */ |
| 5903 | return 1; |
| 5904 | } |
| 5905 | } |
| 5906 | *cur_end = term; /* restore the string terminator */ |
| 5907 | return done; |
| 5908 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 5909 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5910 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5911 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5912 | /* |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5913 | * 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] | 5914 | * 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] | 5915 | * unparsable request. Since it can manage the switch to another backend, it |
| 5916 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5917 | */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5918 | 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] | 5919 | { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5920 | struct http_txn *txn = &s->txn; |
| 5921 | struct hdr_exp *exp; |
| 5922 | |
| 5923 | for (exp = px->req_exp; exp; exp = exp->next) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5924 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5925 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5926 | /* |
| 5927 | * The interleaving of transformations and verdicts |
| 5928 | * makes it difficult to decide to continue or stop |
| 5929 | * the evaluation. |
| 5930 | */ |
| 5931 | |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5932 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) |
| 5933 | break; |
| 5934 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5935 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5936 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5937 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5938 | continue; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5939 | |
| 5940 | /* if this filter had a condition, evaluate it now and skip to |
| 5941 | * next filter if the condition does not match. |
| 5942 | */ |
| 5943 | if (exp->cond) { |
| 5944 | ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_REQ); |
| 5945 | ret = acl_pass(ret); |
| 5946 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 5947 | ret = !ret; |
| 5948 | |
| 5949 | if (!ret) |
| 5950 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5951 | } |
| 5952 | |
| 5953 | /* Apply the filter to the request line. */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5954 | ret = apply_filter_to_req_line(s, req, exp); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5955 | if (unlikely(ret < 0)) |
| 5956 | return -1; |
| 5957 | |
| 5958 | if (likely(ret == 0)) { |
| 5959 | /* The filter did not match the request, it can be |
| 5960 | * iterated through all headers. |
| 5961 | */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5962 | apply_filter_to_req_headers(s, req, exp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5963 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5964 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5965 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5966 | } |
| 5967 | |
| 5968 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5969 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5970 | /* |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5971 | * Try to retrieve the server associated to the appsession. |
| 5972 | * If the server is found, it's assigned to the session. |
| 5973 | */ |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5974 | 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] | 5975 | struct http_txn *txn = &t->txn; |
| 5976 | appsess *asession = NULL; |
| 5977 | char *sessid_temp = NULL; |
| 5978 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5979 | if (len > t->be->appsession_len) { |
| 5980 | len = t->be->appsession_len; |
| 5981 | } |
| 5982 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5983 | if (t->be->options2 & PR_O2_AS_REQL) { |
| 5984 | /* 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] | 5985 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5986 | /* 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] | 5987 | pool_free2(apools.sessid, txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5988 | } |
| 5989 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 5990 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5991 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5992 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5993 | return; |
| 5994 | } |
| 5995 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 5996 | memcpy(txn->sessid, buf, len); |
| 5997 | txn->sessid[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5998 | } |
| 5999 | |
| 6000 | if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) { |
| 6001 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 6002 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 6003 | return; |
| 6004 | } |
| 6005 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6006 | memcpy(sessid_temp, buf, len); |
| 6007 | sessid_temp[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6008 | |
| 6009 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp); |
| 6010 | /* free previously allocated memory */ |
| 6011 | pool_free2(apools.sessid, sessid_temp); |
| 6012 | |
| 6013 | if (asession != NULL) { |
| 6014 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 6015 | if (!(t->be->options2 & PR_O2_AS_REQL)) |
| 6016 | asession->request_count++; |
| 6017 | |
| 6018 | if (asession->serverid != NULL) { |
| 6019 | struct server *srv = t->be->srv; |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 6020 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6021 | while (srv) { |
| 6022 | if (strcmp(srv->id, asession->serverid) == 0) { |
Willy Tarreau | 4de9149 | 2010-01-22 19:10:05 +0100 | [diff] [blame] | 6023 | if ((srv->state & SRV_RUNNING) || |
| 6024 | (t->be->options & PR_O_PERSIST) || |
| 6025 | (t->flags & SN_FORCE_PRST)) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6026 | /* we found the server and it's usable */ |
| 6027 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | 2a6d88d | 2010-01-24 13:10:43 +0100 | [diff] [blame] | 6028 | txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6029 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 6030 | set_target_server(&t->target, srv); |
Willy Tarreau | 664beb8 | 2011-03-10 11:38:29 +0100 | [diff] [blame] | 6031 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6032 | break; |
| 6033 | } else { |
| 6034 | txn->flags &= ~TX_CK_MASK; |
| 6035 | txn->flags |= TX_CK_DOWN; |
| 6036 | } |
| 6037 | } |
| 6038 | srv = srv->next; |
| 6039 | } |
| 6040 | } |
| 6041 | } |
| 6042 | } |
| 6043 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6044 | /* Find the end of a cookie value contained between <s> and <e>. It works the |
| 6045 | * same way as with headers above except that the semi-colon also ends a token. |
| 6046 | * See RFC2965 for more information. Note that it requires a valid header to |
| 6047 | * return a valid result. |
| 6048 | */ |
| 6049 | char *find_cookie_value_end(char *s, const char *e) |
| 6050 | { |
| 6051 | int quoted, qdpair; |
| 6052 | |
| 6053 | quoted = qdpair = 0; |
| 6054 | for (; s < e; s++) { |
| 6055 | if (qdpair) qdpair = 0; |
| 6056 | else if (quoted) { |
| 6057 | if (*s == '\\') qdpair = 1; |
| 6058 | else if (*s == '"') quoted = 0; |
| 6059 | } |
| 6060 | else if (*s == '"') quoted = 1; |
| 6061 | else if (*s == ',' || *s == ';') return s; |
| 6062 | } |
| 6063 | return s; |
| 6064 | } |
| 6065 | |
| 6066 | /* Delete a value in a header between delimiters <from> and <next> in buffer |
| 6067 | * <buf>. The number of characters displaced is returned, and the pointer to |
| 6068 | * the first delimiter is updated if required. The function tries as much as |
| 6069 | * possible to respect the following principles : |
| 6070 | * - replace <from> delimiter by the <next> one unless <from> points to a |
| 6071 | * colon, in which case <next> is simply removed |
| 6072 | * - set exactly one space character after the new first delimiter, unless |
| 6073 | * there are not enough characters in the block being moved to do so. |
| 6074 | * - remove unneeded spaces before the previous delimiter and after the new |
| 6075 | * one. |
| 6076 | * |
| 6077 | * It is the caller's responsibility to ensure that : |
| 6078 | * - <from> points to a valid delimiter or the colon ; |
| 6079 | * - <next> points to a valid delimiter or the final CR/LF ; |
| 6080 | * - there are non-space chars before <from> ; |
| 6081 | * - there is a CR/LF at or after <next>. |
| 6082 | */ |
| 6083 | int del_hdr_value(struct buffer *buf, char **from, char *next) |
| 6084 | { |
| 6085 | char *prev = *from; |
| 6086 | |
| 6087 | if (*prev == ':') { |
| 6088 | /* We're removing the first value, preserve the colon and add a |
| 6089 | * space if possible. |
| 6090 | */ |
| 6091 | if (!http_is_crlf[(unsigned char)*next]) |
| 6092 | next++; |
| 6093 | prev++; |
| 6094 | if (prev < next) |
| 6095 | *prev++ = ' '; |
| 6096 | |
| 6097 | while (http_is_spht[(unsigned char)*next]) |
| 6098 | next++; |
| 6099 | } else { |
| 6100 | /* Remove useless spaces before the old delimiter. */ |
| 6101 | while (http_is_spht[(unsigned char)*(prev-1)]) |
| 6102 | prev--; |
| 6103 | *from = prev; |
| 6104 | |
| 6105 | /* copy the delimiter and if possible a space if we're |
| 6106 | * not at the end of the line. |
| 6107 | */ |
| 6108 | if (!http_is_crlf[(unsigned char)*next]) { |
| 6109 | *prev++ = *next++; |
| 6110 | if (prev + 1 < next) |
| 6111 | *prev++ = ' '; |
| 6112 | while (http_is_spht[(unsigned char)*next]) |
| 6113 | next++; |
| 6114 | } |
| 6115 | } |
| 6116 | return buffer_replace2(buf, prev, next, NULL, 0); |
| 6117 | } |
| 6118 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6119 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 6120 | * 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] | 6121 | * desirable to call it only when needed. This code is quite complex because |
| 6122 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 6123 | * highly recommended not to touch this part without a good reason ! |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6124 | */ |
| 6125 | void manage_client_side_cookies(struct session *t, struct buffer *req) |
| 6126 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6127 | struct http_txn *txn = &t->txn; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6128 | int preserve_hdr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6129 | int cur_idx, old_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6130 | char *hdr_beg, *hdr_end, *hdr_next, *del_from; |
| 6131 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6132 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6133 | /* Iterate through the headers, we start with the start line. */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 6134 | old_idx = 0; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6135 | hdr_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6136 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6137 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6138 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6139 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6140 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6141 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6142 | hdr_beg = hdr_next; |
| 6143 | hdr_end = hdr_beg + cur_hdr->len; |
| 6144 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6145 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6146 | /* We have one full header between hdr_beg and hdr_end, and the |
| 6147 | * next header starts at hdr_next. We're only interested in |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6148 | * "Cookie:" headers. |
| 6149 | */ |
| 6150 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6151 | val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6152 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6153 | old_idx = cur_idx; |
| 6154 | continue; |
| 6155 | } |
| 6156 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6157 | del_from = NULL; /* nothing to be deleted */ |
| 6158 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 6159 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6160 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 6161 | * attributes whose name begin with a '$', and associate them with |
| 6162 | * the right cookie, if we want to delete this cookie. |
| 6163 | * So there are 3 cases for each cookie read : |
| 6164 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 6165 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 6166 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 6167 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 6168 | * "special" cookie. |
| 6169 | * At the end of loop, if a "special" cookie remains, we may have to |
| 6170 | * remove it. If no application cookie persists in the header, we |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6171 | * *MUST* delete it. |
| 6172 | * |
| 6173 | * Note: RFC2965 is unclear about the processing of spaces around |
| 6174 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 6175 | * the RFC explicitly allows spaces before it, and not within the |
| 6176 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 6177 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 6178 | * after the equal sign too, resulting in some (rare) buggy |
| 6179 | * implementations trying to do that. So let's do what servers do. |
| 6180 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 6181 | * allowed quoted strings in values, with any possible character |
| 6182 | * after a backslash, including control chars and delimitors, which |
| 6183 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 6184 | * within values even without quotes. |
| 6185 | * |
| 6186 | * We have to keep multiple pointers in order to support cookie |
| 6187 | * removal at the beginning, middle or end of header without |
| 6188 | * corrupting the header. All of these headers are valid : |
| 6189 | * |
| 6190 | * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n |
| 6191 | * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n |
| 6192 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 6193 | * | | | | | | | | | |
| 6194 | * | | | | | | | | hdr_end <--+ |
| 6195 | * | | | | | | | +--> next |
| 6196 | * | | | | | | +----> val_end |
| 6197 | * | | | | | +-----------> val_beg |
| 6198 | * | | | | +--------------> equal |
| 6199 | * | | | +----------------> att_end |
| 6200 | * | | +---------------------> att_beg |
| 6201 | * | +--------------------------> prev |
| 6202 | * +--------------------------------> hdr_beg |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6203 | */ |
| 6204 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6205 | for (prev = hdr_beg + 6; prev < hdr_end; prev = next) { |
| 6206 | /* Iterate through all cookies on this line */ |
| 6207 | |
| 6208 | /* find att_beg */ |
| 6209 | att_beg = prev + 1; |
| 6210 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 6211 | att_beg++; |
| 6212 | |
| 6213 | /* find att_end : this is the first character after the last non |
| 6214 | * space before the equal. It may be equal to hdr_end. |
| 6215 | */ |
| 6216 | equal = att_end = att_beg; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6217 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6218 | while (equal < hdr_end) { |
| 6219 | if (*equal == '=' || *equal == ',' || *equal == ';') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6220 | break; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6221 | if (http_is_spht[(unsigned char)*equal++]) |
| 6222 | continue; |
| 6223 | att_end = equal; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6224 | } |
| 6225 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6226 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 6227 | * is between <att_beg> and <equal>, both may be identical. |
| 6228 | */ |
| 6229 | |
| 6230 | /* look for end of cookie if there is an equal sign */ |
| 6231 | if (equal < hdr_end && *equal == '=') { |
| 6232 | /* look for the beginning of the value */ |
| 6233 | val_beg = equal + 1; |
| 6234 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 6235 | val_beg++; |
| 6236 | |
| 6237 | /* find the end of the value, respecting quotes */ |
| 6238 | next = find_cookie_value_end(val_beg, hdr_end); |
| 6239 | |
| 6240 | /* make val_end point to the first white space or delimitor after the value */ |
| 6241 | val_end = next; |
| 6242 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 6243 | val_end--; |
| 6244 | } else { |
| 6245 | val_beg = val_end = next = equal; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6246 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6247 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6248 | /* We have nothing to do with attributes beginning with '$'. However, |
| 6249 | * they will automatically be removed if a header before them is removed, |
| 6250 | * since they're supposed to be linked together. |
| 6251 | */ |
| 6252 | if (*att_beg == '$') |
| 6253 | continue; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6254 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6255 | /* Ignore cookies with no equal sign */ |
| 6256 | if (equal == next) { |
| 6257 | /* This is not our cookie, so we must preserve it. But if we already |
| 6258 | * scheduled another cookie for removal, we cannot remove the |
| 6259 | * complete header, but we can remove the previous block itself. |
| 6260 | */ |
| 6261 | preserve_hdr = 1; |
| 6262 | if (del_from != NULL) { |
| 6263 | int delta = del_hdr_value(req, &del_from, prev); |
| 6264 | val_end += delta; |
| 6265 | next += delta; |
| 6266 | hdr_end += delta; |
| 6267 | hdr_next += delta; |
| 6268 | cur_hdr->len += delta; |
| 6269 | http_msg_move_end(&txn->req, delta); |
| 6270 | prev = del_from; |
| 6271 | del_from = NULL; |
| 6272 | } |
| 6273 | continue; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6274 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6275 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6276 | /* if there are spaces around the equal sign, we need to |
| 6277 | * strip them otherwise we'll get trouble for cookie captures, |
| 6278 | * or even for rewrites. Since this happens extremely rarely, |
| 6279 | * it does not hurt performance. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6280 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6281 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 6282 | int stripped_before = 0; |
| 6283 | int stripped_after = 0; |
| 6284 | |
| 6285 | if (att_end != equal) { |
| 6286 | stripped_before = buffer_replace2(req, att_end, equal, NULL, 0); |
| 6287 | equal += stripped_before; |
| 6288 | val_beg += stripped_before; |
| 6289 | } |
| 6290 | |
| 6291 | if (val_beg > equal + 1) { |
| 6292 | stripped_after = buffer_replace2(req, equal + 1, val_beg, NULL, 0); |
| 6293 | val_beg += stripped_after; |
| 6294 | stripped_before += stripped_after; |
| 6295 | } |
| 6296 | |
| 6297 | val_end += stripped_before; |
| 6298 | next += stripped_before; |
| 6299 | hdr_end += stripped_before; |
| 6300 | hdr_next += stripped_before; |
| 6301 | cur_hdr->len += stripped_before; |
| 6302 | http_msg_move_end(&txn->req, stripped_before); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6303 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6304 | /* now everything is as on the diagram above */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6305 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6306 | /* First, let's see if we want to capture this cookie. We check |
| 6307 | * that we don't already have a client side cookie, because we |
| 6308 | * can only capture one. Also as an optimisation, we ignore |
| 6309 | * cookies shorter than the declared name. |
| 6310 | */ |
| 6311 | if (t->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 6312 | (val_end - att_beg >= t->fe->capture_namelen) && |
| 6313 | memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
| 6314 | int log_len = val_end - att_beg; |
| 6315 | |
| 6316 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
| 6317 | Alert("HTTP logging : out of memory.\n"); |
| 6318 | } else { |
| 6319 | if (log_len > t->fe->capture_len) |
| 6320 | log_len = t->fe->capture_len; |
| 6321 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 6322 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6323 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6324 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6325 | |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6326 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 6327 | * following form : |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6328 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6329 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6330 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6331 | * For cookies in prefix mode, the form is : |
| 6332 | * |
| 6333 | * Cookie: NAME=SRV~VALUE |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6334 | */ |
| 6335 | if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 6336 | (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) { |
| 6337 | struct server *srv = t->be->srv; |
| 6338 | char *delim; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6339 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6340 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 6341 | * have the server ID between val_beg and delim, and the original cookie between |
| 6342 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 6343 | * |
| 6344 | * Cookie: NAME=SRV; # in all but prefix modes |
| 6345 | * Cookie: NAME=SRV~OPAQUE ; # in prefix mode |
| 6346 | * | || || | |+-> next |
| 6347 | * | || || | +--> val_end |
| 6348 | * | || || +---------> delim |
| 6349 | * | || |+------------> val_beg |
| 6350 | * | || +-------------> att_end = equal |
| 6351 | * | |+-----------------> att_beg |
| 6352 | * | +------------------> prev |
| 6353 | * +-------------------------> hdr_beg |
| 6354 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6355 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6356 | if (t->be->options & PR_O_COOK_PFX) { |
| 6357 | for (delim = val_beg; delim < val_end; delim++) |
| 6358 | if (*delim == COOKIE_DELIM) |
| 6359 | break; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6360 | } else { |
| 6361 | char *vbar1; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6362 | delim = val_end; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6363 | /* Now check if the cookie contains a date field, which would |
| 6364 | * appear after a vertical bar ('|') just after the server name |
| 6365 | * and before the delimiter. |
| 6366 | */ |
| 6367 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 6368 | if (vbar1) { |
| 6369 | /* OK, so left of the bar is the server's cookie and |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6370 | * right is the last seen date. It is a base64 encoded |
| 6371 | * 30-bit value representing the UNIX date since the |
| 6372 | * epoch in 4-second quantities. |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6373 | */ |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6374 | int val; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6375 | delim = vbar1++; |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6376 | if (val_end - vbar1 >= 5) { |
| 6377 | val = b64tos30(vbar1); |
| 6378 | if (val > 0) |
| 6379 | txn->cookie_last_date = val << 2; |
| 6380 | } |
| 6381 | /* look for a second vertical bar */ |
| 6382 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 6383 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 6384 | val = b64tos30(vbar1 + 1); |
| 6385 | if (val > 0) |
| 6386 | txn->cookie_first_date = val << 2; |
| 6387 | } |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6388 | } |
| 6389 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6390 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6391 | /* if the cookie has an expiration date and the proxy wants to check |
| 6392 | * it, then we do that now. We first check if the cookie is too old, |
| 6393 | * then only if it has expired. We detect strict overflow because the |
| 6394 | * time resolution here is not great (4 seconds). Cookies with dates |
| 6395 | * in the future are ignored if their offset is beyond one day. This |
| 6396 | * allows an admin to fix timezone issues without expiring everyone |
| 6397 | * and at the same time avoids keeping unwanted side effects for too |
| 6398 | * long. |
| 6399 | */ |
| 6400 | if (txn->cookie_first_date && t->be->cookie_maxlife && |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6401 | (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) || |
| 6402 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6403 | txn->flags &= ~TX_CK_MASK; |
| 6404 | txn->flags |= TX_CK_OLD; |
| 6405 | delim = val_beg; // let's pretend we have not found the cookie |
| 6406 | txn->cookie_first_date = 0; |
| 6407 | txn->cookie_last_date = 0; |
| 6408 | } |
| 6409 | else if (txn->cookie_last_date && t->be->cookie_maxidle && |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6410 | (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) || |
| 6411 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6412 | txn->flags &= ~TX_CK_MASK; |
| 6413 | txn->flags |= TX_CK_EXPIRED; |
| 6414 | delim = val_beg; // let's pretend we have not found the cookie |
| 6415 | txn->cookie_first_date = 0; |
| 6416 | txn->cookie_last_date = 0; |
| 6417 | } |
| 6418 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6419 | /* Here, we'll look for the first running server which supports the cookie. |
| 6420 | * This allows to share a same cookie between several servers, for example |
| 6421 | * to dedicate backup servers to specific servers only. |
| 6422 | * However, to prevent clients from sticking to cookie-less backup server |
| 6423 | * when they have incidentely learned an empty cookie, we simply ignore |
| 6424 | * empty cookies and mark them as invalid. |
| 6425 | * The same behaviour is applied when persistence must be ignored. |
| 6426 | */ |
| 6427 | if ((delim == val_beg) || (t->flags & SN_IGNORE_PRST)) |
| 6428 | srv = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6429 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6430 | while (srv) { |
| 6431 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 6432 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
| 6433 | if ((srv->state & SRV_RUNNING) || |
| 6434 | (t->be->options & PR_O_PERSIST) || |
| 6435 | (t->flags & SN_FORCE_PRST)) { |
| 6436 | /* we found the server and we can use it */ |
| 6437 | txn->flags &= ~TX_CK_MASK; |
| 6438 | txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN; |
| 6439 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 6440 | set_target_server(&t->target, srv); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6441 | break; |
| 6442 | } else { |
| 6443 | /* we found a server, but it's down, |
| 6444 | * mark it as such and go on in case |
| 6445 | * another one is available. |
| 6446 | */ |
| 6447 | txn->flags &= ~TX_CK_MASK; |
| 6448 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6449 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6450 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6451 | srv = srv->next; |
| 6452 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6453 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6454 | if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6455 | /* no server matched this cookie */ |
| 6456 | txn->flags &= ~TX_CK_MASK; |
| 6457 | txn->flags |= TX_CK_INVALID; |
| 6458 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6459 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6460 | /* depending on the cookie mode, we may have to either : |
| 6461 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 6462 | * the server never sees it ; |
| 6463 | * - remove the server id from the cookie value, and tag the cookie as an |
| 6464 | * application cookie so that it does not get accidentely removed later, |
| 6465 | * if we're in cookie prefix mode |
| 6466 | */ |
| 6467 | if ((t->be->options & PR_O_COOK_PFX) && (delim != val_end)) { |
| 6468 | int delta; /* negative */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6469 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6470 | delta = buffer_replace2(req, val_beg, delim + 1, NULL, 0); |
| 6471 | val_end += delta; |
| 6472 | next += delta; |
| 6473 | hdr_end += delta; |
| 6474 | hdr_next += delta; |
| 6475 | cur_hdr->len += delta; |
| 6476 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6477 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6478 | del_from = NULL; |
| 6479 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 6480 | } |
| 6481 | else if (del_from == NULL && |
| 6482 | (t->be->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) { |
| 6483 | del_from = prev; |
| 6484 | } |
| 6485 | } else { |
| 6486 | /* This is not our cookie, so we must preserve it. But if we already |
| 6487 | * scheduled another cookie for removal, we cannot remove the |
| 6488 | * complete header, but we can remove the previous block itself. |
| 6489 | */ |
| 6490 | preserve_hdr = 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6491 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6492 | if (del_from != NULL) { |
| 6493 | int delta = del_hdr_value(req, &del_from, prev); |
Willy Tarreau | b810554 | 2010-11-24 18:31:28 +0100 | [diff] [blame] | 6494 | if (att_beg >= del_from) |
| 6495 | att_beg += delta; |
| 6496 | if (att_end >= del_from) |
| 6497 | att_end += delta; |
| 6498 | val_beg += delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6499 | val_end += delta; |
| 6500 | next += delta; |
| 6501 | hdr_end += delta; |
| 6502 | hdr_next += delta; |
| 6503 | cur_hdr->len += delta; |
| 6504 | http_msg_move_end(&txn->req, delta); |
| 6505 | prev = del_from; |
| 6506 | del_from = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6507 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6508 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6509 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6510 | /* Look for the appsession cookie unless persistence must be ignored */ |
| 6511 | if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) { |
| 6512 | int cmp_len, value_len; |
| 6513 | char *value_begin; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 6514 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6515 | if (t->be->options2 & PR_O2_AS_PFX) { |
| 6516 | cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len); |
| 6517 | value_begin = att_beg + t->be->appsession_name_len; |
| 6518 | value_len = val_end - att_beg - t->be->appsession_name_len; |
| 6519 | } else { |
| 6520 | cmp_len = att_end - att_beg; |
| 6521 | value_begin = val_beg; |
| 6522 | value_len = val_end - val_beg; |
| 6523 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6524 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6525 | /* let's see if the cookie is our appcookie */ |
| 6526 | if (cmp_len == t->be->appsession_name_len && |
| 6527 | memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) { |
| 6528 | manage_client_side_appsession(t, value_begin, value_len); |
| 6529 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6530 | } |
| 6531 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6532 | /* continue with next cookie on this header line */ |
| 6533 | att_beg = next; |
| 6534 | } /* for each cookie */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6535 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6536 | /* There are no more cookies on this line. |
| 6537 | * We may still have one (or several) marked for deletion at the |
| 6538 | * end of the line. We must do this now in two ways : |
| 6539 | * - if some cookies must be preserved, we only delete from the |
| 6540 | * mark to the end of line ; |
| 6541 | * - if nothing needs to be preserved, simply delete the whole header |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6542 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6543 | if (del_from) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6544 | int delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6545 | if (preserve_hdr) { |
| 6546 | delta = del_hdr_value(req, &del_from, hdr_end); |
| 6547 | hdr_end = del_from; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6548 | cur_hdr->len += delta; |
| 6549 | } else { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6550 | delta = buffer_replace2(req, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6551 | |
| 6552 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6553 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6554 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6555 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6556 | cur_idx = old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6557 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6558 | hdr_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6559 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6560 | } |
| 6561 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6562 | /* check next header */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6563 | old_idx = cur_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6564 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6565 | } |
| 6566 | |
| 6567 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6568 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 6569 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 6570 | */ |
| 6571 | int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 6572 | { |
| 6573 | char term; |
| 6574 | char *cur_ptr, *cur_end, *cur_next; |
| 6575 | int cur_idx, old_idx, last_hdr; |
| 6576 | struct http_txn *txn = &t->txn; |
| 6577 | struct hdr_idx_elem *cur_hdr; |
| 6578 | int len, delta; |
| 6579 | |
| 6580 | last_hdr = 0; |
| 6581 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6582 | cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6583 | old_idx = 0; |
| 6584 | |
| 6585 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6586 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6587 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6588 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6589 | (exp->action == ACT_ALLOW || |
| 6590 | exp->action == ACT_DENY)) |
| 6591 | return 0; |
| 6592 | |
| 6593 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 6594 | if (!cur_idx) |
| 6595 | break; |
| 6596 | |
| 6597 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 6598 | cur_ptr = cur_next; |
| 6599 | cur_end = cur_ptr + cur_hdr->len; |
| 6600 | cur_next = cur_end + cur_hdr->cr + 1; |
| 6601 | |
| 6602 | /* Now we have one header between cur_ptr and cur_end, |
| 6603 | * and the next header starts at cur_next. |
| 6604 | */ |
| 6605 | |
| 6606 | /* The annoying part is that pattern matching needs |
| 6607 | * that we modify the contents to null-terminate all |
| 6608 | * strings before testing them. |
| 6609 | */ |
| 6610 | |
| 6611 | term = *cur_end; |
| 6612 | *cur_end = '\0'; |
| 6613 | |
| 6614 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 6615 | switch (exp->action) { |
| 6616 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6617 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6618 | last_hdr = 1; |
| 6619 | break; |
| 6620 | |
| 6621 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6622 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6623 | last_hdr = 1; |
| 6624 | break; |
| 6625 | |
| 6626 | case ACT_REPLACE: |
| 6627 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 6628 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 6629 | /* FIXME: if the user adds a newline in the replacement, the |
| 6630 | * index will not be recalculated for now, and the new line |
| 6631 | * will not be counted as a new header. |
| 6632 | */ |
| 6633 | |
| 6634 | cur_end += delta; |
| 6635 | cur_next += delta; |
| 6636 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6637 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6638 | break; |
| 6639 | |
| 6640 | case ACT_REMOVE: |
| 6641 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 6642 | cur_next += delta; |
| 6643 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6644 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6645 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6646 | txn->hdr_idx.used--; |
| 6647 | cur_hdr->len = 0; |
| 6648 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6649 | cur_idx = old_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6650 | break; |
| 6651 | |
| 6652 | } |
| 6653 | } |
| 6654 | if (cur_end) |
| 6655 | *cur_end = term; /* restore the string terminator */ |
| 6656 | |
| 6657 | /* keep the link from this header to next one in case of later |
| 6658 | * removal of next header. |
| 6659 | */ |
| 6660 | old_idx = cur_idx; |
| 6661 | } |
| 6662 | return 0; |
| 6663 | } |
| 6664 | |
| 6665 | |
| 6666 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 6667 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 6668 | * or -1 if a replacement resulted in an invalid status line. |
| 6669 | */ |
| 6670 | int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 6671 | { |
| 6672 | char term; |
| 6673 | char *cur_ptr, *cur_end; |
| 6674 | int done; |
| 6675 | struct http_txn *txn = &t->txn; |
| 6676 | int len, delta; |
| 6677 | |
| 6678 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6679 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6680 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6681 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6682 | (exp->action == ACT_ALLOW || |
| 6683 | exp->action == ACT_DENY)) |
| 6684 | return 0; |
| 6685 | else if (exp->action == ACT_REMOVE) |
| 6686 | return 0; |
| 6687 | |
| 6688 | done = 0; |
| 6689 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6690 | cur_ptr = txn->rsp.sol; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 6691 | cur_end = cur_ptr + txn->rsp.sl.st.l; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6692 | |
| 6693 | /* Now we have the status line between cur_ptr and cur_end */ |
| 6694 | |
| 6695 | /* The annoying part is that pattern matching needs |
| 6696 | * that we modify the contents to null-terminate all |
| 6697 | * strings before testing them. |
| 6698 | */ |
| 6699 | |
| 6700 | term = *cur_end; |
| 6701 | *cur_end = '\0'; |
| 6702 | |
| 6703 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 6704 | switch (exp->action) { |
| 6705 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6706 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6707 | done = 1; |
| 6708 | break; |
| 6709 | |
| 6710 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6711 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6712 | done = 1; |
| 6713 | break; |
| 6714 | |
| 6715 | case ACT_REPLACE: |
| 6716 | *cur_end = term; /* restore the string terminator */ |
| 6717 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 6718 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 6719 | /* FIXME: if the user adds a newline in the replacement, the |
| 6720 | * index will not be recalculated for now, and the new line |
| 6721 | * will not be counted as a new header. |
| 6722 | */ |
| 6723 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6724 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6725 | cur_end += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6726 | cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 6727 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6728 | cur_ptr, cur_end + 1, |
| 6729 | NULL, NULL); |
| 6730 | if (unlikely(!cur_end)) |
| 6731 | return -1; |
| 6732 | |
| 6733 | /* we have a full respnse and we know that we have either a CR |
| 6734 | * or an LF at <ptr>. |
| 6735 | */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6736 | 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] | 6737 | 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] | 6738 | /* there is no point trying this regex on headers */ |
| 6739 | return 1; |
| 6740 | } |
| 6741 | } |
| 6742 | *cur_end = term; /* restore the string terminator */ |
| 6743 | return done; |
| 6744 | } |
| 6745 | |
| 6746 | |
| 6747 | |
| 6748 | /* |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6749 | * 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] | 6750 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 6751 | * unparsable response. |
| 6752 | */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6753 | 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] | 6754 | { |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6755 | struct http_txn *txn = &s->txn; |
| 6756 | struct hdr_exp *exp; |
| 6757 | |
| 6758 | for (exp = px->rsp_exp; exp; exp = exp->next) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6759 | int ret; |
| 6760 | |
| 6761 | /* |
| 6762 | * The interleaving of transformations and verdicts |
| 6763 | * makes it difficult to decide to continue or stop |
| 6764 | * the evaluation. |
| 6765 | */ |
| 6766 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6767 | if (txn->flags & TX_SVDENY) |
| 6768 | break; |
| 6769 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6770 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6771 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 6772 | exp->action == ACT_PASS)) { |
| 6773 | exp = exp->next; |
| 6774 | continue; |
| 6775 | } |
| 6776 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6777 | /* if this filter had a condition, evaluate it now and skip to |
| 6778 | * next filter if the condition does not match. |
| 6779 | */ |
| 6780 | if (exp->cond) { |
| 6781 | ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_RTR); |
| 6782 | ret = acl_pass(ret); |
| 6783 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 6784 | ret = !ret; |
| 6785 | if (!ret) |
| 6786 | continue; |
| 6787 | } |
| 6788 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6789 | /* Apply the filter to the status line. */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6790 | ret = apply_filter_to_sts_line(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6791 | if (unlikely(ret < 0)) |
| 6792 | return -1; |
| 6793 | |
| 6794 | if (likely(ret == 0)) { |
| 6795 | /* The filter did not match the response, it can be |
| 6796 | * iterated through all headers. |
| 6797 | */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6798 | apply_filter_to_resp_headers(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6799 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6800 | } |
| 6801 | return 0; |
| 6802 | } |
| 6803 | |
| 6804 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6805 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 6806 | * 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] | 6807 | * desirable to call it only when needed. This function is also used when we |
| 6808 | * 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] | 6809 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6810 | void manage_server_side_cookies(struct session *t, struct buffer *res) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6811 | { |
| 6812 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6813 | struct server *srv; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6814 | int is_cookie2; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6815 | int cur_idx, old_idx, delta; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6816 | char *hdr_beg, *hdr_end, *hdr_next; |
| 6817 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6818 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6819 | /* Iterate through the headers. |
| 6820 | * we start with the start line. |
| 6821 | */ |
| 6822 | old_idx = 0; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6823 | hdr_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6824 | |
| 6825 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 6826 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6827 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6828 | |
| 6829 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6830 | hdr_beg = hdr_next; |
| 6831 | hdr_end = hdr_beg + cur_hdr->len; |
| 6832 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6833 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6834 | /* We have one full header between hdr_beg and hdr_end, and the |
| 6835 | * next header starts at hdr_next. We're only interested in |
| 6836 | * "Set-Cookie" and "Set-Cookie2" headers. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6837 | */ |
| 6838 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6839 | is_cookie2 = 0; |
| 6840 | prev = hdr_beg + 10; |
| 6841 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6842 | if (!val) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6843 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11); |
| 6844 | if (!val) { |
| 6845 | old_idx = cur_idx; |
| 6846 | continue; |
| 6847 | } |
| 6848 | is_cookie2 = 1; |
| 6849 | prev = hdr_beg + 11; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6850 | } |
| 6851 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6852 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 6853 | * <prev> points to the colon. |
| 6854 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 6855 | txn->flags |= TX_SCK_PRESENT; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6856 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6857 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 6858 | * check-cache is enabled) and we are not interested in checking |
| 6859 | * them. Warning, the cookie capture is declared in the frontend. |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 6860 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6861 | if (t->be->cookie_name == NULL && |
| 6862 | t->be->appsession_name == NULL && |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 6863 | t->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6864 | return; |
| 6865 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6866 | /* OK so now we know we have to process this response cookie. |
| 6867 | * The format of the Set-Cookie header is slightly different |
| 6868 | * from the format of the Cookie header in that it does not |
| 6869 | * support the comma as a cookie delimiter (thus the header |
| 6870 | * cannot be folded) because the Expires attribute described in |
| 6871 | * the original Netscape's spec may contain an unquoted date |
| 6872 | * with a comma inside. We have to live with this because |
| 6873 | * many browsers don't support Max-Age and some browsers don't |
| 6874 | * support quoted strings. However the Set-Cookie2 header is |
| 6875 | * clean. |
| 6876 | * |
| 6877 | * We have to keep multiple pointers in order to support cookie |
| 6878 | * removal at the beginning, middle or end of header without |
| 6879 | * corrupting the header (in case of set-cookie2). A special |
| 6880 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 6881 | * fields after the first semi-colon. The <next> pointer points |
| 6882 | * either to the end of line (set-cookie) or next unquoted comma |
| 6883 | * (set-cookie2). All of these headers are valid : |
| 6884 | * |
| 6885 | * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n |
| 6886 | * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 6887 | * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 6888 | * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n |
| 6889 | * | | | | | | | | | | |
| 6890 | * | | | | | | | | +-> next hdr_end <--+ |
| 6891 | * | | | | | | | +------------> scav |
| 6892 | * | | | | | | +--------------> val_end |
| 6893 | * | | | | | +--------------------> val_beg |
| 6894 | * | | | | +----------------------> equal |
| 6895 | * | | | +------------------------> att_end |
| 6896 | * | | +----------------------------> att_beg |
| 6897 | * | +------------------------------> prev |
| 6898 | * +-----------------------------------------> hdr_beg |
| 6899 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6900 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6901 | for (; prev < hdr_end; prev = next) { |
| 6902 | /* Iterate through all cookies on this line */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6903 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6904 | /* find att_beg */ |
| 6905 | att_beg = prev + 1; |
| 6906 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 6907 | att_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6908 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6909 | /* find att_end : this is the first character after the last non |
| 6910 | * space before the equal. It may be equal to hdr_end. |
| 6911 | */ |
| 6912 | equal = att_end = att_beg; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6913 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6914 | while (equal < hdr_end) { |
| 6915 | if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ',')) |
| 6916 | break; |
| 6917 | if (http_is_spht[(unsigned char)*equal++]) |
| 6918 | continue; |
| 6919 | att_end = equal; |
| 6920 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6921 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6922 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 6923 | * is between <att_beg> and <equal>, both may be identical. |
| 6924 | */ |
| 6925 | |
| 6926 | /* look for end of cookie if there is an equal sign */ |
| 6927 | if (equal < hdr_end && *equal == '=') { |
| 6928 | /* look for the beginning of the value */ |
| 6929 | val_beg = equal + 1; |
| 6930 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 6931 | val_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6932 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6933 | /* find the end of the value, respecting quotes */ |
| 6934 | next = find_cookie_value_end(val_beg, hdr_end); |
| 6935 | |
| 6936 | /* make val_end point to the first white space or delimitor after the value */ |
| 6937 | val_end = next; |
| 6938 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 6939 | val_end--; |
| 6940 | } else { |
| 6941 | /* <equal> points to next comma, semi-colon or EOL */ |
| 6942 | val_beg = val_end = next = equal; |
| 6943 | } |
| 6944 | |
| 6945 | if (next < hdr_end) { |
| 6946 | /* Set-Cookie2 supports multiple cookies, and <next> points to |
| 6947 | * a colon or semi-colon before the end. So skip all attr-value |
| 6948 | * pairs and look for the next comma. For Set-Cookie, since |
| 6949 | * commas are permitted in values, skip to the end. |
| 6950 | */ |
| 6951 | if (is_cookie2) |
| 6952 | next = find_hdr_value_end(next, hdr_end); |
| 6953 | else |
| 6954 | next = hdr_end; |
| 6955 | } |
| 6956 | |
| 6957 | /* Now everything is as on the diagram above */ |
| 6958 | |
| 6959 | /* Ignore cookies with no equal sign */ |
| 6960 | if (equal == val_end) |
| 6961 | continue; |
| 6962 | |
| 6963 | /* If there are spaces around the equal sign, we need to |
| 6964 | * strip them otherwise we'll get trouble for cookie captures, |
| 6965 | * or even for rewrites. Since this happens extremely rarely, |
| 6966 | * it does not hurt performance. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6967 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6968 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 6969 | int stripped_before = 0; |
| 6970 | int stripped_after = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6971 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6972 | if (att_end != equal) { |
| 6973 | stripped_before = buffer_replace2(res, att_end, equal, NULL, 0); |
| 6974 | equal += stripped_before; |
| 6975 | val_beg += stripped_before; |
| 6976 | } |
| 6977 | |
| 6978 | if (val_beg > equal + 1) { |
| 6979 | stripped_after = buffer_replace2(res, equal + 1, val_beg, NULL, 0); |
| 6980 | val_beg += stripped_after; |
| 6981 | stripped_before += stripped_after; |
| 6982 | } |
| 6983 | |
| 6984 | val_end += stripped_before; |
| 6985 | next += stripped_before; |
| 6986 | hdr_end += stripped_before; |
| 6987 | hdr_next += stripped_before; |
| 6988 | cur_hdr->len += stripped_before; |
Willy Tarreau | 1fc1f45 | 2011-04-07 22:35:37 +0200 | [diff] [blame] | 6989 | http_msg_move_end(&txn->rsp, stripped_before); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6990 | } |
| 6991 | |
| 6992 | /* First, let's see if we want to capture this cookie. We check |
| 6993 | * that we don't already have a server side cookie, because we |
| 6994 | * can only capture one. Also as an optimisation, we ignore |
| 6995 | * cookies shorter than the declared name. |
| 6996 | */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 6997 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 6998 | txn->srv_cookie == NULL && |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6999 | (val_end - att_beg >= t->fe->capture_namelen) && |
| 7000 | memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
| 7001 | int log_len = val_end - att_beg; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 7002 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7003 | Alert("HTTP logging : out of memory.\n"); |
| 7004 | } |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 7005 | else { |
| 7006 | if (log_len > t->fe->capture_len) |
| 7007 | log_len = t->fe->capture_len; |
| 7008 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 7009 | txn->srv_cookie[log_len] = 0; |
| 7010 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7011 | } |
| 7012 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7013 | srv = target_srv(&t->target); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7014 | /* now check if we need to process it for persistence */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7015 | if (!(t->flags & SN_IGNORE_PRST) && |
| 7016 | (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 7017 | (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) { |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7018 | /* assume passive cookie by default */ |
| 7019 | txn->flags &= ~TX_SCK_MASK; |
| 7020 | txn->flags |= TX_SCK_FOUND; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7021 | |
| 7022 | /* If the cookie is in insert mode on a known server, we'll delete |
| 7023 | * this occurrence because we'll insert another one later. |
| 7024 | * 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] | 7025 | * a direct access. |
| 7026 | */ |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 7027 | if (t->be->options2 & PR_O2_COOK_PSV) { |
| 7028 | /* The "preserve" flag was set, we don't want to touch the |
| 7029 | * server's cookie. |
| 7030 | */ |
| 7031 | } |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7032 | else if ((srv && (t->be->options & PR_O_COOK_INS)) || |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 7033 | ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7034 | /* this cookie must be deleted */ |
| 7035 | if (*prev == ':' && next == hdr_end) { |
| 7036 | /* whole header */ |
| 7037 | delta = buffer_replace2(res, hdr_beg, hdr_next, NULL, 0); |
| 7038 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7039 | txn->hdr_idx.used--; |
| 7040 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7041 | cur_idx = old_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7042 | hdr_next += delta; |
| 7043 | http_msg_move_end(&txn->rsp, delta); |
| 7044 | /* note: while both invalid now, <next> and <hdr_end> |
| 7045 | * are still equal, so the for() will stop as expected. |
| 7046 | */ |
| 7047 | } else { |
| 7048 | /* just remove the value */ |
| 7049 | int delta = del_hdr_value(res, &prev, next); |
| 7050 | next = prev; |
| 7051 | hdr_end += delta; |
| 7052 | hdr_next += delta; |
| 7053 | cur_hdr->len += delta; |
| 7054 | http_msg_move_end(&txn->rsp, delta); |
| 7055 | } |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7056 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7057 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7058 | /* and go on with next cookie */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7059 | } |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7060 | else if (srv && srv->cookie && (t->be->options & PR_O_COOK_RW)) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7061 | /* replace bytes val_beg->val_end with the cookie name associated |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7062 | * with this server since we know it. |
| 7063 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7064 | delta = buffer_replace2(res, val_beg, val_end, srv->cookie, srv->cklen); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7065 | next += delta; |
| 7066 | hdr_end += delta; |
| 7067 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7068 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7069 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7070 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7071 | txn->flags &= ~TX_SCK_MASK; |
| 7072 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7073 | } |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7074 | else if (srv && srv && (t->be->options & PR_O_COOK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7075 | /* insert the cookie name associated with this server |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7076 | * before existing cookie, and insert a delimiter between them.. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7077 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7078 | 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] | 7079 | next += delta; |
| 7080 | hdr_end += delta; |
| 7081 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7082 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7083 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7084 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7085 | val_beg[srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7086 | txn->flags &= ~TX_SCK_MASK; |
| 7087 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7088 | } |
| 7089 | } |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 7090 | /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */ |
| 7091 | else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7092 | int cmp_len, value_len; |
| 7093 | char *value_begin; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7094 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7095 | if (t->be->options2 & PR_O2_AS_PFX) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7096 | cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len); |
| 7097 | value_begin = att_beg + t->be->appsession_name_len; |
| 7098 | 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] | 7099 | } else { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7100 | cmp_len = att_end - att_beg; |
| 7101 | value_begin = val_beg; |
| 7102 | value_len = MIN(t->be->appsession_len, val_end - val_beg); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7103 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7104 | |
Cyril Bonté | 17530c3 | 2010-04-06 21:11:10 +0200 | [diff] [blame] | 7105 | if ((cmp_len == t->be->appsession_name_len) && |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7106 | (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) { |
| 7107 | /* free a possibly previously allocated memory */ |
| 7108 | pool_free2(apools.sessid, txn->sessid); |
| 7109 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7110 | /* Store the sessid in the session for future use */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7111 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7112 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 7113 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 7114 | return; |
| 7115 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7116 | memcpy(txn->sessid, value_begin, value_len); |
| 7117 | txn->sessid[value_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7118 | } |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7119 | } |
| 7120 | /* that's done for this cookie, check the next one on the same |
| 7121 | * line when next != hdr_end (only if is_cookie2). |
| 7122 | */ |
| 7123 | } |
| 7124 | /* check next header */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7125 | old_idx = cur_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7126 | } |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7127 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7128 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7129 | appsess *asession = NULL; |
| 7130 | /* only do insert, if lookup fails */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7131 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7132 | if (asession == NULL) { |
Willy Tarreau | 1fac753 | 2010-01-09 19:23:06 +0100 | [diff] [blame] | 7133 | size_t server_id_len; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7134 | if ((asession = pool_alloc2(pool2_appsess)) == NULL) { |
| 7135 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
| 7136 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n"); |
| 7137 | return; |
| 7138 | } |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 7139 | asession->serverid = NULL; /* to avoid a double free in case of allocation error */ |
| 7140 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7141 | if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) { |
| 7142 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 7143 | 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] | 7144 | t->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7145 | return; |
| 7146 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7147 | memcpy(asession->sessid, txn->sessid, t->be->appsession_len); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7148 | asession->sessid[t->be->appsession_len] = 0; |
| 7149 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7150 | server_id_len = strlen(target_srv(&t->target)->id) + 1; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7151 | if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) { |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 7152 | Alert("Not enough Memory process_srv():asession->serverid:malloc().\n"); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7153 | 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] | 7154 | t->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7155 | return; |
| 7156 | } |
| 7157 | asession->serverid[0] = '\0'; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7158 | memcpy(asession->serverid, target_srv(&t->target)->id, server_id_len); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7159 | |
| 7160 | asession->request_count = 0; |
| 7161 | appsession_hash_insert(&(t->be->htbl_proxy), asession); |
| 7162 | } |
| 7163 | |
| 7164 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 7165 | asession->request_count++; |
| 7166 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7167 | } |
| 7168 | |
| 7169 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7170 | /* |
| 7171 | * Check if response is cacheable or not. Updates t->flags. |
| 7172 | */ |
| 7173 | void check_response_for_cacheability(struct session *t, struct buffer *rtr) |
| 7174 | { |
| 7175 | struct http_txn *txn = &t->txn; |
| 7176 | char *p1, *p2; |
| 7177 | |
| 7178 | char *cur_ptr, *cur_end, *cur_next; |
| 7179 | int cur_idx; |
| 7180 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 7181 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7182 | return; |
| 7183 | |
| 7184 | /* Iterate through the headers. |
| 7185 | * we start with the start line. |
| 7186 | */ |
| 7187 | cur_idx = 0; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7188 | cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7189 | |
| 7190 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 7191 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7192 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7193 | |
| 7194 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 7195 | cur_ptr = cur_next; |
| 7196 | cur_end = cur_ptr + cur_hdr->len; |
| 7197 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7198 | |
| 7199 | /* We have one full header between cur_ptr and cur_end, and the |
| 7200 | * next header starts at cur_next. We're only interested in |
| 7201 | * "Cookie:" headers. |
| 7202 | */ |
| 7203 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7204 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 7205 | if (val) { |
| 7206 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 7207 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 7208 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 7209 | return; |
| 7210 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7211 | } |
| 7212 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7213 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 7214 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7215 | continue; |
| 7216 | |
| 7217 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 7218 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7219 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7220 | |
| 7221 | if (p1 >= cur_end) /* no more info */ |
| 7222 | continue; |
| 7223 | |
| 7224 | /* p1 is at the beginning of the value */ |
| 7225 | p2 = p1; |
| 7226 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 7227 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7228 | p2++; |
| 7229 | |
| 7230 | /* we have a complete value between p1 and p2 */ |
| 7231 | if (p2 < cur_end && *p2 == '=') { |
| 7232 | /* we have something of the form no-cache="set-cookie" */ |
| 7233 | if ((cur_end - p1 >= 21) && |
| 7234 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 7235 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7236 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7237 | continue; |
| 7238 | } |
| 7239 | |
| 7240 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 7241 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
| 7242 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 7243 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 7244 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7245 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7246 | return; |
| 7247 | } |
| 7248 | |
| 7249 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7250 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7251 | continue; |
| 7252 | } |
| 7253 | } |
| 7254 | } |
| 7255 | |
| 7256 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7257 | /* |
| 7258 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 7259 | * If the server is found, it's assigned to the session. |
| 7260 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7261 | 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] | 7262 | { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7263 | char *end_params, *first_param, *cur_param, *next_param; |
| 7264 | char separator; |
| 7265 | int value_len; |
| 7266 | |
| 7267 | int mode = t->be->options2 & PR_O2_AS_M_ANY; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7268 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 7269 | if (t->be->appsession_name == NULL || |
Cyril Bonté | 17530c3 | 2010-04-06 21:11:10 +0200 | [diff] [blame] | 7270 | (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] | 7271 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7272 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7273 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7274 | first_param = NULL; |
| 7275 | switch (mode) { |
| 7276 | case PR_O2_AS_M_PP: |
| 7277 | first_param = memchr(begin, ';', len); |
| 7278 | break; |
| 7279 | case PR_O2_AS_M_QS: |
| 7280 | first_param = memchr(begin, '?', len); |
| 7281 | break; |
| 7282 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7283 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7284 | if (first_param == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7285 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7286 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7287 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7288 | switch (mode) { |
| 7289 | case PR_O2_AS_M_PP: |
| 7290 | if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) { |
| 7291 | end_params = (char *) begin + len; |
| 7292 | } |
| 7293 | separator = ';'; |
| 7294 | break; |
| 7295 | case PR_O2_AS_M_QS: |
| 7296 | end_params = (char *) begin + len; |
| 7297 | separator = '&'; |
| 7298 | break; |
| 7299 | default: |
| 7300 | /* unknown mode, shouldn't happen */ |
| 7301 | return; |
| 7302 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7303 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7304 | cur_param = next_param = end_params; |
| 7305 | while (cur_param > first_param) { |
| 7306 | cur_param--; |
| 7307 | if ((cur_param[0] == separator) || (cur_param == first_param)) { |
| 7308 | /* let's see if this is the appsession parameter */ |
| 7309 | if ((cur_param + t->be->appsession_name_len + 1 < next_param) && |
| 7310 | ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') && |
| 7311 | (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) { |
| 7312 | /* Cool... it's the right one */ |
| 7313 | cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2); |
| 7314 | value_len = MIN(t->be->appsession_len, next_param - cur_param); |
| 7315 | if (value_len > 0) { |
| 7316 | manage_client_side_appsession(t, cur_param, value_len); |
| 7317 | } |
| 7318 | break; |
| 7319 | } |
| 7320 | next_param = cur_param; |
| 7321 | } |
| 7322 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7323 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 7324 | Alert("get_srv_from_appsession\n"); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 7325 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7326 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7327 | } |
| 7328 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7329 | /* |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7330 | * 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] | 7331 | * for the current backend. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7332 | * |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7333 | * 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] | 7334 | * uri_auth field is valid. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7335 | * |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7336 | * Returns 1 if stats should be provided, otherwise 0. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7337 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7338 | 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] | 7339 | { |
| 7340 | struct uri_auth *uri_auth = backend->uri_auth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7341 | char *h; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7342 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7343 | if (!uri_auth) |
| 7344 | return 0; |
| 7345 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7346 | 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] | 7347 | return 0; |
| 7348 | |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7349 | memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats)); |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 7350 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7351 | /* check URI size */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7352 | if (uri_auth->uri_len > txn->req.sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7353 | return 0; |
| 7354 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7355 | h = txn->req.sol + txn->req.sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7356 | |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 7357 | /* the URI is in h */ |
| 7358 | if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7359 | return 0; |
| 7360 | |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7361 | h += uri_auth->uri_len; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7362 | 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] | 7363 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7364 | si->applet.ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7365 | break; |
| 7366 | } |
| 7367 | h++; |
| 7368 | } |
| 7369 | |
| 7370 | if (uri_auth->refresh) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7371 | h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len; |
| 7372 | 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] | 7373 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7374 | si->applet.ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7375 | break; |
| 7376 | } |
| 7377 | h++; |
| 7378 | } |
| 7379 | } |
| 7380 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7381 | h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len; |
| 7382 | 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] | 7383 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7384 | si->applet.ctx.stats.flags |= STAT_FMT_CSV; |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 7385 | break; |
| 7386 | } |
| 7387 | h++; |
| 7388 | } |
| 7389 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7390 | h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len; |
| 7391 | while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 8) { |
| 7392 | if (memcmp(h, ";st=", 4) == 0) { |
| 7393 | h += 4; |
| 7394 | |
| 7395 | if (memcmp(h, STAT_STATUS_DONE, 4) == 0) |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7396 | si->applet.ctx.stats.st_code = STAT_STATUS_DONE; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7397 | else if (memcmp(h, STAT_STATUS_NONE, 4) == 0) |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7398 | si->applet.ctx.stats.st_code = STAT_STATUS_NONE; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7399 | else if (memcmp(h, STAT_STATUS_EXCD, 4) == 0) |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7400 | si->applet.ctx.stats.st_code = STAT_STATUS_EXCD; |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 7401 | else if (memcmp(h, STAT_STATUS_DENY, 4) == 0) |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7402 | si->applet.ctx.stats.st_code = STAT_STATUS_DENY; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7403 | else |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7404 | si->applet.ctx.stats.st_code = STAT_STATUS_UNKN; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7405 | break; |
| 7406 | } |
| 7407 | h++; |
| 7408 | } |
| 7409 | |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7410 | si->applet.ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO; |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 7411 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7412 | return 1; |
| 7413 | } |
| 7414 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7415 | /* |
| 7416 | * 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] | 7417 | * WARNING: it's unlikely that we've reached HTTP_MSG_BODY here so we must not |
| 7418 | * assume that msg->sol = buf->data + msg->som. |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7419 | */ |
| 7420 | void http_capture_bad_message(struct error_snapshot *es, struct session *s, |
| 7421 | struct buffer *buf, struct http_msg *msg, |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 7422 | int state, struct proxy *other_end) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7423 | { |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7424 | if (buf->r <= (buf->data + msg->som)) { /* message wraps */ |
| 7425 | int len1 = buf->size - msg->som; |
| 7426 | es->len = buf->r - (buf->data + msg->som) + buf->size; |
| 7427 | memcpy(es->buf, buf->data + msg->som, MIN(len1, sizeof(es->buf))); |
| 7428 | if (es->len > len1 && len1 < sizeof(es->buf)) |
| 7429 | memcpy(es->buf, buf->data, MIN(es->len, sizeof(es->buf)) - len1); |
| 7430 | } |
| 7431 | else { |
| 7432 | es->len = buf->r - (buf->data + msg->som); |
| 7433 | memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf))); |
| 7434 | } |
| 7435 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7436 | if (msg->err_pos >= 0) |
Willy Tarreau | 2df8d71 | 2009-05-01 11:33:17 +0200 | [diff] [blame] | 7437 | es->pos = msg->err_pos - msg->som; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7438 | else if (buf->lr >= (buf->data + msg->som)) |
Willy Tarreau | 2df8d71 | 2009-05-01 11:33:17 +0200 | [diff] [blame] | 7439 | es->pos = buf->lr - (buf->data + msg->som); |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7440 | else |
| 7441 | es->pos = buf->lr - (buf->data + msg->som) + buf->size; |
| 7442 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7443 | es->when = date; // user-visible date |
| 7444 | es->sid = s->uniq_id; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7445 | es->srv = target_srv(&s->target); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7446 | es->oe = other_end; |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 7447 | es->src = s->req->prod->addr.c.from; |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 7448 | es->state = state; |
| 7449 | es->flags = buf->flags; |
Willy Tarreau | 10479e4 | 2010-12-12 14:00:34 +0100 | [diff] [blame] | 7450 | es->ev_id = error_snapshot_id++; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7451 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7452 | |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7453 | /* return the IP address pointed to by occurrence <occ> of header <hname> in |
| 7454 | * HTTP message <msg> indexed in <idx>. If <occ> is strictly positive, the |
| 7455 | * occurrence number corresponding to this value is returned. If <occ> is |
| 7456 | * strictly negative, the occurrence number before the end corresponding to |
| 7457 | * this value is returned. If <occ> is null, any value is returned, so it is |
| 7458 | * not recommended to use it that way. Negative occurrences are limited to |
| 7459 | * a small value because it is required to keep them in memory while scanning. |
| 7460 | * IP address 0.0.0.0 is returned if no match is found. |
| 7461 | */ |
| 7462 | unsigned int get_ip_from_hdr2(struct http_msg *msg, const char *hname, int hlen, struct hdr_idx *idx, int occ) |
| 7463 | { |
| 7464 | struct hdr_ctx ctx; |
| 7465 | unsigned int hdr_hist[MAX_HDR_HISTORY]; |
| 7466 | unsigned int hist_ptr; |
| 7467 | int found = 0; |
| 7468 | |
| 7469 | ctx.idx = 0; |
| 7470 | if (occ >= 0) { |
| 7471 | while (http_find_header2(hname, hlen, msg->sol, idx, &ctx)) { |
| 7472 | occ--; |
| 7473 | if (occ <= 0) { |
| 7474 | found = 1; |
| 7475 | break; |
| 7476 | } |
| 7477 | } |
| 7478 | if (!found) |
| 7479 | return 0; |
| 7480 | return inetaddr_host_lim(ctx.line+ctx.val, ctx.line+ctx.val+ctx.vlen); |
| 7481 | } |
| 7482 | |
| 7483 | /* negative occurrence, we scan all the list then walk back */ |
| 7484 | if (-occ > MAX_HDR_HISTORY) |
| 7485 | return 0; |
| 7486 | |
| 7487 | hist_ptr = 0; |
| 7488 | hdr_hist[hist_ptr] = 0; |
| 7489 | while (http_find_header2(hname, hlen, msg->sol, idx, &ctx)) { |
| 7490 | hdr_hist[hist_ptr++] = inetaddr_host_lim(ctx.line+ctx.val, ctx.line+ctx.val+ctx.vlen); |
| 7491 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 7492 | hist_ptr = 0; |
| 7493 | found++; |
| 7494 | } |
| 7495 | if (-occ > found) |
| 7496 | return 0; |
| 7497 | /* OK now we have the last occurrence in [hist_ptr-1], and we need to |
| 7498 | * find occurrence -occ, so we have to check [hist_ptr+occ]. |
| 7499 | */ |
| 7500 | hist_ptr += occ; |
| 7501 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 7502 | hist_ptr -= MAX_HDR_HISTORY; |
| 7503 | return hdr_hist[hist_ptr]; |
| 7504 | } |
| 7505 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 7506 | /* |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7507 | * Print a debug line with a header |
| 7508 | */ |
| 7509 | void debug_hdr(const char *dir, struct session *t, const char *start, const char *end) |
| 7510 | { |
| 7511 | int len, max; |
| 7512 | 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] | 7513 | 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] | 7514 | max = end - start; |
| 7515 | UBOUND(max, sizeof(trash) - len - 1); |
| 7516 | len += strlcpy2(trash + len, start, max + 1); |
| 7517 | trash[len++] = '\n'; |
| 7518 | write(1, trash, len); |
| 7519 | } |
| 7520 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7521 | /* |
| 7522 | * Initialize a new HTTP transaction for session <s>. It is assumed that all |
| 7523 | * the required fields are properly allocated and that we only need to (re)init |
| 7524 | * them. This should be used before processing any new request. |
| 7525 | */ |
| 7526 | void http_init_txn(struct session *s) |
| 7527 | { |
| 7528 | struct http_txn *txn = &s->txn; |
| 7529 | struct proxy *fe = s->fe; |
| 7530 | |
| 7531 | txn->flags = 0; |
| 7532 | txn->status = -1; |
| 7533 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7534 | txn->cookie_first_date = 0; |
| 7535 | txn->cookie_last_date = 0; |
| 7536 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7537 | txn->req.sol = txn->req.eol = NULL; |
| 7538 | txn->req.som = txn->req.eoh = 0; /* relative to the buffer */ |
| 7539 | txn->rsp.sol = txn->rsp.eol = NULL; |
| 7540 | txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */ |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 7541 | txn->req.chunk_len = 0LL; |
| 7542 | txn->req.body_len = 0LL; |
| 7543 | txn->rsp.chunk_len = 0LL; |
| 7544 | txn->rsp.body_len = 0LL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7545 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 7546 | 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] | 7547 | |
| 7548 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7549 | |
| 7550 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 7551 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 7552 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 7553 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7554 | if (txn->req.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7555 | memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *)); |
| 7556 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7557 | if (txn->rsp.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7558 | memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
| 7559 | |
| 7560 | if (txn->hdr_idx.v) |
| 7561 | hdr_idx_init(&txn->hdr_idx); |
| 7562 | } |
| 7563 | |
| 7564 | /* to be used at the end of a transaction */ |
| 7565 | void http_end_txn(struct session *s) |
| 7566 | { |
| 7567 | struct http_txn *txn = &s->txn; |
| 7568 | |
| 7569 | /* these ones will have been dynamically allocated */ |
| 7570 | pool_free2(pool2_requri, txn->uri); |
| 7571 | pool_free2(pool2_capture, txn->cli_cookie); |
| 7572 | pool_free2(pool2_capture, txn->srv_cookie); |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7573 | pool_free2(apools.sessid, txn->sessid); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 7574 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7575 | txn->sessid = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7576 | txn->uri = NULL; |
| 7577 | txn->srv_cookie = NULL; |
| 7578 | txn->cli_cookie = NULL; |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7579 | |
| 7580 | if (txn->req.cap) { |
| 7581 | struct cap_hdr *h; |
| 7582 | for (h = s->fe->req_cap; h; h = h->next) |
| 7583 | pool_free2(h->pool, txn->req.cap[h->index]); |
| 7584 | memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *)); |
| 7585 | } |
| 7586 | |
| 7587 | if (txn->rsp.cap) { |
| 7588 | struct cap_hdr *h; |
| 7589 | for (h = s->fe->rsp_cap; h; h = h->next) |
| 7590 | pool_free2(h->pool, txn->rsp.cap[h->index]); |
| 7591 | memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *)); |
| 7592 | } |
| 7593 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7594 | } |
| 7595 | |
| 7596 | /* to be used at the end of a transaction to prepare a new one */ |
| 7597 | void http_reset_txn(struct session *s) |
| 7598 | { |
| 7599 | http_end_txn(s); |
| 7600 | http_init_txn(s); |
| 7601 | |
| 7602 | s->be = s->fe; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7603 | s->logs.logwait = s->fe->to_log; |
Simon Horman | af51495 | 2011-06-21 14:34:57 +0900 | [diff] [blame] | 7604 | session_del_srv_conn(s); |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 7605 | clear_target(&s->target); |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 7606 | /* re-init store persistence */ |
| 7607 | s->store_count = 0; |
| 7608 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7609 | s->pend_pos = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7610 | |
| 7611 | s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */ |
| 7612 | |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 7613 | /* We must trim any excess data from the response buffer, because we |
| 7614 | * may have blocked an invalid response from a server that we don't |
| 7615 | * want to accidentely forward once we disable the analysers, nor do |
| 7616 | * we want those data to come along with next response. A typical |
| 7617 | * example of such data would be from a buggy server responding to |
| 7618 | * a HEAD with some data, or sending more than the advertised |
| 7619 | * content-length. |
| 7620 | */ |
| 7621 | if (unlikely(s->rep->l > s->rep->send_max)) { |
| 7622 | s->rep->l = s->rep->send_max; |
| 7623 | s->rep->r = s->rep->w + s->rep->l; |
| 7624 | if (s->rep->r >= s->rep->data + s->rep->size) |
| 7625 | s->rep->r -= s->rep->size; |
| 7626 | } |
| 7627 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7628 | s->req->rto = s->fe->timeout.client; |
Willy Tarreau | d04e858 | 2010-05-31 12:31:35 +0200 | [diff] [blame] | 7629 | s->req->wto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7630 | |
Willy Tarreau | d04e858 | 2010-05-31 12:31:35 +0200 | [diff] [blame] | 7631 | s->rep->rto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7632 | s->rep->wto = s->fe->timeout.client; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7633 | |
| 7634 | s->req->rex = TICK_ETERNITY; |
| 7635 | s->req->wex = TICK_ETERNITY; |
| 7636 | s->req->analyse_exp = TICK_ETERNITY; |
| 7637 | s->rep->rex = TICK_ETERNITY; |
| 7638 | s->rep->wex = TICK_ETERNITY; |
| 7639 | s->rep->analyse_exp = TICK_ETERNITY; |
| 7640 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7641 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 7642 | void free_http_req_rules(struct list *r) { |
| 7643 | struct http_req_rule *tr, *pr; |
| 7644 | |
| 7645 | list_for_each_entry_safe(pr, tr, r, list) { |
| 7646 | LIST_DEL(&pr->list); |
| 7647 | if (pr->action == HTTP_REQ_ACT_HTTP_AUTH) |
| 7648 | free(pr->http_auth.realm); |
| 7649 | |
| 7650 | free(pr); |
| 7651 | } |
| 7652 | } |
| 7653 | |
| 7654 | struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
| 7655 | { |
| 7656 | struct http_req_rule *rule; |
| 7657 | int cur_arg; |
| 7658 | |
| 7659 | rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule)); |
| 7660 | if (!rule) { |
| 7661 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
| 7662 | return NULL; |
| 7663 | } |
| 7664 | |
| 7665 | if (!*args[0]) { |
| 7666 | goto req_error_parsing; |
| 7667 | } else if (!strcmp(args[0], "allow")) { |
| 7668 | rule->action = HTTP_REQ_ACT_ALLOW; |
| 7669 | cur_arg = 1; |
| 7670 | } else if (!strcmp(args[0], "deny")) { |
| 7671 | rule->action = HTTP_REQ_ACT_DENY; |
| 7672 | cur_arg = 1; |
| 7673 | } else if (!strcmp(args[0], "auth")) { |
| 7674 | rule->action = HTTP_REQ_ACT_HTTP_AUTH; |
| 7675 | cur_arg = 1; |
| 7676 | |
| 7677 | while(*args[cur_arg]) { |
| 7678 | if (!strcmp(args[cur_arg], "realm")) { |
| 7679 | rule->http_auth.realm = strdup(args[cur_arg + 1]); |
| 7680 | cur_arg+=2; |
| 7681 | continue; |
| 7682 | } else |
| 7683 | break; |
| 7684 | } |
| 7685 | } else { |
| 7686 | req_error_parsing: |
| 7687 | Alert("parsing [%s:%d]: %s '%s', expects 'allow', 'deny', 'auth'.\n", |
| 7688 | file, linenum, *args[1]?"unknown parameter":"missing keyword in", args[*args[1]?1:0]); |
| 7689 | return NULL; |
| 7690 | } |
| 7691 | |
| 7692 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 7693 | struct acl_cond *cond; |
| 7694 | |
| 7695 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg)) == NULL) { |
| 7696 | Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition.\n", |
| 7697 | file, linenum, args[0]); |
| 7698 | return NULL; |
| 7699 | } |
| 7700 | rule->cond = cond; |
| 7701 | } |
| 7702 | else if (*args[cur_arg]) { |
| 7703 | Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or" |
| 7704 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 7705 | file, linenum, args[0], args[cur_arg]); |
| 7706 | return NULL; |
| 7707 | } |
| 7708 | |
| 7709 | return rule; |
| 7710 | } |
| 7711 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7712 | /************************************************************************/ |
| 7713 | /* The code below is dedicated to ACL parsing and matching */ |
| 7714 | /************************************************************************/ |
| 7715 | |
| 7716 | |
| 7717 | |
| 7718 | |
| 7719 | /* 1. Check on METHOD |
| 7720 | * We use the pre-parsed method if it is known, and store its number as an |
| 7721 | * integer. If it is unknown, we use the pointer and the length. |
| 7722 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7723 | 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] | 7724 | { |
| 7725 | int len, meth; |
| 7726 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7727 | len = strlen(*text); |
| 7728 | meth = find_http_meth(*text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7729 | |
| 7730 | pattern->val.i = meth; |
| 7731 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7732 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7733 | if (!pattern->ptr.str) |
| 7734 | return 0; |
| 7735 | pattern->len = len; |
| 7736 | } |
| 7737 | return 1; |
| 7738 | } |
| 7739 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7740 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 7741 | acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7742 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7743 | { |
| 7744 | int meth; |
| 7745 | struct http_txn *txn = l7; |
| 7746 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7747 | if (!txn) |
| 7748 | return 0; |
| 7749 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7750 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7751 | return 0; |
| 7752 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7753 | meth = txn->meth; |
| 7754 | test->i = meth; |
| 7755 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7756 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7757 | /* ensure the indexes are not affected */ |
| 7758 | return 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7759 | test->len = txn->req.sl.rq.m_l; |
| 7760 | test->ptr = txn->req.sol; |
| 7761 | } |
| 7762 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 7763 | return 1; |
| 7764 | } |
| 7765 | |
| 7766 | static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern) |
| 7767 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 7768 | int icase; |
| 7769 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7770 | if (test->i != pattern->val.i) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 7771 | return ACL_PAT_FAIL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7772 | |
| 7773 | if (test->i != HTTP_METH_OTHER) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 7774 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7775 | |
| 7776 | /* Other method, we must compare the strings */ |
| 7777 | if (pattern->len != test->len) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 7778 | return ACL_PAT_FAIL; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 7779 | |
| 7780 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
| 7781 | if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) || |
| 7782 | (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0)) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 7783 | return ACL_PAT_FAIL; |
| 7784 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7785 | } |
| 7786 | |
| 7787 | /* 2. Check on Request/Status Version |
| 7788 | * We simply compare strings here. |
| 7789 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7790 | 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] | 7791 | { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7792 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7793 | if (!pattern->ptr.str) |
| 7794 | return 0; |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7795 | pattern->len = strlen(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7796 | return 1; |
| 7797 | } |
| 7798 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7799 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 7800 | acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7801 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7802 | { |
| 7803 | struct http_txn *txn = l7; |
| 7804 | char *ptr; |
| 7805 | int len; |
| 7806 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7807 | if (!txn) |
| 7808 | return 0; |
| 7809 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7810 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7811 | return 0; |
| 7812 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7813 | len = txn->req.sl.rq.v_l; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7814 | ptr = txn->req.sol + txn->req.sl.rq.v; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7815 | |
| 7816 | while ((len-- > 0) && (*ptr++ != '/')); |
| 7817 | if (len <= 0) |
| 7818 | return 0; |
| 7819 | |
| 7820 | test->ptr = ptr; |
| 7821 | test->len = len; |
| 7822 | |
| 7823 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 7824 | return 1; |
| 7825 | } |
| 7826 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7827 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 7828 | acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7829 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7830 | { |
| 7831 | struct http_txn *txn = l7; |
| 7832 | char *ptr; |
| 7833 | int len; |
| 7834 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7835 | if (!txn) |
| 7836 | return 0; |
| 7837 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7838 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7839 | return 0; |
| 7840 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7841 | len = txn->rsp.sl.st.v_l; |
| 7842 | ptr = txn->rsp.sol; |
| 7843 | |
| 7844 | while ((len-- > 0) && (*ptr++ != '/')); |
| 7845 | if (len <= 0) |
| 7846 | return 0; |
| 7847 | |
| 7848 | test->ptr = ptr; |
| 7849 | test->len = len; |
| 7850 | |
| 7851 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 7852 | return 1; |
| 7853 | } |
| 7854 | |
| 7855 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7856 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 7857 | acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7858 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7859 | { |
| 7860 | struct http_txn *txn = l7; |
| 7861 | char *ptr; |
| 7862 | int len; |
| 7863 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7864 | if (!txn) |
| 7865 | return 0; |
| 7866 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7867 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7868 | return 0; |
| 7869 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7870 | len = txn->rsp.sl.st.c_l; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7871 | ptr = txn->rsp.sol + txn->rsp.sl.st.c; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7872 | |
| 7873 | test->i = __strl2ui(ptr, len); |
| 7874 | test->flags = ACL_TEST_F_VOL_1ST; |
| 7875 | return 1; |
| 7876 | } |
| 7877 | |
| 7878 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7879 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 7880 | acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7881 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7882 | { |
| 7883 | struct http_txn *txn = l7; |
| 7884 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7885 | if (!txn) |
| 7886 | return 0; |
| 7887 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7888 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7889 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7890 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7891 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7892 | /* ensure the indexes are not affected */ |
| 7893 | return 0; |
| 7894 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7895 | test->len = txn->req.sl.rq.u_l; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7896 | test->ptr = txn->req.sol + txn->req.sl.rq.u; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7897 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 7898 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 7899 | test->flags = ACL_TEST_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7900 | return 1; |
| 7901 | } |
| 7902 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7903 | static int |
| 7904 | acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7905 | struct acl_expr *expr, struct acl_test *test) |
| 7906 | { |
| 7907 | struct http_txn *txn = l7; |
| 7908 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7909 | if (!txn) |
| 7910 | return 0; |
| 7911 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7912 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7913 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7914 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7915 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7916 | /* ensure the indexes are not affected */ |
| 7917 | return 0; |
| 7918 | |
| 7919 | /* Parse HTTP request */ |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 7920 | url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->addr.s.to); |
| 7921 | test->ptr = (void *)&((struct sockaddr_in *)&l4->req->cons->addr.s.to)->sin_addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7922 | test->i = AF_INET; |
| 7923 | |
| 7924 | /* |
| 7925 | * If we are parsing url in frontend space, we prepare backend stage |
| 7926 | * to not parse again the same url ! optimization lazyness... |
| 7927 | */ |
| 7928 | if (px->options & PR_O_HTTP_PROXY) |
| 7929 | l4->flags |= SN_ADDR_SET; |
| 7930 | |
| 7931 | test->flags = ACL_TEST_F_READ_ONLY; |
| 7932 | return 1; |
| 7933 | } |
| 7934 | |
| 7935 | static int |
| 7936 | acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7937 | struct acl_expr *expr, struct acl_test *test) |
| 7938 | { |
| 7939 | struct http_txn *txn = l7; |
| 7940 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7941 | if (!txn) |
| 7942 | return 0; |
| 7943 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7944 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7945 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7946 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7947 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7948 | /* ensure the indexes are not affected */ |
| 7949 | return 0; |
| 7950 | |
| 7951 | /* Same optimization as url_ip */ |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 7952 | url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->addr.s.to); |
| 7953 | test->i = ntohs(((struct sockaddr_in *)&l4->req->cons->addr.s.to)->sin_port); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7954 | |
| 7955 | if (px->options & PR_O_HTTP_PROXY) |
| 7956 | l4->flags |= SN_ADDR_SET; |
| 7957 | |
| 7958 | test->flags = ACL_TEST_F_READ_ONLY; |
| 7959 | return 1; |
| 7960 | } |
| 7961 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7962 | /* 5. Check on HTTP header. A pointer to the beginning of the value is returned. |
| 7963 | * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr(). |
| 7964 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7965 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7966 | 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] | 7967 | struct acl_expr *expr, struct acl_test *test) |
| 7968 | { |
| 7969 | struct http_txn *txn = l7; |
| 7970 | struct hdr_idx *idx = &txn->hdr_idx; |
| 7971 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7972 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7973 | if (!txn) |
| 7974 | return 0; |
| 7975 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7976 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 7977 | /* search for header from the beginning */ |
| 7978 | ctx->idx = 0; |
| 7979 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7980 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 7981 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 7982 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 7983 | test->len = ctx->vlen; |
| 7984 | test->ptr = (char *)ctx->line + ctx->val; |
| 7985 | return 1; |
| 7986 | } |
| 7987 | |
| 7988 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 7989 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 7990 | return 0; |
| 7991 | } |
| 7992 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7993 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7994 | acl_fetch_chdr(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->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8003 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8004 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8005 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 8006 | /* ensure the indexes are not affected */ |
| 8007 | return 0; |
| 8008 | |
| 8009 | return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test); |
| 8010 | } |
| 8011 | |
| 8012 | static int |
| 8013 | acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8014 | struct acl_expr *expr, struct acl_test *test) |
| 8015 | { |
| 8016 | struct http_txn *txn = l7; |
| 8017 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8018 | if (!txn) |
| 8019 | return 0; |
| 8020 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8021 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8022 | return 0; |
| 8023 | |
| 8024 | return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test); |
| 8025 | } |
| 8026 | |
| 8027 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 8028 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 8029 | */ |
| 8030 | static int |
| 8031 | 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] | 8032 | struct acl_expr *expr, struct acl_test *test) |
| 8033 | { |
| 8034 | struct http_txn *txn = l7; |
| 8035 | struct hdr_idx *idx = &txn->hdr_idx; |
| 8036 | struct hdr_ctx ctx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8037 | int cnt; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8038 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8039 | if (!txn) |
| 8040 | return 0; |
| 8041 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8042 | ctx.idx = 0; |
| 8043 | cnt = 0; |
| 8044 | while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx)) |
| 8045 | cnt++; |
| 8046 | |
| 8047 | test->i = cnt; |
| 8048 | test->flags = ACL_TEST_F_VOL_HDR; |
| 8049 | return 1; |
| 8050 | } |
| 8051 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8052 | static int |
| 8053 | acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8054 | struct acl_expr *expr, struct acl_test *test) |
| 8055 | { |
| 8056 | struct http_txn *txn = l7; |
| 8057 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8058 | if (!txn) |
| 8059 | return 0; |
| 8060 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8061 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8062 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8063 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8064 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 8065 | /* ensure the indexes are not affected */ |
| 8066 | return 0; |
| 8067 | |
| 8068 | return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test); |
| 8069 | } |
| 8070 | |
| 8071 | static int |
| 8072 | acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8073 | struct acl_expr *expr, struct acl_test *test) |
| 8074 | { |
| 8075 | struct http_txn *txn = l7; |
| 8076 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8077 | if (!txn) |
| 8078 | return 0; |
| 8079 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8080 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8081 | return 0; |
| 8082 | |
| 8083 | return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test); |
| 8084 | } |
| 8085 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8086 | /* 7. Check on HTTP header's integer value. The integer value is returned. |
| 8087 | * FIXME: the type is 'int', it may not be appropriate for everything. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8088 | * 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] | 8089 | */ |
| 8090 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8091 | 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] | 8092 | struct acl_expr *expr, struct acl_test *test) |
| 8093 | { |
| 8094 | struct http_txn *txn = l7; |
| 8095 | struct hdr_idx *idx = &txn->hdr_idx; |
| 8096 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8097 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8098 | if (!txn) |
| 8099 | return 0; |
| 8100 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8101 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 8102 | /* search for header from the beginning */ |
| 8103 | ctx->idx = 0; |
| 8104 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8105 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 8106 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 8107 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 8108 | test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen); |
| 8109 | return 1; |
| 8110 | } |
| 8111 | |
| 8112 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 8113 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 8114 | return 0; |
| 8115 | } |
| 8116 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8117 | static int |
| 8118 | acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8119 | struct acl_expr *expr, struct acl_test *test) |
| 8120 | { |
| 8121 | struct http_txn *txn = l7; |
| 8122 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8123 | if (!txn) |
| 8124 | return 0; |
| 8125 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8126 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8127 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8128 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8129 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 8130 | /* ensure the indexes are not affected */ |
| 8131 | return 0; |
| 8132 | |
| 8133 | return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test); |
| 8134 | } |
| 8135 | |
| 8136 | static int |
| 8137 | acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8138 | struct acl_expr *expr, struct acl_test *test) |
| 8139 | { |
| 8140 | struct http_txn *txn = l7; |
| 8141 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8142 | if (!txn) |
| 8143 | return 0; |
| 8144 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8145 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8146 | return 0; |
| 8147 | |
| 8148 | return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test); |
| 8149 | } |
| 8150 | |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 8151 | /* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned. |
| 8152 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 8153 | */ |
| 8154 | static int |
| 8155 | acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol, |
| 8156 | struct acl_expr *expr, struct acl_test *test) |
| 8157 | { |
| 8158 | struct http_txn *txn = l7; |
| 8159 | struct hdr_idx *idx = &txn->hdr_idx; |
| 8160 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
| 8161 | |
| 8162 | if (!txn) |
| 8163 | return 0; |
| 8164 | |
| 8165 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 8166 | /* search for header from the beginning */ |
| 8167 | ctx->idx = 0; |
| 8168 | |
| 8169 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 8170 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 8171 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 8172 | /* Same optimization as url_ip */ |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 8173 | memset(&((struct sockaddr_in *)&l4->req->cons->addr.s.to)->sin_addr, 0, sizeof(((struct sockaddr_in *)&l4->req->cons->addr.s.to)->sin_addr)); |
| 8174 | url2ipv4((char *)ctx->line + ctx->val, &((struct sockaddr_in *)&l4->req->cons->addr.s.to)->sin_addr); |
| 8175 | test->ptr = (void *)&((struct sockaddr_in *)&l4->req->cons->addr.s.to)->sin_addr; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 8176 | test->i = AF_INET; |
| 8177 | return 1; |
| 8178 | } |
| 8179 | |
| 8180 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 8181 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 8182 | return 0; |
| 8183 | } |
| 8184 | |
| 8185 | static int |
| 8186 | acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8187 | struct acl_expr *expr, struct acl_test *test) |
| 8188 | { |
| 8189 | struct http_txn *txn = l7; |
| 8190 | |
| 8191 | if (!txn) |
| 8192 | return 0; |
| 8193 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8194 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 8195 | return 0; |
| 8196 | |
| 8197 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 8198 | /* ensure the indexes are not affected */ |
| 8199 | return 0; |
| 8200 | |
| 8201 | return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test); |
| 8202 | } |
| 8203 | |
| 8204 | static int |
| 8205 | acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8206 | struct acl_expr *expr, struct acl_test *test) |
| 8207 | { |
| 8208 | struct http_txn *txn = l7; |
| 8209 | |
| 8210 | if (!txn) |
| 8211 | return 0; |
| 8212 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8213 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 8214 | return 0; |
| 8215 | |
| 8216 | return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test); |
| 8217 | } |
| 8218 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8219 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 8220 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 8221 | */ |
| 8222 | static int |
| 8223 | acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8224 | struct acl_expr *expr, struct acl_test *test) |
| 8225 | { |
| 8226 | struct http_txn *txn = l7; |
| 8227 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8228 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8229 | if (!txn) |
| 8230 | return 0; |
| 8231 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8232 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8233 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8234 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8235 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 8236 | /* ensure the indexes are not affected */ |
| 8237 | return 0; |
| 8238 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 8239 | 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] | 8240 | ptr = http_get_path(txn); |
| 8241 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8242 | return 0; |
| 8243 | |
| 8244 | /* OK, we got the '/' ! */ |
| 8245 | test->ptr = ptr; |
| 8246 | |
| 8247 | while (ptr < end && *ptr != '?') |
| 8248 | ptr++; |
| 8249 | |
| 8250 | test->len = ptr - test->ptr; |
| 8251 | |
| 8252 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 8253 | test->flags = ACL_TEST_F_VOL_1ST; |
| 8254 | return 1; |
| 8255 | } |
| 8256 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8257 | static int |
| 8258 | acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir, |
| 8259 | struct acl_expr *expr, struct acl_test *test) |
| 8260 | { |
| 8261 | struct buffer *req = s->req; |
| 8262 | struct http_txn *txn = &s->txn; |
| 8263 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8264 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8265 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 8266 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 8267 | */ |
| 8268 | |
| 8269 | if (!s || !req) |
| 8270 | return 0; |
| 8271 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8272 | if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8273 | /* Already decoded as OK */ |
| 8274 | test->flags |= ACL_TEST_F_SET_RES_PASS; |
| 8275 | return 1; |
| 8276 | } |
| 8277 | |
| 8278 | /* Try to decode HTTP request */ |
| 8279 | if (likely(req->lr < req->r)) |
| 8280 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
| 8281 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8282 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8283 | if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) { |
| 8284 | test->flags |= ACL_TEST_F_SET_RES_FAIL; |
| 8285 | return 1; |
| 8286 | } |
| 8287 | /* wait for final state */ |
| 8288 | test->flags |= ACL_TEST_F_MAY_CHANGE; |
| 8289 | return 0; |
| 8290 | } |
| 8291 | |
| 8292 | /* OK we got a valid HTTP request. We have some minor preparation to |
| 8293 | * perform so that further checks can rely on HTTP tests. |
| 8294 | */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 8295 | txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8296 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 8297 | s->flags |= SN_REDIRECTABLE; |
| 8298 | |
| 8299 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) { |
| 8300 | test->flags |= ACL_TEST_F_SET_RES_FAIL; |
| 8301 | return 1; |
| 8302 | } |
| 8303 | |
| 8304 | test->flags |= ACL_TEST_F_SET_RES_PASS; |
| 8305 | return 1; |
| 8306 | } |
| 8307 | |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 8308 | /* return a valid test if the current request is the first one on the connection */ |
| 8309 | static int |
| 8310 | acl_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, int dir, |
| 8311 | struct acl_expr *expr, struct acl_test *test) |
| 8312 | { |
| 8313 | if (!s) |
| 8314 | return 0; |
| 8315 | |
| 8316 | if (s->txn.flags & TX_NOT_FIRST) |
| 8317 | test->flags |= ACL_TEST_F_SET_RES_FAIL; |
| 8318 | else |
| 8319 | test->flags |= ACL_TEST_F_SET_RES_PASS; |
| 8320 | |
| 8321 | return 1; |
| 8322 | } |
| 8323 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8324 | static int |
| 8325 | acl_fetch_http_auth(struct proxy *px, struct session *s, void *l7, int dir, |
| 8326 | struct acl_expr *expr, struct acl_test *test) |
| 8327 | { |
| 8328 | |
| 8329 | if (!s) |
| 8330 | return 0; |
| 8331 | |
| 8332 | if (!get_http_auth(s)) |
| 8333 | return 0; |
| 8334 | |
| 8335 | test->ctx.a[0] = expr->arg.ul; |
| 8336 | test->ctx.a[1] = s->txn.auth.user; |
| 8337 | test->ctx.a[2] = s->txn.auth.pass; |
| 8338 | |
| 8339 | test->flags |= ACL_TEST_F_READ_ONLY | ACL_TEST_F_NULL_MATCH; |
| 8340 | |
| 8341 | return 1; |
| 8342 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8343 | |
| 8344 | /************************************************************************/ |
| 8345 | /* All supported keywords must be declared here. */ |
| 8346 | /************************************************************************/ |
| 8347 | |
| 8348 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 8349 | static struct acl_kw_list acl_kws = {{ },{ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8350 | { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT }, |
| 8351 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 8352 | { "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] | 8353 | { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP }, |
| 8354 | { "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] | 8355 | { "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] | 8356 | |
Willy Tarreau | c426296 | 2010-05-10 23:42:40 +0200 | [diff] [blame] | 8357 | { "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] | 8358 | { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 8359 | { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 8360 | { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 8361 | { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 8362 | { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 8363 | { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
Willy Tarreau | b337b53 | 2010-05-13 20:03:41 +0200 | [diff] [blame] | 8364 | { "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] | 8365 | { "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] | 8366 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 8367 | /* 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] | 8368 | { "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] | 8369 | { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 8370 | { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 8371 | { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 8372 | { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 8373 | { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 8374 | { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 8375 | { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE }, |
| 8376 | { "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] | 8377 | { "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] | 8378 | |
Willy Tarreau | c426296 | 2010-05-10 23:42:40 +0200 | [diff] [blame] | 8379 | { "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] | 8380 | { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE }, |
| 8381 | { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE }, |
| 8382 | { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE }, |
| 8383 | { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE }, |
| 8384 | { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE }, |
| 8385 | { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE }, |
| 8386 | { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE }, |
| 8387 | { "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] | 8388 | { "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] | 8389 | |
Willy Tarreau | c426296 | 2010-05-10 23:42:40 +0200 | [diff] [blame] | 8390 | { "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] | 8391 | { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 8392 | { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 8393 | { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 8394 | { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 8395 | { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 8396 | { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8397 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 8398 | #if 0 |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8399 | { "line", acl_parse_str, acl_fetch_line, acl_match_str }, |
| 8400 | { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg }, |
| 8401 | { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg }, |
| 8402 | { "line_end", acl_parse_str, acl_fetch_line, acl_match_end }, |
| 8403 | { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub }, |
| 8404 | { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir }, |
| 8405 | { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom }, |
| 8406 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8407 | { "cook", acl_parse_str, acl_fetch_cook, acl_match_str }, |
| 8408 | { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg }, |
| 8409 | { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg }, |
| 8410 | { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end }, |
| 8411 | { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub }, |
| 8412 | { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir }, |
| 8413 | { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom }, |
| 8414 | { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst }, |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8415 | #endif |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8416 | |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 8417 | { "http_auth", acl_parse_nothing, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT }, |
| 8418 | { "http_auth_group", acl_parse_strcat, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT }, |
| 8419 | { "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] | 8420 | { NULL, NULL, NULL, NULL }, |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8421 | }}; |
| 8422 | |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8423 | /************************************************************************/ |
| 8424 | /* The code below is dedicated to pattern fetching and matching */ |
| 8425 | /************************************************************************/ |
| 8426 | |
| 8427 | /* extract the IP address from the last occurrence of specified header. Note |
| 8428 | * that we should normally first extract the string then convert it to IP, |
| 8429 | * but right now we have all the functions to do this seemlessly, and we will |
| 8430 | * be able to change that later without touching the configuration. |
| 8431 | */ |
| 8432 | static int |
| 8433 | pattern_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 8434 | 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] | 8435 | { |
| 8436 | struct http_txn *txn = l7; |
| 8437 | |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 8438 | data->ip.s_addr = htonl(get_ip_from_hdr2(&txn->req, arg_p->data.str.str, arg_p->data.str.len, &txn->hdr_idx, -1)); |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8439 | return data->ip.s_addr != 0; |
| 8440 | } |
| 8441 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8442 | /* |
| 8443 | * Given a path string and its length, find the position of beginning of the |
| 8444 | * query string. Returns NULL if no query string is found in the path. |
| 8445 | * |
| 8446 | * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22: |
| 8447 | * |
| 8448 | * find_query_string(path, n) points to "yo=mama;ye=daddy" string. |
| 8449 | */ |
| 8450 | static inline char *find_query_string(char *path, size_t path_l) |
| 8451 | { |
| 8452 | char *p; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 8453 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8454 | p = memchr(path, '?', path_l); |
| 8455 | return p ? p + 1 : NULL; |
| 8456 | } |
| 8457 | |
| 8458 | static inline int is_param_delimiter(char c) |
| 8459 | { |
| 8460 | return c == '&' || c == ';'; |
| 8461 | } |
| 8462 | |
| 8463 | /* |
| 8464 | * Given a url parameter, find the starting position of the first occurence, |
| 8465 | * or NULL if the parameter is not found. |
| 8466 | * |
| 8467 | * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye", |
| 8468 | * the function will return query_string+8. |
| 8469 | */ |
| 8470 | static char* |
| 8471 | find_url_param_pos(char* query_string, size_t query_string_l, |
| 8472 | char* url_param_name, size_t url_param_name_l) |
| 8473 | { |
| 8474 | char *pos, *last; |
| 8475 | |
| 8476 | pos = query_string; |
| 8477 | last = query_string + query_string_l - url_param_name_l - 1; |
| 8478 | |
| 8479 | while (pos <= last) { |
| 8480 | if (pos[url_param_name_l] == '=') { |
| 8481 | if (memcmp(pos, url_param_name, url_param_name_l) == 0) |
| 8482 | return pos; |
| 8483 | pos += url_param_name_l + 1; |
| 8484 | } |
| 8485 | while (pos <= last && !is_param_delimiter(*pos)) |
| 8486 | pos++; |
| 8487 | pos++; |
| 8488 | } |
| 8489 | return NULL; |
| 8490 | } |
| 8491 | |
| 8492 | /* |
| 8493 | * Given a url parameter name, returns its value and size into *value and |
| 8494 | * *value_l respectively, and returns non-zero. If the parameter is not found, |
| 8495 | * zero is returned and value/value_l are not touched. |
| 8496 | */ |
| 8497 | static int |
| 8498 | find_url_param_value(char* path, size_t path_l, |
| 8499 | char* url_param_name, size_t url_param_name_l, |
| 8500 | char** value, size_t* value_l) |
| 8501 | { |
| 8502 | char *query_string, *qs_end; |
| 8503 | char *arg_start; |
| 8504 | char *value_start, *value_end; |
| 8505 | |
| 8506 | query_string = find_query_string(path, path_l); |
| 8507 | if (!query_string) |
| 8508 | return 0; |
| 8509 | |
| 8510 | qs_end = path + path_l; |
| 8511 | arg_start = find_url_param_pos(query_string, qs_end - query_string, |
| 8512 | url_param_name, url_param_name_l); |
| 8513 | if (!arg_start) |
| 8514 | return 0; |
| 8515 | |
| 8516 | value_start = arg_start + url_param_name_l + 1; |
| 8517 | value_end = value_start; |
| 8518 | |
| 8519 | while ((value_end < qs_end) && !is_param_delimiter(*value_end)) |
| 8520 | value_end++; |
| 8521 | |
| 8522 | *value = value_start; |
| 8523 | *value_l = value_end - value_start; |
Willy Tarreau | 0013433 | 2011-01-04 14:57:34 +0100 | [diff] [blame] | 8524 | return value_end != value_start; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8525 | } |
| 8526 | |
| 8527 | static int |
| 8528 | pattern_fetch_url_param(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8529 | const struct pattern_arg *arg_p, int arg_i, union pattern_data *data) |
| 8530 | { |
| 8531 | struct http_txn *txn = l7; |
| 8532 | struct http_msg *msg = &txn->req; |
| 8533 | char *url_param_value; |
| 8534 | size_t url_param_value_l; |
| 8535 | |
| 8536 | if (!find_url_param_value(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l, |
| 8537 | arg_p->data.str.str, arg_p->data.str.len, |
| 8538 | &url_param_value, &url_param_value_l)) |
| 8539 | return 0; |
| 8540 | |
| 8541 | data->str.str = url_param_value; |
| 8542 | data->str.len = url_param_value_l; |
| 8543 | return 1; |
| 8544 | } |
| 8545 | |
Willy Tarreau | b3eb221 | 2011-07-01 16:16:17 +0200 | [diff] [blame] | 8546 | /* Try to find the last occurrence of a cookie name in a cookie header value. |
| 8547 | * The pointer and size of the last occurrence of the cookie value is returned |
| 8548 | * into *value and *value_l, and the function returns non-zero if the value was |
| 8549 | * found. Otherwise if the cookie was not found, zero is returned and neither |
| 8550 | * value nor value_l are touched. The input hdr string should begin at the |
| 8551 | * header's value, and its size should be in hdr_l. <list> must be non-zero if |
| 8552 | * value may represent a list of values (cookie headers). |
| 8553 | */ |
| 8554 | static int |
| 8555 | extract_cookie_value(char *hdr, size_t hdr_l, |
| 8556 | char *cookie_name, size_t cookie_name_l, int list, |
| 8557 | char **value, size_t *value_l) |
| 8558 | { |
| 8559 | int found = 0; |
| 8560 | char *equal, *att_end, *att_beg, *hdr_end, *val_beg, *val_end; |
| 8561 | char *next; |
| 8562 | |
| 8563 | /* Note that multiple cookies may be delimited with semi-colons, so we |
| 8564 | * also have to loop on this. |
| 8565 | */ |
| 8566 | |
| 8567 | /* we search at least a cookie name followed by an equal, and more |
| 8568 | * generally something like this : |
| 8569 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 8570 | */ |
| 8571 | if (hdr_l < cookie_name_l + 1) |
| 8572 | return 0; |
| 8573 | |
| 8574 | hdr_end = hdr + hdr_l; |
| 8575 | |
| 8576 | for (att_beg = hdr; att_beg < hdr_end; att_beg = next + 1) { |
| 8577 | /* Iterate through all cookies on this line */ |
| 8578 | |
| 8579 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 8580 | att_beg++; |
| 8581 | |
| 8582 | /* find att_end : this is the first character after the last non |
| 8583 | * space before the equal. It may be equal to hdr_end. |
| 8584 | */ |
| 8585 | equal = att_end = att_beg; |
| 8586 | |
| 8587 | while (equal < hdr_end) { |
| 8588 | if (*equal == '=' || *equal == ';' || (list && *equal == ',')) |
| 8589 | break; |
| 8590 | if (http_is_spht[(unsigned char)*equal++]) |
| 8591 | continue; |
| 8592 | att_end = equal; |
| 8593 | } |
| 8594 | |
| 8595 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 8596 | * is between <att_beg> and <equal>, both may be identical. |
| 8597 | */ |
| 8598 | |
| 8599 | /* look for end of cookie if there is an equal sign */ |
| 8600 | if (equal < hdr_end && *equal == '=') { |
| 8601 | /* look for the beginning of the value */ |
| 8602 | val_beg = equal + 1; |
| 8603 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 8604 | val_beg++; |
| 8605 | |
| 8606 | /* find the end of the value, respecting quotes */ |
| 8607 | next = find_cookie_value_end(val_beg, hdr_end); |
| 8608 | |
| 8609 | /* make val_end point to the first white space or delimitor after the value */ |
| 8610 | val_end = next; |
| 8611 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 8612 | val_end--; |
| 8613 | } else { |
| 8614 | val_beg = val_end = next = equal; |
| 8615 | } |
| 8616 | |
| 8617 | /* We have nothing to do with attributes beginning with '$'. However, |
| 8618 | * they will automatically be removed if a header before them is removed, |
| 8619 | * since they're supposed to be linked together. |
| 8620 | */ |
| 8621 | if (*att_beg == '$') |
| 8622 | continue; |
| 8623 | |
| 8624 | /* Ignore cookies with no equal sign */ |
| 8625 | if (equal == next) |
| 8626 | continue; |
| 8627 | |
| 8628 | /* Now we have the cookie name between att_beg and att_end, and |
| 8629 | * its value between val_beg and val_end. |
| 8630 | */ |
| 8631 | |
| 8632 | if (att_end - att_beg == cookie_name_l && |
| 8633 | memcmp(att_beg, cookie_name, cookie_name_l) == 0) { |
| 8634 | found = 1; |
| 8635 | *value = val_beg; |
| 8636 | *value_l = val_end - val_beg; |
| 8637 | /* right now we want to catch the last occurrence |
| 8638 | * of the cookie, so let's go on searching. |
| 8639 | */ |
| 8640 | } |
| 8641 | |
| 8642 | /* Set-Cookie headers only have the name in the first attr=value part */ |
| 8643 | if (!list) |
| 8644 | break; |
| 8645 | } |
| 8646 | |
| 8647 | return found; |
| 8648 | } |
| 8649 | |
| 8650 | /* Try to find in request or response message is in <msg> and whose transaction |
| 8651 | * is in <txn> the last occurrence of a cookie name in all cookie header values |
| 8652 | * whose header name is <hdr_name> with name of length <hdr_name_len>. The |
| 8653 | * pointer and size of the last occurrence of the cookie value is returned into |
| 8654 | * <value> and <value_l>, and the function returns non-zero if the value was |
| 8655 | * found. Otherwise if the cookie was not found, zero is returned and neither |
| 8656 | * value nor value_l are touched. The input hdr string should begin at the |
| 8657 | * header's value, and its size should be in hdr_l. <list> must be non-zero if |
| 8658 | * value may represent a list of values (cookie headers). |
| 8659 | */ |
| 8660 | |
| 8661 | static int |
| 8662 | find_cookie_value(struct http_msg *msg, struct http_txn *txn, |
| 8663 | const char *hdr_name, int hdr_name_len, |
| 8664 | char *cookie_name, size_t cookie_name_l, int list, |
| 8665 | char **value, size_t *value_l) |
| 8666 | { |
| 8667 | struct hdr_ctx ctx; |
| 8668 | int found = 0; |
| 8669 | |
| 8670 | ctx.idx = 0; |
| 8671 | while (http_find_header2(hdr_name, hdr_name_len, msg->sol, &txn->hdr_idx, &ctx)) { |
| 8672 | if (ctx.vlen < cookie_name_l + 1) |
| 8673 | continue; |
| 8674 | |
| 8675 | found |= extract_cookie_value(ctx.line + ctx.val, ctx.vlen, |
| 8676 | cookie_name, cookie_name_l, 1, |
| 8677 | value, value_l); |
| 8678 | } |
| 8679 | return found; |
| 8680 | } |
| 8681 | |
| 8682 | static int |
| 8683 | pattern_fetch_cookie(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8684 | const struct pattern_arg *arg_p, int arg_i, union pattern_data *data) |
| 8685 | { |
| 8686 | struct http_txn *txn = l7; |
| 8687 | struct http_msg *msg = &txn->req; |
| 8688 | char *cookie_value; |
| 8689 | size_t cookie_value_l; |
| 8690 | int found = 0; |
| 8691 | |
| 8692 | found = find_cookie_value(msg, txn, "Cookie", 6, |
| 8693 | arg_p->data.str.str, arg_p->data.str.len, 1, |
| 8694 | &cookie_value, &cookie_value_l); |
| 8695 | if (found) { |
| 8696 | data->str.str = cookie_value; |
| 8697 | data->str.len = cookie_value_l; |
| 8698 | } |
| 8699 | |
| 8700 | return found; |
| 8701 | } |
| 8702 | |
| 8703 | |
| 8704 | static int |
| 8705 | pattern_fetch_set_cookie(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8706 | const struct pattern_arg *arg_p, int arg_i, union pattern_data *data) |
| 8707 | { |
| 8708 | struct http_txn *txn = l7; |
| 8709 | struct http_msg *msg = &txn->rsp; |
| 8710 | char *cookie_value; |
| 8711 | size_t cookie_value_l; |
| 8712 | int found = 0; |
| 8713 | |
| 8714 | found = find_cookie_value(msg, txn, "Set-Cookie", 10, |
| 8715 | arg_p->data.str.str, arg_p->data.str.len, 1, |
| 8716 | &cookie_value, &cookie_value_l); |
| 8717 | if (found) { |
| 8718 | data->str.str = cookie_value; |
| 8719 | data->str.len = cookie_value_l; |
| 8720 | } |
| 8721 | |
| 8722 | return found; |
| 8723 | } |
| 8724 | |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 8725 | |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8726 | /************************************************************************/ |
| 8727 | /* All supported keywords must be declared here. */ |
| 8728 | /************************************************************************/ |
| 8729 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 8730 | static struct pattern_fetch_kw_list pattern_fetch_keywords = {{ },{ |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 8731 | { "hdr", pattern_fetch_hdr_ip, pattern_arg_str, PATTERN_TYPE_IP, PATTERN_FETCH_REQ }, |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8732 | { "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] | 8733 | { "cookie", pattern_fetch_cookie, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ }, |
| 8734 | { "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] | 8735 | { NULL, NULL, NULL, 0, 0 }, |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8736 | }}; |
| 8737 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8738 | |
| 8739 | __attribute__((constructor)) |
| 8740 | static void __http_protocol_init(void) |
| 8741 | { |
| 8742 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8743 | pattern_register_fetches(&pattern_fetch_keywords); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8744 | } |
| 8745 | |
| 8746 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8747 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 8748 | * Local variables: |
| 8749 | * c-indent-level: 8 |
| 8750 | * c-basic-offset: 8 |
| 8751 | * End: |
| 8752 | */ |