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