Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CORS preflight (HTTP options method) #39

Open
danielweck opened this issue Nov 19, 2018 · 0 comments
Open

CORS preflight (HTTP options method) #39

danielweck opened this issue Nov 19, 2018 · 0 comments

Comments

@danielweck
Copy link
Member

Currently, a number of HTTP routes / URL paths defined in r2-streamer-js invoke the setResponseCORS() utility function in order to populate HTTP responses with adequate CORS headers:

public setResponseCORS(res: express.Response) {
res.setHeader("Access-Control-Allow-Origin",
"*");
res.setHeader("Access-Control-Allow-Methods",
"GET, HEAD, OPTIONS"); // POST, DELETE, PUT, PATCH
res.setHeader("Access-Control-Allow-Headers",
"Content-Type, Content-Length, Accept-Ranges, Content-Range, Range, Link, Transfer-Encoding");
res.setHeader("Access-Control-Expose-Headers",
"Content-Type, Content-Length, Accept-Ranges, Content-Range, Range, Link, Transfer-Encoding");
}

( https://github.com/readium/r2-streamer-js/blob/develop/src/http/server.ts#L259-L271 )

For example, for serving the ReadiumWebPublicationManifest (JSON):

server.setResponseCORS(res);
res.set("Content-Type", "application/webpub+json; charset=utf-8");

( https://github.com/readium/r2-streamer-js/blob/develop/src/http/server-manifestjson.ts#L344-L345 )

However, what about HTTP options for CORS? How is this useful? What are the use-cases?

Code example:

public expressOptions(paths: string[], func: express.Handler) {
    this.expressApp.options(paths, func);
}

...added to:

public expressUse(pathf: string, func: express.Handler) {
this.expressApp.use(pathf, func);
}
public expressGet(paths: string[], func: express.Handler) {
this.expressApp.get(paths, func);
}

( https://github.com/readium/r2-streamer-js/blob/develop/src/http/server.ts#L136-L142 )

...and:

_server.expressOptions(["/URL_ROUTE_PATH"],
    (req: express.Request, res: express.Response) => {

    res.setHeader("Access-Control-Allow-Origin", "*");
    res.setHeader("Access-Control-Allow-Credentials", true);
    res.setHeader("Access-Control-Allow-Methods", req.headers["access-control-request-method"]);
    res.setHeader("Access-Control-Allow-Headers", req.headers["access-control-request-headers"]);
    res.status(200).end();
});

Inspired from NYPL's customizations:

https://github.com/NYPL-Simplified/opds-web-client/blob/1600781c267e241e5deb4094007e50df79eb6236/packages/server/index.js#L73
( https://github.com/NYPL-Simplified/opds-web-client/blob/master/packages/server/index.js#L73 )

CC @aslagle

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant