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 | |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 22 | #include <netinet/tcp.h> |
| 23 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 24 | #include <sys/socket.h> |
| 25 | #include <sys/stat.h> |
| 26 | #include <sys/types.h> |
| 27 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 28 | #include <common/appsession.h> |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 29 | #include <common/base64.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 30 | #include <common/compat.h> |
| 31 | #include <common/config.h> |
Willy Tarreau | a4cd1f5 | 2006-12-16 19:57:26 +0100 | [diff] [blame] | 32 | #include <common/debug.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 33 | #include <common/memory.h> |
| 34 | #include <common/mini-clist.h> |
| 35 | #include <common/standard.h> |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 36 | #include <common/ticks.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 37 | #include <common/time.h> |
| 38 | #include <common/uri_auth.h> |
| 39 | #include <common/version.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 40 | |
| 41 | #include <types/capture.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 42 | #include <types/global.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 43 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 44 | #include <proto/acl.h> |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 45 | #include <proto/auth.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 46 | #include <proto/backend.h> |
| 47 | #include <proto/buffers.h> |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 48 | #include <proto/checks.h> |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 49 | #include <proto/dumpstats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 50 | #include <proto/fd.h> |
Willy Tarreau | 03fa5df | 2010-05-24 21:02:37 +0200 | [diff] [blame] | 51 | #include <proto/frontend.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 52 | #include <proto/log.h> |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 53 | #include <proto/hdr_idx.h> |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 54 | #include <proto/pattern.h> |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 55 | #include <proto/proto_tcp.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 56 | #include <proto/proto_http.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 57 | #include <proto/proxy.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 58 | #include <proto/queue.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 59 | #include <proto/server.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 60 | #include <proto/session.h> |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 61 | #include <proto/stream_interface.h> |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 62 | #include <proto/stream_sock.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 63 | #include <proto/task.h> |
| 64 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 65 | const char HTTP_100[] = |
| 66 | "HTTP/1.1 100 Continue\r\n\r\n"; |
| 67 | |
| 68 | const struct chunk http_100_chunk = { |
| 69 | .str = (char *)&HTTP_100, |
| 70 | .len = sizeof(HTTP_100)-1 |
| 71 | }; |
| 72 | |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 73 | /* Warning: no "connection" header is provided with the 3xx messages below */ |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 74 | const char *HTTP_301 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 75 | "HTTP/1.1 301 Moved Permanently\r\n" |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 76 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 77 | "Content-length: 0\r\n" |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 78 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 79 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 80 | const char *HTTP_302 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 81 | "HTTP/1.1 302 Found\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 82 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 83 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 84 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 85 | |
| 86 | /* same as 302 except that the browser MUST retry with the GET method */ |
| 87 | const char *HTTP_303 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 88 | "HTTP/1.1 303 See Other\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 89 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 90 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 91 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 92 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 93 | /* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */ |
| 94 | const char *HTTP_401_fmt = |
| 95 | "HTTP/1.0 401 Unauthorized\r\n" |
| 96 | "Cache-Control: no-cache\r\n" |
| 97 | "Connection: close\r\n" |
Willy Tarreau | 791d66d | 2006-07-08 16:53:38 +0200 | [diff] [blame] | 98 | "Content-Type: text/html\r\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 99 | "WWW-Authenticate: Basic realm=\"%s\"\r\n" |
| 100 | "\r\n" |
| 101 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 102 | |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 103 | const char *HTTP_407_fmt = |
| 104 | "HTTP/1.0 407 Unauthorized\r\n" |
| 105 | "Cache-Control: no-cache\r\n" |
| 106 | "Connection: close\r\n" |
| 107 | "Content-Type: text/html\r\n" |
| 108 | "Proxy-Authenticate: Basic realm=\"%s\"\r\n" |
| 109 | "\r\n" |
| 110 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 111 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 112 | |
| 113 | const int http_err_codes[HTTP_ERR_SIZE] = { |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 114 | [HTTP_ERR_200] = 200, /* used by "monitor-uri" */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 115 | [HTTP_ERR_400] = 400, |
| 116 | [HTTP_ERR_403] = 403, |
| 117 | [HTTP_ERR_408] = 408, |
| 118 | [HTTP_ERR_500] = 500, |
| 119 | [HTTP_ERR_502] = 502, |
| 120 | [HTTP_ERR_503] = 503, |
| 121 | [HTTP_ERR_504] = 504, |
| 122 | }; |
| 123 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 124 | static const char *http_err_msgs[HTTP_ERR_SIZE] = { |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 125 | [HTTP_ERR_200] = |
| 126 | "HTTP/1.0 200 OK\r\n" |
| 127 | "Cache-Control: no-cache\r\n" |
| 128 | "Connection: close\r\n" |
| 129 | "Content-Type: text/html\r\n" |
| 130 | "\r\n" |
| 131 | "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n", |
| 132 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 133 | [HTTP_ERR_400] = |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 134 | "HTTP/1.0 400 Bad request\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 135 | "Cache-Control: no-cache\r\n" |
| 136 | "Connection: close\r\n" |
| 137 | "Content-Type: text/html\r\n" |
| 138 | "\r\n" |
| 139 | "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n", |
| 140 | |
| 141 | [HTTP_ERR_403] = |
| 142 | "HTTP/1.0 403 Forbidden\r\n" |
| 143 | "Cache-Control: no-cache\r\n" |
| 144 | "Connection: close\r\n" |
| 145 | "Content-Type: text/html\r\n" |
| 146 | "\r\n" |
| 147 | "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n", |
| 148 | |
| 149 | [HTTP_ERR_408] = |
| 150 | "HTTP/1.0 408 Request Time-out\r\n" |
| 151 | "Cache-Control: no-cache\r\n" |
| 152 | "Connection: close\r\n" |
| 153 | "Content-Type: text/html\r\n" |
| 154 | "\r\n" |
| 155 | "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n", |
| 156 | |
| 157 | [HTTP_ERR_500] = |
| 158 | "HTTP/1.0 500 Server Error\r\n" |
| 159 | "Cache-Control: no-cache\r\n" |
| 160 | "Connection: close\r\n" |
| 161 | "Content-Type: text/html\r\n" |
| 162 | "\r\n" |
| 163 | "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n", |
| 164 | |
| 165 | [HTTP_ERR_502] = |
| 166 | "HTTP/1.0 502 Bad Gateway\r\n" |
| 167 | "Cache-Control: no-cache\r\n" |
| 168 | "Connection: close\r\n" |
| 169 | "Content-Type: text/html\r\n" |
| 170 | "\r\n" |
| 171 | "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n", |
| 172 | |
| 173 | [HTTP_ERR_503] = |
| 174 | "HTTP/1.0 503 Service Unavailable\r\n" |
| 175 | "Cache-Control: no-cache\r\n" |
| 176 | "Connection: close\r\n" |
| 177 | "Content-Type: text/html\r\n" |
| 178 | "\r\n" |
| 179 | "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n", |
| 180 | |
| 181 | [HTTP_ERR_504] = |
| 182 | "HTTP/1.0 504 Gateway Time-out\r\n" |
| 183 | "Cache-Control: no-cache\r\n" |
| 184 | "Connection: close\r\n" |
| 185 | "Content-Type: text/html\r\n" |
| 186 | "\r\n" |
| 187 | "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n", |
| 188 | |
| 189 | }; |
| 190 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 191 | /* We must put the messages here since GCC cannot initialize consts depending |
| 192 | * on strlen(). |
| 193 | */ |
| 194 | struct chunk http_err_chunks[HTTP_ERR_SIZE]; |
| 195 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 196 | #define FD_SETS_ARE_BITFIELDS |
| 197 | #ifdef FD_SETS_ARE_BITFIELDS |
| 198 | /* |
| 199 | * This map is used with all the FD_* macros to check whether a particular bit |
| 200 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 201 | * which should be encoded. When FD_ISSET() returns non-zero, it means that the |
| 202 | * byte should be encoded. Be careful to always pass bytes from 0 to 255 |
| 203 | * exclusively to the macros. |
| 204 | */ |
| 205 | fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 206 | fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 207 | |
| 208 | #else |
| 209 | #error "Check if your OS uses bitfields for fd_sets" |
| 210 | #endif |
| 211 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 212 | void init_proto_http() |
| 213 | { |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 214 | int i; |
| 215 | char *tmp; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 216 | int msg; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 217 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 218 | for (msg = 0; msg < HTTP_ERR_SIZE; msg++) { |
| 219 | if (!http_err_msgs[msg]) { |
| 220 | Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg); |
| 221 | abort(); |
| 222 | } |
| 223 | |
| 224 | http_err_chunks[msg].str = (char *)http_err_msgs[msg]; |
| 225 | http_err_chunks[msg].len = strlen(http_err_msgs[msg]); |
| 226 | } |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 227 | |
| 228 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 229 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 230 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 231 | * printable characters above. |
| 232 | */ |
| 233 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 234 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
| 235 | for (i = 0; i < 32; i++) { |
| 236 | FD_SET(i, hdr_encode_map); |
| 237 | FD_SET(i, url_encode_map); |
| 238 | } |
| 239 | for (i = 127; i < 256; i++) { |
| 240 | FD_SET(i, hdr_encode_map); |
| 241 | FD_SET(i, url_encode_map); |
| 242 | } |
| 243 | |
| 244 | tmp = "\"#{|}"; |
| 245 | while (*tmp) { |
| 246 | FD_SET(*tmp, hdr_encode_map); |
| 247 | tmp++; |
| 248 | } |
| 249 | |
| 250 | tmp = "\"#"; |
| 251 | while (*tmp) { |
| 252 | FD_SET(*tmp, url_encode_map); |
| 253 | tmp++; |
| 254 | } |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 255 | |
| 256 | /* memory allocations */ |
| 257 | pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED); |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 258 | pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 259 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 260 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 261 | /* |
| 262 | * We have 26 list of methods (1 per first letter), each of which can have |
| 263 | * up to 3 entries (2 valid, 1 null). |
| 264 | */ |
| 265 | struct http_method_desc { |
| 266 | http_meth_t meth; |
| 267 | int len; |
| 268 | const char text[8]; |
| 269 | }; |
| 270 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 271 | const struct http_method_desc http_methods[26][3] = { |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 272 | ['C' - 'A'] = { |
| 273 | [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" }, |
| 274 | }, |
| 275 | ['D' - 'A'] = { |
| 276 | [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" }, |
| 277 | }, |
| 278 | ['G' - 'A'] = { |
| 279 | [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" }, |
| 280 | }, |
| 281 | ['H' - 'A'] = { |
| 282 | [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" }, |
| 283 | }, |
| 284 | ['P' - 'A'] = { |
| 285 | [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" }, |
| 286 | [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" }, |
| 287 | }, |
| 288 | ['T' - 'A'] = { |
| 289 | [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" }, |
| 290 | }, |
| 291 | /* rest is empty like this : |
| 292 | * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" }, |
| 293 | */ |
| 294 | }; |
| 295 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 296 | /* 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] | 297 | * 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] | 298 | * RFC2616 for those chars. |
| 299 | */ |
| 300 | |
| 301 | const char http_is_spht[256] = { |
| 302 | [' '] = 1, ['\t'] = 1, |
| 303 | }; |
| 304 | |
| 305 | const char http_is_crlf[256] = { |
| 306 | ['\r'] = 1, ['\n'] = 1, |
| 307 | }; |
| 308 | |
| 309 | const char http_is_lws[256] = { |
| 310 | [' '] = 1, ['\t'] = 1, |
| 311 | ['\r'] = 1, ['\n'] = 1, |
| 312 | }; |
| 313 | |
| 314 | const char http_is_sep[256] = { |
| 315 | ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1, |
| 316 | ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1, |
| 317 | ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1, |
| 318 | ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1, |
| 319 | [' '] = 1, ['\t'] = 1, ['\\'] = 1, |
| 320 | }; |
| 321 | |
| 322 | const char http_is_ctl[256] = { |
| 323 | [0 ... 31] = 1, |
| 324 | [127] = 1, |
| 325 | }; |
| 326 | |
| 327 | /* |
| 328 | * A token is any ASCII char that is neither a separator nor a CTL char. |
| 329 | * Do not overwrite values in assignment since gcc-2.95 will not handle |
| 330 | * them correctly. Instead, define every non-CTL char's status. |
| 331 | */ |
| 332 | const char http_is_token[256] = { |
| 333 | [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1, |
| 334 | ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1, |
| 335 | ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1, |
| 336 | [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0, |
| 337 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, |
| 338 | ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1, |
| 339 | ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0, |
| 340 | ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0, |
| 341 | ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1, |
| 342 | ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1, |
| 343 | ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1, |
| 344 | ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1, |
| 345 | ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1, |
| 346 | ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1, |
| 347 | ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0, |
| 348 | ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1, |
| 349 | ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1, |
| 350 | ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1, |
| 351 | ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1, |
| 352 | ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1, |
| 353 | ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1, |
| 354 | ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1, |
| 355 | ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0, |
| 356 | ['|'] = 1, ['}'] = 0, ['~'] = 1, |
| 357 | }; |
| 358 | |
| 359 | |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 360 | /* |
| 361 | * An http ver_token is any ASCII which can be found in an HTTP version, |
| 362 | * which includes 'H', 'T', 'P', '/', '.' and any digit. |
| 363 | */ |
| 364 | const char http_is_ver_token[256] = { |
| 365 | ['.'] = 1, ['/'] = 1, |
| 366 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1, |
| 367 | ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1, |
| 368 | ['H'] = 1, ['P'] = 1, ['T'] = 1, |
| 369 | }; |
| 370 | |
| 371 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 372 | /* |
Willy Tarreau | e988a79 | 2010-01-04 21:13:14 +0100 | [diff] [blame] | 373 | * Silent debug that outputs only in strace, using fd #-1. Trash is modified. |
| 374 | */ |
| 375 | #if defined(DEBUG_FSM) |
| 376 | static void http_silent_debug(int line, struct session *s) |
| 377 | { |
| 378 | int size = 0; |
| 379 | size += snprintf(trash + size, sizeof(trash) - size, |
| 380 | "[%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", |
| 381 | line, |
| 382 | s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers, |
| 383 | 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); |
| 384 | write(-1, trash, size); |
| 385 | size = 0; |
| 386 | size += snprintf(trash + size, sizeof(trash) - size, |
| 387 | " %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", |
| 388 | line, |
| 389 | s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers, |
| 390 | 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); |
| 391 | |
| 392 | write(-1, trash, size); |
| 393 | } |
| 394 | #else |
| 395 | #define http_silent_debug(l,s) do { } while (0) |
| 396 | #endif |
| 397 | |
| 398 | /* |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 399 | * Adds a header and its CRLF at the tail of buffer <b>, just before the last |
| 400 | * CRLF. Text length is measured first, so it cannot be NULL. |
| 401 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 402 | * of headers is automatically adjusted. The number of bytes added is returned |
| 403 | * on success, otherwise <0 is returned indicating an error. |
| 404 | */ |
| 405 | int http_header_add_tail(struct buffer *b, struct http_msg *msg, |
| 406 | struct hdr_idx *hdr_idx, const char *text) |
| 407 | { |
| 408 | int bytes, len; |
| 409 | |
| 410 | len = strlen(text); |
| 411 | bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len); |
| 412 | if (!bytes) |
| 413 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 414 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 415 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 416 | } |
| 417 | |
| 418 | /* |
| 419 | * Adds a header and its CRLF at the tail of buffer <b>, just before the last |
| 420 | * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then |
| 421 | * the buffer is only opened and the space reserved, but nothing is copied. |
| 422 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 423 | * of headers is automatically adjusted. The number of bytes added is returned |
| 424 | * on success, otherwise <0 is returned indicating an error. |
| 425 | */ |
| 426 | int http_header_add_tail2(struct buffer *b, struct http_msg *msg, |
| 427 | struct hdr_idx *hdr_idx, const char *text, int len) |
| 428 | { |
| 429 | int bytes; |
| 430 | |
| 431 | bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len); |
| 432 | if (!bytes) |
| 433 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 434 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 435 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 436 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 437 | |
| 438 | /* |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 439 | * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| 440 | * If so, returns the position of the first non-space character relative to |
| 441 | * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| 442 | * to return a pointer to the place after the first space. Returns 0 if the |
| 443 | * header name does not match. Checks are case-insensitive. |
| 444 | */ |
| 445 | int http_header_match2(const char *hdr, const char *end, |
| 446 | const char *name, int len) |
| 447 | { |
| 448 | const char *val; |
| 449 | |
| 450 | if (hdr + len >= end) |
| 451 | return 0; |
| 452 | if (hdr[len] != ':') |
| 453 | return 0; |
| 454 | if (strncasecmp(hdr, name, len) != 0) |
| 455 | return 0; |
| 456 | val = hdr + len + 1; |
| 457 | while (val < end && HTTP_IS_SPHT(*val)) |
| 458 | val++; |
| 459 | if ((val >= end) && (len + 2 <= end - hdr)) |
| 460 | return len + 2; /* we may replace starting from second space */ |
| 461 | return val - hdr; |
| 462 | } |
| 463 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 464 | /* Find the end of the header value contained between <s> and <e>. See RFC2616, |
| 465 | * par 2.2 for more information. Note that it requires a valid header to return |
| 466 | * a valid result. This works for headers defined as comma-separated lists. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 467 | */ |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 468 | char *find_hdr_value_end(char *s, const char *e) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 469 | { |
| 470 | int quoted, qdpair; |
| 471 | |
| 472 | quoted = qdpair = 0; |
| 473 | for (; s < e; s++) { |
| 474 | if (qdpair) qdpair = 0; |
Willy Tarreau | 0f7f51f | 2010-08-30 11:06:34 +0200 | [diff] [blame] | 475 | else if (quoted) { |
| 476 | if (*s == '\\') qdpair = 1; |
| 477 | else if (*s == '"') quoted = 0; |
| 478 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 479 | else if (*s == '"') quoted = 1; |
| 480 | else if (*s == ',') return s; |
| 481 | } |
| 482 | return s; |
| 483 | } |
| 484 | |
| 485 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 486 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 487 | * structure holds everything necessary to use the header and find next |
| 488 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 489 | * beginning. Otherwise, the next occurrence is returned. The function returns |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 490 | * 1 when it finds a value, and 0 when there is no more. It is designed to work |
| 491 | * with headers defined as comma-separated lists. As a special case, if ctx->val |
| 492 | * is NULL when searching for a new values of a header, the current header is |
| 493 | * rescanned. This allows rescanning after a header deletion. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 494 | */ |
| 495 | int http_find_header2(const char *name, int len, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 496 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 497 | struct hdr_ctx *ctx) |
| 498 | { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 499 | char *eol, *sov; |
| 500 | int cur_idx, old_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 501 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 502 | cur_idx = ctx->idx; |
| 503 | if (cur_idx) { |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 504 | /* We have previously returned a value, let's search |
| 505 | * another one on the same line. |
| 506 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 507 | sol = ctx->line; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 508 | ctx->del = ctx->val + ctx->vlen + ctx->tws; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 509 | sov = sol + ctx->del; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 510 | eol = sol + idx->v[cur_idx].len; |
| 511 | |
| 512 | if (sov >= eol) |
| 513 | /* no more values in this header */ |
| 514 | goto next_hdr; |
| 515 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 516 | /* values remaining for this header, skip the comma but save it |
| 517 | * for later use (eg: for header deletion). |
| 518 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 519 | sov++; |
| 520 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 521 | sov++; |
| 522 | |
| 523 | goto return_hdr; |
| 524 | } |
| 525 | |
| 526 | /* first request for this header */ |
| 527 | sol += hdr_idx_first_pos(idx); |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 528 | old_idx = 0; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 529 | cur_idx = hdr_idx_first_idx(idx); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 530 | while (cur_idx) { |
| 531 | eol = sol + idx->v[cur_idx].len; |
| 532 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 533 | if (len == 0) { |
| 534 | /* No argument was passed, we want any header. |
| 535 | * To achieve this, we simply build a fake request. */ |
| 536 | while (sol + len < eol && sol[len] != ':') |
| 537 | len++; |
| 538 | name = sol; |
| 539 | } |
| 540 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 541 | if ((len < eol - sol) && |
| 542 | (sol[len] == ':') && |
| 543 | (strncasecmp(sol, name, len) == 0)) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 544 | ctx->del = len; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 545 | sov = sol + len + 1; |
| 546 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 547 | sov++; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 548 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 549 | ctx->line = sol; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 550 | ctx->prev = old_idx; |
| 551 | return_hdr: |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 552 | ctx->idx = cur_idx; |
| 553 | ctx->val = sov - sol; |
| 554 | |
| 555 | eol = find_hdr_value_end(sov, eol); |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 556 | ctx->tws = 0; |
Willy Tarreau | 275600b | 2011-09-16 08:11:26 +0200 | [diff] [blame] | 557 | while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) { |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 558 | eol--; |
| 559 | ctx->tws++; |
| 560 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 561 | ctx->vlen = eol - sov; |
| 562 | return 1; |
| 563 | } |
| 564 | next_hdr: |
| 565 | sol = eol + idx->v[cur_idx].cr + 1; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 566 | old_idx = cur_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 567 | cur_idx = idx->v[cur_idx].next; |
| 568 | } |
| 569 | return 0; |
| 570 | } |
| 571 | |
| 572 | int http_find_header(const char *name, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 573 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 574 | struct hdr_ctx *ctx) |
| 575 | { |
| 576 | return http_find_header2(name, strlen(name), sol, idx, ctx); |
| 577 | } |
| 578 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 579 | /* Remove one value of a header. This only works on a <ctx> returned by one of |
| 580 | * the http_find_header functions. The value is removed, as well as surrounding |
| 581 | * commas if any. If the removed value was alone, the whole header is removed. |
| 582 | * The ctx is always updated accordingly, as well as buffer <buf> and HTTP |
| 583 | * message <msg>. The new index is returned. If it is zero, it means there is |
| 584 | * no more header, so any processing may stop. The ctx is always left in a form |
| 585 | * that can be handled by http_find_header2() to find next occurrence. |
| 586 | */ |
| 587 | int http_remove_header2(struct http_msg *msg, struct buffer *buf, |
| 588 | struct hdr_idx *idx, struct hdr_ctx *ctx) |
| 589 | { |
| 590 | int cur_idx = ctx->idx; |
| 591 | char *sol = ctx->line; |
| 592 | struct hdr_idx_elem *hdr; |
| 593 | int delta, skip_comma; |
| 594 | |
| 595 | if (!cur_idx) |
| 596 | return 0; |
| 597 | |
| 598 | hdr = &idx->v[cur_idx]; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 599 | if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 600 | /* This was the only value of the header, we must now remove it entirely. */ |
| 601 | delta = buffer_replace2(buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0); |
| 602 | http_msg_move_end(msg, delta); |
| 603 | idx->used--; |
| 604 | hdr->len = 0; /* unused entry */ |
| 605 | idx->v[ctx->prev].next = idx->v[ctx->idx].next; |
Willy Tarreau | 5c4784f | 2011-02-12 13:07:35 +0100 | [diff] [blame] | 606 | if (idx->tail == ctx->idx) |
| 607 | idx->tail = ctx->prev; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 608 | ctx->idx = ctx->prev; /* walk back to the end of previous header */ |
| 609 | ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1; |
| 610 | ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */ |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 611 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 612 | return ctx->idx; |
| 613 | } |
| 614 | |
| 615 | /* This was not the only value of this header. We have to remove between |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 616 | * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the |
| 617 | * last entry of the list, we remove the last separator. |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 618 | */ |
| 619 | |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 620 | skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 621 | delta = buffer_replace2(buf, sol + ctx->del + skip_comma, |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 622 | sol + ctx->val + ctx->vlen + ctx->tws + skip_comma, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 623 | NULL, 0); |
| 624 | hdr->len += delta; |
| 625 | http_msg_move_end(msg, delta); |
| 626 | ctx->val = ctx->del; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 627 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 628 | return ctx->idx; |
| 629 | } |
| 630 | |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 631 | /* This function handles a server error at the stream interface level. The |
| 632 | * stream interface is assumed to be already in a closed state. An optional |
| 633 | * message is copied into the input buffer, and an HTTP status code stored. |
| 634 | * 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] | 635 | * in this buffer will be lost. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 636 | */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 637 | static void http_server_error(struct session *t, struct stream_interface *si, |
| 638 | int err, int finst, int status, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 639 | { |
Willy Tarreau | d5fd51c | 2010-01-22 14:17:47 +0100 | [diff] [blame] | 640 | buffer_auto_read(si->ob); |
| 641 | buffer_abort(si->ob); |
| 642 | buffer_auto_close(si->ob); |
| 643 | buffer_erase(si->ob); |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 644 | buffer_auto_close(si->ib); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 645 | buffer_auto_read(si->ib); |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 646 | if (status > 0 && msg) { |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 647 | t->txn.status = status; |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 648 | buffer_write(si->ib, msg->str, msg->len); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 649 | } |
| 650 | if (!(t->flags & SN_ERR_MASK)) |
| 651 | t->flags |= err; |
| 652 | if (!(t->flags & SN_FINST_MASK)) |
| 653 | t->flags |= finst; |
| 654 | } |
| 655 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 656 | /* This function returns the appropriate error location for the given session |
| 657 | * and message. |
| 658 | */ |
| 659 | |
| 660 | struct chunk *error_message(struct session *s, int msgnum) |
| 661 | { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 662 | if (s->be->errmsg[msgnum].str) |
| 663 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 664 | else if (s->fe->errmsg[msgnum].str) |
| 665 | return &s->fe->errmsg[msgnum]; |
| 666 | else |
| 667 | return &http_err_chunks[msgnum]; |
| 668 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 669 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 670 | /* |
| 671 | * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text |
| 672 | * string), HTTP_METH_OTHER for unknown methods, or the identified method. |
| 673 | */ |
| 674 | static http_meth_t find_http_meth(const char *str, const int len) |
| 675 | { |
| 676 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 677 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 678 | |
| 679 | m = ((unsigned)*str - 'A'); |
| 680 | |
| 681 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 682 | for (h = http_methods[m]; h->len > 0; h++) { |
| 683 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 684 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 685 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 686 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 687 | }; |
| 688 | return HTTP_METH_OTHER; |
| 689 | } |
| 690 | return HTTP_METH_NONE; |
| 691 | |
| 692 | } |
| 693 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 694 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 695 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 696 | * It is returned otherwise. |
| 697 | */ |
| 698 | static char * |
| 699 | http_get_path(struct http_txn *txn) |
| 700 | { |
| 701 | char *ptr, *end; |
| 702 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 703 | ptr = txn->req.sol + txn->req.sl.rq.u; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 704 | end = ptr + txn->req.sl.rq.u_l; |
| 705 | |
| 706 | if (ptr >= end) |
| 707 | return NULL; |
| 708 | |
| 709 | /* RFC2616, par. 5.1.2 : |
| 710 | * Request-URI = "*" | absuri | abspath | authority |
| 711 | */ |
| 712 | |
| 713 | if (*ptr == '*') |
| 714 | return NULL; |
| 715 | |
| 716 | if (isalpha((unsigned char)*ptr)) { |
| 717 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 718 | ptr++; |
| 719 | while (ptr < end && |
| 720 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 721 | ptr++; |
| 722 | /* skip '://' */ |
| 723 | if (ptr == end || *ptr++ != ':') |
| 724 | return NULL; |
| 725 | if (ptr == end || *ptr++ != '/') |
| 726 | return NULL; |
| 727 | if (ptr == end || *ptr++ != '/') |
| 728 | return NULL; |
| 729 | } |
| 730 | /* skip [user[:passwd]@]host[:[port]] */ |
| 731 | |
| 732 | while (ptr < end && *ptr != '/') |
| 733 | ptr++; |
| 734 | |
| 735 | if (ptr == end) |
| 736 | return NULL; |
| 737 | |
| 738 | /* OK, we got the '/' ! */ |
| 739 | return ptr; |
| 740 | } |
| 741 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 742 | /* Returns a 302 for a redirectable request. This may only be called just after |
| 743 | * the stream interface has moved to SI_ST_ASS. Unprocessable requests are |
| 744 | * left unchanged and will follow normal proxy processing. |
| 745 | */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 746 | void perform_http_redirect(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 747 | { |
| 748 | struct http_txn *txn; |
| 749 | struct chunk rdr; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 750 | struct server *srv; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 751 | char *path; |
| 752 | int len; |
| 753 | |
| 754 | /* 1: create the response header */ |
| 755 | rdr.len = strlen(HTTP_302); |
| 756 | rdr.str = trash; |
Willy Tarreau | 59e0b0f | 2010-01-09 21:29:23 +0100 | [diff] [blame] | 757 | rdr.size = sizeof(trash); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 758 | memcpy(rdr.str, HTTP_302, rdr.len); |
| 759 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 760 | srv = target_srv(&s->target); |
| 761 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 762 | /* 2: add the server's prefix */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 763 | if (rdr.len + srv->rdr_len > rdr.size) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 764 | return; |
| 765 | |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 766 | /* special prefix "/" means don't change URL */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 767 | if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') { |
| 768 | memcpy(rdr.str + rdr.len, srv->rdr_pfx, srv->rdr_len); |
| 769 | rdr.len += srv->rdr_len; |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 770 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 771 | |
| 772 | /* 3: add the request URI */ |
| 773 | txn = &s->txn; |
| 774 | path = http_get_path(txn); |
| 775 | if (!path) |
| 776 | return; |
| 777 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 778 | 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] | 779 | if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 780 | return; |
| 781 | |
| 782 | memcpy(rdr.str + rdr.len, path, len); |
| 783 | rdr.len += len; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 784 | |
| 785 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 786 | memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 787 | rdr.len += 29; |
| 788 | } else { |
| 789 | memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23); |
| 790 | rdr.len += 23; |
| 791 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 792 | |
| 793 | /* prepare to return without error. */ |
Willy Tarreau | 99126c3 | 2008-11-27 10:30:51 +0100 | [diff] [blame] | 794 | si->shutr(si); |
| 795 | si->shutw(si); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 796 | si->err_type = SI_ET_NONE; |
| 797 | si->err_loc = NULL; |
| 798 | si->state = SI_ST_CLO; |
| 799 | |
| 800 | /* send the message */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 801 | 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] | 802 | |
| 803 | /* 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] | 804 | if (srv) |
| 805 | srv_inc_sess_ctr(srv); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 806 | } |
| 807 | |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 808 | /* Return the error message corresponding to si->err_type. It is assumed |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 809 | * that the server side is closed. Note that err_type is actually a |
| 810 | * bitmask, where almost only aborts may be cumulated with other |
| 811 | * values. We consider that aborted operations are more important |
| 812 | * than timeouts or errors due to the fact that nobody else in the |
| 813 | * logs might explain incomplete retries. All others should avoid |
| 814 | * being cumulated. It should normally not be possible to have multiple |
| 815 | * aborts at once, but just in case, the first one in sequence is reported. |
| 816 | */ |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 817 | void http_return_srv_error(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 818 | { |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 819 | int err_type = si->err_type; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 820 | |
| 821 | if (err_type & SI_ET_QUEUE_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 822 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q, |
| 823 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 824 | else if (err_type & SI_ET_CONN_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 825 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C, |
| 826 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 827 | else if (err_type & SI_ET_QUEUE_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 828 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q, |
| 829 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 830 | else if (err_type & SI_ET_QUEUE_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 831 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q, |
| 832 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 833 | else if (err_type & SI_ET_CONN_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 834 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C, |
| 835 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 836 | else if (err_type & SI_ET_CONN_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 837 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C, |
| 838 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 839 | else /* SI_ET_CONN_OTHER and others */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 840 | http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C, |
| 841 | 500, error_message(s, HTTP_ERR_500)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 842 | } |
| 843 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 844 | extern const char sess_term_cond[8]; |
| 845 | extern const char sess_fin_state[8]; |
| 846 | extern const char *monthname[12]; |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 847 | const char sess_cookie[8] = "NIDVEO67"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie, Expired cookie, Old cookie, unknown */ |
| 848 | const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive), |
| 849 | Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten, |
| 850 | Set-cookie Updated, unknown, unknown */ |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 851 | struct pool_head *pool2_requri; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 852 | struct pool_head *pool2_capture; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 853 | |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 854 | void http_sess_clflog(struct session *s) |
| 855 | { |
Cyril Bonté | acd7d63 | 2010-11-01 19:26:02 +0100 | [diff] [blame] | 856 | char pn[INET6_ADDRSTRLEN]; |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 857 | struct proxy *fe = s->fe; |
| 858 | struct proxy *be = s->be; |
| 859 | struct proxy *prx_log; |
| 860 | struct http_txn *txn = &s->txn; |
| 861 | int tolog, level, err; |
| 862 | char *uri, *h; |
Willy Tarreau | 71904a4 | 2011-02-13 14:30:26 +0100 | [diff] [blame] | 863 | const char *svid; |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 864 | struct tm tm; |
| 865 | static char tmpline[MAX_SYSLOG_LEN]; |
| 866 | int hdr; |
| 867 | size_t w; |
| 868 | int t_request; |
| 869 | |
| 870 | prx_log = fe; |
| 871 | err = (s->flags & (SN_ERR_MASK | SN_REDISP)) || |
Willy Tarreau | ee28de0 | 2010-06-01 09:51:00 +0200 | [diff] [blame] | 872 | (s->req->cons->conn_retries != be->conn_retries) || |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 873 | txn->status >= 500; |
| 874 | |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 875 | if (addr_to_str(&s->req->prod->addr.from, pn, sizeof(pn)) == AF_UNIX) |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 876 | snprintf(pn, sizeof(pn), "unix:%d", s->listener->luid); |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 877 | |
| 878 | get_gmtime(s->logs.accept_date.tv_sec, &tm); |
| 879 | |
| 880 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
| 881 | tolog = fe->to_log; |
| 882 | |
| 883 | h = tmpline; |
| 884 | |
| 885 | w = snprintf(h, sizeof(tmpline), |
| 886 | "%s - - [%02d/%s/%04d:%02d:%02d:%02d +0000]", |
| 887 | pn, |
| 888 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
| 889 | tm.tm_hour, tm.tm_min, tm.tm_sec); |
| 890 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 891 | goto trunc; |
| 892 | h += w; |
| 893 | |
| 894 | if (h >= tmpline + sizeof(tmpline) - 4) |
| 895 | goto trunc; |
| 896 | |
| 897 | *(h++) = ' '; |
| 898 | *(h++) = '\"'; |
| 899 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
| 900 | h = encode_string(h, tmpline + sizeof(tmpline) - 1, |
| 901 | '#', url_encode_map, uri); |
| 902 | *(h++) = '\"'; |
| 903 | |
| 904 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), " %d %lld", txn->status, s->logs.bytes_out); |
| 905 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 906 | goto trunc; |
| 907 | h += w; |
| 908 | |
| 909 | if (h >= tmpline + sizeof(tmpline) - 9) |
| 910 | goto trunc; |
| 911 | memcpy(h, " \"-\" \"-\"", 8); |
| 912 | h += 8; |
| 913 | |
| 914 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), |
| 915 | " %d %03d", |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 916 | s->req->prod->addr.from.ss_family == AF_UNIX ? s->listener->luid : |
| 917 | get_host_port(&s->req->prod->addr.from), |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 918 | (int)s->logs.accept_date.tv_usec/1000); |
| 919 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 920 | goto trunc; |
| 921 | h += w; |
| 922 | |
| 923 | w = strlen(fe->id); |
| 924 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 925 | goto trunc; |
| 926 | *(h++) = ' '; |
| 927 | *(h++) = '\"'; |
| 928 | memcpy(h, fe->id, w); |
| 929 | h += w; |
| 930 | *(h++) = '\"'; |
| 931 | |
| 932 | w = strlen(be->id); |
| 933 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 934 | goto trunc; |
| 935 | *(h++) = ' '; |
| 936 | *(h++) = '\"'; |
| 937 | memcpy(h, be->id, w); |
| 938 | h += w; |
| 939 | *(h++) = '\"'; |
| 940 | |
Willy Tarreau | 71904a4 | 2011-02-13 14:30:26 +0100 | [diff] [blame] | 941 | if (!(tolog & LW_SVID)) |
| 942 | svid = "-"; |
Willy Tarreau | 7b7a8e9 | 2011-03-27 19:53:06 +0200 | [diff] [blame] | 943 | else switch (s->target.type) { |
Willy Tarreau | 71904a4 | 2011-02-13 14:30:26 +0100 | [diff] [blame] | 944 | case TARG_TYPE_SERVER: |
Willy Tarreau | 7b7a8e9 | 2011-03-27 19:53:06 +0200 | [diff] [blame] | 945 | svid = s->target.ptr.s->id; |
Willy Tarreau | 71904a4 | 2011-02-13 14:30:26 +0100 | [diff] [blame] | 946 | break; |
| 947 | case TARG_TYPE_APPLET: |
Willy Tarreau | 7b7a8e9 | 2011-03-27 19:53:06 +0200 | [diff] [blame] | 948 | svid = s->target.ptr.a->name; |
Willy Tarreau | 71904a4 | 2011-02-13 14:30:26 +0100 | [diff] [blame] | 949 | break; |
| 950 | default: |
| 951 | svid = "<NOSRV>"; |
| 952 | break; |
| 953 | } |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 954 | |
| 955 | w = strlen(svid); |
| 956 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 957 | goto trunc; |
| 958 | *(h++) = ' '; |
| 959 | *(h++) = '\"'; |
| 960 | memcpy(h, svid, w); |
| 961 | h += w; |
| 962 | *(h++) = '\"'; |
| 963 | |
| 964 | t_request = -1; |
| 965 | if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept)) |
| 966 | t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request); |
| 967 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), |
| 968 | " %d %ld %ld %ld %ld", |
| 969 | t_request, |
| 970 | (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1, |
| 971 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, |
| 972 | (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
| 973 | s->logs.t_close); |
| 974 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 975 | goto trunc; |
| 976 | h += w; |
| 977 | |
| 978 | if (h >= tmpline + sizeof(tmpline) - 8) |
| 979 | goto trunc; |
| 980 | *(h++) = ' '; |
| 981 | *(h++) = '\"'; |
| 982 | *(h++) = sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT]; |
| 983 | *(h++) = sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT]; |
| 984 | *(h++) = (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-', |
| 985 | *(h++) = (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-'; |
| 986 | *(h++) = '\"'; |
| 987 | |
| 988 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), |
| 989 | " %d %d %d %d %d %ld %ld", |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 990 | 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] | 991 | (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] | 992 | s->logs.srv_queue_size, s->logs.prx_queue_size); |
| 993 | |
| 994 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 995 | goto trunc; |
| 996 | h += w; |
| 997 | |
| 998 | if (txn->cli_cookie) { |
| 999 | w = strlen(txn->cli_cookie); |
| 1000 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 1001 | goto trunc; |
| 1002 | *(h++) = ' '; |
| 1003 | *(h++) = '\"'; |
| 1004 | memcpy(h, txn->cli_cookie, w); |
| 1005 | h += w; |
| 1006 | *(h++) = '\"'; |
| 1007 | } else { |
| 1008 | if (h >= tmpline + sizeof(tmpline) - 5) |
| 1009 | goto trunc; |
| 1010 | memcpy(h, " \"-\"", 4); |
| 1011 | h += 4; |
| 1012 | } |
| 1013 | |
| 1014 | if (txn->srv_cookie) { |
| 1015 | w = strlen(txn->srv_cookie); |
| 1016 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 1017 | goto trunc; |
| 1018 | *(h++) = ' '; |
| 1019 | *(h++) = '\"'; |
| 1020 | memcpy(h, txn->srv_cookie, w); |
| 1021 | h += w; |
| 1022 | *(h++) = '\"'; |
| 1023 | } else { |
| 1024 | if (h >= tmpline + sizeof(tmpline) - 5) |
| 1025 | goto trunc; |
| 1026 | memcpy(h, " \"-\"", 4); |
| 1027 | h += 4; |
| 1028 | } |
| 1029 | |
| 1030 | if ((fe->to_log & LW_REQHDR) && txn->req.cap) { |
| 1031 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 1032 | if (h >= sizeof (tmpline) + tmpline - 4) |
| 1033 | goto trunc; |
Cyril Bonté | 7f2c539 | 2010-03-13 15:15:07 +0100 | [diff] [blame] | 1034 | if (txn->req.cap[hdr] != NULL) { |
| 1035 | *(h++) = ' '; |
| 1036 | *(h++) = '\"'; |
| 1037 | h = encode_string(h, tmpline + sizeof(tmpline) - 2, |
| 1038 | '#', hdr_encode_map, txn->req.cap[hdr]); |
| 1039 | *(h++) = '\"'; |
| 1040 | } else { |
| 1041 | memcpy(h, " \"-\"", 4); |
| 1042 | h += 4; |
| 1043 | } |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 1044 | } |
| 1045 | } |
| 1046 | |
| 1047 | if ((fe->to_log & LW_RSPHDR) && txn->rsp.cap) { |
| 1048 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 1049 | if (h >= sizeof (tmpline) + tmpline - 4) |
| 1050 | goto trunc; |
Cyril Bonté | 7f2c539 | 2010-03-13 15:15:07 +0100 | [diff] [blame] | 1051 | if (txn->rsp.cap[hdr] != NULL) { |
| 1052 | *(h++) = ' '; |
| 1053 | *(h++) = '\"'; |
| 1054 | h = encode_string(h, tmpline + sizeof(tmpline) - 2, |
| 1055 | '#', hdr_encode_map, txn->rsp.cap[hdr]); |
| 1056 | *(h++) = '\"'; |
| 1057 | } else { |
| 1058 | memcpy(h, " \"-\"", 4); |
| 1059 | h += 4; |
| 1060 | } |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 1061 | } |
| 1062 | } |
| 1063 | |
| 1064 | trunc: |
| 1065 | *h = '\0'; |
| 1066 | |
| 1067 | level = LOG_INFO; |
| 1068 | if (err && (fe->options2 & PR_O2_LOGERRORS)) |
| 1069 | level = LOG_ERR; |
| 1070 | |
| 1071 | send_log(prx_log, level, "%s\n", tmpline); |
| 1072 | |
| 1073 | s->logs.logwait = 0; |
| 1074 | } |
| 1075 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1076 | /* |
| 1077 | * send a log for the session when we have enough info about it. |
| 1078 | * Will not log if the frontend has no log defined. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1079 | */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1080 | void http_sess_log(struct session *s) |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1081 | { |
Cyril Bonté | acd7d63 | 2010-11-01 19:26:02 +0100 | [diff] [blame] | 1082 | char pn[INET6_ADDRSTRLEN]; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1083 | struct proxy *fe = s->fe; |
| 1084 | struct proxy *be = s->be; |
| 1085 | struct proxy *prx_log; |
| 1086 | struct http_txn *txn = &s->txn; |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 1087 | int tolog, level, err; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1088 | char *uri, *h; |
Willy Tarreau | 71904a4 | 2011-02-13 14:30:26 +0100 | [diff] [blame] | 1089 | const char *svid; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 1090 | struct tm tm; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1091 | static char tmpline[MAX_SYSLOG_LEN]; |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1092 | int t_request; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1093 | int hdr; |
| 1094 | |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 1095 | /* if we don't want to log normal traffic, return now */ |
| 1096 | err = (s->flags & (SN_ERR_MASK | SN_REDISP)) || |
Willy Tarreau | ee28de0 | 2010-06-01 09:51:00 +0200 | [diff] [blame] | 1097 | (s->req->cons->conn_retries != be->conn_retries) || |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 1098 | txn->status >= 500; |
| 1099 | if (!err && (fe->options2 & PR_O2_NOLOGNORM)) |
| 1100 | return; |
| 1101 | |
William Lallemand | 0f99e34 | 2011-10-12 17:50:54 +0200 | [diff] [blame] | 1102 | if (LIST_ISEMPTY(&fe->logsrvs)) |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1103 | return; |
| 1104 | prx_log = fe; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1105 | |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 1106 | if (prx_log->options2 & PR_O2_CLFLOG) |
| 1107 | return http_sess_clflog(s); |
| 1108 | |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 1109 | if (addr_to_str(&s->req->prod->addr.from, pn, sizeof(pn)) == AF_UNIX) |
Willy Tarreau | 631f01c | 2011-09-05 00:36:48 +0200 | [diff] [blame] | 1110 | snprintf(pn, sizeof(pn), "unix:%d", s->listener->luid); |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1111 | |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 1112 | get_localtime(s->logs.accept_date.tv_sec, &tm); |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1113 | |
| 1114 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
| 1115 | tolog = fe->to_log; |
| 1116 | |
| 1117 | h = tmpline; |
| 1118 | if (fe->to_log & LW_REQHDR && |
| 1119 | txn->req.cap && |
| 1120 | (h < tmpline + sizeof(tmpline) - 10)) { |
| 1121 | *(h++) = ' '; |
| 1122 | *(h++) = '{'; |
| 1123 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 1124 | if (hdr) |
| 1125 | *(h++) = '|'; |
| 1126 | if (txn->req.cap[hdr] != NULL) |
| 1127 | h = encode_string(h, tmpline + sizeof(tmpline) - 7, |
| 1128 | '#', hdr_encode_map, txn->req.cap[hdr]); |
| 1129 | } |
| 1130 | *(h++) = '}'; |
| 1131 | } |
| 1132 | |
| 1133 | if (fe->to_log & LW_RSPHDR && |
| 1134 | txn->rsp.cap && |
| 1135 | (h < tmpline + sizeof(tmpline) - 7)) { |
| 1136 | *(h++) = ' '; |
| 1137 | *(h++) = '{'; |
| 1138 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 1139 | if (hdr) |
| 1140 | *(h++) = '|'; |
| 1141 | if (txn->rsp.cap[hdr] != NULL) |
| 1142 | h = encode_string(h, tmpline + sizeof(tmpline) - 4, |
| 1143 | '#', hdr_encode_map, txn->rsp.cap[hdr]); |
| 1144 | } |
| 1145 | *(h++) = '}'; |
| 1146 | } |
| 1147 | |
| 1148 | if (h < tmpline + sizeof(tmpline) - 4) { |
| 1149 | *(h++) = ' '; |
| 1150 | *(h++) = '"'; |
| 1151 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
| 1152 | h = encode_string(h, tmpline + sizeof(tmpline) - 1, |
| 1153 | '#', url_encode_map, uri); |
| 1154 | *(h++) = '"'; |
| 1155 | } |
| 1156 | *h = '\0'; |
| 1157 | |
Willy Tarreau | 71904a4 | 2011-02-13 14:30:26 +0100 | [diff] [blame] | 1158 | if (!(tolog & LW_SVID)) |
| 1159 | svid = "-"; |
Willy Tarreau | 7b7a8e9 | 2011-03-27 19:53:06 +0200 | [diff] [blame] | 1160 | else switch (s->target.type) { |
Willy Tarreau | 71904a4 | 2011-02-13 14:30:26 +0100 | [diff] [blame] | 1161 | case TARG_TYPE_SERVER: |
Willy Tarreau | 7b7a8e9 | 2011-03-27 19:53:06 +0200 | [diff] [blame] | 1162 | svid = s->target.ptr.s->id; |
Willy Tarreau | 71904a4 | 2011-02-13 14:30:26 +0100 | [diff] [blame] | 1163 | break; |
| 1164 | case TARG_TYPE_APPLET: |
Willy Tarreau | 7b7a8e9 | 2011-03-27 19:53:06 +0200 | [diff] [blame] | 1165 | svid = s->target.ptr.a->name; |
Willy Tarreau | 71904a4 | 2011-02-13 14:30:26 +0100 | [diff] [blame] | 1166 | break; |
| 1167 | default: |
| 1168 | svid = "<NOSRV>"; |
| 1169 | break; |
| 1170 | } |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1171 | |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1172 | t_request = -1; |
| 1173 | if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept)) |
| 1174 | t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request); |
| 1175 | |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 1176 | level = LOG_INFO; |
| 1177 | if (err && (fe->options2 & PR_O2_LOGERRORS)) |
| 1178 | level = LOG_ERR; |
| 1179 | |
| 1180 | send_log(prx_log, level, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1181 | "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]" |
Willy Tarreau | 1772ece | 2009-04-03 14:49:12 +0200 | [diff] [blame] | 1182 | " %s %s/%s %d/%ld/%ld/%ld/%s%ld %d %s%lld" |
| 1183 | " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %ld/%ld%s\n", |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 1184 | (s->req->prod->addr.from.ss_family == AF_UNIX) ? "unix" : pn, |
| 1185 | (s->req->prod->addr.from.ss_family == AF_UNIX) ? s->listener->luid : |
| 1186 | get_host_port(&s->req->prod->addr.from), |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 1187 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
Willy Tarreau | 1772ece | 2009-04-03 14:49:12 +0200 | [diff] [blame] | 1188 | 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] | 1189 | fe->id, be->id, svid, |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1190 | t_request, |
| 1191 | (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1192 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, |
| 1193 | (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
| 1194 | (tolog & LW_BYTES) ? "" : "+", s->logs.t_close, |
| 1195 | txn->status, |
Willy Tarreau | 8b3977f | 2008-01-18 11:16:32 +0100 | [diff] [blame] | 1196 | (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1197 | txn->cli_cookie ? txn->cli_cookie : "-", |
| 1198 | txn->srv_cookie ? txn->srv_cookie : "-", |
| 1199 | sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT], |
| 1200 | sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT], |
| 1201 | (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-', |
| 1202 | (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] | 1203 | 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] | 1204 | (s->flags & SN_REDISP)?"+":"", |
Willy Tarreau | ee28de0 | 2010-06-01 09:51:00 +0200 | [diff] [blame] | 1205 | (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] | 1206 | s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline); |
| 1207 | |
| 1208 | s->logs.logwait = 0; |
| 1209 | } |
| 1210 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1211 | |
| 1212 | /* |
| 1213 | * Capture headers from message starting at <som> according to header list |
| 1214 | * <cap_hdr>, and fill the <idx> structure appropriately. |
| 1215 | */ |
| 1216 | void capture_headers(char *som, struct hdr_idx *idx, |
| 1217 | char **cap, struct cap_hdr *cap_hdr) |
| 1218 | { |
| 1219 | char *eol, *sol, *col, *sov; |
| 1220 | int cur_idx; |
| 1221 | struct cap_hdr *h; |
| 1222 | int len; |
| 1223 | |
| 1224 | sol = som + hdr_idx_first_pos(idx); |
| 1225 | cur_idx = hdr_idx_first_idx(idx); |
| 1226 | |
| 1227 | while (cur_idx) { |
| 1228 | eol = sol + idx->v[cur_idx].len; |
| 1229 | |
| 1230 | col = sol; |
| 1231 | while (col < eol && *col != ':') |
| 1232 | col++; |
| 1233 | |
| 1234 | sov = col + 1; |
| 1235 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 1236 | sov++; |
| 1237 | |
| 1238 | for (h = cap_hdr; h; h = h->next) { |
| 1239 | if ((h->namelen == col - sol) && |
| 1240 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 1241 | if (cap[h->index] == NULL) |
| 1242 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 1243 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1244 | |
| 1245 | if (cap[h->index] == NULL) { |
| 1246 | Alert("HTTP capture : out of memory.\n"); |
| 1247 | continue; |
| 1248 | } |
| 1249 | |
| 1250 | len = eol - sov; |
| 1251 | if (len > h->len) |
| 1252 | len = h->len; |
| 1253 | |
| 1254 | memcpy(cap[h->index], sov, len); |
| 1255 | cap[h->index][len]=0; |
| 1256 | } |
| 1257 | } |
| 1258 | sol = eol + idx->v[cur_idx].cr + 1; |
| 1259 | cur_idx = idx->v[cur_idx].next; |
| 1260 | } |
| 1261 | } |
| 1262 | |
| 1263 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1264 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 1265 | */ |
| 1266 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 1267 | |
| 1268 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 1269 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 1270 | */ |
| 1271 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 1272 | ptr++; \ |
| 1273 | if (likely(ptr < end)) \ |
| 1274 | goto good; \ |
| 1275 | else { \ |
| 1276 | state = (st); \ |
| 1277 | goto http_msg_ood; \ |
| 1278 | } \ |
| 1279 | } while (0) |
| 1280 | |
| 1281 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1282 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1283 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1284 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 1285 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 1286 | * will give undefined results. |
| 1287 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1288 | * and that msg->sol points to the beginning of the response. |
| 1289 | * If a complete line is found (which implies that at least one CR or LF is |
| 1290 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1291 | * returned indicating an incomplete line (which does not mean that parts have |
| 1292 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1293 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1294 | * upon next call. |
| 1295 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1296 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1297 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1298 | * 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] | 1299 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1300 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1301 | const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf, |
| 1302 | unsigned int state, const char *ptr, const char *end, |
| 1303 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1304 | { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1305 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1306 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1307 | http_msg_rpver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1308 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1309 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 1310 | |
| 1311 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1312 | msg->sl.st.v_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1313 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1314 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1315 | state = HTTP_MSG_ERROR; |
| 1316 | break; |
| 1317 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1318 | case HTTP_MSG_RPVER_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1319 | http_msg_rpver_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1320 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1321 | msg->sl.st.c = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1322 | goto http_msg_rpcode; |
| 1323 | } |
| 1324 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1325 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1326 | /* so it's a CR/LF, this is invalid */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1327 | state = HTTP_MSG_ERROR; |
| 1328 | break; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1329 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1330 | case HTTP_MSG_RPCODE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1331 | http_msg_rpcode: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1332 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1333 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 1334 | |
| 1335 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1336 | 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] | 1337 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1338 | } |
| 1339 | |
| 1340 | /* so it's a CR/LF, so there is no reason phrase */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1341 | 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] | 1342 | http_msg_rsp_reason: |
| 1343 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1344 | msg->sl.st.r = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1345 | msg->sl.st.r_l = 0; |
| 1346 | goto http_msg_rpline_eol; |
| 1347 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1348 | case HTTP_MSG_RPCODE_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1349 | http_msg_rpcode_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1350 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1351 | msg->sl.st.r = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1352 | goto http_msg_rpreason; |
| 1353 | } |
| 1354 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1355 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1356 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1357 | goto http_msg_rsp_reason; |
| 1358 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1359 | case HTTP_MSG_RPREASON: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1360 | http_msg_rpreason: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1361 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1362 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1363 | 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] | 1364 | http_msg_rpline_eol: |
| 1365 | /* We have seen the end of line. Note that we do not |
| 1366 | * necessarily have the \n yet, but at least we know that we |
| 1367 | * have EITHER \r OR \n, otherwise the response would not be |
| 1368 | * complete. We can then record the response length and return |
| 1369 | * to the caller which will be able to register it. |
| 1370 | */ |
| 1371 | msg->sl.st.l = ptr - msg->sol; |
| 1372 | return ptr; |
| 1373 | |
| 1374 | #ifdef DEBUG_FULL |
| 1375 | default: |
| 1376 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1377 | exit(1); |
| 1378 | #endif |
| 1379 | } |
| 1380 | |
| 1381 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1382 | /* out of valid data */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1383 | if (ret_state) |
| 1384 | *ret_state = state; |
| 1385 | if (ret_ptr) |
| 1386 | *ret_ptr = (char *)ptr; |
| 1387 | return NULL; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1388 | } |
| 1389 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1390 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1391 | * This function parses a request line between <ptr> and <end>, starting with |
| 1392 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1393 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1394 | * will give undefined results. |
| 1395 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1396 | * and that msg->sol points to the beginning of the request. |
| 1397 | * If a complete line is found (which implies that at least one CR or LF is |
| 1398 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1399 | * returned indicating an incomplete line (which does not mean that parts have |
| 1400 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1401 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1402 | * upon next call. |
| 1403 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1404 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1405 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1406 | * 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] | 1407 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1408 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1409 | const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf, |
| 1410 | unsigned int state, const char *ptr, const char *end, |
| 1411 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1412 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1413 | switch (state) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1414 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1415 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1416 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1417 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1418 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1419 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1420 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1421 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1422 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1423 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1424 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1425 | /* HTTP 0.9 request */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1426 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1427 | http_msg_req09_uri: |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1428 | msg->sl.rq.u = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1429 | http_msg_req09_uri_e: |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1430 | 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] | 1431 | http_msg_req09_ver: |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1432 | msg->sl.rq.v = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1433 | msg->sl.rq.v_l = 0; |
| 1434 | goto http_msg_rqline_eol; |
| 1435 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1436 | state = HTTP_MSG_ERROR; |
| 1437 | break; |
| 1438 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1439 | case HTTP_MSG_RQMETH_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1440 | http_msg_rqmeth_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1441 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1442 | msg->sl.rq.u = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1443 | goto http_msg_rquri; |
| 1444 | } |
| 1445 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1446 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1447 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1448 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1449 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1450 | case HTTP_MSG_RQURI: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1451 | http_msg_rquri: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1452 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1453 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1454 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1455 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1456 | 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] | 1457 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1458 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1459 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1460 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1461 | goto http_msg_req09_uri_e; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1462 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1463 | case HTTP_MSG_RQURI_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1464 | http_msg_rquri_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1465 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1466 | msg->sl.rq.v = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1467 | goto http_msg_rqver; |
| 1468 | } |
| 1469 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1470 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1471 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1472 | goto http_msg_req09_ver; |
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_RQVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1475 | http_msg_rqver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1476 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1477 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1478 | |
| 1479 | if (likely(HTTP_IS_CRLF(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1480 | 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] | 1481 | http_msg_rqline_eol: |
| 1482 | /* We have seen the end of line. Note that we do not |
| 1483 | * necessarily have the \n yet, but at least we know that we |
| 1484 | * have EITHER \r OR \n, otherwise the request would not be |
| 1485 | * complete. We can then record the request length and return |
| 1486 | * to the caller which will be able to register it. |
| 1487 | */ |
| 1488 | msg->sl.rq.l = ptr - msg->sol; |
| 1489 | return ptr; |
| 1490 | } |
| 1491 | |
| 1492 | /* neither an HTTP_VER token nor a CRLF */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1493 | state = HTTP_MSG_ERROR; |
| 1494 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1495 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1496 | #ifdef DEBUG_FULL |
| 1497 | default: |
| 1498 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1499 | exit(1); |
| 1500 | #endif |
| 1501 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1502 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1503 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1504 | /* out of valid data */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1505 | if (ret_state) |
| 1506 | *ret_state = state; |
| 1507 | if (ret_ptr) |
| 1508 | *ret_ptr = (char *)ptr; |
| 1509 | return NULL; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1510 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1511 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1512 | /* |
| 1513 | * Returns the data from Authorization header. Function may be called more |
| 1514 | * than once so data is stored in txn->auth_data. When no header is found |
| 1515 | * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid |
| 1516 | * searching again for something we are unable to find anyway. |
| 1517 | */ |
| 1518 | |
| 1519 | char get_http_auth_buff[BUFSIZE]; |
| 1520 | |
| 1521 | int |
| 1522 | get_http_auth(struct session *s) |
| 1523 | { |
| 1524 | |
| 1525 | struct http_txn *txn = &s->txn; |
| 1526 | struct chunk auth_method; |
| 1527 | struct hdr_ctx ctx; |
| 1528 | char *h, *p; |
| 1529 | int len; |
| 1530 | |
| 1531 | #ifdef DEBUG_AUTH |
| 1532 | printf("Auth for session %p: %d\n", s, txn->auth.method); |
| 1533 | #endif |
| 1534 | |
| 1535 | if (txn->auth.method == HTTP_AUTH_WRONG) |
| 1536 | return 0; |
| 1537 | |
| 1538 | if (txn->auth.method) |
| 1539 | return 1; |
| 1540 | |
| 1541 | txn->auth.method = HTTP_AUTH_WRONG; |
| 1542 | |
| 1543 | ctx.idx = 0; |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 1544 | |
| 1545 | if (txn->flags & TX_USE_PX_CONN) { |
| 1546 | h = "Proxy-Authorization"; |
| 1547 | len = strlen(h); |
| 1548 | } else { |
| 1549 | h = "Authorization"; |
| 1550 | len = strlen(h); |
| 1551 | } |
| 1552 | |
| 1553 | 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] | 1554 | return 0; |
| 1555 | |
| 1556 | h = ctx.line + ctx.val; |
| 1557 | |
| 1558 | p = memchr(h, ' ', ctx.vlen); |
| 1559 | if (!p || p == h) |
| 1560 | return 0; |
| 1561 | |
| 1562 | chunk_initlen(&auth_method, h, 0, p-h); |
| 1563 | chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1); |
| 1564 | |
| 1565 | if (!strncasecmp("Basic", auth_method.str, auth_method.len)) { |
| 1566 | |
| 1567 | len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len, |
| 1568 | get_http_auth_buff, BUFSIZE - 1); |
| 1569 | |
| 1570 | if (len < 0) |
| 1571 | return 0; |
| 1572 | |
| 1573 | |
| 1574 | get_http_auth_buff[len] = '\0'; |
| 1575 | |
| 1576 | p = strchr(get_http_auth_buff, ':'); |
| 1577 | |
| 1578 | if (!p) |
| 1579 | return 0; |
| 1580 | |
| 1581 | txn->auth.user = get_http_auth_buff; |
| 1582 | *p = '\0'; |
| 1583 | txn->auth.pass = p+1; |
| 1584 | |
| 1585 | txn->auth.method = HTTP_AUTH_BASIC; |
| 1586 | return 1; |
| 1587 | } |
| 1588 | |
| 1589 | return 0; |
| 1590 | } |
| 1591 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1592 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1593 | /* |
| 1594 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1595 | * depending on the initial msg->msg_state. It can be preempted everywhere |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1596 | * when data are missing and recalled at the exact same location with no |
| 1597 | * information loss. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1598 | * 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] | 1599 | * fields. Note that msg->som and msg->sol will be initialized after completing |
| 1600 | * the first state, so that none of the msg pointers has to be initialized |
| 1601 | * prior to the first call. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1602 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1603 | void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx) |
| 1604 | { |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1605 | unsigned int state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1606 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1607 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1608 | state = msg->msg_state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1609 | ptr = buf->lr; |
| 1610 | end = buf->r; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1611 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1612 | if (unlikely(ptr >= end)) |
| 1613 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1614 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1615 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1616 | /* |
| 1617 | * First, states that are specific to the response only. |
| 1618 | * We check them first so that request and headers are |
| 1619 | * closer to each other (accessed more often). |
| 1620 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1621 | case HTTP_MSG_RPBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1622 | http_msg_rpbefore: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1623 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1624 | /* we have a start of message, but we have to check |
| 1625 | * first if we need to remove some CRLF. We can only |
| 1626 | * do this when send_max=0. |
| 1627 | */ |
| 1628 | char *beg = buf->w + buf->send_max; |
| 1629 | if (beg >= buf->data + buf->size) |
| 1630 | beg -= buf->size; |
| 1631 | if (unlikely(ptr != beg)) { |
| 1632 | if (buf->send_max) |
| 1633 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1634 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1635 | buffer_ignore(buf, ptr - beg); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1636 | } |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1637 | msg->som = ptr - buf->data; |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 1638 | msg->sol = ptr; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1639 | hdr_idx_init(idx); |
| 1640 | state = HTTP_MSG_RPVER; |
| 1641 | goto http_msg_rpver; |
| 1642 | } |
| 1643 | |
| 1644 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1645 | goto http_msg_invalid; |
| 1646 | |
| 1647 | if (unlikely(*ptr == '\n')) |
| 1648 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1649 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1650 | /* stop here */ |
| 1651 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1652 | case HTTP_MSG_RPBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1653 | http_msg_rpbefore_cr: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1654 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1655 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1656 | /* stop here */ |
| 1657 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1658 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1659 | http_msg_rpver: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1660 | case HTTP_MSG_RPVER_SP: |
| 1661 | case HTTP_MSG_RPCODE: |
| 1662 | case HTTP_MSG_RPCODE_SP: |
| 1663 | case HTTP_MSG_RPREASON: |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1664 | ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1665 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1666 | if (unlikely(!ptr)) |
| 1667 | return; |
| 1668 | |
| 1669 | /* we have a full response and we know that we have either a CR |
| 1670 | * or an LF at <ptr>. |
| 1671 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1672 | //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] | 1673 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1674 | |
| 1675 | msg->sol = ptr; |
| 1676 | if (likely(*ptr == '\r')) |
| 1677 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1678 | goto http_msg_rpline_end; |
| 1679 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1680 | case HTTP_MSG_RPLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1681 | http_msg_rpline_end: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1682 | /* msg->sol must point to the first of CR or LF. */ |
| 1683 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1684 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1685 | /* stop here */ |
| 1686 | |
| 1687 | /* |
| 1688 | * Second, states that are specific to the request only |
| 1689 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1690 | case HTTP_MSG_RQBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1691 | http_msg_rqbefore: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1692 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1693 | /* we have a start of message, but we have to check |
| 1694 | * first if we need to remove some CRLF. We can only |
| 1695 | * do this when send_max=0. |
| 1696 | */ |
| 1697 | char *beg = buf->w + buf->send_max; |
| 1698 | if (beg >= buf->data + buf->size) |
| 1699 | beg -= buf->size; |
| 1700 | if (likely(ptr != beg)) { |
| 1701 | if (buf->send_max) |
| 1702 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1703 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1704 | buffer_ignore(buf, ptr - beg); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1705 | } |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1706 | msg->som = ptr - buf->data; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1707 | msg->sol = ptr; |
Willy Tarreau | f0d058e | 2007-01-25 12:03:42 +0100 | [diff] [blame] | 1708 | /* we will need this when keep-alive will be supported |
| 1709 | hdr_idx_init(idx); |
| 1710 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1711 | state = HTTP_MSG_RQMETH; |
| 1712 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1713 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1714 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1715 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1716 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1717 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1718 | if (unlikely(*ptr == '\n')) |
| 1719 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1720 | 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] | 1721 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1722 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1723 | case HTTP_MSG_RQBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1724 | http_msg_rqbefore_cr: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1725 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1726 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1727 | /* stop here */ |
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 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1730 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1731 | case HTTP_MSG_RQMETH_SP: |
| 1732 | case HTTP_MSG_RQURI: |
| 1733 | case HTTP_MSG_RQURI_SP: |
| 1734 | case HTTP_MSG_RQVER: |
| 1735 | ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1736 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1737 | if (unlikely(!ptr)) |
| 1738 | return; |
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 | /* we have a full request and we know that we have either a CR |
| 1741 | * or an LF at <ptr>. |
| 1742 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1743 | //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] | 1744 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1745 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1746 | msg->sol = ptr; |
| 1747 | if (likely(*ptr == '\r')) |
| 1748 | 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] | 1749 | goto http_msg_rqline_end; |
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 | case HTTP_MSG_RQLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1752 | http_msg_rqline_end: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1753 | /* check for HTTP/0.9 request : no version information available. |
| 1754 | * msg->sol must point to the first of CR or LF. |
| 1755 | */ |
| 1756 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1757 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1758 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1759 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1760 | 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] | 1761 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1762 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1763 | /* |
| 1764 | * Common states below |
| 1765 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1766 | case HTTP_MSG_HDR_FIRST: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1767 | http_msg_hdr_first: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1768 | msg->sol = ptr; |
| 1769 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1770 | goto http_msg_hdr_name; |
| 1771 | } |
| 1772 | |
| 1773 | if (likely(*ptr == '\r')) |
| 1774 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1775 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1776 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1777 | case HTTP_MSG_HDR_NAME: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1778 | http_msg_hdr_name: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1779 | /* assumes msg->sol points to the first char */ |
| 1780 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1781 | 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] | 1782 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1783 | if (likely(*ptr == ':')) { |
| 1784 | msg->col = ptr - buf->data; |
| 1785 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP); |
| 1786 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1787 | |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1788 | if (likely(msg->err_pos < -1) || *ptr == '\n') |
| 1789 | goto http_msg_invalid; |
| 1790 | |
| 1791 | if (msg->err_pos == -1) /* capture error pointer */ |
| 1792 | msg->err_pos = ptr - buf->data; /* >= 0 now */ |
| 1793 | |
| 1794 | /* and we still accept this non-token character */ |
| 1795 | 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] | 1796 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1797 | case HTTP_MSG_HDR_L1_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1798 | http_msg_hdr_l1_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1799 | /* assumes msg->sol points to the first char and msg->col to the colon */ |
| 1800 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1801 | 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] | 1802 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1803 | /* header value can be basically anything except CR/LF */ |
| 1804 | msg->sov = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1805 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1806 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1807 | goto http_msg_hdr_val; |
| 1808 | } |
| 1809 | |
| 1810 | if (likely(*ptr == '\r')) |
| 1811 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1812 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1813 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1814 | case HTTP_MSG_HDR_L1_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1815 | http_msg_hdr_l1_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1816 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1817 | 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] | 1818 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1819 | case HTTP_MSG_HDR_L1_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1820 | http_msg_hdr_l1_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1821 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1822 | /* replace HT,CR,LF with spaces */ |
| 1823 | for (; buf->data+msg->sov < ptr; msg->sov++) |
| 1824 | buf->data[msg->sov] = ' '; |
| 1825 | goto http_msg_hdr_l1_sp; |
| 1826 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1827 | /* we had a header consisting only in spaces ! */ |
| 1828 | msg->eol = buf->data + msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1829 | goto http_msg_complete_header; |
| 1830 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1831 | case HTTP_MSG_HDR_VAL: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1832 | http_msg_hdr_val: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1833 | /* assumes msg->sol points to the first char, msg->col to the |
| 1834 | * colon, and msg->sov points to the first character of the |
| 1835 | * value. |
| 1836 | */ |
| 1837 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1838 | 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] | 1839 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1840 | msg->eol = ptr; |
| 1841 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1842 | * real header end in case it ends with lots of LWS, but is this |
| 1843 | * really needed ? |
| 1844 | */ |
| 1845 | if (likely(*ptr == '\r')) |
| 1846 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1847 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1848 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1849 | case HTTP_MSG_HDR_L2_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1850 | http_msg_hdr_l2_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1851 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1852 | 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] | 1853 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1854 | case HTTP_MSG_HDR_L2_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1855 | http_msg_hdr_l2_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1856 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1857 | /* LWS: replace HT,CR,LF with spaces */ |
| 1858 | for (; msg->eol < ptr; msg->eol++) |
| 1859 | *msg->eol = ' '; |
| 1860 | goto http_msg_hdr_val; |
| 1861 | } |
| 1862 | http_msg_complete_header: |
| 1863 | /* |
| 1864 | * It was a new header, so the last one is finished. |
| 1865 | * Assumes msg->sol points to the first char, msg->col to the |
| 1866 | * colon, msg->sov points to the first character of the value |
| 1867 | * and msg->eol to the first CR or LF so we know how the line |
| 1868 | * ends. We insert last header into the index. |
| 1869 | */ |
| 1870 | /* |
| 1871 | fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol); |
| 1872 | write(2, msg->sol, msg->eol-msg->sol); |
| 1873 | fprintf(stderr,"\n"); |
| 1874 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1875 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1876 | if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r', |
| 1877 | idx, idx->tail) < 0)) |
| 1878 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1879 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1880 | msg->sol = ptr; |
| 1881 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1882 | goto http_msg_hdr_name; |
| 1883 | } |
| 1884 | |
| 1885 | if (likely(*ptr == '\r')) |
| 1886 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1887 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1888 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1889 | case HTTP_MSG_LAST_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1890 | http_msg_last_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1891 | /* Assumes msg->sol points to the first of either CR or LF */ |
| 1892 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1893 | ptr++; |
| 1894 | buf->lr = ptr; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1895 | msg->col = msg->sov = buf->lr - buf->data; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1896 | msg->eoh = msg->sol - buf->data; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1897 | msg->sol = buf->data + msg->som; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1898 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1899 | return; |
| 1900 | #ifdef DEBUG_FULL |
| 1901 | default: |
| 1902 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1903 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1904 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1905 | } |
| 1906 | http_msg_ood: |
| 1907 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1908 | msg->msg_state = state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1909 | buf->lr = ptr; |
| 1910 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1911 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1912 | http_msg_invalid: |
| 1913 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1914 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1915 | buf->lr = ptr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1916 | return; |
| 1917 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1918 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1919 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 1920 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 1921 | * nothing is done and 1 is returned. |
| 1922 | */ |
| 1923 | static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn) |
| 1924 | { |
| 1925 | int delta; |
| 1926 | char *cur_end; |
| 1927 | |
| 1928 | if (msg->sl.rq.v_l != 0) |
| 1929 | return 1; |
| 1930 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1931 | cur_end = msg->sol + msg->sl.rq.l; |
| 1932 | delta = 0; |
| 1933 | |
| 1934 | if (msg->sl.rq.u_l == 0) { |
| 1935 | /* if no URI was set, add "/" */ |
| 1936 | delta = buffer_replace2(req, cur_end, cur_end, " /", 2); |
| 1937 | cur_end += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1938 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1939 | } |
| 1940 | /* add HTTP version */ |
| 1941 | 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] | 1942 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1943 | cur_end += delta; |
| 1944 | cur_end = (char *)http_parse_reqline(msg, req->data, |
| 1945 | HTTP_MSG_RQMETH, |
| 1946 | msg->sol, cur_end + 1, |
| 1947 | NULL, NULL); |
| 1948 | if (unlikely(!cur_end)) |
| 1949 | return 0; |
| 1950 | |
| 1951 | /* we have a full HTTP/1.0 request now and we know that |
| 1952 | * we have either a CR or an LF at <ptr>. |
| 1953 | */ |
| 1954 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 1955 | return 1; |
| 1956 | } |
| 1957 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1958 | /* Parse the Connection: header of an HTTP request, looking for both "close" |
| 1959 | * and "keep-alive" values. If a buffer is provided and we already know that |
| 1960 | * some headers may safely be removed, we remove them now. The <to_del> flags |
| 1961 | * are used for that : |
| 1962 | * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses) |
| 1963 | * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1). |
| 1964 | * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was |
| 1965 | * found, and TX_CON_*_SET is adjusted depending on what is left so only |
| 1966 | * harmless combinations may be removed. Do not call that after changes have |
| 1967 | * been processed. If unused, the buffer can be NULL, and no data will be |
| 1968 | * changed. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1969 | */ |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1970 | 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] | 1971 | { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1972 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1973 | const char *hdr_val = "Connection"; |
| 1974 | int hdr_len = 10; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1975 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1976 | if (txn->flags & TX_HDR_CONN_PRS) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1977 | return; |
| 1978 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1979 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1980 | hdr_val = "Proxy-Connection"; |
| 1981 | hdr_len = 16; |
| 1982 | } |
| 1983 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1984 | ctx.idx = 0; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1985 | txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET); |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1986 | 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] | 1987 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1988 | txn->flags |= TX_HDR_CONN_KAL; |
| 1989 | if ((to_del & 2) && buf) |
| 1990 | http_remove_header2(msg, buf, &txn->hdr_idx, &ctx); |
| 1991 | else |
| 1992 | txn->flags |= TX_CON_KAL_SET; |
| 1993 | } |
| 1994 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1995 | txn->flags |= TX_HDR_CONN_CLO; |
| 1996 | if ((to_del & 1) && buf) |
| 1997 | http_remove_header2(msg, buf, &txn->hdr_idx, &ctx); |
| 1998 | else |
| 1999 | txn->flags |= TX_CON_CLO_SET; |
| 2000 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2001 | } |
| 2002 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2003 | txn->flags |= TX_HDR_CONN_PRS; |
| 2004 | return; |
| 2005 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2006 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2007 | /* Apply desired changes on the Connection: header. Values may be removed and/or |
| 2008 | * added depending on the <wanted> flags, which are exclusively composed of |
| 2009 | * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The |
| 2010 | * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left. |
| 2011 | */ |
| 2012 | void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, int wanted) |
| 2013 | { |
| 2014 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2015 | const char *hdr_val = "Connection"; |
| 2016 | int hdr_len = 10; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2017 | |
| 2018 | ctx.idx = 0; |
| 2019 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2020 | |
| 2021 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 2022 | hdr_val = "Proxy-Connection"; |
| 2023 | hdr_len = 16; |
| 2024 | } |
| 2025 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2026 | txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET); |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2027 | 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] | 2028 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 2029 | if (wanted & TX_CON_KAL_SET) |
| 2030 | txn->flags |= TX_CON_KAL_SET; |
| 2031 | else |
| 2032 | http_remove_header2(msg, buf, &txn->hdr_idx, &ctx); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2033 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2034 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 2035 | if (wanted & TX_CON_CLO_SET) |
| 2036 | txn->flags |= TX_CON_CLO_SET; |
| 2037 | else |
| 2038 | http_remove_header2(msg, buf, &txn->hdr_idx, &ctx); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2039 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2040 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2041 | |
| 2042 | if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 2043 | return; |
| 2044 | |
| 2045 | if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) { |
| 2046 | txn->flags |= TX_CON_CLO_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2047 | hdr_val = "Connection: close"; |
| 2048 | hdr_len = 17; |
| 2049 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 2050 | hdr_val = "Proxy-Connection: close"; |
| 2051 | hdr_len = 23; |
| 2052 | } |
| 2053 | 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] | 2054 | } |
| 2055 | |
| 2056 | if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) { |
| 2057 | txn->flags |= TX_CON_KAL_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2058 | hdr_val = "Connection: keep-alive"; |
| 2059 | hdr_len = 22; |
| 2060 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 2061 | hdr_val = "Proxy-Connection: keep-alive"; |
| 2062 | hdr_len = 28; |
| 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 | return; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2067 | } |
| 2068 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2069 | /* Parse the chunk size at buf->lr. Once done, it adjusts ->lr to point to the |
| 2070 | * first byte of body, and increments msg->sov by the number of bytes parsed, |
| 2071 | * so that we know we can forward between ->som and ->sov. Note that due to |
| 2072 | * possible wrapping at the end of the buffer, it is possible that msg->sov is |
| 2073 | * lower than msg->som. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2074 | * 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] | 2075 | * 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] | 2076 | */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2077 | int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2078 | { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2079 | char *ptr = buf->lr; |
| 2080 | char *end = buf->data + buf->size; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2081 | unsigned int chunk = 0; |
| 2082 | |
| 2083 | /* The chunk size is in the following form, though we are only |
| 2084 | * interested in the size and CRLF : |
| 2085 | * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF |
| 2086 | */ |
| 2087 | while (1) { |
| 2088 | int c; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2089 | if (ptr == buf->r) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2090 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2091 | c = hex2i(*ptr); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2092 | if (c < 0) /* not a hex digit anymore */ |
| 2093 | break; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2094 | if (++ptr >= end) |
| 2095 | ptr = buf->data; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2096 | if (chunk & 0xF000000) /* overflow will occur */ |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2097 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2098 | chunk = (chunk << 4) + c; |
| 2099 | } |
| 2100 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2101 | /* empty size not allowed */ |
| 2102 | if (ptr == buf->lr) |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2103 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2104 | |
| 2105 | while (http_is_spht[(unsigned char)*ptr]) { |
| 2106 | if (++ptr >= end) |
| 2107 | ptr = buf->data; |
| 2108 | if (ptr == buf->r) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2109 | return 0; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2110 | } |
| 2111 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2112 | /* Up to there, we know that at least one byte is present at *ptr. Check |
| 2113 | * for the end of chunk size. |
| 2114 | */ |
| 2115 | while (1) { |
| 2116 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 2117 | /* we now have a CR or an LF at ptr */ |
| 2118 | if (likely(*ptr == '\r')) { |
| 2119 | if (++ptr >= end) |
| 2120 | ptr = buf->data; |
| 2121 | if (ptr == buf->r) |
| 2122 | return 0; |
| 2123 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2124 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2125 | if (*ptr != '\n') |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2126 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2127 | if (++ptr >= end) |
| 2128 | ptr = buf->data; |
| 2129 | /* done */ |
| 2130 | break; |
| 2131 | } |
| 2132 | else if (*ptr == ';') { |
| 2133 | /* chunk extension, ends at next CRLF */ |
| 2134 | if (++ptr >= end) |
| 2135 | ptr = buf->data; |
| 2136 | if (ptr == buf->r) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2137 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2138 | |
| 2139 | while (!HTTP_IS_CRLF(*ptr)) { |
| 2140 | if (++ptr >= end) |
| 2141 | ptr = buf->data; |
| 2142 | if (ptr == buf->r) |
| 2143 | return 0; |
| 2144 | } |
| 2145 | /* we have a CRLF now, loop above */ |
| 2146 | continue; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2147 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2148 | else |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2149 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2150 | } |
| 2151 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2152 | /* OK we found our CRLF and now <ptr> points to the next byte, |
| 2153 | * which may or may not be present. We save that into ->lr and |
| 2154 | * ->sov. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2155 | */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2156 | msg->sov += ptr - buf->lr; |
| 2157 | buf->lr = ptr; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2158 | msg->chunk_len = chunk; |
| 2159 | msg->body_len += chunk; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2160 | msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2161 | return 1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2162 | error: |
| 2163 | msg->err_pos = ptr - buf->data; |
| 2164 | return -1; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2165 | } |
| 2166 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2167 | /* This function skips trailers in the buffer <buf> associated with HTTP |
| 2168 | * message <msg>. The first visited position is buf->lr. If the end of |
| 2169 | * the trailers is found, it is automatically scheduled to be forwarded, |
| 2170 | * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0. |
| 2171 | * If not enough data are available, the function does not change anything |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2172 | * except maybe buf->lr and msg->sov if it could parse some lines, and returns |
| 2173 | * zero. If a parse error is encountered, the function returns < 0 and does not |
| 2174 | * change anything except maybe buf->lr and msg->sov. Note that the message |
| 2175 | * must already be in HTTP_MSG_TRAILERS state before calling this function, |
| 2176 | * which implies that all non-trailers data have already been scheduled for |
| 2177 | * forwarding, and that the difference between msg->som and msg->sov exactly |
| 2178 | * matches the length of trailers already parsed and not forwarded. It is also |
| 2179 | * important to note that this function is designed to be able to parse wrapped |
| 2180 | * headers at end of buffer. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2181 | */ |
| 2182 | int http_forward_trailers(struct buffer *buf, struct http_msg *msg) |
| 2183 | { |
| 2184 | /* we have buf->lr which points to next line. Look for CRLF. */ |
| 2185 | while (1) { |
| 2186 | char *p1 = NULL, *p2 = NULL; |
| 2187 | char *ptr = buf->lr; |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2188 | int bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2189 | |
| 2190 | /* scan current line and stop at LF or CRLF */ |
| 2191 | while (1) { |
| 2192 | if (ptr == buf->r) |
| 2193 | return 0; |
| 2194 | |
| 2195 | if (*ptr == '\n') { |
| 2196 | if (!p1) |
| 2197 | p1 = ptr; |
| 2198 | p2 = ptr; |
| 2199 | break; |
| 2200 | } |
| 2201 | |
| 2202 | if (*ptr == '\r') { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2203 | if (p1) { |
| 2204 | msg->err_pos = ptr - buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2205 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2206 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2207 | p1 = ptr; |
| 2208 | } |
| 2209 | |
| 2210 | ptr++; |
| 2211 | if (ptr >= buf->data + buf->size) |
| 2212 | ptr = buf->data; |
| 2213 | } |
| 2214 | |
| 2215 | /* after LF; point to beginning of next line */ |
| 2216 | p2++; |
| 2217 | if (p2 >= buf->data + buf->size) |
| 2218 | p2 = buf->data; |
| 2219 | |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2220 | bytes = p2 - buf->lr; |
| 2221 | if (bytes < 0) |
| 2222 | bytes += buf->size; |
| 2223 | |
| 2224 | /* schedule this line for forwarding */ |
| 2225 | msg->sov += bytes; |
| 2226 | if (msg->sov >= buf->size) |
| 2227 | msg->sov -= buf->size; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2228 | |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2229 | if (p1 == buf->lr) { |
| 2230 | /* LF/CRLF at beginning of line => end of trailers at p2. |
| 2231 | * Everything was scheduled for forwarding, there's nothing |
| 2232 | * left from this message. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 2233 | */ |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2234 | buf->lr = p2; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2235 | msg->msg_state = HTTP_MSG_DONE; |
| 2236 | return 1; |
| 2237 | } |
| 2238 | /* OK, next line then */ |
| 2239 | buf->lr = p2; |
| 2240 | } |
| 2241 | } |
| 2242 | |
| 2243 | /* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or |
| 2244 | * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov, |
| 2245 | * ->som, buf->lr in order to include this part into the next forwarding phase. |
| 2246 | * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If |
| 2247 | * not enough data are available, the function does not change anything and |
| 2248 | * returns zero. If a parse error is encountered, the function returns < 0 and |
| 2249 | * does not change anything. Note: this function is designed to parse wrapped |
| 2250 | * CRLF at the end of the buffer. |
| 2251 | */ |
| 2252 | int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg) |
| 2253 | { |
| 2254 | char *ptr; |
| 2255 | int bytes; |
| 2256 | |
| 2257 | /* NB: we'll check data availabilty at the end. It's not a |
| 2258 | * problem because whatever we match first will be checked |
| 2259 | * against the correct length. |
| 2260 | */ |
| 2261 | bytes = 1; |
| 2262 | ptr = buf->lr; |
| 2263 | if (*ptr == '\r') { |
| 2264 | bytes++; |
| 2265 | ptr++; |
| 2266 | if (ptr >= buf->data + buf->size) |
| 2267 | ptr = buf->data; |
| 2268 | } |
| 2269 | |
Willy Tarreau | bf3f1de | 2010-03-17 15:54:24 +0100 | [diff] [blame] | 2270 | if (bytes > buf->l - buf->send_max) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2271 | return 0; |
| 2272 | |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2273 | if (*ptr != '\n') { |
| 2274 | msg->err_pos = ptr - buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2275 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2276 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2277 | |
| 2278 | ptr++; |
| 2279 | if (ptr >= buf->data + buf->size) |
| 2280 | ptr = buf->data; |
| 2281 | buf->lr = ptr; |
| 2282 | /* prepare the CRLF to be forwarded. msg->som may be before data but we don't care */ |
| 2283 | msg->sov = ptr - buf->data; |
| 2284 | msg->som = msg->sov - bytes; |
| 2285 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 2286 | return 1; |
| 2287 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2288 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2289 | void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg) |
| 2290 | { |
| 2291 | char *end = buf->data + buf->size; |
| 2292 | int off = buf->data + buf->size - buf->w; |
| 2293 | |
| 2294 | /* two possible cases : |
| 2295 | * - the buffer is in one contiguous block, we move it in-place |
Willy Tarreau | 8096de9 | 2010-02-26 11:12:27 +0100 | [diff] [blame] | 2296 | * - 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] | 2297 | */ |
| 2298 | if (buf->l) { |
Willy Tarreau | 8096de9 | 2010-02-26 11:12:27 +0100 | [diff] [blame] | 2299 | int block1 = buf->l; |
| 2300 | int block2 = 0; |
| 2301 | if (buf->r <= buf->w) { |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2302 | /* non-contiguous block */ |
Willy Tarreau | 8096de9 | 2010-02-26 11:12:27 +0100 | [diff] [blame] | 2303 | block1 = buf->data + buf->size - buf->w; |
| 2304 | block2 = buf->r - buf->data; |
| 2305 | } |
| 2306 | if (block2) |
| 2307 | memcpy(swap_buffer, buf->data, block2); |
| 2308 | memmove(buf->data, buf->w, block1); |
| 2309 | if (block2) |
| 2310 | memcpy(buf->data + block1, swap_buffer, block2); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2311 | } |
| 2312 | |
| 2313 | /* adjust all known pointers */ |
| 2314 | buf->w = buf->data; |
| 2315 | buf->lr += off; if (buf->lr >= end) buf->lr -= buf->size; |
| 2316 | buf->r += off; if (buf->r >= end) buf->r -= buf->size; |
| 2317 | msg->sol += off; if (msg->sol >= end) msg->sol -= buf->size; |
| 2318 | msg->eol += off; if (msg->eol >= end) msg->eol -= buf->size; |
| 2319 | |
| 2320 | /* adjust relative pointers */ |
| 2321 | msg->som = 0; |
| 2322 | msg->eoh += off; if (msg->eoh >= buf->size) msg->eoh -= buf->size; |
| 2323 | msg->col += off; if (msg->col >= buf->size) msg->col -= buf->size; |
| 2324 | msg->sov += off; if (msg->sov >= buf->size) msg->sov -= buf->size; |
| 2325 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2326 | if (msg->err_pos >= 0) { |
| 2327 | msg->err_pos += off; |
| 2328 | if (msg->err_pos >= buf->size) |
| 2329 | msg->err_pos -= buf->size; |
| 2330 | } |
| 2331 | |
| 2332 | buf->flags &= ~BF_FULL; |
| 2333 | if (buf->l >= buffer_max_len(buf)) |
| 2334 | buf->flags |= BF_FULL; |
| 2335 | } |
| 2336 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2337 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 2338 | * processing can continue on next analysers, or zero if it either needs more |
| 2339 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
| 2340 | * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers |
| 2341 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 2342 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2343 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2344 | 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] | 2345 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2346 | /* |
| 2347 | * We will parse the partial (or complete) lines. |
| 2348 | * We will check the request syntax, and also join multi-line |
| 2349 | * headers. An index of all the lines will be elaborated while |
| 2350 | * parsing. |
| 2351 | * |
| 2352 | * For the parsing, we use a 28 states FSM. |
| 2353 | * |
| 2354 | * Here is the information we currently have : |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 2355 | * req->data + msg->som = beginning of request |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2356 | * req->data + msg->eoh = end of processed headers / start of current one |
| 2357 | * msg->eol = end of current header or line (LF or CRLF) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2358 | * req->lr = first non-visited byte |
| 2359 | * req->r = end of data |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2360 | * |
| 2361 | * At end of parsing, we may perform a capture of the error (if any), and |
| 2362 | * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE). |
| 2363 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 2364 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2365 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2366 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2367 | int cur_idx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2368 | int use_close_only; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2369 | struct http_txn *txn = &s->txn; |
| 2370 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2371 | struct hdr_ctx ctx; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2372 | |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 2373 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 2374 | now_ms, __FUNCTION__, |
| 2375 | s, |
| 2376 | req, |
| 2377 | req->rex, req->wex, |
| 2378 | req->flags, |
| 2379 | req->l, |
| 2380 | req->analysers); |
| 2381 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 2382 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 2383 | s->srv_error = http_return_srv_error; |
| 2384 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2385 | /* There's a protected area at the end of the buffer for rewriting |
| 2386 | * purposes. We don't want to start to parse the request if the |
| 2387 | * protected area is affected, because we may have to move processed |
| 2388 | * data later, which is much more complicated. |
| 2389 | */ |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2390 | if (req->l && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2391 | if ((txn->flags & TX_NOT_FIRST) && |
| 2392 | unlikely((req->flags & BF_FULL) || |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2393 | req->r < req->lr || |
| 2394 | req->r > req->data + req->size - global.tune.maxrewrite)) { |
| 2395 | if (req->send_max) { |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 2396 | if (req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) |
| 2397 | goto failed_keep_alive; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2398 | /* some data has still not left the buffer, wake us once that's done */ |
| 2399 | buffer_dont_connect(req); |
| 2400 | req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 2401 | return 0; |
| 2402 | } |
Willy Tarreau | 0499e35 | 2010-12-17 07:13:42 +0100 | [diff] [blame] | 2403 | 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] | 2404 | http_buffer_heavy_realign(req, msg); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2405 | } |
| 2406 | |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2407 | /* Note that we have the same problem with the response ; we |
| 2408 | * may want to send a redirect, error or anything which requires |
| 2409 | * some spare space. So we'll ensure that we have at least |
| 2410 | * maxrewrite bytes available in the response buffer before |
| 2411 | * processing that one. This will only affect pipelined |
| 2412 | * keep-alive requests. |
| 2413 | */ |
| 2414 | if ((txn->flags & TX_NOT_FIRST) && |
| 2415 | unlikely((s->rep->flags & BF_FULL) || |
| 2416 | s->rep->r < s->rep->lr || |
| 2417 | s->rep->r > s->rep->data + s->rep->size - global.tune.maxrewrite)) { |
| 2418 | if (s->rep->send_max) { |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 2419 | if (s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) |
| 2420 | goto failed_keep_alive; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2421 | /* don't let a connection request be initiated */ |
| 2422 | buffer_dont_connect(req); |
Willy Tarreau | ff7b588 | 2010-01-22 14:41:29 +0100 | [diff] [blame] | 2423 | s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 0499e35 | 2010-12-17 07:13:42 +0100 | [diff] [blame] | 2424 | s->rep->analysers |= an_bit; /* wake us up once it changes */ |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2425 | return 0; |
| 2426 | } |
| 2427 | } |
| 2428 | |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2429 | if (likely(req->lr < req->r)) |
| 2430 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2431 | } |
| 2432 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2433 | /* 1: we might have to print this header in debug mode */ |
| 2434 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2435 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | c3bfeeb | 2010-04-16 09:14:45 +0200 | [diff] [blame] | 2436 | msg->sol && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2437 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2438 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2439 | |
Willy Tarreau | 663308b | 2010-06-07 14:06:08 +0200 | [diff] [blame] | 2440 | sol = req->data + msg->som; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2441 | eol = sol + msg->sl.rq.l; |
| 2442 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2443 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2444 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2445 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2446 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2447 | while (cur_idx) { |
| 2448 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2449 | debug_hdr("clihdr", s, sol, eol); |
| 2450 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2451 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2452 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2453 | } |
| 2454 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2455 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2456 | /* |
| 2457 | * Now we quickly check if we have found a full valid request. |
| 2458 | * If not so, we check the FD and buffer states before leaving. |
| 2459 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2460 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2461 | * requests are checked first. When waiting for a second request |
| 2462 | * on a keep-alive session, if we encounter and error, close, t/o, |
| 2463 | * we note the error in the session flags but don't set any state. |
| 2464 | * Since the error will be noted there, it will not be counted by |
| 2465 | * process_session() as a frontend error. |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2466 | * Last, we may increase some tracked counters' http request errors on |
| 2467 | * the cases that are deliberately the client's fault. For instance, |
| 2468 | * a timeout or connection reset is not counted as an error. However |
| 2469 | * a bad request is. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2470 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2471 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2472 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2473 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2474 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2475 | */ |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2476 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2477 | session_inc_http_req_ctr(s); |
| 2478 | session_inc_http_err_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2479 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2480 | goto return_bad_req; |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2481 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2482 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2483 | /* 1: Since we are in header mode, if there's no space |
| 2484 | * left for headers, we won't be able to free more |
| 2485 | * later, so the session will never terminate. We |
| 2486 | * must terminate it now. |
| 2487 | */ |
| 2488 | if (unlikely(req->flags & BF_FULL)) { |
| 2489 | /* FIXME: check if URI is set and return Status |
| 2490 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2491 | */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2492 | session_inc_http_req_ctr(s); |
| 2493 | session_inc_http_err_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2494 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 2495 | if (msg->err_pos < 0) |
| 2496 | msg->err_pos = req->l; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2497 | goto return_bad_req; |
| 2498 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2499 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2500 | /* 2: have we encountered a read error ? */ |
| 2501 | else if (req->flags & BF_READ_ERROR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2502 | if (!(s->flags & SN_ERR_MASK)) |
| 2503 | s->flags |= SN_ERR_CLICL; |
| 2504 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2505 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2506 | goto failed_keep_alive; |
| 2507 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2508 | /* we cannot return any message on error */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2509 | if (msg->err_pos >= 0) { |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 2510 | 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] | 2511 | session_inc_http_err_ctr(s); |
| 2512 | } |
| 2513 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2514 | msg->msg_state = HTTP_MSG_ERROR; |
| 2515 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2516 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2517 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2518 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2519 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2520 | if (s->listener->counters) |
| 2521 | s->listener->counters->failed_req++; |
| 2522 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2523 | if (!(s->flags & SN_FINST_MASK)) |
| 2524 | s->flags |= SN_FINST_R; |
| 2525 | return 0; |
| 2526 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2527 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2528 | /* 3: has the read timeout expired ? */ |
| 2529 | 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] | 2530 | if (!(s->flags & SN_ERR_MASK)) |
| 2531 | s->flags |= SN_ERR_CLITO; |
| 2532 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2533 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2534 | goto failed_keep_alive; |
| 2535 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2536 | /* read timeout : give up with an error message. */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2537 | if (msg->err_pos >= 0) { |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 2538 | 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] | 2539 | session_inc_http_err_ctr(s); |
| 2540 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2541 | txn->status = 408; |
| 2542 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408)); |
| 2543 | msg->msg_state = HTTP_MSG_ERROR; |
| 2544 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2545 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2546 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2547 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2548 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2549 | if (s->listener->counters) |
| 2550 | s->listener->counters->failed_req++; |
| 2551 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2552 | if (!(s->flags & SN_FINST_MASK)) |
| 2553 | s->flags |= SN_FINST_R; |
| 2554 | return 0; |
| 2555 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2556 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2557 | /* 4: have we encountered a close ? */ |
| 2558 | else if (req->flags & BF_SHUTR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2559 | if (!(s->flags & SN_ERR_MASK)) |
| 2560 | s->flags |= SN_ERR_CLICL; |
| 2561 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2562 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2563 | goto failed_keep_alive; |
| 2564 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2565 | if (msg->err_pos >= 0) |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 2566 | 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] | 2567 | txn->status = 400; |
| 2568 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
| 2569 | msg->msg_state = HTTP_MSG_ERROR; |
| 2570 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2571 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2572 | session_inc_http_err_ctr(s); |
| 2573 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2574 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2575 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2576 | if (s->listener->counters) |
| 2577 | s->listener->counters->failed_req++; |
| 2578 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2579 | if (!(s->flags & SN_FINST_MASK)) |
| 2580 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2581 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2582 | } |
| 2583 | |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2584 | buffer_dont_connect(req); |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2585 | req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | ff7b588 | 2010-01-22 14:41:29 +0100 | [diff] [blame] | 2586 | s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2587 | #ifdef TCP_QUICKACK |
| 2588 | if (s->listener->options & LI_O_NOQUICKACK) { |
| 2589 | /* We need more data, we have to re-enable quick-ack in case we |
| 2590 | * previously disabled it, otherwise we might cause the client |
| 2591 | * to delay next data. |
| 2592 | */ |
| 2593 | setsockopt(s->si[0].fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); |
| 2594 | } |
| 2595 | #endif |
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 | eabea07 | 2011-09-10 23:29:44 +0200 | [diff] [blame] | 2684 | s->fe->fe_counters.intercepted_req++; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2685 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2686 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2687 | list_for_each_entry(cond, &s->fe->mon_fail_cond, list) { |
| 2688 | 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] | 2689 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2690 | ret = acl_pass(ret); |
| 2691 | if (cond->pol == ACL_COND_UNLESS) |
| 2692 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2693 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2694 | if (ret) { |
| 2695 | /* we fail this request, let's return 503 service unavail */ |
| 2696 | txn->status = 503; |
| 2697 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503)); |
| 2698 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2699 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2700 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 2701 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2702 | /* nothing to fail, let's reply normaly */ |
| 2703 | txn->status = 200; |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 2704 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_200)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2705 | goto return_prx_cond; |
| 2706 | } |
| 2707 | |
| 2708 | /* |
| 2709 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 2710 | * Note: we cannot log anymore if the request has been |
| 2711 | * classified as invalid. |
| 2712 | */ |
| 2713 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 2714 | /* we have a complete HTTP request that we must log */ |
| 2715 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
| 2716 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2717 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2718 | if (urilen >= REQURI_LEN) |
| 2719 | urilen = REQURI_LEN - 1; |
| 2720 | memcpy(txn->uri, &req->data[msg->som], urilen); |
| 2721 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2722 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2723 | if (!(s->logs.logwait &= ~LW_REQ)) |
| 2724 | s->do_log(s); |
| 2725 | } else { |
| 2726 | Alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2727 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2728 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2729 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2730 | /* 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] | 2731 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) |
| 2732 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2733 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2734 | /* ... and check if the request is HTTP/1.1 or above */ |
| 2735 | if ((msg->sl.rq.v_l == 8) && |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2736 | ((msg->sol[msg->sl.rq.v + 5] > '1') || |
| 2737 | ((msg->sol[msg->sl.rq.v + 5] == '1') && |
| 2738 | (msg->sol[msg->sl.rq.v + 7] >= '1')))) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2739 | txn->flags |= TX_REQ_VER_11; |
| 2740 | |
| 2741 | /* "connection" has not been parsed yet */ |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2742 | 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] | 2743 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2744 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 2745 | * we have what looks like a proxied connection instead of a connection, |
| 2746 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 2747 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 2748 | * happen to do that despite being non-standard :-( |
| 2749 | * We consider that a request not beginning with either '/' or '*' is |
| 2750 | * a proxied connection, which covers both "scheme://location" and |
| 2751 | * CONNECT ip:port. |
| 2752 | */ |
| 2753 | if ((s->fe->options2 & PR_O2_USE_PXHDR) && |
| 2754 | msg->sol[msg->sl.rq.u] != '/' && msg->sol[msg->sl.rq.u] != '*') |
| 2755 | txn->flags |= TX_USE_PX_CONN; |
| 2756 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2757 | /* transfer length unknown*/ |
| 2758 | txn->flags &= ~TX_REQ_XFER_LEN; |
| 2759 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2760 | /* 5: we may need to capture headers */ |
| 2761 | if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap)) |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2762 | capture_headers(msg->sol, &txn->hdr_idx, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2763 | txn->req.cap, s->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2764 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2765 | /* 6: determine the transfer-length. |
| 2766 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 2767 | * the presence of a message-body in a REQUEST and its transfer length |
| 2768 | * must be determined that way (in order of precedence) : |
| 2769 | * 1. The presence of a message-body in a request is signaled by the |
| 2770 | * inclusion of a Content-Length or Transfer-Encoding header field |
| 2771 | * in the request's header fields. When a request message contains |
| 2772 | * both a message-body of non-zero length and a method that does |
| 2773 | * not define any semantics for that request message-body, then an |
| 2774 | * origin server SHOULD either ignore the message-body or respond |
| 2775 | * with an appropriate error message (e.g., 413). A proxy or |
| 2776 | * gateway, when presented the same request, SHOULD either forward |
| 2777 | * the request inbound with the message- body or ignore the |
| 2778 | * message-body when determining a response. |
| 2779 | * |
| 2780 | * 2. If a Transfer-Encoding header field (Section 9.7) is present |
| 2781 | * and the "chunked" transfer-coding (Section 6.2) is used, the |
| 2782 | * transfer-length is defined by the use of this transfer-coding. |
| 2783 | * If a Transfer-Encoding header field is present and the "chunked" |
| 2784 | * transfer-coding is not present, the transfer-length is defined |
| 2785 | * by the sender closing the connection. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2786 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2787 | * 3. If a Content-Length header field is present, its decimal value in |
| 2788 | * OCTETs represents both the entity-length and the transfer-length. |
| 2789 | * If a message is received with both a Transfer-Encoding header |
| 2790 | * field and a Content-Length header field, the latter MUST be ignored. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2791 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2792 | * 4. By the server closing the connection. (Closing the connection |
| 2793 | * cannot be used to indicate the end of a request body, since that |
| 2794 | * would leave no possibility for the server to send back a response.) |
| 2795 | * |
| 2796 | * Whenever a transfer-coding is applied to a message-body, the set of |
| 2797 | * transfer-codings MUST include "chunked", unless the message indicates |
| 2798 | * it is terminated by closing the connection. When the "chunked" |
| 2799 | * transfer-coding is used, it MUST be the last transfer-coding applied |
| 2800 | * to the message-body. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2801 | */ |
| 2802 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2803 | use_close_only = 0; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2804 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2805 | /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
Willy Tarreau | 9e13c3c | 2009-12-22 09:59:58 +0100 | [diff] [blame] | 2806 | while ((txn->flags & TX_REQ_VER_11) && |
| 2807 | http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2808 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
| 2809 | txn->flags |= (TX_REQ_TE_CHNK | TX_REQ_XFER_LEN); |
| 2810 | else if (txn->flags & TX_REQ_TE_CHNK) { |
| 2811 | /* bad transfer-encoding (chunked followed by something else) */ |
| 2812 | use_close_only = 1; |
| 2813 | txn->flags &= ~(TX_REQ_TE_CHNK | TX_REQ_XFER_LEN); |
| 2814 | break; |
| 2815 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2816 | } |
| 2817 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2818 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2819 | while (!(txn->flags & TX_REQ_TE_CHNK) && !use_close_only && |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2820 | http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) { |
| 2821 | signed long long cl; |
| 2822 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2823 | if (!ctx.vlen) { |
| 2824 | msg->err_pos = ctx.line + ctx.val - req->data; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2825 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2826 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2827 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2828 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
| 2829 | msg->err_pos = ctx.line + ctx.val - req->data; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2830 | goto return_bad_req; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2831 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2832 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2833 | if (cl < 0) { |
| 2834 | msg->err_pos = ctx.line + ctx.val - req->data; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2835 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2836 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2837 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2838 | if ((txn->flags & TX_REQ_CNT_LEN) && (msg->chunk_len != cl)) { |
| 2839 | msg->err_pos = ctx.line + ctx.val - req->data; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2840 | goto return_bad_req; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2841 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2842 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2843 | txn->flags |= TX_REQ_CNT_LEN | TX_REQ_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2844 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2845 | } |
| 2846 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2847 | /* bodyless requests have a known length */ |
| 2848 | if (!use_close_only) |
| 2849 | txn->flags |= TX_REQ_XFER_LEN; |
| 2850 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2851 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2852 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2853 | req->analyse_exp = TICK_ETERNITY; |
| 2854 | return 1; |
| 2855 | |
| 2856 | return_bad_req: |
| 2857 | /* We centralize bad requests processing here */ |
| 2858 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 2859 | /* we detected a parsing error. We want to archive this request |
| 2860 | * in the dedicated proxy area for later troubleshooting. |
| 2861 | */ |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 2862 | 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] | 2863 | } |
| 2864 | |
| 2865 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 2866 | txn->status = 400; |
| 2867 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2868 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2869 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2870 | if (s->listener->counters) |
| 2871 | s->listener->counters->failed_req++; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2872 | |
| 2873 | return_prx_cond: |
| 2874 | if (!(s->flags & SN_ERR_MASK)) |
| 2875 | s->flags |= SN_ERR_PRXCOND; |
| 2876 | if (!(s->flags & SN_FINST_MASK)) |
| 2877 | s->flags |= SN_FINST_R; |
| 2878 | |
| 2879 | req->analysers = 0; |
| 2880 | req->analyse_exp = TICK_ETERNITY; |
| 2881 | return 0; |
| 2882 | } |
| 2883 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2884 | /* We reached the stats page through a POST request. |
| 2885 | * Parse the posted data and enable/disable servers if necessary. |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2886 | * 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] | 2887 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2888 | 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] | 2889 | { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2890 | struct proxy *px; |
| 2891 | struct server *sv; |
| 2892 | |
| 2893 | char *backend = NULL; |
| 2894 | int action = 0; |
| 2895 | |
| 2896 | char *first_param, *cur_param, *next_param, *end_params; |
| 2897 | |
| 2898 | first_param = req->data + txn->req.eoh + 2; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2899 | end_params = first_param + txn->req.body_len; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2900 | |
| 2901 | cur_param = next_param = end_params; |
| 2902 | |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2903 | if (end_params >= req->data + req->size - global.tune.maxrewrite) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2904 | /* Prevent buffer overflow */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2905 | si->applet.ctx.stats.st_code = STAT_STATUS_EXCD; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2906 | return 1; |
| 2907 | } |
| 2908 | else if (end_params > req->data + req->l) { |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2909 | /* we need more data */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2910 | si->applet.ctx.stats.st_code = STAT_STATUS_NONE; |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2911 | return 0; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2912 | } |
| 2913 | |
| 2914 | *end_params = '\0'; |
| 2915 | |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2916 | si->applet.ctx.stats.st_code = STAT_STATUS_NONE; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2917 | |
| 2918 | /* |
| 2919 | * Parse the parameters in reverse order to only store the last value. |
| 2920 | * From the html form, the backend and the action are at the end. |
| 2921 | */ |
| 2922 | while (cur_param > first_param) { |
| 2923 | char *key, *value; |
| 2924 | |
| 2925 | cur_param--; |
| 2926 | if ((*cur_param == '&') || (cur_param == first_param)) { |
| 2927 | /* Parse the key */ |
| 2928 | key = cur_param; |
| 2929 | if (cur_param != first_param) { |
| 2930 | /* delimit the string for the next loop */ |
| 2931 | *key++ = '\0'; |
| 2932 | } |
| 2933 | |
| 2934 | /* Parse the value */ |
| 2935 | value = key; |
| 2936 | while (*value != '\0' && *value != '=') { |
| 2937 | value++; |
| 2938 | } |
| 2939 | if (*value == '=') { |
| 2940 | /* Ok, a value is found, we can mark the end of the key */ |
| 2941 | *value++ = '\0'; |
| 2942 | } |
| 2943 | |
Willy Tarreau | bf9c2fc | 2011-05-31 18:06:18 +0200 | [diff] [blame] | 2944 | if (!url_decode(key) || !url_decode(value)) |
| 2945 | break; |
| 2946 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2947 | /* Now we can check the key to see what to do */ |
| 2948 | if (!backend && strcmp(key, "b") == 0) { |
| 2949 | backend = value; |
| 2950 | } |
| 2951 | else if (!action && strcmp(key, "action") == 0) { |
| 2952 | if (strcmp(value, "disable") == 0) { |
| 2953 | action = 1; |
| 2954 | } |
| 2955 | else if (strcmp(value, "enable") == 0) { |
| 2956 | action = 2; |
| 2957 | } else { |
| 2958 | /* unknown action, no need to continue */ |
| 2959 | break; |
| 2960 | } |
| 2961 | } |
| 2962 | else if (strcmp(key, "s") == 0) { |
| 2963 | if (backend && action && get_backend_server(backend, value, &px, &sv)) { |
| 2964 | switch (action) { |
| 2965 | case 1: |
Cyril Bonté | 1e2a170 | 2011-03-03 21:05:17 +0100 | [diff] [blame] | 2966 | if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2967 | /* Not already in maintenance, we can change the server state */ |
| 2968 | sv->state |= SRV_MAINTAIN; |
| 2969 | set_server_down(sv); |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2970 | si->applet.ctx.stats.st_code = STAT_STATUS_DONE; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2971 | } |
| 2972 | break; |
| 2973 | case 2: |
Cyril Bonté | 1e2a170 | 2011-03-03 21:05:17 +0100 | [diff] [blame] | 2974 | if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2975 | /* Already in maintenance, we can change the server state */ |
| 2976 | set_server_up(sv); |
Willy Tarreau | 7046130 | 2010-10-22 14:39:02 +0200 | [diff] [blame] | 2977 | sv->health = sv->rise; /* up, but will fall down at first failure */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2978 | si->applet.ctx.stats.st_code = STAT_STATUS_DONE; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2979 | } |
| 2980 | break; |
| 2981 | } |
| 2982 | } |
| 2983 | } |
| 2984 | next_param = cur_param; |
| 2985 | } |
| 2986 | } |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2987 | return 1; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2988 | } |
| 2989 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2990 | /* returns a pointer to the first rule which forbids access (deny or http_auth), |
| 2991 | * or NULL if everything's OK. |
| 2992 | */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2993 | static inline struct http_req_rule * |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2994 | http_check_access_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn) |
| 2995 | { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2996 | struct http_req_rule *rule; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2997 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2998 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2999 | int ret = 1; |
| 3000 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3001 | if (rule->action >= HTTP_REQ_ACT_MAX) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3002 | continue; |
| 3003 | |
| 3004 | /* check condition, but only if attached */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3005 | if (rule->cond) { |
| 3006 | ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ); |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3007 | ret = acl_pass(ret); |
| 3008 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3009 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3010 | ret = !ret; |
| 3011 | } |
| 3012 | |
| 3013 | if (ret) { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3014 | if (rule->action == HTTP_REQ_ACT_ALLOW) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3015 | return NULL; /* no problem */ |
| 3016 | else |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3017 | return rule; /* most likely a deny or auth rule */ |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3018 | } |
| 3019 | } |
| 3020 | return NULL; |
| 3021 | } |
| 3022 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3023 | /* This stream analyser runs all HTTP request processing which is common to |
| 3024 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 3025 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3026 | * 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] | 3027 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 3028 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3029 | */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3030 | 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] | 3031 | { |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3032 | struct http_txn *txn = &s->txn; |
| 3033 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3034 | struct acl_cond *cond; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3035 | struct http_req_rule *http_req_last_rule = NULL; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3036 | struct redirect_rule *rule; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 3037 | struct cond_wordlist *wl; |
Simon Horman | 70735c9 | 2011-06-07 11:07:50 +0900 | [diff] [blame] | 3038 | int do_stats; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3039 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3040 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3041 | /* we need more data */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3042 | buffer_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3043 | return 0; |
| 3044 | } |
| 3045 | |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3046 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3047 | req->analyse_exp = TICK_ETERNITY; |
| 3048 | |
| 3049 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 3050 | now_ms, __FUNCTION__, |
| 3051 | s, |
| 3052 | req, |
| 3053 | req->rex, req->wex, |
| 3054 | req->flags, |
| 3055 | req->l, |
| 3056 | req->analysers); |
| 3057 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3058 | /* first check whether we have some ACLs set to block this request */ |
| 3059 | list_for_each_entry(cond, &px->block_cond, list) { |
| 3060 | int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ); |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 3061 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3062 | ret = acl_pass(ret); |
| 3063 | if (cond->pol == ACL_COND_UNLESS) |
| 3064 | ret = !ret; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 3065 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3066 | if (ret) { |
| 3067 | txn->status = 403; |
| 3068 | /* let's log the request time */ |
| 3069 | s->logs.tv_request = now; |
| 3070 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3071 | session_inc_http_err_ctr(s); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3072 | goto return_prx_cond; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3073 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3074 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3075 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3076 | /* evaluate http-request rules */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3077 | 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] | 3078 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3079 | /* evaluate stats http-request rules only if http-request is OK */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3080 | if (!http_req_last_rule) { |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3081 | do_stats = stats_check_uri(s->rep->prod, txn, px); |
| 3082 | if (do_stats) |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3083 | 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] | 3084 | } |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3085 | else |
| 3086 | do_stats = 0; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3087 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3088 | /* return a 403 if either rule has blocked */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3089 | 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] | 3090 | txn->status = 403; |
| 3091 | s->logs.tv_request = now; |
| 3092 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3093 | session_inc_http_err_ctr(s); |
Willy Tarreau | 6da0f6d | 2011-01-06 18:19:50 +0100 | [diff] [blame] | 3094 | s->fe->fe_counters.denied_req++; |
| 3095 | if (an_bit == AN_REQ_HTTP_PROCESS_BE) |
| 3096 | s->be->be_counters.denied_req++; |
| 3097 | if (s->listener->counters) |
| 3098 | s->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3099 | goto return_prx_cond; |
| 3100 | } |
| 3101 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3102 | /* try headers filters */ |
| 3103 | if (px->req_exp != NULL) { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 3104 | if (apply_filters_to_request(s, req, px) < 0) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3105 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3106 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3107 | /* has the request been denied ? */ |
| 3108 | if (txn->flags & TX_CLDENY) { |
| 3109 | /* no need to go further */ |
| 3110 | txn->status = 403; |
| 3111 | /* let's log the request time */ |
| 3112 | s->logs.tv_request = now; |
| 3113 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3114 | session_inc_http_err_ctr(s); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3115 | goto return_prx_cond; |
| 3116 | } |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 3117 | |
| 3118 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 3119 | * which may be the same as the connect timeout if unspecified. |
| 3120 | * If unset, then set it to zero because we really want it to |
| 3121 | * eventually expire. We build the tarpit as an analyser. |
| 3122 | */ |
| 3123 | if (txn->flags & TX_CLTARPIT) { |
| 3124 | buffer_erase(s->req); |
| 3125 | /* wipe the request out so that we can drop the connection early |
| 3126 | * if the client closes first. |
| 3127 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3128 | buffer_dont_connect(req); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 3129 | req->analysers = 0; /* remove switching rules etc... */ |
| 3130 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 3131 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 3132 | if (!req->analyse_exp) |
| 3133 | req->analyse_exp = tick_add(now_ms, 0); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3134 | session_inc_http_err_ctr(s); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 3135 | return 1; |
| 3136 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3137 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3138 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3139 | /* Until set to anything else, the connection mode is set as TUNNEL. It will |
| 3140 | * only change if both the request and the config reference something else. |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 3141 | * Option httpclose by itself does not set a mode, it remains a tunnel mode |
| 3142 | * 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] | 3143 | * affect it (eg: server-close/keep-alive + httpclose = close). Note that we |
| 3144 | * avoid to redo the same work if FE and BE have the same settings (common). |
| 3145 | * The method consists in checking if options changed between the two calls |
| 3146 | * (implying that either one is non-null, or one of them is non-null and we |
| 3147 | * are there for the first time. |
Willy Tarreau | 4273664 | 2009-10-18 21:04:35 +0200 | [diff] [blame] | 3148 | */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3149 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 3150 | if ((!(txn->flags & TX_HDR_CONN_PRS) && |
| 3151 | (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) || |
| 3152 | ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) != |
| 3153 | (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] | 3154 | int tmp = TX_CON_WANT_TUN; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3155 | |
Cyril Bonté | 9ea2b9a | 2010-12-29 09:36:56 +0100 | [diff] [blame] | 3156 | if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE || |
| 3157 | ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3158 | tmp = TX_CON_WANT_KAL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 3159 | if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO) |
| 3160 | tmp = TX_CON_WANT_SCL; |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 3161 | if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3162 | tmp = TX_CON_WANT_CLO; |
| 3163 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3164 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
| 3165 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 3166 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3167 | if (!(txn->flags & TX_HDR_CONN_PRS)) { |
| 3168 | /* parse the Connection header and possibly clean it */ |
| 3169 | int to_del = 0; |
| 3170 | if ((txn->flags & TX_REQ_VER_11) || |
Willy Tarreau | 8a8e1d9 | 2010-04-05 16:15:16 +0200 | [diff] [blame] | 3171 | ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL && |
| 3172 | !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3173 | to_del |= 2; /* remove "keep-alive" */ |
| 3174 | if (!(txn->flags & TX_REQ_VER_11)) |
| 3175 | to_del |= 1; /* remove "close" */ |
| 3176 | http_parse_connection_header(txn, msg, req, to_del); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 3177 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3178 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3179 | /* check if client or config asks for explicit close in KAL/SCL */ |
| 3180 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 3181 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) && |
| 3182 | ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */ |
| 3183 | (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] | 3184 | ((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] | 3185 | !(txn->flags & TX_REQ_XFER_LEN) || /* no length known => close */ |
| 3186 | s->fe->state == PR_STSTOPPED)) /* frontend is stopping */ |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3187 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 3188 | } |
Willy Tarreau | 7859991 | 2009-10-17 20:12:21 +0200 | [diff] [blame] | 3189 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3190 | /* we can be blocked here because the request needs to be authenticated, |
| 3191 | * either to pass or to access stats. |
| 3192 | */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3193 | 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] | 3194 | struct chunk msg; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3195 | char *realm = http_req_last_rule->http_auth.realm; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3196 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3197 | if (!realm) |
| 3198 | realm = do_stats?STATS_DEFAULT_REALM:px->id; |
| 3199 | |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 3200 | 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] | 3201 | chunk_initlen(&msg, trash, sizeof(trash), strlen(trash)); |
| 3202 | txn->status = 401; |
| 3203 | stream_int_retnclose(req->prod, &msg); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3204 | /* on 401 we still count one error, because normal browsing |
| 3205 | * won't significantly increase the counter but brute force |
| 3206 | * attempts will. |
| 3207 | */ |
| 3208 | session_inc_http_err_ctr(s); |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3209 | goto return_prx_cond; |
| 3210 | } |
| 3211 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3212 | /* add request headers from the rule sets in the same order */ |
| 3213 | list_for_each_entry(wl, &px->req_add, list) { |
| 3214 | if (wl->cond) { |
| 3215 | int ret = acl_exec_cond(wl->cond, px, s, txn, ACL_DIR_REQ); |
| 3216 | ret = acl_pass(ret); |
| 3217 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 3218 | ret = !ret; |
| 3219 | if (!ret) |
| 3220 | continue; |
| 3221 | } |
| 3222 | |
| 3223 | if (unlikely(http_header_add_tail(req, &txn->req, &txn->hdr_idx, wl->s) < 0)) |
| 3224 | goto return_bad_req; |
| 3225 | } |
| 3226 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3227 | if (do_stats) { |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 3228 | struct stats_admin_rule *stats_admin_rule; |
| 3229 | |
| 3230 | /* We need to provide stats for this request. |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3231 | * FIXME!!! that one is rather dangerous, we want to |
| 3232 | * make it follow standard rules (eg: clear req->analysers). |
| 3233 | */ |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3234 | |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 3235 | /* now check whether we have some admin rules for this request */ |
| 3236 | list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) { |
| 3237 | int ret = 1; |
| 3238 | |
| 3239 | if (stats_admin_rule->cond) { |
| 3240 | ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ); |
| 3241 | ret = acl_pass(ret); |
| 3242 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 3243 | ret = !ret; |
| 3244 | } |
| 3245 | |
| 3246 | if (ret) { |
| 3247 | /* no rule, or the rule matches */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 3248 | s->rep->prod->applet.ctx.stats.flags |= STAT_ADMIN; |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 3249 | break; |
| 3250 | } |
| 3251 | } |
| 3252 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 3253 | /* Was the status page requested with a POST ? */ |
| 3254 | if (txn->meth == HTTP_METH_POST) { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 3255 | if (s->rep->prod->applet.ctx.stats.flags & STAT_ADMIN) { |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 3256 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 3257 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 3258 | * send an HTTP/1.1 100 Continue intermediate response. |
| 3259 | */ |
| 3260 | if (txn->flags & TX_REQ_VER_11) { |
| 3261 | struct hdr_ctx ctx; |
| 3262 | ctx.idx = 0; |
| 3263 | /* Expect is allowed in 1.1, look for it */ |
| 3264 | if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) && |
| 3265 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) { |
| 3266 | buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len); |
| 3267 | } |
| 3268 | } |
| 3269 | msg->msg_state = HTTP_MSG_100_SENT; |
| 3270 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 3271 | } |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 3272 | if (!http_process_req_stat_post(s->rep->prod, txn, req)) { |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 3273 | /* we need more data */ |
| 3274 | req->analysers |= an_bit; |
| 3275 | buffer_dont_connect(req); |
| 3276 | return 0; |
| 3277 | } |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 3278 | } else { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 3279 | s->rep->prod->applet.ctx.stats.st_code = STAT_STATUS_DENY; |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 3280 | } |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 3281 | } |
| 3282 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3283 | s->logs.tv_request = now; |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3284 | s->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | b24281b | 2011-02-13 13:16:36 +0100 | [diff] [blame] | 3285 | stream_int_register_handler(s->rep->prod, &http_stats_applet); |
Willy Tarreau | 7b7a8e9 | 2011-03-27 19:53:06 +0200 | [diff] [blame] | 3286 | copy_target(&s->target, &s->rep->prod->target); // for logging only |
Willy Tarreau | bc4af05 | 2011-02-13 13:25:14 +0100 | [diff] [blame] | 3287 | s->rep->prod->applet.private = s; |
| 3288 | s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0; |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3289 | req->analysers = 0; |
Willy Tarreau | eabea07 | 2011-09-10 23:29:44 +0200 | [diff] [blame] | 3290 | if (s->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 3291 | s->fe->fe_counters.intercepted_req++; |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3292 | |
| 3293 | return 0; |
| 3294 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3295 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 3296 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3297 | /* check whether we have some ACLs set to redirect this request */ |
| 3298 | list_for_each_entry(rule, &px->redirect_rules, list) { |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3299 | int ret = ACL_PAT_PASS; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3300 | |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3301 | if (rule->cond) { |
| 3302 | ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ); |
| 3303 | ret = acl_pass(ret); |
| 3304 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 3305 | ret = !ret; |
| 3306 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3307 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3308 | if (ret) { |
Willy Tarreau | 3bb9c23 | 2010-01-03 12:24:37 +0100 | [diff] [blame] | 3309 | struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 }; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3310 | const char *msg_fmt; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3311 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3312 | /* build redirect message */ |
| 3313 | switch(rule->code) { |
| 3314 | case 303: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3315 | msg_fmt = HTTP_303; |
| 3316 | break; |
| 3317 | case 301: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3318 | msg_fmt = HTTP_301; |
| 3319 | break; |
| 3320 | case 302: |
| 3321 | default: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3322 | msg_fmt = HTTP_302; |
| 3323 | break; |
| 3324 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3325 | |
Willy Tarreau | 3bb9c23 | 2010-01-03 12:24:37 +0100 | [diff] [blame] | 3326 | if (unlikely(!chunk_strcpy(&rdr, msg_fmt))) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3327 | goto return_bad_req; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3328 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3329 | switch(rule->type) { |
| 3330 | case REDIRECT_TYPE_PREFIX: { |
| 3331 | const char *path; |
| 3332 | int pathlen; |
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 | path = http_get_path(txn); |
| 3335 | /* build message using path */ |
| 3336 | if (path) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 3337 | 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] | 3338 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3339 | int qs = 0; |
| 3340 | while (qs < pathlen) { |
| 3341 | if (path[qs] == '?') { |
| 3342 | pathlen = qs; |
| 3343 | break; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3344 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3345 | qs++; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3346 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3347 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3348 | } else { |
| 3349 | path = "/"; |
| 3350 | pathlen = 1; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3351 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3352 | |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 3353 | if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3354 | goto return_bad_req; |
| 3355 | |
| 3356 | /* add prefix. Note that if prefix == "/", we don't want to |
| 3357 | * add anything, otherwise it makes it hard for the user to |
| 3358 | * configure a self-redirection. |
| 3359 | */ |
| 3360 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3361 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 3362 | rdr.len += rule->rdr_len; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3363 | } |
| 3364 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3365 | /* add path */ |
| 3366 | memcpy(rdr.str + rdr.len, path, pathlen); |
| 3367 | rdr.len += pathlen; |
Willy Tarreau | 81e3b4f | 2010-01-10 00:42:19 +0100 | [diff] [blame] | 3368 | |
| 3369 | /* append a slash at the end of the location is needed and missing */ |
| 3370 | if (rdr.len && rdr.str[rdr.len - 1] != '/' && |
| 3371 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 3372 | if (rdr.len > rdr.size - 5) |
| 3373 | goto return_bad_req; |
| 3374 | rdr.str[rdr.len] = '/'; |
| 3375 | rdr.len++; |
| 3376 | } |
| 3377 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3378 | break; |
| 3379 | } |
| 3380 | case REDIRECT_TYPE_LOCATION: |
| 3381 | default: |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 3382 | if (rdr.len + rule->rdr_len > rdr.size - 4) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3383 | goto return_bad_req; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3384 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3385 | /* add location */ |
| 3386 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 3387 | rdr.len += rule->rdr_len; |
| 3388 | break; |
| 3389 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3390 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3391 | if (rule->cookie_len) { |
| 3392 | memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14); |
| 3393 | rdr.len += 14; |
| 3394 | memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len); |
| 3395 | rdr.len += rule->cookie_len; |
| 3396 | memcpy(rdr.str + rdr.len, "\r\n", 2); |
| 3397 | rdr.len += 2; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3398 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3399 | |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3400 | /* add end of headers and the keep-alive/close status. |
| 3401 | * We may choose to set keep-alive if the Location begins |
| 3402 | * with a slash, because the client will come back to the |
| 3403 | * same server. |
| 3404 | */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3405 | txn->status = rule->code; |
| 3406 | /* let's log the request time */ |
| 3407 | s->logs.tv_request = now; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3408 | |
| 3409 | if (rule->rdr_len >= 1 && *rule->rdr_str == '/' && |
| 3410 | (txn->flags & TX_REQ_XFER_LEN) && |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 3411 | !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.body_len && |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3412 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 3413 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 3414 | /* keep-alive possible */ |
Willy Tarreau | 7566145 | 2010-01-10 10:35:01 +0100 | [diff] [blame] | 3415 | if (!(txn->flags & TX_REQ_VER_11)) { |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 3416 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 3417 | memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: keep-alive", 30); |
| 3418 | rdr.len += 30; |
| 3419 | } else { |
| 3420 | memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24); |
| 3421 | rdr.len += 24; |
| 3422 | } |
Willy Tarreau | 7566145 | 2010-01-10 10:35:01 +0100 | [diff] [blame] | 3423 | } |
| 3424 | memcpy(rdr.str + rdr.len, "\r\n\r\n", 4); |
| 3425 | rdr.len += 4; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3426 | buffer_write(req->prod->ob, rdr.str, rdr.len); |
| 3427 | /* "eat" the request */ |
| 3428 | buffer_ignore(req, msg->sov - msg->som); |
| 3429 | msg->som = msg->sov; |
| 3430 | req->analysers = AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 9300fb2 | 2010-01-05 00:58:24 +0100 | [diff] [blame] | 3431 | s->rep->analysers = AN_RES_HTTP_XFER_BODY; |
| 3432 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3433 | txn->rsp.msg_state = HTTP_MSG_DONE; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3434 | break; |
| 3435 | } else { |
| 3436 | /* keep-alive not possible */ |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 3437 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 3438 | memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 3439 | rdr.len += 29; |
| 3440 | } else { |
| 3441 | memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23); |
| 3442 | rdr.len += 23; |
| 3443 | } |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 3444 | stream_int_retnclose(req->prod, &rdr); |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3445 | goto return_prx_cond; |
| 3446 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3447 | } |
| 3448 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3449 | |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 3450 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 3451 | * If this happens, then the data will not come immediately, so we must |
| 3452 | * send all what we have without waiting. Note that due to the small gain |
| 3453 | * in waiting for the body of the request, it's easier to simply put the |
| 3454 | * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove |
| 3455 | * itself once used. |
| 3456 | */ |
| 3457 | req->flags |= BF_SEND_DONTWAIT; |
| 3458 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3459 | /* that's OK for us now, let's move on to next analysers */ |
| 3460 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 3461 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3462 | return_bad_req: |
| 3463 | /* We centralize bad requests processing here */ |
| 3464 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 3465 | /* we detected a parsing error. We want to archive this request |
| 3466 | * in the dedicated proxy area for later troubleshooting. |
| 3467 | */ |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 3468 | 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] | 3469 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3470 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3471 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3472 | txn->status = 400; |
| 3473 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3474 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3475 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3476 | if (s->listener->counters) |
| 3477 | s->listener->counters->failed_req++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 3478 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3479 | return_prx_cond: |
| 3480 | if (!(s->flags & SN_ERR_MASK)) |
| 3481 | s->flags |= SN_ERR_PRXCOND; |
| 3482 | if (!(s->flags & SN_FINST_MASK)) |
| 3483 | s->flags |= SN_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 3484 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3485 | req->analysers = 0; |
| 3486 | req->analyse_exp = TICK_ETERNITY; |
| 3487 | return 0; |
| 3488 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3489 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3490 | /* This function performs all the processing enabled for the current request. |
| 3491 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 3492 | * needs more data, encounters an error, or wants to immediately abort the |
| 3493 | * request. It relies on buffers flags, and updates s->req->analysers. |
| 3494 | */ |
| 3495 | int http_process_request(struct session *s, struct buffer *req, int an_bit) |
| 3496 | { |
| 3497 | struct http_txn *txn = &s->txn; |
| 3498 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3499 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3500 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3501 | /* we need more data */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3502 | buffer_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3503 | return 0; |
| 3504 | } |
| 3505 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3506 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 3507 | now_ms, __FUNCTION__, |
| 3508 | s, |
| 3509 | req, |
| 3510 | req->rex, req->wex, |
| 3511 | req->flags, |
| 3512 | req->l, |
| 3513 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3514 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3515 | /* |
| 3516 | * Right now, we know that we have processed the entire headers |
| 3517 | * and that unwanted requests have been filtered out. We can do |
| 3518 | * whatever we want with the remaining request. Also, now we |
| 3519 | * may have separate values for ->fe, ->be. |
| 3520 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3521 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3522 | /* |
| 3523 | * If HTTP PROXY is set we simply get remote server address |
| 3524 | * parsing incoming request. |
| 3525 | */ |
| 3526 | if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) { |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3527 | url2sa(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l, &s->req->cons->addr.to); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3528 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3529 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3530 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3531 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3532 | * Note that doing so might move headers in the request, but |
| 3533 | * the fields will stay coherent and the URI will not move. |
| 3534 | * This should only be performed in the backend. |
| 3535 | */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 3536 | 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] | 3537 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
| 3538 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 3539 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3540 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3541 | * 8: the appsession cookie was looked up very early in 1.2, |
| 3542 | * so let's do the same now. |
| 3543 | */ |
| 3544 | |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 3545 | /* It needs to look into the URI unless persistence must be ignored */ |
| 3546 | 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] | 3547 | 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] | 3548 | } |
| 3549 | |
| 3550 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3551 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 3552 | * asks for it. |
| 3553 | */ |
| 3554 | if ((s->fe->options | s->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 3555 | struct hdr_ctx ctx = { .idx = 0 }; |
| 3556 | |
| 3557 | if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) && |
Sagi Bashari | 1611e2d | 2011-10-08 22:48:48 +0200 | [diff] [blame] | 3558 | http_find_header2(s->be->fwdfor_hdr_name, s->be->fwdfor_hdr_len, txn->req.sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 3559 | /* The header is set to be added only if none is present |
| 3560 | * and we found it, so don't do anything. |
| 3561 | */ |
| 3562 | } |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3563 | else if (s->req->prod->addr.from.ss_family == AF_INET) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3564 | /* Add an X-Forwarded-For header unless the source IP is |
| 3565 | * in the 'except' network range. |
| 3566 | */ |
| 3567 | if ((!s->fe->except_mask.s_addr || |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3568 | (((struct sockaddr_in *)&s->req->prod->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3569 | != s->fe->except_net.s_addr) && |
| 3570 | (!s->be->except_mask.s_addr || |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3571 | (((struct sockaddr_in *)&s->req->prod->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3572 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3573 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3574 | unsigned char *pn; |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3575 | pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.from)->sin_addr; |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3576 | |
| 3577 | /* Note: we rely on the backend to get the header name to be used for |
| 3578 | * x-forwarded-for, because the header is really meant for the backends. |
| 3579 | * However, if the backend did not specify any option, we have to rely |
| 3580 | * on the frontend's header name. |
| 3581 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3582 | if (s->be->fwdfor_hdr_len) { |
| 3583 | len = s->be->fwdfor_hdr_len; |
| 3584 | memcpy(trash, s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3585 | } else { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3586 | len = s->fe->fwdfor_hdr_len; |
| 3587 | memcpy(trash, s->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3588 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3589 | 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] | 3590 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 3591 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 3592 | &txn->hdr_idx, trash, len) < 0)) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3593 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3594 | } |
| 3595 | } |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3596 | else if (s->req->prod->addr.from.ss_family == AF_INET6) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3597 | /* FIXME: for the sake of completeness, we should also support |
| 3598 | * 'except' here, although it is mostly useless in this case. |
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 | int len; |
| 3601 | char pn[INET6_ADDRSTRLEN]; |
| 3602 | inet_ntop(AF_INET6, |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3603 | (const void *)&((struct sockaddr_in6 *)(&s->req->prod->addr.from))->sin6_addr, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3604 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3605 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3606 | /* Note: we rely on the backend to get the header name to be used for |
| 3607 | * x-forwarded-for, because the header is really meant for the backends. |
| 3608 | * However, if the backend did not specify any option, we have to rely |
| 3609 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3610 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3611 | if (s->be->fwdfor_hdr_len) { |
| 3612 | len = s->be->fwdfor_hdr_len; |
| 3613 | memcpy(trash, s->be->fwdfor_hdr_name, len); |
| 3614 | } else { |
| 3615 | len = s->fe->fwdfor_hdr_len; |
| 3616 | memcpy(trash, s->fe->fwdfor_hdr_name, len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3617 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3618 | len += sprintf(trash + len, ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3619 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3620 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 3621 | &txn->hdr_idx, trash, len) < 0)) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3622 | goto return_bad_req; |
| 3623 | } |
| 3624 | } |
| 3625 | |
| 3626 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3627 | * 10: add X-Original-To if either the frontend or the backend |
| 3628 | * asks for it. |
| 3629 | */ |
| 3630 | if ((s->fe->options | s->be->options) & PR_O_ORGTO) { |
| 3631 | |
| 3632 | /* FIXME: don't know if IPv6 can handle that case too. */ |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3633 | if (s->req->prod->addr.from.ss_family == AF_INET) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3634 | /* Add an X-Original-To header unless the destination IP is |
| 3635 | * in the 'except' network range. |
| 3636 | */ |
| 3637 | if (!(s->flags & SN_FRT_ADDR_SET)) |
| 3638 | get_frt_addr(s); |
| 3639 | |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3640 | if (s->req->prod->addr.to.ss_family == AF_INET && |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 3641 | ((!s->fe->except_mask_to.s_addr || |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3642 | (((struct sockaddr_in *)&s->req->prod->addr.to)->sin_addr.s_addr & s->fe->except_mask_to.s_addr) |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 3643 | != s->fe->except_to.s_addr) && |
| 3644 | (!s->be->except_mask_to.s_addr || |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3645 | (((struct sockaddr_in *)&s->req->prod->addr.to)->sin_addr.s_addr & s->be->except_mask_to.s_addr) |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 3646 | != s->be->except_to.s_addr))) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3647 | int len; |
| 3648 | unsigned char *pn; |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3649 | pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.to)->sin_addr; |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3650 | |
| 3651 | /* Note: we rely on the backend to get the header name to be used for |
| 3652 | * x-original-to, because the header is really meant for the backends. |
| 3653 | * However, if the backend did not specify any option, we have to rely |
| 3654 | * on the frontend's header name. |
| 3655 | */ |
| 3656 | if (s->be->orgto_hdr_len) { |
| 3657 | len = s->be->orgto_hdr_len; |
| 3658 | memcpy(trash, s->be->orgto_hdr_name, len); |
| 3659 | } else { |
| 3660 | len = s->fe->orgto_hdr_len; |
| 3661 | memcpy(trash, s->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3662 | } |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3663 | len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]); |
| 3664 | |
| 3665 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 3666 | &txn->hdr_idx, trash, len) < 0)) |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3667 | goto return_bad_req; |
| 3668 | } |
| 3669 | } |
| 3670 | } |
| 3671 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3672 | /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */ |
| 3673 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Cyril Bonté | 9ea2b9a | 2010-12-29 09:36:56 +0100 | [diff] [blame] | 3674 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3675 | unsigned int want_flags = 0; |
| 3676 | |
| 3677 | if (txn->flags & TX_REQ_VER_11) { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 3678 | if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL || |
| 3679 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) && |
| 3680 | !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3681 | want_flags |= TX_CON_CLO_SET; |
| 3682 | } else { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 3683 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
| 3684 | !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) || |
| 3685 | ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3686 | want_flags |= TX_CON_KAL_SET; |
| 3687 | } |
| 3688 | |
| 3689 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 3690 | http_change_connection_header(txn, msg, req, want_flags); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3691 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3692 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3693 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3694 | /* If we have no server assigned yet and we're balancing on url_param |
| 3695 | * with a POST request, we may be interested in checking the body for |
| 3696 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3697 | */ |
| 3698 | if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) && |
| 3699 | s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3700 | s->be->url_param_post_limit != 0 && |
Willy Tarreau | 61a21a3 | 2011-03-01 20:35:49 +0100 | [diff] [blame] | 3701 | (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK))) { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3702 | buffer_dont_connect(req); |
| 3703 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3704 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3705 | |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 3706 | if (txn->flags & TX_REQ_XFER_LEN) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3707 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 3708 | #ifdef TCP_QUICKACK |
| 3709 | /* We expect some data from the client. Unless we know for sure |
| 3710 | * we already have a full request, we have to re-enable quick-ack |
| 3711 | * in case we previously disabled it, otherwise we might cause |
| 3712 | * the client to delay further data. |
| 3713 | */ |
| 3714 | if ((s->listener->options & LI_O_NOQUICKACK) && |
| 3715 | ((txn->flags & TX_REQ_TE_CHNK) || |
| 3716 | (msg->body_len > req->l - txn->req.eoh - 2))) |
| 3717 | setsockopt(s->si[0].fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); |
| 3718 | #endif |
| 3719 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 3720 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3721 | /************************************************************* |
| 3722 | * OK, that's finished for the headers. We have done what we * |
| 3723 | * could. Let's switch to the DATA state. * |
| 3724 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3725 | req->analyse_exp = TICK_ETERNITY; |
| 3726 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3727 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3728 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3729 | /* OK let's go on with the BODY now */ |
| 3730 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3731 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3732 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3733 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 3734 | /* we detected a parsing error. We want to archive this request |
| 3735 | * in the dedicated proxy area for later troubleshooting. |
| 3736 | */ |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 3737 | 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] | 3738 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3739 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3740 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3741 | txn->status = 400; |
| 3742 | req->analysers = 0; |
| 3743 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3744 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3745 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3746 | if (s->listener->counters) |
| 3747 | s->listener->counters->failed_req++; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3748 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3749 | if (!(s->flags & SN_ERR_MASK)) |
| 3750 | s->flags |= SN_ERR_PRXCOND; |
| 3751 | if (!(s->flags & SN_FINST_MASK)) |
| 3752 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3753 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3754 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3755 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3756 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 3757 | * returns zero, at the beginning because it prevents any other processing |
| 3758 | * from occurring, and at the end because it terminates the request. |
| 3759 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3760 | 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] | 3761 | { |
| 3762 | struct http_txn *txn = &s->txn; |
| 3763 | |
| 3764 | /* This connection is being tarpitted. The CLIENT side has |
| 3765 | * already set the connect expiration date to the right |
| 3766 | * timeout. We just have to check that the client is still |
| 3767 | * there and that the timeout has not expired. |
| 3768 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3769 | buffer_dont_connect(req); |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3770 | if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 && |
| 3771 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 3772 | return 0; |
| 3773 | |
| 3774 | /* We will set the queue timer to the time spent, just for |
| 3775 | * logging purposes. We fake a 500 server error, so that the |
| 3776 | * attacker will not suspect his connection has been tarpitted. |
| 3777 | * It will not cause trouble to the logs because we can exclude |
| 3778 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 3779 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3780 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 3781 | |
| 3782 | txn->status = 500; |
| 3783 | if (req->flags != BF_READ_ERROR) |
| 3784 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500)); |
| 3785 | |
| 3786 | req->analysers = 0; |
| 3787 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3788 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3789 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3790 | if (s->listener->counters) |
| 3791 | s->listener->counters->failed_req++; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3792 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3793 | if (!(s->flags & SN_ERR_MASK)) |
| 3794 | s->flags |= SN_ERR_PRXCOND; |
| 3795 | if (!(s->flags & SN_FINST_MASK)) |
| 3796 | s->flags |= SN_FINST_T; |
| 3797 | return 0; |
| 3798 | } |
| 3799 | |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3800 | /* This function is an analyser which processes the HTTP request body. It looks |
| 3801 | * for parameters to be used for the load balancing algorithm (url_param). It |
| 3802 | * must only be called after the standard HTTP request processing has occurred, |
| 3803 | * because it expects the request to be parsed. It returns zero if it needs to |
| 3804 | * read more data, or 1 once it has completed its analysis. |
| 3805 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3806 | 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] | 3807 | { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3808 | struct http_txn *txn = &s->txn; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3809 | struct http_msg *msg = &s->txn.req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3810 | long long limit = s->be->url_param_post_limit; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3811 | |
| 3812 | /* We have to parse the HTTP request body to find any required data. |
| 3813 | * "balance url_param check_post" should have been the only way to get |
| 3814 | * into this. We were brought here after HTTP header analysis, so all |
| 3815 | * related structures are ready. |
| 3816 | */ |
| 3817 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3818 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 3819 | goto missing_data; |
| 3820 | |
| 3821 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 3822 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 3823 | * send an HTTP/1.1 100 Continue intermediate response. |
| 3824 | */ |
Willy Tarreau | 9e13c3c | 2009-12-22 09:59:58 +0100 | [diff] [blame] | 3825 | if (txn->flags & TX_REQ_VER_11) { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3826 | struct hdr_ctx ctx; |
| 3827 | ctx.idx = 0; |
| 3828 | /* Expect is allowed in 1.1, look for it */ |
| 3829 | if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) && |
| 3830 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) { |
| 3831 | buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len); |
| 3832 | } |
| 3833 | } |
| 3834 | msg->msg_state = HTTP_MSG_100_SENT; |
| 3835 | } |
| 3836 | |
| 3837 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3838 | /* we have msg->col and msg->sov which both point to the first |
| 3839 | * byte of message body. msg->som still points to the beginning |
| 3840 | * of the message. We must save the body in req->lr because it |
| 3841 | * survives buffer re-alignments. |
| 3842 | */ |
| 3843 | req->lr = req->data + msg->sov; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3844 | if (txn->flags & TX_REQ_TE_CHNK) |
| 3845 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 3846 | else |
| 3847 | msg->msg_state = HTTP_MSG_DATA; |
| 3848 | } |
| 3849 | |
| 3850 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 3851 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3852 | * set ->sov and ->lr to point to the body and switch to DATA or |
| 3853 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3854 | */ |
| 3855 | int ret = http_parse_chunk_size(req, msg); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3856 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3857 | if (!ret) |
| 3858 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3859 | else if (ret < 0) { |
| 3860 | session_inc_http_err_ctr(s); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3861 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3862 | } |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3863 | } |
| 3864 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3865 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3866 | * We have the first non-header byte in msg->col, which is either the |
| 3867 | * beginning of the chunk size or of the data. The first data byte is in |
| 3868 | * msg->sov, which is equal to msg->col when not using transfer-encoding. |
| 3869 | * 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] | 3870 | */ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3871 | |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 3872 | if (msg->body_len < limit) |
| 3873 | limit = msg->body_len; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3874 | |
Willy Tarreau | 7c96f67 | 2009-12-27 22:47:25 +0100 | [diff] [blame] | 3875 | 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] | 3876 | goto http_end; |
| 3877 | |
| 3878 | missing_data: |
| 3879 | /* we get here if we need to wait for more data */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3880 | if (req->flags & BF_FULL) { |
| 3881 | session_inc_http_err_ctr(s); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3882 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3883 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3884 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3885 | if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) { |
| 3886 | txn->status = 408; |
| 3887 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408)); |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 3888 | |
| 3889 | if (!(s->flags & SN_ERR_MASK)) |
| 3890 | s->flags |= SN_ERR_CLITO; |
| 3891 | if (!(s->flags & SN_FINST_MASK)) |
| 3892 | s->flags |= SN_FINST_D; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3893 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3894 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3895 | |
| 3896 | /* we get here if we need to wait for more data */ |
| 3897 | if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) { |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3898 | /* Not enough data. We'll re-use the http-request |
| 3899 | * timeout here. Ideally, we should set the timeout |
| 3900 | * relative to the accept() date. We just set the |
| 3901 | * request timeout once at the beginning of the |
| 3902 | * request. |
| 3903 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3904 | buffer_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3905 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 3906 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3907 | return 0; |
| 3908 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3909 | |
| 3910 | http_end: |
| 3911 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 3912 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 3913 | req->analysers &= ~an_bit; |
| 3914 | req->analyse_exp = TICK_ETERNITY; |
| 3915 | return 1; |
| 3916 | |
| 3917 | return_bad_req: /* let's centralize all bad requests */ |
| 3918 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3919 | txn->status = 400; |
| 3920 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
| 3921 | |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 3922 | if (!(s->flags & SN_ERR_MASK)) |
| 3923 | s->flags |= SN_ERR_PRXCOND; |
| 3924 | if (!(s->flags & SN_FINST_MASK)) |
| 3925 | s->flags |= SN_FINST_R; |
| 3926 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3927 | return_err_msg: |
| 3928 | req->analysers = 0; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3929 | s->fe->fe_counters.failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3930 | if (s->listener->counters) |
| 3931 | s->listener->counters->failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3932 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3933 | } |
| 3934 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3935 | int http_send_name_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, struct proxy* be, const char* srv_name) { |
| 3936 | |
| 3937 | struct hdr_ctx ctx; |
| 3938 | |
| 3939 | ctx.idx = 0; |
| 3940 | |
| 3941 | char *hdr_name = be->server_id_hdr_name; |
| 3942 | int hdr_name_len = be->server_id_hdr_len; |
| 3943 | |
| 3944 | char *hdr_val; |
| 3945 | |
| 3946 | while (http_find_header2(hdr_name, hdr_name_len, msg->sol, &txn->hdr_idx, &ctx)) { |
| 3947 | /* remove any existing values from the header */ |
| 3948 | http_remove_header2(msg, buf, &txn->hdr_idx, &ctx); |
| 3949 | } |
| 3950 | |
| 3951 | /* Add the new header requested with the server value */ |
| 3952 | hdr_val = trash; |
| 3953 | memcpy(hdr_val, hdr_name, hdr_name_len); |
| 3954 | hdr_val += hdr_name_len; |
| 3955 | *hdr_val++ = ':'; |
| 3956 | *hdr_val++ = ' '; |
| 3957 | hdr_val += strlcpy2(hdr_val, srv_name, trash + sizeof(trash) - hdr_val); |
| 3958 | http_header_add_tail2(buf, msg, &txn->hdr_idx, trash, hdr_val - trash); |
| 3959 | |
| 3960 | return 0; |
| 3961 | } |
| 3962 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3963 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 3964 | * right now but it works. |
| 3965 | */ |
| 3966 | void http_end_txn_clean_session(struct session *s) |
| 3967 | { |
| 3968 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 3969 | * server's connections. We need a safer way to reinitialize buffer |
| 3970 | * flags. We also need a more accurate method for computing per-request |
| 3971 | * data. |
| 3972 | */ |
| 3973 | http_silent_debug(__LINE__, s); |
| 3974 | |
| 3975 | s->req->cons->flags |= SI_FL_NOLINGER; |
| 3976 | s->req->cons->shutr(s->req->cons); |
| 3977 | s->req->cons->shutw(s->req->cons); |
| 3978 | |
| 3979 | http_silent_debug(__LINE__, s); |
| 3980 | |
| 3981 | if (s->flags & SN_BE_ASSIGNED) |
| 3982 | s->be->beconn--; |
| 3983 | |
| 3984 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 3985 | session_process_counters(s); |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 3986 | session_stop_backend_counters(s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3987 | |
| 3988 | if (s->txn.status) { |
| 3989 | int n; |
| 3990 | |
| 3991 | n = s->txn.status / 100; |
| 3992 | if (n < 1 || n > 5) |
| 3993 | n = 0; |
| 3994 | |
| 3995 | if (s->fe->mode == PR_MODE_HTTP) |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3996 | s->fe->fe_counters.p.http.rsp[n]++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3997 | |
Willy Tarreau | 2465779 | 2010-02-26 10:30:28 +0100 | [diff] [blame] | 3998 | if ((s->flags & SN_BE_ASSIGNED) && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3999 | (s->be->mode == PR_MODE_HTTP)) |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4000 | s->be->be_counters.p.http.rsp[n]++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4001 | } |
| 4002 | |
| 4003 | /* don't count other requests' data */ |
| 4004 | s->logs.bytes_in -= s->req->l - s->req->send_max; |
| 4005 | s->logs.bytes_out -= s->rep->l - s->rep->send_max; |
| 4006 | |
| 4007 | /* let's do a final log if we need it */ |
| 4008 | if (s->logs.logwait && |
| 4009 | !(s->flags & SN_MONITOR) && |
| 4010 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
| 4011 | s->do_log(s); |
| 4012 | } |
| 4013 | |
| 4014 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 4015 | s->logs.tv_accept = now; /* corrected date for internal use */ |
| 4016 | tv_zero(&s->logs.tv_request); |
| 4017 | s->logs.t_queue = -1; |
| 4018 | s->logs.t_connect = -1; |
| 4019 | s->logs.t_data = -1; |
| 4020 | s->logs.t_close = 0; |
| 4021 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 4022 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 4023 | |
| 4024 | s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max; |
| 4025 | s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max; |
| 4026 | |
| 4027 | if (s->pend_pos) |
| 4028 | pendconn_free(s->pend_pos); |
| 4029 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4030 | if (target_srv(&s->target)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4031 | if (s->flags & SN_CURR_SESS) { |
| 4032 | s->flags &= ~SN_CURR_SESS; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4033 | target_srv(&s->target)->cur_sess--; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4034 | } |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4035 | if (may_dequeue_tasks(target_srv(&s->target), s->be)) |
| 4036 | process_srv_queue(target_srv(&s->target)); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4037 | } |
| 4038 | |
| 4039 | if (unlikely(s->srv_conn)) |
| 4040 | sess_change_server(s, NULL); |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 4041 | clear_target(&s->target); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4042 | |
| 4043 | s->req->cons->state = s->req->cons->prev_state = SI_ST_INI; |
| 4044 | s->req->cons->fd = -1; /* just to help with debugging */ |
| 4045 | s->req->cons->err_type = SI_ET_NONE; |
Willy Tarreau | 0b3a411 | 2011-03-27 19:16:56 +0200 | [diff] [blame] | 4046 | s->req->cons->conn_retries = 0; /* used for logging too */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4047 | s->req->cons->err_loc = NULL; |
| 4048 | s->req->cons->exp = TICK_ETERNITY; |
| 4049 | s->req->cons->flags = SI_FL_NONE; |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 4050 | s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST|BF_NEVER_WAIT); |
| 4051 | 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] | 4052 | 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] | 4053 | s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE); |
| 4054 | s->txn.meth = 0; |
| 4055 | http_reset_txn(s); |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 4056 | s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ; |
Willy Tarreau | ee55dc0 | 2010-06-01 10:56:34 +0200 | [diff] [blame] | 4057 | if (s->fe->options2 & PR_O2_INDEPSTR) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4058 | s->req->cons->flags |= SI_FL_INDEP_STR; |
| 4059 | |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 4060 | if (s->fe->options2 & PR_O2_NODELAY) { |
| 4061 | s->req->flags |= BF_NEVER_WAIT; |
| 4062 | s->rep->flags |= BF_NEVER_WAIT; |
| 4063 | } |
| 4064 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4065 | /* if the request buffer is not empty, it means we're |
| 4066 | * about to process another request, so send pending |
| 4067 | * data with MSG_MORE to merge TCP packets when possible. |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 4068 | * Just don't do this if the buffer is close to be full, |
| 4069 | * because the request will wait for it to flush a little |
| 4070 | * bit before proceeding. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4071 | */ |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 4072 | if (s->req->l > s->req->send_max) { |
| 4073 | if (s->rep->send_max && |
| 4074 | !(s->rep->flags & BF_FULL) && |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 4075 | s->rep->r <= s->rep->data + s->rep->size - global.tune.maxrewrite) |
| 4076 | s->rep->flags |= BF_EXPECT_MORE; |
| 4077 | } |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4078 | |
| 4079 | /* we're removing the analysers, we MUST re-enable events detection */ |
| 4080 | buffer_auto_read(s->req); |
| 4081 | buffer_auto_close(s->req); |
| 4082 | buffer_auto_read(s->rep); |
| 4083 | buffer_auto_close(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4084 | |
| 4085 | /* make ->lr point to the first non-forwarded byte */ |
| 4086 | s->req->lr = s->req->w + s->req->send_max; |
| 4087 | if (s->req->lr >= s->req->data + s->req->size) |
| 4088 | s->req->lr -= s->req->size; |
| 4089 | s->rep->lr = s->rep->w + s->rep->send_max; |
| 4090 | if (s->rep->lr >= s->rep->data + s->rep->size) |
| 4091 | s->rep->lr -= s->req->size; |
| 4092 | |
Willy Tarreau | 342b11c | 2010-11-24 16:22:09 +0100 | [diff] [blame] | 4093 | s->req->analysers = s->listener->analysers; |
| 4094 | s->req->analysers &= ~AN_REQ_DECODE_PROXY; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4095 | s->rep->analysers = 0; |
| 4096 | |
| 4097 | http_silent_debug(__LINE__, s); |
| 4098 | } |
| 4099 | |
| 4100 | |
| 4101 | /* This function updates the request state machine according to the response |
| 4102 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 4103 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 4104 | * it is only used to find when a request/response couple is complete. Both |
| 4105 | * this function and its equivalent should loop until both return zero. It |
| 4106 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 4107 | */ |
| 4108 | int http_sync_req_state(struct session *s) |
| 4109 | { |
| 4110 | struct buffer *buf = s->req; |
| 4111 | struct http_txn *txn = &s->txn; |
| 4112 | unsigned int old_flags = buf->flags; |
| 4113 | unsigned int old_state = txn->req.msg_state; |
| 4114 | |
| 4115 | http_silent_debug(__LINE__, s); |
| 4116 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) |
| 4117 | return 0; |
| 4118 | |
| 4119 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4120 | /* No need to read anymore, the request was completely parsed. |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4121 | * We can shut the read side unless we want to abort_on_close, |
| 4122 | * or we have a POST request. The issue with POST requests is |
| 4123 | * that some browsers still send a CRLF after the request, and |
| 4124 | * this CRLF must be read so that it does not remain in the kernel |
| 4125 | * buffers, otherwise a close could cause an RST on some systems |
| 4126 | * (eg: Linux). |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4127 | */ |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4128 | 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] | 4129 | buffer_dont_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4130 | |
| 4131 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 4132 | goto wait_other_side; |
| 4133 | |
| 4134 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 4135 | /* The server has not finished to respond, so we |
| 4136 | * don't want to move in order not to upset it. |
| 4137 | */ |
| 4138 | goto wait_other_side; |
| 4139 | } |
| 4140 | |
| 4141 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 4142 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4143 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4144 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
| 4145 | goto wait_other_side; |
| 4146 | } |
| 4147 | |
| 4148 | /* When we get here, it means that both the request and the |
| 4149 | * response have finished receiving. Depending on the connection |
| 4150 | * mode, we'll have to wait for the last bytes to leave in either |
| 4151 | * direction, and sometimes for a close to be effective. |
| 4152 | */ |
| 4153 | |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4154 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 4155 | /* Server-close mode : queue a connection close to the server */ |
| 4156 | if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4157 | buffer_shutw_now(buf); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4158 | } |
| 4159 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4160 | /* Option forceclose is set, or either side wants to close, |
| 4161 | * let's enforce it now that we're not expecting any new |
| 4162 | * data to come. The caller knows the session is complete |
| 4163 | * once both states are CLOSED. |
| 4164 | */ |
| 4165 | if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4166 | buffer_shutr_now(buf); |
| 4167 | buffer_shutw_now(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4168 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4169 | } |
| 4170 | else { |
| 4171 | /* The last possible modes are keep-alive and tunnel. Since tunnel |
| 4172 | * mode does not set the body analyser, we can't reach this place |
| 4173 | * in tunnel mode, so we're left with keep-alive only. |
| 4174 | * This mode is currently not implemented, we switch to tunnel mode. |
| 4175 | */ |
| 4176 | buffer_auto_read(buf); |
| 4177 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4178 | } |
| 4179 | |
| 4180 | if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) { |
| 4181 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4182 | buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
| 4183 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4184 | if (!(buf->flags & BF_OUT_EMPTY)) { |
| 4185 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 4186 | goto http_msg_closing; |
| 4187 | } |
| 4188 | else { |
| 4189 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 4190 | goto http_msg_closed; |
| 4191 | } |
| 4192 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4193 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4194 | } |
| 4195 | |
| 4196 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 4197 | http_msg_closing: |
| 4198 | /* nothing else to forward, just waiting for the output buffer |
| 4199 | * to be empty and for the shutw_now to take effect. |
| 4200 | */ |
| 4201 | if (buf->flags & BF_OUT_EMPTY) { |
| 4202 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 4203 | goto http_msg_closed; |
| 4204 | } |
| 4205 | else if (buf->flags & BF_SHUTW) { |
| 4206 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4207 | goto wait_other_side; |
| 4208 | } |
| 4209 | } |
| 4210 | |
| 4211 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 4212 | http_msg_closed: |
| 4213 | goto wait_other_side; |
| 4214 | } |
| 4215 | |
| 4216 | wait_other_side: |
| 4217 | http_silent_debug(__LINE__, s); |
| 4218 | return txn->req.msg_state != old_state || buf->flags != old_flags; |
| 4219 | } |
| 4220 | |
| 4221 | |
| 4222 | /* This function updates the response state machine according to the request |
| 4223 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 4224 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 4225 | * it is only used to find when a request/response couple is complete. Both |
| 4226 | * this function and its equivalent should loop until both return zero. It |
| 4227 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 4228 | */ |
| 4229 | int http_sync_res_state(struct session *s) |
| 4230 | { |
| 4231 | struct buffer *buf = s->rep; |
| 4232 | struct http_txn *txn = &s->txn; |
| 4233 | unsigned int old_flags = buf->flags; |
| 4234 | unsigned int old_state = txn->rsp.msg_state; |
| 4235 | |
| 4236 | http_silent_debug(__LINE__, s); |
| 4237 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 4238 | return 0; |
| 4239 | |
| 4240 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 4241 | /* In theory, we don't need to read anymore, but we must |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4242 | * still monitor the server connection for a possible close |
| 4243 | * while the request is being uploaded, so we don't disable |
| 4244 | * reading. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4245 | */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4246 | /* buffer_dont_read(buf); */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4247 | |
| 4248 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 4249 | goto wait_other_side; |
| 4250 | |
| 4251 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 4252 | /* The client seems to still be sending data, probably |
| 4253 | * because we got an error response during an upload. |
| 4254 | * We have the choice of either breaking the connection |
| 4255 | * or letting it pass through. Let's do the later. |
| 4256 | */ |
| 4257 | goto wait_other_side; |
| 4258 | } |
| 4259 | |
| 4260 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 4261 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4262 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4263 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
| 4264 | goto wait_other_side; |
| 4265 | } |
| 4266 | |
| 4267 | /* When we get here, it means that both the request and the |
| 4268 | * response have finished receiving. Depending on the connection |
| 4269 | * mode, we'll have to wait for the last bytes to leave in either |
| 4270 | * direction, and sometimes for a close to be effective. |
| 4271 | */ |
| 4272 | |
| 4273 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 4274 | /* Server-close mode : shut read and wait for the request |
| 4275 | * side to close its output buffer. The caller will detect |
| 4276 | * when we're in DONE and the other is in CLOSED and will |
| 4277 | * catch that for the final cleanup. |
| 4278 | */ |
| 4279 | if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW))) |
| 4280 | buffer_shutr_now(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4281 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4282 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4283 | /* Option forceclose is set, or either side wants to close, |
| 4284 | * let's enforce it now that we're not expecting any new |
| 4285 | * data to come. The caller knows the session is complete |
| 4286 | * once both states are CLOSED. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4287 | */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4288 | if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) { |
| 4289 | buffer_shutr_now(buf); |
| 4290 | buffer_shutw_now(buf); |
| 4291 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4292 | } |
| 4293 | else { |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4294 | /* The last possible modes are keep-alive and tunnel. Since tunnel |
| 4295 | * mode does not set the body analyser, we can't reach this place |
| 4296 | * in tunnel mode, so we're left with keep-alive only. |
| 4297 | * This mode is currently not implemented, we switch to tunnel mode. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4298 | */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4299 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4300 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4301 | } |
| 4302 | |
| 4303 | if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) { |
| 4304 | /* if we've just closed an output, let's switch */ |
| 4305 | if (!(buf->flags & BF_OUT_EMPTY)) { |
| 4306 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 4307 | goto http_msg_closing; |
| 4308 | } |
| 4309 | else { |
| 4310 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4311 | goto http_msg_closed; |
| 4312 | } |
| 4313 | } |
| 4314 | goto wait_other_side; |
| 4315 | } |
| 4316 | |
| 4317 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 4318 | http_msg_closing: |
| 4319 | /* nothing else to forward, just waiting for the output buffer |
| 4320 | * to be empty and for the shutw_now to take effect. |
| 4321 | */ |
| 4322 | if (buf->flags & BF_OUT_EMPTY) { |
| 4323 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4324 | goto http_msg_closed; |
| 4325 | } |
| 4326 | else if (buf->flags & BF_SHUTW) { |
| 4327 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4328 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4329 | if (target_srv(&s->target)) |
| 4330 | target_srv(&s->target)->counters.cli_aborts++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4331 | goto wait_other_side; |
| 4332 | } |
| 4333 | } |
| 4334 | |
| 4335 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 4336 | http_msg_closed: |
| 4337 | /* drop any pending data */ |
| 4338 | buffer_ignore(buf, buf->l - buf->send_max); |
| 4339 | buffer_auto_close(buf); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4340 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4341 | goto wait_other_side; |
| 4342 | } |
| 4343 | |
| 4344 | wait_other_side: |
| 4345 | http_silent_debug(__LINE__, s); |
| 4346 | return txn->rsp.msg_state != old_state || buf->flags != old_flags; |
| 4347 | } |
| 4348 | |
| 4349 | |
| 4350 | /* Resync the request and response state machines. Return 1 if either state |
| 4351 | * changes. |
| 4352 | */ |
| 4353 | int http_resync_states(struct session *s) |
| 4354 | { |
| 4355 | struct http_txn *txn = &s->txn; |
| 4356 | int old_req_state = txn->req.msg_state; |
| 4357 | int old_res_state = txn->rsp.msg_state; |
| 4358 | |
| 4359 | http_silent_debug(__LINE__, s); |
| 4360 | http_sync_req_state(s); |
| 4361 | while (1) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4362 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4363 | if (!http_sync_res_state(s)) |
| 4364 | break; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4365 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4366 | if (!http_sync_req_state(s)) |
| 4367 | break; |
| 4368 | } |
| 4369 | http_silent_debug(__LINE__, s); |
| 4370 | /* OK, both state machines agree on a compatible state. |
| 4371 | * There are a few cases we're interested in : |
| 4372 | * - HTTP_MSG_TUNNEL on either means we have to disable both analysers |
| 4373 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 4374 | * directions, so let's simply disable both analysers. |
| 4375 | * - HTTP_MSG_CLOSED on the response only means we must abort the |
| 4376 | * request. |
| 4377 | * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response |
| 4378 | * with server-close mode means we've completed one request and we |
| 4379 | * must re-initialize the server connection. |
| 4380 | */ |
| 4381 | |
| 4382 | if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 4383 | txn->rsp.msg_state == HTTP_MSG_TUNNEL || |
| 4384 | (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 4385 | txn->rsp.msg_state == HTTP_MSG_CLOSED)) { |
| 4386 | s->req->analysers = 0; |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4387 | buffer_auto_close(s->req); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4388 | buffer_auto_read(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4389 | s->rep->analysers = 0; |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4390 | buffer_auto_close(s->rep); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4391 | buffer_auto_read(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4392 | } |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4393 | else if (txn->rsp.msg_state == HTTP_MSG_CLOSED || |
| 4394 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4395 | txn->req.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4396 | (s->rep->flags & BF_SHUTW)) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4397 | s->rep->analysers = 0; |
| 4398 | buffer_auto_close(s->rep); |
| 4399 | buffer_auto_read(s->rep); |
| 4400 | s->req->analysers = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4401 | buffer_abort(s->req); |
| 4402 | buffer_auto_close(s->req); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4403 | buffer_auto_read(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4404 | buffer_ignore(s->req, s->req->l - s->req->send_max); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4405 | } |
| 4406 | else if (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 4407 | txn->rsp.msg_state == HTTP_MSG_DONE && |
| 4408 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) { |
| 4409 | /* server-close: terminate this server connection and |
| 4410 | * reinitialize a fresh-new transaction. |
| 4411 | */ |
| 4412 | http_end_txn_clean_session(s); |
| 4413 | } |
| 4414 | |
| 4415 | http_silent_debug(__LINE__, s); |
| 4416 | return txn->req.msg_state != old_req_state || |
| 4417 | txn->rsp.msg_state != old_res_state; |
| 4418 | } |
| 4419 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4420 | /* This function is an analyser which forwards request body (including chunk |
| 4421 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 4422 | * zero byte. The only situation where it must not be called is when we're in |
| 4423 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 4424 | * remaining data and to resync after end of body. It expects the msg_state to |
| 4425 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 4426 | * 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] | 4427 | * 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] | 4428 | * bytes of pending data + the headers if not already done (between som and sov). |
| 4429 | * It eventually adjusts som to match sov after the data in between have been sent. |
| 4430 | */ |
| 4431 | int http_request_forward_body(struct session *s, struct buffer *req, int an_bit) |
| 4432 | { |
| 4433 | struct http_txn *txn = &s->txn; |
| 4434 | struct http_msg *msg = &s->txn.req; |
| 4435 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4436 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4437 | return 0; |
| 4438 | |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 4439 | if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) || |
| 4440 | ((req->flags & BF_SHUTW) && (req->to_forward || req->send_max))) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4441 | /* Output closed while we were sending data. We must abort and |
| 4442 | * wake the other side up. |
| 4443 | */ |
| 4444 | msg->msg_state = HTTP_MSG_ERROR; |
| 4445 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 4446 | return 1; |
| 4447 | } |
| 4448 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4449 | /* in most states, we should abort in case of early close */ |
| 4450 | buffer_auto_close(req); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4451 | |
| 4452 | /* Note that we don't have to send 100-continue back because we don't |
| 4453 | * need the data to complete our job, and it's up to the server to |
| 4454 | * decide whether to return 100, 417 or anything else in return of |
| 4455 | * an "Expect: 100-continue" header. |
| 4456 | */ |
| 4457 | |
| 4458 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 4459 | /* we have msg->col and msg->sov which both point to the first |
| 4460 | * byte of message body. msg->som still points to the beginning |
| 4461 | * of the message. We must save the body in req->lr because it |
| 4462 | * survives buffer re-alignments. |
| 4463 | */ |
| 4464 | req->lr = req->data + msg->sov; |
| 4465 | if (txn->flags & TX_REQ_TE_CHNK) |
| 4466 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 4467 | else { |
| 4468 | msg->msg_state = HTTP_MSG_DATA; |
| 4469 | } |
| 4470 | } |
| 4471 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4472 | while (1) { |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 4473 | int bytes; |
| 4474 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4475 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 4476 | /* we may have some data pending */ |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 4477 | bytes = msg->sov - msg->som; |
| 4478 | if (msg->chunk_len || bytes) { |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 4479 | msg->som = msg->sov; |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 4480 | if (likely(bytes < 0)) /* sov may have wrapped at the end */ |
| 4481 | bytes += req->size; |
| 4482 | msg->chunk_len += (unsigned int)bytes; |
| 4483 | msg->chunk_len -= buffer_forward(req, msg->chunk_len); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 4484 | } |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 4485 | |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4486 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 4487 | /* must still forward */ |
| 4488 | if (req->to_forward) |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4489 | goto missing_data; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4490 | |
| 4491 | /* nothing left to forward */ |
| 4492 | if (txn->flags & TX_REQ_TE_CHNK) |
| 4493 | msg->msg_state = HTTP_MSG_DATA_CRLF; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4494 | else |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4495 | msg->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4496 | } |
| 4497 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4498 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4499 | * set ->sov and ->lr to point to the body and switch to DATA or |
| 4500 | * TRAILERS state. |
| 4501 | */ |
| 4502 | int ret = http_parse_chunk_size(req, msg); |
| 4503 | |
| 4504 | if (!ret) |
| 4505 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4506 | else if (ret < 0) { |
| 4507 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4508 | if (msg->err_pos >= 0) |
| 4509 | 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] | 4510 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4511 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4512 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4513 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4514 | else if (msg->msg_state == HTTP_MSG_DATA_CRLF) { |
| 4515 | /* we want the CRLF after the data */ |
| 4516 | int ret; |
| 4517 | |
Willy Tarreau | d3347ee | 2010-01-04 02:02:25 +0100 | [diff] [blame] | 4518 | req->lr = req->w + req->send_max; |
| 4519 | if (req->lr >= req->data + req->size) |
| 4520 | req->lr -= req->size; |
| 4521 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4522 | ret = http_skip_chunk_crlf(req, msg); |
| 4523 | |
| 4524 | if (ret == 0) |
| 4525 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4526 | else if (ret < 0) { |
| 4527 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4528 | if (msg->err_pos >= 0) |
| 4529 | 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] | 4530 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4531 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4532 | /* we're in MSG_CHUNK_SIZE now */ |
| 4533 | } |
| 4534 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
| 4535 | int ret = http_forward_trailers(req, msg); |
| 4536 | |
| 4537 | if (ret == 0) |
| 4538 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4539 | else if (ret < 0) { |
| 4540 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4541 | if (msg->err_pos >= 0) |
| 4542 | 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] | 4543 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4544 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4545 | /* we're in HTTP_MSG_DONE now */ |
| 4546 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4547 | else { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4548 | int old_state = msg->msg_state; |
| 4549 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4550 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4551 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4552 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 4553 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
| 4554 | buffer_dont_close(req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4555 | if (http_resync_states(s)) { |
| 4556 | /* some state changes occurred, maybe the analyser |
| 4557 | * was disabled too. |
Willy Tarreau | face839 | 2010-01-03 11:37:54 +0100 | [diff] [blame] | 4558 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 4559 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 4560 | if (req->flags & BF_SHUTW) { |
| 4561 | /* request errors are most likely due to |
| 4562 | * the server aborting the transfer. |
| 4563 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4564 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 4565 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4566 | if (msg->err_pos >= 0) |
| 4567 | 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] | 4568 | goto return_bad_req; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 4569 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4570 | return 1; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4571 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 4572 | |
| 4573 | /* If "option abortonclose" is set on the backend, we |
| 4574 | * want to monitor the client's connection and forward |
| 4575 | * any shutdown notification to the server, which will |
| 4576 | * decide whether to close or to go on processing the |
| 4577 | * request. |
| 4578 | */ |
| 4579 | if (s->be->options & PR_O_ABRT_CLOSE) { |
| 4580 | buffer_auto_read(req); |
| 4581 | buffer_auto_close(req); |
| 4582 | } |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4583 | else if (s->txn.meth == HTTP_METH_POST) { |
| 4584 | /* POST requests may require to read extra CRLF |
| 4585 | * sent by broken browsers and which could cause |
| 4586 | * an RST to be sent upon close on some systems |
| 4587 | * (eg: Linux). |
| 4588 | */ |
| 4589 | buffer_auto_read(req); |
| 4590 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 4591 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4592 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4593 | } |
| 4594 | } |
| 4595 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4596 | missing_data: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4597 | /* stop waiting for data if the input is closed before the end */ |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4598 | if (req->flags & BF_SHUTR) { |
| 4599 | if (!(s->flags & SN_ERR_MASK)) |
| 4600 | s->flags |= SN_ERR_CLICL; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4601 | if (!(s->flags & SN_FINST_MASK)) { |
| 4602 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 4603 | s->flags |= SN_FINST_H; |
| 4604 | else |
| 4605 | s->flags |= SN_FINST_D; |
| 4606 | } |
| 4607 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4608 | s->fe->fe_counters.cli_aborts++; |
| 4609 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4610 | if (target_srv(&s->target)) |
| 4611 | target_srv(&s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4612 | |
| 4613 | goto return_bad_req_stats_ok; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4614 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4615 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4616 | /* waiting for the last bits to leave the buffer */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4617 | if (req->flags & BF_SHUTW) |
| 4618 | goto aborted_xfer; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4619 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4620 | /* When TE: chunked is used, we need to get there again to parse remaining |
| 4621 | * chunks even if the client has closed, so we don't want to set BF_DONTCLOSE. |
| 4622 | */ |
| 4623 | if (txn->flags & TX_REQ_TE_CHNK) |
| 4624 | buffer_dont_close(req); |
| 4625 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4626 | /* 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] | 4627 | * what we did. So we always set the BF_EXPECT_MORE flag so that the |
| 4628 | * system knows it must not set a PUSH on this first part. Interactive |
| 4629 | * modes are already handled by the stream sock layer. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4630 | */ |
Willy Tarreau | 0729303 | 2011-05-30 18:29:28 +0200 | [diff] [blame] | 4631 | req->flags |= BF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4632 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4633 | http_silent_debug(__LINE__, s); |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4634 | return 0; |
| 4635 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4636 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4637 | s->fe->fe_counters.failed_req++; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4638 | if (s->listener->counters) |
| 4639 | s->listener->counters->failed_req++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4640 | return_bad_req_stats_ok: |
| 4641 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4642 | if (txn->status) { |
| 4643 | /* Note: we don't send any error if some data were already sent */ |
| 4644 | stream_int_retnclose(req->prod, NULL); |
| 4645 | } else { |
| 4646 | txn->status = 400; |
| 4647 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
| 4648 | } |
| 4649 | req->analysers = 0; |
| 4650 | 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] | 4651 | |
| 4652 | if (!(s->flags & SN_ERR_MASK)) |
| 4653 | s->flags |= SN_ERR_PRXCOND; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4654 | if (!(s->flags & SN_FINST_MASK)) { |
| 4655 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 4656 | s->flags |= SN_FINST_H; |
| 4657 | else |
| 4658 | s->flags |= SN_FINST_D; |
| 4659 | } |
| 4660 | return 0; |
| 4661 | |
| 4662 | aborted_xfer: |
| 4663 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4664 | if (txn->status) { |
| 4665 | /* Note: we don't send any error if some data were already sent */ |
| 4666 | stream_int_retnclose(req->prod, NULL); |
| 4667 | } else { |
| 4668 | txn->status = 502; |
| 4669 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_502)); |
| 4670 | } |
| 4671 | req->analysers = 0; |
| 4672 | s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */ |
| 4673 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4674 | s->fe->fe_counters.srv_aborts++; |
| 4675 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4676 | if (target_srv(&s->target)) |
| 4677 | target_srv(&s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4678 | |
| 4679 | if (!(s->flags & SN_ERR_MASK)) |
| 4680 | s->flags |= SN_ERR_SRVCL; |
| 4681 | if (!(s->flags & SN_FINST_MASK)) { |
| 4682 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 4683 | s->flags |= SN_FINST_H; |
| 4684 | else |
| 4685 | s->flags |= SN_FINST_D; |
| 4686 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4687 | return 0; |
| 4688 | } |
| 4689 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4690 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 4691 | * processing can continue on next analysers, or zero if it either needs more |
| 4692 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
| 4693 | * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers |
| 4694 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 4695 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4696 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4697 | 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] | 4698 | { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4699 | struct http_txn *txn = &s->txn; |
| 4700 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4701 | struct hdr_ctx ctx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4702 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4703 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4704 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4705 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 4706 | 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] | 4707 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4708 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 4709 | rep, |
| 4710 | rep->rex, rep->wex, |
| 4711 | rep->flags, |
| 4712 | rep->l, |
| 4713 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 4714 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4715 | /* |
| 4716 | * Now parse the partial (or complete) lines. |
| 4717 | * We will check the response syntax, and also join multi-line |
| 4718 | * headers. An index of all the lines will be elaborated while |
| 4719 | * parsing. |
| 4720 | * |
| 4721 | * For the parsing, we use a 28 states FSM. |
| 4722 | * |
| 4723 | * Here is the information we currently have : |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4724 | * rep->data + msg->som = beginning of response |
| 4725 | * rep->data + msg->eoh = end of processed headers / start of current one |
| 4726 | * msg->eol = end of current header or line (LF or CRLF) |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4727 | * rep->lr = first non-visited byte |
| 4728 | * rep->r = end of data |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4729 | * Once we reach MSG_BODY, rep->sol = rep->data + msg->som |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4730 | */ |
| 4731 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4732 | /* There's a protected area at the end of the buffer for rewriting |
| 4733 | * purposes. We don't want to start to parse the request if the |
| 4734 | * protected area is affected, because we may have to move processed |
| 4735 | * data later, which is much more complicated. |
| 4736 | */ |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 4737 | if (rep->l && msg->msg_state < HTTP_MSG_ERROR) { |
| 4738 | if (unlikely((rep->flags & BF_FULL) || |
| 4739 | rep->r < rep->lr || |
| 4740 | rep->r > rep->data + rep->size - global.tune.maxrewrite)) { |
| 4741 | if (rep->send_max) { |
| 4742 | /* 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] | 4743 | if (rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) |
| 4744 | goto abort_response; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 4745 | buffer_dont_close(rep); |
| 4746 | rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 4747 | return 0; |
| 4748 | } |
| 4749 | if (rep->l <= rep->size - global.tune.maxrewrite) |
| 4750 | http_buffer_heavy_realign(rep, msg); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4751 | } |
| 4752 | |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 4753 | if (likely(rep->lr < rep->r)) |
| 4754 | http_msg_analyzer(rep, msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4755 | } |
| 4756 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4757 | /* 1: we might have to print this header in debug mode */ |
| 4758 | if (unlikely((global.mode & MODE_DEBUG) && |
| 4759 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | c3bfeeb | 2010-04-16 09:14:45 +0200 | [diff] [blame] | 4760 | msg->sol && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4761 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4762 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4763 | |
Willy Tarreau | 663308b | 2010-06-07 14:06:08 +0200 | [diff] [blame] | 4764 | sol = rep->data + msg->som; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 4765 | eol = sol + msg->sl.st.l; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4766 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4767 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4768 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 4769 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4770 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4771 | while (cur_idx) { |
| 4772 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 4773 | debug_hdr("srvhdr", s, sol, eol); |
| 4774 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 4775 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 4776 | } |
| 4777 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4778 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4779 | /* |
| 4780 | * Now we quickly check if we have found a full valid response. |
| 4781 | * If not so, we check the FD and buffer states before leaving. |
| 4782 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4783 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4784 | * responses are checked first. |
| 4785 | * |
| 4786 | * Depending on whether the client is still there or not, we |
| 4787 | * may send an error response back or not. Note that normally |
| 4788 | * we should only check for HTTP status there, and check I/O |
| 4789 | * errors somewhere else. |
| 4790 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4791 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4792 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4793 | /* Invalid response */ |
| 4794 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 4795 | /* we detected a parsing error. We want to archive this response |
| 4796 | * in the dedicated proxy area for later troubleshooting. |
| 4797 | */ |
| 4798 | hdr_response_bad: |
| 4799 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 4800 | 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] | 4801 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4802 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4803 | if (target_srv(&s->target)) { |
| 4804 | target_srv(&s->target)->counters.failed_resp++; |
| 4805 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4806 | } |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 4807 | abort_response: |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4808 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4809 | rep->analysers = 0; |
| 4810 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4811 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 0b89fbb | 2010-02-02 09:57:24 +0100 | [diff] [blame] | 4812 | buffer_ignore(rep, rep->l - rep->send_max); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4813 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
| 4814 | |
| 4815 | if (!(s->flags & SN_ERR_MASK)) |
| 4816 | s->flags |= SN_ERR_PRXCOND; |
| 4817 | if (!(s->flags & SN_FINST_MASK)) |
| 4818 | s->flags |= SN_FINST_H; |
| 4819 | |
| 4820 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4821 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4822 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4823 | /* too large response does not fit in buffer. */ |
| 4824 | else if (rep->flags & BF_FULL) { |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 4825 | if (msg->err_pos < 0) |
| 4826 | msg->err_pos = rep->l; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4827 | goto hdr_response_bad; |
| 4828 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4829 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4830 | /* read error */ |
| 4831 | else if (rep->flags & BF_READ_ERROR) { |
| 4832 | if (msg->err_pos >= 0) |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 4833 | 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] | 4834 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4835 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4836 | if (target_srv(&s->target)) { |
| 4837 | target_srv(&s->target)->counters.failed_resp++; |
| 4838 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_ERROR); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4839 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 4840 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4841 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4842 | rep->analysers = 0; |
| 4843 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4844 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 0b89fbb | 2010-02-02 09:57:24 +0100 | [diff] [blame] | 4845 | buffer_ignore(rep, rep->l - rep->send_max); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4846 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 4847 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4848 | if (!(s->flags & SN_ERR_MASK)) |
| 4849 | s->flags |= SN_ERR_SRVCL; |
| 4850 | if (!(s->flags & SN_FINST_MASK)) |
| 4851 | s->flags |= SN_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 4852 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4853 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4854 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4855 | /* read timeout : return a 504 to the client. */ |
| 4856 | else if (rep->flags & BF_READ_TIMEOUT) { |
| 4857 | if (msg->err_pos >= 0) |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 4858 | 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] | 4859 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4860 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4861 | if (target_srv(&s->target)) { |
| 4862 | target_srv(&s->target)->counters.failed_resp++; |
| 4863 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4864 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4865 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4866 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4867 | rep->analysers = 0; |
| 4868 | txn->status = 504; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4869 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 0b89fbb | 2010-02-02 09:57:24 +0100 | [diff] [blame] | 4870 | buffer_ignore(rep, rep->l - rep->send_max); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4871 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504)); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4872 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4873 | if (!(s->flags & SN_ERR_MASK)) |
| 4874 | s->flags |= SN_ERR_SRVTO; |
| 4875 | if (!(s->flags & SN_FINST_MASK)) |
| 4876 | s->flags |= SN_FINST_H; |
| 4877 | return 0; |
| 4878 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 4879 | |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 4880 | /* close from server, capture the response if the server has started to respond */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4881 | else if (rep->flags & BF_SHUTR) { |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 4882 | if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0) |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 4883 | 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] | 4884 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4885 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4886 | if (target_srv(&s->target)) { |
| 4887 | target_srv(&s->target)->counters.failed_resp++; |
| 4888 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4889 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4890 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4891 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4892 | rep->analysers = 0; |
| 4893 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4894 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 0b89fbb | 2010-02-02 09:57:24 +0100 | [diff] [blame] | 4895 | buffer_ignore(rep, rep->l - rep->send_max); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4896 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4897 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4898 | if (!(s->flags & SN_ERR_MASK)) |
| 4899 | s->flags |= SN_ERR_SRVCL; |
| 4900 | if (!(s->flags & SN_FINST_MASK)) |
| 4901 | s->flags |= SN_FINST_H; |
| 4902 | return 0; |
| 4903 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4904 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4905 | /* write error to client (we don't send any message then) */ |
| 4906 | else if (rep->flags & BF_WRITE_ERROR) { |
| 4907 | if (msg->err_pos >= 0) |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 4908 | 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] | 4909 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4910 | s->be->be_counters.failed_resp++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4911 | rep->analysers = 0; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4912 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4913 | |
| 4914 | if (!(s->flags & SN_ERR_MASK)) |
| 4915 | s->flags |= SN_ERR_CLICL; |
| 4916 | if (!(s->flags & SN_FINST_MASK)) |
| 4917 | s->flags |= SN_FINST_H; |
| 4918 | |
| 4919 | /* process_session() will take care of the error */ |
| 4920 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4921 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4922 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4923 | buffer_dont_close(rep); |
| 4924 | return 0; |
| 4925 | } |
| 4926 | |
| 4927 | /* More interesting part now : we know that we have a complete |
| 4928 | * response which at least looks like HTTP. We have an indicator |
| 4929 | * of each header's length, so we can parse them quickly. |
| 4930 | */ |
| 4931 | |
| 4932 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 4933 | 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] | 4934 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4935 | /* |
| 4936 | * 1: get the status code |
| 4937 | */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4938 | n = msg->sol[msg->sl.st.c] - '0'; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4939 | if (n < 1 || n > 5) |
| 4940 | n = 0; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4941 | /* when the client triggers a 4xx from the server, it's most often due |
| 4942 | * to a missing object or permission. These events should be tracked |
| 4943 | * because if they happen often, it may indicate a brute force or a |
| 4944 | * vulnerability scan. |
| 4945 | */ |
| 4946 | if (n == 4) |
| 4947 | session_inc_http_err_ctr(s); |
| 4948 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4949 | if (target_srv(&s->target)) |
| 4950 | target_srv(&s->target)->counters.p.http.rsp[n]++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4951 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4952 | /* check if the response is HTTP/1.1 or above */ |
| 4953 | if ((msg->sl.st.v_l == 8) && |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4954 | ((msg->sol[5] > '1') || |
| 4955 | ((msg->sol[5] == '1') && |
| 4956 | (msg->sol[7] >= '1')))) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4957 | txn->flags |= TX_RES_VER_11; |
| 4958 | |
| 4959 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4960 | 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] | 4961 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4962 | /* transfer length unknown*/ |
| 4963 | txn->flags &= ~TX_RES_XFER_LEN; |
| 4964 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4965 | 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] | 4966 | |
Willy Tarreau | 3965040 | 2010-03-15 19:44:39 +0100 | [diff] [blame] | 4967 | /* Adjust server's health based on status code. Note: status codes 501 |
| 4968 | * and 505 are triggered on demand by client request, so we must not |
| 4969 | * count them as server failures. |
| 4970 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4971 | if (target_srv(&s->target)) { |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 4972 | 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] | 4973 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_OK); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 4974 | else |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4975 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_STS); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 4976 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4977 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4978 | /* |
| 4979 | * 2: check for cacheability. |
| 4980 | */ |
| 4981 | |
| 4982 | switch (txn->status) { |
| 4983 | case 200: |
| 4984 | case 203: |
| 4985 | case 206: |
| 4986 | case 300: |
| 4987 | case 301: |
| 4988 | case 410: |
| 4989 | /* RFC2616 @13.4: |
| 4990 | * "A response received with a status code of |
| 4991 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 4992 | * by a cache (...) unless a cache-control |
| 4993 | * directive prohibits caching." |
| 4994 | * |
| 4995 | * RFC2616 @9.5: POST method : |
| 4996 | * "Responses to this method are not cacheable, |
| 4997 | * unless the response includes appropriate |
| 4998 | * Cache-Control or Expires header fields." |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4999 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5000 | if (likely(txn->meth != HTTP_METH_POST) && |
| 5001 | (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC))) |
| 5002 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 5003 | break; |
| 5004 | default: |
| 5005 | break; |
| 5006 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5007 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5008 | /* |
| 5009 | * 3: we may need to capture headers |
| 5010 | */ |
| 5011 | s->logs.logwait &= ~LW_RESP; |
| 5012 | if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap)) |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5013 | capture_headers(msg->sol, &txn->hdr_idx, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5014 | txn->rsp.cap, s->fe->rsp_cap); |
| 5015 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5016 | /* 4: determine the transfer-length. |
| 5017 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 5018 | * the presence of a message-body in a RESPONSE and its transfer length |
| 5019 | * must be determined that way : |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5020 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5021 | * All responses to the HEAD request method MUST NOT include a |
| 5022 | * message-body, even though the presence of entity-header fields |
| 5023 | * might lead one to believe they do. All 1xx (informational), 204 |
| 5024 | * (No Content), and 304 (Not Modified) responses MUST NOT include a |
| 5025 | * message-body. All other responses do include a message-body, |
| 5026 | * although it MAY be of zero length. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5027 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5028 | * 1. Any response which "MUST NOT" include a message-body (such as the |
| 5029 | * 1xx, 204 and 304 responses and any response to a HEAD request) is |
| 5030 | * always terminated by the first empty line after the header fields, |
| 5031 | * regardless of the entity-header fields present in the message. |
| 5032 | * |
| 5033 | * 2. If a Transfer-Encoding header field (Section 9.7) is present and |
| 5034 | * the "chunked" transfer-coding (Section 6.2) is used, the |
| 5035 | * transfer-length is defined by the use of this transfer-coding. |
| 5036 | * If a Transfer-Encoding header field is present and the "chunked" |
| 5037 | * transfer-coding is not present, the transfer-length is defined by |
| 5038 | * the sender closing the connection. |
| 5039 | * |
| 5040 | * 3. If a Content-Length header field is present, its decimal value in |
| 5041 | * OCTETs represents both the entity-length and the transfer-length. |
| 5042 | * If a message is received with both a Transfer-Encoding header |
| 5043 | * field and a Content-Length header field, the latter MUST be ignored. |
| 5044 | * |
| 5045 | * 4. If the message uses the media type "multipart/byteranges", and |
| 5046 | * the transfer-length is not otherwise specified, then this self- |
| 5047 | * delimiting media type defines the transfer-length. This media |
| 5048 | * type MUST NOT be used unless the sender knows that the recipient |
| 5049 | * can parse it; the presence in a request of a Range header with |
| 5050 | * multiple byte-range specifiers from a 1.1 client implies that the |
| 5051 | * client can parse multipart/byteranges responses. |
| 5052 | * |
| 5053 | * 5. By the server closing the connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5054 | */ |
| 5055 | |
| 5056 | /* Skip parsing if no content length is possible. The response flags |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5057 | * 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] | 5058 | * 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] | 5059 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 5060 | */ |
| 5061 | if (txn->meth == HTTP_METH_HEAD || |
| 5062 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5063 | txn->status == 204 || txn->status == 304) { |
| 5064 | txn->flags |= TX_RES_XFER_LEN; |
| 5065 | goto skip_content_length; |
| 5066 | } |
| 5067 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5068 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5069 | ctx.idx = 0; |
Willy Tarreau | 9e13c3c | 2009-12-22 09:59:58 +0100 | [diff] [blame] | 5070 | while ((txn->flags & TX_RES_VER_11) && |
| 5071 | http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5072 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
| 5073 | txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN); |
| 5074 | else if (txn->flags & TX_RES_TE_CHNK) { |
| 5075 | /* bad transfer-encoding (chunked followed by something else) */ |
| 5076 | use_close_only = 1; |
| 5077 | txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN); |
| 5078 | break; |
| 5079 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5080 | } |
| 5081 | |
| 5082 | /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */ |
| 5083 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5084 | while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only && |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5085 | http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) { |
| 5086 | signed long long cl; |
| 5087 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5088 | if (!ctx.vlen) { |
| 5089 | msg->err_pos = ctx.line + ctx.val - rep->data; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5090 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5091 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5092 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5093 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
| 5094 | msg->err_pos = ctx.line + ctx.val - rep->data; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5095 | goto hdr_response_bad; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5096 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5097 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5098 | if (cl < 0) { |
| 5099 | msg->err_pos = ctx.line + ctx.val - rep->data; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5100 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5101 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5102 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5103 | if ((txn->flags & TX_RES_CNT_LEN) && (msg->chunk_len != cl)) { |
| 5104 | msg->err_pos = ctx.line + ctx.val - rep->data; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5105 | goto hdr_response_bad; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5106 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5107 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5108 | txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5109 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5110 | } |
| 5111 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5112 | /* FIXME: we should also implement the multipart/byterange method. |
| 5113 | * For now on, we resort to close mode in this case (unknown length). |
| 5114 | */ |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5115 | skip_content_length: |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5116 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5117 | /* end of job, return OK */ |
| 5118 | rep->analysers &= ~an_bit; |
| 5119 | rep->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5120 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5121 | return 1; |
| 5122 | } |
| 5123 | |
| 5124 | /* This function performs all the processing enabled for the current response. |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 5125 | * It normally returns 1 unless it wants to break. It relies on buffers flags, |
| 5126 | * and updates t->rep->analysers. It might make sense to explode it into several |
| 5127 | * other functions. It works like process_request (see indications above). |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5128 | */ |
| 5129 | int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px) |
| 5130 | { |
| 5131 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5132 | struct http_msg *msg = &txn->rsp; |
| 5133 | struct proxy *cur_proxy; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 5134 | struct cond_wordlist *wl; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5135 | |
| 5136 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 5137 | now_ms, __FUNCTION__, |
| 5138 | t, |
| 5139 | rep, |
| 5140 | rep->rex, rep->wex, |
| 5141 | rep->flags, |
| 5142 | rep->l, |
| 5143 | rep->analysers); |
| 5144 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5145 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5146 | return 0; |
| 5147 | |
| 5148 | rep->analysers &= ~an_bit; |
| 5149 | rep->analyse_exp = TICK_ETERNITY; |
| 5150 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5151 | /* Now we have to check if we need to modify the Connection header. |
| 5152 | * This is more difficult on the response than it is on the request, |
| 5153 | * because we can have two different HTTP versions and we don't know |
| 5154 | * how the client will interprete a response. For instance, let's say |
| 5155 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 5156 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 5157 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 5158 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 5159 | * the lack of header as a keep-alive. Thus we will use two flags |
| 5160 | * indicating how a request MAY be understood by the client. In case |
| 5161 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 5162 | * ambiguous cases such as both close and keepalive are seen, then we |
| 5163 | * will fall back to explicit close. Note that we won't take risks with |
| 5164 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5165 | * See doc/internals/connection-header.txt for the complete matrix. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5166 | */ |
| 5167 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5168 | if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || |
| 5169 | txn->status == 101)) { |
| 5170 | /* Either we've established an explicit tunnel, or we're |
| 5171 | * switching the protocol. In both cases, we're very unlikely |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5172 | * to understand the next protocols. We have to switch to tunnel |
| 5173 | * mode, so that we transfer the request and responses then let |
| 5174 | * this protocol pass unmodified. When we later implement specific |
| 5175 | * parsers for such protocols, we'll want to check the Upgrade |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5176 | * header which contains information about that protocol for |
| 5177 | * responses with status 101 (eg: see RFC2817 about TLS). |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5178 | */ |
| 5179 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN; |
| 5180 | } |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5181 | else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) && |
| 5182 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN || |
| 5183 | ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5184 | int to_del = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5185 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5186 | /* on unknown transfer length, we must close */ |
| 5187 | if (!(txn->flags & TX_RES_XFER_LEN) && |
| 5188 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 5189 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5190 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5191 | /* now adjust header transformations depending on current state */ |
| 5192 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN || |
| 5193 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5194 | to_del |= 2; /* remove "keep-alive" on any response */ |
| 5195 | if (!(txn->flags & TX_RES_VER_11)) |
| 5196 | to_del |= 1; /* remove "close" for HTTP/1.0 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5197 | } |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5198 | else { /* SCL / KAL */ |
| 5199 | to_del |= 1; /* remove "close" on any response */ |
| 5200 | if ((txn->flags & (TX_RES_VER_11|TX_REQ_VER_11)) == (TX_RES_VER_11|TX_REQ_VER_11)) |
| 5201 | to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5202 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5203 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5204 | /* Parse and remove some headers from the connection header */ |
| 5205 | http_parse_connection_header(txn, msg, rep, to_del); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5206 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5207 | /* Some keep-alive responses are converted to Server-close if |
| 5208 | * the server wants to close. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5209 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5210 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) { |
| 5211 | if ((txn->flags & TX_HDR_CONN_CLO) || |
| 5212 | (txn->flags & (TX_HDR_CONN_KAL|TX_RES_VER_11)) == 0) |
| 5213 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5214 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5215 | } |
| 5216 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5217 | if (1) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5218 | /* |
| 5219 | * 3: we will have to evaluate the filters. |
| 5220 | * As opposed to version 1.2, now they will be evaluated in the |
| 5221 | * filters order and not in the header order. This means that |
| 5222 | * each filter has to be validated among all headers. |
| 5223 | * |
| 5224 | * Filters are tried with ->be first, then with ->fe if it is |
| 5225 | * different from ->be. |
| 5226 | */ |
| 5227 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5228 | cur_proxy = t->be; |
| 5229 | while (1) { |
| 5230 | struct proxy *rule_set = cur_proxy; |
| 5231 | |
| 5232 | /* try headers filters */ |
| 5233 | if (rule_set->rsp_exp != NULL) { |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 5234 | if (apply_filters_to_response(t, rep, rule_set) < 0) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5235 | return_bad_resp: |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5236 | if (target_srv(&t->target)) { |
| 5237 | target_srv(&t->target)->counters.failed_resp++; |
| 5238 | health_adjust(target_srv(&t->target), HANA_STATUS_HTTP_RSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5239 | } |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5240 | t->be->be_counters.failed_resp++; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5241 | return_srv_prx_502: |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 5242 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5243 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5244 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 0b89fbb | 2010-02-02 09:57:24 +0100 | [diff] [blame] | 5245 | buffer_ignore(rep, rep->l - rep->send_max); |
Willy Tarreau | 8e89b84 | 2009-10-18 23:56:35 +0200 | [diff] [blame] | 5246 | stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502)); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5247 | if (!(t->flags & SN_ERR_MASK)) |
| 5248 | t->flags |= SN_ERR_PRXCOND; |
| 5249 | if (!(t->flags & SN_FINST_MASK)) |
| 5250 | t->flags |= SN_FINST_H; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 5251 | return 0; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5252 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5253 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5254 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5255 | /* has the response been denied ? */ |
| 5256 | if (txn->flags & TX_SVDENY) { |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5257 | if (target_srv(&t->target)) |
| 5258 | target_srv(&t->target)->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5259 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5260 | t->be->be_counters.denied_resp++; |
| 5261 | t->fe->fe_counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5262 | if (t->listener->counters) |
| 5263 | t->listener->counters->denied_resp++; |
| 5264 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5265 | goto return_srv_prx_502; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 5266 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5267 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5268 | /* add response headers from the rule sets in the same order */ |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 5269 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 5270 | if (txn->status < 200) |
| 5271 | break; |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 5272 | if (wl->cond) { |
| 5273 | int ret = acl_exec_cond(wl->cond, px, t, txn, ACL_DIR_RTR); |
| 5274 | ret = acl_pass(ret); |
| 5275 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 5276 | ret = !ret; |
| 5277 | if (!ret) |
| 5278 | continue; |
| 5279 | } |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 5280 | 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] | 5281 | goto return_bad_resp; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 5282 | } |
| 5283 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5284 | /* check whether we're already working on the frontend */ |
| 5285 | if (cur_proxy == t->fe) |
| 5286 | break; |
| 5287 | cur_proxy = t->fe; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5288 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5289 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5290 | /* |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5291 | * We may be facing a 100-continue response, in which case this |
| 5292 | * is not the right response, and we're waiting for the next one. |
| 5293 | * 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] | 5294 | * next one. |
| 5295 | */ |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5296 | if (unlikely(txn->status == 100)) { |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5297 | hdr_idx_init(&txn->hdr_idx); |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5298 | buffer_forward(rep, rep->lr - msg->sol); |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5299 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 5300 | txn->status = 0; |
| 5301 | rep->analysers |= AN_RES_WAIT_HTTP | an_bit; |
| 5302 | return 1; |
| 5303 | } |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5304 | else if (unlikely(txn->status < 200)) |
| 5305 | goto skip_header_mangling; |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5306 | |
| 5307 | /* we don't have any 1xx status code now */ |
| 5308 | |
| 5309 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5310 | * 4: check for server cookie. |
| 5311 | */ |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5312 | if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name || |
| 5313 | (t->be->options & PR_O_CHK_CACHE)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5314 | manage_server_side_cookies(t, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5315 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5316 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5317 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5318 | * 5: check for cache-control or pragma headers if required. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5319 | */ |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5320 | 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] | 5321 | check_response_for_cacheability(t, rep); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5322 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5323 | /* |
| 5324 | * 6: add server cookie in the response if needed |
| 5325 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5326 | if (target_srv(&t->target) && (t->be->options & PR_O_COOK_INS) && |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 5327 | !((txn->flags & TX_SCK_FOUND) && (t->be->options2 & PR_O2_COOK_PSV)) && |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5328 | (!(t->flags & SN_DIRECT) || |
| 5329 | ((t->be->cookie_maxidle || txn->cookie_last_date) && |
| 5330 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 5331 | (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 5332 | (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 5333 | (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST)) && |
| 5334 | !(t->flags & SN_IGNORE_PRST)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5335 | int len; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5336 | /* the server is known, it's not the one the client requested, or the |
| 5337 | * cookie's last seen date needs to be refreshed. We have to |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5338 | * insert a set-cookie here, except if we want to insert only on POST |
| 5339 | * requests and this one isn't. Note that servers which don't have cookies |
| 5340 | * (eg: some backup servers) will return a full cookie removal request. |
| 5341 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5342 | if (!target_srv(&t->target)->cookie) { |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5343 | len = sprintf(trash, |
| 5344 | "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
| 5345 | t->be->cookie_name); |
| 5346 | } |
| 5347 | else { |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5348 | 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] | 5349 | |
| 5350 | if (t->be->cookie_maxidle || t->be->cookie_maxlife) { |
| 5351 | /* emit last_date, which is mandatory */ |
| 5352 | trash[len++] = COOKIE_DELIM_DATE; |
| 5353 | s30tob64((date.tv_sec+3) >> 2, trash + len); len += 5; |
| 5354 | if (t->be->cookie_maxlife) { |
| 5355 | /* emit first_date, which is either the original one or |
| 5356 | * the current date. |
| 5357 | */ |
| 5358 | trash[len++] = COOKIE_DELIM_DATE; |
| 5359 | s30tob64(txn->cookie_first_date ? |
| 5360 | txn->cookie_first_date >> 2 : |
| 5361 | (date.tv_sec+3) >> 2, trash + len); |
| 5362 | len += 5; |
| 5363 | } |
| 5364 | } |
| 5365 | len += sprintf(trash + len, "; path=/"); |
| 5366 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5367 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5368 | if (t->be->cookie_domain) |
| 5369 | len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5370 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5371 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 5372 | trash, len) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5373 | goto return_bad_resp; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5374 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 5375 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5376 | if (target_srv(&t->target)->cookie && (t->flags & SN_DIRECT)) |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5377 | /* the server did not change, only the date was updated */ |
| 5378 | txn->flags |= TX_SCK_UPDATED; |
| 5379 | else |
| 5380 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5381 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5382 | /* Here, we will tell an eventual cache on the client side that we don't |
| 5383 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 5384 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 5385 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 5386 | */ |
| 5387 | if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5388 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5389 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 5390 | |
| 5391 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 5392 | "Cache-control: private", 22) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5393 | goto return_bad_resp; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5394 | } |
| 5395 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5396 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5397 | /* |
| 5398 | * 7: check if result will be cacheable with a cookie. |
| 5399 | * We'll block the response if security checks have caught |
| 5400 | * nasty things such as a cacheable cookie. |
| 5401 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 5402 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 5403 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5404 | (t->be->options & PR_O_CHK_CACHE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5405 | |
| 5406 | /* we're in presence of a cacheable response containing |
| 5407 | * a set-cookie header. We'll block it as requested by |
| 5408 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5409 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5410 | if (target_srv(&t->target)) |
| 5411 | target_srv(&t->target)->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5412 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5413 | t->be->be_counters.denied_resp++; |
| 5414 | t->fe->fe_counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5415 | if (t->listener->counters) |
| 5416 | t->listener->counters->denied_resp++; |
| 5417 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5418 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5419 | 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] | 5420 | send_log(t->be, LOG_ALERT, |
| 5421 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5422 | 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] | 5423 | goto return_srv_prx_502; |
| 5424 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5425 | |
| 5426 | /* |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5427 | * 8: adjust "Connection: close" or "Connection: keep-alive" if needed. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5428 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5429 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
| 5430 | ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) { |
| 5431 | unsigned int want_flags = 0; |
| 5432 | |
| 5433 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5434 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5435 | /* we want a keep-alive response here. Keep-alive header |
| 5436 | * required if either side is not 1.1. |
| 5437 | */ |
| 5438 | if ((txn->flags & (TX_REQ_VER_11|TX_RES_VER_11)) != (TX_REQ_VER_11|TX_RES_VER_11)) |
| 5439 | want_flags |= TX_CON_KAL_SET; |
| 5440 | } |
| 5441 | else { |
| 5442 | /* we want a close response here. Close header required if |
| 5443 | * the server is 1.1, regardless of the client. |
| 5444 | */ |
| 5445 | if (txn->flags & TX_RES_VER_11) |
| 5446 | want_flags |= TX_CON_CLO_SET; |
| 5447 | } |
| 5448 | |
| 5449 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 5450 | http_change_connection_header(txn, msg, rep, want_flags); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5451 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5452 | |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5453 | skip_header_mangling: |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5454 | if ((txn->flags & TX_RES_XFER_LEN) || |
| 5455 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5456 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 5457 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5458 | /************************************************************* |
| 5459 | * OK, that's finished for the headers. We have done what we * |
| 5460 | * could. Let's switch to the DATA state. * |
| 5461 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5462 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5463 | t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5464 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5465 | /* if the user wants to log as soon as possible, without counting |
| 5466 | * bytes from the server, then this is the right moment. We have |
| 5467 | * to temporarily assign bytes_out to log what we currently have. |
| 5468 | */ |
| 5469 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 5470 | t->logs.t_close = t->logs.t_data; /* to get a valid end date */ |
| 5471 | t->logs.bytes_out = txn->rsp.eoh; |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 5472 | t->do_log(t); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5473 | t->logs.bytes_out = 0; |
| 5474 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5475 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5476 | /* Note: we must not try to cheat by jumping directly to DATA, |
| 5477 | * otherwise we would not let the client side wake up. |
| 5478 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5479 | |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 5480 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5481 | } |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 5482 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5483 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5484 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5485 | /* This function is an analyser which forwards response body (including chunk |
| 5486 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 5487 | * zero byte. The only situation where it must not be called is when we're in |
| 5488 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 5489 | * remaining data and to resync after end of body. It expects the msg_state to |
| 5490 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 5491 | * 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] | 5492 | * 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] | 5493 | * bytes of pending data + the headers if not already done (between som and sov). |
| 5494 | * It eventually adjusts som to match sov after the data in between have been sent. |
| 5495 | */ |
| 5496 | int http_response_forward_body(struct session *s, struct buffer *res, int an_bit) |
| 5497 | { |
| 5498 | struct http_txn *txn = &s->txn; |
| 5499 | struct http_msg *msg = &s->txn.rsp; |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5500 | int bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5501 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5502 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 5503 | return 0; |
| 5504 | |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 5505 | 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] | 5506 | ((res->flags & BF_SHUTW) && (res->to_forward || res->send_max)) || |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 5507 | !s->req->analysers) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5508 | /* Output closed while we were sending data. We must abort and |
| 5509 | * wake the other side up. |
| 5510 | */ |
| 5511 | msg->msg_state = HTTP_MSG_ERROR; |
| 5512 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 5513 | return 1; |
| 5514 | } |
| 5515 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5516 | /* in most states, we should abort in case of early close */ |
| 5517 | buffer_auto_close(res); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5518 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5519 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 5520 | /* we have msg->col and msg->sov which both point to the first |
| 5521 | * byte of message body. msg->som still points to the beginning |
| 5522 | * of the message. We must save the body in req->lr because it |
| 5523 | * survives buffer re-alignments. |
| 5524 | */ |
| 5525 | res->lr = res->data + msg->sov; |
| 5526 | if (txn->flags & TX_RES_TE_CHNK) |
| 5527 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 5528 | else { |
| 5529 | msg->msg_state = HTTP_MSG_DATA; |
| 5530 | } |
| 5531 | } |
| 5532 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5533 | while (1) { |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5534 | int bytes; |
| 5535 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5536 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 5537 | /* we may have some data pending */ |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5538 | bytes = msg->sov - msg->som; |
| 5539 | if (msg->chunk_len || bytes) { |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 5540 | msg->som = msg->sov; |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5541 | if (likely(bytes < 0)) /* sov may have wrapped at the end */ |
| 5542 | bytes += res->size; |
| 5543 | msg->chunk_len += (unsigned int)bytes; |
| 5544 | msg->chunk_len -= buffer_forward(res, msg->chunk_len); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 5545 | } |
| 5546 | |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5547 | |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5548 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 5549 | /* must still forward */ |
| 5550 | if (res->to_forward) |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5551 | goto missing_data; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5552 | |
| 5553 | /* nothing left to forward */ |
| 5554 | if (txn->flags & TX_RES_TE_CHNK) |
| 5555 | msg->msg_state = HTTP_MSG_DATA_CRLF; |
| 5556 | else |
| 5557 | msg->msg_state = HTTP_MSG_DONE; |
| 5558 | } |
| 5559 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5560 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5561 | * set ->sov to point to the body and switch to DATA or TRAILERS state. |
| 5562 | */ |
| 5563 | int ret = http_parse_chunk_size(res, msg); |
| 5564 | |
| 5565 | if (!ret) |
| 5566 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5567 | else if (ret < 0) { |
| 5568 | if (msg->err_pos >= 0) |
| 5569 | 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] | 5570 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5571 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5572 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5573 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5574 | else if (msg->msg_state == HTTP_MSG_DATA_CRLF) { |
| 5575 | /* we want the CRLF after the data */ |
| 5576 | int ret; |
| 5577 | |
Willy Tarreau | d3347ee | 2010-01-04 02:02:25 +0100 | [diff] [blame] | 5578 | res->lr = res->w + res->send_max; |
| 5579 | if (res->lr >= res->data + res->size) |
| 5580 | res->lr -= res->size; |
| 5581 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5582 | ret = http_skip_chunk_crlf(res, msg); |
| 5583 | |
| 5584 | if (!ret) |
| 5585 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5586 | else if (ret < 0) { |
| 5587 | if (msg->err_pos >= 0) |
| 5588 | 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] | 5589 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5590 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5591 | /* we're in MSG_CHUNK_SIZE now */ |
| 5592 | } |
| 5593 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
| 5594 | int ret = http_forward_trailers(res, msg); |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5595 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5596 | if (ret == 0) |
| 5597 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5598 | else if (ret < 0) { |
| 5599 | if (msg->err_pos >= 0) |
| 5600 | 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] | 5601 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5602 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5603 | /* we're in HTTP_MSG_DONE now */ |
| 5604 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5605 | else { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5606 | int old_state = msg->msg_state; |
| 5607 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5608 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5609 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5610 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5611 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
| 5612 | buffer_dont_close(res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5613 | if (http_resync_states(s)) { |
| 5614 | http_silent_debug(__LINE__, s); |
| 5615 | /* some state changes occurred, maybe the analyser |
| 5616 | * was disabled too. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5617 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5618 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 5619 | if (res->flags & BF_SHUTW) { |
| 5620 | /* response errors are most likely due to |
| 5621 | * the client aborting the transfer. |
| 5622 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5623 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5624 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5625 | if (msg->err_pos >= 0) |
| 5626 | 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] | 5627 | goto return_bad_res; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5628 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5629 | return 1; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5630 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5631 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5632 | } |
| 5633 | } |
| 5634 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5635 | missing_data: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5636 | /* stop waiting for data if the input is closed before the end */ |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5637 | if (res->flags & BF_SHUTR) { |
| 5638 | if (!(s->flags & SN_ERR_MASK)) |
| 5639 | s->flags |= SN_ERR_SRVCL; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5640 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5641 | if (target_srv(&s->target)) |
| 5642 | target_srv(&s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5643 | goto return_bad_res_stats_ok; |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5644 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5645 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5646 | if (res->flags & BF_SHUTW) |
| 5647 | goto aborted_xfer; |
| 5648 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5649 | /* 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] | 5650 | if (!s->req->analysers) |
| 5651 | goto return_bad_res; |
| 5652 | |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5653 | /* forward any pending data */ |
| 5654 | bytes = msg->sov - msg->som; |
| 5655 | if (msg->chunk_len || bytes) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5656 | msg->som = msg->sov; |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5657 | if (likely(bytes < 0)) /* sov may have wrapped at the end */ |
| 5658 | bytes += res->size; |
| 5659 | msg->chunk_len += (unsigned int)bytes; |
| 5660 | msg->chunk_len -= buffer_forward(res, msg->chunk_len); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5661 | } |
| 5662 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5663 | /* When TE: chunked is used, we need to get there again to parse remaining |
| 5664 | * chunks even if the server has closed, so we don't want to set BF_DONTCLOSE. |
| 5665 | * Similarly, with keep-alive on the client side, we don't want to forward a |
| 5666 | * close. |
| 5667 | */ |
| 5668 | if ((txn->flags & TX_RES_TE_CHNK) || |
| 5669 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5670 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
| 5671 | buffer_dont_close(res); |
| 5672 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5673 | /* 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] | 5674 | * what we did. So we always set the BF_EXPECT_MORE flag so that the |
| 5675 | * system knows it must not set a PUSH on this first part. Interactive |
| 5676 | * modes are already handled by the stream sock layer. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5677 | */ |
Willy Tarreau | 0729303 | 2011-05-30 18:29:28 +0200 | [diff] [blame] | 5678 | res->flags |= BF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5679 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5680 | /* the session handler will take care of timeouts and errors */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5681 | http_silent_debug(__LINE__, s); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5682 | return 0; |
| 5683 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5684 | return_bad_res: /* let's centralize all bad responses */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5685 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5686 | if (target_srv(&s->target)) |
| 5687 | target_srv(&s->target)->counters.failed_resp++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5688 | |
| 5689 | return_bad_res_stats_ok: |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5690 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 5691 | /* don't send any error message as we're in the body */ |
| 5692 | stream_int_retnclose(res->cons, NULL); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5693 | res->analysers = 0; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5694 | 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] | 5695 | if (target_srv(&s->target)) |
| 5696 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5697 | |
| 5698 | if (!(s->flags & SN_ERR_MASK)) |
| 5699 | s->flags |= SN_ERR_PRXCOND; |
| 5700 | if (!(s->flags & SN_FINST_MASK)) |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5701 | s->flags |= SN_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5702 | return 0; |
| 5703 | |
| 5704 | aborted_xfer: |
| 5705 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 5706 | /* don't send any error message as we're in the body */ |
| 5707 | stream_int_retnclose(res->cons, NULL); |
| 5708 | res->analysers = 0; |
| 5709 | s->req->analysers = 0; /* we're in data phase, we want to abort both directions */ |
| 5710 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5711 | s->fe->fe_counters.cli_aborts++; |
| 5712 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5713 | if (target_srv(&s->target)) |
| 5714 | target_srv(&s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5715 | |
| 5716 | if (!(s->flags & SN_ERR_MASK)) |
| 5717 | s->flags |= SN_ERR_CLICL; |
| 5718 | if (!(s->flags & SN_FINST_MASK)) |
| 5719 | s->flags |= SN_FINST_D; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5720 | return 0; |
| 5721 | } |
| 5722 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5723 | /* Iterate the same filter through all request headers. |
| 5724 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5725 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 5726 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5727 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5728 | 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] | 5729 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5730 | char term; |
| 5731 | char *cur_ptr, *cur_end, *cur_next; |
| 5732 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5733 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5734 | struct hdr_idx_elem *cur_hdr; |
| 5735 | int len, delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 5736 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5737 | last_hdr = 0; |
| 5738 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5739 | cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5740 | old_idx = 0; |
| 5741 | |
| 5742 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5743 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5744 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5745 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5746 | (exp->action == ACT_ALLOW || |
| 5747 | exp->action == ACT_DENY || |
| 5748 | exp->action == ACT_TARPIT)) |
| 5749 | return 0; |
| 5750 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5751 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5752 | if (!cur_idx) |
| 5753 | break; |
| 5754 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5755 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5756 | cur_ptr = cur_next; |
| 5757 | cur_end = cur_ptr + cur_hdr->len; |
| 5758 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5759 | |
| 5760 | /* Now we have one header between cur_ptr and cur_end, |
| 5761 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5762 | */ |
| 5763 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5764 | /* The annoying part is that pattern matching needs |
| 5765 | * that we modify the contents to null-terminate all |
| 5766 | * strings before testing them. |
| 5767 | */ |
| 5768 | |
| 5769 | term = *cur_end; |
| 5770 | *cur_end = '\0'; |
| 5771 | |
| 5772 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5773 | switch (exp->action) { |
| 5774 | case ACT_SETBE: |
| 5775 | /* It is not possible to jump a second time. |
| 5776 | * FIXME: should we return an HTTP/500 here so that |
| 5777 | * the admin knows there's a problem ? |
| 5778 | */ |
| 5779 | if (t->be != t->fe) |
| 5780 | break; |
| 5781 | |
| 5782 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 5783 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5784 | last_hdr = 1; |
| 5785 | break; |
| 5786 | |
| 5787 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5788 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5789 | last_hdr = 1; |
| 5790 | break; |
| 5791 | |
| 5792 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5793 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5794 | last_hdr = 1; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5795 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5796 | t->fe->fe_counters.denied_req++; |
| 5797 | if (t->fe != t->be) |
| 5798 | t->be->be_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5799 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 5800 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5801 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5802 | break; |
| 5803 | |
| 5804 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5805 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5806 | last_hdr = 1; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5807 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5808 | t->fe->fe_counters.denied_req++; |
| 5809 | if (t->fe != t->be) |
| 5810 | t->be->be_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5811 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 5812 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5813 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5814 | break; |
| 5815 | |
| 5816 | case ACT_REPLACE: |
| 5817 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 5818 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 5819 | /* FIXME: if the user adds a newline in the replacement, the |
| 5820 | * index will not be recalculated for now, and the new line |
| 5821 | * will not be counted as a new header. |
| 5822 | */ |
| 5823 | |
| 5824 | cur_end += delta; |
| 5825 | cur_next += delta; |
| 5826 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5827 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5828 | break; |
| 5829 | |
| 5830 | case ACT_REMOVE: |
| 5831 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
| 5832 | cur_next += delta; |
| 5833 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5834 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5835 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 5836 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5837 | cur_hdr->len = 0; |
| 5838 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 5839 | cur_idx = old_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5840 | break; |
| 5841 | |
| 5842 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5843 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5844 | if (cur_end) |
| 5845 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5846 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5847 | /* keep the link from this header to next one in case of later |
| 5848 | * removal of next header. |
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 | old_idx = cur_idx; |
| 5851 | } |
| 5852 | return 0; |
| 5853 | } |
| 5854 | |
| 5855 | |
| 5856 | /* Apply the filter to the request line. |
| 5857 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 5858 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5859 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 5860 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5861 | */ |
| 5862 | int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 5863 | { |
| 5864 | char term; |
| 5865 | char *cur_ptr, *cur_end; |
| 5866 | int done; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5867 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5868 | int len, delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5869 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5870 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5871 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5872 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5873 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5874 | (exp->action == ACT_ALLOW || |
| 5875 | exp->action == ACT_DENY || |
| 5876 | exp->action == ACT_TARPIT)) |
| 5877 | return 0; |
| 5878 | else if (exp->action == ACT_REMOVE) |
| 5879 | return 0; |
| 5880 | |
| 5881 | done = 0; |
| 5882 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5883 | cur_ptr = txn->req.sol; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5884 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5885 | |
| 5886 | /* Now we have the request line between cur_ptr and cur_end */ |
| 5887 | |
| 5888 | /* The annoying part is that pattern matching needs |
| 5889 | * that we modify the contents to null-terminate all |
| 5890 | * strings before testing them. |
| 5891 | */ |
| 5892 | |
| 5893 | term = *cur_end; |
| 5894 | *cur_end = '\0'; |
| 5895 | |
| 5896 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5897 | switch (exp->action) { |
| 5898 | case ACT_SETBE: |
| 5899 | /* It is not possible to jump a second time. |
| 5900 | * FIXME: should we return an HTTP/500 here so that |
| 5901 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5902 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5903 | if (t->be != t->fe) |
| 5904 | break; |
| 5905 | |
| 5906 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 5907 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5908 | done = 1; |
| 5909 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5910 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5911 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5912 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5913 | done = 1; |
| 5914 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5915 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5916 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5917 | txn->flags |= TX_CLDENY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5918 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5919 | t->fe->fe_counters.denied_req++; |
| 5920 | if (t->fe != t->be) |
| 5921 | t->be->be_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5922 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 5923 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5924 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5925 | done = 1; |
| 5926 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5927 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5928 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5929 | txn->flags |= TX_CLTARPIT; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5930 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5931 | t->fe->fe_counters.denied_req++; |
| 5932 | if (t->fe != t->be) |
| 5933 | t->be->be_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5934 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 5935 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5936 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5937 | done = 1; |
| 5938 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5939 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5940 | case ACT_REPLACE: |
| 5941 | *cur_end = term; /* restore the string terminator */ |
| 5942 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 5943 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 5944 | /* FIXME: if the user adds a newline in the replacement, the |
| 5945 | * index will not be recalculated for now, and the new line |
| 5946 | * will not be counted as a new header. |
| 5947 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5948 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5949 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5950 | cur_end += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5951 | cur_end = (char *)http_parse_reqline(&txn->req, req->data, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5952 | HTTP_MSG_RQMETH, |
| 5953 | cur_ptr, cur_end + 1, |
| 5954 | NULL, NULL); |
| 5955 | if (unlikely(!cur_end)) |
| 5956 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5957 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5958 | /* we have a full request and we know that we have either a CR |
| 5959 | * or an LF at <ptr>. |
| 5960 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5961 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 5962 | 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] | 5963 | /* there is no point trying this regex on headers */ |
| 5964 | return 1; |
| 5965 | } |
| 5966 | } |
| 5967 | *cur_end = term; /* restore the string terminator */ |
| 5968 | return done; |
| 5969 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 5970 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5971 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5972 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5973 | /* |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5974 | * 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] | 5975 | * 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] | 5976 | * unparsable request. Since it can manage the switch to another backend, it |
| 5977 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5978 | */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5979 | 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] | 5980 | { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5981 | struct http_txn *txn = &s->txn; |
| 5982 | struct hdr_exp *exp; |
| 5983 | |
| 5984 | for (exp = px->req_exp; exp; exp = exp->next) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5985 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5986 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5987 | /* |
| 5988 | * The interleaving of transformations and verdicts |
| 5989 | * makes it difficult to decide to continue or stop |
| 5990 | * the evaluation. |
| 5991 | */ |
| 5992 | |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5993 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) |
| 5994 | break; |
| 5995 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5996 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5997 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5998 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5999 | continue; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6000 | |
| 6001 | /* if this filter had a condition, evaluate it now and skip to |
| 6002 | * next filter if the condition does not match. |
| 6003 | */ |
| 6004 | if (exp->cond) { |
| 6005 | ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_REQ); |
| 6006 | ret = acl_pass(ret); |
| 6007 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 6008 | ret = !ret; |
| 6009 | |
| 6010 | if (!ret) |
| 6011 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6012 | } |
| 6013 | |
| 6014 | /* Apply the filter to the request line. */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6015 | ret = apply_filter_to_req_line(s, req, exp); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6016 | if (unlikely(ret < 0)) |
| 6017 | return -1; |
| 6018 | |
| 6019 | if (likely(ret == 0)) { |
| 6020 | /* The filter did not match the request, it can be |
| 6021 | * iterated through all headers. |
| 6022 | */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6023 | apply_filter_to_req_headers(s, req, exp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6024 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6025 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6026 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6027 | } |
| 6028 | |
| 6029 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6030 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6031 | /* |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6032 | * Try to retrieve the server associated to the appsession. |
| 6033 | * If the server is found, it's assigned to the session. |
| 6034 | */ |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6035 | 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] | 6036 | struct http_txn *txn = &t->txn; |
| 6037 | appsess *asession = NULL; |
| 6038 | char *sessid_temp = NULL; |
| 6039 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6040 | if (len > t->be->appsession_len) { |
| 6041 | len = t->be->appsession_len; |
| 6042 | } |
| 6043 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6044 | if (t->be->options2 & PR_O2_AS_REQL) { |
| 6045 | /* 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] | 6046 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6047 | /* 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] | 6048 | pool_free2(apools.sessid, txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6049 | } |
| 6050 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6051 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6052 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 6053 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 6054 | return; |
| 6055 | } |
| 6056 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6057 | memcpy(txn->sessid, buf, len); |
| 6058 | txn->sessid[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6059 | } |
| 6060 | |
| 6061 | if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) { |
| 6062 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 6063 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 6064 | return; |
| 6065 | } |
| 6066 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6067 | memcpy(sessid_temp, buf, len); |
| 6068 | sessid_temp[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6069 | |
| 6070 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp); |
| 6071 | /* free previously allocated memory */ |
| 6072 | pool_free2(apools.sessid, sessid_temp); |
| 6073 | |
| 6074 | if (asession != NULL) { |
| 6075 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 6076 | if (!(t->be->options2 & PR_O2_AS_REQL)) |
| 6077 | asession->request_count++; |
| 6078 | |
| 6079 | if (asession->serverid != NULL) { |
| 6080 | struct server *srv = t->be->srv; |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 6081 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6082 | while (srv) { |
| 6083 | if (strcmp(srv->id, asession->serverid) == 0) { |
Willy Tarreau | 4de9149 | 2010-01-22 19:10:05 +0100 | [diff] [blame] | 6084 | if ((srv->state & SRV_RUNNING) || |
| 6085 | (t->be->options & PR_O_PERSIST) || |
| 6086 | (t->flags & SN_FORCE_PRST)) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6087 | /* we found the server and it's usable */ |
| 6088 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | 2a6d88d | 2010-01-24 13:10:43 +0100 | [diff] [blame] | 6089 | txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6090 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 6091 | set_target_server(&t->target, srv); |
Willy Tarreau | 664beb8 | 2011-03-10 11:38:29 +0100 | [diff] [blame] | 6092 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6093 | break; |
| 6094 | } else { |
| 6095 | txn->flags &= ~TX_CK_MASK; |
| 6096 | txn->flags |= TX_CK_DOWN; |
| 6097 | } |
| 6098 | } |
| 6099 | srv = srv->next; |
| 6100 | } |
| 6101 | } |
| 6102 | } |
| 6103 | } |
| 6104 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6105 | /* Find the end of a cookie value contained between <s> and <e>. It works the |
| 6106 | * same way as with headers above except that the semi-colon also ends a token. |
| 6107 | * See RFC2965 for more information. Note that it requires a valid header to |
| 6108 | * return a valid result. |
| 6109 | */ |
| 6110 | char *find_cookie_value_end(char *s, const char *e) |
| 6111 | { |
| 6112 | int quoted, qdpair; |
| 6113 | |
| 6114 | quoted = qdpair = 0; |
| 6115 | for (; s < e; s++) { |
| 6116 | if (qdpair) qdpair = 0; |
| 6117 | else if (quoted) { |
| 6118 | if (*s == '\\') qdpair = 1; |
| 6119 | else if (*s == '"') quoted = 0; |
| 6120 | } |
| 6121 | else if (*s == '"') quoted = 1; |
| 6122 | else if (*s == ',' || *s == ';') return s; |
| 6123 | } |
| 6124 | return s; |
| 6125 | } |
| 6126 | |
| 6127 | /* Delete a value in a header between delimiters <from> and <next> in buffer |
| 6128 | * <buf>. The number of characters displaced is returned, and the pointer to |
| 6129 | * the first delimiter is updated if required. The function tries as much as |
| 6130 | * possible to respect the following principles : |
| 6131 | * - replace <from> delimiter by the <next> one unless <from> points to a |
| 6132 | * colon, in which case <next> is simply removed |
| 6133 | * - set exactly one space character after the new first delimiter, unless |
| 6134 | * there are not enough characters in the block being moved to do so. |
| 6135 | * - remove unneeded spaces before the previous delimiter and after the new |
| 6136 | * one. |
| 6137 | * |
| 6138 | * It is the caller's responsibility to ensure that : |
| 6139 | * - <from> points to a valid delimiter or the colon ; |
| 6140 | * - <next> points to a valid delimiter or the final CR/LF ; |
| 6141 | * - there are non-space chars before <from> ; |
| 6142 | * - there is a CR/LF at or after <next>. |
| 6143 | */ |
| 6144 | int del_hdr_value(struct buffer *buf, char **from, char *next) |
| 6145 | { |
| 6146 | char *prev = *from; |
| 6147 | |
| 6148 | if (*prev == ':') { |
| 6149 | /* We're removing the first value, preserve the colon and add a |
| 6150 | * space if possible. |
| 6151 | */ |
| 6152 | if (!http_is_crlf[(unsigned char)*next]) |
| 6153 | next++; |
| 6154 | prev++; |
| 6155 | if (prev < next) |
| 6156 | *prev++ = ' '; |
| 6157 | |
| 6158 | while (http_is_spht[(unsigned char)*next]) |
| 6159 | next++; |
| 6160 | } else { |
| 6161 | /* Remove useless spaces before the old delimiter. */ |
| 6162 | while (http_is_spht[(unsigned char)*(prev-1)]) |
| 6163 | prev--; |
| 6164 | *from = prev; |
| 6165 | |
| 6166 | /* copy the delimiter and if possible a space if we're |
| 6167 | * not at the end of the line. |
| 6168 | */ |
| 6169 | if (!http_is_crlf[(unsigned char)*next]) { |
| 6170 | *prev++ = *next++; |
| 6171 | if (prev + 1 < next) |
| 6172 | *prev++ = ' '; |
| 6173 | while (http_is_spht[(unsigned char)*next]) |
| 6174 | next++; |
| 6175 | } |
| 6176 | } |
| 6177 | return buffer_replace2(buf, prev, next, NULL, 0); |
| 6178 | } |
| 6179 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6180 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 6181 | * 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] | 6182 | * desirable to call it only when needed. This code is quite complex because |
| 6183 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 6184 | * highly recommended not to touch this part without a good reason ! |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6185 | */ |
| 6186 | void manage_client_side_cookies(struct session *t, struct buffer *req) |
| 6187 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6188 | struct http_txn *txn = &t->txn; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6189 | int preserve_hdr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6190 | int cur_idx, old_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6191 | char *hdr_beg, *hdr_end, *hdr_next, *del_from; |
| 6192 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6193 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6194 | /* Iterate through the headers, we start with the start line. */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 6195 | old_idx = 0; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6196 | hdr_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6197 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6198 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6199 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6200 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6201 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6202 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6203 | hdr_beg = hdr_next; |
| 6204 | hdr_end = hdr_beg + cur_hdr->len; |
| 6205 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6206 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6207 | /* We have one full header between hdr_beg and hdr_end, and the |
| 6208 | * next header starts at hdr_next. We're only interested in |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6209 | * "Cookie:" headers. |
| 6210 | */ |
| 6211 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6212 | val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6213 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6214 | old_idx = cur_idx; |
| 6215 | continue; |
| 6216 | } |
| 6217 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6218 | del_from = NULL; /* nothing to be deleted */ |
| 6219 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 6220 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6221 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 6222 | * attributes whose name begin with a '$', and associate them with |
| 6223 | * the right cookie, if we want to delete this cookie. |
| 6224 | * So there are 3 cases for each cookie read : |
| 6225 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 6226 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 6227 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 6228 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 6229 | * "special" cookie. |
| 6230 | * At the end of loop, if a "special" cookie remains, we may have to |
| 6231 | * remove it. If no application cookie persists in the header, we |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6232 | * *MUST* delete it. |
| 6233 | * |
| 6234 | * Note: RFC2965 is unclear about the processing of spaces around |
| 6235 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 6236 | * the RFC explicitly allows spaces before it, and not within the |
| 6237 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 6238 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 6239 | * after the equal sign too, resulting in some (rare) buggy |
| 6240 | * implementations trying to do that. So let's do what servers do. |
| 6241 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 6242 | * allowed quoted strings in values, with any possible character |
| 6243 | * after a backslash, including control chars and delimitors, which |
| 6244 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 6245 | * within values even without quotes. |
| 6246 | * |
| 6247 | * We have to keep multiple pointers in order to support cookie |
| 6248 | * removal at the beginning, middle or end of header without |
| 6249 | * corrupting the header. All of these headers are valid : |
| 6250 | * |
| 6251 | * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n |
| 6252 | * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n |
| 6253 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 6254 | * | | | | | | | | | |
| 6255 | * | | | | | | | | hdr_end <--+ |
| 6256 | * | | | | | | | +--> next |
| 6257 | * | | | | | | +----> val_end |
| 6258 | * | | | | | +-----------> val_beg |
| 6259 | * | | | | +--------------> equal |
| 6260 | * | | | +----------------> att_end |
| 6261 | * | | +---------------------> att_beg |
| 6262 | * | +--------------------------> prev |
| 6263 | * +--------------------------------> hdr_beg |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6264 | */ |
| 6265 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6266 | for (prev = hdr_beg + 6; prev < hdr_end; prev = next) { |
| 6267 | /* Iterate through all cookies on this line */ |
| 6268 | |
| 6269 | /* find att_beg */ |
| 6270 | att_beg = prev + 1; |
| 6271 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 6272 | att_beg++; |
| 6273 | |
| 6274 | /* find att_end : this is the first character after the last non |
| 6275 | * space before the equal. It may be equal to hdr_end. |
| 6276 | */ |
| 6277 | equal = att_end = att_beg; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6278 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6279 | while (equal < hdr_end) { |
| 6280 | if (*equal == '=' || *equal == ',' || *equal == ';') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6281 | break; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6282 | if (http_is_spht[(unsigned char)*equal++]) |
| 6283 | continue; |
| 6284 | att_end = equal; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6285 | } |
| 6286 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6287 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 6288 | * is between <att_beg> and <equal>, both may be identical. |
| 6289 | */ |
| 6290 | |
| 6291 | /* look for end of cookie if there is an equal sign */ |
| 6292 | if (equal < hdr_end && *equal == '=') { |
| 6293 | /* look for the beginning of the value */ |
| 6294 | val_beg = equal + 1; |
| 6295 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 6296 | val_beg++; |
| 6297 | |
| 6298 | /* find the end of the value, respecting quotes */ |
| 6299 | next = find_cookie_value_end(val_beg, hdr_end); |
| 6300 | |
| 6301 | /* make val_end point to the first white space or delimitor after the value */ |
| 6302 | val_end = next; |
| 6303 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 6304 | val_end--; |
| 6305 | } else { |
| 6306 | val_beg = val_end = next = equal; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6307 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6308 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6309 | /* We have nothing to do with attributes beginning with '$'. However, |
| 6310 | * they will automatically be removed if a header before them is removed, |
| 6311 | * since they're supposed to be linked together. |
| 6312 | */ |
| 6313 | if (*att_beg == '$') |
| 6314 | continue; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6315 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6316 | /* Ignore cookies with no equal sign */ |
| 6317 | if (equal == next) { |
| 6318 | /* This is not our cookie, so we must preserve it. But if we already |
| 6319 | * scheduled another cookie for removal, we cannot remove the |
| 6320 | * complete header, but we can remove the previous block itself. |
| 6321 | */ |
| 6322 | preserve_hdr = 1; |
| 6323 | if (del_from != NULL) { |
| 6324 | int delta = del_hdr_value(req, &del_from, prev); |
| 6325 | val_end += delta; |
| 6326 | next += delta; |
| 6327 | hdr_end += delta; |
| 6328 | hdr_next += delta; |
| 6329 | cur_hdr->len += delta; |
| 6330 | http_msg_move_end(&txn->req, delta); |
| 6331 | prev = del_from; |
| 6332 | del_from = NULL; |
| 6333 | } |
| 6334 | continue; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6335 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6336 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6337 | /* if there are spaces around the equal sign, we need to |
| 6338 | * strip them otherwise we'll get trouble for cookie captures, |
| 6339 | * or even for rewrites. Since this happens extremely rarely, |
| 6340 | * it does not hurt performance. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6341 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6342 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 6343 | int stripped_before = 0; |
| 6344 | int stripped_after = 0; |
| 6345 | |
| 6346 | if (att_end != equal) { |
| 6347 | stripped_before = buffer_replace2(req, att_end, equal, NULL, 0); |
| 6348 | equal += stripped_before; |
| 6349 | val_beg += stripped_before; |
| 6350 | } |
| 6351 | |
| 6352 | if (val_beg > equal + 1) { |
| 6353 | stripped_after = buffer_replace2(req, equal + 1, val_beg, NULL, 0); |
| 6354 | val_beg += stripped_after; |
| 6355 | stripped_before += stripped_after; |
| 6356 | } |
| 6357 | |
| 6358 | val_end += stripped_before; |
| 6359 | next += stripped_before; |
| 6360 | hdr_end += stripped_before; |
| 6361 | hdr_next += stripped_before; |
| 6362 | cur_hdr->len += stripped_before; |
| 6363 | http_msg_move_end(&txn->req, stripped_before); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6364 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6365 | /* now everything is as on the diagram above */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6366 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6367 | /* First, let's see if we want to capture this cookie. We check |
| 6368 | * that we don't already have a client side cookie, because we |
| 6369 | * can only capture one. Also as an optimisation, we ignore |
| 6370 | * cookies shorter than the declared name. |
| 6371 | */ |
| 6372 | if (t->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 6373 | (val_end - att_beg >= t->fe->capture_namelen) && |
| 6374 | memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
| 6375 | int log_len = val_end - att_beg; |
| 6376 | |
| 6377 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
| 6378 | Alert("HTTP logging : out of memory.\n"); |
| 6379 | } else { |
| 6380 | if (log_len > t->fe->capture_len) |
| 6381 | log_len = t->fe->capture_len; |
| 6382 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 6383 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6384 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6385 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6386 | |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6387 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 6388 | * following form : |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6389 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6390 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6391 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6392 | * For cookies in prefix mode, the form is : |
| 6393 | * |
| 6394 | * Cookie: NAME=SRV~VALUE |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6395 | */ |
| 6396 | if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 6397 | (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) { |
| 6398 | struct server *srv = t->be->srv; |
| 6399 | char *delim; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6400 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6401 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 6402 | * have the server ID between val_beg and delim, and the original cookie between |
| 6403 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 6404 | * |
| 6405 | * Cookie: NAME=SRV; # in all but prefix modes |
| 6406 | * Cookie: NAME=SRV~OPAQUE ; # in prefix mode |
| 6407 | * | || || | |+-> next |
| 6408 | * | || || | +--> val_end |
| 6409 | * | || || +---------> delim |
| 6410 | * | || |+------------> val_beg |
| 6411 | * | || +-------------> att_end = equal |
| 6412 | * | |+-----------------> att_beg |
| 6413 | * | +------------------> prev |
| 6414 | * +-------------------------> hdr_beg |
| 6415 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6416 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6417 | if (t->be->options & PR_O_COOK_PFX) { |
| 6418 | for (delim = val_beg; delim < val_end; delim++) |
| 6419 | if (*delim == COOKIE_DELIM) |
| 6420 | break; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6421 | } else { |
| 6422 | char *vbar1; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6423 | delim = val_end; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6424 | /* Now check if the cookie contains a date field, which would |
| 6425 | * appear after a vertical bar ('|') just after the server name |
| 6426 | * and before the delimiter. |
| 6427 | */ |
| 6428 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 6429 | if (vbar1) { |
| 6430 | /* OK, so left of the bar is the server's cookie and |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6431 | * right is the last seen date. It is a base64 encoded |
| 6432 | * 30-bit value representing the UNIX date since the |
| 6433 | * epoch in 4-second quantities. |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6434 | */ |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6435 | int val; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6436 | delim = vbar1++; |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6437 | if (val_end - vbar1 >= 5) { |
| 6438 | val = b64tos30(vbar1); |
| 6439 | if (val > 0) |
| 6440 | txn->cookie_last_date = val << 2; |
| 6441 | } |
| 6442 | /* look for a second vertical bar */ |
| 6443 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 6444 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 6445 | val = b64tos30(vbar1 + 1); |
| 6446 | if (val > 0) |
| 6447 | txn->cookie_first_date = val << 2; |
| 6448 | } |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6449 | } |
| 6450 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6451 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6452 | /* if the cookie has an expiration date and the proxy wants to check |
| 6453 | * it, then we do that now. We first check if the cookie is too old, |
| 6454 | * then only if it has expired. We detect strict overflow because the |
| 6455 | * time resolution here is not great (4 seconds). Cookies with dates |
| 6456 | * in the future are ignored if their offset is beyond one day. This |
| 6457 | * allows an admin to fix timezone issues without expiring everyone |
| 6458 | * and at the same time avoids keeping unwanted side effects for too |
| 6459 | * long. |
| 6460 | */ |
| 6461 | if (txn->cookie_first_date && t->be->cookie_maxlife && |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6462 | (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) || |
| 6463 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6464 | txn->flags &= ~TX_CK_MASK; |
| 6465 | txn->flags |= TX_CK_OLD; |
| 6466 | delim = val_beg; // let's pretend we have not found the cookie |
| 6467 | txn->cookie_first_date = 0; |
| 6468 | txn->cookie_last_date = 0; |
| 6469 | } |
| 6470 | else if (txn->cookie_last_date && t->be->cookie_maxidle && |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6471 | (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) || |
| 6472 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6473 | txn->flags &= ~TX_CK_MASK; |
| 6474 | txn->flags |= TX_CK_EXPIRED; |
| 6475 | delim = val_beg; // let's pretend we have not found the cookie |
| 6476 | txn->cookie_first_date = 0; |
| 6477 | txn->cookie_last_date = 0; |
| 6478 | } |
| 6479 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6480 | /* Here, we'll look for the first running server which supports the cookie. |
| 6481 | * This allows to share a same cookie between several servers, for example |
| 6482 | * to dedicate backup servers to specific servers only. |
| 6483 | * However, to prevent clients from sticking to cookie-less backup server |
| 6484 | * when they have incidentely learned an empty cookie, we simply ignore |
| 6485 | * empty cookies and mark them as invalid. |
| 6486 | * The same behaviour is applied when persistence must be ignored. |
| 6487 | */ |
| 6488 | if ((delim == val_beg) || (t->flags & SN_IGNORE_PRST)) |
| 6489 | srv = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6490 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6491 | while (srv) { |
| 6492 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 6493 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
| 6494 | if ((srv->state & SRV_RUNNING) || |
| 6495 | (t->be->options & PR_O_PERSIST) || |
| 6496 | (t->flags & SN_FORCE_PRST)) { |
| 6497 | /* we found the server and we can use it */ |
| 6498 | txn->flags &= ~TX_CK_MASK; |
| 6499 | txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN; |
| 6500 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 6501 | set_target_server(&t->target, srv); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6502 | break; |
| 6503 | } else { |
| 6504 | /* we found a server, but it's down, |
| 6505 | * mark it as such and go on in case |
| 6506 | * another one is available. |
| 6507 | */ |
| 6508 | txn->flags &= ~TX_CK_MASK; |
| 6509 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6510 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6511 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6512 | srv = srv->next; |
| 6513 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6514 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6515 | 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] | 6516 | /* no server matched this cookie */ |
| 6517 | txn->flags &= ~TX_CK_MASK; |
| 6518 | txn->flags |= TX_CK_INVALID; |
| 6519 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6520 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6521 | /* depending on the cookie mode, we may have to either : |
| 6522 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 6523 | * the server never sees it ; |
| 6524 | * - remove the server id from the cookie value, and tag the cookie as an |
| 6525 | * application cookie so that it does not get accidentely removed later, |
| 6526 | * if we're in cookie prefix mode |
| 6527 | */ |
| 6528 | if ((t->be->options & PR_O_COOK_PFX) && (delim != val_end)) { |
| 6529 | int delta; /* negative */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6530 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6531 | delta = buffer_replace2(req, val_beg, delim + 1, NULL, 0); |
| 6532 | val_end += delta; |
| 6533 | next += delta; |
| 6534 | hdr_end += delta; |
| 6535 | hdr_next += delta; |
| 6536 | cur_hdr->len += delta; |
| 6537 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6538 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6539 | del_from = NULL; |
| 6540 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 6541 | } |
| 6542 | else if (del_from == NULL && |
| 6543 | (t->be->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) { |
| 6544 | del_from = prev; |
| 6545 | } |
| 6546 | } else { |
| 6547 | /* This is not our cookie, so we must preserve it. But if we already |
| 6548 | * scheduled another cookie for removal, we cannot remove the |
| 6549 | * complete header, but we can remove the previous block itself. |
| 6550 | */ |
| 6551 | preserve_hdr = 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6552 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6553 | if (del_from != NULL) { |
| 6554 | int delta = del_hdr_value(req, &del_from, prev); |
Willy Tarreau | b810554 | 2010-11-24 18:31:28 +0100 | [diff] [blame] | 6555 | if (att_beg >= del_from) |
| 6556 | att_beg += delta; |
| 6557 | if (att_end >= del_from) |
| 6558 | att_end += delta; |
| 6559 | val_beg += delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6560 | val_end += delta; |
| 6561 | next += delta; |
| 6562 | hdr_end += delta; |
| 6563 | hdr_next += delta; |
| 6564 | cur_hdr->len += delta; |
| 6565 | http_msg_move_end(&txn->req, delta); |
| 6566 | prev = del_from; |
| 6567 | del_from = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6568 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6569 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6570 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6571 | /* Look for the appsession cookie unless persistence must be ignored */ |
| 6572 | if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) { |
| 6573 | int cmp_len, value_len; |
| 6574 | char *value_begin; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 6575 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6576 | if (t->be->options2 & PR_O2_AS_PFX) { |
| 6577 | cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len); |
| 6578 | value_begin = att_beg + t->be->appsession_name_len; |
| 6579 | value_len = val_end - att_beg - t->be->appsession_name_len; |
| 6580 | } else { |
| 6581 | cmp_len = att_end - att_beg; |
| 6582 | value_begin = val_beg; |
| 6583 | value_len = val_end - val_beg; |
| 6584 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6585 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6586 | /* let's see if the cookie is our appcookie */ |
| 6587 | if (cmp_len == t->be->appsession_name_len && |
| 6588 | memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) { |
| 6589 | manage_client_side_appsession(t, value_begin, value_len); |
| 6590 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6591 | } |
| 6592 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6593 | /* continue with next cookie on this header line */ |
| 6594 | att_beg = next; |
| 6595 | } /* for each cookie */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6596 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6597 | /* There are no more cookies on this line. |
| 6598 | * We may still have one (or several) marked for deletion at the |
| 6599 | * end of the line. We must do this now in two ways : |
| 6600 | * - if some cookies must be preserved, we only delete from the |
| 6601 | * mark to the end of line ; |
| 6602 | * - if nothing needs to be preserved, simply delete the whole header |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6603 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6604 | if (del_from) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6605 | int delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6606 | if (preserve_hdr) { |
| 6607 | delta = del_hdr_value(req, &del_from, hdr_end); |
| 6608 | hdr_end = del_from; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6609 | cur_hdr->len += delta; |
| 6610 | } else { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6611 | delta = buffer_replace2(req, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6612 | |
| 6613 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6614 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6615 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6616 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6617 | cur_idx = old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6618 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6619 | hdr_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6620 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6621 | } |
| 6622 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6623 | /* check next header */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6624 | old_idx = cur_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6625 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6626 | } |
| 6627 | |
| 6628 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6629 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 6630 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 6631 | */ |
| 6632 | int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 6633 | { |
| 6634 | char term; |
| 6635 | char *cur_ptr, *cur_end, *cur_next; |
| 6636 | int cur_idx, old_idx, last_hdr; |
| 6637 | struct http_txn *txn = &t->txn; |
| 6638 | struct hdr_idx_elem *cur_hdr; |
| 6639 | int len, delta; |
| 6640 | |
| 6641 | last_hdr = 0; |
| 6642 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6643 | cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6644 | old_idx = 0; |
| 6645 | |
| 6646 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6647 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6648 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6649 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6650 | (exp->action == ACT_ALLOW || |
| 6651 | exp->action == ACT_DENY)) |
| 6652 | return 0; |
| 6653 | |
| 6654 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 6655 | if (!cur_idx) |
| 6656 | break; |
| 6657 | |
| 6658 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 6659 | cur_ptr = cur_next; |
| 6660 | cur_end = cur_ptr + cur_hdr->len; |
| 6661 | cur_next = cur_end + cur_hdr->cr + 1; |
| 6662 | |
| 6663 | /* Now we have one header between cur_ptr and cur_end, |
| 6664 | * and the next header starts at cur_next. |
| 6665 | */ |
| 6666 | |
| 6667 | /* The annoying part is that pattern matching needs |
| 6668 | * that we modify the contents to null-terminate all |
| 6669 | * strings before testing them. |
| 6670 | */ |
| 6671 | |
| 6672 | term = *cur_end; |
| 6673 | *cur_end = '\0'; |
| 6674 | |
| 6675 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 6676 | switch (exp->action) { |
| 6677 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6678 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6679 | last_hdr = 1; |
| 6680 | break; |
| 6681 | |
| 6682 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6683 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6684 | last_hdr = 1; |
| 6685 | break; |
| 6686 | |
| 6687 | case ACT_REPLACE: |
| 6688 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 6689 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 6690 | /* FIXME: if the user adds a newline in the replacement, the |
| 6691 | * index will not be recalculated for now, and the new line |
| 6692 | * will not be counted as a new header. |
| 6693 | */ |
| 6694 | |
| 6695 | cur_end += delta; |
| 6696 | cur_next += delta; |
| 6697 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6698 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6699 | break; |
| 6700 | |
| 6701 | case ACT_REMOVE: |
| 6702 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 6703 | cur_next += delta; |
| 6704 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6705 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6706 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6707 | txn->hdr_idx.used--; |
| 6708 | cur_hdr->len = 0; |
| 6709 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6710 | cur_idx = old_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6711 | break; |
| 6712 | |
| 6713 | } |
| 6714 | } |
| 6715 | if (cur_end) |
| 6716 | *cur_end = term; /* restore the string terminator */ |
| 6717 | |
| 6718 | /* keep the link from this header to next one in case of later |
| 6719 | * removal of next header. |
| 6720 | */ |
| 6721 | old_idx = cur_idx; |
| 6722 | } |
| 6723 | return 0; |
| 6724 | } |
| 6725 | |
| 6726 | |
| 6727 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 6728 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 6729 | * or -1 if a replacement resulted in an invalid status line. |
| 6730 | */ |
| 6731 | int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 6732 | { |
| 6733 | char term; |
| 6734 | char *cur_ptr, *cur_end; |
| 6735 | int done; |
| 6736 | struct http_txn *txn = &t->txn; |
| 6737 | int len, delta; |
| 6738 | |
| 6739 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6740 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6741 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6742 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6743 | (exp->action == ACT_ALLOW || |
| 6744 | exp->action == ACT_DENY)) |
| 6745 | return 0; |
| 6746 | else if (exp->action == ACT_REMOVE) |
| 6747 | return 0; |
| 6748 | |
| 6749 | done = 0; |
| 6750 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6751 | cur_ptr = txn->rsp.sol; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 6752 | cur_end = cur_ptr + txn->rsp.sl.st.l; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6753 | |
| 6754 | /* Now we have the status line between cur_ptr and cur_end */ |
| 6755 | |
| 6756 | /* The annoying part is that pattern matching needs |
| 6757 | * that we modify the contents to null-terminate all |
| 6758 | * strings before testing them. |
| 6759 | */ |
| 6760 | |
| 6761 | term = *cur_end; |
| 6762 | *cur_end = '\0'; |
| 6763 | |
| 6764 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 6765 | switch (exp->action) { |
| 6766 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6767 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6768 | done = 1; |
| 6769 | break; |
| 6770 | |
| 6771 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6772 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6773 | done = 1; |
| 6774 | break; |
| 6775 | |
| 6776 | case ACT_REPLACE: |
| 6777 | *cur_end = term; /* restore the string terminator */ |
| 6778 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 6779 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 6780 | /* FIXME: if the user adds a newline in the replacement, the |
| 6781 | * index will not be recalculated for now, and the new line |
| 6782 | * will not be counted as a new header. |
| 6783 | */ |
| 6784 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6785 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6786 | cur_end += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6787 | cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 6788 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6789 | cur_ptr, cur_end + 1, |
| 6790 | NULL, NULL); |
| 6791 | if (unlikely(!cur_end)) |
| 6792 | return -1; |
| 6793 | |
| 6794 | /* we have a full respnse and we know that we have either a CR |
| 6795 | * or an LF at <ptr>. |
| 6796 | */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6797 | 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] | 6798 | 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] | 6799 | /* there is no point trying this regex on headers */ |
| 6800 | return 1; |
| 6801 | } |
| 6802 | } |
| 6803 | *cur_end = term; /* restore the string terminator */ |
| 6804 | return done; |
| 6805 | } |
| 6806 | |
| 6807 | |
| 6808 | |
| 6809 | /* |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6810 | * 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] | 6811 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 6812 | * unparsable response. |
| 6813 | */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6814 | 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] | 6815 | { |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6816 | struct http_txn *txn = &s->txn; |
| 6817 | struct hdr_exp *exp; |
| 6818 | |
| 6819 | for (exp = px->rsp_exp; exp; exp = exp->next) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6820 | int ret; |
| 6821 | |
| 6822 | /* |
| 6823 | * The interleaving of transformations and verdicts |
| 6824 | * makes it difficult to decide to continue or stop |
| 6825 | * the evaluation. |
| 6826 | */ |
| 6827 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6828 | if (txn->flags & TX_SVDENY) |
| 6829 | break; |
| 6830 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6831 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6832 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 6833 | exp->action == ACT_PASS)) { |
| 6834 | exp = exp->next; |
| 6835 | continue; |
| 6836 | } |
| 6837 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6838 | /* if this filter had a condition, evaluate it now and skip to |
| 6839 | * next filter if the condition does not match. |
| 6840 | */ |
| 6841 | if (exp->cond) { |
| 6842 | ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_RTR); |
| 6843 | ret = acl_pass(ret); |
| 6844 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 6845 | ret = !ret; |
| 6846 | if (!ret) |
| 6847 | continue; |
| 6848 | } |
| 6849 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6850 | /* Apply the filter to the status line. */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6851 | ret = apply_filter_to_sts_line(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6852 | if (unlikely(ret < 0)) |
| 6853 | return -1; |
| 6854 | |
| 6855 | if (likely(ret == 0)) { |
| 6856 | /* The filter did not match the response, it can be |
| 6857 | * iterated through all headers. |
| 6858 | */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6859 | apply_filter_to_resp_headers(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6860 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6861 | } |
| 6862 | return 0; |
| 6863 | } |
| 6864 | |
| 6865 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6866 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 6867 | * 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] | 6868 | * desirable to call it only when needed. This function is also used when we |
| 6869 | * 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] | 6870 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6871 | void manage_server_side_cookies(struct session *t, struct buffer *res) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6872 | { |
| 6873 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6874 | struct server *srv; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6875 | int is_cookie2; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6876 | int cur_idx, old_idx, delta; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6877 | char *hdr_beg, *hdr_end, *hdr_next; |
| 6878 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6879 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6880 | /* Iterate through the headers. |
| 6881 | * we start with the start line. |
| 6882 | */ |
| 6883 | old_idx = 0; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6884 | hdr_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6885 | |
| 6886 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 6887 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6888 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6889 | |
| 6890 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6891 | hdr_beg = hdr_next; |
| 6892 | hdr_end = hdr_beg + cur_hdr->len; |
| 6893 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6894 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6895 | /* We have one full header between hdr_beg and hdr_end, and the |
| 6896 | * next header starts at hdr_next. We're only interested in |
| 6897 | * "Set-Cookie" and "Set-Cookie2" headers. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6898 | */ |
| 6899 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6900 | is_cookie2 = 0; |
| 6901 | prev = hdr_beg + 10; |
| 6902 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6903 | if (!val) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6904 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11); |
| 6905 | if (!val) { |
| 6906 | old_idx = cur_idx; |
| 6907 | continue; |
| 6908 | } |
| 6909 | is_cookie2 = 1; |
| 6910 | prev = hdr_beg + 11; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6911 | } |
| 6912 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6913 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 6914 | * <prev> points to the colon. |
| 6915 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 6916 | txn->flags |= TX_SCK_PRESENT; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6917 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6918 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 6919 | * check-cache is enabled) and we are not interested in checking |
| 6920 | * them. Warning, the cookie capture is declared in the frontend. |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 6921 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6922 | if (t->be->cookie_name == NULL && |
| 6923 | t->be->appsession_name == NULL && |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 6924 | t->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6925 | return; |
| 6926 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6927 | /* OK so now we know we have to process this response cookie. |
| 6928 | * The format of the Set-Cookie header is slightly different |
| 6929 | * from the format of the Cookie header in that it does not |
| 6930 | * support the comma as a cookie delimiter (thus the header |
| 6931 | * cannot be folded) because the Expires attribute described in |
| 6932 | * the original Netscape's spec may contain an unquoted date |
| 6933 | * with a comma inside. We have to live with this because |
| 6934 | * many browsers don't support Max-Age and some browsers don't |
| 6935 | * support quoted strings. However the Set-Cookie2 header is |
| 6936 | * clean. |
| 6937 | * |
| 6938 | * We have to keep multiple pointers in order to support cookie |
| 6939 | * removal at the beginning, middle or end of header without |
| 6940 | * corrupting the header (in case of set-cookie2). A special |
| 6941 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 6942 | * fields after the first semi-colon. The <next> pointer points |
| 6943 | * either to the end of line (set-cookie) or next unquoted comma |
| 6944 | * (set-cookie2). All of these headers are valid : |
| 6945 | * |
| 6946 | * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n |
| 6947 | * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 6948 | * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 6949 | * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n |
| 6950 | * | | | | | | | | | | |
| 6951 | * | | | | | | | | +-> next hdr_end <--+ |
| 6952 | * | | | | | | | +------------> scav |
| 6953 | * | | | | | | +--------------> val_end |
| 6954 | * | | | | | +--------------------> val_beg |
| 6955 | * | | | | +----------------------> equal |
| 6956 | * | | | +------------------------> att_end |
| 6957 | * | | +----------------------------> att_beg |
| 6958 | * | +------------------------------> prev |
| 6959 | * +-----------------------------------------> hdr_beg |
| 6960 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6961 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6962 | for (; prev < hdr_end; prev = next) { |
| 6963 | /* Iterate through all cookies on this line */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6964 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6965 | /* find att_beg */ |
| 6966 | att_beg = prev + 1; |
| 6967 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 6968 | att_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6969 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6970 | /* find att_end : this is the first character after the last non |
| 6971 | * space before the equal. It may be equal to hdr_end. |
| 6972 | */ |
| 6973 | equal = att_end = att_beg; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6974 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6975 | while (equal < hdr_end) { |
| 6976 | if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ',')) |
| 6977 | break; |
| 6978 | if (http_is_spht[(unsigned char)*equal++]) |
| 6979 | continue; |
| 6980 | att_end = equal; |
| 6981 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6982 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6983 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 6984 | * is between <att_beg> and <equal>, both may be identical. |
| 6985 | */ |
| 6986 | |
| 6987 | /* look for end of cookie if there is an equal sign */ |
| 6988 | if (equal < hdr_end && *equal == '=') { |
| 6989 | /* look for the beginning of the value */ |
| 6990 | val_beg = equal + 1; |
| 6991 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 6992 | val_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6993 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6994 | /* find the end of the value, respecting quotes */ |
| 6995 | next = find_cookie_value_end(val_beg, hdr_end); |
| 6996 | |
| 6997 | /* make val_end point to the first white space or delimitor after the value */ |
| 6998 | val_end = next; |
| 6999 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 7000 | val_end--; |
| 7001 | } else { |
| 7002 | /* <equal> points to next comma, semi-colon or EOL */ |
| 7003 | val_beg = val_end = next = equal; |
| 7004 | } |
| 7005 | |
| 7006 | if (next < hdr_end) { |
| 7007 | /* Set-Cookie2 supports multiple cookies, and <next> points to |
| 7008 | * a colon or semi-colon before the end. So skip all attr-value |
| 7009 | * pairs and look for the next comma. For Set-Cookie, since |
| 7010 | * commas are permitted in values, skip to the end. |
| 7011 | */ |
| 7012 | if (is_cookie2) |
| 7013 | next = find_hdr_value_end(next, hdr_end); |
| 7014 | else |
| 7015 | next = hdr_end; |
| 7016 | } |
| 7017 | |
| 7018 | /* Now everything is as on the diagram above */ |
| 7019 | |
| 7020 | /* Ignore cookies with no equal sign */ |
| 7021 | if (equal == val_end) |
| 7022 | continue; |
| 7023 | |
| 7024 | /* If there are spaces around the equal sign, we need to |
| 7025 | * strip them otherwise we'll get trouble for cookie captures, |
| 7026 | * or even for rewrites. Since this happens extremely rarely, |
| 7027 | * it does not hurt performance. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7028 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7029 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 7030 | int stripped_before = 0; |
| 7031 | int stripped_after = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7032 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7033 | if (att_end != equal) { |
| 7034 | stripped_before = buffer_replace2(res, att_end, equal, NULL, 0); |
| 7035 | equal += stripped_before; |
| 7036 | val_beg += stripped_before; |
| 7037 | } |
| 7038 | |
| 7039 | if (val_beg > equal + 1) { |
| 7040 | stripped_after = buffer_replace2(res, equal + 1, val_beg, NULL, 0); |
| 7041 | val_beg += stripped_after; |
| 7042 | stripped_before += stripped_after; |
| 7043 | } |
| 7044 | |
| 7045 | val_end += stripped_before; |
| 7046 | next += stripped_before; |
| 7047 | hdr_end += stripped_before; |
| 7048 | hdr_next += stripped_before; |
| 7049 | cur_hdr->len += stripped_before; |
Willy Tarreau | 1fc1f45 | 2011-04-07 22:35:37 +0200 | [diff] [blame] | 7050 | http_msg_move_end(&txn->rsp, stripped_before); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7051 | } |
| 7052 | |
| 7053 | /* First, let's see if we want to capture this cookie. We check |
| 7054 | * that we don't already have a server side cookie, because we |
| 7055 | * can only capture one. Also as an optimisation, we ignore |
| 7056 | * cookies shorter than the declared name. |
| 7057 | */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 7058 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 7059 | txn->srv_cookie == NULL && |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7060 | (val_end - att_beg >= t->fe->capture_namelen) && |
| 7061 | memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
| 7062 | int log_len = val_end - att_beg; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 7063 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7064 | Alert("HTTP logging : out of memory.\n"); |
| 7065 | } |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 7066 | else { |
| 7067 | if (log_len > t->fe->capture_len) |
| 7068 | log_len = t->fe->capture_len; |
| 7069 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 7070 | txn->srv_cookie[log_len] = 0; |
| 7071 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7072 | } |
| 7073 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7074 | srv = target_srv(&t->target); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7075 | /* now check if we need to process it for persistence */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7076 | if (!(t->flags & SN_IGNORE_PRST) && |
| 7077 | (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 7078 | (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) { |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7079 | /* assume passive cookie by default */ |
| 7080 | txn->flags &= ~TX_SCK_MASK; |
| 7081 | txn->flags |= TX_SCK_FOUND; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7082 | |
| 7083 | /* If the cookie is in insert mode on a known server, we'll delete |
| 7084 | * this occurrence because we'll insert another one later. |
| 7085 | * 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] | 7086 | * a direct access. |
| 7087 | */ |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 7088 | if (t->be->options2 & PR_O2_COOK_PSV) { |
| 7089 | /* The "preserve" flag was set, we don't want to touch the |
| 7090 | * server's cookie. |
| 7091 | */ |
| 7092 | } |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7093 | else if ((srv && (t->be->options & PR_O_COOK_INS)) || |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 7094 | ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7095 | /* this cookie must be deleted */ |
| 7096 | if (*prev == ':' && next == hdr_end) { |
| 7097 | /* whole header */ |
| 7098 | delta = buffer_replace2(res, hdr_beg, hdr_next, NULL, 0); |
| 7099 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7100 | txn->hdr_idx.used--; |
| 7101 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7102 | cur_idx = old_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7103 | hdr_next += delta; |
| 7104 | http_msg_move_end(&txn->rsp, delta); |
| 7105 | /* note: while both invalid now, <next> and <hdr_end> |
| 7106 | * are still equal, so the for() will stop as expected. |
| 7107 | */ |
| 7108 | } else { |
| 7109 | /* just remove the value */ |
| 7110 | int delta = del_hdr_value(res, &prev, next); |
| 7111 | next = prev; |
| 7112 | hdr_end += delta; |
| 7113 | hdr_next += delta; |
| 7114 | cur_hdr->len += delta; |
| 7115 | http_msg_move_end(&txn->rsp, delta); |
| 7116 | } |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7117 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7118 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7119 | /* and go on with next cookie */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7120 | } |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7121 | else if (srv && srv->cookie && (t->be->options & PR_O_COOK_RW)) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7122 | /* replace bytes val_beg->val_end with the cookie name associated |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7123 | * with this server since we know it. |
| 7124 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7125 | delta = buffer_replace2(res, val_beg, val_end, srv->cookie, srv->cklen); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7126 | next += delta; |
| 7127 | hdr_end += delta; |
| 7128 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7129 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7130 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7131 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7132 | txn->flags &= ~TX_SCK_MASK; |
| 7133 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7134 | } |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7135 | else if (srv && srv && (t->be->options & PR_O_COOK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7136 | /* insert the cookie name associated with this server |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7137 | * before existing cookie, and insert a delimiter between them.. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7138 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7139 | 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] | 7140 | next += delta; |
| 7141 | hdr_end += delta; |
| 7142 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7143 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7144 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7145 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7146 | val_beg[srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7147 | txn->flags &= ~TX_SCK_MASK; |
| 7148 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7149 | } |
| 7150 | } |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 7151 | /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */ |
| 7152 | else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7153 | int cmp_len, value_len; |
| 7154 | char *value_begin; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7155 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7156 | if (t->be->options2 & PR_O2_AS_PFX) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7157 | cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len); |
| 7158 | value_begin = att_beg + t->be->appsession_name_len; |
| 7159 | 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] | 7160 | } else { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7161 | cmp_len = att_end - att_beg; |
| 7162 | value_begin = val_beg; |
| 7163 | value_len = MIN(t->be->appsession_len, val_end - val_beg); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7164 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7165 | |
Cyril Bonté | 17530c3 | 2010-04-06 21:11:10 +0200 | [diff] [blame] | 7166 | if ((cmp_len == t->be->appsession_name_len) && |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7167 | (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) { |
| 7168 | /* free a possibly previously allocated memory */ |
| 7169 | pool_free2(apools.sessid, txn->sessid); |
| 7170 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7171 | /* Store the sessid in the session for future use */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7172 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7173 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 7174 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 7175 | return; |
| 7176 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7177 | memcpy(txn->sessid, value_begin, value_len); |
| 7178 | txn->sessid[value_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7179 | } |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7180 | } |
| 7181 | /* that's done for this cookie, check the next one on the same |
| 7182 | * line when next != hdr_end (only if is_cookie2). |
| 7183 | */ |
| 7184 | } |
| 7185 | /* check next header */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7186 | old_idx = cur_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7187 | } |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7188 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7189 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7190 | appsess *asession = NULL; |
| 7191 | /* only do insert, if lookup fails */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7192 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7193 | if (asession == NULL) { |
Willy Tarreau | 1fac753 | 2010-01-09 19:23:06 +0100 | [diff] [blame] | 7194 | size_t server_id_len; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7195 | if ((asession = pool_alloc2(pool2_appsess)) == NULL) { |
| 7196 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
| 7197 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n"); |
| 7198 | return; |
| 7199 | } |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 7200 | asession->serverid = NULL; /* to avoid a double free in case of allocation error */ |
| 7201 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7202 | if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) { |
| 7203 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 7204 | 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] | 7205 | t->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7206 | return; |
| 7207 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7208 | memcpy(asession->sessid, txn->sessid, t->be->appsession_len); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7209 | asession->sessid[t->be->appsession_len] = 0; |
| 7210 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7211 | server_id_len = strlen(target_srv(&t->target)->id) + 1; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7212 | if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) { |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 7213 | Alert("Not enough Memory process_srv():asession->serverid:malloc().\n"); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7214 | 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] | 7215 | t->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7216 | return; |
| 7217 | } |
| 7218 | asession->serverid[0] = '\0'; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7219 | memcpy(asession->serverid, target_srv(&t->target)->id, server_id_len); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7220 | |
| 7221 | asession->request_count = 0; |
| 7222 | appsession_hash_insert(&(t->be->htbl_proxy), asession); |
| 7223 | } |
| 7224 | |
| 7225 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 7226 | asession->request_count++; |
| 7227 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7228 | } |
| 7229 | |
| 7230 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7231 | /* |
| 7232 | * Check if response is cacheable or not. Updates t->flags. |
| 7233 | */ |
| 7234 | void check_response_for_cacheability(struct session *t, struct buffer *rtr) |
| 7235 | { |
| 7236 | struct http_txn *txn = &t->txn; |
| 7237 | char *p1, *p2; |
| 7238 | |
| 7239 | char *cur_ptr, *cur_end, *cur_next; |
| 7240 | int cur_idx; |
| 7241 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 7242 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7243 | return; |
| 7244 | |
| 7245 | /* Iterate through the headers. |
| 7246 | * we start with the start line. |
| 7247 | */ |
| 7248 | cur_idx = 0; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7249 | cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7250 | |
| 7251 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 7252 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7253 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7254 | |
| 7255 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 7256 | cur_ptr = cur_next; |
| 7257 | cur_end = cur_ptr + cur_hdr->len; |
| 7258 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7259 | |
| 7260 | /* We have one full header between cur_ptr and cur_end, and the |
| 7261 | * next header starts at cur_next. We're only interested in |
| 7262 | * "Cookie:" headers. |
| 7263 | */ |
| 7264 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7265 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 7266 | if (val) { |
| 7267 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 7268 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 7269 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 7270 | return; |
| 7271 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7272 | } |
| 7273 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7274 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 7275 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7276 | continue; |
| 7277 | |
| 7278 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 7279 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7280 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7281 | |
| 7282 | if (p1 >= cur_end) /* no more info */ |
| 7283 | continue; |
| 7284 | |
| 7285 | /* p1 is at the beginning of the value */ |
| 7286 | p2 = p1; |
| 7287 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 7288 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7289 | p2++; |
| 7290 | |
| 7291 | /* we have a complete value between p1 and p2 */ |
| 7292 | if (p2 < cur_end && *p2 == '=') { |
| 7293 | /* we have something of the form no-cache="set-cookie" */ |
| 7294 | if ((cur_end - p1 >= 21) && |
| 7295 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 7296 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7297 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7298 | continue; |
| 7299 | } |
| 7300 | |
| 7301 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 7302 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
| 7303 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 7304 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 7305 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7306 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7307 | return; |
| 7308 | } |
| 7309 | |
| 7310 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7311 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7312 | continue; |
| 7313 | } |
| 7314 | } |
| 7315 | } |
| 7316 | |
| 7317 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7318 | /* |
| 7319 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 7320 | * If the server is found, it's assigned to the session. |
| 7321 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7322 | 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] | 7323 | { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7324 | char *end_params, *first_param, *cur_param, *next_param; |
| 7325 | char separator; |
| 7326 | int value_len; |
| 7327 | |
| 7328 | int mode = t->be->options2 & PR_O2_AS_M_ANY; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7329 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 7330 | if (t->be->appsession_name == NULL || |
Cyril Bonté | 17530c3 | 2010-04-06 21:11:10 +0200 | [diff] [blame] | 7331 | (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] | 7332 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7333 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7334 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7335 | first_param = NULL; |
| 7336 | switch (mode) { |
| 7337 | case PR_O2_AS_M_PP: |
| 7338 | first_param = memchr(begin, ';', len); |
| 7339 | break; |
| 7340 | case PR_O2_AS_M_QS: |
| 7341 | first_param = memchr(begin, '?', len); |
| 7342 | break; |
| 7343 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7344 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7345 | if (first_param == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7346 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7347 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7348 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7349 | switch (mode) { |
| 7350 | case PR_O2_AS_M_PP: |
| 7351 | if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) { |
| 7352 | end_params = (char *) begin + len; |
| 7353 | } |
| 7354 | separator = ';'; |
| 7355 | break; |
| 7356 | case PR_O2_AS_M_QS: |
| 7357 | end_params = (char *) begin + len; |
| 7358 | separator = '&'; |
| 7359 | break; |
| 7360 | default: |
| 7361 | /* unknown mode, shouldn't happen */ |
| 7362 | return; |
| 7363 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7364 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7365 | cur_param = next_param = end_params; |
| 7366 | while (cur_param > first_param) { |
| 7367 | cur_param--; |
| 7368 | if ((cur_param[0] == separator) || (cur_param == first_param)) { |
| 7369 | /* let's see if this is the appsession parameter */ |
| 7370 | if ((cur_param + t->be->appsession_name_len + 1 < next_param) && |
| 7371 | ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') && |
| 7372 | (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) { |
| 7373 | /* Cool... it's the right one */ |
| 7374 | cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2); |
| 7375 | value_len = MIN(t->be->appsession_len, next_param - cur_param); |
| 7376 | if (value_len > 0) { |
| 7377 | manage_client_side_appsession(t, cur_param, value_len); |
| 7378 | } |
| 7379 | break; |
| 7380 | } |
| 7381 | next_param = cur_param; |
| 7382 | } |
| 7383 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7384 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 7385 | Alert("get_srv_from_appsession\n"); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 7386 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7387 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7388 | } |
| 7389 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7390 | /* |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7391 | * 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] | 7392 | * for the current backend. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7393 | * |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7394 | * 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] | 7395 | * uri_auth field is valid. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7396 | * |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7397 | * Returns 1 if stats should be provided, otherwise 0. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7398 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7399 | 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] | 7400 | { |
| 7401 | struct uri_auth *uri_auth = backend->uri_auth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7402 | char *h; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7403 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7404 | if (!uri_auth) |
| 7405 | return 0; |
| 7406 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7407 | 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] | 7408 | return 0; |
| 7409 | |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7410 | memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats)); |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 7411 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7412 | /* check URI size */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7413 | if (uri_auth->uri_len > txn->req.sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7414 | return 0; |
| 7415 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7416 | h = txn->req.sol + txn->req.sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7417 | |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 7418 | /* the URI is in h */ |
| 7419 | if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7420 | return 0; |
| 7421 | |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7422 | h += uri_auth->uri_len; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7423 | 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] | 7424 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7425 | si->applet.ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7426 | break; |
| 7427 | } |
| 7428 | h++; |
| 7429 | } |
| 7430 | |
| 7431 | if (uri_auth->refresh) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7432 | h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len; |
| 7433 | 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] | 7434 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7435 | si->applet.ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7436 | break; |
| 7437 | } |
| 7438 | h++; |
| 7439 | } |
| 7440 | } |
| 7441 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7442 | h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len; |
| 7443 | 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] | 7444 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7445 | si->applet.ctx.stats.flags |= STAT_FMT_CSV; |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 7446 | break; |
| 7447 | } |
| 7448 | h++; |
| 7449 | } |
| 7450 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7451 | h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len; |
| 7452 | while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 8) { |
| 7453 | if (memcmp(h, ";st=", 4) == 0) { |
| 7454 | h += 4; |
| 7455 | |
| 7456 | if (memcmp(h, STAT_STATUS_DONE, 4) == 0) |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7457 | si->applet.ctx.stats.st_code = STAT_STATUS_DONE; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7458 | else if (memcmp(h, STAT_STATUS_NONE, 4) == 0) |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7459 | si->applet.ctx.stats.st_code = STAT_STATUS_NONE; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7460 | else if (memcmp(h, STAT_STATUS_EXCD, 4) == 0) |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7461 | si->applet.ctx.stats.st_code = STAT_STATUS_EXCD; |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 7462 | else if (memcmp(h, STAT_STATUS_DENY, 4) == 0) |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7463 | si->applet.ctx.stats.st_code = STAT_STATUS_DENY; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7464 | else |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7465 | si->applet.ctx.stats.st_code = STAT_STATUS_UNKN; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7466 | break; |
| 7467 | } |
| 7468 | h++; |
| 7469 | } |
| 7470 | |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7471 | si->applet.ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO; |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 7472 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7473 | return 1; |
| 7474 | } |
| 7475 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7476 | /* |
| 7477 | * 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] | 7478 | * WARNING: it's unlikely that we've reached HTTP_MSG_BODY here so we must not |
| 7479 | * assume that msg->sol = buf->data + msg->som. |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7480 | */ |
| 7481 | void http_capture_bad_message(struct error_snapshot *es, struct session *s, |
| 7482 | struct buffer *buf, struct http_msg *msg, |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 7483 | int state, struct proxy *other_end) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7484 | { |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7485 | if (buf->r <= (buf->data + msg->som)) { /* message wraps */ |
| 7486 | int len1 = buf->size - msg->som; |
| 7487 | es->len = buf->r - (buf->data + msg->som) + buf->size; |
| 7488 | memcpy(es->buf, buf->data + msg->som, MIN(len1, sizeof(es->buf))); |
| 7489 | if (es->len > len1 && len1 < sizeof(es->buf)) |
| 7490 | memcpy(es->buf, buf->data, MIN(es->len, sizeof(es->buf)) - len1); |
| 7491 | } |
| 7492 | else { |
| 7493 | es->len = buf->r - (buf->data + msg->som); |
| 7494 | memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf))); |
| 7495 | } |
| 7496 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7497 | if (msg->err_pos >= 0) |
Willy Tarreau | 2df8d71 | 2009-05-01 11:33:17 +0200 | [diff] [blame] | 7498 | es->pos = msg->err_pos - msg->som; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7499 | else if (buf->lr >= (buf->data + msg->som)) |
Willy Tarreau | 2df8d71 | 2009-05-01 11:33:17 +0200 | [diff] [blame] | 7500 | es->pos = buf->lr - (buf->data + msg->som); |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7501 | else |
| 7502 | es->pos = buf->lr - (buf->data + msg->som) + buf->size; |
| 7503 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7504 | es->when = date; // user-visible date |
| 7505 | es->sid = s->uniq_id; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7506 | es->srv = target_srv(&s->target); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7507 | es->oe = other_end; |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 7508 | es->src = s->req->prod->addr.from; |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 7509 | es->state = state; |
| 7510 | es->flags = buf->flags; |
Willy Tarreau | 10479e4 | 2010-12-12 14:00:34 +0100 | [diff] [blame] | 7511 | es->ev_id = error_snapshot_id++; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7512 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7513 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7514 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 7515 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 7516 | * performed over the whole headers. Otherwise it must contain a valid header |
| 7517 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 7518 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 7519 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 7520 | * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is |
| 7521 | * -1. |
| 7522 | * The return value is 0 if nothing was found, or non-zero otherwise. |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7523 | */ |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7524 | unsigned int http_get_hdr(struct http_msg *msg, const char *hname, int hlen, |
| 7525 | struct hdr_idx *idx, int occ, |
| 7526 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7527 | { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7528 | struct hdr_ctx local_ctx; |
| 7529 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 7530 | int len_hist[MAX_HDR_HISTORY]; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7531 | unsigned int hist_ptr; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7532 | int found; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7533 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7534 | if (!ctx) { |
| 7535 | local_ctx.idx = 0; |
| 7536 | ctx = &local_ctx; |
| 7537 | } |
| 7538 | |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7539 | if (occ >= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7540 | /* search from the beginning */ |
| 7541 | while (http_find_header2(hname, hlen, msg->sol, idx, ctx)) { |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7542 | occ--; |
| 7543 | if (occ <= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7544 | *vptr = ctx->line + ctx->val; |
| 7545 | *vlen = ctx->vlen; |
| 7546 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7547 | } |
| 7548 | } |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7549 | return 0; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7550 | } |
| 7551 | |
| 7552 | /* negative occurrence, we scan all the list then walk back */ |
| 7553 | if (-occ > MAX_HDR_HISTORY) |
| 7554 | return 0; |
| 7555 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7556 | found = hist_ptr = 0; |
| 7557 | while (http_find_header2(hname, hlen, msg->sol, idx, ctx)) { |
| 7558 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 7559 | len_hist[hist_ptr] = ctx->vlen; |
| 7560 | if (++hist_ptr >= MAX_HDR_HISTORY) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7561 | hist_ptr = 0; |
| 7562 | found++; |
| 7563 | } |
| 7564 | if (-occ > found) |
| 7565 | return 0; |
| 7566 | /* OK now we have the last occurrence in [hist_ptr-1], and we need to |
| 7567 | * find occurrence -occ, so we have to check [hist_ptr+occ]. |
| 7568 | */ |
| 7569 | hist_ptr += occ; |
| 7570 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 7571 | hist_ptr -= MAX_HDR_HISTORY; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7572 | *vptr = ptr_hist[hist_ptr]; |
| 7573 | *vlen = len_hist[hist_ptr]; |
| 7574 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7575 | } |
| 7576 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 7577 | /* |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7578 | * Print a debug line with a header |
| 7579 | */ |
| 7580 | void debug_hdr(const char *dir, struct session *t, const char *start, const char *end) |
| 7581 | { |
| 7582 | int len, max; |
| 7583 | 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] | 7584 | 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] | 7585 | max = end - start; |
| 7586 | UBOUND(max, sizeof(trash) - len - 1); |
| 7587 | len += strlcpy2(trash + len, start, max + 1); |
| 7588 | trash[len++] = '\n'; |
| 7589 | write(1, trash, len); |
| 7590 | } |
| 7591 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7592 | /* |
| 7593 | * Initialize a new HTTP transaction for session <s>. It is assumed that all |
| 7594 | * the required fields are properly allocated and that we only need to (re)init |
| 7595 | * them. This should be used before processing any new request. |
| 7596 | */ |
| 7597 | void http_init_txn(struct session *s) |
| 7598 | { |
| 7599 | struct http_txn *txn = &s->txn; |
| 7600 | struct proxy *fe = s->fe; |
| 7601 | |
| 7602 | txn->flags = 0; |
| 7603 | txn->status = -1; |
| 7604 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7605 | txn->cookie_first_date = 0; |
| 7606 | txn->cookie_last_date = 0; |
| 7607 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7608 | txn->req.sol = txn->req.eol = NULL; |
| 7609 | txn->req.som = txn->req.eoh = 0; /* relative to the buffer */ |
| 7610 | txn->rsp.sol = txn->rsp.eol = NULL; |
| 7611 | txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */ |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 7612 | txn->req.chunk_len = 0LL; |
| 7613 | txn->req.body_len = 0LL; |
| 7614 | txn->rsp.chunk_len = 0LL; |
| 7615 | txn->rsp.body_len = 0LL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7616 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 7617 | 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] | 7618 | |
| 7619 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7620 | |
| 7621 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 7622 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 7623 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 7624 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7625 | if (txn->req.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7626 | memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *)); |
| 7627 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7628 | if (txn->rsp.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7629 | memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
| 7630 | |
| 7631 | if (txn->hdr_idx.v) |
| 7632 | hdr_idx_init(&txn->hdr_idx); |
| 7633 | } |
| 7634 | |
| 7635 | /* to be used at the end of a transaction */ |
| 7636 | void http_end_txn(struct session *s) |
| 7637 | { |
| 7638 | struct http_txn *txn = &s->txn; |
| 7639 | |
| 7640 | /* these ones will have been dynamically allocated */ |
| 7641 | pool_free2(pool2_requri, txn->uri); |
| 7642 | pool_free2(pool2_capture, txn->cli_cookie); |
| 7643 | pool_free2(pool2_capture, txn->srv_cookie); |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7644 | pool_free2(apools.sessid, txn->sessid); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 7645 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7646 | txn->sessid = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7647 | txn->uri = NULL; |
| 7648 | txn->srv_cookie = NULL; |
| 7649 | txn->cli_cookie = NULL; |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7650 | |
| 7651 | if (txn->req.cap) { |
| 7652 | struct cap_hdr *h; |
| 7653 | for (h = s->fe->req_cap; h; h = h->next) |
| 7654 | pool_free2(h->pool, txn->req.cap[h->index]); |
| 7655 | memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *)); |
| 7656 | } |
| 7657 | |
| 7658 | if (txn->rsp.cap) { |
| 7659 | struct cap_hdr *h; |
| 7660 | for (h = s->fe->rsp_cap; h; h = h->next) |
| 7661 | pool_free2(h->pool, txn->rsp.cap[h->index]); |
| 7662 | memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *)); |
| 7663 | } |
| 7664 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7665 | } |
| 7666 | |
| 7667 | /* to be used at the end of a transaction to prepare a new one */ |
| 7668 | void http_reset_txn(struct session *s) |
| 7669 | { |
| 7670 | http_end_txn(s); |
| 7671 | http_init_txn(s); |
| 7672 | |
| 7673 | s->be = s->fe; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7674 | s->logs.logwait = s->fe->to_log; |
Simon Horman | af51495 | 2011-06-21 14:34:57 +0900 | [diff] [blame] | 7675 | session_del_srv_conn(s); |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 7676 | clear_target(&s->target); |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 7677 | /* re-init store persistence */ |
| 7678 | s->store_count = 0; |
| 7679 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7680 | s->pend_pos = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7681 | |
| 7682 | s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */ |
| 7683 | |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 7684 | /* We must trim any excess data from the response buffer, because we |
| 7685 | * may have blocked an invalid response from a server that we don't |
| 7686 | * want to accidentely forward once we disable the analysers, nor do |
| 7687 | * we want those data to come along with next response. A typical |
| 7688 | * example of such data would be from a buggy server responding to |
| 7689 | * a HEAD with some data, or sending more than the advertised |
| 7690 | * content-length. |
| 7691 | */ |
| 7692 | if (unlikely(s->rep->l > s->rep->send_max)) { |
| 7693 | s->rep->l = s->rep->send_max; |
| 7694 | s->rep->r = s->rep->w + s->rep->l; |
| 7695 | if (s->rep->r >= s->rep->data + s->rep->size) |
| 7696 | s->rep->r -= s->rep->size; |
| 7697 | } |
| 7698 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7699 | s->req->rto = s->fe->timeout.client; |
Willy Tarreau | d04e858 | 2010-05-31 12:31:35 +0200 | [diff] [blame] | 7700 | s->req->wto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7701 | |
Willy Tarreau | d04e858 | 2010-05-31 12:31:35 +0200 | [diff] [blame] | 7702 | s->rep->rto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7703 | s->rep->wto = s->fe->timeout.client; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7704 | |
| 7705 | s->req->rex = TICK_ETERNITY; |
| 7706 | s->req->wex = TICK_ETERNITY; |
| 7707 | s->req->analyse_exp = TICK_ETERNITY; |
| 7708 | s->rep->rex = TICK_ETERNITY; |
| 7709 | s->rep->wex = TICK_ETERNITY; |
| 7710 | s->rep->analyse_exp = TICK_ETERNITY; |
| 7711 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7712 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 7713 | void free_http_req_rules(struct list *r) { |
| 7714 | struct http_req_rule *tr, *pr; |
| 7715 | |
| 7716 | list_for_each_entry_safe(pr, tr, r, list) { |
| 7717 | LIST_DEL(&pr->list); |
| 7718 | if (pr->action == HTTP_REQ_ACT_HTTP_AUTH) |
| 7719 | free(pr->http_auth.realm); |
| 7720 | |
| 7721 | free(pr); |
| 7722 | } |
| 7723 | } |
| 7724 | |
| 7725 | struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
| 7726 | { |
| 7727 | struct http_req_rule *rule; |
| 7728 | int cur_arg; |
| 7729 | |
| 7730 | rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule)); |
| 7731 | if (!rule) { |
| 7732 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
| 7733 | return NULL; |
| 7734 | } |
| 7735 | |
| 7736 | if (!*args[0]) { |
| 7737 | goto req_error_parsing; |
| 7738 | } else if (!strcmp(args[0], "allow")) { |
| 7739 | rule->action = HTTP_REQ_ACT_ALLOW; |
| 7740 | cur_arg = 1; |
| 7741 | } else if (!strcmp(args[0], "deny")) { |
| 7742 | rule->action = HTTP_REQ_ACT_DENY; |
| 7743 | cur_arg = 1; |
| 7744 | } else if (!strcmp(args[0], "auth")) { |
| 7745 | rule->action = HTTP_REQ_ACT_HTTP_AUTH; |
| 7746 | cur_arg = 1; |
| 7747 | |
| 7748 | while(*args[cur_arg]) { |
| 7749 | if (!strcmp(args[cur_arg], "realm")) { |
| 7750 | rule->http_auth.realm = strdup(args[cur_arg + 1]); |
| 7751 | cur_arg+=2; |
| 7752 | continue; |
| 7753 | } else |
| 7754 | break; |
| 7755 | } |
| 7756 | } else { |
| 7757 | req_error_parsing: |
| 7758 | Alert("parsing [%s:%d]: %s '%s', expects 'allow', 'deny', 'auth'.\n", |
| 7759 | file, linenum, *args[1]?"unknown parameter":"missing keyword in", args[*args[1]?1:0]); |
| 7760 | return NULL; |
| 7761 | } |
| 7762 | |
| 7763 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 7764 | struct acl_cond *cond; |
| 7765 | |
| 7766 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg)) == NULL) { |
| 7767 | Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition.\n", |
| 7768 | file, linenum, args[0]); |
| 7769 | return NULL; |
| 7770 | } |
| 7771 | rule->cond = cond; |
| 7772 | } |
| 7773 | else if (*args[cur_arg]) { |
| 7774 | Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or" |
| 7775 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 7776 | file, linenum, args[0], args[cur_arg]); |
| 7777 | return NULL; |
| 7778 | } |
| 7779 | |
| 7780 | return rule; |
| 7781 | } |
| 7782 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7783 | /************************************************************************/ |
| 7784 | /* The code below is dedicated to ACL parsing and matching */ |
| 7785 | /************************************************************************/ |
| 7786 | |
| 7787 | |
| 7788 | |
| 7789 | |
| 7790 | /* 1. Check on METHOD |
| 7791 | * We use the pre-parsed method if it is known, and store its number as an |
| 7792 | * integer. If it is unknown, we use the pointer and the length. |
| 7793 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7794 | 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] | 7795 | { |
| 7796 | int len, meth; |
| 7797 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7798 | len = strlen(*text); |
| 7799 | meth = find_http_meth(*text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7800 | |
| 7801 | pattern->val.i = meth; |
| 7802 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7803 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7804 | if (!pattern->ptr.str) |
| 7805 | return 0; |
| 7806 | pattern->len = len; |
| 7807 | } |
| 7808 | return 1; |
| 7809 | } |
| 7810 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7811 | /* This function fetches the method of current HTTP request and stores |
| 7812 | * it in the global pattern struct as a chunk. There are two possibilities : |
| 7813 | * - if the method is known (not HTTP_METH_OTHER), its identifier is stored |
| 7814 | * in <len> and <ptr> is NULL ; |
| 7815 | * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and |
| 7816 | * <len> to its length. |
| 7817 | * This is intended to be used with acl_match_meth() only. |
| 7818 | */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7819 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 7820 | acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7821 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7822 | { |
| 7823 | int meth; |
| 7824 | struct http_txn *txn = l7; |
| 7825 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7826 | if (!txn) |
| 7827 | return 0; |
| 7828 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7829 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7830 | return 0; |
| 7831 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7832 | meth = txn->meth; |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7833 | temp_pattern.data.str.len = meth; |
| 7834 | temp_pattern.data.str.str = NULL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7835 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7836 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7837 | /* ensure the indexes are not affected */ |
| 7838 | return 0; |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7839 | temp_pattern.data.str.len = txn->req.sl.rq.m_l; |
| 7840 | temp_pattern.data.str.str = txn->req.sol; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7841 | } |
| 7842 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 7843 | return 1; |
| 7844 | } |
| 7845 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7846 | /* See above how the method is stored in the global pattern */ |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7847 | static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern) |
| 7848 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 7849 | int icase; |
| 7850 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7851 | |
| 7852 | if (temp_pattern.data.str.str == NULL) { |
| 7853 | /* well-known method */ |
| 7854 | if (temp_pattern.data.str.len == pattern->val.i) |
| 7855 | return ACL_PAT_PASS; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 7856 | return ACL_PAT_FAIL; |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7857 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7858 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7859 | /* Uncommon method, only HTTP_METH_OTHER is accepted now */ |
| 7860 | if (pattern->val.i != HTTP_METH_OTHER) |
| 7861 | return ACL_PAT_FAIL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7862 | |
| 7863 | /* Other method, we must compare the strings */ |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7864 | if (pattern->len != temp_pattern.data.str.len) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 7865 | return ACL_PAT_FAIL; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 7866 | |
| 7867 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7868 | if ((icase && strncasecmp(pattern->ptr.str, temp_pattern.data.str.str, temp_pattern.data.str.len) != 0) || |
| 7869 | (!icase && strncmp(pattern->ptr.str, temp_pattern.data.str.str, temp_pattern.data.str.len) != 0)) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 7870 | return ACL_PAT_FAIL; |
| 7871 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7872 | } |
| 7873 | |
| 7874 | /* 2. Check on Request/Status Version |
| 7875 | * We simply compare strings here. |
| 7876 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7877 | 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] | 7878 | { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7879 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7880 | if (!pattern->ptr.str) |
| 7881 | return 0; |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7882 | pattern->len = strlen(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7883 | return 1; |
| 7884 | } |
| 7885 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7886 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 7887 | acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7888 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7889 | { |
| 7890 | struct http_txn *txn = l7; |
| 7891 | char *ptr; |
| 7892 | int len; |
| 7893 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7894 | if (!txn) |
| 7895 | return 0; |
| 7896 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7897 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7898 | return 0; |
| 7899 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7900 | len = txn->req.sl.rq.v_l; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7901 | ptr = txn->req.sol + txn->req.sl.rq.v; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7902 | |
| 7903 | while ((len-- > 0) && (*ptr++ != '/')); |
| 7904 | if (len <= 0) |
| 7905 | return 0; |
| 7906 | |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 7907 | temp_pattern.data.str.str = ptr; |
| 7908 | temp_pattern.data.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7909 | |
| 7910 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 7911 | return 1; |
| 7912 | } |
| 7913 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7914 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 7915 | acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7916 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7917 | { |
| 7918 | struct http_txn *txn = l7; |
| 7919 | char *ptr; |
| 7920 | int len; |
| 7921 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7922 | if (!txn) |
| 7923 | return 0; |
| 7924 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7925 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7926 | return 0; |
| 7927 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7928 | len = txn->rsp.sl.st.v_l; |
| 7929 | ptr = txn->rsp.sol; |
| 7930 | |
| 7931 | while ((len-- > 0) && (*ptr++ != '/')); |
| 7932 | if (len <= 0) |
| 7933 | return 0; |
| 7934 | |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 7935 | temp_pattern.data.str.str = ptr; |
| 7936 | temp_pattern.data.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7937 | |
| 7938 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 7939 | return 1; |
| 7940 | } |
| 7941 | |
| 7942 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7943 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 7944 | acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7945 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7946 | { |
| 7947 | struct http_txn *txn = l7; |
| 7948 | char *ptr; |
| 7949 | int len; |
| 7950 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7951 | if (!txn) |
| 7952 | return 0; |
| 7953 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7954 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7955 | return 0; |
| 7956 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7957 | len = txn->rsp.sl.st.c_l; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7958 | ptr = txn->rsp.sol + txn->rsp.sl.st.c; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7959 | |
Willy Tarreau | a5e3756 | 2011-12-16 17:06:15 +0100 | [diff] [blame] | 7960 | temp_pattern.data.integer = __strl2ui(ptr, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7961 | test->flags = ACL_TEST_F_VOL_1ST; |
| 7962 | return 1; |
| 7963 | } |
| 7964 | |
| 7965 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7966 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 7967 | acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7968 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7969 | { |
| 7970 | struct http_txn *txn = l7; |
| 7971 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7972 | if (!txn) |
| 7973 | return 0; |
| 7974 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7975 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7976 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7977 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7978 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7979 | /* ensure the indexes are not affected */ |
| 7980 | return 0; |
| 7981 | |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 7982 | temp_pattern.data.str.len = txn->req.sl.rq.u_l; |
| 7983 | temp_pattern.data.str.str = txn->req.sol + txn->req.sl.rq.u; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7984 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 7985 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 7986 | test->flags = ACL_TEST_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7987 | return 1; |
| 7988 | } |
| 7989 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7990 | static int |
| 7991 | acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7992 | struct acl_expr *expr, struct acl_test *test) |
| 7993 | { |
| 7994 | struct http_txn *txn = l7; |
| 7995 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7996 | if (!txn) |
| 7997 | return 0; |
| 7998 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7999 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 8000 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8001 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 8002 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 8003 | /* ensure the indexes are not affected */ |
| 8004 | return 0; |
| 8005 | |
| 8006 | /* Parse HTTP request */ |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 8007 | url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->addr.to); |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 8008 | if (((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_family != AF_INET) |
| 8009 | return 0; |
| 8010 | temp_pattern.type = PATTERN_TYPE_IP; |
| 8011 | temp_pattern.data.ip = ((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 8012 | |
| 8013 | /* |
| 8014 | * If we are parsing url in frontend space, we prepare backend stage |
| 8015 | * to not parse again the same url ! optimization lazyness... |
| 8016 | */ |
| 8017 | if (px->options & PR_O_HTTP_PROXY) |
| 8018 | l4->flags |= SN_ADDR_SET; |
| 8019 | |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 8020 | test->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 8021 | return 1; |
| 8022 | } |
| 8023 | |
| 8024 | static int |
| 8025 | acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8026 | struct acl_expr *expr, struct acl_test *test) |
| 8027 | { |
| 8028 | struct http_txn *txn = l7; |
| 8029 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8030 | if (!txn) |
| 8031 | return 0; |
| 8032 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8033 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 8034 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8035 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 8036 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 8037 | /* ensure the indexes are not affected */ |
| 8038 | return 0; |
| 8039 | |
| 8040 | /* Same optimization as url_ip */ |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 8041 | url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->addr.to); |
Willy Tarreau | a5e3756 | 2011-12-16 17:06:15 +0100 | [diff] [blame] | 8042 | temp_pattern.data.integer = ntohs(((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_port); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 8043 | |
| 8044 | if (px->options & PR_O_HTTP_PROXY) |
| 8045 | l4->flags |= SN_ADDR_SET; |
| 8046 | |
| 8047 | test->flags = ACL_TEST_F_READ_ONLY; |
| 8048 | return 1; |
| 8049 | } |
| 8050 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8051 | /* 5. Check on HTTP header. A pointer to the beginning of the value is returned. |
| 8052 | * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr(). |
| 8053 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8054 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8055 | 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] | 8056 | struct acl_expr *expr, struct acl_test *test) |
| 8057 | { |
| 8058 | struct http_txn *txn = l7; |
| 8059 | struct hdr_idx *idx = &txn->hdr_idx; |
| 8060 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8061 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8062 | if (!txn) |
| 8063 | return 0; |
| 8064 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8065 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 8066 | /* search for header from the beginning */ |
| 8067 | ctx->idx = 0; |
| 8068 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8069 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 8070 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 8071 | test->flags |= ACL_TEST_F_VOL_HDR; |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 8072 | temp_pattern.data.str.str = (char *)ctx->line + ctx->val; |
| 8073 | temp_pattern.data.str.len = ctx->vlen; |
| 8074 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8075 | return 1; |
| 8076 | } |
| 8077 | |
| 8078 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 8079 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 8080 | return 0; |
| 8081 | } |
| 8082 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8083 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8084 | acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8085 | struct acl_expr *expr, struct acl_test *test) |
| 8086 | { |
| 8087 | struct http_txn *txn = l7; |
| 8088 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8089 | if (!txn) |
| 8090 | return 0; |
| 8091 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8092 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8093 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8094 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8095 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 8096 | /* ensure the indexes are not affected */ |
| 8097 | return 0; |
| 8098 | |
| 8099 | return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test); |
| 8100 | } |
| 8101 | |
| 8102 | static int |
| 8103 | acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8104 | struct acl_expr *expr, struct acl_test *test) |
| 8105 | { |
| 8106 | struct http_txn *txn = l7; |
| 8107 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8108 | if (!txn) |
| 8109 | return 0; |
| 8110 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8111 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8112 | return 0; |
| 8113 | |
| 8114 | return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test); |
| 8115 | } |
| 8116 | |
| 8117 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 8118 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 8119 | */ |
| 8120 | static int |
| 8121 | 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] | 8122 | struct acl_expr *expr, struct acl_test *test) |
| 8123 | { |
| 8124 | struct http_txn *txn = l7; |
| 8125 | struct hdr_idx *idx = &txn->hdr_idx; |
| 8126 | struct hdr_ctx ctx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8127 | int cnt; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8128 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8129 | if (!txn) |
| 8130 | return 0; |
| 8131 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8132 | ctx.idx = 0; |
| 8133 | cnt = 0; |
| 8134 | while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx)) |
| 8135 | cnt++; |
| 8136 | |
Willy Tarreau | a5e3756 | 2011-12-16 17:06:15 +0100 | [diff] [blame] | 8137 | temp_pattern.data.integer = cnt; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8138 | test->flags = ACL_TEST_F_VOL_HDR; |
| 8139 | return 1; |
| 8140 | } |
| 8141 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8142 | static int |
| 8143 | acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8144 | struct acl_expr *expr, struct acl_test *test) |
| 8145 | { |
| 8146 | struct http_txn *txn = l7; |
| 8147 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8148 | if (!txn) |
| 8149 | return 0; |
| 8150 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8151 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8152 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8153 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8154 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 8155 | /* ensure the indexes are not affected */ |
| 8156 | return 0; |
| 8157 | |
| 8158 | return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test); |
| 8159 | } |
| 8160 | |
| 8161 | static int |
| 8162 | acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8163 | struct acl_expr *expr, struct acl_test *test) |
| 8164 | { |
| 8165 | struct http_txn *txn = l7; |
| 8166 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8167 | if (!txn) |
| 8168 | return 0; |
| 8169 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8170 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8171 | return 0; |
| 8172 | |
| 8173 | return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test); |
| 8174 | } |
| 8175 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8176 | /* 7. Check on HTTP header's integer value. The integer value is returned. |
| 8177 | * FIXME: the type is 'int', it may not be appropriate for everything. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8178 | * 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] | 8179 | */ |
| 8180 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8181 | 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] | 8182 | struct acl_expr *expr, struct acl_test *test) |
| 8183 | { |
| 8184 | struct http_txn *txn = l7; |
| 8185 | struct hdr_idx *idx = &txn->hdr_idx; |
| 8186 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8187 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8188 | if (!txn) |
| 8189 | return 0; |
| 8190 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8191 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 8192 | /* search for header from the beginning */ |
| 8193 | ctx->idx = 0; |
| 8194 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8195 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 8196 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 8197 | test->flags |= ACL_TEST_F_VOL_HDR; |
Willy Tarreau | a5e3756 | 2011-12-16 17:06:15 +0100 | [diff] [blame] | 8198 | temp_pattern.data.integer = strl2ic((char *)ctx->line + ctx->val, ctx->vlen); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8199 | return 1; |
| 8200 | } |
| 8201 | |
| 8202 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 8203 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 8204 | return 0; |
| 8205 | } |
| 8206 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8207 | static int |
| 8208 | acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8209 | struct acl_expr *expr, struct acl_test *test) |
| 8210 | { |
| 8211 | struct http_txn *txn = l7; |
| 8212 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8213 | if (!txn) |
| 8214 | return 0; |
| 8215 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8216 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8217 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8218 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8219 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 8220 | /* ensure the indexes are not affected */ |
| 8221 | return 0; |
| 8222 | |
| 8223 | return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test); |
| 8224 | } |
| 8225 | |
| 8226 | static int |
| 8227 | acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8228 | struct acl_expr *expr, struct acl_test *test) |
| 8229 | { |
| 8230 | struct http_txn *txn = l7; |
| 8231 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8232 | if (!txn) |
| 8233 | return 0; |
| 8234 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8235 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8236 | return 0; |
| 8237 | |
| 8238 | return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test); |
| 8239 | } |
| 8240 | |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 8241 | /* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned. |
| 8242 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 8243 | */ |
| 8244 | static int |
| 8245 | acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol, |
| 8246 | struct acl_expr *expr, struct acl_test *test) |
| 8247 | { |
| 8248 | struct http_txn *txn = l7; |
| 8249 | struct hdr_idx *idx = &txn->hdr_idx; |
| 8250 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
| 8251 | |
| 8252 | if (!txn) |
| 8253 | return 0; |
| 8254 | |
| 8255 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 8256 | /* search for header from the beginning */ |
| 8257 | ctx->idx = 0; |
| 8258 | |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 8259 | while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 8260 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 8261 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 8262 | /* Same optimization as url_ip */ |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 8263 | temp_pattern.type = PATTERN_TYPE_IP; |
| 8264 | if (url2ipv4((char *)ctx->line + ctx->val, &temp_pattern.data.ip)) |
| 8265 | return 1; |
| 8266 | /* Dods not look like an IP address, let's fetch next one */ |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 8267 | } |
| 8268 | |
| 8269 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 8270 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 8271 | return 0; |
| 8272 | } |
| 8273 | |
| 8274 | static int |
| 8275 | acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8276 | struct acl_expr *expr, struct acl_test *test) |
| 8277 | { |
| 8278 | struct http_txn *txn = l7; |
| 8279 | |
| 8280 | if (!txn) |
| 8281 | return 0; |
| 8282 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8283 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 8284 | return 0; |
| 8285 | |
| 8286 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 8287 | /* ensure the indexes are not affected */ |
| 8288 | return 0; |
| 8289 | |
| 8290 | return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test); |
| 8291 | } |
| 8292 | |
| 8293 | static int |
| 8294 | acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8295 | struct acl_expr *expr, struct acl_test *test) |
| 8296 | { |
| 8297 | struct http_txn *txn = l7; |
| 8298 | |
| 8299 | if (!txn) |
| 8300 | return 0; |
| 8301 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8302 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 8303 | return 0; |
| 8304 | |
| 8305 | return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test); |
| 8306 | } |
| 8307 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8308 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 8309 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 8310 | */ |
| 8311 | static int |
| 8312 | acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8313 | struct acl_expr *expr, struct acl_test *test) |
| 8314 | { |
| 8315 | struct http_txn *txn = l7; |
| 8316 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8317 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8318 | if (!txn) |
| 8319 | return 0; |
| 8320 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8321 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8322 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8323 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8324 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 8325 | /* ensure the indexes are not affected */ |
| 8326 | return 0; |
| 8327 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 8328 | 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] | 8329 | ptr = http_get_path(txn); |
| 8330 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8331 | return 0; |
| 8332 | |
| 8333 | /* OK, we got the '/' ! */ |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 8334 | temp_pattern.data.str.str = ptr; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8335 | |
| 8336 | while (ptr < end && *ptr != '?') |
| 8337 | ptr++; |
| 8338 | |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 8339 | temp_pattern.data.str.len = ptr - temp_pattern.data.str.str; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8340 | |
| 8341 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 8342 | test->flags = ACL_TEST_F_VOL_1ST; |
| 8343 | return 1; |
| 8344 | } |
| 8345 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8346 | static int |
| 8347 | acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir, |
| 8348 | struct acl_expr *expr, struct acl_test *test) |
| 8349 | { |
| 8350 | struct buffer *req = s->req; |
| 8351 | struct http_txn *txn = &s->txn; |
| 8352 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8353 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8354 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 8355 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 8356 | */ |
| 8357 | |
| 8358 | if (!s || !req) |
| 8359 | return 0; |
| 8360 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8361 | if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8362 | /* Already decoded as OK */ |
| 8363 | test->flags |= ACL_TEST_F_SET_RES_PASS; |
| 8364 | return 1; |
| 8365 | } |
| 8366 | |
| 8367 | /* Try to decode HTTP request */ |
| 8368 | if (likely(req->lr < req->r)) |
| 8369 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
| 8370 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8371 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8372 | if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) { |
| 8373 | test->flags |= ACL_TEST_F_SET_RES_FAIL; |
| 8374 | return 1; |
| 8375 | } |
| 8376 | /* wait for final state */ |
| 8377 | test->flags |= ACL_TEST_F_MAY_CHANGE; |
| 8378 | return 0; |
| 8379 | } |
| 8380 | |
| 8381 | /* OK we got a valid HTTP request. We have some minor preparation to |
| 8382 | * perform so that further checks can rely on HTTP tests. |
| 8383 | */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 8384 | txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8385 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 8386 | s->flags |= SN_REDIRECTABLE; |
| 8387 | |
| 8388 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) { |
| 8389 | test->flags |= ACL_TEST_F_SET_RES_FAIL; |
| 8390 | return 1; |
| 8391 | } |
| 8392 | |
| 8393 | test->flags |= ACL_TEST_F_SET_RES_PASS; |
| 8394 | return 1; |
| 8395 | } |
| 8396 | |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 8397 | /* return a valid test if the current request is the first one on the connection */ |
| 8398 | static int |
| 8399 | acl_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, int dir, |
| 8400 | struct acl_expr *expr, struct acl_test *test) |
| 8401 | { |
| 8402 | if (!s) |
| 8403 | return 0; |
| 8404 | |
| 8405 | if (s->txn.flags & TX_NOT_FIRST) |
| 8406 | test->flags |= ACL_TEST_F_SET_RES_FAIL; |
| 8407 | else |
| 8408 | test->flags |= ACL_TEST_F_SET_RES_PASS; |
| 8409 | |
| 8410 | return 1; |
| 8411 | } |
| 8412 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8413 | static int |
| 8414 | acl_fetch_http_auth(struct proxy *px, struct session *s, void *l7, int dir, |
| 8415 | struct acl_expr *expr, struct acl_test *test) |
| 8416 | { |
| 8417 | |
| 8418 | if (!s) |
| 8419 | return 0; |
| 8420 | |
| 8421 | if (!get_http_auth(s)) |
| 8422 | return 0; |
| 8423 | |
| 8424 | test->ctx.a[0] = expr->arg.ul; |
| 8425 | test->ctx.a[1] = s->txn.auth.user; |
| 8426 | test->ctx.a[2] = s->txn.auth.pass; |
| 8427 | |
| 8428 | test->flags |= ACL_TEST_F_READ_ONLY | ACL_TEST_F_NULL_MATCH; |
| 8429 | |
| 8430 | return 1; |
| 8431 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8432 | |
| 8433 | /************************************************************************/ |
| 8434 | /* All supported keywords must be declared here. */ |
| 8435 | /************************************************************************/ |
| 8436 | |
| 8437 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 8438 | static struct acl_kw_list acl_kws = {{ },{ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8439 | { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT }, |
| 8440 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 8441 | { "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] | 8442 | { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP }, |
| 8443 | { "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] | 8444 | { "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] | 8445 | |
Willy Tarreau | c426296 | 2010-05-10 23:42:40 +0200 | [diff] [blame] | 8446 | { "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] | 8447 | { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 8448 | { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 8449 | { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 8450 | { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 8451 | { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 8452 | { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
Willy Tarreau | 0e69854 | 2011-09-16 08:32:32 +0200 | [diff] [blame] | 8453 | { "url_len", acl_parse_int, acl_fetch_url, acl_match_len, ACL_USE_L7REQ_VOLATILE }, |
Willy Tarreau | b337b53 | 2010-05-13 20:03:41 +0200 | [diff] [blame] | 8454 | { "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] | 8455 | { "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] | 8456 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 8457 | /* 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] | 8458 | { "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] | 8459 | { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 8460 | { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 8461 | { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 8462 | { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 8463 | { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 8464 | { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 8465 | { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE }, |
Willy Tarreau | 0e69854 | 2011-09-16 08:32:32 +0200 | [diff] [blame] | 8466 | { "hdr_len", acl_parse_int, acl_fetch_chdr, acl_match_len, ACL_USE_L7REQ_VOLATILE }, |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 8467 | { "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] | 8468 | { "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] | 8469 | |
Willy Tarreau | c426296 | 2010-05-10 23:42:40 +0200 | [diff] [blame] | 8470 | { "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] | 8471 | { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE }, |
| 8472 | { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE }, |
| 8473 | { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE }, |
| 8474 | { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE }, |
| 8475 | { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE }, |
| 8476 | { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE }, |
| 8477 | { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE }, |
Willy Tarreau | 0e69854 | 2011-09-16 08:32:32 +0200 | [diff] [blame] | 8478 | { "shdr_len", acl_parse_int, acl_fetch_shdr, acl_match_len, ACL_USE_L7RTR_VOLATILE }, |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 8479 | { "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] | 8480 | { "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] | 8481 | |
Willy Tarreau | c426296 | 2010-05-10 23:42:40 +0200 | [diff] [blame] | 8482 | { "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] | 8483 | { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 8484 | { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 8485 | { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 8486 | { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 8487 | { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 8488 | { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
Willy Tarreau | 0e69854 | 2011-09-16 08:32:32 +0200 | [diff] [blame] | 8489 | { "path_len", acl_parse_int, acl_fetch_path, acl_match_len, ACL_USE_L7REQ_VOLATILE }, |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8490 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 8491 | #if 0 |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8492 | { "line", acl_parse_str, acl_fetch_line, acl_match_str }, |
| 8493 | { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg }, |
| 8494 | { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg }, |
| 8495 | { "line_end", acl_parse_str, acl_fetch_line, acl_match_end }, |
| 8496 | { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub }, |
| 8497 | { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir }, |
| 8498 | { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom }, |
| 8499 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8500 | { "cook", acl_parse_str, acl_fetch_cook, acl_match_str }, |
| 8501 | { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg }, |
| 8502 | { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg }, |
| 8503 | { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end }, |
| 8504 | { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub }, |
| 8505 | { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir }, |
| 8506 | { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom }, |
| 8507 | { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst }, |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8508 | #endif |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8509 | |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 8510 | { "http_auth", acl_parse_nothing, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT }, |
| 8511 | { "http_auth_group", acl_parse_strcat, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT }, |
| 8512 | { "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] | 8513 | { NULL, NULL, NULL, NULL }, |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8514 | }}; |
| 8515 | |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8516 | /************************************************************************/ |
| 8517 | /* The code below is dedicated to pattern fetching and matching */ |
| 8518 | /************************************************************************/ |
| 8519 | |
Willy Tarreau | e428fb7 | 2011-12-16 21:50:30 +0100 | [diff] [blame] | 8520 | /* Returns the last occurrence of specified header. */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8521 | static int |
Willy Tarreau | e428fb7 | 2011-12-16 21:50:30 +0100 | [diff] [blame] | 8522 | pattern_fetch_hdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8523 | 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] | 8524 | { |
| 8525 | struct http_txn *txn = l7; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8526 | |
Willy Tarreau | e428fb7 | 2011-12-16 21:50:30 +0100 | [diff] [blame] | 8527 | return http_get_hdr(&txn->req, arg_p->data.str.str, arg_p->data.str.len, &txn->hdr_idx, |
| 8528 | -1, NULL, &data->str.str, &data->str.len); |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8529 | } |
| 8530 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8531 | /* |
| 8532 | * Given a path string and its length, find the position of beginning of the |
| 8533 | * query string. Returns NULL if no query string is found in the path. |
| 8534 | * |
| 8535 | * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22: |
| 8536 | * |
| 8537 | * find_query_string(path, n) points to "yo=mama;ye=daddy" string. |
| 8538 | */ |
| 8539 | static inline char *find_query_string(char *path, size_t path_l) |
| 8540 | { |
| 8541 | char *p; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 8542 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8543 | p = memchr(path, '?', path_l); |
| 8544 | return p ? p + 1 : NULL; |
| 8545 | } |
| 8546 | |
| 8547 | static inline int is_param_delimiter(char c) |
| 8548 | { |
| 8549 | return c == '&' || c == ';'; |
| 8550 | } |
| 8551 | |
| 8552 | /* |
| 8553 | * Given a url parameter, find the starting position of the first occurence, |
| 8554 | * or NULL if the parameter is not found. |
| 8555 | * |
| 8556 | * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye", |
| 8557 | * the function will return query_string+8. |
| 8558 | */ |
| 8559 | static char* |
| 8560 | find_url_param_pos(char* query_string, size_t query_string_l, |
| 8561 | char* url_param_name, size_t url_param_name_l) |
| 8562 | { |
| 8563 | char *pos, *last; |
| 8564 | |
| 8565 | pos = query_string; |
| 8566 | last = query_string + query_string_l - url_param_name_l - 1; |
| 8567 | |
| 8568 | while (pos <= last) { |
| 8569 | if (pos[url_param_name_l] == '=') { |
| 8570 | if (memcmp(pos, url_param_name, url_param_name_l) == 0) |
| 8571 | return pos; |
| 8572 | pos += url_param_name_l + 1; |
| 8573 | } |
| 8574 | while (pos <= last && !is_param_delimiter(*pos)) |
| 8575 | pos++; |
| 8576 | pos++; |
| 8577 | } |
| 8578 | return NULL; |
| 8579 | } |
| 8580 | |
| 8581 | /* |
| 8582 | * Given a url parameter name, returns its value and size into *value and |
| 8583 | * *value_l respectively, and returns non-zero. If the parameter is not found, |
| 8584 | * zero is returned and value/value_l are not touched. |
| 8585 | */ |
| 8586 | static int |
| 8587 | find_url_param_value(char* path, size_t path_l, |
| 8588 | char* url_param_name, size_t url_param_name_l, |
| 8589 | char** value, size_t* value_l) |
| 8590 | { |
| 8591 | char *query_string, *qs_end; |
| 8592 | char *arg_start; |
| 8593 | char *value_start, *value_end; |
| 8594 | |
| 8595 | query_string = find_query_string(path, path_l); |
| 8596 | if (!query_string) |
| 8597 | return 0; |
| 8598 | |
| 8599 | qs_end = path + path_l; |
| 8600 | arg_start = find_url_param_pos(query_string, qs_end - query_string, |
| 8601 | url_param_name, url_param_name_l); |
| 8602 | if (!arg_start) |
| 8603 | return 0; |
| 8604 | |
| 8605 | value_start = arg_start + url_param_name_l + 1; |
| 8606 | value_end = value_start; |
| 8607 | |
| 8608 | while ((value_end < qs_end) && !is_param_delimiter(*value_end)) |
| 8609 | value_end++; |
| 8610 | |
| 8611 | *value = value_start; |
| 8612 | *value_l = value_end - value_start; |
Willy Tarreau | 0013433 | 2011-01-04 14:57:34 +0100 | [diff] [blame] | 8613 | return value_end != value_start; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8614 | } |
| 8615 | |
| 8616 | static int |
| 8617 | pattern_fetch_url_param(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8618 | const struct pattern_arg *arg_p, int arg_i, union pattern_data *data) |
| 8619 | { |
| 8620 | struct http_txn *txn = l7; |
| 8621 | struct http_msg *msg = &txn->req; |
| 8622 | char *url_param_value; |
| 8623 | size_t url_param_value_l; |
| 8624 | |
| 8625 | if (!find_url_param_value(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l, |
| 8626 | arg_p->data.str.str, arg_p->data.str.len, |
| 8627 | &url_param_value, &url_param_value_l)) |
| 8628 | return 0; |
| 8629 | |
| 8630 | data->str.str = url_param_value; |
| 8631 | data->str.len = url_param_value_l; |
| 8632 | return 1; |
| 8633 | } |
| 8634 | |
Willy Tarreau | b3eb221 | 2011-07-01 16:16:17 +0200 | [diff] [blame] | 8635 | /* Try to find the last occurrence of a cookie name in a cookie header value. |
| 8636 | * The pointer and size of the last occurrence of the cookie value is returned |
| 8637 | * into *value and *value_l, and the function returns non-zero if the value was |
| 8638 | * found. Otherwise if the cookie was not found, zero is returned and neither |
| 8639 | * value nor value_l are touched. The input hdr string should begin at the |
| 8640 | * header's value, and its size should be in hdr_l. <list> must be non-zero if |
| 8641 | * value may represent a list of values (cookie headers). |
| 8642 | */ |
| 8643 | static int |
| 8644 | extract_cookie_value(char *hdr, size_t hdr_l, |
| 8645 | char *cookie_name, size_t cookie_name_l, int list, |
| 8646 | char **value, size_t *value_l) |
| 8647 | { |
| 8648 | int found = 0; |
| 8649 | char *equal, *att_end, *att_beg, *hdr_end, *val_beg, *val_end; |
| 8650 | char *next; |
| 8651 | |
| 8652 | /* Note that multiple cookies may be delimited with semi-colons, so we |
| 8653 | * also have to loop on this. |
| 8654 | */ |
| 8655 | |
| 8656 | /* we search at least a cookie name followed by an equal, and more |
| 8657 | * generally something like this : |
| 8658 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 8659 | */ |
| 8660 | if (hdr_l < cookie_name_l + 1) |
| 8661 | return 0; |
| 8662 | |
| 8663 | hdr_end = hdr + hdr_l; |
| 8664 | |
| 8665 | for (att_beg = hdr; att_beg < hdr_end; att_beg = next + 1) { |
| 8666 | /* Iterate through all cookies on this line */ |
| 8667 | |
| 8668 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 8669 | att_beg++; |
| 8670 | |
| 8671 | /* find att_end : this is the first character after the last non |
| 8672 | * space before the equal. It may be equal to hdr_end. |
| 8673 | */ |
| 8674 | equal = att_end = att_beg; |
| 8675 | |
| 8676 | while (equal < hdr_end) { |
| 8677 | if (*equal == '=' || *equal == ';' || (list && *equal == ',')) |
| 8678 | break; |
| 8679 | if (http_is_spht[(unsigned char)*equal++]) |
| 8680 | continue; |
| 8681 | att_end = equal; |
| 8682 | } |
| 8683 | |
| 8684 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 8685 | * is between <att_beg> and <equal>, both may be identical. |
| 8686 | */ |
| 8687 | |
| 8688 | /* look for end of cookie if there is an equal sign */ |
| 8689 | if (equal < hdr_end && *equal == '=') { |
| 8690 | /* look for the beginning of the value */ |
| 8691 | val_beg = equal + 1; |
| 8692 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 8693 | val_beg++; |
| 8694 | |
| 8695 | /* find the end of the value, respecting quotes */ |
| 8696 | next = find_cookie_value_end(val_beg, hdr_end); |
| 8697 | |
| 8698 | /* make val_end point to the first white space or delimitor after the value */ |
| 8699 | val_end = next; |
| 8700 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 8701 | val_end--; |
| 8702 | } else { |
| 8703 | val_beg = val_end = next = equal; |
| 8704 | } |
| 8705 | |
| 8706 | /* We have nothing to do with attributes beginning with '$'. However, |
| 8707 | * they will automatically be removed if a header before them is removed, |
| 8708 | * since they're supposed to be linked together. |
| 8709 | */ |
| 8710 | if (*att_beg == '$') |
| 8711 | continue; |
| 8712 | |
| 8713 | /* Ignore cookies with no equal sign */ |
| 8714 | if (equal == next) |
| 8715 | continue; |
| 8716 | |
| 8717 | /* Now we have the cookie name between att_beg and att_end, and |
| 8718 | * its value between val_beg and val_end. |
| 8719 | */ |
| 8720 | |
| 8721 | if (att_end - att_beg == cookie_name_l && |
| 8722 | memcmp(att_beg, cookie_name, cookie_name_l) == 0) { |
| 8723 | found = 1; |
| 8724 | *value = val_beg; |
| 8725 | *value_l = val_end - val_beg; |
| 8726 | /* right now we want to catch the last occurrence |
| 8727 | * of the cookie, so let's go on searching. |
| 8728 | */ |
| 8729 | } |
| 8730 | |
| 8731 | /* Set-Cookie headers only have the name in the first attr=value part */ |
| 8732 | if (!list) |
| 8733 | break; |
| 8734 | } |
| 8735 | |
| 8736 | return found; |
| 8737 | } |
| 8738 | |
| 8739 | /* Try to find in request or response message is in <msg> and whose transaction |
| 8740 | * is in <txn> the last occurrence of a cookie name in all cookie header values |
| 8741 | * whose header name is <hdr_name> with name of length <hdr_name_len>. The |
| 8742 | * pointer and size of the last occurrence of the cookie value is returned into |
| 8743 | * <value> and <value_l>, and the function returns non-zero if the value was |
| 8744 | * found. Otherwise if the cookie was not found, zero is returned and neither |
| 8745 | * value nor value_l are touched. The input hdr string should begin at the |
| 8746 | * header's value, and its size should be in hdr_l. <list> must be non-zero if |
| 8747 | * value may represent a list of values (cookie headers). |
| 8748 | */ |
| 8749 | |
| 8750 | static int |
| 8751 | find_cookie_value(struct http_msg *msg, struct http_txn *txn, |
| 8752 | const char *hdr_name, int hdr_name_len, |
| 8753 | char *cookie_name, size_t cookie_name_l, int list, |
| 8754 | char **value, size_t *value_l) |
| 8755 | { |
| 8756 | struct hdr_ctx ctx; |
| 8757 | int found = 0; |
| 8758 | |
| 8759 | ctx.idx = 0; |
| 8760 | while (http_find_header2(hdr_name, hdr_name_len, msg->sol, &txn->hdr_idx, &ctx)) { |
| 8761 | if (ctx.vlen < cookie_name_l + 1) |
| 8762 | continue; |
| 8763 | |
| 8764 | found |= extract_cookie_value(ctx.line + ctx.val, ctx.vlen, |
| 8765 | cookie_name, cookie_name_l, 1, |
| 8766 | value, value_l); |
| 8767 | } |
| 8768 | return found; |
| 8769 | } |
| 8770 | |
| 8771 | static int |
| 8772 | pattern_fetch_cookie(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8773 | const struct pattern_arg *arg_p, int arg_i, union pattern_data *data) |
| 8774 | { |
| 8775 | struct http_txn *txn = l7; |
| 8776 | struct http_msg *msg = &txn->req; |
| 8777 | char *cookie_value; |
| 8778 | size_t cookie_value_l; |
| 8779 | int found = 0; |
| 8780 | |
| 8781 | found = find_cookie_value(msg, txn, "Cookie", 6, |
| 8782 | arg_p->data.str.str, arg_p->data.str.len, 1, |
| 8783 | &cookie_value, &cookie_value_l); |
| 8784 | if (found) { |
| 8785 | data->str.str = cookie_value; |
| 8786 | data->str.len = cookie_value_l; |
| 8787 | } |
| 8788 | |
| 8789 | return found; |
| 8790 | } |
| 8791 | |
| 8792 | |
| 8793 | static int |
| 8794 | pattern_fetch_set_cookie(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8795 | const struct pattern_arg *arg_p, int arg_i, union pattern_data *data) |
| 8796 | { |
| 8797 | struct http_txn *txn = l7; |
| 8798 | struct http_msg *msg = &txn->rsp; |
| 8799 | char *cookie_value; |
| 8800 | size_t cookie_value_l; |
| 8801 | int found = 0; |
| 8802 | |
| 8803 | found = find_cookie_value(msg, txn, "Set-Cookie", 10, |
| 8804 | arg_p->data.str.str, arg_p->data.str.len, 1, |
| 8805 | &cookie_value, &cookie_value_l); |
| 8806 | if (found) { |
| 8807 | data->str.str = cookie_value; |
| 8808 | data->str.len = cookie_value_l; |
| 8809 | } |
| 8810 | |
| 8811 | return found; |
| 8812 | } |
| 8813 | |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 8814 | |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8815 | /************************************************************************/ |
| 8816 | /* All supported keywords must be declared here. */ |
| 8817 | /************************************************************************/ |
| 8818 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 8819 | static struct pattern_fetch_kw_list pattern_fetch_keywords = {{ },{ |
Willy Tarreau | e428fb7 | 2011-12-16 21:50:30 +0100 | [diff] [blame] | 8820 | { "hdr", pattern_fetch_hdr, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ }, |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8821 | { "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] | 8822 | { "cookie", pattern_fetch_cookie, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ }, |
| 8823 | { "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] | 8824 | { NULL, NULL, NULL, 0, 0 }, |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8825 | }}; |
| 8826 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8827 | |
| 8828 | __attribute__((constructor)) |
| 8829 | static void __http_protocol_init(void) |
| 8830 | { |
| 8831 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8832 | pattern_register_fetches(&pattern_fetch_keywords); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8833 | } |
| 8834 | |
| 8835 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8836 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 8837 | * Local variables: |
| 8838 | * c-indent-level: 8 |
| 8839 | * c-basic-offset: 8 |
| 8840 | * End: |
| 8841 | */ |