Skip to content

Commit

Permalink
fix(js): send correct pubky-host header
Browse files Browse the repository at this point in the history
  • Loading branch information
Nuhvi committed Dec 19, 2024
1 parent f840ce3 commit daa06b6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 36 deletions.
41 changes: 18 additions & 23 deletions pubky-homeserver/src/core/extractors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
}

Expand All @@ -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())
}
}

Expand Down
30 changes: 17 additions & 13 deletions pubky/src/wasm/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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());
}
Expand Down Expand Up @@ -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
Expand All @@ -94,32 +94,36 @@ 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://");
url.set_host(Some(&format!("_pubky.{}", url.host_str().unwrap_or(""))))
.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;
Expand Down

0 comments on commit daa06b6

Please sign in to comment.