Skip to content

Commit

Permalink
fix loading empty contents returns 404 response
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysd committed Feb 2, 2024
1 parent a8d95aa commit fddab17
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 17 deletions.
8 changes: 4 additions & 4 deletions v2/src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ impl Assets {
Self { hljs_css, markdown_css }
}

pub fn load(&self, path: &str) -> (Cow<'static, [u8]>, &'static str) {
pub fn load(&self, path: &str) -> (Option<Cow<'static, [u8]>>, &'static str) {
let mime = guess_mime(path);

#[rustfmt::skip]
Expand All @@ -198,19 +198,19 @@ impl Assets {
#[cfg(debug_assertions)]
"/bundle.js.map" => BUNDLE_JS_MAP.into(),
#[cfg(target_os = "windows")]
"/favicon.ico" => Cow::Owned(vec![]),
"/favicon.ico" => return (None, mime),
path => {
log::debug!("Dynamically loading external resource {:?}", path);
match fs::read(path) {
Ok(content) => content.into(),
Err(err) => {
log::error!("Could not read external resource {:?}: {}", path, err);
Cow::Owned(vec![])
return (None, mime);
}
}
}
};

(body, mime)
(Some(body), mime)
}
}
19 changes: 6 additions & 13 deletions v2/src/wry/webview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ fn create_webview(window: &Window, event_loop: &EventLoop, config: &Config) -> R
log::debug!("Opening local path {:?}", path);
UserEvent::OpenLocalPath(path)
} else {
log::debug!("Navigating to URL {:?}", url);
log::debug!("Navigating to external URL {:?}", url);
UserEvent::OpenExternalLink(url)
};

Expand All @@ -124,18 +124,11 @@ fn create_webview(window: &Window, event_loop: &EventLoop, config: &Config) -> R
let uri = request.uri();
log::debug!("Handling custom protocol: {:?}", uri);
let path = uri.path();
let (body, mime) = loader.load(path);
let status = if body.is_empty() { 404 } else { 200 };
Response::builder().status(status).header(CONTENT_TYPE, mime).body(body).unwrap_or_else(
|err| {
log::error!("Could not build response for request {:?}: {:?}", uri, err);
Response::builder()
.status(404)
.header(CONTENT_TYPE, "application/octet-stream")
.body(vec![].into())
.unwrap()
},
)
let (content, mime) = loader.load(path);
let (body, status) =
if let Some(content) = content { (content, 200) } else { (vec![].into(), 404) };
// The header and status are never invalid so `.unwrap()` call never panics
Response::builder().status(status).header(CONTENT_TYPE, mime).body(body).unwrap()
})
.with_web_context(&mut context)
.with_focused(true)
Expand Down

0 comments on commit fddab17

Please sign in to comment.