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