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