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> |
| 9 | #include <haproxy/mux_quic-t.h> |
| 10 | #include <haproxy/stream.h> |
| 11 | |
Amaury Denoyelle | db44338 | 2021-11-30 11:23:29 +0100 | [diff] [blame] | 12 | static int hq_interop_decode_qcs(struct qcs *qcs, int fin, void *ctx) |
Amaury Denoyelle | 154bc7f | 2021-11-12 16:09:54 +0100 | [diff] [blame] | 13 | { |
| 14 | struct buffer *rxbuf = &qcs->rx.buf; |
| 15 | struct htx *htx; |
| 16 | struct htx_sl *sl; |
| 17 | struct conn_stream *cs; |
| 18 | struct buffer htx_buf = BUF_NULL; |
| 19 | struct ist path; |
Frédéric Lécaille | afd373c | 2021-12-15 22:38:48 +0100 | [diff] [blame] | 20 | char *ptr = b_head(rxbuf); |
| 21 | char *end = b_wrap(rxbuf); |
| 22 | size_t size = b_size(rxbuf); |
| 23 | size_t data = b_data(rxbuf); |
Amaury Denoyelle | 154bc7f | 2021-11-12 16:09:54 +0100 | [diff] [blame] | 24 | |
| 25 | b_alloc(&htx_buf); |
| 26 | htx = htx_from_buf(&htx_buf); |
| 27 | |
| 28 | /* skip method */ |
Frédéric Lécaille | afd373c | 2021-12-15 22:38:48 +0100 | [diff] [blame] | 29 | while (data && HTTP_IS_TOKEN(*ptr)) { |
| 30 | if (++ptr == end) |
| 31 | ptr -= size; |
| 32 | data--; |
| 33 | } |
| 34 | |
| 35 | if (!data || !HTTP_IS_SPHT(*ptr)) { |
| 36 | fprintf(stderr, "truncated stream\n"); |
| 37 | return 0; |
| 38 | } |
| 39 | |
| 40 | if (++ptr == end) |
| 41 | ptr -= size; |
| 42 | |
| 43 | if (!--data) { |
| 44 | fprintf(stderr, "truncated stream\n"); |
| 45 | return 0; |
| 46 | } |
Amaury Denoyelle | 154bc7f | 2021-11-12 16:09:54 +0100 | [diff] [blame] | 47 | |
| 48 | /* extract path */ |
| 49 | BUG_ON(HTTP_IS_LWS(*ptr)); |
| 50 | path.ptr = ptr; |
Frédéric Lécaille | afd373c | 2021-12-15 22:38:48 +0100 | [diff] [blame] | 51 | while (data && !HTTP_IS_LWS(*ptr)) { |
| 52 | if (++ptr == end) |
| 53 | ptr -= size; |
| 54 | data--; |
| 55 | } |
| 56 | |
| 57 | if (!data) { |
| 58 | fprintf(stderr, "truncated stream\n"); |
| 59 | return 0; |
| 60 | } |
| 61 | |
Amaury Denoyelle | 154bc7f | 2021-11-12 16:09:54 +0100 | [diff] [blame] | 62 | BUG_ON(!HTTP_IS_LWS(*ptr)); |
| 63 | path.len = ptr - path.ptr; |
| 64 | |
| 65 | 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] | 66 | if (!sl) |
| 67 | return -1; |
| 68 | |
Amaury Denoyelle | 154bc7f | 2021-11-12 16:09:54 +0100 | [diff] [blame] | 69 | sl->flags |= HTX_SL_F_BODYLESS; |
| 70 | sl->info.req.meth = find_http_meth("GET", 3); |
| 71 | |
| 72 | htx_add_endof(htx, HTX_BLK_EOH); |
| 73 | htx_to_buf(htx, &htx_buf); |
| 74 | |
| 75 | cs = cs_new(qcs->qcc->conn, qcs->qcc->conn->target); |
| 76 | cs->ctx = qcs; |
| 77 | stream_create_from_cs(cs, &htx_buf); |
| 78 | |
| 79 | b_del(rxbuf, b_data(rxbuf)); |
| 80 | b_free(&htx_buf); |
| 81 | |
Amaury Denoyelle | db44338 | 2021-11-30 11:23:29 +0100 | [diff] [blame] | 82 | if (fin) |
| 83 | htx->flags |= HTX_FL_EOM; |
| 84 | |
Amaury Denoyelle | 154bc7f | 2021-11-12 16:09:54 +0100 | [diff] [blame] | 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | static struct buffer *mux_get_buf(struct qcs *qcs) |
| 89 | { |
| 90 | if (!b_size(&qcs->tx.buf)) |
| 91 | b_alloc(&qcs->tx.buf); |
| 92 | |
| 93 | return &qcs->tx.buf; |
| 94 | } |
| 95 | |
| 96 | static size_t hq_interop_snd_buf(struct conn_stream *cs, struct buffer *buf, |
| 97 | size_t count, int flags) |
| 98 | { |
| 99 | struct qcs *qcs = cs->ctx; |
| 100 | struct htx *htx; |
| 101 | enum htx_blk_type btype; |
| 102 | struct htx_blk *blk; |
| 103 | int32_t idx; |
| 104 | uint32_t bsize, fsize; |
| 105 | struct buffer *res, outbuf; |
| 106 | size_t total = 0; |
| 107 | |
| 108 | htx = htx_from_buf(buf); |
| 109 | res = mux_get_buf(qcs); |
| 110 | outbuf = b_make(b_tail(res), b_contig_space(res), 0, 0); |
| 111 | |
Amaury Denoyelle | 84ea8dc | 2021-12-03 14:40:01 +0100 | [diff] [blame] | 112 | while (count && !htx_is_empty(htx) && !(qcs->flags & QC_SF_BLK_MROOM)) { |
Amaury Denoyelle | 154bc7f | 2021-11-12 16:09:54 +0100 | [diff] [blame] | 113 | /* Not implemented : QUIC on backend side */ |
| 114 | idx = htx_get_head(htx); |
| 115 | blk = htx_get_blk(htx, idx); |
| 116 | btype = htx_get_blk_type(blk); |
| 117 | fsize = bsize = htx_get_blksz(blk); |
| 118 | |
| 119 | BUG_ON(btype == HTX_BLK_REQ_SL); |
| 120 | |
| 121 | switch (btype) { |
| 122 | case HTX_BLK_DATA: |
| 123 | if (fsize > count) |
| 124 | fsize = count; |
Amaury Denoyelle | 5ede40b | 2021-12-07 16:19:03 +0100 | [diff] [blame] | 125 | |
Amaury Denoyelle | 1ac9544 | 2021-12-09 10:07:23 +0100 | [diff] [blame] | 126 | if (b_room(&outbuf) < fsize) |
| 127 | fsize = b_room(&outbuf); |
Amaury Denoyelle | 5ede40b | 2021-12-07 16:19:03 +0100 | [diff] [blame] | 128 | |
| 129 | if (!fsize) { |
| 130 | qcs->flags |= QC_SF_BLK_MROOM; |
| 131 | goto end; |
| 132 | } |
| 133 | |
Amaury Denoyelle | 154bc7f | 2021-11-12 16:09:54 +0100 | [diff] [blame] | 134 | b_putblk(&outbuf, htx_get_blk_ptr(htx, blk), fsize); |
| 135 | total += fsize; |
| 136 | count -= fsize; |
| 137 | |
| 138 | if (fsize == bsize) |
| 139 | htx_remove_blk(htx, blk); |
| 140 | else |
| 141 | htx_cut_data_blk(htx, blk, fsize); |
| 142 | break; |
| 143 | |
| 144 | /* only body is transfered on HTTP/0.9 */ |
| 145 | case HTX_BLK_RES_SL: |
| 146 | case HTX_BLK_TLR: |
| 147 | case HTX_BLK_EOT: |
| 148 | default: |
| 149 | htx_remove_blk(htx, blk); |
| 150 | total += bsize; |
| 151 | count -= bsize; |
| 152 | break; |
| 153 | } |
| 154 | } |
| 155 | |
Amaury Denoyelle | 5ede40b | 2021-12-07 16:19:03 +0100 | [diff] [blame] | 156 | end: |
Amaury Denoyelle | c2025c1 | 2021-12-03 15:03:36 +0100 | [diff] [blame] | 157 | if ((htx->flags & HTX_FL_EOM) && htx_is_empty(htx)) |
| 158 | qcs->flags |= QC_SF_FIN_STREAM; |
| 159 | |
Amaury Denoyelle | 154bc7f | 2021-11-12 16:09:54 +0100 | [diff] [blame] | 160 | b_add(res, b_data(&outbuf)); |
| 161 | |
| 162 | if (total) { |
| 163 | if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND)) |
| 164 | tasklet_wakeup(qcs->qcc->wait_event.tasklet); |
| 165 | } |
| 166 | |
| 167 | return total; |
| 168 | } |
| 169 | |
| 170 | const struct qcc_app_ops hq_interop_ops = { |
| 171 | .decode_qcs = hq_interop_decode_qcs, |
| 172 | .snd_buf = hq_interop_snd_buf, |
| 173 | }; |