-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reqwest as proxy library in cf worker. Again.
- Loading branch information
Showing
6 changed files
with
107 additions
and
105 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,71 @@ | ||
use std::collections::HashMap; | ||
use reqwest::{Method as ReqwestMethod, Response as ReqwestResponse}; | ||
use std::{collections::HashMap, convert::TryFrom}; | ||
use worker::{console_log, Headers as CfHeaders, Method as CfMethod, Response as CfResponse}; | ||
|
||
use worker::{Headers, Result}; | ||
pub fn convert_cf_method_to_reqwest( | ||
cf_method: &CfMethod, | ||
) -> Result<ReqwestMethod, http::method::InvalidMethod> { | ||
let method_str = match cf_method { | ||
CfMethod::Get => "GET", | ||
CfMethod::Post => "POST", | ||
CfMethod::Put => "PUT", | ||
CfMethod::Delete => "DELETE", | ||
CfMethod::Options => "OPTIONS", | ||
CfMethod::Head => "HEAD", | ||
CfMethod::Patch => "PATCH", | ||
CfMethod::Connect => "CONNECT", | ||
CfMethod::Trace => "TRACE", | ||
}; | ||
|
||
ReqwestMethod::try_from(method_str) | ||
} | ||
|
||
pub fn merge_headers( | ||
original_headers: HashMap<String, String>, | ||
extra_headers: HashMap<String, String>, | ||
) -> Result<Headers> { | ||
let mut new_headers = Headers::new(); | ||
) -> reqwest::header::HeaderMap { | ||
let mut header_map = reqwest::header::HeaderMap::new(); | ||
for (key, value) in original_headers | ||
.into_iter() | ||
.chain(extra_headers.into_iter()) | ||
{ | ||
new_headers.set(&key, &value)?; | ||
if let Ok(header_name) = reqwest::header::HeaderName::from_bytes(key.as_bytes()) { | ||
if let Ok(header_value) = reqwest::header::HeaderValue::from_str(&value) { | ||
header_map.append(header_name, header_value); | ||
} | ||
} | ||
} | ||
header_map | ||
} | ||
|
||
pub async fn convert_reqwest_response_to_cf( | ||
response: ReqwestResponse, | ||
extra_headers: HashMap<String, String>, | ||
) -> worker::Result<CfResponse> { | ||
let status = response.status(); | ||
let headers = response.headers().clone(); | ||
|
||
let body_bytes = match response.bytes().await { | ||
Ok(bytes) => bytes, | ||
Err(_) => return CfResponse::error("Error reading response body", 502), | ||
}; | ||
|
||
let cf_response = match CfResponse::from_bytes(body_bytes.to_vec()) { | ||
Ok(response) => response, | ||
Err(_) => return CfResponse::error("Error creating response body", 500), | ||
}; | ||
|
||
let mut cf_headers = CfHeaders::from(headers); | ||
|
||
for (key, value) in extra_headers { | ||
let header_res = cf_headers.set(&key, &value); | ||
if header_res.is_err() { | ||
console_log!("failed to set response header: {}", header_res.unwrap_err()); | ||
} | ||
} | ||
|
||
let cf_response = cf_response.with_headers(cf_headers); | ||
let cf_response = cf_response.with_status(status.into()); | ||
|
||
Ok(new_headers) | ||
Ok(cf_response) | ||
} |
Oops, something went wrong.