Browse Source

n2o_proto to n4u_proto (cowboy 1.0.1 to 2.9.0)

221V 3 years ago
parent
commit
3cbb5af632

+ 1 - 1
ebin/n4u.app

@@ -2,7 +2,7 @@
   {description, "N4U WebSocket Application Server"},
   {vsn, "4.4.20"},
   {applications, [kernel, stdlib, asn1, public_key, ssl, crypto, ranch, cowboy, fs, active, sh, gproc, nitro]},
-  {modules, [n2o, n4u_app, n4u_sup, n4u_async, n2o_xhr, n4u_cx, n2o_cowboy, n2o_multipart, n2o_static, n2o_stream, n4u_document, n2o_proto, n4u_relay, n4u_error, n4u_io, n4u_log, n4u_mq, n4u_pickle, n4u_query, n4u_secret, n4u_session, n4u_syn, n4u_client, n4u_file, n4u_heart, n4u_http, n4u_nitrogen, n4u_text, wf, wf_convert, wf_utils]},
+  {modules, [n2o, n4u_app, n4u_sup, n4u_async, n2o_xhr, n4u_cx, n2o_cowboy, n2o_multipart, n2o_static, n2o_stream, n4u_document, n4u_proto, n4u_relay, n4u_error, n4u_io, n4u_log, n4u_mq, n4u_pickle, n4u_query, n4u_secret, n4u_session, n4u_syn, n4u_client, n4u_file, n4u_heart, n4u_http, n4u_nitrogen, n4u_text, wf, wf_convert, wf_utils]},
   {registered, [n4u_sup]},
   {mod, {n4u_app, []}},
   {env, []}

+ 2 - 0
src/endpoints/cowboy/n2o_cowboy.erl

@@ -17,8 +17,10 @@ handle(Req, State) ->  {ok, NewReq} = n4u_document:run(Req), {ok, NewReq, State}
 params(Req) -> cowboy_req:qs_vals(Req).
 form(Req) -> {ok,Params,NewReq} = cowboy_req:body_qs(Req), {Params,NewReq}.
 path(Req) -> {Path,_NewReq} = cowboy_req:path(Req), Path.
+get_header(Name, Req, Default) -> cowboy_req:header(Name, Req, Default).
 request_body(Req) -> cowboy_req:body(Req).
 headers(Req) -> cowboy_req:headers(Req).
+set_header(Name, Value, Req) -> cowboy_req:set_resp_header(Name, Value, 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).

+ 8 - 8
src/endpoints/cowboy/n2o_stream.erl

@@ -14,11 +14,11 @@ upgrade({undefined,R},{T,R,O})     -> initialize(T,R,O);
 upgrade({B,R},_) when is_binary(B) -> websocket(R,cowboy_bstr:to_lower(B)).
 
 handle(R,S)                  -> body(cowboy_req:body(R),S).
-info(M,R,S)                  -> xhr(n2o_proto:info(M,R,S)).
-terminate(_,R,S)             -> n2o_proto:terminate(R,S).
-initialize(T,R,O)            -> xhr(n2o_proto:init(T,R,[{formatter,json}|O],xhr)).
+info(M,R,S)                  -> xhr(n4u_proto:info(M,R,S)).
+terminate(_,R,S)             -> n4u_proto:terminate(R,S).
+initialize(T,R,O)            -> xhr(n4u_proto:init(T,R,[{formatter,json}|O],xhr)).
 
-body({ok,D,R2},S)            -> xhr(n2o_proto:stream({type(D),D},R2,S));
+body({ok,D,R2},S)            -> xhr(n4u_proto:stream({type(D),D},R2,S));
 body(R,S)                    -> {ok,R,S}.
 down(R)                      -> {shutdown,R,undefined}.
 reply(D,R,Code)              -> {ok,R2}=cowboy_req:reply(Code,[],D,R), R2.
@@ -33,11 +33,11 @@ xhr({reply,D,R,S})           -> {ok,reply(D,R,200),S}.
 
 % WebSocket
 
-websocket_info(I,R,S)        -> ws(n2o_proto:info(I,R,S)).
-websocket_handle(D,R,S)      -> ws(n2o_proto:stream(D,R,S)).
+websocket_info(I,R,S)        -> ws(n4u_proto:info(I,R,S)).
+websocket_handle(D,R,S)      -> ws(n4u_proto:stream(D,R,S)).
 %websocket_handle(_,R,S)      -> {ok,R,S,hibernate}.
-websocket_init(T,R,O)        -> ws(n2o_proto:init(T,R,[{formatter,bert}|O],ws)).
-websocket_terminate(_,R,S)   -> n2o_proto:terminate(R,S).
+websocket_init(T,R,O)        -> ws(n4u_proto:init(T,R,[{formatter,bert}|O],ws)).
+websocket_terminate(_,R,S)   -> n4u_proto:terminate(R,S).
 
 ws({ok,R,S})                 -> {ok,R,S,hibernate};
 ws({shutdown,R,S})           -> {shutdown,R,S};

+ 0 - 46
src/endpoints/n2o_proto.erl

@@ -1,46 +0,0 @@
--module(n2o_proto).
--description('N4U Protocol WebSocket endpoint handler').
--include_lib("n4u/include/n4u.hrl").
--compile([export_all, nowarn_export_all]).
-
-formatter(O)-> case lists:keyfind(formatter,1,O) of {formatter,F} -> F; X -> X end.
-upack(D)    -> binary_to_term(D,[safe]).
-protocols() -> wf:config(n2o,protocols,[ n4u_heart,
-                                         n4u_nitrogen,
-                                         n4u_file,
-                                         n4u_client,
-                                         n4u_http ]).
-
-terminate(_,#cx{module=Module}) -> catch Module:event(terminate).
-init(_Transport, Req, _Opts, _) ->
-    wf:actions([]),
-    Ctx = (wf:init_context(Req))#cx{formatter=formatter(_Opts)},
-    NewCtx = wf:fold(init,Ctx#cx.handlers,Ctx),
-    wf:context(NewCtx),
-    wf:reg(broadcast,{wf:peer(Req)}),
-    {Origin, _} = cowboy_req:header(<<"origin">>, Req, <<"*">>),
-    ConfigOrigin = wf:to_binary(wf:config(n2o,origin,Origin)),
-    wf:info(?MODULE,"Origin: ~p",[ConfigOrigin]),
-    Req1 = wf:header(<<"Access-Control-Allow-Origin">>, ConfigOrigin, NewCtx#cx.req),
-    {ok, Req1, NewCtx}.
-
-% N4U top level protocol NOP REPLY PUSH
-
-info(M,R,S)               -> filter(M,R,S,protocols(),[]).
-stream({text,_}=M,R,S)    -> filter(M,R,S,protocols(),[]);
-stream({binary,<<>>},R,S) -> nop(R,S);
-stream({binary,D},R,S)    -> filter(upack(D),R,S,protocols(),[]);
-stream(_,R,S)             -> nop(R,S).
-
-filter(M,R,S,Protos,Acc)  -> case application:get_env(n4u, filter, {?MODULE, push}) of
-                                  undefined -> push(M,R,S,Protos,Acc);
-                                  {Mod,Fun} -> Mod:Fun(M,R,S,Protos,Acc) end.
-
-nop(R,S)                  -> {reply,<<>>,R,S}.
-reply(M,R,S)              -> {reply,M,R,S}.
-push(_,R,S,[],_)          -> nop(R,S);
-push(M,R,S,[H|T],Acc)     ->
-    case H:info(M,R,S) of
-         {unknown,_,_,_}  -> push(M,R,S,T,Acc);
-         {reply,M1,R1,S1} -> reply(M1,R1,S1);
-                        A -> push(M,R,S,T,[A|Acc]) end.

+ 85 - 0
src/endpoints/n4u_proto.erl

@@ -0,0 +1,85 @@
+-module(n4u_proto).
+-include_lib("n4u/include/n4u.hrl").
+
+-export([init/4, terminate/2]).
+-export([info/3, stream/3, nop/2, reply/3, push/5]).
+-export([protocols/0]).
+
+
+% N4U Protocol WebSocket endpoint handler
+
+formatter(O)->
+  case lists:keyfind(formatter, 1, O) of
+    {formatter, F} -> F;
+    X -> X
+  end.
+
+
+upack(D) -> erlang:binary_to_term(D, [safe]).
+
+
+protocols() -> application:get_env(n4u, protocols, [ n4u_heart,
+                                                     n4u_nitrogen,
+                                                     n4u_file,
+                                                     n4u_client,
+                                                     n4u_http ]).
+
+
+terminate(_R, #cx{module = Module}) ->
+  catch Module:event(terminate);
+
+%terminate(_R, []) -> ok;
+terminate(R, C) ->
+  wf:info(?MODULE, "terminate: ~p ~p~n", [R, C]),
+  ok.
+
+
+init(_Transport, Req, Opts, _N) ->
+  erlang:put(actions, []),
+  Ctx = (n4u_cx:init_context(Req))#cx{formatter = formatter(Opts)},
+  %wf:info(?MODULE, "init: ~p~n ~p~n ~p~n ~p~n ~p~n", [_Transport, Req, Opts, _N, Ctx]),
+  NewCtx = n4u_cx:fold(init, Ctx#cx.handlers, Ctx),
+  erlang:put(context, NewCtx),
+  wf:reg(broadcast, {wf:peer(Req)}),
+  Origin = wf:get_header(<<"origin">>, Req, <<"*">>),
+  Config_Origin = nitro:to_binary(application:get_env(n4u, origin, Origin)),
+  wf:info(?MODULE, "Origin: ~p", [Config_Origin]),
+  Req1 = wf:set_header(<<"Access-Control-Allow-Origin">>, Config_Origin, NewCtx#cx.req),
+  {ok, Req1, NewCtx}.
+
+
+
+%% N4U top level protocol NOP REPLY PUSH
+
+%info(M, R, S)                -> filter(M, R, S, protocols(), []).
+%stream({text, _} = M, R, S)  -> filter(M, R, S, protocols(), []);
+%stream({binary, <<>>}, R, S) -> nop(R, S);
+%stream({binary, D}, R, S)    -> filter(upack(D), R, S, protocols(), []);
+%stream(_, R, S)              -> nop(R, S).
+%
+%filter(M, R, S, Protos, Acc) ->
+%  case application:get_env(n4u, filter, {?MODULE, push}) of
+%    undefined -> push(M, R, S, Protos, Acc);
+%    {Mod, Fun} -> Mod:Fun(M, R, S, Protos, Acc)
+%  end.
+
+
+info(M, R, S)                -> push(M, R, S, protocols(), []).
+stream({text, _} = M, R, S)  -> push(M, R, S, protocols(), []);
+stream({binary, <<>>}, R, S) -> nop(R, S);
+stream({binary, D}, R, S)    -> push(upack(D), R, S, protocols(), []);
+stream(_, R, S)              -> nop(R, S).
+
+
+nop(R, S)                 -> {reply, <<>>, R, S}.
+reply(M, R, S)            -> {reply, M, R, S}.
+push(_, R, S, [], _)      -> nop(R, S);
+push(M, R, S, [H|T], Acc) ->
+  %io:format("n4u_proto:::push ~p~n~p~n~p~n~p~n", [H, M, R, S]),
+  %io:format("n4u_proto:::push ~p~n~p~n", [H, M]),
+  case H:info(M, R, S) of
+    {unknown, _, _, _}  -> push(M, R, S, T, Acc);
+    {reply, M1, R1, S1} -> reply(M1, R1, S1);
+                      A -> push(M, R, S, T, [A|Acc])
+  end.
+

+ 1 - 1
src/protocols/n4u_heart.erl

@@ -9,7 +9,7 @@ info({text, <<"PING">> = _Ping} = Message, Req, State) ->
 
 info({text, <<"N4U,", Process/binary>> = _InitMarker} = Message, Req, State) ->
   wf:info(?MODULE, "N4U INIT: ~p", [Message]),
-  n2o_proto:push({init, Process}, Req, State, n2o_proto:protocols(), []);
+  n4u_proto:push({init, Process}, Req, State, n4u_proto:protocols(), []);
 
 
 % ETS access protocol

+ 3 - 1
src/wf.erl

@@ -157,7 +157,7 @@ cookie(Name,Value,Path,TTL) -> n4u_cx:add_cookie(Name,Value,Path,TTL).
 % Bridge Information
 
 -ifndef(BRIDGE).
--define(BRIDGE, (wf:config(n2o,bridge,n2o_cowboy))).
+-define(BRIDGE, (application:get_env(n4u, bridge, n2o_cowboy))).
 -endif.
 
 cookie_req(Cookie,Req) -> ?BRIDGE:cookie(Cookie, Req).
@@ -166,10 +166,12 @@ params(Req) -> ?BRIDGE:params(Req).
 form(Req) -> ?BRIDGE:form(Req).
 cookies_req(Req) -> ?BRIDGE:cookies(Req).
 headers(Req) -> ?BRIDGE:headers(Req).
+get_header(Name, Req, Default) -> ?BRIDGE:get_header(Name, Req, Default).
 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).
+set_header(Name, Val, Req) -> ?BRIDGE:set_header(Name, Val, Req).
 header(Name,Val,Req) -> ?BRIDGE:header(Name, Val, Req).
 response(Html,Req) -> ?BRIDGE:response(Html,Req).
 reply(Status,Req) -> ?BRIDGE:reply(Status,Req).