1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
use crate::body::BodyHandle;
use crate::request::RequestHandle;
use crate::response::ResponseHandle;
use crate::Error;
use bytes::{Buf, Bytes, BytesMut};
pub use fastly_shared::{HttpVersion, XqdStatus, XQD_ABI_VERSION};

#[link(wasm_import_module = "env")]
extern "C" {
    #[no_mangle]
    pub fn xqd_body_append(dst_handle: BodyHandle, src_handle: BodyHandle) -> XqdStatus;

    #[no_mangle]
    pub fn xqd_body_new(handle_out: *mut BodyHandle) -> XqdStatus;

    #[no_mangle]
    pub fn xqd_body_read(
        body_handle: BodyHandle,
        buf: *mut u8,
        buf_len: usize,
        nread_out: *mut usize,
    ) -> XqdStatus;

    #[no_mangle]
    pub fn xqd_body_write(
        body_handle: BodyHandle,
        buf: *const u8,
        buf_len: usize,
        end: fastly_shared::BodyWriteEnd,
        nwritten_out: *mut usize,
    ) -> XqdStatus;

    #[no_mangle]
    /// Tell the runtime what ABI version this program is using (XQD_ABI_VERSION)
    pub fn xqd_init(abi_version: u64) -> XqdStatus;

    #[no_mangle]
    pub fn xqd_req_body_downstream_get(
        req_handle_out: *mut RequestHandle,
        body_handle_out: *mut BodyHandle,
    ) -> XqdStatus;

    #[no_mangle]
    pub fn xqd_req_header_append(
        req_handle: RequestHandle,
        name: *const u8,
        name_len: usize,
        value: *const u8,
        value_len: usize,
    ) -> XqdStatus;

    #[no_mangle]
    pub fn xqd_req_header_insert(
        req_handle: RequestHandle,
        name: *const u8,
        name_len: usize,
        value: *const u8,
        value_len: usize,
    ) -> XqdStatus;

    #[no_mangle]
    pub fn xqd_req_header_names_get(
        req_handle: RequestHandle,
        buf: *mut u8,
        buf_len: usize,
        cursor: u32,
        ending_cursor: *mut i64,
        nwritten: *mut usize,
    ) -> XqdStatus;

    #[no_mangle]
    pub fn xqd_req_header_values_get(
        req_handle: RequestHandle,
        name: *const u8,
        name_len: usize,
        buf: *mut u8,
        buf_len: usize,
        cursor: u32,
        ending_cursor: *mut i64,
        nwritten: *mut usize,
    ) -> XqdStatus;

    #[no_mangle]
    pub fn xqd_req_header_values_set(
        req_handle: RequestHandle,
        name: *const u8,
        name_len: usize,
        values: *const u8,
        values_len: usize,
    ) -> XqdStatus;

    #[no_mangle]
    pub fn xqd_req_method_get(
        req_handle: RequestHandle,
        method: *mut u8,
        method_max_len: usize,
        nwritten: *mut usize,
    ) -> XqdStatus;

    #[no_mangle]
    pub fn xqd_req_method_set(
        req_handle: RequestHandle,
        method: *const u8,
        method_len: usize,
    ) -> XqdStatus;

    #[no_mangle]
    pub fn xqd_req_new(req_handle_out: *mut RequestHandle) -> XqdStatus;

    #[no_mangle]
    pub fn xqd_req_send(
        req_handle: RequestHandle,
        body_handle: BodyHandle,
        backend: *const u8,
        backend_len: usize,
        ttl: i32,
        resp_handle_out: *mut ResponseHandle,
        resp_body_handle_out: *mut BodyHandle,
    ) -> XqdStatus;

    #[no_mangle]
    pub fn xqd_req_uri_get(
        req_handle: RequestHandle,
        uri: *mut u8,
        uri_max_len: usize,
        nwritten: *mut usize,
    ) -> XqdStatus;

    #[no_mangle]
    pub fn xqd_req_uri_set(req_handle: RequestHandle, uri: *const u8, uri_len: usize) -> XqdStatus;

    #[no_mangle]
    pub fn xqd_req_version_get(req_handle: RequestHandle, version: *mut u32) -> XqdStatus;

    #[no_mangle]
    pub fn xqd_req_version_set(req_handle: RequestHandle, version: u32) -> XqdStatus;

    #[no_mangle]
    pub fn xqd_resp_header_append(
        resp_handle: ResponseHandle,
        name: *const u8,
        name_len: usize,
        value: *const u8,
        value_len: usize,
    ) -> XqdStatus;

    #[no_mangle]
    pub fn xqd_resp_header_insert(
        resp_handle: ResponseHandle,
        name: *const u8,
        name_len: usize,
        value: *const u8,
        value_len: usize,
    ) -> XqdStatus;

    #[no_mangle]
    pub fn xqd_resp_header_names_get(
        resp_handle: ResponseHandle,
        buf: *mut u8,
        buf_len: usize,
        cursor: u32,
        ending_cursor: *mut i64,
        nwritten: *mut usize,
    ) -> XqdStatus;

    #[no_mangle]
    pub fn xqd_resp_header_value_get(
        resp_handle: ResponseHandle,
        name: *const u8,
        name_len: usize,
        value: *mut u8,
        value_max_len: usize,
        nwritten: *mut usize,
    ) -> XqdStatus;

    #[no_mangle]
    pub fn xqd_resp_header_values_get(
        resp_handle: ResponseHandle,
        name: *const u8,
        name_len: usize,
        buf: *mut u8,
        buf_len: usize,
        cursor: u32,
        ending_cursor: *mut i64,
        nwritten: *mut usize,
    ) -> XqdStatus;

    #[no_mangle]
    pub fn xqd_resp_header_values_set(
        resp_handle: ResponseHandle,
        name: *const u8,
        name_len: usize,
        values: *const u8,
        values_len: usize,
    ) -> XqdStatus;

    #[no_mangle]
    pub fn xqd_resp_new(resp_handle_out: *mut ResponseHandle) -> XqdStatus;

    #[no_mangle]
    pub fn xqd_resp_send_downstream(
        resp_handle: ResponseHandle,
        body_handle: BodyHandle,
    ) -> XqdStatus;

    #[no_mangle]
    pub fn xqd_resp_status_get(resp_handle: ResponseHandle, status: *mut u16) -> XqdStatus;

    #[no_mangle]
    pub fn xqd_resp_status_set(resp_handle: ResponseHandle, status: u16) -> XqdStatus;

    #[no_mangle]
    pub fn xqd_resp_version_get(resp_handle: ResponseHandle, version: *mut u32) -> XqdStatus;

    #[no_mangle]
    pub fn xqd_resp_version_set(resp_handle: ResponseHandle, version: u32) -> XqdStatus;

    #[no_mangle]
    pub fn xqd_uap_parse(
        user_agent: *const u8,
        user_agent_max_len: usize,
        family: *mut u8,
        family_max_len: usize,
        family_written: *mut usize,
        major: *mut u8,
        major_max_len: usize,
        major_written: *mut usize,
        minor: *mut u8,
        minor_max_len: usize,
        minor_written: *mut usize,
        patch: *mut u8,
        patch_max_len: usize,
        patch_written: *mut usize
        ) -> XqdStatus;

}

pub(crate) struct MultiValueHostcall<F> {
    fill_buf: F,
    term: u8,
    buf: BytesMut,
    max_buf_size: usize,
    cursor: u32,
    is_done: bool,
}

impl<F> MultiValueHostcall<F> {
    pub(crate) fn new(term: u8, max_buf_size: usize, fill_buf: F) -> Self {
        Self {
            fill_buf,
            term,
            buf: BytesMut::with_capacity(max_buf_size),
            max_buf_size,
            cursor: 0,
            is_done: false,
        }
    }
}

impl<F> std::iter::Iterator for MultiValueHostcall<F>
where
    F: Fn(*mut u8, usize, u32, *mut i64, *mut usize) -> XqdStatus,
{
    type Item = Result<Bytes, Error>;

    fn next(&mut self) -> Option<Self::Item> {
        // first fill the buffer, if it's empty
        if self.buf.is_empty() {
            if self.is_done {
                // if there are no more calls to make, and the buffer is empty, we're done
                return None;
            }
            self.buf.reserve(self.max_buf_size);
            let mut ending_cursor = 0;
            let mut nwritten = 0;
            let status = (self.fill_buf)(
                self.buf.as_mut_ptr(),
                self.buf.capacity(),
                self.cursor,
                &mut ending_cursor,
                &mut nwritten,
            );
            if status.is_err() {
                self.is_done = true;
                // TODO ACF 2019-12-02: make sure these implementation detail error messages don't
                // bubble out to the user
                return Some(Err(Error::new("MultiValueHostcall closure returned error")));
            }
            if nwritten == 0 {
                // if we get no bytes, we're definitely done; this only comes up if there are no
                // values at all, otherwise we see the ending cursor at -1 and stop
                self.is_done = true;
                return None;
            }
            unsafe {
                self.buf.set_len(nwritten);
            }
            if self.cursor == ending_cursor as u32 {
                self.is_done = true;
                // if the cursor doesn't advance at all, that means there's a value that's too big
                // to fit in our buffer
                return Some(Err(Error::new("MultiValueHostcall buffer too small")));
            }
            if ending_cursor < 0 {
                // no more calls necessary after this one
                self.is_done = true;
            } else {
                // otherwise adjust the cursor for the next fill
                self.cursor += ending_cursor as u32;
            }
        }
        // find the index of the first terminator byte in the buffer
        let first_term_ix = if let Some(ix) = self.buf.iter().position(|b| b == &self.term) {
            ix
        } else {
            // if the terminator is not found, that violates the protocol of these hostcalls, which
            // must always terminate each element with the terminator
            self.is_done = true;
            return Some(Err(Error::new(
                "MultiValueHostcall separator byte not found",
            )));
        };
        // split off the first element from the buffer
        let elt = self.buf.split_to(first_term_ix);
        // drop the terminator byte, which now remains in the buffer
        self.buf.advance(1);
        Some(Ok(elt.freeze()))
    }
}

impl<F> std::iter::FusedIterator for MultiValueHostcall<F> where
    F: Fn(*mut u8, usize, u32, *mut i64, *mut usize) -> XqdStatus
{
}