Document proxy.rs

This commit is contained in:
spikecodes 2021-03-10 19:02:03 -08:00
parent 4a51b7cfb0
commit 118ff9485c
No known key found for this signature in database
GPG Key ID: 004CECFF9B463BCB
2 changed files with 8 additions and 2 deletions

View File

@ -2,6 +2,7 @@ use async_std::{io, net::TcpStream, prelude::*};
use async_tls::TlsConnector; use async_tls::TlsConnector;
use tide::{http::url::Url, Request, Response}; use tide::{http::url::Url, Request, Response};
/// Handle tide routes to proxy by parsing `params` from `req`uest.
pub async fn handler(req: Request<()>, format: &str, params: Vec<&str>) -> tide::Result { pub async fn handler(req: Request<()>, format: &str, params: Vec<&str>) -> tide::Result {
let mut url = format.to_string(); let mut url = format.to_string();
@ -13,6 +14,9 @@ pub async fn handler(req: Request<()>, format: &str, params: Vec<&str>) -> tide:
request(url).await request(url).await
} }
/// Sends a request to a Reddit media domain and proxy the response.
///
/// Relays the `Content-Length` and `Content-Type` header.
async fn request(url: String) -> tide::Result { async fn request(url: String) -> tide::Result {
// Parse url into parts // Parse url into parts
let parts = Url::parse(&url).unwrap(); let parts = Url::parse(&url).unwrap();
@ -65,9 +69,11 @@ async fn request(url: String) -> tide::Result {
.unwrap_or_default() .unwrap_or_default()
}; };
// Parse Content-Length and Content-Type from headers
let content_length = header("Content-Length"); let content_length = header("Content-Length");
let content_type = header("Content-Type"); let content_type = header("Content-Type");
// Build response
Ok( Ok(
Response::builder(status) Response::builder(status)
.body(tide::http::Body::from_bytes(body)) .body(tide::http::Body::from_bytes(body))

View File

@ -411,10 +411,10 @@ pub fn format_url(url: &str) -> String {
Ok(parsed) => { Ok(parsed) => {
let domain = parsed.domain().unwrap_or_default(); let domain = parsed.domain().unwrap_or_default();
let capture = |regex: &str, format: &str, levels: i16| { let capture = |regex: &str, format: &str, segments: i16| {
Regex::new(regex) Regex::new(regex)
.map(|re| match re.captures(url) { .map(|re| match re.captures(url) {
Some(caps) => match levels { Some(caps) => match segments {
1 => [format, &caps[1], "/"].join(""), 1 => [format, &caps[1], "/"].join(""),
2 => [format, &caps[1], "/", &caps[2], "/"].join(""), 2 => [format, &caps[1], "/", &caps[2], "/"].join(""),
_ => String::new(), _ => String::new(),