Skip to content

Commit

Permalink
feat(js-poc): separate query params from path
Browse files Browse the repository at this point in the history
  • Loading branch information
matthias-wright committed Apr 29, 2024
1 parent c86feea commit dc495c5
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 12 deletions.
2 changes: 2 additions & 0 deletions services/js-poc/examples/js-poc-client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ async fn main() -> anyhow::Result<()> {
origin,
uri,
path: None,
query_params: None,
url_fragment: None,
method: None,
headers: None,
param,
Expand Down
56 changes: 44 additions & 12 deletions services/js-poc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashMap;

use anyhow::{anyhow, bail, Context};
use arrayref::array_ref;
use cid::Cid;
Expand Down Expand Up @@ -110,6 +112,8 @@ async fn handle_request(
origin,
uri,
path,
query_params: _,
url_fragment: _,
method: _,
headers: _,
param,
Expand Down Expand Up @@ -284,14 +288,16 @@ fn extract_request(url: &Url, body: &[u8], detail: &TransportDetail) -> Option<R
if path.is_empty() {
path.push('/');
}
if let Some(q) = url.query() {
path.push('?');
path.push_str(q);
}
if let Some(f) = url.fragment() {
path.push('#');
path.push_str(f);
}

let query_params: HashMap<String, String> = url
.query_pairs()
.map(|(key, val)| (key.to_string(), val.to_string()))
.collect();
let query_params = if query_params.is_empty() {
None
} else {
Some(query_params)
};

let param = if body.is_empty() {
url.query_pairs()
Expand All @@ -315,6 +321,8 @@ fn extract_request(url: &Url, body: &[u8], detail: &TransportDetail) -> Option<R
origin,
uri: seg2.to_string(),
path: Some(path),
query_params,
url_fragment: url.fragment().map(|frag| frag.to_string()),
method,
headers,
param,
Expand Down Expand Up @@ -343,6 +351,8 @@ mod tests {
origin: Origin::Blake3,
uri: "content-hash".to_string(),
path: Some("/".to_string()),
query_params: None,
url_fragment: None,
method: None,
headers: None,
param: None,
Expand All @@ -359,6 +369,8 @@ mod tests {
origin: Origin::Blake3,
uri: "content-hash".to_string(),
path: Some("/a".to_string()),
query_params: None,
url_fragment: None,
method: None,
headers: None,
param: None,
Expand All @@ -375,12 +387,16 @@ mod tests {
origin: Origin::Blake3,
uri: "content-hash".to_string(),
path: Some("/a/b".to_string()),
query_params: None,
url_fragment: None,
method: None,
headers: None,
param: None,
})
);

let mut query_params = HashMap::new();
query_params.insert("a".to_string(), "4".to_string());
assert_eq!(
extract_request(
&Url::parse("http://fleek/blake3/content-hash/a/b?a=4").unwrap(),
Expand All @@ -390,13 +406,17 @@ mod tests {
Some(Request {
origin: Origin::Blake3,
uri: "content-hash".to_string(),
path: Some("/a/b?a=4".to_string()),
path: Some("/a/b".to_string()),
query_params: Some(query_params),
url_fragment: None,
method: None,
headers: None,
param: None,
})
);

let mut query_params = HashMap::new();
query_params.insert("a".to_string(), "4".to_string());
assert_eq!(
extract_request(
&Url::parse("http://fleek/blake3/content-hash/a/b?a=4#hello").unwrap(),
Expand All @@ -406,13 +426,18 @@ mod tests {
Some(Request {
origin: Origin::Blake3,
uri: "content-hash".to_string(),
path: Some("/a/b?a=4#hello".to_string()),
path: Some("/a/b".to_string()),
query_params: Some(query_params),
url_fragment: Some("hello".to_string()),
method: None,
headers: None,
param: None,
})
);

let mut query_params = HashMap::new();
query_params.insert("a".to_string(), "4".to_string());
query_params.insert("param".to_string(), "{\"a\": 4}".to_string());
assert_eq!(
extract_request(
&Url::parse(
Expand All @@ -425,13 +450,18 @@ mod tests {
Some(Request {
origin: Origin::Blake3,
uri: "content-hash".to_string(),
path: Some("/a/b?a=4&param=%7B%22a%22%3A%204%7D#hello".to_string()),
path: Some("/a/b".to_string()),
query_params: Some(query_params),
url_fragment: Some("hello".to_string()),
method: None,
headers: None,
param: Some(json!({"a": 4})),
})
);

let mut query_params = HashMap::new();
query_params.insert("a".to_string(), "4".to_string());
query_params.insert("param".to_string(), "{\"a\": 4}".to_string());
assert_eq!(
extract_request(
&Url::parse(
Expand All @@ -444,7 +474,9 @@ mod tests {
Some(Request {
origin: Origin::Blake3,
uri: "content-hash".to_string(),
path: Some("/a/b?a=4&param=%7B%22a%22%3A%204%7D#hello".to_string()),
path: Some("/a/b".to_string()),
query_params: Some(query_params),
url_fragment: Some("hello".to_string()),
method: None,
headers: None,
param: Some(json!({"hello": 5})),
Expand Down
4 changes: 4 additions & 0 deletions services/js-poc/src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ pub struct Request {
pub uri: String,
/// Optional path to provide as the window location
pub path: Option<String>,
/// The query params from the url, if they exist
pub query_params: Option<HashMap<String, String>>,
/// The url fragment identifier, if it exists
pub url_fragment: Option<String>,
/// Http method
pub method: Option<HttpMethod>,
/// Headers from the http request
Expand Down

0 comments on commit dc495c5

Please sign in to comment.