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