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