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