From 8b286431e642ae16dacc42cce10b02f5e25ad1e5 Mon Sep 17 00:00:00 2001 From: Aiden McClelland Date: Thu, 10 Mar 2022 16:41:04 -0700 Subject: [PATCH] handle new content-types from marketplace.get --- backend/Cargo.lock | 1 + backend/Cargo.toml | 1 + backend/src/marketplace.rs | 38 +++++++++++++++++++++++++++++++++----- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/backend/Cargo.lock b/backend/Cargo.lock index ec5a522ae..5198ff719 100644 --- a/backend/Cargo.lock +++ b/backend/Cargo.lock @@ -865,6 +865,7 @@ dependencies = [ "async-trait", "avahi-sys", "base32", + "base64", "basic-cookies", "bollard", "chrono", diff --git a/backend/Cargo.toml b/backend/Cargo.toml index 38b816b54..ae32d4ff8 100644 --- a/backend/Cargo.toml +++ b/backend/Cargo.toml @@ -51,6 +51,7 @@ avahi-sys = { git = "https://github.com/Start9Labs/avahi-sys", version = "0.10.0 "dynamic", ], optional = true } base32 = "0.4.0" +base64 = "0.13.0" basic-cookies = "0.1.4" bollard = "0.11.0" chrono = { version = "0.4.19", features = ["serde"] } diff --git a/backend/src/marketplace.rs b/backend/src/marketplace.rs index af326992b..aa5cf8d82 100644 --- a/backend/src/marketplace.rs +++ b/backend/src/marketplace.rs @@ -12,15 +12,43 @@ pub fn marketplace() -> Result<(), Error> { #[command] pub async fn get(#[arg] url: Url) -> Result { - let response = reqwest::get(url) + let mut response = reqwest::get(url) .await .with_kind(crate::ErrorKind::Network)?; let status = response.status(); if status.is_success() { - response - .json() - .await - .with_kind(crate::ErrorKind::Deserialization) + match response + .headers_mut() + .remove("Content-Type") + .as_ref() + .and_then(|h| h.to_str().ok()) + { + Some("application/json") => response + .json() + .await + .with_kind(crate::ErrorKind::Deserialization), + Some("text/plain") => Ok(Value::String( + response + .text() + .await + .with_kind(crate::ErrorKind::Registry)?, + )), + Some(ctype) => Ok(Value::String(format!( + "data:{};base64,{}", + ctype, + base64::encode_config( + &response + .bytes() + .await + .with_kind(crate::ErrorKind::Registry)?, + base64::URL_SAFE + ) + ))), + _ => Err(Error::new( + eyre!("missing Content-Type"), + crate::ErrorKind::Registry, + )), + } } else { let message = response.text().await.with_kind(crate::ErrorKind::Network)?; Err(Error::new(