allow to configure cors on http

Allow to configure cors on http, if its enabled or not, and what it should output
This commit is contained in:
Shay Banon 2012-09-06 20:35:36 +02:00
parent f659cad8d6
commit b26fd600f0
2 changed files with 13 additions and 7 deletions

View File

@ -73,13 +73,15 @@ public class NettyHttpChannel implements HttpChannel {
resp = new DefaultHttpResponse(HttpVersion.HTTP_1_1, status);
}
if (RestUtils.isBrowser(request.getHeader(HttpHeaders.Names.USER_AGENT))) {
// Add support for cross-origin Ajax requests (CORS)
resp.addHeader("Access-Control-Allow-Origin", "*");
if (request.getMethod() == HttpMethod.OPTIONS) {
// Allow Ajax requests based on the CORS "preflight" request
resp.addHeader("Access-Control-Max-Age", 1728000);
resp.addHeader("Access-Control-Allow-Methods", "OPTIONS, HEAD, GET, POST, PUT, DELETE");
resp.addHeader("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, Content-Length");
if (transport.settings().getAsBoolean("http.cors.enabled", true)) {
// Add support for cross-origin Ajax requests (CORS)
resp.addHeader("Access-Control-Allow-Origin", transport.settings().get("http.cors.allow-origin", "*"));
if (request.getMethod() == HttpMethod.OPTIONS) {
// Allow Ajax requests based on the CORS "preflight" request
resp.addHeader("Access-Control-Max-Age", transport.settings().getAsInt("http.cors.max-age", 1728000));
resp.addHeader("Access-Control-Allow-Methods", transport.settings().get("http.cors.allow-methods", "OPTIONS, HEAD, GET, POST, PUT, DELETE"));
resp.addHeader("Access-Control-Allow-Headers", transport.settings().get("http.cors.allow-headers", "X-Requested-With, Content-Type, Content-Length"));
}
}
}

View File

@ -162,6 +162,10 @@ public class NettyHttpServerTransport extends AbstractLifecycleComponent<HttpSer
maxChunkSize, maxHeaderSize, maxInitialLineLength, this.maxContentLength, receivePredictorMin, receivePredictorMax);
}
public Settings settings() {
return this.settings;
}
public void httpServerAdapter(HttpServerAdapter httpServerAdapter) {
this.httpServerAdapter = httpServerAdapter;
}