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