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