Skip to content

Commit

Permalink
fixed ureq bc breaks
Browse files Browse the repository at this point in the history
  • Loading branch information
jaytaph committed Feb 7, 2025
1 parent 675fbbc commit 2c538f3
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 38 deletions.
4 changes: 2 additions & 2 deletions crates/gosub_net/src/http/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ mod tests {
fn test_headers() {
let mut headers = Headers::new();

headers.set_str("Content-Type", "application/json");
headers.set("Content-Type", "application/json");
assert_eq!(headers.get("Content-Type").unwrap(), "application/json");

headers.set_str("Content-Type", "text/html");
headers.set("Content-Type", "text/html");
assert_eq!(headers.get("Content-Type").unwrap(), "text/html");
assert_eq!(headers.all().len(), 1);
}
Expand Down
6 changes: 3 additions & 3 deletions crates/gosub_net/src/http/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ mod tests {

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

let s = format!("{}", req);
assert_eq!(s, "GET / HTTP/1.1\nHeaders:\n Accept: text/html\n Accept-Encoding: gzip, deflate, br\n Content-Type: application/json\nCookies:\n foo=bar\n qux=wok\nBody: 0 bytes\n");
Expand Down
2 changes: 1 addition & 1 deletion crates/gosub_net/src/http/request_impl/ureq_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl TryFrom<http::response::Response<Body>> for Response {
http::Version::HTTP_3 => "http/3.0".into(),
_ => "http/1.0".into(),
},
headers: get_headers(&response.headers()),
headers: get_headers(response.headers()),
body: response.body_mut().read_to_vec()?,
cookies: Default::default(),
})
Expand Down
2 changes: 1 addition & 1 deletion crates/gosub_net/src/http/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ mod tests {
assert_eq!(s, "HTTP/1.1 0\nHeaders:\nCookies:\nBody: 0 bytes\n");

response.status = 200;
response.headers.set_str("Content-Type", "application/json");
response.headers.set("Content-Type", "application/json");
response.cookies.insert("session".to_string(), "1234567890".to_string());
response.body = b"Hello, world!".to_vec();

Expand Down
1 change: 0 additions & 1 deletion examples/vello-renderer/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use gosub_interface::config::ModuleConfiguration;
use gosub_interface::input::{InputEvent, MouseButton};
use gosub_interface::render_backend::{Point, RenderBackend, SizeU32, FP};
use gosub_shared::types::Result;
use log::info;
use winit::event::{ElementState, MouseScrollDelta, WindowEvent};
use winit::event_loop::ActiveEventLoop;
use winit::keyboard::{KeyCode, ModifiersState, PhysicalKey};
Expand Down
2 changes: 1 addition & 1 deletion examples/vello-renderer/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub struct Window<'a, C: ModuleConfiguration> {
pub(crate) handles: Handles<C>,
}

impl<'a, C: ModuleConfiguration<ChromeHandle = WinitEventLoopHandle<C>>> Window<'a, C> {
impl<C: ModuleConfiguration<ChromeHandle = WinitEventLoopHandle<C>>> Window<'_, C> {
pub fn new(
event_loop: &ActiveEventLoop,
backend: &mut C::RenderBackend,
Expand Down
4 changes: 2 additions & 2 deletions src/bin/css3-parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ fn main() -> Result<()> {

let css = if url.starts_with("http://") || url.starts_with("https://") {
// Fetch the html from the url
let response = ureq::get(&url).call()?;
let mut response = ureq::get(&url).call()?;
if response.status() != 200 {
bail!(format!("Could not get url. Status code {}", response.status()));
}
response.into_string()?
response.body_mut().read_to_string()?
} else if url.starts_with("file://") {
let path = url.trim_start_matches("file://");
fs::read_to_string(path)?
Expand Down
6 changes: 3 additions & 3 deletions src/bin/display-text-tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ fn main() -> Result<()> {
.unwrap();

// Fetch the html from the url
let response = ureq::get(&url).call().map_err(Box::new)?;
if !response.status() == 200 {
let mut response = ureq::get(&url).call().map_err(Box::new)?;
if response.status() != 200 {
println!("could not get url. Status code {}", response.status());
exit(1);
}
let html = response.into_string()?;
let html = response.body_mut().read_to_string()?;

let mut stream = ByteStream::new(Encoding::UTF8, None);
stream.read_from_str(&html, Some(Encoding::UTF8));
Expand Down
4 changes: 2 additions & 2 deletions src/bin/gosub-parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ fn main() -> Result<()> {

let html = if url.scheme() == "http" || url.scheme() == "https" {
// Fetch the html from the url
let response = ureq::get(url.as_ref()).call()?;
let mut response = ureq::get(url.as_ref()).call()?;
if response.status() != 200 {
bail!("Could not get url. Status code {}", response.status());
}
response.into_string()?
response.body_mut().read_to_string()?
} else if url.scheme() == "file" {
// Get html from the file
fs::read_to_string(url.to_string().trim_start_matches("file://"))?
Expand Down
36 changes: 14 additions & 22 deletions src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use {
gosub_shared::byte_stream::{ByteStream, Encoding},
gosub_shared::types::{ParseError, Result},
gosub_shared::{timing_start, timing_stop},
std::io::Read,
url::Url,
};

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

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

match req.call() {
Ok(resp) => {
Ok(mut resp) => {
fetch_response.response = Response::new();
fetch_response.response.status = resp.status();
fetch_response.response.version = format!("{:?}", resp.http_version());
for key in &resp.headers_names() {
for value in resp.all(key) {
fetch_response.response.headers.set_str(key.as_str(), value);
}
fetch_response.response.status = resp.status().as_u16();
fetch_response.response.version = format!("{:?}", resp.version());
for (key, val) in resp.headers().iter() {
fetch_response
.response
.headers
.set(key.as_str(), val.to_str().unwrap_or(""));
}
// TODO: cookies
// for cookie in resp.cookies() {
// fetch_response.response.cookies.insert(cookie.name().to_string(), cookie.value().to_string());
// }

let len = if let Some(header) = resp.header("Content-Length") {
header.parse::<usize>().unwrap_or_default()
} else {
MAX_BYTES as usize
};

let mut bytes: Vec<u8> = Vec::with_capacity(len);
resp.into_reader().take(MAX_BYTES).read_to_end(&mut bytes)?;
fetch_response.response.body = bytes;
fetch_response.response.body = resp.body_mut().read_to_vec()?;
}
Err(e) => {
return Err(Error::Generic(format!("Failed to fetch URL: {}", e)).into());
Expand Down Expand Up @@ -189,7 +181,7 @@ mod tests {
fn test_fetch_url() {
let url = "https://gosub.io/";
let mut headers = Headers::new();
headers.set_str("User-Agent", USER_AGENT);
headers.set("User-Agent", USER_AGENT);
let cookies = CookieJar::new();

let resp = fetch_url::<Config>("GET", url, headers, cookies);
Expand Down

0 comments on commit 2c538f3

Please sign in to comment.