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