Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HTT/1 mux-demux for connections |
| 3 | * |
| 4 | * Copyright 2018 Christopher Faulet <cfaulet@haproxy.com> |
| 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 | #include <common/cfgparse.h> |
| 13 | #include <common/config.h> |
Willy Tarreau | afba57a | 2018-12-11 13:44:24 +0100 | [diff] [blame] | 14 | #include <common/h1.h> |
Christopher Faulet | 0ef372a | 2019-04-08 10:57:20 +0200 | [diff] [blame] | 15 | #include <common/h2.h> |
Willy Tarreau | b96b77e | 2018-12-11 10:22:41 +0100 | [diff] [blame] | 16 | #include <common/htx.h> |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 17 | #include <common/initcall.h> |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 18 | |
Christopher Faulet | a99ff4d | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 19 | #include <ebistree.h> |
| 20 | |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 21 | #include <types/pipe.h> |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 22 | #include <types/proxy.h> |
| 23 | #include <types/session.h> |
| 24 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 25 | #include <proto/connection.h> |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 26 | #include <proto/http_htx.h> |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 27 | #include <proto/log.h> |
Olivier Houchard | 44d5914 | 2018-12-13 18:46:22 +0100 | [diff] [blame] | 28 | #include <proto/session.h> |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 29 | #include <proto/stream.h> |
| 30 | #include <proto/stream_interface.h> |
| 31 | |
| 32 | /* |
| 33 | * H1 Connection flags (32 bits) |
| 34 | */ |
| 35 | #define H1C_F_NONE 0x00000000 |
| 36 | |
| 37 | /* Flags indicating why writing output data are blocked */ |
| 38 | #define H1C_F_OUT_ALLOC 0x00000001 /* mux is blocked on lack of output buffer */ |
| 39 | #define H1C_F_OUT_FULL 0x00000002 /* mux is blocked on output buffer full */ |
| 40 | /* 0x00000004 - 0x00000008 unused */ |
| 41 | |
| 42 | /* Flags indicating why reading input data are blocked. */ |
| 43 | #define H1C_F_IN_ALLOC 0x00000010 /* mux is blocked on lack of input buffer */ |
| 44 | #define H1C_F_IN_FULL 0x00000020 /* mux is blocked on input buffer full */ |
Christopher Faulet | f36e72b | 2019-12-05 11:18:31 +0100 | [diff] [blame] | 45 | #define H1C_F_IN_BUSY 0x00000040 /* mux is blocked on input waiting the other side */ |
Christopher Faulet | e31a0f1 | 2019-12-05 10:23:37 +0100 | [diff] [blame] | 46 | /* 0x00000040 - 0x00000400 unused */ |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 47 | |
Christopher Faulet | e31a0f1 | 2019-12-05 10:23:37 +0100 | [diff] [blame] | 48 | #define H1C_F_CS_WAIT_CONN 0x00000800 /* waiting for the connection establishment */ |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 49 | #define H1C_F_CS_ERROR 0x00001000 /* connection must be closed ASAP because an error occurred */ |
| 50 | #define H1C_F_CS_SHUTW_NOW 0x00002000 /* connection must be shut down for writes ASAP */ |
Christopher Faulet | f36e72b | 2019-12-05 11:18:31 +0100 | [diff] [blame] | 51 | #define H1C_F_CS_SHUTDOWN 0x00004000 /* connection is shut down */ |
Christopher Faulet | e31a0f1 | 2019-12-05 10:23:37 +0100 | [diff] [blame] | 52 | #define H1C_F_CS_IDLE 0x00008000 /* connection is idle and may be reused |
| 53 | * (exclusive to all H1C_F_CS flags and never set when an h1s is attached) */ |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 54 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 55 | #define H1C_F_WAIT_NEXT_REQ 0x00010000 /* waiting for the next request to start, use keep-alive timeout */ |
Christopher Faulet | 0ef372a | 2019-04-08 10:57:20 +0200 | [diff] [blame] | 56 | #define H1C_F_UPG_H2C 0x00020000 /* set if an upgrade to h2 should be done */ |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 57 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 58 | /* |
| 59 | * H1 Stream flags (32 bits) |
| 60 | */ |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 61 | #define H1S_F_NONE 0x00000000 |
| 62 | #define H1S_F_ERROR 0x00000001 /* An error occurred on the H1 stream */ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 63 | #define H1S_F_REQ_ERROR 0x00000002 /* An error occurred during the request parsing/xfer */ |
| 64 | #define H1S_F_RES_ERROR 0x00000004 /* An error occurred during the response parsing/xfer */ |
Willy Tarreau | c493c9c | 2019-06-03 14:18:22 +0200 | [diff] [blame] | 65 | #define H1S_F_REOS 0x00000008 /* End of input stream seen even if not delivered yet */ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 66 | #define H1S_F_WANT_KAL 0x00000010 |
| 67 | #define H1S_F_WANT_TUN 0x00000020 |
| 68 | #define H1S_F_WANT_CLO 0x00000040 |
| 69 | #define H1S_F_WANT_MSK 0x00000070 |
| 70 | #define H1S_F_NOT_FIRST 0x00000080 /* The H1 stream is not the first one */ |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 71 | #define H1S_F_BUF_FLUSH 0x00000100 /* Flush input buffer and don't read more data */ |
Christopher Faulet | d44ad5b | 2018-11-19 21:52:12 +0100 | [diff] [blame] | 72 | #define H1S_F_SPLICED_DATA 0x00000200 /* Set when the kernel splicing is in used */ |
Willy Tarreau | 34d2348 | 2019-01-03 17:46:56 +0100 | [diff] [blame] | 73 | #define H1S_F_HAVE_I_TLR 0x00000800 /* Set during input process to know the trailers were processed */ |
Willy Tarreau | 6203815 | 2019-08-23 09:29:29 +0200 | [diff] [blame] | 74 | #define H1S_F_APPEND_EOM 0x00001000 /* Send EOM to the HTX buffer */ |
| 75 | /* 0x00002000 .. 0x00002000 unused */ |
Christopher Faulet | ada34b6 | 2019-05-24 16:36:43 +0200 | [diff] [blame] | 76 | #define H1S_F_HAVE_O_CONN 0x00004000 /* Set during output process to know connection mode was processed */ |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 77 | |
| 78 | /* H1 connection descriptor */ |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 79 | struct h1c { |
| 80 | struct connection *conn; |
| 81 | struct proxy *px; |
| 82 | uint32_t flags; /* Connection flags: H1C_F_* */ |
| 83 | |
| 84 | struct buffer ibuf; /* Input buffer to store data before parsing */ |
| 85 | struct buffer obuf; /* Output buffer to store data after reformatting */ |
| 86 | |
| 87 | struct buffer_wait buf_wait; /* Wait list for buffer allocation */ |
| 88 | struct wait_event wait_event; /* To be used if we're waiting for I/Os */ |
| 89 | |
| 90 | struct h1s *h1s; /* H1 stream descriptor */ |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 91 | struct task *task; /* timeout management task */ |
| 92 | int timeout; /* idle timeout duration in ticks */ |
| 93 | int shut_timeout; /* idle timeout duration in ticks after stream shutdown */ |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | /* H1 stream descriptor */ |
| 97 | struct h1s { |
| 98 | struct h1c *h1c; |
| 99 | struct conn_stream *cs; |
Christopher Faulet | feb1174 | 2018-11-29 15:12:34 +0100 | [diff] [blame] | 100 | struct cs_info csinfo; /* CS info, only used for client connections */ |
| 101 | uint32_t flags; /* Connection flags: H1S_F_* */ |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 102 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 103 | struct wait_event *recv_wait; /* Address of the wait_event the conn_stream associated is waiting on */ |
| 104 | struct wait_event *send_wait; /* Address of the wait_event the conn_stream associated is waiting on */ |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 105 | |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 106 | struct session *sess; /* Associated session */ |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 107 | struct h1m req; |
| 108 | struct h1m res; |
| 109 | |
| 110 | enum http_meth_t meth; /* HTTP resquest method */ |
| 111 | uint16_t status; /* HTTP response status */ |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 112 | }; |
| 113 | |
Christopher Faulet | a99ff4d | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 114 | /* Map of headers used to convert outgoing headers */ |
| 115 | struct h1_hdrs_map { |
| 116 | char *name; |
| 117 | struct eb_root map; |
| 118 | }; |
| 119 | |
| 120 | /* An entry in a headers map */ |
| 121 | struct h1_hdr_entry { |
| 122 | struct ist name; |
| 123 | struct ebpt_node node; |
| 124 | }; |
| 125 | |
| 126 | /* Declare the headers map */ |
| 127 | static struct h1_hdrs_map hdrs_map = { .name = NULL, .map = EB_ROOT }; |
| 128 | |
| 129 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 130 | /* the h1c and h1s pools */ |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 131 | DECLARE_STATIC_POOL(pool_head_h1c, "h1c", sizeof(struct h1c)); |
| 132 | DECLARE_STATIC_POOL(pool_head_h1s, "h1s", sizeof(struct h1s)); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 133 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 134 | static int h1_recv(struct h1c *h1c); |
| 135 | static int h1_send(struct h1c *h1c); |
| 136 | static int h1_process(struct h1c *h1c); |
| 137 | static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short state); |
Christopher Faulet | 666a0c4 | 2019-01-08 11:12:04 +0100 | [diff] [blame] | 138 | static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode); |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 139 | static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 140 | |
| 141 | /*****************************************************/ |
| 142 | /* functions below are for dynamic buffer management */ |
| 143 | /*****************************************************/ |
| 144 | /* |
Christopher Faulet | 7caf150 | 2019-12-05 12:30:55 +0100 | [diff] [blame] | 145 | * Indicates whether or not we may receive data. The rules are the following : |
| 146 | * - if an error or a shutdown for reads was detected on the connection we |
| 147 | must not attempt to receive |
| 148 | * - if the input buffer failed to be allocated or is full , we must not try |
| 149 | * to receive |
| 150 | * - if he input processing is busy waiting for the output side, we may |
| 151 | * attemp to receive |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 152 | * - otherwise must may not attempt to receive |
| 153 | */ |
| 154 | static inline int h1_recv_allowed(const struct h1c *h1c) |
| 155 | { |
Christopher Faulet | 7caf150 | 2019-12-05 12:30:55 +0100 | [diff] [blame] | 156 | if (h1c->flags & H1C_F_CS_ERROR) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 157 | return 0; |
| 158 | |
Christopher Faulet | 7caf150 | 2019-12-05 12:30:55 +0100 | [diff] [blame] | 159 | if (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH)) |
Christopher Faulet | cf56b99 | 2018-12-11 16:12:31 +0100 | [diff] [blame] | 160 | return 0; |
| 161 | |
Christopher Faulet | cb55f48 | 2018-12-10 11:56:47 +0100 | [diff] [blame] | 162 | if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL|H1C_F_IN_BUSY))) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 163 | return 1; |
| 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | /* |
| 169 | * Tries to grab a buffer and to re-enables processing on mux <target>. The h1 |
| 170 | * flags are used to figure what buffer was requested. It returns 1 if the |
| 171 | * allocation succeeds, in which case the connection is woken up, or 0 if it's |
| 172 | * impossible to wake up and we prefer to be woken up later. |
| 173 | */ |
| 174 | static int h1_buf_available(void *target) |
| 175 | { |
| 176 | struct h1c *h1c = target; |
| 177 | |
| 178 | if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc_margin(&h1c->ibuf, 0)) { |
| 179 | h1c->flags &= ~H1C_F_IN_ALLOC; |
| 180 | if (h1_recv_allowed(h1c)) |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 181 | tasklet_wakeup(h1c->wait_event.tasklet); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 182 | return 1; |
| 183 | } |
| 184 | |
| 185 | if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc_margin(&h1c->obuf, 0)) { |
| 186 | h1c->flags &= ~H1C_F_OUT_ALLOC; |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 187 | tasklet_wakeup(h1c->wait_event.tasklet); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 188 | return 1; |
| 189 | } |
| 190 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | /* |
| 195 | * Allocate a buffer. If if fails, it adds the mux in buffer wait queue. |
| 196 | */ |
| 197 | static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr) |
| 198 | { |
| 199 | struct buffer *buf = NULL; |
| 200 | |
| 201 | if (likely(LIST_ISEMPTY(&h1c->buf_wait.list)) && |
| 202 | unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) { |
| 203 | h1c->buf_wait.target = h1c; |
| 204 | h1c->buf_wait.wakeup_cb = h1_buf_available; |
| 205 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
| 206 | LIST_ADDQ(&buffer_wq, &h1c->buf_wait.list); |
| 207 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
| 208 | __conn_xprt_stop_recv(h1c->conn); |
| 209 | } |
| 210 | return buf; |
| 211 | } |
| 212 | |
| 213 | /* |
| 214 | * Release a buffer, if any, and try to wake up entities waiting in the buffer |
| 215 | * wait queue. |
| 216 | */ |
| 217 | static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr) |
| 218 | { |
| 219 | if (bptr->size) { |
| 220 | b_free(bptr); |
| 221 | offer_buffers(h1c->buf_wait.target, tasks_run_queue); |
| 222 | } |
| 223 | } |
| 224 | |
Christopher Faulet | e31a0f1 | 2019-12-05 10:23:37 +0100 | [diff] [blame] | 225 | /* returns the number of streams in use on a connection to figure if it's idle |
| 226 | * or not. We rely on H1C_F_CS_IDLE to know if the connection is in-use or |
| 227 | * not. This flag is only set when no H1S is attached and when the previous |
| 228 | * stream, if any, was fully terminated without any error and in K/A mode. |
Willy Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 229 | */ |
| 230 | static int h1_used_streams(struct connection *conn) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 231 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 232 | struct h1c *h1c = conn->ctx; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 233 | |
Christopher Faulet | e31a0f1 | 2019-12-05 10:23:37 +0100 | [diff] [blame] | 234 | return ((h1c->flags & H1C_F_CS_IDLE) ? 0 : 1); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 235 | } |
| 236 | |
Willy Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 237 | /* returns the number of streams still available on a connection */ |
| 238 | static int h1_avail_streams(struct connection *conn) |
Olivier Houchard | 8defe4b | 2018-12-02 01:31:17 +0100 | [diff] [blame] | 239 | { |
Willy Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 240 | return 1 - h1_used_streams(conn); |
Olivier Houchard | 8defe4b | 2018-12-02 01:31:17 +0100 | [diff] [blame] | 241 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 242 | |
Willy Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 243 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 244 | /*****************************************************************/ |
| 245 | /* functions below are dedicated to the mux setup and management */ |
| 246 | /*****************************************************************/ |
Willy Tarreau | fbdf90a | 2019-06-03 13:42:54 +0200 | [diff] [blame] | 247 | |
| 248 | /* returns non-zero if there are input data pending for stream h1s. */ |
| 249 | static inline size_t h1s_data_pending(const struct h1s *h1s) |
| 250 | { |
| 251 | const struct h1m *h1m; |
| 252 | |
| 253 | h1m = conn_is_back(h1s->h1c->conn) ? &h1s->res : &h1s->req; |
| 254 | if (h1m->state == H1_MSG_DONE) |
| 255 | return 0; // data not for this stream (e.g. pipelining) |
| 256 | |
| 257 | return b_data(&h1s->h1c->ibuf); |
| 258 | } |
| 259 | |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 260 | static struct conn_stream *h1s_new_cs(struct h1s *h1s) |
| 261 | { |
| 262 | struct conn_stream *cs; |
| 263 | |
| 264 | cs = cs_new(h1s->h1c->conn); |
| 265 | if (!cs) |
| 266 | goto err; |
| 267 | h1s->cs = cs; |
| 268 | cs->ctx = h1s; |
| 269 | |
| 270 | if (h1s->flags & H1S_F_NOT_FIRST) |
| 271 | cs->flags |= CS_FL_NOT_FIRST; |
| 272 | |
Willy Tarreau | 7e9dd73 | 2020-01-17 16:19:34 +0100 | [diff] [blame] | 273 | if (global.tune.options & GTUNE_USE_SPLICE) |
| 274 | cs->flags |= CS_FL_MAY_SPLICE; |
| 275 | |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 276 | if (stream_create_from_cs(cs) < 0) |
| 277 | goto err; |
| 278 | return cs; |
| 279 | |
| 280 | err: |
| 281 | cs_free(cs); |
| 282 | h1s->cs = NULL; |
| 283 | return NULL; |
| 284 | } |
| 285 | |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 286 | static struct h1s *h1s_create(struct h1c *h1c, struct conn_stream *cs, struct session *sess) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 287 | { |
| 288 | struct h1s *h1s; |
| 289 | |
| 290 | h1s = pool_alloc(pool_head_h1s); |
| 291 | if (!h1s) |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 292 | goto fail; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 293 | |
| 294 | h1s->h1c = h1c; |
| 295 | h1c->h1s = h1s; |
| 296 | |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 297 | h1s->sess = sess; |
| 298 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 299 | h1s->cs = NULL; |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 300 | h1s->flags = H1S_F_WANT_KAL; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 301 | |
| 302 | h1s->recv_wait = NULL; |
| 303 | h1s->send_wait = NULL; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 304 | |
| 305 | h1m_init_req(&h1s->req); |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 306 | h1s->req.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 307 | |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 308 | h1m_init_res(&h1s->res); |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 309 | h1s->res.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR); |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 310 | |
| 311 | h1s->status = 0; |
| 312 | h1s->meth = HTTP_METH_OTHER; |
| 313 | |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 314 | if (h1c->flags & H1C_F_WAIT_NEXT_REQ) |
| 315 | h1s->flags |= H1S_F_NOT_FIRST; |
Christopher Faulet | e31a0f1 | 2019-12-05 10:23:37 +0100 | [diff] [blame] | 316 | h1c->flags &= ~(H1C_F_CS_IDLE|H1C_F_WAIT_NEXT_REQ); |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 317 | |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 318 | if (!conn_is_back(h1c->conn)) { |
| 319 | if (h1c->px->options2 & PR_O2_REQBUG_OK) |
| 320 | h1s->req.err_pos = -1; |
Christopher Faulet | e9b7072 | 2019-04-08 10:46:02 +0200 | [diff] [blame] | 321 | |
| 322 | /* For frontend connections we should always have a session */ |
| 323 | if (!sess) |
| 324 | sess = h1c->conn->owner; |
| 325 | |
Dave Pirotte | cf67ec5 | 2019-07-10 13:57:38 +0000 | [diff] [blame] | 326 | /* Timers for subsequent sessions on the same HTTP 1.x connection |
| 327 | * measure from `now`, not from the connection accept time */ |
| 328 | if (h1s->flags & H1S_F_NOT_FIRST) { |
| 329 | h1s->csinfo.create_date = date; |
| 330 | h1s->csinfo.tv_create = now; |
| 331 | h1s->csinfo.t_handshake = 0; |
| 332 | h1s->csinfo.t_idle = -1; |
| 333 | } |
| 334 | else { |
| 335 | h1s->csinfo.create_date = sess->accept_date; |
| 336 | h1s->csinfo.tv_create = sess->tv_accept; |
| 337 | h1s->csinfo.t_handshake = sess->t_handshake; |
| 338 | h1s->csinfo.t_idle = -1; |
| 339 | } |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 340 | } |
| 341 | else { |
| 342 | if (h1c->px->options2 & PR_O2_RSPBUG_OK) |
| 343 | h1s->res.err_pos = -1; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 344 | |
Christopher Faulet | feb1174 | 2018-11-29 15:12:34 +0100 | [diff] [blame] | 345 | h1s->csinfo.create_date = date; |
| 346 | h1s->csinfo.tv_create = now; |
| 347 | h1s->csinfo.t_handshake = 0; |
| 348 | h1s->csinfo.t_idle = -1; |
Christopher Faulet | e9b7072 | 2019-04-08 10:46:02 +0200 | [diff] [blame] | 349 | } |
Christopher Faulet | feb1174 | 2018-11-29 15:12:34 +0100 | [diff] [blame] | 350 | |
Christopher Faulet | e9b7072 | 2019-04-08 10:46:02 +0200 | [diff] [blame] | 351 | /* If a conn_stream already exists, attach it to this H1S. Otherwise we |
| 352 | * create a new one. |
| 353 | */ |
| 354 | if (cs) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 355 | cs->ctx = h1s; |
| 356 | h1s->cs = cs; |
| 357 | } |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 358 | else { |
| 359 | cs = h1s_new_cs(h1s); |
| 360 | if (!cs) |
| 361 | goto fail; |
| 362 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 363 | return h1s; |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 364 | |
| 365 | fail: |
| 366 | pool_free(pool_head_h1s, h1s); |
| 367 | return NULL; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | static void h1s_destroy(struct h1s *h1s) |
| 371 | { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 372 | if (h1s) { |
| 373 | struct h1c *h1c = h1s->h1c; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 374 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 375 | h1c->h1s = NULL; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 376 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 377 | if (h1s->recv_wait != NULL) |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 378 | h1s->recv_wait->events &= ~SUB_RETRY_RECV; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 379 | if (h1s->send_wait != NULL) |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 380 | h1s->send_wait->events &= ~SUB_RETRY_SEND; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 381 | |
Christopher Faulet | cb55f48 | 2018-12-10 11:56:47 +0100 | [diff] [blame] | 382 | h1c->flags &= ~H1C_F_IN_BUSY; |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 383 | if (h1s->flags & (H1S_F_REQ_ERROR|H1S_F_RES_ERROR)) |
| 384 | h1c->flags |= H1C_F_CS_ERROR; |
Christopher Faulet | e31a0f1 | 2019-12-05 10:23:37 +0100 | [diff] [blame] | 385 | |
| 386 | if (!(h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTDOWN)) && /* No error/shutdown on h1c */ |
| 387 | !(h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) && /* No error/shutdown on conn */ |
| 388 | (h1s->flags & H1S_F_WANT_KAL) && /* K/A possible */ |
| 389 | h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) { /* req/res in DONE state */ |
| 390 | h1c->flags |= (H1C_F_CS_IDLE|H1C_F_WAIT_NEXT_REQ); |
| 391 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 392 | pool_free(pool_head_h1s, h1s); |
| 393 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 394 | } |
| 395 | |
Christopher Faulet | feb1174 | 2018-11-29 15:12:34 +0100 | [diff] [blame] | 396 | static const struct cs_info *h1_get_cs_info(struct conn_stream *cs) |
| 397 | { |
| 398 | struct h1s *h1s = cs->ctx; |
| 399 | |
| 400 | if (h1s && !conn_is_back(cs->conn)) |
| 401 | return &h1s->csinfo; |
| 402 | return NULL; |
| 403 | } |
| 404 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 405 | /* |
Christopher Faulet | 51f73eb | 2019-04-08 11:22:47 +0200 | [diff] [blame] | 406 | * Initialize the mux once it's attached. It is expected that conn->ctx points |
| 407 | * to the existing conn_stream (for outgoing connections or for incoming onces |
| 408 | * during a mux upgrade) or NULL (for incoming ones during the connexion |
| 409 | * establishment). <input> is always used as Input buffer and may contain |
| 410 | * data. It is the caller responsibility to not reuse it anymore. Returns < 0 on |
| 411 | * error. |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 412 | */ |
Christopher Faulet | 51f73eb | 2019-04-08 11:22:47 +0200 | [diff] [blame] | 413 | static int h1_init(struct connection *conn, struct proxy *proxy, struct session *sess, |
| 414 | struct buffer *input) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 415 | { |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 416 | struct h1c *h1c; |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 417 | struct task *t = NULL; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 418 | |
| 419 | h1c = pool_alloc(pool_head_h1c); |
| 420 | if (!h1c) |
| 421 | goto fail_h1c; |
| 422 | h1c->conn = conn; |
| 423 | h1c->px = proxy; |
| 424 | |
Christopher Faulet | e31a0f1 | 2019-12-05 10:23:37 +0100 | [diff] [blame] | 425 | h1c->flags = H1C_F_CS_IDLE; |
Christopher Faulet | 51f73eb | 2019-04-08 11:22:47 +0200 | [diff] [blame] | 426 | h1c->ibuf = *input; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 427 | h1c->obuf = BUF_NULL; |
| 428 | h1c->h1s = NULL; |
Christopher Faulet | 51f73eb | 2019-04-08 11:22:47 +0200 | [diff] [blame] | 429 | h1c->task = NULL; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 430 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 431 | LIST_INIT(&h1c->buf_wait.list); |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 432 | h1c->wait_event.tasklet = tasklet_new(); |
| 433 | if (!h1c->wait_event.tasklet) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 434 | goto fail; |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 435 | h1c->wait_event.tasklet->process = h1_io_cb; |
| 436 | h1c->wait_event.tasklet->context = h1c; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 437 | h1c->wait_event.events = 0; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 438 | |
Christopher Faulet | e9b7072 | 2019-04-08 10:46:02 +0200 | [diff] [blame] | 439 | if (conn_is_back(conn)) { |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 440 | h1c->shut_timeout = h1c->timeout = proxy->timeout.server; |
| 441 | if (tick_isset(proxy->timeout.serverfin)) |
| 442 | h1c->shut_timeout = proxy->timeout.serverfin; |
| 443 | } else { |
| 444 | h1c->shut_timeout = h1c->timeout = proxy->timeout.client; |
| 445 | if (tick_isset(proxy->timeout.clientfin)) |
| 446 | h1c->shut_timeout = proxy->timeout.clientfin; |
| 447 | } |
| 448 | if (tick_isset(h1c->timeout)) { |
| 449 | t = task_new(tid_bit); |
| 450 | if (!t) |
| 451 | goto fail; |
| 452 | |
| 453 | h1c->task = t; |
| 454 | t->process = h1_timeout_task; |
| 455 | t->context = h1c; |
| 456 | t->expire = tick_add(now_ms, h1c->timeout); |
| 457 | } |
| 458 | |
Olivier Houchard | 985234d | 2019-06-13 17:54:33 +0200 | [diff] [blame] | 459 | if (!(conn->flags & CO_FL_CONNECTED) || (conn->flags & CO_FL_HANDSHAKE)) |
Christopher Faulet | 3b88b8d | 2018-10-26 17:36:03 +0200 | [diff] [blame] | 460 | h1c->flags |= H1C_F_CS_WAIT_CONN; |
| 461 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 462 | /* Always Create a new H1S */ |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 463 | if (!h1s_create(h1c, conn->ctx, sess)) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 464 | goto fail; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 465 | |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 466 | conn->ctx = h1c; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 467 | |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 468 | |
| 469 | if (t) |
| 470 | task_queue(t); |
| 471 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 472 | /* Try to read, if nothing is available yet we'll just subscribe */ |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 473 | tasklet_wakeup(h1c->wait_event.tasklet); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 474 | |
| 475 | /* mux->wake will be called soon to complete the operation */ |
| 476 | return 0; |
| 477 | |
| 478 | fail: |
Willy Tarreau | f656279 | 2019-05-07 19:05:35 +0200 | [diff] [blame] | 479 | task_destroy(t); |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 480 | if (h1c->wait_event.tasklet) |
| 481 | tasklet_free(h1c->wait_event.tasklet); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 482 | pool_free(pool_head_h1c, h1c); |
| 483 | fail_h1c: |
| 484 | return -1; |
| 485 | } |
| 486 | |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 487 | /* release function. This one should be called to free all resources allocated |
| 488 | * to the mux. |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 489 | */ |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 490 | static void h1_release(struct h1c *h1c) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 491 | { |
Christopher Faulet | 61840e7 | 2019-04-15 09:33:32 +0200 | [diff] [blame] | 492 | struct connection *conn = NULL; |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 493 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 494 | if (h1c) { |
Christopher Faulet | 61840e7 | 2019-04-15 09:33:32 +0200 | [diff] [blame] | 495 | /* The connection must be aattached to this mux to be released */ |
| 496 | if (h1c->conn && h1c->conn->ctx == h1c) |
| 497 | conn = h1c->conn; |
| 498 | |
| 499 | if (conn && h1c->flags & H1C_F_UPG_H2C) { |
Christopher Faulet | 0ef372a | 2019-04-08 10:57:20 +0200 | [diff] [blame] | 500 | h1c->flags &= ~H1C_F_UPG_H2C; |
Olivier Houchard | 13f556e | 2019-07-03 13:08:18 +0200 | [diff] [blame] | 501 | /* Make sure we're no longer subscribed to anything */ |
| 502 | if (h1c->wait_event.events) |
| 503 | conn->xprt->unsubscribe(conn, conn->xprt_ctx, |
| 504 | h1c->wait_event.events, &h1c->wait_event); |
Olivier Houchard | 45c4437 | 2019-06-11 14:06:23 +0200 | [diff] [blame] | 505 | if (conn_upgrade_mux_fe(conn, NULL, &h1c->ibuf, ist("h2"), PROTO_MODE_HTX) != -1) { |
Christopher Faulet | 0ef372a | 2019-04-08 10:57:20 +0200 | [diff] [blame] | 506 | /* connection successfully upgraded to H2, this |
| 507 | * mux was already released */ |
| 508 | return; |
| 509 | } |
| 510 | sess_log(conn->owner); /* Log if the upgrade failed */ |
| 511 | } |
| 512 | |
Olivier Houchard | 45c4437 | 2019-06-11 14:06:23 +0200 | [diff] [blame] | 513 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 514 | if (!LIST_ISEMPTY(&h1c->buf_wait.list)) { |
| 515 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
| 516 | LIST_DEL(&h1c->buf_wait.list); |
| 517 | LIST_INIT(&h1c->buf_wait.list); |
| 518 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
| 519 | } |
| 520 | |
| 521 | h1_release_buf(h1c, &h1c->ibuf); |
| 522 | h1_release_buf(h1c, &h1c->obuf); |
| 523 | |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 524 | if (h1c->task) { |
| 525 | h1c->task->context = NULL; |
| 526 | task_wakeup(h1c->task, TASK_WOKEN_OTHER); |
| 527 | h1c->task = NULL; |
| 528 | } |
| 529 | |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 530 | if (h1c->wait_event.tasklet) |
| 531 | tasklet_free(h1c->wait_event.tasklet); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 532 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 533 | h1s_destroy(h1c->h1s); |
Olivier Houchard | 0e07937 | 2019-04-15 17:51:16 +0200 | [diff] [blame] | 534 | if (conn && h1c->wait_event.events != 0) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 535 | conn->xprt->unsubscribe(conn, conn->xprt_ctx, h1c->wait_event.events, |
Olivier Houchard | 0e07937 | 2019-04-15 17:51:16 +0200 | [diff] [blame] | 536 | &h1c->wait_event); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 537 | pool_free(pool_head_h1c, h1c); |
| 538 | } |
| 539 | |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 540 | if (conn) { |
| 541 | conn->mux = NULL; |
| 542 | conn->ctx = NULL; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 543 | |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 544 | conn_stop_tracking(conn); |
| 545 | conn_full_close(conn); |
| 546 | if (conn->destroy_cb) |
| 547 | conn->destroy_cb(conn); |
| 548 | conn_free(conn); |
| 549 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | /******************************************************/ |
| 553 | /* functions below are for the H1 protocol processing */ |
| 554 | /******************************************************/ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 555 | /* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is |
| 556 | * greater or equal to 1.1 |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 557 | */ |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 558 | static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 559 | { |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 560 | const char *p = HTX_SL_REQ_VPTR(sl); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 561 | |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 562 | if ((HTX_SL_REQ_VLEN(sl) == 8) && |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 563 | (*(p + 5) > '1' || |
| 564 | (*(p + 5) == '1' && *(p + 7) >= '1'))) |
| 565 | h1m->flags |= H1_MF_VER_11; |
| 566 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 567 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 568 | /* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is |
| 569 | * greater or equal to 1.1 |
| 570 | */ |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 571 | static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 572 | { |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 573 | const char *p = HTX_SL_RES_VPTR(sl); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 574 | |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 575 | if ((HTX_SL_RES_VLEN(sl) == 8) && |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 576 | (*(p + 5) > '1' || |
| 577 | (*(p + 5) == '1' && *(p + 7) >= '1'))) |
| 578 | h1m->flags |= H1_MF_VER_11; |
| 579 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 580 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 581 | /* |
| 582 | * Check the validity of the request version. If the version is valid, it |
| 583 | * returns 1. Otherwise, it returns 0. |
| 584 | */ |
| 585 | static int h1_process_req_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl) |
| 586 | { |
| 587 | struct h1c *h1c = h1s->h1c; |
| 588 | |
| 589 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 590 | * exactly one digit "." one digit. This check may be disabled using |
| 591 | * option accept-invalid-http-request. |
| 592 | */ |
| 593 | if (!(h1c->px->options2 & PR_O2_REQBUG_OK)) { |
| 594 | if (sl.rq.v.len != 8) |
| 595 | return 0; |
| 596 | |
| 597 | if (*(sl.rq.v.ptr + 4) != '/' || |
| 598 | !isdigit((unsigned char)*(sl.rq.v.ptr + 5)) || |
| 599 | *(sl.rq.v.ptr + 6) != '.' || |
| 600 | !isdigit((unsigned char)*(sl.rq.v.ptr + 7))) |
| 601 | return 0; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 602 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 603 | else if (!sl.rq.v.len) { |
| 604 | /* try to convert HTTP/0.9 requests to HTTP/1.0 */ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 605 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 606 | /* RFC 1945 allows only GET for HTTP/0.9 requests */ |
| 607 | if (sl.rq.meth != HTTP_METH_GET) |
| 608 | return 0; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 609 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 610 | /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */ |
| 611 | if (!sl.rq.u.len) |
| 612 | return 0; |
| 613 | |
| 614 | /* Add HTTP version */ |
| 615 | sl.rq.v = ist("HTTP/1.0"); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 616 | return 1; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 617 | } |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 618 | |
| 619 | if ((sl.rq.v.len == 8) && |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 620 | ((*(sl.rq.v.ptr + 5) > '1') || |
| 621 | ((*(sl.rq.v.ptr + 5) == '1') && (*(sl.rq.v.ptr + 7) >= '1')))) |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 622 | h1m->flags |= H1_MF_VER_11; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 623 | return 1; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 624 | } |
| 625 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 626 | /* |
| 627 | * Check the validity of the response version. If the version is valid, it |
| 628 | * returns 1. Otherwise, it returns 0. |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 629 | */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 630 | static int h1_process_res_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 631 | { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 632 | struct h1c *h1c = h1s->h1c; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 633 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 634 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 635 | * exactly one digit "." one digit. This check may be disabled using |
| 636 | * option accept-invalid-http-request. |
| 637 | */ |
| 638 | if (!(h1c->px->options2 & PR_O2_RSPBUG_OK)) { |
| 639 | if (sl.st.v.len != 8) |
| 640 | return 0; |
| 641 | |
| 642 | if (*(sl.st.v.ptr + 4) != '/' || |
| 643 | !isdigit((unsigned char)*(sl.st.v.ptr + 5)) || |
| 644 | *(sl.st.v.ptr + 6) != '.' || |
| 645 | !isdigit((unsigned char)*(sl.st.v.ptr + 7))) |
| 646 | return 0; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 647 | } |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 648 | |
| 649 | if ((sl.st.v.len == 8) && |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 650 | ((*(sl.st.v.ptr + 5) > '1') || |
| 651 | ((*(sl.st.v.ptr + 5) == '1') && (*(sl.st.v.ptr + 7) >= '1')))) |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 652 | h1m->flags |= H1_MF_VER_11; |
| 653 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 654 | return 1; |
| 655 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 656 | |
| 657 | /* Deduce the connection mode of the client connection, depending on the |
| 658 | * configuration and the H1 message flags. This function is called twice, the |
| 659 | * first time when the request is parsed and the second time when the response |
| 660 | * is parsed. |
| 661 | */ |
| 662 | static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m) |
| 663 | { |
| 664 | struct proxy *fe = h1s->h1c->px; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 665 | |
| 666 | if (h1m->flags & H1_MF_RESP) { |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 667 | /* Output direction: second pass */ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 668 | if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) || |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 669 | h1s->status == 101) { |
| 670 | /* Either we've established an explicit tunnel, or we're |
| 671 | * switching the protocol. In both cases, we're very unlikely to |
| 672 | * understand the next protocols. We have to switch to tunnel |
| 673 | * mode, so that we transfer the request and responses then let |
| 674 | * this protocol pass unmodified. When we later implement |
| 675 | * specific parsers for such protocols, we'll want to check the |
| 676 | * Upgrade header which contains information about that protocol |
| 677 | * for responses with status 101 (eg: see RFC2817 about TLS). |
| 678 | */ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 679 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN; |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 680 | } |
| 681 | else if (h1s->flags & H1S_F_WANT_KAL) { |
| 682 | /* By default the client is in KAL mode. CLOSE mode mean |
| 683 | * it is imposed by the client itself. So only change |
| 684 | * KAL mode here. */ |
| 685 | if (!(h1m->flags & H1_MF_XFER_LEN) || (h1m->flags & H1_MF_CONN_CLO)) { |
| 686 | /* no length known or explicit close => close */ |
| 687 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
| 688 | } |
| 689 | else if (!(h1m->flags & H1_MF_CONN_KAL) && |
| 690 | (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) { |
| 691 | /* no explict keep-alive and option httpclose => close */ |
| 692 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
| 693 | } |
| 694 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 695 | } |
| 696 | else { |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 697 | /* Input direction: first pass */ |
| 698 | if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || h1m->flags & H1_MF_CONN_CLO) { |
| 699 | /* no explicit keep-alive in HTTP/1.0 or explicit close => close*/ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 700 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 701 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */ |
| 705 | if (h1s->flags & H1S_F_WANT_KAL && fe->state == PR_STSTOPPED) |
| 706 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
| 707 | } |
| 708 | |
| 709 | /* Deduce the connection mode of the client connection, depending on the |
| 710 | * configuration and the H1 message flags. This function is called twice, the |
| 711 | * first time when the request is parsed and the second time when the response |
| 712 | * is parsed. |
| 713 | */ |
| 714 | static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m) |
| 715 | { |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 716 | struct session *sess = h1s->sess; |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 717 | struct proxy *be = h1s->h1c->px; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 718 | int fe_flags = sess ? sess->fe->options : 0; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 719 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 720 | if (h1m->flags & H1_MF_RESP) { |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 721 | /* Input direction: second pass */ |
| 722 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 723 | if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) || |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 724 | h1s->status == 101) { |
| 725 | /* Either we've established an explicit tunnel, or we're |
| 726 | * switching the protocol. In both cases, we're very unlikely to |
| 727 | * understand the next protocols. We have to switch to tunnel |
| 728 | * mode, so that we transfer the request and responses then let |
| 729 | * this protocol pass unmodified. When we later implement |
| 730 | * specific parsers for such protocols, we'll want to check the |
| 731 | * Upgrade header which contains information about that protocol |
| 732 | * for responses with status 101 (eg: see RFC2817 about TLS). |
| 733 | */ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 734 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN; |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 735 | } |
| 736 | else if (h1s->flags & H1S_F_WANT_KAL) { |
| 737 | /* By default the server is in KAL mode. CLOSE mode mean |
| 738 | * it is imposed by haproxy itself. So only change KAL |
| 739 | * mode here. */ |
| 740 | if (!(h1m->flags & H1_MF_XFER_LEN) || h1m->flags & H1_MF_CONN_CLO || |
| 741 | !(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))){ |
| 742 | /* no length known or explicit close or no explicit keep-alive in HTTP/1.0 => close */ |
| 743 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
| 744 | } |
| 745 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 746 | } |
Christopher Faulet | 7003378 | 2018-12-05 13:50:11 +0100 | [diff] [blame] | 747 | else { |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 748 | /* Output direction: first pass */ |
| 749 | if (h1m->flags & H1_MF_CONN_CLO) { |
| 750 | /* explicit close => close */ |
Christopher Faulet | 7003378 | 2018-12-05 13:50:11 +0100 | [diff] [blame] | 751 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 752 | } |
| 753 | else if (!(h1m->flags & H1_MF_CONN_KAL) && |
| 754 | ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL || |
| 755 | (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL || |
| 756 | (fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_CLO || |
| 757 | (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)) { |
| 758 | /* no explicit keep-alive option httpclose/server-close => close */ |
| 759 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
| 760 | } |
Christopher Faulet | 7003378 | 2018-12-05 13:50:11 +0100 | [diff] [blame] | 761 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 762 | |
| 763 | /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */ |
| 764 | if (h1s->flags & H1S_F_WANT_KAL && be->state == PR_STSTOPPED) |
| 765 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 766 | } |
| 767 | |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 768 | static void h1_update_req_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 769 | { |
| 770 | struct proxy *px = h1s->h1c->px; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 771 | |
| 772 | /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage" |
| 773 | * token is found |
| 774 | */ |
| 775 | if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 776 | return; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 777 | |
| 778 | if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) { |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 779 | if (!(h1m->flags & H1_MF_VER_11)) |
| 780 | *conn_val = ist("keep-alive"); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 781 | } |
| 782 | else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */ |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 783 | if (h1m->flags & H1_MF_VER_11) |
| 784 | *conn_val = ist("close"); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 785 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 786 | } |
| 787 | |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 788 | static void h1_update_res_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 789 | { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 790 | /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage" |
| 791 | * token is found |
| 792 | */ |
| 793 | if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 794 | return; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 795 | |
| 796 | if (h1s->flags & H1S_F_WANT_KAL) { |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 797 | if (!(h1m->flags & H1_MF_VER_11) || |
| 798 | !((h1m->flags & h1s->req.flags) & H1_MF_VER_11)) |
| 799 | *conn_val = ist("keep-alive"); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 800 | } |
| 801 | else { /* H1S_F_WANT_CLO */ |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 802 | if (h1m->flags & H1_MF_VER_11) |
| 803 | *conn_val = ist("close"); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 804 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 805 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 806 | |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 807 | static void h1_process_input_conn_mode(struct h1s *h1s, struct h1m *h1m, struct htx *htx) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 808 | { |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 809 | if (!conn_is_back(h1s->h1c->conn)) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 810 | h1_set_cli_conn_mode(h1s, h1m); |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 811 | else |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 812 | h1_set_srv_conn_mode(h1s, h1m); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 813 | } |
| 814 | |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 815 | static void h1_process_output_conn_mode(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val) |
| 816 | { |
| 817 | if (!conn_is_back(h1s->h1c->conn)) |
| 818 | h1_set_cli_conn_mode(h1s, h1m); |
| 819 | else |
| 820 | h1_set_srv_conn_mode(h1s, h1m); |
| 821 | |
| 822 | if (!(h1m->flags & H1_MF_RESP)) |
| 823 | h1_update_req_conn_value(h1s, h1m, conn_val); |
| 824 | else |
| 825 | h1_update_res_conn_value(h1s, h1m, conn_val); |
| 826 | } |
Christopher Faulet | e44769b | 2018-11-29 23:01:45 +0100 | [diff] [blame] | 827 | |
Christopher Faulet | a99ff4d | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 828 | /* Try to adjust the case of the message header name using the global map |
| 829 | * <hdrs_map>. |
| 830 | */ |
| 831 | static void h1_adjust_case_outgoing_hdr(struct h1s *h1s, struct h1m *h1m, struct ist *name) |
| 832 | { |
| 833 | struct ebpt_node *node; |
| 834 | struct h1_hdr_entry *entry; |
| 835 | |
| 836 | /* No entry in the map, do nothing */ |
| 837 | if (eb_is_empty(&hdrs_map.map)) |
| 838 | return; |
| 839 | |
| 840 | /* No conversion fo the request headers */ |
| 841 | if (!(h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGSRV)) |
| 842 | return; |
| 843 | |
| 844 | /* No conversion fo the response headers */ |
| 845 | if ((h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGCLI)) |
| 846 | return; |
| 847 | |
| 848 | node = ebis_lookup_len(&hdrs_map.map, name->ptr, name->len); |
| 849 | if (!node) |
| 850 | return; |
| 851 | entry = container_of(node, struct h1_hdr_entry, node); |
| 852 | name->ptr = entry->name.ptr; |
| 853 | name->len = entry->name.len; |
| 854 | } |
| 855 | |
Christopher Faulet | e44769b | 2018-11-29 23:01:45 +0100 | [diff] [blame] | 856 | /* Append the description of what is present in error snapshot <es> into <out>. |
| 857 | * The description must be small enough to always fit in a buffer. The output |
| 858 | * buffer may be the trash so the trash must not be used inside this function. |
| 859 | */ |
| 860 | static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es) |
| 861 | { |
| 862 | chunk_appendf(out, |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 863 | " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n" |
| 864 | " H1 msg state %s(%d), H1 msg flags 0x%08x\n" |
| 865 | " H1 chunk len %lld bytes, H1 body len %lld bytes :\n", |
| 866 | es->ctx.h1.c_flags, es->ctx.h1.s_flags, |
| 867 | h1m_state_str(es->ctx.h1.state), es->ctx.h1.state, |
| 868 | es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen); |
Christopher Faulet | e44769b | 2018-11-29 23:01:45 +0100 | [diff] [blame] | 869 | } |
| 870 | /* |
| 871 | * Capture a bad request or response and archive it in the proxy's structure. |
| 872 | * By default it tries to report the error position as h1m->err_pos. However if |
| 873 | * this one is not set, it will then report h1m->next, which is the last known |
| 874 | * parsing point. The function is able to deal with wrapping buffers. It always |
| 875 | * displays buffers as a contiguous area starting at buf->p. The direction is |
| 876 | * determined thanks to the h1m's flags. |
| 877 | */ |
| 878 | static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s, |
| 879 | struct h1m *h1m, struct buffer *buf) |
| 880 | { |
| 881 | struct session *sess = h1c->conn->owner; |
| 882 | struct proxy *proxy = h1c->px; |
| 883 | struct proxy *other_end = sess->fe; |
| 884 | union error_snapshot_ctx ctx; |
| 885 | |
Willy Tarreau | 598d7fc | 2018-12-18 18:10:38 +0100 | [diff] [blame] | 886 | if (h1s->cs->data && !(h1m->flags & H1_MF_RESP)) |
Christopher Faulet | e44769b | 2018-11-29 23:01:45 +0100 | [diff] [blame] | 887 | other_end = si_strm(h1s->cs->data)->be; |
| 888 | |
| 889 | /* http-specific part now */ |
| 890 | ctx.h1.state = h1m->state; |
| 891 | ctx.h1.c_flags = h1c->flags; |
| 892 | ctx.h1.s_flags = h1s->flags; |
| 893 | ctx.h1.m_flags = h1m->flags; |
| 894 | ctx.h1.m_clen = h1m->curr_len; |
| 895 | ctx.h1.m_blen = h1m->body_len; |
| 896 | |
| 897 | proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end, |
| 898 | h1c->conn->target, sess, buf, 0, 0, |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 899 | (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next, |
| 900 | &ctx, h1_show_error_snapshot); |
Christopher Faulet | e44769b | 2018-11-29 23:01:45 +0100 | [diff] [blame] | 901 | } |
| 902 | |
Christopher Faulet | adb2220 | 2018-12-12 10:32:09 +0100 | [diff] [blame] | 903 | /* Emit the chunksize followed by a CRLF in front of data of the buffer |
| 904 | * <buf>. It goes backwards and starts with the byte before the buffer's |
| 905 | * head. The caller is responsible for ensuring there is enough room left before |
| 906 | * the buffer's head for the string. |
| 907 | */ |
| 908 | static void h1_emit_chunk_size(struct buffer *buf, size_t chksz) |
| 909 | { |
| 910 | char *beg, *end; |
| 911 | |
| 912 | beg = end = b_head(buf); |
| 913 | *--beg = '\n'; |
| 914 | *--beg = '\r'; |
| 915 | do { |
| 916 | *--beg = hextab[chksz & 0xF]; |
| 917 | } while (chksz >>= 4); |
| 918 | buf->head -= (end - beg); |
| 919 | b_add(buf, end - beg); |
| 920 | } |
| 921 | |
| 922 | /* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for |
| 923 | * ensuring there is enough room left in the buffer for the string. */ |
| 924 | static void h1_emit_chunk_crlf(struct buffer *buf) |
| 925 | { |
| 926 | *(b_peek(buf, b_data(buf))) = '\r'; |
| 927 | *(b_peek(buf, b_data(buf) + 1)) = '\n'; |
| 928 | b_add(buf, 2); |
| 929 | } |
| 930 | |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 931 | /* |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 932 | * Switch the request to tunnel mode. This function must only be called for |
Christopher Faulet | ac4778c | 2019-11-15 11:14:23 +0100 | [diff] [blame] | 933 | * CONNECT requests. On the client side, if the response is not finished, the |
| 934 | * mux is mark as busy on input. |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 935 | */ |
| 936 | static void h1_set_req_tunnel_mode(struct h1s *h1s) |
| 937 | { |
| 938 | h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK); |
| 939 | h1s->req.state = H1_MSG_TUNNEL; |
Christopher Faulet | ac4778c | 2019-11-15 11:14:23 +0100 | [diff] [blame] | 940 | if (!conn_is_back(h1s->h1c->conn) && h1s->res.state < H1_MSG_DONE) |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 941 | h1s->h1c->flags |= H1C_F_IN_BUSY; |
Christopher Faulet | ac4778c | 2019-11-15 11:14:23 +0100 | [diff] [blame] | 942 | else if (h1s->h1c->flags & H1C_F_IN_BUSY) { |
| 943 | h1s->h1c->flags &= ~H1C_F_IN_BUSY; |
| 944 | tasklet_wakeup(h1s->h1c->wait_event.tasklet); |
| 945 | } |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | /* |
| 949 | * Switch the response to tunnel mode. This function must only be called on |
Christopher Faulet | ac4778c | 2019-11-15 11:14:23 +0100 | [diff] [blame] | 950 | * successfull replies to CONNECT requests or on protocol switching. In this |
| 951 | * last case, this function takes care to switch the request to tunnel mode if |
| 952 | * possible. On the server side, if the request is not finished, the mux is mark |
| 953 | * as busy on input. |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 954 | */ |
| 955 | static void h1_set_res_tunnel_mode(struct h1s *h1s) |
| 956 | { |
Christopher Faulet | ac4778c | 2019-11-15 11:14:23 +0100 | [diff] [blame] | 957 | /* On protocol switching, switch the request to tunnel mode if it is in |
| 958 | * DONE state. Otherwise we will wait the end of the request to switch |
| 959 | * it in tunnel mode. |
| 960 | */ |
| 961 | if (h1s->status == 101 && h1s->req.state == H1_MSG_DONE) { |
| 962 | h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK); |
| 963 | h1s->req.state = H1_MSG_TUNNEL; |
| 964 | } |
| 965 | |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 966 | h1s->res.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK); |
| 967 | h1s->res.state = H1_MSG_TUNNEL; |
| 968 | if (conn_is_back(h1s->h1c->conn) && h1s->req.state < H1_MSG_DONE) |
| 969 | h1s->h1c->flags |= H1C_F_IN_BUSY; |
Christopher Faulet | ac4778c | 2019-11-15 11:14:23 +0100 | [diff] [blame] | 970 | else if (h1s->h1c->flags & H1C_F_IN_BUSY) { |
| 971 | h1s->h1c->flags &= ~H1C_F_IN_BUSY; |
| 972 | tasklet_wakeup(h1s->h1c->wait_event.tasklet); |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 973 | } |
| 974 | } |
| 975 | |
Christopher Faulet | 82f0160 | 2019-05-24 16:50:16 +0200 | [diff] [blame] | 976 | /* Estimate the size of the HTX headers after the parsing, including the EOH. */ |
| 977 | static size_t h1_eval_htx_hdrs_size(struct http_hdr *hdrs) |
| 978 | { |
| 979 | size_t sz = 0; |
| 980 | int i; |
| 981 | |
| 982 | for (i = 0; hdrs[i].n.len; i++) |
| 983 | sz += sizeof(struct htx_blk) + hdrs[i].n.len + hdrs[i].v.len; |
| 984 | sz += sizeof(struct htx_blk) + 1; |
| 985 | return sz; |
| 986 | } |
| 987 | |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 988 | /* Estimate the size of the HTX request after the parsing. */ |
| 989 | static size_t h1_eval_htx_req_size(struct h1m *h1m, union h1_sl *h1sl, struct http_hdr *hdrs) |
| 990 | { |
| 991 | size_t sz; |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 992 | |
| 993 | /* size of the HTX start-line */ |
Christopher Faulet | 1d76cef | 2019-09-03 16:16:50 +0200 | [diff] [blame] | 994 | sz = sizeof(struct htx_blk) + sizeof(struct htx_sl) + h1sl->rq.m.len + h1sl->rq.u.len + h1sl->rq.v.len; |
Christopher Faulet | 82f0160 | 2019-05-24 16:50:16 +0200 | [diff] [blame] | 995 | sz += h1_eval_htx_hdrs_size(hdrs); |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 996 | return sz; |
| 997 | } |
| 998 | |
| 999 | /* Estimate the size of the HTX response after the parsing. */ |
| 1000 | static size_t h1_eval_htx_res_size(struct h1m *h1m, union h1_sl *h1sl, struct http_hdr *hdrs) |
| 1001 | { |
| 1002 | size_t sz; |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1003 | |
| 1004 | /* size of the HTX start-line */ |
Christopher Faulet | 1d76cef | 2019-09-03 16:16:50 +0200 | [diff] [blame] | 1005 | sz = sizeof(struct htx_blk) + sizeof(struct htx_sl) + h1sl->st.v.len + h1sl->st.c.len + h1sl->st.r.len; |
Christopher Faulet | 82f0160 | 2019-05-24 16:50:16 +0200 | [diff] [blame] | 1006 | sz += h1_eval_htx_hdrs_size(hdrs); |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1007 | return sz; |
| 1008 | } |
| 1009 | |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 1010 | /* |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1011 | * Add the EOM in the HTX message and switch the message to the DONE state. It |
| 1012 | * returns the number of bytes parsed if > 0, or 0 if iet couldn't proceed. This |
| 1013 | * functions is responsible to update the parser state <h1m>. It also add the |
| 1014 | * flag CS_FL_EOI on the CS. |
| 1015 | */ |
| 1016 | static size_t h1_process_eom(struct h1s *h1s, struct h1m *h1m, struct htx *htx, size_t max) |
| 1017 | { |
Willy Tarreau | 6203815 | 2019-08-23 09:29:29 +0200 | [diff] [blame] | 1018 | if (max < sizeof(struct htx_blk) + 1 || !htx_add_endof(htx, HTX_BLK_EOM)) { |
| 1019 | h1s->flags |= H1S_F_APPEND_EOM; |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1020 | return 0; |
Willy Tarreau | 6203815 | 2019-08-23 09:29:29 +0200 | [diff] [blame] | 1021 | } |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1022 | |
Willy Tarreau | 6203815 | 2019-08-23 09:29:29 +0200 | [diff] [blame] | 1023 | h1s->flags &= ~H1S_F_APPEND_EOM; |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1024 | h1m->state = H1_MSG_DONE; |
| 1025 | h1s->cs->flags |= CS_FL_EOI; |
| 1026 | return (sizeof(struct htx_blk) + 1); |
| 1027 | } |
| 1028 | |
| 1029 | /* |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1030 | * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1031 | * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR |
| 1032 | * flag and filling h1s->err_pos and h1s->err_state fields. This functions is |
Joseph Herlant | 30bc509 | 2018-11-25 10:52:20 -0800 | [diff] [blame] | 1033 | * responsible to update the parser state <h1m>. |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1034 | */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1035 | static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx, |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1036 | struct buffer *buf, size_t *ofs, size_t max) |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1037 | { |
Christopher Faulet | a39d8ad | 2019-05-15 15:54:39 +0200 | [diff] [blame] | 1038 | struct htx_sl *sl; |
Christopher Faulet | e4ab11b | 2019-06-11 15:05:37 +0200 | [diff] [blame] | 1039 | struct http_hdr hdrs[global.tune.max_http_hdr]; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1040 | union h1_sl h1sl; |
| 1041 | unsigned int flags = HTX_SL_F_NONE; |
Christopher Faulet | a39d8ad | 2019-05-15 15:54:39 +0200 | [diff] [blame] | 1042 | size_t used; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1043 | int ret = 0; |
| 1044 | |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1045 | if (!max || !b_data(buf)) |
Christopher Faulet | d44ad5b | 2018-11-19 21:52:12 +0100 | [diff] [blame] | 1046 | goto end; |
| 1047 | |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1048 | /* Realing input buffer if necessary */ |
| 1049 | if (b_head(buf) + b_data(buf) > b_wrap(buf)) |
| 1050 | b_slow_realign(buf, trash.area, 0); |
| 1051 | |
Christopher Faulet | 842f41f | 2019-09-25 09:10:46 +0200 | [diff] [blame] | 1052 | if (!(h1s->flags & H1S_F_NOT_FIRST) && !(h1m->flags & H1_MF_RESP)) { |
Christopher Faulet | 0ef372a | 2019-04-08 10:57:20 +0200 | [diff] [blame] | 1053 | /* Try to match H2 preface before parsing the request headers. */ |
| 1054 | ret = b_isteq(buf, 0, b_data(buf), ist(H2_CONN_PREFACE)); |
| 1055 | if (ret > 0) |
| 1056 | goto h2c_upgrade; |
| 1057 | } |
| 1058 | |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1059 | ret = h1_headers_to_hdr_list(b_peek(buf, *ofs), b_tail(buf), |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1060 | hdrs, sizeof(hdrs)/sizeof(hdrs[0]), h1m, &h1sl); |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1061 | if (ret <= 0) { |
Willy Tarreau | 9a408ab | 2019-08-23 08:11:36 +0200 | [diff] [blame] | 1062 | /* Incomplete or invalid message. If the input buffer only |
| 1063 | * contains headers and is full, which is detected by it being |
| 1064 | * full and the offset to be zero, it's an error because |
| 1065 | * headers are too large to be handled by the parser. */ |
| 1066 | if (ret < 0 || (!ret && !*ofs && !buf_room_for_htx_data(buf))) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1067 | goto error; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1068 | goto end; |
| 1069 | } |
| 1070 | |
Christopher Faulet | b4bad50 | 2019-10-11 14:22:00 +0200 | [diff] [blame] | 1071 | if (h1m->err_pos >= 0) { |
| 1072 | /* Maybe we found an error during the parsing while we were |
| 1073 | * configured not to block on that, so we have to capture it |
| 1074 | * now. |
| 1075 | */ |
| 1076 | h1_capture_bad_message(h1s->h1c, h1s, h1m, buf); |
| 1077 | } |
| 1078 | |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1079 | /* messages headers fully parsed, do some checks to prepare the body |
| 1080 | * parsing. |
| 1081 | */ |
| 1082 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1083 | /* Save the request's method or the response's status, check if the body |
| 1084 | * length is known and check the VSN validity */ |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1085 | if (!(h1m->flags & H1_MF_RESP)) { |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1086 | h1s->meth = h1sl.rq.meth; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1087 | |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 1088 | /* By default, request have always a known length */ |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1089 | h1m->flags |= H1_MF_XFER_LEN; |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 1090 | |
| 1091 | if (h1s->meth == HTTP_METH_CONNECT) { |
| 1092 | /* Switch CONNECT requests to tunnel mode */ |
| 1093 | h1_set_req_tunnel_mode(h1s); |
| 1094 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1095 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1096 | if (!h1_process_req_vsn(h1s, h1m, h1sl)) { |
| 1097 | h1m->err_pos = h1sl.rq.v.ptr - b_head(buf); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1098 | h1m->err_state = h1m->state; |
| 1099 | goto vsn_error; |
| 1100 | } |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1101 | } |
| 1102 | else { |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1103 | h1s->status = h1sl.st.status; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1104 | |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 1105 | if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) || |
| 1106 | h1s->status == 101) { |
| 1107 | /* Switch successfull replies to CONNECT requests and |
| 1108 | * protocol switching to tunnel mode. */ |
| 1109 | h1_set_res_tunnel_mode(h1s); |
| 1110 | } |
| 1111 | else if ((h1s->meth == HTTP_METH_HEAD) || |
| 1112 | (h1s->status >= 100 && h1s->status < 200) || |
| 1113 | (h1s->status == 204) || (h1s->status == 304)) { |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 1114 | /* Responses known to have no body. */ |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1115 | h1m->flags &= ~(H1_MF_CLEN|H1_MF_CHNK); |
| 1116 | h1m->flags |= H1_MF_XFER_LEN; |
| 1117 | h1m->curr_len = h1m->body_len = 0; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1118 | } |
| 1119 | else if (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) { |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 1120 | /* Responses with a known body length. */ |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1121 | h1m->flags |= H1_MF_XFER_LEN; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1122 | } |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 1123 | else { |
| 1124 | /* Responses with an unknown body length */ |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1125 | h1m->state = H1_MSG_TUNNEL; |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 1126 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1127 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1128 | if (!h1_process_res_vsn(h1s, h1m, h1sl)) { |
| 1129 | h1m->err_pos = h1sl.st.v.ptr - b_head(buf); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1130 | h1m->err_state = h1m->state; |
| 1131 | goto vsn_error; |
| 1132 | } |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1133 | } |
| 1134 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1135 | /* Set HTX start-line flags */ |
| 1136 | if (h1m->flags & H1_MF_VER_11) |
| 1137 | flags |= HTX_SL_F_VER_11; |
| 1138 | if (h1m->flags & H1_MF_XFER_ENC) |
| 1139 | flags |= HTX_SL_F_XFER_ENC; |
| 1140 | if (h1m->flags & H1_MF_XFER_LEN) { |
| 1141 | flags |= HTX_SL_F_XFER_LEN; |
| 1142 | if (h1m->flags & H1_MF_CHNK) |
| 1143 | flags |= HTX_SL_F_CHNK; |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 1144 | else if (h1m->flags & H1_MF_CLEN) { |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1145 | flags |= HTX_SL_F_CLEN; |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 1146 | if (h1m->body_len == 0) |
| 1147 | flags |= HTX_SL_F_BODYLESS; |
| 1148 | } |
| 1149 | else |
Christopher Faulet | b2db4fa | 2018-11-27 16:51:09 +0100 | [diff] [blame] | 1150 | flags |= HTX_SL_F_BODYLESS; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1151 | } |
| 1152 | |
Christopher Faulet | a39d8ad | 2019-05-15 15:54:39 +0200 | [diff] [blame] | 1153 | used = htx_used_space(htx); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1154 | if (!(h1m->flags & H1_MF_RESP)) { |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 1155 | if (h1_eval_htx_req_size(h1m, &h1sl, hdrs) > max) { |
| 1156 | if (htx_is_empty(htx)) |
| 1157 | goto error; |
| 1158 | h1m_init_req(h1m); |
| 1159 | h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR); |
| 1160 | ret = 0; |
| 1161 | goto end; |
| 1162 | } |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1163 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1164 | sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v); |
| 1165 | if (!sl || !htx_add_all_headers(htx, hdrs)) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1166 | goto error; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1167 | sl->info.req.meth = h1s->meth; |
Christopher Faulet | 42993a8 | 2019-06-14 10:31:25 +0200 | [diff] [blame] | 1168 | |
| 1169 | /* Check if the uri contains an explicit scheme and if it is |
| 1170 | * "http" or "https". */ |
| 1171 | if (h1sl.rq.u.len && h1sl.rq.u.ptr[0] != '/') { |
| 1172 | sl->flags |= HTX_SL_F_HAS_SCHM; |
| 1173 | if (h1sl.rq.u.len > 4 && (h1sl.rq.u.ptr[0] | 0x20) == 'h') |
| 1174 | sl->flags |= ((h1sl.rq.u.ptr[4] == ':') ? HTX_SL_F_SCHM_HTTP : HTX_SL_F_SCHM_HTTPS); |
| 1175 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1176 | } |
| 1177 | else { |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 1178 | if (h1_eval_htx_res_size(h1m, &h1sl, hdrs) > max) { |
| 1179 | if (htx_is_empty(htx)) |
| 1180 | goto error; |
| 1181 | h1m_init_res(h1m); |
| 1182 | h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR); |
| 1183 | ret = 0; |
| 1184 | goto end; |
| 1185 | } |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1186 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1187 | flags |= HTX_SL_F_IS_RESP; |
| 1188 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, h1sl.st.v, h1sl.st.c, h1sl.st.r); |
| 1189 | if (!sl || !htx_add_all_headers(htx, hdrs)) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1190 | goto error; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1191 | sl->info.res.status = h1s->status; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1192 | } |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1193 | |
Christopher Faulet | a39d8ad | 2019-05-15 15:54:39 +0200 | [diff] [blame] | 1194 | /* Set bytes used in the HTX mesage for the headers now */ |
| 1195 | sl->hdrs_bytes = htx_used_space(htx) - used; |
| 1196 | |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1197 | h1_process_input_conn_mode(h1s, h1m, htx); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1198 | |
| 1199 | /* If body length cannot be determined, set htx->extra to |
| 1200 | * ULLONG_MAX. This value is impossible in other cases. |
| 1201 | */ |
| 1202 | htx->extra = ((h1m->flags & H1_MF_XFER_LEN) ? h1m->curr_len : ULLONG_MAX); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1203 | *ofs += ret; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1204 | end: |
| 1205 | return ret; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1206 | |
| 1207 | error: |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1208 | h1m->err_state = h1m->state; |
| 1209 | h1m->err_pos = h1m->next; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1210 | vsn_error: |
| 1211 | h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR); |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 1212 | h1s->cs->flags |= CS_FL_EOI; |
| 1213 | htx->flags |= HTX_FL_PARSING_ERROR; |
Christopher Faulet | e44769b | 2018-11-29 23:01:45 +0100 | [diff] [blame] | 1214 | h1_capture_bad_message(h1s->h1c, h1s, h1m, buf); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1215 | ret = 0; |
| 1216 | goto end; |
Christopher Faulet | 0ef372a | 2019-04-08 10:57:20 +0200 | [diff] [blame] | 1217 | |
| 1218 | h2c_upgrade: |
| 1219 | h1s->h1c->flags |= H1C_F_UPG_H2C; |
Christopher Faulet | 8e9e3ef | 2019-05-17 09:14:10 +0200 | [diff] [blame] | 1220 | h1s->cs->flags |= CS_FL_EOI; |
Christopher Faulet | 0ef372a | 2019-04-08 10:57:20 +0200 | [diff] [blame] | 1221 | htx->flags |= HTX_FL_UPGRADE; |
| 1222 | ret = 0; |
| 1223 | goto end; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1224 | } |
| 1225 | |
| 1226 | /* |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1227 | * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it |
| 1228 | * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1229 | * and filling h1s->err_pos and h1s->err_state fields. This functions is |
Joseph Herlant | 30bc509 | 2018-11-25 10:52:20 -0800 | [diff] [blame] | 1230 | * responsible to update the parser state <h1m>. |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1231 | */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1232 | static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx *htx, |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 1233 | struct buffer *buf, size_t *ofs, size_t max, |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1234 | struct buffer *htxbuf) |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1235 | { |
| 1236 | size_t total = 0; |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1237 | int32_t ret = 0; |
Christopher Faulet | afe5784 | 2019-01-21 11:55:02 +0100 | [diff] [blame] | 1238 | |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1239 | if (h1m->flags & H1_MF_XFER_LEN) { |
| 1240 | if (h1m->flags & H1_MF_CLEN) { |
| 1241 | /* content-length: read only h2m->body_len */ |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1242 | ret = htx_get_max_blksz(htx, max); |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1243 | if ((uint64_t)ret > h1m->curr_len) |
| 1244 | ret = h1m->curr_len; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1245 | if (ret > b_contig_data(buf, *ofs)) |
| 1246 | ret = b_contig_data(buf, *ofs); |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1247 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1248 | if (ret) { |
Willy Tarreau | 78f548f | 2018-12-05 10:02:39 +0100 | [diff] [blame] | 1249 | /* very often with large files we'll face the following |
| 1250 | * situation : |
| 1251 | * - htx is empty and points to <htxbuf> |
| 1252 | * - ret == buf->data |
| 1253 | * - buf->head == sizeof(struct htx) |
| 1254 | * => we can swap the buffers and place an htx header into |
| 1255 | * the target buffer instead |
| 1256 | */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 1257 | int32_t try = ret; |
| 1258 | |
Willy Tarreau | 78f548f | 2018-12-05 10:02:39 +0100 | [diff] [blame] | 1259 | if (unlikely(htx_is_empty(htx) && ret == b_data(buf) && |
| 1260 | !*ofs && b_head_ofs(buf) == sizeof(struct htx))) { |
| 1261 | void *raw_area = buf->area; |
| 1262 | void *htx_area = htxbuf->area; |
| 1263 | struct htx_blk *blk; |
| 1264 | |
| 1265 | buf->area = htx_area; |
| 1266 | htxbuf->area = raw_area; |
| 1267 | htx = (struct htx *)htxbuf->area; |
| 1268 | htx->size = htxbuf->size - sizeof(*htx); |
| 1269 | htx_reset(htx); |
| 1270 | b_set_data(htxbuf, b_size(htxbuf)); |
| 1271 | |
| 1272 | blk = htx_add_blk(htx, HTX_BLK_DATA, ret); |
| 1273 | blk->info += ret; |
| 1274 | /* nothing else to do, the old buffer now contains an |
| 1275 | * empty pre-initialized HTX header |
| 1276 | */ |
| 1277 | } |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 1278 | else { |
| 1279 | ret = htx_add_data(htx, ist2(b_peek(buf, *ofs), try)); |
| 1280 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1281 | h1m->curr_len -= ret; |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1282 | max -= sizeof(struct htx_blk) + ret; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1283 | *ofs += ret; |
| 1284 | total += ret; |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 1285 | if (ret < try) |
| 1286 | goto end; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1287 | } |
| 1288 | |
| 1289 | if (!h1m->curr_len) { |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1290 | if (!h1_process_eom(h1s, h1m, htx, max)) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1291 | goto end; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1292 | } |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1293 | } |
| 1294 | else if (h1m->flags & H1_MF_CHNK) { |
| 1295 | new_chunk: |
| 1296 | /* te:chunked : parse chunks */ |
| 1297 | if (h1m->state == H1_MSG_CHUNK_CRLF) { |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1298 | ret = h1_skip_chunk_crlf(buf, *ofs, b_data(buf)); |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1299 | if (ret <= 0) |
| 1300 | goto end; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1301 | h1m->state = H1_MSG_CHUNK_SIZE; |
| 1302 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1303 | *ofs += ret; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1304 | total += ret; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1305 | } |
| 1306 | |
| 1307 | if (h1m->state == H1_MSG_CHUNK_SIZE) { |
| 1308 | unsigned int chksz; |
| 1309 | |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1310 | ret = h1_parse_chunk_size(buf, *ofs, b_data(buf), &chksz); |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1311 | if (ret <= 0) |
| 1312 | goto end; |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 1313 | h1m->state = ((!chksz) ? H1_MSG_TRAILERS : H1_MSG_DATA); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1314 | |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1315 | h1m->curr_len = chksz; |
| 1316 | h1m->body_len += chksz; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1317 | *ofs += ret; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1318 | total += ret; |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1319 | |
| 1320 | if (!h1m->curr_len) |
| 1321 | goto end; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1322 | } |
| 1323 | |
| 1324 | if (h1m->state == H1_MSG_DATA) { |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1325 | ret = htx_get_max_blksz(htx, max); |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1326 | if ((uint64_t)ret > h1m->curr_len) |
| 1327 | ret = h1m->curr_len; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1328 | if (ret > b_contig_data(buf, *ofs)) |
| 1329 | ret = b_contig_data(buf, *ofs); |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1330 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1331 | if (ret) { |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 1332 | int32_t try = ret; |
| 1333 | |
| 1334 | ret = htx_add_data(htx, ist2(b_peek(buf, *ofs), try)); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1335 | h1m->curr_len -= ret; |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1336 | max -= sizeof(struct htx_blk) + ret; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1337 | *ofs += ret; |
| 1338 | total += ret; |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 1339 | if (ret < try) |
| 1340 | goto end; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1341 | } |
| 1342 | if (!h1m->curr_len) { |
| 1343 | h1m->state = H1_MSG_CHUNK_CRLF; |
| 1344 | goto new_chunk; |
| 1345 | } |
| 1346 | goto end; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1347 | } |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1348 | } |
| 1349 | else { |
| 1350 | /* XFER_LEN is set but not CLEN nor CHNK, it means there |
| 1351 | * is no body. Switch the message in DONE state |
| 1352 | */ |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1353 | if (!h1_process_eom(h1s, h1m, htx, max)) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1354 | goto end; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1355 | } |
| 1356 | } |
| 1357 | else { |
| 1358 | /* no content length, read till SHUTW */ |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1359 | ret = htx_get_max_blksz(htx, max); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1360 | if (ret > b_contig_data(buf, *ofs)) |
| 1361 | ret = b_contig_data(buf, *ofs); |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1362 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1363 | if (ret) { |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 1364 | int32_t try = ret; |
| 1365 | |
| 1366 | ret = htx_add_data(htx, ist2(b_peek(buf, *ofs), try)); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1367 | |
Olivier Houchard | cf42d5a | 2018-12-04 17:41:58 +0100 | [diff] [blame] | 1368 | *ofs += ret; |
| 1369 | total = ret; |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 1370 | if (ret < try) |
| 1371 | goto end; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1372 | } |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1373 | } |
| 1374 | |
| 1375 | end: |
| 1376 | if (ret < 0) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1377 | h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR); |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 1378 | h1s->cs->flags |= CS_FL_EOI; |
| 1379 | htx->flags |= HTX_FL_PARSING_ERROR; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1380 | h1m->err_state = h1m->state; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1381 | h1m->err_pos = *ofs + max + ret; |
Christopher Faulet | e44769b | 2018-11-29 23:01:45 +0100 | [diff] [blame] | 1382 | h1_capture_bad_message(h1s->h1c, h1s, h1m, buf); |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1383 | return 0; |
| 1384 | } |
Willy Tarreau | 7e9dd73 | 2020-01-17 16:19:34 +0100 | [diff] [blame] | 1385 | |
Christopher Faulet | afadc9a | 2020-07-03 14:51:15 +0200 | [diff] [blame] | 1386 | if (h1s->cs && !(h1m->flags & H1_MF_CHNK) && |
| 1387 | ((h1m->state == H1_MSG_DATA && h1m->curr_len) || (h1m->state == H1_MSG_TUNNEL))) |
Willy Tarreau | 7e9dd73 | 2020-01-17 16:19:34 +0100 | [diff] [blame] | 1388 | h1s->cs->flags |= CS_FL_MAY_SPLICE; |
| 1389 | else if (h1s->cs) |
| 1390 | h1s->cs->flags &= ~CS_FL_MAY_SPLICE; |
| 1391 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1392 | /* update htx->extra, only when the body length is known */ |
| 1393 | if (h1m->flags & H1_MF_XFER_LEN) |
| 1394 | htx->extra = h1m->curr_len; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1395 | return total; |
| 1396 | } |
| 1397 | |
| 1398 | /* |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1399 | * Parse HTTP/1 trailers. It returns the number of bytes parsed if > 0, or 0 if |
| 1400 | * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR |
| 1401 | * flag and filling h1s->err_pos and h1s->err_state fields. This functions is |
| 1402 | * responsible to update the parser state <h1m>. |
| 1403 | */ |
| 1404 | static size_t h1_process_trailers(struct h1s *h1s, struct h1m *h1m, struct htx *htx, |
| 1405 | struct buffer *buf, size_t *ofs, size_t max) |
| 1406 | { |
Christopher Faulet | e4ab11b | 2019-06-11 15:05:37 +0200 | [diff] [blame] | 1407 | struct http_hdr hdrs[global.tune.max_http_hdr]; |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1408 | struct h1m tlr_h1m; |
| 1409 | int ret = 0; |
| 1410 | |
| 1411 | if (!max || !b_data(buf)) |
| 1412 | goto end; |
| 1413 | |
| 1414 | /* Realing input buffer if necessary */ |
| 1415 | if (b_peek(buf, *ofs) > b_tail(buf)) |
| 1416 | b_slow_realign(buf, trash.area, 0); |
| 1417 | |
| 1418 | tlr_h1m.flags = (H1_MF_NO_PHDR|H1_MF_HDRS_ONLY); |
| 1419 | ret = h1_headers_to_hdr_list(b_peek(buf, *ofs), b_tail(buf), |
| 1420 | hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &tlr_h1m, NULL); |
| 1421 | if (ret <= 0) { |
Willy Tarreau | 9a408ab | 2019-08-23 08:11:36 +0200 | [diff] [blame] | 1422 | /* Incomplete or invalid trailers. If the input buffer only |
| 1423 | * contains trailers and is full, which is detected by it being |
| 1424 | * full and the offset to be zero, it's an error because |
| 1425 | * trailers are too large to be handled by the parser. */ |
| 1426 | if (ret < 0 || (!ret && !*ofs && !buf_room_for_htx_data(buf))) |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1427 | goto error; |
| 1428 | goto end; |
| 1429 | } |
| 1430 | |
| 1431 | /* messages trailers fully parsed. */ |
| 1432 | if (h1_eval_htx_hdrs_size(hdrs) > max) { |
| 1433 | if (htx_is_empty(htx)) |
| 1434 | goto error; |
| 1435 | ret = 0; |
| 1436 | goto end; |
| 1437 | } |
| 1438 | |
| 1439 | if (!htx_add_all_trailers(htx, hdrs)) |
| 1440 | goto error; |
| 1441 | |
| 1442 | *ofs += ret; |
| 1443 | h1s->flags |= H1S_F_HAVE_I_TLR; |
| 1444 | end: |
| 1445 | return ret; |
| 1446 | |
| 1447 | error: |
| 1448 | h1m->err_state = h1m->state; |
| 1449 | h1m->err_pos = h1m->next; |
| 1450 | h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR); |
| 1451 | h1s->cs->flags |= CS_FL_EOI; |
| 1452 | htx->flags |= HTX_FL_PARSING_ERROR; |
| 1453 | h1_capture_bad_message(h1s->h1c, h1s, h1m, buf); |
| 1454 | ret = 0; |
| 1455 | goto end; |
| 1456 | } |
| 1457 | |
| 1458 | /* |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1459 | * Process incoming data. It parses data and transfer them from h1c->ibuf into |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1460 | * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if |
| 1461 | * it couldn't proceed. |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1462 | */ |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1463 | static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, size_t count) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1464 | { |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1465 | struct h1s *h1s = h1c->h1s; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1466 | struct h1m *h1m; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1467 | struct htx *htx; |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1468 | size_t ret, data; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1469 | size_t total = 0; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1470 | int errflag; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1471 | |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1472 | htx = htx_from_buf(buf); |
Willy Tarreau | 3a6190f | 2018-12-16 08:29:56 +0100 | [diff] [blame] | 1473 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1474 | if (!conn_is_back(h1c->conn)) { |
| 1475 | h1m = &h1s->req; |
| 1476 | errflag = H1S_F_REQ_ERROR; |
| 1477 | } |
| 1478 | else { |
| 1479 | h1m = &h1s->res; |
| 1480 | errflag = H1S_F_RES_ERROR; |
| 1481 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1482 | |
Christopher Faulet | 7402776 | 2019-02-26 14:45:05 +0100 | [diff] [blame] | 1483 | data = htx->data; |
Christopher Faulet | cd9f6a7 | 2019-07-04 21:22:34 +0200 | [diff] [blame] | 1484 | if (h1s->flags & errflag) |
| 1485 | goto end; |
| 1486 | |
Christopher Faulet | d44ad5b | 2018-11-19 21:52:12 +0100 | [diff] [blame] | 1487 | do { |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1488 | size_t used = htx_used_space(htx); |
| 1489 | |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1490 | if (h1m->state <= H1_MSG_LAST_LF) { |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1491 | ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count); |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1492 | if (!ret) |
| 1493 | break; |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 1494 | if ((h1m->flags & H1_MF_RESP) && |
| 1495 | h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) { |
| 1496 | h1m_init_res(&h1s->res); |
| 1497 | h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR); |
| 1498 | } |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1499 | } |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1500 | else if (h1m->state < H1_MSG_TRAILERS) { |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1501 | ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf); |
Willy Tarreau | 78f548f | 2018-12-05 10:02:39 +0100 | [diff] [blame] | 1502 | htx = htx_from_buf(buf); |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1503 | if (!ret) |
| 1504 | break; |
| 1505 | } |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1506 | else if (h1m->state == H1_MSG_TRAILERS) { |
| 1507 | if (!(h1s->flags & H1S_F_HAVE_I_TLR)) { |
| 1508 | ret = h1_process_trailers(h1s, h1m, htx, &h1c->ibuf, &total, count); |
| 1509 | if (!ret) |
| 1510 | break; |
| 1511 | } |
Christopher Faulet | 1021e72 | 2019-09-03 21:55:14 +0200 | [diff] [blame] | 1512 | else if (!h1_process_eom(h1s, h1m, htx, count)) |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1513 | break; |
| 1514 | } |
Christopher Faulet | cb55f48 | 2018-12-10 11:56:47 +0100 | [diff] [blame] | 1515 | else if (h1m->state == H1_MSG_DONE) { |
Christopher Faulet | ac4778c | 2019-11-15 11:14:23 +0100 | [diff] [blame] | 1516 | if (!(h1m->flags & H1_MF_RESP) && h1s->status == 101) |
| 1517 | h1_set_req_tunnel_mode(h1s); |
Christopher Faulet | affe0cb | 2020-07-10 10:01:26 +0200 | [diff] [blame] | 1518 | else if (h1s->req.state < H1_MSG_DONE || h1s->res.state < H1_MSG_DONE) { |
Christopher Faulet | 2f320ee | 2019-04-16 20:26:53 +0200 | [diff] [blame] | 1519 | h1c->flags |= H1C_F_IN_BUSY; |
Christopher Faulet | affe0cb | 2020-07-10 10:01:26 +0200 | [diff] [blame] | 1520 | break; |
| 1521 | } |
| 1522 | else |
| 1523 | break; |
Christopher Faulet | cb55f48 | 2018-12-10 11:56:47 +0100 | [diff] [blame] | 1524 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1525 | else if (h1m->state == H1_MSG_TUNNEL) { |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1526 | ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf); |
Willy Tarreau | 78f548f | 2018-12-05 10:02:39 +0100 | [diff] [blame] | 1527 | htx = htx_from_buf(buf); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1528 | if (!ret) |
| 1529 | break; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1530 | } |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1531 | else { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1532 | h1s->flags |= errflag; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1533 | break; |
| 1534 | } |
| 1535 | |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1536 | count -= htx_used_space(htx) - used; |
Christopher Faulet | 98e2bc2 | 2019-09-03 16:26:15 +0200 | [diff] [blame] | 1537 | } while (!(h1s->flags & errflag)); |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1538 | |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1539 | if (h1s->flags & errflag) |
| 1540 | goto parsing_err; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1541 | |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1542 | b_del(&h1c->ibuf, total); |
| 1543 | |
| 1544 | end: |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 1545 | htx_to_buf(htx, buf); |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1546 | ret = htx->data - data; |
Willy Tarreau | 45f2b89 | 2018-12-05 07:59:27 +0100 | [diff] [blame] | 1547 | if (h1c->flags & H1C_F_IN_FULL && buf_room_for_htx_data(&h1c->ibuf)) { |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1548 | h1c->flags &= ~H1C_F_IN_FULL; |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 1549 | tasklet_wakeup(h1c->wait_event.tasklet); |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1550 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1551 | |
Christopher Faulet | cf56b99 | 2018-12-11 16:12:31 +0100 | [diff] [blame] | 1552 | h1s->cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM); |
| 1553 | |
Christopher Faulet | cdc90e9 | 2019-03-28 13:28:46 +0100 | [diff] [blame] | 1554 | if (!b_data(&h1c->ibuf)) |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1555 | h1_release_buf(h1c, &h1c->ibuf); |
Christopher Faulet | a882a46 | 2019-12-06 15:59:05 +0100 | [diff] [blame] | 1556 | |
| 1557 | if ((h1s_data_pending(h1s) && !htx_is_empty(htx)) || (h1s->flags & H1S_F_APPEND_EOM)) |
Christopher Faulet | cf56b99 | 2018-12-11 16:12:31 +0100 | [diff] [blame] | 1558 | h1s->cs->flags |= CS_FL_RCV_MORE | CS_FL_WANT_ROOM; |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1559 | |
Willy Tarreau | 6203815 | 2019-08-23 09:29:29 +0200 | [diff] [blame] | 1560 | if (((h1s->flags & (H1S_F_REOS|H1S_F_APPEND_EOM)) == H1S_F_REOS) && |
| 1561 | (!h1s_data_pending(h1s) || htx_is_empty(htx))) { |
Christopher Faulet | f6ce9d6 | 2018-12-10 15:30:06 +0100 | [diff] [blame] | 1562 | h1s->cs->flags |= CS_FL_EOS; |
Christopher Faulet | 2692238 | 2019-03-08 15:13:41 +0100 | [diff] [blame] | 1563 | if (h1m->state > H1_MSG_LAST_LF && h1m->state < H1_MSG_DONE) |
Christopher Faulet | b8d2ee0 | 2019-02-25 15:29:51 +0100 | [diff] [blame] | 1564 | h1s->cs->flags |= CS_FL_ERROR; |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1565 | } |
Christopher Faulet | f6ce9d6 | 2018-12-10 15:30:06 +0100 | [diff] [blame] | 1566 | |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1567 | return ret; |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1568 | |
| 1569 | parsing_err: |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1570 | b_reset(&h1c->ibuf); |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 1571 | htx_to_buf(htx, buf); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1572 | return 0; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1573 | } |
| 1574 | |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1575 | /* |
| 1576 | * Process outgoing data. It parses data and transfer them from the channel buffer into |
| 1577 | * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or |
| 1578 | * 0 if it couldn't proceed. |
| 1579 | */ |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1580 | static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count) |
| 1581 | { |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1582 | struct h1s *h1s = h1c->h1s; |
| 1583 | struct h1m *h1m; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1584 | struct htx *chn_htx; |
| 1585 | struct htx_blk *blk; |
Christopher Faulet | 65fc1dc | 2019-06-25 21:41:02 +0200 | [diff] [blame] | 1586 | struct buffer tmp; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1587 | size_t total = 0; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1588 | int errflag; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1589 | |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1590 | if (!count) |
| 1591 | goto end; |
Willy Tarreau | 37dd54d | 2018-12-15 14:48:31 +0100 | [diff] [blame] | 1592 | |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 1593 | chn_htx = htxbuf(buf); |
Willy Tarreau | 37dd54d | 2018-12-15 14:48:31 +0100 | [diff] [blame] | 1594 | if (htx_is_empty(chn_htx)) |
| 1595 | goto end; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1596 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1597 | if (!h1_get_buf(h1c, &h1c->obuf)) { |
| 1598 | h1c->flags |= H1C_F_OUT_ALLOC; |
| 1599 | goto end; |
| 1600 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1601 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1602 | if (!conn_is_back(h1c->conn)) { |
| 1603 | h1m = &h1s->res; |
| 1604 | errflag = H1S_F_RES_ERROR; |
| 1605 | } |
| 1606 | else { |
| 1607 | h1m = &h1s->req; |
| 1608 | errflag = H1S_F_REQ_ERROR; |
| 1609 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1610 | |
Christopher Faulet | cd9f6a7 | 2019-07-04 21:22:34 +0200 | [diff] [blame] | 1611 | if (h1s->flags & errflag) |
| 1612 | goto end; |
| 1613 | |
Willy Tarreau | 37dd54d | 2018-12-15 14:48:31 +0100 | [diff] [blame] | 1614 | /* the htx is non-empty thus has at least one block */ |
Willy Tarreau | 3815b22 | 2018-12-11 19:50:43 +0100 | [diff] [blame] | 1615 | blk = htx_get_head_blk(chn_htx); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1616 | |
Willy Tarreau | 3815b22 | 2018-12-11 19:50:43 +0100 | [diff] [blame] | 1617 | /* Perform some optimizations to reduce the number of buffer copies. |
| 1618 | * First, if the mux's buffer is empty and the htx area contains |
| 1619 | * exactly one data block of the same size as the requested count, |
| 1620 | * then it's possible to simply swap the caller's buffer with the |
| 1621 | * mux's output buffer and adjust offsets and length to match the |
| 1622 | * entire DATA HTX block in the middle. In this case we perform a |
| 1623 | * true zero-copy operation from end-to-end. This is the situation |
| 1624 | * that happens all the time with large files. Second, if this is not |
| 1625 | * possible, but the mux's output buffer is empty, we still have an |
| 1626 | * opportunity to avoid the copy to the intermediary buffer, by making |
| 1627 | * the intermediary buffer's area point to the output buffer's area. |
| 1628 | * In this case we want to skip the HTX header to make sure that copies |
| 1629 | * remain aligned and that this operation remains possible all the |
| 1630 | * time. This goes for headers, data blocks and any data extracted from |
| 1631 | * the HTX blocks. |
Willy Tarreau | c5efa33 | 2018-12-05 11:19:27 +0100 | [diff] [blame] | 1632 | */ |
| 1633 | if (!b_data(&h1c->obuf)) { |
Willy Tarreau | 3815b22 | 2018-12-11 19:50:43 +0100 | [diff] [blame] | 1634 | if (chn_htx->used == 1 && |
Willy Tarreau | 37dd54d | 2018-12-15 14:48:31 +0100 | [diff] [blame] | 1635 | htx_get_blk_type(blk) == HTX_BLK_DATA && |
Willy Tarreau | 3815b22 | 2018-12-11 19:50:43 +0100 | [diff] [blame] | 1636 | htx_get_blk_value(chn_htx, blk).len == count) { |
| 1637 | void *old_area = h1c->obuf.area; |
| 1638 | |
| 1639 | h1c->obuf.area = buf->area; |
Christopher Faulet | 65fc1dc | 2019-06-25 21:41:02 +0200 | [diff] [blame] | 1640 | h1c->obuf.head = sizeof(struct htx) + blk->addr; |
Willy Tarreau | 3815b22 | 2018-12-11 19:50:43 +0100 | [diff] [blame] | 1641 | h1c->obuf.data = count; |
| 1642 | |
| 1643 | buf->area = old_area; |
| 1644 | buf->data = buf->head = 0; |
Christopher Faulet | adb2220 | 2018-12-12 10:32:09 +0100 | [diff] [blame] | 1645 | |
| 1646 | /* The message is chunked. We need to emit the chunk |
| 1647 | * size. We have at least the size of the struct htx to |
| 1648 | * write the chunk envelope. It should be enough. |
| 1649 | */ |
| 1650 | if (h1m->flags & H1_MF_CHNK) { |
| 1651 | h1_emit_chunk_size(&h1c->obuf, count); |
| 1652 | h1_emit_chunk_crlf(&h1c->obuf); |
| 1653 | } |
| 1654 | |
Willy Tarreau | 3815b22 | 2018-12-11 19:50:43 +0100 | [diff] [blame] | 1655 | total += count; |
| 1656 | goto out; |
| 1657 | } |
Christopher Faulet | 65fc1dc | 2019-06-25 21:41:02 +0200 | [diff] [blame] | 1658 | tmp.area = h1c->obuf.area + h1c->obuf.head; |
Willy Tarreau | c5efa33 | 2018-12-05 11:19:27 +0100 | [diff] [blame] | 1659 | } |
Christopher Faulet | 65fc1dc | 2019-06-25 21:41:02 +0200 | [diff] [blame] | 1660 | else |
| 1661 | tmp.area = trash.area; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1662 | |
Christopher Faulet | 65fc1dc | 2019-06-25 21:41:02 +0200 | [diff] [blame] | 1663 | tmp.data = 0; |
| 1664 | tmp.size = b_room(&h1c->obuf); |
Christopher Faulet | b2e8416 | 2018-12-06 11:39:49 +0100 | [diff] [blame] | 1665 | while (count && !(h1s->flags & errflag) && blk) { |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 1666 | struct htx_sl *sl; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1667 | struct ist n, v; |
Christopher Faulet | b2e8416 | 2018-12-06 11:39:49 +0100 | [diff] [blame] | 1668 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1669 | uint32_t sz = htx_get_blksz(blk); |
Christopher Faulet | b2e8416 | 2018-12-06 11:39:49 +0100 | [diff] [blame] | 1670 | uint32_t vlen; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1671 | |
Christopher Faulet | b2e8416 | 2018-12-06 11:39:49 +0100 | [diff] [blame] | 1672 | vlen = sz; |
| 1673 | if (vlen > count) { |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1674 | if (type != HTX_BLK_DATA) |
Christopher Faulet | 85fc6ef | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 1675 | goto full; |
Christopher Faulet | b2e8416 | 2018-12-06 11:39:49 +0100 | [diff] [blame] | 1676 | vlen = count; |
| 1677 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1678 | |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 1679 | if (type == HTX_BLK_UNUSED) |
| 1680 | goto nextblk; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1681 | |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 1682 | switch (h1m->state) { |
| 1683 | case H1_MSG_RQBEFORE: |
| 1684 | if (type != HTX_BLK_REQ_SL) |
| 1685 | goto error; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1686 | sl = htx_get_blk_ptr(chn_htx, blk); |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 1687 | h1s->meth = sl->info.req.meth; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1688 | h1_parse_req_vsn(h1m, sl); |
Christopher Faulet | 65fc1dc | 2019-06-25 21:41:02 +0200 | [diff] [blame] | 1689 | if (!htx_reqline_to_h1(sl, &tmp)) |
Christopher Faulet | 85fc6ef | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 1690 | goto full; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1691 | h1m->flags |= H1_MF_XFER_LEN; |
Christopher Faulet | 1f890dd | 2019-02-18 10:33:16 +0100 | [diff] [blame] | 1692 | if (sl->flags & HTX_SL_F_BODYLESS) |
| 1693 | h1m->flags |= H1_MF_CLEN; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1694 | h1m->state = H1_MSG_HDR_FIRST; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1695 | break; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1696 | |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 1697 | case H1_MSG_RPBEFORE: |
| 1698 | if (type != HTX_BLK_RES_SL) |
| 1699 | goto error; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1700 | sl = htx_get_blk_ptr(chn_htx, blk); |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 1701 | h1s->status = sl->info.res.status; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1702 | h1_parse_res_vsn(h1m, sl); |
Christopher Faulet | 65fc1dc | 2019-06-25 21:41:02 +0200 | [diff] [blame] | 1703 | if (!htx_stline_to_h1(sl, &tmp)) |
Christopher Faulet | 85fc6ef | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 1704 | goto full; |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 1705 | if (sl->flags & HTX_SL_F_XFER_LEN) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1706 | h1m->flags |= H1_MF_XFER_LEN; |
Christopher Faulet | d1ebb1e | 2018-11-28 16:32:50 +0100 | [diff] [blame] | 1707 | if (sl->info.res.status < 200 && |
| 1708 | (sl->info.res.status == 100 || sl->info.res.status >= 102)) |
Christopher Faulet | ada34b6 | 2019-05-24 16:36:43 +0200 | [diff] [blame] | 1709 | h1s->flags |= H1S_F_HAVE_O_CONN; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1710 | h1m->state = H1_MSG_HDR_FIRST; |
| 1711 | break; |
| 1712 | |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 1713 | case H1_MSG_HDR_FIRST: |
| 1714 | case H1_MSG_HDR_NAME: |
| 1715 | case H1_MSG_HDR_L2_LWS: |
| 1716 | if (type == HTX_BLK_EOH) |
| 1717 | goto last_lf; |
| 1718 | if (type != HTX_BLK_HDR) |
| 1719 | goto error; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1720 | h1m->state = H1_MSG_HDR_NAME; |
| 1721 | n = htx_get_blk_name(chn_htx, blk); |
| 1722 | v = htx_get_blk_value(chn_htx, blk); |
| 1723 | |
| 1724 | if (isteqi(n, ist("transfer-encoding"))) |
| 1725 | h1_parse_xfer_enc_header(h1m, v); |
Willy Tarreau | 27cd223 | 2019-01-03 21:52:42 +0100 | [diff] [blame] | 1726 | else if (isteqi(n, ist("content-length"))) { |
Christopher Faulet | 5220ef2 | 2019-03-27 15:44:56 +0100 | [diff] [blame] | 1727 | /* Only skip C-L header with invalid value. */ |
| 1728 | if (h1_parse_cont_len_header(h1m, &v) < 0) |
Willy Tarreau | 27cd223 | 2019-01-03 21:52:42 +0100 | [diff] [blame] | 1729 | goto skip_hdr; |
| 1730 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1731 | else if (isteqi(n, ist("connection"))) { |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1732 | h1_parse_connection_header(h1m, &v); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1733 | if (!v.len) |
| 1734 | goto skip_hdr; |
| 1735 | } |
| 1736 | |
Christopher Faulet | a99ff4d | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 1737 | /* Try to adjust the case of the header name */ |
| 1738 | if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV)) |
| 1739 | h1_adjust_case_outgoing_hdr(h1s, h1m, &n); |
Christopher Faulet | 65fc1dc | 2019-06-25 21:41:02 +0200 | [diff] [blame] | 1740 | if (!htx_hdr_to_h1(n, v, &tmp)) |
Christopher Faulet | 85fc6ef | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 1741 | goto full; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1742 | skip_hdr: |
| 1743 | h1m->state = H1_MSG_HDR_L2_LWS; |
| 1744 | break; |
| 1745 | |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 1746 | case H1_MSG_LAST_LF: |
| 1747 | if (type != HTX_BLK_EOH) |
| 1748 | goto error; |
| 1749 | last_lf: |
Christopher Faulet | ada34b6 | 2019-05-24 16:36:43 +0200 | [diff] [blame] | 1750 | h1m->state = H1_MSG_LAST_LF; |
| 1751 | if (!(h1s->flags & H1S_F_HAVE_O_CONN)) { |
Christopher Faulet | d1ebb1e | 2018-11-28 16:32:50 +0100 | [diff] [blame] | 1752 | /* There is no "Connection:" header and |
| 1753 | * it the conn_mode must be |
| 1754 | * processed. So do it */ |
Christopher Faulet | 661bfc3 | 2019-06-17 14:07:46 +0200 | [diff] [blame] | 1755 | n = ist("connection"); |
Christopher Faulet | d1ebb1e | 2018-11-28 16:32:50 +0100 | [diff] [blame] | 1756 | v = ist(""); |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1757 | h1_process_output_conn_mode(h1s, h1m, &v); |
Christopher Faulet | d1ebb1e | 2018-11-28 16:32:50 +0100 | [diff] [blame] | 1758 | if (v.len) { |
Christopher Faulet | a99ff4d | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 1759 | /* Try to adjust the case of the header name */ |
| 1760 | if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV)) |
| 1761 | h1_adjust_case_outgoing_hdr(h1s, h1m, &n); |
Christopher Faulet | 65fc1dc | 2019-06-25 21:41:02 +0200 | [diff] [blame] | 1762 | if (!htx_hdr_to_h1(n, v, &tmp)) |
Christopher Faulet | 85fc6ef | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 1763 | goto full; |
Christopher Faulet | d1ebb1e | 2018-11-28 16:32:50 +0100 | [diff] [blame] | 1764 | } |
Christopher Faulet | ada34b6 | 2019-05-24 16:36:43 +0200 | [diff] [blame] | 1765 | h1s->flags |= H1S_F_HAVE_O_CONN; |
Christopher Faulet | d1ebb1e | 2018-11-28 16:32:50 +0100 | [diff] [blame] | 1766 | } |
Willy Tarreau | 4710d20 | 2019-01-03 17:39:54 +0100 | [diff] [blame] | 1767 | |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 1768 | if ((h1s->meth != HTTP_METH_CONNECT && |
| 1769 | (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) == |
Christopher Faulet | 1f890dd | 2019-02-18 10:33:16 +0100 | [diff] [blame] | 1770 | (H1_MF_VER_11|H1_MF_XFER_LEN)) || |
| 1771 | (h1s->status >= 200 && h1s->status != 204 && h1s->status != 304 && |
| 1772 | h1s->meth != HTTP_METH_HEAD && !(h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) && |
| 1773 | (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) == |
| 1774 | (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) { |
Willy Tarreau | 4710d20 | 2019-01-03 17:39:54 +0100 | [diff] [blame] | 1775 | /* chunking needed but header not seen */ |
Christopher Faulet | e5addb0 | 2019-10-04 10:23:51 +0200 | [diff] [blame] | 1776 | n = ist("transfer-encoding"); |
| 1777 | v = ist("chunked"); |
| 1778 | if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV)) |
| 1779 | h1_adjust_case_outgoing_hdr(h1s, h1m, &n); |
| 1780 | if (!htx_hdr_to_h1(n, v, &tmp)) |
Christopher Faulet | 85fc6ef | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 1781 | goto full; |
Willy Tarreau | 4710d20 | 2019-01-03 17:39:54 +0100 | [diff] [blame] | 1782 | h1m->flags |= H1_MF_CHNK; |
| 1783 | } |
| 1784 | |
Christopher Faulet | 65fc1dc | 2019-06-25 21:41:02 +0200 | [diff] [blame] | 1785 | if (!chunk_memcat(&tmp, "\r\n", 2)) |
Christopher Faulet | 85fc6ef | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 1786 | goto full; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1787 | |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 1788 | if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) { |
| 1789 | /* a CONNECT request is sent to the server. Switch it to tunnel mode. */ |
| 1790 | h1_set_req_tunnel_mode(h1s); |
| 1791 | } |
Christopher Faulet | ac4778c | 2019-11-15 11:14:23 +0100 | [diff] [blame] | 1792 | else if ((h1m->flags & H1_MF_RESP) && |
| 1793 | ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) || h1s->status == 101)) { |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 1794 | /* a successfull reply to a CONNECT or a protocol switching is sent |
Christopher Faulet | ac4778c | 2019-11-15 11:14:23 +0100 | [diff] [blame] | 1795 | * to the client. Switch the response to tunnel mode. |
| 1796 | */ |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 1797 | h1_set_res_tunnel_mode(h1s); |
| 1798 | } |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 1799 | else if ((h1m->flags & H1_MF_RESP) && |
| 1800 | h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) { |
| 1801 | h1m_init_res(&h1s->res); |
| 1802 | h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR); |
Christopher Faulet | ada34b6 | 2019-05-24 16:36:43 +0200 | [diff] [blame] | 1803 | h1s->flags &= ~H1S_F_HAVE_O_CONN; |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 1804 | } |
Christopher Faulet | 33d58b5 | 2019-07-01 16:17:30 +0200 | [diff] [blame] | 1805 | else if ((h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_HEAD) |
| 1806 | h1m->state = H1_MSG_DONE; |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 1807 | else |
| 1808 | h1m->state = H1_MSG_DATA; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1809 | break; |
| 1810 | |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 1811 | case H1_MSG_DATA: |
Christopher Faulet | aaa2506 | 2019-07-04 17:12:12 +0200 | [diff] [blame] | 1812 | case H1_MSG_TUNNEL: |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1813 | if (type == HTX_BLK_EOM) { |
| 1814 | /* Chunked message without explicit trailers */ |
| 1815 | if (h1m->flags & H1_MF_CHNK) { |
Christopher Faulet | 65fc1dc | 2019-06-25 21:41:02 +0200 | [diff] [blame] | 1816 | if (!chunk_memcat(&tmp, "0\r\n\r\n", 5)) |
Christopher Faulet | 85fc6ef | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 1817 | goto full; |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1818 | } |
| 1819 | goto done; |
| 1820 | } |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1821 | else if (type == HTX_BLK_EOT || type == HTX_BLK_TLR) { |
Christopher Faulet | 5213168 | 2019-06-27 17:40:14 +0200 | [diff] [blame] | 1822 | /* If the message is not chunked, never |
| 1823 | * add the last chunk. */ |
| 1824 | if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(&tmp, "0\r\n", 3)) |
Christopher Faulet | 85fc6ef | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 1825 | goto full; |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 1826 | goto trailers; |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1827 | } |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 1828 | else if (type != HTX_BLK_DATA) |
| 1829 | goto error; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1830 | v = htx_get_blk_value(chn_htx, blk); |
Christopher Faulet | b2e8416 | 2018-12-06 11:39:49 +0100 | [diff] [blame] | 1831 | v.len = vlen; |
Christopher Faulet | 65fc1dc | 2019-06-25 21:41:02 +0200 | [diff] [blame] | 1832 | if (!htx_data_to_h1(v, &tmp, !!(h1m->flags & H1_MF_CHNK))) |
Christopher Faulet | 85fc6ef | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 1833 | goto full; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1834 | break; |
| 1835 | |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 1836 | case H1_MSG_TRAILERS: |
| 1837 | if (type == HTX_BLK_EOM) |
| 1838 | goto done; |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1839 | else if (type != HTX_BLK_TLR && type != HTX_BLK_EOT) |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 1840 | goto error; |
| 1841 | trailers: |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1842 | h1m->state = H1_MSG_TRAILERS; |
Christopher Faulet | 5213168 | 2019-06-27 17:40:14 +0200 | [diff] [blame] | 1843 | /* If the message is not chunked, ignore |
| 1844 | * trailers. It may happen with H2 messages. */ |
| 1845 | if (!(h1m->flags & H1_MF_CHNK)) |
| 1846 | break; |
| 1847 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1848 | if (type == HTX_BLK_EOT) { |
Christopher Faulet | 65fc1dc | 2019-06-25 21:41:02 +0200 | [diff] [blame] | 1849 | if (!chunk_memcat(&tmp, "\r\n", 2)) |
Christopher Faulet | 85fc6ef | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 1850 | goto full; |
Christopher Faulet | 3218821 | 2018-11-20 18:21:43 +0100 | [diff] [blame] | 1851 | } |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1852 | else { // HTX_BLK_TLR |
| 1853 | n = htx_get_blk_name(chn_htx, blk); |
| 1854 | v = htx_get_blk_value(chn_htx, blk); |
Christopher Faulet | a99ff4d | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 1855 | |
| 1856 | /* Try to adjust the case of the header name */ |
| 1857 | if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV)) |
| 1858 | h1_adjust_case_outgoing_hdr(h1s, h1m, &n); |
Christopher Faulet | 65fc1dc | 2019-06-25 21:41:02 +0200 | [diff] [blame] | 1859 | if (!htx_hdr_to_h1(n, v, &tmp)) |
Christopher Faulet | 85fc6ef | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 1860 | goto full; |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1861 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1862 | break; |
| 1863 | |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 1864 | case H1_MSG_DONE: |
| 1865 | if (type != HTX_BLK_EOM) |
| 1866 | goto error; |
| 1867 | done: |
| 1868 | h1m->state = H1_MSG_DONE; |
Christopher Faulet | ac4778c | 2019-11-15 11:14:23 +0100 | [diff] [blame] | 1869 | if (!(h1m->flags & H1_MF_RESP) && h1s->status == 101) |
| 1870 | h1_set_req_tunnel_mode(h1s); |
| 1871 | else if (h1s->h1c->flags & H1C_F_IN_BUSY) { |
Christopher Faulet | cd67bff | 2019-06-14 16:54:15 +0200 | [diff] [blame] | 1872 | h1s->h1c->flags &= ~H1C_F_IN_BUSY; |
| 1873 | tasklet_wakeup(h1s->h1c->wait_event.tasklet); |
| 1874 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1875 | break; |
| 1876 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1877 | default: |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 1878 | error: |
Christopher Faulet | c431df8 | 2019-06-26 15:16:28 +0200 | [diff] [blame] | 1879 | /* Unexpected error during output processing */ |
| 1880 | chn_htx->flags |= HTX_FL_PARSING_ERROR; |
Christopher Faulet | a2ea158 | 2019-05-28 10:35:18 +0200 | [diff] [blame] | 1881 | h1s->flags |= errflag; |
Christopher Faulet | c431df8 | 2019-06-26 15:16:28 +0200 | [diff] [blame] | 1882 | h1c->flags |= H1C_F_CS_ERROR; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1883 | break; |
| 1884 | } |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 1885 | |
| 1886 | nextblk: |
Christopher Faulet | b2e8416 | 2018-12-06 11:39:49 +0100 | [diff] [blame] | 1887 | total += vlen; |
| 1888 | count -= vlen; |
| 1889 | if (sz == vlen) |
| 1890 | blk = htx_remove_blk(chn_htx, blk); |
| 1891 | else { |
| 1892 | htx_cut_data_blk(chn_htx, blk, vlen); |
| 1893 | break; |
| 1894 | } |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1895 | } |
| 1896 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1897 | copy: |
Willy Tarreau | c5efa33 | 2018-12-05 11:19:27 +0100 | [diff] [blame] | 1898 | /* when the output buffer is empty, tmp shares the same area so that we |
| 1899 | * only have to update pointers and lengths. |
| 1900 | */ |
Christopher Faulet | 65fc1dc | 2019-06-25 21:41:02 +0200 | [diff] [blame] | 1901 | if (tmp.area == h1c->obuf.area + h1c->obuf.head) |
| 1902 | h1c->obuf.data = tmp.data; |
Willy Tarreau | c5efa33 | 2018-12-05 11:19:27 +0100 | [diff] [blame] | 1903 | else |
Christopher Faulet | 65fc1dc | 2019-06-25 21:41:02 +0200 | [diff] [blame] | 1904 | b_putblk(&h1c->obuf, tmp.area, tmp.data); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1905 | |
Willy Tarreau | 3815b22 | 2018-12-11 19:50:43 +0100 | [diff] [blame] | 1906 | htx_to_buf(chn_htx, buf); |
| 1907 | out: |
Willy Tarreau | 45f2b89 | 2018-12-05 07:59:27 +0100 | [diff] [blame] | 1908 | if (!buf_room_for_htx_data(&h1c->obuf)) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1909 | h1c->flags |= H1C_F_OUT_FULL; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1910 | end: |
| 1911 | return total; |
Christopher Faulet | 85fc6ef | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 1912 | |
| 1913 | full: |
| 1914 | h1c->flags |= H1C_F_OUT_FULL; |
| 1915 | goto copy; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1916 | } |
| 1917 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1918 | /*********************************************************/ |
| 1919 | /* functions below are I/O callbacks from the connection */ |
| 1920 | /*********************************************************/ |
Christopher Faulet | e17fa2f | 2018-12-11 16:25:36 +0100 | [diff] [blame] | 1921 | static void h1_wake_stream_for_recv(struct h1s *h1s) |
| 1922 | { |
| 1923 | if (h1s && h1s->recv_wait) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 1924 | h1s->recv_wait->events &= ~SUB_RETRY_RECV; |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 1925 | tasklet_wakeup(h1s->recv_wait->tasklet); |
Christopher Faulet | e17fa2f | 2018-12-11 16:25:36 +0100 | [diff] [blame] | 1926 | h1s->recv_wait = NULL; |
| 1927 | } |
| 1928 | } |
| 1929 | static void h1_wake_stream_for_send(struct h1s *h1s) |
| 1930 | { |
| 1931 | if (h1s && h1s->send_wait) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 1932 | h1s->send_wait->events &= ~SUB_RETRY_SEND; |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 1933 | tasklet_wakeup(h1s->send_wait->tasklet); |
Christopher Faulet | e17fa2f | 2018-12-11 16:25:36 +0100 | [diff] [blame] | 1934 | h1s->send_wait = NULL; |
| 1935 | } |
| 1936 | } |
| 1937 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1938 | /* |
| 1939 | * Attempt to read data, and subscribe if none available |
| 1940 | */ |
| 1941 | static int h1_recv(struct h1c *h1c) |
| 1942 | { |
| 1943 | struct connection *conn = h1c->conn; |
Christopher Faulet | feb1174 | 2018-11-29 15:12:34 +0100 | [diff] [blame] | 1944 | struct h1s *h1s = h1c->h1s; |
Olivier Houchard | 75159a9 | 2018-12-03 18:46:09 +0100 | [diff] [blame] | 1945 | size_t ret = 0, max; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1946 | int rcvd = 0; |
| 1947 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 1948 | if (h1c->wait_event.events & SUB_RETRY_RECV) |
Christopher Faulet | c386a88 | 2018-12-04 16:06:28 +0100 | [diff] [blame] | 1949 | return (b_data(&h1c->ibuf)); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1950 | |
Olivier Houchard | 5b06cb3 | 2019-08-13 15:21:59 +0200 | [diff] [blame] | 1951 | if (!(conn->flags & CO_FL_ERROR) && h1c->flags & H1C_F_CS_WAIT_CONN) |
Willy Tarreau | d58f27f | 2019-06-03 10:12:22 +0200 | [diff] [blame] | 1952 | return 0; |
Willy Tarreau | d58f27f | 2019-06-03 10:12:22 +0200 | [diff] [blame] | 1953 | |
Olivier Houchard | 75159a9 | 2018-12-03 18:46:09 +0100 | [diff] [blame] | 1954 | if (!h1_recv_allowed(h1c)) { |
| 1955 | rcvd = 1; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1956 | goto end; |
Olivier Houchard | 75159a9 | 2018-12-03 18:46:09 +0100 | [diff] [blame] | 1957 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1958 | |
Christopher Faulet | f7d5ff3 | 2019-04-16 13:55:08 +0200 | [diff] [blame] | 1959 | if (!h1_get_buf(h1c, &h1c->ibuf)) { |
| 1960 | h1c->flags |= H1C_F_IN_ALLOC; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1961 | goto end; |
| 1962 | } |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 1963 | |
Christopher Faulet | f7d5ff3 | 2019-04-16 13:55:08 +0200 | [diff] [blame] | 1964 | if (h1s && (h1s->flags & (H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA))) { |
Willy Tarreau | fbdf90a | 2019-06-03 13:42:54 +0200 | [diff] [blame] | 1965 | if (!h1s_data_pending(h1s)) |
Christopher Faulet | f7d5ff3 | 2019-04-16 13:55:08 +0200 | [diff] [blame] | 1966 | h1_wake_stream_for_recv(h1s); |
| 1967 | rcvd = 1; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1968 | goto end; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1969 | } |
| 1970 | |
Olivier Houchard | 29a22bc | 2018-12-04 18:16:45 +0100 | [diff] [blame] | 1971 | /* |
| 1972 | * If we only have a small amount of data, realign it, |
| 1973 | * it's probably cheaper than doing 2 recv() calls. |
| 1974 | */ |
| 1975 | if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128) |
| 1976 | b_slow_realign(&h1c->ibuf, trash.area, 0); |
| 1977 | |
Willy Tarreau | 45f2b89 | 2018-12-05 07:59:27 +0100 | [diff] [blame] | 1978 | max = buf_room_for_htx_data(&h1c->ibuf); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1979 | if (max) { |
| 1980 | h1c->flags &= ~H1C_F_IN_FULL; |
Willy Tarreau | 78f548f | 2018-12-05 10:02:39 +0100 | [diff] [blame] | 1981 | |
Willy Tarreau | e0f24ee | 2018-12-14 10:51:23 +0100 | [diff] [blame] | 1982 | b_realign_if_empty(&h1c->ibuf); |
Willy Tarreau | 78f548f | 2018-12-05 10:02:39 +0100 | [diff] [blame] | 1983 | if (!b_data(&h1c->ibuf)) { |
| 1984 | /* try to pre-align the buffer like the rxbufs will be |
| 1985 | * to optimize memory copies. |
| 1986 | */ |
Willy Tarreau | 78f548f | 2018-12-05 10:02:39 +0100 | [diff] [blame] | 1987 | h1c->ibuf.head = sizeof(struct htx); |
| 1988 | } |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 1989 | ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, &h1c->ibuf, max, 0); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1990 | } |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1991 | if (ret > 0) { |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1992 | rcvd = 1; |
Christopher Faulet | feb1174 | 2018-11-29 15:12:34 +0100 | [diff] [blame] | 1993 | if (h1s && h1s->cs) { |
Christopher Faulet | 37e3607 | 2018-12-04 15:54:12 +0100 | [diff] [blame] | 1994 | h1s->cs->flags |= (CS_FL_READ_PARTIAL|CS_FL_RCV_MORE); |
Christopher Faulet | feb1174 | 2018-11-29 15:12:34 +0100 | [diff] [blame] | 1995 | if (h1s->csinfo.t_idle == -1) |
| 1996 | h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake; |
| 1997 | } |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1998 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1999 | |
Christopher Faulet | cf56b99 | 2018-12-11 16:12:31 +0100 | [diff] [blame] | 2000 | if (!h1_recv_allowed(h1c) || !buf_room_for_htx_data(&h1c->ibuf)) { |
Christopher Faulet | 81d4843 | 2018-11-19 21:22:43 +0100 | [diff] [blame] | 2001 | rcvd = 1; |
Christopher Faulet | cf56b99 | 2018-12-11 16:12:31 +0100 | [diff] [blame] | 2002 | goto end; |
| 2003 | } |
| 2004 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2005 | conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2006 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2007 | end: |
Christopher Faulet | e17fa2f | 2018-12-11 16:25:36 +0100 | [diff] [blame] | 2008 | if (ret > 0 || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn)) |
| 2009 | h1_wake_stream_for_recv(h1s); |
Olivier Houchard | 75159a9 | 2018-12-03 18:46:09 +0100 | [diff] [blame] | 2010 | |
Willy Tarreau | c493c9c | 2019-06-03 14:18:22 +0200 | [diff] [blame] | 2011 | if (conn_xprt_read0_pending(conn) && h1s) { |
| 2012 | h1s->flags |= H1S_F_REOS; |
Willy Tarreau | fbdf90a | 2019-06-03 13:42:54 +0200 | [diff] [blame] | 2013 | rcvd = 1; |
| 2014 | } |
| 2015 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2016 | if (!b_data(&h1c->ibuf)) |
| 2017 | h1_release_buf(h1c, &h1c->ibuf); |
Willy Tarreau | 45f2b89 | 2018-12-05 07:59:27 +0100 | [diff] [blame] | 2018 | else if (!buf_room_for_htx_data(&h1c->ibuf)) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2019 | h1c->flags |= H1C_F_IN_FULL; |
| 2020 | return rcvd; |
| 2021 | } |
| 2022 | |
| 2023 | |
| 2024 | /* |
| 2025 | * Try to send data if possible |
| 2026 | */ |
| 2027 | static int h1_send(struct h1c *h1c) |
| 2028 | { |
| 2029 | struct connection *conn = h1c->conn; |
| 2030 | unsigned int flags = 0; |
| 2031 | size_t ret; |
| 2032 | int sent = 0; |
| 2033 | |
| 2034 | if (conn->flags & CO_FL_ERROR) |
| 2035 | return 0; |
| 2036 | |
Christopher Faulet | 3b88b8d | 2018-10-26 17:36:03 +0200 | [diff] [blame] | 2037 | if (h1c->flags & H1C_F_CS_WAIT_CONN) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2038 | if (!(h1c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2039 | conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event); |
Christopher Faulet | 3b88b8d | 2018-10-26 17:36:03 +0200 | [diff] [blame] | 2040 | return 0; |
| 2041 | } |
| 2042 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2043 | if (!b_data(&h1c->obuf)) |
| 2044 | goto end; |
| 2045 | |
| 2046 | if (h1c->flags & H1C_F_OUT_FULL) |
| 2047 | flags |= CO_SFL_MSG_MORE; |
| 2048 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2049 | ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h1c->obuf, b_data(&h1c->obuf), flags); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2050 | if (ret > 0) { |
| 2051 | h1c->flags &= ~H1C_F_OUT_FULL; |
| 2052 | b_del(&h1c->obuf, ret); |
| 2053 | sent = 1; |
| 2054 | } |
| 2055 | |
Christopher Faulet | 145aa47 | 2018-12-06 10:56:20 +0100 | [diff] [blame] | 2056 | if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) { |
| 2057 | /* error or output closed, nothing to send, clear the buffer to release it */ |
| 2058 | b_reset(&h1c->obuf); |
| 2059 | } |
| 2060 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2061 | end: |
Christopher Faulet | e17fa2f | 2018-12-11 16:25:36 +0100 | [diff] [blame] | 2062 | if (!(h1c->flags & H1C_F_OUT_FULL)) |
| 2063 | h1_wake_stream_for_send(h1c->h1s); |
Olivier Houchard | 75159a9 | 2018-12-03 18:46:09 +0100 | [diff] [blame] | 2064 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2065 | /* We're done, no more to send */ |
| 2066 | if (!b_data(&h1c->obuf)) { |
| 2067 | h1_release_buf(h1c, &h1c->obuf); |
| 2068 | if (h1c->flags & H1C_F_CS_SHUTW_NOW) |
Christopher Faulet | 666a0c4 | 2019-01-08 11:12:04 +0100 | [diff] [blame] | 2069 | h1_shutw_conn(conn, CS_SHW_NORMAL); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2070 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2071 | else if (!(h1c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2072 | conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2073 | |
| 2074 | return sent; |
| 2075 | } |
| 2076 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2077 | |
| 2078 | /* callback called on any event by the connection handler. |
| 2079 | * It applies changes and returns zero, or < 0 if it wants immediate |
| 2080 | * destruction of the connection. |
| 2081 | */ |
| 2082 | static int h1_process(struct h1c * h1c) |
| 2083 | { |
| 2084 | struct connection *conn = h1c->conn; |
Christopher Faulet | feb1174 | 2018-11-29 15:12:34 +0100 | [diff] [blame] | 2085 | struct h1s *h1s = h1c->h1s; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2086 | |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 2087 | if (!conn->ctx) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2088 | return -1; |
| 2089 | |
Christopher Faulet | 3b88b8d | 2018-10-26 17:36:03 +0200 | [diff] [blame] | 2090 | if (h1c->flags & H1C_F_CS_WAIT_CONN) { |
Olivier Houchard | 690e0f0 | 2019-06-11 16:37:24 +0200 | [diff] [blame] | 2091 | if (!(conn->flags & (CO_FL_CONNECTED|CO_FL_ERROR)) || |
Olivier Houchard | 6063003 | 2019-06-13 17:37:00 +0200 | [diff] [blame] | 2092 | (!(conn->flags & CO_FL_ERROR) && (conn->flags & CO_FL_HANDSHAKE))) |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 2093 | goto end; |
| 2094 | h1c->flags &= ~H1C_F_CS_WAIT_CONN; |
Christopher Faulet | a0883e6 | 2018-12-11 16:26:50 +0100 | [diff] [blame] | 2095 | h1_wake_stream_for_send(h1s); |
Christopher Faulet | 3b88b8d | 2018-10-26 17:36:03 +0200 | [diff] [blame] | 2096 | } |
| 2097 | |
Christopher Faulet | feb1174 | 2018-11-29 15:12:34 +0100 | [diff] [blame] | 2098 | if (!h1s) { |
Christopher Faulet | 470438c | 2019-07-11 15:40:25 +0200 | [diff] [blame] | 2099 | if (h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN) || |
Christopher Faulet | e31a0f1 | 2019-12-05 10:23:37 +0100 | [diff] [blame] | 2100 | conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 2101 | goto release; |
Christopher Faulet | e31a0f1 | 2019-12-05 10:23:37 +0100 | [diff] [blame] | 2102 | if (!conn_is_back(conn) && (h1c->flags & H1C_F_CS_IDLE)) { |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 2103 | if (!h1s_create(h1c, NULL, NULL)) |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 2104 | goto release; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2105 | } |
Christopher Faulet | 1a7ad7a | 2018-12-04 16:10:44 +0100 | [diff] [blame] | 2106 | else |
Olivier Houchard | e728478 | 2018-12-06 18:54:54 +0100 | [diff] [blame] | 2107 | goto end; |
Christopher Faulet | feb1174 | 2018-11-29 15:12:34 +0100 | [diff] [blame] | 2108 | h1s = h1c->h1s; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2109 | } |
| 2110 | |
Christopher Faulet | feb1174 | 2018-11-29 15:12:34 +0100 | [diff] [blame] | 2111 | if (b_data(&h1c->ibuf) && h1s->csinfo.t_idle == -1) |
| 2112 | h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake; |
| 2113 | |
Willy Tarreau | c493c9c | 2019-06-03 14:18:22 +0200 | [diff] [blame] | 2114 | if (conn_xprt_read0_pending(conn)) |
| 2115 | h1s->flags |= H1S_F_REOS; |
| 2116 | |
Willy Tarreau | fbdf90a | 2019-06-03 13:42:54 +0200 | [diff] [blame] | 2117 | if (!h1s_data_pending(h1s) && h1s && h1s->cs && h1s->cs->data_cb->wake && |
Willy Tarreau | c493c9c | 2019-06-03 14:18:22 +0200 | [diff] [blame] | 2118 | (h1s->flags & H1S_F_REOS || h1c->flags & H1C_F_CS_ERROR || |
Olivier Houchard | 32d75ed | 2019-01-14 17:27:23 +0100 | [diff] [blame] | 2119 | conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) { |
Olivier Houchard | 75159a9 | 2018-12-03 18:46:09 +0100 | [diff] [blame] | 2120 | if (h1c->flags & H1C_F_CS_ERROR || conn->flags & CO_FL_ERROR) |
Willy Tarreau | c493c9c | 2019-06-03 14:18:22 +0200 | [diff] [blame] | 2121 | h1s->cs->flags |= CS_FL_ERROR; |
Olivier Houchard | 75159a9 | 2018-12-03 18:46:09 +0100 | [diff] [blame] | 2122 | h1s->cs->data_cb->wake(h1s->cs); |
| 2123 | } |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 2124 | end: |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 2125 | if (h1c->task) { |
| 2126 | h1c->task->expire = TICK_ETERNITY; |
| 2127 | if (b_data(&h1c->obuf)) { |
Christopher Faulet | 666a0c4 | 2019-01-08 11:12:04 +0100 | [diff] [blame] | 2128 | h1c->task->expire = tick_add(now_ms, ((h1c->flags & (H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTDOWN)) |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 2129 | ? h1c->shut_timeout |
| 2130 | : h1c->timeout)); |
| 2131 | task_queue(h1c->task); |
| 2132 | } |
| 2133 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2134 | return 0; |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 2135 | |
| 2136 | release: |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 2137 | h1_release(h1c); |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 2138 | return -1; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2139 | } |
| 2140 | |
| 2141 | static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status) |
| 2142 | { |
| 2143 | struct h1c *h1c = ctx; |
| 2144 | int ret = 0; |
| 2145 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2146 | if (!(h1c->wait_event.events & SUB_RETRY_SEND)) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2147 | ret = h1_send(h1c); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2148 | if (!(h1c->wait_event.events & SUB_RETRY_RECV)) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2149 | ret |= h1_recv(h1c); |
Christopher Faulet | 81d4843 | 2018-11-19 21:22:43 +0100 | [diff] [blame] | 2150 | if (ret || !h1c->h1s) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2151 | h1_process(h1c); |
| 2152 | return NULL; |
| 2153 | } |
| 2154 | |
Olivier Houchard | 9a86fcb | 2018-12-11 16:47:14 +0100 | [diff] [blame] | 2155 | static void h1_reset(struct connection *conn) |
| 2156 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 2157 | struct h1c *h1c = conn->ctx; |
Olivier Houchard | 9a86fcb | 2018-12-11 16:47:14 +0100 | [diff] [blame] | 2158 | |
| 2159 | /* Reset the flags, and let the mux know we're waiting for a connection */ |
| 2160 | h1c->flags = H1C_F_CS_WAIT_CONN; |
| 2161 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2162 | |
| 2163 | static int h1_wake(struct connection *conn) |
| 2164 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 2165 | struct h1c *h1c = conn->ctx; |
Olivier Houchard | 75159a9 | 2018-12-03 18:46:09 +0100 | [diff] [blame] | 2166 | int ret; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2167 | |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 2168 | h1_send(h1c); |
Olivier Houchard | 75159a9 | 2018-12-03 18:46:09 +0100 | [diff] [blame] | 2169 | ret = h1_process(h1c); |
| 2170 | if (ret == 0) { |
| 2171 | struct h1s *h1s = h1c->h1s; |
| 2172 | |
| 2173 | if (h1s && h1s->cs && h1s->cs->data_cb->wake) |
| 2174 | ret = h1s->cs->data_cb->wake(h1s->cs); |
| 2175 | } |
| 2176 | return ret; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2177 | } |
| 2178 | |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 2179 | /* Connection timeout management. The principle is that if there's no receipt |
| 2180 | * nor sending for a certain amount of time, the connection is closed. |
| 2181 | */ |
| 2182 | static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state) |
| 2183 | { |
| 2184 | struct h1c *h1c = context; |
| 2185 | int expired = tick_is_expired(t->expire, now_ms); |
| 2186 | |
| 2187 | if (!expired && h1c) |
| 2188 | return t; |
| 2189 | |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 2190 | task_destroy(t); |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 2191 | |
| 2192 | if (!h1c) { |
| 2193 | /* resources were already deleted */ |
| 2194 | return NULL; |
| 2195 | } |
| 2196 | |
| 2197 | h1c->task = NULL; |
| 2198 | /* If a stream is still attached to the mux, just set an error and wait |
| 2199 | * for the stream's timeout. Otherwise, release the mux. This is only ok |
| 2200 | * because same timeouts are used. |
| 2201 | */ |
| 2202 | if (h1c->h1s && h1c->h1s->cs) |
| 2203 | h1c->flags |= H1C_F_CS_ERROR; |
| 2204 | else |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 2205 | h1_release(h1c); |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 2206 | return NULL; |
| 2207 | } |
| 2208 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2209 | /*******************************************/ |
| 2210 | /* functions below are used by the streams */ |
| 2211 | /*******************************************/ |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 2212 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2213 | /* |
| 2214 | * Attach a new stream to a connection |
| 2215 | * (Used for outgoing connections) |
| 2216 | */ |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 2217 | static struct conn_stream *h1_attach(struct connection *conn, struct session *sess) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2218 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 2219 | struct h1c *h1c = conn->ctx; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2220 | struct conn_stream *cs = NULL; |
| 2221 | struct h1s *h1s; |
| 2222 | |
| 2223 | if (h1c->flags & H1C_F_CS_ERROR) |
| 2224 | goto end; |
| 2225 | |
| 2226 | cs = cs_new(h1c->conn); |
| 2227 | if (!cs) |
| 2228 | goto end; |
| 2229 | |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 2230 | h1s = h1s_create(h1c, cs, sess); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2231 | if (h1s == NULL) |
| 2232 | goto end; |
| 2233 | |
| 2234 | return cs; |
| 2235 | end: |
| 2236 | cs_free(cs); |
| 2237 | return NULL; |
| 2238 | } |
| 2239 | |
| 2240 | /* Retrieves a valid conn_stream from this connection, or returns NULL. For |
| 2241 | * this mux, it's easy as we can only store a single conn_stream. |
| 2242 | */ |
| 2243 | static const struct conn_stream *h1_get_first_cs(const struct connection *conn) |
| 2244 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 2245 | struct h1c *h1c = conn->ctx; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2246 | struct h1s *h1s = h1c->h1s; |
| 2247 | |
| 2248 | if (h1s) |
| 2249 | return h1s->cs; |
| 2250 | |
| 2251 | return NULL; |
| 2252 | } |
| 2253 | |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 2254 | static void h1_destroy(void *ctx) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2255 | { |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 2256 | struct h1c *h1c = ctx; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2257 | |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 2258 | if (!h1c->h1s || !h1c->conn || h1c->conn->ctx != h1c) |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 2259 | h1_release(h1c); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2260 | } |
| 2261 | |
| 2262 | /* |
| 2263 | * Detach the stream from the connection and possibly release the connection. |
| 2264 | */ |
| 2265 | static void h1_detach(struct conn_stream *cs) |
| 2266 | { |
| 2267 | struct h1s *h1s = cs->ctx; |
| 2268 | struct h1c *h1c; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 2269 | struct session *sess; |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 2270 | int is_not_first; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2271 | |
| 2272 | cs->ctx = NULL; |
| 2273 | if (!h1s) |
| 2274 | return; |
| 2275 | |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 2276 | sess = h1s->sess; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2277 | h1c = h1s->h1c; |
| 2278 | h1s->cs = NULL; |
| 2279 | |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 2280 | is_not_first = h1s->flags & H1S_F_NOT_FIRST; |
| 2281 | h1s_destroy(h1s); |
| 2282 | |
Christopher Faulet | e31a0f1 | 2019-12-05 10:23:37 +0100 | [diff] [blame] | 2283 | if (conn_is_back(h1c->conn) && (h1c->flags & H1C_F_CS_IDLE)) { |
Christopher Faulet | 0bf28f8 | 2019-07-19 14:51:06 +0200 | [diff] [blame] | 2284 | /* If there are any excess server data in the input buffer, |
| 2285 | * release it and close the connection ASAP (some data may |
| 2286 | * remain in the output buffer). This happens if a server sends |
| 2287 | * invalid responses. So in such case, we don't want to reuse |
| 2288 | * the connection |
Christopher Faulet | 39e679b | 2019-07-19 11:34:08 +0200 | [diff] [blame] | 2289 | */ |
Christopher Faulet | 0bf28f8 | 2019-07-19 14:51:06 +0200 | [diff] [blame] | 2290 | if (b_data(&h1c->ibuf)) { |
| 2291 | h1_release_buf(h1c, &h1c->ibuf); |
Christopher Faulet | e31a0f1 | 2019-12-05 10:23:37 +0100 | [diff] [blame] | 2292 | h1c->flags = (h1c->flags & ~H1C_F_CS_IDLE) | H1C_F_CS_SHUTW_NOW; |
Christopher Faulet | 0bf28f8 | 2019-07-19 14:51:06 +0200 | [diff] [blame] | 2293 | goto release; |
| 2294 | } |
Christopher Faulet | 39e679b | 2019-07-19 11:34:08 +0200 | [diff] [blame] | 2295 | |
Christopher Faulet | 9400a39 | 2018-11-23 23:10:39 +0100 | [diff] [blame] | 2296 | /* Never ever allow to reuse a connection from a non-reuse backend */ |
Olivier Houchard | 44d5914 | 2018-12-13 18:46:22 +0100 | [diff] [blame] | 2297 | if ((h1c->px->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR) |
Christopher Faulet | 9400a39 | 2018-11-23 23:10:39 +0100 | [diff] [blame] | 2298 | h1c->conn->flags |= CO_FL_PRIVATE; |
| 2299 | |
Olivier Houchard | 44d5914 | 2018-12-13 18:46:22 +0100 | [diff] [blame] | 2300 | if (!(h1c->conn->owner)) { |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 2301 | h1c->conn->owner = sess; |
Olivier Houchard | 351411f | 2018-12-27 17:20:54 +0100 | [diff] [blame] | 2302 | if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) { |
| 2303 | h1c->conn->owner = NULL; |
| 2304 | if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn)) |
| 2305 | /* The server doesn't want it, let's kill the connection right away */ |
Olivier Houchard | 109ba13 | 2020-02-14 13:23:45 +0100 | [diff] [blame] | 2306 | h1c->conn->mux->destroy(h1c); |
Olivier Houchard | 351411f | 2018-12-27 17:20:54 +0100 | [diff] [blame] | 2307 | else |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 2308 | tasklet_wakeup(h1c->wait_event.tasklet); |
Olivier Houchard | 351411f | 2018-12-27 17:20:54 +0100 | [diff] [blame] | 2309 | return; |
Olivier Houchard | 351411f | 2018-12-27 17:20:54 +0100 | [diff] [blame] | 2310 | } |
Olivier Houchard | 44d5914 | 2018-12-13 18:46:22 +0100 | [diff] [blame] | 2311 | } |
Olivier Houchard | a4d4fdf | 2018-12-14 19:27:06 +0100 | [diff] [blame] | 2312 | if (h1c->conn->owner == sess) { |
| 2313 | int ret = session_check_idle_conn(sess, h1c->conn); |
| 2314 | if (ret == -1) |
| 2315 | /* The connection got destroyed, let's leave */ |
| 2316 | return; |
| 2317 | else if (ret == 1) { |
| 2318 | /* The connection was added to the server list, |
| 2319 | * wake the task so we can subscribe to events |
| 2320 | */ |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 2321 | tasklet_wakeup(h1c->wait_event.tasklet); |
Olivier Houchard | a4d4fdf | 2018-12-14 19:27:06 +0100 | [diff] [blame] | 2322 | return; |
| 2323 | } |
| 2324 | } |
Christopher Faulet | 9400a39 | 2018-11-23 23:10:39 +0100 | [diff] [blame] | 2325 | /* we're in keep-alive with an idle connection, monitor it if not already done */ |
Olivier Houchard | 44d5914 | 2018-12-13 18:46:22 +0100 | [diff] [blame] | 2326 | if (LIST_ISEMPTY(&h1c->conn->list)) { |
Christopher Faulet | 9400a39 | 2018-11-23 23:10:39 +0100 | [diff] [blame] | 2327 | struct server *srv = objt_server(h1c->conn->target); |
| 2328 | |
| 2329 | if (srv) { |
| 2330 | if (h1c->conn->flags & CO_FL_PRIVATE) |
| 2331 | LIST_ADD(&srv->priv_conns[tid], &h1c->conn->list); |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 2332 | else if (is_not_first) |
Christopher Faulet | 9400a39 | 2018-11-23 23:10:39 +0100 | [diff] [blame] | 2333 | LIST_ADD(&srv->safe_conns[tid], &h1c->conn->list); |
| 2334 | else |
| 2335 | LIST_ADD(&srv->idle_conns[tid], &h1c->conn->list); |
| 2336 | } |
| 2337 | } |
| 2338 | } |
| 2339 | |
Christopher Faulet | 0bf28f8 | 2019-07-19 14:51:06 +0200 | [diff] [blame] | 2340 | release: |
Christopher Faulet | 9fa93f6 | 2019-06-28 17:41:42 +0200 | [diff] [blame] | 2341 | /* We don't want to close right now unless the connection is in error or shut down for writes */ |
Christopher Faulet | 470438c | 2019-07-11 15:40:25 +0200 | [diff] [blame] | 2342 | if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN|H1C_F_UPG_H2C)) || |
| 2343 | (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) || |
| 2344 | ((h1c->flags & H1C_F_CS_SHUTW_NOW) && !b_data(&h1c->obuf)) || |
| 2345 | !h1c->conn->owner) |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 2346 | h1_release(h1c); |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 2347 | else { |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 2348 | tasklet_wakeup(h1c->wait_event.tasklet); |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 2349 | if (h1c->task) { |
| 2350 | h1c->task->expire = TICK_ETERNITY; |
| 2351 | if (b_data(&h1c->obuf)) { |
Christopher Faulet | 666a0c4 | 2019-01-08 11:12:04 +0100 | [diff] [blame] | 2352 | h1c->task->expire = tick_add(now_ms, ((h1c->flags & (H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTDOWN)) |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 2353 | ? h1c->shut_timeout |
| 2354 | : h1c->timeout)); |
| 2355 | task_queue(h1c->task); |
| 2356 | } |
| 2357 | } |
| 2358 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2359 | } |
| 2360 | |
| 2361 | |
| 2362 | static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode) |
| 2363 | { |
| 2364 | struct h1s *h1s = cs->ctx; |
Christopher Faulet | 7f36636 | 2019-04-08 10:51:20 +0200 | [diff] [blame] | 2365 | struct h1c *h1c; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2366 | |
| 2367 | if (!h1s) |
| 2368 | return; |
Christopher Faulet | 7f36636 | 2019-04-08 10:51:20 +0200 | [diff] [blame] | 2369 | h1c = h1s->h1c; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2370 | |
Christopher Faulet | 7f36636 | 2019-04-08 10:51:20 +0200 | [diff] [blame] | 2371 | if ((cs->flags & CS_FL_KILL_CONN) || (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) |
| 2372 | goto do_shutr; |
| 2373 | |
Christopher Faulet | 0ef372a | 2019-04-08 10:57:20 +0200 | [diff] [blame] | 2374 | if ((h1c->flags & H1C_F_UPG_H2C) || (h1s->flags & H1S_F_WANT_KAL)) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2375 | return; |
| 2376 | |
Christopher Faulet | 7f36636 | 2019-04-08 10:51:20 +0200 | [diff] [blame] | 2377 | do_shutr: |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2378 | /* NOTE: Be sure to handle abort (cf. h2_shutr) */ |
| 2379 | if (cs->flags & CS_FL_SHR) |
| 2380 | return; |
| 2381 | if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2382 | cs->conn->xprt->shutr(cs->conn, cs->conn->xprt_ctx, |
Christopher Faulet | f36e72b | 2019-12-05 11:18:31 +0100 | [diff] [blame] | 2383 | (mode == CS_SHR_DRAIN)); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2384 | } |
| 2385 | |
| 2386 | static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode) |
| 2387 | { |
| 2388 | struct h1s *h1s = cs->ctx; |
| 2389 | struct h1c *h1c; |
| 2390 | |
| 2391 | if (!h1s) |
| 2392 | return; |
| 2393 | h1c = h1s->h1c; |
| 2394 | |
Christopher Faulet | 7f36636 | 2019-04-08 10:51:20 +0200 | [diff] [blame] | 2395 | if ((cs->flags & CS_FL_KILL_CONN) || (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) |
| 2396 | goto do_shutw; |
Olivier Houchard | d2e88c7 | 2018-12-19 15:55:23 +0100 | [diff] [blame] | 2397 | |
Christopher Faulet | 0ef372a | 2019-04-08 10:57:20 +0200 | [diff] [blame] | 2398 | if ((h1c->flags & H1C_F_UPG_H2C) || |
| 2399 | ((h1s->flags & H1S_F_WANT_KAL) && h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2400 | return; |
| 2401 | |
Christopher Faulet | 7f36636 | 2019-04-08 10:51:20 +0200 | [diff] [blame] | 2402 | do_shutw: |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2403 | h1c->flags |= H1C_F_CS_SHUTW_NOW; |
| 2404 | if ((cs->flags & CS_FL_SHW) || b_data(&h1c->obuf)) |
| 2405 | return; |
| 2406 | |
Christopher Faulet | 666a0c4 | 2019-01-08 11:12:04 +0100 | [diff] [blame] | 2407 | h1_shutw_conn(cs->conn, mode); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2408 | } |
| 2409 | |
Christopher Faulet | 666a0c4 | 2019-01-08 11:12:04 +0100 | [diff] [blame] | 2410 | static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2411 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 2412 | struct h1c *h1c = conn->ctx; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2413 | |
Christopher Faulet | 666a0c4 | 2019-01-08 11:12:04 +0100 | [diff] [blame] | 2414 | conn_xprt_shutw(conn); |
| 2415 | conn_sock_shutw(conn, (mode == CS_SHW_NORMAL)); |
Christopher Faulet | f36e72b | 2019-12-05 11:18:31 +0100 | [diff] [blame] | 2416 | h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2417 | } |
| 2418 | |
| 2419 | /* Called from the upper layer, to unsubscribe to events */ |
| 2420 | static int h1_unsubscribe(struct conn_stream *cs, int event_type, void *param) |
| 2421 | { |
| 2422 | struct wait_event *sw; |
| 2423 | struct h1s *h1s = cs->ctx; |
| 2424 | |
| 2425 | if (!h1s) |
| 2426 | return 0; |
| 2427 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2428 | if (event_type & SUB_RETRY_RECV) { |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2429 | sw = param; |
Olivier Houchard | 00b8f7c | 2019-05-14 18:02:23 +0200 | [diff] [blame] | 2430 | BUG_ON(h1s->recv_wait != sw); |
| 2431 | sw->events &= ~SUB_RETRY_RECV; |
| 2432 | h1s->recv_wait = NULL; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2433 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2434 | if (event_type & SUB_RETRY_SEND) { |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2435 | sw = param; |
Olivier Houchard | 00b8f7c | 2019-05-14 18:02:23 +0200 | [diff] [blame] | 2436 | BUG_ON(h1s->send_wait != sw); |
| 2437 | sw->events &= ~SUB_RETRY_SEND; |
| 2438 | h1s->send_wait = NULL; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2439 | } |
| 2440 | return 0; |
| 2441 | } |
| 2442 | |
| 2443 | /* Called from the upper layer, to subscribe to events, such as being able to send */ |
| 2444 | static int h1_subscribe(struct conn_stream *cs, int event_type, void *param) |
| 2445 | { |
| 2446 | struct wait_event *sw; |
| 2447 | struct h1s *h1s = cs->ctx; |
| 2448 | |
| 2449 | if (!h1s) |
| 2450 | return -1; |
| 2451 | |
| 2452 | switch (event_type) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2453 | case SUB_RETRY_RECV: |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2454 | sw = param; |
Olivier Houchard | 00b8f7c | 2019-05-14 18:02:23 +0200 | [diff] [blame] | 2455 | BUG_ON(h1s->recv_wait != NULL || (sw->events & SUB_RETRY_RECV)); |
| 2456 | sw->events |= SUB_RETRY_RECV; |
| 2457 | h1s->recv_wait = sw; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2458 | return 0; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2459 | case SUB_RETRY_SEND: |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2460 | sw = param; |
Olivier Houchard | 00b8f7c | 2019-05-14 18:02:23 +0200 | [diff] [blame] | 2461 | BUG_ON(h1s->send_wait != NULL || (sw->events & SUB_RETRY_SEND)); |
| 2462 | sw->events |= SUB_RETRY_SEND; |
| 2463 | h1s->send_wait = sw; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2464 | return 0; |
| 2465 | default: |
| 2466 | break; |
| 2467 | } |
| 2468 | return -1; |
| 2469 | } |
| 2470 | |
| 2471 | /* Called from the upper layer, to receive data */ |
| 2472 | static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags) |
| 2473 | { |
| 2474 | struct h1s *h1s = cs->ctx; |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 2475 | struct h1c *h1c = h1s->h1c; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2476 | size_t ret = 0; |
| 2477 | |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 2478 | if (!(h1c->flags & H1C_F_IN_ALLOC)) |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 2479 | ret = h1_process_input(h1c, buf, count); |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 2480 | |
Christopher Faulet | e18777b | 2019-04-16 16:46:36 +0200 | [diff] [blame] | 2481 | if (flags & CO_RFL_BUF_FLUSH) { |
| 2482 | struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res); |
| 2483 | |
Christopher Faulet | afadc9a | 2020-07-03 14:51:15 +0200 | [diff] [blame] | 2484 | if (h1m->state == H1_MSG_TUNNEL || (h1m->state == H1_MSG_DATA && h1m->curr_len)) |
Christopher Faulet | e18777b | 2019-04-16 16:46:36 +0200 | [diff] [blame] | 2485 | h1s->flags |= H1S_F_BUF_FLUSH; |
| 2486 | } |
Christopher Faulet | d44ad5b | 2018-11-19 21:52:12 +0100 | [diff] [blame] | 2487 | else if (ret > 0 || (h1s->flags & H1S_F_SPLICED_DATA)) { |
Christopher Faulet | 0528ae2 | 2020-07-03 15:12:00 +0200 | [diff] [blame] | 2488 | if (ret) |
| 2489 | h1s->flags &= ~H1S_F_SPLICED_DATA; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2490 | if (!(h1c->wait_event.events & SUB_RETRY_RECV)) |
Christopher Faulet | d2a762f | 2020-07-24 16:31:14 +0200 | [diff] [blame] | 2491 | tasklet_wakeup(h1c->wait_event.tasklet); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2492 | } |
| 2493 | return ret; |
| 2494 | } |
| 2495 | |
| 2496 | |
| 2497 | /* Called from the upper layer, to send data */ |
| 2498 | static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags) |
| 2499 | { |
| 2500 | struct h1s *h1s = cs->ctx; |
| 2501 | struct h1c *h1c; |
Christopher Faulet | 5d37dac | 2018-11-22 10:58:42 +0100 | [diff] [blame] | 2502 | size_t total = 0; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2503 | |
| 2504 | if (!h1s) |
Christopher Faulet | 5d37dac | 2018-11-22 10:58:42 +0100 | [diff] [blame] | 2505 | return 0; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2506 | |
| 2507 | h1c = h1s->h1c; |
Christopher Faulet | 3b88b8d | 2018-10-26 17:36:03 +0200 | [diff] [blame] | 2508 | if (h1c->flags & H1C_F_CS_WAIT_CONN) |
| 2509 | return 0; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2510 | |
Christopher Faulet | c31872f | 2019-06-04 22:09:36 +0200 | [diff] [blame] | 2511 | while (count) { |
Christopher Faulet | 5d37dac | 2018-11-22 10:58:42 +0100 | [diff] [blame] | 2512 | size_t ret = 0; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2513 | |
Christopher Faulet | 5d37dac | 2018-11-22 10:58:42 +0100 | [diff] [blame] | 2514 | if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC))) |
| 2515 | ret = h1_process_output(h1c, buf, count); |
| 2516 | if (!ret) |
| 2517 | break; |
| 2518 | total += ret; |
Christopher Faulet | c31872f | 2019-06-04 22:09:36 +0200 | [diff] [blame] | 2519 | count -= ret; |
Olivier Houchard | a101286 | 2020-01-15 19:13:32 +0100 | [diff] [blame] | 2520 | if ((h1c->wait_event.events & SUB_RETRY_SEND) || !h1_send(h1c)) |
Christopher Faulet | 5d37dac | 2018-11-22 10:58:42 +0100 | [diff] [blame] | 2521 | break; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2522 | } |
Christopher Faulet | f96c322 | 2018-11-20 18:38:01 +0100 | [diff] [blame] | 2523 | |
Christopher Faulet | 5d37dac | 2018-11-22 10:58:42 +0100 | [diff] [blame] | 2524 | return total; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2525 | } |
| 2526 | |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 2527 | #if defined(USE_LINUX_SPLICE) |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 2528 | /* Send and get, using splicing */ |
| 2529 | static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count) |
| 2530 | { |
| 2531 | struct h1s *h1s = cs->ctx; |
| 2532 | struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res); |
| 2533 | int ret = 0; |
| 2534 | |
Christopher Faulet | 2c411be | 2019-11-05 16:24:27 +0100 | [diff] [blame] | 2535 | if ((h1m->flags & H1_MF_CHNK) || (h1m->state != H1_MSG_DATA && h1m->state != H1_MSG_TUNNEL)) { |
Christopher Faulet | e18777b | 2019-04-16 16:46:36 +0200 | [diff] [blame] | 2536 | h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA); |
Christopher Faulet | 1146f97 | 2019-05-29 14:35:24 +0200 | [diff] [blame] | 2537 | if (!(h1s->h1c->wait_event.events & SUB_RETRY_RECV)) |
| 2538 | cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_RECV, &h1s->h1c->wait_event); |
Christopher Faulet | e18777b | 2019-04-16 16:46:36 +0200 | [diff] [blame] | 2539 | goto end; |
| 2540 | } |
| 2541 | |
Willy Tarreau | fbdf90a | 2019-06-03 13:42:54 +0200 | [diff] [blame] | 2542 | if (h1s_data_pending(h1s)) { |
Christopher Faulet | d44ad5b | 2018-11-19 21:52:12 +0100 | [diff] [blame] | 2543 | h1s->flags |= H1S_F_BUF_FLUSH; |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 2544 | goto end; |
Christopher Faulet | d44ad5b | 2018-11-19 21:52:12 +0100 | [diff] [blame] | 2545 | } |
| 2546 | |
| 2547 | h1s->flags &= ~H1S_F_BUF_FLUSH; |
| 2548 | h1s->flags |= H1S_F_SPLICED_DATA; |
Christopher Faulet | 8e8168a | 2020-07-03 15:02:25 +0200 | [diff] [blame] | 2549 | |
| 2550 | if (!h1_recv_allowed(h1s->h1c)) |
| 2551 | goto end; |
| 2552 | |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 2553 | if (h1m->state == H1_MSG_DATA && count > h1m->curr_len) |
| 2554 | count = h1m->curr_len; |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2555 | ret = cs->conn->xprt->rcv_pipe(cs->conn, cs->conn->xprt_ctx, pipe, count); |
Christopher Faulet | 290988a | 2020-07-24 16:13:12 +0200 | [diff] [blame^] | 2556 | if (ret <= 0) |
| 2557 | h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA); |
| 2558 | else if (h1m->state == H1_MSG_DATA) { |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 2559 | h1m->curr_len -= ret; |
Christopher Faulet | e18777b | 2019-04-16 16:46:36 +0200 | [diff] [blame] | 2560 | if (!h1m->curr_len) |
| 2561 | h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA); |
| 2562 | } |
| 2563 | |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 2564 | end: |
Christopher Faulet | 038ad81 | 2019-04-17 11:03:22 +0200 | [diff] [blame] | 2565 | if (conn_xprt_read0_pending(cs->conn)) { |
Willy Tarreau | c493c9c | 2019-06-03 14:18:22 +0200 | [diff] [blame] | 2566 | h1s->flags |= H1S_F_REOS; |
Christopher Faulet | ac4778c | 2019-11-15 11:14:23 +0100 | [diff] [blame] | 2567 | h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA); |
Christopher Faulet | 038ad81 | 2019-04-17 11:03:22 +0200 | [diff] [blame] | 2568 | } |
Willy Tarreau | 7e9dd73 | 2020-01-17 16:19:34 +0100 | [diff] [blame] | 2569 | |
Christopher Faulet | e0ca6ad | 2020-07-07 10:56:40 +0200 | [diff] [blame] | 2570 | if ((h1s->flags & H1S_F_REOS) || |
| 2571 | (h1m->state != H1_MSG_TUNNEL && h1m->state != H1_MSG_DATA) || |
Christopher Faulet | afadc9a | 2020-07-03 14:51:15 +0200 | [diff] [blame] | 2572 | (h1m->state == H1_MSG_DATA && !h1m->curr_len)) |
Willy Tarreau | 7e9dd73 | 2020-01-17 16:19:34 +0100 | [diff] [blame] | 2573 | cs->flags &= ~CS_FL_MAY_SPLICE; |
| 2574 | |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 2575 | return ret; |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 2576 | } |
| 2577 | |
| 2578 | static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe) |
| 2579 | { |
| 2580 | struct h1s *h1s = cs->ctx; |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 2581 | int ret = 0; |
| 2582 | |
| 2583 | if (b_data(&h1s->h1c->obuf)) |
| 2584 | goto end; |
| 2585 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2586 | ret = cs->conn->xprt->snd_pipe(cs->conn, cs->conn->xprt_ctx, pipe); |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 2587 | end: |
Christopher Faulet | d44ad5b | 2018-11-19 21:52:12 +0100 | [diff] [blame] | 2588 | if (pipe->data) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2589 | if (!(h1s->h1c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2590 | cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_SEND, &h1s->h1c->wait_event); |
Christopher Faulet | d44ad5b | 2018-11-19 21:52:12 +0100 | [diff] [blame] | 2591 | } |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 2592 | return ret; |
| 2593 | } |
| 2594 | #endif |
| 2595 | |
Olivier Houchard | 198d129 | 2019-10-25 16:19:26 +0200 | [diff] [blame] | 2596 | static int h1_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output) |
| 2597 | { |
| 2598 | int ret = 0; |
| 2599 | switch (mux_ctl) { |
| 2600 | case MUX_STATUS: |
| 2601 | if (conn->flags & CO_FL_CONNECTED) |
| 2602 | ret |= MUX_STATUS_READY; |
| 2603 | return ret; |
| 2604 | default: |
| 2605 | return -1; |
| 2606 | } |
| 2607 | } |
| 2608 | |
Olivier Houchard | a8f6b43 | 2018-12-21 15:20:29 +0100 | [diff] [blame] | 2609 | /* for debugging with CLI's "show fd" command */ |
| 2610 | static void h1_show_fd(struct buffer *msg, struct connection *conn) |
| 2611 | { |
| 2612 | struct h1c *h1c = conn->ctx; |
| 2613 | struct h1s *h1s = h1c->h1s; |
| 2614 | |
Christopher Faulet | f376a31 | 2019-01-04 15:16:06 +0100 | [diff] [blame] | 2615 | chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u", |
| 2616 | h1c->flags, h1c->wait_event.events, |
Olivier Houchard | a8f6b43 | 2018-12-21 15:20:29 +0100 | [diff] [blame] | 2617 | (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf), |
| 2618 | (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf), |
| 2619 | (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf), |
| 2620 | (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf)); |
| 2621 | |
| 2622 | if (h1s) { |
| 2623 | char *method; |
| 2624 | |
| 2625 | if (h1s->meth < HTTP_METH_OTHER) |
| 2626 | method = http_known_methods[h1s->meth].ptr; |
| 2627 | else |
| 2628 | method = "UNKNOWN"; |
| 2629 | chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .req.state=%s .res.state=%s" |
| 2630 | " .meth=%s status=%d", |
| 2631 | h1s, h1s->flags, |
| 2632 | h1m_state_str(h1s->req.state), |
| 2633 | h1m_state_str(h1s->res.state), method, h1s->status); |
| 2634 | if (h1s->cs) |
| 2635 | chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p", |
| 2636 | h1s->cs->flags, h1s->cs->data); |
| 2637 | } |
Christopher Faulet | a99ff4d | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 2638 | } |
| 2639 | |
| 2640 | |
| 2641 | /* Add an entry in the headers map. Returns -1 on error and 0 on success. */ |
| 2642 | static int add_hdr_case_adjust(const char *from, const char *to, char **err) |
| 2643 | { |
| 2644 | struct h1_hdr_entry *entry; |
| 2645 | |
| 2646 | /* Be sure there is a non-empty <to> */ |
| 2647 | if (!strlen(to)) { |
| 2648 | memprintf(err, "expect <to>"); |
| 2649 | return -1; |
| 2650 | } |
| 2651 | |
| 2652 | /* Be sure only the case differs between <from> and <to> */ |
| 2653 | if (strcasecmp(from, to)) { |
| 2654 | memprintf(err, "<from> and <to> must not differ execpt the case"); |
| 2655 | return -1; |
| 2656 | } |
| 2657 | |
| 2658 | /* Be sure <from> does not already existsin the tree */ |
| 2659 | if (ebis_lookup(&hdrs_map.map, from)) { |
| 2660 | memprintf(err, "duplicate entry '%s'", from); |
| 2661 | return -1; |
| 2662 | } |
| 2663 | |
| 2664 | /* Create the entry and insert it in the tree */ |
| 2665 | entry = malloc(sizeof(*entry)); |
| 2666 | if (!entry) { |
| 2667 | memprintf(err, "out of memory"); |
| 2668 | return -1; |
| 2669 | } |
| 2670 | |
| 2671 | entry->node.key = strdup(from); |
| 2672 | entry->name.ptr = strdup(to); |
| 2673 | entry->name.len = strlen(to); |
| 2674 | if (!entry->node.key || !entry->name.ptr) { |
| 2675 | free(entry->node.key); |
| 2676 | free(entry->name.ptr); |
| 2677 | free(entry); |
| 2678 | memprintf(err, "out of memory"); |
| 2679 | return -1; |
| 2680 | } |
| 2681 | ebis_insert(&hdrs_map.map, &entry->node); |
| 2682 | return 0; |
| 2683 | } |
| 2684 | |
| 2685 | static void h1_hdeaders_case_adjust_deinit() |
| 2686 | { |
| 2687 | struct ebpt_node *node, *next; |
| 2688 | struct h1_hdr_entry *entry; |
| 2689 | |
| 2690 | node = ebpt_first(&hdrs_map.map); |
| 2691 | while (node) { |
| 2692 | next = ebpt_next(node); |
| 2693 | ebpt_delete(node); |
| 2694 | entry = container_of(node, struct h1_hdr_entry, node); |
| 2695 | free(entry->node.key); |
| 2696 | free(entry->name.ptr); |
| 2697 | free(entry); |
| 2698 | node = next; |
| 2699 | } |
| 2700 | free(hdrs_map.name); |
Olivier Houchard | a8f6b43 | 2018-12-21 15:20:29 +0100 | [diff] [blame] | 2701 | } |
| 2702 | |
Christopher Faulet | a99ff4d | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 2703 | static int cfg_h1_headers_case_adjust_postparser() |
| 2704 | { |
| 2705 | FILE *file = NULL; |
| 2706 | char *c, *key_beg, *key_end, *value_beg, *value_end; |
| 2707 | char *err; |
| 2708 | int rc, line = 0, err_code = 0; |
| 2709 | |
| 2710 | if (!hdrs_map.name) |
| 2711 | goto end; |
| 2712 | |
| 2713 | file = fopen(hdrs_map.name, "r"); |
| 2714 | if (!file) { |
| 2715 | ha_alert("config : h1-outgoing-headers-case-adjust-file '%s': failed to open file.\n", |
| 2716 | hdrs_map.name); |
| 2717 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2718 | goto end; |
| 2719 | } |
| 2720 | |
| 2721 | /* now parse all lines. The file may contain only two header name per |
| 2722 | * line, separated by spaces. All heading and trailing spaces will be |
| 2723 | * ignored. Lines starting with a # are ignored. |
| 2724 | */ |
| 2725 | while (fgets(trash.area, trash.size, file) != NULL) { |
| 2726 | line++; |
| 2727 | c = trash.area; |
| 2728 | |
| 2729 | /* strip leading spaces and tabs */ |
| 2730 | while (*c == ' ' || *c == '\t') |
| 2731 | c++; |
| 2732 | |
| 2733 | /* ignore emptu lines, or lines beginning with a dash */ |
| 2734 | if (*c == '#' || *c == '\0' || *c == '\r' || *c == '\n') |
| 2735 | continue; |
| 2736 | |
| 2737 | /* look for the end of the key */ |
| 2738 | key_beg = c; |
| 2739 | while (*c != '\0' && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r') |
| 2740 | c++; |
| 2741 | key_end = c; |
| 2742 | |
| 2743 | /* strip middle spaces and tabs */ |
| 2744 | while (*c == ' ' || *c == '\t') |
| 2745 | c++; |
| 2746 | |
| 2747 | /* look for the end of the value, it is the end of the line */ |
| 2748 | value_beg = c; |
| 2749 | while (*c && *c != '\n' && *c != '\r') |
| 2750 | c++; |
| 2751 | value_end = c; |
| 2752 | |
| 2753 | /* trim possibly trailing spaces and tabs */ |
| 2754 | while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t')) |
| 2755 | value_end--; |
| 2756 | |
| 2757 | /* set final \0 and check entries */ |
| 2758 | *key_end = '\0'; |
| 2759 | *value_end = '\0'; |
| 2760 | |
| 2761 | err = NULL; |
| 2762 | rc = add_hdr_case_adjust(key_beg, value_beg, &err); |
| 2763 | if (rc < 0) { |
Christopher Faulet | a99ff4d | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 2764 | ha_alert("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n", |
| 2765 | hdrs_map.name, err, line); |
| 2766 | err_code |= ERR_ALERT | ERR_FATAL; |
Christopher Faulet | 27f2017 | 2019-07-30 16:51:42 +0200 | [diff] [blame] | 2767 | free(err); |
Christopher Faulet | a99ff4d | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 2768 | goto end; |
| 2769 | } |
| 2770 | if (rc > 0) { |
Christopher Faulet | a99ff4d | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 2771 | ha_warning("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n", |
| 2772 | hdrs_map.name, err, line); |
| 2773 | err_code |= ERR_WARN; |
Christopher Faulet | 27f2017 | 2019-07-30 16:51:42 +0200 | [diff] [blame] | 2774 | free(err); |
Christopher Faulet | a99ff4d | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 2775 | } |
| 2776 | } |
| 2777 | |
| 2778 | end: |
| 2779 | if (file) |
| 2780 | fclose(file); |
| 2781 | hap_register_post_deinit(h1_hdeaders_case_adjust_deinit); |
| 2782 | return err_code; |
| 2783 | } |
| 2784 | |
| 2785 | |
| 2786 | /* config parser for global "h1-outgoing-header-case-adjust" */ |
| 2787 | static int cfg_parse_h1_header_case_adjust(char **args, int section_type, struct proxy *curpx, |
| 2788 | struct proxy *defpx, const char *file, int line, |
| 2789 | char **err) |
| 2790 | { |
| 2791 | if (too_many_args(2, args, err, NULL)) |
| 2792 | return -1; |
| 2793 | if (!*(args[1]) || !*(args[2])) { |
| 2794 | memprintf(err, "'%s' expects <from> and <to> as argument.", args[0]); |
| 2795 | return -1; |
| 2796 | } |
| 2797 | return add_hdr_case_adjust(args[1], args[2], err); |
| 2798 | } |
| 2799 | |
| 2800 | /* config parser for global "h1-outgoing-headers-case-adjust-file" */ |
| 2801 | static int cfg_parse_h1_headers_case_adjust_file(char **args, int section_type, struct proxy *curpx, |
| 2802 | struct proxy *defpx, const char *file, int line, |
| 2803 | char **err) |
| 2804 | { |
| 2805 | if (too_many_args(1, args, err, NULL)) |
| 2806 | return -1; |
| 2807 | if (!*(args[1])) { |
| 2808 | memprintf(err, "'%s' expects <file> as argument.", args[0]); |
| 2809 | return -1; |
| 2810 | } |
| 2811 | free(hdrs_map.name); |
| 2812 | hdrs_map.name = strdup(args[1]); |
| 2813 | return 0; |
| 2814 | } |
| 2815 | |
| 2816 | |
| 2817 | /* config keyword parsers */ |
| 2818 | static struct cfg_kw_list cfg_kws = {{ }, { |
| 2819 | { CFG_GLOBAL, "h1-case-adjust", cfg_parse_h1_header_case_adjust }, |
| 2820 | { CFG_GLOBAL, "h1-case-adjust-file", cfg_parse_h1_headers_case_adjust_file }, |
| 2821 | { 0, NULL, NULL }, |
| 2822 | } |
| 2823 | }; |
| 2824 | |
| 2825 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 2826 | REGISTER_CONFIG_POSTPARSER("h1-headers-map", cfg_h1_headers_case_adjust_postparser); |
| 2827 | |
| 2828 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2829 | /****************************************/ |
| 2830 | /* MUX initialization and instanciation */ |
| 2831 | /****************************************/ |
| 2832 | |
| 2833 | /* The mux operations */ |
Willy Tarreau | f77a158 | 2019-01-10 10:00:08 +0100 | [diff] [blame] | 2834 | static const struct mux_ops mux_h1_ops = { |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2835 | .init = h1_init, |
| 2836 | .wake = h1_wake, |
| 2837 | .attach = h1_attach, |
| 2838 | .get_first_cs = h1_get_first_cs, |
Christopher Faulet | feb1174 | 2018-11-29 15:12:34 +0100 | [diff] [blame] | 2839 | .get_cs_info = h1_get_cs_info, |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2840 | .detach = h1_detach, |
| 2841 | .destroy = h1_destroy, |
| 2842 | .avail_streams = h1_avail_streams, |
Willy Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 2843 | .used_streams = h1_used_streams, |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2844 | .rcv_buf = h1_rcv_buf, |
| 2845 | .snd_buf = h1_snd_buf, |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 2846 | #if defined(USE_LINUX_SPLICE) |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 2847 | .rcv_pipe = h1_rcv_pipe, |
| 2848 | .snd_pipe = h1_snd_pipe, |
| 2849 | #endif |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2850 | .subscribe = h1_subscribe, |
| 2851 | .unsubscribe = h1_unsubscribe, |
| 2852 | .shutr = h1_shutr, |
| 2853 | .shutw = h1_shutw, |
Olivier Houchard | a8f6b43 | 2018-12-21 15:20:29 +0100 | [diff] [blame] | 2854 | .show_fd = h1_show_fd, |
Olivier Houchard | 9a86fcb | 2018-12-11 16:47:14 +0100 | [diff] [blame] | 2855 | .reset = h1_reset, |
Olivier Houchard | 198d129 | 2019-10-25 16:19:26 +0200 | [diff] [blame] | 2856 | .ctl = h1_ctl, |
Christopher Faulet | 9f38f5a | 2019-04-03 09:53:32 +0200 | [diff] [blame] | 2857 | .flags = MX_FL_HTX, |
Willy Tarreau | 0a7a4fb | 2019-05-22 11:36:54 +0200 | [diff] [blame] | 2858 | .name = "H1", |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2859 | }; |
| 2860 | |
| 2861 | |
| 2862 | /* this mux registers default HTX proto */ |
| 2863 | static struct mux_proto_list mux_proto_htx = |
| 2864 | { .token = IST(""), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops }; |
| 2865 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 2866 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx); |
| 2867 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2868 | /* |
| 2869 | * Local variables: |
| 2870 | * c-indent-level: 8 |
| 2871 | * c-basic-offset: 8 |
| 2872 | * End: |
| 2873 | */ |