Amaury Denoyelle | 154bc7f | 2021-11-12 16:09:54 +0100 | [diff] [blame] | 1 | #include <haproxy/hq_interop.h> |
| 2 | |
| 3 | #include <import/ist.h> |
| 4 | #include <haproxy/buf.h> |
| 5 | #include <haproxy/connection.h> |
| 6 | #include <haproxy/dynbuf.h> |
| 7 | #include <haproxy/htx.h> |
| 8 | #include <haproxy/http.h> |
Amaury Denoyelle | 846cc04 | 2022-04-04 16:13:44 +0200 | [diff] [blame] | 9 | #include <haproxy/mux_quic.h> |
Amaury Denoyelle | 381d813 | 2023-02-17 09:51:20 +0100 | [diff] [blame] | 10 | #include <haproxy/qmux_http.h> |
Amaury Denoyelle | 154bc7f | 2021-11-12 16:09:54 +0100 | [diff] [blame] | 11 | |
Amaury Denoyelle | 1f21ebd | 2022-06-07 17:30:55 +0200 | [diff] [blame] | 12 | static ssize_t hq_interop_decode_qcs(struct qcs *qcs, struct buffer *b, int fin) |
Amaury Denoyelle | 154bc7f | 2021-11-12 16:09:54 +0100 | [diff] [blame] | 13 | { |
Amaury Denoyelle | 154bc7f | 2021-11-12 16:09:54 +0100 | [diff] [blame] | 14 | struct htx *htx; |
| 15 | struct htx_sl *sl; |
Amaury Denoyelle | 154bc7f | 2021-11-12 16:09:54 +0100 | [diff] [blame] | 16 | struct buffer htx_buf = BUF_NULL; |
| 17 | struct ist path; |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 18 | char *ptr = b_head(b); |
Amaury Denoyelle | 62eef85 | 2022-06-03 16:40:34 +0200 | [diff] [blame] | 19 | size_t data = b_data(b); |
Amaury Denoyelle | 154bc7f | 2021-11-12 16:09:54 +0100 | [diff] [blame] | 20 | |
Amaury Denoyelle | c5bc10f | 2023-10-04 15:46:06 +0200 | [diff] [blame] | 21 | /* hq-interop parser does not support buffer wrapping. */ |
| 22 | BUG_ON(b_data(b) != b_contig_data(b, 0)); |
Amaury Denoyelle | 93dd23c | 2023-05-11 16:49:28 +0200 | [diff] [blame] | 23 | |
Amaury Denoyelle | c5bc10f | 2023-10-04 15:46:06 +0200 | [diff] [blame] | 24 | /* hq-interop parser is only done once full message is received. */ |
| 25 | if (!fin) |
Amaury Denoyelle | 381d813 | 2023-02-17 09:51:20 +0100 | [diff] [blame] | 26 | return 0; |
Amaury Denoyelle | 381d813 | 2023-02-17 09:51:20 +0100 | [diff] [blame] | 27 | |
Amaury Denoyelle | 154bc7f | 2021-11-12 16:09:54 +0100 | [diff] [blame] | 28 | b_alloc(&htx_buf); |
| 29 | htx = htx_from_buf(&htx_buf); |
| 30 | |
| 31 | /* skip method */ |
Frédéric Lécaille | afd373c | 2021-12-15 22:38:48 +0100 | [diff] [blame] | 32 | while (data && HTTP_IS_TOKEN(*ptr)) { |
Amaury Denoyelle | c5bc10f | 2023-10-04 15:46:06 +0200 | [diff] [blame] | 33 | ptr++; |
Frédéric Lécaille | afd373c | 2021-12-15 22:38:48 +0100 | [diff] [blame] | 34 | data--; |
| 35 | } |
| 36 | |
| 37 | if (!data || !HTTP_IS_SPHT(*ptr)) { |
| 38 | fprintf(stderr, "truncated stream\n"); |
Amaury Denoyelle | c5bc10f | 2023-10-04 15:46:06 +0200 | [diff] [blame] | 39 | return -1; |
Frédéric Lécaille | afd373c | 2021-12-15 22:38:48 +0100 | [diff] [blame] | 40 | } |
| 41 | |
Amaury Denoyelle | c5bc10f | 2023-10-04 15:46:06 +0200 | [diff] [blame] | 42 | ptr++; |
Frédéric Lécaille | afd373c | 2021-12-15 22:38:48 +0100 | [diff] [blame] | 43 | if (!--data) { |
| 44 | fprintf(stderr, "truncated stream\n"); |
Amaury Denoyelle | c5bc10f | 2023-10-04 15:46:06 +0200 | [diff] [blame] | 45 | return -1; |
| 46 | } |
| 47 | |
| 48 | if (HTTP_IS_LWS(*ptr)) { |
| 49 | fprintf(stderr, "malformed stream\n"); |
| 50 | return -1; |
Frédéric Lécaille | afd373c | 2021-12-15 22:38:48 +0100 | [diff] [blame] | 51 | } |
Amaury Denoyelle | 154bc7f | 2021-11-12 16:09:54 +0100 | [diff] [blame] | 52 | |
| 53 | /* extract path */ |
Amaury Denoyelle | 154bc7f | 2021-11-12 16:09:54 +0100 | [diff] [blame] | 54 | path.ptr = ptr; |
Frédéric Lécaille | afd373c | 2021-12-15 22:38:48 +0100 | [diff] [blame] | 55 | while (data && !HTTP_IS_LWS(*ptr)) { |
Amaury Denoyelle | c5bc10f | 2023-10-04 15:46:06 +0200 | [diff] [blame] | 56 | ptr++; |
Frédéric Lécaille | afd373c | 2021-12-15 22:38:48 +0100 | [diff] [blame] | 57 | data--; |
| 58 | } |
| 59 | |
| 60 | if (!data) { |
| 61 | fprintf(stderr, "truncated stream\n"); |
Amaury Denoyelle | c5bc10f | 2023-10-04 15:46:06 +0200 | [diff] [blame] | 62 | return -1; |
Frédéric Lécaille | afd373c | 2021-12-15 22:38:48 +0100 | [diff] [blame] | 63 | } |
| 64 | |
Amaury Denoyelle | 154bc7f | 2021-11-12 16:09:54 +0100 | [diff] [blame] | 65 | path.len = ptr - path.ptr; |
| 66 | |
| 67 | sl = htx_add_stline(htx, HTX_BLK_REQ_SL, 0, ist("GET"), path, ist("HTTP/1.0")); |
Amaury Denoyelle | b48c59a | 2021-11-18 14:40:26 +0100 | [diff] [blame] | 68 | if (!sl) |
Amaury Denoyelle | 1f21ebd | 2022-06-07 17:30:55 +0200 | [diff] [blame] | 69 | return -1; |
Amaury Denoyelle | b48c59a | 2021-11-18 14:40:26 +0100 | [diff] [blame] | 70 | |
Amaury Denoyelle | 154bc7f | 2021-11-12 16:09:54 +0100 | [diff] [blame] | 71 | sl->flags |= HTX_SL_F_BODYLESS; |
| 72 | sl->info.req.meth = find_http_meth("GET", 3); |
| 73 | |
| 74 | htx_add_endof(htx, HTX_BLK_EOH); |
Amaury Denoyelle | c5bc10f | 2023-10-04 15:46:06 +0200 | [diff] [blame] | 75 | htx->flags |= HTX_FL_EOM; |
Amaury Denoyelle | 154bc7f | 2021-11-12 16:09:54 +0100 | [diff] [blame] | 76 | htx_to_buf(htx, &htx_buf); |
| 77 | |
Amaury Denoyelle | d68f8b5 | 2023-05-30 15:04:46 +0200 | [diff] [blame] | 78 | if (!qcs_attach_sc(qcs, &htx_buf, fin)) |
Amaury Denoyelle | 1f21ebd | 2022-06-07 17:30:55 +0200 | [diff] [blame] | 79 | return -1; |
Amaury Denoyelle | 154bc7f | 2021-11-12 16:09:54 +0100 | [diff] [blame] | 80 | |
Amaury Denoyelle | 154bc7f | 2021-11-12 16:09:54 +0100 | [diff] [blame] | 81 | b_free(&htx_buf); |
| 82 | |
Amaury Denoyelle | 1f21ebd | 2022-06-07 17:30:55 +0200 | [diff] [blame] | 83 | return b_data(b); |
Amaury Denoyelle | 154bc7f | 2021-11-12 16:09:54 +0100 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | static struct buffer *mux_get_buf(struct qcs *qcs) |
| 87 | { |
| 88 | if (!b_size(&qcs->tx.buf)) |
| 89 | b_alloc(&qcs->tx.buf); |
| 90 | |
| 91 | return &qcs->tx.buf; |
| 92 | } |
| 93 | |
Amaury Denoyelle | 9534e59 | 2022-09-19 17:14:27 +0200 | [diff] [blame] | 94 | static size_t hq_interop_snd_buf(struct qcs *qcs, struct htx *htx, |
| 95 | size_t count) |
Amaury Denoyelle | 154bc7f | 2021-11-12 16:09:54 +0100 | [diff] [blame] | 96 | { |
Amaury Denoyelle | 154bc7f | 2021-11-12 16:09:54 +0100 | [diff] [blame] | 97 | enum htx_blk_type btype; |
| 98 | struct htx_blk *blk; |
| 99 | int32_t idx; |
| 100 | uint32_t bsize, fsize; |
| 101 | struct buffer *res, outbuf; |
| 102 | size_t total = 0; |
| 103 | |
Amaury Denoyelle | 154bc7f | 2021-11-12 16:09:54 +0100 | [diff] [blame] | 104 | res = mux_get_buf(qcs); |
| 105 | outbuf = b_make(b_tail(res), b_contig_space(res), 0, 0); |
| 106 | |
Amaury Denoyelle | 84ea8dc | 2021-12-03 14:40:01 +0100 | [diff] [blame] | 107 | while (count && !htx_is_empty(htx) && !(qcs->flags & QC_SF_BLK_MROOM)) { |
Amaury Denoyelle | 154bc7f | 2021-11-12 16:09:54 +0100 | [diff] [blame] | 108 | /* Not implemented : QUIC on backend side */ |
| 109 | idx = htx_get_head(htx); |
| 110 | blk = htx_get_blk(htx, idx); |
| 111 | btype = htx_get_blk_type(blk); |
| 112 | fsize = bsize = htx_get_blksz(blk); |
| 113 | |
| 114 | BUG_ON(btype == HTX_BLK_REQ_SL); |
| 115 | |
| 116 | switch (btype) { |
| 117 | case HTX_BLK_DATA: |
| 118 | if (fsize > count) |
| 119 | fsize = count; |
Amaury Denoyelle | 5ede40b | 2021-12-07 16:19:03 +0100 | [diff] [blame] | 120 | |
Amaury Denoyelle | 1ac9544 | 2021-12-09 10:07:23 +0100 | [diff] [blame] | 121 | if (b_room(&outbuf) < fsize) |
| 122 | fsize = b_room(&outbuf); |
Amaury Denoyelle | 5ede40b | 2021-12-07 16:19:03 +0100 | [diff] [blame] | 123 | |
| 124 | if (!fsize) { |
| 125 | qcs->flags |= QC_SF_BLK_MROOM; |
| 126 | goto end; |
| 127 | } |
| 128 | |
Amaury Denoyelle | 154bc7f | 2021-11-12 16:09:54 +0100 | [diff] [blame] | 129 | b_putblk(&outbuf, htx_get_blk_ptr(htx, blk), fsize); |
| 130 | total += fsize; |
| 131 | count -= fsize; |
| 132 | |
| 133 | if (fsize == bsize) |
| 134 | htx_remove_blk(htx, blk); |
| 135 | else |
| 136 | htx_cut_data_blk(htx, blk, fsize); |
| 137 | break; |
| 138 | |
Ilya Shipitsin | 5e87bcf | 2021-12-25 11:45:52 +0500 | [diff] [blame] | 139 | /* only body is transferred on HTTP/0.9 */ |
Amaury Denoyelle | 154bc7f | 2021-11-12 16:09:54 +0100 | [diff] [blame] | 140 | case HTX_BLK_RES_SL: |
| 141 | case HTX_BLK_TLR: |
| 142 | case HTX_BLK_EOT: |
| 143 | default: |
| 144 | htx_remove_blk(htx, blk); |
| 145 | total += bsize; |
| 146 | count -= bsize; |
| 147 | break; |
| 148 | } |
| 149 | } |
| 150 | |
Amaury Denoyelle | 5ede40b | 2021-12-07 16:19:03 +0100 | [diff] [blame] | 151 | end: |
Amaury Denoyelle | 154bc7f | 2021-11-12 16:09:54 +0100 | [diff] [blame] | 152 | b_add(res, b_data(&outbuf)); |
| 153 | |
Amaury Denoyelle | 154bc7f | 2021-11-12 16:09:54 +0100 | [diff] [blame] | 154 | return total; |
| 155 | } |
| 156 | |
Amaury Denoyelle | 30e260e | 2022-08-03 11:17:57 +0200 | [diff] [blame] | 157 | static int hq_interop_attach(struct qcs *qcs, void *conn_ctx) |
| 158 | { |
| 159 | qcs_wait_http_req(qcs); |
| 160 | return 0; |
| 161 | } |
| 162 | |
Amaury Denoyelle | 154bc7f | 2021-11-12 16:09:54 +0100 | [diff] [blame] | 163 | const struct qcc_app_ops hq_interop_ops = { |
| 164 | .decode_qcs = hq_interop_decode_qcs, |
| 165 | .snd_buf = hq_interop_snd_buf, |
Amaury Denoyelle | 30e260e | 2022-08-03 11:17:57 +0200 | [diff] [blame] | 166 | .attach = hq_interop_attach, |
Amaury Denoyelle | 154bc7f | 2021-11-12 16:09:54 +0100 | [diff] [blame] | 167 | }; |