Skip to content

Commit

Permalink
Access-Control-Allow-Originheader/OPTIONS request
Browse files Browse the repository at this point in the history
  • Loading branch information
5HT committed Aug 4, 2013
1 parent cd80d44 commit 0ae211f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
20 changes: 19 additions & 1 deletion src/bullet_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,32 @@ init(Transport, Req, Opts, <<"POST">>) ->
{shutdown, Req2, HandlerState} ->
{shutdown, Req2, State#state{handler_state=HandlerState}}
end;
init(Transport, Req, Opts, <<"OPTIONS">>) ->
{handler, Handler} = lists:keyfind(handler, 1, Opts),
State = #state{handler=Handler},
case Handler:init(Transport, Req, Opts, once) of
{ok, Req2, HandlerState} ->
{ok, cowboy_req:compact(Req2), State#state{handler_state=HandlerState}};
{shutdown, Req2, HandlerState} ->
{shutdown, Req2, State#state{handler_state=HandlerState}}
end;

init(_Transport, Req, _Opts, _Method) ->
{ok, Req2} = cowboy_req:reply(405, [], [], Req),
{shutdown, Req2, undefined}.

handle(Req, State) ->
{Method, Req2} = cowboy_req:method(Req),
handle(Req2, State, Method).

handle(Req, State, <<"OPTIONS">>)->
Headers = [
{<<"Content-Type">>, <<"text/html; charset=utf-8">>},
{<<"Access-Control-Allow-Origin">>, <<"*">>},
{<<"Access-Control-Allow-Headers">>, <<"X-Socket-Transport, Content-Type, x-requested-with, Accept">>},
{<<"Access-Control-Allow-Methods">>, <<"GET, POST, PUT, DELETE, OPTIONS">>}
],
{ok, Req1} = cowboy_req:reply(200, Headers, [], cowboy_req:compact(Req)),
{ok, Req1, State};
handle(Req, State=#state{handler=Handler, handler_state=HandlerState},
<<"POST">>) ->
case cowboy_req:body(Req) of
Expand Down
11 changes: 6 additions & 5 deletions src/n2o_bullet.erl
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ init(_Transport, Req, _Opts, _Active) ->
NewCtx = wf_core:fold(init,Ctx#context.handlers,Ctx),
wf_context:context(NewCtx),
put(page_module,NewCtx#context.module),
{ok, NewCtx#context.req, NewCtx}.
Req1 = wf:header(<<"Access-Control-Allow-Origin">>, <<"*">>, NewCtx#context.req),
{ok, Req1, NewCtx}.

stream(<<"ping">>, Req, State) ->
io:format("ping received~n"),
{reply, <<"pong">>, Req, State};
stream({text,Data}, Req, State) ->
error_logger:info_msg("Text Received ~p",[Data]),
self() ! Data,
stream({text,Data}, Req, State) ->
error_logger:info_msg("Text Received ~p",[Data]),
self() ! Data,
{ok, Req,State};
stream({binary,Info}, Req, State) ->
% error_logger:info_msg("Binary Received: ~p",[Info]),
Expand Down Expand Up @@ -49,7 +50,7 @@ stream({binary,Info}, Req, State) ->
wf_context:clear_actions(),

{reply,[Render,RenderGenActions], Req, State};
stream(Data, Req, State) ->
stream(Data, Req, State) ->
error_logger:info_msg("Data Received ~p",[Data]),
self() ! Data,
{ok, Req,State}.
Expand Down
1 change: 1 addition & 0 deletions src/n2o_cowboy.erl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ params(Req) -> {Params,NewReq} = cowboy_req:qs_vals(Req), Params.
path(Req) -> {Path,NewReq} = cowboy_req:path(Req), Path.
request_body(Req) -> cowboy_req:body(Req).
headers(Req) -> cowboy_req:headers(Req).
header(Name, Value, Req) -> cowboy_req:set_resp_header(Name, Value, Req).
response(Html,Req) -> cowboy_req:set_resp_body(Html,Req).
reply(StatusCode,Req) -> cowboy_req:reply(StatusCode, Req).
cookies(Req) -> {Cookies,Req} = cowboy_req:cookies(Req), Cookies.
Expand Down
1 change: 1 addition & 0 deletions src/wf.erl
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ peer(Req) -> ?BRIDGE:peer(Req).
path(Req) -> ?BRIDGE:path(Req).
request_body(Req) -> ?BRIDGE:request_body(Req).
delete_cookie(Cookie,Req) -> ?BRIDGE:delete_cookie(Cookie,Req).
header(Name, Val, Req) -> ?BRIDGE:header(Name, Val, Req).
response(Html,Req) -> ?BRIDGE:response(Html,Req).
reply(Status,Req) -> ?BRIDGE:reply(Status,Req).

Expand Down

0 comments on commit 0ae211f

Please sign in to comment.