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> |
| 14 | |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 15 | #include <types/pipe.h> |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 16 | #include <types/proxy.h> |
| 17 | #include <types/session.h> |
| 18 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 19 | #include <proto/connection.h> |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 20 | #include <proto/h1.h> |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 21 | #include <proto/http_htx.h> |
| 22 | #include <proto/htx.h> |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 23 | #include <proto/log.h> |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 24 | #include <proto/stream.h> |
| 25 | #include <proto/stream_interface.h> |
| 26 | |
| 27 | /* |
| 28 | * H1 Connection flags (32 bits) |
| 29 | */ |
| 30 | #define H1C_F_NONE 0x00000000 |
| 31 | |
| 32 | /* Flags indicating why writing output data are blocked */ |
| 33 | #define H1C_F_OUT_ALLOC 0x00000001 /* mux is blocked on lack of output buffer */ |
| 34 | #define H1C_F_OUT_FULL 0x00000002 /* mux is blocked on output buffer full */ |
| 35 | /* 0x00000004 - 0x00000008 unused */ |
| 36 | |
| 37 | /* Flags indicating why reading input data are blocked. */ |
| 38 | #define H1C_F_IN_ALLOC 0x00000010 /* mux is blocked on lack of input buffer */ |
| 39 | #define H1C_F_IN_FULL 0x00000020 /* mux is blocked on input buffer full */ |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 40 | /* 0x00000040 - 0x00000800 unused */ |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 41 | |
| 42 | #define H1C_F_CS_ERROR 0x00001000 /* connection must be closed ASAP because an error occurred */ |
| 43 | #define H1C_F_CS_SHUTW_NOW 0x00002000 /* connection must be shut down for writes ASAP */ |
| 44 | #define H1C_F_CS_SHUTW 0x00004000 /* connection is already shut down */ |
Christopher Faulet | 3b88b8d | 2018-10-26 17:36:03 +0200 | [diff] [blame] | 45 | #define H1C_F_CS_WAIT_CONN 0x00008000 /* waiting for the connection establishment */ |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 46 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 47 | #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] | 48 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 49 | /* |
| 50 | * H1 Stream flags (32 bits) |
| 51 | */ |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 52 | #define H1S_F_NONE 0x00000000 |
| 53 | #define H1S_F_ERROR 0x00000001 /* An error occurred on the H1 stream */ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 54 | #define H1S_F_REQ_ERROR 0x00000002 /* An error occurred during the request parsing/xfer */ |
| 55 | #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] | 56 | /* 0x00000008 unused */ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 57 | #define H1S_F_WANT_KAL 0x00000010 |
| 58 | #define H1S_F_WANT_TUN 0x00000020 |
| 59 | #define H1S_F_WANT_CLO 0x00000040 |
| 60 | #define H1S_F_WANT_MSK 0x00000070 |
| 61 | #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] | 62 | #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] | 63 | #define H1S_F_SPLICED_DATA 0x00000200 /* Set when the kernel splicing is in used */ |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 64 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 65 | |
| 66 | /* H1 connection descriptor */ |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 67 | struct h1c { |
| 68 | struct connection *conn; |
| 69 | struct proxy *px; |
| 70 | uint32_t flags; /* Connection flags: H1C_F_* */ |
| 71 | |
| 72 | struct buffer ibuf; /* Input buffer to store data before parsing */ |
| 73 | struct buffer obuf; /* Output buffer to store data after reformatting */ |
| 74 | |
| 75 | struct buffer_wait buf_wait; /* Wait list for buffer allocation */ |
| 76 | struct wait_event wait_event; /* To be used if we're waiting for I/Os */ |
| 77 | |
| 78 | struct h1s *h1s; /* H1 stream descriptor */ |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | /* H1 stream descriptor */ |
| 82 | struct h1s { |
| 83 | struct h1c *h1c; |
| 84 | struct conn_stream *cs; |
| 85 | uint32_t flags; /* Connection flags: H1S_F_* */ |
| 86 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 87 | struct wait_event *recv_wait; /* Address of the wait_event the conn_stream associated is waiting on */ |
| 88 | 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] | 89 | |
| 90 | struct h1m req; |
| 91 | struct h1m res; |
| 92 | |
| 93 | enum http_meth_t meth; /* HTTP resquest method */ |
| 94 | uint16_t status; /* HTTP response status */ |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | /* the h1c and h1s pools */ |
| 98 | static struct pool_head *pool_head_h1c; |
| 99 | static struct pool_head *pool_head_h1s; |
| 100 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 101 | static int h1_recv(struct h1c *h1c); |
| 102 | static int h1_send(struct h1c *h1c); |
| 103 | static int h1_process(struct h1c *h1c); |
| 104 | static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short state); |
| 105 | static void h1_shutw_conn(struct connection *conn); |
| 106 | |
| 107 | /*****************************************************/ |
| 108 | /* functions below are for dynamic buffer management */ |
| 109 | /*****************************************************/ |
| 110 | /* |
| 111 | * Indicates whether or not the we may call the h1_recv() function to |
| 112 | * attempt to receive data into the buffer and/or parse pending data. The |
| 113 | * condition is a bit complex due to some API limits for now. The rules are the |
| 114 | * following : |
| 115 | * - if an error or a shutdown was detected on the connection and the buffer |
| 116 | * is empty, we must not attempt to receive |
| 117 | * - if the input buffer failed to be allocated, we must not try to receive |
| 118 | * and we know there is nothing pending |
| 119 | * - if no flag indicates a blocking condition, we may attempt to receive, |
| 120 | * regardless of whether the input buffer is full or not, so that only de |
| 121 | * receiving part decides whether or not to block. This is needed because |
| 122 | * the connection API indeed prevents us from re-enabling receipt that is |
| 123 | * already enabled in a polled state, so we must always immediately stop as |
| 124 | * soon as the mux can't proceed so as never to hit an end of read with data |
| 125 | * pending in the buffers. |
| 126 | * - otherwise must may not attempt to receive |
| 127 | */ |
| 128 | static inline int h1_recv_allowed(const struct h1c *h1c) |
| 129 | { |
| 130 | if (b_data(&h1c->ibuf) == 0 && |
Christopher Faulet | 7e346f3 | 2018-11-20 17:13:52 +0100 | [diff] [blame] | 131 | (h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTW) || |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 132 | h1c->conn->flags & CO_FL_ERROR || |
| 133 | conn_xprt_read0_pending(h1c->conn))) |
| 134 | return 0; |
| 135 | |
| 136 | if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL))) |
| 137 | return 1; |
| 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | /* |
| 143 | * Tries to grab a buffer and to re-enables processing on mux <target>. The h1 |
| 144 | * flags are used to figure what buffer was requested. It returns 1 if the |
| 145 | * allocation succeeds, in which case the connection is woken up, or 0 if it's |
| 146 | * impossible to wake up and we prefer to be woken up later. |
| 147 | */ |
| 148 | static int h1_buf_available(void *target) |
| 149 | { |
| 150 | struct h1c *h1c = target; |
| 151 | |
| 152 | if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc_margin(&h1c->ibuf, 0)) { |
| 153 | h1c->flags &= ~H1C_F_IN_ALLOC; |
| 154 | if (h1_recv_allowed(h1c)) |
| 155 | tasklet_wakeup(h1c->wait_event.task); |
| 156 | return 1; |
| 157 | } |
| 158 | |
| 159 | if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc_margin(&h1c->obuf, 0)) { |
| 160 | h1c->flags &= ~H1C_F_OUT_ALLOC; |
| 161 | tasklet_wakeup(h1c->wait_event.task); |
| 162 | return 1; |
| 163 | } |
| 164 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | /* |
| 169 | * Allocate a buffer. If if fails, it adds the mux in buffer wait queue. |
| 170 | */ |
| 171 | static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr) |
| 172 | { |
| 173 | struct buffer *buf = NULL; |
| 174 | |
| 175 | if (likely(LIST_ISEMPTY(&h1c->buf_wait.list)) && |
| 176 | unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) { |
| 177 | h1c->buf_wait.target = h1c; |
| 178 | h1c->buf_wait.wakeup_cb = h1_buf_available; |
| 179 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
| 180 | LIST_ADDQ(&buffer_wq, &h1c->buf_wait.list); |
| 181 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
| 182 | __conn_xprt_stop_recv(h1c->conn); |
| 183 | } |
| 184 | return buf; |
| 185 | } |
| 186 | |
| 187 | /* |
| 188 | * Release a buffer, if any, and try to wake up entities waiting in the buffer |
| 189 | * wait queue. |
| 190 | */ |
| 191 | static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr) |
| 192 | { |
| 193 | if (bptr->size) { |
| 194 | b_free(bptr); |
| 195 | offer_buffers(h1c->buf_wait.target, tasks_run_queue); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | static int h1_avail_streams(struct connection *conn) |
| 200 | { |
| 201 | struct h1c *h1c = conn->mux_ctx; |
| 202 | |
| 203 | return h1c->h1s ? 0 : 1; |
| 204 | } |
| 205 | |
| 206 | |
| 207 | /*****************************************************************/ |
| 208 | /* functions below are dedicated to the mux setup and management */ |
| 209 | /*****************************************************************/ |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 210 | static struct conn_stream *h1s_new_cs(struct h1s *h1s) |
| 211 | { |
| 212 | struct conn_stream *cs; |
| 213 | |
| 214 | cs = cs_new(h1s->h1c->conn); |
| 215 | if (!cs) |
| 216 | goto err; |
| 217 | h1s->cs = cs; |
| 218 | cs->ctx = h1s; |
| 219 | |
| 220 | if (h1s->flags & H1S_F_NOT_FIRST) |
| 221 | cs->flags |= CS_FL_NOT_FIRST; |
| 222 | |
| 223 | if (stream_create_from_cs(cs) < 0) |
| 224 | goto err; |
| 225 | return cs; |
| 226 | |
| 227 | err: |
| 228 | cs_free(cs); |
| 229 | h1s->cs = NULL; |
| 230 | return NULL; |
| 231 | } |
| 232 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 233 | static struct h1s *h1s_create(struct h1c *h1c, struct conn_stream *cs) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 234 | { |
| 235 | struct h1s *h1s; |
| 236 | |
| 237 | h1s = pool_alloc(pool_head_h1s); |
| 238 | if (!h1s) |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 239 | goto fail; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 240 | |
| 241 | h1s->h1c = h1c; |
| 242 | h1c->h1s = h1s; |
| 243 | |
| 244 | h1s->cs = NULL; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 245 | h1s->flags = H1S_F_NONE; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 246 | |
| 247 | h1s->recv_wait = NULL; |
| 248 | h1s->send_wait = NULL; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 249 | |
| 250 | h1m_init_req(&h1s->req); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 251 | h1s->req.flags |= H1_MF_NO_PHDR; |
| 252 | |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 253 | h1m_init_res(&h1s->res); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 254 | h1s->res.flags |= H1_MF_NO_PHDR; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 255 | |
| 256 | h1s->status = 0; |
| 257 | h1s->meth = HTTP_METH_OTHER; |
| 258 | |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 259 | if (h1c->flags & H1C_F_WAIT_NEXT_REQ) |
| 260 | h1s->flags |= H1S_F_NOT_FIRST; |
| 261 | h1c->flags &= ~H1C_F_WAIT_NEXT_REQ; |
| 262 | |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 263 | if (!conn_is_back(h1c->conn)) { |
| 264 | if (h1c->px->options2 & PR_O2_REQBUG_OK) |
| 265 | h1s->req.err_pos = -1; |
| 266 | } |
| 267 | else { |
| 268 | if (h1c->px->options2 & PR_O2_RSPBUG_OK) |
| 269 | h1s->res.err_pos = -1; |
| 270 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 271 | |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 272 | /* If a conn_stream already exists, attach it to this H1S. Otherwise we |
| 273 | * create a new one. |
| 274 | */ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 275 | if (cs) { |
| 276 | cs->ctx = h1s; |
| 277 | h1s->cs = cs; |
| 278 | } |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 279 | else { |
| 280 | cs = h1s_new_cs(h1s); |
| 281 | if (!cs) |
| 282 | goto fail; |
| 283 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 284 | return h1s; |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 285 | |
| 286 | fail: |
| 287 | pool_free(pool_head_h1s, h1s); |
| 288 | return NULL; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | static void h1s_destroy(struct h1s *h1s) |
| 292 | { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 293 | if (h1s) { |
| 294 | struct h1c *h1c = h1s->h1c; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 295 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 296 | h1c->h1s = NULL; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 297 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 298 | if (h1s->recv_wait != NULL) |
| 299 | h1s->recv_wait->wait_reason &= ~SUB_CAN_RECV; |
| 300 | if (h1s->send_wait != NULL) |
| 301 | h1s->send_wait->wait_reason &= ~SUB_CAN_SEND; |
| 302 | |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 303 | h1c->flags |= H1C_F_WAIT_NEXT_REQ; |
| 304 | if (h1s->flags & (H1S_F_REQ_ERROR|H1S_F_RES_ERROR)) |
| 305 | h1c->flags |= H1C_F_CS_ERROR; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 306 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 307 | cs_free(h1s->cs); |
| 308 | pool_free(pool_head_h1s, h1s); |
| 309 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | /* |
| 313 | * Initialize the mux once it's attached. It is expected that conn->mux_ctx |
| 314 | * points to the existing conn_stream (for outgoing connections) or NULL (for |
| 315 | * incoming ones). Returns < 0 on error. |
| 316 | */ |
| 317 | static int h1_init(struct connection *conn, struct proxy *proxy) |
| 318 | { |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 319 | struct h1c *h1c; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 320 | |
| 321 | h1c = pool_alloc(pool_head_h1c); |
| 322 | if (!h1c) |
| 323 | goto fail_h1c; |
| 324 | h1c->conn = conn; |
| 325 | h1c->px = proxy; |
| 326 | |
| 327 | h1c->flags = H1C_F_NONE; |
| 328 | h1c->ibuf = BUF_NULL; |
| 329 | h1c->obuf = BUF_NULL; |
| 330 | h1c->h1s = NULL; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 331 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 332 | LIST_INIT(&h1c->buf_wait.list); |
| 333 | h1c->wait_event.task = tasklet_new(); |
| 334 | if (!h1c->wait_event.task) |
| 335 | goto fail; |
| 336 | h1c->wait_event.task->process = h1_io_cb; |
| 337 | h1c->wait_event.task->context = h1c; |
| 338 | h1c->wait_event.wait_reason = 0; |
| 339 | |
Christopher Faulet | 3b88b8d | 2018-10-26 17:36:03 +0200 | [diff] [blame] | 340 | if (!(conn->flags & CO_FL_CONNECTED)) |
| 341 | h1c->flags |= H1C_F_CS_WAIT_CONN; |
| 342 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 343 | /* Always Create a new H1S */ |
| 344 | if (!h1s_create(h1c, conn->mux_ctx)) |
| 345 | goto fail; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 346 | |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 347 | conn->mux_ctx = h1c; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 348 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 349 | /* Try to read, if nothing is available yet we'll just subscribe */ |
| 350 | if (h1_recv(h1c)) |
| 351 | h1_process(h1c); |
| 352 | |
| 353 | /* mux->wake will be called soon to complete the operation */ |
| 354 | return 0; |
| 355 | |
| 356 | fail: |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 357 | if (h1c->wait_event.task) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 358 | tasklet_free(h1c->wait_event.task); |
| 359 | pool_free(pool_head_h1c, h1c); |
| 360 | fail_h1c: |
| 361 | return -1; |
| 362 | } |
| 363 | |
| 364 | |
| 365 | /* release function for a connection. This one should be called to free all |
| 366 | * resources allocated to the mux. |
| 367 | */ |
| 368 | static void h1_release(struct connection *conn) |
| 369 | { |
| 370 | struct h1c *h1c = conn->mux_ctx; |
| 371 | |
| 372 | LIST_DEL(&conn->list); |
| 373 | |
| 374 | if (h1c) { |
| 375 | if (!LIST_ISEMPTY(&h1c->buf_wait.list)) { |
| 376 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
| 377 | LIST_DEL(&h1c->buf_wait.list); |
| 378 | LIST_INIT(&h1c->buf_wait.list); |
| 379 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
| 380 | } |
| 381 | |
| 382 | h1_release_buf(h1c, &h1c->ibuf); |
| 383 | h1_release_buf(h1c, &h1c->obuf); |
| 384 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 385 | if (h1c->wait_event.task) |
| 386 | tasklet_free(h1c->wait_event.task); |
| 387 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 388 | h1s_destroy(h1c->h1s); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 389 | if (h1c->wait_event.wait_reason != 0) |
| 390 | conn->xprt->unsubscribe(conn, h1c->wait_event.wait_reason, |
| 391 | &h1c->wait_event); |
| 392 | pool_free(pool_head_h1c, h1c); |
| 393 | } |
| 394 | |
| 395 | conn->mux = NULL; |
| 396 | conn->mux_ctx = NULL; |
| 397 | |
| 398 | conn_stop_tracking(conn); |
| 399 | conn_full_close(conn); |
| 400 | if (conn->destroy_cb) |
| 401 | conn->destroy_cb(conn); |
| 402 | conn_free(conn); |
| 403 | } |
| 404 | |
| 405 | /******************************************************/ |
| 406 | /* functions below are for the H1 protocol processing */ |
| 407 | /******************************************************/ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 408 | /* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is |
| 409 | * greater or equal to 1.1 |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 410 | */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 411 | static void h1_parse_req_vsn(struct h1m *h1m, const union htx_sl *sl) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 412 | { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 413 | const char *p = sl->rq.l + sl->rq.m_len + sl->rq.u_len; |
| 414 | |
| 415 | if ((sl->rq.v_len == 8) && |
| 416 | (*(p + 5) > '1' || |
| 417 | (*(p + 5) == '1' && *(p + 7) >= '1'))) |
| 418 | h1m->flags |= H1_MF_VER_11; |
| 419 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 420 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 421 | /* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is |
| 422 | * greater or equal to 1.1 |
| 423 | */ |
| 424 | static void h1_parse_res_vsn(struct h1m *h1m, const union htx_sl *sl) |
| 425 | { |
| 426 | const char *p = sl->rq.l; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 427 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 428 | if ((sl->st.v_len == 8) && |
| 429 | (*(p + 5) > '1' || |
| 430 | (*(p + 5) == '1' && *(p + 7) >= '1'))) |
| 431 | h1m->flags |= H1_MF_VER_11; |
| 432 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 433 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 434 | /* |
| 435 | * Check the validity of the request version. If the version is valid, it |
| 436 | * returns 1. Otherwise, it returns 0. |
| 437 | */ |
| 438 | static int h1_process_req_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl) |
| 439 | { |
| 440 | struct h1c *h1c = h1s->h1c; |
| 441 | |
| 442 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 443 | * exactly one digit "." one digit. This check may be disabled using |
| 444 | * option accept-invalid-http-request. |
| 445 | */ |
| 446 | if (!(h1c->px->options2 & PR_O2_REQBUG_OK)) { |
| 447 | if (sl.rq.v.len != 8) |
| 448 | return 0; |
| 449 | |
| 450 | if (*(sl.rq.v.ptr + 4) != '/' || |
| 451 | !isdigit((unsigned char)*(sl.rq.v.ptr + 5)) || |
| 452 | *(sl.rq.v.ptr + 6) != '.' || |
| 453 | !isdigit((unsigned char)*(sl.rq.v.ptr + 7))) |
| 454 | return 0; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 455 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 456 | else if (!sl.rq.v.len) { |
| 457 | /* try to convert HTTP/0.9 requests to HTTP/1.0 */ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 458 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 459 | /* RFC 1945 allows only GET for HTTP/0.9 requests */ |
| 460 | if (sl.rq.meth != HTTP_METH_GET) |
| 461 | return 0; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 462 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 463 | /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */ |
| 464 | if (!sl.rq.u.len) |
| 465 | return 0; |
| 466 | |
| 467 | /* Add HTTP version */ |
| 468 | sl.rq.v = ist("HTTP/1.0"); |
| 469 | } |
| 470 | return 1; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 471 | } |
| 472 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 473 | /* |
| 474 | * Check the validity of the response version. If the version is valid, it |
| 475 | * returns 1. Otherwise, it returns 0. |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 476 | */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 477 | 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] | 478 | { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 479 | struct h1c *h1c = h1s->h1c; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 480 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 481 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 482 | * exactly one digit "." one digit. This check may be disabled using |
| 483 | * option accept-invalid-http-request. |
| 484 | */ |
| 485 | if (!(h1c->px->options2 & PR_O2_RSPBUG_OK)) { |
| 486 | if (sl.st.v.len != 8) |
| 487 | return 0; |
| 488 | |
| 489 | if (*(sl.st.v.ptr + 4) != '/' || |
| 490 | !isdigit((unsigned char)*(sl.st.v.ptr + 5)) || |
| 491 | *(sl.st.v.ptr + 6) != '.' || |
| 492 | !isdigit((unsigned char)*(sl.st.v.ptr + 7))) |
| 493 | return 0; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 494 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 495 | return 1; |
| 496 | } |
| 497 | /* Remove all "Connection:" headers from the HTX message <htx> */ |
| 498 | static void h1_remove_conn_hdrs(struct h1m *h1m, struct htx *htx) |
| 499 | { |
| 500 | struct ist hdr = {.ptr = "Connection", .len = 10}; |
| 501 | struct http_hdr_ctx ctx; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 502 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 503 | while (http_find_header(htx, hdr, &ctx, 1)) |
| 504 | http_remove_header(htx, &ctx); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 505 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 506 | h1m->flags &= ~(H1_MF_CONN_KAL|H1_MF_CONN_CLO); |
| 507 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 508 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 509 | /* Add a "Connection:" header with the value <value> into the HTX message |
| 510 | * <htx>. |
| 511 | */ |
| 512 | static void h1_add_conn_hdr(struct h1m *h1m, struct htx *htx, struct ist value) |
| 513 | { |
| 514 | struct ist hdr = {.ptr = "Connection", .len = 10}; |
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 | http_add_header(htx, hdr, value); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | /* Deduce the connection mode of the client connection, depending on the |
| 520 | * configuration and the H1 message flags. This function is called twice, the |
| 521 | * first time when the request is parsed and the second time when the response |
| 522 | * is parsed. |
| 523 | */ |
| 524 | static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m) |
| 525 | { |
| 526 | struct proxy *fe = h1s->h1c->px; |
| 527 | int flag = H1S_F_WANT_KAL; /* For client connection: server-close == keepalive */ |
| 528 | |
| 529 | /* Tunnel mode can only by set on the frontend */ |
| 530 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN) |
| 531 | flag = H1S_F_WANT_TUN; |
| 532 | else if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) |
| 533 | flag = H1S_F_WANT_CLO; |
| 534 | |
| 535 | /* flags order: CLO > SCL > TUN > KAL */ |
| 536 | if ((h1s->flags & H1S_F_WANT_MSK) < flag) |
| 537 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | flag; |
| 538 | |
| 539 | if (h1m->flags & H1_MF_RESP) { |
| 540 | /* Either we've established an explicit tunnel, or we're |
| 541 | * switching the protocol. In both cases, we're very unlikely to |
| 542 | * understand the next protocols. We have to switch to tunnel |
| 543 | * mode, so that we transfer the request and responses then let |
| 544 | * this protocol pass unmodified. When we later implement |
| 545 | * specific parsers for such protocols, we'll want to check the |
| 546 | * Upgrade header which contains information about that protocol |
| 547 | * for responses with status 101 (eg: see RFC2817 about TLS). |
| 548 | */ |
| 549 | if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) || |
| 550 | h1s->status == 101) |
| 551 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN; |
| 552 | else if (!(h1m->flags & H1_MF_XFER_LEN)) /* no length known => close */ |
| 553 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
| 554 | } |
| 555 | else { |
| 556 | if (h1s->flags & H1S_F_WANT_KAL && |
| 557 | (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || /* no KA in HTTP/1.0 */ |
| 558 | h1m->flags & H1_MF_CONN_CLO)) /* explicit close */ |
| 559 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
| 560 | } |
| 561 | |
| 562 | /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */ |
| 563 | if (h1s->flags & H1S_F_WANT_KAL && fe->state == PR_STSTOPPED) |
| 564 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
| 565 | } |
| 566 | |
| 567 | /* Deduce the connection mode of the client connection, depending on the |
| 568 | * configuration and the H1 message flags. This function is called twice, the |
| 569 | * first time when the request is parsed and the second time when the response |
| 570 | * is parsed. |
| 571 | */ |
| 572 | static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m) |
| 573 | { |
| 574 | struct proxy *be = h1s->h1c->px; |
| 575 | struct proxy *fe = strm_fe(si_strm(h1s->cs->data)); |
| 576 | int flag = H1S_F_WANT_KAL; |
| 577 | |
| 578 | /* Tunnel mode can only by set on the frontend */ |
| 579 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN) |
| 580 | flag = H1S_F_WANT_TUN; |
| 581 | |
| 582 | /* For the server connection: server-close == httpclose */ |
| 583 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL || |
| 584 | (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL || |
| 585 | (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO || |
| 586 | (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) |
| 587 | flag = H1S_F_WANT_CLO; |
| 588 | |
| 589 | /* flags order: CLO > SCL > TUN > KAL */ |
| 590 | if ((h1s->flags & H1S_F_WANT_MSK) < flag) |
| 591 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | flag; |
| 592 | |
| 593 | if (h1m->flags & H1_MF_RESP) { |
| 594 | /* Either we've established an explicit tunnel, or we're |
| 595 | * switching the protocol. In both cases, we're very unlikely to |
| 596 | * understand the next protocols. We have to switch to tunnel |
| 597 | * mode, so that we transfer the request and responses then let |
| 598 | * this protocol pass unmodified. When we later implement |
| 599 | * specific parsers for such protocols, we'll want to check the |
| 600 | * Upgrade header which contains information about that protocol |
| 601 | * for responses with status 101 (eg: see RFC2817 about TLS). |
| 602 | */ |
| 603 | if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) || |
| 604 | h1s->status == 101) |
| 605 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN; |
| 606 | else if (!(h1m->flags & H1_MF_XFER_LEN)) /* no length known => close */ |
| 607 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
| 608 | else if (h1s->flags & H1S_F_WANT_KAL && |
| 609 | (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || /* no KA in HTTP/1.0 */ |
| 610 | h1m->flags & H1_MF_CONN_CLO)) /* explicit close */ |
| 611 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
| 612 | } |
| 613 | |
| 614 | /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */ |
| 615 | if (h1s->flags & H1S_F_WANT_KAL && be->state == PR_STSTOPPED) |
| 616 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 617 | } |
| 618 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 619 | static void h1_update_req_conn_hdr(struct h1s *h1s, struct h1m *h1m, |
| 620 | struct htx *htx, struct ist *conn_val) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 621 | { |
| 622 | struct proxy *px = h1s->h1c->px; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 623 | |
| 624 | /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage" |
| 625 | * token is found |
| 626 | */ |
| 627 | 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] | 628 | return; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 629 | |
| 630 | 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] | 631 | if (h1m->flags & H1_MF_CONN_CLO) { |
| 632 | if (conn_val) |
| 633 | *conn_val = ist(""); |
| 634 | if (htx) |
| 635 | h1_remove_conn_hdrs(h1m, htx); |
| 636 | } |
| 637 | if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))) { |
| 638 | if (conn_val) |
| 639 | *conn_val = ist("keep-alive"); |
| 640 | if (htx) |
| 641 | h1_add_conn_hdr(h1m, htx, ist("keep-alive")); |
| 642 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 643 | } |
| 644 | else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 645 | if (h1m->flags & H1_MF_CONN_KAL) { |
| 646 | if (conn_val) |
| 647 | *conn_val = ist(""); |
| 648 | if (htx) |
| 649 | h1_remove_conn_hdrs(h1m, htx); |
| 650 | } |
| 651 | if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_CLO)) == H1_MF_VER_11) { |
| 652 | if (conn_val) |
| 653 | *conn_val = ist("close"); |
| 654 | if (htx) |
| 655 | h1_add_conn_hdr(h1m, htx, ist("close")); |
| 656 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 657 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 658 | } |
| 659 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 660 | static void h1_update_res_conn_hdr(struct h1s *h1s, struct h1m *h1m, |
| 661 | struct htx *htx, struct ist *conn_val) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 662 | { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 663 | /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage" |
| 664 | * token is found |
| 665 | */ |
| 666 | 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] | 667 | return; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 668 | |
| 669 | if (h1s->flags & H1S_F_WANT_KAL) { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 670 | if (h1m->flags & H1_MF_CONN_CLO) { |
| 671 | if (conn_val) |
| 672 | *conn_val = ist(""); |
| 673 | if (htx) |
| 674 | h1_remove_conn_hdrs(h1m, htx); |
| 675 | } |
| 676 | if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))) { |
| 677 | if (conn_val) |
| 678 | *conn_val = ist("keep-alive"); |
| 679 | if (htx) |
| 680 | h1_add_conn_hdr(h1m, htx, ist("keep-alive")); |
| 681 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 682 | } |
| 683 | else { /* H1S_F_WANT_CLO */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 684 | if (h1m->flags & H1_MF_CONN_KAL) { |
| 685 | if (conn_val) |
| 686 | *conn_val = ist(""); |
| 687 | if (htx) |
| 688 | h1_remove_conn_hdrs(h1m, htx); |
| 689 | } |
| 690 | if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_CLO)) == H1_MF_VER_11) { |
| 691 | if (conn_val) |
| 692 | *conn_val = ist("close"); |
| 693 | if (htx) |
| 694 | h1_add_conn_hdr(h1m, htx, ist("close")); |
| 695 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 696 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 697 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 698 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 699 | /* Set the right connection mode and update "Connection:" header if |
| 700 | * needed. <htx> and <conn_val> can be NULL. When <htx> is not NULL, the HTX |
| 701 | * message is updated accordingly. When <conn_val> is not NULL, it is set with |
| 702 | * the new header value. |
| 703 | */ |
| 704 | static void h1_process_conn_mode(struct h1s *h1s, struct h1m *h1m, |
| 705 | struct htx *htx, struct ist *conn_val) |
| 706 | { |
| 707 | if (!conn_is_back(h1s->h1c->conn)) { |
| 708 | h1_set_cli_conn_mode(h1s, h1m); |
| 709 | if (h1m->flags & H1_MF_RESP) |
| 710 | h1_update_res_conn_hdr(h1s, h1m, htx, conn_val); |
| 711 | } |
| 712 | else { |
| 713 | h1_set_srv_conn_mode(h1s, h1m); |
| 714 | if (!(h1m->flags & H1_MF_RESP)) |
| 715 | h1_update_req_conn_hdr(h1s, h1m, htx, conn_val); |
| 716 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 717 | } |
| 718 | |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 719 | /* |
| 720 | * 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] | 721 | * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR |
| 722 | * flag and filling h1s->err_pos and h1s->err_state fields. This functions is |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 723 | * responsibile to update the parser state <h1m>. |
| 724 | */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 725 | 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] | 726 | struct buffer *buf, size_t *ofs, size_t max) |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 727 | { |
| 728 | struct http_hdr hdrs[MAX_HTTP_HDR]; |
| 729 | union h1_sl sl; |
| 730 | int ret = 0; |
| 731 | |
Christopher Faulet | d44ad5b | 2018-11-19 21:52:12 +0100 | [diff] [blame] | 732 | if (!max) |
| 733 | goto end; |
| 734 | |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 735 | /* Realing input buffer if necessary */ |
| 736 | if (b_head(buf) + b_data(buf) > b_wrap(buf)) |
| 737 | b_slow_realign(buf, trash.area, 0); |
| 738 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 739 | ret = h1_headers_to_hdr_list(b_peek(buf, *ofs), b_peek(buf, *ofs) + max, |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 740 | hdrs, sizeof(hdrs)/sizeof(hdrs[0]), h1m, &sl); |
| 741 | if (ret <= 0) { |
| 742 | /* Incomplete or invalid message. If the buffer is full, it's an |
| 743 | * error because headers are too large to be handled by the |
| 744 | * parser. */ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 745 | if (ret < 0 || (!ret && b_full(buf))) |
| 746 | goto error; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 747 | goto end; |
| 748 | } |
| 749 | |
| 750 | /* messages headers fully parsed, do some checks to prepare the body |
| 751 | * parsing. |
| 752 | */ |
| 753 | |
| 754 | /* Be sure to keep some space to do headers rewritting */ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 755 | if (ret > (b_size(buf) - global.tune.maxrewrite)) |
| 756 | goto error; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 757 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 758 | /* Save the request's method or the response's status, check if the body |
| 759 | * length is known and check the VSN validity */ |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 760 | if (!(h1m->flags & H1_MF_RESP)) { |
| 761 | h1s->meth = sl.rq.meth; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 762 | |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 763 | /* Request have always a known length */ |
| 764 | h1m->flags |= H1_MF_XFER_LEN; |
| 765 | if (!(h1m->flags & H1_MF_CHNK) && !h1m->body_len) |
| 766 | h1m->state = H1_MSG_DONE; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 767 | |
| 768 | if (!h1_process_req_vsn(h1s, h1m, sl)) { |
| 769 | h1m->err_pos = sl.rq.v.ptr - b_head(buf); |
| 770 | h1m->err_state = h1m->state; |
| 771 | goto vsn_error; |
| 772 | } |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 773 | } |
| 774 | else { |
| 775 | h1s->status = sl.st.status; |
| 776 | |
| 777 | if ((h1s->meth == HTTP_METH_HEAD) || |
| 778 | (h1s->status >= 100 && h1s->status < 200) || |
| 779 | (h1s->status == 204) || (h1s->status == 304) || |
| 780 | (h1s->meth == HTTP_METH_CONNECT && h1s->status == 200)) { |
| 781 | h1m->flags &= ~(H1_MF_CLEN|H1_MF_CHNK); |
| 782 | h1m->flags |= H1_MF_XFER_LEN; |
| 783 | h1m->curr_len = h1m->body_len = 0; |
| 784 | h1m->state = H1_MSG_DONE; |
| 785 | } |
| 786 | else if (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) { |
| 787 | h1m->flags |= H1_MF_XFER_LEN; |
| 788 | if ((h1m->flags & H1_MF_CLEN) && !h1m->body_len) |
| 789 | h1m->state = H1_MSG_DONE; |
| 790 | } |
| 791 | else |
| 792 | h1m->state = H1_MSG_TUNNEL; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 793 | |
| 794 | if (!h1_process_res_vsn(h1s, h1m, sl)) { |
| 795 | h1m->err_pos = sl.st.v.ptr - b_head(buf); |
| 796 | h1m->err_state = h1m->state; |
| 797 | goto vsn_error; |
| 798 | } |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 799 | } |
| 800 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 801 | if (!(h1m->flags & H1_MF_RESP)) { |
| 802 | if (!htx_add_reqline(htx, sl) || !htx_add_all_headers(htx, hdrs)) |
| 803 | goto error; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 804 | } |
| 805 | else { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 806 | if (!htx_add_resline(htx, sl) || !htx_add_all_headers(htx, hdrs)) |
| 807 | goto error; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 808 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 809 | if (h1m->state == H1_MSG_DONE) |
| 810 | if (!htx_add_endof(htx, HTX_BLK_EOM)) |
| 811 | goto error; |
| 812 | |
| 813 | h1_process_conn_mode(h1s, h1m, htx, NULL); |
| 814 | |
| 815 | /* If body length cannot be determined, set htx->extra to |
| 816 | * ULLONG_MAX. This value is impossible in other cases. |
| 817 | */ |
| 818 | htx->extra = ((h1m->flags & H1_MF_XFER_LEN) ? h1m->curr_len : ULLONG_MAX); |
| 819 | |
| 820 | /* Recheck there is enough space to do headers rewritting */ |
| 821 | if (htx_used_space(htx) > b_size(buf) - global.tune.maxrewrite) |
| 822 | goto error; |
| 823 | |
| 824 | *ofs += ret; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 825 | end: |
| 826 | return ret; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 827 | |
| 828 | error: |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 829 | h1m->err_state = h1m->state; |
| 830 | h1m->err_pos = h1m->next; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 831 | vsn_error: |
| 832 | h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 833 | ret = 0; |
| 834 | goto end; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | /* |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 838 | * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it |
| 839 | * 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] | 840 | * and filling h1s->err_pos and h1s->err_state fields. This functions is |
| 841 | * responsibile to update the parser state <h1m>. |
| 842 | */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 843 | static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx *htx, |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 844 | struct buffer *buf, size_t *ofs, size_t max) |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 845 | { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 846 | uint32_t data_space = htx_free_data_space(htx); |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 847 | size_t total = 0; |
| 848 | int ret = 0; |
| 849 | |
| 850 | if (h1m->flags & H1_MF_XFER_LEN) { |
| 851 | if (h1m->flags & H1_MF_CLEN) { |
| 852 | /* content-length: read only h2m->body_len */ |
| 853 | ret = max; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 854 | if (ret > data_space) |
| 855 | ret = data_space; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 856 | if ((uint64_t)ret > h1m->curr_len) |
| 857 | ret = h1m->curr_len; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 858 | if (ret > b_contig_data(buf, *ofs)) |
| 859 | ret = b_contig_data(buf, *ofs); |
| 860 | if (ret) { |
| 861 | if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret))) |
| 862 | goto end; |
| 863 | h1m->curr_len -= ret; |
| 864 | *ofs += ret; |
| 865 | total += ret; |
| 866 | } |
| 867 | |
| 868 | if (!h1m->curr_len) { |
| 869 | if (!htx_add_endof(htx, HTX_BLK_EOM)) |
| 870 | goto end; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 871 | h1m->state = H1_MSG_DONE; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 872 | } |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 873 | } |
| 874 | else if (h1m->flags & H1_MF_CHNK) { |
| 875 | new_chunk: |
| 876 | /* te:chunked : parse chunks */ |
| 877 | if (h1m->state == H1_MSG_CHUNK_CRLF) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 878 | ret = h1_skip_chunk_crlf(buf, *ofs, *ofs + max); |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 879 | if (ret <= 0) |
| 880 | goto end; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 881 | h1m->state = H1_MSG_CHUNK_SIZE; |
| 882 | |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 883 | max -= ret; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 884 | *ofs += ret; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 885 | total += ret; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | if (h1m->state == H1_MSG_CHUNK_SIZE) { |
| 889 | unsigned int chksz; |
| 890 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 891 | ret = h1_parse_chunk_size(buf, *ofs, *ofs + max, &chksz); |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 892 | if (ret <= 0) |
| 893 | goto end; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 894 | if (!chksz) { |
| 895 | if (!htx_add_endof(htx, HTX_BLK_EOD)) |
| 896 | goto end; |
| 897 | h1m->state = H1_MSG_TRAILERS; |
| 898 | } |
| 899 | else |
| 900 | h1m->state = H1_MSG_DATA; |
| 901 | |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 902 | h1m->curr_len = chksz; |
| 903 | h1m->body_len += chksz; |
| 904 | max -= ret; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 905 | *ofs += ret; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 906 | total += ret; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | if (h1m->state == H1_MSG_DATA) { |
| 910 | ret = max; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 911 | if (ret > data_space) |
| 912 | ret = data_space; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 913 | if ((uint64_t)ret > h1m->curr_len) |
| 914 | ret = h1m->curr_len; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 915 | if (ret > b_contig_data(buf, *ofs)) |
| 916 | ret = b_contig_data(buf, *ofs); |
| 917 | if (ret) { |
| 918 | if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret))) |
| 919 | goto end; |
| 920 | h1m->curr_len -= ret; |
| 921 | max -= ret; |
| 922 | *ofs += ret; |
| 923 | total += ret; |
| 924 | } |
| 925 | if (!h1m->curr_len) { |
| 926 | h1m->state = H1_MSG_CHUNK_CRLF; |
| 927 | goto new_chunk; |
| 928 | } |
| 929 | goto end; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 930 | } |
| 931 | |
| 932 | if (h1m->state == H1_MSG_TRAILERS) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 933 | ret = h1_measure_trailers(buf, *ofs, *ofs + max); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 934 | if (ret > data_space) |
| 935 | ret = (htx_is_empty(htx) ? -1 : 0); |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 936 | if (ret <= 0) |
| 937 | goto end; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 938 | |
| 939 | /* Realing input buffer if tailers wrap. For now |
| 940 | * this is a workaroung. Because trailers are |
| 941 | * not split on CRLF, like headers, there is no |
| 942 | * way to know where to split it when trailers |
| 943 | * wrap. This is a limitation of |
| 944 | * h1_measure_trailers. |
| 945 | */ |
| 946 | if (b_peek(buf, *ofs) > b_peek(buf, *ofs + ret)) |
| 947 | b_slow_realign(buf, trash.area, 0); |
| 948 | |
| 949 | if (!htx_add_trailer(htx, ist2(b_peek(buf, *ofs), ret))) |
| 950 | goto end; |
| 951 | |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 952 | max -= ret; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 953 | *ofs += ret; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 954 | total += ret; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 955 | |
| 956 | /* FIXME: if it fails here, this is a problem, |
| 957 | * because there is no way to return here. */ |
| 958 | if (!htx_add_endof(htx, HTX_BLK_EOM)) |
| 959 | goto end; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 960 | h1m->state = H1_MSG_DONE; |
| 961 | } |
| 962 | } |
| 963 | else { |
| 964 | /* XFER_LEN is set but not CLEN nor CHNK, it means there |
| 965 | * is no body. Switch the message in DONE state |
| 966 | */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 967 | if (!htx_add_endof(htx, HTX_BLK_EOM)) |
| 968 | goto end; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 969 | h1m->state = H1_MSG_DONE; |
| 970 | } |
| 971 | } |
| 972 | else { |
| 973 | /* no content length, read till SHUTW */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 974 | ret = max; |
| 975 | if (ret > data_space) |
| 976 | ret = data_space; |
| 977 | if (ret > b_contig_data(buf, *ofs)) |
| 978 | ret = b_contig_data(buf, *ofs); |
| 979 | if (ret) { |
| 980 | if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret))) |
| 981 | goto end; |
| 982 | |
| 983 | *ofs += max; |
| 984 | total = max; |
| 985 | } |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | end: |
| 989 | if (ret < 0) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 990 | 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] | 991 | h1m->err_state = h1m->state; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 992 | h1m->err_pos = *ofs + max + ret; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 993 | return 0; |
| 994 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 995 | /* update htx->extra, only when the body length is known */ |
| 996 | if (h1m->flags & H1_MF_XFER_LEN) |
| 997 | htx->extra = h1m->curr_len; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 998 | return total; |
| 999 | } |
| 1000 | |
| 1001 | /* |
| 1002 | * Synchronize the request and the response before reseting them. Except for 1xx |
| 1003 | * responses, we wait that the request and the response are in DONE state and |
| 1004 | * that all data are forwarded for both. For 1xx responses, only the response is |
| 1005 | * reset, waiting the final one. Many 1xx messages can be sent. |
| 1006 | */ |
| 1007 | static void h1_sync_messages(struct h1c *h1c) |
| 1008 | { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1009 | struct h1s *h1s = h1c->h1s; |
| 1010 | |
| 1011 | if (!h1s) |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1012 | return; |
| 1013 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1014 | if (h1s->res.state == H1_MSG_DONE && |
| 1015 | (h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) && |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1016 | (conn_is_back(h1c->conn) || !b_data(&h1c->obuf))) { |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1017 | /* For 100-Continue response or any other informational 1xx |
| 1018 | * response which is non-final, don't reset the request, the |
| 1019 | * transaction is not finished. We take care the response was |
| 1020 | * transferred before. |
| 1021 | */ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1022 | h1m_init_res(&h1s->res); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1023 | h1s->res.flags |= H1_MF_NO_PHDR; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1024 | } |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1025 | else if (!b_data(&h1c->obuf) && |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1026 | h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) { |
| 1027 | if (h1s->flags & H1S_F_WANT_TUN) { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1028 | h1m_init_req(&h1s->req); |
| 1029 | h1m_init_res(&h1s->res); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1030 | h1s->req.state = H1_MSG_TUNNEL; |
| 1031 | h1s->res.state = H1_MSG_TUNNEL; |
| 1032 | } |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1033 | } |
| 1034 | } |
| 1035 | |
| 1036 | /* |
| 1037 | * 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] | 1038 | * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if |
| 1039 | * it couldn't proceed. |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1040 | */ |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1041 | 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] | 1042 | { |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1043 | struct h1s *h1s = h1c->h1s; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1044 | struct h1m *h1m; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1045 | struct htx *htx; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1046 | size_t total = 0; |
| 1047 | size_t ret = 0; |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1048 | size_t count, max; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1049 | int errflag; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1050 | |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1051 | htx = htx_from_buf(buf); |
| 1052 | count = b_data(&h1c->ibuf); |
| 1053 | max = htx_free_space(htx); |
| 1054 | if (flags & CO_RFL_KEEP_RSV) { |
| 1055 | if (max < global.tune.maxrewrite) |
| 1056 | goto end; |
| 1057 | max -= global.tune.maxrewrite; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1058 | } |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1059 | if (count > max) |
| 1060 | count = max; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1061 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1062 | if (!conn_is_back(h1c->conn)) { |
| 1063 | h1m = &h1s->req; |
| 1064 | errflag = H1S_F_REQ_ERROR; |
| 1065 | } |
| 1066 | else { |
| 1067 | h1m = &h1s->res; |
| 1068 | errflag = H1S_F_RES_ERROR; |
| 1069 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1070 | |
Christopher Faulet | d44ad5b | 2018-11-19 21:52:12 +0100 | [diff] [blame] | 1071 | do { |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1072 | if (h1m->state <= H1_MSG_LAST_LF) { |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1073 | ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count); |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1074 | if (!ret) |
| 1075 | break; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1076 | } |
| 1077 | else if (h1m->state <= H1_MSG_TRAILERS) { |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1078 | ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count); |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1079 | if (!ret) |
| 1080 | break; |
| 1081 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1082 | else if (h1m->state == H1_MSG_DONE) |
| 1083 | break; |
| 1084 | else if (h1m->state == H1_MSG_TUNNEL) { |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1085 | ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1086 | if (!ret) |
| 1087 | break; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1088 | } |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1089 | else { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1090 | h1s->flags |= errflag; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1091 | break; |
| 1092 | } |
| 1093 | |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1094 | count -= ret; |
Christopher Faulet | d44ad5b | 2018-11-19 21:52:12 +0100 | [diff] [blame] | 1095 | } while (!(h1s->flags & errflag) && count); |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1096 | |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1097 | if (h1s->flags & errflag) |
| 1098 | goto parsing_err; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1099 | |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1100 | b_del(&h1c->ibuf, total); |
| 1101 | |
| 1102 | end: |
| 1103 | if (htx_is_not_empty(htx)) |
| 1104 | b_set_data(buf, b_size(buf)); |
| 1105 | else { |
| 1106 | htx_reset(htx); |
| 1107 | b_set_data(buf, 0); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1108 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1109 | |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1110 | if (h1c->flags & H1C_F_IN_FULL && b_room(&h1c->ibuf)) { |
| 1111 | h1c->flags &= ~H1C_F_IN_FULL; |
| 1112 | tasklet_wakeup(h1c->wait_event.task); |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1113 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1114 | |
Christopher Faulet | 9c38840 | 2018-11-19 21:54:26 +0100 | [diff] [blame] | 1115 | if (b_data(&h1c->ibuf)) { |
| 1116 | if (!htx_is_empty(htx)) |
| 1117 | h1s->cs->flags |= CS_FL_RCV_MORE; |
| 1118 | } |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1119 | else { |
| 1120 | h1_release_buf(h1c, &h1c->ibuf); |
| 1121 | h1_sync_messages(h1c); |
| 1122 | |
| 1123 | h1s->cs->flags &= ~CS_FL_RCV_MORE; |
| 1124 | if (h1s->cs->flags & CS_FL_REOS) |
| 1125 | h1s->cs->flags |= CS_FL_EOS; |
| 1126 | } |
| 1127 | return total; |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1128 | |
| 1129 | parsing_err: |
| 1130 | // FIXME: create an error snapshot here |
| 1131 | b_reset(&h1c->ibuf); |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1132 | htx->flags |= HTX_FL_PARSING_ERROR; |
| 1133 | b_set_data(buf, b_size(buf)); |
| 1134 | h1s->cs->flags |= CS_FL_EOS; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1135 | return 0; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1136 | } |
| 1137 | |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1138 | /* |
| 1139 | * Process outgoing data. It parses data and transfer them from the channel buffer into |
| 1140 | * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or |
| 1141 | * 0 if it couldn't proceed. |
| 1142 | */ |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1143 | static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count) |
| 1144 | { |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1145 | struct h1s *h1s = h1c->h1s; |
| 1146 | struct h1m *h1m; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1147 | struct htx *chn_htx; |
| 1148 | struct htx_blk *blk; |
| 1149 | struct buffer *tmp; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1150 | size_t total = 0; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1151 | int errflag; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1152 | |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1153 | if (!count) |
| 1154 | goto end; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1155 | chn_htx = htx_from_buf(buf); |
| 1156 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1157 | if (!h1_get_buf(h1c, &h1c->obuf)) { |
| 1158 | h1c->flags |= H1C_F_OUT_ALLOC; |
| 1159 | goto end; |
| 1160 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1161 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1162 | if (!conn_is_back(h1c->conn)) { |
| 1163 | h1m = &h1s->res; |
| 1164 | errflag = H1S_F_RES_ERROR; |
| 1165 | } |
| 1166 | else { |
| 1167 | h1m = &h1s->req; |
| 1168 | errflag = H1S_F_REQ_ERROR; |
| 1169 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1170 | |
| 1171 | |
| 1172 | tmp = get_trash_chunk(); |
| 1173 | tmp->size = b_room(&h1c->obuf); |
| 1174 | |
| 1175 | blk = htx_get_head_blk(chn_htx); |
| 1176 | while (!(h1s->flags & errflag) && blk) { |
| 1177 | union htx_sl *sl; |
| 1178 | struct ist n, v; |
| 1179 | uint32_t sz = htx_get_blksz(blk); |
| 1180 | |
| 1181 | if (total + sz > count) |
| 1182 | goto copy; |
| 1183 | |
| 1184 | switch (htx_get_blk_type(blk)) { |
| 1185 | case HTX_BLK_UNUSED: |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1186 | break; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1187 | |
| 1188 | case HTX_BLK_REQ_SL: |
| 1189 | sl = htx_get_blk_ptr(chn_htx, blk); |
| 1190 | h1s->meth = sl->rq.meth; |
| 1191 | h1_parse_req_vsn(h1m, sl); |
| 1192 | if (!htx_reqline_to_str(sl, tmp)) |
| 1193 | goto copy; |
| 1194 | h1m->flags |= H1_MF_XFER_LEN; |
| 1195 | h1m->state = H1_MSG_HDR_FIRST; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1196 | break; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1197 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1198 | case HTX_BLK_RES_SL: |
| 1199 | sl = htx_get_blk_ptr(chn_htx, blk); |
| 1200 | h1s->status = sl->st.status; |
| 1201 | h1_parse_res_vsn(h1m, sl); |
| 1202 | if (!htx_stline_to_str(sl, tmp)) |
| 1203 | goto copy; |
| 1204 | if (chn_htx->extra != ULLONG_MAX) |
| 1205 | h1m->flags |= H1_MF_XFER_LEN; |
| 1206 | h1m->state = H1_MSG_HDR_FIRST; |
| 1207 | break; |
| 1208 | |
| 1209 | case HTX_BLK_HDR: |
| 1210 | if (h1m->state == H1_MSG_HDR_FIRST) { |
| 1211 | struct http_hdr_ctx ctx; |
| 1212 | |
| 1213 | n = ist("Connection"); |
| 1214 | v = ist(""); |
| 1215 | |
| 1216 | /* If there is no "Connection:" header, |
| 1217 | * process conn_mode now and add the |
| 1218 | * right one. |
| 1219 | */ |
| 1220 | ctx.blk = blk; |
| 1221 | if (http_find_header(chn_htx, n, &ctx, 1)) |
| 1222 | goto process_hdr; |
| 1223 | h1_process_conn_mode(h1s, h1m, NULL, &v); |
| 1224 | if (!v.len) |
| 1225 | goto process_hdr; |
| 1226 | |
| 1227 | if (!htx_hdr_to_str(n, v, tmp)) |
| 1228 | goto copy; |
| 1229 | } |
| 1230 | process_hdr: |
| 1231 | h1m->state = H1_MSG_HDR_NAME; |
| 1232 | n = htx_get_blk_name(chn_htx, blk); |
| 1233 | v = htx_get_blk_value(chn_htx, blk); |
| 1234 | |
| 1235 | if (isteqi(n, ist("transfer-encoding"))) |
| 1236 | h1_parse_xfer_enc_header(h1m, v); |
| 1237 | else if (isteqi(n, ist("connection"))) { |
| 1238 | h1_parse_connection_header(h1m, v); |
| 1239 | h1_process_conn_mode(h1s, h1m, NULL, &v); |
| 1240 | if (!v.len) |
| 1241 | goto skip_hdr; |
| 1242 | } |
| 1243 | |
| 1244 | if (!htx_hdr_to_str(n, v, tmp)) |
| 1245 | goto copy; |
| 1246 | skip_hdr: |
| 1247 | h1m->state = H1_MSG_HDR_L2_LWS; |
| 1248 | break; |
| 1249 | |
| 1250 | case HTX_BLK_PHDR: |
| 1251 | /* not implemented yet */ |
| 1252 | h1m->flags |= errflag; |
| 1253 | break; |
| 1254 | |
| 1255 | case HTX_BLK_EOH: |
| 1256 | h1m->state = H1_MSG_LAST_LF; |
| 1257 | if (!chunk_memcat(tmp, "\r\n", 2)) |
| 1258 | goto copy; |
| 1259 | |
| 1260 | h1m->state = H1_MSG_DATA; |
| 1261 | break; |
| 1262 | |
| 1263 | case HTX_BLK_DATA: |
| 1264 | v = htx_get_blk_value(chn_htx, blk); |
| 1265 | if (!htx_data_to_str(v, tmp, !!(h1m->flags & H1_MF_CHNK))) |
| 1266 | goto copy; |
| 1267 | break; |
| 1268 | |
| 1269 | case HTX_BLK_EOD: |
| 1270 | if (!chunk_memcat(tmp, "0\r\n", 3)) |
| 1271 | goto copy; |
| 1272 | h1m->state = H1_MSG_TRAILERS; |
| 1273 | break; |
| 1274 | |
| 1275 | case HTX_BLK_TLR: |
| 1276 | v = htx_get_blk_value(chn_htx, blk); |
| 1277 | if (!htx_trailer_to_str(v, tmp)) |
| 1278 | goto copy; |
| 1279 | break; |
| 1280 | |
| 1281 | case HTX_BLK_EOM: |
| 1282 | /* if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(tmp, "\r\n", 2)) */ |
| 1283 | /* goto copy; */ |
| 1284 | h1m->state = H1_MSG_DONE; |
| 1285 | break; |
| 1286 | |
| 1287 | case HTX_BLK_OOB: |
| 1288 | v = htx_get_blk_value(chn_htx, blk); |
| 1289 | if (!chunk_memcat(tmp, v.ptr, v.len)) |
| 1290 | goto copy; |
| 1291 | break; |
| 1292 | |
| 1293 | default: |
| 1294 | h1m->flags |= errflag; |
| 1295 | break; |
| 1296 | } |
| 1297 | total += sz; |
| 1298 | blk = htx_remove_blk(chn_htx, blk); |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1299 | } |
| 1300 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1301 | copy: |
| 1302 | b_putblk(&h1c->obuf, tmp->area, tmp->data); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1303 | |
| 1304 | if (b_full(&h1c->obuf)) |
| 1305 | h1c->flags |= H1C_F_OUT_FULL; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1306 | if (htx_is_empty(chn_htx)) { |
| 1307 | htx_reset(chn_htx); |
| 1308 | b_set_data(buf, 0); |
| 1309 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1310 | end: |
| 1311 | return total; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1312 | } |
| 1313 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1314 | /*********************************************************/ |
| 1315 | /* functions below are I/O callbacks from the connection */ |
| 1316 | /*********************************************************/ |
| 1317 | /* |
| 1318 | * Attempt to read data, and subscribe if none available |
| 1319 | */ |
| 1320 | static int h1_recv(struct h1c *h1c) |
| 1321 | { |
| 1322 | struct connection *conn = h1c->conn; |
| 1323 | size_t ret, max; |
| 1324 | int rcvd = 0; |
| 1325 | |
| 1326 | if (h1c->wait_event.wait_reason & SUB_CAN_RECV) |
| 1327 | return 0; |
| 1328 | |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1329 | if (!h1_recv_allowed(h1c)) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1330 | goto end; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1331 | |
Christopher Faulet | d44ad5b | 2018-11-19 21:52:12 +0100 | [diff] [blame] | 1332 | if (h1c->h1s && (h1c->h1s->flags & (H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA))) { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1333 | rcvd = 1; |
| 1334 | goto end; |
| 1335 | } |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 1336 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1337 | if (!h1_get_buf(h1c, &h1c->ibuf)) { |
| 1338 | h1c->flags |= H1C_F_IN_ALLOC; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1339 | goto end; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1340 | } |
| 1341 | |
| 1342 | ret = 0; |
| 1343 | max = b_room(&h1c->ibuf); |
| 1344 | if (max) { |
| 1345 | h1c->flags &= ~H1C_F_IN_FULL; |
| 1346 | ret = conn->xprt->rcv_buf(conn, &h1c->ibuf, max, 0); |
| 1347 | } |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1348 | if (ret > 0) { |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1349 | rcvd = 1; |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1350 | if (h1c->h1s && h1c->h1s->cs) |
| 1351 | h1c->h1s->cs->flags |= CS_FL_READ_PARTIAL; |
| 1352 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1353 | |
| 1354 | if (h1_recv_allowed(h1c)) |
| 1355 | conn->xprt->subscribe(conn, SUB_CAN_RECV, &h1c->wait_event); |
Christopher Faulet | 81d4843 | 2018-11-19 21:22:43 +0100 | [diff] [blame] | 1356 | else |
| 1357 | rcvd = 1; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1358 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1359 | end: |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1360 | if (!b_data(&h1c->ibuf)) |
| 1361 | h1_release_buf(h1c, &h1c->ibuf); |
| 1362 | else if (b_full(&h1c->ibuf)) |
| 1363 | h1c->flags |= H1C_F_IN_FULL; |
| 1364 | return rcvd; |
| 1365 | } |
| 1366 | |
| 1367 | |
| 1368 | /* |
| 1369 | * Try to send data if possible |
| 1370 | */ |
| 1371 | static int h1_send(struct h1c *h1c) |
| 1372 | { |
| 1373 | struct connection *conn = h1c->conn; |
| 1374 | unsigned int flags = 0; |
| 1375 | size_t ret; |
| 1376 | int sent = 0; |
| 1377 | |
| 1378 | if (conn->flags & CO_FL_ERROR) |
| 1379 | return 0; |
| 1380 | |
Christopher Faulet | 3b88b8d | 2018-10-26 17:36:03 +0200 | [diff] [blame] | 1381 | if (h1c->flags & H1C_F_CS_WAIT_CONN) { |
| 1382 | if (!(h1c->wait_event.wait_reason & SUB_CAN_SEND)) |
| 1383 | conn->xprt->subscribe(conn, SUB_CAN_SEND, &h1c->wait_event); |
| 1384 | return 0; |
| 1385 | } |
| 1386 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1387 | if (!b_data(&h1c->obuf)) |
| 1388 | goto end; |
| 1389 | |
| 1390 | if (h1c->flags & H1C_F_OUT_FULL) |
| 1391 | flags |= CO_SFL_MSG_MORE; |
| 1392 | |
| 1393 | ret = conn->xprt->snd_buf(conn, &h1c->obuf, b_data(&h1c->obuf), flags); |
| 1394 | if (ret > 0) { |
| 1395 | h1c->flags &= ~H1C_F_OUT_FULL; |
| 1396 | b_del(&h1c->obuf, ret); |
| 1397 | sent = 1; |
| 1398 | } |
| 1399 | |
| 1400 | end: |
| 1401 | /* We're done, no more to send */ |
| 1402 | if (!b_data(&h1c->obuf)) { |
| 1403 | h1_release_buf(h1c, &h1c->obuf); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1404 | h1_sync_messages(h1c); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1405 | if (h1c->flags & H1C_F_CS_SHUTW_NOW) |
| 1406 | h1_shutw_conn(conn); |
| 1407 | } |
| 1408 | else if (!(h1c->wait_event.wait_reason & SUB_CAN_SEND)) |
| 1409 | conn->xprt->subscribe(conn, SUB_CAN_SEND, &h1c->wait_event); |
| 1410 | |
| 1411 | return sent; |
| 1412 | } |
| 1413 | |
| 1414 | |
| 1415 | static void h1_wake_stream(struct h1c *h1c) |
| 1416 | { |
| 1417 | struct connection *conn = h1c->conn; |
| 1418 | struct h1s *h1s = h1c->h1s; |
| 1419 | uint32_t flags = 0; |
| 1420 | int dont_wake = 0; |
| 1421 | |
| 1422 | if (!h1s || !h1s->cs) |
| 1423 | return; |
| 1424 | |
| 1425 | if ((h1c->flags & H1C_F_CS_ERROR) || (conn->flags & CO_FL_ERROR)) |
| 1426 | flags |= CS_FL_ERROR; |
| 1427 | if (conn_xprt_read0_pending(conn)) |
| 1428 | flags |= CS_FL_REOS; |
| 1429 | |
| 1430 | h1s->cs->flags |= flags; |
| 1431 | if (h1s->recv_wait) { |
| 1432 | h1s->recv_wait->wait_reason &= ~SUB_CAN_RECV; |
| 1433 | tasklet_wakeup(h1s->recv_wait->task); |
| 1434 | h1s->recv_wait = NULL; |
| 1435 | dont_wake = 1; |
| 1436 | } |
| 1437 | if (h1s->send_wait) { |
| 1438 | h1s->send_wait->wait_reason &= ~SUB_CAN_SEND; |
| 1439 | tasklet_wakeup(h1s->send_wait->task); |
| 1440 | h1s->send_wait = NULL; |
| 1441 | dont_wake = 1; |
| 1442 | } |
| 1443 | if (!dont_wake && h1s->cs->data_cb->wake) |
| 1444 | h1s->cs->data_cb->wake(h1s->cs); |
| 1445 | } |
| 1446 | |
| 1447 | /* callback called on any event by the connection handler. |
| 1448 | * It applies changes and returns zero, or < 0 if it wants immediate |
| 1449 | * destruction of the connection. |
| 1450 | */ |
| 1451 | static int h1_process(struct h1c * h1c) |
| 1452 | { |
| 1453 | struct connection *conn = h1c->conn; |
| 1454 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1455 | if (!conn->mux_ctx) |
| 1456 | return -1; |
| 1457 | |
Christopher Faulet | 3b88b8d | 2018-10-26 17:36:03 +0200 | [diff] [blame] | 1458 | if (h1c->flags & H1C_F_CS_WAIT_CONN) { |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1459 | if (!(conn->flags & (CO_FL_CONNECTED|CO_FL_ERROR))) |
| 1460 | goto end; |
| 1461 | h1c->flags &= ~H1C_F_CS_WAIT_CONN; |
Christopher Faulet | 3b88b8d | 2018-10-26 17:36:03 +0200 | [diff] [blame] | 1462 | } |
| 1463 | |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1464 | if (!h1c->h1s) { |
| 1465 | if (h1c->flags & H1C_F_CS_ERROR || |
| 1466 | conn->flags & CO_FL_ERROR || |
| 1467 | conn_xprt_read0_pending(conn)) |
| 1468 | goto release; |
Christopher Faulet | 81d4843 | 2018-11-19 21:22:43 +0100 | [diff] [blame] | 1469 | 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] | 1470 | if (!h1s_create(h1c, NULL)) |
| 1471 | goto release; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1472 | } |
| 1473 | } |
| 1474 | |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1475 | h1_wake_stream(h1c); |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1476 | end: |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1477 | return 0; |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1478 | |
| 1479 | release: |
| 1480 | h1_release(conn); |
| 1481 | return -1; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1482 | } |
| 1483 | |
| 1484 | static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status) |
| 1485 | { |
| 1486 | struct h1c *h1c = ctx; |
| 1487 | int ret = 0; |
| 1488 | |
| 1489 | if (!(h1c->wait_event.wait_reason & SUB_CAN_SEND)) |
| 1490 | ret = h1_send(h1c); |
| 1491 | if (!(h1c->wait_event.wait_reason & SUB_CAN_RECV)) |
| 1492 | ret |= h1_recv(h1c); |
Christopher Faulet | 81d4843 | 2018-11-19 21:22:43 +0100 | [diff] [blame] | 1493 | if (ret || !h1c->h1s) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1494 | h1_process(h1c); |
| 1495 | return NULL; |
| 1496 | } |
| 1497 | |
| 1498 | |
| 1499 | static int h1_wake(struct connection *conn) |
| 1500 | { |
| 1501 | struct h1c *h1c = conn->mux_ctx; |
| 1502 | |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1503 | h1_send(h1c); |
| 1504 | return h1_process(h1c); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1505 | } |
| 1506 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1507 | /*******************************************/ |
| 1508 | /* functions below are used by the streams */ |
| 1509 | /*******************************************/ |
| 1510 | /* |
| 1511 | * Attach a new stream to a connection |
| 1512 | * (Used for outgoing connections) |
| 1513 | */ |
| 1514 | static struct conn_stream *h1_attach(struct connection *conn) |
| 1515 | { |
| 1516 | struct h1c *h1c = conn->mux_ctx; |
| 1517 | struct conn_stream *cs = NULL; |
| 1518 | struct h1s *h1s; |
| 1519 | |
| 1520 | if (h1c->flags & H1C_F_CS_ERROR) |
| 1521 | goto end; |
| 1522 | |
| 1523 | cs = cs_new(h1c->conn); |
| 1524 | if (!cs) |
| 1525 | goto end; |
| 1526 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1527 | h1s = h1s_create(h1c, cs); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1528 | if (h1s == NULL) |
| 1529 | goto end; |
| 1530 | |
| 1531 | return cs; |
| 1532 | end: |
| 1533 | cs_free(cs); |
| 1534 | return NULL; |
| 1535 | } |
| 1536 | |
| 1537 | /* Retrieves a valid conn_stream from this connection, or returns NULL. For |
| 1538 | * this mux, it's easy as we can only store a single conn_stream. |
| 1539 | */ |
| 1540 | static const struct conn_stream *h1_get_first_cs(const struct connection *conn) |
| 1541 | { |
| 1542 | struct h1c *h1c = conn->mux_ctx; |
| 1543 | struct h1s *h1s = h1c->h1s; |
| 1544 | |
| 1545 | if (h1s) |
| 1546 | return h1s->cs; |
| 1547 | |
| 1548 | return NULL; |
| 1549 | } |
| 1550 | |
| 1551 | static void h1_destroy(struct connection *conn) |
| 1552 | { |
| 1553 | struct h1c *h1c = conn->mux_ctx; |
| 1554 | |
| 1555 | if (!h1c->h1s) |
| 1556 | h1_release(conn); |
| 1557 | } |
| 1558 | |
| 1559 | /* |
| 1560 | * Detach the stream from the connection and possibly release the connection. |
| 1561 | */ |
| 1562 | static void h1_detach(struct conn_stream *cs) |
| 1563 | { |
| 1564 | struct h1s *h1s = cs->ctx; |
| 1565 | struct h1c *h1c; |
| 1566 | |
| 1567 | cs->ctx = NULL; |
| 1568 | if (!h1s) |
| 1569 | return; |
| 1570 | |
| 1571 | h1c = h1s->h1c; |
| 1572 | h1s->cs = NULL; |
| 1573 | |
| 1574 | h1s_destroy(h1s); |
| 1575 | |
| 1576 | /* We don't want to close right now unless the connection is in error */ |
| 1577 | if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTW)) || |
| 1578 | (h1c->conn->flags & CO_FL_ERROR)) |
| 1579 | h1_release(h1c->conn); |
| 1580 | else |
| 1581 | tasklet_wakeup(h1c->wait_event.task); |
| 1582 | } |
| 1583 | |
| 1584 | |
| 1585 | static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode) |
| 1586 | { |
| 1587 | struct h1s *h1s = cs->ctx; |
| 1588 | |
| 1589 | if (!h1s) |
| 1590 | return; |
| 1591 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1592 | if ((h1s->flags & H1S_F_WANT_KAL) && !(cs->flags & (CS_FL_REOS|CS_FL_EOS))) |
| 1593 | return; |
| 1594 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1595 | /* NOTE: Be sure to handle abort (cf. h2_shutr) */ |
| 1596 | if (cs->flags & CS_FL_SHR) |
| 1597 | return; |
| 1598 | if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr) |
| 1599 | cs->conn->xprt->shutr(cs->conn, (mode == CS_SHR_DRAIN)); |
| 1600 | if (cs->flags & CS_FL_SHW) { |
| 1601 | h1s->h1c->flags = (h1s->h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTW; |
| 1602 | conn_full_close(cs->conn); |
| 1603 | } |
| 1604 | } |
| 1605 | |
| 1606 | static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode) |
| 1607 | { |
| 1608 | struct h1s *h1s = cs->ctx; |
| 1609 | struct h1c *h1c; |
| 1610 | |
| 1611 | if (!h1s) |
| 1612 | return; |
| 1613 | h1c = h1s->h1c; |
| 1614 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1615 | if ((h1s->flags & H1S_F_WANT_KAL) && |
| 1616 | !(cs->flags & (CS_FL_REOS|CS_FL_EOS)) && |
| 1617 | h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) |
| 1618 | return; |
| 1619 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1620 | h1c->flags |= H1C_F_CS_SHUTW_NOW; |
| 1621 | if ((cs->flags & CS_FL_SHW) || b_data(&h1c->obuf)) |
| 1622 | return; |
| 1623 | |
| 1624 | h1_shutw_conn(cs->conn); |
| 1625 | } |
| 1626 | |
| 1627 | static void h1_shutw_conn(struct connection *conn) |
| 1628 | { |
| 1629 | struct h1c *h1c = conn->mux_ctx; |
| 1630 | |
| 1631 | if (conn_xprt_ready(conn) && conn->xprt->shutw) |
| 1632 | conn->xprt->shutw(conn, 1); |
| 1633 | if (!(conn->flags & CO_FL_SOCK_RD_SH)) |
| 1634 | conn_sock_shutw(conn, 1); |
| 1635 | else { |
| 1636 | h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTW; |
| 1637 | conn_full_close(conn); |
| 1638 | } |
| 1639 | } |
| 1640 | |
| 1641 | /* Called from the upper layer, to unsubscribe to events */ |
| 1642 | static int h1_unsubscribe(struct conn_stream *cs, int event_type, void *param) |
| 1643 | { |
| 1644 | struct wait_event *sw; |
| 1645 | struct h1s *h1s = cs->ctx; |
| 1646 | |
| 1647 | if (!h1s) |
| 1648 | return 0; |
| 1649 | |
| 1650 | if (event_type & SUB_CAN_RECV) { |
| 1651 | sw = param; |
| 1652 | if (h1s->recv_wait == sw) { |
| 1653 | sw->wait_reason &= ~SUB_CAN_RECV; |
| 1654 | h1s->recv_wait = NULL; |
| 1655 | } |
| 1656 | } |
| 1657 | if (event_type & SUB_CAN_SEND) { |
| 1658 | sw = param; |
| 1659 | if (h1s->send_wait == sw) { |
| 1660 | sw->wait_reason &= ~SUB_CAN_SEND; |
| 1661 | h1s->send_wait = NULL; |
| 1662 | } |
| 1663 | } |
| 1664 | return 0; |
| 1665 | } |
| 1666 | |
| 1667 | /* Called from the upper layer, to subscribe to events, such as being able to send */ |
| 1668 | static int h1_subscribe(struct conn_stream *cs, int event_type, void *param) |
| 1669 | { |
| 1670 | struct wait_event *sw; |
| 1671 | struct h1s *h1s = cs->ctx; |
| 1672 | |
| 1673 | if (!h1s) |
| 1674 | return -1; |
| 1675 | |
| 1676 | switch (event_type) { |
| 1677 | case SUB_CAN_RECV: |
| 1678 | sw = param; |
| 1679 | if (!(sw->wait_reason & SUB_CAN_RECV)) { |
| 1680 | sw->wait_reason |= SUB_CAN_RECV; |
| 1681 | sw->handle = h1s; |
| 1682 | h1s->recv_wait = sw; |
| 1683 | } |
| 1684 | return 0; |
| 1685 | case SUB_CAN_SEND: |
| 1686 | sw = param; |
| 1687 | if (!(sw->wait_reason & SUB_CAN_SEND)) { |
| 1688 | sw->wait_reason |= SUB_CAN_SEND; |
| 1689 | sw->handle = h1s; |
| 1690 | h1s->send_wait = sw; |
| 1691 | } |
| 1692 | return 0; |
| 1693 | default: |
| 1694 | break; |
| 1695 | } |
| 1696 | return -1; |
| 1697 | } |
| 1698 | |
| 1699 | /* Called from the upper layer, to receive data */ |
| 1700 | static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags) |
| 1701 | { |
| 1702 | struct h1s *h1s = cs->ctx; |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1703 | struct h1c *h1c = h1s->h1c; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1704 | size_t ret = 0; |
| 1705 | |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1706 | if (!(h1c->flags & H1C_F_IN_ALLOC)) |
| 1707 | ret = h1_process_input(h1c, buf, flags); |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 1708 | |
| 1709 | if (flags & CO_RFL_BUF_FLUSH) |
| 1710 | h1s->flags |= H1S_F_BUF_FLUSH; |
Christopher Faulet | d44ad5b | 2018-11-19 21:52:12 +0100 | [diff] [blame] | 1711 | else if (ret > 0 || (h1s->flags & H1S_F_SPLICED_DATA)) { |
| 1712 | h1s->flags &= ~H1S_F_SPLICED_DATA; |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1713 | if (!(h1c->wait_event.wait_reason & SUB_CAN_RECV)) |
| 1714 | tasklet_wakeup(h1c->wait_event.task); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1715 | } |
| 1716 | return ret; |
| 1717 | } |
| 1718 | |
| 1719 | |
| 1720 | /* Called from the upper layer, to send data */ |
| 1721 | static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags) |
| 1722 | { |
| 1723 | struct h1s *h1s = cs->ctx; |
| 1724 | struct h1c *h1c; |
| 1725 | size_t ret = 0; |
| 1726 | |
| 1727 | if (!h1s) |
| 1728 | return ret; |
| 1729 | |
| 1730 | h1c = h1s->h1c; |
| 1731 | |
Christopher Faulet | 3b88b8d | 2018-10-26 17:36:03 +0200 | [diff] [blame] | 1732 | if (h1c->flags & H1C_F_CS_WAIT_CONN) |
| 1733 | return 0; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1734 | |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1735 | if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC))) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1736 | ret = h1_process_output(h1c, buf, count); |
| 1737 | if (ret > 0) { |
| 1738 | h1_send(h1c); |
| 1739 | |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1740 | /* We need to do that because of the infinite forwarding. <buf> |
| 1741 | * contains HTX messages so when infinite forwarding is enabled, |
| 1742 | * count is equal to the buffer size. From outside, the buffer |
| 1743 | * appears as full. |
| 1744 | */ |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1745 | if (!b_data(buf)) |
| 1746 | ret = count; |
| 1747 | } |
| 1748 | return ret; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1749 | } |
| 1750 | |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 1751 | #if defined(CONFIG_HAP_LINUX_SPLICE) |
| 1752 | /* Send and get, using splicing */ |
| 1753 | static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count) |
| 1754 | { |
| 1755 | struct h1s *h1s = cs->ctx; |
| 1756 | struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res); |
| 1757 | int ret = 0; |
| 1758 | |
Christopher Faulet | d44ad5b | 2018-11-19 21:52:12 +0100 | [diff] [blame] | 1759 | if (b_data(&h1s->h1c->ibuf)) { |
| 1760 | h1s->flags |= H1S_F_BUF_FLUSH; |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 1761 | goto end; |
Christopher Faulet | d44ad5b | 2018-11-19 21:52:12 +0100 | [diff] [blame] | 1762 | } |
| 1763 | |
| 1764 | h1s->flags &= ~H1S_F_BUF_FLUSH; |
| 1765 | h1s->flags |= H1S_F_SPLICED_DATA; |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 1766 | if (h1m->state == H1_MSG_DATA && count > h1m->curr_len) |
| 1767 | count = h1m->curr_len; |
| 1768 | ret = cs->conn->xprt->rcv_pipe(cs->conn, pipe, count); |
| 1769 | if (h1m->state == H1_MSG_DATA && ret > 0) |
| 1770 | h1m->curr_len -= ret; |
| 1771 | end: |
| 1772 | return ret; |
| 1773 | |
| 1774 | } |
| 1775 | |
| 1776 | static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe) |
| 1777 | { |
| 1778 | struct h1s *h1s = cs->ctx; |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 1779 | int ret = 0; |
| 1780 | |
| 1781 | if (b_data(&h1s->h1c->obuf)) |
| 1782 | goto end; |
| 1783 | |
| 1784 | ret = cs->conn->xprt->snd_pipe(cs->conn, pipe); |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 1785 | end: |
Christopher Faulet | d44ad5b | 2018-11-19 21:52:12 +0100 | [diff] [blame] | 1786 | if (pipe->data) { |
| 1787 | if (!(h1s->h1c->wait_event.wait_reason & SUB_CAN_SEND)) |
| 1788 | cs->conn->xprt->subscribe(cs->conn, SUB_CAN_SEND, &h1s->h1c->wait_event); |
| 1789 | } |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 1790 | return ret; |
| 1791 | } |
| 1792 | #endif |
| 1793 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1794 | /****************************************/ |
| 1795 | /* MUX initialization and instanciation */ |
| 1796 | /****************************************/ |
| 1797 | |
| 1798 | /* The mux operations */ |
| 1799 | const struct mux_ops mux_h1_ops = { |
| 1800 | .init = h1_init, |
| 1801 | .wake = h1_wake, |
| 1802 | .attach = h1_attach, |
| 1803 | .get_first_cs = h1_get_first_cs, |
| 1804 | .detach = h1_detach, |
| 1805 | .destroy = h1_destroy, |
| 1806 | .avail_streams = h1_avail_streams, |
| 1807 | .rcv_buf = h1_rcv_buf, |
| 1808 | .snd_buf = h1_snd_buf, |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 1809 | #if defined(CONFIG_HAP_LINUX_SPLICE) |
| 1810 | .rcv_pipe = h1_rcv_pipe, |
| 1811 | .snd_pipe = h1_snd_pipe, |
| 1812 | #endif |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1813 | .subscribe = h1_subscribe, |
| 1814 | .unsubscribe = h1_unsubscribe, |
| 1815 | .shutr = h1_shutr, |
| 1816 | .shutw = h1_shutw, |
| 1817 | .flags = MX_FL_NONE, |
| 1818 | .name = "h1", |
| 1819 | }; |
| 1820 | |
| 1821 | |
| 1822 | /* this mux registers default HTX proto */ |
| 1823 | static struct mux_proto_list mux_proto_htx = |
| 1824 | { .token = IST(""), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops }; |
| 1825 | |
| 1826 | static void __h1_deinit(void) |
| 1827 | { |
| 1828 | pool_destroy(pool_head_h1c); |
| 1829 | pool_destroy(pool_head_h1s); |
| 1830 | } |
| 1831 | |
| 1832 | __attribute__((constructor)) |
| 1833 | static void __h1_init(void) |
| 1834 | { |
| 1835 | register_mux_proto(&mux_proto_htx); |
| 1836 | hap_register_post_deinit(__h1_deinit); |
| 1837 | pool_head_h1c = create_pool("h1c", sizeof(struct h1c), MEM_F_SHARED); |
| 1838 | pool_head_h1s = create_pool("h1s", sizeof(struct h1s), MEM_F_SHARED); |
| 1839 | } |
| 1840 | /* |
| 1841 | * Local variables: |
| 1842 | * c-indent-level: 8 |
| 1843 | * c-basic-offset: 8 |
| 1844 | * End: |
| 1845 | */ |