Skip to content

Commit

Permalink
feat: wrap with html for non curl clients
Browse files Browse the repository at this point in the history
  • Loading branch information
ozwaldorf committed Oct 12, 2024
1 parent dc827f4 commit f2596ba
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ pad = "0.1"
humanize-bytes = "1.0"
humantime = "2.1"
urlencoding = "2.1.3"

htmlescape = "0.3.1"
34 changes: 26 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::io::{BufRead, Write};
use std::io::{BufRead, Read, Write};
use std::time::{Duration, SystemTime};

use fastly::cache::simple::CacheEntry;
Expand Down Expand Up @@ -157,13 +157,31 @@ fn handle_get(req: Request) -> Result<Response, Error> {
// Usage page
Some("") => {
let usage = get_usage(&host)?;
Ok(Response::from_body(usage)
.with_content_type(mime::TEXT_PLAIN_UTF_8)
.with_header(
// Some browsers will set the title to this header
header::CONTENT_DISPOSITION,
r#"inline; filename="no bs pastebin.txt"; filename*=UTF-8''no%20bs%20pastebin.txt"#,
))

// For all other clients other than curl, wrap with html (ie, browsers)
if let Some(agent) = req.get_header_str("user-agent") {
if !agent.starts_with("curl") {
let wrapped = "\
<!DOCTYPE html>
<html>
<head>
<title>no bs pastebin</title>
</head>
<body>
<pre>"
.to_string()
+ htmlescape::encode_minimal(&String::from_utf8_lossy(&usage.into_bytes()))
.as_str()
+ "\
</pre>
</body>
</html>";

return Ok(Response::new().with_body_text_html(&wrapped));
}
}

Ok(Response::from_body(usage).with_content_type(mime::TEXT_PLAIN_UTF_8))
},

// Privacy policy page
Expand Down

0 comments on commit f2596ba

Please sign in to comment.