ic-http-gateway 0.4.3

An HTTP Gateway implementation for interfacing with the Internet Computer over HTTP
Documentation
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
use crate::protocol::validate;
use crate::{HttpGatewayResponseBody, ResponseBodyStream};
use bytes::Bytes;
use candid::Principal;
use futures::{stream, Stream, StreamExt, TryStreamExt};
use http_body::Frame;
use http_body_util::{BodyExt, Full};
use ic_agent::{Agent, AgentError};
use ic_http_certification::{HttpRequest, HttpResponse, StatusCode};
use ic_response_verification::MAX_VERIFICATION_VERSION;
use ic_utils::interfaces::http_request::HeaderField;
use ic_utils::{
    call::SyncCall,
    interfaces::http_request::{
        HttpRequestCanister, HttpRequestStreamingCallbackAny, HttpResponse as AgentResponse,
        StreamingCallbackHttpResponse, StreamingStrategy, Token,
    },
};

// Limit the total number of calls to an HTTP Request loop to 1000 for now.
static MAX_HTTP_REQUEST_STREAM_CALLBACK_CALL_COUNT: usize = 1000;

// Limit the total number of calls to an HTTP request look that can be verified
static MAX_VERIFIED_HTTP_REQUEST_STREAM_CALLBACK_CALL_COUNT: usize = 4;

// Limit the number of Stream Callbacks buffered
static STREAM_CALLBACK_BUFFER: usize = 2;

pub type AgentResponseAny = AgentResponse<Token, HttpRequestStreamingCallbackAny>;

pub async fn get_body_and_streaming_body(
    agent: &Agent,
    response: &AgentResponseAny,
) -> Result<HttpGatewayResponseBody, AgentError> {
    // if we already have the full body, we can return it early
    let Some(StreamingStrategy::Callback(callback_strategy)) = response.streaming_strategy.clone()
    else {
        return Ok(HttpGatewayResponseBody::Right(Full::from(
            response.body.clone(),
        )));
    };

    let (streamed_body, token) = create_stream(
        agent.clone(),
        callback_strategy.callback.clone(),
        Some(callback_strategy.token),
    )
    .take(MAX_VERIFIED_HTTP_REQUEST_STREAM_CALLBACK_CALL_COUNT)
    .map(|x| async move { x })
    .buffered(STREAM_CALLBACK_BUFFER)
    .try_fold(
        (vec![], None::<Token>),
        |mut accum, (mut body, token)| async move {
            accum.0.append(&mut body);
            accum.1 = token;

            Ok(accum)
        },
    )
    .await?;

    let streamed_body = [response.body.clone(), streamed_body].concat();

    // if we still have a token at this point,
    // we were unable to collect the response within the allowed certified callback limit,
    // fallback to uncertified streaming using what we've streamed so far as the initial body
    if token.is_some() {
        let body_stream = create_body_stream(
            agent.clone(),
            callback_strategy.callback,
            token,
            streamed_body,
        );

        return Ok(HttpGatewayResponseBody::Left(body_stream));
    };

    // if we no longer have a token at this point,
    // we were able to collect the response within the allow certified callback limit,
    // return this collected response as a standard response body so it will be verified
    Ok(HttpGatewayResponseBody::Right(Full::from(streamed_body)))
}

fn create_body_stream(
    agent: Agent,
    callback: HttpRequestStreamingCallbackAny,
    token: Option<Token>,
    initial_body: Vec<u8>,
) -> ResponseBodyStream {
    let chunks_stream = create_stream(agent, callback, token)
        .map(|chunk| chunk.map(|(body, _)| Frame::data(Bytes::from(body))));

    let body_stream = stream::once(async move { Ok(Frame::data(Bytes::from(initial_body))) })
        .chain(chunks_stream)
        .take(MAX_HTTP_REQUEST_STREAM_CALLBACK_CALL_COUNT)
        .map(|x| async move { x })
        .buffered(STREAM_CALLBACK_BUFFER);

    ResponseBodyStream::new(Box::pin(body_stream))
}

fn create_stream(
    agent: Agent,
    callback: HttpRequestStreamingCallbackAny,
    token: Option<Token>,
) -> impl Stream<Item = Result<(Vec<u8>, Option<Token>), AgentError>> {
    futures::stream::try_unfold(
        (agent, callback, token),
        |(agent, callback, token)| async move {
            let Some(token) = token else {
                return Ok(None);
            };

            let canister = HttpRequestCanister::create(&agent, callback.0.principal);
            match canister
                .http_request_stream_callback(&callback.0.method, token)
                .call()
                .await
            {
                Ok((StreamingCallbackHttpResponse { body, token },)) => {
                    Ok(Some(((body, token.clone()), (agent, callback, token))))
                }
                Err(e) => Err(e),
            }
        },
    )
}

#[derive(Clone, Debug)]
struct StreamState<'a> {
    pub http_request: HttpRequest<'a>,
    pub canister_id: Principal,
    pub total_length: usize,
    pub fetched_length: usize,
    pub skip_verification: bool,
}

pub async fn get_206_stream_response_body_and_total_length(
    agent: &Agent,
    http_request: HttpRequest<'static>,
    canister_id: Principal,
    response_headers: &Vec<HeaderField<'static>>,
    response_206_body: HttpGatewayResponseBody,
    skip_verification: bool,
) -> Result<(HttpGatewayResponseBody, usize), AgentError> {
    let HttpGatewayResponseBody::Right(body) = response_206_body else {
        return Err(AgentError::InvalidHttpResponse(
            "Expected full 206 response".to_string(),
        ));
    };
    // The expect below should never panic because `Either::Right` will always have a full body
    let streamed_body = body
        .collect()
        .await
        .expect("missing streamed chunk body")
        .to_bytes()
        .to_vec();
    let stream_state = get_initial_stream_state(
        http_request,
        canister_id,
        response_headers,
        skip_verification,
    )?;
    let content_length = stream_state.total_length;

    let body_stream = create_206_body_stream(agent.clone(), stream_state, streamed_body);
    Ok((HttpGatewayResponseBody::Left(body_stream), content_length))
}

#[derive(Debug)]
struct ContentRangeValues {
    pub range_begin: usize,
    pub range_end: usize,
    pub total_length: usize,
}

fn parse_content_range_header_str(
    content_range_str: &str,
) -> Result<ContentRangeValues, AgentError> {
    // expected format: `bytes 21010-47021/47022`
    let str_value = content_range_str.trim();
    if !str_value.starts_with("bytes ") {
        return Err(AgentError::InvalidHttpResponse(format!(
            "Invalid Content-Range header '{}'",
            content_range_str
        )));
    }
    let str_value = str_value.trim_start_matches("bytes ");

    let str_value_parts = str_value.split('-').collect::<Vec<_>>();
    if str_value_parts.len() != 2 {
        return Err(AgentError::InvalidHttpResponse(format!(
            "Invalid bytes spec in Content-Range header '{}'",
            content_range_str
        )));
    }
    let range_begin = str_value_parts[0].parse::<usize>().map_err(|e| {
        AgentError::InvalidHttpResponse(format!(
            "Invalid range_begin in '{}': {}",
            content_range_str, e
        ))
    })?;

    let other_value_parts = str_value_parts[1].split('/').collect::<Vec<_>>();
    if other_value_parts.len() != 2 {
        return Err(AgentError::InvalidHttpResponse(format!(
            "Invalid bytes spec in Content-Range header '{}'",
            content_range_str
        )));
    }
    let range_end = other_value_parts[0].parse::<usize>().map_err(|e| {
        AgentError::InvalidHttpResponse(format!(
            "Invalid range_end in '{}': {}",
            content_range_str, e
        ))
    })?;
    let total_length = other_value_parts[1].parse::<usize>().map_err(|e| {
        AgentError::InvalidHttpResponse(format!(
            "Invalid total_length in '{}': {}",
            content_range_str, e
        ))
    })?;

    let rv = ContentRangeValues {
        range_begin,
        range_end,
        total_length,
    };
    if rv.range_begin > rv.range_end
        || rv.range_begin >= rv.total_length
        || rv.range_end >= rv.total_length
    {
        Err(AgentError::InvalidHttpResponse(format!(
            "inconsistent Content-Range header {}: {:?}",
            content_range_str, rv
        )))
    } else {
        Ok(rv)
    }
}

fn get_content_range_header_str(
    response_headers: &Vec<HeaderField<'static>>,
) -> Result<String, AgentError> {
    for HeaderField(name, value) in response_headers {
        if name.eq_ignore_ascii_case(http::header::CONTENT_RANGE.as_ref()) {
            return Ok(value.to_string());
        }
    }
    Err(AgentError::InvalidHttpResponse(
        "missing Content-Range header in 206 response".to_string(),
    ))
}

fn get_content_range_values(
    response_headers: &Vec<HeaderField<'static>>,
    fetched_length: usize,
) -> Result<ContentRangeValues, AgentError> {
    let str_value = get_content_range_header_str(response_headers)?;
    let range_values = parse_content_range_header_str(&str_value)?;

    if range_values.range_begin > fetched_length {
        return Err(AgentError::InvalidHttpResponse(format!(
            "chunk out-of-order: range_begin={} is larger than expected begin={} ",
            range_values.range_begin, fetched_length
        )));
    }
    if range_values.range_end < fetched_length {
        return Err(AgentError::InvalidHttpResponse(format!(
            "chunk out-of-order: range_end={} is smaller than length fetched so far={} ",
            range_values.range_begin, fetched_length
        )));
    }
    Ok(range_values)
}

fn get_initial_stream_state<'a>(
    http_request: HttpRequest<'a>,
    canister_id: Principal,
    response_headers: &Vec<HeaderField<'static>>,
    skip_verification: bool,
) -> Result<StreamState<'a>, AgentError> {
    let range_values = get_content_range_values(response_headers, 0)?;

    Ok(StreamState {
        http_request,
        canister_id,
        total_length: range_values.total_length,
        fetched_length: range_values
            .range_end
            .saturating_sub(range_values.range_begin)
            + 1,
        skip_verification,
    })
}

fn create_206_body_stream(
    agent: Agent,
    stream_state: StreamState<'static>,
    initial_body: Vec<u8>,
) -> ResponseBodyStream {
    let chunks_stream = create_206_stream(agent, Some(stream_state))
        .map(|chunk| chunk.map(|(body, _)| Frame::data(Bytes::from(body))));

    let body_stream = stream::once(async move { Ok(Frame::data(Bytes::from(initial_body))) })
        .chain(chunks_stream)
        .take(MAX_HTTP_REQUEST_STREAM_CALLBACK_CALL_COUNT)
        .map(|x| async move { x })
        .buffered(STREAM_CALLBACK_BUFFER);

    ResponseBodyStream::new(Box::pin(body_stream))
}

fn create_206_stream(
    agent: Agent,
    maybe_stream_state: Option<StreamState>,
) -> impl Stream<Item = Result<(Vec<u8>, Option<StreamState>), AgentError>> {
    futures::stream::try_unfold(
        (agent, maybe_stream_state),
        |(agent, maybe_stream_state)| async move {
            let Some(stream_state) = maybe_stream_state else {
                return Ok(None);
            };
            let canister = HttpRequestCanister::create(&agent, stream_state.canister_id);
            let next_chunk_begin = stream_state.fetched_length;

            let range_header = ("Range".to_string(), format!("bytes={}-", next_chunk_begin));
            let mut updated_headers = stream_state.http_request.headers().to_vec();
            updated_headers.push(range_header.clone());
            let headers = updated_headers
                .iter()
                .map(|(name, value)| HeaderField(name.into(), value.into()))
                .collect::<Vec<HeaderField>>()
                .into_iter();
            let query_result = canister
                .http_request(
                    &stream_state.http_request.method(),
                    &stream_state.http_request.url(),
                    headers,
                    &stream_state.http_request.body(),
                    Some(&u16::from(MAX_VERIFICATION_VERSION)),
                )
                .call()
                .await;
            let agent_response = match query_result {
                Ok((response,)) => response,
                Err(e) => return Err(e),
            };
            let range_values =
                get_content_range_values(&agent_response.headers, stream_state.fetched_length)?;
            let new_bytes_begin = stream_state
                .fetched_length
                .saturating_sub(range_values.range_begin);
            let chunk_length = range_values
                .range_end
                .saturating_sub(stream_state.fetched_length)
                + 1;
            let current_fetched_length = stream_state.fetched_length + chunk_length;
            // Verify the chunk from the range response.
            if agent_response.streaming_strategy.is_some() {
                return Err(AgentError::InvalidHttpResponse(
                    "unexpected StreamingStrategy".to_string(),
                ));
            }

            let Ok(status_code) = StatusCode::from_u16(agent_response.status_code) else {
                return Err(AgentError::InvalidHttpResponse(format!(
                    "Invalid canister response status code: {}",
                    agent_response.status_code
                )));
            };
            let response = HttpResponse::builder()
                .with_status_code(status_code)
                .with_headers(
                    agent_response
                        .headers
                        .iter()
                        .map(|HeaderField(k, v)| (k.to_string(), v.to_string()))
                        .collect(),
                )
                .with_body(agent_response.body.clone())
                .build();
            let mut http_request = stream_state.http_request.clone();
            http_request.headers_mut().push(range_header);
            let validation_result = validate(
                &agent,
                &stream_state.canister_id,
                http_request,
                response,
                stream_state.skip_verification,
            );

            if let Err(e) = validation_result {
                return Err(AgentError::InvalidHttpResponse(format!(
                    "CertificateVerificationFailed for a chunk starting at {}, error: {}",
                    stream_state.fetched_length, e
                )));
            }
            let maybe_new_state = if current_fetched_length < stream_state.total_length {
                Some(StreamState {
                    fetched_length: current_fetched_length,
                    ..stream_state
                })
            } else {
                None
            };
            Ok(Some((
                (
                    agent_response.body[new_bytes_begin..].to_vec(),
                    maybe_new_state.clone(),
                ),
                (agent, maybe_new_state),
            )))
        },
    )
}

#[cfg(test)]
mod tests {
    use super::*;
    use assert_matches::assert_matches;
    use std::borrow::Cow;

    #[test]
    fn should_parse_content_range_header_str() {
        let header_values = [
            ContentRangeValues {
                range_begin: 0,
                range_end: 0,
                total_length: 1,
            },
            ContentRangeValues {
                range_begin: 100,
                range_end: 2000,
                total_length: 3000,
            },
            ContentRangeValues {
                range_begin: 10_000,
                range_end: 300_000,
                total_length: 500_000,
            },
        ];
        for v in header_values {
            let input = format!("bytes {}-{}/{}", v.range_begin, v.range_end, v.total_length);
            let result = parse_content_range_header_str(&input);
            let output = result.unwrap_or_else(|_| panic!("failed parsing '{}'", input));
            assert_eq!(v.range_begin, output.range_begin);
            assert_eq!(v.range_end, output.range_end);
            assert_eq!(v.total_length, output.total_length);
        }
    }

    #[test]
    fn should_fail_parse_content_range_header_str_on_malformed_input() {
        let malformed_inputs = [
            "byte 1-2/3",
            "bites 2-4/8",
            "bytes 100-200/asdf",
            "bytes 12345",
            "something else",
            "bytes dead-beef/123456",
        ];
        for input in malformed_inputs {
            let result = parse_content_range_header_str(input);
            assert_matches!(result, Err(e) if format!("{}", e).contains("Invalid "));
        }
    }

    #[test]
    fn should_fail_parse_content_range_header_str_on_inconsistent_input() {
        let inconsistent_inputs = ["bytes 100-200/190", "bytes 200-150/400", "bytes 100-110/40"];
        for input in inconsistent_inputs {
            let result = parse_content_range_header_str(input);
            assert_matches!(result, Err(e) if format!("{}", e).contains("inconsistent Content-Range header"));
        }
    }

    #[test]
    fn should_get_initial_stream_state() {
        let http_request = HttpRequest::get("http://example.com/some_file")
            .with_headers(vec![("Xyz".to_string(), "some value".to_string())])
            .with_body(vec![42])
            .build();
        let canister_id = Principal::from_slice(&[1, 2, 3, 4]);
        let response_headers = vec![HeaderField(
            Cow::from("Content-Range"),
            Cow::from("bytes 0-2/10"), // fetched 3 bytes, total length is 10
        )];
        let skip_verification = false;
        let state = get_initial_stream_state(
            http_request.clone(),
            canister_id,
            &response_headers,
            skip_verification,
        )
        .expect("failed constructing StreamState");
        assert_eq!(state.http_request, http_request);
        assert_eq!(state.canister_id, canister_id);
        assert_eq!(state.fetched_length, 3);
        assert_eq!(state.total_length, 10);
        assert_eq!(state.skip_verification, skip_verification);
    }

    #[test]
    fn should_fail_get_initial_stream_state_without_content_range_header() {
        let http_request = HttpRequest::get("http://example.com/some_file")
            .with_headers(vec![("Xyz".to_string(), "some value".to_string())])
            .with_body(vec![42])
            .build();
        let canister_id = Principal::from_slice(&[1, 2, 3, 4]);
        let response_headers = vec![HeaderField(
            Cow::from("other header"),
            Cow::from("other value"),
        )];
        let result = get_initial_stream_state(http_request, canister_id, &response_headers, false);
        assert_matches!(result, Err(e) if format!("{}", e).contains("missing Content-Range header"));
    }

    #[test]
    fn should_fail_get_initial_stream_state_with_malformed_content_range_header() {
        let http_request = HttpRequest::get("http://example.com/some_file")
            .with_headers(vec![("Xyz".to_string(), "some value".to_string())])
            .with_body(vec![42])
            .build();
        let canister_id = Principal::from_slice(&[1, 2, 3, 4]);
        let response_headers = vec![HeaderField(
            Cow::from("Content-Range"),
            Cow::from("bytes 42/10"),
        )];
        let result = get_initial_stream_state(http_request, canister_id, &response_headers, false);
        assert_matches!(result, Err(e) if format!("{}", e).contains("Invalid bytes spec in Content-Range header"));
    }

    #[test]
    fn should_fail_get_initial_stream_state_with_inconsistent_content_range_header() {
        let http_request = HttpRequest::get("http://example.com/some_file")
            .with_headers(vec![("Xyz".to_string(), "some value".to_string())])
            .with_body(vec![42])
            .build();
        let canister_id = Principal::from_slice(&[1, 2, 3, 4]);
        let response_headers = vec![HeaderField(
            Cow::from("Content-Range"),
            Cow::from("bytes 40-100/90"),
        )];
        let result = get_initial_stream_state(http_request, canister_id, &response_headers, false);
        assert_matches!(result, Err(e) if format!("{}", e).contains("inconsistent Content-Range header"));
    }
}