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