Commit a27fd3a4 authored by Heinz N. Gies's avatar Heinz N. Gies Committed by GitHub

Merge pull request #19 from dataloop/cors-middleware

CORS middleware
parents 7c26a1d6 05782ec8
......@@ -11,10 +11,7 @@ init(_Transport, Req, []) ->
-dialyzer({no_opaque, handle/2}).
handle(Req, State) ->
Req0 = cowboy_req:set_resp_header(
<<"access-control-allow-origin">>, <<"*">>, Req),
{ContentType, Req1} = dalmatiner_idx_handler:content_type(Req0),
{ContentType, Req1} = dalmatiner_idx_handler:content_type(Req),
case ContentType of
html ->
F = fun (Socket, Transport) ->
......
......@@ -11,10 +11,7 @@ init(_Transport, Req, []) ->
-dialyzer({no_opaque, handle/2}).
handle(Req, State) ->
Req0 = cowboy_req:set_resp_header(
<<"access-control-allow-origin">>, <<"*">>, Req),
{ContentType, Req1} = dalmatiner_idx_handler:content_type(Req0),
{ContentType, Req1} = dalmatiner_idx_handler:content_type(Req),
case ContentType of
html ->
F = fun (Socket, Transport) ->
......
-module(dalmatiner_cors_m).
-behaviour(cowboy_middleware).
-export([execute/2]).
%%
%% CORS middleware
%% =====================================
-spec execute(cowboy_req:req(), [{atom(), any()}]) ->
{ok, cowboy_req:req(), [{atom(), any()}]} |
{halt, cowboy_req:req()}.
execute(Req, Env) ->
R1 = cowboy_req:set_resp_header(<<"access-control-allow-origin">>,
<<"*">>, Req),
case cowboy_req:method(R1) of
{<<"OPTIONS">>, R2} ->
R3 = cowboy_req:set_resp_header(<<"access-control-allow-methods">>,
<<"GET">>, R2),
R4 = cowboy_req:set_resp_header(<<"access-control-allow-headers">>,
<<"Authorization">>, R3),
R5 = cowboy_req:set_resp_header(<<"access-control-max-age">>,
<<"3600">>, R4),
{ok, R6} = cowboy_req:reply(200, R5),
{halt, R6};
{_, R2} ->
{ok, R2, Env}
end.
......@@ -75,7 +75,11 @@ start(_StartType, _StartArgs) ->
%% Name, NbAcceptors, TransOpts, ProtoOpts
{ok, _} = cowboy:start_http(dalmatiner_http_listener, Listeners,
[{port, Port}],
[{env, [{dispatch, Dispatch}]},
[{env, [{dispatch, Dispatch}]}
{middlewares,
[cowboy_router,
dalmatiner_cors_m,
cowboy_handler]},
{max_keepalive, 5},
{timeout, 50000}]),
dalmatiner_frontend_sup:start_link().
......
......@@ -11,10 +11,7 @@ init(_Transport, Req, []) ->
-dialyzer({no_opaque, handle/2}).
handle(Req, State) ->
Req0 = cowboy_req:set_resp_header(
<<"access-control-allow-origin">>, <<"*">>, Req),
{ContentType, Req1} = dalmatiner_idx_handler:content_type(Req0),
{ContentType, Req1} = dalmatiner_idx_handler:content_type(Req),
case ContentType of
html ->
F = fun (Socket, Transport) ->
......
......@@ -10,9 +10,7 @@ init(_Transport, Req, []) ->
-dialyzer({no_opaque, handle/2}).
handle(Req, State) ->
Req0 = cowboy_req:set_resp_header(<<"access-control-allow-origin">>,
<<"*">>, Req),
case cowboy_req:qs_val(<<"q">>, Req0) of
case cowboy_req:qs_val(<<"q">>, Req) of
{undefined, Req1} ->
F = fun (Socket, Transport) ->
File = code:priv_dir(dalmatiner_frontend) ++
......
......@@ -11,9 +11,7 @@ init(_Transport, Req, []) ->
-dialyzer({no_opaque, handle/2}).
handle(Req, State) ->
Req0 = cowboy_req:set_resp_header(
<<"access-control-allow-origin">>, <<"*">>, Req),
{[Bucket | Path], Req1} = cowboy_req:path_info(Req0),
{[Bucket | Path], Req1} = cowboy_req:path_info(Req),
{ContentType, Req2} = dalmatiner_idx_handler:content_type(Req1),
case ContentType of
html ->
......
......@@ -11,10 +11,7 @@ init(_Transport, Req, []) ->
-dialyzer({no_opaque, handle/2}).
handle(Req, State) ->
Req0 = cowboy_req:set_resp_header(
<<"access-control-allow-origin">>, <<"*">>, Req),
{ContentType, Req1} = dalmatiner_idx_handler:content_type(Req0),
{ContentType, Req1} = dalmatiner_idx_handler:content_type(Req),
case ContentType of
html ->
F = fun (Socket, Transport) ->
......
......@@ -11,10 +11,7 @@ init(_Transport, Req, []) ->
-dialyzer({no_opaque, handle/2}).
handle(Req, State) ->
Req0 = cowboy_req:set_resp_header(
<<"access-control-allow-origin">>, <<"*">>, Req),
{ContentType, Req1} = dalmatiner_idx_handler:content_type(Req0),
{ContentType, Req1} = dalmatiner_idx_handler:content_type(Req),
case ContentType of
html ->
F = fun (Socket, Transport) ->
......
......@@ -11,10 +11,7 @@ init(_Transport, Req, []) ->
-dialyzer({no_opaque, handle/2}).
handle(Req, State) ->
Req0 = cowboy_req:set_resp_header(
<<"access-control-allow-origin">>, <<"*">>, Req),
{ContentType, Req1} = dalmatiner_idx_handler:content_type(Req0),
{ContentType, Req1} = dalmatiner_idx_handler:content_type(Req),
case ContentType of
html ->
F = fun (Socket, Transport) ->
......
......@@ -11,10 +11,7 @@ init(_Transport, Req, []) ->
-dialyzer({no_opaque, handle/2}).
handle(Req, State) ->
Req0 = cowboy_req:set_resp_header(
<<"access-control-allow-origin">>, <<"*">>, Req),
{ContentType, Req1} = dalmatiner_idx_handler:content_type(Req0),
{ContentType, Req1} = dalmatiner_idx_handler:content_type(Req),
case ContentType of
html ->
F = fun (Socket, Transport) ->
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment