blob: 075e785b04514adc201894ea30d095f1c16beeb9 [file] [log] [blame]
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +01001#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
12static int hq_interop_decode_qcs(struct qcs *qcs, void *ctx)
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;
20 char *ptr;
21
22 b_alloc(&htx_buf);
23 htx = htx_from_buf(&htx_buf);
24
25 /* skip method */
26 ptr = b_orig(rxbuf);
27 while (HTTP_IS_TOKEN(*ptr))
28 ++ptr;
29 BUG_ON(!HTTP_IS_SPHT(*ptr));
30 ++ptr;
31
32 /* extract path */
33 BUG_ON(HTTP_IS_LWS(*ptr));
34 path.ptr = ptr;
35 while (!HTTP_IS_LWS(*ptr))
36 ++ptr;
37 BUG_ON(!HTTP_IS_LWS(*ptr));
38 path.len = ptr - path.ptr;
39
40 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, 0, ist("GET"), path, ist("HTTP/1.0"));
Amaury Denoyelleb48c59a2021-11-18 14:40:26 +010041 if (!sl)
42 return -1;
43
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +010044 sl->flags |= HTX_SL_F_BODYLESS;
45 sl->info.req.meth = find_http_meth("GET", 3);
46
47 htx_add_endof(htx, HTX_BLK_EOH);
48 htx_to_buf(htx, &htx_buf);
49
50 cs = cs_new(qcs->qcc->conn, qcs->qcc->conn->target);
51 cs->ctx = qcs;
52 stream_create_from_cs(cs, &htx_buf);
53
54 b_del(rxbuf, b_data(rxbuf));
55 b_free(&htx_buf);
56
57 return 0;
58}
59
60static struct buffer *mux_get_buf(struct qcs *qcs)
61{
62 if (!b_size(&qcs->tx.buf))
63 b_alloc(&qcs->tx.buf);
64
65 return &qcs->tx.buf;
66}
67
68static size_t hq_interop_snd_buf(struct conn_stream *cs, struct buffer *buf,
69 size_t count, int flags)
70{
71 struct qcs *qcs = cs->ctx;
72 struct htx *htx;
73 enum htx_blk_type btype;
74 struct htx_blk *blk;
75 int32_t idx;
76 uint32_t bsize, fsize;
77 struct buffer *res, outbuf;
78 size_t total = 0;
79
80 htx = htx_from_buf(buf);
81 res = mux_get_buf(qcs);
82 outbuf = b_make(b_tail(res), b_contig_space(res), 0, 0);
83
84 while (count && !htx_is_empty(htx) && !(qcs->flags & QC_SF_BLK_MROOM)) {
85 /* Not implemented : QUIC on backend side */
86 idx = htx_get_head(htx);
87 blk = htx_get_blk(htx, idx);
88 btype = htx_get_blk_type(blk);
89 fsize = bsize = htx_get_blksz(blk);
90
91 BUG_ON(btype == HTX_BLK_REQ_SL);
92
93 switch (btype) {
94 case HTX_BLK_DATA:
95 if (fsize > count)
96 fsize = count;
97 b_putblk(&outbuf, htx_get_blk_ptr(htx, blk), fsize);
98 total += fsize;
99 count -= fsize;
100
101 if (fsize == bsize)
102 htx_remove_blk(htx, blk);
103 else
104 htx_cut_data_blk(htx, blk, fsize);
105 break;
106
107 /* only body is transfered on HTTP/0.9 */
108 case HTX_BLK_RES_SL:
109 case HTX_BLK_TLR:
110 case HTX_BLK_EOT:
111 default:
112 htx_remove_blk(htx, blk);
113 total += bsize;
114 count -= bsize;
115 break;
116 }
117 }
118
119 if ((htx->flags & HTX_FL_EOM) && htx_is_empty(htx))
120 qcs->flags |= QC_SF_FIN_STREAM;
121
122 b_add(res, b_data(&outbuf));
123
124 if (total) {
125 if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND))
126 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
127 }
128
129 return total;
130}
131
132const struct qcc_app_ops hq_interop_ops = {
133 .decode_qcs = hq_interop_decode_qcs,
134 .snd_buf = hq_interop_snd_buf,
135};