mirror of
https://github.com/aljazceru/pubky-core.git
synced 2025-12-31 04:44:37 +01:00
fix(js): send correct pubky-host header
This commit is contained in:
@@ -13,11 +13,17 @@ use pkarr::PublicKey;
|
||||
use crate::core::error::{Error, Result};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Pubky(PublicKey);
|
||||
pub enum Pubky {
|
||||
Host(PublicKey),
|
||||
PubkyHost(PublicKey),
|
||||
}
|
||||
|
||||
impl Pubky {
|
||||
pub fn public_key(&self) -> &PublicKey {
|
||||
&self.0
|
||||
match self {
|
||||
Pubky::Host(p) => p,
|
||||
Pubky::PubkyHost(p) => p,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,34 +35,23 @@ where
|
||||
type Rejection = Response;
|
||||
|
||||
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
|
||||
let headers_to_check = ["host", "pkarr-host"];
|
||||
let headers_to_check = ["host", "pubky-host"];
|
||||
|
||||
for header in headers_to_check {
|
||||
if let Some(Ok(pkarr_host)) = parts.headers.get(header).map(|h| h.to_str()) {
|
||||
if let Ok(public_key) = PublicKey::try_from(pkarr_host) {
|
||||
tracing::debug!(?pkarr_host);
|
||||
if let Some(Ok(pubky_host)) = parts.headers.get(header).map(|h| h.to_str()) {
|
||||
if let Ok(public_key) = PublicKey::try_from(pubky_host) {
|
||||
tracing::debug!(?pubky_host);
|
||||
|
||||
return Ok(Pubky(public_key));
|
||||
if header == "host" {
|
||||
return Ok(Pubky::Host(public_key));
|
||||
}
|
||||
|
||||
return Ok(Pubky::PubkyHost(public_key));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: return an error and remove all param based routes
|
||||
|
||||
let params: Path<HashMap<String, String>> =
|
||||
parts.extract().await.map_err(IntoResponse::into_response)?;
|
||||
|
||||
let pubky_id = params
|
||||
.get("pubky")
|
||||
.ok_or_else(|| (StatusCode::NOT_FOUND, "pubky param missing").into_response())?;
|
||||
|
||||
let public_key = PublicKey::try_from(pubky_id.to_string())
|
||||
.map_err(Error::try_from)
|
||||
.map_err(IntoResponse::into_response)?;
|
||||
|
||||
// TODO: return 404 if the user doesn't exist, but exclude signups.
|
||||
|
||||
Ok(Pubky(public_key))
|
||||
Err(Error::new(StatusCode::NOT_FOUND, "Pubky host not found".into()).into_response())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@ impl Client {
|
||||
|
||||
let request_init = request_init.unwrap_or_default();
|
||||
|
||||
if let Some(pkarr_host) = self.prepare_request(&mut url).await {
|
||||
if let Some(pubky_host) = self.prepare_request(&mut url).await {
|
||||
let headers = request_init.get_headers();
|
||||
|
||||
let headers = if headers.is_null() || headers.is_undefined() {
|
||||
@@ -36,7 +36,7 @@ impl Client {
|
||||
Headers::from(headers)
|
||||
};
|
||||
|
||||
headers.append("pkarr-host", &pkarr_host)?;
|
||||
headers.append("pubky-host", &pubky_host)?;
|
||||
|
||||
request_init.set_headers(&headers.into());
|
||||
}
|
||||
@@ -80,10 +80,10 @@ impl Client {
|
||||
let original_url = url.as_str();
|
||||
let mut url = Url::parse(original_url).expect("Invalid url in inner_request");
|
||||
|
||||
if let Some(pkarr_host) = self.prepare_request(&mut url).await {
|
||||
if let Some(pubky_host) = self.prepare_request(&mut url).await {
|
||||
self.http
|
||||
.request(method, url.clone())
|
||||
.header::<&str, &str>("pkarr-host", &pkarr_host)
|
||||
.header::<&str, &str>("pubky-host", &pubky_host)
|
||||
.fetch_credentials_include()
|
||||
} else {
|
||||
self.http
|
||||
@@ -94,8 +94,10 @@ impl Client {
|
||||
|
||||
/// - Transforms pubky:// url to http(s):// urls
|
||||
/// - Resolves a clearnet host to call with fetch
|
||||
/// - Returns the `pkarr-host` value if available
|
||||
/// - Returns the `pubky-host` value if available
|
||||
pub(super) async fn prepare_request(&self, url: &mut Url) -> Option<String> {
|
||||
let host = url.host_str().unwrap_or("").to_string();
|
||||
|
||||
if url.scheme() == "pubky" {
|
||||
*url = Url::parse(&format!("https{}", &url.as_str()[5..]))
|
||||
.expect("couldn't replace pubky:// with https://");
|
||||
@@ -103,23 +105,25 @@ impl Client {
|
||||
.expect("couldn't map pubk://<pubky> to https://_pubky.<pubky>");
|
||||
}
|
||||
|
||||
// TODO: move at the begining of the method.
|
||||
let host = url.host_str().unwrap_or("").to_string();
|
||||
|
||||
let mut pkarr_host = None;
|
||||
let mut pubky_host = None;
|
||||
|
||||
if PublicKey::try_from(host.clone()).is_ok() {
|
||||
self.transform_url(url, &host).await;
|
||||
self.transform_url(url).await;
|
||||
|
||||
pkarr_host = Some(host);
|
||||
pubky_host = Some(host);
|
||||
|
||||
log::debug!("Transformed URL to: {}", url.as_str());
|
||||
};
|
||||
log::debug!("Prepare request");
|
||||
|
||||
pkarr_host
|
||||
pubky_host
|
||||
}
|
||||
|
||||
pub async fn transform_url(&self, url: &mut Url, qname: &str) {
|
||||
pub async fn transform_url(&self, url: &mut Url) {
|
||||
let clone = url.clone();
|
||||
let qname = clone.host_str().unwrap_or("");
|
||||
log::debug!("Prepare request {}, {}", url.as_str(), qname);
|
||||
|
||||
let mut stream = self.pkarr.resolve_https_endpoints(qname);
|
||||
|
||||
let mut so_far: Option<Endpoint> = None;
|
||||
|
||||
Reference in New Issue
Block a user