Pass origin::Descr::Git url around in parsed form, perform stricter validation on it
This commit is contained in:
parent
ac66ebf6cc
commit
57ee5ff30e
@ -110,7 +110,7 @@ mod tests {
|
|||||||
|
|
||||||
let settings = domain::Settings {
|
let settings = domain::Settings {
|
||||||
origin_descr: Descr::Git {
|
origin_descr: Descr::Git {
|
||||||
url: "bar".to_string(),
|
url: "http://bar".parse().unwrap(),
|
||||||
branch_name: "baz".to_string(),
|
branch_name: "baz".to_string(),
|
||||||
},
|
},
|
||||||
add_path_prefix: None,
|
add_path_prefix: None,
|
||||||
@ -126,7 +126,7 @@ mod tests {
|
|||||||
|
|
||||||
let new_settings = domain::Settings {
|
let new_settings = domain::Settings {
|
||||||
origin_descr: Descr::Git {
|
origin_descr: Descr::Git {
|
||||||
url: "BAR".to_string(),
|
url: "https://BAR".parse().unwrap(),
|
||||||
branch_name: "BAZ".to_string(),
|
branch_name: "BAZ".to_string(),
|
||||||
},
|
},
|
||||||
add_path_prefix: None,
|
add_path_prefix: None,
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
mod config;
|
mod config;
|
||||||
mod descr;
|
pub mod descr;
|
||||||
pub mod git;
|
pub mod git;
|
||||||
|
pub mod git_proxy;
|
||||||
pub mod mux;
|
pub mod mux;
|
||||||
|
|
||||||
pub use config::*;
|
pub use config::*;
|
||||||
|
@ -1,19 +1,53 @@
|
|||||||
use hex::ToHex;
|
use hex::ToHex;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use serde_with::{serde_as, DisplayFromStr};
|
||||||
use sha2::{Digest, Sha256};
|
use sha2::{Digest, Sha256};
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
use crate::error::unexpected::{self, Mappable};
|
||||||
pub struct DescrHttpProxyHeader {
|
|
||||||
pub name: String,
|
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
||||||
pub value: String,
|
pub struct GitUrl {
|
||||||
|
pub parsed: http::uri::Uri,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl std::str::FromStr for GitUrl {
|
||||||
|
type Err = unexpected::Error;
|
||||||
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
|
let parsed: http::Uri = s
|
||||||
|
.parse()
|
||||||
|
.map_unexpected_while(|| format!("parsing as url"))?;
|
||||||
|
|
||||||
|
let scheme = parsed
|
||||||
|
.scheme()
|
||||||
|
.map_unexpected_while(|| format!("extracting scheme"))?;
|
||||||
|
|
||||||
|
if scheme != "http" && scheme != "https" {
|
||||||
|
return Err(unexpected::Error::from(
|
||||||
|
"only http(s) git URLs are supported",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(GitUrl { parsed })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::fmt::Display for GitUrl {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
self.parsed.fmt(f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[serde_as]
|
||||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||||
#[serde(tag = "kind")]
|
#[serde(tag = "kind")]
|
||||||
/// A unique description of an origin, from where a domain might be served.
|
/// A unique description of an origin, from where a domain might be served.
|
||||||
pub enum Descr {
|
pub enum Descr {
|
||||||
#[serde(rename = "git")]
|
#[serde(rename = "git")]
|
||||||
Git { url: String, branch_name: String },
|
Git {
|
||||||
|
#[serde_as(as = "DisplayFromStr")]
|
||||||
|
url: GitUrl,
|
||||||
|
branch_name: String,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Descr {
|
impl Descr {
|
||||||
@ -29,7 +63,7 @@ impl Descr {
|
|||||||
match self {
|
match self {
|
||||||
Descr::Git { url, branch_name } => {
|
Descr::Git { url, branch_name } => {
|
||||||
h_update("git");
|
h_update("git");
|
||||||
h_update(url);
|
h_update(url.parsed.to_string().as_str());
|
||||||
h_update(branch_name);
|
h_update(branch_name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -45,11 +79,20 @@ mod descr_tests {
|
|||||||
fn id() {
|
fn id() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
super::Descr::Git {
|
super::Descr::Git {
|
||||||
url: String::from("foo"),
|
url: "https://foo".parse().unwrap(),
|
||||||
branch_name: String::from("bar"),
|
branch_name: String::from("bar"),
|
||||||
}
|
}
|
||||||
.id(),
|
.id(),
|
||||||
"424130b9e6c1675c983b4b97579877e16d8a6377e4fe10970e6d210811c3b7ac",
|
"0b12637c1994e61db59ad9bea5fcaf5d2a1e5ecad00c58320271e721e4295ceb",
|
||||||
)
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
super::Descr::Git {
|
||||||
|
url: "http://foo".parse().unwrap(),
|
||||||
|
branch_name: String::from("bar"),
|
||||||
|
}
|
||||||
|
.id(),
|
||||||
|
"d14f4c75c938e46c2d9da05ea14b6e41ba86ac9945bb10c0b2f91faee1ec0bce",
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -56,12 +56,12 @@ impl FSStore {
|
|||||||
format!("origin/{branch_name}")
|
format!("origin/{branch_name}")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deconstruct_descr(descr: &origin::Descr) -> (&str, &str) {
|
fn deconstruct_descr(descr: &origin::Descr) -> (String, &str) {
|
||||||
let origin::Descr::Git {
|
let origin::Descr::Git {
|
||||||
ref url,
|
ref url,
|
||||||
ref branch_name,
|
ref branch_name,
|
||||||
} = descr;
|
} = descr;
|
||||||
(url, branch_name)
|
(url.parsed.to_string(), branch_name)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_repo_snapshot(
|
fn create_repo_snapshot(
|
||||||
@ -359,6 +359,7 @@ impl super::Store for FSStore {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::origin::{self, Config, Store};
|
use crate::origin::{self, Config, Store};
|
||||||
@ -428,3 +429,4 @@ mod tests {
|
|||||||
assert_eq!(descr, descrs[0]);
|
assert_eq!(descr, descrs[0]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_with::{serde_as, NoneAsEmptyString};
|
use serde_with::{serde_as, DisplayFromStr, NoneAsEmptyString};
|
||||||
|
|
||||||
use crate::{domain, error::unexpected, origin};
|
use crate::{domain, error::unexpected, origin};
|
||||||
|
|
||||||
@ -10,7 +10,8 @@ use crate::{domain, error::unexpected, origin};
|
|||||||
pub struct UrlEncodedDomainSettings {
|
pub struct UrlEncodedDomainSettings {
|
||||||
domain_setting_origin_descr_kind: String,
|
domain_setting_origin_descr_kind: String,
|
||||||
|
|
||||||
domain_setting_origin_descr_git_url: Option<String>,
|
#[serde_as(as = "Option<DisplayFromStr>")]
|
||||||
|
domain_setting_origin_descr_git_url: Option<origin::descr::GitUrl>,
|
||||||
domain_setting_origin_descr_git_branch_name: Option<String>,
|
domain_setting_origin_descr_git_branch_name: Option<String>,
|
||||||
|
|
||||||
domain_setting_origin_descr_proxy_url: Option<String>,
|
domain_setting_origin_descr_proxy_url: Option<String>,
|
||||||
|
Loading…
Reference in New Issue
Block a user