Skip to content

Commit

Permalink
review: update docs, rename parse functions, modify_request_uri non a…
Browse files Browse the repository at this point in the history
…sync
  • Loading branch information
laruh committed Jun 27, 2024
1 parent c66f316 commit e81cfb9
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/net/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ use jwt::{get_cached_token_or_generate_one, JwtClaims};
use serde_json::json;
use std::net::SocketAddr;

/// Value
/// Header value for `hyper::header::CONTENT_TYPE`
pub(crate) const APPLICATION_JSON: &str = "application/json";
/// Header
/// Represents `X-Forwarded-For` Header key
pub(crate) const X_FORWARDED_FOR: &str = "x-forwarded-for";
async fn get_healthcheck() -> GenericResult<Response<Body>> {
let json = json!({
Expand Down
11 changes: 6 additions & 5 deletions src/proxy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ pub(crate) enum ProxyType {
/// This helps in managing the logic for routing and processing requests appropriately within the proxy layer.
#[derive(Clone, Debug, PartialEq)]
pub(crate) enum PayloadData {
/// Quicknode feature requires body payload and Signed Message in X-Auth-Payload header
Quicknode {
payload: QuicknodePayload,
signed_message: SignedMessage,
},
/// Moralis feature requires only Signed Message in X-Auth-Payload header
/// Moralis feature requires only Signed Message in X-Auth-Payload header and doesn't have body
Moralis(SignedMessage),
}

Expand All @@ -58,15 +59,15 @@ pub(crate) async fn generate_payload_from_req(
match proxy_type {
ProxyType::Quicknode => {
let (req, payload, signed_message) =
parse_body_payload::<QuicknodePayload>(req).await?;
parse_body_and_auth_header::<QuicknodePayload>(req).await?;
let payload_data = PayloadData::Quicknode {
payload,
signed_message,
};
Ok((req, payload_data))
}
ProxyType::Moralis => {
let (req, signed_message) = parse_header_payload(req).await?;
let (req, signed_message) = parse_auth_header(req).await?;
Ok((req, PayloadData::Moralis(signed_message)))
}
}
Expand Down Expand Up @@ -134,7 +135,7 @@ pub(crate) async fn validation_middleware(
/// This function extracts the `X-Auth-Payload` header from the request, parses it into a `SignedMessage`,
/// and then reads and deserializes the request body into a specified type `T`.
/// If the body is empty or the header is missing, an error is returned.
async fn parse_body_payload<T>(
async fn parse_body_and_auth_header<T>(
req: Request<Body>,
) -> GenericResult<(Request<Body>, T, SignedMessage)>
where
Expand All @@ -157,7 +158,7 @@ where
}

/// Parses [SignedMessage] value from X-Auth-Payload header
async fn parse_header_payload(req: Request<Body>) -> GenericResult<(Request<Body>, SignedMessage)> {
async fn parse_auth_header(req: Request<Body>) -> GenericResult<(Request<Body>, SignedMessage)> {
let (parts, body) = req.into_parts();
let header_value = parts
.headers
Expand Down
13 changes: 5 additions & 8 deletions src/proxy/moralis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub(crate) async fn proxy_moralis(

let original_req_uri = req.uri().clone();

if let Err(e) = modify_request_uri(&mut req, proxy_route).await {
if let Err(e) = modify_request_uri(&mut req, proxy_route) {
log::error!(
"{}",
log_format!(
Expand Down Expand Up @@ -90,10 +90,7 @@ pub(crate) async fn proxy_moralis(
/// Modifies the URI of an HTTP request by replacing its base URI with the outbound URI specified in `ProxyRoute`,
/// while incorporating the path and query parameters from the original request URI. Additionally, this function
/// removes the first path segment from the original URI.
async fn modify_request_uri(
req: &mut Request<Body>,
proxy_route: &ProxyRoute,
) -> GenericResult<()> {
fn modify_request_uri(req: &mut Request<Body>, proxy_route: &ProxyRoute) -> GenericResult<()> {
let proxy_base_uri = proxy_route.outbound_route.parse::<Uri>()?;

let req_uri = req.uri().clone();
Expand Down Expand Up @@ -234,7 +231,7 @@ pub(crate) async fn validation_middleware_moralis(

#[tokio::test]
async fn test_parse_moralis_payload() {
use super::{parse_header_payload, X_AUTH_PAYLOAD};
use super::{parse_auth_header, X_AUTH_PAYLOAD};
use hyper::header::HeaderName;
use hyper::Method;

Expand All @@ -256,7 +253,7 @@ async fn test_parse_moralis_payload() {
.body(Body::empty())
.unwrap();

let (mut req, payload) = parse_header_payload(req).await.unwrap();
let (mut req, payload) = parse_auth_header(req).await.unwrap();

let body_bytes = hyper::body::to_bytes(req.body_mut()).await.unwrap();
assert!(
Expand Down Expand Up @@ -301,7 +298,7 @@ async fn test_modify_request_uri() {
rate_limiter: None,
};

modify_request_uri(&mut req, &proxy_route).await.unwrap();
modify_request_uri(&mut req, &proxy_route).unwrap();

assert_eq!(
req.uri(),
Expand Down
7 changes: 4 additions & 3 deletions src/proxy/quicknode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ fn test_quicknode_payload_serialzation_and_deserialization() {

#[tokio::test]
async fn test_parse_quicknode_payload() {
use super::{parse_body_payload, X_AUTH_PAYLOAD};
use super::{parse_body_and_auth_header, X_AUTH_PAYLOAD};
use hyper::Method;

let serialized_payload = json!({
Expand Down Expand Up @@ -362,8 +362,9 @@ async fn test_parse_quicknode_payload() {
"dummy-value".parse().unwrap(),
);

let (mut req, payload, signed_message) =
parse_body_payload::<QuicknodePayload>(req).await.unwrap();
let (mut req, payload, signed_message) = parse_body_and_auth_header::<QuicknodePayload>(req)
.await
.unwrap();

let body_bytes = hyper::body::to_bytes(req.body_mut()).await.unwrap();
assert!(
Expand Down

0 comments on commit e81cfb9

Please sign in to comment.