#Sending a NamedFile with some headers

1 messages · Page 1 of 1 (latest)

jade tartan
#

I need to serve some files and some headers. Specifically:

CROSS_ORIGIN_EMBEDDER_POLICY = require-corp
CROSS_ORIGIN_OPENER_POLICY = same-origin

The files I need to serve are a js file and an html file.
My current implementation look like this (but I don't know why it doesn't work):

fn open(x: PathBuf) -> String {
    return fs::read_to_string(x).expect("Something went wrong");
}

#[get("/mithics")]
async fn mithics() -> impl Responder {
    NamedFile::open_async("./content/mithics.html").await
}

#[get("mithics.js")]
async fn mithics_js() -> impl Responder {
    let mut file = PathBuf::new();
    file.push("./content/mithics.js");
    HttpResponse::Ok()
        .content_type(ContentType::plaintext())
        .append_header(("CROSS_ORIGIN_EMBEDDER_POLICY", "require-corp"))
        .append_header(("CROSS_ORIGIN_OPENER_POLICY", "same-origin"))
        .body(open(file))
}

I looked online and after looking at:

jade tartan
#

I solved the problem by myself

#

The problem was that the wrong headers were sent and also the headers need to be sent along with the html, not the javascript

elder shuttle
#

Definitely true for those headers. To make things simpler you could use the DefaultHeaders middleware.