use fastly::{http::{StatusCode, HeaderName, header::*}, mime, Error, Request, Response};
static ALLOWED_HEADERS: [HeaderName; 1] = [CONTENT_TYPE];
fn handle_request(req: Request) -> Result<(), Error> {
let mut beresp = req.clone_without_body().send("origin_0")?;
filter_headers(&mut beresp);
if beresp
.get_content_type()
.map(|c| c.subtype() == mime::HTML)
.unwrap_or(false)
{
let processor = esi::Processor::new(
esi::Configuration::default()
);
processor.execute_esi(req, beresp, &|req| {
match req.get_path() {
"/_fragments/content.html" => Ok(Response::from_body("<p>Content injected at the edge.</p>")),
_ => Ok(req.with_ttl(120).send("origin_0")?)
}
})?;
Ok(())
} else {
beresp.send_to_client();
Ok(())
}
}
fn filter_headers(resp: &mut Response) {
let to_remove: Vec<_> = resp
.get_header_names()
.filter(|header| !ALLOWED_HEADERS.contains(header))
.cloned()
.collect();
for header in to_remove {
resp.remove_header(header);
}
}
fn main() {
env_logger::builder()
.filter(None, log::LevelFilter::Trace)
.init();
if let Err(err) = handle_request(Request::from_client()) {
println!("returning error response");
Response::from_status(StatusCode::INTERNAL_SERVER_ERROR)
.with_body(err.to_string())
.send_to_client();
}
}