blob: 0e5561d8aa9792b13c3ebbeae5eda4e0fff02fd2 [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
Amaury Denoyelledb443382021-11-30 11:23:29 +010012static int hq_interop_decode_qcs(struct qcs *qcs, int fin, void *ctx)
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +010013{
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écailleafd373c2021-12-15 22:38:48 +010020 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 Denoyelle154bc7f2021-11-12 16:09:54 +010024
25 b_alloc(&htx_buf);
26 htx = htx_from_buf(&htx_buf);
27
28 /* skip method */
Frédéric Lécailleafd373c2021-12-15 22:38:48 +010029 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 Denoyelle154bc7f2021-11-12 16:09:54 +010047
48 /* extract path */
49 BUG_ON(HTTP_IS_LWS(*ptr));
50 path.ptr = ptr;
Frédéric Lécailleafd373c2021-12-15 22:38:48 +010051 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 Denoyelle154bc7f2021-11-12 16:09:54 +010062 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 Denoyelleb48c59a2021-11-18 14:40:26 +010066 if (!sl)
67 return -1;
68
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +010069 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
Christopher Fauletcda94ac2021-12-23 17:28:17 +010075 cs = cs_new();
Frédéric Lécaille1e1fb5d2022-02-15 09:13:05 +010076 if (!cs)
77 return -1;
Christopher Fauletcda94ac2021-12-23 17:28:17 +010078 cs_attach_endp(cs, &qcs->qcc->conn->obj_type, qcs);
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +010079 cs->ctx = qcs;
Christopher Faulet9264a2c2022-02-24 11:13:57 +010080 stream_new(qcs->qcc->conn->owner, cs, &htx_buf);
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +010081
82 b_del(rxbuf, b_data(rxbuf));
83 b_free(&htx_buf);
84
Amaury Denoyelledb443382021-11-30 11:23:29 +010085 if (fin)
86 htx->flags |= HTX_FL_EOM;
87
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +010088 return 0;
89}
90
91static struct buffer *mux_get_buf(struct qcs *qcs)
92{
93 if (!b_size(&qcs->tx.buf))
94 b_alloc(&qcs->tx.buf);
95
96 return &qcs->tx.buf;
97}
98
99static size_t hq_interop_snd_buf(struct conn_stream *cs, struct buffer *buf,
100 size_t count, int flags)
101{
102 struct qcs *qcs = cs->ctx;
103 struct htx *htx;
104 enum htx_blk_type btype;
105 struct htx_blk *blk;
106 int32_t idx;
107 uint32_t bsize, fsize;
108 struct buffer *res, outbuf;
109 size_t total = 0;
110
111 htx = htx_from_buf(buf);
112 res = mux_get_buf(qcs);
113 outbuf = b_make(b_tail(res), b_contig_space(res), 0, 0);
114
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +0100115 while (count && !htx_is_empty(htx) && !(qcs->flags & QC_SF_BLK_MROOM)) {
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +0100116 /* Not implemented : QUIC on backend side */
117 idx = htx_get_head(htx);
118 blk = htx_get_blk(htx, idx);
119 btype = htx_get_blk_type(blk);
120 fsize = bsize = htx_get_blksz(blk);
121
122 BUG_ON(btype == HTX_BLK_REQ_SL);
123
124 switch (btype) {
125 case HTX_BLK_DATA:
126 if (fsize > count)
127 fsize = count;
Amaury Denoyelle5ede40b2021-12-07 16:19:03 +0100128
Amaury Denoyelle1ac95442021-12-09 10:07:23 +0100129 if (b_room(&outbuf) < fsize)
130 fsize = b_room(&outbuf);
Amaury Denoyelle5ede40b2021-12-07 16:19:03 +0100131
132 if (!fsize) {
133 qcs->flags |= QC_SF_BLK_MROOM;
134 goto end;
135 }
136
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +0100137 b_putblk(&outbuf, htx_get_blk_ptr(htx, blk), fsize);
138 total += fsize;
139 count -= fsize;
140
141 if (fsize == bsize)
142 htx_remove_blk(htx, blk);
143 else
144 htx_cut_data_blk(htx, blk, fsize);
145 break;
146
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +0500147 /* only body is transferred on HTTP/0.9 */
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +0100148 case HTX_BLK_RES_SL:
149 case HTX_BLK_TLR:
150 case HTX_BLK_EOT:
151 default:
152 htx_remove_blk(htx, blk);
153 total += bsize;
154 count -= bsize;
155 break;
156 }
157 }
158
Amaury Denoyelle5ede40b2021-12-07 16:19:03 +0100159 end:
Amaury Denoyellec2025c12021-12-03 15:03:36 +0100160 if ((htx->flags & HTX_FL_EOM) && htx_is_empty(htx))
161 qcs->flags |= QC_SF_FIN_STREAM;
162
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +0100163 b_add(res, b_data(&outbuf));
164
165 if (total) {
166 if (!(qcs->qcc->wait_event.events & SUB_RETRY_SEND))
167 tasklet_wakeup(qcs->qcc->wait_event.tasklet);
168 }
169
170 return total;
171}
172
173const struct qcc_app_ops hq_interop_ops = {
174 .decode_qcs = hq_interop_decode_qcs,
175 .snd_buf = hq_interop_snd_buf,
176};