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