mirror of
https://github.com/MercuryWorkshop/epoxy-tls.git
synced 2025-05-13 14:30:02 -04:00
title case headers and several other innaccuracies
This commit is contained in:
parent
6e973a98d5
commit
85b57b8019
1 changed files with 34 additions and 15 deletions
|
@ -12,7 +12,11 @@ use std::sync::Arc;
|
||||||
|
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use http::{uri, HeaderName, HeaderValue, Request, Response};
|
use http::{uri, HeaderName, HeaderValue, Request, Response};
|
||||||
use hyper::{body::Incoming, client::conn as hyper_conn, Uri};
|
use hyper::{
|
||||||
|
body::Incoming,
|
||||||
|
client::conn::{self as hyper_conn, http1::Builder},
|
||||||
|
Uri,
|
||||||
|
};
|
||||||
use js_sys::{Array, Object, Reflect, Uint8Array};
|
use js_sys::{Array, Object, Reflect, Uint8Array};
|
||||||
use penguin_mux_wasm::{Multiplexor, MuxStream, Role};
|
use penguin_mux_wasm::{Multiplexor, MuxStream, Role};
|
||||||
use tokio_rustls::{client::TlsStream, rustls, rustls::RootCertStore, TlsConnector};
|
use tokio_rustls::{client::TlsStream, rustls, rustls::RootCertStore, TlsConnector};
|
||||||
|
@ -33,8 +37,10 @@ type WsTcpUnencryptedStream = MuxStream<WsStreamWrapper>;
|
||||||
type WsTcpStream = Either<WsTcpTlsStream, WsTcpUnencryptedStream>;
|
type WsTcpStream = Either<WsTcpTlsStream, WsTcpUnencryptedStream>;
|
||||||
|
|
||||||
async fn send_req(req: http::Request<HttpBody>, io: WsTcpStream) -> Result<WsTcpResponse, JsError> {
|
async fn send_req(req: http::Request<HttpBody>, io: WsTcpStream) -> Result<WsTcpResponse, JsError> {
|
||||||
let (mut req_sender, conn) =
|
let (mut req_sender, conn) = Builder::new()
|
||||||
hyper_conn::http1::handshake::<TokioIo<WsTcpStream>, HttpBody>(TokioIo::new(io))
|
.title_case_headers(true)
|
||||||
|
.preserve_header_case(true)
|
||||||
|
.handshake(TokioIo::new(io))
|
||||||
.await
|
.await
|
||||||
.replace_err("Failed to connect to host")?;
|
.replace_err("Failed to connect to host")?;
|
||||||
|
|
||||||
|
@ -55,7 +61,11 @@ async fn send_req(req: http::Request<HttpBody>, io: WsTcpStream) -> Result<WsTcp
|
||||||
if utils::is_redirect(res.status().as_u16())
|
if utils::is_redirect(res.status().as_u16())
|
||||||
&& let Some(location) = res.headers().get("Location")
|
&& let Some(location) = res.headers().get("Location")
|
||||||
&& let Ok(redirect_url) = new_req.uri().get_redirect(location)
|
&& let Ok(redirect_url) = new_req.uri().get_redirect(location)
|
||||||
&& let Some(redirect_url_authority) = redirect_url.clone().authority().replace_err("Redirect URL must have an authority").ok()
|
&& let Some(redirect_url_authority) = redirect_url
|
||||||
|
.clone()
|
||||||
|
.authority()
|
||||||
|
.replace_err("Redirect URL must have an authority")
|
||||||
|
.ok()
|
||||||
{
|
{
|
||||||
let should_strip = new_req.uri().is_same_host(&redirect_url);
|
let should_strip = new_req.uri().is_same_host(&redirect_url);
|
||||||
if should_strip {
|
if should_strip {
|
||||||
|
@ -66,9 +76,10 @@ async fn send_req(req: http::Request<HttpBody>, io: WsTcpStream) -> Result<WsTcp
|
||||||
let new_url = redirect_url.clone();
|
let new_url = redirect_url.clone();
|
||||||
*new_req.uri_mut() = redirect_url;
|
*new_req.uri_mut() = redirect_url;
|
||||||
new_req.headers_mut().remove("Host");
|
new_req.headers_mut().remove("Host");
|
||||||
new_req
|
new_req.headers_mut().insert(
|
||||||
.headers_mut()
|
"Host",
|
||||||
.insert("Host", HeaderValue::from_str(redirect_url_authority.as_str())?);
|
HeaderValue::from_str(redirect_url_authority.as_str())?,
|
||||||
|
);
|
||||||
Ok(WsTcpResponse::Redirect((res, new_req, new_url)))
|
Ok(WsTcpResponse::Redirect((res, new_req, new_url)))
|
||||||
} else {
|
} else {
|
||||||
Ok(WsTcpResponse::Success(res))
|
Ok(WsTcpResponse::Success(res))
|
||||||
|
@ -94,7 +105,11 @@ pub struct WsTcp {
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
impl WsTcp {
|
impl WsTcp {
|
||||||
#[wasm_bindgen(constructor)]
|
#[wasm_bindgen(constructor)]
|
||||||
pub async fn new(ws_url: String, useragent: String, redirect_limit: usize) -> Result<WsTcp, JsError> {
|
pub async fn new(
|
||||||
|
ws_url: String,
|
||||||
|
useragent: String,
|
||||||
|
redirect_limit: usize,
|
||||||
|
) -> Result<WsTcp, JsError> {
|
||||||
let ws_uri = ws_url
|
let ws_uri = ws_url
|
||||||
.parse::<uri::Uri>()
|
.parse::<uri::Uri>()
|
||||||
.replace_err("Failed to parse websocket url")?;
|
.replace_err("Failed to parse websocket url")?;
|
||||||
|
@ -126,7 +141,7 @@ impl WsTcp {
|
||||||
mux,
|
mux,
|
||||||
rustls_config,
|
rustls_config,
|
||||||
useragent,
|
useragent,
|
||||||
redirect_limit
|
redirect_limit,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -164,7 +179,7 @@ impl WsTcp {
|
||||||
let mut redirected = false;
|
let mut redirected = false;
|
||||||
let uri = req.uri().clone();
|
let uri = req.uri().clone();
|
||||||
let mut current_resp: WsTcpResponse = send_req(req, self.get_http_io(&uri).await?).await?;
|
let mut current_resp: WsTcpResponse = send_req(req, self.get_http_io(&uri).await?).await?;
|
||||||
for _ in 0..self.redirect_limit-1 {
|
for _ in 0..self.redirect_limit - 1 {
|
||||||
match current_resp {
|
match current_resp {
|
||||||
WsTcpResponse::Success(_) => break,
|
WsTcpResponse::Success(_) => break,
|
||||||
WsTcpResponse::Redirect((_, req, new_url)) => {
|
WsTcpResponse::Redirect((_, req, new_url)) => {
|
||||||
|
@ -243,9 +258,13 @@ impl WsTcp {
|
||||||
}
|
}
|
||||||
|
|
||||||
builder = builder
|
builder = builder
|
||||||
.header("Host", uri_host)
|
// this breaks a shit ton of things
|
||||||
.header("Connection", "close")
|
// .header("Host", uri_host)
|
||||||
.header("User-Agent", self.useragent.clone());
|
// .header("User-Agent", self.useragent.clone())
|
||||||
|
.header("Connection", "close");
|
||||||
|
if body_bytes.len() == 0 {
|
||||||
|
builder = builder.header("Content-Length", 0);
|
||||||
|
}
|
||||||
|
|
||||||
let request = builder
|
let request = builder
|
||||||
.body(HttpBody::new(body_bytes))
|
.body(HttpBody::new(body_bytes))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue