Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HTTP protocol analyzer |
| 3 | * |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 4 | * Copyright 2000-2011 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <ctype.h> |
| 14 | #include <errno.h> |
| 15 | #include <fcntl.h> |
| 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
| 19 | #include <syslog.h> |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 20 | #include <time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 21 | |
| 22 | #include <sys/socket.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <sys/types.h> |
| 25 | |
Willy Tarreau | b05405a | 2012-01-23 15:35:52 +0100 | [diff] [blame] | 26 | #include <netinet/tcp.h> |
| 27 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 28 | #include <common/base64.h> |
Frédéric Lécaille | a41d531 | 2018-01-29 12:05:07 +0100 | [diff] [blame] | 29 | #include <common/cfgparse.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 30 | #include <common/chunk.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 31 | #include <common/compat.h> |
| 32 | #include <common/config.h> |
Willy Tarreau | a4cd1f5 | 2006-12-16 19:57:26 +0100 | [diff] [blame] | 33 | #include <common/debug.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 34 | #include <common/memory.h> |
| 35 | #include <common/mini-clist.h> |
| 36 | #include <common/standard.h> |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 37 | #include <common/ticks.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 38 | #include <common/time.h> |
| 39 | #include <common/uri_auth.h> |
| 40 | #include <common/version.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 41 | |
| 42 | #include <types/capture.h> |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 43 | #include <types/cli.h> |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 44 | #include <types/filters.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 45 | #include <types/global.h> |
William Lallemand | 71bd11a | 2017-11-20 19:13:14 +0100 | [diff] [blame] | 46 | #include <types/cache.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 47 | #include <types/stats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 48 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 49 | #include <proto/acl.h> |
Thierry FOURNIER | 322a124 | 2015-08-19 09:07:47 +0200 | [diff] [blame] | 50 | #include <proto/action.h> |
Willy Tarreau | 61612d4 | 2012-04-19 18:42:05 +0200 | [diff] [blame] | 51 | #include <proto/arg.h> |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 52 | #include <proto/auth.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 53 | #include <proto/backend.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 54 | #include <proto/channel.h> |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 55 | #include <proto/checks.h> |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 56 | #include <proto/cli.h> |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 57 | #include <proto/compression.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 58 | #include <proto/stats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 59 | #include <proto/fd.h> |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 60 | #include <proto/filters.h> |
Willy Tarreau | 03fa5df | 2010-05-24 21:02:37 +0200 | [diff] [blame] | 61 | #include <proto/frontend.h> |
Willy Tarreau | 0da5b3b | 2017-09-21 09:30:46 +0200 | [diff] [blame] | 62 | #include <proto/h1.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 63 | #include <proto/log.h> |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 64 | #include <proto/hdr_idx.h> |
Patrick Hemmer | e3faf02 | 2018-08-22 10:02:00 -0400 | [diff] [blame] | 65 | #include <proto/hlua.h> |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 66 | #include <proto/pattern.h> |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 67 | #include <proto/proto_tcp.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 68 | #include <proto/proto_http.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 69 | #include <proto/proxy.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 70 | #include <proto/queue.h> |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 71 | #include <proto/sample.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 72 | #include <proto/server.h> |
Olivier Houchard | 985f139 | 2018-11-30 17:31:52 +0100 | [diff] [blame] | 73 | #include <proto/session.h> |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 74 | #include <proto/stream.h> |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 75 | #include <proto/stream_interface.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 76 | #include <proto/task.h> |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 77 | #include <proto/pattern.h> |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 78 | #include <proto/vars.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 79 | |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 80 | /* status codes available for the stats admin page (strictly 4 chars length) */ |
| 81 | const char *stat_status_codes[STAT_STATUS_SIZE] = { |
| 82 | [STAT_STATUS_DENY] = "DENY", |
| 83 | [STAT_STATUS_DONE] = "DONE", |
| 84 | [STAT_STATUS_ERRP] = "ERRP", |
| 85 | [STAT_STATUS_EXCD] = "EXCD", |
| 86 | [STAT_STATUS_NONE] = "NONE", |
| 87 | [STAT_STATUS_PART] = "PART", |
| 88 | [STAT_STATUS_UNKN] = "UNKN", |
| 89 | }; |
| 90 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 91 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 92 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
Willy Tarreau | 4d893d4 | 2018-07-12 15:43:32 +0200 | [diff] [blame] | 93 | * the last CRLF. <len> bytes are copied, not counting the CRLF. |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 94 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 95 | * of headers is automatically adjusted. The number of bytes added is returned |
| 96 | * on success, otherwise <0 is returned indicating an error. |
| 97 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 98 | int http_header_add_tail2(struct http_msg *msg, |
| 99 | struct hdr_idx *hdr_idx, const char *text, int len) |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 100 | { |
| 101 | int bytes; |
| 102 | |
Willy Tarreau | 4d893d4 | 2018-07-12 15:43:32 +0200 | [diff] [blame] | 103 | bytes = ci_insert_line2(msg->chn, msg->eoh, text, len); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 104 | if (!bytes) |
| 105 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 106 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 107 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 108 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 109 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 110 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 111 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 112 | * structure holds everything necessary to use the header and find next |
| 113 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 114 | * beginning. Otherwise, the next occurrence is returned. The function returns |
| 115 | * 1 when it finds a value, and 0 when there is no more. It is very similar to |
| 116 | * http_find_header2() except that it is designed to work with full-line headers |
| 117 | * whose comma is not a delimiter but is part of the syntax. As a special case, |
| 118 | * if ctx->val is NULL when searching for a new values of a header, the current |
| 119 | * header is rescanned. This allows rescanning after a header deletion. |
| 120 | */ |
| 121 | int http_find_full_header2(const char *name, int len, |
| 122 | char *sol, struct hdr_idx *idx, |
| 123 | struct hdr_ctx *ctx) |
| 124 | { |
| 125 | char *eol, *sov; |
| 126 | int cur_idx, old_idx; |
| 127 | |
| 128 | cur_idx = ctx->idx; |
| 129 | if (cur_idx) { |
| 130 | /* We have previously returned a header, let's search another one */ |
| 131 | sol = ctx->line; |
| 132 | eol = sol + idx->v[cur_idx].len; |
| 133 | goto next_hdr; |
| 134 | } |
| 135 | |
| 136 | /* first request for this header */ |
| 137 | sol += hdr_idx_first_pos(idx); |
| 138 | old_idx = 0; |
| 139 | cur_idx = hdr_idx_first_idx(idx); |
| 140 | while (cur_idx) { |
| 141 | eol = sol + idx->v[cur_idx].len; |
| 142 | |
| 143 | if (len == 0) { |
| 144 | /* No argument was passed, we want any header. |
| 145 | * To achieve this, we simply build a fake request. */ |
| 146 | while (sol + len < eol && sol[len] != ':') |
| 147 | len++; |
| 148 | name = sol; |
| 149 | } |
| 150 | |
| 151 | if ((len < eol - sol) && |
| 152 | (sol[len] == ':') && |
| 153 | (strncasecmp(sol, name, len) == 0)) { |
| 154 | ctx->del = len; |
| 155 | sov = sol + len + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 156 | while (sov < eol && HTTP_IS_LWS(*sov)) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 157 | sov++; |
| 158 | |
| 159 | ctx->line = sol; |
| 160 | ctx->prev = old_idx; |
| 161 | ctx->idx = cur_idx; |
| 162 | ctx->val = sov - sol; |
| 163 | ctx->tws = 0; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 164 | while (eol > sov && HTTP_IS_LWS(*(eol - 1))) { |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 165 | eol--; |
| 166 | ctx->tws++; |
| 167 | } |
| 168 | ctx->vlen = eol - sov; |
| 169 | return 1; |
| 170 | } |
| 171 | next_hdr: |
| 172 | sol = eol + idx->v[cur_idx].cr + 1; |
| 173 | old_idx = cur_idx; |
| 174 | cur_idx = idx->v[cur_idx].next; |
| 175 | } |
| 176 | return 0; |
| 177 | } |
| 178 | |
Willy Tarreau | c90dc23 | 2015-02-20 13:51:36 +0100 | [diff] [blame] | 179 | /* Find the first or next header field in message buffer <sol> using headers |
| 180 | * index <idx>, and return it in the <ctx> structure. This structure holds |
| 181 | * everything necessary to use the header and find next occurrence. If its |
| 182 | * <idx> member is 0, the first header is retrieved. Otherwise, the next |
| 183 | * occurrence is returned. The function returns 1 when it finds a value, and |
| 184 | * 0 when there is no more. It is equivalent to http_find_full_header2() with |
| 185 | * no header name. |
| 186 | */ |
| 187 | int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx) |
| 188 | { |
| 189 | char *eol, *sov; |
| 190 | int cur_idx, old_idx; |
| 191 | int len; |
| 192 | |
| 193 | cur_idx = ctx->idx; |
| 194 | if (cur_idx) { |
| 195 | /* We have previously returned a header, let's search another one */ |
| 196 | sol = ctx->line; |
| 197 | eol = sol + idx->v[cur_idx].len; |
| 198 | goto next_hdr; |
| 199 | } |
| 200 | |
| 201 | /* first request for this header */ |
| 202 | sol += hdr_idx_first_pos(idx); |
| 203 | old_idx = 0; |
| 204 | cur_idx = hdr_idx_first_idx(idx); |
| 205 | while (cur_idx) { |
| 206 | eol = sol + idx->v[cur_idx].len; |
| 207 | |
| 208 | len = 0; |
| 209 | while (1) { |
| 210 | if (len >= eol - sol) |
| 211 | goto next_hdr; |
| 212 | if (sol[len] == ':') |
| 213 | break; |
| 214 | len++; |
| 215 | } |
| 216 | |
| 217 | ctx->del = len; |
| 218 | sov = sol + len + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 219 | while (sov < eol && HTTP_IS_LWS(*sov)) |
Willy Tarreau | c90dc23 | 2015-02-20 13:51:36 +0100 | [diff] [blame] | 220 | sov++; |
| 221 | |
| 222 | ctx->line = sol; |
| 223 | ctx->prev = old_idx; |
| 224 | ctx->idx = cur_idx; |
| 225 | ctx->val = sov - sol; |
| 226 | ctx->tws = 0; |
| 227 | |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 228 | while (eol > sov && HTTP_IS_LWS(*(eol - 1))) { |
Willy Tarreau | c90dc23 | 2015-02-20 13:51:36 +0100 | [diff] [blame] | 229 | eol--; |
| 230 | ctx->tws++; |
| 231 | } |
| 232 | ctx->vlen = eol - sov; |
| 233 | return 1; |
| 234 | |
| 235 | next_hdr: |
| 236 | sol = eol + idx->v[cur_idx].cr + 1; |
| 237 | old_idx = cur_idx; |
| 238 | cur_idx = idx->v[cur_idx].next; |
| 239 | } |
| 240 | return 0; |
| 241 | } |
| 242 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 243 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 244 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 245 | * structure holds everything necessary to use the header and find next |
| 246 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 247 | * beginning. Otherwise, the next occurrence is returned. The function returns |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 248 | * 1 when it finds a value, and 0 when there is no more. It is designed to work |
| 249 | * with headers defined as comma-separated lists. As a special case, if ctx->val |
| 250 | * is NULL when searching for a new values of a header, the current header is |
| 251 | * rescanned. This allows rescanning after a header deletion. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 252 | */ |
| 253 | int http_find_header2(const char *name, int len, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 254 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 255 | struct hdr_ctx *ctx) |
| 256 | { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 257 | char *eol, *sov; |
| 258 | int cur_idx, old_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 259 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 260 | cur_idx = ctx->idx; |
| 261 | if (cur_idx) { |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 262 | /* We have previously returned a value, let's search |
| 263 | * another one on the same line. |
| 264 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 265 | sol = ctx->line; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 266 | ctx->del = ctx->val + ctx->vlen + ctx->tws; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 267 | sov = sol + ctx->del; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 268 | eol = sol + idx->v[cur_idx].len; |
| 269 | |
| 270 | if (sov >= eol) |
| 271 | /* no more values in this header */ |
| 272 | goto next_hdr; |
| 273 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 274 | /* values remaining for this header, skip the comma but save it |
| 275 | * for later use (eg: for header deletion). |
| 276 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 277 | sov++; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 278 | while (sov < eol && HTTP_IS_LWS((*sov))) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 279 | sov++; |
| 280 | |
| 281 | goto return_hdr; |
| 282 | } |
| 283 | |
| 284 | /* first request for this header */ |
| 285 | sol += hdr_idx_first_pos(idx); |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 286 | old_idx = 0; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 287 | cur_idx = hdr_idx_first_idx(idx); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 288 | while (cur_idx) { |
| 289 | eol = sol + idx->v[cur_idx].len; |
| 290 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 291 | if (len == 0) { |
| 292 | /* No argument was passed, we want any header. |
| 293 | * To achieve this, we simply build a fake request. */ |
| 294 | while (sol + len < eol && sol[len] != ':') |
| 295 | len++; |
| 296 | name = sol; |
| 297 | } |
| 298 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 299 | if ((len < eol - sol) && |
| 300 | (sol[len] == ':') && |
| 301 | (strncasecmp(sol, name, len) == 0)) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 302 | ctx->del = len; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 303 | sov = sol + len + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 304 | while (sov < eol && HTTP_IS_LWS(*sov)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 305 | sov++; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 306 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 307 | ctx->line = sol; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 308 | ctx->prev = old_idx; |
| 309 | return_hdr: |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 310 | ctx->idx = cur_idx; |
| 311 | ctx->val = sov - sol; |
| 312 | |
Willy Tarreau | ab813a4 | 2018-09-10 18:41:28 +0200 | [diff] [blame] | 313 | eol = http_find_hdr_value_end(sov, eol); |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 314 | ctx->tws = 0; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 315 | while (eol > sov && HTTP_IS_LWS(*(eol - 1))) { |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 316 | eol--; |
| 317 | ctx->tws++; |
| 318 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 319 | ctx->vlen = eol - sov; |
| 320 | return 1; |
| 321 | } |
| 322 | next_hdr: |
| 323 | sol = eol + idx->v[cur_idx].cr + 1; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 324 | old_idx = cur_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 325 | cur_idx = idx->v[cur_idx].next; |
| 326 | } |
| 327 | return 0; |
| 328 | } |
| 329 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 330 | /* Remove one value of a header. This only works on a <ctx> returned by one of |
| 331 | * the http_find_header functions. The value is removed, as well as surrounding |
| 332 | * commas if any. If the removed value was alone, the whole header is removed. |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 333 | * The ctx is always updated accordingly, as well as the buffer and HTTP |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 334 | * message <msg>. The new index is returned. If it is zero, it means there is |
| 335 | * no more header, so any processing may stop. The ctx is always left in a form |
| 336 | * that can be handled by http_find_header2() to find next occurrence. |
| 337 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 338 | int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx) |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 339 | { |
| 340 | int cur_idx = ctx->idx; |
| 341 | char *sol = ctx->line; |
| 342 | struct hdr_idx_elem *hdr; |
| 343 | int delta, skip_comma; |
| 344 | |
| 345 | if (!cur_idx) |
| 346 | return 0; |
| 347 | |
| 348 | hdr = &idx->v[cur_idx]; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 349 | if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 350 | /* This was the only value of the header, we must now remove it entirely. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 351 | delta = b_rep_blk(&msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0); |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 352 | http_msg_move_end(msg, delta); |
| 353 | idx->used--; |
| 354 | hdr->len = 0; /* unused entry */ |
| 355 | idx->v[ctx->prev].next = idx->v[ctx->idx].next; |
Willy Tarreau | 5c4784f | 2011-02-12 13:07:35 +0100 | [diff] [blame] | 356 | if (idx->tail == ctx->idx) |
| 357 | idx->tail = ctx->prev; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 358 | ctx->idx = ctx->prev; /* walk back to the end of previous header */ |
Willy Tarreau | 7c1c217 | 2015-01-07 17:23:50 +0100 | [diff] [blame] | 359 | ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 360 | 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] | 361 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 362 | return ctx->idx; |
| 363 | } |
| 364 | |
| 365 | /* 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] | 366 | * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the |
| 367 | * last entry of the list, we remove the last separator. |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 368 | */ |
| 369 | |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 370 | skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 371 | delta = b_rep_blk(&msg->chn->buf, sol + ctx->del + skip_comma, |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 372 | sol + ctx->val + ctx->vlen + ctx->tws + skip_comma, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 373 | NULL, 0); |
| 374 | hdr->len += delta; |
| 375 | http_msg_move_end(msg, delta); |
| 376 | ctx->val = ctx->del; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 377 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 378 | return ctx->idx; |
| 379 | } |
| 380 | |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 381 | /* This function handles a server error at the stream interface level. The |
| 382 | * stream interface is assumed to be already in a closed state. An optional |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 383 | * message is copied into the input buffer. |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 384 | * 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] | 385 | * in this buffer will be lost. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 386 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 387 | static void http_server_error(struct stream *s, struct stream_interface *si, |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 388 | int err, int finst, const struct buffer *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 389 | { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 390 | if (IS_HTX_STRM(s)) |
| 391 | return htx_server_error(s, si, err, finst, msg); |
| 392 | |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 393 | FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg)); |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 394 | channel_auto_read(si_oc(si)); |
| 395 | channel_abort(si_oc(si)); |
| 396 | channel_auto_close(si_oc(si)); |
| 397 | channel_erase(si_oc(si)); |
| 398 | channel_auto_close(si_ic(si)); |
| 399 | channel_auto_read(si_ic(si)); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 400 | if (msg) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 401 | co_inject(si_ic(si), msg->area, msg->data); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 402 | if (!(s->flags & SF_ERR_MASK)) |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 403 | s->flags |= err; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 404 | if (!(s->flags & SF_FINST_MASK)) |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 405 | s->flags |= finst; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 406 | } |
| 407 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 408 | /* This function returns the appropriate error location for the given stream |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 409 | * and message. |
| 410 | */ |
| 411 | |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 412 | struct buffer *http_error_message(struct stream *s) |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 413 | { |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 414 | const int msgnum = http_get_status_idx(s->txn->status); |
| 415 | |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 416 | if (IS_HTX_STRM(s)) |
| 417 | return htx_error_message(s); |
| 418 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 419 | if (s->be->errmsg[msgnum].area) |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 420 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 421 | else if (strm_fe(s)->errmsg[msgnum].area) |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 422 | return &strm_fe(s)->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 423 | else |
| 424 | return &http_err_chunks[msgnum]; |
| 425 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 426 | |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 427 | void |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 428 | http_reply_and_close(struct stream *s, short status, struct buffer *msg) |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 429 | { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 430 | if (IS_HTX_STRM(s)) |
| 431 | return htx_reply_and_close(s, status, msg); |
| 432 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 433 | s->txn->flags &= ~TX_WAIT_NEXT_RQ; |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 434 | FLT_STRM_CB(s, flt_http_reply(s, status, msg)); |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 435 | stream_int_retnclose(&s->si[0], msg); |
| 436 | } |
| 437 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 438 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 439 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 440 | * It is returned otherwise. |
| 441 | */ |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 442 | char *http_txn_get_path(const struct http_txn *txn) |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 443 | { |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 444 | struct ist ret; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 445 | |
Willy Tarreau | 9d9ccdb | 2018-10-28 20:13:12 +0100 | [diff] [blame] | 446 | if (!txn->req.chn->buf.size) |
| 447 | return NULL; |
| 448 | |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 449 | ret = http_get_path(ist2(ci_head(txn->req.chn) + txn->req.sl.rq.u, txn->req.sl.rq.u_l)); |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 450 | |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 451 | return ret.ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 452 | } |
| 453 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 454 | /* Returns a 302 for a redirectable request that reaches a server working in |
| 455 | * in redirect mode. This may only be called just after the stream interface |
| 456 | * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will |
| 457 | * follow normal proxy processing. NOTE: this function is designed to support |
| 458 | * being called once data are scheduled for forwarding. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 459 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 460 | void http_perform_server_redirect(struct stream *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 461 | { |
| 462 | struct http_txn *txn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 463 | struct server *srv; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 464 | char *path; |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 465 | int len, rewind; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 466 | |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 467 | if (IS_HTX_STRM(s)) |
| 468 | return htx_perform_server_redirect(s, si); |
| 469 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 470 | /* 1: create the response header */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 471 | trash.data = strlen(HTTP_302); |
| 472 | memcpy(trash.area, HTTP_302, trash.data); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 473 | |
Willy Tarreau | b05e48a | 2018-09-20 11:12:58 +0200 | [diff] [blame] | 474 | srv = __objt_server(s->target); |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 475 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 476 | /* 2: add the server's prefix */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 477 | if (trash.data + srv->rdr_len > trash.size) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 478 | return; |
| 479 | |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 480 | /* special prefix "/" means don't change URL */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 481 | if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 482 | memcpy(trash.area + trash.data, srv->rdr_pfx, srv->rdr_len); |
| 483 | trash.data += srv->rdr_len; |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 484 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 485 | |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 486 | /* 3: add the request URI. Since it was already forwarded, we need |
| 487 | * to temporarily rewind the buffer. |
| 488 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 489 | txn = s->txn; |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 490 | c_rew(&s->req, rewind = http_hdr_rewind(&txn->req)); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 491 | |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 492 | path = http_txn_get_path(txn); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 493 | len = b_dist(&s->req.buf, path, c_ptr(&s->req, txn->req.sl.rq.u + txn->req.sl.rq.u_l)); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 494 | |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 495 | c_adv(&s->req, rewind); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 496 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 497 | if (!path) |
| 498 | return; |
| 499 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 500 | if (trash.data + len > trash.size - 4) /* 4 for CRLF-CRLF */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 501 | return; |
| 502 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 503 | memcpy(trash.area + trash.data, path, len); |
| 504 | trash.data += len; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 505 | |
| 506 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 507 | memcpy(trash.area + trash.data, |
| 508 | "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 509 | trash.data += 29; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 510 | } else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 511 | memcpy(trash.area + trash.data, |
| 512 | "\r\nConnection: close\r\n\r\n", 23); |
| 513 | trash.data += 23; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 514 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 515 | |
| 516 | /* prepare to return without error. */ |
Willy Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 517 | si_shutr(si); |
| 518 | si_shutw(si); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 519 | si->err_type = SI_ET_NONE; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 520 | si->state = SI_ST_CLO; |
| 521 | |
| 522 | /* send the message */ |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 523 | txn->status = 302; |
| 524 | http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, &trash); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 525 | |
| 526 | /* FIXME: we should increase a counter of redirects per server and per backend. */ |
Willy Tarreau | 4521ba6 | 2013-01-24 01:25:25 +0100 | [diff] [blame] | 527 | srv_inc_sess_ctr(srv); |
Bhaskar Maddala | a20cb85 | 2014-02-03 16:26:46 -0500 | [diff] [blame] | 528 | srv_set_sess_last(srv); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 529 | } |
| 530 | |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 531 | /* Return the error message corresponding to si->err_type. It is assumed |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 532 | * that the server side is closed. Note that err_type is actually a |
| 533 | * bitmask, where almost only aborts may be cumulated with other |
| 534 | * values. We consider that aborted operations are more important |
| 535 | * than timeouts or errors due to the fact that nobody else in the |
| 536 | * logs might explain incomplete retries. All others should avoid |
| 537 | * being cumulated. It should normally not be possible to have multiple |
| 538 | * aborts at once, but just in case, the first one in sequence is reported. |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 539 | * Note that connection errors appearing on the second request of a keep-alive |
| 540 | * connection are not reported since this allows the client to retry. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 541 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 542 | void http_return_srv_error(struct stream *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 543 | { |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 544 | int err_type = si->err_type; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 545 | |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 546 | /* set s->txn->status for http_error_message(s) */ |
| 547 | s->txn->status = 503; |
| 548 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 549 | if (err_type & SI_ET_QUEUE_ABRT) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 550 | http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 551 | http_error_message(s)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 552 | else if (err_type & SI_ET_CONN_ABRT) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 553 | http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 554 | (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 555 | http_error_message(s)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 556 | else if (err_type & SI_ET_QUEUE_TO) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 557 | http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 558 | http_error_message(s)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 559 | else if (err_type & SI_ET_QUEUE_ERR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 560 | http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 561 | http_error_message(s)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 562 | else if (err_type & SI_ET_CONN_TO) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 563 | http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 564 | (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 565 | http_error_message(s)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 566 | else if (err_type & SI_ET_CONN_ERR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 567 | http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 568 | (s->flags & SF_SRV_REUSED) ? NULL : |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 569 | http_error_message(s)); |
Willy Tarreau | 2d400bb | 2012-05-14 12:11:47 +0200 | [diff] [blame] | 570 | else if (err_type & SI_ET_CONN_RES) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 571 | http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 572 | (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 573 | http_error_message(s)); |
| 574 | else { /* SI_ET_CONN_OTHER and others */ |
| 575 | s->txn->status = 500; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 576 | http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 577 | http_error_message(s)); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 578 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 579 | } |
| 580 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 581 | extern const char sess_term_cond[8]; |
| 582 | extern const char sess_fin_state[8]; |
| 583 | extern const char *monthname[12]; |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 584 | |
| 585 | DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn)); |
| 586 | DECLARE_POOL(pool_head_uniqueid, "uniqueid", UNIQUEID_LEN); |
| 587 | |
| 588 | struct pool_head *pool_head_requri = NULL; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 589 | struct pool_head *pool_head_capture = NULL; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 590 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 591 | /* |
| 592 | * Capture headers from message starting at <som> according to header list |
Willy Tarreau | 54da8db | 2014-06-13 16:11:48 +0200 | [diff] [blame] | 593 | * <cap_hdr>, and fill the <cap> pointers appropriately. |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 594 | */ |
Christopher Faulet | 10079f5 | 2018-10-03 15:17:28 +0200 | [diff] [blame] | 595 | void http_capture_headers(char *som, struct hdr_idx *idx, |
| 596 | char **cap, struct cap_hdr *cap_hdr) |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 597 | { |
| 598 | char *eol, *sol, *col, *sov; |
| 599 | int cur_idx; |
| 600 | struct cap_hdr *h; |
| 601 | int len; |
| 602 | |
| 603 | sol = som + hdr_idx_first_pos(idx); |
| 604 | cur_idx = hdr_idx_first_idx(idx); |
| 605 | |
| 606 | while (cur_idx) { |
| 607 | eol = sol + idx->v[cur_idx].len; |
| 608 | |
| 609 | col = sol; |
| 610 | while (col < eol && *col != ':') |
| 611 | col++; |
| 612 | |
| 613 | sov = col + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 614 | while (sov < eol && HTTP_IS_LWS(*sov)) |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 615 | sov++; |
| 616 | |
| 617 | for (h = cap_hdr; h; h = h->next) { |
Willy Tarreau | 54da8db | 2014-06-13 16:11:48 +0200 | [diff] [blame] | 618 | if (h->namelen && (h->namelen == col - sol) && |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 619 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 620 | if (cap[h->index] == NULL) |
| 621 | cap[h->index] = |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 622 | pool_alloc(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 623 | |
| 624 | if (cap[h->index] == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 625 | ha_alert("HTTP capture : out of memory.\n"); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 626 | continue; |
| 627 | } |
| 628 | |
| 629 | len = eol - sov; |
| 630 | if (len > h->len) |
| 631 | len = h->len; |
| 632 | |
| 633 | memcpy(cap[h->index], sov, len); |
| 634 | cap[h->index][len]=0; |
| 635 | } |
| 636 | } |
| 637 | sol = eol + idx->v[cur_idx].cr + 1; |
| 638 | cur_idx = idx->v[cur_idx].next; |
| 639 | } |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 640 | } |
| 641 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 642 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 643 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 644 | * nothing is done and 1 is returned. |
| 645 | */ |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 646 | int http_upgrade_v09_to_v10(struct http_txn *txn) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 647 | { |
| 648 | int delta; |
| 649 | char *cur_end; |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 650 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 651 | |
| 652 | if (msg->sl.rq.v_l != 0) |
| 653 | return 1; |
| 654 | |
Apollon Oikonomopoulos | 25a1522 | 2014-04-06 02:46:00 +0300 | [diff] [blame] | 655 | /* RFC 1945 allows only GET for HTTP/0.9 requests */ |
| 656 | if (txn->meth != HTTP_METH_GET) |
| 657 | return 0; |
| 658 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 659 | cur_end = ci_head(msg->chn) + msg->sl.rq.l; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 660 | |
| 661 | if (msg->sl.rq.u_l == 0) { |
Apollon Oikonomopoulos | 25a1522 | 2014-04-06 02:46:00 +0300 | [diff] [blame] | 662 | /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */ |
| 663 | return 0; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 664 | } |
| 665 | /* add HTTP version */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 666 | delta = b_rep_blk(&msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11); |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 667 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 668 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 669 | cur_end = (char *)http_parse_reqline(msg, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 670 | HTTP_MSG_RQMETH, |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 671 | ci_head(msg->chn), cur_end + 1, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 672 | NULL, NULL); |
| 673 | if (unlikely(!cur_end)) |
| 674 | return 0; |
| 675 | |
| 676 | /* we have a full HTTP/1.0 request now and we know that |
| 677 | * we have either a CR or an LF at <ptr>. |
| 678 | */ |
| 679 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 680 | return 1; |
| 681 | } |
| 682 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 683 | /* Parse the Connection: header of an HTTP request, looking for both "close" |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 684 | * and "keep-alive" values. If we already know that some headers may safely |
| 685 | * be removed, we remove them now. The <to_del> flags are used for that : |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 686 | * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses) |
| 687 | * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1). |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 688 | * Presence of the "Upgrade" token is also checked and reported. |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 689 | * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was |
| 690 | * found, and TX_CON_*_SET is adjusted depending on what is left so only |
| 691 | * harmless combinations may be removed. Do not call that after changes have |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 692 | * been processed. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 693 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 694 | void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 695 | { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 696 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 697 | const char *hdr_val = "Connection"; |
| 698 | int hdr_len = 10; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 699 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 700 | if (txn->flags & TX_HDR_CONN_PRS) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 701 | return; |
| 702 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 703 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 704 | hdr_val = "Proxy-Connection"; |
| 705 | hdr_len = 16; |
| 706 | } |
| 707 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 708 | ctx.idx = 0; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 709 | txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET); |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 710 | while (http_find_header2(hdr_val, hdr_len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 711 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 712 | txn->flags |= TX_HDR_CONN_KAL; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 713 | if (to_del & 2) |
| 714 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 715 | else |
| 716 | txn->flags |= TX_CON_KAL_SET; |
| 717 | } |
| 718 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 719 | txn->flags |= TX_HDR_CONN_CLO; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 720 | if (to_del & 1) |
| 721 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 722 | else |
| 723 | txn->flags |= TX_CON_CLO_SET; |
| 724 | } |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 725 | else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) { |
| 726 | txn->flags |= TX_HDR_CONN_UPG; |
| 727 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 728 | } |
| 729 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 730 | txn->flags |= TX_HDR_CONN_PRS; |
| 731 | return; |
| 732 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 733 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 734 | /* Apply desired changes on the Connection: header. Values may be removed and/or |
| 735 | * added depending on the <wanted> flags, which are exclusively composed of |
| 736 | * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The |
| 737 | * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left. |
| 738 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 739 | void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 740 | { |
| 741 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 742 | const char *hdr_val = "Connection"; |
| 743 | int hdr_len = 10; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 744 | |
| 745 | ctx.idx = 0; |
| 746 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 747 | |
| 748 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 749 | hdr_val = "Proxy-Connection"; |
| 750 | hdr_len = 16; |
| 751 | } |
| 752 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 753 | txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET); |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 754 | while (http_find_header2(hdr_val, hdr_len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 755 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 756 | if (wanted & TX_CON_KAL_SET) |
| 757 | txn->flags |= TX_CON_KAL_SET; |
| 758 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 759 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 760 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 761 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 762 | if (wanted & TX_CON_CLO_SET) |
| 763 | txn->flags |= TX_CON_CLO_SET; |
| 764 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 765 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 766 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 767 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 768 | |
| 769 | if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 770 | return; |
| 771 | |
| 772 | if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) { |
| 773 | txn->flags |= TX_CON_CLO_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 774 | hdr_val = "Connection: close"; |
| 775 | hdr_len = 17; |
| 776 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 777 | hdr_val = "Proxy-Connection: close"; |
| 778 | hdr_len = 23; |
| 779 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 780 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) { |
| 784 | txn->flags |= TX_CON_KAL_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 785 | hdr_val = "Connection: keep-alive"; |
| 786 | hdr_len = 22; |
| 787 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 788 | hdr_val = "Proxy-Connection: keep-alive"; |
| 789 | hdr_len = 28; |
| 790 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 791 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 792 | } |
| 793 | return; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 794 | } |
| 795 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 796 | void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg) |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 797 | { |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 798 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 799 | int tmp = TX_CON_WANT_KAL; |
| 800 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 801 | if (IS_HTX_STRM(s)) |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 802 | return htx_adjust_conn_mode(s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 803 | |
Christopher Faulet | 315b39c | 2018-09-21 16:26:19 +0200 | [diff] [blame] | 804 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN || |
| 805 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN) |
| 806 | tmp = TX_CON_WANT_TUN; |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 807 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 808 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL || |
Christopher Faulet | 315b39c | 2018-09-21 16:26:19 +0200 | [diff] [blame] | 809 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 810 | tmp = TX_CON_WANT_SCL; |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 811 | |
Christopher Faulet | 315b39c | 2018-09-21 16:26:19 +0200 | [diff] [blame] | 812 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO || |
| 813 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 814 | tmp = TX_CON_WANT_CLO; |
| 815 | |
| 816 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
| 817 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
| 818 | |
| 819 | if (!(txn->flags & TX_HDR_CONN_PRS) && |
| 820 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) { |
| 821 | /* parse the Connection header and possibly clean it */ |
| 822 | int to_del = 0; |
| 823 | if ((msg->flags & HTTP_MSGF_VER_11) || |
| 824 | ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 825 | !((fe->options2|s->be->options2) & PR_O2_FAKE_KA))) |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 826 | to_del |= 2; /* remove "keep-alive" */ |
| 827 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
| 828 | to_del |= 1; /* remove "close" */ |
| 829 | http_parse_connection_header(txn, msg, to_del); |
| 830 | } |
| 831 | |
| 832 | /* check if client or config asks for explicit close in KAL/SCL */ |
| 833 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 834 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) && |
| 835 | ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */ |
| 836 | (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */ |
| 837 | !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 838 | fe->state == PR_STSTOPPED)) /* frontend is stopping */ |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 839 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 840 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 841 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 842 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 843 | * processing can continue on next analysers, or zero if it either needs more |
| 844 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 845 | * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 846 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 847 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 848 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 849 | int http_wait_for_request(struct stream *s, struct channel *req, int an_bit) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 850 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 851 | /* |
| 852 | * We will parse the partial (or complete) lines. |
| 853 | * We will check the request syntax, and also join multi-line |
| 854 | * headers. An index of all the lines will be elaborated while |
| 855 | * parsing. |
| 856 | * |
| 857 | * For the parsing, we use a 28 states FSM. |
| 858 | * |
| 859 | * Here is the information we currently have : |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 860 | * ci_head(req) = beginning of request |
| 861 | * ci_head(req) + msg->eoh = end of processed headers / start of current one |
| 862 | * ci_tail(req) = end of input data |
| 863 | * msg->eol = end of current header or line (LF or CRLF) |
| 864 | * msg->next = first non-visited byte |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 865 | * |
| 866 | * At end of parsing, we may perform a capture of the error (if any), and |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 867 | * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 868 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 869 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 870 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 871 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 872 | int cur_idx; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 873 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 874 | struct http_txn *txn = s->txn; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 875 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 876 | struct hdr_ctx ctx; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 877 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 878 | if (IS_HTX_STRM(s)) |
| 879 | return htx_wait_for_request(s, req, an_bit); |
| 880 | |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 881 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n", |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 882 | now_ms, __FUNCTION__, |
| 883 | s, |
| 884 | req, |
| 885 | req->rex, req->wex, |
| 886 | req->flags, |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 887 | ci_data(req), |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 888 | req->analysers); |
| 889 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 890 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 891 | s->srv_error = http_return_srv_error; |
| 892 | |
Rian McGuire | 89fcb7d | 2018-04-24 11:19:21 -0300 | [diff] [blame] | 893 | /* If there is data available for analysis, log the end of the idle time. */ |
Willy Tarreau | d760eec | 2018-07-10 09:50:25 +0200 | [diff] [blame] | 894 | if (c_data(req) && s->logs.t_idle == -1) |
Rian McGuire | 89fcb7d | 2018-04-24 11:19:21 -0300 | [diff] [blame] | 895 | s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake; |
| 896 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 897 | /* There's a protected area at the end of the buffer for rewriting |
| 898 | * purposes. We don't want to start to parse the request if the |
| 899 | * protected area is affected, because we may have to move processed |
| 900 | * data later, which is much more complicated. |
| 901 | */ |
Willy Tarreau | d760eec | 2018-07-10 09:50:25 +0200 | [diff] [blame] | 902 | if (c_data(req) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 903 | if (txn->flags & TX_NOT_FIRST) { |
Willy Tarreau | ba0902e | 2015-01-13 14:39:16 +0100 | [diff] [blame] | 904 | if (unlikely(!channel_is_rewritable(req))) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 905 | if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 906 | goto failed_keep_alive; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 907 | /* some data has still not left the buffer, wake us once that's done */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 908 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 909 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 910 | req->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 911 | return 0; |
| 912 | } |
Willy Tarreau | 188e230 | 2018-06-15 11:11:53 +0200 | [diff] [blame] | 913 | if (unlikely(ci_tail(req) < c_ptr(req, msg->next) || |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 914 | ci_tail(req) > b_wrap(&req->buf) - global.tune.maxrewrite)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 915 | channel_slow_realign(req, trash.area); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 916 | } |
| 917 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 918 | if (likely(msg->next < ci_data(req))) /* some unparsed data are available */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 919 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 920 | } |
| 921 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 922 | /* 1: we might have to print this header in debug mode */ |
| 923 | if (unlikely((global.mode & MODE_DEBUG) && |
| 924 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 7d59e90 | 2014-10-21 19:36:09 +0200 | [diff] [blame] | 925 | msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 926 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 927 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 928 | sol = ci_head(req); |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 929 | /* this is a bit complex : in case of error on the request line, |
| 930 | * we know that rq.l is still zero, so we display only the part |
| 931 | * up to the end of the line (truncated by debug_hdr). |
| 932 | */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 933 | eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : ci_data(req)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 934 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 935 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 936 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 937 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 938 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 939 | while (cur_idx) { |
| 940 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 941 | debug_hdr("clihdr", s, sol, eol); |
| 942 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 943 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 944 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 945 | } |
| 946 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 947 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 948 | /* |
| 949 | * Now we quickly check if we have found a full valid request. |
| 950 | * If not so, we check the FD and buffer states before leaving. |
| 951 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 952 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 953 | * requests are checked first. When waiting for a second request |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 954 | * on a keep-alive stream, if we encounter and error, close, t/o, |
| 955 | * we note the error in the stream flags but don't set any state. |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 956 | * Since the error will be noted there, it will not be counted by |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 957 | * process_stream() as a frontend error. |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 958 | * Last, we may increase some tracked counters' http request errors on |
| 959 | * the cases that are deliberately the client's fault. For instance, |
| 960 | * a timeout or connection reset is not counted as an error. However |
| 961 | * a bad request is. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 962 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 963 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 964 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 965 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 966 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 967 | */ |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 968 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 969 | stream_inc_http_req_ctr(s); |
| 970 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 971 | proxy_inc_fe_req_ctr(sess->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 972 | goto return_bad_req; |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 973 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 974 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 975 | /* 1: Since we are in header mode, if there's no space |
| 976 | * left for headers, we won't be able to free more |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 977 | * later, so the stream will never terminate. We |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 978 | * must terminate it now. |
| 979 | */ |
Willy Tarreau | 2375233 | 2018-06-15 14:54:53 +0200 | [diff] [blame] | 980 | if (unlikely(channel_full(req, global.tune.maxrewrite))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 981 | /* FIXME: check if URI is set and return Status |
| 982 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 983 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 984 | stream_inc_http_req_ctr(s); |
| 985 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 986 | proxy_inc_fe_req_ctr(sess->fe); |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 987 | if (msg->err_pos < 0) |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 988 | msg->err_pos = ci_data(req); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 989 | goto return_bad_req; |
| 990 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 991 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 992 | /* 2: have we encountered a read error ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 993 | else if (req->flags & CF_READ_ERROR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 994 | if (!(s->flags & SF_ERR_MASK)) |
| 995 | s->flags |= SF_ERR_CLICL; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 996 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 997 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 998 | goto failed_keep_alive; |
| 999 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 1000 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 1001 | goto failed_keep_alive; |
| 1002 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1003 | /* we cannot return any message on error */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 1004 | if (msg->err_pos >= 0) { |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 1005 | http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1006 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 1007 | } |
| 1008 | |
Willy Tarreau | dc979f2 | 2012-12-04 10:39:01 +0100 | [diff] [blame] | 1009 | txn->status = 400; |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1010 | msg->err_state = msg->msg_state; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1011 | msg->msg_state = HTTP_MSG_ERROR; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 1012 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 1013 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1014 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1015 | proxy_inc_fe_req_ctr(sess->fe); |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 1016 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1017 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 1018 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 1019 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1020 | if (!(s->flags & SF_FINST_MASK)) |
| 1021 | s->flags |= SF_FINST_R; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1022 | return 0; |
| 1023 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 1024 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1025 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 1026 | else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1027 | if (!(s->flags & SF_ERR_MASK)) |
| 1028 | s->flags |= SF_ERR_CLITO; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 1029 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 1030 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 1031 | goto failed_keep_alive; |
| 1032 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 1033 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 1034 | goto failed_keep_alive; |
| 1035 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1036 | /* read timeout : give up with an error message. */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 1037 | if (msg->err_pos >= 0) { |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 1038 | http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1039 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 1040 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1041 | txn->status = 408; |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1042 | msg->err_state = msg->msg_state; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1043 | msg->msg_state = HTTP_MSG_ERROR; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1044 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 1045 | req->analysers &= AN_REQ_FLT_END; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 1046 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1047 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1048 | proxy_inc_fe_req_ctr(sess->fe); |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 1049 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1050 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 1051 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 1052 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1053 | if (!(s->flags & SF_FINST_MASK)) |
| 1054 | s->flags |= SF_FINST_R; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1055 | return 0; |
| 1056 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1057 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1058 | /* 4: have we encountered a close ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 1059 | else if (req->flags & CF_SHUTR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1060 | if (!(s->flags & SF_ERR_MASK)) |
| 1061 | s->flags |= SF_ERR_CLICL; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 1062 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 1063 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 1064 | goto failed_keep_alive; |
| 1065 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 1066 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 1067 | goto failed_keep_alive; |
| 1068 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 1069 | if (msg->err_pos >= 0) |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 1070 | http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1071 | txn->status = 400; |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1072 | msg->err_state = msg->msg_state; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1073 | msg->msg_state = HTTP_MSG_ERROR; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1074 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 1075 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1076 | stream_inc_http_err_ctr(s); |
| 1077 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1078 | proxy_inc_fe_req_ctr(sess->fe); |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 1079 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1080 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 1081 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 1082 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1083 | if (!(s->flags & SF_FINST_MASK)) |
| 1084 | s->flags |= SF_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1085 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1086 | } |
| 1087 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 1088 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 1089 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1090 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 1091 | #ifdef TCP_QUICKACK |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1092 | if (sess->listener->options & LI_O_NOQUICKACK && ci_data(req) && |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 1093 | objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) { |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 1094 | /* We need more data, we have to re-enable quick-ack in case we |
| 1095 | * previously disabled it, otherwise we might cause the client |
| 1096 | * to delay next data. |
| 1097 | */ |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 1098 | setsockopt(__objt_conn(sess->origin)->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 1099 | } |
| 1100 | #endif |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 1101 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 1102 | if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) { |
| 1103 | /* If the client starts to talk, let's fall back to |
| 1104 | * request timeout processing. |
| 1105 | */ |
| 1106 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 1107 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 1108 | } |
| 1109 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1110 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 1111 | if (!tick_isset(req->analyse_exp)) { |
| 1112 | if ((msg->msg_state == HTTP_MSG_RQBEFORE) && |
| 1113 | (txn->flags & TX_WAIT_NEXT_RQ) && |
| 1114 | tick_isset(s->be->timeout.httpka)) |
| 1115 | req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka); |
| 1116 | else |
| 1117 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
| 1118 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1119 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1120 | /* we're not ready yet */ |
| 1121 | return 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 1122 | |
| 1123 | failed_keep_alive: |
| 1124 | /* Here we process low-level errors for keep-alive requests. In |
| 1125 | * short, if the request is not the first one and it experiences |
| 1126 | * a timeout, read error or shutdown, we just silently close so |
| 1127 | * that the client can try again. |
| 1128 | */ |
| 1129 | txn->status = 0; |
| 1130 | msg->msg_state = HTTP_MSG_RQBEFORE; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 1131 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 1132 | s->logs.logwait = 0; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 1133 | s->logs.level = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1134 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 1135 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 1136 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1137 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1138 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1139 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 1140 | * complete the request parsing by setting a few fields we will need |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1141 | * later. At this point, we have the last CRLF at req->buf.data + msg->eoh. |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1142 | * If the request is in HTTP/0.9 form, the rule is still true, and eoh |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1143 | * points to the CRLF of the request line. msg->next points to the first |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1144 | * byte after the last LF. msg->sov points to the first byte of data. |
| 1145 | * msg->eol cannot be trusted because it may have been left uninitialized |
| 1146 | * (for instance in the absence of headers). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1147 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1148 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1149 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1150 | proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */ |
Willy Tarreau | d9b587f | 2010-02-26 10:05:55 +0100 | [diff] [blame] | 1151 | |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 1152 | if (txn->flags & TX_WAIT_NEXT_RQ) { |
| 1153 | /* kill the pending keep-alive timeout */ |
| 1154 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 1155 | req->analyse_exp = TICK_ETERNITY; |
| 1156 | } |
| 1157 | |
| 1158 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1159 | /* Maybe we found in invalid header name while we were configured not |
| 1160 | * to block on that, so we have to capture it now. |
| 1161 | */ |
| 1162 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 1163 | http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 1164 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1165 | /* |
| 1166 | * 1: identify the method |
| 1167 | */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1168 | txn->meth = find_http_meth(ci_head(req), msg->sl.rq.m_l); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1169 | |
| 1170 | /* we can make use of server redirect on GET and HEAD */ |
| 1171 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1172 | s->flags |= SF_REDIRECTABLE; |
Willy Tarreau | 9165979 | 2017-11-10 19:38:10 +0100 | [diff] [blame] | 1173 | else if (txn->meth == HTTP_METH_OTHER && |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1174 | msg->sl.rq.m_l == 3 && memcmp(ci_head(req), "PRI", 3) == 0) { |
Willy Tarreau | 9165979 | 2017-11-10 19:38:10 +0100 | [diff] [blame] | 1175 | /* PRI is reserved for the HTTP/2 preface */ |
| 1176 | msg->err_pos = 0; |
| 1177 | goto return_bad_req; |
| 1178 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 1179 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1180 | /* |
| 1181 | * 2: check if the URI matches the monitor_uri. |
| 1182 | * We have to do this for every request which gets in, because |
| 1183 | * the monitor-uri is defined by the frontend. |
| 1184 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1185 | if (unlikely((sess->fe->monitor_uri_len != 0) && |
| 1186 | (sess->fe->monitor_uri_len == msg->sl.rq.u_l) && |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1187 | !memcmp(ci_head(req) + msg->sl.rq.u, |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1188 | sess->fe->monitor_uri, |
| 1189 | sess->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1190 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1191 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1192 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1193 | struct acl_cond *cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1194 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1195 | s->flags |= SF_MONITOR; |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 1196 | HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1); |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1197 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1198 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1199 | list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 1200 | int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 1201 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1202 | ret = acl_pass(ret); |
| 1203 | if (cond->pol == ACL_COND_UNLESS) |
| 1204 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1205 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1206 | if (ret) { |
| 1207 | /* we fail this request, let's return 503 service unavail */ |
| 1208 | txn->status = 503; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1209 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1210 | if (!(s->flags & SF_ERR_MASK)) |
| 1211 | s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1212 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1213 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1214 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 1215 | |
Joseph Herlant | 5ba8025 | 2018-11-15 09:25:36 -0800 | [diff] [blame] | 1216 | /* nothing to fail, let's reply normally */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1217 | txn->status = 200; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1218 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1219 | if (!(s->flags & SF_ERR_MASK)) |
| 1220 | s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1221 | goto return_prx_cond; |
| 1222 | } |
| 1223 | |
| 1224 | /* |
| 1225 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 1226 | * Note: we cannot log anymore if the request has been |
| 1227 | * classified as invalid. |
| 1228 | */ |
| 1229 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 1230 | /* we have a complete HTTP request that we must log */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1231 | if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1232 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1233 | |
Stéphane Cottin | 23e9e93 | 2017-05-18 08:58:41 +0200 | [diff] [blame] | 1234 | if (urilen >= global.tune.requri_len ) |
| 1235 | urilen = global.tune.requri_len - 1; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1236 | memcpy(txn->uri, ci_head(req), urilen); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1237 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1238 | |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 1239 | if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT))) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1240 | s->do_log(s); |
| 1241 | } else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1242 | ha_alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1243 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1244 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1245 | |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 1246 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 1247 | * exactly one digit "." one digit. This check may be disabled using |
| 1248 | * option accept-invalid-http-request. |
| 1249 | */ |
| 1250 | if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) { |
| 1251 | if (msg->sl.rq.v_l != 8) { |
| 1252 | msg->err_pos = msg->sl.rq.v; |
| 1253 | goto return_bad_req; |
| 1254 | } |
| 1255 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1256 | if (ci_head(req)[msg->sl.rq.v + 4] != '/' || |
| 1257 | !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 5]) || |
| 1258 | ci_head(req)[msg->sl.rq.v + 6] != '.' || |
| 1259 | !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 7])) { |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 1260 | msg->err_pos = msg->sl.rq.v + 4; |
| 1261 | goto return_bad_req; |
| 1262 | } |
| 1263 | } |
Willy Tarreau | 1331766 | 2015-05-01 13:47:08 +0200 | [diff] [blame] | 1264 | else { |
| 1265 | /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */ |
| 1266 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
| 1267 | goto return_bad_req; |
| 1268 | } |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 1269 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1270 | /* ... and check if the request is HTTP/1.1 or above */ |
| 1271 | if ((msg->sl.rq.v_l == 8) && |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1272 | ((ci_head(req)[msg->sl.rq.v + 5] > '1') || |
| 1273 | ((ci_head(req)[msg->sl.rq.v + 5] == '1') && |
| 1274 | (ci_head(req)[msg->sl.rq.v + 7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 1275 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1276 | |
| 1277 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 1278 | txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL | TX_HDR_CONN_UPG); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1279 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1280 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 1281 | * we have what looks like a proxied connection instead of a connection, |
| 1282 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 1283 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 1284 | * happen to do that despite being non-standard :-( |
| 1285 | * We consider that a request not beginning with either '/' or '*' is |
| 1286 | * a proxied connection, which covers both "scheme://location" and |
| 1287 | * CONNECT ip:port. |
| 1288 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1289 | if ((sess->fe->options2 & PR_O2_USE_PXHDR) && |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1290 | ci_head(req)[msg->sl.rq.u] != '/' && ci_head(req)[msg->sl.rq.u] != '*') |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1291 | txn->flags |= TX_USE_PX_CONN; |
| 1292 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 1293 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 1294 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 1295 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1296 | /* 5: we may need to capture headers */ |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 1297 | if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap)) |
Christopher Faulet | 10079f5 | 2018-10-03 15:17:28 +0200 | [diff] [blame] | 1298 | http_capture_headers(ci_head(req), &txn->hdr_idx, |
| 1299 | s->req_cap, sess->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 1300 | |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 1301 | /* 6: determine the transfer-length according to RFC2616 #4.4, updated |
| 1302 | * by RFC7230#3.3.3 : |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 1303 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 1304 | * The length of a message body is determined by one of the following |
| 1305 | * (in order of precedence): |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1306 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 1307 | * 1. Any response to a HEAD request and any response with a 1xx |
| 1308 | * (Informational), 204 (No Content), or 304 (Not Modified) status |
| 1309 | * code is always terminated by the first empty line after the |
| 1310 | * header fields, regardless of the header fields present in the |
| 1311 | * message, and thus cannot contain a message body. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1312 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 1313 | * 2. Any 2xx (Successful) response to a CONNECT request implies that |
| 1314 | * the connection will become a tunnel immediately after the empty |
| 1315 | * line that concludes the header fields. A client MUST ignore any |
| 1316 | * Content-Length or Transfer-Encoding header fields received in |
| 1317 | * such a message. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 1318 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 1319 | * 3. If a Transfer-Encoding header field is present and the chunked |
| 1320 | * transfer coding (Section 4.1) is the final encoding, the message |
| 1321 | * body length is determined by reading and decoding the chunked |
| 1322 | * data until the transfer coding indicates the data is complete. |
| 1323 | * |
| 1324 | * If a Transfer-Encoding header field is present in a response and |
| 1325 | * the chunked transfer coding is not the final encoding, the |
| 1326 | * message body length is determined by reading the connection until |
| 1327 | * it is closed by the server. If a Transfer-Encoding header field |
| 1328 | * is present in a request and the chunked transfer coding is not |
| 1329 | * the final encoding, the message body length cannot be determined |
| 1330 | * reliably; the server MUST respond with the 400 (Bad Request) |
| 1331 | * status code and then close the connection. |
| 1332 | * |
| 1333 | * If a message is received with both a Transfer-Encoding and a |
| 1334 | * Content-Length header field, the Transfer-Encoding overrides the |
| 1335 | * Content-Length. Such a message might indicate an attempt to |
| 1336 | * perform request smuggling (Section 9.5) or response splitting |
| 1337 | * (Section 9.4) and ought to be handled as an error. A sender MUST |
| 1338 | * remove the received Content-Length field prior to forwarding such |
| 1339 | * a message downstream. |
| 1340 | * |
| 1341 | * 4. If a message is received without Transfer-Encoding and with |
| 1342 | * either multiple Content-Length header fields having differing |
| 1343 | * field-values or a single Content-Length header field having an |
| 1344 | * invalid value, then the message framing is invalid and the |
| 1345 | * recipient MUST treat it as an unrecoverable error. If this is a |
| 1346 | * request message, the server MUST respond with a 400 (Bad Request) |
| 1347 | * status code and then close the connection. If this is a response |
| 1348 | * message received by a proxy, the proxy MUST close the connection |
| 1349 | * to the server, discard the received response, and send a 502 (Bad |
| 1350 | * Gateway) response to the client. If this is a response message |
| 1351 | * received by a user agent, the user agent MUST close the |
| 1352 | * connection to the server and discard the received response. |
| 1353 | * |
| 1354 | * 5. If a valid Content-Length header field is present without |
| 1355 | * Transfer-Encoding, its decimal value defines the expected message |
| 1356 | * body length in octets. If the sender closes the connection or |
| 1357 | * the recipient times out before the indicated number of octets are |
| 1358 | * received, the recipient MUST consider the message to be |
| 1359 | * incomplete and close the connection. |
| 1360 | * |
| 1361 | * 6. If this is a request message and none of the above are true, then |
| 1362 | * the message body length is zero (no message body is present). |
| 1363 | * |
| 1364 | * 7. Otherwise, this is a response message without a declared message |
| 1365 | * body length, so the message body length is determined by the |
| 1366 | * number of octets received prior to the server closing the |
| 1367 | * connection. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1368 | */ |
| 1369 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1370 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 1371 | /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1372 | while (http_find_header2("Transfer-Encoding", 17, ci_head(req), &txn->hdr_idx, &ctx)) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 1373 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 1374 | msg->flags |= HTTP_MSGF_TE_CHNK; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 1375 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | 34dfc60 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 1376 | /* chunked not last, return badreq */ |
| 1377 | goto return_bad_req; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 1378 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1379 | } |
| 1380 | |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 1381 | /* Chunked requests must have their content-length removed */ |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1382 | ctx.idx = 0; |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 1383 | if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1384 | while (http_find_header2("Content-Length", 14, ci_head(req), &txn->hdr_idx, &ctx)) |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 1385 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 1386 | } |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1387 | else while (http_find_header2("Content-Length", 14, ci_head(req), &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1388 | signed long long cl; |
| 1389 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 1390 | if (!ctx.vlen) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1391 | msg->err_pos = ctx.line + ctx.val - ci_head(req); |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1392 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 1393 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1394 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 1395 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1396 | msg->err_pos = ctx.line + ctx.val - ci_head(req); |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1397 | goto return_bad_req; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 1398 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1399 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 1400 | if (cl < 0) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1401 | msg->err_pos = ctx.line + ctx.val - ci_head(req); |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1402 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 1403 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1404 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 1405 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1406 | msg->err_pos = ctx.line + ctx.val - ci_head(req); |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1407 | goto return_bad_req; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 1408 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1409 | |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 1410 | msg->flags |= HTTP_MSGF_CNT_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 1411 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1412 | } |
| 1413 | |
Willy Tarreau | 34dfc60 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 1414 | /* even bodyless requests have a known length */ |
| 1415 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 1416 | |
Willy Tarreau | 179085c | 2014-04-28 16:48:56 +0200 | [diff] [blame] | 1417 | /* Until set to anything else, the connection mode is set as Keep-Alive. It will |
| 1418 | * only change if both the request and the config reference something else. |
| 1419 | * Option httpclose by itself sets tunnel mode where headers are mangled. |
| 1420 | * However, if another mode is set, it will affect it (eg: server-close/ |
| 1421 | * keep-alive + httpclose = close). Note that we avoid to redo the same work |
| 1422 | * if FE and BE have the same settings (common). The method consists in |
| 1423 | * checking if options changed between the two calls (implying that either |
| 1424 | * one is non-null, or one of them is non-null and we are there for the first |
| 1425 | * time. |
| 1426 | */ |
| 1427 | if (!(txn->flags & TX_HDR_CONN_PRS) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1428 | ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE))) |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 1429 | http_adjust_conn_mode(s, txn, msg); |
Willy Tarreau | 179085c | 2014-04-28 16:48:56 +0200 | [diff] [blame] | 1430 | |
Willy Tarreau | 9fbe18e | 2015-05-01 22:42:08 +0200 | [diff] [blame] | 1431 | /* we may have to wait for the request's body */ |
| 1432 | if ((s->be->options & PR_O_WREQ_BODY) && |
| 1433 | (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK))) |
| 1434 | req->analysers |= AN_REQ_HTTP_BODY; |
| 1435 | |
Willy Tarreau | 83ece46 | 2017-12-21 15:13:09 +0100 | [diff] [blame] | 1436 | /* |
| 1437 | * RFC7234#4: |
| 1438 | * A cache MUST write through requests with methods |
| 1439 | * that are unsafe (Section 4.2.1 of [RFC7231]) to |
| 1440 | * the origin server; i.e., a cache is not allowed |
| 1441 | * to generate a reply to such a request before |
| 1442 | * having forwarded the request and having received |
| 1443 | * a corresponding response. |
| 1444 | * |
| 1445 | * RFC7231#4.2.1: |
| 1446 | * Of the request methods defined by this |
| 1447 | * specification, the GET, HEAD, OPTIONS, and TRACE |
| 1448 | * methods are defined to be safe. |
| 1449 | */ |
| 1450 | if (likely(txn->meth == HTTP_METH_GET || |
| 1451 | txn->meth == HTTP_METH_HEAD || |
| 1452 | txn->meth == HTTP_METH_OPTIONS || |
| 1453 | txn->meth == HTTP_METH_TRACE)) |
| 1454 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 1455 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1456 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 1457 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1458 | req->analyse_exp = TICK_ETERNITY; |
| 1459 | return 1; |
| 1460 | |
| 1461 | return_bad_req: |
| 1462 | /* We centralize bad requests processing here */ |
| 1463 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 1464 | /* we detected a parsing error. We want to archive this request |
| 1465 | * in the dedicated proxy area for later troubleshooting. |
| 1466 | */ |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 1467 | http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1468 | } |
| 1469 | |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1470 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1471 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 1472 | txn->status = 400; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1473 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 1474 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 1475 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1476 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 1477 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1478 | |
| 1479 | return_prx_cond: |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1480 | if (!(s->flags & SF_ERR_MASK)) |
| 1481 | s->flags |= SF_ERR_PRXCOND; |
| 1482 | if (!(s->flags & SF_FINST_MASK)) |
| 1483 | s->flags |= SF_FINST_R; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1484 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 1485 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1486 | req->analyse_exp = TICK_ETERNITY; |
| 1487 | return 0; |
| 1488 | } |
| 1489 | |
Willy Tarreau | 4f8a83c | 2012-06-04 00:26:23 +0200 | [diff] [blame] | 1490 | |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 1491 | /* This function prepares an applet to handle the stats. It can deal with the |
| 1492 | * "100-continue" expectation, check that admin rules are met for POST requests, |
| 1493 | * and program a response message if something was unexpected. It cannot fail |
| 1494 | * and always relies on the stats applet to complete the job. It does not touch |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1495 | * analysers nor counters, which are left to the caller. It does not touch |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1496 | * s->target which is supposed to already point to the stats applet. The caller |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1497 | * is expected to have already assigned an appctx to the stream. |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 1498 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1499 | int http_handle_stats(struct stream *s, struct channel *req) |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 1500 | { |
| 1501 | struct stats_admin_rule *stats_admin_rule; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 1502 | struct stream_interface *si = &s->si[1]; |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 1503 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1504 | struct http_txn *txn = s->txn; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 1505 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1506 | struct uri_auth *uri_auth = s->be->uri_auth; |
| 1507 | const char *uri, *h, *lookup; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1508 | struct appctx *appctx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1509 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1510 | appctx = si_appctx(si); |
| 1511 | memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats)); |
| 1512 | appctx->st1 = appctx->st2 = 0; |
| 1513 | appctx->ctx.stats.st_code = STAT_STATUS_INIT; |
| 1514 | appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1515 | if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD)) |
Willy Tarreau | af3cf70 | 2014-04-22 22:19:53 +0200 | [diff] [blame] | 1516 | appctx->ctx.stats.flags |= STAT_CHUNKED; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1517 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1518 | uri = ci_head(msg->chn) + msg->sl.rq.u; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1519 | lookup = uri + uri_auth->uri_len; |
| 1520 | |
| 1521 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) { |
| 1522 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1523 | appctx->ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1524 | break; |
| 1525 | } |
| 1526 | } |
| 1527 | |
| 1528 | if (uri_auth->refresh) { |
| 1529 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) { |
| 1530 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1531 | appctx->ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1532 | break; |
| 1533 | } |
| 1534 | } |
| 1535 | } |
| 1536 | |
| 1537 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) { |
| 1538 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1539 | appctx->ctx.stats.flags &= ~STAT_FMT_HTML; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1540 | break; |
| 1541 | } |
| 1542 | } |
| 1543 | |
Willy Tarreau | 1e62df9 | 2016-01-11 18:57:53 +0100 | [diff] [blame] | 1544 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) { |
| 1545 | if (memcmp(h, ";typed", 6) == 0) { |
| 1546 | appctx->ctx.stats.flags &= ~STAT_FMT_HTML; |
| 1547 | appctx->ctx.stats.flags |= STAT_FMT_TYPED; |
| 1548 | break; |
| 1549 | } |
| 1550 | } |
| 1551 | |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1552 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 1553 | if (memcmp(h, ";st=", 4) == 0) { |
| 1554 | int i; |
| 1555 | h += 4; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1556 | appctx->ctx.stats.st_code = STAT_STATUS_UNKN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1557 | for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) { |
| 1558 | if (strncmp(stat_status_codes[i], h, 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1559 | appctx->ctx.stats.st_code = i; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1560 | break; |
| 1561 | } |
| 1562 | } |
| 1563 | break; |
| 1564 | } |
| 1565 | } |
| 1566 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1567 | appctx->ctx.stats.scope_str = 0; |
| 1568 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1569 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 1570 | if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) { |
| 1571 | int itx = 0; |
| 1572 | const char *h2; |
| 1573 | char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1]; |
| 1574 | const char *err; |
| 1575 | |
| 1576 | h += strlen(STAT_SCOPE_INPUT_NAME) + 1; |
| 1577 | h2 = h; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1578 | appctx->ctx.stats.scope_str = h2 - ci_head(msg->chn); |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1579 | while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') { |
| 1580 | itx++; |
| 1581 | h++; |
| 1582 | } |
| 1583 | |
| 1584 | if (itx > STAT_SCOPE_TXT_MAXLEN) |
| 1585 | itx = STAT_SCOPE_TXT_MAXLEN; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1586 | appctx->ctx.stats.scope_len = itx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1587 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1588 | /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1589 | memcpy(scope_txt, h2, itx); |
| 1590 | scope_txt[itx] = '\0'; |
| 1591 | err = invalid_char(scope_txt); |
| 1592 | if (err) { |
| 1593 | /* bad char in search text => clear scope */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1594 | appctx->ctx.stats.scope_str = 0; |
| 1595 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1596 | } |
| 1597 | break; |
| 1598 | } |
| 1599 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 1600 | |
| 1601 | /* now check whether we have some admin rules for this request */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1602 | list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 1603 | int ret = 1; |
| 1604 | |
| 1605 | if (stats_admin_rule->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 1606 | ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 1607 | ret = acl_pass(ret); |
| 1608 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 1609 | ret = !ret; |
| 1610 | } |
| 1611 | |
| 1612 | if (ret) { |
| 1613 | /* no rule, or the rule matches */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1614 | appctx->ctx.stats.flags |= STAT_ADMIN; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 1615 | break; |
| 1616 | } |
| 1617 | } |
| 1618 | |
| 1619 | /* Was the status page requested with a POST ? */ |
Willy Tarreau | b8cdf52 | 2015-05-29 01:09:15 +0200 | [diff] [blame] | 1620 | if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1621 | if (appctx->ctx.stats.flags & STAT_ADMIN) { |
Willy Tarreau | cfe7fdd | 2014-04-26 22:08:32 +0200 | [diff] [blame] | 1622 | /* we'll need the request body, possibly after sending 100-continue */ |
Willy Tarreau | b8cdf52 | 2015-05-29 01:09:15 +0200 | [diff] [blame] | 1623 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) |
| 1624 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1625 | appctx->st0 = STAT_HTTP_POST; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 1626 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 1627 | else { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1628 | appctx->ctx.stats.st_code = STAT_STATUS_DENY; |
| 1629 | appctx->st0 = STAT_HTTP_LAST; |
de Lafond Guillaume | 88c278f | 2013-04-15 19:27:10 +0200 | [diff] [blame] | 1630 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 1631 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 1632 | else { |
| 1633 | /* So it was another method (GET/HEAD) */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1634 | appctx->st0 = STAT_HTTP_HEAD; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 1635 | } |
| 1636 | |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 1637 | s->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 1638 | return 1; |
| 1639 | } |
| 1640 | |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 1641 | /* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets |
| 1642 | * (as per RFC3260 #4 and BCP37 #4.2 and #5.2). |
| 1643 | */ |
Vincent Bernat | 6e61589 | 2016-05-18 16:17:44 +0200 | [diff] [blame] | 1644 | void inet_set_tos(int fd, const struct sockaddr_storage *from, int tos) |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 1645 | { |
| 1646 | #ifdef IP_TOS |
Vincent Bernat | 6e61589 | 2016-05-18 16:17:44 +0200 | [diff] [blame] | 1647 | if (from->ss_family == AF_INET) |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 1648 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 1649 | #endif |
| 1650 | #ifdef IPV6_TCLASS |
Vincent Bernat | 6e61589 | 2016-05-18 16:17:44 +0200 | [diff] [blame] | 1651 | if (from->ss_family == AF_INET6) { |
| 1652 | if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)from)->sin6_addr)) |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 1653 | /* v4-mapped addresses need IP_TOS */ |
| 1654 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 1655 | else |
| 1656 | setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)); |
| 1657 | } |
| 1658 | #endif |
| 1659 | } |
| 1660 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1661 | int http_transform_header_str(struct stream* s, struct http_msg *msg, |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 1662 | const char* name, unsigned int name_len, |
| 1663 | const char *str, struct my_regex *re, |
| 1664 | int action) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 1665 | { |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 1666 | struct hdr_ctx ctx; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1667 | char *buf = ci_head(msg->chn); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1668 | struct hdr_idx *idx = &s->txn->hdr_idx; |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 1669 | int (*http_find_hdr_func)(const char *name, int len, char *sol, |
| 1670 | struct hdr_idx *idx, struct hdr_ctx *ctx); |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1671 | struct buffer *output = get_trash_chunk(); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 1672 | |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 1673 | ctx.idx = 0; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 1674 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 1675 | /* Choose the header browsing function. */ |
| 1676 | switch (action) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1677 | case ACT_HTTP_REPLACE_VAL: |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 1678 | http_find_hdr_func = http_find_header2; |
| 1679 | break; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1680 | case ACT_HTTP_REPLACE_HDR: |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 1681 | http_find_hdr_func = http_find_full_header2; |
| 1682 | break; |
| 1683 | default: /* impossible */ |
| 1684 | return -1; |
| 1685 | } |
| 1686 | |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 1687 | while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) { |
| 1688 | struct hdr_idx_elem *hdr = idx->v + ctx.idx; |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 1689 | int delta, len; |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 1690 | char *val = ctx.line + ctx.val; |
| 1691 | char* val_end = val + ctx.vlen; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 1692 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 1693 | if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0)) |
| 1694 | continue; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 1695 | |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 1696 | len = exp_replace(output->area, output->size, val, str, pmatch); |
| 1697 | if (len == -1) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 1698 | return -1; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 1699 | |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 1700 | delta = b_rep_blk(&msg->chn->buf, val, val_end, output->area, len); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 1701 | |
| 1702 | hdr->len += delta; |
| 1703 | http_msg_move_end(msg, delta); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 1704 | |
| 1705 | /* Adjust the length of the current value of the index. */ |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 1706 | ctx.vlen += delta; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 1707 | } |
| 1708 | |
| 1709 | return 0; |
| 1710 | } |
| 1711 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1712 | static int http_transform_header(struct stream* s, struct http_msg *msg, |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 1713 | const char* name, unsigned int name_len, |
| 1714 | struct list *fmt, struct my_regex *re, |
| 1715 | int action) |
| 1716 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1717 | struct buffer *replace; |
Christopher Faulet | 07a0fec | 2017-02-08 12:17:07 +0100 | [diff] [blame] | 1718 | int ret = -1; |
| 1719 | |
| 1720 | replace = alloc_trash_chunk(); |
| 1721 | if (!replace) |
| 1722 | goto leave; |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 1723 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1724 | replace->data = build_logline(s, replace->area, replace->size, fmt); |
| 1725 | if (replace->data >= replace->size - 1) |
Christopher Faulet | 07a0fec | 2017-02-08 12:17:07 +0100 | [diff] [blame] | 1726 | goto leave; |
| 1727 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1728 | ret = http_transform_header_str(s, msg, name, name_len, replace->area, |
| 1729 | re, action); |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 1730 | |
Christopher Faulet | 07a0fec | 2017-02-08 12:17:07 +0100 | [diff] [blame] | 1731 | leave: |
| 1732 | free_trash_chunk(replace); |
| 1733 | return ret; |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 1734 | } |
| 1735 | |
Frédéric Lécaille | 9ca51aa | 2018-11-12 10:06:54 +0100 | [diff] [blame] | 1736 | /* |
| 1737 | * Build an HTTP Early Hint HTTP 103 response header with <name> as name and with a value |
| 1738 | * built according to <fmt> log line format. |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1739 | * If <early_hints> is NULL, it is allocated and the HTTP 103 response first |
| 1740 | * line is inserted before the header. If an error occurred <early_hints> is |
| 1741 | * released and NULL is returned. On success the updated buffer is returned. |
Frédéric Lécaille | 9ca51aa | 2018-11-12 10:06:54 +0100 | [diff] [blame] | 1742 | */ |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1743 | static struct buffer *http_apply_early_hint_rule(struct stream* s, struct buffer *early_hints, |
| 1744 | const char* name, unsigned int name_len, |
| 1745 | struct list *fmt) |
Frédéric Lécaille | 9ca51aa | 2018-11-12 10:06:54 +0100 | [diff] [blame] | 1746 | { |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1747 | if (!early_hints) { |
| 1748 | early_hints = alloc_trash_chunk(); |
| 1749 | if (!early_hints) |
| 1750 | goto fail; |
| 1751 | if (!chunk_memcat(early_hints, HTTP_103.ptr, HTTP_103.len)) |
| 1752 | goto fail; |
| 1753 | } |
Frédéric Lécaille | 9ca51aa | 2018-11-12 10:06:54 +0100 | [diff] [blame] | 1754 | |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1755 | if (!chunk_memcat(early_hints, name, name_len) || !chunk_memcat(early_hints, ": ", 2)) |
| 1756 | goto fail; |
Frédéric Lécaille | 9ca51aa | 2018-11-12 10:06:54 +0100 | [diff] [blame] | 1757 | |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1758 | early_hints->data += build_logline(s, b_tail(early_hints), b_room(early_hints), fmt); |
| 1759 | if (!chunk_memcat(early_hints, "\r\n", 2)) |
| 1760 | goto fail; |
Frédéric Lécaille | 9ca51aa | 2018-11-12 10:06:54 +0100 | [diff] [blame] | 1761 | |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1762 | return early_hints; |
Frédéric Lécaille | 9ca51aa | 2018-11-12 10:06:54 +0100 | [diff] [blame] | 1763 | |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1764 | fail: |
| 1765 | free_trash_chunk(early_hints); |
| 1766 | return NULL; |
| 1767 | } |
Frédéric Lécaille | 9ca51aa | 2018-11-12 10:06:54 +0100 | [diff] [blame] | 1768 | |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1769 | /* Sends an HTTP 103 response. Before sending it, the last CRLF finishing the |
| 1770 | * response is added. If an error occurred or if another response was already |
| 1771 | * sent, this function does nothing. |
| 1772 | */ |
| 1773 | static void http_send_early_hints(struct stream *s, struct buffer *early_hints) |
| 1774 | { |
| 1775 | struct channel *chn = s->txn->rsp.chn; |
| 1776 | char *cur_ptr = ci_head(chn); |
| 1777 | int ret; |
Frédéric Lécaille | 9ca51aa | 2018-11-12 10:06:54 +0100 | [diff] [blame] | 1778 | |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1779 | /* If a response was already sent, skip early hints */ |
| 1780 | if (s->txn->status > 0) |
| 1781 | return; |
Frédéric Lécaille | 9ca51aa | 2018-11-12 10:06:54 +0100 | [diff] [blame] | 1782 | |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1783 | if (!chunk_memcat(early_hints, "\r\n", 2)) |
| 1784 | return; |
Frédéric Lécaille | 9ca51aa | 2018-11-12 10:06:54 +0100 | [diff] [blame] | 1785 | |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1786 | ret = b_rep_blk(&chn->buf, cur_ptr, cur_ptr, b_head(early_hints), b_data(early_hints)); |
| 1787 | c_adv(chn, ret); |
| 1788 | chn->total += ret; |
Frédéric Lécaille | 9ca51aa | 2018-11-12 10:06:54 +0100 | [diff] [blame] | 1789 | } |
| 1790 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1791 | /* Executes the http-request rules <rules> for stream <s>, proxy <px> and |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 1792 | * transaction <txn>. Returns the verdict of the first rule that prevents |
| 1793 | * further processing of the request (auth, deny, ...), and defaults to |
| 1794 | * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or |
| 1795 | * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 1796 | * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL |
| 1797 | * and a deny/tarpit rule is matched, it will be filled with this rule's deny |
| 1798 | * status. |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 1799 | */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 1800 | enum rule_result |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 1801 | http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 1802 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 1803 | struct session *sess = strm_sess(s); |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 1804 | struct http_txn *txn = s->txn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1805 | struct connection *cli_conn; |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 1806 | struct act_rule *rule; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 1807 | struct hdr_ctx ctx; |
Willy Tarreau | ae3c010 | 2014-04-28 23:22:08 +0200 | [diff] [blame] | 1808 | const char *auth_realm; |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1809 | struct buffer *early_hints = NULL; |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 1810 | enum rule_result rule_ret = HTTP_RULE_RES_CONT; |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 1811 | int act_flags = 0; |
Thierry Fournier | 4b788f7 | 2016-06-01 13:35:36 +0200 | [diff] [blame] | 1812 | int len; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 1813 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 1814 | /* If "the current_rule_list" match the executed rule list, we are in |
| 1815 | * resume condition. If a resume is needed it is always in the action |
| 1816 | * and never in the ACL or converters. In this case, we initialise the |
| 1817 | * current rule, and go to the action execution point. |
| 1818 | */ |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 1819 | if (s->current_rule) { |
| 1820 | rule = s->current_rule; |
| 1821 | s->current_rule = NULL; |
| 1822 | if (s->current_rule_list == rules) |
| 1823 | goto resume_execution; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 1824 | } |
| 1825 | s->current_rule_list = rules; |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 1826 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 1827 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 1828 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 1829 | /* check optional condition */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 1830 | if (rule->cond) { |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 1831 | int ret; |
| 1832 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 1833 | ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 1834 | ret = acl_pass(ret); |
| 1835 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 1836 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 1837 | ret = !ret; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 1838 | |
| 1839 | if (!ret) /* condition not matched */ |
| 1840 | continue; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 1841 | } |
| 1842 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 1843 | act_flags |= ACT_FLAG_FIRST; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 1844 | resume_execution: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 1845 | switch (rule->action) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1846 | case ACT_ACTION_ALLOW: |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 1847 | rule_ret = HTTP_RULE_RES_STOP; |
| 1848 | goto end; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 1849 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1850 | case ACT_ACTION_DENY: |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 1851 | if (deny_status) |
| 1852 | *deny_status = rule->deny_status; |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 1853 | rule_ret = HTTP_RULE_RES_DENY; |
| 1854 | goto end; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 1855 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1856 | case ACT_HTTP_REQ_TARPIT: |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 1857 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 1858 | if (deny_status) |
| 1859 | *deny_status = rule->deny_status; |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 1860 | rule_ret = HTTP_RULE_RES_DENY; |
| 1861 | goto end; |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 1862 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1863 | case ACT_HTTP_REQ_AUTH: |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1864 | /* Be sure to send any pending HTTP 103 response first */ |
| 1865 | if (early_hints) { |
| 1866 | http_send_early_hints(s, early_hints); |
| 1867 | free_trash_chunk(early_hints); |
| 1868 | early_hints = NULL; |
| 1869 | } |
Willy Tarreau | ae3c010 | 2014-04-28 23:22:08 +0200 | [diff] [blame] | 1870 | /* Auth might be performed on regular http-req rules as well as on stats */ |
| 1871 | auth_realm = rule->arg.auth.realm; |
| 1872 | if (!auth_realm) { |
| 1873 | if (px->uri_auth && rules == &px->uri_auth->http_req_rules) |
| 1874 | auth_realm = STATS_DEFAULT_REALM; |
| 1875 | else |
| 1876 | auth_realm = px->id; |
| 1877 | } |
| 1878 | /* send 401/407 depending on whether we use a proxy or not. We still |
| 1879 | * count one error, because normal browsing won't significantly |
| 1880 | * increase the counter but brute force attempts will. |
| 1881 | */ |
| 1882 | chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm); |
| 1883 | txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 1884 | http_reply_and_close(s, txn->status, &trash); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1885 | stream_inc_http_err_ctr(s); |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 1886 | rule_ret = HTTP_RULE_RES_ABRT; |
| 1887 | goto end; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 1888 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1889 | case ACT_HTTP_REDIR: |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1890 | /* Be sure to send any pending HTTP 103 response first */ |
| 1891 | if (early_hints) { |
| 1892 | http_send_early_hints(s, early_hints); |
| 1893 | free_trash_chunk(early_hints); |
| 1894 | early_hints = NULL; |
| 1895 | } |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 1896 | rule_ret = HTTP_RULE_RES_DONE; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 1897 | if (!http_apply_redirect_rule(rule->arg.redir, s, txn)) |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 1898 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 1899 | goto end; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 1900 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1901 | case ACT_HTTP_SET_NICE: |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 1902 | s->task->nice = rule->arg.nice; |
| 1903 | break; |
| 1904 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1905 | case ACT_HTTP_SET_TOS: |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 1906 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 1907 | inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos); |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 1908 | break; |
| 1909 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1910 | case ACT_HTTP_SET_MARK: |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 1911 | #ifdef SO_MARK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 1912 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 1913 | setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark)); |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 1914 | #endif |
| 1915 | break; |
| 1916 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1917 | case ACT_HTTP_SET_LOGL: |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 1918 | s->logs.level = rule->arg.loglevel; |
| 1919 | break; |
| 1920 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1921 | case ACT_HTTP_REPLACE_HDR: |
| 1922 | case ACT_HTTP_REPLACE_VAL: |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 1923 | if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name, |
| 1924 | rule->arg.hdr_add.name_len, |
| 1925 | &rule->arg.hdr_add.fmt, |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 1926 | &rule->arg.hdr_add.re, rule->action)) { |
| 1927 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 1928 | goto end; |
| 1929 | } |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 1930 | break; |
| 1931 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1932 | case ACT_HTTP_DEL_HDR: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 1933 | ctx.idx = 0; |
| 1934 | /* remove all occurrences of the header */ |
| 1935 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1936 | ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 1937 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 1938 | } |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 1939 | break; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 1940 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1941 | case ACT_HTTP_SET_HDR: |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 1942 | case ACT_HTTP_ADD_HDR: { |
Thierry Fournier | 4b788f7 | 2016-06-01 13:35:36 +0200 | [diff] [blame] | 1943 | /* The scope of the trash buffer must be limited to this function. The |
| 1944 | * build_logline() function can execute a lot of other function which |
| 1945 | * can use the trash buffer. So for limiting the scope of this global |
| 1946 | * buffer, we build first the header value using build_logline, and |
| 1947 | * after we store the header name. |
| 1948 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1949 | struct buffer *replace; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 1950 | |
| 1951 | replace = alloc_trash_chunk(); |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 1952 | if (!replace) { |
| 1953 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 1954 | goto end; |
| 1955 | } |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 1956 | |
Thierry Fournier | 4b788f7 | 2016-06-01 13:35:36 +0200 | [diff] [blame] | 1957 | len = rule->arg.hdr_add.name_len + 2, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1958 | len += build_logline(s, replace->area + len, |
| 1959 | replace->size - len, |
| 1960 | &rule->arg.hdr_add.fmt); |
| 1961 | memcpy(replace->area, rule->arg.hdr_add.name, |
| 1962 | rule->arg.hdr_add.name_len); |
| 1963 | replace->area[rule->arg.hdr_add.name_len] = ':'; |
| 1964 | replace->area[rule->arg.hdr_add.name_len + 1] = ' '; |
| 1965 | replace->data = len; |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 1966 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1967 | if (rule->action == ACT_HTTP_SET_HDR) { |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 1968 | /* remove all occurrences of the header */ |
| 1969 | ctx.idx = 0; |
| 1970 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1971 | ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 1972 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
| 1973 | } |
| 1974 | } |
| 1975 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1976 | if (http_header_add_tail2(&txn->req, &txn->hdr_idx, replace->area, replace->data) < 0) { |
Tim Duesterhus | 3fd1973 | 2018-05-27 20:35:08 +0200 | [diff] [blame] | 1977 | static unsigned char rate_limit = 0; |
| 1978 | |
| 1979 | if ((rate_limit++ & 255) == 0) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1980 | replace->area[rule->arg.hdr_add.name_len] = 0; |
| 1981 | send_log(px, LOG_WARNING, "Proxy %s failed to add or set the request header '%s' for request #%u. You might need to increase tune.maxrewrite.", px->id, |
| 1982 | replace->area, s->uniq_id); |
Tim Duesterhus | 3fd1973 | 2018-05-27 20:35:08 +0200 | [diff] [blame] | 1983 | } |
| 1984 | |
| 1985 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1); |
| 1986 | if (sess->fe != s->be) |
| 1987 | HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1); |
| 1988 | if (sess->listener->counters) |
| 1989 | HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1); |
| 1990 | } |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 1991 | |
| 1992 | free_trash_chunk(replace); |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 1993 | break; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 1994 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 1995 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1996 | case ACT_HTTP_DEL_ACL: |
| 1997 | case ACT_HTTP_DEL_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 1998 | struct pat_ref *ref; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1999 | struct buffer *key; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2000 | |
| 2001 | /* collect reference */ |
| 2002 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 2003 | if (!ref) |
| 2004 | continue; |
| 2005 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2006 | /* allocate key */ |
| 2007 | key = alloc_trash_chunk(); |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 2008 | if (!key) { |
| 2009 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 2010 | goto end; |
| 2011 | } |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2012 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2013 | /* collect key */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2014 | key->data = build_logline(s, key->area, key->size, |
| 2015 | &rule->arg.map.key); |
| 2016 | key->area[key->data] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2017 | |
| 2018 | /* perform update */ |
| 2019 | /* returned code: 1=ok, 0=ko */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2020 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2021 | pat_ref_delete(ref, key->area); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2022 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2023 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2024 | free_trash_chunk(key); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2025 | break; |
| 2026 | } |
| 2027 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2028 | case ACT_HTTP_ADD_ACL: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2029 | struct pat_ref *ref; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 2030 | struct buffer *key; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2031 | |
| 2032 | /* collect reference */ |
| 2033 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 2034 | if (!ref) |
| 2035 | continue; |
| 2036 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2037 | /* allocate key */ |
| 2038 | key = alloc_trash_chunk(); |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 2039 | if (!key) { |
| 2040 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 2041 | goto end; |
| 2042 | } |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2043 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2044 | /* collect key */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2045 | key->data = build_logline(s, key->area, key->size, |
| 2046 | &rule->arg.map.key); |
| 2047 | key->area[key->data] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2048 | |
| 2049 | /* perform update */ |
| 2050 | /* add entry only if it does not already exist */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2051 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2052 | if (pat_ref_find_elt(ref, key->area) == NULL) |
| 2053 | pat_ref_add(ref, key->area, NULL, NULL); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2054 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2055 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2056 | free_trash_chunk(key); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2057 | break; |
| 2058 | } |
| 2059 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2060 | case ACT_HTTP_SET_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2061 | struct pat_ref *ref; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 2062 | struct buffer *key, *value; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2063 | |
| 2064 | /* collect reference */ |
| 2065 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 2066 | if (!ref) |
| 2067 | continue; |
| 2068 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2069 | /* allocate key */ |
| 2070 | key = alloc_trash_chunk(); |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 2071 | if (!key) { |
| 2072 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 2073 | goto end; |
| 2074 | } |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2075 | |
| 2076 | /* allocate value */ |
| 2077 | value = alloc_trash_chunk(); |
| 2078 | if (!value) { |
| 2079 | free_trash_chunk(key); |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 2080 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 2081 | goto end; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2082 | } |
| 2083 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2084 | /* collect key */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2085 | key->data = build_logline(s, key->area, key->size, |
| 2086 | &rule->arg.map.key); |
| 2087 | key->area[key->data] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2088 | |
| 2089 | /* collect value */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2090 | value->data = build_logline(s, value->area, |
| 2091 | value->size, |
| 2092 | &rule->arg.map.value); |
| 2093 | value->area[value->data] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2094 | |
| 2095 | /* perform update */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2096 | if (pat_ref_find_elt(ref, key->area) != NULL) |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2097 | /* update entry if it exists */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2098 | pat_ref_set(ref, key->area, value->area, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2099 | else |
| 2100 | /* insert a new entry */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2101 | pat_ref_add(ref, key->area, value->area, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2102 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2103 | free_trash_chunk(key); |
| 2104 | free_trash_chunk(value); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2105 | break; |
| 2106 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 2107 | |
Frédéric Lécaille | 9ca51aa | 2018-11-12 10:06:54 +0100 | [diff] [blame] | 2108 | case ACT_HTTP_EARLY_HINT: |
| 2109 | if (!(txn->req.flags & HTTP_MSGF_VER_11)) |
| 2110 | break; |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 2111 | early_hints = http_apply_early_hint_rule(s, early_hints, |
| 2112 | rule->arg.early_hint.name, |
| 2113 | rule->arg.early_hint.name_len, |
| 2114 | &rule->arg.early_hint.fmt); |
| 2115 | if (!early_hints) { |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 2116 | rule_ret = HTTP_RULE_RES_DONE; |
| 2117 | goto end; |
| 2118 | } |
Frédéric Lécaille | 9ca51aa | 2018-11-12 10:06:54 +0100 | [diff] [blame] | 2119 | break; |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 2120 | case ACT_CUSTOM: |
Willy Tarreau | 7aa15b0 | 2017-12-20 16:56:50 +0100 | [diff] [blame] | 2121 | if ((s->req.flags & CF_READ_ERROR) || |
| 2122 | ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) && |
| 2123 | !(s->si[0].flags & SI_FL_CLEAN_ABRT) && |
| 2124 | (px->options & PR_O_ABRT_CLOSE))) |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2125 | act_flags |= ACT_FLAG_FINAL; |
Willy Tarreau | 3945868 | 2015-09-27 10:33:15 +0200 | [diff] [blame] | 2126 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2127 | switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) { |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 2128 | case ACT_RET_ERR: |
| 2129 | case ACT_RET_CONT: |
| 2130 | break; |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 2131 | case ACT_RET_STOP: |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 2132 | rule_ret = HTTP_RULE_RES_DONE; |
| 2133 | goto end; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 2134 | case ACT_RET_YIELD: |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 2135 | s->current_rule = rule; |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 2136 | rule_ret = HTTP_RULE_RES_YIELD; |
| 2137 | goto end; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2138 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 2139 | break; |
| 2140 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2141 | case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX: |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2142 | /* Note: only the first valid tracking parameter of each |
| 2143 | * applies. |
| 2144 | */ |
| 2145 | |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 2146 | if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) { |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2147 | struct stktable *t; |
| 2148 | struct stksess *ts; |
| 2149 | struct stktable_key *key; |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 2150 | void *ptr1, *ptr2; |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2151 | |
Thierry FOURNIER | 5ec63e0 | 2015-08-04 09:09:48 +0200 | [diff] [blame] | 2152 | t = rule->arg.trk_ctr.table.t; |
| 2153 | key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_REQ | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2154 | |
| 2155 | if (key && (ts = stktable_get_entry(t, key))) { |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 2156 | stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2157 | |
| 2158 | /* let's count a new HTTP request as it's the first time we do it */ |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 2159 | ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT); |
| 2160 | ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE); |
| 2161 | if (ptr1 || ptr2) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2162 | HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2163 | |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 2164 | if (ptr1) |
| 2165 | stktable_data_cast(ptr1, http_req_cnt)++; |
| 2166 | |
| 2167 | if (ptr2) |
| 2168 | update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate), |
| 2169 | t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1); |
| 2170 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2171 | HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock); |
Emeric Brun | 0fed0b0 | 2017-11-29 16:15:07 +0100 | [diff] [blame] | 2172 | |
| 2173 | /* If data was modified, we need to touch to re-schedule sync */ |
| 2174 | stktable_touch_local(t, ts, 0); |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 2175 | } |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2176 | |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 2177 | stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT); |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2178 | if (sess->fe != s->be) |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 2179 | stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2180 | } |
| 2181 | } |
Adis Nezirovic | 2fbcafc | 2015-07-06 15:44:30 +0200 | [diff] [blame] | 2182 | break; |
| 2183 | |
Joseph Herlant | 5ba8025 | 2018-11-15 09:25:36 -0800 | [diff] [blame] | 2184 | /* other flags exists, but normally, they never be matched. */ |
Thierry FOURNIER | 22e4901 | 2015-08-05 19:13:48 +0200 | [diff] [blame] | 2185 | default: |
| 2186 | break; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2187 | } |
| 2188 | } |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2189 | |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 2190 | end: |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 2191 | if (early_hints) { |
| 2192 | http_send_early_hints(s, early_hints); |
| 2193 | free_trash_chunk(early_hints); |
Frédéric Lécaille | 9ca51aa | 2018-11-12 10:06:54 +0100 | [diff] [blame] | 2194 | } |
| 2195 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2196 | /* we reached the end of the rules, nothing to report */ |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 2197 | return rule_ret; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2198 | } |
| 2199 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2200 | |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 2201 | /* Executes the http-response rules <rules> for stream <s> and proxy <px>. It |
| 2202 | * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP, |
| 2203 | * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT |
| 2204 | * is returned, the process can continue the evaluation of next rule list. If |
| 2205 | * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ |
| 2206 | * is returned, it means the operation could not be processed and a server error |
| 2207 | * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a |
| 2208 | * deny rule. If *YIELD is returned, the caller must call again the function |
| 2209 | * with the same context. |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2210 | */ |
Christopher Faulet | 10079f5 | 2018-10-03 15:17:28 +0200 | [diff] [blame] | 2211 | enum rule_result |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 2212 | http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s) |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2213 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2214 | struct session *sess = strm_sess(s); |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 2215 | struct http_txn *txn = s->txn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2216 | struct connection *cli_conn; |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 2217 | struct act_rule *rule; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2218 | struct hdr_ctx ctx; |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 2219 | enum rule_result rule_ret = HTTP_RULE_RES_CONT; |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2220 | int act_flags = 0; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2221 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2222 | /* If "the current_rule_list" match the executed rule list, we are in |
| 2223 | * resume condition. If a resume is needed it is always in the action |
| 2224 | * and never in the ACL or converters. In this case, we initialise the |
| 2225 | * current rule, and go to the action execution point. |
| 2226 | */ |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 2227 | if (s->current_rule) { |
| 2228 | rule = s->current_rule; |
| 2229 | s->current_rule = NULL; |
| 2230 | if (s->current_rule_list == rules) |
| 2231 | goto resume_execution; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2232 | } |
| 2233 | s->current_rule_list = rules; |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 2234 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2235 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2236 | |
| 2237 | /* check optional condition */ |
| 2238 | if (rule->cond) { |
| 2239 | int ret; |
| 2240 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 2241 | ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2242 | ret = acl_pass(ret); |
| 2243 | |
| 2244 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 2245 | ret = !ret; |
| 2246 | |
| 2247 | if (!ret) /* condition not matched */ |
| 2248 | continue; |
| 2249 | } |
| 2250 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2251 | act_flags |= ACT_FLAG_FIRST; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2252 | resume_execution: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2253 | switch (rule->action) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2254 | case ACT_ACTION_ALLOW: |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 2255 | rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */ |
| 2256 | goto end; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2257 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2258 | case ACT_ACTION_DENY: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2259 | txn->flags |= TX_SVDENY; |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 2260 | rule_ret = HTTP_RULE_RES_STOP; |
| 2261 | goto end; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2262 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2263 | case ACT_HTTP_SET_NICE: |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 2264 | s->task->nice = rule->arg.nice; |
| 2265 | break; |
| 2266 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2267 | case ACT_HTTP_SET_TOS: |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2268 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 2269 | inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos); |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 2270 | break; |
| 2271 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2272 | case ACT_HTTP_SET_MARK: |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 2273 | #ifdef SO_MARK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2274 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 2275 | setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark)); |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 2276 | #endif |
| 2277 | break; |
| 2278 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2279 | case ACT_HTTP_SET_LOGL: |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 2280 | s->logs.level = rule->arg.loglevel; |
| 2281 | break; |
| 2282 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2283 | case ACT_HTTP_REPLACE_HDR: |
| 2284 | case ACT_HTTP_REPLACE_VAL: |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 2285 | if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name, |
| 2286 | rule->arg.hdr_add.name_len, |
| 2287 | &rule->arg.hdr_add.fmt, |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 2288 | &rule->arg.hdr_add.re, rule->action)) { |
| 2289 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 2290 | goto end; |
| 2291 | } |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 2292 | break; |
| 2293 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2294 | case ACT_HTTP_DEL_HDR: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2295 | ctx.idx = 0; |
| 2296 | /* remove all occurrences of the header */ |
| 2297 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 2298 | ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) { |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2299 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 2300 | } |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 2301 | break; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2302 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2303 | case ACT_HTTP_SET_HDR: |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2304 | case ACT_HTTP_ADD_HDR: { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 2305 | struct buffer *replace; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2306 | |
| 2307 | replace = alloc_trash_chunk(); |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 2308 | if (!replace) { |
| 2309 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 2310 | goto end; |
| 2311 | } |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2312 | |
| 2313 | chunk_printf(replace, "%s: ", rule->arg.hdr_add.name); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2314 | memcpy(replace->area, rule->arg.hdr_add.name, |
| 2315 | rule->arg.hdr_add.name_len); |
| 2316 | replace->data = rule->arg.hdr_add.name_len; |
| 2317 | replace->area[replace->data++] = ':'; |
| 2318 | replace->area[replace->data++] = ' '; |
| 2319 | replace->data += build_logline(s, |
| 2320 | replace->area + replace->data, |
| 2321 | replace->size - replace->data, |
| 2322 | &rule->arg.hdr_add.fmt); |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 2323 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2324 | if (rule->action == ACT_HTTP_SET_HDR) { |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 2325 | /* remove all occurrences of the header */ |
| 2326 | ctx.idx = 0; |
| 2327 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 2328 | ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 2329 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 2330 | } |
| 2331 | } |
Tim Duesterhus | 3fd1973 | 2018-05-27 20:35:08 +0200 | [diff] [blame] | 2332 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2333 | if (http_header_add_tail2(&txn->rsp, &txn->hdr_idx, replace->area, replace->data) < 0) { |
Tim Duesterhus | 3fd1973 | 2018-05-27 20:35:08 +0200 | [diff] [blame] | 2334 | static unsigned char rate_limit = 0; |
| 2335 | |
| 2336 | if ((rate_limit++ & 255) == 0) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2337 | replace->area[rule->arg.hdr_add.name_len] = 0; |
| 2338 | send_log(px, LOG_WARNING, "Proxy %s failed to add or set the response header '%s' for request #%u. You might need to increase tune.maxrewrite.", px->id, |
| 2339 | replace->area, s->uniq_id); |
Tim Duesterhus | 3fd1973 | 2018-05-27 20:35:08 +0200 | [diff] [blame] | 2340 | } |
| 2341 | |
| 2342 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1); |
| 2343 | if (sess->fe != s->be) |
| 2344 | HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1); |
| 2345 | if (sess->listener->counters) |
| 2346 | HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1); |
| 2347 | if (objt_server(s->target)) |
| 2348 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_rewrites, 1); |
| 2349 | } |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2350 | |
| 2351 | free_trash_chunk(replace); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2352 | break; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2353 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2354 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2355 | case ACT_HTTP_DEL_ACL: |
| 2356 | case ACT_HTTP_DEL_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2357 | struct pat_ref *ref; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 2358 | struct buffer *key; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2359 | |
| 2360 | /* collect reference */ |
| 2361 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 2362 | if (!ref) |
| 2363 | continue; |
| 2364 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2365 | /* allocate key */ |
| 2366 | key = alloc_trash_chunk(); |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 2367 | if (!key) { |
| 2368 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 2369 | goto end; |
| 2370 | } |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2371 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2372 | /* collect key */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2373 | key->data = build_logline(s, key->area, key->size, |
| 2374 | &rule->arg.map.key); |
| 2375 | key->area[key->data] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2376 | |
| 2377 | /* perform update */ |
| 2378 | /* returned code: 1=ok, 0=ko */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2379 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2380 | pat_ref_delete(ref, key->area); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2381 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2382 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2383 | free_trash_chunk(key); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2384 | break; |
| 2385 | } |
| 2386 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2387 | case ACT_HTTP_ADD_ACL: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2388 | struct pat_ref *ref; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 2389 | struct buffer *key; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2390 | |
| 2391 | /* collect reference */ |
| 2392 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 2393 | if (!ref) |
| 2394 | continue; |
| 2395 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2396 | /* allocate key */ |
| 2397 | key = alloc_trash_chunk(); |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 2398 | if (!key) { |
| 2399 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 2400 | goto end; |
| 2401 | } |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2402 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2403 | /* collect key */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2404 | key->data = build_logline(s, key->area, key->size, |
| 2405 | &rule->arg.map.key); |
| 2406 | key->area[key->data] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2407 | |
| 2408 | /* perform update */ |
| 2409 | /* check if the entry already exists */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2410 | if (pat_ref_find_elt(ref, key->area) == NULL) |
| 2411 | pat_ref_add(ref, key->area, NULL, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2412 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2413 | free_trash_chunk(key); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2414 | break; |
| 2415 | } |
| 2416 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2417 | case ACT_HTTP_SET_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2418 | struct pat_ref *ref; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 2419 | struct buffer *key, *value; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2420 | |
| 2421 | /* collect reference */ |
| 2422 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 2423 | if (!ref) |
| 2424 | continue; |
| 2425 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2426 | /* allocate key */ |
| 2427 | key = alloc_trash_chunk(); |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 2428 | if (!key) { |
| 2429 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 2430 | goto end; |
| 2431 | } |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2432 | |
| 2433 | /* allocate value */ |
| 2434 | value = alloc_trash_chunk(); |
| 2435 | if (!value) { |
| 2436 | free_trash_chunk(key); |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 2437 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 2438 | goto end; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2439 | } |
| 2440 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2441 | /* collect key */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2442 | key->data = build_logline(s, key->area, key->size, |
| 2443 | &rule->arg.map.key); |
| 2444 | key->area[key->data] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2445 | |
| 2446 | /* collect value */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2447 | value->data = build_logline(s, value->area, |
| 2448 | value->size, |
| 2449 | &rule->arg.map.value); |
| 2450 | value->area[value->data] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2451 | |
| 2452 | /* perform update */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2453 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2454 | if (pat_ref_find_elt(ref, key->area) != NULL) |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2455 | /* update entry if it exists */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2456 | pat_ref_set(ref, key->area, value->area, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2457 | else |
| 2458 | /* insert a new entry */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2459 | pat_ref_add(ref, key->area, value->area, NULL); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2460 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2461 | free_trash_chunk(key); |
| 2462 | free_trash_chunk(value); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2463 | break; |
| 2464 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 2465 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2466 | case ACT_HTTP_REDIR: |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 2467 | rule_ret = HTTP_RULE_RES_DONE; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 2468 | if (!http_apply_redirect_rule(rule->arg.redir, s, txn)) |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 2469 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 2470 | goto end; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 2471 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 2472 | case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX: |
| 2473 | /* Note: only the first valid tracking parameter of each |
| 2474 | * applies. |
| 2475 | */ |
| 2476 | |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 2477 | if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) { |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 2478 | struct stktable *t; |
| 2479 | struct stksess *ts; |
| 2480 | struct stktable_key *key; |
| 2481 | void *ptr; |
| 2482 | |
| 2483 | t = rule->arg.trk_ctr.table.t; |
| 2484 | key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL); |
| 2485 | |
| 2486 | if (key && (ts = stktable_get_entry(t, key))) { |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 2487 | stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts); |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 2488 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2489 | HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock); |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 2490 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 2491 | /* let's count a new HTTP request as it's the first time we do it */ |
| 2492 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT); |
| 2493 | if (ptr) |
| 2494 | stktable_data_cast(ptr, http_req_cnt)++; |
| 2495 | |
| 2496 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE); |
| 2497 | if (ptr) |
| 2498 | update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate), |
| 2499 | t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1); |
| 2500 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 2501 | /* When the client triggers a 4xx from the server, it's most often due |
| 2502 | * to a missing object or permission. These events should be tracked |
| 2503 | * because if they happen often, it may indicate a brute force or a |
| 2504 | * vulnerability scan. Normally this is done when receiving the response |
| 2505 | * but here we're tracking after this ought to have been done so we have |
| 2506 | * to do it on purpose. |
| 2507 | */ |
Willy Tarreau | 3146a4c | 2016-07-26 15:22:33 +0200 | [diff] [blame] | 2508 | if ((unsigned)(txn->status - 400) < 100) { |
| 2509 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT); |
| 2510 | if (ptr) |
| 2511 | stktable_data_cast(ptr, http_err_cnt)++; |
| 2512 | |
| 2513 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE); |
| 2514 | if (ptr) |
| 2515 | update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate), |
| 2516 | t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1); |
| 2517 | } |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 2518 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2519 | HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock); |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 2520 | |
Emeric Brun | 0fed0b0 | 2017-11-29 16:15:07 +0100 | [diff] [blame] | 2521 | /* If data was modified, we need to touch to re-schedule sync */ |
| 2522 | stktable_touch_local(t, ts, 0); |
| 2523 | |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 2524 | stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT); |
| 2525 | if (sess->fe != s->be) |
| 2526 | stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND); |
| 2527 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 2528 | } |
| 2529 | } |
| 2530 | break; |
| 2531 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 2532 | case ACT_CUSTOM: |
Willy Tarreau | 7aa15b0 | 2017-12-20 16:56:50 +0100 | [diff] [blame] | 2533 | if ((s->req.flags & CF_READ_ERROR) || |
| 2534 | ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) && |
| 2535 | !(s->si[0].flags & SI_FL_CLEAN_ABRT) && |
| 2536 | (px->options & PR_O_ABRT_CLOSE))) |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2537 | act_flags |= ACT_FLAG_FINAL; |
Willy Tarreau | 3945868 | 2015-09-27 10:33:15 +0200 | [diff] [blame] | 2538 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2539 | switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) { |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 2540 | case ACT_RET_ERR: |
| 2541 | case ACT_RET_CONT: |
| 2542 | break; |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 2543 | case ACT_RET_STOP: |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 2544 | rule_ret = HTTP_RULE_RES_STOP; |
| 2545 | goto end; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 2546 | case ACT_RET_YIELD: |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 2547 | s->current_rule = rule; |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 2548 | rule_ret = HTTP_RULE_RES_YIELD; |
| 2549 | goto end; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2550 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 2551 | break; |
| 2552 | |
Joseph Herlant | 5ba8025 | 2018-11-15 09:25:36 -0800 | [diff] [blame] | 2553 | /* other flags exists, but normally, they never be matched. */ |
Thierry FOURNIER | 22e4901 | 2015-08-05 19:13:48 +0200 | [diff] [blame] | 2554 | default: |
| 2555 | break; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2556 | } |
| 2557 | } |
| 2558 | |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 2559 | end: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2560 | /* we reached the end of the rules, nothing to report */ |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 2561 | return rule_ret; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2562 | } |
| 2563 | |
| 2564 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2565 | /* Perform an HTTP redirect based on the information in <rule>. The function |
| 2566 | * returns non-zero on success, or zero in case of a, irrecoverable error such |
| 2567 | * as too large a request to build a valid response. |
| 2568 | */ |
Christopher Faulet | 10079f5 | 2018-10-03 15:17:28 +0200 | [diff] [blame] | 2569 | int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn) |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2570 | { |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 2571 | struct http_msg *req = &txn->req; |
| 2572 | struct http_msg *res = &txn->rsp; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2573 | const char *msg_fmt; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 2574 | struct buffer *chunk; |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2575 | int ret = 0; |
| 2576 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2577 | if (IS_HTX_STRM(s)) |
| 2578 | return htx_apply_redirect_rule(rule, s, txn); |
| 2579 | |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2580 | chunk = alloc_trash_chunk(); |
| 2581 | if (!chunk) |
| 2582 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2583 | |
| 2584 | /* build redirect message */ |
| 2585 | switch(rule->code) { |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 2586 | case 308: |
| 2587 | msg_fmt = HTTP_308; |
| 2588 | break; |
| 2589 | case 307: |
| 2590 | msg_fmt = HTTP_307; |
| 2591 | break; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2592 | case 303: |
| 2593 | msg_fmt = HTTP_303; |
| 2594 | break; |
| 2595 | case 301: |
| 2596 | msg_fmt = HTTP_301; |
| 2597 | break; |
| 2598 | case 302: |
| 2599 | default: |
| 2600 | msg_fmt = HTTP_302; |
| 2601 | break; |
| 2602 | } |
| 2603 | |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2604 | if (unlikely(!chunk_strcpy(chunk, msg_fmt))) |
| 2605 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2606 | |
| 2607 | switch(rule->type) { |
| 2608 | case REDIRECT_TYPE_SCHEME: { |
| 2609 | const char *path; |
| 2610 | const char *host; |
| 2611 | struct hdr_ctx ctx; |
| 2612 | int pathlen; |
| 2613 | int hostlen; |
| 2614 | |
| 2615 | host = ""; |
| 2616 | hostlen = 0; |
| 2617 | ctx.idx = 0; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 2618 | if (http_find_header2("Host", 4, ci_head(req->chn), &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2619 | host = ctx.line + ctx.val; |
| 2620 | hostlen = ctx.vlen; |
| 2621 | } |
| 2622 | |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 2623 | path = http_txn_get_path(txn); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2624 | /* build message using path */ |
| 2625 | if (path) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 2626 | pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2627 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 2628 | int qs = 0; |
| 2629 | while (qs < pathlen) { |
| 2630 | if (path[qs] == '?') { |
| 2631 | pathlen = qs; |
| 2632 | break; |
| 2633 | } |
| 2634 | qs++; |
| 2635 | } |
| 2636 | } |
| 2637 | } else { |
| 2638 | path = "/"; |
| 2639 | pathlen = 1; |
| 2640 | } |
| 2641 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2642 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 2643 | /* check if we can add scheme + "://" + host + path */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2644 | if (chunk->data + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4) |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2645 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2646 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2647 | /* add scheme */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2648 | memcpy(chunk->area + chunk->data, rule->rdr_str, |
| 2649 | rule->rdr_len); |
| 2650 | chunk->data += rule->rdr_len; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2651 | } |
| 2652 | else { |
| 2653 | /* add scheme with executing log format */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2654 | chunk->data += build_logline(s, |
| 2655 | chunk->area + chunk->data, |
| 2656 | chunk->size - chunk->data, |
| 2657 | &rule->rdr_fmt); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2658 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2659 | /* check if we can add scheme + "://" + host + path */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2660 | if (chunk->data + 3 + hostlen + pathlen > chunk->size - 4) |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2661 | goto leave; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2662 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2663 | /* add "://" */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2664 | memcpy(chunk->area + chunk->data, "://", 3); |
| 2665 | chunk->data += 3; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2666 | |
| 2667 | /* add host */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2668 | memcpy(chunk->area + chunk->data, host, hostlen); |
| 2669 | chunk->data += hostlen; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2670 | |
| 2671 | /* add path */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2672 | memcpy(chunk->area + chunk->data, path, pathlen); |
| 2673 | chunk->data += pathlen; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2674 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2675 | /* append a slash at the end of the location if needed and missing */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2676 | if (chunk->data && chunk->area[chunk->data - 1] != '/' && |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2677 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2678 | if (chunk->data > chunk->size - 5) |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2679 | goto leave; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2680 | chunk->area[chunk->data] = '/'; |
| 2681 | chunk->data++; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2682 | } |
| 2683 | |
| 2684 | break; |
| 2685 | } |
| 2686 | case REDIRECT_TYPE_PREFIX: { |
| 2687 | const char *path; |
| 2688 | int pathlen; |
| 2689 | |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 2690 | path = http_txn_get_path(txn); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2691 | /* build message using path */ |
| 2692 | if (path) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 2693 | pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2694 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 2695 | int qs = 0; |
| 2696 | while (qs < pathlen) { |
| 2697 | if (path[qs] == '?') { |
| 2698 | pathlen = qs; |
| 2699 | break; |
| 2700 | } |
| 2701 | qs++; |
| 2702 | } |
| 2703 | } |
| 2704 | } else { |
| 2705 | path = "/"; |
| 2706 | pathlen = 1; |
| 2707 | } |
| 2708 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2709 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2710 | if (chunk->data + rule->rdr_len + pathlen > chunk->size - 4) |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2711 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2712 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2713 | /* add prefix. Note that if prefix == "/", we don't want to |
| 2714 | * add anything, otherwise it makes it hard for the user to |
| 2715 | * configure a self-redirection. |
| 2716 | */ |
| 2717 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2718 | memcpy(chunk->area + chunk->data, |
| 2719 | rule->rdr_str, rule->rdr_len); |
| 2720 | chunk->data += rule->rdr_len; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2721 | } |
| 2722 | } |
| 2723 | else { |
| 2724 | /* add prefix with executing log format */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2725 | chunk->data += build_logline(s, |
| 2726 | chunk->area + chunk->data, |
| 2727 | chunk->size - chunk->data, |
| 2728 | &rule->rdr_fmt); |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2729 | |
| 2730 | /* Check length */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2731 | if (chunk->data + pathlen > chunk->size - 4) |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2732 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2733 | } |
| 2734 | |
| 2735 | /* add path */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2736 | memcpy(chunk->area + chunk->data, path, pathlen); |
| 2737 | chunk->data += pathlen; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2738 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2739 | /* append a slash at the end of the location if needed and missing */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2740 | if (chunk->data && chunk->area[chunk->data - 1] != '/' && |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2741 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2742 | if (chunk->data > chunk->size - 5) |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2743 | goto leave; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2744 | chunk->area[chunk->data] = '/'; |
| 2745 | chunk->data++; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2746 | } |
| 2747 | |
| 2748 | break; |
| 2749 | } |
| 2750 | case REDIRECT_TYPE_LOCATION: |
| 2751 | default: |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2752 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2753 | if (chunk->data + rule->rdr_len > chunk->size - 4) |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2754 | goto leave; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2755 | |
| 2756 | /* add location */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2757 | memcpy(chunk->area + chunk->data, rule->rdr_str, |
| 2758 | rule->rdr_len); |
| 2759 | chunk->data += rule->rdr_len; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2760 | } |
| 2761 | else { |
| 2762 | /* add location with executing log format */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2763 | chunk->data += build_logline(s, |
| 2764 | chunk->area + chunk->data, |
| 2765 | chunk->size - chunk->data, |
| 2766 | &rule->rdr_fmt); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2767 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2768 | /* Check left length */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2769 | if (chunk->data > chunk->size - 4) |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2770 | goto leave; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2771 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2772 | break; |
| 2773 | } |
| 2774 | |
| 2775 | if (rule->cookie_len) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2776 | memcpy(chunk->area + chunk->data, "\r\nSet-Cookie: ", 14); |
| 2777 | chunk->data += 14; |
| 2778 | memcpy(chunk->area + chunk->data, rule->cookie_str, |
| 2779 | rule->cookie_len); |
| 2780 | chunk->data += rule->cookie_len; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2781 | } |
| 2782 | |
Willy Tarreau | 19b1412 | 2017-02-28 09:48:11 +0100 | [diff] [blame] | 2783 | /* add end of headers and the keep-alive/close status. */ |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2784 | txn->status = rule->code; |
| 2785 | /* let's log the request time */ |
| 2786 | s->logs.tv_request = now; |
| 2787 | |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 2788 | if (((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) && |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2789 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 2790 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 2791 | /* keep-alive possible */ |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 2792 | if (!(req->flags & HTTP_MSGF_VER_11)) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2793 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2794 | memcpy(chunk->area + chunk->data, |
| 2795 | "\r\nProxy-Connection: keep-alive", 30); |
| 2796 | chunk->data += 30; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2797 | } else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2798 | memcpy(chunk->area + chunk->data, |
| 2799 | "\r\nConnection: keep-alive", 24); |
| 2800 | chunk->data += 24; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2801 | } |
| 2802 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2803 | memcpy(chunk->area + chunk->data, "\r\n\r\n", 4); |
| 2804 | chunk->data += 4; |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2805 | FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk)); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2806 | co_inject(res->chn, chunk->area, chunk->data); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2807 | /* "eat" the request */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2808 | b_del(&req->chn->buf, req->sov); |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 2809 | req->next -= req->sov; |
| 2810 | req->sov = 0; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 2811 | s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_REQ_FLT_END); |
Christopher Faulet | 014e39c | 2017-03-10 13:52:30 +0100 | [diff] [blame] | 2812 | s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->res.analysers & AN_RES_FLT_END); |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 2813 | req->msg_state = HTTP_MSG_CLOSED; |
| 2814 | res->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 2815 | /* Trim any possible response */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2816 | b_set_data(&res->chn->buf, co_data(res->chn)); |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 2817 | res->next = res->sov = 0; |
Christopher Faulet | 5d468ca | 2017-09-11 09:27:29 +0200 | [diff] [blame] | 2818 | /* let the server side turn to SI_ST_CLO */ |
| 2819 | channel_shutw_now(req->chn); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2820 | } else { |
| 2821 | /* keep-alive not possible */ |
| 2822 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2823 | memcpy(chunk->area + chunk->data, |
| 2824 | "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 2825 | chunk->data += 29; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2826 | } else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2827 | memcpy(chunk->area + chunk->data, |
| 2828 | "\r\nConnection: close\r\n\r\n", 23); |
| 2829 | chunk->data += 23; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2830 | } |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2831 | http_reply_and_close(s, txn->status, chunk); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 2832 | req->chn->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2833 | } |
| 2834 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2835 | if (!(s->flags & SF_ERR_MASK)) |
| 2836 | s->flags |= SF_ERR_LOCAL; |
| 2837 | if (!(s->flags & SF_FINST_MASK)) |
| 2838 | s->flags |= SF_FINST_R; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2839 | |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2840 | ret = 1; |
| 2841 | leave: |
| 2842 | free_trash_chunk(chunk); |
| 2843 | return ret; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2844 | } |
| 2845 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2846 | /* This stream analyser runs all HTTP request processing which is common to |
| 2847 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 2848 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2849 | * 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] | 2850 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 2851 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2852 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2853 | int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px) |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2854 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2855 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 2856 | struct http_txn *txn = s->txn; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2857 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2858 | struct redirect_rule *rule; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 2859 | struct cond_wordlist *wl; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2860 | enum rule_result verdict; |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 2861 | int deny_status = HTTP_ERR_403; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2862 | struct connection *conn = objt_conn(sess->origin); |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2863 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2864 | if (IS_HTX_STRM(s)) |
| 2865 | return htx_process_req_common(s, req, an_bit, px); |
| 2866 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2867 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2868 | /* we need more data */ |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2869 | goto return_prx_yield; |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2870 | } |
| 2871 | |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 2872 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n", |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2873 | now_ms, __FUNCTION__, |
| 2874 | s, |
| 2875 | req, |
| 2876 | req->rex, req->wex, |
| 2877 | req->flags, |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 2878 | ci_data(req), |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2879 | req->analysers); |
| 2880 | |
Willy Tarreau | 6541083 | 2014-04-28 21:25:43 +0200 | [diff] [blame] | 2881 | /* just in case we have some per-backend tracking */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2882 | stream_inc_be_http_req_ctr(s); |
Willy Tarreau | 6541083 | 2014-04-28 21:25:43 +0200 | [diff] [blame] | 2883 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2884 | /* evaluate http-request rules */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2885 | if (!LIST_ISEMPTY(&px->http_req_rules)) { |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 2886 | verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status); |
Willy Tarreau | 5142594 | 2010-02-01 10:40:19 +0100 | [diff] [blame] | 2887 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2888 | switch (verdict) { |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2889 | case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */ |
| 2890 | goto return_prx_yield; |
| 2891 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2892 | case HTTP_RULE_RES_CONT: |
| 2893 | case HTTP_RULE_RES_STOP: /* nothing to do */ |
| 2894 | break; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 2895 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2896 | case HTTP_RULE_RES_DENY: /* deny or tarpit */ |
| 2897 | if (txn->flags & TX_CLTARPIT) |
| 2898 | goto tarpit; |
| 2899 | goto deny; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 2900 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2901 | case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */ |
| 2902 | goto return_prx_cond; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 2903 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2904 | case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */ |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 2905 | goto done; |
| 2906 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2907 | case HTTP_RULE_RES_BADREQ: /* failed with a bad request */ |
| 2908 | goto return_bad_req; |
| 2909 | } |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 2910 | } |
| 2911 | |
Olivier Houchard | 25ae45a | 2017-11-29 19:51:19 +0100 | [diff] [blame] | 2912 | if (conn && (conn->flags & CO_FL_EARLY_DATA) && |
| 2913 | (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2914 | struct hdr_ctx ctx; |
| 2915 | |
| 2916 | ctx.idx = 0; |
| 2917 | if (!http_find_header2("Early-Data", strlen("Early-Data"), |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 2918 | ci_head(&s->req), &txn->hdr_idx, &ctx)) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2919 | if (unlikely(http_header_add_tail2(&txn->req, |
| 2920 | &txn->hdr_idx, "Early-Data: 1", |
Christopher Faulet | 005e79e | 2018-07-20 09:54:26 +0200 | [diff] [blame] | 2921 | strlen("Early-Data: 1")) < 0)) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2922 | goto return_bad_req; |
| 2923 | } |
| 2924 | } |
| 2925 | |
| 2926 | } |
| 2927 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 2928 | /* OK at this stage, we know that the request was accepted according to |
| 2929 | * the http-request rules, we can check for the stats. Note that the |
| 2930 | * URI is detected *before* the req* rules in order not to be affected |
| 2931 | * by a possible reqrep, while they are processed *after* so that a |
| 2932 | * reqdeny can still block them. This clearly needs to change in 1.6! |
| 2933 | */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 2934 | if (stats_check_uri(&s->si[1], txn, px)) { |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 2935 | s->target = &http_stats_applet.obj_type; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 2936 | if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) { |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 2937 | txn->status = 500; |
| 2938 | s->logs.tv_request = now; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 2939 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2940 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2941 | if (!(s->flags & SF_ERR_MASK)) |
| 2942 | s->flags |= SF_ERR_RESOURCE; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 2943 | goto return_prx_cond; |
| 2944 | } |
| 2945 | |
| 2946 | /* parse the whole stats request and extract the relevant information */ |
| 2947 | http_handle_stats(s, req); |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 2948 | verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status); |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2949 | /* not all actions implemented: deny, allow, auth */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 2950 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2951 | if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */ |
| 2952 | goto deny; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 2953 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2954 | if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */ |
| 2955 | goto return_prx_cond; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 2956 | } |
| 2957 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 2958 | /* evaluate the req* rules except reqadd */ |
| 2959 | if (px->req_exp != NULL) { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 2960 | if (apply_filters_to_request(s, req, px) < 0) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2961 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2962 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 2963 | if (txn->flags & TX_CLDENY) |
| 2964 | goto deny; |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 2965 | |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 2966 | if (txn->flags & TX_CLTARPIT) { |
| 2967 | deny_status = HTTP_ERR_500; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 2968 | goto tarpit; |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 2969 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2970 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2971 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2972 | /* add request headers from the rule sets in the same order */ |
| 2973 | list_for_each_entry(wl, &px->req_add, list) { |
| 2974 | if (wl->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 2975 | int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2976 | ret = acl_pass(ret); |
| 2977 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 2978 | ret = !ret; |
| 2979 | if (!ret) |
| 2980 | continue; |
| 2981 | } |
| 2982 | |
Christopher Faulet | 10079f5 | 2018-10-03 15:17:28 +0200 | [diff] [blame] | 2983 | if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, wl->s, strlen(wl->s)) < 0)) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2984 | goto return_bad_req; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 2985 | } |
| 2986 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 2987 | |
| 2988 | /* Proceed with the stats now. */ |
William Lallemand | 71bd11a | 2017-11-20 19:13:14 +0100 | [diff] [blame] | 2989 | if (unlikely(objt_applet(s->target) == &http_stats_applet) || |
| 2990 | unlikely(objt_applet(s->target) == &http_cache_applet)) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2991 | /* process the stats request now */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2992 | if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */ |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 2993 | HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1); |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 2994 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2995 | if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is |
| 2996 | s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy |
| 2997 | if (!(s->flags & SF_FINST_MASK)) |
| 2998 | s->flags |= SF_FINST_R; |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 2999 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 3000 | /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */ |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3001 | req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END); |
| 3002 | req->analysers &= ~AN_REQ_FLT_XFER_DATA; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 3003 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3004 | goto done; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3005 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 3006 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3007 | /* check whether we have some ACLs set to redirect this request */ |
| 3008 | list_for_each_entry(rule, &px->redirect_rules, list) { |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3009 | if (rule->cond) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3010 | int ret; |
| 3011 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 3012 | ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3013 | ret = acl_pass(ret); |
| 3014 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 3015 | ret = !ret; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3016 | if (!ret) |
| 3017 | continue; |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3018 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 3019 | if (!http_apply_redirect_rule(rule, s, txn)) |
| 3020 | goto return_bad_req; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3021 | goto done; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3022 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3023 | |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 3024 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 3025 | * If this happens, then the data will not come immediately, so we must |
| 3026 | * send all what we have without waiting. Note that due to the small gain |
| 3027 | * in waiting for the body of the request, it's easier to simply put the |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 3028 | * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 3029 | * itself once used. |
| 3030 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 3031 | req->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 3032 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3033 | done: /* done with this analyser, continue with next ones that the calling |
| 3034 | * points will have set, if any. |
| 3035 | */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3036 | req->analyse_exp = TICK_ETERNITY; |
Thierry FOURNIER | 7566e30 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 3037 | done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */ |
| 3038 | req->analysers &= ~an_bit; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3039 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 3040 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3041 | tarpit: |
Willy Tarreau | 6a0bca9 | 2017-06-11 17:56:27 +0200 | [diff] [blame] | 3042 | /* Allow cookie logging |
| 3043 | */ |
| 3044 | if (s->be->cookie_name || sess->fe->capture_name) |
| 3045 | manage_client_side_cookies(s, req); |
| 3046 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3047 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 3048 | * which may be the same as the connect timeout if unspecified. |
| 3049 | * If unset, then set it to zero because we really want it to |
| 3050 | * eventually expire. We build the tarpit as an analyser. |
| 3051 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 3052 | channel_erase(&s->req); |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3053 | |
| 3054 | /* wipe the request out so that we can drop the connection early |
| 3055 | * if the client closes first. |
| 3056 | */ |
| 3057 | channel_dont_connect(req); |
Bertrand Paquet | 83a2c3d | 2016-04-06 11:58:31 +0200 | [diff] [blame] | 3058 | |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 3059 | txn->status = http_err_codes[deny_status]; |
| 3060 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3061 | req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3062 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 3063 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 3064 | if (!req->analyse_exp) |
| 3065 | req->analyse_exp = tick_add(now_ms, 0); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3066 | stream_inc_http_err_ctr(s); |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3067 | HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3068 | if (sess->fe != s->be) |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3069 | HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3070 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 3071 | HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1); |
Thierry FOURNIER | 7566e30 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 3072 | goto done_without_exp; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3073 | |
| 3074 | deny: /* this request was blocked (denied) */ |
Bertrand Paquet | 83a2c3d | 2016-04-06 11:58:31 +0200 | [diff] [blame] | 3075 | |
| 3076 | /* Allow cookie logging |
| 3077 | */ |
| 3078 | if (s->be->cookie_name || sess->fe->capture_name) |
| 3079 | manage_client_side_cookies(s, req); |
| 3080 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3081 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 3082 | txn->status = http_err_codes[deny_status]; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3083 | s->logs.tv_request = now; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3084 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3085 | stream_inc_http_err_ctr(s); |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3086 | HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3087 | if (sess->fe != s->be) |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3088 | HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3089 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 3090 | HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1); |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3091 | goto return_prx_cond; |
| 3092 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3093 | return_bad_req: |
| 3094 | /* We centralize bad requests processing here */ |
| 3095 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 3096 | /* we detected a parsing error. We want to archive this request |
| 3097 | * in the dedicated proxy area for later troubleshooting. |
| 3098 | */ |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 3099 | http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3100 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3101 | |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 3102 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3103 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3104 | txn->status = 400; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3105 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3106 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3107 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3108 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 3109 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 3110 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3111 | return_prx_cond: |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3112 | if (!(s->flags & SF_ERR_MASK)) |
| 3113 | s->flags |= SF_ERR_PRXCOND; |
| 3114 | if (!(s->flags & SF_FINST_MASK)) |
| 3115 | s->flags |= SF_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 3116 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3117 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3118 | req->analyse_exp = TICK_ETERNITY; |
| 3119 | return 0; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3120 | |
| 3121 | return_prx_yield: |
| 3122 | channel_dont_connect(req); |
| 3123 | return 0; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3124 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3125 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3126 | /* This function performs all the processing enabled for the current request. |
| 3127 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 3128 | * needs more data, encounters an error, or wants to immediately abort the |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 3129 | * request. It relies on buffers flags, and updates s->req.analysers. |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3130 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3131 | int http_process_request(struct stream *s, struct channel *req, int an_bit) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3132 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3133 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3134 | struct http_txn *txn = s->txn; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3135 | struct http_msg *msg = &txn->req; |
Willy Tarreau | ee335e6 | 2015-04-21 18:15:13 +0200 | [diff] [blame] | 3136 | struct connection *cli_conn = objt_conn(strm_sess(s)->origin); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3137 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 3138 | if (IS_HTX_STRM(s)) |
| 3139 | return htx_process_request(s, req, an_bit); |
| 3140 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3141 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3142 | /* we need more data */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3143 | channel_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3144 | return 0; |
| 3145 | } |
| 3146 | |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 3147 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n", |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3148 | now_ms, __FUNCTION__, |
| 3149 | s, |
| 3150 | req, |
| 3151 | req->rex, req->wex, |
| 3152 | req->flags, |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 3153 | ci_data(req), |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3154 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3155 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3156 | /* |
| 3157 | * Right now, we know that we have processed the entire headers |
| 3158 | * and that unwanted requests have been filtered out. We can do |
| 3159 | * whatever we want with the remaining request. Also, now we |
| 3160 | * may have separate values for ->fe, ->be. |
| 3161 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3162 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3163 | /* |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3164 | * If HTTP PROXY is set we simply get remote server address parsing |
| 3165 | * incoming request. Note that this requires that a connection is |
| 3166 | * allocated on the server side. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3167 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3168 | if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) { |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 3169 | struct connection *conn; |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 3170 | char *path; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 3171 | |
Willy Tarreau | 9471b8c | 2013-12-15 13:31:35 +0100 | [diff] [blame] | 3172 | /* Note that for now we don't reuse existing proxy connections */ |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 3173 | if (unlikely((conn = cs_conn(si_alloc_cs(&s->si[1], NULL))) == NULL)) { |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 3174 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 3175 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3176 | txn->status = 500; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3177 | req->analysers &= AN_REQ_FLT_END; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3178 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 3179 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3180 | if (!(s->flags & SF_ERR_MASK)) |
| 3181 | s->flags |= SF_ERR_RESOURCE; |
| 3182 | if (!(s->flags & SF_FINST_MASK)) |
| 3183 | s->flags |= SF_FINST_R; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 3184 | |
| 3185 | return 0; |
| 3186 | } |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 3187 | |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 3188 | path = http_txn_get_path(txn); |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3189 | if (url2sa(ci_head(req) + msg->sl.rq.u, |
| 3190 | path ? path - (ci_head(req) + msg->sl.rq.u) : msg->sl.rq.u_l, |
Christopher Faulet | 11ebb20 | 2018-04-13 15:53:12 +0200 | [diff] [blame] | 3191 | &conn->addr.to, NULL) == -1) |
| 3192 | goto return_bad_req; |
| 3193 | |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 3194 | /* if the path was found, we have to remove everything between |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3195 | * ci_head(req) + msg->sl.rq.u and path (excluded). If it was not |
| 3196 | * found, we need to replace from ci_head(req) + msg->sl.rq.u for |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 3197 | * u_l characters by a single "/". |
| 3198 | */ |
| 3199 | if (path) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3200 | char *cur_ptr = ci_head(req); |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 3201 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 3202 | int delta; |
| 3203 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3204 | delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u, path, NULL, 0); |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 3205 | http_msg_move_end(&txn->req, delta); |
| 3206 | cur_end += delta; |
| 3207 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 3208 | goto return_bad_req; |
| 3209 | } |
| 3210 | else { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3211 | char *cur_ptr = ci_head(req); |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 3212 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 3213 | int delta; |
| 3214 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3215 | delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u, |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3216 | cur_ptr + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1); |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 3217 | http_msg_move_end(&txn->req, delta); |
| 3218 | cur_end += delta; |
| 3219 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 3220 | goto return_bad_req; |
| 3221 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3222 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3223 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3224 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3225 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3226 | * Note that doing so might move headers in the request, but |
| 3227 | * the fields will stay coherent and the URI will not move. |
| 3228 | * This should only be performed in the backend. |
| 3229 | */ |
Bertrand Paquet | 83a2c3d | 2016-04-06 11:58:31 +0200 | [diff] [blame] | 3230 | if (s->be->cookie_name || sess->fe->capture_name) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3231 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 3232 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3233 | /* add unique-id if "header-unique-id" is specified */ |
| 3234 | |
Thierry Fournier | f4011dd | 2016-03-29 17:23:51 +0200 | [diff] [blame] | 3235 | if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 3236 | if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL) |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 3237 | goto return_bad_req; |
| 3238 | s->unique_id[0] = '\0'; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3239 | build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id); |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 3240 | } |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3241 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3242 | if (sess->fe->header_unique_id && s->unique_id) { |
Willy Tarreau | 5f6333c | 2018-08-22 05:14:37 +0200 | [diff] [blame] | 3243 | if (chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id) < 0) |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3244 | goto return_bad_req; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3245 | if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, trash.data) < 0)) |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3246 | goto return_bad_req; |
| 3247 | } |
| 3248 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3249 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3250 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 3251 | * asks for it. |
| 3252 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3253 | if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 3254 | struct hdr_ctx ctx = { .idx = 0 }; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3255 | if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) && |
| 3256 | http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name, |
| 3257 | s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len, |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3258 | ci_head(req), &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 3259 | /* The header is set to be added only if none is present |
| 3260 | * and we found it, so don't do anything. |
| 3261 | */ |
| 3262 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3263 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3264 | /* Add an X-Forwarded-For header unless the source IP is |
| 3265 | * in the 'except' network range. |
| 3266 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3267 | if ((!sess->fe->except_mask.s_addr || |
| 3268 | (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr) |
| 3269 | != sess->fe->except_net.s_addr) && |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3270 | (!s->be->except_mask.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3271 | (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3272 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3273 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3274 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3275 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr; |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3276 | |
| 3277 | /* Note: we rely on the backend to get the header name to be used for |
| 3278 | * x-forwarded-for, because the header is really meant for the backends. |
| 3279 | * However, if the backend did not specify any option, we have to rely |
| 3280 | * on the frontend's header name. |
| 3281 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3282 | if (s->be->fwdfor_hdr_len) { |
| 3283 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3284 | memcpy(trash.area, |
| 3285 | s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3286 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3287 | len = sess->fe->fwdfor_hdr_len; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3288 | memcpy(trash.area, |
| 3289 | sess->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3290 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3291 | len += snprintf(trash.area + len, |
| 3292 | trash.size - len, |
| 3293 | ": %d.%d.%d.%d", pn[0], pn[1], |
| 3294 | pn[2], pn[3]); |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 3295 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3296 | if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0)) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3297 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3298 | } |
| 3299 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3300 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3301 | /* FIXME: for the sake of completeness, we should also support |
| 3302 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3303 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3304 | int len; |
| 3305 | char pn[INET6_ADDRSTRLEN]; |
| 3306 | inet_ntop(AF_INET6, |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3307 | (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3308 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3309 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3310 | /* Note: we rely on the backend to get the header name to be used for |
| 3311 | * x-forwarded-for, because the header is really meant for the backends. |
| 3312 | * However, if the backend did not specify any option, we have to rely |
| 3313 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3314 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3315 | if (s->be->fwdfor_hdr_len) { |
| 3316 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3317 | memcpy(trash.area, s->be->fwdfor_hdr_name, |
| 3318 | len); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3319 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3320 | len = sess->fe->fwdfor_hdr_len; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3321 | memcpy(trash.area, sess->fe->fwdfor_hdr_name, |
| 3322 | len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3323 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3324 | len += snprintf(trash.area + len, trash.size - len, |
| 3325 | ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3326 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3327 | if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0)) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3328 | goto return_bad_req; |
| 3329 | } |
| 3330 | } |
| 3331 | |
| 3332 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3333 | * 10: add X-Original-To if either the frontend or the backend |
| 3334 | * asks for it. |
| 3335 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3336 | if ((sess->fe->options | s->be->options) & PR_O_ORGTO) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3337 | |
| 3338 | /* FIXME: don't know if IPv6 can handle that case too. */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3339 | if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3340 | /* Add an X-Original-To header unless the destination IP is |
| 3341 | * in the 'except' network range. |
| 3342 | */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3343 | conn_get_to_addr(cli_conn); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3344 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3345 | if (cli_conn->addr.to.ss_family == AF_INET && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3346 | ((!sess->fe->except_mask_to.s_addr || |
| 3347 | (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr) |
| 3348 | != sess->fe->except_to.s_addr) && |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 3349 | (!s->be->except_mask_to.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3350 | (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & s->be->except_mask_to.s_addr) |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 3351 | != s->be->except_to.s_addr))) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3352 | int len; |
| 3353 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3354 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr; |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3355 | |
| 3356 | /* Note: we rely on the backend to get the header name to be used for |
| 3357 | * x-original-to, because the header is really meant for the backends. |
| 3358 | * However, if the backend did not specify any option, we have to rely |
| 3359 | * on the frontend's header name. |
| 3360 | */ |
| 3361 | if (s->be->orgto_hdr_len) { |
| 3362 | len = s->be->orgto_hdr_len; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3363 | memcpy(trash.area, |
| 3364 | s->be->orgto_hdr_name, len); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3365 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3366 | len = sess->fe->orgto_hdr_len; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3367 | memcpy(trash.area, |
| 3368 | sess->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3369 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3370 | len += snprintf(trash.area + len, |
| 3371 | trash.size - len, |
| 3372 | ": %d.%d.%d.%d", pn[0], pn[1], |
| 3373 | pn[2], pn[3]); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3374 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3375 | if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0)) |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3376 | goto return_bad_req; |
| 3377 | } |
| 3378 | } |
| 3379 | } |
| 3380 | |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 3381 | /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. |
| 3382 | * If an "Upgrade" token is found, the header is left untouched in order not to have |
| 3383 | * to deal with some servers bugs : some of them fail an Upgrade if anything but |
| 3384 | * "Upgrade" is present in the Connection header. |
| 3385 | */ |
Christopher Faulet | 315b39c | 2018-09-21 16:26:19 +0200 | [diff] [blame] | 3386 | if (!(txn->flags & TX_HDR_CONN_UPG) && (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3387 | unsigned int want_flags = 0; |
| 3388 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3389 | if (msg->flags & HTTP_MSGF_VER_11) { |
Christopher Faulet | 315b39c | 2018-09-21 16:26:19 +0200 | [diff] [blame] | 3390 | if ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3391 | !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3392 | want_flags |= TX_CON_CLO_SET; |
| 3393 | } else { |
Christopher Faulet | 315b39c | 2018-09-21 16:26:19 +0200 | [diff] [blame] | 3394 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3395 | ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3396 | want_flags |= TX_CON_KAL_SET; |
| 3397 | } |
| 3398 | |
| 3399 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3400 | http_change_connection_header(txn, msg, want_flags); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3401 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3402 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3403 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3404 | /* If we have no server assigned yet and we're balancing on url_param |
| 3405 | * with a POST request, we may be interested in checking the body for |
| 3406 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3407 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3408 | if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) && |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3409 | s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3410 | (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3411 | channel_dont_connect(req); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3412 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3413 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3414 | |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 3415 | req->analysers &= ~AN_REQ_FLT_XFER_DATA; |
| 3416 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 3417 | #ifdef TCP_QUICKACK |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 3418 | /* We expect some data from the client. Unless we know for sure |
| 3419 | * we already have a full request, we have to re-enable quick-ack |
| 3420 | * in case we previously disabled it, otherwise we might cause |
| 3421 | * the client to delay further data. |
| 3422 | */ |
| 3423 | if ((sess->listener->options & LI_O_NOQUICKACK) && |
| 3424 | cli_conn && conn_ctrl_ready(cli_conn) && |
| 3425 | ((msg->flags & HTTP_MSGF_TE_CHNK) || |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3426 | (msg->body_len > ci_data(req) - txn->req.eoh - 2))) |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 3427 | setsockopt(cli_conn->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 3428 | #endif |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 3429 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3430 | /************************************************************* |
| 3431 | * OK, that's finished for the headers. We have done what we * |
| 3432 | * could. Let's switch to the DATA state. * |
| 3433 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3434 | req->analyse_exp = TICK_ETERNITY; |
| 3435 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3436 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3437 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3438 | /* OK let's go on with the BODY now */ |
| 3439 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3440 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3441 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3442 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 3443 | /* we detected a parsing error. We want to archive this request |
| 3444 | * in the dedicated proxy area for later troubleshooting. |
| 3445 | */ |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 3446 | http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 3447 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3448 | |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 3449 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3450 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3451 | txn->status = 400; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3452 | req->analysers &= AN_REQ_FLT_END; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3453 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3454 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3455 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3456 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 3457 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3458 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3459 | if (!(s->flags & SF_ERR_MASK)) |
| 3460 | s->flags |= SF_ERR_PRXCOND; |
| 3461 | if (!(s->flags & SF_FINST_MASK)) |
| 3462 | s->flags |= SF_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3463 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3464 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3465 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3466 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 3467 | * returns zero, at the beginning because it prevents any other processing |
| 3468 | * from occurring, and at the end because it terminates the request. |
| 3469 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3470 | int http_process_tarpit(struct stream *s, struct channel *req, int an_bit) |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3471 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3472 | struct http_txn *txn = s->txn; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3473 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 3474 | if (IS_HTX_STRM(s)) |
| 3475 | return htx_process_tarpit(s, req, an_bit); |
| 3476 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3477 | /* This connection is being tarpitted. The CLIENT side has |
| 3478 | * already set the connect expiration date to the right |
| 3479 | * timeout. We just have to check that the client is still |
| 3480 | * there and that the timeout has not expired. |
| 3481 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3482 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 3483 | if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 && |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3484 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 3485 | return 0; |
| 3486 | |
| 3487 | /* We will set the queue timer to the time spent, just for |
| 3488 | * logging purposes. We fake a 500 server error, so that the |
| 3489 | * attacker will not suspect his connection has been tarpitted. |
| 3490 | * It will not cause trouble to the logs because we can exclude |
| 3491 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 3492 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3493 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 3494 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 3495 | if (!(req->flags & CF_READ_ERROR)) |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3496 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3497 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3498 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3499 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3500 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3501 | if (!(s->flags & SF_ERR_MASK)) |
| 3502 | s->flags |= SF_ERR_PRXCOND; |
| 3503 | if (!(s->flags & SF_FINST_MASK)) |
| 3504 | s->flags |= SF_FINST_T; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3505 | return 0; |
| 3506 | } |
| 3507 | |
Willy Tarreau | 5a8f947 | 2014-04-10 11:16:06 +0200 | [diff] [blame] | 3508 | /* This function is an analyser which waits for the HTTP request body. It waits |
| 3509 | * for either the buffer to be full, or the full advertised contents to have |
| 3510 | * reached the buffer. It must only be called after the standard HTTP request |
| 3511 | * processing has occurred, because it expects the request to be parsed and will |
| 3512 | * look for the Expect header. It may send a 100-Continue interim response. It |
| 3513 | * takes in input any state starting from HTTP_MSG_BODY and leaves with one of |
| 3514 | * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it |
| 3515 | * needs to read more data, or 1 once it has completed its analysis. |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3516 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3517 | int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit) |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3518 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3519 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3520 | struct http_txn *txn = s->txn; |
| 3521 | struct http_msg *msg = &s->txn->req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3522 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 3523 | if (IS_HTX_STRM(s)) |
| 3524 | return htx_wait_for_request_body(s, req, an_bit); |
| 3525 | |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3526 | /* We have to parse the HTTP request body to find any required data. |
| 3527 | * "balance url_param check_post" should have been the only way to get |
| 3528 | * into this. We were brought here after HTTP header analysis, so all |
| 3529 | * related structures are ready. |
| 3530 | */ |
| 3531 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 3532 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 3533 | /* This is the first call */ |
| 3534 | if (msg->msg_state < HTTP_MSG_BODY) |
| 3535 | goto missing_data; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3536 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 3537 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 3538 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 3539 | * send an HTTP/1.1 100 Continue intermediate response. |
| 3540 | */ |
| 3541 | if (msg->flags & HTTP_MSGF_VER_11) { |
| 3542 | struct hdr_ctx ctx; |
| 3543 | ctx.idx = 0; |
| 3544 | /* Expect is allowed in 1.1, look for it */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3545 | if (http_find_header2("Expect", 6, ci_head(req), &txn->hdr_idx, &ctx) && |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 3546 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) { |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 3547 | co_inject(&s->res, HTTP_100.ptr, HTTP_100.len); |
Thierry FOURNIER / OZON.IO | 43ad11d | 2016-12-12 15:19:58 +0100 | [diff] [blame] | 3548 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 3549 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3550 | } |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 3551 | msg->msg_state = HTTP_MSG_100_SENT; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3552 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3553 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 3554 | /* we have msg->sov which points to the first byte of message body. |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3555 | * ci_head(req) still points to the beginning of the message. We |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 3556 | * must save the body in msg->next because it survives buffer |
| 3557 | * re-alignments. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3558 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 3559 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 3560 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3561 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3562 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 3563 | else |
| 3564 | msg->msg_state = HTTP_MSG_DATA; |
| 3565 | } |
| 3566 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 3567 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) { |
| 3568 | /* We're in content-length mode, we just have to wait for enough data. */ |
Willy Tarreau | e115b49 | 2015-05-01 23:05:14 +0200 | [diff] [blame] | 3569 | if (http_body_bytes(msg) < msg->body_len) |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 3570 | goto missing_data; |
| 3571 | |
| 3572 | /* OK we have everything we need now */ |
| 3573 | goto http_end; |
| 3574 | } |
| 3575 | |
| 3576 | /* OK here we're parsing a chunked-encoded message */ |
| 3577 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3578 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 3579 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 3580 | * set ->sov and ->next to point to the body and switch to DATA or |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3581 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3582 | */ |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 3583 | unsigned int chunk; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3584 | int ret = h1_parse_chunk_size(&req->buf, co_data(req) + msg->next, c_data(req), &chunk); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3585 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3586 | if (!ret) |
| 3587 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3588 | else if (ret < 0) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3589 | msg->err_pos = ci_data(req) + ret; |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 3590 | if (msg->err_pos < 0) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3591 | msg->err_pos += req->buf.size; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3592 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3593 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3594 | } |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 3595 | |
| 3596 | msg->chunk_len = chunk; |
| 3597 | msg->body_len += chunk; |
| 3598 | |
| 3599 | msg->sol = ret; |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 3600 | msg->next += ret; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 3601 | msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3602 | } |
| 3603 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3604 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | e115b49 | 2015-05-01 23:05:14 +0200 | [diff] [blame] | 3605 | * We have the first data byte is in msg->sov + msg->sol. We're waiting |
| 3606 | * for at least a whole chunk or the whole content length bytes after |
| 3607 | * msg->sov + msg->sol. |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3608 | */ |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 3609 | if (msg->msg_state == HTTP_MSG_TRAILERS) |
| 3610 | goto http_end; |
| 3611 | |
Willy Tarreau | e115b49 | 2015-05-01 23:05:14 +0200 | [diff] [blame] | 3612 | if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3613 | goto http_end; |
| 3614 | |
| 3615 | missing_data: |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 3616 | /* we get here if we need to wait for more data. If the buffer is full, |
| 3617 | * we have the maximum we can expect. |
| 3618 | */ |
Willy Tarreau | 2375233 | 2018-06-15 14:54:53 +0200 | [diff] [blame] | 3619 | if (channel_full(req, global.tune.maxrewrite)) |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 3620 | goto http_end; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3621 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 3622 | if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3623 | txn->status = 408; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3624 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 3625 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3626 | if (!(s->flags & SF_ERR_MASK)) |
| 3627 | s->flags |= SF_ERR_CLITO; |
| 3628 | if (!(s->flags & SF_FINST_MASK)) |
| 3629 | s->flags |= SF_FINST_D; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3630 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3631 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3632 | |
| 3633 | /* we get here if we need to wait for more data */ |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 3634 | if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) { |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3635 | /* Not enough data. We'll re-use the http-request |
| 3636 | * timeout here. Ideally, we should set the timeout |
| 3637 | * relative to the accept() date. We just set the |
| 3638 | * request timeout once at the beginning of the |
| 3639 | * request. |
| 3640 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3641 | channel_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3642 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 3643 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3644 | return 0; |
| 3645 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3646 | |
| 3647 | http_end: |
| 3648 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 3649 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 3650 | req->analysers &= ~an_bit; |
| 3651 | req->analyse_exp = TICK_ETERNITY; |
| 3652 | return 1; |
| 3653 | |
| 3654 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 3655 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3656 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3657 | txn->status = 400; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3658 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3659 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3660 | if (!(s->flags & SF_ERR_MASK)) |
| 3661 | s->flags |= SF_ERR_PRXCOND; |
| 3662 | if (!(s->flags & SF_FINST_MASK)) |
| 3663 | s->flags |= SF_FINST_R; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 3664 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3665 | return_err_msg: |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3666 | req->analysers &= AN_REQ_FLT_END; |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3667 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3668 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 3669 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3670 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3671 | } |
| 3672 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 3673 | /* send a server's name with an outgoing request over an established connection. |
| 3674 | * Note: this function is designed to be called once the request has been scheduled |
| 3675 | * for being forwarded. This is the reason why it rewinds the buffer before |
| 3676 | * proceeding. |
| 3677 | */ |
Christopher Faulet | 27a3dc8 | 2018-10-23 15:34:07 +0200 | [diff] [blame] | 3678 | int http_send_name_header(struct stream *s, struct proxy* be, const char* srv_name) { |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3679 | |
| 3680 | struct hdr_ctx ctx; |
Christopher Faulet | 27a3dc8 | 2018-10-23 15:34:07 +0200 | [diff] [blame] | 3681 | struct http_txn *txn = s->txn; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3682 | char *hdr_name = be->server_id_hdr_name; |
| 3683 | int hdr_name_len = be->server_id_hdr_len; |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 3684 | struct channel *chn = txn->req.chn; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3685 | char *hdr_val; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 3686 | unsigned int old_o, old_i; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3687 | |
Christopher Faulet | 64159df | 2018-10-24 21:15:35 +0200 | [diff] [blame] | 3688 | if (IS_HTX_STRM(s)) |
| 3689 | return htx_send_name_header(s, be, srv_name); |
William Lallemand | d9e9066 | 2012-01-30 17:27:17 +0100 | [diff] [blame] | 3690 | ctx.idx = 0; |
| 3691 | |
Willy Tarreau | 211cdec | 2014-04-17 20:18:08 +0200 | [diff] [blame] | 3692 | old_o = http_hdr_rewind(&txn->req); |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 3693 | if (old_o) { |
| 3694 | /* The request was already skipped, let's restore it */ |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 3695 | c_rew(chn, old_o); |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 3696 | txn->req.next += old_o; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 3697 | txn->req.sov += old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 3698 | } |
| 3699 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3700 | old_i = ci_data(chn); |
| 3701 | while (http_find_header2(hdr_name, hdr_name_len, ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) { |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3702 | /* remove any existing values from the header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3703 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3704 | } |
| 3705 | |
| 3706 | /* Add the new header requested with the server value */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3707 | hdr_val = trash.area; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3708 | memcpy(hdr_val, hdr_name, hdr_name_len); |
| 3709 | hdr_val += hdr_name_len; |
| 3710 | *hdr_val++ = ':'; |
| 3711 | *hdr_val++ = ' '; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3712 | hdr_val += strlcpy2(hdr_val, srv_name, |
| 3713 | trash.area + trash.size - hdr_val); |
| 3714 | http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, |
| 3715 | hdr_val - trash.area); |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3716 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 3717 | if (old_o) { |
| 3718 | /* If this was a forwarded request, we must readjust the amount of |
| 3719 | * data to be forwarded in order to take into account the size |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 3720 | * variations. Note that the current state is >= HTTP_MSG_BODY, |
| 3721 | * so we don't have to adjust ->sol. |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 3722 | */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3723 | old_o += ci_data(chn) - old_i; |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 3724 | c_adv(chn, old_o); |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 3725 | txn->req.next -= old_o; |
| 3726 | txn->req.sov -= old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 3727 | } |
| 3728 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3729 | return 0; |
| 3730 | } |
| 3731 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3732 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 3733 | * right now but it works. |
| 3734 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3735 | void http_end_txn_clean_session(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3736 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3737 | int prev_status = s->txn->status; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 3738 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 858b103 | 2015-12-07 17:04:59 +0100 | [diff] [blame] | 3739 | struct proxy *be = s->be; |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 3740 | struct conn_stream *cs; |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 3741 | struct connection *srv_conn; |
| 3742 | struct server *srv; |
Willy Tarreau | 449d74a | 2015-08-05 17:16:33 +0200 | [diff] [blame] | 3743 | unsigned int prev_flags = s->txn->flags; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 3744 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3745 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 3746 | * server's connections. We need a safer way to reinitialize buffer |
| 3747 | * flags. We also need a more accurate method for computing per-request |
| 3748 | * data. |
| 3749 | */ |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 3750 | cs = objt_cs(s->si[1].end); |
| 3751 | srv_conn = cs_conn(cs); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3752 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 3753 | /* unless we're doing keep-alive, we want to quickly close the connection |
| 3754 | * to the server. |
| 3755 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3756 | if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
Olivier Houchard | 7c6f8b1 | 2018-11-13 16:48:36 +0100 | [diff] [blame] | 3757 | !si_conn_ready(&s->si[1]) || !srv_conn->owner) { |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3758 | s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF; |
| 3759 | si_shutr(&s->si[1]); |
| 3760 | si_shutw(&s->si[1]); |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 3761 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3762 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3763 | if (s->flags & SF_BE_ASSIGNED) { |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3764 | HA_ATOMIC_SUB(&be->beconn, 1); |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 3765 | if (unlikely(s->srv_conn)) |
| 3766 | sess_change_server(s, NULL); |
| 3767 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3768 | |
| 3769 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3770 | stream_process_counters(s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3771 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3772 | if (s->txn->status) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3773 | int n; |
| 3774 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3775 | n = s->txn->status / 100; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3776 | if (n < 1 || n > 5) |
| 3777 | n = 0; |
| 3778 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3779 | if (fe->mode == PR_MODE_HTTP) { |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3780 | HA_ATOMIC_ADD(&fe->fe_counters.p.http.rsp[n], 1); |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 3781 | } |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3782 | if ((s->flags & SF_BE_ASSIGNED) && |
Willy Tarreau | 858b103 | 2015-12-07 17:04:59 +0100 | [diff] [blame] | 3783 | (be->mode == PR_MODE_HTTP)) { |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3784 | HA_ATOMIC_ADD(&be->be_counters.p.http.rsp[n], 1); |
| 3785 | HA_ATOMIC_ADD(&be->be_counters.p.http.cum_req, 1); |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 3786 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3787 | } |
| 3788 | |
| 3789 | /* don't count other requests' data */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3790 | s->logs.bytes_in -= ci_data(&s->req); |
| 3791 | s->logs.bytes_out -= ci_data(&s->res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3792 | |
Willy Tarreau | 66425e3 | 2018-07-25 06:55:12 +0200 | [diff] [blame] | 3793 | /* we may need to know the position in the queue */ |
| 3794 | pendconn_free(s); |
| 3795 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3796 | /* let's do a final log if we need it */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3797 | if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3798 | !(s->flags & SF_MONITOR) && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3799 | (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3800 | s->do_log(s); |
| 3801 | } |
| 3802 | |
Willy Tarreau | d713bcc | 2014-06-25 15:36:04 +0200 | [diff] [blame] | 3803 | /* stop tracking content-based counters */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3804 | stream_stop_content_counters(s); |
| 3805 | stream_update_time_stats(s); |
Willy Tarreau | 4bfc580 | 2014-06-17 12:19:18 +0200 | [diff] [blame] | 3806 | |
Willy Tarreau | 9efd745 | 2018-05-31 14:48:54 +0200 | [diff] [blame] | 3807 | /* reset the profiling counter */ |
| 3808 | s->task->calls = 0; |
| 3809 | s->task->cpu_time = 0; |
| 3810 | s->task->lat_time = 0; |
| 3811 | s->task->call_date = (profiling & HA_PROF_TASKS) ? now_mono_time() : 0; |
| 3812 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3813 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 3814 | s->logs.tv_accept = now; /* corrected date for internal use */ |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 3815 | s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */ |
| 3816 | s->logs.t_idle = -1; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3817 | tv_zero(&s->logs.tv_request); |
| 3818 | s->logs.t_queue = -1; |
| 3819 | s->logs.t_connect = -1; |
| 3820 | s->logs.t_data = -1; |
| 3821 | s->logs.t_close = 0; |
Patrick Hemmer | ffe5e8c | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 3822 | s->logs.prx_queue_pos = 0; /* we get the number of pending conns before us */ |
| 3823 | s->logs.srv_queue_pos = 0; /* we will get this number soon */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3824 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3825 | s->logs.bytes_in = s->req.total = ci_data(&s->req); |
| 3826 | s->logs.bytes_out = s->res.total = ci_data(&s->res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3827 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 3828 | if (objt_server(s->target)) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3829 | if (s->flags & SF_CURR_SESS) { |
| 3830 | s->flags &= ~SF_CURR_SESS; |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 3831 | HA_ATOMIC_SUB(&__objt_server(s->target)->cur_sess, 1); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3832 | } |
Willy Tarreau | 858b103 | 2015-12-07 17:04:59 +0100 | [diff] [blame] | 3833 | if (may_dequeue_tasks(objt_server(s->target), be)) |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 3834 | process_srv_queue(objt_server(s->target)); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3835 | } |
| 3836 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 3837 | s->target = NULL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3838 | |
Olivier Houchard | 7c6f8b1 | 2018-11-13 16:48:36 +0100 | [diff] [blame] | 3839 | |
| 3840 | /* If we're doing keepalive, first call the mux detach() method |
| 3841 | * to let it know we want to detach without freing the connection. |
| 3842 | * We then can call si_release_endpoint() to destroy the conn_stream |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 3843 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3844 | if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
Olivier Houchard | 985f139 | 2018-11-30 17:31:52 +0100 | [diff] [blame] | 3845 | !si_conn_ready(&s->si[1])) |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 3846 | srv_conn = NULL; |
Olivier Houchard | 985f139 | 2018-11-30 17:31:52 +0100 | [diff] [blame] | 3847 | else if (!srv_conn->owner) { |
| 3848 | srv_conn->owner = s->sess; |
| 3849 | session_add_conn(s->sess, srv_conn, s->target); |
| 3850 | } |
Olivier Houchard | 7c6f8b1 | 2018-11-13 16:48:36 +0100 | [diff] [blame] | 3851 | si_release_endpoint(&s->si[1]); |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 3852 | |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3853 | s->si[1].state = s->si[1].prev_state = SI_ST_INI; |
| 3854 | s->si[1].err_type = SI_ET_NONE; |
| 3855 | s->si[1].conn_retries = 0; /* used for logging too */ |
| 3856 | s->si[1].exp = TICK_ETERNITY; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3857 | s->si[1].flags &= SI_FL_ISBACK | SI_FL_DONT_WAKE; /* we're in the context of process_stream */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 3858 | s->req.flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT|CF_WAKE_CONNECT|CF_WROTE_DATA); |
Willy Tarreau | ede3d88 | 2018-10-24 17:17:56 +0200 | [diff] [blame] | 3859 | s->res.flags &= ~(CF_SHUTR|CF_SHUTR_NOW|CF_READ_ATTACHED|CF_READ_ERROR|CF_READ_NOEXP|CF_STREAMER|CF_STREAMER_FAST|CF_WRITE_PARTIAL|CF_NEVER_WAIT|CF_WROTE_DATA); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3860 | s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST); |
| 3861 | s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED); |
| 3862 | s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP); |
Willy Tarreau | 543db62 | 2012-11-15 16:41:22 +0100 | [diff] [blame] | 3863 | |
Patrick Hemmer | e3faf02 | 2018-08-22 10:02:00 -0400 | [diff] [blame] | 3864 | hlua_ctx_destroy(s->hlua); |
| 3865 | s->hlua = NULL; |
| 3866 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3867 | s->txn->meth = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3868 | http_reset_txn(s); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3869 | s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 3870 | |
| 3871 | if (prev_status == 401 || prev_status == 407) { |
| 3872 | /* In HTTP keep-alive mode, if we receive a 401, we still have |
| 3873 | * a chance of being able to send the visitor again to the same |
| 3874 | * server over the same connection. This is required by some |
| 3875 | * broken protocols such as NTLM, and anyway whenever there is |
| 3876 | * an opportunity for sending the challenge to the proper place, |
Lukas Tribus | 80512b1 | 2018-10-27 20:07:40 +0200 | [diff] [blame] | 3877 | * it's better to do it (at least it helps with debugging), at |
| 3878 | * least for non-deterministic load balancing algorithms. |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 3879 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3880 | s->txn->flags |= TX_PREFER_LAST; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 3881 | } |
| 3882 | |
Willy Tarreau | 53f9685 | 2016-02-02 18:50:47 +0100 | [diff] [blame] | 3883 | /* Never ever allow to reuse a connection from a non-reuse backend */ |
| 3884 | if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR) |
| 3885 | srv_conn->flags |= CO_FL_PRIVATE; |
| 3886 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3887 | if (fe->options2 & PR_O2_INDEPSTR) |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3888 | s->si[1].flags |= SI_FL_INDEP_STR; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3889 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3890 | if (fe->options2 & PR_O2_NODELAY) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 3891 | s->req.flags |= CF_NEVER_WAIT; |
| 3892 | s->res.flags |= CF_NEVER_WAIT; |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 3893 | } |
| 3894 | |
Willy Tarreau | 714ea78 | 2015-11-25 20:11:11 +0100 | [diff] [blame] | 3895 | /* we're removing the analysers, we MUST re-enable events detection. |
| 3896 | * We don't enable close on the response channel since it's either |
| 3897 | * already closed, or in keep-alive with an idle connection handler. |
| 3898 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 3899 | channel_auto_read(&s->req); |
| 3900 | channel_auto_close(&s->req); |
| 3901 | channel_auto_read(&s->res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3902 | |
Willy Tarreau | 1c59bd5 | 2015-11-02 20:20:11 +0100 | [diff] [blame] | 3903 | /* we're in keep-alive with an idle connection, monitor it if not already done */ |
| 3904 | if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) { |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 3905 | srv = objt_server(srv_conn->target); |
Olivier Houchard | 7c6f8b1 | 2018-11-13 16:48:36 +0100 | [diff] [blame] | 3906 | if (srv) { |
| 3907 | if (srv_conn->flags & CO_FL_PRIVATE) |
| 3908 | LIST_ADD(&srv->priv_conns[tid], &srv_conn->list); |
| 3909 | else if (prev_flags & TX_NOT_FIRST) |
| 3910 | /* note: we check the request, not the connection, but |
| 3911 | * this is valid for strategies SAFE and AGGR, and in |
| 3912 | * case of ALWS, we don't care anyway. |
| 3913 | */ |
| 3914 | LIST_ADD(&srv->safe_conns[tid], &srv_conn->list); |
| 3915 | else |
| 3916 | LIST_ADD(&srv->idle_conns[tid], &srv_conn->list); |
| 3917 | } |
Willy Tarreau | 4320eaa | 2015-08-05 11:08:30 +0200 | [diff] [blame] | 3918 | } |
Christopher Faulet | e600624 | 2017-03-10 11:52:44 +0100 | [diff] [blame] | 3919 | s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0; |
| 3920 | s->res.analysers = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3921 | } |
| 3922 | |
| 3923 | |
| 3924 | /* This function updates the request state machine according to the response |
| 3925 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 3926 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 3927 | * it is only used to find when a request/response couple is complete. Both |
| 3928 | * this function and its equivalent should loop until both return zero. It |
| 3929 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 3930 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3931 | int http_sync_req_state(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3932 | { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 3933 | struct channel *chn = &s->req; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3934 | struct http_txn *txn = s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 3935 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3936 | unsigned int old_state = txn->req.msg_state; |
| 3937 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 3938 | if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3939 | return 0; |
| 3940 | |
| 3941 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3942 | /* No need to read anymore, the request was completely parsed. |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 3943 | * We can shut the read side unless we want to abort_on_close, |
| 3944 | * or we have a POST request. The issue with POST requests is |
| 3945 | * that some browsers still send a CRLF after the request, and |
| 3946 | * this CRLF must be read so that it does not remain in the kernel |
| 3947 | * buffers, otherwise a close could cause an RST on some systems |
| 3948 | * (eg: Linux). |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 3949 | * Note that if we're using keep-alive on the client side, we'd |
| 3950 | * rather poll now and keep the polling enabled for the whole |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3951 | * stream's life than enabling/disabling it between each |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 3952 | * response and next request. |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3953 | */ |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 3954 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 3955 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
Willy Tarreau | 7aa15b0 | 2017-12-20 16:56:50 +0100 | [diff] [blame] | 3956 | (!(s->be->options & PR_O_ABRT_CLOSE) || |
| 3957 | (s->si[0].flags & SI_FL_CLEAN_ABRT)) && |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 3958 | txn->meth != HTTP_METH_POST) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 3959 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3960 | |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 3961 | /* if the server closes the connection, we want to immediately react |
| 3962 | * and close the socket to save packets and syscalls. |
| 3963 | */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3964 | s->si[1].flags |= SI_FL_NOHALF; |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 3965 | |
Willy Tarreau | 7f876a1 | 2015-11-18 11:59:55 +0100 | [diff] [blame] | 3966 | /* In any case we've finished parsing the request so we must |
| 3967 | * disable Nagle when sending data because 1) we're not going |
| 3968 | * to shut this side, and 2) the server is waiting for us to |
| 3969 | * send pending data. |
| 3970 | */ |
| 3971 | chn->flags |= CF_NEVER_WAIT; |
| 3972 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3973 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 3974 | goto wait_other_side; |
| 3975 | |
| 3976 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 3977 | /* The server has not finished to respond, so we |
| 3978 | * don't want to move in order not to upset it. |
| 3979 | */ |
| 3980 | goto wait_other_side; |
| 3981 | } |
| 3982 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3983 | /* When we get here, it means that both the request and the |
| 3984 | * response have finished receiving. Depending on the connection |
| 3985 | * mode, we'll have to wait for the last bytes to leave in either |
| 3986 | * direction, and sometimes for a close to be effective. |
| 3987 | */ |
| 3988 | |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3989 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 3990 | /* Server-close mode : queue a connection close to the server */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 3991 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) |
| 3992 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3993 | } |
| 3994 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 3995 | /* Option forceclose is set, or either side wants to close, |
| 3996 | * let's enforce it now that we're not expecting any new |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3997 | * data to come. The caller knows the stream is complete |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3998 | * once both states are CLOSED. |
Christopher Faulet | 1486b0a | 2017-07-18 11:42:08 +0200 | [diff] [blame] | 3999 | * |
| 4000 | * However, there is an exception if the response |
| 4001 | * length is undefined. In this case, we need to wait |
| 4002 | * the close from the server. The response will be |
| 4003 | * switched in TUNNEL mode until the end. |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4004 | */ |
Christopher Faulet | 1486b0a | 2017-07-18 11:42:08 +0200 | [diff] [blame] | 4005 | if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) && |
| 4006 | txn->rsp.msg_state != HTTP_MSG_CLOSED) |
| 4007 | goto check_channel_flags; |
| 4008 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4009 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 4010 | channel_shutr_now(chn); |
| 4011 | channel_shutw_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4012 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4013 | } |
| 4014 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4015 | /* The last possible modes are keep-alive and tunnel. Tunnel mode |
| 4016 | * will not have any analyser so it needs to poll for reads. |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4017 | */ |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4018 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) { |
| 4019 | channel_auto_read(chn); |
| 4020 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4021 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4022 | } |
| 4023 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 4024 | goto check_channel_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4025 | } |
| 4026 | |
| 4027 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 4028 | http_msg_closing: |
| 4029 | /* nothing else to forward, just waiting for the output buffer |
| 4030 | * to be empty and for the shutw_now to take effect. |
| 4031 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4032 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4033 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 4034 | goto http_msg_closed; |
| 4035 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4036 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4037 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4038 | txn->req.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4039 | } |
Christopher Faulet | 56d2609 | 2017-07-20 11:05:10 +0200 | [diff] [blame] | 4040 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4041 | } |
| 4042 | |
| 4043 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 4044 | http_msg_closed: |
Willy Tarreau | 8059351 | 2017-12-14 10:43:31 +0100 | [diff] [blame] | 4045 | /* if we don't know whether the server will close, we need to hard close */ |
| 4046 | if (txn->rsp.flags & HTTP_MSGF_XFER_LEN) |
| 4047 | s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
| 4048 | |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 4049 | /* see above in MSG_DONE why we only do this in these states */ |
| 4050 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 4051 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
Willy Tarreau | 7aa15b0 | 2017-12-20 16:56:50 +0100 | [diff] [blame] | 4052 | (!(s->be->options & PR_O_ABRT_CLOSE) || |
| 4053 | (s->si[0].flags & SI_FL_CLEAN_ABRT))) |
Willy Tarreau | 2e7a165 | 2013-12-15 15:32:10 +0100 | [diff] [blame] | 4054 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4055 | goto wait_other_side; |
| 4056 | } |
| 4057 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 4058 | check_channel_flags: |
| 4059 | /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */ |
| 4060 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
| 4061 | /* if we've just closed an output, let's switch */ |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 4062 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 4063 | goto http_msg_closing; |
| 4064 | } |
| 4065 | |
| 4066 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4067 | wait_other_side: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4068 | return txn->req.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4069 | } |
| 4070 | |
| 4071 | |
| 4072 | /* This function updates the response state machine according to the request |
| 4073 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 4074 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 4075 | * it is only used to find when a request/response couple is complete. Both |
| 4076 | * this function and its equivalent should loop until both return zero. It |
| 4077 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 4078 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4079 | int http_sync_res_state(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4080 | { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4081 | struct channel *chn = &s->res; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4082 | struct http_txn *txn = s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4083 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4084 | unsigned int old_state = txn->rsp.msg_state; |
| 4085 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 4086 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4087 | return 0; |
| 4088 | |
| 4089 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 4090 | /* In theory, we don't need to read anymore, but we must |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4091 | * still monitor the server connection for a possible close |
| 4092 | * while the request is being uploaded, so we don't disable |
| 4093 | * reading. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4094 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4095 | /* channel_dont_read(chn); */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4096 | |
| 4097 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 4098 | goto wait_other_side; |
| 4099 | |
| 4100 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 4101 | /* The client seems to still be sending data, probably |
| 4102 | * because we got an error response during an upload. |
| 4103 | * We have the choice of either breaking the connection |
| 4104 | * or letting it pass through. Let's do the later. |
| 4105 | */ |
| 4106 | goto wait_other_side; |
| 4107 | } |
| 4108 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4109 | /* When we get here, it means that both the request and the |
| 4110 | * response have finished receiving. Depending on the connection |
| 4111 | * mode, we'll have to wait for the last bytes to leave in either |
| 4112 | * direction, and sometimes for a close to be effective. |
| 4113 | */ |
| 4114 | |
| 4115 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 4116 | /* Server-close mode : shut read and wait for the request |
| 4117 | * side to close its output buffer. The caller will detect |
| 4118 | * when we're in DONE and the other is in CLOSED and will |
| 4119 | * catch that for the final cleanup. |
| 4120 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4121 | if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW))) |
| 4122 | channel_shutr_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4123 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4124 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4125 | /* Option forceclose is set, or either side wants to close, |
| 4126 | * let's enforce it now that we're not expecting any new |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4127 | * data to come. The caller knows the stream is complete |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4128 | * once both states are CLOSED. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4129 | */ |
Christopher Faulet | fd04fcf | 2018-02-02 15:54:15 +0100 | [diff] [blame] | 4130 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4131 | channel_shutr_now(chn); |
| 4132 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4133 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4134 | } |
| 4135 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4136 | /* The last possible modes are keep-alive and tunnel. Tunnel will |
| 4137 | * need to forward remaining data. Keep-alive will need to monitor |
| 4138 | * for connection closing. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4139 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4140 | channel_auto_read(chn); |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 4141 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4142 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
| 4143 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4144 | } |
| 4145 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 4146 | goto check_channel_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4147 | } |
| 4148 | |
| 4149 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 4150 | http_msg_closing: |
| 4151 | /* nothing else to forward, just waiting for the output buffer |
| 4152 | * to be empty and for the shutw_now to take effect. |
| 4153 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4154 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4155 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4156 | goto http_msg_closed; |
| 4157 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4158 | else if (chn->flags & CF_SHUTW) { |
Christopher Faulet | a3992e0 | 2017-07-18 10:35:55 +0200 | [diff] [blame] | 4159 | txn->rsp.err_state = txn->rsp.msg_state; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4160 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4161 | HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4162 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 4163 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4164 | } |
Christopher Faulet | 56d2609 | 2017-07-20 11:05:10 +0200 | [diff] [blame] | 4165 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4166 | } |
| 4167 | |
| 4168 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 4169 | http_msg_closed: |
| 4170 | /* drop any pending data */ |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 4171 | channel_truncate(chn); |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4172 | channel_auto_close(chn); |
| 4173 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4174 | goto wait_other_side; |
| 4175 | } |
| 4176 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 4177 | check_channel_flags: |
| 4178 | /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */ |
| 4179 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
| 4180 | /* if we've just closed an output, let's switch */ |
| 4181 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 4182 | goto http_msg_closing; |
| 4183 | } |
| 4184 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4185 | wait_other_side: |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 4186 | /* We force the response to leave immediately if we're waiting for the |
| 4187 | * other side, since there is no pending shutdown to push it out. |
| 4188 | */ |
| 4189 | if (!channel_is_empty(chn)) |
| 4190 | chn->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4191 | return txn->rsp.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4192 | } |
| 4193 | |
| 4194 | |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4195 | /* Resync the request and response state machines. */ |
| 4196 | void http_resync_states(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4197 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4198 | struct http_txn *txn = s->txn; |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4199 | #ifdef DEBUG_FULL |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4200 | int old_req_state = txn->req.msg_state; |
| 4201 | int old_res_state = txn->rsp.msg_state; |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4202 | #endif |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4203 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4204 | http_sync_req_state(s); |
| 4205 | while (1) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4206 | if (!http_sync_res_state(s)) |
| 4207 | break; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4208 | if (!http_sync_req_state(s)) |
| 4209 | break; |
| 4210 | } |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 4211 | |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4212 | DPRINTF(stderr,"[%u] %s: stream=%p old=%s,%s cur=%s,%s " |
| 4213 | "req->analysers=0x%08x res->analysers=0x%08x\n", |
| 4214 | now_ms, __FUNCTION__, s, |
Willy Tarreau | 0da5b3b | 2017-09-21 09:30:46 +0200 | [diff] [blame] | 4215 | h1_msg_state_str(old_req_state), h1_msg_state_str(old_res_state), |
| 4216 | h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state), |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4217 | s->req.analysers, s->res.analysers); |
Christopher Faulet | 814d270 | 2017-03-30 11:33:44 +0200 | [diff] [blame] | 4218 | |
| 4219 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4220 | /* OK, both state machines agree on a compatible state. |
| 4221 | * There are a few cases we're interested in : |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4222 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 4223 | * directions, so let's simply disable both analysers. |
Christopher Faulet | f77bb53 | 2017-07-18 11:18:46 +0200 | [diff] [blame] | 4224 | * - HTTP_MSG_CLOSED on the response only or HTTP_MSG_ERROR on either |
| 4225 | * means we must abort the request. |
| 4226 | * - HTTP_MSG_TUNNEL on either means we have to disable analyser on |
| 4227 | * corresponding channel. |
| 4228 | * - HTTP_MSG_DONE or HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE |
| 4229 | * on the response with server-close mode means we've completed one |
| 4230 | * request and we must re-initialize the server connection. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4231 | */ |
Christopher Faulet | f77bb53 | 2017-07-18 11:18:46 +0200 | [diff] [blame] | 4232 | if (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 4233 | txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4234 | s->req.analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4235 | channel_auto_close(&s->req); |
| 4236 | channel_auto_read(&s->req); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4237 | s->res.analysers &= AN_RES_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4238 | channel_auto_close(&s->res); |
| 4239 | channel_auto_read(&s->res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4240 | } |
Christopher Faulet | f77bb53 | 2017-07-18 11:18:46 +0200 | [diff] [blame] | 4241 | else if (txn->rsp.msg_state == HTTP_MSG_CLOSED || |
| 4242 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4243 | txn->req.msg_state == HTTP_MSG_ERROR) { |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4244 | s->res.analysers &= AN_RES_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4245 | channel_auto_close(&s->res); |
| 4246 | channel_auto_read(&s->res); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4247 | s->req.analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4248 | channel_abort(&s->req); |
| 4249 | channel_auto_close(&s->req); |
| 4250 | channel_auto_read(&s->req); |
| 4251 | channel_truncate(&s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4252 | } |
Christopher Faulet | f77bb53 | 2017-07-18 11:18:46 +0200 | [diff] [blame] | 4253 | else if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 4254 | txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 4255 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 4256 | s->req.analysers &= AN_REQ_FLT_END; |
| 4257 | if (HAS_REQ_DATA_FILTERS(s)) |
| 4258 | s->req.analysers |= AN_REQ_FLT_XFER_DATA; |
| 4259 | } |
| 4260 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 4261 | s->res.analysers &= AN_RES_FLT_END; |
| 4262 | if (HAS_RSP_DATA_FILTERS(s)) |
| 4263 | s->res.analysers |= AN_RES_FLT_XFER_DATA; |
| 4264 | } |
| 4265 | channel_auto_close(&s->req); |
| 4266 | channel_auto_read(&s->req); |
| 4267 | channel_auto_close(&s->res); |
| 4268 | channel_auto_read(&s->res); |
| 4269 | } |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4270 | else if ((txn->req.msg_state == HTTP_MSG_DONE || |
| 4271 | txn->req.msg_state == HTTP_MSG_CLOSED) && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4272 | txn->rsp.msg_state == HTTP_MSG_DONE && |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4273 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 4274 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 4275 | /* server-close/keep-alive: terminate this transaction, |
| 4276 | * possibly killing the server connection and reinitialize |
Willy Tarreau | 3de5bd6 | 2016-05-02 16:07:07 +0200 | [diff] [blame] | 4277 | * a fresh-new transaction, but only once we're sure there's |
| 4278 | * enough room in the request and response buffer to process |
Christopher Faulet | c0c672a | 2017-03-28 11:51:33 +0200 | [diff] [blame] | 4279 | * another request. They must not hold any pending output data |
| 4280 | * and the response buffer must realigned |
| 4281 | * (realign is done is http_end_txn_clean_session). |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4282 | */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4283 | if (co_data(&s->req)) |
Willy Tarreau | 3de5bd6 | 2016-05-02 16:07:07 +0200 | [diff] [blame] | 4284 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4285 | else if (co_data(&s->res)) |
Willy Tarreau | 3de5bd6 | 2016-05-02 16:07:07 +0200 | [diff] [blame] | 4286 | s->res.flags |= CF_WAKE_WRITE; |
Christopher Faulet | a81ff60 | 2017-07-18 22:01:05 +0200 | [diff] [blame] | 4287 | else { |
| 4288 | s->req.analysers = AN_REQ_FLT_END; |
| 4289 | s->res.analysers = AN_RES_FLT_END; |
| 4290 | txn->flags |= TX_WAIT_CLEANUP; |
| 4291 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4292 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4293 | } |
| 4294 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4295 | /* This function is an analyser which forwards request body (including chunk |
| 4296 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 4297 | * zero byte. The only situation where it must not be called is when we're in |
| 4298 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 4299 | * remaining data and to resync after end of body. It expects the msg_state to |
| 4300 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4301 | * read more data, or 1 once we can go on with next request or end the stream. |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4302 | * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 4303 | * bytes of pending data + the headers if not already done. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4304 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4305 | int http_request_forward_body(struct stream *s, struct channel *req, int an_bit) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4306 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4307 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4308 | struct http_txn *txn = s->txn; |
| 4309 | struct http_msg *msg = &s->txn->req; |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 4310 | int ret; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4311 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 4312 | if (IS_HTX_STRM(s)) |
| 4313 | return htx_request_forward_body(s, req, an_bit); |
| 4314 | |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 4315 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n", |
Christopher Faulet | 814d270 | 2017-03-30 11:33:44 +0200 | [diff] [blame] | 4316 | now_ms, __FUNCTION__, |
| 4317 | s, |
| 4318 | req, |
| 4319 | req->rex, req->wex, |
| 4320 | req->flags, |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 4321 | ci_data(req), |
Christopher Faulet | 814d270 | 2017-03-30 11:33:44 +0200 | [diff] [blame] | 4322 | req->analysers); |
| 4323 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4324 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4325 | return 0; |
| 4326 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4327 | if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) || |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4328 | ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4329 | /* Output closed while we were sending data. We must abort and |
| 4330 | * wake the other side up. |
| 4331 | */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4332 | msg->err_state = msg->msg_state; |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4333 | msg->msg_state = HTTP_MSG_ERROR; |
| 4334 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 4335 | return 1; |
| 4336 | } |
| 4337 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4338 | /* Note that we don't have to send 100-continue back because we don't |
| 4339 | * need the data to complete our job, and it's up to the server to |
| 4340 | * decide whether to return 100, 417 or anything else in return of |
| 4341 | * an "Expect: 100-continue" header. |
| 4342 | */ |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4343 | if (msg->msg_state == HTTP_MSG_BODY) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4344 | msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 4345 | ? HTTP_MSG_CHUNK_SIZE |
| 4346 | : HTTP_MSG_DATA); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4347 | |
| 4348 | /* TODO/filters: when http-buffer-request option is set or if a |
| 4349 | * rule on url_param exists, the first chunk size could be |
| 4350 | * already parsed. In that case, msg->next is after the chunk |
| 4351 | * size (including the CRLF after the size). So this case should |
| 4352 | * be handled to */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4353 | } |
| 4354 | |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 4355 | /* Some post-connect processing might want us to refrain from starting to |
| 4356 | * forward data. Currently, the only reason for this is "balance url_param" |
| 4357 | * whichs need to parse/process the request after we've enabled forwarding. |
| 4358 | */ |
| 4359 | if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4360 | if (!(s->res.flags & CF_READ_ATTACHED)) { |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 4361 | channel_auto_connect(req); |
Willy Tarreau | 644c101 | 2014-04-30 18:11:11 +0200 | [diff] [blame] | 4362 | req->flags |= CF_WAKE_CONNECT; |
Willy Tarreau | ba20dfc | 2018-05-16 11:35:05 +0200 | [diff] [blame] | 4363 | channel_dont_close(req); /* don't fail on early shutr */ |
| 4364 | goto waiting; |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 4365 | } |
| 4366 | msg->flags &= ~HTTP_MSGF_WAIT_CONN; |
| 4367 | } |
| 4368 | |
Willy Tarreau | 80a92c0 | 2014-03-12 10:41:13 +0100 | [diff] [blame] | 4369 | /* in most states, we should abort in case of early close */ |
| 4370 | channel_auto_close(req); |
| 4371 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 4372 | if (req->to_forward) { |
| 4373 | /* We can't process the buffer's contents yet */ |
| 4374 | req->flags |= CF_WAKE_WRITE; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4375 | goto missing_data_or_waiting; |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 4376 | } |
| 4377 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4378 | if (msg->msg_state < HTTP_MSG_DONE) { |
| 4379 | ret = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 4380 | ? http_msg_forward_chunked_body(s, msg) |
| 4381 | : http_msg_forward_body(s, msg)); |
| 4382 | if (!ret) |
| 4383 | goto missing_data_or_waiting; |
| 4384 | if (ret < 0) |
| 4385 | goto return_bad_req; |
| 4386 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4387 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4388 | /* other states, DONE...TUNNEL */ |
| 4389 | /* we don't want to forward closes on DONE except in tunnel mode. */ |
| 4390 | if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 4391 | channel_dont_close(req); |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 4392 | |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4393 | http_resync_states(s); |
| 4394 | if (!(req->analysers & an_bit)) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4395 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 4396 | if (req->flags & CF_SHUTW) { |
| 4397 | /* request errors are most likely due to the |
| 4398 | * server aborting the transfer. */ |
| 4399 | goto aborted_xfer; |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4400 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4401 | if (msg->err_pos >= 0) |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 4402 | http_capture_bad_message(sess->fe, s, msg, msg->err_state, s->be); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4403 | goto return_bad_req; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4404 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4405 | return 1; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4406 | } |
| 4407 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4408 | /* If "option abortonclose" is set on the backend, we want to monitor |
| 4409 | * the client's connection and forward any shutdown notification to the |
| 4410 | * server, which will decide whether to close or to go on processing the |
| 4411 | * request. We only do that in tunnel mode, and not in other modes since |
| 4412 | * it can be abused to exhaust source ports. */ |
Willy Tarreau | 7aa15b0 | 2017-12-20 16:56:50 +0100 | [diff] [blame] | 4413 | if ((s->be->options & PR_O_ABRT_CLOSE) && !(s->si[0].flags & SI_FL_CLEAN_ABRT)) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4414 | channel_auto_read(req); |
| 4415 | if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && |
| 4416 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)) |
| 4417 | s->si[1].flags |= SI_FL_NOLINGER; |
| 4418 | channel_auto_close(req); |
| 4419 | } |
| 4420 | else if (s->txn->meth == HTTP_METH_POST) { |
| 4421 | /* POST requests may require to read extra CRLF sent by broken |
| 4422 | * browsers and which could cause an RST to be sent upon close |
| 4423 | * on some systems (eg: Linux). */ |
| 4424 | channel_auto_read(req); |
| 4425 | } |
| 4426 | return 0; |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 4427 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4428 | missing_data_or_waiting: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4429 | /* stop waiting for data if the input is closed before the end */ |
Christopher Faulet | a33510b | 2017-03-31 15:37:29 +0200 | [diff] [blame] | 4430 | if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4431 | if (!(s->flags & SF_ERR_MASK)) |
| 4432 | s->flags |= SF_ERR_CLICL; |
| 4433 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4434 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4435 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4436 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4437 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4438 | } |
| 4439 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4440 | HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1); |
| 4441 | HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4442 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 4443 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4444 | |
| 4445 | goto return_bad_req_stats_ok; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4446 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4447 | |
Willy Tarreau | ba20dfc | 2018-05-16 11:35:05 +0200 | [diff] [blame] | 4448 | waiting: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4449 | /* waiting for the last bits to leave the buffer */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4450 | if (req->flags & CF_SHUTW) |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4451 | goto aborted_xfer; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4452 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4453 | /* When TE: chunked is used, we need to get there again to parse remaining |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4454 | * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE. |
Willy Tarreau | ff47b3f | 2017-12-29 16:30:31 +0100 | [diff] [blame] | 4455 | * And when content-length is used, we never want to let the possible |
| 4456 | * shutdown be forwarded to the other side, as the state machine will |
| 4457 | * take care of it once the client responds. It's also important to |
| 4458 | * prevent TIME_WAITs from accumulating on the backend side, and for |
| 4459 | * HTTP/2 where the last frame comes with a shutdown. |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4460 | */ |
Willy Tarreau | ff47b3f | 2017-12-29 16:30:31 +0100 | [diff] [blame] | 4461 | if (msg->flags & (HTTP_MSGF_TE_CHNK|HTTP_MSGF_CNT_LEN)) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4462 | channel_dont_close(req); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4463 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4464 | /* We know that more data are expected, but we couldn't send more that |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4465 | * what we did. So we always set the CF_EXPECT_MORE flag so that the |
Willy Tarreau | 0729303 | 2011-05-30 18:29:28 +0200 | [diff] [blame] | 4466 | * system knows it must not set a PUSH on this first part. Interactive |
Willy Tarreau | 869fc1e | 2012-03-05 08:29:20 +0100 | [diff] [blame] | 4467 | * modes are already handled by the stream sock layer. We must not do |
| 4468 | * this in content-length mode because it could present the MSG_MORE |
| 4469 | * flag with the last block of forwarded data, which would cause an |
| 4470 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4471 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4472 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4473 | req->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4474 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4475 | return 0; |
| 4476 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4477 | return_bad_req: /* let's centralize all bad requests */ |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4478 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4479 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 4480 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 4481 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4482 | return_bad_req_stats_ok: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4483 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4484 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4485 | if (txn->status) { |
| 4486 | /* Note: we don't send any error if some data were already sent */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 4487 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4488 | } else { |
| 4489 | txn->status = 400; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4490 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4491 | } |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4492 | req->analysers &= AN_REQ_FLT_END; |
| 4493 | s->res.analysers &= AN_RES_FLT_END; /* we're in data phase, we want to abort both directions */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4494 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4495 | if (!(s->flags & SF_ERR_MASK)) |
| 4496 | s->flags |= SF_ERR_PRXCOND; |
| 4497 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4498 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4499 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4500 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4501 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4502 | } |
| 4503 | return 0; |
| 4504 | |
| 4505 | aborted_xfer: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4506 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4507 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4508 | if (txn->status) { |
| 4509 | /* Note: we don't send any error if some data were already sent */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 4510 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4511 | } else { |
| 4512 | txn->status = 502; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4513 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4514 | } |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4515 | req->analysers &= AN_REQ_FLT_END; |
| 4516 | s->res.analysers &= AN_RES_FLT_END; /* we're in data phase, we want to abort both directions */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4517 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4518 | HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1); |
| 4519 | HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4520 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 4521 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4522 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4523 | if (!(s->flags & SF_ERR_MASK)) |
| 4524 | s->flags |= SF_ERR_SRVCL; |
| 4525 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4526 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4527 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4528 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4529 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4530 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4531 | return 0; |
| 4532 | } |
| 4533 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4534 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 4535 | * processing can continue on next analysers, or zero if it either needs more |
| 4536 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4537 | * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4538 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 4539 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4540 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4541 | int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit) |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4542 | { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4543 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4544 | struct http_txn *txn = s->txn; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4545 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4546 | struct hdr_ctx ctx; |
Lukas Tribus | fd9b68c | 2018-10-27 20:06:59 +0200 | [diff] [blame] | 4547 | struct connection *srv_conn; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4548 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4549 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4550 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4551 | |
Lukas Tribus | fd9b68c | 2018-10-27 20:06:59 +0200 | [diff] [blame] | 4552 | srv_conn = cs_conn(objt_cs(s->si[1].end)); |
| 4553 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 4554 | if (IS_HTX_STRM(s)) |
| 4555 | return htx_wait_for_response(s, rep, an_bit); |
| 4556 | |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 4557 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n", |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 4558 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4559 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 4560 | rep, |
| 4561 | rep->rex, rep->wex, |
| 4562 | rep->flags, |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4563 | ci_data(rep), |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 4564 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 4565 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4566 | /* |
| 4567 | * Now parse the partial (or complete) lines. |
| 4568 | * We will check the response syntax, and also join multi-line |
| 4569 | * headers. An index of all the lines will be elaborated while |
| 4570 | * parsing. |
| 4571 | * |
| 4572 | * For the parsing, we use a 28 states FSM. |
| 4573 | * |
| 4574 | * Here is the information we currently have : |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4575 | * ci_head(rep) = beginning of response |
| 4576 | * ci_head(rep) + msg->eoh = end of processed headers / start of current one |
| 4577 | * ci_tail(rep) = end of input data |
| 4578 | * msg->eol = end of current header or line (LF or CRLF) |
| 4579 | * msg->next = first non-visited byte |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4580 | */ |
| 4581 | |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 4582 | next_one: |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4583 | /* There's a protected area at the end of the buffer for rewriting |
| 4584 | * purposes. We don't want to start to parse the request if the |
| 4585 | * protected area is affected, because we may have to move processed |
| 4586 | * data later, which is much more complicated. |
| 4587 | */ |
Willy Tarreau | d760eec | 2018-07-10 09:50:25 +0200 | [diff] [blame] | 4588 | if (c_data(rep) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | ba0902e | 2015-01-13 14:39:16 +0100 | [diff] [blame] | 4589 | if (unlikely(!channel_is_rewritable(rep))) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 4590 | /* some data has still not left the buffer, wake us once that's done */ |
| 4591 | if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) |
| 4592 | goto abort_response; |
| 4593 | channel_dont_close(rep); |
| 4594 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 4595 | rep->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 4596 | return 0; |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4597 | } |
| 4598 | |
Willy Tarreau | 188e230 | 2018-06-15 11:11:53 +0200 | [diff] [blame] | 4599 | if (unlikely(ci_tail(rep) < c_ptr(rep, msg->next) || |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 4600 | ci_tail(rep) > b_wrap(&rep->buf) - global.tune.maxrewrite)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4601 | channel_slow_realign(rep, trash.area); |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 4602 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4603 | if (likely(msg->next < ci_data(rep))) |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 4604 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4605 | } |
| 4606 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4607 | /* 1: we might have to print this header in debug mode */ |
| 4608 | if (unlikely((global.mode & MODE_DEBUG) && |
| 4609 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 7d59e90 | 2014-10-21 19:36:09 +0200 | [diff] [blame] | 4610 | msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4611 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4612 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4613 | sol = ci_head(rep); |
| 4614 | eol = sol + (msg->sl.st.l ? msg->sl.st.l : ci_data(rep)); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4615 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4616 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4617 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 4618 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4619 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4620 | while (cur_idx) { |
| 4621 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 4622 | debug_hdr("srvhdr", s, sol, eol); |
| 4623 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 4624 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 4625 | } |
| 4626 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4627 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4628 | /* |
| 4629 | * Now we quickly check if we have found a full valid response. |
| 4630 | * If not so, we check the FD and buffer states before leaving. |
| 4631 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4632 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4633 | * responses are checked first. |
| 4634 | * |
| 4635 | * Depending on whether the client is still there or not, we |
| 4636 | * may send an error response back or not. Note that normally |
| 4637 | * we should only check for HTTP status there, and check I/O |
| 4638 | * errors somewhere else. |
| 4639 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4640 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4641 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4642 | /* Invalid response */ |
| 4643 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 4644 | /* we detected a parsing error. We want to archive this response |
| 4645 | * in the dedicated proxy area for later troubleshooting. |
| 4646 | */ |
| 4647 | hdr_response_bad: |
| 4648 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 4649 | http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4650 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4651 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4652 | if (objt_server(s->target)) { |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 4653 | HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1); |
| 4654 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4655 | } |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 4656 | abort_response: |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4657 | channel_auto_close(rep); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4658 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4659 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4660 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 4661 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4662 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4663 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4664 | if (!(s->flags & SF_ERR_MASK)) |
| 4665 | s->flags |= SF_ERR_PRXCOND; |
| 4666 | if (!(s->flags & SF_FINST_MASK)) |
| 4667 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4668 | |
| 4669 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4670 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4671 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4672 | /* too large response does not fit in buffer. */ |
Willy Tarreau | 2375233 | 2018-06-15 14:54:53 +0200 | [diff] [blame] | 4673 | else if (channel_full(rep, global.tune.maxrewrite)) { |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 4674 | if (msg->err_pos < 0) |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4675 | msg->err_pos = ci_data(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4676 | goto hdr_response_bad; |
| 4677 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4678 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4679 | /* read error */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4680 | else if (rep->flags & CF_READ_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4681 | if (msg->err_pos >= 0) |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 4682 | http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 4683 | else if (txn->flags & TX_NOT_FIRST) |
| 4684 | goto abort_keep_alive; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4685 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4686 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4687 | if (objt_server(s->target)) { |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 4688 | HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1); |
| 4689 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4690 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 4691 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4692 | channel_auto_close(rep); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4693 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4694 | txn->status = 502; |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 4695 | |
| 4696 | /* Check to see if the server refused the early data. |
| 4697 | * If so, just send a 425 |
| 4698 | */ |
| 4699 | if (objt_cs(s->si[1].end)) { |
| 4700 | struct connection *conn = objt_cs(s->si[1].end)->conn; |
| 4701 | |
| 4702 | if (conn->err_code == CO_ER_SSL_EARLY_FAILED) |
| 4703 | txn->status = 425; |
| 4704 | } |
| 4705 | |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4706 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 4707 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4708 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 4709 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4710 | if (!(s->flags & SF_ERR_MASK)) |
| 4711 | s->flags |= SF_ERR_SRVCL; |
| 4712 | if (!(s->flags & SF_FINST_MASK)) |
| 4713 | s->flags |= SF_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 4714 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4715 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4716 | |
Willy Tarreau | 6f0a7ba | 2014-06-23 15:22:31 +0200 | [diff] [blame] | 4717 | /* read timeout : return a 504 to the client. */ |
| 4718 | else if (rep->flags & CF_READ_TIMEOUT) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4719 | if (msg->err_pos >= 0) |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 4720 | http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4721 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4722 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4723 | if (objt_server(s->target)) { |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 4724 | HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1); |
| 4725 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4726 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4727 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4728 | channel_auto_close(rep); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4729 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4730 | txn->status = 504; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4731 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 4732 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4733 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4734 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4735 | if (!(s->flags & SF_ERR_MASK)) |
| 4736 | s->flags |= SF_ERR_SRVTO; |
| 4737 | if (!(s->flags & SF_FINST_MASK)) |
| 4738 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4739 | return 0; |
| 4740 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 4741 | |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 4742 | /* client abort with an abortonclose */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4743 | else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) { |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4744 | HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1); |
| 4745 | HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1); |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 4746 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 4747 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1); |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 4748 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4749 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 4750 | channel_auto_close(rep); |
| 4751 | |
| 4752 | txn->status = 400; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 4753 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4754 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 4755 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4756 | if (!(s->flags & SF_ERR_MASK)) |
| 4757 | s->flags |= SF_ERR_CLICL; |
| 4758 | if (!(s->flags & SF_FINST_MASK)) |
| 4759 | s->flags |= SF_FINST_H; |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 4760 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4761 | /* process_stream() will take care of the error */ |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 4762 | return 0; |
| 4763 | } |
| 4764 | |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 4765 | /* close from server, capture the response if the server has started to respond */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4766 | else if (rep->flags & CF_SHUTR) { |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 4767 | if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0) |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 4768 | http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 4769 | else if (txn->flags & TX_NOT_FIRST) |
| 4770 | goto abort_keep_alive; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4771 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4772 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4773 | if (objt_server(s->target)) { |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 4774 | HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1); |
| 4775 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4776 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4777 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4778 | channel_auto_close(rep); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4779 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4780 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4781 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 4782 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4783 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4784 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4785 | if (!(s->flags & SF_ERR_MASK)) |
| 4786 | s->flags |= SF_ERR_SRVCL; |
| 4787 | if (!(s->flags & SF_FINST_MASK)) |
| 4788 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4789 | return 0; |
| 4790 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4791 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4792 | /* write error to client (we don't send any message then) */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4793 | else if (rep->flags & CF_WRITE_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4794 | if (msg->err_pos >= 0) |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 4795 | http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 4796 | else if (txn->flags & TX_NOT_FIRST) |
| 4797 | goto abort_keep_alive; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4798 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4799 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4800 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4801 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4802 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4803 | if (!(s->flags & SF_ERR_MASK)) |
| 4804 | s->flags |= SF_ERR_CLICL; |
| 4805 | if (!(s->flags & SF_FINST_MASK)) |
| 4806 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4807 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4808 | /* process_stream() will take care of the error */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4809 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4810 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4811 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4812 | channel_dont_close(rep); |
Willy Tarreau | 3f3997e | 2013-12-15 15:21:32 +0100 | [diff] [blame] | 4813 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4814 | return 0; |
| 4815 | } |
| 4816 | |
| 4817 | /* More interesting part now : we know that we have a complete |
| 4818 | * response which at least looks like HTTP. We have an indicator |
| 4819 | * of each header's length, so we can parse them quickly. |
| 4820 | */ |
| 4821 | |
| 4822 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 4823 | http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4824 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4825 | /* |
| 4826 | * 1: get the status code |
| 4827 | */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4828 | n = ci_head(rep)[msg->sl.st.c] - '0'; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4829 | if (n < 1 || n > 5) |
| 4830 | n = 0; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4831 | /* when the client triggers a 4xx from the server, it's most often due |
| 4832 | * to a missing object or permission. These events should be tracked |
| 4833 | * because if they happen often, it may indicate a brute force or a |
| 4834 | * vulnerability scan. |
| 4835 | */ |
| 4836 | if (n == 4) |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4837 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4838 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4839 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 4840 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.p.http.rsp[n], 1); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4841 | |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 4842 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 4843 | * exactly one digit "." one digit. This check may be disabled using |
| 4844 | * option accept-invalid-http-response. |
| 4845 | */ |
| 4846 | if (!(s->be->options2 & PR_O2_RSPBUG_OK)) { |
| 4847 | if (msg->sl.st.v_l != 8) { |
| 4848 | msg->err_pos = 0; |
| 4849 | goto hdr_response_bad; |
| 4850 | } |
| 4851 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4852 | if (ci_head(rep)[4] != '/' || |
| 4853 | !isdigit((unsigned char)ci_head(rep)[5]) || |
| 4854 | ci_head(rep)[6] != '.' || |
| 4855 | !isdigit((unsigned char)ci_head(rep)[7])) { |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 4856 | msg->err_pos = 4; |
| 4857 | goto hdr_response_bad; |
| 4858 | } |
| 4859 | } |
| 4860 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4861 | /* check if the response is HTTP/1.1 or above */ |
| 4862 | if ((msg->sl.st.v_l == 8) && |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4863 | ((ci_head(rep)[5] > '1') || |
| 4864 | ((ci_head(rep)[5] == '1') && (ci_head(rep)[7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4865 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4866 | |
| 4867 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 4868 | txn->flags &= ~(TX_HDR_CONN_PRS|TX_HDR_CONN_CLO|TX_HDR_CONN_KAL|TX_HDR_CONN_UPG|TX_CON_CLO_SET|TX_CON_KAL_SET); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4869 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4870 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4871 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4872 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4873 | txn->status = strl2ui(ci_head(rep) + msg->sl.st.c, msg->sl.st.c_l); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4874 | |
Willy Tarreau | 3965040 | 2010-03-15 19:44:39 +0100 | [diff] [blame] | 4875 | /* Adjust server's health based on status code. Note: status codes 501 |
| 4876 | * and 505 are triggered on demand by client request, so we must not |
| 4877 | * count them as server failures. |
| 4878 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4879 | if (objt_server(s->target)) { |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 4880 | if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505)) |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 4881 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 4882 | else |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 4883 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 4884 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4885 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4886 | /* |
Willy Tarreau | a14ad72 | 2017-07-07 11:36:32 +0200 | [diff] [blame] | 4887 | * We may be facing a 100-continue response, or any other informational |
| 4888 | * 1xx response which is non-final, in which case this is not the right |
| 4889 | * response, and we're waiting for the next one. Let's allow this response |
| 4890 | * to go to the client and wait for the next one. There's an exception for |
| 4891 | * 101 which is used later in the code to switch protocols. |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4892 | */ |
Willy Tarreau | a14ad72 | 2017-07-07 11:36:32 +0200 | [diff] [blame] | 4893 | if (txn->status < 200 && |
| 4894 | (txn->status == 100 || txn->status >= 102)) { |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 4895 | hdr_idx_init(&txn->hdr_idx); |
| 4896 | msg->next -= channel_forward(rep, msg->next); |
| 4897 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 4898 | txn->status = 0; |
| 4899 | s->logs.t_data = -1; /* was not a response yet */ |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 4900 | FLT_STRM_CB(s, flt_http_reset(s, msg)); |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 4901 | goto next_one; |
Willy Tarreau | a14ad72 | 2017-07-07 11:36:32 +0200 | [diff] [blame] | 4902 | } |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 4903 | |
Willy Tarreau | a14ad72 | 2017-07-07 11:36:32 +0200 | [diff] [blame] | 4904 | /* |
| 4905 | * 2: check for cacheability. |
| 4906 | */ |
| 4907 | |
| 4908 | switch (txn->status) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4909 | case 200: |
| 4910 | case 203: |
Willy Tarreau | c55ddce | 2017-12-21 11:41:38 +0100 | [diff] [blame] | 4911 | case 204: |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4912 | case 206: |
| 4913 | case 300: |
| 4914 | case 301: |
Willy Tarreau | c55ddce | 2017-12-21 11:41:38 +0100 | [diff] [blame] | 4915 | case 404: |
| 4916 | case 405: |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4917 | case 410: |
Willy Tarreau | c55ddce | 2017-12-21 11:41:38 +0100 | [diff] [blame] | 4918 | case 414: |
| 4919 | case 501: |
Willy Tarreau | 83ece46 | 2017-12-21 15:13:09 +0100 | [diff] [blame] | 4920 | break; |
| 4921 | default: |
Willy Tarreau | c55ddce | 2017-12-21 11:41:38 +0100 | [diff] [blame] | 4922 | /* RFC7231#6.1: |
| 4923 | * Responses with status codes that are defined as |
| 4924 | * cacheable by default (e.g., 200, 203, 204, 206, |
| 4925 | * 300, 301, 404, 405, 410, 414, and 501 in this |
| 4926 | * specification) can be reused by a cache with |
| 4927 | * heuristic expiration unless otherwise indicated |
| 4928 | * by the method definition or explicit cache |
| 4929 | * controls [RFC7234]; all other status codes are |
| 4930 | * not cacheable by default. |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4931 | */ |
Willy Tarreau | 83ece46 | 2017-12-21 15:13:09 +0100 | [diff] [blame] | 4932 | txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4933 | break; |
| 4934 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4935 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4936 | /* |
| 4937 | * 3: we may need to capture headers |
| 4938 | */ |
| 4939 | s->logs.logwait &= ~LW_RESP; |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 4940 | if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap)) |
Christopher Faulet | 10079f5 | 2018-10-03 15:17:28 +0200 | [diff] [blame] | 4941 | http_capture_headers(ci_head(rep), &txn->hdr_idx, |
| 4942 | s->res_cap, sess->fe->rsp_cap); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4943 | |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 4944 | /* 4: determine the transfer-length according to RFC2616 #4.4, updated |
| 4945 | * by RFC7230#3.3.3 : |
| 4946 | * |
| 4947 | * The length of a message body is determined by one of the following |
| 4948 | * (in order of precedence): |
| 4949 | * |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 4950 | * 1. Any 2xx (Successful) response to a CONNECT request implies that |
| 4951 | * the connection will become a tunnel immediately after the empty |
| 4952 | * line that concludes the header fields. A client MUST ignore |
| 4953 | * any Content-Length or Transfer-Encoding header fields received |
| 4954 | * in such a message. Any 101 response (Switching Protocols) is |
| 4955 | * managed in the same manner. |
| 4956 | * |
| 4957 | * 2. Any response to a HEAD request and any response with a 1xx |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 4958 | * (Informational), 204 (No Content), or 304 (Not Modified) status |
| 4959 | * code is always terminated by the first empty line after the |
| 4960 | * header fields, regardless of the header fields present in the |
| 4961 | * message, and thus cannot contain a message body. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4962 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 4963 | * 3. If a Transfer-Encoding header field is present and the chunked |
| 4964 | * transfer coding (Section 4.1) is the final encoding, the message |
| 4965 | * body length is determined by reading and decoding the chunked |
| 4966 | * data until the transfer coding indicates the data is complete. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4967 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 4968 | * If a Transfer-Encoding header field is present in a response and |
| 4969 | * the chunked transfer coding is not the final encoding, the |
| 4970 | * message body length is determined by reading the connection until |
| 4971 | * it is closed by the server. If a Transfer-Encoding header field |
| 4972 | * is present in a request and the chunked transfer coding is not |
| 4973 | * the final encoding, the message body length cannot be determined |
| 4974 | * reliably; the server MUST respond with the 400 (Bad Request) |
| 4975 | * status code and then close the connection. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4976 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 4977 | * If a message is received with both a Transfer-Encoding and a |
| 4978 | * Content-Length header field, the Transfer-Encoding overrides the |
| 4979 | * Content-Length. Such a message might indicate an attempt to |
| 4980 | * perform request smuggling (Section 9.5) or response splitting |
| 4981 | * (Section 9.4) and ought to be handled as an error. A sender MUST |
| 4982 | * remove the received Content-Length field prior to forwarding such |
| 4983 | * a message downstream. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4984 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 4985 | * 4. If a message is received without Transfer-Encoding and with |
| 4986 | * either multiple Content-Length header fields having differing |
| 4987 | * field-values or a single Content-Length header field having an |
| 4988 | * invalid value, then the message framing is invalid and the |
| 4989 | * recipient MUST treat it as an unrecoverable error. If this is a |
| 4990 | * request message, the server MUST respond with a 400 (Bad Request) |
| 4991 | * status code and then close the connection. If this is a response |
| 4992 | * message received by a proxy, the proxy MUST close the connection |
| 4993 | * to the server, discard the received response, and send a 502 (Bad |
| 4994 | * Gateway) response to the client. If this is a response message |
| 4995 | * received by a user agent, the user agent MUST close the |
| 4996 | * connection to the server and discard the received response. |
| 4997 | * |
| 4998 | * 5. If a valid Content-Length header field is present without |
| 4999 | * Transfer-Encoding, its decimal value defines the expected message |
| 5000 | * body length in octets. If the sender closes the connection or |
| 5001 | * the recipient times out before the indicated number of octets are |
| 5002 | * received, the recipient MUST consider the message to be |
| 5003 | * incomplete and close the connection. |
| 5004 | * |
| 5005 | * 6. If this is a request message and none of the above are true, then |
| 5006 | * the message body length is zero (no message body is present). |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5007 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 5008 | * 7. Otherwise, this is a response message without a declared message |
| 5009 | * body length, so the message body length is determined by the |
| 5010 | * number of octets received prior to the server closing the |
| 5011 | * connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5012 | */ |
| 5013 | |
| 5014 | /* Skip parsing if no content length is possible. The response flags |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5015 | * 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] | 5016 | * 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] | 5017 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 5018 | */ |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 5019 | if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || |
| 5020 | txn->status == 101)) { |
| 5021 | /* Either we've established an explicit tunnel, or we're |
| 5022 | * switching the protocol. In both cases, we're very unlikely |
| 5023 | * to understand the next protocols. We have to switch to tunnel |
| 5024 | * mode, so that we transfer the request and responses then let |
| 5025 | * this protocol pass unmodified. When we later implement specific |
| 5026 | * parsers for such protocols, we'll want to check the Upgrade |
| 5027 | * header which contains information about that protocol for |
| 5028 | * responses with status 101 (eg: see RFC2817 about TLS). |
| 5029 | */ |
| 5030 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN; |
| 5031 | msg->flags |= HTTP_MSGF_XFER_LEN; |
| 5032 | goto end; |
| 5033 | } |
| 5034 | |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5035 | if (txn->meth == HTTP_METH_HEAD || |
| 5036 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5037 | txn->status == 204 || txn->status == 304) { |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5038 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5039 | goto skip_content_length; |
| 5040 | } |
| 5041 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5042 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5043 | ctx.idx = 0; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5044 | while (http_find_header2("Transfer-Encoding", 17, ci_head(rep), &txn->hdr_idx, &ctx)) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5045 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5046 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 5047 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5048 | /* bad transfer-encoding (chunked followed by something else) */ |
| 5049 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5050 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5051 | break; |
| 5052 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5053 | } |
| 5054 | |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 5055 | /* Chunked responses must have their content-length removed */ |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5056 | ctx.idx = 0; |
Willy Tarreau | b4d0c03 | 2015-05-01 10:25:45 +0200 | [diff] [blame] | 5057 | if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5058 | while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx)) |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 5059 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 5060 | } |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5061 | else while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx)) { |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5062 | signed long long cl; |
| 5063 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5064 | if (!ctx.vlen) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5065 | msg->err_pos = ctx.line + ctx.val - ci_head(rep); |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5066 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5067 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5068 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5069 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5070 | msg->err_pos = ctx.line + ctx.val - ci_head(rep); |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5071 | goto hdr_response_bad; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5072 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5073 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5074 | if (cl < 0) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5075 | msg->err_pos = ctx.line + ctx.val - ci_head(rep); |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5076 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5077 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5078 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5079 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5080 | msg->err_pos = ctx.line + ctx.val - ci_head(rep); |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5081 | goto hdr_response_bad; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5082 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5083 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5084 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5085 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5086 | } |
| 5087 | |
Lukas Tribus | fd9b68c | 2018-10-27 20:06:59 +0200 | [diff] [blame] | 5088 | /* check for NTML authentication headers in 401 (WWW-Authenticate) and |
| 5089 | * 407 (Proxy-Authenticate) responses and set the connection to private |
| 5090 | */ |
| 5091 | if (srv_conn && txn->status == 401) { |
| 5092 | /* check for Negotiate/NTLM WWW-Authenticate headers */ |
| 5093 | ctx.idx = 0; |
| 5094 | while (http_find_header2("WWW-Authenticate", 16, ci_head(rep), &txn->hdr_idx, &ctx)) { |
| 5095 | if ((ctx.vlen >= 9 && word_match(ctx.line + ctx.val, ctx.vlen, "Negotiate", 9)) || |
| 5096 | (ctx.vlen >= 4 && word_match(ctx.line + ctx.val, ctx.vlen, "NTLM", 4))) |
| 5097 | srv_conn->flags |= CO_FL_PRIVATE; |
| 5098 | } |
| 5099 | } else if (srv_conn && txn->status == 407) { |
| 5100 | /* check for Negotiate/NTLM Proxy-Authenticate headers */ |
| 5101 | ctx.idx = 0; |
| 5102 | while (http_find_header2("Proxy-Authenticate", 18, ci_head(rep), &txn->hdr_idx, &ctx)) { |
| 5103 | if ((ctx.vlen >= 9 && word_match(ctx.line + ctx.val, ctx.vlen, "Negotiate", 9)) || |
| 5104 | (ctx.vlen >= 4 && word_match(ctx.line + ctx.val, ctx.vlen, "NTLM", 4))) |
| 5105 | srv_conn->flags |= CO_FL_PRIVATE; |
| 5106 | } |
| 5107 | } |
| 5108 | |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 5109 | skip_content_length: |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5110 | /* Now we have to check if we need to modify the Connection header. |
| 5111 | * This is more difficult on the response than it is on the request, |
| 5112 | * because we can have two different HTTP versions and we don't know |
| 5113 | * how the client will interprete a response. For instance, let's say |
| 5114 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 5115 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 5116 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 5117 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 5118 | * the lack of header as a keep-alive. Thus we will use two flags |
| 5119 | * indicating how a request MAY be understood by the client. In case |
| 5120 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 5121 | * ambiguous cases such as both close and keepalive are seen, then we |
| 5122 | * will fall back to explicit close. Note that we won't take risks with |
| 5123 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5124 | * See doc/internals/connection-header.txt for the complete matrix. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5125 | */ |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 5126 | if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) && |
Christopher Faulet | 315b39c | 2018-09-21 16:26:19 +0200 | [diff] [blame] | 5127 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5128 | int to_del = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5129 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5130 | /* on unknown transfer length, we must close */ |
Christopher Faulet | 315b39c | 2018-09-21 16:26:19 +0200 | [diff] [blame] | 5131 | if (!(msg->flags & HTTP_MSGF_XFER_LEN)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5132 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5133 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5134 | /* now adjust header transformations depending on current state */ |
Christopher Faulet | 315b39c | 2018-09-21 16:26:19 +0200 | [diff] [blame] | 5135 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5136 | to_del |= 2; /* remove "keep-alive" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5137 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5138 | to_del |= 1; /* remove "close" for HTTP/1.0 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5139 | } |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5140 | else { /* SCL / KAL */ |
| 5141 | to_del |= 1; /* remove "close" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5142 | if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5143 | to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5144 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5145 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5146 | /* Parse and remove some headers from the connection header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 5147 | http_parse_connection_header(txn, msg, to_del); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5148 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5149 | /* Some keep-alive responses are converted to Server-close if |
| 5150 | * the server wants to close. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5151 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5152 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) { |
| 5153 | if ((txn->flags & TX_HDR_CONN_CLO) || |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5154 | (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11))) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5155 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5156 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5157 | } |
| 5158 | |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 5159 | end: |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 5160 | /* we want to have the response time before we start processing it */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 5161 | s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 5162 | |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5163 | /* end of job, return OK */ |
| 5164 | rep->analysers &= ~an_bit; |
| 5165 | rep->analyse_exp = TICK_ETERNITY; |
| 5166 | channel_auto_close(rep); |
| 5167 | return 1; |
| 5168 | |
| 5169 | abort_keep_alive: |
| 5170 | /* A keep-alive request to the server failed on a network error. |
| 5171 | * The client is required to retry. We need to close without returning |
| 5172 | * any other information so that the client retries. |
| 5173 | */ |
| 5174 | txn->status = 0; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5175 | rep->analysers &= AN_RES_FLT_END; |
| 5176 | s->req.analysers &= AN_REQ_FLT_END; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5177 | channel_auto_close(rep); |
| 5178 | s->logs.logwait = 0; |
| 5179 | s->logs.level = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5180 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5181 | channel_truncate(rep); |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 5182 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5183 | return 0; |
| 5184 | } |
| 5185 | |
| 5186 | /* This function performs all the processing enabled for the current response. |
| 5187 | * It normally returns 1 unless it wants to break. It relies on buffers flags, |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5188 | * and updates s->res.analysers. It might make sense to explode it into several |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5189 | * other functions. It works like process_request (see indications above). |
| 5190 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5191 | int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px) |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5192 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5193 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5194 | struct http_txn *txn = s->txn; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5195 | struct http_msg *msg = &txn->rsp; |
| 5196 | struct proxy *cur_proxy; |
| 5197 | struct cond_wordlist *wl; |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 5198 | enum rule_result ret = HTTP_RULE_RES_CONT; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5199 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 5200 | if (IS_HTX_STRM(s)) |
| 5201 | return htx_process_res_common(s, rep, an_bit, px); |
| 5202 | |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 5203 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n", |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5204 | now_ms, __FUNCTION__, |
| 5205 | s, |
| 5206 | rep, |
| 5207 | rep->rex, rep->wex, |
| 5208 | rep->flags, |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5209 | ci_data(rep), |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5210 | rep->analysers); |
| 5211 | |
| 5212 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
| 5213 | return 0; |
| 5214 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 5215 | /* The stats applet needs to adjust the Connection header but we don't |
| 5216 | * apply any filter there. |
| 5217 | */ |
Willy Tarreau | 612adb8 | 2015-03-10 15:25:54 +0100 | [diff] [blame] | 5218 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
| 5219 | rep->analysers &= ~an_bit; |
| 5220 | rep->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 5221 | goto skip_filters; |
Willy Tarreau | 612adb8 | 2015-03-10 15:25:54 +0100 | [diff] [blame] | 5222 | } |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 5223 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5224 | /* |
| 5225 | * We will have to evaluate the filters. |
| 5226 | * As opposed to version 1.2, now they will be evaluated in the |
| 5227 | * filters order and not in the header order. This means that |
| 5228 | * each filter has to be validated among all headers. |
| 5229 | * |
| 5230 | * Filters are tried with ->be first, then with ->fe if it is |
| 5231 | * different from ->be. |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 5232 | * |
| 5233 | * Maybe we are in resume condiion. In this case I choose the |
| 5234 | * "struct proxy" which contains the rule list matching the resume |
| 5235 | * pointer. If none of theses "struct proxy" match, I initialise |
| 5236 | * the process with the first one. |
| 5237 | * |
| 5238 | * In fact, I check only correspondance betwwen the current list |
| 5239 | * pointer and the ->fe rule list. If it doesn't match, I initialize |
| 5240 | * the loop with the ->be. |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5241 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5242 | if (s->current_rule_list == &sess->fe->http_res_rules) |
| 5243 | cur_proxy = sess->fe; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 5244 | else |
| 5245 | cur_proxy = s->be; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5246 | while (1) { |
| 5247 | struct proxy *rule_set = cur_proxy; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5248 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5249 | /* evaluate http-response rules */ |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 5250 | if (ret == HTTP_RULE_RES_CONT) { |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 5251 | ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 5252 | |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 5253 | if (ret == HTTP_RULE_RES_BADREQ) |
| 5254 | goto return_srv_prx_502; |
| 5255 | |
| 5256 | if (ret == HTTP_RULE_RES_DONE) { |
| 5257 | rep->analysers &= ~an_bit; |
| 5258 | rep->analyse_exp = TICK_ETERNITY; |
| 5259 | return 1; |
| 5260 | } |
| 5261 | } |
| 5262 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 5263 | /* we need to be called again. */ |
| 5264 | if (ret == HTTP_RULE_RES_YIELD) { |
| 5265 | channel_dont_close(rep); |
| 5266 | return 0; |
| 5267 | } |
| 5268 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5269 | /* try headers filters */ |
| 5270 | if (rule_set->rsp_exp != NULL) { |
| 5271 | if (apply_filters_to_response(s, rep, rule_set) < 0) { |
| 5272 | return_bad_resp: |
| 5273 | if (objt_server(s->target)) { |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 5274 | HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1); |
| 5275 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5276 | } |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5277 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5278 | return_srv_prx_502: |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5279 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5280 | txn->status = 502; |
| 5281 | s->logs.t_data = -1; /* was not a valid response */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5282 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5283 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 5284 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5285 | if (!(s->flags & SF_ERR_MASK)) |
| 5286 | s->flags |= SF_ERR_PRXCOND; |
| 5287 | if (!(s->flags & SF_FINST_MASK)) |
| 5288 | s->flags |= SF_FINST_H; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5289 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5290 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5291 | } |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5292 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5293 | /* has the response been denied ? */ |
| 5294 | if (txn->flags & TX_SVDENY) { |
| 5295 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5296 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5297 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5298 | HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1); |
| 5299 | HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5300 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 5301 | HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5302 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5303 | goto return_srv_prx_502; |
| 5304 | } |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 5305 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5306 | /* add response headers from the rule sets in the same order */ |
| 5307 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 5308 | if (txn->status < 200 && txn->status != 101) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5309 | break; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5310 | if (wl->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 5311 | int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL); |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5312 | ret = acl_pass(ret); |
| 5313 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 5314 | ret = !ret; |
| 5315 | if (!ret) |
| 5316 | continue; |
| 5317 | } |
Christopher Faulet | 10079f5 | 2018-10-03 15:17:28 +0200 | [diff] [blame] | 5318 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, wl->s, strlen(wl->s)) < 0)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5319 | goto return_bad_resp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5320 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5321 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5322 | /* check whether we're already working on the frontend */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5323 | if (cur_proxy == sess->fe) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5324 | break; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5325 | cur_proxy = sess->fe; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5326 | } |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5327 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 5328 | /* After this point, this anayzer can't return yield, so we can |
| 5329 | * remove the bit corresponding to this analyzer from the list. |
| 5330 | * |
| 5331 | * Note that the intermediate returns and goto found previously |
| 5332 | * reset the analyzers. |
| 5333 | */ |
| 5334 | rep->analysers &= ~an_bit; |
| 5335 | rep->analyse_exp = TICK_ETERNITY; |
| 5336 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5337 | /* OK that's all we can do for 1xx responses */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 5338 | if (unlikely(txn->status < 200 && txn->status != 101)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5339 | goto skip_header_mangling; |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5340 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5341 | /* |
| 5342 | * Now check for a server cookie. |
| 5343 | */ |
Willy Tarreau | 53a09d5 | 2015-08-10 18:59:40 +0200 | [diff] [blame] | 5344 | if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5345 | manage_server_side_cookies(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5346 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5347 | /* |
| 5348 | * Check for cache-control or pragma headers if required. |
| 5349 | */ |
Willy Tarreau | 12b32f2 | 2017-12-21 16:08:09 +0100 | [diff] [blame] | 5350 | if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5351 | check_response_for_cacheability(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5352 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5353 | /* |
| 5354 | * Add server cookie in the response if needed |
| 5355 | */ |
| 5356 | if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) && |
| 5357 | !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5358 | (!(s->flags & SF_DIRECT) || |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5359 | ((s->be->cookie_maxidle || txn->cookie_last_date) && |
| 5360 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 5361 | (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 5362 | (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
| 5363 | (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5364 | !(s->flags & SF_IGNORE_PRST)) { |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5365 | /* the server is known, it's not the one the client requested, or the |
| 5366 | * cookie's last seen date needs to be refreshed. We have to |
| 5367 | * insert a set-cookie here, except if we want to insert only on POST |
| 5368 | * requests and this one isn't. Note that servers which don't have cookies |
| 5369 | * (eg: some backup servers) will return a full cookie removal request. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5370 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5371 | if (!objt_server(s->target)->cookie) { |
| 5372 | chunk_printf(&trash, |
| 5373 | "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
| 5374 | s->be->cookie_name); |
| 5375 | } |
| 5376 | else { |
| 5377 | chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5378 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5379 | if (s->be->cookie_maxidle || s->be->cookie_maxlife) { |
| 5380 | /* emit last_date, which is mandatory */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5381 | trash.area[trash.data++] = COOKIE_DELIM_DATE; |
| 5382 | s30tob64((date.tv_sec+3) >> 2, |
| 5383 | trash.area + trash.data); |
| 5384 | trash.data += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5385 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5386 | if (s->be->cookie_maxlife) { |
| 5387 | /* emit first_date, which is either the original one or |
| 5388 | * the current date. |
| 5389 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5390 | trash.area[trash.data++] = COOKIE_DELIM_DATE; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5391 | s30tob64(txn->cookie_first_date ? |
| 5392 | txn->cookie_first_date >> 2 : |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5393 | (date.tv_sec+3) >> 2, |
| 5394 | trash.area + trash.data); |
| 5395 | trash.data += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5396 | } |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5397 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5398 | chunk_appendf(&trash, "; path=/"); |
| 5399 | } |
Willy Tarreau | 4992dd2 | 2012-05-31 21:02:17 +0200 | [diff] [blame] | 5400 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5401 | if (s->be->cookie_domain) |
| 5402 | chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain); |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5403 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5404 | if (s->be->ck_opts & PR_CK_HTTPONLY) |
| 5405 | chunk_appendf(&trash, "; HttpOnly"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5406 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5407 | if (s->be->ck_opts & PR_CK_SECURE) |
| 5408 | chunk_appendf(&trash, "; Secure"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5409 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5410 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.area, trash.data) < 0)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5411 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5412 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5413 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | b05e48a | 2018-09-20 11:12:58 +0200 | [diff] [blame] | 5414 | if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5415 | /* the server did not change, only the date was updated */ |
| 5416 | txn->flags |= TX_SCK_UPDATED; |
| 5417 | else |
| 5418 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5419 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5420 | /* Here, we will tell an eventual cache on the client side that we don't |
| 5421 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 5422 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 5423 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5424 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5425 | if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5426 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5427 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5428 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5429 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, |
| 5430 | "Cache-control: private", 22) < 0)) |
| 5431 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5432 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5433 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5434 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5435 | /* |
| 5436 | * Check if result will be cacheable with a cookie. |
| 5437 | * We'll block the response if security checks have caught |
| 5438 | * nasty things such as a cacheable cookie. |
| 5439 | */ |
| 5440 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 5441 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
| 5442 | (s->be->options & PR_O_CHK_CACHE)) { |
| 5443 | /* we're in presence of a cacheable response containing |
| 5444 | * a set-cookie header. We'll block it as requested by |
| 5445 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5446 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5447 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5448 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1); |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5449 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5450 | HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1); |
| 5451 | HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5452 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 5453 | HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5454 | |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5455 | ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 5456 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5457 | send_log(s->be, LOG_ALERT, |
| 5458 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 5459 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
| 5460 | goto return_srv_prx_502; |
| 5461 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 5462 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 5463 | skip_filters: |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5464 | /* |
| 5465 | * Adjust "Connection: close" or "Connection: keep-alive" if needed. |
| 5466 | * If an "Upgrade" token is found, the header is left untouched in order |
| 5467 | * not to have to deal with some client bugs : some of them fail an upgrade |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 5468 | * if anything but "Upgrade" is present in the Connection header. We don't |
| 5469 | * want to touch any 101 response either since it's switching to another |
| 5470 | * protocol. |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5471 | */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 5472 | if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) && |
Christopher Faulet | 315b39c | 2018-09-21 16:26:19 +0200 | [diff] [blame] | 5473 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) { |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5474 | unsigned int want_flags = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5475 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5476 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5477 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5478 | /* we want a keep-alive response here. Keep-alive header |
| 5479 | * required if either side is not 1.1. |
| 5480 | */ |
| 5481 | if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11)) |
| 5482 | want_flags |= TX_CON_KAL_SET; |
| 5483 | } |
Christopher Faulet | 315b39c | 2018-09-21 16:26:19 +0200 | [diff] [blame] | 5484 | else { /* CLO */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5485 | /* we want a close response here. Close header required if |
| 5486 | * the server is 1.1, regardless of the client. |
| 5487 | */ |
| 5488 | if (msg->flags & HTTP_MSGF_VER_11) |
| 5489 | want_flags |= TX_CON_CLO_SET; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5490 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5491 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5492 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 5493 | http_change_connection_header(txn, msg, want_flags); |
| 5494 | } |
| 5495 | |
| 5496 | skip_header_mangling: |
Christopher Faulet | 69744d9 | 2017-03-30 10:54:35 +0200 | [diff] [blame] | 5497 | /* Always enter in the body analyzer */ |
| 5498 | rep->analysers &= ~AN_RES_FLT_XFER_DATA; |
| 5499 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5500 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5501 | /* if the user wants to log as soon as possible, without counting |
| 5502 | * bytes from the server, then this is the right moment. We have |
| 5503 | * to temporarily assign bytes_out to log what we currently have. |
| 5504 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5505 | if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) { |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5506 | s->logs.t_close = s->logs.t_data; /* to get a valid end date */ |
| 5507 | s->logs.bytes_out = txn->rsp.eoh; |
| 5508 | s->do_log(s); |
| 5509 | s->logs.bytes_out = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5510 | } |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 5511 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5512 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5513 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5514 | /* This function is an analyser which forwards response body (including chunk |
| 5515 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 5516 | * zero byte. The only situation where it must not be called is when we're in |
| 5517 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 5518 | * remaining data and to resync after end of body. It expects the msg_state to |
| 5519 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5520 | * read more data, or 1 once we can go on with next request or end the stream. |
Willy Tarreau | d351021 | 2014-04-21 11:24:13 +0200 | [diff] [blame] | 5521 | * |
| 5522 | * It is capable of compressing response data both in content-length mode and |
| 5523 | * in chunked mode. The state machines follows different flows depending on |
| 5524 | * whether content-length and chunked modes are used, since there are no |
| 5525 | * trailers in content-length : |
| 5526 | * |
| 5527 | * chk-mode cl-mode |
| 5528 | * ,----- BODY -----. |
| 5529 | * / \ |
| 5530 | * V size > 0 V chk-mode |
| 5531 | * .--> SIZE -------------> DATA -------------> CRLF |
| 5532 | * | | size == 0 | last byte | |
| 5533 | * | v final crlf v inspected | |
| 5534 | * | TRAILERS -----------> DONE | |
| 5535 | * | | |
| 5536 | * `----------------------------------------------' |
| 5537 | * |
| 5538 | * Compression only happens in the DATA state, and must be flushed in final |
| 5539 | * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding |
| 5540 | * is performed at once on final states for all bytes parsed, or when leaving |
| 5541 | * on missing data. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5542 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5543 | int http_response_forward_body(struct stream *s, struct channel *res, int an_bit) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5544 | { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5545 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5546 | struct http_txn *txn = s->txn; |
| 5547 | struct http_msg *msg = &s->txn->rsp; |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 5548 | int ret; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5549 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 5550 | if (IS_HTX_STRM(s)) |
| 5551 | return htx_response_forward_body(s, res, an_bit); |
| 5552 | |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 5553 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n", |
Christopher Faulet | 814d270 | 2017-03-30 11:33:44 +0200 | [diff] [blame] | 5554 | now_ms, __FUNCTION__, |
| 5555 | s, |
| 5556 | res, |
| 5557 | res->rex, res->wex, |
| 5558 | res->flags, |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 5559 | ci_data(res), |
Christopher Faulet | 814d270 | 2017-03-30 11:33:44 +0200 | [diff] [blame] | 5560 | res->analysers); |
| 5561 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5562 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 5563 | return 0; |
| 5564 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5565 | if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) || |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5566 | ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res))) || |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5567 | !s->req.analysers) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5568 | /* Output closed while we were sending data. We must abort and |
| 5569 | * wake the other side up. |
| 5570 | */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 5571 | msg->err_state = msg->msg_state; |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5572 | msg->msg_state = HTTP_MSG_ERROR; |
| 5573 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 5574 | return 1; |
| 5575 | } |
| 5576 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5577 | /* in most states, we should abort in case of early close */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5578 | channel_auto_close(res); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5579 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5580 | if (msg->msg_state == HTTP_MSG_BODY) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5581 | msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 5582 | ? HTTP_MSG_CHUNK_SIZE |
| 5583 | : HTTP_MSG_DATA); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5584 | } |
| 5585 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 5586 | if (res->to_forward) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5587 | /* We can't process the buffer's contents yet */ |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 5588 | res->flags |= CF_WAKE_WRITE; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5589 | goto missing_data_or_waiting; |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 5590 | } |
| 5591 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5592 | if (msg->msg_state < HTTP_MSG_DONE) { |
| 5593 | ret = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 5594 | ? http_msg_forward_chunked_body(s, msg) |
| 5595 | : http_msg_forward_body(s, msg)); |
| 5596 | if (!ret) |
| 5597 | goto missing_data_or_waiting; |
| 5598 | if (ret < 0) |
| 5599 | goto return_bad_res; |
| 5600 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5601 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5602 | /* other states, DONE...TUNNEL */ |
| 5603 | /* for keep-alive we don't want to forward closes on DONE */ |
| 5604 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5605 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
| 5606 | channel_dont_close(res); |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 5607 | |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 5608 | http_resync_states(s); |
| 5609 | if (!(res->analysers & an_bit)) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5610 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 5611 | if (res->flags & CF_SHUTW) { |
| 5612 | /* response errors are most likely due to the |
| 5613 | * client aborting the transfer. */ |
| 5614 | goto aborted_xfer; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5615 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5616 | if (msg->err_pos >= 0) |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 5617 | http_capture_bad_message(s->be, s, msg, msg->err_state, strm_fe(s)); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5618 | goto return_bad_res; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5619 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5620 | return 1; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5621 | } |
Willy Tarreau | f51d03c | 2016-05-02 15:25:15 +0200 | [diff] [blame] | 5622 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5623 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5624 | missing_data_or_waiting: |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5625 | if (res->flags & CF_SHUTW) |
| 5626 | goto aborted_xfer; |
| 5627 | |
| 5628 | /* stop waiting for data if the input is closed before the end. If the |
| 5629 | * client side was already closed, it means that the client has aborted, |
| 5630 | * so we don't want to count this as a server abort. Otherwise it's a |
| 5631 | * server abort. |
| 5632 | */ |
Christopher Faulet | a33510b | 2017-03-31 15:37:29 +0200 | [diff] [blame] | 5633 | if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5634 | if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW)) |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5635 | goto aborted_xfer; |
Christopher Faulet | a46bbd8 | 2015-06-19 09:00:58 +0200 | [diff] [blame] | 5636 | /* If we have some pending data, we continue the processing */ |
Willy Tarreau | 5ba6552 | 2018-06-15 15:14:53 +0200 | [diff] [blame] | 5637 | if (!ci_data(res)) { |
Christopher Faulet | a46bbd8 | 2015-06-19 09:00:58 +0200 | [diff] [blame] | 5638 | if (!(s->flags & SF_ERR_MASK)) |
| 5639 | s->flags |= SF_ERR_SRVCL; |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5640 | HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1); |
Christopher Faulet | a46bbd8 | 2015-06-19 09:00:58 +0200 | [diff] [blame] | 5641 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5642 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1); |
Christopher Faulet | a46bbd8 | 2015-06-19 09:00:58 +0200 | [diff] [blame] | 5643 | goto return_bad_res_stats_ok; |
| 5644 | } |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5645 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5646 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5647 | /* we need to obey the req analyser, so if it leaves, we must too */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5648 | if (!s->req.analysers) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5649 | goto return_bad_res; |
| 5650 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5651 | /* When TE: chunked is used, we need to get there again to parse |
| 5652 | * remaining chunks even if the server has closed, so we don't want to |
Christopher Faulet | 69744d9 | 2017-03-30 10:54:35 +0200 | [diff] [blame] | 5653 | * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side |
| 5654 | * or if there are filters registered on the stream, we don't want to |
| 5655 | * forward a close |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5656 | */ |
Christopher Faulet | 69744d9 | 2017-03-30 10:54:35 +0200 | [diff] [blame] | 5657 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || |
Christopher Faulet | f1cc5d0 | 2017-02-08 09:45:13 +0100 | [diff] [blame] | 5658 | HAS_DATA_FILTERS(s, res) || |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5659 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5660 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5661 | channel_dont_close(res); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5662 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5663 | /* We know that more data are expected, but we couldn't send more that |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5664 | * what we did. So we always set the CF_EXPECT_MORE flag so that the |
Willy Tarreau | 0729303 | 2011-05-30 18:29:28 +0200 | [diff] [blame] | 5665 | * system knows it must not set a PUSH on this first part. Interactive |
Willy Tarreau | 869fc1e | 2012-03-05 08:29:20 +0100 | [diff] [blame] | 5666 | * modes are already handled by the stream sock layer. We must not do |
| 5667 | * this in content-length mode because it could present the MSG_MORE |
| 5668 | * flag with the last block of forwarded data, which would cause an |
| 5669 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5670 | */ |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 5671 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING)) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5672 | res->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5673 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5674 | /* the stream handler will take care of timeouts and errors */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5675 | return 0; |
| 5676 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5677 | return_bad_res: /* let's centralize all bad responses */ |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5678 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5679 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5680 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5681 | |
| 5682 | return_bad_res_stats_ok: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 5683 | txn->rsp.err_state = txn->rsp.msg_state; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5684 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 5685 | /* don't send any error message as we're in the body */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 5686 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5687 | res->analysers &= AN_RES_FLT_END; |
| 5688 | s->req.analysers &= AN_REQ_FLT_END; /* we're in data phase, we want to abort both directions */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5689 | if (objt_server(s->target)) |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 5690 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5691 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5692 | if (!(s->flags & SF_ERR_MASK)) |
| 5693 | s->flags |= SF_ERR_PRXCOND; |
| 5694 | if (!(s->flags & SF_FINST_MASK)) |
| 5695 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5696 | return 0; |
| 5697 | |
| 5698 | aborted_xfer: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 5699 | txn->rsp.err_state = txn->rsp.msg_state; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5700 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 5701 | /* don't send any error message as we're in the body */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 5702 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5703 | res->analysers &= AN_RES_FLT_END; |
| 5704 | s->req.analysers &= AN_REQ_FLT_END; /* we're in data phase, we want to abort both directions */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5705 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5706 | HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1); |
| 5707 | HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5708 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5709 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5710 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5711 | if (!(s->flags & SF_ERR_MASK)) |
| 5712 | s->flags |= SF_ERR_CLICL; |
| 5713 | if (!(s->flags & SF_FINST_MASK)) |
| 5714 | s->flags |= SF_FINST_D; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5715 | return 0; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5716 | } |
| 5717 | |
| 5718 | |
Christopher Faulet | 10079f5 | 2018-10-03 15:17:28 +0200 | [diff] [blame] | 5719 | int http_msg_forward_body(struct stream *s, struct http_msg *msg) |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5720 | { |
| 5721 | struct channel *chn = msg->chn; |
| 5722 | int ret; |
| 5723 | |
| 5724 | /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */ |
| 5725 | |
| 5726 | if (msg->msg_state == HTTP_MSG_ENDING) |
| 5727 | goto ending; |
| 5728 | |
| 5729 | /* Neither content-length, nor transfer-encoding was found, so we must |
| 5730 | * read the body until the server connection is closed. In that case, we |
| 5731 | * eat data as they come. Of course, this happens for response only. */ |
| 5732 | if (!(msg->flags & HTTP_MSGF_XFER_LEN)) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5733 | unsigned long long len = ci_data(chn) - msg->next; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5734 | msg->chunk_len += len; |
| 5735 | msg->body_len += len; |
| 5736 | } |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 5737 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg), |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5738 | /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next), |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 5739 | /* on_error */ goto error); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5740 | msg->next += ret; |
| 5741 | msg->chunk_len -= ret; |
| 5742 | if (msg->chunk_len) { |
| 5743 | /* input empty or output full */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5744 | if (ci_data(chn) > msg->next) |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5745 | chn->flags |= CF_WAKE_WRITE; |
| 5746 | goto missing_data_or_waiting; |
| 5747 | } |
| 5748 | |
Christopher Faulet | 1486b0a | 2017-07-18 11:42:08 +0200 | [diff] [blame] | 5749 | /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is |
| 5750 | * always set for a request. */ |
| 5751 | if (!(msg->flags & HTTP_MSGF_XFER_LEN)) { |
| 5752 | /* The server still sending data that should be filtered */ |
| 5753 | if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn)) |
| 5754 | goto missing_data_or_waiting; |
Christopher Faulet | fd04fcf | 2018-02-02 15:54:15 +0100 | [diff] [blame] | 5755 | msg->msg_state = HTTP_MSG_TUNNEL; |
| 5756 | goto ending; |
Christopher Faulet | 1486b0a | 2017-07-18 11:42:08 +0200 | [diff] [blame] | 5757 | } |
Christopher Faulet | f1cc5d0 | 2017-02-08 09:45:13 +0100 | [diff] [blame] | 5758 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5759 | msg->msg_state = HTTP_MSG_ENDING; |
| 5760 | |
| 5761 | ending: |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 5762 | /* we may have some pending data starting at res->buf.p such as a last |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5763 | * chunk of data or trailers. */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 5764 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next), |
| 5765 | /* default_ret */ msg->next, |
| 5766 | /* on_error */ goto error); |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 5767 | c_adv(chn, ret); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5768 | msg->next -= ret; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 5769 | if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)) |
| 5770 | msg->sov -= ret; |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 5771 | if (msg->next) |
| 5772 | goto waiting; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 5773 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 5774 | FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg), |
| 5775 | /* default_ret */ 1, |
| 5776 | /* on_error */ goto error, |
| 5777 | /* on_wait */ goto waiting); |
Christopher Faulet | fd04fcf | 2018-02-02 15:54:15 +0100 | [diff] [blame] | 5778 | if (msg->msg_state == HTTP_MSG_ENDING) |
| 5779 | msg->msg_state = HTTP_MSG_DONE; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5780 | return 1; |
| 5781 | |
| 5782 | missing_data_or_waiting: |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 5783 | /* we may have some pending data starting at chn->buf.p */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 5784 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next), |
| 5785 | /* default_ret */ msg->next, |
| 5786 | /* on_error */ goto error); |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 5787 | c_adv(chn, ret); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5788 | msg->next -= ret; |
| 5789 | if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0) |
| 5790 | msg->sov -= ret; |
Christopher Faulet | 75e2eb6 | 2015-12-15 10:41:47 +0100 | [diff] [blame] | 5791 | if (!HAS_DATA_FILTERS(s, chn)) |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5792 | msg->chunk_len -= channel_forward(chn, msg->chunk_len); |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 5793 | waiting: |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5794 | return 0; |
| 5795 | error: |
| 5796 | return -1; |
| 5797 | } |
| 5798 | |
Christopher Faulet | 10079f5 | 2018-10-03 15:17:28 +0200 | [diff] [blame] | 5799 | int http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg) |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5800 | { |
| 5801 | struct channel *chn = msg->chn; |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 5802 | unsigned int chunk; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5803 | int ret; |
| 5804 | |
| 5805 | /* Here we have the guarantee to be in one of the following state: |
| 5806 | * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF, |
| 5807 | * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */ |
| 5808 | |
Christopher Faulet | ca874b8 | 2018-09-20 11:31:01 +0200 | [diff] [blame] | 5809 | if (msg->msg_state == HTTP_MSG_ENDING) |
| 5810 | goto ending; |
| 5811 | |
| 5812 | /* Don't parse chunks if there is no input data */ |
| 5813 | if (!ci_data(chn)) |
| 5814 | goto waiting; |
| 5815 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5816 | switch_states: |
| 5817 | switch (msg->msg_state) { |
| 5818 | case HTTP_MSG_DATA: |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 5819 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg), |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5820 | /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next), |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 5821 | /* on_error */ goto error); |
| 5822 | msg->next += ret; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5823 | msg->chunk_len -= ret; |
| 5824 | if (msg->chunk_len) { |
| 5825 | /* input empty or output full */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5826 | if (ci_data(chn) > msg->next) |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5827 | chn->flags |= CF_WAKE_WRITE; |
| 5828 | goto missing_data_or_waiting; |
| 5829 | } |
| 5830 | |
| 5831 | /* nothing left to forward for this chunk*/ |
| 5832 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
| 5833 | /* fall through for HTTP_MSG_CHUNK_CRLF */ |
| 5834 | |
| 5835 | case HTTP_MSG_CHUNK_CRLF: |
| 5836 | /* we want the CRLF after the data */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 5837 | ret = h1_skip_chunk_crlf(&chn->buf, co_data(chn) + msg->next, c_data(chn)); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5838 | if (ret == 0) |
| 5839 | goto missing_data_or_waiting; |
Willy Tarreau | b289256 | 2017-09-21 11:33:54 +0200 | [diff] [blame] | 5840 | if (ret < 0) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5841 | msg->err_pos = ci_data(chn) + ret; |
Willy Tarreau | b289256 | 2017-09-21 11:33:54 +0200 | [diff] [blame] | 5842 | if (msg->err_pos < 0) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 5843 | msg->err_pos += chn->buf.size; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5844 | goto chunk_parsing_error; |
Willy Tarreau | b289256 | 2017-09-21 11:33:54 +0200 | [diff] [blame] | 5845 | } |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 5846 | msg->next += ret; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5847 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 5848 | /* fall through for HTTP_MSG_CHUNK_SIZE */ |
| 5849 | |
| 5850 | case HTTP_MSG_CHUNK_SIZE: |
| 5851 | /* read the chunk size and assign it to ->chunk_len, |
| 5852 | * then set ->next to point to the body and switch to |
| 5853 | * DATA or TRAILERS state. |
| 5854 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 5855 | ret = h1_parse_chunk_size(&chn->buf, co_data(chn) + msg->next, c_data(chn), &chunk); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5856 | if (ret == 0) |
| 5857 | goto missing_data_or_waiting; |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 5858 | if (ret < 0) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5859 | msg->err_pos = ci_data(chn) + ret; |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 5860 | if (msg->err_pos < 0) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 5861 | msg->err_pos += chn->buf.size; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5862 | goto chunk_parsing_error; |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 5863 | } |
| 5864 | |
| 5865 | msg->sol = ret; |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 5866 | msg->next += ret; |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 5867 | msg->chunk_len = chunk; |
| 5868 | msg->body_len += chunk; |
| 5869 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5870 | if (msg->chunk_len) { |
| 5871 | msg->msg_state = HTTP_MSG_DATA; |
| 5872 | goto switch_states; |
| 5873 | } |
| 5874 | msg->msg_state = HTTP_MSG_TRAILERS; |
| 5875 | /* fall through for HTTP_MSG_TRAILERS */ |
| 5876 | |
| 5877 | case HTTP_MSG_TRAILERS: |
| 5878 | ret = http_forward_trailers(msg); |
| 5879 | if (ret < 0) |
| 5880 | goto chunk_parsing_error; |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 5881 | FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg), |
| 5882 | /* default_ret */ 1, |
| 5883 | /* on_error */ goto error); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5884 | msg->next += msg->sol; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5885 | if (!ret) |
| 5886 | goto missing_data_or_waiting; |
| 5887 | break; |
| 5888 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5889 | default: |
| 5890 | /* This should no happen in this function */ |
| 5891 | goto error; |
| 5892 | } |
| 5893 | |
| 5894 | msg->msg_state = HTTP_MSG_ENDING; |
| 5895 | ending: |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 5896 | /* we may have some pending data starting at res->buf.p such as a last |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5897 | * chunk of data or trailers. */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 5898 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next), |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5899 | /* default_ret */ msg->next, |
| 5900 | /* on_error */ goto error); |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 5901 | c_adv(chn, ret); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5902 | msg->next -= ret; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 5903 | if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)) |
| 5904 | msg->sov -= ret; |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 5905 | if (msg->next) |
| 5906 | goto waiting; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 5907 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 5908 | FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg), |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5909 | /* default_ret */ 1, |
| 5910 | /* on_error */ goto error, |
| 5911 | /* on_wait */ goto waiting); |
| 5912 | msg->msg_state = HTTP_MSG_DONE; |
| 5913 | return 1; |
| 5914 | |
| 5915 | missing_data_or_waiting: |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 5916 | /* we may have some pending data starting at chn->buf.p */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 5917 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next), |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5918 | /* default_ret */ msg->next, |
| 5919 | /* on_error */ goto error); |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 5920 | c_adv(chn, ret); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5921 | msg->next -= ret; |
| 5922 | if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0) |
| 5923 | msg->sov -= ret; |
Christopher Faulet | 75e2eb6 | 2015-12-15 10:41:47 +0100 | [diff] [blame] | 5924 | if (!HAS_DATA_FILTERS(s, chn)) |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5925 | msg->chunk_len -= channel_forward(chn, msg->chunk_len); |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 5926 | waiting: |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5927 | return 0; |
| 5928 | |
| 5929 | chunk_parsing_error: |
| 5930 | if (msg->err_pos >= 0) { |
| 5931 | if (chn->flags & CF_ISRESP) |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 5932 | http_capture_bad_message(s->be, s, msg, |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5933 | msg->msg_state, strm_fe(s)); |
| 5934 | else |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 5935 | http_capture_bad_message(strm_fe(s), s, |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5936 | msg, msg->msg_state, s->be); |
| 5937 | } |
| 5938 | error: |
| 5939 | return -1; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5940 | } |
| 5941 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5942 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5943 | /* Iterate the same filter through all request headers. |
| 5944 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5945 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 5946 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5947 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5948 | int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5949 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5950 | char *cur_ptr, *cur_end, *cur_next; |
| 5951 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5952 | struct http_txn *txn = s->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5953 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 5954 | int delta, len; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 5955 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5956 | last_hdr = 0; |
| 5957 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5958 | cur_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5959 | old_idx = 0; |
| 5960 | |
| 5961 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5962 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5963 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5964 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5965 | (exp->action == ACT_ALLOW || |
| 5966 | exp->action == ACT_DENY || |
| 5967 | exp->action == ACT_TARPIT)) |
| 5968 | return 0; |
| 5969 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5970 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5971 | if (!cur_idx) |
| 5972 | break; |
| 5973 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5974 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5975 | cur_ptr = cur_next; |
| 5976 | cur_end = cur_ptr + cur_hdr->len; |
| 5977 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5978 | |
| 5979 | /* Now we have one header between cur_ptr and cur_end, |
| 5980 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5981 | */ |
| 5982 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 5983 | if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5984 | switch (exp->action) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5985 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5986 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5987 | last_hdr = 1; |
| 5988 | break; |
| 5989 | |
| 5990 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5991 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5992 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5993 | break; |
| 5994 | |
| 5995 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5996 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5997 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5998 | break; |
| 5999 | |
| 6000 | case ACT_REPLACE: |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 6001 | len = exp_replace(trash.area, |
| 6002 | trash.size, cur_ptr, |
| 6003 | exp->replace, pmatch); |
| 6004 | if (len < 0) |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 6005 | return -1; |
| 6006 | |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 6007 | delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len); |
| 6008 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6009 | /* FIXME: if the user adds a newline in the replacement, the |
| 6010 | * index will not be recalculated for now, and the new line |
| 6011 | * will not be counted as a new header. |
| 6012 | */ |
| 6013 | |
| 6014 | cur_end += delta; |
| 6015 | cur_next += delta; |
| 6016 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6017 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6018 | break; |
| 6019 | |
| 6020 | case ACT_REMOVE: |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 6021 | delta = b_rep_blk(&req->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6022 | cur_next += delta; |
| 6023 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6024 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6025 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6026 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6027 | cur_hdr->len = 0; |
| 6028 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6029 | cur_idx = old_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6030 | break; |
| 6031 | |
| 6032 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6033 | } |
| 6034 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6035 | /* keep the link from this header to next one in case of later |
| 6036 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6037 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6038 | old_idx = cur_idx; |
| 6039 | } |
| 6040 | return 0; |
| 6041 | } |
| 6042 | |
| 6043 | |
| 6044 | /* Apply the filter to the request line. |
| 6045 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 6046 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6047 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 6048 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6049 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6050 | int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6051 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6052 | char *cur_ptr, *cur_end; |
| 6053 | int done; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6054 | struct http_txn *txn = s->txn; |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 6055 | int delta, len; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6056 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6057 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6058 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6059 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6060 | (exp->action == ACT_ALLOW || |
| 6061 | exp->action == ACT_DENY || |
| 6062 | exp->action == ACT_TARPIT)) |
| 6063 | return 0; |
| 6064 | else if (exp->action == ACT_REMOVE) |
| 6065 | return 0; |
| 6066 | |
| 6067 | done = 0; |
| 6068 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 6069 | cur_ptr = ci_head(req); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6070 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6071 | |
| 6072 | /* Now we have the request line between cur_ptr and cur_end */ |
| 6073 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 6074 | if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6075 | switch (exp->action) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6076 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6077 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6078 | done = 1; |
| 6079 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6080 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6081 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6082 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6083 | done = 1; |
| 6084 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6085 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6086 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6087 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6088 | done = 1; |
| 6089 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6090 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6091 | case ACT_REPLACE: |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 6092 | len = exp_replace(trash.area, trash.size, |
| 6093 | cur_ptr, exp->replace, pmatch); |
| 6094 | if (len < 0) |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 6095 | return -1; |
| 6096 | |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 6097 | delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len); |
| 6098 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6099 | /* FIXME: if the user adds a newline in the replacement, the |
| 6100 | * index will not be recalculated for now, and the new line |
| 6101 | * will not be counted as a new header. |
| 6102 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6103 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6104 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6105 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 6106 | cur_end = (char *)http_parse_reqline(&txn->req, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6107 | HTTP_MSG_RQMETH, |
| 6108 | cur_ptr, cur_end + 1, |
| 6109 | NULL, NULL); |
| 6110 | if (unlikely(!cur_end)) |
| 6111 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6112 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6113 | /* we have a full request and we know that we have either a CR |
| 6114 | * or an LF at <ptr>. |
| 6115 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6116 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 6117 | 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] | 6118 | /* there is no point trying this regex on headers */ |
| 6119 | return 1; |
| 6120 | } |
| 6121 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6122 | return done; |
| 6123 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 6124 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6125 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6126 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6127 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6128 | * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6129 | * 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] | 6130 | * unparsable request. Since it can manage the switch to another backend, it |
| 6131 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6132 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6133 | int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6134 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 6135 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6136 | struct http_txn *txn = s->txn; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6137 | struct hdr_exp *exp; |
| 6138 | |
| 6139 | for (exp = px->req_exp; exp; exp = exp->next) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6140 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6141 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6142 | /* |
| 6143 | * The interleaving of transformations and verdicts |
| 6144 | * makes it difficult to decide to continue or stop |
| 6145 | * the evaluation. |
| 6146 | */ |
| 6147 | |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6148 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) |
| 6149 | break; |
| 6150 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6151 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6152 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6153 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6154 | continue; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6155 | |
| 6156 | /* if this filter had a condition, evaluate it now and skip to |
| 6157 | * next filter if the condition does not match. |
| 6158 | */ |
| 6159 | if (exp->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 6160 | ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6161 | ret = acl_pass(ret); |
| 6162 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 6163 | ret = !ret; |
| 6164 | |
| 6165 | if (!ret) |
| 6166 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6167 | } |
| 6168 | |
| 6169 | /* Apply the filter to the request line. */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6170 | ret = apply_filter_to_req_line(s, req, exp); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6171 | if (unlikely(ret < 0)) |
| 6172 | return -1; |
| 6173 | |
| 6174 | if (likely(ret == 0)) { |
| 6175 | /* The filter did not match the request, it can be |
| 6176 | * iterated through all headers. |
| 6177 | */ |
Willy Tarreau | 34d4c3c | 2015-01-30 20:58:58 +0100 | [diff] [blame] | 6178 | if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0)) |
| 6179 | return -1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6180 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6181 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6182 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6183 | } |
| 6184 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6185 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6186 | /* Delete a value in a header between delimiters <from> and <next> in buffer |
| 6187 | * <buf>. The number of characters displaced is returned, and the pointer to |
| 6188 | * the first delimiter is updated if required. The function tries as much as |
| 6189 | * possible to respect the following principles : |
| 6190 | * - replace <from> delimiter by the <next> one unless <from> points to a |
| 6191 | * colon, in which case <next> is simply removed |
| 6192 | * - set exactly one space character after the new first delimiter, unless |
| 6193 | * there are not enough characters in the block being moved to do so. |
| 6194 | * - remove unneeded spaces before the previous delimiter and after the new |
| 6195 | * one. |
| 6196 | * |
| 6197 | * It is the caller's responsibility to ensure that : |
| 6198 | * - <from> points to a valid delimiter or the colon ; |
| 6199 | * - <next> points to a valid delimiter or the final CR/LF ; |
| 6200 | * - there are non-space chars before <from> ; |
| 6201 | * - there is a CR/LF at or after <next>. |
| 6202 | */ |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 6203 | int del_hdr_value(struct buffer *buf, char **from, char *next) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6204 | { |
| 6205 | char *prev = *from; |
| 6206 | |
| 6207 | if (*prev == ':') { |
| 6208 | /* We're removing the first value, preserve the colon and add a |
| 6209 | * space if possible. |
| 6210 | */ |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6211 | if (!HTTP_IS_CRLF(*next)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6212 | next++; |
| 6213 | prev++; |
| 6214 | if (prev < next) |
| 6215 | *prev++ = ' '; |
| 6216 | |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6217 | while (HTTP_IS_SPHT(*next)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6218 | next++; |
| 6219 | } else { |
| 6220 | /* Remove useless spaces before the old delimiter. */ |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6221 | while (HTTP_IS_SPHT(*(prev-1))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6222 | prev--; |
| 6223 | *from = prev; |
| 6224 | |
| 6225 | /* copy the delimiter and if possible a space if we're |
| 6226 | * not at the end of the line. |
| 6227 | */ |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6228 | if (!HTTP_IS_CRLF(*next)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6229 | *prev++ = *next++; |
| 6230 | if (prev + 1 < next) |
| 6231 | *prev++ = ' '; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6232 | while (HTTP_IS_SPHT(*next)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6233 | next++; |
| 6234 | } |
| 6235 | } |
Willy Tarreau | e312802 | 2018-07-12 15:55:34 +0200 | [diff] [blame] | 6236 | return b_rep_blk(buf, prev, next, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6237 | } |
| 6238 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6239 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 6240 | * 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] | 6241 | * desirable to call it only when needed. This code is quite complex because |
| 6242 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 6243 | * highly recommended not to touch this part without a good reason ! |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6244 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6245 | void manage_client_side_cookies(struct stream *s, struct channel *req) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6246 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6247 | struct http_txn *txn = s->txn; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6248 | struct session *sess = s->sess; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6249 | int preserve_hdr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6250 | int cur_idx, old_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6251 | char *hdr_beg, *hdr_end, *hdr_next, *del_from; |
| 6252 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6253 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6254 | /* Iterate through the headers, we start with the start line. */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 6255 | old_idx = 0; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 6256 | hdr_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6257 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6258 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6259 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6260 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6261 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6262 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6263 | hdr_beg = hdr_next; |
| 6264 | hdr_end = hdr_beg + cur_hdr->len; |
| 6265 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6266 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6267 | /* We have one full header between hdr_beg and hdr_end, and the |
| 6268 | * next header starts at hdr_next. We're only interested in |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6269 | * "Cookie:" headers. |
| 6270 | */ |
| 6271 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6272 | val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6273 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6274 | old_idx = cur_idx; |
| 6275 | continue; |
| 6276 | } |
| 6277 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6278 | del_from = NULL; /* nothing to be deleted */ |
| 6279 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 6280 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6281 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 6282 | * attributes whose name begin with a '$', and associate them with |
| 6283 | * the right cookie, if we want to delete this cookie. |
| 6284 | * So there are 3 cases for each cookie read : |
| 6285 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 6286 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 6287 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 6288 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 6289 | * "special" cookie. |
| 6290 | * At the end of loop, if a "special" cookie remains, we may have to |
| 6291 | * remove it. If no application cookie persists in the header, we |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6292 | * *MUST* delete it. |
| 6293 | * |
| 6294 | * Note: RFC2965 is unclear about the processing of spaces around |
| 6295 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 6296 | * the RFC explicitly allows spaces before it, and not within the |
| 6297 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 6298 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 6299 | * after the equal sign too, resulting in some (rare) buggy |
| 6300 | * implementations trying to do that. So let's do what servers do. |
| 6301 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 6302 | * allowed quoted strings in values, with any possible character |
| 6303 | * after a backslash, including control chars and delimitors, which |
| 6304 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 6305 | * within values even without quotes. |
| 6306 | * |
| 6307 | * We have to keep multiple pointers in order to support cookie |
| 6308 | * removal at the beginning, middle or end of header without |
| 6309 | * corrupting the header. All of these headers are valid : |
| 6310 | * |
| 6311 | * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n |
| 6312 | * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n |
| 6313 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 6314 | * | | | | | | | | | |
| 6315 | * | | | | | | | | hdr_end <--+ |
| 6316 | * | | | | | | | +--> next |
| 6317 | * | | | | | | +----> val_end |
| 6318 | * | | | | | +-----------> val_beg |
| 6319 | * | | | | +--------------> equal |
| 6320 | * | | | +----------------> att_end |
| 6321 | * | | +---------------------> att_beg |
| 6322 | * | +--------------------------> prev |
| 6323 | * +--------------------------------> hdr_beg |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6324 | */ |
| 6325 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6326 | for (prev = hdr_beg + 6; prev < hdr_end; prev = next) { |
| 6327 | /* Iterate through all cookies on this line */ |
| 6328 | |
| 6329 | /* find att_beg */ |
| 6330 | att_beg = prev + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6331 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6332 | att_beg++; |
| 6333 | |
| 6334 | /* find att_end : this is the first character after the last non |
| 6335 | * space before the equal. It may be equal to hdr_end. |
| 6336 | */ |
| 6337 | equal = att_end = att_beg; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6338 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6339 | while (equal < hdr_end) { |
| 6340 | if (*equal == '=' || *equal == ',' || *equal == ';') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6341 | break; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6342 | if (HTTP_IS_SPHT(*equal++)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6343 | continue; |
| 6344 | att_end = equal; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6345 | } |
| 6346 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6347 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 6348 | * is between <att_beg> and <equal>, both may be identical. |
| 6349 | */ |
| 6350 | |
| 6351 | /* look for end of cookie if there is an equal sign */ |
| 6352 | if (equal < hdr_end && *equal == '=') { |
| 6353 | /* look for the beginning of the value */ |
| 6354 | val_beg = equal + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6355 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6356 | val_beg++; |
| 6357 | |
| 6358 | /* find the end of the value, respecting quotes */ |
Willy Tarreau | ab813a4 | 2018-09-10 18:41:28 +0200 | [diff] [blame] | 6359 | next = http_find_cookie_value_end(val_beg, hdr_end); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6360 | |
| 6361 | /* make val_end point to the first white space or delimitor after the value */ |
| 6362 | val_end = next; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6363 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6364 | val_end--; |
| 6365 | } else { |
| 6366 | val_beg = val_end = next = equal; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6367 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6368 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6369 | /* We have nothing to do with attributes beginning with '$'. However, |
| 6370 | * they will automatically be removed if a header before them is removed, |
| 6371 | * since they're supposed to be linked together. |
| 6372 | */ |
| 6373 | if (*att_beg == '$') |
| 6374 | continue; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6375 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6376 | /* Ignore cookies with no equal sign */ |
| 6377 | if (equal == next) { |
| 6378 | /* This is not our cookie, so we must preserve it. But if we already |
| 6379 | * scheduled another cookie for removal, we cannot remove the |
| 6380 | * complete header, but we can remove the previous block itself. |
| 6381 | */ |
| 6382 | preserve_hdr = 1; |
| 6383 | if (del_from != NULL) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 6384 | int delta = del_hdr_value(&req->buf, &del_from, prev); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6385 | val_end += delta; |
| 6386 | next += delta; |
| 6387 | hdr_end += delta; |
| 6388 | hdr_next += delta; |
| 6389 | cur_hdr->len += delta; |
| 6390 | http_msg_move_end(&txn->req, delta); |
| 6391 | prev = del_from; |
| 6392 | del_from = NULL; |
| 6393 | } |
| 6394 | continue; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6395 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6396 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6397 | /* if there are spaces around the equal sign, we need to |
| 6398 | * strip them otherwise we'll get trouble for cookie captures, |
| 6399 | * or even for rewrites. Since this happens extremely rarely, |
| 6400 | * it does not hurt performance. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6401 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6402 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 6403 | int stripped_before = 0; |
| 6404 | int stripped_after = 0; |
| 6405 | |
| 6406 | if (att_end != equal) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 6407 | stripped_before = b_rep_blk(&req->buf, att_end, equal, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6408 | equal += stripped_before; |
| 6409 | val_beg += stripped_before; |
| 6410 | } |
| 6411 | |
| 6412 | if (val_beg > equal + 1) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 6413 | stripped_after = b_rep_blk(&req->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6414 | val_beg += stripped_after; |
| 6415 | stripped_before += stripped_after; |
| 6416 | } |
| 6417 | |
| 6418 | val_end += stripped_before; |
| 6419 | next += stripped_before; |
| 6420 | hdr_end += stripped_before; |
| 6421 | hdr_next += stripped_before; |
| 6422 | cur_hdr->len += stripped_before; |
| 6423 | http_msg_move_end(&txn->req, stripped_before); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6424 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6425 | /* now everything is as on the diagram above */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6426 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6427 | /* First, let's see if we want to capture this cookie. We check |
| 6428 | * that we don't already have a client side cookie, because we |
| 6429 | * can only capture one. Also as an optimisation, we ignore |
| 6430 | * cookies shorter than the declared name. |
| 6431 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6432 | if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 6433 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 6434 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6435 | int log_len = val_end - att_beg; |
| 6436 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6437 | if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 6438 | ha_alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6439 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6440 | if (log_len > sess->fe->capture_len) |
| 6441 | log_len = sess->fe->capture_len; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6442 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 6443 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6444 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6445 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6446 | |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6447 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 6448 | * following form : |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6449 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6450 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6451 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6452 | * For cookies in prefix mode, the form is : |
| 6453 | * |
| 6454 | * Cookie: NAME=SRV~VALUE |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6455 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6456 | if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 6457 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
| 6458 | struct server *srv = s->be->srv; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6459 | char *delim; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6460 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6461 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 6462 | * have the server ID between val_beg and delim, and the original cookie between |
| 6463 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 6464 | * |
| 6465 | * Cookie: NAME=SRV; # in all but prefix modes |
| 6466 | * Cookie: NAME=SRV~OPAQUE ; # in prefix mode |
| 6467 | * | || || | |+-> next |
| 6468 | * | || || | +--> val_end |
| 6469 | * | || || +---------> delim |
| 6470 | * | || |+------------> val_beg |
| 6471 | * | || +-------------> att_end = equal |
| 6472 | * | |+-----------------> att_beg |
| 6473 | * | +------------------> prev |
| 6474 | * +-------------------------> hdr_beg |
| 6475 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6476 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6477 | if (s->be->ck_opts & PR_CK_PFX) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6478 | for (delim = val_beg; delim < val_end; delim++) |
| 6479 | if (*delim == COOKIE_DELIM) |
| 6480 | break; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6481 | } else { |
| 6482 | char *vbar1; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6483 | delim = val_end; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6484 | /* Now check if the cookie contains a date field, which would |
| 6485 | * appear after a vertical bar ('|') just after the server name |
| 6486 | * and before the delimiter. |
| 6487 | */ |
| 6488 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 6489 | if (vbar1) { |
| 6490 | /* OK, so left of the bar is the server's cookie and |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6491 | * right is the last seen date. It is a base64 encoded |
| 6492 | * 30-bit value representing the UNIX date since the |
| 6493 | * epoch in 4-second quantities. |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6494 | */ |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6495 | int val; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6496 | delim = vbar1++; |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6497 | if (val_end - vbar1 >= 5) { |
| 6498 | val = b64tos30(vbar1); |
| 6499 | if (val > 0) |
| 6500 | txn->cookie_last_date = val << 2; |
| 6501 | } |
| 6502 | /* look for a second vertical bar */ |
| 6503 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 6504 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 6505 | val = b64tos30(vbar1 + 1); |
| 6506 | if (val > 0) |
| 6507 | txn->cookie_first_date = val << 2; |
| 6508 | } |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6509 | } |
| 6510 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6511 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6512 | /* if the cookie has an expiration date and the proxy wants to check |
| 6513 | * it, then we do that now. We first check if the cookie is too old, |
| 6514 | * then only if it has expired. We detect strict overflow because the |
| 6515 | * time resolution here is not great (4 seconds). Cookies with dates |
| 6516 | * in the future are ignored if their offset is beyond one day. This |
| 6517 | * allows an admin to fix timezone issues without expiring everyone |
| 6518 | * and at the same time avoids keeping unwanted side effects for too |
| 6519 | * long. |
| 6520 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6521 | if (txn->cookie_first_date && s->be->cookie_maxlife && |
| 6522 | (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) || |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6523 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6524 | txn->flags &= ~TX_CK_MASK; |
| 6525 | txn->flags |= TX_CK_OLD; |
| 6526 | delim = val_beg; // let's pretend we have not found the cookie |
| 6527 | txn->cookie_first_date = 0; |
| 6528 | txn->cookie_last_date = 0; |
| 6529 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6530 | else if (txn->cookie_last_date && s->be->cookie_maxidle && |
| 6531 | (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) || |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6532 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6533 | txn->flags &= ~TX_CK_MASK; |
| 6534 | txn->flags |= TX_CK_EXPIRED; |
| 6535 | delim = val_beg; // let's pretend we have not found the cookie |
| 6536 | txn->cookie_first_date = 0; |
| 6537 | txn->cookie_last_date = 0; |
| 6538 | } |
| 6539 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6540 | /* Here, we'll look for the first running server which supports the cookie. |
| 6541 | * This allows to share a same cookie between several servers, for example |
| 6542 | * to dedicate backup servers to specific servers only. |
| 6543 | * However, to prevent clients from sticking to cookie-less backup server |
| 6544 | * when they have incidentely learned an empty cookie, we simply ignore |
| 6545 | * empty cookies and mark them as invalid. |
| 6546 | * The same behaviour is applied when persistence must be ignored. |
| 6547 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6548 | if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6549 | srv = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6550 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6551 | while (srv) { |
| 6552 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 6553 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 6554 | if ((srv->cur_state != SRV_ST_STOPPED) || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6555 | (s->be->options & PR_O_PERSIST) || |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6556 | (s->flags & SF_FORCE_PRST)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6557 | /* we found the server and we can use it */ |
| 6558 | txn->flags &= ~TX_CK_MASK; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 6559 | txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6560 | s->flags |= SF_DIRECT | SF_ASSIGNED; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6561 | s->target = &srv->obj_type; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6562 | break; |
| 6563 | } else { |
| 6564 | /* we found a server, but it's down, |
| 6565 | * mark it as such and go on in case |
| 6566 | * another one is available. |
| 6567 | */ |
| 6568 | txn->flags &= ~TX_CK_MASK; |
| 6569 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6570 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6571 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6572 | srv = srv->next; |
| 6573 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6574 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6575 | if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) { |
Willy Tarreau | c89ccb6 | 2012-04-05 21:18:22 +0200 | [diff] [blame] | 6576 | /* no server matched this cookie or we deliberately skipped it */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6577 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6578 | if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
Willy Tarreau | c89ccb6 | 2012-04-05 21:18:22 +0200 | [diff] [blame] | 6579 | txn->flags |= TX_CK_UNUSED; |
| 6580 | else |
| 6581 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6582 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6583 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6584 | /* depending on the cookie mode, we may have to either : |
| 6585 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 6586 | * the server never sees it ; |
| 6587 | * - remove the server id from the cookie value, and tag the cookie as an |
Joseph Herlant | 5ba8025 | 2018-11-15 09:25:36 -0800 | [diff] [blame] | 6588 | * application cookie so that it does not get accidently removed later, |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6589 | * if we're in cookie prefix mode |
| 6590 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6591 | if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6592 | int delta; /* negative */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6593 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 6594 | delta = b_rep_blk(&req->buf, val_beg, delim + 1, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6595 | val_end += delta; |
| 6596 | next += delta; |
| 6597 | hdr_end += delta; |
| 6598 | hdr_next += delta; |
| 6599 | cur_hdr->len += delta; |
| 6600 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6601 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6602 | del_from = NULL; |
| 6603 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 6604 | } |
| 6605 | else if (del_from == NULL && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6606 | (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6607 | del_from = prev; |
| 6608 | } |
| 6609 | } else { |
| 6610 | /* This is not our cookie, so we must preserve it. But if we already |
| 6611 | * scheduled another cookie for removal, we cannot remove the |
| 6612 | * complete header, but we can remove the previous block itself. |
| 6613 | */ |
| 6614 | preserve_hdr = 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6615 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6616 | if (del_from != NULL) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 6617 | int delta = del_hdr_value(&req->buf, &del_from, prev); |
Willy Tarreau | b810554 | 2010-11-24 18:31:28 +0100 | [diff] [blame] | 6618 | if (att_beg >= del_from) |
| 6619 | att_beg += delta; |
| 6620 | if (att_end >= del_from) |
| 6621 | att_end += delta; |
| 6622 | val_beg += delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6623 | val_end += delta; |
| 6624 | next += delta; |
| 6625 | hdr_end += delta; |
| 6626 | hdr_next += delta; |
| 6627 | cur_hdr->len += delta; |
| 6628 | http_msg_move_end(&txn->req, delta); |
| 6629 | prev = del_from; |
| 6630 | del_from = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6631 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6632 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6633 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6634 | /* continue with next cookie on this header line */ |
| 6635 | att_beg = next; |
| 6636 | } /* for each cookie */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6637 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6638 | /* There are no more cookies on this line. |
| 6639 | * We may still have one (or several) marked for deletion at the |
| 6640 | * end of the line. We must do this now in two ways : |
| 6641 | * - if some cookies must be preserved, we only delete from the |
| 6642 | * mark to the end of line ; |
| 6643 | * - if nothing needs to be preserved, simply delete the whole header |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6644 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6645 | if (del_from) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6646 | int delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6647 | if (preserve_hdr) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 6648 | delta = del_hdr_value(&req->buf, &del_from, hdr_end); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6649 | hdr_end = del_from; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6650 | cur_hdr->len += delta; |
| 6651 | } else { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 6652 | delta = b_rep_blk(&req->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6653 | |
| 6654 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6655 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6656 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6657 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6658 | cur_idx = old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6659 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6660 | hdr_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6661 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6662 | } |
| 6663 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6664 | /* check next header */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6665 | old_idx = cur_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6666 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6667 | } |
| 6668 | |
| 6669 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6670 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 6671 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 6672 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6673 | int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6674 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6675 | char *cur_ptr, *cur_end, *cur_next; |
| 6676 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6677 | struct http_txn *txn = s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6678 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 6679 | int delta, len; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6680 | |
| 6681 | last_hdr = 0; |
| 6682 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 6683 | cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6684 | old_idx = 0; |
| 6685 | |
| 6686 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6687 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6688 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6689 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6690 | (exp->action == ACT_ALLOW || |
| 6691 | exp->action == ACT_DENY)) |
| 6692 | return 0; |
| 6693 | |
| 6694 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 6695 | if (!cur_idx) |
| 6696 | break; |
| 6697 | |
| 6698 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 6699 | cur_ptr = cur_next; |
| 6700 | cur_end = cur_ptr + cur_hdr->len; |
| 6701 | cur_next = cur_end + cur_hdr->cr + 1; |
| 6702 | |
| 6703 | /* Now we have one header between cur_ptr and cur_end, |
| 6704 | * and the next header starts at cur_next. |
| 6705 | */ |
| 6706 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 6707 | if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6708 | switch (exp->action) { |
| 6709 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6710 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6711 | last_hdr = 1; |
| 6712 | break; |
| 6713 | |
| 6714 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6715 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6716 | last_hdr = 1; |
| 6717 | break; |
| 6718 | |
| 6719 | case ACT_REPLACE: |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 6720 | len = exp_replace(trash.area, |
| 6721 | trash.size, cur_ptr, |
| 6722 | exp->replace, pmatch); |
| 6723 | if (len < 0) |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 6724 | return -1; |
| 6725 | |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 6726 | delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len); |
| 6727 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6728 | /* FIXME: if the user adds a newline in the replacement, the |
| 6729 | * index will not be recalculated for now, and the new line |
| 6730 | * will not be counted as a new header. |
| 6731 | */ |
| 6732 | |
| 6733 | cur_end += delta; |
| 6734 | cur_next += delta; |
| 6735 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6736 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6737 | break; |
| 6738 | |
| 6739 | case ACT_REMOVE: |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 6740 | delta = b_rep_blk(&rtr->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6741 | cur_next += delta; |
| 6742 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6743 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6744 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6745 | txn->hdr_idx.used--; |
| 6746 | cur_hdr->len = 0; |
| 6747 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6748 | cur_idx = old_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6749 | break; |
| 6750 | |
| 6751 | } |
| 6752 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6753 | |
| 6754 | /* keep the link from this header to next one in case of later |
| 6755 | * removal of next header. |
| 6756 | */ |
| 6757 | old_idx = cur_idx; |
| 6758 | } |
| 6759 | return 0; |
| 6760 | } |
| 6761 | |
| 6762 | |
| 6763 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 6764 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 6765 | * or -1 if a replacement resulted in an invalid status line. |
| 6766 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6767 | int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6768 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6769 | char *cur_ptr, *cur_end; |
| 6770 | int done; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6771 | struct http_txn *txn = s->txn; |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 6772 | int delta, len; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6773 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6774 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6775 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6776 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6777 | (exp->action == ACT_ALLOW || |
| 6778 | exp->action == ACT_DENY)) |
| 6779 | return 0; |
| 6780 | else if (exp->action == ACT_REMOVE) |
| 6781 | return 0; |
| 6782 | |
| 6783 | done = 0; |
| 6784 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 6785 | cur_ptr = ci_head(rtr); |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 6786 | cur_end = cur_ptr + txn->rsp.sl.st.l; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6787 | |
| 6788 | /* Now we have the status line between cur_ptr and cur_end */ |
| 6789 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 6790 | if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6791 | switch (exp->action) { |
| 6792 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6793 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6794 | done = 1; |
| 6795 | break; |
| 6796 | |
| 6797 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6798 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6799 | done = 1; |
| 6800 | break; |
| 6801 | |
| 6802 | case ACT_REPLACE: |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 6803 | len = exp_replace(trash.area, trash.size, |
| 6804 | cur_ptr, exp->replace, pmatch); |
| 6805 | if (len < 0) |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 6806 | return -1; |
| 6807 | |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 6808 | delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len); |
| 6809 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6810 | /* FIXME: if the user adds a newline in the replacement, the |
| 6811 | * index will not be recalculated for now, and the new line |
| 6812 | * will not be counted as a new header. |
| 6813 | */ |
| 6814 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6815 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6816 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 6817 | cur_end = (char *)http_parse_stsline(&txn->rsp, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 6818 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6819 | cur_ptr, cur_end + 1, |
| 6820 | NULL, NULL); |
| 6821 | if (unlikely(!cur_end)) |
| 6822 | return -1; |
| 6823 | |
| 6824 | /* we have a full respnse and we know that we have either a CR |
| 6825 | * or an LF at <ptr>. |
| 6826 | */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 6827 | txn->status = strl2ui(ci_head(rtr) + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l); |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 6828 | 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] | 6829 | /* there is no point trying this regex on headers */ |
| 6830 | return 1; |
| 6831 | } |
| 6832 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6833 | return done; |
| 6834 | } |
| 6835 | |
| 6836 | |
| 6837 | |
| 6838 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6839 | * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6840 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 6841 | * unparsable response. |
| 6842 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6843 | int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6844 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 6845 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6846 | struct http_txn *txn = s->txn; |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6847 | struct hdr_exp *exp; |
| 6848 | |
| 6849 | for (exp = px->rsp_exp; exp; exp = exp->next) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6850 | int ret; |
| 6851 | |
| 6852 | /* |
| 6853 | * The interleaving of transformations and verdicts |
| 6854 | * makes it difficult to decide to continue or stop |
| 6855 | * the evaluation. |
| 6856 | */ |
| 6857 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6858 | if (txn->flags & TX_SVDENY) |
| 6859 | break; |
| 6860 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6861 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6862 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 6863 | exp->action == ACT_PASS)) { |
| 6864 | exp = exp->next; |
| 6865 | continue; |
| 6866 | } |
| 6867 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6868 | /* if this filter had a condition, evaluate it now and skip to |
| 6869 | * next filter if the condition does not match. |
| 6870 | */ |
| 6871 | if (exp->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 6872 | ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL); |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6873 | ret = acl_pass(ret); |
| 6874 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 6875 | ret = !ret; |
| 6876 | if (!ret) |
| 6877 | continue; |
| 6878 | } |
| 6879 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6880 | /* Apply the filter to the status line. */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6881 | ret = apply_filter_to_sts_line(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6882 | if (unlikely(ret < 0)) |
| 6883 | return -1; |
| 6884 | |
| 6885 | if (likely(ret == 0)) { |
| 6886 | /* The filter did not match the response, it can be |
| 6887 | * iterated through all headers. |
| 6888 | */ |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 6889 | if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0)) |
| 6890 | return -1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6891 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6892 | } |
| 6893 | return 0; |
| 6894 | } |
| 6895 | |
| 6896 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6897 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 6898 | * 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] | 6899 | * desirable to call it only when needed. This function is also used when we |
| 6900 | * 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] | 6901 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6902 | void manage_server_side_cookies(struct stream *s, struct channel *res) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6903 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6904 | struct http_txn *txn = s->txn; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6905 | struct session *sess = s->sess; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6906 | struct server *srv; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6907 | int is_cookie2; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6908 | int cur_idx, old_idx, delta; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6909 | char *hdr_beg, *hdr_end, *hdr_next; |
| 6910 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6911 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6912 | /* Iterate through the headers. |
| 6913 | * we start with the start line. |
| 6914 | */ |
| 6915 | old_idx = 0; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 6916 | hdr_next = ci_head(res) + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6917 | |
| 6918 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 6919 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6920 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6921 | |
| 6922 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6923 | hdr_beg = hdr_next; |
| 6924 | hdr_end = hdr_beg + cur_hdr->len; |
| 6925 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6926 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6927 | /* We have one full header between hdr_beg and hdr_end, and the |
| 6928 | * next header starts at hdr_next. We're only interested in |
| 6929 | * "Set-Cookie" and "Set-Cookie2" headers. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6930 | */ |
| 6931 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6932 | is_cookie2 = 0; |
| 6933 | prev = hdr_beg + 10; |
| 6934 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6935 | if (!val) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6936 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11); |
| 6937 | if (!val) { |
| 6938 | old_idx = cur_idx; |
| 6939 | continue; |
| 6940 | } |
| 6941 | is_cookie2 = 1; |
| 6942 | prev = hdr_beg + 11; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6943 | } |
| 6944 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6945 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 6946 | * <prev> points to the colon. |
| 6947 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 6948 | txn->flags |= TX_SCK_PRESENT; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6949 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6950 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 6951 | * check-cache is enabled) and we are not interested in checking |
| 6952 | * them. Warning, the cookie capture is declared in the frontend. |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 6953 | */ |
Willy Tarreau | 53a09d5 | 2015-08-10 18:59:40 +0200 | [diff] [blame] | 6954 | if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6955 | return; |
| 6956 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6957 | /* OK so now we know we have to process this response cookie. |
| 6958 | * The format of the Set-Cookie header is slightly different |
| 6959 | * from the format of the Cookie header in that it does not |
| 6960 | * support the comma as a cookie delimiter (thus the header |
| 6961 | * cannot be folded) because the Expires attribute described in |
| 6962 | * the original Netscape's spec may contain an unquoted date |
| 6963 | * with a comma inside. We have to live with this because |
| 6964 | * many browsers don't support Max-Age and some browsers don't |
| 6965 | * support quoted strings. However the Set-Cookie2 header is |
| 6966 | * clean. |
| 6967 | * |
| 6968 | * We have to keep multiple pointers in order to support cookie |
| 6969 | * removal at the beginning, middle or end of header without |
| 6970 | * corrupting the header (in case of set-cookie2). A special |
| 6971 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 6972 | * fields after the first semi-colon. The <next> pointer points |
| 6973 | * either to the end of line (set-cookie) or next unquoted comma |
| 6974 | * (set-cookie2). All of these headers are valid : |
| 6975 | * |
| 6976 | * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n |
| 6977 | * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 6978 | * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 6979 | * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n |
| 6980 | * | | | | | | | | | | |
| 6981 | * | | | | | | | | +-> next hdr_end <--+ |
| 6982 | * | | | | | | | +------------> scav |
| 6983 | * | | | | | | +--------------> val_end |
| 6984 | * | | | | | +--------------------> val_beg |
| 6985 | * | | | | +----------------------> equal |
| 6986 | * | | | +------------------------> att_end |
| 6987 | * | | +----------------------------> att_beg |
| 6988 | * | +------------------------------> prev |
| 6989 | * +-----------------------------------------> hdr_beg |
| 6990 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6991 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6992 | for (; prev < hdr_end; prev = next) { |
| 6993 | /* Iterate through all cookies on this line */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6994 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6995 | /* find att_beg */ |
| 6996 | att_beg = prev + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6997 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6998 | att_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6999 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7000 | /* find att_end : this is the first character after the last non |
| 7001 | * space before the equal. It may be equal to hdr_end. |
| 7002 | */ |
| 7003 | equal = att_end = att_beg; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7004 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7005 | while (equal < hdr_end) { |
| 7006 | if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ',')) |
| 7007 | break; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7008 | if (HTTP_IS_SPHT(*equal++)) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7009 | continue; |
| 7010 | att_end = equal; |
| 7011 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7012 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7013 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 7014 | * is between <att_beg> and <equal>, both may be identical. |
| 7015 | */ |
| 7016 | |
| 7017 | /* look for end of cookie if there is an equal sign */ |
| 7018 | if (equal < hdr_end && *equal == '=') { |
| 7019 | /* look for the beginning of the value */ |
| 7020 | val_beg = equal + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7021 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7022 | val_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7023 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7024 | /* find the end of the value, respecting quotes */ |
Willy Tarreau | ab813a4 | 2018-09-10 18:41:28 +0200 | [diff] [blame] | 7025 | next = http_find_cookie_value_end(val_beg, hdr_end); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7026 | |
| 7027 | /* make val_end point to the first white space or delimitor after the value */ |
| 7028 | val_end = next; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 7029 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7030 | val_end--; |
| 7031 | } else { |
| 7032 | /* <equal> points to next comma, semi-colon or EOL */ |
| 7033 | val_beg = val_end = next = equal; |
| 7034 | } |
| 7035 | |
| 7036 | if (next < hdr_end) { |
| 7037 | /* Set-Cookie2 supports multiple cookies, and <next> points to |
| 7038 | * a colon or semi-colon before the end. So skip all attr-value |
| 7039 | * pairs and look for the next comma. For Set-Cookie, since |
| 7040 | * commas are permitted in values, skip to the end. |
| 7041 | */ |
| 7042 | if (is_cookie2) |
Willy Tarreau | ab813a4 | 2018-09-10 18:41:28 +0200 | [diff] [blame] | 7043 | next = http_find_hdr_value_end(next, hdr_end); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7044 | else |
| 7045 | next = hdr_end; |
| 7046 | } |
| 7047 | |
| 7048 | /* Now everything is as on the diagram above */ |
| 7049 | |
| 7050 | /* Ignore cookies with no equal sign */ |
| 7051 | if (equal == val_end) |
| 7052 | continue; |
| 7053 | |
| 7054 | /* If there are spaces around the equal sign, we need to |
| 7055 | * strip them otherwise we'll get trouble for cookie captures, |
| 7056 | * or even for rewrites. Since this happens extremely rarely, |
| 7057 | * it does not hurt performance. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7058 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7059 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 7060 | int stripped_before = 0; |
| 7061 | int stripped_after = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7062 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7063 | if (att_end != equal) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 7064 | stripped_before = b_rep_blk(&res->buf, att_end, equal, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7065 | equal += stripped_before; |
| 7066 | val_beg += stripped_before; |
| 7067 | } |
| 7068 | |
| 7069 | if (val_beg > equal + 1) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 7070 | stripped_after = b_rep_blk(&res->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7071 | val_beg += stripped_after; |
| 7072 | stripped_before += stripped_after; |
| 7073 | } |
| 7074 | |
| 7075 | val_end += stripped_before; |
| 7076 | next += stripped_before; |
| 7077 | hdr_end += stripped_before; |
| 7078 | hdr_next += stripped_before; |
| 7079 | cur_hdr->len += stripped_before; |
Willy Tarreau | 1fc1f45 | 2011-04-07 22:35:37 +0200 | [diff] [blame] | 7080 | http_msg_move_end(&txn->rsp, stripped_before); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7081 | } |
| 7082 | |
| 7083 | /* First, let's see if we want to capture this cookie. We check |
| 7084 | * that we don't already have a server side cookie, because we |
| 7085 | * can only capture one. Also as an optimisation, we ignore |
| 7086 | * cookies shorter than the declared name. |
| 7087 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7088 | if (sess->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 7089 | txn->srv_cookie == NULL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7090 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 7091 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7092 | int log_len = val_end - att_beg; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7093 | if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 7094 | ha_alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7095 | } |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 7096 | else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7097 | if (log_len > sess->fe->capture_len) |
| 7098 | log_len = sess->fe->capture_len; |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 7099 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 7100 | txn->srv_cookie[log_len] = 0; |
| 7101 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7102 | } |
| 7103 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7104 | srv = objt_server(s->target); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7105 | /* now check if we need to process it for persistence */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7106 | if (!(s->flags & SF_IGNORE_PRST) && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7107 | (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 7108 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7109 | /* assume passive cookie by default */ |
| 7110 | txn->flags &= ~TX_SCK_MASK; |
| 7111 | txn->flags |= TX_SCK_FOUND; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7112 | |
| 7113 | /* If the cookie is in insert mode on a known server, we'll delete |
| 7114 | * this occurrence because we'll insert another one later. |
| 7115 | * 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] | 7116 | * a direct access. |
| 7117 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7118 | if (s->be->ck_opts & PR_CK_PSV) { |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 7119 | /* The "preserve" flag was set, we don't want to touch the |
| 7120 | * server's cookie. |
| 7121 | */ |
| 7122 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7123 | else if ((srv && (s->be->ck_opts & PR_CK_INS)) || |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7124 | ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7125 | /* this cookie must be deleted */ |
| 7126 | if (*prev == ':' && next == hdr_end) { |
| 7127 | /* whole header */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 7128 | delta = b_rep_blk(&res->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7129 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7130 | txn->hdr_idx.used--; |
| 7131 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7132 | cur_idx = old_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7133 | hdr_next += delta; |
| 7134 | http_msg_move_end(&txn->rsp, delta); |
| 7135 | /* note: while both invalid now, <next> and <hdr_end> |
| 7136 | * are still equal, so the for() will stop as expected. |
| 7137 | */ |
| 7138 | } else { |
| 7139 | /* just remove the value */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 7140 | int delta = del_hdr_value(&res->buf, &prev, next); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7141 | next = prev; |
| 7142 | hdr_end += delta; |
| 7143 | hdr_next += delta; |
| 7144 | cur_hdr->len += delta; |
| 7145 | http_msg_move_end(&txn->rsp, delta); |
| 7146 | } |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7147 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7148 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7149 | /* and go on with next cookie */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7150 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7151 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7152 | /* replace bytes val_beg->val_end with the cookie name associated |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7153 | * with this server since we know it. |
| 7154 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 7155 | delta = b_rep_blk(&res->buf, val_beg, val_end, srv->cookie, srv->cklen); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7156 | next += delta; |
| 7157 | hdr_end += delta; |
| 7158 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7159 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7160 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7161 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7162 | txn->flags &= ~TX_SCK_MASK; |
| 7163 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7164 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7165 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7166 | /* insert the cookie name associated with this server |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7167 | * before existing cookie, and insert a delimiter between them.. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7168 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 7169 | delta = b_rep_blk(&res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7170 | next += delta; |
| 7171 | hdr_end += delta; |
| 7172 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7173 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7174 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7175 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7176 | val_beg[srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7177 | txn->flags &= ~TX_SCK_MASK; |
| 7178 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7179 | } |
| 7180 | } |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 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 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7188 | } |
| 7189 | |
| 7190 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7191 | /* |
Willy Tarreau | 0ad8e0d | 2017-12-22 15:03:36 +0100 | [diff] [blame] | 7192 | * Parses the Cache-Control and Pragma request header fields to determine if |
| 7193 | * the request may be served from the cache and/or if it is cacheable. Updates |
| 7194 | * s->txn->flags. |
| 7195 | */ |
| 7196 | void check_request_for_cacheability(struct stream *s, struct channel *chn) |
| 7197 | { |
| 7198 | struct http_txn *txn = s->txn; |
| 7199 | char *p1, *p2; |
| 7200 | char *cur_ptr, *cur_end, *cur_next; |
| 7201 | int pragma_found; |
| 7202 | int cc_found; |
| 7203 | int cur_idx; |
| 7204 | |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 7205 | if (IS_HTX_STRM(s)) |
| 7206 | return htx_check_request_for_cacheability(s, chn); |
| 7207 | |
Willy Tarreau | 0ad8e0d | 2017-12-22 15:03:36 +0100 | [diff] [blame] | 7208 | if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE) |
| 7209 | return; /* nothing more to do here */ |
| 7210 | |
| 7211 | cur_idx = 0; |
| 7212 | pragma_found = cc_found = 0; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 7213 | cur_next = ci_head(chn) + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 0ad8e0d | 2017-12-22 15:03:36 +0100 | [diff] [blame] | 7214 | |
| 7215 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 7216 | struct hdr_idx_elem *cur_hdr; |
| 7217 | int val; |
| 7218 | |
| 7219 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 7220 | cur_ptr = cur_next; |
| 7221 | cur_end = cur_ptr + cur_hdr->len; |
| 7222 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7223 | |
| 7224 | /* We have one full header between cur_ptr and cur_end, and the |
| 7225 | * next header starts at cur_next. |
| 7226 | */ |
| 7227 | |
| 7228 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 7229 | if (val) { |
| 7230 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 7231 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 7232 | pragma_found = 1; |
| 7233 | continue; |
| 7234 | } |
| 7235 | } |
| 7236 | |
William Lallemand | 8a16fe0 | 2018-05-22 11:04:33 +0200 | [diff] [blame] | 7237 | /* Don't use the cache and don't try to store if we found the |
| 7238 | * Authorization header */ |
| 7239 | val = http_header_match2(cur_ptr, cur_end, "Authorization", 13); |
| 7240 | if (val) { |
| 7241 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 7242 | txn->flags |= TX_CACHE_IGNORE; |
| 7243 | continue; |
| 7244 | } |
| 7245 | |
Willy Tarreau | 0ad8e0d | 2017-12-22 15:03:36 +0100 | [diff] [blame] | 7246 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 7247 | if (!val) |
| 7248 | continue; |
| 7249 | |
| 7250 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 7251 | cc_found = 1; |
| 7252 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
| 7253 | |
| 7254 | if (p1 >= cur_end) /* no more info */ |
| 7255 | continue; |
| 7256 | |
| 7257 | /* p1 is at the beginning of the value */ |
| 7258 | p2 = p1; |
| 7259 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
| 7260 | p2++; |
| 7261 | |
| 7262 | /* we have a complete value between p1 and p2. We don't check the |
| 7263 | * values after max-age, max-stale nor min-fresh, we simply don't |
| 7264 | * use the cache when they're specified. |
| 7265 | */ |
| 7266 | if (((p2 - p1 == 7) && strncasecmp(p1, "max-age", 7) == 0) || |
| 7267 | ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) || |
| 7268 | ((p2 - p1 == 9) && strncasecmp(p1, "max-stale", 9) == 0) || |
| 7269 | ((p2 - p1 == 9) && strncasecmp(p1, "min-fresh", 9) == 0)) { |
| 7270 | txn->flags |= TX_CACHE_IGNORE; |
| 7271 | continue; |
| 7272 | } |
| 7273 | |
| 7274 | if ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) { |
| 7275 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 7276 | continue; |
| 7277 | } |
| 7278 | } |
| 7279 | |
| 7280 | /* RFC7234#5.4: |
| 7281 | * When the Cache-Control header field is also present and |
| 7282 | * understood in a request, Pragma is ignored. |
| 7283 | * When the Cache-Control header field is not present in a |
| 7284 | * request, caches MUST consider the no-cache request |
| 7285 | * pragma-directive as having the same effect as if |
| 7286 | * "Cache-Control: no-cache" were present. |
| 7287 | */ |
| 7288 | if (!cc_found && pragma_found) |
| 7289 | txn->flags |= TX_CACHE_IGNORE; |
| 7290 | } |
| 7291 | |
| 7292 | /* |
Willy Tarreau | d3900cc | 2017-12-22 15:35:11 +0100 | [diff] [blame] | 7293 | * Check if response is cacheable or not. Updates s->txn->flags. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7294 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7295 | void check_response_for_cacheability(struct stream *s, struct channel *rtr) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7296 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7297 | struct http_txn *txn = s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7298 | char *p1, *p2; |
| 7299 | |
| 7300 | char *cur_ptr, *cur_end, *cur_next; |
| 7301 | int cur_idx; |
| 7302 | |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 7303 | |
| 7304 | if (IS_HTX_STRM(s)) |
| 7305 | return htx_check_response_for_cacheability(s, rtr); |
| 7306 | |
Willy Tarreau | 12b32f2 | 2017-12-21 16:08:09 +0100 | [diff] [blame] | 7307 | if (txn->status < 200) { |
| 7308 | /* do not try to cache interim responses! */ |
| 7309 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7310 | return; |
Willy Tarreau | 12b32f2 | 2017-12-21 16:08:09 +0100 | [diff] [blame] | 7311 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7312 | |
| 7313 | /* Iterate through the headers. |
| 7314 | * we start with the start line. |
| 7315 | */ |
| 7316 | cur_idx = 0; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 7317 | cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7318 | |
| 7319 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 7320 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7321 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7322 | |
| 7323 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 7324 | cur_ptr = cur_next; |
| 7325 | cur_end = cur_ptr + cur_hdr->len; |
| 7326 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7327 | |
| 7328 | /* We have one full header between cur_ptr and cur_end, and the |
Willy Tarreau | d3900cc | 2017-12-22 15:35:11 +0100 | [diff] [blame] | 7329 | * next header starts at cur_next. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7330 | */ |
| 7331 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7332 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 7333 | if (val) { |
| 7334 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 7335 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 7336 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 7337 | return; |
| 7338 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7339 | } |
| 7340 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7341 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 7342 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7343 | continue; |
| 7344 | |
| 7345 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 7346 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7347 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7348 | |
| 7349 | if (p1 >= cur_end) /* no more info */ |
| 7350 | continue; |
| 7351 | |
| 7352 | /* p1 is at the beginning of the value */ |
| 7353 | p2 = p1; |
| 7354 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 7355 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7356 | p2++; |
| 7357 | |
| 7358 | /* we have a complete value between p1 and p2 */ |
| 7359 | if (p2 < cur_end && *p2 == '=') { |
Willy Tarreau | d3900cc | 2017-12-22 15:35:11 +0100 | [diff] [blame] | 7360 | if (((cur_end - p2) > 1 && (p2 - p1 == 7) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 7361 | ((cur_end - p2) > 1 && (p2 - p1 == 8) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
| 7362 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 7363 | continue; |
| 7364 | } |
| 7365 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7366 | /* we have something of the form no-cache="set-cookie" */ |
| 7367 | if ((cur_end - p1 >= 21) && |
| 7368 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 7369 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7370 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7371 | continue; |
| 7372 | } |
| 7373 | |
| 7374 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 7375 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
Willy Tarreau | 5b15f90 | 2013-07-04 12:46:56 +0200 | [diff] [blame] | 7376 | ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) || |
Willy Tarreau | d3900cc | 2017-12-22 15:35:11 +0100 | [diff] [blame] | 7377 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7378 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7379 | return; |
| 7380 | } |
| 7381 | |
| 7382 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7383 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7384 | continue; |
| 7385 | } |
| 7386 | } |
| 7387 | } |
| 7388 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 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 | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7402 | struct http_msg *msg = &txn->req; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 7403 | const char *uri = ci_head(msg->chn)+ msg->sl.rq.u; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7404 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7405 | if (!uri_auth) |
| 7406 | return 0; |
| 7407 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7408 | 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] | 7409 | return 0; |
| 7410 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7411 | /* check URI size */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7412 | if (uri_auth->uri_len > msg->sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7413 | return 0; |
| 7414 | |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 7415 | if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7416 | return 0; |
| 7417 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7418 | return 1; |
| 7419 | } |
| 7420 | |
Willy Tarreau | 7ccdd8d | 2018-09-07 14:01:39 +0200 | [diff] [blame] | 7421 | /* Append the description of what is present in error snapshot <es> into <out>. |
| 7422 | * The description must be small enough to always fit in a trash. The output |
| 7423 | * buffer may be the trash so the trash must not be used inside this function. |
| 7424 | */ |
| 7425 | void http_show_error_snapshot(struct buffer *out, const struct error_snapshot *es) |
| 7426 | { |
Christopher Faulet | ed26fb8 | 2018-11-29 22:53:30 +0100 | [diff] [blame] | 7427 | chunk_appendf(out, |
Willy Tarreau | 7ccdd8d | 2018-09-07 14:01:39 +0200 | [diff] [blame] | 7428 | " stream #%d, stream flags 0x%08x, tx flags 0x%08x\n" |
| 7429 | " HTTP msg state %s(%d), msg flags 0x%08x\n" |
| 7430 | " HTTP chunk len %lld bytes, HTTP body len %lld bytes, channel flags 0x%08x :\n", |
| 7431 | es->ctx.http.sid, es->ctx.http.s_flags, es->ctx.http.t_flags, |
| 7432 | h1_msg_state_str(es->ctx.http.state), es->ctx.http.state, |
| 7433 | es->ctx.http.m_flags, es->ctx.http.m_clen, |
| 7434 | es->ctx.http.m_blen, es->ctx.http.b_flags); |
| 7435 | } |
| 7436 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7437 | /* |
| 7438 | * Capture a bad request or response and archive it in the proxy's structure. |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7439 | * By default it tries to report the error position as msg->err_pos. However if |
| 7440 | * this one is not set, it will then report msg->next, which is the last known |
| 7441 | * parsing point. The function is able to deal with wrapping buffers. It always |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 7442 | * displays buffers as a contiguous area starting at buf->p. The direction is |
| 7443 | * determined thanks to the channel's flags. |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7444 | */ |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 7445 | void http_capture_bad_message(struct proxy *proxy, struct stream *s, |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 7446 | struct http_msg *msg, |
Willy Tarreau | 0da5b3b | 2017-09-21 09:30:46 +0200 | [diff] [blame] | 7447 | enum h1_state state, struct proxy *other_end) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7448 | { |
Willy Tarreau | ef3ca73 | 2018-09-07 15:47:35 +0200 | [diff] [blame] | 7449 | union error_snapshot_ctx ctx; |
| 7450 | long ofs; |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 7451 | |
Willy Tarreau | ef3ca73 | 2018-09-07 15:47:35 +0200 | [diff] [blame] | 7452 | /* http-specific part now */ |
| 7453 | ctx.http.sid = s->uniq_id; |
| 7454 | ctx.http.state = state; |
| 7455 | ctx.http.b_flags = msg->chn->flags; |
| 7456 | ctx.http.s_flags = s->flags; |
| 7457 | ctx.http.t_flags = s->txn->flags; |
| 7458 | ctx.http.m_flags = msg->flags; |
| 7459 | ctx.http.m_clen = msg->chunk_len; |
| 7460 | ctx.http.m_blen = msg->body_len; |
Willy Tarreau | 7480f32 | 2018-09-06 19:41:22 +0200 | [diff] [blame] | 7461 | |
Willy Tarreau | ef3ca73 | 2018-09-07 15:47:35 +0200 | [diff] [blame] | 7462 | ofs = msg->chn->total - ci_data(msg->chn); |
| 7463 | if (ofs < 0) |
| 7464 | ofs = 0; |
Willy Tarreau | 0b5b480 | 2018-09-07 13:49:44 +0200 | [diff] [blame] | 7465 | |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 7466 | proxy_capture_error(proxy, !!(msg->chn->flags & CF_ISRESP), |
Willy Tarreau | ef3ca73 | 2018-09-07 15:47:35 +0200 | [diff] [blame] | 7467 | other_end, s->target, |
| 7468 | strm_sess(s), &msg->chn->buf, |
| 7469 | ofs, co_data(msg->chn), |
| 7470 | (msg->err_pos >= 0) ? msg->err_pos : msg->next, |
| 7471 | &ctx, http_show_error_snapshot); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7472 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7473 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7474 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 7475 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 7476 | * performed over the whole headers. Otherwise it must contain a valid header |
| 7477 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 7478 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 7479 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 7480 | * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 7481 | * -1. The value fetch stops at commas, so this function is suited for use with |
| 7482 | * list headers. |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7483 | * 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] | 7484 | */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 7485 | unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen, |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7486 | struct hdr_idx *idx, int occ, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7487 | struct hdr_ctx *ctx, char **vptr, size_t *vlen) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7488 | { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7489 | struct hdr_ctx local_ctx; |
| 7490 | char *ptr_hist[MAX_HDR_HISTORY]; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7491 | unsigned int len_hist[MAX_HDR_HISTORY]; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7492 | unsigned int hist_ptr; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7493 | int found; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7494 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7495 | if (!ctx) { |
| 7496 | local_ctx.idx = 0; |
| 7497 | ctx = &local_ctx; |
| 7498 | } |
| 7499 | |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7500 | if (occ >= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7501 | /* search from the beginning */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 7502 | while (http_find_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) { |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7503 | occ--; |
| 7504 | if (occ <= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7505 | *vptr = ctx->line + ctx->val; |
| 7506 | *vlen = ctx->vlen; |
| 7507 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7508 | } |
| 7509 | } |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7510 | return 0; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7511 | } |
| 7512 | |
| 7513 | /* negative occurrence, we scan all the list then walk back */ |
| 7514 | if (-occ > MAX_HDR_HISTORY) |
| 7515 | return 0; |
| 7516 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7517 | found = hist_ptr = 0; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 7518 | while (http_find_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7519 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 7520 | len_hist[hist_ptr] = ctx->vlen; |
| 7521 | if (++hist_ptr >= MAX_HDR_HISTORY) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7522 | hist_ptr = 0; |
| 7523 | found++; |
| 7524 | } |
| 7525 | if (-occ > found) |
| 7526 | return 0; |
| 7527 | /* OK now we have the last occurrence in [hist_ptr-1], and we need to |
Willy Tarreau | 67dad27 | 2013-06-12 22:27:44 +0200 | [diff] [blame] | 7528 | * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have |
| 7529 | * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ] |
| 7530 | * to remain in the 0..9 range. |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7531 | */ |
Willy Tarreau | 67dad27 | 2013-06-12 22:27:44 +0200 | [diff] [blame] | 7532 | hist_ptr += occ + MAX_HDR_HISTORY; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7533 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 7534 | hist_ptr -= MAX_HDR_HISTORY; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7535 | *vptr = ptr_hist[hist_ptr]; |
| 7536 | *vlen = len_hist[hist_ptr]; |
| 7537 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7538 | } |
| 7539 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 7540 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 7541 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 7542 | * performed over the whole headers. Otherwise it must contain a valid header |
| 7543 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 7544 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 7545 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 7546 | * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is |
| 7547 | * -1. This function differs from http_get_hdr() in that it only returns full |
| 7548 | * line header values and does not stop at commas. |
| 7549 | * The return value is 0 if nothing was found, or non-zero otherwise. |
| 7550 | */ |
| 7551 | unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen, |
| 7552 | struct hdr_idx *idx, int occ, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7553 | struct hdr_ctx *ctx, char **vptr, size_t *vlen) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 7554 | { |
| 7555 | struct hdr_ctx local_ctx; |
| 7556 | char *ptr_hist[MAX_HDR_HISTORY]; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7557 | unsigned int len_hist[MAX_HDR_HISTORY]; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 7558 | unsigned int hist_ptr; |
| 7559 | int found; |
| 7560 | |
| 7561 | if (!ctx) { |
| 7562 | local_ctx.idx = 0; |
| 7563 | ctx = &local_ctx; |
| 7564 | } |
| 7565 | |
| 7566 | if (occ >= 0) { |
| 7567 | /* search from the beginning */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 7568 | while (http_find_full_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) { |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 7569 | occ--; |
| 7570 | if (occ <= 0) { |
| 7571 | *vptr = ctx->line + ctx->val; |
| 7572 | *vlen = ctx->vlen; |
| 7573 | return 1; |
| 7574 | } |
| 7575 | } |
| 7576 | return 0; |
| 7577 | } |
| 7578 | |
| 7579 | /* negative occurrence, we scan all the list then walk back */ |
| 7580 | if (-occ > MAX_HDR_HISTORY) |
| 7581 | return 0; |
| 7582 | |
| 7583 | found = hist_ptr = 0; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 7584 | while (http_find_full_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) { |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 7585 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 7586 | len_hist[hist_ptr] = ctx->vlen; |
| 7587 | if (++hist_ptr >= MAX_HDR_HISTORY) |
| 7588 | hist_ptr = 0; |
| 7589 | found++; |
| 7590 | } |
| 7591 | if (-occ > found) |
| 7592 | return 0; |
Nenad Merdanovic | 69ad4b9 | 2016-03-29 13:14:30 +0200 | [diff] [blame] | 7593 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 7594 | /* OK now we have the last occurrence in [hist_ptr-1], and we need to |
Nenad Merdanovic | 69ad4b9 | 2016-03-29 13:14:30 +0200 | [diff] [blame] | 7595 | * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have |
| 7596 | * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ] |
| 7597 | * to remain in the 0..9 range. |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 7598 | */ |
Nenad Merdanovic | 69ad4b9 | 2016-03-29 13:14:30 +0200 | [diff] [blame] | 7599 | hist_ptr += occ + MAX_HDR_HISTORY; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 7600 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 7601 | hist_ptr -= MAX_HDR_HISTORY; |
| 7602 | *vptr = ptr_hist[hist_ptr]; |
| 7603 | *vlen = len_hist[hist_ptr]; |
| 7604 | return 1; |
| 7605 | } |
| 7606 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 7607 | /* |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 7608 | * Print a debug line with a header. Always stop at the first CR or LF char, |
| 7609 | * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an |
| 7610 | * arrow is printed after the line which contains the pointer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7611 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7612 | void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7613 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 7614 | struct session *sess = strm_sess(s); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7615 | int max; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 7616 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7617 | chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id, |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7618 | dir, |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 7619 | objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1, |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 7620 | objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1); |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 7621 | |
| 7622 | for (max = 0; start + max < end; max++) |
| 7623 | if (start[max] == '\r' || start[max] == '\n') |
| 7624 | break; |
| 7625 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7626 | UBOUND(max, trash.size - trash.data - 3); |
| 7627 | trash.data += strlcpy2(trash.area + trash.data, start, max + 1); |
| 7628 | trash.area[trash.data++] = '\n'; |
| 7629 | shut_your_big_mouth_gcc(write(1, trash.area, trash.data)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7630 | } |
| 7631 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7632 | |
| 7633 | /* Allocate a new HTTP transaction for stream <s> unless there is one already. |
| 7634 | * The hdr_idx is allocated as well. In case of allocation failure, everything |
| 7635 | * allocated is freed and NULL is returned. Otherwise the new transaction is |
| 7636 | * assigned to the stream and returned. |
| 7637 | */ |
| 7638 | struct http_txn *http_alloc_txn(struct stream *s) |
| 7639 | { |
| 7640 | struct http_txn *txn = s->txn; |
| 7641 | |
| 7642 | if (txn) |
| 7643 | return txn; |
| 7644 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7645 | txn = pool_alloc(pool_head_http_txn); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7646 | if (!txn) |
| 7647 | return txn; |
| 7648 | |
| 7649 | txn->hdr_idx.size = global.tune.max_http_hdr; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7650 | txn->hdr_idx.v = pool_alloc(pool_head_hdr_idx); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7651 | if (!txn->hdr_idx.v) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7652 | pool_free(pool_head_http_txn, txn); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7653 | return NULL; |
| 7654 | } |
| 7655 | |
| 7656 | s->txn = txn; |
| 7657 | return txn; |
| 7658 | } |
| 7659 | |
Thierry FOURNIER | fd50f0b | 2015-09-25 18:53:18 +0200 | [diff] [blame] | 7660 | void http_txn_reset_req(struct http_txn *txn) |
| 7661 | { |
| 7662 | txn->req.flags = 0; |
| 7663 | txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */ |
| 7664 | txn->req.next = 0; |
| 7665 | txn->req.chunk_len = 0LL; |
| 7666 | txn->req.body_len = 0LL; |
| 7667 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 7668 | } |
| 7669 | |
| 7670 | void http_txn_reset_res(struct http_txn *txn) |
| 7671 | { |
| 7672 | txn->rsp.flags = 0; |
| 7673 | txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */ |
| 7674 | txn->rsp.next = 0; |
| 7675 | txn->rsp.chunk_len = 0LL; |
| 7676 | txn->rsp.body_len = 0LL; |
| 7677 | txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */ |
| 7678 | } |
| 7679 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7680 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7681 | * Initialize a new HTTP transaction for stream <s>. It is assumed that all |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7682 | * the required fields are properly allocated and that we only need to (re)init |
| 7683 | * them. This should be used before processing any new request. |
| 7684 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7685 | void http_init_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7686 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7687 | struct http_txn *txn = s->txn; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 7688 | struct proxy *fe = strm_fe(s); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 7689 | struct conn_stream *cs = objt_cs(s->si[0].end); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7690 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 7691 | txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST) |
| 7692 | ? (TX_NOT_FIRST|TX_WAIT_NEXT_RQ) |
| 7693 | : 0); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7694 | txn->status = -1; |
| 7695 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7696 | txn->cookie_first_date = 0; |
| 7697 | txn->cookie_last_date = 0; |
| 7698 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7699 | txn->srv_cookie = NULL; |
| 7700 | txn->cli_cookie = NULL; |
| 7701 | txn->uri = NULL; |
| 7702 | |
Thierry FOURNIER | fd50f0b | 2015-09-25 18:53:18 +0200 | [diff] [blame] | 7703 | http_txn_reset_req(txn); |
| 7704 | http_txn_reset_res(txn); |
| 7705 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7706 | txn->req.chn = &s->req; |
| 7707 | txn->rsp.chn = &s->res; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 7708 | |
| 7709 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7710 | |
| 7711 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 7712 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 7713 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 7714 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7715 | if (txn->hdr_idx.v) |
| 7716 | hdr_idx_init(&txn->hdr_idx); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 7717 | |
| 7718 | vars_init(&s->vars_txn, SCOPE_TXN); |
| 7719 | vars_init(&s->vars_reqres, SCOPE_REQ); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7720 | } |
| 7721 | |
| 7722 | /* to be used at the end of a transaction */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7723 | void http_end_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7724 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7725 | struct http_txn *txn = s->txn; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 7726 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7727 | |
| 7728 | /* these ones will have been dynamically allocated */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7729 | pool_free(pool_head_requri, txn->uri); |
| 7730 | pool_free(pool_head_capture, txn->cli_cookie); |
| 7731 | pool_free(pool_head_capture, txn->srv_cookie); |
| 7732 | pool_free(pool_head_uniqueid, s->unique_id); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 7733 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 7734 | s->unique_id = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7735 | txn->uri = NULL; |
| 7736 | txn->srv_cookie = NULL; |
| 7737 | txn->cli_cookie = NULL; |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7738 | |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 7739 | if (s->req_cap) { |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7740 | struct cap_hdr *h; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7741 | for (h = fe->req_cap; h; h = h->next) |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7742 | pool_free(h->pool, s->req_cap[h->index]); |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 7743 | memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *)); |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7744 | } |
| 7745 | |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 7746 | if (s->res_cap) { |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7747 | struct cap_hdr *h; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7748 | for (h = fe->rsp_cap; h; h = h->next) |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7749 | pool_free(h->pool, s->res_cap[h->index]); |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 7750 | memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7751 | } |
| 7752 | |
Willy Tarreau | cda7f3f | 2018-10-28 13:44:36 +0100 | [diff] [blame] | 7753 | if (!LIST_ISEMPTY(&s->vars_txn.head)) |
| 7754 | vars_prune(&s->vars_txn, s->sess, s); |
| 7755 | if (!LIST_ISEMPTY(&s->vars_reqres.head)) |
| 7756 | vars_prune(&s->vars_reqres, s->sess, s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7757 | } |
| 7758 | |
| 7759 | /* to be used at the end of a transaction to prepare a new one */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7760 | void http_reset_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7761 | { |
| 7762 | http_end_txn(s); |
| 7763 | http_init_txn(s); |
| 7764 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 7765 | /* reinitialise the current rule list pointer to NULL. We are sure that |
| 7766 | * any rulelist match the NULL pointer. |
| 7767 | */ |
| 7768 | s->current_rule_list = NULL; |
| 7769 | |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 7770 | s->be = strm_fe(s); |
| 7771 | s->logs.logwait = strm_fe(s)->to_log; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 7772 | s->logs.level = 0; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7773 | stream_del_srv_conn(s); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7774 | s->target = NULL; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 7775 | /* re-init store persistence */ |
| 7776 | s->store_count = 0; |
Willy Tarreau | 90a7c03 | 2018-09-05 16:21:29 +0200 | [diff] [blame] | 7777 | s->uniq_id = HA_ATOMIC_XADD(&global.req_count, 1); |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 7778 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7779 | s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */ |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7780 | |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 7781 | /* We must trim any excess data from the response buffer, because we |
| 7782 | * may have blocked an invalid response from a server that we don't |
Joseph Herlant | 5ba8025 | 2018-11-15 09:25:36 -0800 | [diff] [blame] | 7783 | * want to accidently forward once we disable the analysers, nor do |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 7784 | * we want those data to come along with next response. A typical |
| 7785 | * example of such data would be from a buggy server responding to |
| 7786 | * a HEAD with some data, or sending more than the advertised |
| 7787 | * content-length. |
| 7788 | */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 7789 | if (unlikely(ci_data(&s->res))) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 7790 | b_set_data(&s->res.buf, co_data(&s->res)); |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 7791 | |
Christopher Faulet | c0c672a | 2017-03-28 11:51:33 +0200 | [diff] [blame] | 7792 | /* Now we can realign the response buffer */ |
Willy Tarreau | d5b343b | 2018-06-06 06:42:46 +0200 | [diff] [blame] | 7793 | c_realign_if_empty(&s->res); |
Christopher Faulet | c0c672a | 2017-03-28 11:51:33 +0200 | [diff] [blame] | 7794 | |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 7795 | s->req.rto = strm_fe(s)->timeout.client; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7796 | s->req.wto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7797 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7798 | s->res.rto = TICK_ETERNITY; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 7799 | s->res.wto = strm_fe(s)->timeout.client; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7800 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7801 | s->req.rex = TICK_ETERNITY; |
| 7802 | s->req.wex = TICK_ETERNITY; |
| 7803 | s->req.analyse_exp = TICK_ETERNITY; |
| 7804 | s->res.rex = TICK_ETERNITY; |
| 7805 | s->res.wex = TICK_ETERNITY; |
| 7806 | s->res.analyse_exp = TICK_ETERNITY; |
Hongbo Long | e39683c | 2017-03-10 18:41:51 +0100 | [diff] [blame] | 7807 | s->si[1].hcto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7808 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7809 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7810 | /* This function executes one of the set-{method,path,query,uri} actions. It |
| 7811 | * takes the string from the variable 'replace' with length 'len', then modifies |
| 7812 | * the relevant part of the request line accordingly. Then it updates various |
| 7813 | * pointers to the next elements which were moved, and the total buffer length. |
| 7814 | * It finds the action to be performed in p[2], previously filled by function |
| 7815 | * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal |
| 7816 | * error, though this can be revisited when this code is finally exploited. |
| 7817 | * |
| 7818 | * 'action' can be '0' to replace method, '1' to replace path, '2' to replace |
| 7819 | * query string and 3 to replace uri. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7820 | * |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7821 | * In query string case, the mark question '?' must be set at the start of the |
| 7822 | * string by the caller, event if the replacement query string is empty. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7823 | */ |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7824 | int http_replace_req_line(int action, const char *replace, int len, |
| 7825 | struct proxy *px, struct stream *s) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7826 | { |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7827 | struct http_txn *txn = s->txn; |
| 7828 | char *cur_ptr, *cur_end; |
| 7829 | int offset = 0; |
| 7830 | int delta; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7831 | |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 7832 | if (IS_HTX_STRM(s)) |
| 7833 | return htx_req_replace_stline(action, replace, len, px, s); |
| 7834 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7835 | switch (action) { |
| 7836 | case 0: // method |
| 7837 | cur_ptr = ci_head(&s->req); |
| 7838 | cur_end = cur_ptr + txn->req.sl.rq.m_l; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7839 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7840 | /* adjust req line offsets and lengths */ |
| 7841 | delta = len - offset - (cur_end - cur_ptr); |
| 7842 | txn->req.sl.rq.m_l += delta; |
| 7843 | txn->req.sl.rq.u += delta; |
| 7844 | txn->req.sl.rq.v += delta; |
| 7845 | break; |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 7846 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7847 | case 1: // path |
| 7848 | cur_ptr = http_txn_get_path(txn); |
| 7849 | if (!cur_ptr) |
| 7850 | cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7851 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7852 | cur_end = cur_ptr; |
| 7853 | while (cur_end < ci_head(&s->req) + txn->req.sl.rq.u + txn->req.sl.rq.u_l && *cur_end != '?') |
| 7854 | cur_end++; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7855 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7856 | /* adjust req line offsets and lengths */ |
| 7857 | delta = len - offset - (cur_end - cur_ptr); |
| 7858 | txn->req.sl.rq.u_l += delta; |
| 7859 | txn->req.sl.rq.v += delta; |
| 7860 | break; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7861 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7862 | case 2: // query |
| 7863 | offset = 1; |
| 7864 | cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u; |
| 7865 | cur_end = cur_ptr + txn->req.sl.rq.u_l; |
| 7866 | while (cur_ptr < cur_end && *cur_ptr != '?') |
| 7867 | cur_ptr++; |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 7868 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7869 | /* skip the question mark or indicate that we must insert it |
| 7870 | * (but only if the format string is not empty then). |
| 7871 | */ |
| 7872 | if (cur_ptr < cur_end) |
| 7873 | cur_ptr++; |
| 7874 | else if (len > 1) |
| 7875 | offset = 0; |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 7876 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7877 | /* adjust req line offsets and lengths */ |
| 7878 | delta = len - offset - (cur_end - cur_ptr); |
| 7879 | txn->req.sl.rq.u_l += delta; |
| 7880 | txn->req.sl.rq.v += delta; |
| 7881 | break; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7882 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7883 | case 3: // uri |
| 7884 | cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u; |
| 7885 | cur_end = cur_ptr + txn->req.sl.rq.u_l; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7886 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7887 | /* adjust req line offsets and lengths */ |
| 7888 | delta = len - offset - (cur_end - cur_ptr); |
| 7889 | txn->req.sl.rq.u_l += delta; |
| 7890 | txn->req.sl.rq.v += delta; |
| 7891 | break; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7892 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7893 | default: |
| 7894 | return -1; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7895 | } |
| 7896 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7897 | /* commit changes and adjust end of message */ |
| 7898 | delta = b_rep_blk(&s->req.buf, cur_ptr, cur_end, replace + offset, len - offset); |
| 7899 | txn->req.sl.rq.l += delta; |
| 7900 | txn->hdr_idx.v[0].len += delta; |
| 7901 | http_msg_move_end(&txn->req, delta); |
| 7902 | return 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7903 | } |
| 7904 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7905 | /* This function replace the HTTP status code and the associated message. The |
| 7906 | * variable <status> contains the new status code. This function never fails. |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7907 | */ |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7908 | void http_set_status(unsigned int status, const char *reason, struct stream *s) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7909 | { |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7910 | struct http_txn *txn = s->txn; |
| 7911 | char *cur_ptr, *cur_end; |
| 7912 | int delta; |
| 7913 | char *res; |
| 7914 | int c_l; |
| 7915 | const char *msg = reason; |
| 7916 | int msg_len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7917 | |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 7918 | if (IS_HTX_STRM(s)) |
| 7919 | return htx_res_set_status(status, reason, s); |
| 7920 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7921 | chunk_reset(&trash); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7922 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7923 | res = ultoa_o(status, trash.area, trash.size); |
| 7924 | c_l = res - trash.area; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7925 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7926 | trash.area[c_l] = ' '; |
| 7927 | trash.data = c_l + 1; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7928 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7929 | /* Do we have a custom reason format string? */ |
| 7930 | if (msg == NULL) |
| 7931 | msg = http_get_reason(status); |
| 7932 | msg_len = strlen(msg); |
| 7933 | strncpy(&trash.area[trash.data], msg, trash.size - trash.data); |
| 7934 | trash.data += msg_len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7935 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7936 | cur_ptr = ci_head(&s->res) + txn->rsp.sl.st.c; |
| 7937 | cur_end = ci_head(&s->res) + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l; |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7938 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7939 | /* commit changes and adjust message */ |
| 7940 | delta = b_rep_blk(&s->res.buf, cur_ptr, cur_end, trash.area, |
| 7941 | trash.data); |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 7942 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7943 | /* adjust res line offsets and lengths */ |
| 7944 | txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l; |
| 7945 | txn->rsp.sl.st.c_l = c_l; |
| 7946 | txn->rsp.sl.st.r_l = msg_len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7947 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7948 | delta = trash.data - (cur_end - cur_ptr); |
| 7949 | txn->rsp.sl.st.l += delta; |
| 7950 | txn->hdr_idx.v[0].len += delta; |
| 7951 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7952 | } |
| 7953 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7954 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 7955 | * Local variables: |
| 7956 | * c-indent-level: 8 |
| 7957 | * c-basic-offset: 8 |
| 7958 | * End: |
| 7959 | */ |