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