MCPcopy Create free account
hub / github.com/awslabs/llrt / build_request

Function build_request

modules/llrt_fetch/src/fetch.rs:488–569  ·  view source on GitHub ↗
(
    ctx: &Ctx<'js>,
    method: &hyper::Method,
    uri: &Uri,
    headers: Option<&Headers>,
    body: Option<&RequestBody<'js>>,
    prev_status: &u16,
    initial_uri: &Uri,
)

Source from the content-addressed store, hash-verified

486 *req = std::mem::take(req).header(ACCEPT_ENCODING, "zstd, br, gzip, deflate");
487 }
488 if !detected_headers.contains(ACCEPT_LANGUAGE.as_str()) {
489 *req = std::mem::take(req).header(ACCEPT_LANGUAGE, "*");
490 }
491 if !detected_headers.contains(ACCEPT.as_str()) {
492 *req = std::mem::take(req).header(ACCEPT, "*/*");
493 }
494}
495
496fn parse_data_url<'js>(ctx: &Ctx<'js>, data_url: &str, method: &Method) -> Result<Response<'js>> {
497 let (mime_type, data) = data_url
498 .split_once(',')
499 .ok_or_else(|| Exception::throw_type(ctx, "Invalid data URL format"))?;
500
501 let mut is_base64 = false;
502 let mut content_type = String::with_capacity(10);
503 for (i, part) in mime_type.split(';').enumerate() {
504 let part = part.trim();
505 if i == 1 || i == 2 {
506 if part == "base64" {
507 is_base64 = true;
508 break;
509 }
510 content_type.push(';');
511 }
512 content_type.push_str(part)
513 }
514
515 let content_type = if content_type.starts_with(';') {
516 ["text/plain", &content_type].concat()
517 } else if content_type.is_empty() {
518 "text/plain;charset=US-ASCII".to_string()
519 } else {
520 content_type
521 };
522
523 let body = if method == Method::HEAD {
524 vec![]
525 } else if is_base64 {
526 bytes_from_b64(data.as_bytes()).or_throw(ctx)?
527 } else {
528 let data = percent_decode_str(data).decode_utf8().or_throw(ctx)?;
529 data.as_bytes().into()
530 };
531
532 let blob = Blob::from_bytes(ctx, body, Some(content_type.clone()))?.into_js(ctx)?;
533
534 let headers = Object::new(ctx.clone())?;
535 headers.set(CONTENT_TYPE.as_str(), content_type)?;
536
537 let options = Object::new(ctx.clone())?;
538 options.set("url", data_url)?;
539 options.set("headers", headers)?;
540 // data: URL spec requires "OK" reason phrase.
541 options.set("statusText", "OK")?;
542
543 Response::new(ctx.clone(), Opt(Some(blob)), Opt(Some(options)))
544}
545

Callers 1

sendFunction · 0.85

Calls 12

is_same_originFunction · 0.85
should_change_methodFunction · 0.85
apply_default_headersFunction · 0.85
uri_originFunction · 0.85
methodMethod · 0.80
or_throwMethod · 0.80
cloneMethod · 0.45
iterMethod · 0.45
as_strMethod · 0.45
bodyMethod · 0.45

Tested by

no test coverage detected