use std::borrow::Cow;
use std::path::Path;
pub struct Asset {
pub path: &'static str,
pub bytes: &'static [u8],
}
macro_rules! web {
($($file:literal),* $(,)?) => {
&[$(Asset {
path: $file,
bytes: include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/playshare/web/", $file)),
}),*]
};
}
pub static FILES: &[Asset] = web![
"lib/client.js",
"lib/emitter.js",
"lib/index.js",
"lib/limits.js",
"lib/names.js",
"lib/party.js",
"lib/relay.js",
"lib/surface.js",
"lib/sync.js",
"lib/testing.js",
"lib/tvpipe.js",
"launcher/index.html",
"launcher/launcher.css",
"launcher/launcher.js",
"picker/index.html",
"picker/picker.css",
"picker/picker.js",
];
pub const SOURCE: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/src/playshare/web");
pub fn get(path: &str) -> Option<Cow<'static, [u8]>> {
if let Some(dir) = std::env::var_os("TVPIPE_PLAYSHARE_WEB").filter(|d| !d.is_empty()) {
return std::fs::read(Path::new(&dir).join(path))
.ok()
.map(Cow::Owned);
}
FILES
.iter()
.find(|f| f.path == path)
.map(|f| Cow::Borrowed(f.bytes))
}
const LOGO: &str = include_str!("../browser/logo.svg");
pub(crate) fn html_escape(s: &str) -> String {
s.replace('&', "&")
.replace('<', "<")
.replace('>', ">")
.replace('"', """)
}
pub fn error_page(status: u16, reason: &str, game: &str, heading: &str, text: &str) -> Vec<u8> {
let e = html_escape;
format!(
concat!(
"<!doctype html>\n<html lang=\"en\">\n<head>\n<meta charset=\"utf-8\">\n",
"<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n",
"<title>{status} {reason} ยท tvpipe</title>\n",
"<link rel=\"stylesheet\" href=\"/_tvpipe/ui.css\">\n</head>\n<body>\n{logo}",
"<header class=\"top\">\n",
" <div class=\"mark\"><svg viewBox=\"0 0 560 520\" aria-hidden=\"true\"><use href=\"#logo\"/></svg><span>tv<b>pipe</b></span></div>\n",
" <div class=\"pair\">tvpipe<span>Party game</span></div>\n",
" <div class=\"pair\">Game<span>{game}</span></div>\n",
"</header>\n<main>\n <section class=\"row ph\">\n",
" <div class=\"meta\"><span class=\"lbl\">Status</span><span class=\"state bad\">{status} {reason}</span></div>\n",
" <div class=\"body\">\n <h1>{heading}</h1>\n <p class=\"lead\">{text}</p>\n </div>\n",
" </section>\n</main>\n</body>\n</html>\n"
),
status = status,
reason = e(reason),
logo = LOGO,
game = e(game),
heading = e(heading),
text = e(text),
)
.into_bytes()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn error_pages_escape_what_they_show() {
let page = error_page(
404,
"Not Found",
"Tom & <Jerry>",
"Nothing \"here\"",
"a < b",
);
let page = String::from_utf8(page).unwrap();
assert!(
page.contains("<span>Tom & <Jerry></span>"),
"{page}"
);
assert!(page.contains("<h1>Nothing "here"</h1>"), "{page}");
assert!(page.contains("<p class=\"lead\">a < b</p>"), "{page}");
assert!(page.contains("<symbol id=\"logo\""), "{page}");
}
}