Skip to content
Permalink
Browse files

refactoring of THttpServerSocket.GetRequest for better error handling…

…, and optional execution context extraction, e.g. to allow asynchronous WebSockets process
  • Loading branch information
Arnaud Bouchez
Arnaud Bouchez committed Jan 16, 2020
1 parent 6cfb082 commit c8e8df99814a4209a79e18637403d11a1e159be1
Showing with 36 additions and 7 deletions.
  1. +33 −5 SynBidirSock.pas
  2. +2 −1 SynProtoRTSPHTTP.pas
  3. +1 −1 SynopseCommit.inc
@@ -969,6 +969,14 @@ TWebCrtSocketProcess = class(TWebSocketProcess)

{ -------------- WebSockets Server classes for bidirectional remote access }

type
TWebSocketServerSocket = class(THttpServerSocket)
public
/// overriden to detect upgrade: websocket header and return grOwned
function GetRequest(withBody: boolean;
headerMaxTix: Int64): THttpServerSocketGetRequestResult; override;
end;

type
{$M+}
TWebSocketServerResp = class;
@@ -3003,12 +3011,16 @@ function HttpServerWebSocketUpgrade(ClientSock: THttpServerSocket;
constructor TWebSocketServer.Create(const aPort: SockString; OnStart,OnStop: TNotifyThreadEvent;
const ProcessName: SockString; ServerThreadPoolCount, KeepAliveTimeOut: integer);
begin
// override with custom processing classes
fSocketClass := TWebSocketServerSocket;
fThreadRespClass := TWebSocketServerResp;
// initialize protocols and connections
fWebSocketConnections := TObjectListLocked.Create(false);
fProtocols := TWebSocketProtocolList.Create;
fSettings.SetDefaults;
fSettings.HeartbeatDelay := 20000;
fCanNotifyCallback := true;
// start the server
inherited Create(aPort,OnStart,OnStop,ProcessName,ServerThreadPoolCount,KeepAliveTimeOut);
end;

@@ -3038,10 +3050,11 @@ procedure TWebSocketServer.Process(ClientSock: THttpServerSocket;
begin
if ClientSock.ConnectionUpgrade and ClientSock.KeepAliveClient and
IdemPropNameU('GET',ClientSock.Method) and
IdemPropNameU(ClientSock.Upgrade,'websocket') and
ConnectionThread.InheritsFrom(TWebSocketServerResp) then begin
err := WebSocketProcessUpgrade(ClientSock,TWebSocketServerResp(ConnectionThread));
if err<>STATUS_SUCCESS then
WebSocketLog.Add.Log(sllTrace,'Process: WebSocketProcessUpgrade failure %',[err],self);
WebSocketLog.Add.Log(sllTrace,'Process: WebSocketProcessUpgrade failed as %',[err],self);
end else
inherited Process(ClientSock,ConnectionID,ConnectionThread);
end;
@@ -3232,6 +3245,18 @@ function TWebSocketProcessServer.ComputeContext(
end;


{ TWebSocketServerSocket }

function TWebSocketServerSocket.GetRequest(withBody: boolean;
headerMaxTix: Int64): THttpServerSocketGetRequestResult;
begin
result := inherited GetRequest(withBody, headerMaxTix);
if (result=grHeaderReceived) and ConnectionUpgrade and KeepAliveClient and
IdemPropNameU('GET',Method) and IdemPropNameU(Upgrade,'websocket') then
//writeln('!!');
end;


{ -------------- WebSockets Client classes for bidirectional remote access }

{ THttpClientWebSockets }
@@ -3256,8 +3281,10 @@ function THttpClientWebSockets.Request(const url, method: SockString;
block: TWebSocketProcessNotifyCallback;
resthead: RawUTF8;
begin
if fProcess<>nil then
if fProcess.fClientThread.fThreadState>sRun then
if fProcess<>nil then begin
if fProcess.fClientThread.fThreadState=sCreate then
sleep(10); // allow TWebSocketProcessClientThread.Execute warmup
if fProcess.fClientThread.fThreadState<>sRun then
// WebSockets closed by server side
result := STATUS_NOTIMPLEMENTED else begin
// send the REST request over WebSockets
@@ -3279,8 +3306,9 @@ function THttpClientWebSockets.Request(const url, method: SockString;
finally
Ctxt.Free;
end;
end else
// standard HTTP/1.1 REST request
end;
end else
// standard HTTP/1.1 REST request (before WebSocketsUpgrade call)
result := inherited Request(url,method,KeepAlive,header,Data,DataType,retry);
end;

@@ -267,7 +267,8 @@ function TRTSPOverHTTPServer.ConnectionCreate(aSocket: TSocket; const aRemoteIp:
sock := TProxySocket.Create(nil);
try
sock.InitRequest(aSocket,aRemoteIP);
if sock.GetRequest({withBody=}false, {headertix=}0) and (sock.URL <> '') then begin
if (sock.GetRequest({withBody=}false, {headertix=}0)=grHeaderReceived) and
(sock.URL <> '') then begin
if log<>nil then
log.Log(sllTrace, 'ConnectionCreate received % % %', [sock.Method, sock.URL,
sock.HeaderGetText], self);
@@ -1 +1 @@
'1.18.5570'
'1.18.5571'

0 comments on commit c8e8df9

Please sign in to comment.
You can’t perform that action at this time.