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