|
|
@ -16,7 +16,7 @@ |
|
|
|
]). |
|
|
|
|
|
|
|
%% eNet callback |
|
|
|
-export([newCon/2]). |
|
|
|
-export([newConn/2]). |
|
|
|
|
|
|
|
-export([ |
|
|
|
init_it/2 |
|
|
@ -26,7 +26,7 @@ |
|
|
|
, system_terminate/4 |
|
|
|
]). |
|
|
|
|
|
|
|
newCon(_Sock, WsMod) -> |
|
|
|
newConn(_Sock, WsMod) -> |
|
|
|
?MODULE:start_link(WsMod). |
|
|
|
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% genActor start %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
|
@ -77,8 +77,7 @@ loop(Parent, State) -> |
|
|
|
{ok, NewState} -> |
|
|
|
loop(Parent, NewState); |
|
|
|
{stop, Reason} -> |
|
|
|
terminate(Reason, State), |
|
|
|
exit(Reason) |
|
|
|
terminate(Reason, State) |
|
|
|
end |
|
|
|
end. |
|
|
|
|
|
|
@ -89,6 +88,7 @@ init(WsMod) -> |
|
|
|
{ok, #wsState{wsMod = WsMod}}. |
|
|
|
|
|
|
|
handleMsg({tcp, _Socket, Data}, State) -> |
|
|
|
?wsWarn("IMY***************tcp data ~p ~n XXXXXXXXXXXX ~p ~n", [Data, State]), |
|
|
|
#wsState{stage = Stage, buffer = Buffer, socket = Socket} = State, |
|
|
|
case wsHttpProtocol:request(Stage, <<Buffer/binary, Data/binary>>, State) of |
|
|
|
{ok, _NewState} = LRet -> |
|
|
@ -98,17 +98,22 @@ handleMsg({tcp, _Socket, Data}, State) -> |
|
|
|
#wsState{buffer = Buffer, socket = Socket, temHeader = TemHeader, method = Method} = NewState, |
|
|
|
case doResponse(Response, Socket, TemHeader, Method) of |
|
|
|
keep_alive -> |
|
|
|
handleMsg({tcp, _Socket, Buffer}, newWsState(NewState)); |
|
|
|
case Buffer of |
|
|
|
<<>> -> |
|
|
|
{ok, newWsState(NewState)}; |
|
|
|
_ -> |
|
|
|
handleMsg({tcp, Socket, Buffer}, newWsState(NewState)) |
|
|
|
end; |
|
|
|
close -> |
|
|
|
{stop, close} |
|
|
|
{stop, normal} |
|
|
|
end; |
|
|
|
Err -> |
|
|
|
?wsErr("recv the http data error ~p~n", [Err]), |
|
|
|
sendBadRequest(Socket), |
|
|
|
{stop, close} |
|
|
|
{stop, Err} |
|
|
|
end; |
|
|
|
handleMsg({tcp_closed, _Socket}, _State) -> |
|
|
|
{stop, tcp_closed}; |
|
|
|
{stop, normal}; |
|
|
|
handleMsg({tcp_error, _Socket, Reason}, _State) -> |
|
|
|
?wsErr("the http tcp socket error ~p~n", [Reason]), |
|
|
|
{stop, tcp_error}; |
|
|
@ -123,23 +128,28 @@ handleMsg({ssl, _Socket, Data}, State) -> |
|
|
|
#wsState{buffer = Buffer, temHeader = TemHeader, method = Method} = NewState, |
|
|
|
case doResponse(Response, Socket, TemHeader, Method) of |
|
|
|
keep_alive -> |
|
|
|
handleMsg({tcp, Socket, Buffer}, newWsState(NewState)); |
|
|
|
case Buffer of |
|
|
|
<<>> -> |
|
|
|
{ok, newWsState(NewState)}; |
|
|
|
_ -> |
|
|
|
handleMsg({ssl, Socket, Buffer}, newWsState(NewState)) |
|
|
|
end; |
|
|
|
close -> |
|
|
|
{stop, close} |
|
|
|
{stop, normal} |
|
|
|
end; |
|
|
|
Err -> |
|
|
|
?wsErr("recv the http data error ~p~n", [Err]), |
|
|
|
sendBadRequest(Socket), |
|
|
|
{stop, close} |
|
|
|
{stop, Err} |
|
|
|
end; |
|
|
|
handleMsg({ssl_closed, _Socket}, _State) -> |
|
|
|
{stop, ssl_closed}; |
|
|
|
{stop, normal}; |
|
|
|
handleMsg({ssl_error, _Socket, Reason}, _State) -> |
|
|
|
?wsErr("the http ssl socket error ~p~n", [Reason]), |
|
|
|
{stop, ssl_error}; |
|
|
|
handleMsg({?mSockReady, Sock}, _State) -> |
|
|
|
handleMsg({?mSockReady, Sock}, State) -> |
|
|
|
inet:setopts(Sock, [{packet, raw}, {active, true}]), |
|
|
|
{ok, #wsState{socket = Sock}}; |
|
|
|
{ok, State#wsState{socket = Sock}}; |
|
|
|
handleMsg({?mSockReady, Sock, SslOpts, SslHSTet}, State) -> |
|
|
|
case wsSslAcceptor:handshake(Sock, SslOpts, SslHSTet) of |
|
|
|
{ok, SslSock} -> |
|
|
@ -153,9 +163,9 @@ handleMsg(_Msg, _State) -> |
|
|
|
?wsErr("~p info receive unexpect msg ~p ~n ", [?MODULE, _Msg]), |
|
|
|
kpS. |
|
|
|
|
|
|
|
terminate(_Reason, #wsState{socket = Socket} = _State) -> |
|
|
|
terminate(Reason, #wsState{socket = Socket} = _State) -> |
|
|
|
wsNet:close(Socket), |
|
|
|
ok. |
|
|
|
exit(Reason). |
|
|
|
|
|
|
|
newWsState(WsState) -> |
|
|
|
WsState#wsState{ |
|
|
@ -193,19 +203,19 @@ doHandle(State) -> |
|
|
|
{HttpCode, Body} -> {response, HttpCode, [], Body}; |
|
|
|
%% Unexpected |
|
|
|
Unexpected -> |
|
|
|
?wsErr("handle return error ~p ~p~n", [WsReq, Unexpected]), |
|
|
|
?wsErr("handle return error WsReq:~p Ret:~p~n", [WsReq, Unexpected]), |
|
|
|
{response, 500, [], <<"Internal server error">>} |
|
|
|
catch |
|
|
|
throw:{ResponseCode, Headers, Body} when is_integer(ResponseCode) -> |
|
|
|
{response, ResponseCode, Headers, Body}; |
|
|
|
throw:Exc:Stacktrace -> |
|
|
|
?wsErr("handle catch throw ~p ~p ~p~n", [WsReq, Exc, Stacktrace]), |
|
|
|
?wsErr("handle catch throw WsReq:~p R:~p S:~p~n", [WsReq, Exc, Stacktrace]), |
|
|
|
{response, 500, [], <<"Internal server error">>}; |
|
|
|
error:Error:Stacktrace -> |
|
|
|
?wsErr("handle catch error ~p ~p ~p~n", [WsReq, Error, Stacktrace]), |
|
|
|
?wsErr("handle catch error WsReq:~p R:~p S:~p~n", [WsReq, Error, Stacktrace]), |
|
|
|
{response, 500, [], <<"Internal server error">>}; |
|
|
|
exit:Exit:Stacktrace -> |
|
|
|
?wsErr("handle catch exit ~p ~p ~p~n", [WsReq, Exit, Stacktrace]), |
|
|
|
?wsErr("handle catch exit WsReq:~p R:~p S:~p~n", [WsReq, Exit, Stacktrace]), |
|
|
|
{response, 500, [], <<"Internal server error">>} |
|
|
|
end. |
|
|
|
|
|
|
@ -244,8 +254,8 @@ doResponse({file, ResponseCode, UserHeaders, Filename, Range}, Socket, TemHeader |
|
|
|
case Ret of |
|
|
|
ok -> |
|
|
|
closeOrKeepAlive(UserHeaders, TemHeader); |
|
|
|
{error, Reason} -> |
|
|
|
{stop, Reason} |
|
|
|
_Err -> |
|
|
|
{stop, Ret} |
|
|
|
end |
|
|
|
end. |
|
|
|
|
|
|
@ -443,7 +453,7 @@ connection(UserHeaders, ReqHeader) -> |
|
|
|
end. |
|
|
|
|
|
|
|
contentLength(Headers, Body) -> |
|
|
|
case lists:keyfind(?CONTENT_LENGTH_HEADER, Headers) of |
|
|
|
case lists:keyfind(?CONTENT_LENGTH_HEADER, 1, Headers) of |
|
|
|
false -> |
|
|
|
{?CONTENT_LENGTH_HEADER, iolist_size(Body)}; |
|
|
|
_ -> |
|
|
@ -451,7 +461,7 @@ contentLength(Headers, Body) -> |
|
|
|
end. |
|
|
|
|
|
|
|
transferEncoding(Headers) -> |
|
|
|
case lists:keyfind(?TRANSFER_ENCODING_HEADER, Headers) of |
|
|
|
case lists:keyfind(?TRANSFER_ENCODING_HEADER, 1, Headers) of |
|
|
|
false -> |
|
|
|
{?TRANSFER_ENCODING_HEADER, <<"chunked">>}; |
|
|
|
_ -> |
|
|
|