(
ctx: &Ctx<'js>,
method: &hyper::Method,
uri: &Uri,
headers: Option<&Headers>,
body: Option<&RequestBody<'js>>,
prev_status: &u16,
initial_uri: &Uri,
)
| 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 | |
| 496 | fn 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 |
no test coverage detected