Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
jaytaph committed Feb 7, 2025
1 parent ef845f3 commit 675fbbc
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 56 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 13 additions & 7 deletions crates/gosub_html5/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4118,7 +4118,7 @@ impl<'a, C: HasDocument> Html5Parser<'a, C> {
);
return None;
}
let response = response.expect("result");
let mut response = response.expect("result");

if response.status() != 200 {
warn!(
Expand All @@ -4128,14 +4128,20 @@ impl<'a, C: HasDocument> Html5Parser<'a, C> {
);
return None;
}
if response.content_type() != "text/css" {
warn!(
"External stylesheet has no text/css content type: {} ",
response.content_type()
);
let content_type = response.headers().get("Content-Type");
match content_type {
Some(content_type) => {
let content_type = content_type.to_str().unwrap_or("");
if !content_type.starts_with("text/css") {
warn!("External stylesheet has no text/css content type: {} ", content_type);
}
}
None => {
warn!("External stylesheet has no content type: {} ", url);
}
}

match response.into_string() {
match response.body_mut().read_to_string() {
Ok(css) => css,
Err(err) => {
warn!("Could not load external stylesheet from {}. Error: {}", url, err);
Expand Down
3 changes: 0 additions & 3 deletions crates/gosub_net/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@ anyhow = "1.0.94"
log = "0.4.22"
domain-lookup-tree = "0.1"
cookie = { version = "0.18.1", features = ["secure", "private"] }
http = "1.2.0"
url = "2.5.4"



[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
hickory-resolver = "0.24.2"
ureq = "3.0.3"
Expand Down
2 changes: 0 additions & 2 deletions crates/gosub_net/src/http/fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ pub trait RequestAgent: Debug {
type Error: Error;

fn new() -> Self;

fn get(&self, url: &str) -> impl Future<Output = Result<Response>>;

fn get_req(&self, req: &Request) -> impl Future<Output = Result<Response>>;
}

Expand Down
6 changes: 1 addition & 5 deletions crates/gosub_net/src/http/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,10 @@ impl Headers {
}
}

pub fn set_str(&mut self, key: &str, value: &str) {
pub fn set(&mut self, key: &str, value: &str) {
self.headers.insert(key.to_string(), value.to_string());
}

pub fn set(&mut self, key: String, value: String) {
self.headers.insert(key, value);
}

pub fn get(&self, key: &str) -> Option<&String> {
self.headers.get(key)
}
Expand Down
10 changes: 7 additions & 3 deletions crates/gosub_net/src/http/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ impl Request {
}
}

pub fn add_header(&mut self, key: &str, value: &str) {
self.headers.set(key, value);
}

pub fn headers(&mut self, headers: Headers) {
self.headers = headers;
}
Expand Down Expand Up @@ -63,11 +67,11 @@ mod tests {
req.headers(Headers::new());
req.cookies(CookieJar::new());

req.headers.set_str("Content-Type", "application/json");
req.headers.set("Content-Type", "application/json");
req.cookies.add(Cookie::new("qux", "wok"));
req.cookies.add(Cookie::new("foo", "bar"));
req.headers.set_str("Accept", "text/html");
req.headers.set_str("Accept-Encoding", "gzip, deflate, br");
req.headers.set("Accept", "text/html");
req.headers.set("Accept-Encoding", "gzip, deflate, br");

assert_eq!(req.method, "GET");
assert_eq!(req.uri, "/");
Expand Down
54 changes: 23 additions & 31 deletions crates/gosub_net/src/http/request_impl/ureq_impl.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ureq::Agent;
use ureq::{http, Agent, Body};

use crate::http::fetcher::RequestAgent;
use crate::http::headers::Headers;
Expand All @@ -17,10 +17,10 @@ impl From<Agent> for UreqAgent {
}

impl RequestAgent for UreqAgent {
type Error = ureq::Error;
type Error = http::Error;

fn new() -> Self {
Agent::new().into()
Agent::new_with_defaults().into()
}

async fn get(&self, url: &str) -> gosub_shared::types::Result<Response> {
Expand All @@ -33,42 +33,34 @@ impl RequestAgent for UreqAgent {
}
}

fn get_headers(response: &ureq::Response) -> Headers {
let names = response.headers_names();
fn get_headers(http_headers: &http::header::HeaderMap) -> Headers {
let mut headers = Headers::with_capacity(http_headers.len());

let mut headers = Headers::with_capacity(names.len());

for name in names {
let header = response.header(&name).unwrap_or_default().to_string();

headers.set(name, header);
for (name, value) in http_headers.iter() {
headers.set(name.as_str(), value.to_str().unwrap_or_default());
}

headers
}

impl TryFrom<ureq::Response> for Response {
impl TryFrom<http::response::Response<Body>> for Response {
type Error = anyhow::Error;

fn try_from(value: ureq::Response) -> std::result::Result<Self, Self::Error> {
let body = Vec::with_capacity(
value
.header("Content-Length")
.map(|s| s.parse().unwrap_or(0))
.unwrap_or(0),
);

let mut this = Self {
status: value.status(),
status_text: value.status_text().to_string(),
version: value.http_version().to_string(),
headers: get_headers(&value),
body,
fn try_from(mut response: http::response::Response<Body>) -> Result<Self, Self::Error> {
Ok(Self {
status: response.status().as_u16(),
status_text: response.status().to_string(),
version: match response.version() {
http::Version::HTTP_09 => "http/0.9".into(),
http::Version::HTTP_10 => "http/1.0".into(),
http::Version::HTTP_11 => "http/1.1".into(),
http::Version::HTTP_2 => "http/2.0".into(),
http::Version::HTTP_3 => "http/3.0".into(),
_ => "http/1.0".into(),
},
headers: get_headers(&response.headers()),
body: response.body_mut().read_to_vec()?,
cookies: Default::default(),
};

value.into_reader().read_to_end(&mut this.body)?;

Ok(this)
})
}
}
8 changes: 4 additions & 4 deletions src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub struct FetchResponse<C: HasDocument> {
pub document: C::Document,
/// Parse errors that occurred while parsing the document tree
pub parse_errors: Vec<ParseError>,
/// Rendertree that is generated from the document tree and css tree
/// Render tree that is generated from the document tree and css tree
pub render_tree: String,
}

Expand Down Expand Up @@ -99,10 +99,10 @@ fn fetch_url<C: HasHtmlParser>(
// Fetch the HTML document from the site
let t_id = timing_start!("http.transfer", parts.host_str().unwrap());

let agent = ureq::agent();
let mut req = agent.request(method, url).set("User-Agent", USER_AGENT);
let mut req = Request::new(method, url, "HTTP/1.1");
req.add_header("User-Agent", USER_AGENT);
for (key, value) in headers.sorted() {
req = req.set(key, value);
req.add_header(key, value);
}

match req.call() {
Expand Down

0 comments on commit 675fbbc

Please sign in to comment.