Commit a94e2f24 authored by Amos Wenger's avatar Amos Wenger

serve actual files

parent ce19a281
Pipeline #14967 failed with stages
in 3 minutes and 7 seconds
......@@ -63,6 +63,7 @@ version = "0.1.0"
dependencies = [
"env_logger 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
"percent-encoding 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"tether 0.3.5",
]
......
......@@ -11,3 +11,4 @@ lto = true
tether = { path = "./vendor/tether" }
env_logger = "0.7.1"
log = "0.4.8"
percent-encoding = "2.1.0"
......@@ -15,18 +15,41 @@ impl tether::Handler for Handler {
}
fn handle_net(&mut self, req: tether::NetRequest) -> Result<(), Box<dyn std::error::Error>> {
info!("[net] requesting {}", req.uri());
if req.uri().find("127.0.0.1").is_some() {
info!("intercepting request!");
let s = include_str!("./resources/index.html");
req.respond(tether::NetResponse {
status_code: 200,
content: s.as_bytes(),
});
} else {
info!("letting request through");
let url = req.url();
info!("[net] requesting {:?}", url);
match url.host_str() {
Some("itch-lite") => {
info!("intercepting request!");
let path = url.path();
let path = percent_encoding::percent_decode(path.as_bytes()).decode_utf8()?;
let path = path.trim_start_matches("/");
info!("path = {:?}", path);
let file_path = std::path::PathBuf::from("src").join("resources").join(path);
info!("file_path = {:?}", file_path);
match std::fs::read(file_path) {
Ok(f) => {
req.respond(tether::NetResponse {
status_code: 200,
content: &f[..],
});
return Ok(());
}
Err(_) => {
req.respond(tether::NetResponse {
status_code: 404,
content: "not found".as_bytes(),
});
return Ok(());
}
}
}
_ => {
info!("letting request through");
}
}
Ok(())
......@@ -54,5 +77,5 @@ fn start() {
});
win.title("itch lite");
win.navigate("http://127.0.0.1/index.html");
win.navigate("http://itch-lite/index.html");
}
......@@ -65,7 +65,7 @@
}
window.testFetchLocalhost = () => {
testFetch("http://127.0.0.1:8080/index.html")
testFetch("http://itch-lite/index.html")
};
window.testFetchRemote = () => {
......@@ -76,10 +76,11 @@
</head>
<body>
<p>Here's an image</p>
<p>Here's *two* images</p>
<p>
<img src="https://img.itch.zone/aW1nLzI2NTUzODEucG5n/300x240%23c/6%2B4W1J.png">
<img src="https://itch-lite/sample.png">
</p>
<p>And some buttons:</p>
......
......@@ -10,6 +10,7 @@ use std::ffi::{c_void, CStr, CString};
use std::rc::Rc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::{panic, process};
use url::Url;
thread_local! {
static MAIN_THREAD: Cell<bool> = Cell::new(false);
......@@ -37,15 +38,15 @@ pub trait Handler: 'static {
/// XMLHTTPRequest, a fetch(), an img src, anything
pub struct NetRequest<'a> {
/// The URI that was requested by the webview
request_uri: &'a str,
url: Url,
/// The underlying raw request
req: &'a raw::tether_net_request,
}
impl<'a> NetRequest<'a> {
/// Returns the URI that was requested by the webview
pub fn uri(&self) -> &str {
self.request_uri
pub fn url(&self) -> &Url {
&self.url
}
/// Set the response for this request. bypassing the
......@@ -59,10 +60,9 @@ impl<'a> NetRequest<'a> {
unsafe fn from_raw(
req: &'a raw::tether_net_request,
) -> Result<Self, Box<dyn std::error::Error>> {
Ok(Self {
request_uri: CStr::from_ptr(req.request_uri).to_str()?,
req,
})
let request_uri = CStr::from_ptr(req.request_uri).to_str()?;
let url = Url::parse(request_uri)?;
Ok(Self { url, req })
}
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment