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