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 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 26 | #include <common/base64.h> |
Frédéric Lécaille | a41d531 | 2018-01-29 12:05:07 +0100 | [diff] [blame] | 27 | #include <common/cfgparse.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 28 | #include <common/chunk.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 29 | #include <common/compat.h> |
| 30 | #include <common/config.h> |
Willy Tarreau | a4cd1f5 | 2006-12-16 19:57:26 +0100 | [diff] [blame] | 31 | #include <common/debug.h> |
Willy Tarreau | afba57a | 2018-12-11 13:44:24 +0100 | [diff] [blame] | 32 | #include <common/h1.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 33 | #include <common/memory.h> |
| 34 | #include <common/mini-clist.h> |
| 35 | #include <common/standard.h> |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 36 | #include <common/ticks.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 37 | #include <common/time.h> |
| 38 | #include <common/uri_auth.h> |
| 39 | #include <common/version.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 40 | |
| 41 | #include <types/capture.h> |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 42 | #include <types/cli.h> |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 43 | #include <types/filters.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 44 | #include <types/global.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 45 | #include <types/stats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 46 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 47 | #include <proto/acl.h> |
Thierry FOURNIER | 322a124 | 2015-08-19 09:07:47 +0200 | [diff] [blame] | 48 | #include <proto/action.h> |
Willy Tarreau | 61612d4 | 2012-04-19 18:42:05 +0200 | [diff] [blame] | 49 | #include <proto/arg.h> |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 50 | #include <proto/auth.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 51 | #include <proto/backend.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 52 | #include <proto/channel.h> |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 53 | #include <proto/checks.h> |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 54 | #include <proto/cli.h> |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 55 | #include <proto/compression.h> |
Baptiste Assmann | 333939c | 2019-01-21 08:34:50 +0100 | [diff] [blame] | 56 | #include <proto/dns.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 57 | #include <proto/stats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 58 | #include <proto/fd.h> |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 59 | #include <proto/filters.h> |
Willy Tarreau | 03fa5df | 2010-05-24 21:02:37 +0200 | [diff] [blame] | 60 | #include <proto/frontend.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 61 | #include <proto/log.h> |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 62 | #include <proto/hdr_idx.h> |
Patrick Hemmer | e3faf02 | 2018-08-22 10:02:00 -0400 | [diff] [blame] | 63 | #include <proto/hlua.h> |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 64 | #include <proto/pattern.h> |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 65 | #include <proto/proto_tcp.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 66 | #include <proto/proto_http.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 67 | #include <proto/proxy.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 68 | #include <proto/queue.h> |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 69 | #include <proto/sample.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 70 | #include <proto/server.h> |
Olivier Houchard | 985f139 | 2018-11-30 17:31:52 +0100 | [diff] [blame] | 71 | #include <proto/session.h> |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 72 | #include <proto/stream.h> |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 73 | #include <proto/stream_interface.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 74 | #include <proto/task.h> |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 75 | #include <proto/pattern.h> |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 76 | #include <proto/vars.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 77 | |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 78 | /* This function handles a server error at the stream interface level. The |
| 79 | * 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] | 80 | * message is copied into the input buffer. |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 81 | * 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] | 82 | * in this buffer will be lost. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 83 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 84 | static void http_server_error(struct stream *s, struct stream_interface *si, |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 85 | int err, int finst, const struct buffer *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 86 | { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 87 | if (IS_HTX_STRM(s)) |
| 88 | return htx_server_error(s, si, err, finst, msg); |
| 89 | |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 90 | FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg)); |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 91 | channel_auto_read(si_oc(si)); |
| 92 | channel_abort(si_oc(si)); |
| 93 | channel_auto_close(si_oc(si)); |
| 94 | channel_erase(si_oc(si)); |
| 95 | channel_auto_close(si_ic(si)); |
| 96 | channel_auto_read(si_ic(si)); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 97 | if (msg) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 98 | co_inject(si_ic(si), msg->area, msg->data); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 99 | if (!(s->flags & SF_ERR_MASK)) |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 100 | s->flags |= err; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 101 | if (!(s->flags & SF_FINST_MASK)) |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 102 | s->flags |= finst; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 103 | } |
| 104 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 105 | /* This function returns the appropriate error location for the given stream |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 106 | * and message. |
| 107 | */ |
| 108 | |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 109 | struct buffer *http_error_message(struct stream *s) |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 110 | { |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 111 | const int msgnum = http_get_status_idx(s->txn->status); |
| 112 | |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 113 | if (IS_HTX_STRM(s)) |
| 114 | return htx_error_message(s); |
| 115 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 116 | if (s->be->errmsg[msgnum].area) |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 117 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 118 | else if (strm_fe(s)->errmsg[msgnum].area) |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 119 | return &strm_fe(s)->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 120 | else |
| 121 | return &http_err_chunks[msgnum]; |
| 122 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 123 | |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 124 | void |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 125 | http_reply_and_close(struct stream *s, short status, struct buffer *msg) |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 126 | { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 127 | if (IS_HTX_STRM(s)) |
| 128 | return htx_reply_and_close(s, status, msg); |
| 129 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 130 | s->txn->flags &= ~TX_WAIT_NEXT_RQ; |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 131 | FLT_STRM_CB(s, flt_http_reply(s, status, msg)); |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 132 | si_retnclose(&s->si[0], msg); |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 133 | } |
| 134 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 135 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 136 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 137 | * It is returned otherwise. |
| 138 | */ |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 139 | char *http_txn_get_path(const struct http_txn *txn) |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 140 | { |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 141 | struct ist ret; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 142 | |
Willy Tarreau | 9d9ccdb | 2018-10-28 20:13:12 +0100 | [diff] [blame] | 143 | if (!txn->req.chn->buf.size) |
| 144 | return NULL; |
| 145 | |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 146 | 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] | 147 | |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 148 | return ret.ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 149 | } |
| 150 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 151 | /* Returns a 302 for a redirectable request that reaches a server working in |
| 152 | * in redirect mode. This may only be called just after the stream interface |
| 153 | * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will |
| 154 | * follow normal proxy processing. NOTE: this function is designed to support |
| 155 | * being called once data are scheduled for forwarding. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 156 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 157 | void http_perform_server_redirect(struct stream *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 158 | { |
| 159 | struct http_txn *txn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 160 | struct server *srv; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 161 | char *path; |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 162 | int len, rewind; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 163 | |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 164 | if (IS_HTX_STRM(s)) |
| 165 | return htx_perform_server_redirect(s, si); |
| 166 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 167 | /* 1: create the response header */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 168 | trash.data = strlen(HTTP_302); |
| 169 | memcpy(trash.area, HTTP_302, trash.data); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 170 | |
Willy Tarreau | b05e48a | 2018-09-20 11:12:58 +0200 | [diff] [blame] | 171 | srv = __objt_server(s->target); |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 172 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 173 | /* 2: add the server's prefix */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 174 | if (trash.data + srv->rdr_len > trash.size) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 175 | return; |
| 176 | |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 177 | /* special prefix "/" means don't change URL */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 178 | if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 179 | memcpy(trash.area + trash.data, srv->rdr_pfx, srv->rdr_len); |
| 180 | trash.data += srv->rdr_len; |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 181 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 182 | |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 183 | /* 3: add the request URI. Since it was already forwarded, we need |
| 184 | * to temporarily rewind the buffer. |
| 185 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 186 | txn = s->txn; |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 187 | c_rew(&s->req, rewind = http_hdr_rewind(&txn->req)); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 188 | |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 189 | path = http_txn_get_path(txn); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 190 | 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] | 191 | |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 192 | c_adv(&s->req, rewind); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 193 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 194 | if (!path) |
| 195 | return; |
| 196 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 197 | if (trash.data + len > trash.size - 4) /* 4 for CRLF-CRLF */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 198 | return; |
| 199 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 200 | memcpy(trash.area + trash.data, path, len); |
| 201 | trash.data += len; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 202 | |
| 203 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 204 | memcpy(trash.area + trash.data, |
| 205 | "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 206 | trash.data += 29; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 207 | } else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 208 | memcpy(trash.area + trash.data, |
| 209 | "\r\nConnection: close\r\n\r\n", 23); |
| 210 | trash.data += 23; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 211 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 212 | |
| 213 | /* prepare to return without error. */ |
Willy Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 214 | si_shutr(si); |
| 215 | si_shutw(si); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 216 | si->err_type = SI_ET_NONE; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 217 | si->state = SI_ST_CLO; |
| 218 | |
| 219 | /* send the message */ |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 220 | txn->status = 302; |
| 221 | http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, &trash); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 222 | |
| 223 | /* 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] | 224 | srv_inc_sess_ctr(srv); |
Bhaskar Maddala | a20cb85 | 2014-02-03 16:26:46 -0500 | [diff] [blame] | 225 | srv_set_sess_last(srv); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 226 | } |
| 227 | |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 228 | /* Return the error message corresponding to si->err_type. It is assumed |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 229 | * that the server side is closed. Note that err_type is actually a |
| 230 | * bitmask, where almost only aborts may be cumulated with other |
| 231 | * values. We consider that aborted operations are more important |
| 232 | * than timeouts or errors due to the fact that nobody else in the |
| 233 | * logs might explain incomplete retries. All others should avoid |
| 234 | * being cumulated. It should normally not be possible to have multiple |
| 235 | * 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] | 236 | * Note that connection errors appearing on the second request of a keep-alive |
| 237 | * connection are not reported since this allows the client to retry. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 238 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 239 | void http_return_srv_error(struct stream *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 240 | { |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 241 | int err_type = si->err_type; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 242 | |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 243 | /* set s->txn->status for http_error_message(s) */ |
| 244 | s->txn->status = 503; |
| 245 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 246 | if (err_type & SI_ET_QUEUE_ABRT) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 247 | http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 248 | http_error_message(s)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 249 | else if (err_type & SI_ET_CONN_ABRT) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 250 | http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 251 | (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 252 | http_error_message(s)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 253 | else if (err_type & SI_ET_QUEUE_TO) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 254 | http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 255 | http_error_message(s)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 256 | else if (err_type & SI_ET_QUEUE_ERR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 257 | http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 258 | http_error_message(s)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 259 | else if (err_type & SI_ET_CONN_TO) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 260 | http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 261 | (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 262 | http_error_message(s)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 263 | else if (err_type & SI_ET_CONN_ERR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 264 | http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 265 | (s->flags & SF_SRV_REUSED) ? NULL : |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 266 | http_error_message(s)); |
Willy Tarreau | 2d400bb | 2012-05-14 12:11:47 +0200 | [diff] [blame] | 267 | else if (err_type & SI_ET_CONN_RES) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 268 | http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 269 | (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 270 | http_error_message(s)); |
| 271 | else { /* SI_ET_CONN_OTHER and others */ |
| 272 | s->txn->status = 500; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 273 | http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 274 | http_error_message(s)); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 275 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 276 | } |
| 277 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 278 | extern const char sess_term_cond[8]; |
| 279 | extern const char sess_fin_state[8]; |
| 280 | extern const char *monthname[12]; |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 281 | |
| 282 | DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn)); |
| 283 | DECLARE_POOL(pool_head_uniqueid, "uniqueid", UNIQUEID_LEN); |
| 284 | |
| 285 | struct pool_head *pool_head_requri = NULL; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 286 | struct pool_head *pool_head_capture = NULL; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 287 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 288 | /* |
| 289 | * Capture headers from message starting at <som> according to header list |
Willy Tarreau | 54da8db | 2014-06-13 16:11:48 +0200 | [diff] [blame] | 290 | * <cap_hdr>, and fill the <cap> pointers appropriately. |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 291 | */ |
Christopher Faulet | 10079f5 | 2018-10-03 15:17:28 +0200 | [diff] [blame] | 292 | void http_capture_headers(char *som, struct hdr_idx *idx, |
| 293 | char **cap, struct cap_hdr *cap_hdr) |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 294 | { |
| 295 | char *eol, *sol, *col, *sov; |
| 296 | int cur_idx; |
| 297 | struct cap_hdr *h; |
| 298 | int len; |
| 299 | |
| 300 | sol = som + hdr_idx_first_pos(idx); |
| 301 | cur_idx = hdr_idx_first_idx(idx); |
| 302 | |
| 303 | while (cur_idx) { |
| 304 | eol = sol + idx->v[cur_idx].len; |
| 305 | |
| 306 | col = sol; |
| 307 | while (col < eol && *col != ':') |
| 308 | col++; |
| 309 | |
| 310 | sov = col + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 311 | while (sov < eol && HTTP_IS_LWS(*sov)) |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 312 | sov++; |
| 313 | |
| 314 | for (h = cap_hdr; h; h = h->next) { |
Willy Tarreau | 54da8db | 2014-06-13 16:11:48 +0200 | [diff] [blame] | 315 | if (h->namelen && (h->namelen == col - sol) && |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 316 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 317 | if (cap[h->index] == NULL) |
| 318 | cap[h->index] = |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 319 | pool_alloc(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 320 | |
| 321 | if (cap[h->index] == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 322 | ha_alert("HTTP capture : out of memory.\n"); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 323 | continue; |
| 324 | } |
| 325 | |
| 326 | len = eol - sov; |
| 327 | if (len > h->len) |
| 328 | len = h->len; |
| 329 | |
| 330 | memcpy(cap[h->index], sov, len); |
| 331 | cap[h->index][len]=0; |
| 332 | } |
| 333 | } |
| 334 | sol = eol + idx->v[cur_idx].cr + 1; |
| 335 | cur_idx = idx->v[cur_idx].next; |
| 336 | } |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 337 | } |
| 338 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 339 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 340 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 341 | * nothing is done and 1 is returned. |
| 342 | */ |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 343 | int http_upgrade_v09_to_v10(struct http_txn *txn) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 344 | { |
| 345 | int delta; |
| 346 | char *cur_end; |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 347 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 348 | |
| 349 | if (msg->sl.rq.v_l != 0) |
| 350 | return 1; |
| 351 | |
Apollon Oikonomopoulos | 25a1522 | 2014-04-06 02:46:00 +0300 | [diff] [blame] | 352 | /* RFC 1945 allows only GET for HTTP/0.9 requests */ |
| 353 | if (txn->meth != HTTP_METH_GET) |
| 354 | return 0; |
| 355 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 356 | cur_end = ci_head(msg->chn) + msg->sl.rq.l; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 357 | |
| 358 | if (msg->sl.rq.u_l == 0) { |
Apollon Oikonomopoulos | 25a1522 | 2014-04-06 02:46:00 +0300 | [diff] [blame] | 359 | /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */ |
| 360 | return 0; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 361 | } |
| 362 | /* add HTTP version */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 363 | 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] | 364 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 365 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 366 | cur_end = (char *)http_parse_reqline(msg, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 367 | HTTP_MSG_RQMETH, |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 368 | ci_head(msg->chn), cur_end + 1, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 369 | NULL, NULL); |
| 370 | if (unlikely(!cur_end)) |
| 371 | return 0; |
| 372 | |
| 373 | /* we have a full HTTP/1.0 request now and we know that |
| 374 | * we have either a CR or an LF at <ptr>. |
| 375 | */ |
| 376 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 377 | return 1; |
| 378 | } |
| 379 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 380 | /* Parse the Connection: header of an HTTP request, looking for both "close" |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 381 | * and "keep-alive" values. If we already know that some headers may safely |
| 382 | * 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] | 383 | * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses) |
| 384 | * - 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] | 385 | * Presence of the "Upgrade" token is also checked and reported. |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 386 | * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was |
| 387 | * found, and TX_CON_*_SET is adjusted depending on what is left so only |
| 388 | * harmless combinations may be removed. Do not call that after changes have |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 389 | * been processed. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 390 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 391 | 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] | 392 | { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 393 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 394 | const char *hdr_val = "Connection"; |
| 395 | int hdr_len = 10; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 396 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 397 | if (txn->flags & TX_HDR_CONN_PRS) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 398 | return; |
| 399 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 400 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 401 | hdr_val = "Proxy-Connection"; |
| 402 | hdr_len = 16; |
| 403 | } |
| 404 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 405 | ctx.idx = 0; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 406 | txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET); |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 407 | 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] | 408 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 409 | txn->flags |= TX_HDR_CONN_KAL; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 410 | if (to_del & 2) |
| 411 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 412 | else |
| 413 | txn->flags |= TX_CON_KAL_SET; |
| 414 | } |
| 415 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 416 | txn->flags |= TX_HDR_CONN_CLO; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 417 | if (to_del & 1) |
| 418 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 419 | else |
| 420 | txn->flags |= TX_CON_CLO_SET; |
| 421 | } |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 422 | else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) { |
| 423 | txn->flags |= TX_HDR_CONN_UPG; |
| 424 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 425 | } |
| 426 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 427 | txn->flags |= TX_HDR_CONN_PRS; |
| 428 | return; |
| 429 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 430 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 431 | /* Apply desired changes on the Connection: header. Values may be removed and/or |
| 432 | * added depending on the <wanted> flags, which are exclusively composed of |
| 433 | * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The |
| 434 | * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left. |
| 435 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 436 | 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] | 437 | { |
| 438 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 439 | const char *hdr_val = "Connection"; |
| 440 | int hdr_len = 10; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 441 | |
| 442 | ctx.idx = 0; |
| 443 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 444 | |
| 445 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 446 | hdr_val = "Proxy-Connection"; |
| 447 | hdr_len = 16; |
| 448 | } |
| 449 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 450 | txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET); |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 451 | 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] | 452 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 453 | if (wanted & TX_CON_KAL_SET) |
| 454 | txn->flags |= TX_CON_KAL_SET; |
| 455 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 456 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 457 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 458 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 459 | if (wanted & TX_CON_CLO_SET) |
| 460 | txn->flags |= TX_CON_CLO_SET; |
| 461 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 462 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 463 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 464 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 465 | |
| 466 | if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 467 | return; |
| 468 | |
| 469 | if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) { |
| 470 | txn->flags |= TX_CON_CLO_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 471 | hdr_val = "Connection: close"; |
| 472 | hdr_len = 17; |
| 473 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 474 | hdr_val = "Proxy-Connection: close"; |
| 475 | hdr_len = 23; |
| 476 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 477 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) { |
| 481 | txn->flags |= TX_CON_KAL_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 482 | hdr_val = "Connection: keep-alive"; |
| 483 | hdr_len = 22; |
| 484 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 485 | hdr_val = "Proxy-Connection: keep-alive"; |
| 486 | hdr_len = 28; |
| 487 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 488 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 489 | } |
| 490 | return; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 491 | } |
| 492 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 493 | 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] | 494 | { |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 495 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 496 | int tmp = TX_CON_WANT_KAL; |
| 497 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 498 | if (IS_HTX_STRM(s)) |
Christopher Faulet | 03b9d8b | 2019-03-26 22:02:00 +0100 | [diff] [blame] | 499 | return; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 500 | |
Christopher Faulet | 315b39c | 2018-09-21 16:26:19 +0200 | [diff] [blame] | 501 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN || |
| 502 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN) |
| 503 | tmp = TX_CON_WANT_TUN; |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 504 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 505 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL || |
Christopher Faulet | 315b39c | 2018-09-21 16:26:19 +0200 | [diff] [blame] | 506 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 507 | tmp = TX_CON_WANT_SCL; |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 508 | |
Christopher Faulet | 315b39c | 2018-09-21 16:26:19 +0200 | [diff] [blame] | 509 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO || |
| 510 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 511 | tmp = TX_CON_WANT_CLO; |
| 512 | |
| 513 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
| 514 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
| 515 | |
| 516 | if (!(txn->flags & TX_HDR_CONN_PRS) && |
| 517 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) { |
| 518 | /* parse the Connection header and possibly clean it */ |
| 519 | int to_del = 0; |
| 520 | if ((msg->flags & HTTP_MSGF_VER_11) || |
| 521 | ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 522 | !((fe->options2|s->be->options2) & PR_O2_FAKE_KA))) |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 523 | to_del |= 2; /* remove "keep-alive" */ |
| 524 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
| 525 | to_del |= 1; /* remove "close" */ |
| 526 | http_parse_connection_header(txn, msg, to_del); |
| 527 | } |
| 528 | |
| 529 | /* check if client or config asks for explicit close in KAL/SCL */ |
| 530 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 531 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) && |
| 532 | ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */ |
| 533 | (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */ |
| 534 | !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 535 | fe->state == PR_STSTOPPED)) /* frontend is stopping */ |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 536 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 537 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 538 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 539 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 540 | * processing can continue on next analysers, or zero if it either needs more |
| 541 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 542 | * 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] | 543 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 544 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 545 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 546 | 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] | 547 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 548 | /* |
| 549 | * We will parse the partial (or complete) lines. |
| 550 | * We will check the request syntax, and also join multi-line |
| 551 | * headers. An index of all the lines will be elaborated while |
| 552 | * parsing. |
| 553 | * |
| 554 | * For the parsing, we use a 28 states FSM. |
| 555 | * |
| 556 | * Here is the information we currently have : |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 557 | * ci_head(req) = beginning of request |
| 558 | * ci_head(req) + msg->eoh = end of processed headers / start of current one |
| 559 | * ci_tail(req) = end of input data |
| 560 | * msg->eol = end of current header or line (LF or CRLF) |
| 561 | * msg->next = first non-visited byte |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 562 | * |
| 563 | * 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] | 564 | * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 565 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 566 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 567 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 568 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 569 | int cur_idx; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 570 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 571 | struct http_txn *txn = s->txn; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 572 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 573 | struct hdr_ctx ctx; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 574 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 575 | if (IS_HTX_STRM(s)) |
| 576 | return htx_wait_for_request(s, req, an_bit); |
| 577 | |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 578 | 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] | 579 | now_ms, __FUNCTION__, |
| 580 | s, |
| 581 | req, |
| 582 | req->rex, req->wex, |
| 583 | req->flags, |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 584 | ci_data(req), |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 585 | req->analysers); |
| 586 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 587 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 588 | s->srv_error = http_return_srv_error; |
| 589 | |
Rian McGuire | 89fcb7d | 2018-04-24 11:19:21 -0300 | [diff] [blame] | 590 | /* 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] | 591 | if (c_data(req) && s->logs.t_idle == -1) |
Rian McGuire | 89fcb7d | 2018-04-24 11:19:21 -0300 | [diff] [blame] | 592 | s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake; |
| 593 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 594 | /* There's a protected area at the end of the buffer for rewriting |
| 595 | * purposes. We don't want to start to parse the request if the |
| 596 | * protected area is affected, because we may have to move processed |
| 597 | * data later, which is much more complicated. |
| 598 | */ |
Willy Tarreau | d760eec | 2018-07-10 09:50:25 +0200 | [diff] [blame] | 599 | if (c_data(req) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 600 | if (txn->flags & TX_NOT_FIRST) { |
Christopher Faulet | 3d36d4e | 2019-09-04 09:39:42 +0200 | [diff] [blame] | 601 | if (unlikely(!channel_is_rewritable(req) && co_data(req))) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 602 | 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] | 603 | goto failed_keep_alive; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 604 | /* 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] | 605 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 606 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 607 | req->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 608 | return 0; |
| 609 | } |
Willy Tarreau | 188e230 | 2018-06-15 11:11:53 +0200 | [diff] [blame] | 610 | if (unlikely(ci_tail(req) < c_ptr(req, msg->next) || |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 611 | ci_tail(req) > b_wrap(&req->buf) - global.tune.maxrewrite)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 612 | channel_slow_realign(req, trash.area); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 613 | } |
| 614 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 615 | if (likely(msg->next < ci_data(req))) /* some unparsed data are available */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 616 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 617 | } |
| 618 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 619 | /* 1: we might have to print this header in debug mode */ |
| 620 | if (unlikely((global.mode & MODE_DEBUG) && |
| 621 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 7d59e90 | 2014-10-21 19:36:09 +0200 | [diff] [blame] | 622 | msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 623 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 624 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 625 | sol = ci_head(req); |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 626 | /* this is a bit complex : in case of error on the request line, |
| 627 | * we know that rq.l is still zero, so we display only the part |
| 628 | * up to the end of the line (truncated by debug_hdr). |
| 629 | */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 630 | 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] | 631 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 632 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 633 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 634 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 635 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 636 | while (cur_idx) { |
| 637 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 638 | debug_hdr("clihdr", s, sol, eol); |
| 639 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 640 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 641 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 642 | } |
| 643 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 644 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 645 | /* |
| 646 | * Now we quickly check if we have found a full valid request. |
| 647 | * If not so, we check the FD and buffer states before leaving. |
| 648 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 649 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 650 | * requests are checked first. When waiting for a second request |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 651 | * on a keep-alive stream, if we encounter and error, close, t/o, |
| 652 | * 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] | 653 | * 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] | 654 | * process_stream() as a frontend error. |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 655 | * Last, we may increase some tracked counters' http request errors on |
| 656 | * the cases that are deliberately the client's fault. For instance, |
| 657 | * a timeout or connection reset is not counted as an error. However |
| 658 | * a bad request is. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 659 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 660 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 661 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 662 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 663 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 664 | */ |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 665 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 666 | stream_inc_http_req_ctr(s); |
| 667 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 668 | proxy_inc_fe_req_ctr(sess->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 669 | goto return_bad_req; |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 670 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 671 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 672 | /* 1: Since we are in header mode, if there's no space |
| 673 | * left for headers, we won't be able to free more |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 674 | * later, so the stream will never terminate. We |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 675 | * must terminate it now. |
| 676 | */ |
Willy Tarreau | 2375233 | 2018-06-15 14:54:53 +0200 | [diff] [blame] | 677 | if (unlikely(channel_full(req, global.tune.maxrewrite))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 678 | /* FIXME: check if URI is set and return Status |
| 679 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 680 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 681 | stream_inc_http_req_ctr(s); |
| 682 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 683 | proxy_inc_fe_req_ctr(sess->fe); |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 684 | if (msg->err_pos < 0) |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 685 | msg->err_pos = ci_data(req); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 686 | goto return_bad_req; |
| 687 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 688 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 689 | /* 2: have we encountered a read error ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 690 | else if (req->flags & CF_READ_ERROR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 691 | if (!(s->flags & SF_ERR_MASK)) |
| 692 | s->flags |= SF_ERR_CLICL; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 693 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 694 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 695 | goto failed_keep_alive; |
| 696 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 697 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 698 | goto failed_keep_alive; |
| 699 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 700 | /* we cannot return any message on error */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 701 | if (msg->err_pos >= 0) { |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 702 | 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] | 703 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 704 | } |
| 705 | |
Willy Tarreau | dc979f2 | 2012-12-04 10:39:01 +0100 | [diff] [blame] | 706 | txn->status = 400; |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 707 | msg->err_state = msg->msg_state; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 708 | msg->msg_state = HTTP_MSG_ERROR; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 709 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 710 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 711 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 712 | proxy_inc_fe_req_ctr(sess->fe); |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 713 | _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 714 | if (sess->listener->counters) |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 715 | _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 716 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 717 | if (!(s->flags & SF_FINST_MASK)) |
| 718 | s->flags |= SF_FINST_R; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 719 | return 0; |
| 720 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 721 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 722 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 723 | 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] | 724 | if (!(s->flags & SF_ERR_MASK)) |
| 725 | s->flags |= SF_ERR_CLITO; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 726 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 727 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 728 | goto failed_keep_alive; |
| 729 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 730 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 731 | goto failed_keep_alive; |
| 732 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 733 | /* read timeout : give up with an error message. */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 734 | if (msg->err_pos >= 0) { |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 735 | 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] | 736 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 737 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 738 | txn->status = 408; |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 739 | msg->err_state = msg->msg_state; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 740 | msg->msg_state = HTTP_MSG_ERROR; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 741 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 742 | req->analysers &= AN_REQ_FLT_END; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 743 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 744 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 745 | proxy_inc_fe_req_ctr(sess->fe); |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 746 | _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 747 | if (sess->listener->counters) |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 748 | _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 749 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 750 | if (!(s->flags & SF_FINST_MASK)) |
| 751 | s->flags |= SF_FINST_R; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 752 | return 0; |
| 753 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 754 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 755 | /* 4: have we encountered a close ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 756 | else if (req->flags & CF_SHUTR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 757 | if (!(s->flags & SF_ERR_MASK)) |
| 758 | s->flags |= SF_ERR_CLICL; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 759 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 760 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 761 | goto failed_keep_alive; |
| 762 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 763 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 764 | goto failed_keep_alive; |
| 765 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 766 | if (msg->err_pos >= 0) |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 767 | 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] | 768 | txn->status = 400; |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 769 | msg->err_state = msg->msg_state; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 770 | msg->msg_state = HTTP_MSG_ERROR; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 771 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 772 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 773 | stream_inc_http_err_ctr(s); |
| 774 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 775 | proxy_inc_fe_req_ctr(sess->fe); |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 776 | _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 777 | if (sess->listener->counters) |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 778 | _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 779 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 780 | if (!(s->flags & SF_FINST_MASK)) |
| 781 | s->flags |= SF_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 782 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 783 | } |
| 784 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 785 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 786 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 787 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 788 | |
| 789 | if (sess->listener->options & LI_O_NOQUICKACK && ci_data(req)) { |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 790 | /* We need more data, we have to re-enable quick-ack in case we |
| 791 | * previously disabled it, otherwise we might cause the client |
| 792 | * to delay next data. |
| 793 | */ |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 794 | conn_set_quickack(objt_conn(sess->origin), 1); |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 795 | } |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 796 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 797 | if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) { |
| 798 | /* If the client starts to talk, let's fall back to |
| 799 | * request timeout processing. |
| 800 | */ |
| 801 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 802 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 803 | } |
| 804 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 805 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 806 | if (!tick_isset(req->analyse_exp)) { |
| 807 | if ((msg->msg_state == HTTP_MSG_RQBEFORE) && |
| 808 | (txn->flags & TX_WAIT_NEXT_RQ) && |
| 809 | tick_isset(s->be->timeout.httpka)) |
| 810 | req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka); |
| 811 | else |
| 812 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
| 813 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 814 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 815 | /* we're not ready yet */ |
| 816 | return 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 817 | |
| 818 | failed_keep_alive: |
| 819 | /* Here we process low-level errors for keep-alive requests. In |
| 820 | * short, if the request is not the first one and it experiences |
| 821 | * a timeout, read error or shutdown, we just silently close so |
| 822 | * that the client can try again. |
| 823 | */ |
| 824 | txn->status = 0; |
| 825 | msg->msg_state = HTTP_MSG_RQBEFORE; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 826 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 827 | s->logs.logwait = 0; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 828 | s->logs.level = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 829 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 830 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 831 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 832 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 833 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 834 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 835 | * complete the request parsing by setting a few fields we will need |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 836 | * 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] | 837 | * 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] | 838 | * 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] | 839 | * byte after the last LF. msg->sov points to the first byte of data. |
| 840 | * msg->eol cannot be trusted because it may have been left uninitialized |
| 841 | * (for instance in the absence of headers). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 842 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 843 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 844 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 845 | 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] | 846 | |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 847 | if (txn->flags & TX_WAIT_NEXT_RQ) { |
| 848 | /* kill the pending keep-alive timeout */ |
| 849 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 850 | req->analyse_exp = TICK_ETERNITY; |
| 851 | } |
| 852 | |
| 853 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 854 | /* Maybe we found in invalid header name while we were configured not |
| 855 | * to block on that, so we have to capture it now. |
| 856 | */ |
| 857 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 858 | 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] | 859 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 860 | /* |
| 861 | * 1: identify the method |
| 862 | */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 863 | 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] | 864 | |
| 865 | /* we can make use of server redirect on GET and HEAD */ |
| 866 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 867 | s->flags |= SF_REDIRECTABLE; |
Willy Tarreau | 9165979 | 2017-11-10 19:38:10 +0100 | [diff] [blame] | 868 | else if (txn->meth == HTTP_METH_OTHER && |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 869 | 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] | 870 | /* PRI is reserved for the HTTP/2 preface */ |
| 871 | msg->err_pos = 0; |
| 872 | goto return_bad_req; |
| 873 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 874 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 875 | /* |
| 876 | * 2: check if the URI matches the monitor_uri. |
| 877 | * We have to do this for every request which gets in, because |
| 878 | * the monitor-uri is defined by the frontend. |
| 879 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 880 | if (unlikely((sess->fe->monitor_uri_len != 0) && |
| 881 | (sess->fe->monitor_uri_len == msg->sl.rq.u_l) && |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 882 | !memcmp(ci_head(req) + msg->sl.rq.u, |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 883 | sess->fe->monitor_uri, |
| 884 | sess->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 885 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 886 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 887 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 888 | struct acl_cond *cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 889 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 890 | s->flags |= SF_MONITOR; |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 891 | _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1); |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 892 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 893 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 894 | list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 895 | 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] | 896 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 897 | ret = acl_pass(ret); |
| 898 | if (cond->pol == ACL_COND_UNLESS) |
| 899 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 900 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 901 | if (ret) { |
| 902 | /* we fail this request, let's return 503 service unavail */ |
| 903 | txn->status = 503; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 904 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 905 | if (!(s->flags & SF_ERR_MASK)) |
| 906 | 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] | 907 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 908 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 909 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 910 | |
Joseph Herlant | 5ba8025 | 2018-11-15 09:25:36 -0800 | [diff] [blame] | 911 | /* nothing to fail, let's reply normally */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 912 | txn->status = 200; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 913 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 914 | if (!(s->flags & SF_ERR_MASK)) |
| 915 | 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] | 916 | goto return_prx_cond; |
| 917 | } |
| 918 | |
| 919 | /* |
| 920 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 921 | * Note: we cannot log anymore if the request has been |
| 922 | * classified as invalid. |
| 923 | */ |
| 924 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 925 | /* we have a complete HTTP request that we must log */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 926 | if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 927 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 928 | |
Stéphane Cottin | 23e9e93 | 2017-05-18 08:58:41 +0200 | [diff] [blame] | 929 | if (urilen >= global.tune.requri_len ) |
| 930 | urilen = global.tune.requri_len - 1; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 931 | memcpy(txn->uri, ci_head(req), urilen); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 932 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 933 | |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 934 | if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT))) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 935 | s->do_log(s); |
| 936 | } else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 937 | ha_alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 938 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 939 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 940 | |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 941 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 942 | * exactly one digit "." one digit. This check may be disabled using |
| 943 | * option accept-invalid-http-request. |
| 944 | */ |
| 945 | if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) { |
| 946 | if (msg->sl.rq.v_l != 8) { |
| 947 | msg->err_pos = msg->sl.rq.v; |
| 948 | goto return_bad_req; |
| 949 | } |
| 950 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 951 | if (ci_head(req)[msg->sl.rq.v + 4] != '/' || |
| 952 | !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 5]) || |
| 953 | ci_head(req)[msg->sl.rq.v + 6] != '.' || |
| 954 | !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 7])) { |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 955 | msg->err_pos = msg->sl.rq.v + 4; |
| 956 | goto return_bad_req; |
| 957 | } |
| 958 | } |
Willy Tarreau | 1331766 | 2015-05-01 13:47:08 +0200 | [diff] [blame] | 959 | else { |
| 960 | /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */ |
| 961 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
| 962 | goto return_bad_req; |
| 963 | } |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 964 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 965 | /* ... and check if the request is HTTP/1.1 or above */ |
| 966 | if ((msg->sl.rq.v_l == 8) && |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 967 | ((ci_head(req)[msg->sl.rq.v + 5] > '1') || |
| 968 | ((ci_head(req)[msg->sl.rq.v + 5] == '1') && |
| 969 | (ci_head(req)[msg->sl.rq.v + 7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 970 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 971 | |
| 972 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 973 | 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] | 974 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 975 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 976 | * we have what looks like a proxied connection instead of a connection, |
| 977 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 978 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 979 | * happen to do that despite being non-standard :-( |
| 980 | * We consider that a request not beginning with either '/' or '*' is |
| 981 | * a proxied connection, which covers both "scheme://location" and |
| 982 | * CONNECT ip:port. |
| 983 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 984 | if ((sess->fe->options2 & PR_O2_USE_PXHDR) && |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 985 | 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] | 986 | txn->flags |= TX_USE_PX_CONN; |
| 987 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 988 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 989 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 990 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 991 | /* 5: we may need to capture headers */ |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 992 | if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap)) |
Christopher Faulet | 10079f5 | 2018-10-03 15:17:28 +0200 | [diff] [blame] | 993 | http_capture_headers(ci_head(req), &txn->hdr_idx, |
| 994 | s->req_cap, sess->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 995 | |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 996 | /* 6: determine the transfer-length according to RFC2616 #4.4, updated |
| 997 | * by RFC7230#3.3.3 : |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 998 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 999 | * The length of a message body is determined by one of the following |
| 1000 | * (in order of precedence): |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1001 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 1002 | * 1. Any response to a HEAD request and any response with a 1xx |
| 1003 | * (Informational), 204 (No Content), or 304 (Not Modified) status |
| 1004 | * code is always terminated by the first empty line after the |
| 1005 | * header fields, regardless of the header fields present in the |
| 1006 | * message, and thus cannot contain a message body. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1007 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 1008 | * 2. Any 2xx (Successful) response to a CONNECT request implies that |
| 1009 | * the connection will become a tunnel immediately after the empty |
| 1010 | * line that concludes the header fields. A client MUST ignore any |
| 1011 | * Content-Length or Transfer-Encoding header fields received in |
| 1012 | * such a message. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 1013 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 1014 | * 3. If a Transfer-Encoding header field is present and the chunked |
| 1015 | * transfer coding (Section 4.1) is the final encoding, the message |
| 1016 | * body length is determined by reading and decoding the chunked |
| 1017 | * data until the transfer coding indicates the data is complete. |
| 1018 | * |
| 1019 | * If a Transfer-Encoding header field is present in a response and |
| 1020 | * the chunked transfer coding is not the final encoding, the |
| 1021 | * message body length is determined by reading the connection until |
| 1022 | * it is closed by the server. If a Transfer-Encoding header field |
| 1023 | * is present in a request and the chunked transfer coding is not |
| 1024 | * the final encoding, the message body length cannot be determined |
| 1025 | * reliably; the server MUST respond with the 400 (Bad Request) |
| 1026 | * status code and then close the connection. |
| 1027 | * |
| 1028 | * If a message is received with both a Transfer-Encoding and a |
| 1029 | * Content-Length header field, the Transfer-Encoding overrides the |
| 1030 | * Content-Length. Such a message might indicate an attempt to |
| 1031 | * perform request smuggling (Section 9.5) or response splitting |
| 1032 | * (Section 9.4) and ought to be handled as an error. A sender MUST |
| 1033 | * remove the received Content-Length field prior to forwarding such |
| 1034 | * a message downstream. |
| 1035 | * |
| 1036 | * 4. If a message is received without Transfer-Encoding and with |
| 1037 | * either multiple Content-Length header fields having differing |
| 1038 | * field-values or a single Content-Length header field having an |
| 1039 | * invalid value, then the message framing is invalid and the |
| 1040 | * recipient MUST treat it as an unrecoverable error. If this is a |
| 1041 | * request message, the server MUST respond with a 400 (Bad Request) |
| 1042 | * status code and then close the connection. If this is a response |
| 1043 | * message received by a proxy, the proxy MUST close the connection |
| 1044 | * to the server, discard the received response, and send a 502 (Bad |
| 1045 | * Gateway) response to the client. If this is a response message |
| 1046 | * received by a user agent, the user agent MUST close the |
| 1047 | * connection to the server and discard the received response. |
| 1048 | * |
| 1049 | * 5. If a valid Content-Length header field is present without |
| 1050 | * Transfer-Encoding, its decimal value defines the expected message |
| 1051 | * body length in octets. If the sender closes the connection or |
| 1052 | * the recipient times out before the indicated number of octets are |
| 1053 | * received, the recipient MUST consider the message to be |
| 1054 | * incomplete and close the connection. |
| 1055 | * |
| 1056 | * 6. If this is a request message and none of the above are true, then |
| 1057 | * the message body length is zero (no message body is present). |
| 1058 | * |
| 1059 | * 7. Otherwise, this is a response message without a declared message |
| 1060 | * body length, so the message body length is determined by the |
| 1061 | * number of octets received prior to the server closing the |
| 1062 | * connection. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1063 | */ |
| 1064 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1065 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 1066 | /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1067 | 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] | 1068 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 1069 | msg->flags |= HTTP_MSGF_TE_CHNK; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 1070 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | 34dfc60 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 1071 | /* chunked not last, return badreq */ |
| 1072 | goto return_bad_req; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 1073 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1074 | } |
| 1075 | |
Willy Tarreau | 196a7df | 2019-09-12 14:01:40 +0200 | [diff] [blame] | 1076 | /* "chunked" mandatory if transfer-encoding is used */ |
| 1077 | if (ctx.idx && !(msg->flags & HTTP_MSGF_TE_CHNK)) |
| 1078 | goto return_bad_req; |
| 1079 | |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 1080 | /* Chunked requests must have their content-length removed */ |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1081 | ctx.idx = 0; |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 1082 | if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1083 | 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] | 1084 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 1085 | } |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1086 | 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] | 1087 | signed long long cl; |
| 1088 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 1089 | if (!ctx.vlen) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1090 | msg->err_pos = ctx.line + ctx.val - ci_head(req); |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1091 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 1092 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1093 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 1094 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1095 | msg->err_pos = ctx.line + ctx.val - ci_head(req); |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1096 | goto return_bad_req; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 1097 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1098 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 1099 | if (cl < 0) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1100 | msg->err_pos = ctx.line + ctx.val - ci_head(req); |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1101 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 1102 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1103 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 1104 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1105 | msg->err_pos = ctx.line + ctx.val - ci_head(req); |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1106 | goto return_bad_req; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 1107 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1108 | |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 1109 | msg->flags |= HTTP_MSGF_CNT_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 1110 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1111 | } |
| 1112 | |
Willy Tarreau | 34dfc60 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 1113 | /* even bodyless requests have a known length */ |
| 1114 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 1115 | |
Willy Tarreau | 179085c | 2014-04-28 16:48:56 +0200 | [diff] [blame] | 1116 | /* Until set to anything else, the connection mode is set as Keep-Alive. It will |
| 1117 | * only change if both the request and the config reference something else. |
| 1118 | * Option httpclose by itself sets tunnel mode where headers are mangled. |
| 1119 | * However, if another mode is set, it will affect it (eg: server-close/ |
| 1120 | * keep-alive + httpclose = close). Note that we avoid to redo the same work |
| 1121 | * if FE and BE have the same settings (common). The method consists in |
| 1122 | * checking if options changed between the two calls (implying that either |
| 1123 | * one is non-null, or one of them is non-null and we are there for the first |
| 1124 | * time. |
| 1125 | */ |
| 1126 | if (!(txn->flags & TX_HDR_CONN_PRS) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1127 | ((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] | 1128 | http_adjust_conn_mode(s, txn, msg); |
Willy Tarreau | 179085c | 2014-04-28 16:48:56 +0200 | [diff] [blame] | 1129 | |
Willy Tarreau | 9fbe18e | 2015-05-01 22:42:08 +0200 | [diff] [blame] | 1130 | /* we may have to wait for the request's body */ |
| 1131 | if ((s->be->options & PR_O_WREQ_BODY) && |
| 1132 | (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK))) |
| 1133 | req->analysers |= AN_REQ_HTTP_BODY; |
| 1134 | |
Willy Tarreau | 83ece46 | 2017-12-21 15:13:09 +0100 | [diff] [blame] | 1135 | /* |
| 1136 | * RFC7234#4: |
| 1137 | * A cache MUST write through requests with methods |
| 1138 | * that are unsafe (Section 4.2.1 of [RFC7231]) to |
| 1139 | * the origin server; i.e., a cache is not allowed |
| 1140 | * to generate a reply to such a request before |
| 1141 | * having forwarded the request and having received |
| 1142 | * a corresponding response. |
| 1143 | * |
| 1144 | * RFC7231#4.2.1: |
| 1145 | * Of the request methods defined by this |
| 1146 | * specification, the GET, HEAD, OPTIONS, and TRACE |
| 1147 | * methods are defined to be safe. |
| 1148 | */ |
| 1149 | if (likely(txn->meth == HTTP_METH_GET || |
| 1150 | txn->meth == HTTP_METH_HEAD || |
| 1151 | txn->meth == HTTP_METH_OPTIONS || |
| 1152 | txn->meth == HTTP_METH_TRACE)) |
| 1153 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 1154 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1155 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 1156 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1157 | req->analyse_exp = TICK_ETERNITY; |
| 1158 | return 1; |
| 1159 | |
| 1160 | return_bad_req: |
| 1161 | /* We centralize bad requests processing here */ |
| 1162 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 1163 | /* we detected a parsing error. We want to archive this request |
| 1164 | * in the dedicated proxy area for later troubleshooting. |
| 1165 | */ |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 1166 | 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] | 1167 | } |
| 1168 | |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1169 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1170 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 1171 | txn->status = 400; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1172 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 1173 | |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 1174 | _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1175 | if (sess->listener->counters) |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 1176 | _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1177 | |
| 1178 | return_prx_cond: |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1179 | if (!(s->flags & SF_ERR_MASK)) |
| 1180 | s->flags |= SF_ERR_PRXCOND; |
| 1181 | if (!(s->flags & SF_FINST_MASK)) |
| 1182 | s->flags |= SF_FINST_R; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1183 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 1184 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1185 | req->analyse_exp = TICK_ETERNITY; |
| 1186 | return 0; |
| 1187 | } |
| 1188 | |
Willy Tarreau | 4f8a83c | 2012-06-04 00:26:23 +0200 | [diff] [blame] | 1189 | |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 1190 | /* This function prepares an applet to handle the stats. It can deal with the |
| 1191 | * "100-continue" expectation, check that admin rules are met for POST requests, |
| 1192 | * and program a response message if something was unexpected. It cannot fail |
| 1193 | * 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] | 1194 | * 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] | 1195 | * 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] | 1196 | * is expected to have already assigned an appctx to the stream. |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 1197 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1198 | int http_handle_stats(struct stream *s, struct channel *req) |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 1199 | { |
| 1200 | struct stats_admin_rule *stats_admin_rule; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 1201 | struct stream_interface *si = &s->si[1]; |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 1202 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1203 | struct http_txn *txn = s->txn; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 1204 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1205 | struct uri_auth *uri_auth = s->be->uri_auth; |
| 1206 | const char *uri, *h, *lookup; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1207 | struct appctx *appctx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1208 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1209 | appctx = si_appctx(si); |
| 1210 | memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats)); |
| 1211 | appctx->st1 = appctx->st2 = 0; |
| 1212 | appctx->ctx.stats.st_code = STAT_STATUS_INIT; |
| 1213 | appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1214 | 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] | 1215 | appctx->ctx.stats.flags |= STAT_CHUNKED; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1216 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1217 | uri = ci_head(msg->chn) + msg->sl.rq.u; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1218 | lookup = uri + uri_auth->uri_len; |
| 1219 | |
| 1220 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) { |
| 1221 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1222 | appctx->ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1223 | break; |
| 1224 | } |
| 1225 | } |
| 1226 | |
| 1227 | if (uri_auth->refresh) { |
| 1228 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) { |
| 1229 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1230 | appctx->ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1231 | break; |
| 1232 | } |
| 1233 | } |
| 1234 | } |
| 1235 | |
| 1236 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) { |
| 1237 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1238 | appctx->ctx.stats.flags &= ~STAT_FMT_HTML; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1239 | break; |
| 1240 | } |
| 1241 | } |
| 1242 | |
Willy Tarreau | 1e62df9 | 2016-01-11 18:57:53 +0100 | [diff] [blame] | 1243 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) { |
| 1244 | if (memcmp(h, ";typed", 6) == 0) { |
| 1245 | appctx->ctx.stats.flags &= ~STAT_FMT_HTML; |
| 1246 | appctx->ctx.stats.flags |= STAT_FMT_TYPED; |
| 1247 | break; |
| 1248 | } |
| 1249 | } |
| 1250 | |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1251 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 1252 | if (memcmp(h, ";st=", 4) == 0) { |
| 1253 | int i; |
| 1254 | h += 4; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1255 | appctx->ctx.stats.st_code = STAT_STATUS_UNKN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1256 | for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) { |
| 1257 | if (strncmp(stat_status_codes[i], h, 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1258 | appctx->ctx.stats.st_code = i; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1259 | break; |
| 1260 | } |
| 1261 | } |
| 1262 | break; |
| 1263 | } |
| 1264 | } |
| 1265 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1266 | appctx->ctx.stats.scope_str = 0; |
| 1267 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1268 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 1269 | if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) { |
| 1270 | int itx = 0; |
| 1271 | const char *h2; |
| 1272 | char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1]; |
| 1273 | const char *err; |
| 1274 | |
| 1275 | h += strlen(STAT_SCOPE_INPUT_NAME) + 1; |
| 1276 | h2 = h; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1277 | appctx->ctx.stats.scope_str = h2 - ci_head(msg->chn); |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1278 | while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') { |
| 1279 | itx++; |
| 1280 | h++; |
| 1281 | } |
| 1282 | |
| 1283 | if (itx > STAT_SCOPE_TXT_MAXLEN) |
| 1284 | itx = STAT_SCOPE_TXT_MAXLEN; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1285 | appctx->ctx.stats.scope_len = itx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1286 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1287 | /* 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] | 1288 | memcpy(scope_txt, h2, itx); |
| 1289 | scope_txt[itx] = '\0'; |
| 1290 | err = invalid_char(scope_txt); |
| 1291 | if (err) { |
| 1292 | /* bad char in search text => clear scope */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1293 | appctx->ctx.stats.scope_str = 0; |
| 1294 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1295 | } |
| 1296 | break; |
| 1297 | } |
| 1298 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 1299 | |
| 1300 | /* now check whether we have some admin rules for this request */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1301 | list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 1302 | int ret = 1; |
| 1303 | |
| 1304 | if (stats_admin_rule->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 1305 | 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] | 1306 | ret = acl_pass(ret); |
| 1307 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 1308 | ret = !ret; |
| 1309 | } |
| 1310 | |
| 1311 | if (ret) { |
| 1312 | /* no rule, or the rule matches */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1313 | appctx->ctx.stats.flags |= STAT_ADMIN; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 1314 | break; |
| 1315 | } |
| 1316 | } |
| 1317 | |
Christopher Faulet | 5d45e38 | 2019-02-27 15:15:23 +0100 | [diff] [blame] | 1318 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 1319 | appctx->st0 = STAT_HTTP_HEAD; |
| 1320 | else if (txn->meth == HTTP_METH_POST && (msg->flags & HTTP_MSGF_CNT_LEN)) { |
Christopher Faulet | bcf242a | 2019-03-01 11:36:26 +0100 | [diff] [blame] | 1321 | if (appctx->ctx.stats.flags & STAT_ADMIN) |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1322 | appctx->st0 = STAT_HTTP_POST; |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 1323 | else { |
Christopher Faulet | 5d45e38 | 2019-02-27 15:15:23 +0100 | [diff] [blame] | 1324 | /* POST without admin level */ |
| 1325 | appctx->ctx.stats.flags &= ~STAT_CHUNKED; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1326 | appctx->ctx.stats.st_code = STAT_STATUS_DENY; |
| 1327 | appctx->st0 = STAT_HTTP_LAST; |
de Lafond Guillaume | 88c278f | 2013-04-15 19:27:10 +0200 | [diff] [blame] | 1328 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 1329 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 1330 | else { |
Christopher Faulet | 5d45e38 | 2019-02-27 15:15:23 +0100 | [diff] [blame] | 1331 | /* Unsupported method or chunked POST */ |
| 1332 | appctx->ctx.stats.flags &= ~STAT_CHUNKED; |
| 1333 | appctx->ctx.stats.st_code = STAT_STATUS_IVAL; |
| 1334 | appctx->st0 = STAT_HTTP_LAST; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 1335 | } |
| 1336 | |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 1337 | s->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 1338 | return 1; |
| 1339 | } |
| 1340 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1341 | int http_transform_header_str(struct stream* s, struct http_msg *msg, |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 1342 | const char* name, unsigned int name_len, |
| 1343 | const char *str, struct my_regex *re, |
| 1344 | int action) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 1345 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1346 | struct hdr_idx *idx = &s->txn->hdr_idx; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1347 | struct buffer *output = get_trash_chunk(); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 1348 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 1349 | /* Choose the header browsing function. */ |
| 1350 | switch (action) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1351 | case ACT_HTTP_REPLACE_VAL: |
Willy Tarreau | c5a4fd5 | 2018-12-11 11:42:27 +0100 | [diff] [blame] | 1352 | return http_legacy_replace_header(idx, msg, name, name_len, str, re, output); |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1353 | case ACT_HTTP_REPLACE_HDR: |
Willy Tarreau | c5a4fd5 | 2018-12-11 11:42:27 +0100 | [diff] [blame] | 1354 | return http_legacy_replace_full_header(idx, msg, name, name_len, str, re, output); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 1355 | default: /* impossible */ |
| 1356 | return -1; |
| 1357 | } |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 1358 | } |
| 1359 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1360 | static int http_transform_header(struct stream* s, struct http_msg *msg, |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 1361 | const char* name, unsigned int name_len, |
| 1362 | struct list *fmt, struct my_regex *re, |
| 1363 | int action) |
| 1364 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1365 | struct buffer *replace; |
Christopher Faulet | 07a0fec | 2017-02-08 12:17:07 +0100 | [diff] [blame] | 1366 | int ret = -1; |
| 1367 | |
| 1368 | replace = alloc_trash_chunk(); |
| 1369 | if (!replace) |
| 1370 | goto leave; |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 1371 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1372 | replace->data = build_logline(s, replace->area, replace->size, fmt); |
| 1373 | if (replace->data >= replace->size - 1) |
Christopher Faulet | 07a0fec | 2017-02-08 12:17:07 +0100 | [diff] [blame] | 1374 | goto leave; |
| 1375 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1376 | ret = http_transform_header_str(s, msg, name, name_len, replace->area, |
| 1377 | re, action); |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 1378 | |
Christopher Faulet | 07a0fec | 2017-02-08 12:17:07 +0100 | [diff] [blame] | 1379 | leave: |
| 1380 | free_trash_chunk(replace); |
| 1381 | return ret; |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 1382 | } |
| 1383 | |
Christopher Faulet | 87451fd | 2019-03-01 11:16:34 +0100 | [diff] [blame] | 1384 | /* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. */ |
| 1385 | static void http_handle_expect_hdr(struct stream *s, struct channel *req, struct http_msg *msg) |
| 1386 | { |
| 1387 | /* If we have HTTP/1.1 message with a body and Expect: 100-continue, |
| 1388 | * then we must send an HTTP/1.1 100 Continue intermediate response. |
| 1389 | */ |
| 1390 | if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) && |
| 1391 | (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) { |
| 1392 | struct hdr_ctx ctx; |
| 1393 | ctx.idx = 0; |
| 1394 | /* Expect is allowed in 1.1, look for it */ |
| 1395 | if (http_find_header2("Expect", 6, ci_head(req), &s->txn->hdr_idx, &ctx) && |
| 1396 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) { |
| 1397 | co_inject(&s->res, HTTP_100.ptr, HTTP_100.len); |
| 1398 | http_remove_header2(&s->txn->req, &s->txn->hdr_idx, &ctx); |
| 1399 | } |
| 1400 | } |
| 1401 | } |
| 1402 | |
Frédéric Lécaille | 9ca51aa | 2018-11-12 10:06:54 +0100 | [diff] [blame] | 1403 | /* |
| 1404 | * Build an HTTP Early Hint HTTP 103 response header with <name> as name and with a value |
| 1405 | * built according to <fmt> log line format. |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1406 | * If <early_hints> is NULL, it is allocated and the HTTP 103 response first |
| 1407 | * line is inserted before the header. If an error occurred <early_hints> is |
| 1408 | * 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] | 1409 | */ |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1410 | static struct buffer *http_apply_early_hint_rule(struct stream* s, struct buffer *early_hints, |
| 1411 | const char* name, unsigned int name_len, |
| 1412 | struct list *fmt) |
Frédéric Lécaille | 9ca51aa | 2018-11-12 10:06:54 +0100 | [diff] [blame] | 1413 | { |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1414 | if (!early_hints) { |
| 1415 | early_hints = alloc_trash_chunk(); |
| 1416 | if (!early_hints) |
| 1417 | goto fail; |
| 1418 | if (!chunk_memcat(early_hints, HTTP_103.ptr, HTTP_103.len)) |
| 1419 | goto fail; |
| 1420 | } |
Frédéric Lécaille | 9ca51aa | 2018-11-12 10:06:54 +0100 | [diff] [blame] | 1421 | |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1422 | if (!chunk_memcat(early_hints, name, name_len) || !chunk_memcat(early_hints, ": ", 2)) |
| 1423 | goto fail; |
Frédéric Lécaille | 9ca51aa | 2018-11-12 10:06:54 +0100 | [diff] [blame] | 1424 | |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1425 | early_hints->data += build_logline(s, b_tail(early_hints), b_room(early_hints), fmt); |
| 1426 | if (!chunk_memcat(early_hints, "\r\n", 2)) |
| 1427 | goto fail; |
Frédéric Lécaille | 9ca51aa | 2018-11-12 10:06:54 +0100 | [diff] [blame] | 1428 | |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1429 | return early_hints; |
Frédéric Lécaille | 9ca51aa | 2018-11-12 10:06:54 +0100 | [diff] [blame] | 1430 | |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1431 | fail: |
| 1432 | free_trash_chunk(early_hints); |
| 1433 | return NULL; |
| 1434 | } |
Frédéric Lécaille | 9ca51aa | 2018-11-12 10:06:54 +0100 | [diff] [blame] | 1435 | |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1436 | /* Sends an HTTP 103 response. Before sending it, the last CRLF finishing the |
| 1437 | * response is added. If an error occurred or if another response was already |
| 1438 | * sent, this function does nothing. |
| 1439 | */ |
| 1440 | static void http_send_early_hints(struct stream *s, struct buffer *early_hints) |
| 1441 | { |
| 1442 | struct channel *chn = s->txn->rsp.chn; |
| 1443 | char *cur_ptr = ci_head(chn); |
| 1444 | int ret; |
Frédéric Lécaille | 9ca51aa | 2018-11-12 10:06:54 +0100 | [diff] [blame] | 1445 | |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1446 | /* If a response was already sent, skip early hints */ |
| 1447 | if (s->txn->status > 0) |
| 1448 | return; |
Frédéric Lécaille | 9ca51aa | 2018-11-12 10:06:54 +0100 | [diff] [blame] | 1449 | |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1450 | if (!chunk_memcat(early_hints, "\r\n", 2)) |
| 1451 | return; |
Frédéric Lécaille | 9ca51aa | 2018-11-12 10:06:54 +0100 | [diff] [blame] | 1452 | |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1453 | ret = b_rep_blk(&chn->buf, cur_ptr, cur_ptr, b_head(early_hints), b_data(early_hints)); |
| 1454 | c_adv(chn, ret); |
| 1455 | chn->total += ret; |
Frédéric Lécaille | 9ca51aa | 2018-11-12 10:06:54 +0100 | [diff] [blame] | 1456 | } |
| 1457 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1458 | /* Executes the http-request rules <rules> for stream <s>, proxy <px> and |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 1459 | * transaction <txn>. Returns the verdict of the first rule that prevents |
| 1460 | * further processing of the request (auth, deny, ...), and defaults to |
| 1461 | * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or |
| 1462 | * 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] | 1463 | * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL |
| 1464 | * and a deny/tarpit rule is matched, it will be filled with this rule's deny |
| 1465 | * status. |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 1466 | */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 1467 | enum rule_result |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 1468 | 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] | 1469 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 1470 | struct session *sess = strm_sess(s); |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 1471 | struct http_txn *txn = s->txn; |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 1472 | struct act_rule *rule; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 1473 | struct hdr_ctx ctx; |
Willy Tarreau | ae3c010 | 2014-04-28 23:22:08 +0200 | [diff] [blame] | 1474 | const char *auth_realm; |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1475 | struct buffer *early_hints = NULL; |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 1476 | enum rule_result rule_ret = HTTP_RULE_RES_CONT; |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 1477 | int act_flags = 0; |
Thierry Fournier | 4b788f7 | 2016-06-01 13:35:36 +0200 | [diff] [blame] | 1478 | int len; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 1479 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 1480 | /* If "the current_rule_list" match the executed rule list, we are in |
| 1481 | * resume condition. If a resume is needed it is always in the action |
| 1482 | * and never in the ACL or converters. In this case, we initialise the |
| 1483 | * current rule, and go to the action execution point. |
| 1484 | */ |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 1485 | if (s->current_rule) { |
| 1486 | rule = s->current_rule; |
| 1487 | s->current_rule = NULL; |
| 1488 | if (s->current_rule_list == rules) |
| 1489 | goto resume_execution; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 1490 | } |
| 1491 | s->current_rule_list = rules; |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 1492 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 1493 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 1494 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 1495 | /* check optional condition */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 1496 | if (rule->cond) { |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 1497 | int ret; |
| 1498 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 1499 | 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] | 1500 | ret = acl_pass(ret); |
| 1501 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 1502 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 1503 | ret = !ret; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 1504 | |
| 1505 | if (!ret) /* condition not matched */ |
| 1506 | continue; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 1507 | } |
| 1508 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 1509 | act_flags |= ACT_FLAG_FIRST; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 1510 | resume_execution: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 1511 | switch (rule->action) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1512 | case ACT_ACTION_ALLOW: |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 1513 | rule_ret = HTTP_RULE_RES_STOP; |
| 1514 | goto end; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 1515 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1516 | case ACT_ACTION_DENY: |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 1517 | if (deny_status) |
| 1518 | *deny_status = rule->deny_status; |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 1519 | rule_ret = HTTP_RULE_RES_DENY; |
| 1520 | goto end; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 1521 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1522 | case ACT_HTTP_REQ_TARPIT: |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 1523 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 1524 | if (deny_status) |
| 1525 | *deny_status = rule->deny_status; |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 1526 | rule_ret = HTTP_RULE_RES_DENY; |
| 1527 | goto end; |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 1528 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1529 | case ACT_HTTP_REQ_AUTH: |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1530 | /* Be sure to send any pending HTTP 103 response first */ |
| 1531 | if (early_hints) { |
| 1532 | http_send_early_hints(s, early_hints); |
| 1533 | free_trash_chunk(early_hints); |
| 1534 | early_hints = NULL; |
| 1535 | } |
Willy Tarreau | ae3c010 | 2014-04-28 23:22:08 +0200 | [diff] [blame] | 1536 | /* Auth might be performed on regular http-req rules as well as on stats */ |
| 1537 | auth_realm = rule->arg.auth.realm; |
| 1538 | if (!auth_realm) { |
| 1539 | if (px->uri_auth && rules == &px->uri_auth->http_req_rules) |
| 1540 | auth_realm = STATS_DEFAULT_REALM; |
| 1541 | else |
| 1542 | auth_realm = px->id; |
| 1543 | } |
| 1544 | /* send 401/407 depending on whether we use a proxy or not. We still |
| 1545 | * count one error, because normal browsing won't significantly |
| 1546 | * increase the counter but brute force attempts will. |
| 1547 | */ |
| 1548 | chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm); |
| 1549 | txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 1550 | http_reply_and_close(s, txn->status, &trash); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1551 | stream_inc_http_err_ctr(s); |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 1552 | rule_ret = HTTP_RULE_RES_ABRT; |
| 1553 | goto end; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 1554 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1555 | case ACT_HTTP_REDIR: |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1556 | /* Be sure to send any pending HTTP 103 response first */ |
| 1557 | if (early_hints) { |
| 1558 | http_send_early_hints(s, early_hints); |
| 1559 | free_trash_chunk(early_hints); |
| 1560 | early_hints = NULL; |
| 1561 | } |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 1562 | rule_ret = HTTP_RULE_RES_DONE; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 1563 | if (!http_apply_redirect_rule(rule->arg.redir, s, txn)) |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 1564 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 1565 | goto end; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 1566 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1567 | case ACT_HTTP_SET_NICE: |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 1568 | s->task->nice = rule->arg.nice; |
| 1569 | break; |
| 1570 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1571 | case ACT_HTTP_SET_TOS: |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 1572 | conn_set_tos(objt_conn(sess->origin), rule->arg.tos); |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 1573 | break; |
| 1574 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1575 | case ACT_HTTP_SET_MARK: |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 1576 | conn_set_mark(objt_conn(sess->origin), rule->arg.mark); |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 1577 | break; |
| 1578 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1579 | case ACT_HTTP_SET_LOGL: |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 1580 | s->logs.level = rule->arg.loglevel; |
| 1581 | break; |
| 1582 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1583 | case ACT_HTTP_REPLACE_HDR: |
| 1584 | case ACT_HTTP_REPLACE_VAL: |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 1585 | if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name, |
| 1586 | rule->arg.hdr_add.name_len, |
| 1587 | &rule->arg.hdr_add.fmt, |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 1588 | rule->arg.hdr_add.re, rule->action)) { |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 1589 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 1590 | goto end; |
| 1591 | } |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 1592 | break; |
| 1593 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1594 | case ACT_HTTP_DEL_HDR: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 1595 | ctx.idx = 0; |
| 1596 | /* remove all occurrences of the header */ |
| 1597 | 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] | 1598 | ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 1599 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 1600 | } |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 1601 | break; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 1602 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1603 | case ACT_HTTP_SET_HDR: |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 1604 | case ACT_HTTP_ADD_HDR: { |
Thierry Fournier | 4b788f7 | 2016-06-01 13:35:36 +0200 | [diff] [blame] | 1605 | /* The scope of the trash buffer must be limited to this function. The |
| 1606 | * build_logline() function can execute a lot of other function which |
| 1607 | * can use the trash buffer. So for limiting the scope of this global |
| 1608 | * buffer, we build first the header value using build_logline, and |
| 1609 | * after we store the header name. |
| 1610 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1611 | struct buffer *replace; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 1612 | |
| 1613 | replace = alloc_trash_chunk(); |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 1614 | if (!replace) { |
| 1615 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 1616 | goto end; |
| 1617 | } |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 1618 | |
Thierry Fournier | 4b788f7 | 2016-06-01 13:35:36 +0200 | [diff] [blame] | 1619 | len = rule->arg.hdr_add.name_len + 2, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1620 | len += build_logline(s, replace->area + len, |
| 1621 | replace->size - len, |
| 1622 | &rule->arg.hdr_add.fmt); |
| 1623 | memcpy(replace->area, rule->arg.hdr_add.name, |
| 1624 | rule->arg.hdr_add.name_len); |
| 1625 | replace->area[rule->arg.hdr_add.name_len] = ':'; |
| 1626 | replace->area[rule->arg.hdr_add.name_len + 1] = ' '; |
| 1627 | replace->data = len; |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 1628 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1629 | if (rule->action == ACT_HTTP_SET_HDR) { |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 1630 | /* remove all occurrences of the header */ |
| 1631 | ctx.idx = 0; |
| 1632 | 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] | 1633 | ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 1634 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
| 1635 | } |
| 1636 | } |
| 1637 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1638 | 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] | 1639 | static unsigned char rate_limit = 0; |
| 1640 | |
| 1641 | if ((rate_limit++ & 255) == 0) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1642 | replace->area[rule->arg.hdr_add.name_len] = 0; |
| 1643 | 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, |
| 1644 | replace->area, s->uniq_id); |
Tim Duesterhus | 3fd1973 | 2018-05-27 20:35:08 +0200 | [diff] [blame] | 1645 | } |
| 1646 | |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 1647 | _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1); |
Tim Duesterhus | 3fd1973 | 2018-05-27 20:35:08 +0200 | [diff] [blame] | 1648 | if (sess->fe != s->be) |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 1649 | _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1); |
Tim Duesterhus | 3fd1973 | 2018-05-27 20:35:08 +0200 | [diff] [blame] | 1650 | if (sess->listener->counters) |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 1651 | _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1); |
Tim Duesterhus | 3fd1973 | 2018-05-27 20:35:08 +0200 | [diff] [blame] | 1652 | } |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 1653 | |
| 1654 | free_trash_chunk(replace); |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 1655 | break; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 1656 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 1657 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1658 | case ACT_HTTP_DEL_ACL: |
| 1659 | case ACT_HTTP_DEL_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 1660 | struct pat_ref *ref; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1661 | struct buffer *key; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 1662 | |
| 1663 | /* collect reference */ |
| 1664 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 1665 | if (!ref) |
| 1666 | continue; |
| 1667 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 1668 | /* allocate key */ |
| 1669 | key = alloc_trash_chunk(); |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 1670 | if (!key) { |
| 1671 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 1672 | goto end; |
| 1673 | } |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 1674 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 1675 | /* collect key */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1676 | key->data = build_logline(s, key->area, key->size, |
| 1677 | &rule->arg.map.key); |
| 1678 | key->area[key->data] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 1679 | |
| 1680 | /* perform update */ |
| 1681 | /* returned code: 1=ok, 0=ko */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1682 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1683 | pat_ref_delete(ref, key->area); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1684 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 1685 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 1686 | free_trash_chunk(key); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 1687 | break; |
| 1688 | } |
| 1689 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1690 | case ACT_HTTP_ADD_ACL: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 1691 | struct pat_ref *ref; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1692 | struct buffer *key; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 1693 | |
| 1694 | /* collect reference */ |
| 1695 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 1696 | if (!ref) |
| 1697 | continue; |
| 1698 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 1699 | /* allocate key */ |
| 1700 | key = alloc_trash_chunk(); |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 1701 | if (!key) { |
| 1702 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 1703 | goto end; |
| 1704 | } |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 1705 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 1706 | /* collect key */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1707 | key->data = build_logline(s, key->area, key->size, |
| 1708 | &rule->arg.map.key); |
| 1709 | key->area[key->data] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 1710 | |
| 1711 | /* perform update */ |
| 1712 | /* add entry only if it does not already exist */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1713 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1714 | if (pat_ref_find_elt(ref, key->area) == NULL) |
| 1715 | pat_ref_add(ref, key->area, NULL, NULL); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1716 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 1717 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 1718 | free_trash_chunk(key); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 1719 | break; |
| 1720 | } |
| 1721 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1722 | case ACT_HTTP_SET_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 1723 | struct pat_ref *ref; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1724 | struct buffer *key, *value; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 1725 | |
| 1726 | /* collect reference */ |
| 1727 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 1728 | if (!ref) |
| 1729 | continue; |
| 1730 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 1731 | /* allocate key */ |
| 1732 | key = alloc_trash_chunk(); |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 1733 | if (!key) { |
| 1734 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 1735 | goto end; |
| 1736 | } |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 1737 | |
| 1738 | /* allocate value */ |
| 1739 | value = alloc_trash_chunk(); |
| 1740 | if (!value) { |
| 1741 | free_trash_chunk(key); |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 1742 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 1743 | goto end; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 1744 | } |
| 1745 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 1746 | /* collect key */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1747 | key->data = build_logline(s, key->area, key->size, |
| 1748 | &rule->arg.map.key); |
| 1749 | key->area[key->data] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 1750 | |
| 1751 | /* collect value */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1752 | value->data = build_logline(s, value->area, |
| 1753 | value->size, |
| 1754 | &rule->arg.map.value); |
| 1755 | value->area[value->data] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 1756 | |
| 1757 | /* perform update */ |
Christopher Faulet | e84289e | 2019-04-19 14:50:55 +0200 | [diff] [blame] | 1758 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1759 | if (pat_ref_find_elt(ref, key->area) != NULL) |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 1760 | /* update entry if it exists */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1761 | pat_ref_set(ref, key->area, value->area, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 1762 | else |
| 1763 | /* insert a new entry */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1764 | pat_ref_add(ref, key->area, value->area, NULL); |
Christopher Faulet | e84289e | 2019-04-19 14:50:55 +0200 | [diff] [blame] | 1765 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 1766 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 1767 | free_trash_chunk(key); |
| 1768 | free_trash_chunk(value); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 1769 | break; |
| 1770 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 1771 | |
Frédéric Lécaille | 9ca51aa | 2018-11-12 10:06:54 +0100 | [diff] [blame] | 1772 | case ACT_HTTP_EARLY_HINT: |
| 1773 | if (!(txn->req.flags & HTTP_MSGF_VER_11)) |
| 1774 | break; |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1775 | early_hints = http_apply_early_hint_rule(s, early_hints, |
| 1776 | rule->arg.early_hint.name, |
| 1777 | rule->arg.early_hint.name_len, |
| 1778 | &rule->arg.early_hint.fmt); |
| 1779 | if (!early_hints) { |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 1780 | rule_ret = HTTP_RULE_RES_DONE; |
| 1781 | goto end; |
| 1782 | } |
Frédéric Lécaille | 9ca51aa | 2018-11-12 10:06:54 +0100 | [diff] [blame] | 1783 | break; |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 1784 | case ACT_CUSTOM: |
Willy Tarreau | 7aa15b0 | 2017-12-20 16:56:50 +0100 | [diff] [blame] | 1785 | if ((s->req.flags & CF_READ_ERROR) || |
| 1786 | ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) && |
Willy Tarreau | 7aa15b0 | 2017-12-20 16:56:50 +0100 | [diff] [blame] | 1787 | (px->options & PR_O_ABRT_CLOSE))) |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 1788 | act_flags |= ACT_FLAG_FINAL; |
Willy Tarreau | 3945868 | 2015-09-27 10:33:15 +0200 | [diff] [blame] | 1789 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 1790 | switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) { |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 1791 | case ACT_RET_ERR: |
| 1792 | case ACT_RET_CONT: |
| 1793 | break; |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 1794 | case ACT_RET_STOP: |
Christopher Faulet | 4629d08 | 2019-07-04 11:27:15 +0200 | [diff] [blame] | 1795 | rule_ret = HTTP_RULE_RES_STOP; |
| 1796 | goto end; |
| 1797 | case ACT_RET_DONE: |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 1798 | rule_ret = HTTP_RULE_RES_DONE; |
| 1799 | goto end; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 1800 | case ACT_RET_YIELD: |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 1801 | s->current_rule = rule; |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 1802 | rule_ret = HTTP_RULE_RES_YIELD; |
| 1803 | goto end; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 1804 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 1805 | break; |
| 1806 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1807 | case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX: |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 1808 | /* Note: only the first valid tracking parameter of each |
| 1809 | * applies. |
| 1810 | */ |
| 1811 | |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 1812 | if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) { |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 1813 | struct stktable *t; |
| 1814 | struct stksess *ts; |
| 1815 | struct stktable_key *key; |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 1816 | void *ptr1, *ptr2; |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 1817 | |
Thierry FOURNIER | 5ec63e0 | 2015-08-04 09:09:48 +0200 | [diff] [blame] | 1818 | t = rule->arg.trk_ctr.table.t; |
| 1819 | 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] | 1820 | |
| 1821 | if (key && (ts = stktable_get_entry(t, key))) { |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 1822 | stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 1823 | |
| 1824 | /* 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] | 1825 | ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT); |
| 1826 | ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE); |
| 1827 | if (ptr1 || ptr2) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1828 | HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 1829 | |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 1830 | if (ptr1) |
| 1831 | stktable_data_cast(ptr1, http_req_cnt)++; |
| 1832 | |
| 1833 | if (ptr2) |
| 1834 | update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate), |
| 1835 | t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1); |
| 1836 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1837 | HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock); |
Emeric Brun | 0fed0b0 | 2017-11-29 16:15:07 +0100 | [diff] [blame] | 1838 | |
| 1839 | /* If data was modified, we need to touch to re-schedule sync */ |
| 1840 | stktable_touch_local(t, ts, 0); |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 1841 | } |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 1842 | |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 1843 | stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT); |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 1844 | if (sess->fe != s->be) |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 1845 | stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 1846 | } |
| 1847 | } |
Adis Nezirovic | 2fbcafc | 2015-07-06 15:44:30 +0200 | [diff] [blame] | 1848 | break; |
| 1849 | |
Joseph Herlant | 5ba8025 | 2018-11-15 09:25:36 -0800 | [diff] [blame] | 1850 | /* other flags exists, but normally, they never be matched. */ |
Thierry FOURNIER | 22e4901 | 2015-08-05 19:13:48 +0200 | [diff] [blame] | 1851 | default: |
| 1852 | break; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 1853 | } |
| 1854 | } |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 1855 | |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 1856 | end: |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1857 | if (early_hints) { |
| 1858 | http_send_early_hints(s, early_hints); |
| 1859 | free_trash_chunk(early_hints); |
Frédéric Lécaille | 9ca51aa | 2018-11-12 10:06:54 +0100 | [diff] [blame] | 1860 | } |
| 1861 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 1862 | /* we reached the end of the rules, nothing to report */ |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 1863 | return rule_ret; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 1864 | } |
| 1865 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 1866 | |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 1867 | /* Executes the http-response rules <rules> for stream <s> and proxy <px>. It |
| 1868 | * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP, |
| 1869 | * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT |
| 1870 | * is returned, the process can continue the evaluation of next rule list. If |
| 1871 | * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ |
| 1872 | * is returned, it means the operation could not be processed and a server error |
| 1873 | * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a |
| 1874 | * deny rule. If *YIELD is returned, the caller must call again the function |
| 1875 | * with the same context. |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 1876 | */ |
Christopher Faulet | 10079f5 | 2018-10-03 15:17:28 +0200 | [diff] [blame] | 1877 | enum rule_result |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 1878 | 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] | 1879 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 1880 | struct session *sess = strm_sess(s); |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 1881 | struct http_txn *txn = s->txn; |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 1882 | struct act_rule *rule; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 1883 | struct hdr_ctx ctx; |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 1884 | enum rule_result rule_ret = HTTP_RULE_RES_CONT; |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 1885 | int act_flags = 0; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 1886 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 1887 | /* If "the current_rule_list" match the executed rule list, we are in |
| 1888 | * resume condition. If a resume is needed it is always in the action |
| 1889 | * and never in the ACL or converters. In this case, we initialise the |
| 1890 | * current rule, and go to the action execution point. |
| 1891 | */ |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 1892 | if (s->current_rule) { |
| 1893 | rule = s->current_rule; |
| 1894 | s->current_rule = NULL; |
| 1895 | if (s->current_rule_list == rules) |
| 1896 | goto resume_execution; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 1897 | } |
| 1898 | s->current_rule_list = rules; |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 1899 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 1900 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 1901 | |
| 1902 | /* check optional condition */ |
| 1903 | if (rule->cond) { |
| 1904 | int ret; |
| 1905 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 1906 | 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] | 1907 | ret = acl_pass(ret); |
| 1908 | |
| 1909 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 1910 | ret = !ret; |
| 1911 | |
| 1912 | if (!ret) /* condition not matched */ |
| 1913 | continue; |
| 1914 | } |
| 1915 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 1916 | act_flags |= ACT_FLAG_FIRST; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 1917 | resume_execution: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 1918 | switch (rule->action) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1919 | case ACT_ACTION_ALLOW: |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 1920 | rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */ |
| 1921 | goto end; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 1922 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1923 | case ACT_ACTION_DENY: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 1924 | txn->flags |= TX_SVDENY; |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 1925 | rule_ret = HTTP_RULE_RES_STOP; |
| 1926 | goto end; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 1927 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1928 | case ACT_HTTP_SET_NICE: |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 1929 | s->task->nice = rule->arg.nice; |
| 1930 | break; |
| 1931 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1932 | case ACT_HTTP_SET_TOS: |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 1933 | conn_set_tos(objt_conn(sess->origin), rule->arg.tos); |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 1934 | break; |
| 1935 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1936 | case ACT_HTTP_SET_MARK: |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 1937 | conn_set_mark(objt_conn(sess->origin), rule->arg.mark); |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 1938 | break; |
| 1939 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1940 | case ACT_HTTP_SET_LOGL: |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 1941 | s->logs.level = rule->arg.loglevel; |
| 1942 | break; |
| 1943 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1944 | case ACT_HTTP_REPLACE_HDR: |
| 1945 | case ACT_HTTP_REPLACE_VAL: |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 1946 | if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name, |
| 1947 | rule->arg.hdr_add.name_len, |
| 1948 | &rule->arg.hdr_add.fmt, |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 1949 | rule->arg.hdr_add.re, rule->action)) { |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 1950 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 1951 | goto end; |
| 1952 | } |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 1953 | break; |
| 1954 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1955 | case ACT_HTTP_DEL_HDR: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 1956 | ctx.idx = 0; |
| 1957 | /* remove all occurrences of the header */ |
| 1958 | 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] | 1959 | ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) { |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 1960 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 1961 | } |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 1962 | break; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 1963 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1964 | case ACT_HTTP_SET_HDR: |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 1965 | case ACT_HTTP_ADD_HDR: { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1966 | struct buffer *replace; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 1967 | |
| 1968 | replace = alloc_trash_chunk(); |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 1969 | if (!replace) { |
| 1970 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 1971 | goto end; |
| 1972 | } |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 1973 | |
| 1974 | chunk_printf(replace, "%s: ", rule->arg.hdr_add.name); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1975 | memcpy(replace->area, rule->arg.hdr_add.name, |
| 1976 | rule->arg.hdr_add.name_len); |
| 1977 | replace->data = rule->arg.hdr_add.name_len; |
| 1978 | replace->area[replace->data++] = ':'; |
| 1979 | replace->area[replace->data++] = ' '; |
| 1980 | replace->data += build_logline(s, |
| 1981 | replace->area + replace->data, |
| 1982 | replace->size - replace->data, |
| 1983 | &rule->arg.hdr_add.fmt); |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 1984 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1985 | if (rule->action == ACT_HTTP_SET_HDR) { |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 1986 | /* remove all occurrences of the header */ |
| 1987 | ctx.idx = 0; |
| 1988 | 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] | 1989 | ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 1990 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 1991 | } |
| 1992 | } |
Tim Duesterhus | 3fd1973 | 2018-05-27 20:35:08 +0200 | [diff] [blame] | 1993 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1994 | 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] | 1995 | static unsigned char rate_limit = 0; |
| 1996 | |
| 1997 | if ((rate_limit++ & 255) == 0) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1998 | replace->area[rule->arg.hdr_add.name_len] = 0; |
| 1999 | 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, |
| 2000 | replace->area, s->uniq_id); |
Tim Duesterhus | 3fd1973 | 2018-05-27 20:35:08 +0200 | [diff] [blame] | 2001 | } |
| 2002 | |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 2003 | _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1); |
Tim Duesterhus | 3fd1973 | 2018-05-27 20:35:08 +0200 | [diff] [blame] | 2004 | if (sess->fe != s->be) |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 2005 | _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1); |
Tim Duesterhus | 3fd1973 | 2018-05-27 20:35:08 +0200 | [diff] [blame] | 2006 | if (sess->listener->counters) |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 2007 | _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1); |
Tim Duesterhus | 3fd1973 | 2018-05-27 20:35:08 +0200 | [diff] [blame] | 2008 | if (objt_server(s->target)) |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 2009 | _HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_rewrites, 1); |
Tim Duesterhus | 3fd1973 | 2018-05-27 20:35:08 +0200 | [diff] [blame] | 2010 | } |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2011 | |
| 2012 | free_trash_chunk(replace); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2013 | break; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2014 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2015 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2016 | case ACT_HTTP_DEL_ACL: |
| 2017 | case ACT_HTTP_DEL_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2018 | struct pat_ref *ref; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 2019 | struct buffer *key; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2020 | |
| 2021 | /* collect reference */ |
| 2022 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 2023 | if (!ref) |
| 2024 | continue; |
| 2025 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2026 | /* allocate key */ |
| 2027 | key = alloc_trash_chunk(); |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 2028 | if (!key) { |
| 2029 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 2030 | goto end; |
| 2031 | } |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2032 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2033 | /* collect key */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2034 | key->data = build_logline(s, key->area, key->size, |
| 2035 | &rule->arg.map.key); |
| 2036 | key->area[key->data] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2037 | |
| 2038 | /* perform update */ |
| 2039 | /* returned code: 1=ok, 0=ko */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2040 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2041 | pat_ref_delete(ref, key->area); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2042 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2043 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2044 | free_trash_chunk(key); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2045 | break; |
| 2046 | } |
| 2047 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2048 | case ACT_HTTP_ADD_ACL: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2049 | struct pat_ref *ref; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 2050 | struct buffer *key; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2051 | |
| 2052 | /* collect reference */ |
| 2053 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 2054 | if (!ref) |
| 2055 | continue; |
| 2056 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2057 | /* allocate key */ |
| 2058 | key = alloc_trash_chunk(); |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 2059 | if (!key) { |
| 2060 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 2061 | goto end; |
| 2062 | } |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2063 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2064 | /* collect key */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2065 | key->data = build_logline(s, key->area, key->size, |
| 2066 | &rule->arg.map.key); |
| 2067 | key->area[key->data] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2068 | |
| 2069 | /* perform update */ |
| 2070 | /* check if the entry already exists */ |
Christopher Faulet | e84289e | 2019-04-19 14:50:55 +0200 | [diff] [blame] | 2071 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2072 | if (pat_ref_find_elt(ref, key->area) == NULL) |
| 2073 | pat_ref_add(ref, key->area, NULL, NULL); |
Christopher Faulet | e84289e | 2019-04-19 14:50:55 +0200 | [diff] [blame] | 2074 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2075 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2076 | free_trash_chunk(key); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2077 | break; |
| 2078 | } |
| 2079 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2080 | case ACT_HTTP_SET_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2081 | struct pat_ref *ref; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 2082 | struct buffer *key, *value; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2083 | |
| 2084 | /* collect reference */ |
| 2085 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 2086 | if (!ref) |
| 2087 | continue; |
| 2088 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2089 | /* allocate key */ |
| 2090 | key = alloc_trash_chunk(); |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 2091 | if (!key) { |
| 2092 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 2093 | goto end; |
| 2094 | } |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2095 | |
| 2096 | /* allocate value */ |
| 2097 | value = alloc_trash_chunk(); |
| 2098 | if (!value) { |
| 2099 | free_trash_chunk(key); |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 2100 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 2101 | goto end; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2102 | } |
| 2103 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2104 | /* collect key */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2105 | key->data = build_logline(s, key->area, key->size, |
| 2106 | &rule->arg.map.key); |
| 2107 | key->area[key->data] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2108 | |
| 2109 | /* collect value */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2110 | value->data = build_logline(s, value->area, |
| 2111 | value->size, |
| 2112 | &rule->arg.map.value); |
| 2113 | value->area[value->data] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2114 | |
| 2115 | /* perform update */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2116 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2117 | if (pat_ref_find_elt(ref, key->area) != NULL) |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2118 | /* update entry if it exists */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2119 | pat_ref_set(ref, key->area, value->area, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2120 | else |
| 2121 | /* insert a new entry */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2122 | pat_ref_add(ref, key->area, value->area, NULL); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2123 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2124 | free_trash_chunk(key); |
| 2125 | free_trash_chunk(value); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2126 | break; |
| 2127 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 2128 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2129 | case ACT_HTTP_REDIR: |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 2130 | rule_ret = HTTP_RULE_RES_DONE; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 2131 | if (!http_apply_redirect_rule(rule->arg.redir, s, txn)) |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 2132 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 2133 | goto end; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 2134 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 2135 | case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX: |
| 2136 | /* Note: only the first valid tracking parameter of each |
| 2137 | * applies. |
| 2138 | */ |
| 2139 | |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 2140 | if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) { |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 2141 | struct stktable *t; |
| 2142 | struct stksess *ts; |
| 2143 | struct stktable_key *key; |
| 2144 | void *ptr; |
| 2145 | |
| 2146 | t = rule->arg.trk_ctr.table.t; |
| 2147 | key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL); |
| 2148 | |
| 2149 | if (key && (ts = stktable_get_entry(t, key))) { |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 2150 | stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts); |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 2151 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2152 | HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock); |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 2153 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 2154 | /* let's count a new HTTP request as it's the first time we do it */ |
| 2155 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT); |
| 2156 | if (ptr) |
| 2157 | stktable_data_cast(ptr, http_req_cnt)++; |
| 2158 | |
| 2159 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE); |
| 2160 | if (ptr) |
| 2161 | update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate), |
| 2162 | t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1); |
| 2163 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 2164 | /* When the client triggers a 4xx from the server, it's most often due |
| 2165 | * to a missing object or permission. These events should be tracked |
| 2166 | * because if they happen often, it may indicate a brute force or a |
| 2167 | * vulnerability scan. Normally this is done when receiving the response |
| 2168 | * but here we're tracking after this ought to have been done so we have |
| 2169 | * to do it on purpose. |
| 2170 | */ |
Willy Tarreau | 3146a4c | 2016-07-26 15:22:33 +0200 | [diff] [blame] | 2171 | if ((unsigned)(txn->status - 400) < 100) { |
| 2172 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT); |
| 2173 | if (ptr) |
| 2174 | stktable_data_cast(ptr, http_err_cnt)++; |
| 2175 | |
| 2176 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE); |
| 2177 | if (ptr) |
| 2178 | update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate), |
| 2179 | t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1); |
| 2180 | } |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 2181 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2182 | HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock); |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 2183 | |
Emeric Brun | 0fed0b0 | 2017-11-29 16:15:07 +0100 | [diff] [blame] | 2184 | /* If data was modified, we need to touch to re-schedule sync */ |
| 2185 | stktable_touch_local(t, ts, 0); |
| 2186 | |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 2187 | stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT); |
| 2188 | if (sess->fe != s->be) |
| 2189 | stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND); |
| 2190 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 2191 | } |
| 2192 | } |
| 2193 | break; |
| 2194 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 2195 | case ACT_CUSTOM: |
Willy Tarreau | 7aa15b0 | 2017-12-20 16:56:50 +0100 | [diff] [blame] | 2196 | if ((s->req.flags & CF_READ_ERROR) || |
| 2197 | ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) && |
Willy Tarreau | 7aa15b0 | 2017-12-20 16:56:50 +0100 | [diff] [blame] | 2198 | (px->options & PR_O_ABRT_CLOSE))) |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2199 | act_flags |= ACT_FLAG_FINAL; |
Willy Tarreau | 3945868 | 2015-09-27 10:33:15 +0200 | [diff] [blame] | 2200 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2201 | switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) { |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 2202 | case ACT_RET_ERR: |
| 2203 | case ACT_RET_CONT: |
| 2204 | break; |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 2205 | case ACT_RET_STOP: |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 2206 | rule_ret = HTTP_RULE_RES_STOP; |
| 2207 | goto end; |
Christopher Faulet | 4629d08 | 2019-07-04 11:27:15 +0200 | [diff] [blame] | 2208 | case ACT_RET_DONE: |
| 2209 | rule_ret = HTTP_RULE_RES_DONE; |
| 2210 | goto end; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 2211 | case ACT_RET_YIELD: |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 2212 | s->current_rule = rule; |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 2213 | rule_ret = HTTP_RULE_RES_YIELD; |
| 2214 | goto end; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2215 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 2216 | break; |
| 2217 | |
Joseph Herlant | 5ba8025 | 2018-11-15 09:25:36 -0800 | [diff] [blame] | 2218 | /* other flags exists, but normally, they never be matched. */ |
Thierry FOURNIER | 22e4901 | 2015-08-05 19:13:48 +0200 | [diff] [blame] | 2219 | default: |
| 2220 | break; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2221 | } |
| 2222 | } |
| 2223 | |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 2224 | end: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2225 | /* we reached the end of the rules, nothing to report */ |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 2226 | return rule_ret; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2227 | } |
| 2228 | |
| 2229 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2230 | /* Perform an HTTP redirect based on the information in <rule>. The function |
| 2231 | * returns non-zero on success, or zero in case of a, irrecoverable error such |
| 2232 | * as too large a request to build a valid response. |
| 2233 | */ |
Christopher Faulet | 10079f5 | 2018-10-03 15:17:28 +0200 | [diff] [blame] | 2234 | 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] | 2235 | { |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 2236 | struct http_msg *req = &txn->req; |
| 2237 | struct http_msg *res = &txn->rsp; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2238 | const char *msg_fmt; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 2239 | struct buffer *chunk; |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2240 | int ret = 0; |
| 2241 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2242 | if (IS_HTX_STRM(s)) |
| 2243 | return htx_apply_redirect_rule(rule, s, txn); |
| 2244 | |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2245 | chunk = alloc_trash_chunk(); |
| 2246 | if (!chunk) |
| 2247 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2248 | |
| 2249 | /* build redirect message */ |
| 2250 | switch(rule->code) { |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 2251 | case 308: |
| 2252 | msg_fmt = HTTP_308; |
| 2253 | break; |
| 2254 | case 307: |
| 2255 | msg_fmt = HTTP_307; |
| 2256 | break; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2257 | case 303: |
| 2258 | msg_fmt = HTTP_303; |
| 2259 | break; |
| 2260 | case 301: |
| 2261 | msg_fmt = HTTP_301; |
| 2262 | break; |
| 2263 | case 302: |
| 2264 | default: |
| 2265 | msg_fmt = HTTP_302; |
| 2266 | break; |
| 2267 | } |
| 2268 | |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2269 | if (unlikely(!chunk_strcpy(chunk, msg_fmt))) |
| 2270 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2271 | |
| 2272 | switch(rule->type) { |
| 2273 | case REDIRECT_TYPE_SCHEME: { |
| 2274 | const char *path; |
| 2275 | const char *host; |
| 2276 | struct hdr_ctx ctx; |
| 2277 | int pathlen; |
| 2278 | int hostlen; |
| 2279 | |
| 2280 | host = ""; |
| 2281 | hostlen = 0; |
| 2282 | ctx.idx = 0; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 2283 | 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] | 2284 | host = ctx.line + ctx.val; |
| 2285 | hostlen = ctx.vlen; |
| 2286 | } |
| 2287 | |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 2288 | path = http_txn_get_path(txn); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2289 | /* build message using path */ |
| 2290 | if (path) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 2291 | 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] | 2292 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 2293 | int qs = 0; |
| 2294 | while (qs < pathlen) { |
| 2295 | if (path[qs] == '?') { |
| 2296 | pathlen = qs; |
| 2297 | break; |
| 2298 | } |
| 2299 | qs++; |
| 2300 | } |
| 2301 | } |
| 2302 | } else { |
| 2303 | path = "/"; |
| 2304 | pathlen = 1; |
| 2305 | } |
| 2306 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2307 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 2308 | /* check if we can add scheme + "://" + host + path */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2309 | if (chunk->data + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4) |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2310 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2311 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2312 | /* add scheme */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2313 | memcpy(chunk->area + chunk->data, rule->rdr_str, |
| 2314 | rule->rdr_len); |
| 2315 | chunk->data += rule->rdr_len; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2316 | } |
| 2317 | else { |
| 2318 | /* add scheme with executing log format */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2319 | chunk->data += build_logline(s, |
| 2320 | chunk->area + chunk->data, |
| 2321 | chunk->size - chunk->data, |
| 2322 | &rule->rdr_fmt); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2323 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2324 | /* check if we can add scheme + "://" + host + path */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2325 | if (chunk->data + 3 + hostlen + pathlen > chunk->size - 4) |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2326 | goto leave; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2327 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2328 | /* add "://" */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2329 | memcpy(chunk->area + chunk->data, "://", 3); |
| 2330 | chunk->data += 3; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2331 | |
| 2332 | /* add host */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2333 | memcpy(chunk->area + chunk->data, host, hostlen); |
| 2334 | chunk->data += hostlen; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2335 | |
| 2336 | /* add path */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2337 | memcpy(chunk->area + chunk->data, path, pathlen); |
| 2338 | chunk->data += pathlen; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2339 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2340 | /* 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] | 2341 | if (chunk->data && chunk->area[chunk->data - 1] != '/' && |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2342 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2343 | if (chunk->data > chunk->size - 5) |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2344 | goto leave; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2345 | chunk->area[chunk->data] = '/'; |
| 2346 | chunk->data++; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2347 | } |
| 2348 | |
| 2349 | break; |
| 2350 | } |
| 2351 | case REDIRECT_TYPE_PREFIX: { |
| 2352 | const char *path; |
| 2353 | int pathlen; |
| 2354 | |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 2355 | path = http_txn_get_path(txn); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2356 | /* build message using path */ |
| 2357 | if (path) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 2358 | 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] | 2359 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 2360 | int qs = 0; |
| 2361 | while (qs < pathlen) { |
| 2362 | if (path[qs] == '?') { |
| 2363 | pathlen = qs; |
| 2364 | break; |
| 2365 | } |
| 2366 | qs++; |
| 2367 | } |
| 2368 | } |
| 2369 | } else { |
| 2370 | path = "/"; |
| 2371 | pathlen = 1; |
| 2372 | } |
| 2373 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2374 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2375 | if (chunk->data + rule->rdr_len + pathlen > chunk->size - 4) |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2376 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2377 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2378 | /* add prefix. Note that if prefix == "/", we don't want to |
| 2379 | * add anything, otherwise it makes it hard for the user to |
| 2380 | * configure a self-redirection. |
| 2381 | */ |
| 2382 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2383 | memcpy(chunk->area + chunk->data, |
| 2384 | rule->rdr_str, rule->rdr_len); |
| 2385 | chunk->data += rule->rdr_len; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2386 | } |
| 2387 | } |
| 2388 | else { |
| 2389 | /* add prefix with executing log format */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2390 | chunk->data += build_logline(s, |
| 2391 | chunk->area + chunk->data, |
| 2392 | chunk->size - chunk->data, |
| 2393 | &rule->rdr_fmt); |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2394 | |
| 2395 | /* Check length */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2396 | if (chunk->data + pathlen > chunk->size - 4) |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2397 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2398 | } |
| 2399 | |
| 2400 | /* add path */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2401 | memcpy(chunk->area + chunk->data, path, pathlen); |
| 2402 | chunk->data += pathlen; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2403 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2404 | /* 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] | 2405 | if (chunk->data && chunk->area[chunk->data - 1] != '/' && |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2406 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2407 | if (chunk->data > chunk->size - 5) |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2408 | goto leave; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2409 | chunk->area[chunk->data] = '/'; |
| 2410 | chunk->data++; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2411 | } |
| 2412 | |
| 2413 | break; |
| 2414 | } |
| 2415 | case REDIRECT_TYPE_LOCATION: |
| 2416 | default: |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2417 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2418 | if (chunk->data + rule->rdr_len > chunk->size - 4) |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2419 | goto leave; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2420 | |
| 2421 | /* add location */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2422 | memcpy(chunk->area + chunk->data, rule->rdr_str, |
| 2423 | rule->rdr_len); |
| 2424 | chunk->data += rule->rdr_len; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2425 | } |
| 2426 | else { |
| 2427 | /* add location with executing log format */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2428 | chunk->data += build_logline(s, |
| 2429 | chunk->area + chunk->data, |
| 2430 | chunk->size - chunk->data, |
| 2431 | &rule->rdr_fmt); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2432 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2433 | /* Check left length */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2434 | if (chunk->data > chunk->size - 4) |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2435 | goto leave; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2436 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2437 | break; |
| 2438 | } |
| 2439 | |
| 2440 | if (rule->cookie_len) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2441 | memcpy(chunk->area + chunk->data, "\r\nSet-Cookie: ", 14); |
| 2442 | chunk->data += 14; |
| 2443 | memcpy(chunk->area + chunk->data, rule->cookie_str, |
| 2444 | rule->cookie_len); |
| 2445 | chunk->data += rule->cookie_len; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2446 | } |
| 2447 | |
Willy Tarreau | 19b1412 | 2017-02-28 09:48:11 +0100 | [diff] [blame] | 2448 | /* add end of headers and the keep-alive/close status. */ |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2449 | txn->status = rule->code; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2450 | |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 2451 | 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] | 2452 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 2453 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 2454 | /* keep-alive possible */ |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 2455 | if (!(req->flags & HTTP_MSGF_VER_11)) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2456 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2457 | memcpy(chunk->area + chunk->data, |
| 2458 | "\r\nProxy-Connection: keep-alive", 30); |
| 2459 | chunk->data += 30; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2460 | } else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2461 | memcpy(chunk->area + chunk->data, |
| 2462 | "\r\nConnection: keep-alive", 24); |
| 2463 | chunk->data += 24; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2464 | } |
| 2465 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2466 | memcpy(chunk->area + chunk->data, "\r\n\r\n", 4); |
| 2467 | chunk->data += 4; |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2468 | FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk)); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2469 | co_inject(res->chn, chunk->area, chunk->data); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2470 | /* "eat" the request */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2471 | b_del(&req->chn->buf, req->sov); |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 2472 | req->next -= req->sov; |
| 2473 | req->sov = 0; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 2474 | 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] | 2475 | 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] | 2476 | req->msg_state = HTTP_MSG_CLOSED; |
| 2477 | res->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 2478 | /* Trim any possible response */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2479 | b_set_data(&res->chn->buf, co_data(res->chn)); |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 2480 | res->next = res->sov = 0; |
Christopher Faulet | 5d468ca | 2017-09-11 09:27:29 +0200 | [diff] [blame] | 2481 | /* let the server side turn to SI_ST_CLO */ |
| 2482 | channel_shutw_now(req->chn); |
Willy Tarreau | 67bd3fc | 2019-10-22 18:13:44 +0200 | [diff] [blame] | 2483 | channel_dont_connect(req->chn); |
Christopher Faulet | 7d84db3 | 2020-01-28 09:18:10 +0100 | [diff] [blame] | 2484 | |
| 2485 | if (rule->flags & REDIRECT_FLAG_FROM_REQ) { |
| 2486 | /* let's log the request time */ |
| 2487 | s->logs.tv_request = now; |
| 2488 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2489 | } else { |
| 2490 | /* keep-alive not possible */ |
| 2491 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2492 | memcpy(chunk->area + chunk->data, |
| 2493 | "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 2494 | chunk->data += 29; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2495 | } else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2496 | memcpy(chunk->area + chunk->data, |
| 2497 | "\r\nConnection: close\r\n\r\n", 23); |
| 2498 | chunk->data += 23; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2499 | } |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2500 | http_reply_and_close(s, txn->status, chunk); |
Christopher Faulet | 7d84db3 | 2020-01-28 09:18:10 +0100 | [diff] [blame] | 2501 | |
| 2502 | if (rule->flags & REDIRECT_FLAG_FROM_REQ) { |
| 2503 | /* let's log the request time */ |
| 2504 | s->logs.tv_request = now; |
| 2505 | req->chn->analysers &= AN_REQ_FLT_END; |
| 2506 | if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 2507 | HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1); |
| 2508 | |
| 2509 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2510 | } |
| 2511 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2512 | if (!(s->flags & SF_ERR_MASK)) |
| 2513 | s->flags |= SF_ERR_LOCAL; |
| 2514 | if (!(s->flags & SF_FINST_MASK)) |
Christopher Faulet | 7d84db3 | 2020-01-28 09:18:10 +0100 | [diff] [blame] | 2515 | s->flags |= ((rule->flags & REDIRECT_FLAG_FROM_REQ) ? SF_FINST_R : SF_FINST_H); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2516 | |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2517 | ret = 1; |
| 2518 | leave: |
| 2519 | free_trash_chunk(chunk); |
| 2520 | return ret; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2521 | } |
| 2522 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2523 | /* This stream analyser runs all HTTP request processing which is common to |
| 2524 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 2525 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2526 | * 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] | 2527 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 2528 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2529 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2530 | 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] | 2531 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2532 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 2533 | struct http_txn *txn = s->txn; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2534 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2535 | struct redirect_rule *rule; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 2536 | struct cond_wordlist *wl; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2537 | enum rule_result verdict; |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 2538 | int deny_status = HTTP_ERR_403; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2539 | struct connection *conn = objt_conn(sess->origin); |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2540 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2541 | if (IS_HTX_STRM(s)) |
| 2542 | return htx_process_req_common(s, req, an_bit, px); |
| 2543 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2544 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2545 | /* we need more data */ |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2546 | goto return_prx_yield; |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2547 | } |
| 2548 | |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 2549 | 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] | 2550 | now_ms, __FUNCTION__, |
| 2551 | s, |
| 2552 | req, |
| 2553 | req->rex, req->wex, |
| 2554 | req->flags, |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 2555 | ci_data(req), |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2556 | req->analysers); |
| 2557 | |
Christopher Faulet | 1907ccc | 2019-04-29 13:12:02 +0200 | [diff] [blame] | 2558 | /* just in case we have some per-backend tracking. Only called the first |
| 2559 | * execution of the analyser. */ |
| 2560 | if (!s->current_rule || s->current_rule_list != &px->http_req_rules) |
| 2561 | stream_inc_be_http_req_ctr(s); |
Willy Tarreau | 6541083 | 2014-04-28 21:25:43 +0200 | [diff] [blame] | 2562 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2563 | /* evaluate http-request rules */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2564 | if (!LIST_ISEMPTY(&px->http_req_rules)) { |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 2565 | 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] | 2566 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2567 | switch (verdict) { |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2568 | case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */ |
| 2569 | goto return_prx_yield; |
| 2570 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2571 | case HTTP_RULE_RES_CONT: |
| 2572 | case HTTP_RULE_RES_STOP: /* nothing to do */ |
| 2573 | break; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 2574 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2575 | case HTTP_RULE_RES_DENY: /* deny or tarpit */ |
| 2576 | if (txn->flags & TX_CLTARPIT) |
| 2577 | goto tarpit; |
| 2578 | goto deny; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 2579 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2580 | case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */ |
| 2581 | goto return_prx_cond; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 2582 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2583 | case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */ |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 2584 | goto done; |
| 2585 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2586 | case HTTP_RULE_RES_BADREQ: /* failed with a bad request */ |
| 2587 | goto return_bad_req; |
| 2588 | } |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 2589 | } |
| 2590 | |
Olivier Houchard | 25ae45a | 2017-11-29 19:51:19 +0100 | [diff] [blame] | 2591 | if (conn && (conn->flags & CO_FL_EARLY_DATA) && |
| 2592 | (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2593 | struct hdr_ctx ctx; |
| 2594 | |
| 2595 | ctx.idx = 0; |
| 2596 | if (!http_find_header2("Early-Data", strlen("Early-Data"), |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 2597 | ci_head(&s->req), &txn->hdr_idx, &ctx)) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2598 | if (unlikely(http_header_add_tail2(&txn->req, |
| 2599 | &txn->hdr_idx, "Early-Data: 1", |
Christopher Faulet | 005e79e | 2018-07-20 09:54:26 +0200 | [diff] [blame] | 2600 | strlen("Early-Data: 1")) < 0)) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2601 | goto return_bad_req; |
| 2602 | } |
| 2603 | } |
| 2604 | |
| 2605 | } |
| 2606 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 2607 | /* OK at this stage, we know that the request was accepted according to |
| 2608 | * the http-request rules, we can check for the stats. Note that the |
| 2609 | * URI is detected *before* the req* rules in order not to be affected |
| 2610 | * by a possible reqrep, while they are processed *after* so that a |
| 2611 | * reqdeny can still block them. This clearly needs to change in 1.6! |
| 2612 | */ |
Christopher Faulet | 4629d08 | 2019-07-04 11:27:15 +0200 | [diff] [blame] | 2613 | if (!s->target && stats_check_uri(&s->si[1], txn, px)) { |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 2614 | s->target = &http_stats_applet.obj_type; |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 2615 | if (unlikely(!si_register_handler(&s->si[1], objt_applet(s->target)))) { |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 2616 | txn->status = 500; |
| 2617 | s->logs.tv_request = now; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 2618 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2619 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2620 | if (!(s->flags & SF_ERR_MASK)) |
| 2621 | s->flags |= SF_ERR_RESOURCE; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 2622 | goto return_prx_cond; |
| 2623 | } |
| 2624 | |
| 2625 | /* parse the whole stats request and extract the relevant information */ |
| 2626 | http_handle_stats(s, req); |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 2627 | 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] | 2628 | /* not all actions implemented: deny, allow, auth */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 2629 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2630 | if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */ |
| 2631 | goto deny; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 2632 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2633 | if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */ |
| 2634 | goto return_prx_cond; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 2635 | } |
| 2636 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 2637 | /* evaluate the req* rules except reqadd */ |
| 2638 | if (px->req_exp != NULL) { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 2639 | if (apply_filters_to_request(s, req, px) < 0) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2640 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2641 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 2642 | if (txn->flags & TX_CLDENY) |
| 2643 | goto deny; |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 2644 | |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 2645 | if (txn->flags & TX_CLTARPIT) { |
| 2646 | deny_status = HTTP_ERR_500; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 2647 | goto tarpit; |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 2648 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2649 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2650 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2651 | /* add request headers from the rule sets in the same order */ |
| 2652 | list_for_each_entry(wl, &px->req_add, list) { |
| 2653 | if (wl->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 2654 | 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] | 2655 | ret = acl_pass(ret); |
| 2656 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 2657 | ret = !ret; |
| 2658 | if (!ret) |
| 2659 | continue; |
| 2660 | } |
| 2661 | |
Christopher Faulet | 10079f5 | 2018-10-03 15:17:28 +0200 | [diff] [blame] | 2662 | 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] | 2663 | goto return_bad_req; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 2664 | } |
| 2665 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 2666 | |
| 2667 | /* Proceed with the stats now. */ |
Christopher Faulet | 2571bc6 | 2019-03-01 11:44:26 +0100 | [diff] [blame] | 2668 | if (unlikely(objt_applet(s->target))) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2669 | /* process the stats request now */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2670 | if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */ |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 2671 | _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1); |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 2672 | |
Christopher Faulet | bcf242a | 2019-03-01 11:36:26 +0100 | [diff] [blame] | 2673 | http_handle_expect_hdr(s, req, msg); |
| 2674 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2675 | if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is |
| 2676 | s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy |
| 2677 | if (!(s->flags & SF_FINST_MASK)) |
| 2678 | s->flags |= SF_FINST_R; |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 2679 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 2680 | /* 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] | 2681 | req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END); |
| 2682 | req->analysers &= ~AN_REQ_FLT_XFER_DATA; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 2683 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Christopher Faulet | bcf242a | 2019-03-01 11:36:26 +0100 | [diff] [blame] | 2684 | |
| 2685 | req->flags |= CF_SEND_DONTWAIT; |
| 2686 | s->flags |= SF_ASSIGNED; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 2687 | goto done; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2688 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 2689 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2690 | /* check whether we have some ACLs set to redirect this request */ |
| 2691 | list_for_each_entry(rule, &px->redirect_rules, list) { |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 2692 | if (rule->cond) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2693 | int ret; |
| 2694 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 2695 | 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] | 2696 | ret = acl_pass(ret); |
| 2697 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 2698 | ret = !ret; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2699 | if (!ret) |
| 2700 | continue; |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 2701 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2702 | if (!http_apply_redirect_rule(rule, s, txn)) |
| 2703 | goto return_bad_req; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 2704 | goto done; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2705 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2706 | |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 2707 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 2708 | * If this happens, then the data will not come immediately, so we must |
| 2709 | * send all what we have without waiting. Note that due to the small gain |
| 2710 | * 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] | 2711 | * 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] | 2712 | * itself once used. |
| 2713 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2714 | req->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 2715 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 2716 | done: /* done with this analyser, continue with next ones that the calling |
| 2717 | * points will have set, if any. |
| 2718 | */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 2719 | req->analyse_exp = TICK_ETERNITY; |
Thierry FOURNIER | 7566e30 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 2720 | done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */ |
| 2721 | req->analysers &= ~an_bit; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2722 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2723 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 2724 | tarpit: |
Willy Tarreau | 6a0bca9 | 2017-06-11 17:56:27 +0200 | [diff] [blame] | 2725 | /* Allow cookie logging |
| 2726 | */ |
| 2727 | if (s->be->cookie_name || sess->fe->capture_name) |
| 2728 | manage_client_side_cookies(s, req); |
| 2729 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 2730 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 2731 | * which may be the same as the connect timeout if unspecified. |
| 2732 | * If unset, then set it to zero because we really want it to |
| 2733 | * eventually expire. We build the tarpit as an analyser. |
| 2734 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2735 | channel_erase(&s->req); |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 2736 | |
| 2737 | /* wipe the request out so that we can drop the connection early |
| 2738 | * if the client closes first. |
| 2739 | */ |
| 2740 | channel_dont_connect(req); |
Bertrand Paquet | 83a2c3d | 2016-04-06 11:58:31 +0200 | [diff] [blame] | 2741 | |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 2742 | txn->status = http_err_codes[deny_status]; |
| 2743 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 2744 | req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 2745 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 2746 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 2747 | if (!req->analyse_exp) |
| 2748 | req->analyse_exp = tick_add(now_ms, 0); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2749 | stream_inc_http_err_ctr(s); |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 2750 | _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2751 | if (sess->fe != s->be) |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 2752 | _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2753 | if (sess->listener->counters) |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 2754 | _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1); |
Thierry FOURNIER | 7566e30 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 2755 | goto done_without_exp; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 2756 | |
| 2757 | deny: /* this request was blocked (denied) */ |
Bertrand Paquet | 83a2c3d | 2016-04-06 11:58:31 +0200 | [diff] [blame] | 2758 | |
| 2759 | /* Allow cookie logging |
| 2760 | */ |
| 2761 | if (s->be->cookie_name || sess->fe->capture_name) |
| 2762 | manage_client_side_cookies(s, req); |
| 2763 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2764 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 2765 | txn->status = http_err_codes[deny_status]; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 2766 | s->logs.tv_request = now; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 2767 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2768 | stream_inc_http_err_ctr(s); |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 2769 | _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2770 | if (sess->fe != s->be) |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 2771 | _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2772 | if (sess->listener->counters) |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 2773 | _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1); |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 2774 | goto return_prx_cond; |
| 2775 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2776 | return_bad_req: |
| 2777 | /* We centralize bad requests processing here */ |
| 2778 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 2779 | /* we detected a parsing error. We want to archive this request |
| 2780 | * in the dedicated proxy area for later troubleshooting. |
| 2781 | */ |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 2782 | 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] | 2783 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2784 | |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 2785 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2786 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 2787 | txn->status = 400; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 2788 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2789 | |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 2790 | _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2791 | if (sess->listener->counters) |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 2792 | _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2793 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2794 | return_prx_cond: |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2795 | if (!(s->flags & SF_ERR_MASK)) |
| 2796 | s->flags |= SF_ERR_PRXCOND; |
| 2797 | if (!(s->flags & SF_FINST_MASK)) |
| 2798 | s->flags |= SF_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2799 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 2800 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2801 | req->analyse_exp = TICK_ETERNITY; |
| 2802 | return 0; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2803 | |
| 2804 | return_prx_yield: |
| 2805 | channel_dont_connect(req); |
| 2806 | return 0; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2807 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2808 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2809 | /* This function performs all the processing enabled for the current request. |
| 2810 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 2811 | * needs more data, encounters an error, or wants to immediately abort the |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2812 | * request. It relies on buffers flags, and updates s->req.analysers. |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2813 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2814 | 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] | 2815 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2816 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 2817 | struct http_txn *txn = s->txn; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2818 | struct http_msg *msg = &txn->req; |
Willy Tarreau | ee335e6 | 2015-04-21 18:15:13 +0200 | [diff] [blame] | 2819 | struct connection *cli_conn = objt_conn(strm_sess(s)->origin); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2820 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2821 | if (IS_HTX_STRM(s)) |
| 2822 | return htx_process_request(s, req, an_bit); |
| 2823 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2824 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2825 | /* we need more data */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2826 | channel_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2827 | return 0; |
| 2828 | } |
| 2829 | |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 2830 | 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] | 2831 | now_ms, __FUNCTION__, |
| 2832 | s, |
| 2833 | req, |
| 2834 | req->rex, req->wex, |
| 2835 | req->flags, |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 2836 | ci_data(req), |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2837 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2838 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2839 | /* |
| 2840 | * Right now, we know that we have processed the entire headers |
| 2841 | * and that unwanted requests have been filtered out. We can do |
| 2842 | * whatever we want with the remaining request. Also, now we |
| 2843 | * may have separate values for ->fe, ->be. |
| 2844 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2845 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2846 | /* |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2847 | * If HTTP PROXY is set we simply get remote server address parsing |
| 2848 | * incoming request. Note that this requires that a connection is |
| 2849 | * allocated on the server side. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2850 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2851 | 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] | 2852 | struct connection *conn; |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 2853 | char *path; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 2854 | |
Willy Tarreau | 9471b8c | 2013-12-15 13:31:35 +0100 | [diff] [blame] | 2855 | /* Note that for now we don't reuse existing proxy connections */ |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2856 | 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] | 2857 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 2858 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 2859 | txn->status = 500; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 2860 | req->analysers &= AN_REQ_FLT_END; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 2861 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 2862 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2863 | if (!(s->flags & SF_ERR_MASK)) |
| 2864 | s->flags |= SF_ERR_RESOURCE; |
| 2865 | if (!(s->flags & SF_FINST_MASK)) |
| 2866 | s->flags |= SF_FINST_R; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 2867 | |
| 2868 | return 0; |
| 2869 | } |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 2870 | |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 2871 | path = http_txn_get_path(txn); |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 2872 | if (url2sa(ci_head(req) + msg->sl.rq.u, |
| 2873 | 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] | 2874 | &conn->addr.to, NULL) == -1) |
| 2875 | goto return_bad_req; |
| 2876 | |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 2877 | /* if the path was found, we have to remove everything between |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 2878 | * ci_head(req) + msg->sl.rq.u and path (excluded). If it was not |
| 2879 | * 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] | 2880 | * u_l characters by a single "/". |
| 2881 | */ |
| 2882 | if (path) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 2883 | char *cur_ptr = ci_head(req); |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 2884 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 2885 | int delta; |
| 2886 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2887 | 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] | 2888 | http_msg_move_end(&txn->req, delta); |
| 2889 | cur_end += delta; |
| 2890 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 2891 | goto return_bad_req; |
| 2892 | } |
| 2893 | else { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 2894 | char *cur_ptr = ci_head(req); |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 2895 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 2896 | int delta; |
| 2897 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2898 | delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u, |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 2899 | cur_ptr + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1); |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 2900 | http_msg_move_end(&txn->req, delta); |
| 2901 | cur_end += delta; |
| 2902 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 2903 | goto return_bad_req; |
| 2904 | } |
Willy Tarreau | 3284dd2 | 2019-07-18 16:17:15 +0200 | [diff] [blame] | 2905 | conn->target = &s->be->obj_type; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2906 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2907 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2908 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 2909 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2910 | * Note that doing so might move headers in the request, but |
| 2911 | * the fields will stay coherent and the URI will not move. |
| 2912 | * This should only be performed in the backend. |
| 2913 | */ |
Bertrand Paquet | 83a2c3d | 2016-04-06 11:58:31 +0200 | [diff] [blame] | 2914 | if (s->be->cookie_name || sess->fe->capture_name) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2915 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2916 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 2917 | /* add unique-id if "header-unique-id" is specified */ |
| 2918 | |
Thierry Fournier | f4011dd | 2016-03-29 17:23:51 +0200 | [diff] [blame] | 2919 | if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2920 | if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL) |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 2921 | goto return_bad_req; |
| 2922 | s->unique_id[0] = '\0'; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2923 | 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] | 2924 | } |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 2925 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2926 | if (sess->fe->header_unique_id && s->unique_id) { |
Willy Tarreau | 5f6333c | 2018-08-22 05:14:37 +0200 | [diff] [blame] | 2927 | 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] | 2928 | goto return_bad_req; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2929 | 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] | 2930 | goto return_bad_req; |
| 2931 | } |
| 2932 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 2933 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2934 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 2935 | * asks for it. |
| 2936 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2937 | if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 2938 | struct hdr_ctx ctx = { .idx = 0 }; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2939 | if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) && |
| 2940 | http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name, |
| 2941 | 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] | 2942 | ci_head(req), &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 2943 | /* The header is set to be added only if none is present |
| 2944 | * and we found it, so don't do anything. |
| 2945 | */ |
| 2946 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2947 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2948 | /* Add an X-Forwarded-For header unless the source IP is |
| 2949 | * in the 'except' network range. |
| 2950 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2951 | if ((!sess->fe->except_mask.s_addr || |
| 2952 | (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr) |
| 2953 | != sess->fe->except_net.s_addr) && |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2954 | (!s->be->except_mask.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2955 | (((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] | 2956 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2957 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2958 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2959 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr; |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 2960 | |
| 2961 | /* Note: we rely on the backend to get the header name to be used for |
| 2962 | * x-forwarded-for, because the header is really meant for the backends. |
| 2963 | * However, if the backend did not specify any option, we have to rely |
| 2964 | * on the frontend's header name. |
| 2965 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2966 | if (s->be->fwdfor_hdr_len) { |
| 2967 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2968 | memcpy(trash.area, |
| 2969 | s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 2970 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2971 | len = sess->fe->fwdfor_hdr_len; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2972 | memcpy(trash.area, |
| 2973 | sess->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 2974 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2975 | len += snprintf(trash.area + len, |
| 2976 | trash.size - len, |
| 2977 | ": %d.%d.%d.%d", pn[0], pn[1], |
| 2978 | pn[2], pn[3]); |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 2979 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2980 | 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] | 2981 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2982 | } |
| 2983 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2984 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2985 | /* FIXME: for the sake of completeness, we should also support |
| 2986 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2987 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2988 | int len; |
| 2989 | char pn[INET6_ADDRSTRLEN]; |
| 2990 | inet_ntop(AF_INET6, |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2991 | (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2992 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2993 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2994 | /* Note: we rely on the backend to get the header name to be used for |
| 2995 | * x-forwarded-for, because the header is really meant for the backends. |
| 2996 | * However, if the backend did not specify any option, we have to rely |
| 2997 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2998 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2999 | if (s->be->fwdfor_hdr_len) { |
| 3000 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3001 | memcpy(trash.area, s->be->fwdfor_hdr_name, |
| 3002 | len); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3003 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3004 | len = sess->fe->fwdfor_hdr_len; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3005 | memcpy(trash.area, sess->fe->fwdfor_hdr_name, |
| 3006 | len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3007 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3008 | len += snprintf(trash.area + len, trash.size - len, |
| 3009 | ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3010 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3011 | 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] | 3012 | goto return_bad_req; |
| 3013 | } |
| 3014 | } |
| 3015 | |
| 3016 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3017 | * 10: add X-Original-To if either the frontend or the backend |
| 3018 | * asks for it. |
| 3019 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3020 | if ((sess->fe->options | s->be->options) & PR_O_ORGTO) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3021 | |
| 3022 | /* FIXME: don't know if IPv6 can handle that case too. */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3023 | if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3024 | /* Add an X-Original-To header unless the destination IP is |
| 3025 | * in the 'except' network range. |
| 3026 | */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3027 | conn_get_to_addr(cli_conn); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3028 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3029 | if (cli_conn->addr.to.ss_family == AF_INET && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3030 | ((!sess->fe->except_mask_to.s_addr || |
| 3031 | (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr) |
| 3032 | != sess->fe->except_to.s_addr) && |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 3033 | (!s->be->except_mask_to.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3034 | (((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] | 3035 | != s->be->except_to.s_addr))) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3036 | int len; |
| 3037 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3038 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr; |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3039 | |
| 3040 | /* Note: we rely on the backend to get the header name to be used for |
| 3041 | * x-original-to, because the header is really meant for the backends. |
| 3042 | * However, if the backend did not specify any option, we have to rely |
| 3043 | * on the frontend's header name. |
| 3044 | */ |
| 3045 | if (s->be->orgto_hdr_len) { |
| 3046 | len = s->be->orgto_hdr_len; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3047 | memcpy(trash.area, |
| 3048 | s->be->orgto_hdr_name, len); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3049 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3050 | len = sess->fe->orgto_hdr_len; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3051 | memcpy(trash.area, |
| 3052 | sess->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3053 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3054 | len += snprintf(trash.area + len, |
| 3055 | trash.size - len, |
| 3056 | ": %d.%d.%d.%d", pn[0], pn[1], |
| 3057 | pn[2], pn[3]); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3058 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3059 | 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] | 3060 | goto return_bad_req; |
| 3061 | } |
| 3062 | } |
| 3063 | } |
| 3064 | |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 3065 | /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. |
| 3066 | * If an "Upgrade" token is found, the header is left untouched in order not to have |
| 3067 | * to deal with some servers bugs : some of them fail an Upgrade if anything but |
| 3068 | * "Upgrade" is present in the Connection header. |
| 3069 | */ |
Christopher Faulet | 315b39c | 2018-09-21 16:26:19 +0200 | [diff] [blame] | 3070 | 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] | 3071 | unsigned int want_flags = 0; |
| 3072 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3073 | if (msg->flags & HTTP_MSGF_VER_11) { |
Christopher Faulet | 315b39c | 2018-09-21 16:26:19 +0200 | [diff] [blame] | 3074 | if ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3075 | !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3076 | want_flags |= TX_CON_CLO_SET; |
| 3077 | } else { |
Christopher Faulet | 315b39c | 2018-09-21 16:26:19 +0200 | [diff] [blame] | 3078 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3079 | ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3080 | want_flags |= TX_CON_KAL_SET; |
| 3081 | } |
| 3082 | |
| 3083 | 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] | 3084 | http_change_connection_header(txn, msg, want_flags); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3085 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3086 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3087 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3088 | /* If we have no server assigned yet and we're balancing on url_param |
| 3089 | * with a POST request, we may be interested in checking the body for |
| 3090 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3091 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3092 | if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) && |
Willy Tarreau | 089eaa0 | 2019-01-14 15:17:46 +0100 | [diff] [blame] | 3093 | s->txn->meth == HTTP_METH_POST && |
| 3094 | (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3095 | (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3096 | channel_dont_connect(req); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3097 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3098 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3099 | |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 3100 | req->analysers &= ~AN_REQ_FLT_XFER_DATA; |
| 3101 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 3102 | |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 3103 | /* We expect some data from the client. Unless we know for sure |
| 3104 | * we already have a full request, we have to re-enable quick-ack |
| 3105 | * in case we previously disabled it, otherwise we might cause |
| 3106 | * the client to delay further data. |
| 3107 | */ |
| 3108 | if ((sess->listener->options & LI_O_NOQUICKACK) && |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 3109 | ((msg->flags & HTTP_MSGF_TE_CHNK) || |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3110 | (msg->body_len > ci_data(req) - txn->req.eoh - 2))) |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 3111 | conn_set_quickack(cli_conn, 1); |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 3112 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3113 | /************************************************************* |
| 3114 | * OK, that's finished for the headers. We have done what we * |
| 3115 | * could. Let's switch to the DATA state. * |
| 3116 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3117 | req->analyse_exp = TICK_ETERNITY; |
| 3118 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3119 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3120 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3121 | /* OK let's go on with the BODY now */ |
| 3122 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3123 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3124 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3125 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 3126 | /* we detected a parsing error. We want to archive this request |
| 3127 | * in the dedicated proxy area for later troubleshooting. |
| 3128 | */ |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 3129 | 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] | 3130 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3131 | |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 3132 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3133 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3134 | txn->status = 400; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3135 | req->analysers &= AN_REQ_FLT_END; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3136 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3137 | |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 3138 | _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3139 | if (sess->listener->counters) |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 3140 | _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3141 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3142 | if (!(s->flags & SF_ERR_MASK)) |
| 3143 | s->flags |= SF_ERR_PRXCOND; |
| 3144 | if (!(s->flags & SF_FINST_MASK)) |
| 3145 | s->flags |= SF_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3146 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3147 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3148 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3149 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 3150 | * returns zero, at the beginning because it prevents any other processing |
| 3151 | * from occurring, and at the end because it terminates the request. |
| 3152 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3153 | 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] | 3154 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3155 | struct http_txn *txn = s->txn; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3156 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 3157 | if (IS_HTX_STRM(s)) |
| 3158 | return htx_process_tarpit(s, req, an_bit); |
| 3159 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3160 | /* This connection is being tarpitted. The CLIENT side has |
| 3161 | * already set the connect expiration date to the right |
| 3162 | * timeout. We just have to check that the client is still |
| 3163 | * there and that the timeout has not expired. |
| 3164 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3165 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 3166 | if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 && |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3167 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 3168 | return 0; |
| 3169 | |
| 3170 | /* We will set the queue timer to the time spent, just for |
| 3171 | * logging purposes. We fake a 500 server error, so that the |
| 3172 | * attacker will not suspect his connection has been tarpitted. |
| 3173 | * It will not cause trouble to the logs because we can exclude |
| 3174 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 3175 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3176 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 3177 | |
Christopher Faulet | ef9a169 | 2020-02-21 10:20:46 +0100 | [diff] [blame] | 3178 | http_reply_and_close(s, txn->status, (!(req->flags & CF_READ_ERROR) ? http_error_message(s) : NULL)); |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3179 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3180 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3181 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3182 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3183 | if (!(s->flags & SF_ERR_MASK)) |
| 3184 | s->flags |= SF_ERR_PRXCOND; |
| 3185 | if (!(s->flags & SF_FINST_MASK)) |
| 3186 | s->flags |= SF_FINST_T; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3187 | return 0; |
| 3188 | } |
| 3189 | |
Willy Tarreau | 5a8f947 | 2014-04-10 11:16:06 +0200 | [diff] [blame] | 3190 | /* This function is an analyser which waits for the HTTP request body. It waits |
| 3191 | * for either the buffer to be full, or the full advertised contents to have |
| 3192 | * reached the buffer. It must only be called after the standard HTTP request |
| 3193 | * processing has occurred, because it expects the request to be parsed and will |
| 3194 | * look for the Expect header. It may send a 100-Continue interim response. It |
| 3195 | * takes in input any state starting from HTTP_MSG_BODY and leaves with one of |
| 3196 | * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it |
| 3197 | * 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] | 3198 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3199 | 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] | 3200 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3201 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3202 | struct http_txn *txn = s->txn; |
| 3203 | struct http_msg *msg = &s->txn->req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3204 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 3205 | if (IS_HTX_STRM(s)) |
| 3206 | return htx_wait_for_request_body(s, req, an_bit); |
| 3207 | |
Christopher Faulet | 87451fd | 2019-03-01 11:16:34 +0100 | [diff] [blame] | 3208 | if (msg->msg_state < HTTP_MSG_BODY) |
| 3209 | goto missing_data; |
| 3210 | |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3211 | /* We have to parse the HTTP request body to find any required data. |
| 3212 | * "balance url_param check_post" should have been the only way to get |
| 3213 | * into this. We were brought here after HTTP header analysis, so all |
| 3214 | * related structures are ready. |
| 3215 | */ |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 3216 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
Christopher Faulet | 87451fd | 2019-03-01 11:16:34 +0100 | [diff] [blame] | 3217 | http_handle_expect_hdr(s, req, msg); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3218 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 3219 | /* 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] | 3220 | * ci_head(req) still points to the beginning of the message. We |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 3221 | * must save the body in msg->next because it survives buffer |
| 3222 | * re-alignments. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3223 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 3224 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 3225 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3226 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3227 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 3228 | else |
| 3229 | msg->msg_state = HTTP_MSG_DATA; |
| 3230 | } |
| 3231 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 3232 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) { |
| 3233 | /* 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] | 3234 | if (http_body_bytes(msg) < msg->body_len) |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 3235 | goto missing_data; |
| 3236 | |
| 3237 | /* OK we have everything we need now */ |
| 3238 | goto http_end; |
| 3239 | } |
| 3240 | |
| 3241 | /* OK here we're parsing a chunked-encoded message */ |
| 3242 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3243 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 3244 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 3245 | * 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] | 3246 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3247 | */ |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 3248 | unsigned int chunk; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3249 | 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] | 3250 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3251 | if (!ret) |
| 3252 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3253 | else if (ret < 0) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3254 | msg->err_pos = ci_data(req) + ret; |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 3255 | if (msg->err_pos < 0) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3256 | msg->err_pos += req->buf.size; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3257 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3258 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3259 | } |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 3260 | |
| 3261 | msg->chunk_len = chunk; |
| 3262 | msg->body_len += chunk; |
| 3263 | |
| 3264 | msg->sol = ret; |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 3265 | msg->next += ret; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 3266 | msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3267 | } |
| 3268 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3269 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | e115b49 | 2015-05-01 23:05:14 +0200 | [diff] [blame] | 3270 | * We have the first data byte is in msg->sov + msg->sol. We're waiting |
| 3271 | * for at least a whole chunk or the whole content length bytes after |
| 3272 | * msg->sov + msg->sol. |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3273 | */ |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 3274 | if (msg->msg_state == HTTP_MSG_TRAILERS) |
| 3275 | goto http_end; |
| 3276 | |
Willy Tarreau | e115b49 | 2015-05-01 23:05:14 +0200 | [diff] [blame] | 3277 | 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] | 3278 | goto http_end; |
| 3279 | |
| 3280 | missing_data: |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 3281 | /* we get here if we need to wait for more data. If the buffer is full, |
| 3282 | * we have the maximum we can expect. |
| 3283 | */ |
Willy Tarreau | 2375233 | 2018-06-15 14:54:53 +0200 | [diff] [blame] | 3284 | if (channel_full(req, global.tune.maxrewrite)) |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 3285 | goto http_end; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3286 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 3287 | 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] | 3288 | txn->status = 408; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3289 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 3290 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3291 | if (!(s->flags & SF_ERR_MASK)) |
| 3292 | s->flags |= SF_ERR_CLITO; |
| 3293 | if (!(s->flags & SF_FINST_MASK)) |
| 3294 | s->flags |= SF_FINST_D; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3295 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3296 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3297 | |
| 3298 | /* we get here if we need to wait for more data */ |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 3299 | if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) { |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3300 | /* Not enough data. We'll re-use the http-request |
| 3301 | * timeout here. Ideally, we should set the timeout |
| 3302 | * relative to the accept() date. We just set the |
| 3303 | * request timeout once at the beginning of the |
| 3304 | * request. |
| 3305 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3306 | channel_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3307 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 3308 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3309 | return 0; |
| 3310 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3311 | |
| 3312 | http_end: |
| 3313 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 3314 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 3315 | req->analysers &= ~an_bit; |
| 3316 | req->analyse_exp = TICK_ETERNITY; |
| 3317 | return 1; |
| 3318 | |
| 3319 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 3320 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3321 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3322 | txn->status = 400; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3323 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3324 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3325 | if (!(s->flags & SF_ERR_MASK)) |
| 3326 | s->flags |= SF_ERR_PRXCOND; |
| 3327 | if (!(s->flags & SF_FINST_MASK)) |
| 3328 | s->flags |= SF_FINST_R; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 3329 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3330 | return_err_msg: |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3331 | req->analysers &= AN_REQ_FLT_END; |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 3332 | _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3333 | if (sess->listener->counters) |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 3334 | _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3335 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3336 | } |
| 3337 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 3338 | /* send a server's name with an outgoing request over an established connection. |
| 3339 | * Note: this function is designed to be called once the request has been scheduled |
| 3340 | * for being forwarded. This is the reason why it rewinds the buffer before |
| 3341 | * proceeding. |
| 3342 | */ |
Christopher Faulet | 27a3dc8 | 2018-10-23 15:34:07 +0200 | [diff] [blame] | 3343 | 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] | 3344 | |
| 3345 | struct hdr_ctx ctx; |
Christopher Faulet | 27a3dc8 | 2018-10-23 15:34:07 +0200 | [diff] [blame] | 3346 | struct http_txn *txn = s->txn; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3347 | char *hdr_name = be->server_id_hdr_name; |
| 3348 | int hdr_name_len = be->server_id_hdr_len; |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 3349 | struct channel *chn = txn->req.chn; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3350 | char *hdr_val; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 3351 | unsigned int old_o, old_i; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3352 | |
Christopher Faulet | 64159df | 2018-10-24 21:15:35 +0200 | [diff] [blame] | 3353 | if (IS_HTX_STRM(s)) |
| 3354 | return htx_send_name_header(s, be, srv_name); |
William Lallemand | d9e9066 | 2012-01-30 17:27:17 +0100 | [diff] [blame] | 3355 | ctx.idx = 0; |
| 3356 | |
Willy Tarreau | 211cdec | 2014-04-17 20:18:08 +0200 | [diff] [blame] | 3357 | old_o = http_hdr_rewind(&txn->req); |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 3358 | if (old_o) { |
| 3359 | /* The request was already skipped, let's restore it */ |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 3360 | c_rew(chn, old_o); |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 3361 | txn->req.next += old_o; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 3362 | txn->req.sov += old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 3363 | } |
| 3364 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3365 | old_i = ci_data(chn); |
| 3366 | 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] | 3367 | /* remove any existing values from the header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3368 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3369 | } |
| 3370 | |
| 3371 | /* Add the new header requested with the server value */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3372 | hdr_val = trash.area; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3373 | memcpy(hdr_val, hdr_name, hdr_name_len); |
| 3374 | hdr_val += hdr_name_len; |
| 3375 | *hdr_val++ = ':'; |
| 3376 | *hdr_val++ = ' '; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3377 | hdr_val += strlcpy2(hdr_val, srv_name, |
| 3378 | trash.area + trash.size - hdr_val); |
| 3379 | http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, |
| 3380 | hdr_val - trash.area); |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3381 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 3382 | if (old_o) { |
| 3383 | /* If this was a forwarded request, we must readjust the amount of |
| 3384 | * data to be forwarded in order to take into account the size |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 3385 | * variations. Note that the current state is >= HTTP_MSG_BODY, |
| 3386 | * so we don't have to adjust ->sol. |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 3387 | */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3388 | old_o += ci_data(chn) - old_i; |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 3389 | c_adv(chn, old_o); |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 3390 | txn->req.next -= old_o; |
| 3391 | txn->req.sov -= old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 3392 | } |
| 3393 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3394 | return 0; |
| 3395 | } |
| 3396 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3397 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 3398 | * right now but it works. |
| 3399 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3400 | void http_end_txn_clean_session(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3401 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3402 | int prev_status = s->txn->status; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 3403 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 858b103 | 2015-12-07 17:04:59 +0100 | [diff] [blame] | 3404 | struct proxy *be = s->be; |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 3405 | struct conn_stream *cs; |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 3406 | struct connection *srv_conn; |
| 3407 | struct server *srv; |
Willy Tarreau | 449d74a | 2015-08-05 17:16:33 +0200 | [diff] [blame] | 3408 | unsigned int prev_flags = s->txn->flags; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 3409 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3410 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 3411 | * server's connections. We need a safer way to reinitialize buffer |
| 3412 | * flags. We also need a more accurate method for computing per-request |
| 3413 | * data. |
| 3414 | */ |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 3415 | cs = objt_cs(s->si[1].end); |
| 3416 | srv_conn = cs_conn(cs); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3417 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 3418 | /* unless we're doing keep-alive, we want to quickly close the connection |
| 3419 | * to the server. |
| 3420 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3421 | if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
Olivier Houchard | 7c6f8b1 | 2018-11-13 16:48:36 +0100 | [diff] [blame] | 3422 | !si_conn_ready(&s->si[1]) || !srv_conn->owner) { |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3423 | s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF; |
| 3424 | si_shutr(&s->si[1]); |
| 3425 | si_shutw(&s->si[1]); |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 3426 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3427 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3428 | if (s->flags & SF_BE_ASSIGNED) { |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 3429 | _HA_ATOMIC_SUB(&be->beconn, 1); |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 3430 | if (unlikely(s->srv_conn)) |
| 3431 | sess_change_server(s, NULL); |
| 3432 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3433 | |
| 3434 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3435 | stream_process_counters(s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3436 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3437 | if (s->txn->status) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3438 | int n; |
| 3439 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3440 | n = s->txn->status / 100; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3441 | if (n < 1 || n > 5) |
| 3442 | n = 0; |
| 3443 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3444 | if (fe->mode == PR_MODE_HTTP) { |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 3445 | _HA_ATOMIC_ADD(&fe->fe_counters.p.http.rsp[n], 1); |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 3446 | } |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3447 | if ((s->flags & SF_BE_ASSIGNED) && |
Willy Tarreau | 858b103 | 2015-12-07 17:04:59 +0100 | [diff] [blame] | 3448 | (be->mode == PR_MODE_HTTP)) { |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 3449 | _HA_ATOMIC_ADD(&be->be_counters.p.http.rsp[n], 1); |
| 3450 | _HA_ATOMIC_ADD(&be->be_counters.p.http.cum_req, 1); |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 3451 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3452 | } |
| 3453 | |
| 3454 | /* don't count other requests' data */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3455 | s->logs.bytes_in -= ci_data(&s->req); |
| 3456 | s->logs.bytes_out -= ci_data(&s->res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3457 | |
Willy Tarreau | 66425e3 | 2018-07-25 06:55:12 +0200 | [diff] [blame] | 3458 | /* we may need to know the position in the queue */ |
| 3459 | pendconn_free(s); |
| 3460 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3461 | /* let's do a final log if we need it */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3462 | if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3463 | !(s->flags & SF_MONITOR) && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3464 | (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3465 | s->do_log(s); |
| 3466 | } |
| 3467 | |
Willy Tarreau | d713bcc | 2014-06-25 15:36:04 +0200 | [diff] [blame] | 3468 | /* stop tracking content-based counters */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3469 | stream_stop_content_counters(s); |
| 3470 | stream_update_time_stats(s); |
Willy Tarreau | 4bfc580 | 2014-06-17 12:19:18 +0200 | [diff] [blame] | 3471 | |
Willy Tarreau | 9efd745 | 2018-05-31 14:48:54 +0200 | [diff] [blame] | 3472 | /* reset the profiling counter */ |
| 3473 | s->task->calls = 0; |
| 3474 | s->task->cpu_time = 0; |
| 3475 | s->task->lat_time = 0; |
Willy Tarreau | d2d3348 | 2019-04-25 17:09:07 +0200 | [diff] [blame] | 3476 | s->task->call_date = ((profiling & HA_PROF_TASKS_MASK) >= HA_PROF_TASKS_AUTO) ? now_mono_time() : 0; |
Willy Tarreau | 5e370da | 2019-04-25 18:33:37 +0200 | [diff] [blame] | 3477 | s->call_rate.curr_sec = s->call_rate.curr_ctr = s->call_rate.prev_ctr = 0; |
Willy Tarreau | 9efd745 | 2018-05-31 14:48:54 +0200 | [diff] [blame] | 3478 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3479 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 3480 | s->logs.tv_accept = now; /* corrected date for internal use */ |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 3481 | s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */ |
| 3482 | s->logs.t_idle = -1; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3483 | tv_zero(&s->logs.tv_request); |
| 3484 | s->logs.t_queue = -1; |
| 3485 | s->logs.t_connect = -1; |
| 3486 | s->logs.t_data = -1; |
| 3487 | s->logs.t_close = 0; |
Patrick Hemmer | ffe5e8c | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 3488 | s->logs.prx_queue_pos = 0; /* we get the number of pending conns before us */ |
| 3489 | s->logs.srv_queue_pos = 0; /* we will get this number soon */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3490 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3491 | s->logs.bytes_in = s->req.total = ci_data(&s->req); |
| 3492 | s->logs.bytes_out = s->res.total = ci_data(&s->res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3493 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 3494 | if (objt_server(s->target)) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3495 | if (s->flags & SF_CURR_SESS) { |
| 3496 | s->flags &= ~SF_CURR_SESS; |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 3497 | _HA_ATOMIC_SUB(&__objt_server(s->target)->cur_sess, 1); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3498 | } |
Willy Tarreau | 858b103 | 2015-12-07 17:04:59 +0100 | [diff] [blame] | 3499 | if (may_dequeue_tasks(objt_server(s->target), be)) |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 3500 | process_srv_queue(objt_server(s->target)); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3501 | } |
| 3502 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 3503 | s->target = NULL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3504 | |
Olivier Houchard | 7c6f8b1 | 2018-11-13 16:48:36 +0100 | [diff] [blame] | 3505 | |
| 3506 | /* If we're doing keepalive, first call the mux detach() method |
| 3507 | * to let it know we want to detach without freing the connection. |
| 3508 | * We then can call si_release_endpoint() to destroy the conn_stream |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 3509 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3510 | if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
Olivier Houchard | a70e176 | 2018-12-13 18:01:00 +0100 | [diff] [blame] | 3511 | !si_conn_ready(&s->si[1]) || |
| 3512 | (srv_conn && srv_conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 3513 | srv_conn = NULL; |
Olivier Houchard | 985f139 | 2018-11-30 17:31:52 +0100 | [diff] [blame] | 3514 | else if (!srv_conn->owner) { |
| 3515 | srv_conn->owner = s->sess; |
Olivier Houchard | a4d4fdf | 2018-12-14 19:27:06 +0100 | [diff] [blame] | 3516 | /* Add it unconditionally to the session list, it'll be removed |
| 3517 | * later if needed by session_check_idle_conn(), once we'll |
| 3518 | * have released the endpoint and know if it no longer has |
| 3519 | * attached streams, and so an idling connection |
| 3520 | */ |
Olivier Houchard | 351411f | 2018-12-27 17:20:54 +0100 | [diff] [blame] | 3521 | if (!session_add_conn(s->sess, srv_conn, s->target)) { |
| 3522 | srv_conn->owner = NULL; |
| 3523 | /* Try to add the connection to the server idle list. |
| 3524 | * If it fails, as the connection no longer has an |
| 3525 | * owner, it will be destroy later by |
| 3526 | * si_release_endpoint(), anyway |
| 3527 | */ |
| 3528 | srv_add_to_idle_list(objt_server(srv_conn->target), srv_conn); |
| 3529 | srv_conn = NULL; |
| 3530 | |
| 3531 | } |
Olivier Houchard | 985f139 | 2018-11-30 17:31:52 +0100 | [diff] [blame] | 3532 | } |
Olivier Houchard | 7c6f8b1 | 2018-11-13 16:48:36 +0100 | [diff] [blame] | 3533 | si_release_endpoint(&s->si[1]); |
Olivier Houchard | a4d4fdf | 2018-12-14 19:27:06 +0100 | [diff] [blame] | 3534 | if (srv_conn && srv_conn->owner == s->sess) { |
| 3535 | if (session_check_idle_conn(s->sess, srv_conn) != 0) |
| 3536 | srv_conn = NULL; |
| 3537 | } |
| 3538 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 3539 | |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3540 | s->si[1].state = s->si[1].prev_state = SI_ST_INI; |
| 3541 | s->si[1].err_type = SI_ET_NONE; |
| 3542 | s->si[1].conn_retries = 0; /* used for logging too */ |
| 3543 | s->si[1].exp = TICK_ETERNITY; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3544 | 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] | 3545 | 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] | 3546 | 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] | 3547 | s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST); |
| 3548 | s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED); |
| 3549 | s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP); |
Willy Tarreau | 543db62 | 2012-11-15 16:41:22 +0100 | [diff] [blame] | 3550 | |
Patrick Hemmer | e3faf02 | 2018-08-22 10:02:00 -0400 | [diff] [blame] | 3551 | hlua_ctx_destroy(s->hlua); |
| 3552 | s->hlua = NULL; |
| 3553 | |
Christopher Faulet | b6af6b3 | 2019-11-07 14:27:52 +0100 | [diff] [blame] | 3554 | /* cleanup and reinit capture arrays, if any */ |
| 3555 | if (s->req_cap) { |
| 3556 | struct cap_hdr *h; |
| 3557 | for (h = fe->req_cap; h; h = h->next) |
| 3558 | pool_free(h->pool, s->req_cap[h->index]); |
| 3559 | memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *)); |
| 3560 | } |
| 3561 | |
| 3562 | if (s->res_cap) { |
| 3563 | struct cap_hdr *h; |
| 3564 | for (h = fe->rsp_cap; h; h = h->next) |
| 3565 | pool_free(h->pool, s->res_cap[h->index]); |
| 3566 | memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
| 3567 | } |
| 3568 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3569 | s->txn->meth = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3570 | http_reset_txn(s); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3571 | s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 3572 | |
| 3573 | if (prev_status == 401 || prev_status == 407) { |
| 3574 | /* In HTTP keep-alive mode, if we receive a 401, we still have |
| 3575 | * a chance of being able to send the visitor again to the same |
| 3576 | * server over the same connection. This is required by some |
| 3577 | * broken protocols such as NTLM, and anyway whenever there is |
| 3578 | * an opportunity for sending the challenge to the proper place, |
Lukas Tribus | 80512b1 | 2018-10-27 20:07:40 +0200 | [diff] [blame] | 3579 | * it's better to do it (at least it helps with debugging), at |
| 3580 | * least for non-deterministic load balancing algorithms. |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 3581 | */ |
Olivier Houchard | 250031e | 2019-05-29 15:01:50 +0200 | [diff] [blame] | 3582 | s->sess->flags |= SESS_FL_PREFER_LAST; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 3583 | } |
| 3584 | |
Willy Tarreau | 53f9685 | 2016-02-02 18:50:47 +0100 | [diff] [blame] | 3585 | /* Never ever allow to reuse a connection from a non-reuse backend */ |
| 3586 | if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR) |
| 3587 | srv_conn->flags |= CO_FL_PRIVATE; |
| 3588 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3589 | if (fe->options2 & PR_O2_INDEPSTR) |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3590 | s->si[1].flags |= SI_FL_INDEP_STR; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3591 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3592 | if (fe->options2 & PR_O2_NODELAY) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 3593 | s->req.flags |= CF_NEVER_WAIT; |
| 3594 | s->res.flags |= CF_NEVER_WAIT; |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 3595 | } |
| 3596 | |
Willy Tarreau | 714ea78 | 2015-11-25 20:11:11 +0100 | [diff] [blame] | 3597 | /* we're removing the analysers, we MUST re-enable events detection. |
| 3598 | * We don't enable close on the response channel since it's either |
| 3599 | * already closed, or in keep-alive with an idle connection handler. |
| 3600 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 3601 | channel_auto_read(&s->req); |
| 3602 | channel_auto_close(&s->req); |
| 3603 | channel_auto_read(&s->res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3604 | |
Willy Tarreau | 1c59bd5 | 2015-11-02 20:20:11 +0100 | [diff] [blame] | 3605 | /* we're in keep-alive with an idle connection, monitor it if not already done */ |
| 3606 | if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) { |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 3607 | srv = objt_server(srv_conn->target); |
Olivier Houchard | 7c6f8b1 | 2018-11-13 16:48:36 +0100 | [diff] [blame] | 3608 | if (srv) { |
| 3609 | if (srv_conn->flags & CO_FL_PRIVATE) |
| 3610 | LIST_ADD(&srv->priv_conns[tid], &srv_conn->list); |
| 3611 | else if (prev_flags & TX_NOT_FIRST) |
| 3612 | /* note: we check the request, not the connection, but |
| 3613 | * this is valid for strategies SAFE and AGGR, and in |
| 3614 | * case of ALWS, we don't care anyway. |
| 3615 | */ |
| 3616 | LIST_ADD(&srv->safe_conns[tid], &srv_conn->list); |
| 3617 | else |
| 3618 | LIST_ADD(&srv->idle_conns[tid], &srv_conn->list); |
| 3619 | } |
Willy Tarreau | 4320eaa | 2015-08-05 11:08:30 +0200 | [diff] [blame] | 3620 | } |
Christopher Faulet | e600624 | 2017-03-10 11:52:44 +0100 | [diff] [blame] | 3621 | s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0; |
| 3622 | s->res.analysers = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3623 | } |
| 3624 | |
| 3625 | |
| 3626 | /* This function updates the request state machine according to the response |
| 3627 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 3628 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 3629 | * it is only used to find when a request/response couple is complete. Both |
| 3630 | * this function and its equivalent should loop until both return zero. It |
| 3631 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 3632 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3633 | int http_sync_req_state(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3634 | { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 3635 | struct channel *chn = &s->req; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3636 | struct http_txn *txn = s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 3637 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3638 | unsigned int old_state = txn->req.msg_state; |
| 3639 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 3640 | if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3641 | return 0; |
| 3642 | |
| 3643 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3644 | /* No need to read anymore, the request was completely parsed. |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 3645 | * We can shut the read side unless we want to abort_on_close, |
| 3646 | * or we have a POST request. The issue with POST requests is |
| 3647 | * that some browsers still send a CRLF after the request, and |
| 3648 | * this CRLF must be read so that it does not remain in the kernel |
| 3649 | * buffers, otherwise a close could cause an RST on some systems |
| 3650 | * (eg: Linux). |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 3651 | * Note that if we're using keep-alive on the client side, we'd |
| 3652 | * rather poll now and keep the polling enabled for the whole |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3653 | * stream's life than enabling/disabling it between each |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 3654 | * response and next request. |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3655 | */ |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 3656 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 3657 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
Christopher Faulet | 769d0e9 | 2019-03-22 14:23:18 +0100 | [diff] [blame] | 3658 | !(s->be->options & PR_O_ABRT_CLOSE) && |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 3659 | txn->meth != HTTP_METH_POST) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 3660 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3661 | |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 3662 | /* if the server closes the connection, we want to immediately react |
| 3663 | * and close the socket to save packets and syscalls. |
| 3664 | */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3665 | s->si[1].flags |= SI_FL_NOHALF; |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 3666 | |
Willy Tarreau | 7f876a1 | 2015-11-18 11:59:55 +0100 | [diff] [blame] | 3667 | /* In any case we've finished parsing the request so we must |
| 3668 | * disable Nagle when sending data because 1) we're not going |
| 3669 | * to shut this side, and 2) the server is waiting for us to |
| 3670 | * send pending data. |
| 3671 | */ |
| 3672 | chn->flags |= CF_NEVER_WAIT; |
| 3673 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3674 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 3675 | goto wait_other_side; |
| 3676 | |
| 3677 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 3678 | /* The server has not finished to respond, so we |
| 3679 | * don't want to move in order not to upset it. |
| 3680 | */ |
| 3681 | goto wait_other_side; |
| 3682 | } |
| 3683 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3684 | /* When we get here, it means that both the request and the |
| 3685 | * response have finished receiving. Depending on the connection |
| 3686 | * mode, we'll have to wait for the last bytes to leave in either |
| 3687 | * direction, and sometimes for a close to be effective. |
| 3688 | */ |
| 3689 | |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3690 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 3691 | /* Server-close mode : queue a connection close to the server */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 3692 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) |
| 3693 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3694 | } |
| 3695 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 3696 | /* Option forceclose is set, or either side wants to close, |
| 3697 | * let's enforce it now that we're not expecting any new |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3698 | * data to come. The caller knows the stream is complete |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3699 | * once both states are CLOSED. |
Christopher Faulet | 1486b0a | 2017-07-18 11:42:08 +0200 | [diff] [blame] | 3700 | * |
| 3701 | * However, there is an exception if the response |
| 3702 | * length is undefined. In this case, we need to wait |
| 3703 | * the close from the server. The response will be |
| 3704 | * switched in TUNNEL mode until the end. |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3705 | */ |
Christopher Faulet | 1486b0a | 2017-07-18 11:42:08 +0200 | [diff] [blame] | 3706 | if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) && |
| 3707 | txn->rsp.msg_state != HTTP_MSG_CLOSED) |
| 3708 | goto check_channel_flags; |
| 3709 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 3710 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 3711 | channel_shutr_now(chn); |
| 3712 | channel_shutw_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3713 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3714 | } |
| 3715 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 3716 | /* The last possible modes are keep-alive and tunnel. Tunnel mode |
| 3717 | * will not have any analyser so it needs to poll for reads. |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3718 | */ |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 3719 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) { |
| 3720 | channel_auto_read(chn); |
| 3721 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 3722 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3723 | } |
| 3724 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 3725 | goto check_channel_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3726 | } |
| 3727 | |
| 3728 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 3729 | http_msg_closing: |
| 3730 | /* nothing else to forward, just waiting for the output buffer |
| 3731 | * to be empty and for the shutw_now to take effect. |
| 3732 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 3733 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3734 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3735 | goto http_msg_closed; |
| 3736 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 3737 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 3738 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3739 | txn->req.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3740 | } |
Christopher Faulet | 56d2609 | 2017-07-20 11:05:10 +0200 | [diff] [blame] | 3741 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3742 | } |
| 3743 | |
| 3744 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 3745 | http_msg_closed: |
Willy Tarreau | 8059351 | 2017-12-14 10:43:31 +0100 | [diff] [blame] | 3746 | /* if we don't know whether the server will close, we need to hard close */ |
| 3747 | if (txn->rsp.flags & HTTP_MSGF_XFER_LEN) |
| 3748 | s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
| 3749 | |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 3750 | /* see above in MSG_DONE why we only do this in these states */ |
| 3751 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 3752 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
Christopher Faulet | 769d0e9 | 2019-03-22 14:23:18 +0100 | [diff] [blame] | 3753 | !(s->be->options & PR_O_ABRT_CLOSE)) |
Willy Tarreau | 2e7a165 | 2013-12-15 15:32:10 +0100 | [diff] [blame] | 3754 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3755 | goto wait_other_side; |
| 3756 | } |
| 3757 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 3758 | check_channel_flags: |
| 3759 | /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */ |
| 3760 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
| 3761 | /* if we've just closed an output, let's switch */ |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 3762 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 3763 | goto http_msg_closing; |
| 3764 | } |
| 3765 | |
| 3766 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3767 | wait_other_side: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 3768 | return txn->req.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3769 | } |
| 3770 | |
| 3771 | |
| 3772 | /* This function updates the response state machine according to the request |
| 3773 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 3774 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 3775 | * it is only used to find when a request/response couple is complete. Both |
| 3776 | * this function and its equivalent should loop until both return zero. It |
| 3777 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 3778 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3779 | int http_sync_res_state(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3780 | { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 3781 | struct channel *chn = &s->res; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3782 | struct http_txn *txn = s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 3783 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3784 | unsigned int old_state = txn->rsp.msg_state; |
| 3785 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 3786 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3787 | return 0; |
| 3788 | |
| 3789 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 3790 | /* In theory, we don't need to read anymore, but we must |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3791 | * still monitor the server connection for a possible close |
| 3792 | * while the request is being uploaded, so we don't disable |
| 3793 | * reading. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3794 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 3795 | /* channel_dont_read(chn); */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3796 | |
| 3797 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 3798 | goto wait_other_side; |
| 3799 | |
| 3800 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 3801 | /* The client seems to still be sending data, probably |
| 3802 | * because we got an error response during an upload. |
| 3803 | * We have the choice of either breaking the connection |
| 3804 | * or letting it pass through. Let's do the later. |
| 3805 | */ |
| 3806 | goto wait_other_side; |
| 3807 | } |
| 3808 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3809 | /* When we get here, it means that both the request and the |
| 3810 | * response have finished receiving. Depending on the connection |
| 3811 | * mode, we'll have to wait for the last bytes to leave in either |
| 3812 | * direction, and sometimes for a close to be effective. |
| 3813 | */ |
| 3814 | |
| 3815 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 3816 | /* Server-close mode : shut read and wait for the request |
| 3817 | * side to close its output buffer. The caller will detect |
| 3818 | * when we're in DONE and the other is in CLOSED and will |
| 3819 | * catch that for the final cleanup. |
| 3820 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 3821 | if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW))) |
| 3822 | channel_shutr_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3823 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3824 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 3825 | /* Option forceclose is set, or either side wants to close, |
| 3826 | * let's enforce it now that we're not expecting any new |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3827 | * data to come. The caller knows the stream is complete |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3828 | * once both states are CLOSED. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3829 | */ |
Christopher Faulet | fd04fcf | 2018-02-02 15:54:15 +0100 | [diff] [blame] | 3830 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 3831 | channel_shutr_now(chn); |
| 3832 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3833 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3834 | } |
| 3835 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 3836 | /* The last possible modes are keep-alive and tunnel. Tunnel will |
| 3837 | * need to forward remaining data. Keep-alive will need to monitor |
| 3838 | * for connection closing. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3839 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 3840 | channel_auto_read(chn); |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 3841 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 3842 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
| 3843 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3844 | } |
| 3845 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 3846 | goto check_channel_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3847 | } |
| 3848 | |
| 3849 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 3850 | http_msg_closing: |
| 3851 | /* nothing else to forward, just waiting for the output buffer |
| 3852 | * to be empty and for the shutw_now to take effect. |
| 3853 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 3854 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3855 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 3856 | goto http_msg_closed; |
| 3857 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 3858 | else if (chn->flags & CF_SHUTW) { |
Christopher Faulet | a3992e0 | 2017-07-18 10:35:55 +0200 | [diff] [blame] | 3859 | txn->rsp.err_state = txn->rsp.msg_state; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3860 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 3861 | _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 3862 | if (objt_server(s->target)) |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 3863 | _HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3864 | } |
Christopher Faulet | 56d2609 | 2017-07-20 11:05:10 +0200 | [diff] [blame] | 3865 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3866 | } |
| 3867 | |
| 3868 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 3869 | http_msg_closed: |
| 3870 | /* drop any pending data */ |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 3871 | channel_truncate(chn); |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 3872 | channel_auto_close(chn); |
| 3873 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3874 | goto wait_other_side; |
| 3875 | } |
| 3876 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 3877 | check_channel_flags: |
| 3878 | /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */ |
| 3879 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
| 3880 | /* if we've just closed an output, let's switch */ |
| 3881 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 3882 | goto http_msg_closing; |
| 3883 | } |
| 3884 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3885 | wait_other_side: |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 3886 | /* We force the response to leave immediately if we're waiting for the |
| 3887 | * other side, since there is no pending shutdown to push it out. |
| 3888 | */ |
| 3889 | if (!channel_is_empty(chn)) |
| 3890 | chn->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 3891 | return txn->rsp.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3892 | } |
| 3893 | |
| 3894 | |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 3895 | /* Resync the request and response state machines. */ |
| 3896 | void http_resync_states(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3897 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3898 | struct http_txn *txn = s->txn; |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 3899 | #ifdef DEBUG_FULL |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3900 | int old_req_state = txn->req.msg_state; |
| 3901 | int old_res_state = txn->rsp.msg_state; |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 3902 | #endif |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3903 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3904 | http_sync_req_state(s); |
| 3905 | while (1) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3906 | if (!http_sync_res_state(s)) |
| 3907 | break; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3908 | if (!http_sync_req_state(s)) |
| 3909 | break; |
| 3910 | } |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 3911 | |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 3912 | DPRINTF(stderr,"[%u] %s: stream=%p old=%s,%s cur=%s,%s " |
| 3913 | "req->analysers=0x%08x res->analysers=0x%08x\n", |
| 3914 | now_ms, __FUNCTION__, s, |
Willy Tarreau | 0da5b3b | 2017-09-21 09:30:46 +0200 | [diff] [blame] | 3915 | h1_msg_state_str(old_req_state), h1_msg_state_str(old_res_state), |
| 3916 | 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] | 3917 | s->req.analysers, s->res.analysers); |
Christopher Faulet | 814d270 | 2017-03-30 11:33:44 +0200 | [diff] [blame] | 3918 | |
| 3919 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3920 | /* OK, both state machines agree on a compatible state. |
| 3921 | * There are a few cases we're interested in : |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3922 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 3923 | * directions, so let's simply disable both analysers. |
Christopher Faulet | f77bb53 | 2017-07-18 11:18:46 +0200 | [diff] [blame] | 3924 | * - HTTP_MSG_CLOSED on the response only or HTTP_MSG_ERROR on either |
| 3925 | * means we must abort the request. |
| 3926 | * - HTTP_MSG_TUNNEL on either means we have to disable analyser on |
| 3927 | * corresponding channel. |
| 3928 | * - HTTP_MSG_DONE or HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE |
| 3929 | * on the response with server-close mode means we've completed one |
| 3930 | * request and we must re-initialize the server connection. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3931 | */ |
Christopher Faulet | f77bb53 | 2017-07-18 11:18:46 +0200 | [diff] [blame] | 3932 | if (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 3933 | txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3934 | s->req.analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 3935 | channel_auto_close(&s->req); |
| 3936 | channel_auto_read(&s->req); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3937 | s->res.analysers &= AN_RES_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 3938 | channel_auto_close(&s->res); |
| 3939 | channel_auto_read(&s->res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3940 | } |
Christopher Faulet | f77bb53 | 2017-07-18 11:18:46 +0200 | [diff] [blame] | 3941 | else if (txn->rsp.msg_state == HTTP_MSG_CLOSED || |
| 3942 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 3943 | txn->req.msg_state == HTTP_MSG_ERROR) { |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3944 | s->res.analysers &= AN_RES_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 3945 | channel_auto_close(&s->res); |
| 3946 | channel_auto_read(&s->res); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3947 | s->req.analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 3948 | channel_abort(&s->req); |
| 3949 | channel_auto_close(&s->req); |
| 3950 | channel_auto_read(&s->req); |
| 3951 | channel_truncate(&s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3952 | } |
Christopher Faulet | f77bb53 | 2017-07-18 11:18:46 +0200 | [diff] [blame] | 3953 | else if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 3954 | txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 3955 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 3956 | s->req.analysers &= AN_REQ_FLT_END; |
| 3957 | if (HAS_REQ_DATA_FILTERS(s)) |
| 3958 | s->req.analysers |= AN_REQ_FLT_XFER_DATA; |
| 3959 | } |
| 3960 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 3961 | s->res.analysers &= AN_RES_FLT_END; |
| 3962 | if (HAS_RSP_DATA_FILTERS(s)) |
| 3963 | s->res.analysers |= AN_RES_FLT_XFER_DATA; |
| 3964 | } |
| 3965 | channel_auto_close(&s->req); |
| 3966 | channel_auto_read(&s->req); |
| 3967 | channel_auto_close(&s->res); |
| 3968 | channel_auto_read(&s->res); |
| 3969 | } |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 3970 | else if ((txn->req.msg_state == HTTP_MSG_DONE || |
| 3971 | txn->req.msg_state == HTTP_MSG_CLOSED) && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3972 | txn->rsp.msg_state == HTTP_MSG_DONE && |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 3973 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 3974 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 3975 | /* server-close/keep-alive: terminate this transaction, |
| 3976 | * possibly killing the server connection and reinitialize |
Willy Tarreau | 3de5bd6 | 2016-05-02 16:07:07 +0200 | [diff] [blame] | 3977 | * a fresh-new transaction, but only once we're sure there's |
| 3978 | * enough room in the request and response buffer to process |
Christopher Faulet | c0c672a | 2017-03-28 11:51:33 +0200 | [diff] [blame] | 3979 | * another request. They must not hold any pending output data |
| 3980 | * and the response buffer must realigned |
| 3981 | * (realign is done is http_end_txn_clean_session). |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3982 | */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3983 | if (co_data(&s->req)) |
Willy Tarreau | 3de5bd6 | 2016-05-02 16:07:07 +0200 | [diff] [blame] | 3984 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3985 | else if (co_data(&s->res)) |
Willy Tarreau | 3de5bd6 | 2016-05-02 16:07:07 +0200 | [diff] [blame] | 3986 | s->res.flags |= CF_WAKE_WRITE; |
Christopher Faulet | a81ff60 | 2017-07-18 22:01:05 +0200 | [diff] [blame] | 3987 | else { |
| 3988 | s->req.analysers = AN_REQ_FLT_END; |
| 3989 | s->res.analysers = AN_RES_FLT_END; |
| 3990 | txn->flags |= TX_WAIT_CLEANUP; |
| 3991 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3992 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3993 | } |
| 3994 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3995 | /* This function is an analyser which forwards request body (including chunk |
| 3996 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 3997 | * zero byte. The only situation where it must not be called is when we're in |
| 3998 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 3999 | * remaining data and to resync after end of body. It expects the msg_state to |
| 4000 | * 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] | 4001 | * 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] | 4002 | * 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] | 4003 | * bytes of pending data + the headers if not already done. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4004 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4005 | 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] | 4006 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4007 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4008 | struct http_txn *txn = s->txn; |
| 4009 | struct http_msg *msg = &s->txn->req; |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 4010 | short status = 0; |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 4011 | int ret; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4012 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 4013 | if (IS_HTX_STRM(s)) |
| 4014 | return htx_request_forward_body(s, req, an_bit); |
| 4015 | |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 4016 | 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] | 4017 | now_ms, __FUNCTION__, |
| 4018 | s, |
| 4019 | req, |
| 4020 | req->rex, req->wex, |
| 4021 | req->flags, |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 4022 | ci_data(req), |
Christopher Faulet | 814d270 | 2017-03-30 11:33:44 +0200 | [diff] [blame] | 4023 | req->analysers); |
| 4024 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4025 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4026 | return 0; |
| 4027 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4028 | 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] | 4029 | ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4030 | /* Output closed while we were sending data. We must abort and |
| 4031 | * wake the other side up. |
| 4032 | */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4033 | msg->err_state = msg->msg_state; |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4034 | msg->msg_state = HTTP_MSG_ERROR; |
| 4035 | http_resync_states(s); |
Christopher Faulet | 2f5c784 | 2019-03-19 14:55:11 +0100 | [diff] [blame] | 4036 | return 1; |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 4037 | } |
| 4038 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4039 | /* Note that we don't have to send 100-continue back because we don't |
| 4040 | * need the data to complete our job, and it's up to the server to |
| 4041 | * decide whether to return 100, 417 or anything else in return of |
| 4042 | * an "Expect: 100-continue" header. |
| 4043 | */ |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4044 | if (msg->msg_state == HTTP_MSG_BODY) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4045 | msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 4046 | ? HTTP_MSG_CHUNK_SIZE |
| 4047 | : HTTP_MSG_DATA); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4048 | |
| 4049 | /* TODO/filters: when http-buffer-request option is set or if a |
| 4050 | * rule on url_param exists, the first chunk size could be |
| 4051 | * already parsed. In that case, msg->next is after the chunk |
| 4052 | * size (including the CRLF after the size). So this case should |
| 4053 | * be handled to */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4054 | } |
| 4055 | |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 4056 | /* Some post-connect processing might want us to refrain from starting to |
| 4057 | * forward data. Currently, the only reason for this is "balance url_param" |
| 4058 | * whichs need to parse/process the request after we've enabled forwarding. |
| 4059 | */ |
| 4060 | if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4061 | if (!(s->res.flags & CF_READ_ATTACHED)) { |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 4062 | channel_auto_connect(req); |
Willy Tarreau | 644c101 | 2014-04-30 18:11:11 +0200 | [diff] [blame] | 4063 | req->flags |= CF_WAKE_CONNECT; |
Willy Tarreau | ba20dfc | 2018-05-16 11:35:05 +0200 | [diff] [blame] | 4064 | channel_dont_close(req); /* don't fail on early shutr */ |
| 4065 | goto waiting; |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 4066 | } |
| 4067 | msg->flags &= ~HTTP_MSGF_WAIT_CONN; |
| 4068 | } |
| 4069 | |
Willy Tarreau | 80a92c0 | 2014-03-12 10:41:13 +0100 | [diff] [blame] | 4070 | /* in most states, we should abort in case of early close */ |
| 4071 | channel_auto_close(req); |
| 4072 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 4073 | if (req->to_forward) { |
| 4074 | /* We can't process the buffer's contents yet */ |
| 4075 | req->flags |= CF_WAKE_WRITE; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4076 | goto missing_data_or_waiting; |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 4077 | } |
| 4078 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4079 | if (msg->msg_state < HTTP_MSG_DONE) { |
| 4080 | ret = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 4081 | ? http_msg_forward_chunked_body(s, msg) |
| 4082 | : http_msg_forward_body(s, msg)); |
| 4083 | if (!ret) |
| 4084 | goto missing_data_or_waiting; |
| 4085 | if (ret < 0) |
| 4086 | goto return_bad_req; |
| 4087 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4088 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4089 | /* other states, DONE...TUNNEL */ |
| 4090 | /* we don't want to forward closes on DONE except in tunnel mode. */ |
| 4091 | if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 4092 | channel_dont_close(req); |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 4093 | |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4094 | http_resync_states(s); |
| 4095 | if (!(req->analysers & an_bit)) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4096 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 4097 | if (req->flags & CF_SHUTW) { |
| 4098 | /* request errors are most likely due to the |
| 4099 | * server aborting the transfer. */ |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 4100 | goto return_srv_abort; |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4101 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4102 | if (msg->err_pos >= 0) |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 4103 | 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] | 4104 | goto return_bad_req; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4105 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4106 | return 1; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4107 | } |
| 4108 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4109 | /* If "option abortonclose" is set on the backend, we want to monitor |
| 4110 | * the client's connection and forward any shutdown notification to the |
| 4111 | * server, which will decide whether to close or to go on processing the |
| 4112 | * request. We only do that in tunnel mode, and not in other modes since |
| 4113 | * it can be abused to exhaust source ports. */ |
Christopher Faulet | 769d0e9 | 2019-03-22 14:23:18 +0100 | [diff] [blame] | 4114 | if (s->be->options & PR_O_ABRT_CLOSE) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4115 | channel_auto_read(req); |
| 4116 | if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && |
| 4117 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)) |
| 4118 | s->si[1].flags |= SI_FL_NOLINGER; |
| 4119 | channel_auto_close(req); |
| 4120 | } |
| 4121 | else if (s->txn->meth == HTTP_METH_POST) { |
| 4122 | /* POST requests may require to read extra CRLF sent by broken |
| 4123 | * browsers and which could cause an RST to be sent upon close |
| 4124 | * on some systems (eg: Linux). */ |
| 4125 | channel_auto_read(req); |
| 4126 | } |
| 4127 | return 0; |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 4128 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4129 | missing_data_or_waiting: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4130 | /* stop waiting for data if the input is closed before the end */ |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 4131 | if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) |
| 4132 | goto return_cli_abort; |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4133 | |
Willy Tarreau | ba20dfc | 2018-05-16 11:35:05 +0200 | [diff] [blame] | 4134 | waiting: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4135 | /* waiting for the last bits to leave the buffer */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4136 | if (req->flags & CF_SHUTW) |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 4137 | goto return_srv_abort; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4138 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4139 | /* 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] | 4140 | * 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] | 4141 | * And when content-length is used, we never want to let the possible |
| 4142 | * shutdown be forwarded to the other side, as the state machine will |
| 4143 | * take care of it once the client responds. It's also important to |
| 4144 | * prevent TIME_WAITs from accumulating on the backend side, and for |
| 4145 | * HTTP/2 where the last frame comes with a shutdown. |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4146 | */ |
Willy Tarreau | ff47b3f | 2017-12-29 16:30:31 +0100 | [diff] [blame] | 4147 | if (msg->flags & (HTTP_MSGF_TE_CHNK|HTTP_MSGF_CNT_LEN)) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4148 | channel_dont_close(req); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4149 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4150 | /* 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] | 4151 | * 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] | 4152 | * 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] | 4153 | * modes are already handled by the stream sock layer. We must not do |
| 4154 | * this in content-length mode because it could present the MSG_MORE |
| 4155 | * flag with the last block of forwarded data, which would cause an |
| 4156 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4157 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4158 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4159 | req->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4160 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4161 | return 0; |
| 4162 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 4163 | return_cli_abort: |
| 4164 | _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1); |
| 4165 | _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1); |
| 4166 | if (objt_server(s->target)) |
| 4167 | _HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1); |
| 4168 | if (!(s->flags & SF_ERR_MASK)) |
| 4169 | s->flags |= SF_ERR_CLICL; |
| 4170 | status = 400; |
| 4171 | goto return_error; |
| 4172 | |
| 4173 | return_srv_abort: |
| 4174 | _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1); |
| 4175 | _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1); |
| 4176 | if (objt_server(s->target)) |
| 4177 | _HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1); |
| 4178 | if (!(s->flags & SF_ERR_MASK)) |
| 4179 | s->flags |= SF_ERR_SRVCL; |
| 4180 | status = 502; |
| 4181 | goto return_error; |
| 4182 | |
| 4183 | return_bad_req: /* let's centralize all bad requests */ |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 4184 | _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4185 | if (sess->listener->counters) |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 4186 | _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4187 | if (!(s->flags & SF_ERR_MASK)) |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 4188 | s->flags |= SF_ERR_CLICL; |
| 4189 | status = 400; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4190 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 4191 | return_error: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4192 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4193 | txn->req.msg_state = HTTP_MSG_ERROR; |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 4194 | if (txn->status > 0) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4195 | /* 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] | 4196 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4197 | } else { |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 4198 | txn->status = status; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4199 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4200 | } |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4201 | req->analysers &= AN_REQ_FLT_END; |
| 4202 | s->res.analysers &= AN_RES_FLT_END; /* we're in data phase, we want to abort both directions */ |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 4203 | if (!(s->flags & SF_FINST_MASK)) |
| 4204 | s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4205 | return 0; |
| 4206 | } |
| 4207 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4208 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 4209 | * processing can continue on next analysers, or zero if it either needs more |
| 4210 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4211 | * 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] | 4212 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 4213 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4214 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4215 | 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] | 4216 | { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4217 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4218 | struct http_txn *txn = s->txn; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4219 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4220 | struct hdr_ctx ctx; |
Lukas Tribus | fd9b68c | 2018-10-27 20:06:59 +0200 | [diff] [blame] | 4221 | struct connection *srv_conn; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4222 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4223 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4224 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4225 | |
Lukas Tribus | fd9b68c | 2018-10-27 20:06:59 +0200 | [diff] [blame] | 4226 | srv_conn = cs_conn(objt_cs(s->si[1].end)); |
| 4227 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 4228 | if (IS_HTX_STRM(s)) |
| 4229 | return htx_wait_for_response(s, rep, an_bit); |
| 4230 | |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 4231 | 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] | 4232 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4233 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 4234 | rep, |
| 4235 | rep->rex, rep->wex, |
| 4236 | rep->flags, |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4237 | ci_data(rep), |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 4238 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 4239 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4240 | /* |
| 4241 | * Now parse the partial (or complete) lines. |
| 4242 | * We will check the response syntax, and also join multi-line |
| 4243 | * headers. An index of all the lines will be elaborated while |
| 4244 | * parsing. |
| 4245 | * |
| 4246 | * For the parsing, we use a 28 states FSM. |
| 4247 | * |
| 4248 | * Here is the information we currently have : |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4249 | * ci_head(rep) = beginning of response |
| 4250 | * ci_head(rep) + msg->eoh = end of processed headers / start of current one |
| 4251 | * ci_tail(rep) = end of input data |
| 4252 | * msg->eol = end of current header or line (LF or CRLF) |
| 4253 | * msg->next = first non-visited byte |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4254 | */ |
| 4255 | |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 4256 | next_one: |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4257 | /* There's a protected area at the end of the buffer for rewriting |
| 4258 | * purposes. We don't want to start to parse the request if the |
| 4259 | * protected area is affected, because we may have to move processed |
| 4260 | * data later, which is much more complicated. |
| 4261 | */ |
Willy Tarreau | d760eec | 2018-07-10 09:50:25 +0200 | [diff] [blame] | 4262 | if (c_data(rep) && msg->msg_state < HTTP_MSG_ERROR) { |
Christopher Faulet | 3d36d4e | 2019-09-04 09:39:42 +0200 | [diff] [blame] | 4263 | if (unlikely(!channel_is_rewritable(rep) && co_data(rep))) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 4264 | /* some data has still not left the buffer, wake us once that's done */ |
| 4265 | if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) |
| 4266 | goto abort_response; |
| 4267 | channel_dont_close(rep); |
| 4268 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 4269 | rep->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 4270 | return 0; |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4271 | } |
| 4272 | |
Willy Tarreau | 188e230 | 2018-06-15 11:11:53 +0200 | [diff] [blame] | 4273 | if (unlikely(ci_tail(rep) < c_ptr(rep, msg->next) || |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 4274 | ci_tail(rep) > b_wrap(&rep->buf) - global.tune.maxrewrite)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4275 | channel_slow_realign(rep, trash.area); |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 4276 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4277 | if (likely(msg->next < ci_data(rep))) |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 4278 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4279 | } |
| 4280 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4281 | /* 1: we might have to print this header in debug mode */ |
| 4282 | if (unlikely((global.mode & MODE_DEBUG) && |
| 4283 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 7d59e90 | 2014-10-21 19:36:09 +0200 | [diff] [blame] | 4284 | msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4285 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4286 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4287 | sol = ci_head(rep); |
| 4288 | 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] | 4289 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4290 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4291 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 4292 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4293 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4294 | while (cur_idx) { |
| 4295 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 4296 | debug_hdr("srvhdr", s, sol, eol); |
| 4297 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 4298 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 4299 | } |
| 4300 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4301 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4302 | /* |
| 4303 | * Now we quickly check if we have found a full valid response. |
| 4304 | * If not so, we check the FD and buffer states before leaving. |
| 4305 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4306 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4307 | * responses are checked first. |
| 4308 | * |
| 4309 | * Depending on whether the client is still there or not, we |
| 4310 | * may send an error response back or not. Note that normally |
| 4311 | * we should only check for HTTP status there, and check I/O |
| 4312 | * errors somewhere else. |
| 4313 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4314 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4315 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4316 | /* Invalid response */ |
| 4317 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 4318 | /* we detected a parsing error. We want to archive this response |
| 4319 | * in the dedicated proxy area for later troubleshooting. |
| 4320 | */ |
| 4321 | hdr_response_bad: |
| 4322 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 4323 | 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] | 4324 | |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 4325 | _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4326 | if (objt_server(s->target)) { |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 4327 | _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1); |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 4328 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4329 | } |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 4330 | abort_response: |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4331 | channel_auto_close(rep); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4332 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4333 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4334 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 4335 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4336 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4337 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4338 | if (!(s->flags & SF_ERR_MASK)) |
| 4339 | s->flags |= SF_ERR_PRXCOND; |
| 4340 | if (!(s->flags & SF_FINST_MASK)) |
| 4341 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4342 | |
| 4343 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4344 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4345 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4346 | /* too large response does not fit in buffer. */ |
Willy Tarreau | 2375233 | 2018-06-15 14:54:53 +0200 | [diff] [blame] | 4347 | else if (channel_full(rep, global.tune.maxrewrite)) { |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 4348 | if (msg->err_pos < 0) |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4349 | msg->err_pos = ci_data(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4350 | goto hdr_response_bad; |
| 4351 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4352 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4353 | /* read error */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4354 | else if (rep->flags & CF_READ_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4355 | if (msg->err_pos >= 0) |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 4356 | 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] | 4357 | else if (txn->flags & TX_NOT_FIRST) |
| 4358 | goto abort_keep_alive; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4359 | |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 4360 | _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4361 | if (objt_server(s->target)) { |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 4362 | _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1); |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 4363 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4364 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 4365 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4366 | channel_auto_close(rep); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4367 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4368 | txn->status = 502; |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 4369 | |
| 4370 | /* Check to see if the server refused the early data. |
| 4371 | * If so, just send a 425 |
| 4372 | */ |
| 4373 | if (objt_cs(s->si[1].end)) { |
| 4374 | struct connection *conn = objt_cs(s->si[1].end)->conn; |
| 4375 | |
| 4376 | if (conn->err_code == CO_ER_SSL_EARLY_FAILED) |
| 4377 | txn->status = 425; |
| 4378 | } |
| 4379 | |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4380 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 4381 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4382 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 4383 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4384 | if (!(s->flags & SF_ERR_MASK)) |
| 4385 | s->flags |= SF_ERR_SRVCL; |
| 4386 | if (!(s->flags & SF_FINST_MASK)) |
| 4387 | s->flags |= SF_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 4388 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4389 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4390 | |
Willy Tarreau | 6f0a7ba | 2014-06-23 15:22:31 +0200 | [diff] [blame] | 4391 | /* read timeout : return a 504 to the client. */ |
| 4392 | else if (rep->flags & CF_READ_TIMEOUT) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4393 | if (msg->err_pos >= 0) |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 4394 | 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] | 4395 | |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 4396 | _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4397 | if (objt_server(s->target)) { |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 4398 | _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1); |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 4399 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4400 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4401 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4402 | channel_auto_close(rep); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4403 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4404 | txn->status = 504; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4405 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 4406 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4407 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4408 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4409 | if (!(s->flags & SF_ERR_MASK)) |
| 4410 | s->flags |= SF_ERR_SRVTO; |
| 4411 | if (!(s->flags & SF_FINST_MASK)) |
| 4412 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4413 | return 0; |
| 4414 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 4415 | |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 4416 | /* client abort with an abortonclose */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4417 | else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) { |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 4418 | _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1); |
| 4419 | _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1); |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 4420 | if (objt_server(s->target)) |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 4421 | _HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1); |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 4422 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4423 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 4424 | channel_auto_close(rep); |
| 4425 | |
| 4426 | txn->status = 400; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 4427 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4428 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 4429 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4430 | if (!(s->flags & SF_ERR_MASK)) |
| 4431 | s->flags |= SF_ERR_CLICL; |
| 4432 | if (!(s->flags & SF_FINST_MASK)) |
| 4433 | s->flags |= SF_FINST_H; |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 4434 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4435 | /* process_stream() will take care of the error */ |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 4436 | return 0; |
| 4437 | } |
| 4438 | |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 4439 | /* 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] | 4440 | else if (rep->flags & CF_SHUTR) { |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 4441 | if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0) |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 4442 | 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] | 4443 | else if (txn->flags & TX_NOT_FIRST) |
| 4444 | goto abort_keep_alive; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4445 | |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 4446 | _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4447 | if (objt_server(s->target)) { |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 4448 | _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1); |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 4449 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4450 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4451 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4452 | channel_auto_close(rep); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4453 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4454 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4455 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 4456 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4457 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4458 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4459 | if (!(s->flags & SF_ERR_MASK)) |
| 4460 | s->flags |= SF_ERR_SRVCL; |
| 4461 | if (!(s->flags & SF_FINST_MASK)) |
| 4462 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4463 | return 0; |
| 4464 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4465 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4466 | /* write error to client (we don't send any message then) */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4467 | else if (rep->flags & CF_WRITE_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4468 | if (msg->err_pos >= 0) |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 4469 | 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] | 4470 | else if (txn->flags & TX_NOT_FIRST) |
| 4471 | goto abort_keep_alive; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4472 | |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 4473 | _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4474 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4475 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4476 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4477 | if (!(s->flags & SF_ERR_MASK)) |
| 4478 | s->flags |= SF_ERR_CLICL; |
| 4479 | if (!(s->flags & SF_FINST_MASK)) |
| 4480 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4481 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4482 | /* process_stream() will take care of the error */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4483 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4484 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4485 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4486 | channel_dont_close(rep); |
Willy Tarreau | 3f3997e | 2013-12-15 15:21:32 +0100 | [diff] [blame] | 4487 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4488 | return 0; |
| 4489 | } |
| 4490 | |
| 4491 | /* More interesting part now : we know that we have a complete |
| 4492 | * response which at least looks like HTTP. We have an indicator |
| 4493 | * of each header's length, so we can parse them quickly. |
| 4494 | */ |
| 4495 | |
| 4496 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 4497 | 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] | 4498 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4499 | /* |
| 4500 | * 1: get the status code |
| 4501 | */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4502 | n = ci_head(rep)[msg->sl.st.c] - '0'; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4503 | if (n < 1 || n > 5) |
| 4504 | n = 0; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4505 | /* when the client triggers a 4xx from the server, it's most often due |
| 4506 | * to a missing object or permission. These events should be tracked |
| 4507 | * because if they happen often, it may indicate a brute force or a |
| 4508 | * vulnerability scan. |
| 4509 | */ |
| 4510 | if (n == 4) |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4511 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4512 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4513 | if (objt_server(s->target)) |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 4514 | _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] | 4515 | |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 4516 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 4517 | * exactly one digit "." one digit. This check may be disabled using |
| 4518 | * option accept-invalid-http-response. |
| 4519 | */ |
| 4520 | if (!(s->be->options2 & PR_O2_RSPBUG_OK)) { |
| 4521 | if (msg->sl.st.v_l != 8) { |
| 4522 | msg->err_pos = 0; |
| 4523 | goto hdr_response_bad; |
| 4524 | } |
| 4525 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4526 | if (ci_head(rep)[4] != '/' || |
| 4527 | !isdigit((unsigned char)ci_head(rep)[5]) || |
| 4528 | ci_head(rep)[6] != '.' || |
| 4529 | !isdigit((unsigned char)ci_head(rep)[7])) { |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 4530 | msg->err_pos = 4; |
| 4531 | goto hdr_response_bad; |
| 4532 | } |
| 4533 | } |
| 4534 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4535 | /* check if the response is HTTP/1.1 or above */ |
| 4536 | if ((msg->sl.st.v_l == 8) && |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4537 | ((ci_head(rep)[5] > '1') || |
| 4538 | ((ci_head(rep)[5] == '1') && (ci_head(rep)[7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4539 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4540 | |
| 4541 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 4542 | 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] | 4543 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4544 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4545 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4546 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4547 | 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] | 4548 | |
Willy Tarreau | 3965040 | 2010-03-15 19:44:39 +0100 | [diff] [blame] | 4549 | /* Adjust server's health based on status code. Note: status codes 501 |
| 4550 | * and 505 are triggered on demand by client request, so we must not |
| 4551 | * count them as server failures. |
| 4552 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4553 | if (objt_server(s->target)) { |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 4554 | 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] | 4555 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 4556 | else |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 4557 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 4558 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4559 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4560 | /* |
Willy Tarreau | a14ad72 | 2017-07-07 11:36:32 +0200 | [diff] [blame] | 4561 | * We may be facing a 100-continue response, or any other informational |
| 4562 | * 1xx response which is non-final, in which case this is not the right |
| 4563 | * response, and we're waiting for the next one. Let's allow this response |
| 4564 | * to go to the client and wait for the next one. There's an exception for |
| 4565 | * 101 which is used later in the code to switch protocols. |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4566 | */ |
Willy Tarreau | a14ad72 | 2017-07-07 11:36:32 +0200 | [diff] [blame] | 4567 | if (txn->status < 200 && |
| 4568 | (txn->status == 100 || txn->status >= 102)) { |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 4569 | hdr_idx_init(&txn->hdr_idx); |
| 4570 | msg->next -= channel_forward(rep, msg->next); |
| 4571 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 4572 | txn->status = 0; |
| 4573 | s->logs.t_data = -1; /* was not a response yet */ |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 4574 | FLT_STRM_CB(s, flt_http_reset(s, msg)); |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 4575 | goto next_one; |
Willy Tarreau | a14ad72 | 2017-07-07 11:36:32 +0200 | [diff] [blame] | 4576 | } |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 4577 | |
Willy Tarreau | a14ad72 | 2017-07-07 11:36:32 +0200 | [diff] [blame] | 4578 | /* |
| 4579 | * 2: check for cacheability. |
| 4580 | */ |
| 4581 | |
| 4582 | switch (txn->status) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4583 | case 200: |
| 4584 | case 203: |
Willy Tarreau | c55ddce | 2017-12-21 11:41:38 +0100 | [diff] [blame] | 4585 | case 204: |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4586 | case 206: |
| 4587 | case 300: |
| 4588 | case 301: |
Willy Tarreau | c55ddce | 2017-12-21 11:41:38 +0100 | [diff] [blame] | 4589 | case 404: |
| 4590 | case 405: |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4591 | case 410: |
Willy Tarreau | c55ddce | 2017-12-21 11:41:38 +0100 | [diff] [blame] | 4592 | case 414: |
| 4593 | case 501: |
Willy Tarreau | 83ece46 | 2017-12-21 15:13:09 +0100 | [diff] [blame] | 4594 | break; |
| 4595 | default: |
Willy Tarreau | c55ddce | 2017-12-21 11:41:38 +0100 | [diff] [blame] | 4596 | /* RFC7231#6.1: |
| 4597 | * Responses with status codes that are defined as |
| 4598 | * cacheable by default (e.g., 200, 203, 204, 206, |
| 4599 | * 300, 301, 404, 405, 410, 414, and 501 in this |
| 4600 | * specification) can be reused by a cache with |
| 4601 | * heuristic expiration unless otherwise indicated |
| 4602 | * by the method definition or explicit cache |
| 4603 | * controls [RFC7234]; all other status codes are |
| 4604 | * not cacheable by default. |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4605 | */ |
Willy Tarreau | 83ece46 | 2017-12-21 15:13:09 +0100 | [diff] [blame] | 4606 | txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4607 | break; |
| 4608 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4609 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4610 | /* |
| 4611 | * 3: we may need to capture headers |
| 4612 | */ |
| 4613 | s->logs.logwait &= ~LW_RESP; |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 4614 | if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap)) |
Christopher Faulet | 10079f5 | 2018-10-03 15:17:28 +0200 | [diff] [blame] | 4615 | http_capture_headers(ci_head(rep), &txn->hdr_idx, |
| 4616 | s->res_cap, sess->fe->rsp_cap); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4617 | |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 4618 | /* 4: determine the transfer-length according to RFC2616 #4.4, updated |
| 4619 | * by RFC7230#3.3.3 : |
| 4620 | * |
| 4621 | * The length of a message body is determined by one of the following |
| 4622 | * (in order of precedence): |
| 4623 | * |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 4624 | * 1. Any 2xx (Successful) response to a CONNECT request implies that |
| 4625 | * the connection will become a tunnel immediately after the empty |
| 4626 | * line that concludes the header fields. A client MUST ignore |
| 4627 | * any Content-Length or Transfer-Encoding header fields received |
| 4628 | * in such a message. Any 101 response (Switching Protocols) is |
| 4629 | * managed in the same manner. |
| 4630 | * |
| 4631 | * 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] | 4632 | * (Informational), 204 (No Content), or 304 (Not Modified) status |
| 4633 | * code is always terminated by the first empty line after the |
| 4634 | * header fields, regardless of the header fields present in the |
| 4635 | * message, and thus cannot contain a message body. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4636 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 4637 | * 3. If a Transfer-Encoding header field is present and the chunked |
| 4638 | * transfer coding (Section 4.1) is the final encoding, the message |
| 4639 | * body length is determined by reading and decoding the chunked |
| 4640 | * data until the transfer coding indicates the data is complete. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4641 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 4642 | * If a Transfer-Encoding header field is present in a response and |
| 4643 | * the chunked transfer coding is not the final encoding, the |
| 4644 | * message body length is determined by reading the connection until |
| 4645 | * it is closed by the server. If a Transfer-Encoding header field |
| 4646 | * is present in a request and the chunked transfer coding is not |
| 4647 | * the final encoding, the message body length cannot be determined |
| 4648 | * reliably; the server MUST respond with the 400 (Bad Request) |
| 4649 | * status code and then close the connection. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4650 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 4651 | * If a message is received with both a Transfer-Encoding and a |
| 4652 | * Content-Length header field, the Transfer-Encoding overrides the |
| 4653 | * Content-Length. Such a message might indicate an attempt to |
| 4654 | * perform request smuggling (Section 9.5) or response splitting |
| 4655 | * (Section 9.4) and ought to be handled as an error. A sender MUST |
| 4656 | * remove the received Content-Length field prior to forwarding such |
| 4657 | * a message downstream. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4658 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 4659 | * 4. If a message is received without Transfer-Encoding and with |
| 4660 | * either multiple Content-Length header fields having differing |
| 4661 | * field-values or a single Content-Length header field having an |
| 4662 | * invalid value, then the message framing is invalid and the |
| 4663 | * recipient MUST treat it as an unrecoverable error. If this is a |
| 4664 | * request message, the server MUST respond with a 400 (Bad Request) |
| 4665 | * status code and then close the connection. If this is a response |
| 4666 | * message received by a proxy, the proxy MUST close the connection |
| 4667 | * to the server, discard the received response, and send a 502 (Bad |
| 4668 | * Gateway) response to the client. If this is a response message |
| 4669 | * received by a user agent, the user agent MUST close the |
| 4670 | * connection to the server and discard the received response. |
| 4671 | * |
| 4672 | * 5. If a valid Content-Length header field is present without |
| 4673 | * Transfer-Encoding, its decimal value defines the expected message |
| 4674 | * body length in octets. If the sender closes the connection or |
| 4675 | * the recipient times out before the indicated number of octets are |
| 4676 | * received, the recipient MUST consider the message to be |
| 4677 | * incomplete and close the connection. |
| 4678 | * |
| 4679 | * 6. If this is a request message and none of the above are true, then |
| 4680 | * the message body length is zero (no message body is present). |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4681 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 4682 | * 7. Otherwise, this is a response message without a declared message |
| 4683 | * body length, so the message body length is determined by the |
| 4684 | * number of octets received prior to the server closing the |
| 4685 | * connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4686 | */ |
| 4687 | |
| 4688 | /* Skip parsing if no content length is possible. The response flags |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4689 | * 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] | 4690 | * 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] | 4691 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 4692 | */ |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 4693 | if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || |
| 4694 | txn->status == 101)) { |
| 4695 | /* Either we've established an explicit tunnel, or we're |
| 4696 | * switching the protocol. In both cases, we're very unlikely |
| 4697 | * to understand the next protocols. We have to switch to tunnel |
| 4698 | * mode, so that we transfer the request and responses then let |
| 4699 | * this protocol pass unmodified. When we later implement specific |
| 4700 | * parsers for such protocols, we'll want to check the Upgrade |
| 4701 | * header which contains information about that protocol for |
| 4702 | * responses with status 101 (eg: see RFC2817 about TLS). |
| 4703 | */ |
| 4704 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN; |
| 4705 | msg->flags |= HTTP_MSGF_XFER_LEN; |
| 4706 | goto end; |
| 4707 | } |
| 4708 | |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4709 | if (txn->meth == HTTP_METH_HEAD || |
| 4710 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4711 | txn->status == 204 || txn->status == 304) { |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4712 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4713 | goto skip_content_length; |
| 4714 | } |
| 4715 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4716 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4717 | ctx.idx = 0; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4718 | 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] | 4719 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4720 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 4721 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4722 | /* bad transfer-encoding (chunked followed by something else) */ |
| 4723 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4724 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4725 | break; |
| 4726 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4727 | } |
| 4728 | |
Willy Tarreau | 196a7df | 2019-09-12 14:01:40 +0200 | [diff] [blame] | 4729 | /* "chunked" mandatory if transfer-encoding is used */ |
| 4730 | if (ctx.idx && !(msg->flags & HTTP_MSGF_TE_CHNK)) { |
| 4731 | use_close_only = 1; |
| 4732 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 4733 | } |
| 4734 | |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 4735 | /* Chunked responses must have their content-length removed */ |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4736 | ctx.idx = 0; |
Willy Tarreau | b4d0c03 | 2015-05-01 10:25:45 +0200 | [diff] [blame] | 4737 | if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4738 | 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] | 4739 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 4740 | } |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4741 | 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] | 4742 | signed long long cl; |
| 4743 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4744 | if (!ctx.vlen) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4745 | msg->err_pos = ctx.line + ctx.val - ci_head(rep); |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4746 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4747 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4748 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4749 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4750 | msg->err_pos = ctx.line + ctx.val - ci_head(rep); |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4751 | goto hdr_response_bad; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4752 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4753 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4754 | if (cl < 0) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4755 | msg->err_pos = ctx.line + ctx.val - ci_head(rep); |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4756 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4757 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4758 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4759 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4760 | msg->err_pos = ctx.line + ctx.val - ci_head(rep); |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4761 | goto hdr_response_bad; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4762 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4763 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4764 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4765 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4766 | } |
| 4767 | |
Lukas Tribus | fd9b68c | 2018-10-27 20:06:59 +0200 | [diff] [blame] | 4768 | /* check for NTML authentication headers in 401 (WWW-Authenticate) and |
| 4769 | * 407 (Proxy-Authenticate) responses and set the connection to private |
| 4770 | */ |
| 4771 | if (srv_conn && txn->status == 401) { |
| 4772 | /* check for Negotiate/NTLM WWW-Authenticate headers */ |
| 4773 | ctx.idx = 0; |
| 4774 | while (http_find_header2("WWW-Authenticate", 16, ci_head(rep), &txn->hdr_idx, &ctx)) { |
| 4775 | if ((ctx.vlen >= 9 && word_match(ctx.line + ctx.val, ctx.vlen, "Negotiate", 9)) || |
| 4776 | (ctx.vlen >= 4 && word_match(ctx.line + ctx.val, ctx.vlen, "NTLM", 4))) |
| 4777 | srv_conn->flags |= CO_FL_PRIVATE; |
| 4778 | } |
| 4779 | } else if (srv_conn && txn->status == 407) { |
| 4780 | /* check for Negotiate/NTLM Proxy-Authenticate headers */ |
| 4781 | ctx.idx = 0; |
| 4782 | while (http_find_header2("Proxy-Authenticate", 18, ci_head(rep), &txn->hdr_idx, &ctx)) { |
| 4783 | if ((ctx.vlen >= 9 && word_match(ctx.line + ctx.val, ctx.vlen, "Negotiate", 9)) || |
| 4784 | (ctx.vlen >= 4 && word_match(ctx.line + ctx.val, ctx.vlen, "NTLM", 4))) |
| 4785 | srv_conn->flags |= CO_FL_PRIVATE; |
| 4786 | } |
| 4787 | } |
| 4788 | |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 4789 | skip_content_length: |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4790 | /* Now we have to check if we need to modify the Connection header. |
| 4791 | * This is more difficult on the response than it is on the request, |
| 4792 | * because we can have two different HTTP versions and we don't know |
| 4793 | * how the client will interprete a response. For instance, let's say |
| 4794 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 4795 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 4796 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 4797 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 4798 | * the lack of header as a keep-alive. Thus we will use two flags |
| 4799 | * indicating how a request MAY be understood by the client. In case |
| 4800 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 4801 | * ambiguous cases such as both close and keepalive are seen, then we |
| 4802 | * will fall back to explicit close. Note that we won't take risks with |
| 4803 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4804 | * See doc/internals/connection-header.txt for the complete matrix. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4805 | */ |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 4806 | if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) && |
Christopher Faulet | 315b39c | 2018-09-21 16:26:19 +0200 | [diff] [blame] | 4807 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4808 | int to_del = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4809 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4810 | /* on unknown transfer length, we must close */ |
Christopher Faulet | 315b39c | 2018-09-21 16:26:19 +0200 | [diff] [blame] | 4811 | if (!(msg->flags & HTTP_MSGF_XFER_LEN)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4812 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4813 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4814 | /* now adjust header transformations depending on current state */ |
Christopher Faulet | 315b39c | 2018-09-21 16:26:19 +0200 | [diff] [blame] | 4815 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4816 | to_del |= 2; /* remove "keep-alive" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4817 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4818 | to_del |= 1; /* remove "close" for HTTP/1.0 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4819 | } |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4820 | else { /* SCL / KAL */ |
| 4821 | to_del |= 1; /* remove "close" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4822 | if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4823 | to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4824 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4825 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4826 | /* Parse and remove some headers from the connection header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 4827 | http_parse_connection_header(txn, msg, to_del); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4828 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4829 | /* Some keep-alive responses are converted to Server-close if |
| 4830 | * the server wants to close. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4831 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4832 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) { |
| 4833 | if ((txn->flags & TX_HDR_CONN_CLO) || |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4834 | (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11))) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4835 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4836 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4837 | } |
| 4838 | |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 4839 | end: |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 4840 | /* we want to have the response time before we start processing it */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 4841 | s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 4842 | |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 4843 | /* end of job, return OK */ |
| 4844 | rep->analysers &= ~an_bit; |
| 4845 | rep->analyse_exp = TICK_ETERNITY; |
| 4846 | channel_auto_close(rep); |
| 4847 | return 1; |
| 4848 | |
| 4849 | abort_keep_alive: |
| 4850 | /* A keep-alive request to the server failed on a network error. |
| 4851 | * The client is required to retry. We need to close without returning |
| 4852 | * any other information so that the client retries. |
| 4853 | */ |
| 4854 | txn->status = 0; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4855 | rep->analysers &= AN_RES_FLT_END; |
| 4856 | s->req.analysers &= AN_REQ_FLT_END; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 4857 | channel_auto_close(rep); |
| 4858 | s->logs.logwait = 0; |
| 4859 | s->logs.level = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4860 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 4861 | channel_truncate(rep); |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 4862 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 4863 | return 0; |
| 4864 | } |
| 4865 | |
| 4866 | /* This function performs all the processing enabled for the current response. |
| 4867 | * 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] | 4868 | * 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] | 4869 | * other functions. It works like process_request (see indications above). |
| 4870 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4871 | 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] | 4872 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4873 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4874 | struct http_txn *txn = s->txn; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 4875 | struct http_msg *msg = &txn->rsp; |
| 4876 | struct proxy *cur_proxy; |
| 4877 | struct cond_wordlist *wl; |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 4878 | enum rule_result ret = HTTP_RULE_RES_CONT; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 4879 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 4880 | if (IS_HTX_STRM(s)) |
| 4881 | return htx_process_res_common(s, rep, an_bit, px); |
| 4882 | |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 4883 | 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] | 4884 | now_ms, __FUNCTION__, |
| 4885 | s, |
| 4886 | rep, |
| 4887 | rep->rex, rep->wex, |
| 4888 | rep->flags, |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4889 | ci_data(rep), |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 4890 | rep->analysers); |
| 4891 | |
| 4892 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
| 4893 | return 0; |
| 4894 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 4895 | /* The stats applet needs to adjust the Connection header but we don't |
| 4896 | * apply any filter there. |
| 4897 | */ |
Willy Tarreau | 612adb8 | 2015-03-10 15:25:54 +0100 | [diff] [blame] | 4898 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
| 4899 | rep->analysers &= ~an_bit; |
| 4900 | rep->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 4901 | goto skip_filters; |
Willy Tarreau | 612adb8 | 2015-03-10 15:25:54 +0100 | [diff] [blame] | 4902 | } |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 4903 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 4904 | /* |
| 4905 | * We will have to evaluate the filters. |
| 4906 | * As opposed to version 1.2, now they will be evaluated in the |
| 4907 | * filters order and not in the header order. This means that |
| 4908 | * each filter has to be validated among all headers. |
| 4909 | * |
| 4910 | * Filters are tried with ->be first, then with ->fe if it is |
| 4911 | * different from ->be. |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 4912 | * |
| 4913 | * Maybe we are in resume condiion. In this case I choose the |
| 4914 | * "struct proxy" which contains the rule list matching the resume |
| 4915 | * pointer. If none of theses "struct proxy" match, I initialise |
| 4916 | * the process with the first one. |
| 4917 | * |
| 4918 | * In fact, I check only correspondance betwwen the current list |
| 4919 | * pointer and the ->fe rule list. If it doesn't match, I initialize |
| 4920 | * the loop with the ->be. |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 4921 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4922 | if (s->current_rule_list == &sess->fe->http_res_rules) |
| 4923 | cur_proxy = sess->fe; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 4924 | else |
| 4925 | cur_proxy = s->be; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 4926 | while (1) { |
| 4927 | struct proxy *rule_set = cur_proxy; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4928 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 4929 | /* evaluate http-response rules */ |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 4930 | if (ret == HTTP_RULE_RES_CONT) { |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4931 | 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] | 4932 | |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 4933 | if (ret == HTTP_RULE_RES_BADREQ) |
| 4934 | goto return_srv_prx_502; |
| 4935 | |
| 4936 | if (ret == HTTP_RULE_RES_DONE) { |
| 4937 | rep->analysers &= ~an_bit; |
| 4938 | rep->analyse_exp = TICK_ETERNITY; |
| 4939 | return 1; |
| 4940 | } |
| 4941 | } |
| 4942 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 4943 | /* we need to be called again. */ |
| 4944 | if (ret == HTTP_RULE_RES_YIELD) { |
| 4945 | channel_dont_close(rep); |
| 4946 | return 0; |
| 4947 | } |
| 4948 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 4949 | /* try headers filters */ |
| 4950 | if (rule_set->rsp_exp != NULL) { |
| 4951 | if (apply_filters_to_response(s, rep, rule_set) < 0) { |
| 4952 | return_bad_resp: |
| 4953 | if (objt_server(s->target)) { |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 4954 | _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1); |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 4955 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4956 | } |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 4957 | _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 4958 | return_srv_prx_502: |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4959 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 4960 | txn->status = 502; |
| 4961 | s->logs.t_data = -1; /* was not a valid response */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4962 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 4963 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4964 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4965 | if (!(s->flags & SF_ERR_MASK)) |
| 4966 | s->flags |= SF_ERR_PRXCOND; |
| 4967 | if (!(s->flags & SF_FINST_MASK)) |
| 4968 | s->flags |= SF_FINST_H; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 4969 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4970 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 4971 | } |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4972 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 4973 | /* has the response been denied ? */ |
| 4974 | if (txn->flags & TX_SVDENY) { |
| 4975 | if (objt_server(s->target)) |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 4976 | _HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4977 | |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 4978 | _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1); |
| 4979 | _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4980 | if (sess->listener->counters) |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 4981 | _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4982 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 4983 | goto return_srv_prx_502; |
| 4984 | } |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 4985 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 4986 | /* add response headers from the rule sets in the same order */ |
| 4987 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 4988 | if (txn->status < 200 && txn->status != 101) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4989 | break; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 4990 | if (wl->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 4991 | 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] | 4992 | ret = acl_pass(ret); |
| 4993 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 4994 | ret = !ret; |
| 4995 | if (!ret) |
| 4996 | continue; |
| 4997 | } |
Christopher Faulet | 10079f5 | 2018-10-03 15:17:28 +0200 | [diff] [blame] | 4998 | 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] | 4999 | goto return_bad_resp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5000 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5001 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5002 | /* check whether we're already working on the frontend */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5003 | if (cur_proxy == sess->fe) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5004 | break; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5005 | cur_proxy = sess->fe; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5006 | } |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5007 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 5008 | /* After this point, this anayzer can't return yield, so we can |
| 5009 | * remove the bit corresponding to this analyzer from the list. |
| 5010 | * |
| 5011 | * Note that the intermediate returns and goto found previously |
| 5012 | * reset the analyzers. |
| 5013 | */ |
| 5014 | rep->analysers &= ~an_bit; |
| 5015 | rep->analyse_exp = TICK_ETERNITY; |
| 5016 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5017 | /* OK that's all we can do for 1xx responses */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 5018 | if (unlikely(txn->status < 200 && txn->status != 101)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5019 | goto skip_header_mangling; |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5020 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5021 | /* |
| 5022 | * Now check for a server cookie. |
| 5023 | */ |
Willy Tarreau | 53a09d5 | 2015-08-10 18:59:40 +0200 | [diff] [blame] | 5024 | 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] | 5025 | manage_server_side_cookies(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5026 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5027 | /* |
| 5028 | * Check for cache-control or pragma headers if required. |
| 5029 | */ |
Willy Tarreau | 12b32f2 | 2017-12-21 16:08:09 +0100 | [diff] [blame] | 5030 | 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] | 5031 | check_response_for_cacheability(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5032 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5033 | /* |
| 5034 | * Add server cookie in the response if needed |
| 5035 | */ |
| 5036 | if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) && |
| 5037 | !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5038 | (!(s->flags & SF_DIRECT) || |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5039 | ((s->be->cookie_maxidle || txn->cookie_last_date) && |
| 5040 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 5041 | (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 5042 | (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
| 5043 | (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5044 | !(s->flags & SF_IGNORE_PRST)) { |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5045 | /* the server is known, it's not the one the client requested, or the |
| 5046 | * cookie's last seen date needs to be refreshed. We have to |
| 5047 | * insert a set-cookie here, except if we want to insert only on POST |
| 5048 | * requests and this one isn't. Note that servers which don't have cookies |
| 5049 | * (eg: some backup servers) will return a full cookie removal request. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5050 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5051 | if (!objt_server(s->target)->cookie) { |
| 5052 | chunk_printf(&trash, |
| 5053 | "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
| 5054 | s->be->cookie_name); |
| 5055 | } |
| 5056 | else { |
| 5057 | 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] | 5058 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5059 | if (s->be->cookie_maxidle || s->be->cookie_maxlife) { |
| 5060 | /* emit last_date, which is mandatory */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5061 | trash.area[trash.data++] = COOKIE_DELIM_DATE; |
| 5062 | s30tob64((date.tv_sec+3) >> 2, |
| 5063 | trash.area + trash.data); |
| 5064 | trash.data += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5065 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5066 | if (s->be->cookie_maxlife) { |
| 5067 | /* emit first_date, which is either the original one or |
| 5068 | * the current date. |
| 5069 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5070 | trash.area[trash.data++] = COOKIE_DELIM_DATE; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5071 | s30tob64(txn->cookie_first_date ? |
| 5072 | txn->cookie_first_date >> 2 : |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5073 | (date.tv_sec+3) >> 2, |
| 5074 | trash.area + trash.data); |
| 5075 | trash.data += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5076 | } |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5077 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5078 | chunk_appendf(&trash, "; path=/"); |
| 5079 | } |
Willy Tarreau | 4992dd2 | 2012-05-31 21:02:17 +0200 | [diff] [blame] | 5080 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5081 | if (s->be->cookie_domain) |
| 5082 | chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain); |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5083 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5084 | if (s->be->ck_opts & PR_CK_HTTPONLY) |
| 5085 | chunk_appendf(&trash, "; HttpOnly"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5086 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5087 | if (s->be->ck_opts & PR_CK_SECURE) |
| 5088 | chunk_appendf(&trash, "; Secure"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5089 | |
Christopher Faulet | db2cdbb | 2020-01-21 11:06:48 +0100 | [diff] [blame] | 5090 | if (s->be->cookie_attrs) |
| 5091 | chunk_appendf(&trash, "; %s", s->be->cookie_attrs); |
| 5092 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5093 | 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] | 5094 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5095 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5096 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | b05e48a | 2018-09-20 11:12:58 +0200 | [diff] [blame] | 5097 | if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5098 | /* the server did not change, only the date was updated */ |
| 5099 | txn->flags |= TX_SCK_UPDATED; |
| 5100 | else |
| 5101 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5102 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5103 | /* Here, we will tell an eventual cache on the client side that we don't |
| 5104 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 5105 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 5106 | * 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] | 5107 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5108 | if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5109 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5110 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5111 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5112 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, |
| 5113 | "Cache-control: private", 22) < 0)) |
| 5114 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5115 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5116 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5117 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5118 | /* |
| 5119 | * Check if result will be cacheable with a cookie. |
| 5120 | * We'll block the response if security checks have caught |
| 5121 | * nasty things such as a cacheable cookie. |
| 5122 | */ |
| 5123 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 5124 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
| 5125 | (s->be->options & PR_O_CHK_CACHE)) { |
| 5126 | /* we're in presence of a cacheable response containing |
| 5127 | * a set-cookie header. We'll block it as requested by |
| 5128 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5129 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5130 | if (objt_server(s->target)) |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 5131 | _HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1); |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5132 | |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 5133 | _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1); |
| 5134 | _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5135 | if (sess->listener->counters) |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 5136 | _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5137 | |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5138 | ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 5139 | 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] | 5140 | send_log(s->be, LOG_ALERT, |
| 5141 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 5142 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
| 5143 | goto return_srv_prx_502; |
| 5144 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 5145 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 5146 | skip_filters: |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5147 | /* |
| 5148 | * Adjust "Connection: close" or "Connection: keep-alive" if needed. |
| 5149 | * If an "Upgrade" token is found, the header is left untouched in order |
| 5150 | * 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] | 5151 | * if anything but "Upgrade" is present in the Connection header. We don't |
| 5152 | * want to touch any 101 response either since it's switching to another |
| 5153 | * protocol. |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5154 | */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 5155 | if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) && |
Christopher Faulet | 315b39c | 2018-09-21 16:26:19 +0200 | [diff] [blame] | 5156 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) { |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5157 | unsigned int want_flags = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5158 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5159 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5160 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5161 | /* we want a keep-alive response here. Keep-alive header |
| 5162 | * required if either side is not 1.1. |
| 5163 | */ |
| 5164 | if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11)) |
| 5165 | want_flags |= TX_CON_KAL_SET; |
| 5166 | } |
Christopher Faulet | 315b39c | 2018-09-21 16:26:19 +0200 | [diff] [blame] | 5167 | else { /* CLO */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5168 | /* we want a close response here. Close header required if |
| 5169 | * the server is 1.1, regardless of the client. |
| 5170 | */ |
| 5171 | if (msg->flags & HTTP_MSGF_VER_11) |
| 5172 | want_flags |= TX_CON_CLO_SET; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5173 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5174 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5175 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 5176 | http_change_connection_header(txn, msg, want_flags); |
| 5177 | } |
| 5178 | |
| 5179 | skip_header_mangling: |
Christopher Faulet | 69744d9 | 2017-03-30 10:54:35 +0200 | [diff] [blame] | 5180 | /* Always enter in the body analyzer */ |
| 5181 | rep->analysers &= ~AN_RES_FLT_XFER_DATA; |
| 5182 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5183 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5184 | /* if the user wants to log as soon as possible, without counting |
| 5185 | * bytes from the server, then this is the right moment. We have |
| 5186 | * to temporarily assign bytes_out to log what we currently have. |
| 5187 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5188 | if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) { |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5189 | s->logs.t_close = s->logs.t_data; /* to get a valid end date */ |
| 5190 | s->logs.bytes_out = txn->rsp.eoh; |
| 5191 | s->do_log(s); |
| 5192 | s->logs.bytes_out = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5193 | } |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 5194 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5195 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5196 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5197 | /* This function is an analyser which forwards response body (including chunk |
| 5198 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 5199 | * zero byte. The only situation where it must not be called is when we're in |
| 5200 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 5201 | * remaining data and to resync after end of body. It expects the msg_state to |
| 5202 | * 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] | 5203 | * 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] | 5204 | * |
| 5205 | * It is capable of compressing response data both in content-length mode and |
| 5206 | * in chunked mode. The state machines follows different flows depending on |
| 5207 | * whether content-length and chunked modes are used, since there are no |
| 5208 | * trailers in content-length : |
| 5209 | * |
| 5210 | * chk-mode cl-mode |
| 5211 | * ,----- BODY -----. |
| 5212 | * / \ |
| 5213 | * V size > 0 V chk-mode |
| 5214 | * .--> SIZE -------------> DATA -------------> CRLF |
| 5215 | * | | size == 0 | last byte | |
| 5216 | * | v final crlf v inspected | |
| 5217 | * | TRAILERS -----------> DONE | |
| 5218 | * | | |
| 5219 | * `----------------------------------------------' |
| 5220 | * |
| 5221 | * Compression only happens in the DATA state, and must be flushed in final |
| 5222 | * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding |
| 5223 | * is performed at once on final states for all bytes parsed, or when leaving |
| 5224 | * on missing data. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5225 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5226 | 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] | 5227 | { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5228 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5229 | struct http_txn *txn = s->txn; |
| 5230 | struct http_msg *msg = &s->txn->rsp; |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 5231 | int ret; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5232 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 5233 | if (IS_HTX_STRM(s)) |
| 5234 | return htx_response_forward_body(s, res, an_bit); |
| 5235 | |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 5236 | 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] | 5237 | now_ms, __FUNCTION__, |
| 5238 | s, |
| 5239 | res, |
| 5240 | res->rex, res->wex, |
| 5241 | res->flags, |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 5242 | ci_data(res), |
Christopher Faulet | 814d270 | 2017-03-30 11:33:44 +0200 | [diff] [blame] | 5243 | res->analysers); |
| 5244 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5245 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 5246 | return 0; |
| 5247 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5248 | 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] | 5249 | ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res))) || |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 5250 | !s->req.analysers) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5251 | /* Output closed while we were sending data. We must abort and |
| 5252 | * wake the other side up. |
| 5253 | */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 5254 | msg->err_state = msg->msg_state; |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5255 | msg->msg_state = HTTP_MSG_ERROR; |
| 5256 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 5257 | return 1; |
| 5258 | } |
| 5259 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5260 | /* in most states, we should abort in case of early close */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5261 | channel_auto_close(res); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5262 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5263 | if (msg->msg_state == HTTP_MSG_BODY) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5264 | msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 5265 | ? HTTP_MSG_CHUNK_SIZE |
| 5266 | : HTTP_MSG_DATA); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5267 | } |
| 5268 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 5269 | if (res->to_forward) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5270 | /* We can't process the buffer's contents yet */ |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 5271 | res->flags |= CF_WAKE_WRITE; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5272 | goto missing_data_or_waiting; |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 5273 | } |
| 5274 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5275 | if (msg->msg_state < HTTP_MSG_DONE) { |
| 5276 | ret = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 5277 | ? http_msg_forward_chunked_body(s, msg) |
| 5278 | : http_msg_forward_body(s, msg)); |
| 5279 | if (!ret) |
| 5280 | goto missing_data_or_waiting; |
| 5281 | if (ret < 0) |
| 5282 | goto return_bad_res; |
| 5283 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5284 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5285 | /* other states, DONE...TUNNEL */ |
| 5286 | /* for keep-alive we don't want to forward closes on DONE */ |
| 5287 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5288 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
| 5289 | channel_dont_close(res); |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 5290 | |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 5291 | http_resync_states(s); |
| 5292 | if (!(res->analysers & an_bit)) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5293 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 5294 | if (res->flags & CF_SHUTW) { |
| 5295 | /* response errors are most likely due to the |
| 5296 | * client aborting the transfer. */ |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 5297 | goto return_cli_abort; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5298 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5299 | if (msg->err_pos >= 0) |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 5300 | 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] | 5301 | goto return_bad_res; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5302 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5303 | return 1; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5304 | } |
Willy Tarreau | f51d03c | 2016-05-02 15:25:15 +0200 | [diff] [blame] | 5305 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5306 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5307 | missing_data_or_waiting: |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5308 | if (res->flags & CF_SHUTW) |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 5309 | goto return_cli_abort; |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5310 | |
| 5311 | /* stop waiting for data if the input is closed before the end. If the |
| 5312 | * client side was already closed, it means that the client has aborted, |
| 5313 | * so we don't want to count this as a server abort. Otherwise it's a |
| 5314 | * server abort. |
| 5315 | */ |
Christopher Faulet | a33510b | 2017-03-31 15:37:29 +0200 | [diff] [blame] | 5316 | if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5317 | if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW)) |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 5318 | goto return_cli_abort; |
Christopher Faulet | a46bbd8 | 2015-06-19 09:00:58 +0200 | [diff] [blame] | 5319 | /* If we have some pending data, we continue the processing */ |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 5320 | if (!ci_data(res)) |
| 5321 | goto return_srv_abort; |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5322 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5323 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5324 | /* 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] | 5325 | if (!s->req.analysers) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5326 | goto return_bad_res; |
| 5327 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5328 | /* When TE: chunked is used, we need to get there again to parse |
| 5329 | * 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] | 5330 | * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side |
| 5331 | * or if there are filters registered on the stream, we don't want to |
| 5332 | * forward a close |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5333 | */ |
Christopher Faulet | 69744d9 | 2017-03-30 10:54:35 +0200 | [diff] [blame] | 5334 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || |
Christopher Faulet | f1cc5d0 | 2017-02-08 09:45:13 +0100 | [diff] [blame] | 5335 | HAS_DATA_FILTERS(s, res) || |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5336 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5337 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5338 | channel_dont_close(res); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5339 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5340 | /* 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] | 5341 | * 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] | 5342 | * 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] | 5343 | * modes are already handled by the stream sock layer. We must not do |
| 5344 | * this in content-length mode because it could present the MSG_MORE |
| 5345 | * flag with the last block of forwarded data, which would cause an |
| 5346 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5347 | */ |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 5348 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING)) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5349 | res->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5350 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5351 | /* the stream handler will take care of timeouts and errors */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5352 | return 0; |
| 5353 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 5354 | return_srv_abort: |
| 5355 | _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1); |
| 5356 | _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5357 | if (objt_server(s->target)) |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 5358 | _HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1); |
| 5359 | if (!(s->flags & SF_ERR_MASK)) |
| 5360 | s->flags |= SF_ERR_SRVCL; |
| 5361 | goto return_error; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5362 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 5363 | return_cli_abort: |
| 5364 | _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1); |
| 5365 | _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5366 | if (objt_server(s->target)) |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 5367 | _HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1); |
| 5368 | if (!(s->flags & SF_ERR_MASK)) |
| 5369 | s->flags |= SF_ERR_CLICL; |
| 5370 | goto return_error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5371 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 5372 | return_bad_res: |
| 5373 | _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
| 5374 | if (objt_server(s->target)) { |
| 5375 | _HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1); |
| 5376 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP); |
| 5377 | } |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5378 | if (!(s->flags & SF_ERR_MASK)) |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 5379 | s->flags |= SF_ERR_SRVCL; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5380 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 5381 | return_error: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 5382 | txn->rsp.err_state = txn->rsp.msg_state; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5383 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 5384 | /* don't send any error message as we're in the body */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 5385 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5386 | res->analysers &= AN_RES_FLT_END; |
| 5387 | s->req.analysers &= AN_REQ_FLT_END; /* we're in data phase, we want to abort both directions */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5388 | if (!(s->flags & SF_FINST_MASK)) |
| 5389 | s->flags |= SF_FINST_D; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5390 | return 0; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5391 | } |
| 5392 | |
| 5393 | |
Christopher Faulet | 10079f5 | 2018-10-03 15:17:28 +0200 | [diff] [blame] | 5394 | int http_msg_forward_body(struct stream *s, struct http_msg *msg) |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5395 | { |
| 5396 | struct channel *chn = msg->chn; |
| 5397 | int ret; |
| 5398 | |
| 5399 | /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */ |
| 5400 | |
| 5401 | if (msg->msg_state == HTTP_MSG_ENDING) |
| 5402 | goto ending; |
| 5403 | |
| 5404 | /* Neither content-length, nor transfer-encoding was found, so we must |
| 5405 | * read the body until the server connection is closed. In that case, we |
| 5406 | * eat data as they come. Of course, this happens for response only. */ |
| 5407 | if (!(msg->flags & HTTP_MSGF_XFER_LEN)) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5408 | unsigned long long len = ci_data(chn) - msg->next; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5409 | msg->chunk_len += len; |
| 5410 | msg->body_len += len; |
| 5411 | } |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 5412 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg), |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5413 | /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next), |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 5414 | /* on_error */ goto error); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5415 | msg->next += ret; |
| 5416 | msg->chunk_len -= ret; |
| 5417 | if (msg->chunk_len) { |
| 5418 | /* input empty or output full */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5419 | if (ci_data(chn) > msg->next) |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5420 | chn->flags |= CF_WAKE_WRITE; |
| 5421 | goto missing_data_or_waiting; |
| 5422 | } |
| 5423 | |
Christopher Faulet | 1486b0a | 2017-07-18 11:42:08 +0200 | [diff] [blame] | 5424 | /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is |
| 5425 | * always set for a request. */ |
| 5426 | if (!(msg->flags & HTTP_MSGF_XFER_LEN)) { |
| 5427 | /* The server still sending data that should be filtered */ |
| 5428 | if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn)) |
| 5429 | goto missing_data_or_waiting; |
Christopher Faulet | fd04fcf | 2018-02-02 15:54:15 +0100 | [diff] [blame] | 5430 | msg->msg_state = HTTP_MSG_TUNNEL; |
| 5431 | goto ending; |
Christopher Faulet | 1486b0a | 2017-07-18 11:42:08 +0200 | [diff] [blame] | 5432 | } |
Christopher Faulet | f1cc5d0 | 2017-02-08 09:45:13 +0100 | [diff] [blame] | 5433 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5434 | msg->msg_state = HTTP_MSG_ENDING; |
Christopher Faulet | 297d3e2 | 2019-03-22 14:16:14 +0100 | [diff] [blame] | 5435 | chn->flags |= CF_EOI; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5436 | |
| 5437 | ending: |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 5438 | /* 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] | 5439 | * chunk of data or trailers. */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 5440 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next), |
| 5441 | /* default_ret */ msg->next, |
| 5442 | /* on_error */ goto error); |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 5443 | c_adv(chn, ret); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5444 | msg->next -= ret; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 5445 | if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)) |
| 5446 | msg->sov -= ret; |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 5447 | if (msg->next) |
| 5448 | goto waiting; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 5449 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 5450 | FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg), |
| 5451 | /* default_ret */ 1, |
| 5452 | /* on_error */ goto error, |
| 5453 | /* on_wait */ goto waiting); |
Christopher Faulet | fd04fcf | 2018-02-02 15:54:15 +0100 | [diff] [blame] | 5454 | if (msg->msg_state == HTTP_MSG_ENDING) |
| 5455 | msg->msg_state = HTTP_MSG_DONE; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5456 | return 1; |
| 5457 | |
| 5458 | missing_data_or_waiting: |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 5459 | /* we may have some pending data starting at chn->buf.p */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 5460 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next), |
| 5461 | /* default_ret */ msg->next, |
| 5462 | /* on_error */ goto error); |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 5463 | c_adv(chn, ret); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5464 | msg->next -= ret; |
| 5465 | if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0) |
| 5466 | msg->sov -= ret; |
Christopher Faulet | 75e2eb6 | 2015-12-15 10:41:47 +0100 | [diff] [blame] | 5467 | if (!HAS_DATA_FILTERS(s, chn)) |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5468 | msg->chunk_len -= channel_forward(chn, msg->chunk_len); |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 5469 | waiting: |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5470 | return 0; |
| 5471 | error: |
| 5472 | return -1; |
| 5473 | } |
| 5474 | |
Christopher Faulet | 10079f5 | 2018-10-03 15:17:28 +0200 | [diff] [blame] | 5475 | 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] | 5476 | { |
| 5477 | struct channel *chn = msg->chn; |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 5478 | unsigned int chunk; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5479 | int ret; |
| 5480 | |
| 5481 | /* Here we have the guarantee to be in one of the following state: |
| 5482 | * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF, |
| 5483 | * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */ |
| 5484 | |
Christopher Faulet | ca874b8 | 2018-09-20 11:31:01 +0200 | [diff] [blame] | 5485 | if (msg->msg_state == HTTP_MSG_ENDING) |
| 5486 | goto ending; |
| 5487 | |
| 5488 | /* Don't parse chunks if there is no input data */ |
| 5489 | if (!ci_data(chn)) |
| 5490 | goto waiting; |
| 5491 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5492 | switch_states: |
| 5493 | switch (msg->msg_state) { |
| 5494 | case HTTP_MSG_DATA: |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 5495 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg), |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5496 | /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next), |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 5497 | /* on_error */ goto error); |
| 5498 | msg->next += ret; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5499 | msg->chunk_len -= ret; |
| 5500 | if (msg->chunk_len) { |
| 5501 | /* input empty or output full */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5502 | if (ci_data(chn) > msg->next) |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5503 | chn->flags |= CF_WAKE_WRITE; |
| 5504 | goto missing_data_or_waiting; |
| 5505 | } |
| 5506 | |
| 5507 | /* nothing left to forward for this chunk*/ |
| 5508 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
| 5509 | /* fall through for HTTP_MSG_CHUNK_CRLF */ |
| 5510 | |
| 5511 | case HTTP_MSG_CHUNK_CRLF: |
| 5512 | /* we want the CRLF after the data */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 5513 | 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] | 5514 | if (ret == 0) |
| 5515 | goto missing_data_or_waiting; |
Willy Tarreau | b289256 | 2017-09-21 11:33:54 +0200 | [diff] [blame] | 5516 | if (ret < 0) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5517 | msg->err_pos = ci_data(chn) + ret; |
Willy Tarreau | b289256 | 2017-09-21 11:33:54 +0200 | [diff] [blame] | 5518 | if (msg->err_pos < 0) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 5519 | msg->err_pos += chn->buf.size; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5520 | goto chunk_parsing_error; |
Willy Tarreau | b289256 | 2017-09-21 11:33:54 +0200 | [diff] [blame] | 5521 | } |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 5522 | msg->next += ret; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5523 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 5524 | /* fall through for HTTP_MSG_CHUNK_SIZE */ |
| 5525 | |
| 5526 | case HTTP_MSG_CHUNK_SIZE: |
| 5527 | /* read the chunk size and assign it to ->chunk_len, |
| 5528 | * then set ->next to point to the body and switch to |
| 5529 | * DATA or TRAILERS state. |
| 5530 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 5531 | 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] | 5532 | if (ret == 0) |
| 5533 | goto missing_data_or_waiting; |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 5534 | if (ret < 0) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5535 | msg->err_pos = ci_data(chn) + ret; |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 5536 | if (msg->err_pos < 0) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 5537 | msg->err_pos += chn->buf.size; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5538 | goto chunk_parsing_error; |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 5539 | } |
| 5540 | |
| 5541 | msg->sol = ret; |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 5542 | msg->next += ret; |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 5543 | msg->chunk_len = chunk; |
| 5544 | msg->body_len += chunk; |
| 5545 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5546 | if (msg->chunk_len) { |
| 5547 | msg->msg_state = HTTP_MSG_DATA; |
| 5548 | goto switch_states; |
| 5549 | } |
| 5550 | msg->msg_state = HTTP_MSG_TRAILERS; |
| 5551 | /* fall through for HTTP_MSG_TRAILERS */ |
| 5552 | |
| 5553 | case HTTP_MSG_TRAILERS: |
| 5554 | ret = http_forward_trailers(msg); |
| 5555 | if (ret < 0) |
| 5556 | goto chunk_parsing_error; |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 5557 | FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg), |
| 5558 | /* default_ret */ 1, |
| 5559 | /* on_error */ goto error); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5560 | msg->next += msg->sol; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5561 | if (!ret) |
| 5562 | goto missing_data_or_waiting; |
| 5563 | break; |
| 5564 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5565 | default: |
| 5566 | /* This should no happen in this function */ |
| 5567 | goto error; |
| 5568 | } |
| 5569 | |
| 5570 | msg->msg_state = HTTP_MSG_ENDING; |
Christopher Faulet | 297d3e2 | 2019-03-22 14:16:14 +0100 | [diff] [blame] | 5571 | chn->flags |= CF_EOI; |
| 5572 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5573 | ending: |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 5574 | /* 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] | 5575 | * chunk of data or trailers. */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 5576 | 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] | 5577 | /* default_ret */ msg->next, |
| 5578 | /* on_error */ goto error); |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 5579 | c_adv(chn, ret); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5580 | msg->next -= ret; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 5581 | if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)) |
| 5582 | msg->sov -= ret; |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 5583 | if (msg->next) |
| 5584 | goto waiting; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 5585 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 5586 | FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg), |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5587 | /* default_ret */ 1, |
| 5588 | /* on_error */ goto error, |
| 5589 | /* on_wait */ goto waiting); |
| 5590 | msg->msg_state = HTTP_MSG_DONE; |
| 5591 | return 1; |
| 5592 | |
| 5593 | missing_data_or_waiting: |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 5594 | /* we may have some pending data starting at chn->buf.p */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 5595 | 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] | 5596 | /* default_ret */ msg->next, |
| 5597 | /* on_error */ goto error); |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 5598 | c_adv(chn, ret); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5599 | msg->next -= ret; |
| 5600 | if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0) |
| 5601 | msg->sov -= ret; |
Christopher Faulet | 75e2eb6 | 2015-12-15 10:41:47 +0100 | [diff] [blame] | 5602 | if (!HAS_DATA_FILTERS(s, chn)) |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5603 | msg->chunk_len -= channel_forward(chn, msg->chunk_len); |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 5604 | waiting: |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5605 | return 0; |
| 5606 | |
| 5607 | chunk_parsing_error: |
| 5608 | if (msg->err_pos >= 0) { |
| 5609 | if (chn->flags & CF_ISRESP) |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 5610 | http_capture_bad_message(s->be, s, msg, |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5611 | msg->msg_state, strm_fe(s)); |
| 5612 | else |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 5613 | http_capture_bad_message(strm_fe(s), s, |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5614 | msg, msg->msg_state, s->be); |
| 5615 | } |
| 5616 | error: |
| 5617 | return -1; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5618 | } |
| 5619 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5620 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5621 | /* Iterate the same filter through all request headers. |
| 5622 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5623 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 5624 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5625 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5626 | 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] | 5627 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5628 | char *cur_ptr, *cur_end, *cur_next; |
| 5629 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5630 | struct http_txn *txn = s->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5631 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 5632 | int delta, len; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 5633 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5634 | last_hdr = 0; |
| 5635 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5636 | cur_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5637 | old_idx = 0; |
| 5638 | |
| 5639 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5640 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5641 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5642 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5643 | (exp->action == ACT_ALLOW || |
| 5644 | exp->action == ACT_DENY || |
| 5645 | exp->action == ACT_TARPIT)) |
| 5646 | return 0; |
| 5647 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5648 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5649 | if (!cur_idx) |
| 5650 | break; |
| 5651 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5652 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5653 | cur_ptr = cur_next; |
| 5654 | cur_end = cur_ptr + cur_hdr->len; |
| 5655 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5656 | |
| 5657 | /* Now we have one header between cur_ptr and cur_end, |
| 5658 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5659 | */ |
| 5660 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 5661 | 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] | 5662 | switch (exp->action) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5663 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5664 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5665 | last_hdr = 1; |
| 5666 | break; |
| 5667 | |
| 5668 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5669 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5670 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5671 | break; |
| 5672 | |
| 5673 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5674 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5675 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5676 | break; |
| 5677 | |
| 5678 | case ACT_REPLACE: |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 5679 | len = exp_replace(trash.area, |
| 5680 | trash.size, cur_ptr, |
| 5681 | exp->replace, pmatch); |
| 5682 | if (len < 0) |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 5683 | return -1; |
| 5684 | |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 5685 | delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len); |
| 5686 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5687 | /* FIXME: if the user adds a newline in the replacement, the |
| 5688 | * index will not be recalculated for now, and the new line |
| 5689 | * will not be counted as a new header. |
| 5690 | */ |
| 5691 | |
| 5692 | cur_end += delta; |
| 5693 | cur_next += delta; |
| 5694 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5695 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5696 | break; |
| 5697 | |
| 5698 | case ACT_REMOVE: |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 5699 | delta = b_rep_blk(&req->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5700 | cur_next += delta; |
| 5701 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5702 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5703 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 5704 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5705 | cur_hdr->len = 0; |
| 5706 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 5707 | cur_idx = old_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5708 | break; |
| 5709 | |
| 5710 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5711 | } |
| 5712 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5713 | /* keep the link from this header to next one in case of later |
| 5714 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5715 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5716 | old_idx = cur_idx; |
| 5717 | } |
| 5718 | return 0; |
| 5719 | } |
| 5720 | |
| 5721 | |
| 5722 | /* Apply the filter to the request line. |
| 5723 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 5724 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5725 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 5726 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5727 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5728 | 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] | 5729 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5730 | char *cur_ptr, *cur_end; |
| 5731 | int done; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5732 | struct http_txn *txn = s->txn; |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 5733 | int delta, len; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5734 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5735 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5736 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5737 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5738 | (exp->action == ACT_ALLOW || |
| 5739 | exp->action == ACT_DENY || |
| 5740 | exp->action == ACT_TARPIT)) |
| 5741 | return 0; |
| 5742 | else if (exp->action == ACT_REMOVE) |
| 5743 | return 0; |
| 5744 | |
| 5745 | done = 0; |
| 5746 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5747 | cur_ptr = ci_head(req); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5748 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5749 | |
| 5750 | /* Now we have the request line between cur_ptr and cur_end */ |
| 5751 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 5752 | 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] | 5753 | switch (exp->action) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5754 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5755 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5756 | done = 1; |
| 5757 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5758 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5759 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5760 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5761 | done = 1; |
| 5762 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5763 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5764 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5765 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5766 | done = 1; |
| 5767 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5768 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5769 | case ACT_REPLACE: |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 5770 | len = exp_replace(trash.area, trash.size, |
| 5771 | cur_ptr, exp->replace, pmatch); |
| 5772 | if (len < 0) |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 5773 | return -1; |
| 5774 | |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 5775 | delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len); |
| 5776 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5777 | /* FIXME: if the user adds a newline in the replacement, the |
| 5778 | * index will not be recalculated for now, and the new line |
| 5779 | * will not be counted as a new header. |
| 5780 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5781 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5782 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5783 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 5784 | cur_end = (char *)http_parse_reqline(&txn->req, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5785 | HTTP_MSG_RQMETH, |
| 5786 | cur_ptr, cur_end + 1, |
| 5787 | NULL, NULL); |
| 5788 | if (unlikely(!cur_end)) |
| 5789 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5790 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5791 | /* we have a full request and we know that we have either a CR |
| 5792 | * or an LF at <ptr>. |
| 5793 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5794 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 5795 | 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] | 5796 | /* there is no point trying this regex on headers */ |
| 5797 | return 1; |
| 5798 | } |
| 5799 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5800 | return done; |
| 5801 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 5802 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5803 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5804 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5805 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5806 | * 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] | 5807 | * 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] | 5808 | * unparsable request. Since it can manage the switch to another backend, it |
| 5809 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5810 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5811 | 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] | 5812 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 5813 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5814 | struct http_txn *txn = s->txn; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5815 | struct hdr_exp *exp; |
| 5816 | |
| 5817 | for (exp = px->req_exp; exp; exp = exp->next) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5818 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5819 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5820 | /* |
| 5821 | * The interleaving of transformations and verdicts |
| 5822 | * makes it difficult to decide to continue or stop |
| 5823 | * the evaluation. |
| 5824 | */ |
| 5825 | |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5826 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) |
| 5827 | break; |
| 5828 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5829 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5830 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5831 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5832 | continue; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5833 | |
| 5834 | /* if this filter had a condition, evaluate it now and skip to |
| 5835 | * next filter if the condition does not match. |
| 5836 | */ |
| 5837 | if (exp->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 5838 | 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] | 5839 | ret = acl_pass(ret); |
| 5840 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 5841 | ret = !ret; |
| 5842 | |
| 5843 | if (!ret) |
| 5844 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5845 | } |
| 5846 | |
| 5847 | /* Apply the filter to the request line. */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5848 | ret = apply_filter_to_req_line(s, req, exp); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5849 | if (unlikely(ret < 0)) |
| 5850 | return -1; |
| 5851 | |
| 5852 | if (likely(ret == 0)) { |
| 5853 | /* The filter did not match the request, it can be |
| 5854 | * iterated through all headers. |
| 5855 | */ |
Willy Tarreau | 34d4c3c | 2015-01-30 20:58:58 +0100 | [diff] [blame] | 5856 | if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0)) |
| 5857 | return -1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5858 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5859 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5860 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5861 | } |
| 5862 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5863 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5864 | /* Delete a value in a header between delimiters <from> and <next> in buffer |
| 5865 | * <buf>. The number of characters displaced is returned, and the pointer to |
| 5866 | * the first delimiter is updated if required. The function tries as much as |
| 5867 | * possible to respect the following principles : |
| 5868 | * - replace <from> delimiter by the <next> one unless <from> points to a |
| 5869 | * colon, in which case <next> is simply removed |
| 5870 | * - set exactly one space character after the new first delimiter, unless |
| 5871 | * there are not enough characters in the block being moved to do so. |
| 5872 | * - remove unneeded spaces before the previous delimiter and after the new |
| 5873 | * one. |
| 5874 | * |
| 5875 | * It is the caller's responsibility to ensure that : |
| 5876 | * - <from> points to a valid delimiter or the colon ; |
| 5877 | * - <next> points to a valid delimiter or the final CR/LF ; |
| 5878 | * - there are non-space chars before <from> ; |
| 5879 | * - there is a CR/LF at or after <next>. |
| 5880 | */ |
Willy Tarreau | c5a4fd5 | 2018-12-11 11:42:27 +0100 | [diff] [blame] | 5881 | static int del_hdr_value(struct buffer *buf, char **from, char *next) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5882 | { |
| 5883 | char *prev = *from; |
| 5884 | |
| 5885 | if (*prev == ':') { |
| 5886 | /* We're removing the first value, preserve the colon and add a |
| 5887 | * space if possible. |
| 5888 | */ |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 5889 | if (!HTTP_IS_CRLF(*next)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5890 | next++; |
| 5891 | prev++; |
| 5892 | if (prev < next) |
| 5893 | *prev++ = ' '; |
| 5894 | |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 5895 | while (HTTP_IS_SPHT(*next)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5896 | next++; |
| 5897 | } else { |
| 5898 | /* Remove useless spaces before the old delimiter. */ |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 5899 | while (HTTP_IS_SPHT(*(prev-1))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5900 | prev--; |
| 5901 | *from = prev; |
| 5902 | |
| 5903 | /* copy the delimiter and if possible a space if we're |
| 5904 | * not at the end of the line. |
| 5905 | */ |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 5906 | if (!HTTP_IS_CRLF(*next)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5907 | *prev++ = *next++; |
| 5908 | if (prev + 1 < next) |
| 5909 | *prev++ = ' '; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 5910 | while (HTTP_IS_SPHT(*next)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5911 | next++; |
| 5912 | } |
| 5913 | } |
Willy Tarreau | e312802 | 2018-07-12 15:55:34 +0200 | [diff] [blame] | 5914 | return b_rep_blk(buf, prev, next, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5915 | } |
| 5916 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5917 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 5918 | * 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] | 5919 | * desirable to call it only when needed. This code is quite complex because |
| 5920 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 5921 | * highly recommended not to touch this part without a good reason ! |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5922 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5923 | void manage_client_side_cookies(struct stream *s, struct channel *req) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5924 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5925 | struct http_txn *txn = s->txn; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5926 | struct session *sess = s->sess; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5927 | int preserve_hdr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5928 | int cur_idx, old_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5929 | char *hdr_beg, *hdr_end, *hdr_next, *del_from; |
| 5930 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5931 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5932 | /* Iterate through the headers, we start with the start line. */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 5933 | old_idx = 0; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5934 | hdr_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5935 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5936 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5937 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5938 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5939 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5940 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5941 | hdr_beg = hdr_next; |
| 5942 | hdr_end = hdr_beg + cur_hdr->len; |
| 5943 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5944 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5945 | /* We have one full header between hdr_beg and hdr_end, and the |
| 5946 | * next header starts at hdr_next. We're only interested in |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5947 | * "Cookie:" headers. |
| 5948 | */ |
| 5949 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5950 | val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5951 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5952 | old_idx = cur_idx; |
| 5953 | continue; |
| 5954 | } |
| 5955 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5956 | del_from = NULL; /* nothing to be deleted */ |
| 5957 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 5958 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5959 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 5960 | * attributes whose name begin with a '$', and associate them with |
| 5961 | * the right cookie, if we want to delete this cookie. |
| 5962 | * So there are 3 cases for each cookie read : |
| 5963 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 5964 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 5965 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 5966 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 5967 | * "special" cookie. |
| 5968 | * At the end of loop, if a "special" cookie remains, we may have to |
| 5969 | * remove it. If no application cookie persists in the header, we |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5970 | * *MUST* delete it. |
| 5971 | * |
| 5972 | * Note: RFC2965 is unclear about the processing of spaces around |
| 5973 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 5974 | * the RFC explicitly allows spaces before it, and not within the |
| 5975 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 5976 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 5977 | * after the equal sign too, resulting in some (rare) buggy |
| 5978 | * implementations trying to do that. So let's do what servers do. |
| 5979 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 5980 | * allowed quoted strings in values, with any possible character |
| 5981 | * after a backslash, including control chars and delimitors, which |
| 5982 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 5983 | * within values even without quotes. |
| 5984 | * |
| 5985 | * We have to keep multiple pointers in order to support cookie |
| 5986 | * removal at the beginning, middle or end of header without |
| 5987 | * corrupting the header. All of these headers are valid : |
| 5988 | * |
| 5989 | * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n |
| 5990 | * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n |
| 5991 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 5992 | * | | | | | | | | | |
| 5993 | * | | | | | | | | hdr_end <--+ |
| 5994 | * | | | | | | | +--> next |
| 5995 | * | | | | | | +----> val_end |
| 5996 | * | | | | | +-----------> val_beg |
| 5997 | * | | | | +--------------> equal |
| 5998 | * | | | +----------------> att_end |
| 5999 | * | | +---------------------> att_beg |
| 6000 | * | +--------------------------> prev |
| 6001 | * +--------------------------------> hdr_beg |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6002 | */ |
| 6003 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6004 | for (prev = hdr_beg + 6; prev < hdr_end; prev = next) { |
| 6005 | /* Iterate through all cookies on this line */ |
| 6006 | |
| 6007 | /* find att_beg */ |
| 6008 | att_beg = prev + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6009 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6010 | att_beg++; |
| 6011 | |
| 6012 | /* find att_end : this is the first character after the last non |
| 6013 | * space before the equal. It may be equal to hdr_end. |
| 6014 | */ |
| 6015 | equal = att_end = att_beg; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6016 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6017 | while (equal < hdr_end) { |
| 6018 | if (*equal == '=' || *equal == ',' || *equal == ';') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6019 | break; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6020 | if (HTTP_IS_SPHT(*equal++)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6021 | continue; |
| 6022 | att_end = equal; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6023 | } |
| 6024 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6025 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 6026 | * is between <att_beg> and <equal>, both may be identical. |
| 6027 | */ |
| 6028 | |
| 6029 | /* look for end of cookie if there is an equal sign */ |
| 6030 | if (equal < hdr_end && *equal == '=') { |
| 6031 | /* look for the beginning of the value */ |
| 6032 | val_beg = equal + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6033 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6034 | val_beg++; |
| 6035 | |
| 6036 | /* find the end of the value, respecting quotes */ |
Willy Tarreau | ab813a4 | 2018-09-10 18:41:28 +0200 | [diff] [blame] | 6037 | next = http_find_cookie_value_end(val_beg, hdr_end); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6038 | |
| 6039 | /* make val_end point to the first white space or delimitor after the value */ |
| 6040 | val_end = next; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6041 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6042 | val_end--; |
| 6043 | } else { |
| 6044 | val_beg = val_end = next = equal; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6045 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6046 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6047 | /* We have nothing to do with attributes beginning with '$'. However, |
| 6048 | * they will automatically be removed if a header before them is removed, |
| 6049 | * since they're supposed to be linked together. |
| 6050 | */ |
| 6051 | if (*att_beg == '$') |
| 6052 | continue; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6053 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6054 | /* Ignore cookies with no equal sign */ |
| 6055 | if (equal == next) { |
| 6056 | /* This is not our cookie, so we must preserve it. But if we already |
| 6057 | * scheduled another cookie for removal, we cannot remove the |
| 6058 | * complete header, but we can remove the previous block itself. |
| 6059 | */ |
| 6060 | preserve_hdr = 1; |
| 6061 | if (del_from != NULL) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 6062 | int delta = del_hdr_value(&req->buf, &del_from, prev); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6063 | val_end += delta; |
| 6064 | next += delta; |
| 6065 | hdr_end += delta; |
| 6066 | hdr_next += delta; |
| 6067 | cur_hdr->len += delta; |
| 6068 | http_msg_move_end(&txn->req, delta); |
| 6069 | prev = del_from; |
| 6070 | del_from = NULL; |
| 6071 | } |
| 6072 | continue; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6073 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6074 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6075 | /* if there are spaces around the equal sign, we need to |
| 6076 | * strip them otherwise we'll get trouble for cookie captures, |
| 6077 | * or even for rewrites. Since this happens extremely rarely, |
| 6078 | * it does not hurt performance. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6079 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6080 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 6081 | int stripped_before = 0; |
| 6082 | int stripped_after = 0; |
| 6083 | |
| 6084 | if (att_end != equal) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 6085 | stripped_before = b_rep_blk(&req->buf, att_end, equal, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6086 | equal += stripped_before; |
| 6087 | val_beg += stripped_before; |
| 6088 | } |
| 6089 | |
| 6090 | if (val_beg > equal + 1) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 6091 | 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] | 6092 | val_beg += stripped_after; |
| 6093 | stripped_before += stripped_after; |
| 6094 | } |
| 6095 | |
| 6096 | val_end += stripped_before; |
| 6097 | next += stripped_before; |
| 6098 | hdr_end += stripped_before; |
| 6099 | hdr_next += stripped_before; |
| 6100 | cur_hdr->len += stripped_before; |
| 6101 | http_msg_move_end(&txn->req, stripped_before); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6102 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6103 | /* now everything is as on the diagram above */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6104 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6105 | /* First, let's see if we want to capture this cookie. We check |
| 6106 | * that we don't already have a client side cookie, because we |
| 6107 | * can only capture one. Also as an optimisation, we ignore |
| 6108 | * cookies shorter than the declared name. |
| 6109 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6110 | if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 6111 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 6112 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6113 | int log_len = val_end - att_beg; |
| 6114 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6115 | if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 6116 | ha_alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6117 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6118 | if (log_len > sess->fe->capture_len) |
| 6119 | log_len = sess->fe->capture_len; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6120 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 6121 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6122 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6123 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6124 | |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6125 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 6126 | * following form : |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6127 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6128 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6129 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6130 | * For cookies in prefix mode, the form is : |
| 6131 | * |
| 6132 | * Cookie: NAME=SRV~VALUE |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6133 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6134 | if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 6135 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
| 6136 | struct server *srv = s->be->srv; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6137 | char *delim; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6138 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6139 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 6140 | * have the server ID between val_beg and delim, and the original cookie between |
| 6141 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 6142 | * |
| 6143 | * Cookie: NAME=SRV; # in all but prefix modes |
| 6144 | * Cookie: NAME=SRV~OPAQUE ; # in prefix mode |
| 6145 | * | || || | |+-> next |
| 6146 | * | || || | +--> val_end |
| 6147 | * | || || +---------> delim |
| 6148 | * | || |+------------> val_beg |
| 6149 | * | || +-------------> att_end = equal |
| 6150 | * | |+-----------------> att_beg |
| 6151 | * | +------------------> prev |
| 6152 | * +-------------------------> hdr_beg |
| 6153 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6154 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6155 | if (s->be->ck_opts & PR_CK_PFX) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6156 | for (delim = val_beg; delim < val_end; delim++) |
| 6157 | if (*delim == COOKIE_DELIM) |
| 6158 | break; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6159 | } else { |
| 6160 | char *vbar1; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6161 | delim = val_end; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6162 | /* Now check if the cookie contains a date field, which would |
| 6163 | * appear after a vertical bar ('|') just after the server name |
| 6164 | * and before the delimiter. |
| 6165 | */ |
| 6166 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 6167 | if (vbar1) { |
| 6168 | /* OK, so left of the bar is the server's cookie and |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6169 | * right is the last seen date. It is a base64 encoded |
| 6170 | * 30-bit value representing the UNIX date since the |
| 6171 | * epoch in 4-second quantities. |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6172 | */ |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6173 | int val; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6174 | delim = vbar1++; |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6175 | if (val_end - vbar1 >= 5) { |
| 6176 | val = b64tos30(vbar1); |
| 6177 | if (val > 0) |
| 6178 | txn->cookie_last_date = val << 2; |
| 6179 | } |
| 6180 | /* look for a second vertical bar */ |
| 6181 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 6182 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 6183 | val = b64tos30(vbar1 + 1); |
| 6184 | if (val > 0) |
| 6185 | txn->cookie_first_date = val << 2; |
| 6186 | } |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6187 | } |
| 6188 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6189 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6190 | /* if the cookie has an expiration date and the proxy wants to check |
| 6191 | * it, then we do that now. We first check if the cookie is too old, |
| 6192 | * then only if it has expired. We detect strict overflow because the |
| 6193 | * time resolution here is not great (4 seconds). Cookies with dates |
| 6194 | * in the future are ignored if their offset is beyond one day. This |
| 6195 | * allows an admin to fix timezone issues without expiring everyone |
| 6196 | * and at the same time avoids keeping unwanted side effects for too |
| 6197 | * long. |
| 6198 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6199 | if (txn->cookie_first_date && s->be->cookie_maxlife && |
| 6200 | (((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] | 6201 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6202 | txn->flags &= ~TX_CK_MASK; |
| 6203 | txn->flags |= TX_CK_OLD; |
| 6204 | delim = val_beg; // let's pretend we have not found the cookie |
| 6205 | txn->cookie_first_date = 0; |
| 6206 | txn->cookie_last_date = 0; |
| 6207 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6208 | else if (txn->cookie_last_date && s->be->cookie_maxidle && |
| 6209 | (((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] | 6210 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6211 | txn->flags &= ~TX_CK_MASK; |
| 6212 | txn->flags |= TX_CK_EXPIRED; |
| 6213 | delim = val_beg; // let's pretend we have not found the cookie |
| 6214 | txn->cookie_first_date = 0; |
| 6215 | txn->cookie_last_date = 0; |
| 6216 | } |
| 6217 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6218 | /* Here, we'll look for the first running server which supports the cookie. |
| 6219 | * This allows to share a same cookie between several servers, for example |
| 6220 | * to dedicate backup servers to specific servers only. |
| 6221 | * However, to prevent clients from sticking to cookie-less backup server |
| 6222 | * when they have incidentely learned an empty cookie, we simply ignore |
| 6223 | * empty cookies and mark them as invalid. |
| 6224 | * The same behaviour is applied when persistence must be ignored. |
| 6225 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6226 | if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6227 | srv = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6228 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6229 | while (srv) { |
| 6230 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 6231 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 6232 | if ((srv->cur_state != SRV_ST_STOPPED) || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6233 | (s->be->options & PR_O_PERSIST) || |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6234 | (s->flags & SF_FORCE_PRST)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6235 | /* we found the server and we can use it */ |
| 6236 | txn->flags &= ~TX_CK_MASK; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 6237 | 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] | 6238 | s->flags |= SF_DIRECT | SF_ASSIGNED; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6239 | s->target = &srv->obj_type; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6240 | break; |
| 6241 | } else { |
| 6242 | /* we found a server, but it's down, |
| 6243 | * mark it as such and go on in case |
| 6244 | * another one is available. |
| 6245 | */ |
| 6246 | txn->flags &= ~TX_CK_MASK; |
| 6247 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6248 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6249 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6250 | srv = srv->next; |
| 6251 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6252 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6253 | 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] | 6254 | /* no server matched this cookie or we deliberately skipped it */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6255 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6256 | if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
Willy Tarreau | c89ccb6 | 2012-04-05 21:18:22 +0200 | [diff] [blame] | 6257 | txn->flags |= TX_CK_UNUSED; |
| 6258 | else |
| 6259 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6260 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6261 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6262 | /* depending on the cookie mode, we may have to either : |
| 6263 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 6264 | * the server never sees it ; |
| 6265 | * - 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] | 6266 | * application cookie so that it does not get accidently removed later, |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6267 | * if we're in cookie prefix mode |
| 6268 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6269 | if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6270 | int delta; /* negative */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6271 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 6272 | delta = b_rep_blk(&req->buf, val_beg, delim + 1, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6273 | val_end += delta; |
| 6274 | next += delta; |
| 6275 | hdr_end += delta; |
| 6276 | hdr_next += delta; |
| 6277 | cur_hdr->len += delta; |
| 6278 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6279 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6280 | del_from = NULL; |
| 6281 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 6282 | } |
| 6283 | else if (del_from == NULL && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6284 | (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] | 6285 | del_from = prev; |
| 6286 | } |
| 6287 | } else { |
| 6288 | /* This is not our cookie, so we must preserve it. But if we already |
| 6289 | * scheduled another cookie for removal, we cannot remove the |
| 6290 | * complete header, but we can remove the previous block itself. |
| 6291 | */ |
| 6292 | preserve_hdr = 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6293 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6294 | if (del_from != NULL) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 6295 | int delta = del_hdr_value(&req->buf, &del_from, prev); |
Willy Tarreau | b810554 | 2010-11-24 18:31:28 +0100 | [diff] [blame] | 6296 | if (att_beg >= del_from) |
| 6297 | att_beg += delta; |
| 6298 | if (att_end >= del_from) |
| 6299 | att_end += delta; |
| 6300 | val_beg += delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6301 | val_end += delta; |
| 6302 | next += delta; |
| 6303 | hdr_end += delta; |
| 6304 | hdr_next += delta; |
| 6305 | cur_hdr->len += delta; |
| 6306 | http_msg_move_end(&txn->req, delta); |
| 6307 | prev = del_from; |
| 6308 | del_from = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6309 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6310 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6311 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6312 | /* continue with next cookie on this header line */ |
| 6313 | att_beg = next; |
| 6314 | } /* for each cookie */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6315 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6316 | /* There are no more cookies on this line. |
| 6317 | * We may still have one (or several) marked for deletion at the |
| 6318 | * end of the line. We must do this now in two ways : |
| 6319 | * - if some cookies must be preserved, we only delete from the |
| 6320 | * mark to the end of line ; |
| 6321 | * - if nothing needs to be preserved, simply delete the whole header |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6322 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6323 | if (del_from) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6324 | int delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6325 | if (preserve_hdr) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 6326 | delta = del_hdr_value(&req->buf, &del_from, hdr_end); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6327 | hdr_end = del_from; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6328 | cur_hdr->len += delta; |
| 6329 | } else { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 6330 | delta = b_rep_blk(&req->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6331 | |
| 6332 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6333 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6334 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6335 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6336 | cur_idx = old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6337 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6338 | hdr_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6339 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6340 | } |
| 6341 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6342 | /* check next header */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6343 | old_idx = cur_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6344 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6345 | } |
| 6346 | |
| 6347 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6348 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 6349 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 6350 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6351 | 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] | 6352 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6353 | char *cur_ptr, *cur_end, *cur_next; |
| 6354 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6355 | struct http_txn *txn = s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6356 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 6357 | int delta, len; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6358 | |
| 6359 | last_hdr = 0; |
| 6360 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 6361 | cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6362 | old_idx = 0; |
| 6363 | |
| 6364 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6365 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6366 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6367 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6368 | (exp->action == ACT_ALLOW || |
| 6369 | exp->action == ACT_DENY)) |
| 6370 | return 0; |
| 6371 | |
| 6372 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 6373 | if (!cur_idx) |
| 6374 | break; |
| 6375 | |
| 6376 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 6377 | cur_ptr = cur_next; |
| 6378 | cur_end = cur_ptr + cur_hdr->len; |
| 6379 | cur_next = cur_end + cur_hdr->cr + 1; |
| 6380 | |
| 6381 | /* Now we have one header between cur_ptr and cur_end, |
| 6382 | * and the next header starts at cur_next. |
| 6383 | */ |
| 6384 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 6385 | 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] | 6386 | switch (exp->action) { |
| 6387 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6388 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6389 | last_hdr = 1; |
| 6390 | break; |
| 6391 | |
| 6392 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6393 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6394 | last_hdr = 1; |
| 6395 | break; |
| 6396 | |
| 6397 | case ACT_REPLACE: |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 6398 | len = exp_replace(trash.area, |
| 6399 | trash.size, cur_ptr, |
| 6400 | exp->replace, pmatch); |
| 6401 | if (len < 0) |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 6402 | return -1; |
| 6403 | |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 6404 | delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len); |
| 6405 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6406 | /* FIXME: if the user adds a newline in the replacement, the |
| 6407 | * index will not be recalculated for now, and the new line |
| 6408 | * will not be counted as a new header. |
| 6409 | */ |
| 6410 | |
| 6411 | cur_end += delta; |
| 6412 | cur_next += delta; |
| 6413 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6414 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6415 | break; |
| 6416 | |
| 6417 | case ACT_REMOVE: |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 6418 | delta = b_rep_blk(&rtr->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6419 | cur_next += delta; |
| 6420 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6421 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6422 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6423 | txn->hdr_idx.used--; |
| 6424 | cur_hdr->len = 0; |
| 6425 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6426 | cur_idx = old_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6427 | break; |
| 6428 | |
| 6429 | } |
| 6430 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6431 | |
| 6432 | /* keep the link from this header to next one in case of later |
| 6433 | * removal of next header. |
| 6434 | */ |
| 6435 | old_idx = cur_idx; |
| 6436 | } |
| 6437 | return 0; |
| 6438 | } |
| 6439 | |
| 6440 | |
| 6441 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 6442 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 6443 | * or -1 if a replacement resulted in an invalid status line. |
| 6444 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6445 | 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] | 6446 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6447 | char *cur_ptr, *cur_end; |
| 6448 | int done; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6449 | struct http_txn *txn = s->txn; |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 6450 | int delta, len; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6451 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6452 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6453 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6454 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6455 | (exp->action == ACT_ALLOW || |
| 6456 | exp->action == ACT_DENY)) |
| 6457 | return 0; |
| 6458 | else if (exp->action == ACT_REMOVE) |
| 6459 | return 0; |
| 6460 | |
| 6461 | done = 0; |
| 6462 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 6463 | cur_ptr = ci_head(rtr); |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 6464 | cur_end = cur_ptr + txn->rsp.sl.st.l; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6465 | |
| 6466 | /* Now we have the status line between cur_ptr and cur_end */ |
| 6467 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 6468 | 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] | 6469 | switch (exp->action) { |
| 6470 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6471 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6472 | done = 1; |
| 6473 | break; |
| 6474 | |
| 6475 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6476 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6477 | done = 1; |
| 6478 | break; |
| 6479 | |
| 6480 | case ACT_REPLACE: |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 6481 | len = exp_replace(trash.area, trash.size, |
| 6482 | cur_ptr, exp->replace, pmatch); |
| 6483 | if (len < 0) |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 6484 | return -1; |
| 6485 | |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 6486 | delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len); |
| 6487 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6488 | /* FIXME: if the user adds a newline in the replacement, the |
| 6489 | * index will not be recalculated for now, and the new line |
| 6490 | * will not be counted as a new header. |
| 6491 | */ |
| 6492 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6493 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6494 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 6495 | cur_end = (char *)http_parse_stsline(&txn->rsp, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 6496 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6497 | cur_ptr, cur_end + 1, |
| 6498 | NULL, NULL); |
| 6499 | if (unlikely(!cur_end)) |
| 6500 | return -1; |
| 6501 | |
| 6502 | /* we have a full respnse and we know that we have either a CR |
| 6503 | * or an LF at <ptr>. |
| 6504 | */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 6505 | 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] | 6506 | 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] | 6507 | /* there is no point trying this regex on headers */ |
| 6508 | return 1; |
| 6509 | } |
| 6510 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6511 | return done; |
| 6512 | } |
| 6513 | |
| 6514 | |
| 6515 | |
| 6516 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6517 | * 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] | 6518 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 6519 | * unparsable response. |
| 6520 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6521 | 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] | 6522 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 6523 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6524 | struct http_txn *txn = s->txn; |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6525 | struct hdr_exp *exp; |
| 6526 | |
| 6527 | for (exp = px->rsp_exp; exp; exp = exp->next) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6528 | int ret; |
| 6529 | |
| 6530 | /* |
| 6531 | * The interleaving of transformations and verdicts |
| 6532 | * makes it difficult to decide to continue or stop |
| 6533 | * the evaluation. |
| 6534 | */ |
| 6535 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6536 | if (txn->flags & TX_SVDENY) |
| 6537 | break; |
| 6538 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6539 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6540 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 6541 | exp->action == ACT_PASS)) { |
| 6542 | exp = exp->next; |
| 6543 | continue; |
| 6544 | } |
| 6545 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6546 | /* if this filter had a condition, evaluate it now and skip to |
| 6547 | * next filter if the condition does not match. |
| 6548 | */ |
| 6549 | if (exp->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 6550 | 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] | 6551 | ret = acl_pass(ret); |
| 6552 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 6553 | ret = !ret; |
| 6554 | if (!ret) |
| 6555 | continue; |
| 6556 | } |
| 6557 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6558 | /* Apply the filter to the status line. */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6559 | ret = apply_filter_to_sts_line(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6560 | if (unlikely(ret < 0)) |
| 6561 | return -1; |
| 6562 | |
| 6563 | if (likely(ret == 0)) { |
| 6564 | /* The filter did not match the response, it can be |
| 6565 | * iterated through all headers. |
| 6566 | */ |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 6567 | if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0)) |
| 6568 | return -1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6569 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6570 | } |
| 6571 | return 0; |
| 6572 | } |
| 6573 | |
| 6574 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6575 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 6576 | * 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] | 6577 | * desirable to call it only when needed. This function is also used when we |
| 6578 | * 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] | 6579 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6580 | void manage_server_side_cookies(struct stream *s, struct channel *res) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6581 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6582 | struct http_txn *txn = s->txn; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6583 | struct session *sess = s->sess; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6584 | struct server *srv; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6585 | int is_cookie2; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6586 | int cur_idx, old_idx, delta; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6587 | char *hdr_beg, *hdr_end, *hdr_next; |
| 6588 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6589 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6590 | /* Iterate through the headers. |
| 6591 | * we start with the start line. |
| 6592 | */ |
| 6593 | old_idx = 0; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 6594 | hdr_next = ci_head(res) + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6595 | |
| 6596 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 6597 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6598 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6599 | |
| 6600 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6601 | hdr_beg = hdr_next; |
| 6602 | hdr_end = hdr_beg + cur_hdr->len; |
| 6603 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6604 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6605 | /* We have one full header between hdr_beg and hdr_end, and the |
| 6606 | * next header starts at hdr_next. We're only interested in |
| 6607 | * "Set-Cookie" and "Set-Cookie2" headers. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6608 | */ |
| 6609 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6610 | is_cookie2 = 0; |
| 6611 | prev = hdr_beg + 10; |
| 6612 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6613 | if (!val) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6614 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11); |
| 6615 | if (!val) { |
| 6616 | old_idx = cur_idx; |
| 6617 | continue; |
| 6618 | } |
| 6619 | is_cookie2 = 1; |
| 6620 | prev = hdr_beg + 11; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6621 | } |
| 6622 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6623 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 6624 | * <prev> points to the colon. |
| 6625 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 6626 | txn->flags |= TX_SCK_PRESENT; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6627 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6628 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 6629 | * check-cache is enabled) and we are not interested in checking |
| 6630 | * them. Warning, the cookie capture is declared in the frontend. |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 6631 | */ |
Willy Tarreau | 53a09d5 | 2015-08-10 18:59:40 +0200 | [diff] [blame] | 6632 | if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6633 | return; |
| 6634 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6635 | /* OK so now we know we have to process this response cookie. |
| 6636 | * The format of the Set-Cookie header is slightly different |
| 6637 | * from the format of the Cookie header in that it does not |
| 6638 | * support the comma as a cookie delimiter (thus the header |
| 6639 | * cannot be folded) because the Expires attribute described in |
| 6640 | * the original Netscape's spec may contain an unquoted date |
| 6641 | * with a comma inside. We have to live with this because |
| 6642 | * many browsers don't support Max-Age and some browsers don't |
| 6643 | * support quoted strings. However the Set-Cookie2 header is |
| 6644 | * clean. |
| 6645 | * |
| 6646 | * We have to keep multiple pointers in order to support cookie |
| 6647 | * removal at the beginning, middle or end of header without |
| 6648 | * corrupting the header (in case of set-cookie2). A special |
| 6649 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 6650 | * fields after the first semi-colon. The <next> pointer points |
| 6651 | * either to the end of line (set-cookie) or next unquoted comma |
| 6652 | * (set-cookie2). All of these headers are valid : |
| 6653 | * |
| 6654 | * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n |
| 6655 | * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 6656 | * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 6657 | * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n |
| 6658 | * | | | | | | | | | | |
| 6659 | * | | | | | | | | +-> next hdr_end <--+ |
| 6660 | * | | | | | | | +------------> scav |
| 6661 | * | | | | | | +--------------> val_end |
| 6662 | * | | | | | +--------------------> val_beg |
| 6663 | * | | | | +----------------------> equal |
| 6664 | * | | | +------------------------> att_end |
| 6665 | * | | +----------------------------> att_beg |
| 6666 | * | +------------------------------> prev |
| 6667 | * +-----------------------------------------> hdr_beg |
| 6668 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6669 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6670 | for (; prev < hdr_end; prev = next) { |
| 6671 | /* Iterate through all cookies on this line */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6672 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6673 | /* find att_beg */ |
| 6674 | att_beg = prev + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6675 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6676 | att_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6677 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6678 | /* find att_end : this is the first character after the last non |
| 6679 | * space before the equal. It may be equal to hdr_end. |
| 6680 | */ |
| 6681 | equal = att_end = att_beg; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6682 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6683 | while (equal < hdr_end) { |
| 6684 | if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ',')) |
| 6685 | break; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6686 | if (HTTP_IS_SPHT(*equal++)) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6687 | continue; |
| 6688 | att_end = equal; |
| 6689 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6690 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6691 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 6692 | * is between <att_beg> and <equal>, both may be identical. |
| 6693 | */ |
| 6694 | |
| 6695 | /* look for end of cookie if there is an equal sign */ |
| 6696 | if (equal < hdr_end && *equal == '=') { |
| 6697 | /* look for the beginning of the value */ |
| 6698 | val_beg = equal + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6699 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6700 | val_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6701 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6702 | /* find the end of the value, respecting quotes */ |
Willy Tarreau | ab813a4 | 2018-09-10 18:41:28 +0200 | [diff] [blame] | 6703 | next = http_find_cookie_value_end(val_beg, hdr_end); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6704 | |
| 6705 | /* make val_end point to the first white space or delimitor after the value */ |
| 6706 | val_end = next; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6707 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6708 | val_end--; |
| 6709 | } else { |
| 6710 | /* <equal> points to next comma, semi-colon or EOL */ |
| 6711 | val_beg = val_end = next = equal; |
| 6712 | } |
| 6713 | |
| 6714 | if (next < hdr_end) { |
| 6715 | /* Set-Cookie2 supports multiple cookies, and <next> points to |
| 6716 | * a colon or semi-colon before the end. So skip all attr-value |
| 6717 | * pairs and look for the next comma. For Set-Cookie, since |
| 6718 | * commas are permitted in values, skip to the end. |
| 6719 | */ |
| 6720 | if (is_cookie2) |
Willy Tarreau | ab813a4 | 2018-09-10 18:41:28 +0200 | [diff] [blame] | 6721 | next = http_find_hdr_value_end(next, hdr_end); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6722 | else |
| 6723 | next = hdr_end; |
| 6724 | } |
| 6725 | |
| 6726 | /* Now everything is as on the diagram above */ |
| 6727 | |
| 6728 | /* Ignore cookies with no equal sign */ |
| 6729 | if (equal == val_end) |
| 6730 | continue; |
| 6731 | |
| 6732 | /* If there are spaces around the equal sign, we need to |
| 6733 | * strip them otherwise we'll get trouble for cookie captures, |
| 6734 | * or even for rewrites. Since this happens extremely rarely, |
| 6735 | * it does not hurt performance. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6736 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6737 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 6738 | int stripped_before = 0; |
| 6739 | int stripped_after = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6740 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6741 | if (att_end != equal) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 6742 | stripped_before = b_rep_blk(&res->buf, att_end, equal, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6743 | equal += stripped_before; |
| 6744 | val_beg += stripped_before; |
| 6745 | } |
| 6746 | |
| 6747 | if (val_beg > equal + 1) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 6748 | 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] | 6749 | val_beg += stripped_after; |
| 6750 | stripped_before += stripped_after; |
| 6751 | } |
| 6752 | |
| 6753 | val_end += stripped_before; |
| 6754 | next += stripped_before; |
| 6755 | hdr_end += stripped_before; |
| 6756 | hdr_next += stripped_before; |
| 6757 | cur_hdr->len += stripped_before; |
Willy Tarreau | 1fc1f45 | 2011-04-07 22:35:37 +0200 | [diff] [blame] | 6758 | http_msg_move_end(&txn->rsp, stripped_before); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6759 | } |
| 6760 | |
| 6761 | /* First, let's see if we want to capture this cookie. We check |
| 6762 | * that we don't already have a server side cookie, because we |
| 6763 | * can only capture one. Also as an optimisation, we ignore |
| 6764 | * cookies shorter than the declared name. |
| 6765 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6766 | if (sess->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 6767 | txn->srv_cookie == NULL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6768 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 6769 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6770 | int log_len = val_end - att_beg; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6771 | if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 6772 | ha_alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6773 | } |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 6774 | else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6775 | if (log_len > sess->fe->capture_len) |
| 6776 | log_len = sess->fe->capture_len; |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 6777 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 6778 | txn->srv_cookie[log_len] = 0; |
| 6779 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6780 | } |
| 6781 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6782 | srv = objt_server(s->target); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6783 | /* now check if we need to process it for persistence */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6784 | if (!(s->flags & SF_IGNORE_PRST) && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6785 | (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 6786 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 6787 | /* assume passive cookie by default */ |
| 6788 | txn->flags &= ~TX_SCK_MASK; |
| 6789 | txn->flags |= TX_SCK_FOUND; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6790 | |
| 6791 | /* If the cookie is in insert mode on a known server, we'll delete |
| 6792 | * this occurrence because we'll insert another one later. |
| 6793 | * 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] | 6794 | * a direct access. |
| 6795 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6796 | if (s->be->ck_opts & PR_CK_PSV) { |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 6797 | /* The "preserve" flag was set, we don't want to touch the |
| 6798 | * server's cookie. |
| 6799 | */ |
| 6800 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6801 | else if ((srv && (s->be->ck_opts & PR_CK_INS)) || |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6802 | ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6803 | /* this cookie must be deleted */ |
| 6804 | if (*prev == ':' && next == hdr_end) { |
| 6805 | /* whole header */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 6806 | delta = b_rep_blk(&res->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6807 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6808 | txn->hdr_idx.used--; |
| 6809 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6810 | cur_idx = old_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6811 | hdr_next += delta; |
| 6812 | http_msg_move_end(&txn->rsp, delta); |
| 6813 | /* note: while both invalid now, <next> and <hdr_end> |
| 6814 | * are still equal, so the for() will stop as expected. |
| 6815 | */ |
| 6816 | } else { |
| 6817 | /* just remove the value */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 6818 | int delta = del_hdr_value(&res->buf, &prev, next); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6819 | next = prev; |
| 6820 | hdr_end += delta; |
| 6821 | hdr_next += delta; |
| 6822 | cur_hdr->len += delta; |
| 6823 | http_msg_move_end(&txn->rsp, delta); |
| 6824 | } |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 6825 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6826 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6827 | /* and go on with next cookie */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6828 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6829 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6830 | /* replace bytes val_beg->val_end with the cookie name associated |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6831 | * with this server since we know it. |
| 6832 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 6833 | 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] | 6834 | next += delta; |
| 6835 | hdr_end += delta; |
| 6836 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6837 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6838 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6839 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 6840 | txn->flags &= ~TX_SCK_MASK; |
| 6841 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6842 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6843 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6844 | /* insert the cookie name associated with this server |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6845 | * before existing cookie, and insert a delimiter between them.. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6846 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 6847 | 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] | 6848 | next += delta; |
| 6849 | hdr_end += delta; |
| 6850 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6851 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6852 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6853 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6854 | val_beg[srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 6855 | txn->flags &= ~TX_SCK_MASK; |
| 6856 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6857 | } |
| 6858 | } |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6859 | /* that's done for this cookie, check the next one on the same |
| 6860 | * line when next != hdr_end (only if is_cookie2). |
| 6861 | */ |
| 6862 | } |
| 6863 | /* check next header */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6864 | old_idx = cur_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6865 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6866 | } |
| 6867 | |
| 6868 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6869 | /* |
Willy Tarreau | 0ad8e0d | 2017-12-22 15:03:36 +0100 | [diff] [blame] | 6870 | * Parses the Cache-Control and Pragma request header fields to determine if |
| 6871 | * the request may be served from the cache and/or if it is cacheable. Updates |
| 6872 | * s->txn->flags. |
| 6873 | */ |
| 6874 | void check_request_for_cacheability(struct stream *s, struct channel *chn) |
| 6875 | { |
| 6876 | struct http_txn *txn = s->txn; |
| 6877 | char *p1, *p2; |
| 6878 | char *cur_ptr, *cur_end, *cur_next; |
| 6879 | int pragma_found; |
| 6880 | int cc_found; |
| 6881 | int cur_idx; |
| 6882 | |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 6883 | if (IS_HTX_STRM(s)) |
| 6884 | return htx_check_request_for_cacheability(s, chn); |
| 6885 | |
Willy Tarreau | 0ad8e0d | 2017-12-22 15:03:36 +0100 | [diff] [blame] | 6886 | if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE) |
| 6887 | return; /* nothing more to do here */ |
| 6888 | |
| 6889 | cur_idx = 0; |
| 6890 | pragma_found = cc_found = 0; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 6891 | cur_next = ci_head(chn) + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 0ad8e0d | 2017-12-22 15:03:36 +0100 | [diff] [blame] | 6892 | |
| 6893 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 6894 | struct hdr_idx_elem *cur_hdr; |
| 6895 | int val; |
| 6896 | |
| 6897 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 6898 | cur_ptr = cur_next; |
| 6899 | cur_end = cur_ptr + cur_hdr->len; |
| 6900 | cur_next = cur_end + cur_hdr->cr + 1; |
| 6901 | |
| 6902 | /* We have one full header between cur_ptr and cur_end, and the |
| 6903 | * next header starts at cur_next. |
| 6904 | */ |
| 6905 | |
| 6906 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 6907 | if (val) { |
| 6908 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 6909 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 6910 | pragma_found = 1; |
| 6911 | continue; |
| 6912 | } |
| 6913 | } |
| 6914 | |
William Lallemand | 8a16fe0 | 2018-05-22 11:04:33 +0200 | [diff] [blame] | 6915 | /* Don't use the cache and don't try to store if we found the |
| 6916 | * Authorization header */ |
| 6917 | val = http_header_match2(cur_ptr, cur_end, "Authorization", 13); |
| 6918 | if (val) { |
| 6919 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 6920 | txn->flags |= TX_CACHE_IGNORE; |
| 6921 | continue; |
| 6922 | } |
| 6923 | |
Willy Tarreau | 0ad8e0d | 2017-12-22 15:03:36 +0100 | [diff] [blame] | 6924 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 6925 | if (!val) |
| 6926 | continue; |
| 6927 | |
| 6928 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 6929 | cc_found = 1; |
| 6930 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
| 6931 | |
| 6932 | if (p1 >= cur_end) /* no more info */ |
| 6933 | continue; |
| 6934 | |
| 6935 | /* p1 is at the beginning of the value */ |
| 6936 | p2 = p1; |
| 6937 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
| 6938 | p2++; |
| 6939 | |
| 6940 | /* we have a complete value between p1 and p2. We don't check the |
| 6941 | * values after max-age, max-stale nor min-fresh, we simply don't |
| 6942 | * use the cache when they're specified. |
| 6943 | */ |
| 6944 | if (((p2 - p1 == 7) && strncasecmp(p1, "max-age", 7) == 0) || |
| 6945 | ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) || |
| 6946 | ((p2 - p1 == 9) && strncasecmp(p1, "max-stale", 9) == 0) || |
| 6947 | ((p2 - p1 == 9) && strncasecmp(p1, "min-fresh", 9) == 0)) { |
| 6948 | txn->flags |= TX_CACHE_IGNORE; |
| 6949 | continue; |
| 6950 | } |
| 6951 | |
| 6952 | if ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) { |
| 6953 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 6954 | continue; |
| 6955 | } |
| 6956 | } |
| 6957 | |
| 6958 | /* RFC7234#5.4: |
| 6959 | * When the Cache-Control header field is also present and |
| 6960 | * understood in a request, Pragma is ignored. |
| 6961 | * When the Cache-Control header field is not present in a |
| 6962 | * request, caches MUST consider the no-cache request |
| 6963 | * pragma-directive as having the same effect as if |
| 6964 | * "Cache-Control: no-cache" were present. |
| 6965 | */ |
| 6966 | if (!cc_found && pragma_found) |
| 6967 | txn->flags |= TX_CACHE_IGNORE; |
| 6968 | } |
| 6969 | |
| 6970 | /* |
Willy Tarreau | d3900cc | 2017-12-22 15:35:11 +0100 | [diff] [blame] | 6971 | * Check if response is cacheable or not. Updates s->txn->flags. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6972 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6973 | void check_response_for_cacheability(struct stream *s, struct channel *rtr) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6974 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6975 | struct http_txn *txn = s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6976 | char *p1, *p2; |
| 6977 | |
| 6978 | char *cur_ptr, *cur_end, *cur_next; |
| 6979 | int cur_idx; |
| 6980 | |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 6981 | |
| 6982 | if (IS_HTX_STRM(s)) |
| 6983 | return htx_check_response_for_cacheability(s, rtr); |
| 6984 | |
Willy Tarreau | 12b32f2 | 2017-12-21 16:08:09 +0100 | [diff] [blame] | 6985 | if (txn->status < 200) { |
| 6986 | /* do not try to cache interim responses! */ |
| 6987 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6988 | return; |
Willy Tarreau | 12b32f2 | 2017-12-21 16:08:09 +0100 | [diff] [blame] | 6989 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6990 | |
| 6991 | /* Iterate through the headers. |
| 6992 | * we start with the start line. |
| 6993 | */ |
| 6994 | cur_idx = 0; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 6995 | cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6996 | |
| 6997 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 6998 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6999 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7000 | |
| 7001 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 7002 | cur_ptr = cur_next; |
| 7003 | cur_end = cur_ptr + cur_hdr->len; |
| 7004 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7005 | |
| 7006 | /* 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] | 7007 | * next header starts at cur_next. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7008 | */ |
| 7009 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7010 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 7011 | if (val) { |
| 7012 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 7013 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 7014 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 7015 | return; |
| 7016 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7017 | } |
| 7018 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7019 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 7020 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7021 | continue; |
| 7022 | |
| 7023 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 7024 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7025 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7026 | |
| 7027 | if (p1 >= cur_end) /* no more info */ |
| 7028 | continue; |
| 7029 | |
| 7030 | /* p1 is at the beginning of the value */ |
| 7031 | p2 = p1; |
| 7032 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 7033 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7034 | p2++; |
| 7035 | |
| 7036 | /* we have a complete value between p1 and p2 */ |
| 7037 | if (p2 < cur_end && *p2 == '=') { |
Willy Tarreau | d3900cc | 2017-12-22 15:35:11 +0100 | [diff] [blame] | 7038 | if (((cur_end - p2) > 1 && (p2 - p1 == 7) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 7039 | ((cur_end - p2) > 1 && (p2 - p1 == 8) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
| 7040 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 7041 | continue; |
| 7042 | } |
| 7043 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7044 | /* we have something of the form no-cache="set-cookie" */ |
| 7045 | if ((cur_end - p1 >= 21) && |
| 7046 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 7047 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7048 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7049 | continue; |
| 7050 | } |
| 7051 | |
| 7052 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 7053 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
Willy Tarreau | 5b15f90 | 2013-07-04 12:46:56 +0200 | [diff] [blame] | 7054 | ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) || |
Willy Tarreau | d3900cc | 2017-12-22 15:35:11 +0100 | [diff] [blame] | 7055 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7056 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7057 | return; |
| 7058 | } |
| 7059 | |
| 7060 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7061 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7062 | continue; |
| 7063 | } |
| 7064 | } |
| 7065 | } |
| 7066 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7067 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7068 | /* |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7069 | * 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] | 7070 | * for the current backend. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7071 | * |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7072 | * 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] | 7073 | * uri_auth field is valid. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7074 | * |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7075 | * Returns 1 if stats should be provided, otherwise 0. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7076 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7077 | 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] | 7078 | { |
| 7079 | struct uri_auth *uri_auth = backend->uri_auth; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7080 | struct http_msg *msg = &txn->req; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 7081 | const char *uri = ci_head(msg->chn)+ msg->sl.rq.u; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7082 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7083 | if (!uri_auth) |
| 7084 | return 0; |
| 7085 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7086 | 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] | 7087 | return 0; |
| 7088 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7089 | /* check URI size */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7090 | if (uri_auth->uri_len > msg->sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7091 | return 0; |
| 7092 | |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 7093 | if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7094 | return 0; |
| 7095 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7096 | return 1; |
| 7097 | } |
| 7098 | |
Willy Tarreau | 7ccdd8d | 2018-09-07 14:01:39 +0200 | [diff] [blame] | 7099 | /* Append the description of what is present in error snapshot <es> into <out>. |
| 7100 | * The description must be small enough to always fit in a trash. The output |
| 7101 | * buffer may be the trash so the trash must not be used inside this function. |
| 7102 | */ |
| 7103 | void http_show_error_snapshot(struct buffer *out, const struct error_snapshot *es) |
| 7104 | { |
Christopher Faulet | ed26fb8 | 2018-11-29 22:53:30 +0100 | [diff] [blame] | 7105 | chunk_appendf(out, |
Willy Tarreau | 7ccdd8d | 2018-09-07 14:01:39 +0200 | [diff] [blame] | 7106 | " stream #%d, stream flags 0x%08x, tx flags 0x%08x\n" |
| 7107 | " HTTP msg state %s(%d), msg flags 0x%08x\n" |
| 7108 | " HTTP chunk len %lld bytes, HTTP body len %lld bytes, channel flags 0x%08x :\n", |
| 7109 | es->ctx.http.sid, es->ctx.http.s_flags, es->ctx.http.t_flags, |
| 7110 | h1_msg_state_str(es->ctx.http.state), es->ctx.http.state, |
| 7111 | es->ctx.http.m_flags, es->ctx.http.m_clen, |
| 7112 | es->ctx.http.m_blen, es->ctx.http.b_flags); |
| 7113 | } |
| 7114 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7115 | /* |
| 7116 | * 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] | 7117 | * By default it tries to report the error position as msg->err_pos. However if |
| 7118 | * this one is not set, it will then report msg->next, which is the last known |
| 7119 | * 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] | 7120 | * displays buffers as a contiguous area starting at buf->p. The direction is |
| 7121 | * determined thanks to the channel's flags. |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7122 | */ |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 7123 | void http_capture_bad_message(struct proxy *proxy, struct stream *s, |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 7124 | struct http_msg *msg, |
Willy Tarreau | 0da5b3b | 2017-09-21 09:30:46 +0200 | [diff] [blame] | 7125 | enum h1_state state, struct proxy *other_end) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7126 | { |
Willy Tarreau | ef3ca73 | 2018-09-07 15:47:35 +0200 | [diff] [blame] | 7127 | union error_snapshot_ctx ctx; |
| 7128 | long ofs; |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 7129 | |
Willy Tarreau | ef3ca73 | 2018-09-07 15:47:35 +0200 | [diff] [blame] | 7130 | /* http-specific part now */ |
| 7131 | ctx.http.sid = s->uniq_id; |
| 7132 | ctx.http.state = state; |
| 7133 | ctx.http.b_flags = msg->chn->flags; |
| 7134 | ctx.http.s_flags = s->flags; |
| 7135 | ctx.http.t_flags = s->txn->flags; |
| 7136 | ctx.http.m_flags = msg->flags; |
| 7137 | ctx.http.m_clen = msg->chunk_len; |
| 7138 | ctx.http.m_blen = msg->body_len; |
Willy Tarreau | 7480f32 | 2018-09-06 19:41:22 +0200 | [diff] [blame] | 7139 | |
Willy Tarreau | ef3ca73 | 2018-09-07 15:47:35 +0200 | [diff] [blame] | 7140 | ofs = msg->chn->total - ci_data(msg->chn); |
| 7141 | if (ofs < 0) |
| 7142 | ofs = 0; |
Willy Tarreau | 0b5b480 | 2018-09-07 13:49:44 +0200 | [diff] [blame] | 7143 | |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 7144 | proxy_capture_error(proxy, !!(msg->chn->flags & CF_ISRESP), |
Willy Tarreau | ef3ca73 | 2018-09-07 15:47:35 +0200 | [diff] [blame] | 7145 | other_end, s->target, |
| 7146 | strm_sess(s), &msg->chn->buf, |
| 7147 | ofs, co_data(msg->chn), |
| 7148 | (msg->err_pos >= 0) ? msg->err_pos : msg->next, |
| 7149 | &ctx, http_show_error_snapshot); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7150 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7151 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 7152 | /* |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 7153 | * Print a debug line with a header. Always stop at the first CR or LF char, |
| 7154 | * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an |
| 7155 | * arrow is printed after the line which contains the pointer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7156 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7157 | 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] | 7158 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 7159 | struct session *sess = strm_sess(s); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7160 | int max; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 7161 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7162 | 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] | 7163 | dir, |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 7164 | 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] | 7165 | 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] | 7166 | |
| 7167 | for (max = 0; start + max < end; max++) |
| 7168 | if (start[max] == '\r' || start[max] == '\n') |
| 7169 | break; |
| 7170 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7171 | UBOUND(max, trash.size - trash.data - 3); |
| 7172 | trash.data += strlcpy2(trash.area + trash.data, start, max + 1); |
| 7173 | trash.area[trash.data++] = '\n'; |
| 7174 | shut_your_big_mouth_gcc(write(1, trash.area, trash.data)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7175 | } |
| 7176 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7177 | |
| 7178 | /* Allocate a new HTTP transaction for stream <s> unless there is one already. |
| 7179 | * The hdr_idx is allocated as well. In case of allocation failure, everything |
| 7180 | * allocated is freed and NULL is returned. Otherwise the new transaction is |
| 7181 | * assigned to the stream and returned. |
| 7182 | */ |
| 7183 | struct http_txn *http_alloc_txn(struct stream *s) |
| 7184 | { |
| 7185 | struct http_txn *txn = s->txn; |
| 7186 | |
| 7187 | if (txn) |
| 7188 | return txn; |
| 7189 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7190 | txn = pool_alloc(pool_head_http_txn); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7191 | if (!txn) |
| 7192 | return txn; |
| 7193 | |
| 7194 | txn->hdr_idx.size = global.tune.max_http_hdr; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7195 | txn->hdr_idx.v = pool_alloc(pool_head_hdr_idx); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7196 | if (!txn->hdr_idx.v) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7197 | pool_free(pool_head_http_txn, txn); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7198 | return NULL; |
| 7199 | } |
| 7200 | |
| 7201 | s->txn = txn; |
| 7202 | return txn; |
| 7203 | } |
| 7204 | |
Thierry FOURNIER | fd50f0b | 2015-09-25 18:53:18 +0200 | [diff] [blame] | 7205 | void http_txn_reset_req(struct http_txn *txn) |
| 7206 | { |
| 7207 | txn->req.flags = 0; |
| 7208 | txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */ |
| 7209 | txn->req.next = 0; |
| 7210 | txn->req.chunk_len = 0LL; |
| 7211 | txn->req.body_len = 0LL; |
| 7212 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 7213 | } |
| 7214 | |
| 7215 | void http_txn_reset_res(struct http_txn *txn) |
| 7216 | { |
| 7217 | txn->rsp.flags = 0; |
| 7218 | txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */ |
| 7219 | txn->rsp.next = 0; |
| 7220 | txn->rsp.chunk_len = 0LL; |
| 7221 | txn->rsp.body_len = 0LL; |
| 7222 | txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */ |
| 7223 | } |
| 7224 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7225 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7226 | * 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] | 7227 | * the required fields are properly allocated and that we only need to (re)init |
| 7228 | * them. This should be used before processing any new request. |
| 7229 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7230 | void http_init_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7231 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7232 | struct http_txn *txn = s->txn; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 7233 | struct proxy *fe = strm_fe(s); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 7234 | struct conn_stream *cs = objt_cs(s->si[0].end); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7235 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 7236 | txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST) |
| 7237 | ? (TX_NOT_FIRST|TX_WAIT_NEXT_RQ) |
| 7238 | : 0); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7239 | txn->status = -1; |
Willy Tarreau | c9036c0 | 2019-01-11 19:38:25 +0100 | [diff] [blame] | 7240 | *(unsigned int *)txn->cache_hash = 0; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7241 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7242 | txn->cookie_first_date = 0; |
| 7243 | txn->cookie_last_date = 0; |
| 7244 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7245 | txn->srv_cookie = NULL; |
| 7246 | txn->cli_cookie = NULL; |
| 7247 | txn->uri = NULL; |
| 7248 | |
Thierry FOURNIER | fd50f0b | 2015-09-25 18:53:18 +0200 | [diff] [blame] | 7249 | http_txn_reset_req(txn); |
| 7250 | http_txn_reset_res(txn); |
| 7251 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7252 | txn->req.chn = &s->req; |
| 7253 | txn->rsp.chn = &s->res; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 7254 | |
| 7255 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7256 | |
| 7257 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 7258 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 7259 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 7260 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7261 | if (txn->hdr_idx.v) |
| 7262 | hdr_idx_init(&txn->hdr_idx); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 7263 | |
| 7264 | vars_init(&s->vars_txn, SCOPE_TXN); |
| 7265 | vars_init(&s->vars_reqres, SCOPE_REQ); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7266 | } |
| 7267 | |
| 7268 | /* to be used at the end of a transaction */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7269 | void http_end_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7270 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7271 | struct http_txn *txn = s->txn; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7272 | |
| 7273 | /* these ones will have been dynamically allocated */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7274 | pool_free(pool_head_requri, txn->uri); |
| 7275 | pool_free(pool_head_capture, txn->cli_cookie); |
| 7276 | pool_free(pool_head_capture, txn->srv_cookie); |
| 7277 | pool_free(pool_head_uniqueid, s->unique_id); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 7278 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 7279 | s->unique_id = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7280 | txn->uri = NULL; |
| 7281 | txn->srv_cookie = NULL; |
| 7282 | txn->cli_cookie = NULL; |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7283 | |
Willy Tarreau | cda7f3f | 2018-10-28 13:44:36 +0100 | [diff] [blame] | 7284 | if (!LIST_ISEMPTY(&s->vars_txn.head)) |
| 7285 | vars_prune(&s->vars_txn, s->sess, s); |
| 7286 | if (!LIST_ISEMPTY(&s->vars_reqres.head)) |
| 7287 | vars_prune(&s->vars_reqres, s->sess, s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7288 | } |
| 7289 | |
| 7290 | /* 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] | 7291 | void http_reset_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7292 | { |
| 7293 | http_end_txn(s); |
| 7294 | http_init_txn(s); |
| 7295 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 7296 | /* reinitialise the current rule list pointer to NULL. We are sure that |
| 7297 | * any rulelist match the NULL pointer. |
| 7298 | */ |
| 7299 | s->current_rule_list = NULL; |
| 7300 | |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 7301 | s->be = strm_fe(s); |
| 7302 | s->logs.logwait = strm_fe(s)->to_log; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 7303 | s->logs.level = 0; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7304 | stream_del_srv_conn(s); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7305 | s->target = NULL; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 7306 | /* re-init store persistence */ |
| 7307 | s->store_count = 0; |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 7308 | s->uniq_id = _HA_ATOMIC_XADD(&global.req_count, 1); |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 7309 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7310 | s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */ |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7311 | |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 7312 | /* We must trim any excess data from the response buffer, because we |
| 7313 | * 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] | 7314 | * want to accidently forward once we disable the analysers, nor do |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 7315 | * we want those data to come along with next response. A typical |
| 7316 | * example of such data would be from a buggy server responding to |
| 7317 | * a HEAD with some data, or sending more than the advertised |
| 7318 | * content-length. |
| 7319 | */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 7320 | if (unlikely(ci_data(&s->res))) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 7321 | b_set_data(&s->res.buf, co_data(&s->res)); |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 7322 | |
Christopher Faulet | c0c672a | 2017-03-28 11:51:33 +0200 | [diff] [blame] | 7323 | /* Now we can realign the response buffer */ |
Willy Tarreau | d5b343b | 2018-06-06 06:42:46 +0200 | [diff] [blame] | 7324 | c_realign_if_empty(&s->res); |
Christopher Faulet | c0c672a | 2017-03-28 11:51:33 +0200 | [diff] [blame] | 7325 | |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 7326 | s->req.rto = strm_fe(s)->timeout.client; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7327 | s->req.wto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7328 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7329 | s->res.rto = TICK_ETERNITY; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 7330 | s->res.wto = strm_fe(s)->timeout.client; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7331 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7332 | s->req.rex = TICK_ETERNITY; |
| 7333 | s->req.wex = TICK_ETERNITY; |
| 7334 | s->req.analyse_exp = TICK_ETERNITY; |
| 7335 | s->res.rex = TICK_ETERNITY; |
| 7336 | s->res.wex = TICK_ETERNITY; |
| 7337 | s->res.analyse_exp = TICK_ETERNITY; |
Hongbo Long | e39683c | 2017-03-10 18:41:51 +0100 | [diff] [blame] | 7338 | s->si[1].hcto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7339 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7340 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7341 | /* This function executes one of the set-{method,path,query,uri} actions. It |
| 7342 | * takes the string from the variable 'replace' with length 'len', then modifies |
| 7343 | * the relevant part of the request line accordingly. Then it updates various |
| 7344 | * pointers to the next elements which were moved, and the total buffer length. |
| 7345 | * It finds the action to be performed in p[2], previously filled by function |
| 7346 | * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal |
| 7347 | * error, though this can be revisited when this code is finally exploited. |
| 7348 | * |
| 7349 | * 'action' can be '0' to replace method, '1' to replace path, '2' to replace |
| 7350 | * query string and 3 to replace uri. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7351 | * |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7352 | * In query string case, the mark question '?' must be set at the start of the |
| 7353 | * string by the caller, event if the replacement query string is empty. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7354 | */ |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7355 | int http_replace_req_line(int action, const char *replace, int len, |
| 7356 | struct proxy *px, struct stream *s) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7357 | { |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7358 | struct http_txn *txn = s->txn; |
| 7359 | char *cur_ptr, *cur_end; |
| 7360 | int offset = 0; |
| 7361 | int delta; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7362 | |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 7363 | if (IS_HTX_STRM(s)) |
| 7364 | return htx_req_replace_stline(action, replace, len, px, s); |
| 7365 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7366 | switch (action) { |
| 7367 | case 0: // method |
| 7368 | cur_ptr = ci_head(&s->req); |
| 7369 | cur_end = cur_ptr + txn->req.sl.rq.m_l; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7370 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7371 | /* adjust req line offsets and lengths */ |
| 7372 | delta = len - offset - (cur_end - cur_ptr); |
| 7373 | txn->req.sl.rq.m_l += delta; |
| 7374 | txn->req.sl.rq.u += delta; |
| 7375 | txn->req.sl.rq.v += delta; |
| 7376 | break; |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 7377 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7378 | case 1: // path |
| 7379 | cur_ptr = http_txn_get_path(txn); |
| 7380 | if (!cur_ptr) |
| 7381 | cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7382 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7383 | cur_end = cur_ptr; |
| 7384 | while (cur_end < ci_head(&s->req) + txn->req.sl.rq.u + txn->req.sl.rq.u_l && *cur_end != '?') |
| 7385 | cur_end++; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7386 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7387 | /* adjust req line offsets and lengths */ |
| 7388 | delta = len - offset - (cur_end - cur_ptr); |
| 7389 | txn->req.sl.rq.u_l += delta; |
| 7390 | txn->req.sl.rq.v += delta; |
| 7391 | break; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7392 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7393 | case 2: // query |
| 7394 | offset = 1; |
| 7395 | cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u; |
| 7396 | cur_end = cur_ptr + txn->req.sl.rq.u_l; |
| 7397 | while (cur_ptr < cur_end && *cur_ptr != '?') |
| 7398 | cur_ptr++; |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 7399 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7400 | /* skip the question mark or indicate that we must insert it |
| 7401 | * (but only if the format string is not empty then). |
| 7402 | */ |
| 7403 | if (cur_ptr < cur_end) |
| 7404 | cur_ptr++; |
| 7405 | else if (len > 1) |
| 7406 | offset = 0; |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 7407 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7408 | /* adjust req line offsets and lengths */ |
| 7409 | delta = len - offset - (cur_end - cur_ptr); |
| 7410 | txn->req.sl.rq.u_l += delta; |
| 7411 | txn->req.sl.rq.v += delta; |
| 7412 | break; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7413 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7414 | case 3: // uri |
| 7415 | cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u; |
| 7416 | cur_end = cur_ptr + txn->req.sl.rq.u_l; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7417 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7418 | /* adjust req line offsets and lengths */ |
| 7419 | delta = len - offset - (cur_end - cur_ptr); |
| 7420 | txn->req.sl.rq.u_l += delta; |
| 7421 | txn->req.sl.rq.v += delta; |
| 7422 | break; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7423 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7424 | default: |
| 7425 | return -1; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7426 | } |
| 7427 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7428 | /* commit changes and adjust end of message */ |
| 7429 | delta = b_rep_blk(&s->req.buf, cur_ptr, cur_end, replace + offset, len - offset); |
| 7430 | txn->req.sl.rq.l += delta; |
| 7431 | txn->hdr_idx.v[0].len += delta; |
| 7432 | http_msg_move_end(&txn->req, delta); |
| 7433 | return 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7434 | } |
| 7435 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7436 | /* This function replace the HTTP status code and the associated message. The |
| 7437 | * variable <status> contains the new status code. This function never fails. |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7438 | */ |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7439 | 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] | 7440 | { |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7441 | struct http_txn *txn = s->txn; |
| 7442 | char *cur_ptr, *cur_end; |
| 7443 | int delta; |
| 7444 | char *res; |
| 7445 | int c_l; |
| 7446 | const char *msg = reason; |
| 7447 | int msg_len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7448 | |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 7449 | if (IS_HTX_STRM(s)) |
| 7450 | return htx_res_set_status(status, reason, s); |
| 7451 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7452 | chunk_reset(&trash); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7453 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7454 | res = ultoa_o(status, trash.area, trash.size); |
| 7455 | c_l = res - trash.area; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7456 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7457 | trash.area[c_l] = ' '; |
| 7458 | trash.data = c_l + 1; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7459 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7460 | /* Do we have a custom reason format string? */ |
| 7461 | if (msg == NULL) |
| 7462 | msg = http_get_reason(status); |
| 7463 | msg_len = strlen(msg); |
| 7464 | strncpy(&trash.area[trash.data], msg, trash.size - trash.data); |
| 7465 | trash.data += msg_len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7466 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7467 | cur_ptr = ci_head(&s->res) + txn->rsp.sl.st.c; |
| 7468 | 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] | 7469 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7470 | /* commit changes and adjust message */ |
| 7471 | delta = b_rep_blk(&s->res.buf, cur_ptr, cur_end, trash.area, |
| 7472 | trash.data); |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 7473 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7474 | /* adjust res line offsets and lengths */ |
| 7475 | txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l; |
| 7476 | txn->rsp.sl.st.c_l = c_l; |
| 7477 | txn->rsp.sl.st.r_l = msg_len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7478 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7479 | delta = trash.data - (cur_end - cur_ptr); |
| 7480 | txn->rsp.sl.st.l += delta; |
| 7481 | txn->hdr_idx.v[0].len += delta; |
| 7482 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7483 | } |
| 7484 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7485 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 7486 | * Local variables: |
| 7487 | * c-indent-level: 8 |
| 7488 | * c-basic-offset: 8 |
| 7489 | * End: |
| 7490 | */ |