-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: migrate http request and response types from ic-response-verifi…
…cation to ic-http-certification BREAKING CHANGE:
- Loading branch information
1 parent
fbc7fb2
commit 78e7b4a
Showing
33 changed files
with
524 additions
and
497 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
//! The error module contains types for common errors that may be thrown | ||
//! by other modules in this crate. | ||
/// HTTP certification result type. | ||
pub type HttpCertificationResult<T = ()> = Result<T, HttpCertificationError>; | ||
|
||
/// HTTP certification error type. | ||
#[derive(thiserror::Error, Debug)] | ||
pub enum HttpCertificationError { | ||
/// The URL was malformed and could not be parsed correctly. | ||
#[error(r#"Failed to parse url: "{0}""#)] | ||
MalformedUrl(String), | ||
|
||
/// Error converting UTF-8 string. | ||
#[error(r#"Error converting UTF8 string bytes: "{0}""#)] | ||
Utf8ConversionError(#[from] std::string::FromUtf8Error), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/// An HTTP header field, represented as a tuple of (name, value). | ||
pub type HeaderField = (String, String); |
113 changes: 113 additions & 0 deletions
113
packages/ic-http-certification/src/http/http_request.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
use crate::{HeaderField, HttpCertificationError, HttpCertificationResult}; | ||
use candid::{CandidType, Deserialize}; | ||
use http::Uri; | ||
|
||
/// A Candid-encodable representation of an HTTP request. | ||
/// This struct is used by canisters that implement the HTTP interface required by the HTTP Gateway Protocol. | ||
#[derive(Clone, Debug, CandidType, Deserialize, PartialEq, Eq)] | ||
pub struct HttpRequest { | ||
/// HTTP request method. | ||
pub method: String, | ||
/// Request URL. | ||
pub url: String, | ||
/// HTTP request headers. | ||
pub headers: Vec<HeaderField>, | ||
/// Request body as an array of bytes. | ||
pub body: Vec<u8>, | ||
} | ||
|
||
impl HttpRequest { | ||
/// Returns the path of the request URL, without domain, query parameters or fragments. | ||
pub fn get_path<'a>(&'a self) -> HttpCertificationResult<String> { | ||
let uri = self | ||
.url | ||
.parse::<Uri>() | ||
.map_err(|_| HttpCertificationError::MalformedUrl(self.url.clone()))?; | ||
|
||
let decoded_path = urlencoding::decode(uri.path()).map(|path| path.into_owned())?; | ||
Ok(decoded_path) | ||
} | ||
|
||
/// Returns the query parameters of the request URL, if any, as a string. | ||
pub fn get_query<'a>(&'a self) -> HttpCertificationResult<Option<String>> { | ||
self.url | ||
.parse::<Uri>() | ||
.map(|uri| uri.query().map(|uri| uri.to_owned())) | ||
.map_err(|_| HttpCertificationError::MalformedUrl(self.url.clone())) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn request_get_uri() { | ||
let req = HttpRequest { | ||
method: "GET".to_string(), | ||
url: "https://canister.com/sample-asset.txt".to_string(), | ||
headers: vec![], | ||
body: vec![], | ||
}; | ||
|
||
let path = req.get_path().unwrap(); | ||
let query = req.get_query().unwrap(); | ||
|
||
assert_eq!(path, "/sample-asset.txt"); | ||
assert!(query.is_none()); | ||
} | ||
|
||
#[test] | ||
fn request_get_encoded_uri() { | ||
let test_requests = [ | ||
( | ||
HttpRequest { | ||
method: "GET".to_string(), | ||
url: "https://canister.com/%73ample-asset.txt".to_string(), | ||
headers: vec![], | ||
body: vec![], | ||
}, | ||
"/sample-asset.txt", | ||
"", | ||
), | ||
( | ||
HttpRequest { | ||
method: "GET".to_string(), | ||
url: "https://canister.com/path/123?foo=test%20component&bar=1".to_string(), | ||
headers: vec![], | ||
body: vec![], | ||
}, | ||
"/path/123", | ||
"foo=test%20component&bar=1", | ||
), | ||
( | ||
HttpRequest { | ||
method: "GET".to_string(), | ||
url: "https://canister.com/a%20file.txt".to_string(), | ||
headers: vec![], | ||
body: vec![], | ||
}, | ||
"/a file.txt", | ||
"", | ||
), | ||
( | ||
HttpRequest { | ||
method: "GET".to_string(), | ||
url: "https://canister.com/mujin0722/3888-zjfrd-tqaaa-aaaaf-qakia-cai/%E6%97%A0%E8%AE%BA%E7%BE%8E%E8%81%94%E5%82%A8%E6%98%AF%E5%90%A6%E5%8A%A0%E6%81%AFbtc%E4%BB%8D%E5%B0%86%E5%9B%9E%E5%88%B07%E4%B8%87%E5%88%80".to_string(), | ||
headers: vec![], | ||
body: vec![], | ||
}, | ||
"/mujin0722/3888-zjfrd-tqaaa-aaaaf-qakia-cai/无论美联储是否加息btc仍将回到7万刀", | ||
"", | ||
), | ||
]; | ||
|
||
for (req, expected_path, expected_query) in test_requests.iter() { | ||
let path = req.get_path().unwrap(); | ||
let query = req.get_query().unwrap(); | ||
|
||
assert_eq!(path, *expected_path); | ||
assert_eq!(query.unwrap_or_default(), *expected_query); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
use crate::HeaderField; | ||
use candid::{CandidType, Deserialize}; | ||
|
||
/// A Candid-encodable representation of an HTTP response. | ||
/// This struct is used by canisters that implement the HTTP interface required by the HTTP Gateway Protocol. | ||
#[derive(Clone, Debug, CandidType, Deserialize, PartialEq, Eq)] | ||
pub struct HttpResponse { | ||
/// HTTP response status code. | ||
pub status_code: u16, | ||
/// HTTP response headers. | ||
pub headers: Vec<HeaderField>, | ||
/// Response body as an array of bytes. | ||
pub body: Vec<u8>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
//! The HTTP module contains types for representing HTTP requests and responses in Rust. | ||
//! These types are Candid-encodable and are used by canisters that implement the | ||
//! HTTP interface required by the HTTP Gateway Protocol. | ||
mod header_field; | ||
mod http_request; | ||
mod http_response; | ||
|
||
pub use header_field::*; | ||
pub use http_request::*; | ||
pub use http_response::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.