rtcsrvmodule.pas
来自「Delphi快速开发Web Server」· PAS 代码 · 共 1,960 行 · 第 1/4 页
PAS
1,960 行
if Response.StatusCode=410 then
begin
Write;
Exit;
end;
end
end;
code:=Read;
if (UpperCase(Request.ContentType)='TEXT/XML') then
begin
if (fmt_XMLRPC in FDataFormats) and
(isXMLString(code)) then // data received as XML-RPC
begin
FMT:=fmt_XMLRPC;
Request.Info.asBoolean['$XML']:=True;
crypt:=nil;
end
else
begin
Response.Status(404,'Data Format not supported.');
Write;
Exit;
end;
end
else if (fmt_RTC in FDataFormats) then
begin
FMT:=fmt_RTC;
crypt:=GetCrypt(Session);
if assigned(crypt) and assigned(crypt.Read) then
begin
CryptRead(crypt, code);
try
crypt.ControlKey:=ExtractFinalControlKey(crypt.ControlCounter);
except
on E:Exception do
begin
Response.Status(409,'Encryption Error.');
Write;
Exit;
end;
end;
if crypt.ControlKey='' then
begin
Response.Status(409,'Encryption Key Error.');
Write;
Exit;
end;
end
else if ForceEncryption and (EncryptionKey>0) then
begin
// 412 = Precondition Failed (Encryption required)
Response.Status(412,'Encryption required.');
Write;
Exit;
end
else if Code[length(code)]=#0 then // compressed
begin
{$IFDEF COMPRESS}
try
code:=ZDecompress_Str(code,length(code)-1);
except
on E:Exception do
begin
Response.Status(409,'Decompression error.');
Write;
Exit;
end;
end;
{$ELSE}
Response.Status(409,'Compression not supported.');
Write;
Exit;
{$ENDIF}
end;
end
else
begin
// 404 = Request not supported.
Response.Status(404,'Data Format not supported.');
Write;
Exit;
end;
output:=nil;
at:=0;
try
if Code='' then
Write
else while at<length(code) do
begin
case FMT of
fmt_XMLRPC: MyData:=TRtcValue.FromXMLrpc(code,at);
else MyData:=TRtcValue.FromCode(code,at);
end;
{$IFDEF COMPRESS}
if (FMT=fmt_RTC) and (FCompress<>cNone) and not assigned(output) and
(at<length(code)) then
output:=TRtcHugeString.Create;
{$ENDIF}
try
if not isSimpleValue(MyData) then
begin
if assigned(FFunctions) then
begin
MyResult:=FFunctions.ExecuteData(Sender, MyData);
if MyData<>MyResult then
begin
MyData.Free;
MyData:=MyResult;
end;
end
else
raise Exception.Create('FunctionGroup property undefined, can not execute call.');
end;
if MyData<>nil then
begin
if assigned(output) then
MyData.to_Code(output)
else
begin
{$IFDEF COMPRESS}
if (FMT=fmt_RTC) and (FCompress<>cNone) then
begin
{ We want to send data compressed, but we haven't created the
"output" object until now, which means that this is the last result,
so we don't need "code" anymore. }
code:=myData.toCode;
if length(code)<RTC_MIN_COMPRESS_SIZE then
begin
CryptWrite(crypt,code);
Write(code);
code:='';
end
else
begin
case FCompress of
cFast: temp := ZCompress_Str(code, zcFastest);
cMax: temp := ZCompress_Str(code, zcMax);
else temp := ZCompress_Str(code, zcDefault);
end;
if length(temp)>=length(code)-1 then
begin
temp:='';
CryptWrite(crypt,code);
Write(code);
code:='';
end
else
begin
code:='';
CryptWrite(crypt,temp);
Write(temp);
temp:=#0;
CryptWrite(crypt,temp);
Write(temp);
temp:='';
end;
end;
end
else
{$ENDIF}
begin
if FMT=fmt_XMLRPC then
Write(MyData.toXMLrpcResponse)
else
begin
if assigned(crypt) and assigned(crypt.Write) then
begin
temp:=MyData.toCode;
CryptWrite(crypt,temp);
Write(temp);
end
else
Write(MyData.toCode);
end;
end;
end;
end
else
Write; // no content.
finally
MyData.Free;
end;
end;
except
on E:EDelayedCall do
begin
// Create and Post the DelayedCall
mycall:=e.call;
mycall.Conn:=Sender;
mycall.output:=output;
mycall.FMT:=FMT;
{$IFDEF COMPRESS}
mycall.Compress:=Compression;
{$ENDIF}
mycall.AutoSessionsLive:=AutoSessionsLive;
if assigned(Session) then
begin
mycall.SessionID:=Session.ID;
mycall.crypt:=crypt;
end;
Info.Obj[RTC_SERVERMODULE_DELAYED_CALL]:=mycall;
if Sender.isExtension then
mycall.Execute
else
mycall.Post(MultiThreaded);
Exit; // not sending the reponse now!
end;
on E:Exception do
with TRtcValue.Create do
try
asException:=E.Message;
if assigned(output) then
to_Code(output)
else
begin
if FMT=fmt_XMLRPC then
Write(toXMLrpcResponse)
else
begin
if assigned(crypt) and assigned(crypt.Write) then
begin
temp:=asCode;
CryptWrite(crypt,temp);
Write(temp);
end
else
Write(asCode);
end;
end;
finally
Free;
end;
end;
{$IFDEF COMPRESS}
if assigned(output) then
begin
{ we have stored uncompressed data in "output",
now we need to compress it all and send it out. }
try
code:=output.Get;
finally
output.Free;
end;
if length(code)<RTC_MIN_COMPRESS_SIZE then
begin
CryptWrite(crypt,code);
Write(code);
code:='';
end
else
begin
case FCompress of
cFast: temp := ZCompress_Str(code, zcFastest);
cMax: temp := ZCompress_Str(code, zcMax);
else temp := ZCompress_Str(code, zcDefault);
end;
if length(temp)>=length(code)-1 then
begin
temp:='';
CryptWrite(crypt,code);
Write(code);
code:='';
end
else
begin
code:='';
CryptWrite(crypt,temp);
Write(temp);
temp:=#0;
CryptWrite(crypt,temp);
Write(temp);
temp:='';
end;
end;
end;
{$ENDIF}
if assigned(crypt) and assigned(crypt.Write) then
begin
// Add control key to the end of our response
code:=crypt.ControlKey;
CryptWrite(crypt, code);
Write(code);
end;
end;
end;
end;
procedure TRtcServerModule.Call_DataOut(Sender: TRtcConnection);
begin
// empty
end;
procedure TRtcServerModule.Call_DataIn(Sender: TRtcConnection);
begin
// empty
end;
procedure TRtcServerModule.Call_DataSent(Sender: TRtcConnection);
begin
// empty
end;
procedure TRtcServerModule.Call_ReadyToSend(Sender: TRtcConnection);
begin
// empty
end;
function TRtcServerModule.GetFunctionGroup: TRtcFunctionGroup;
begin
try
Result:=FFunctions;
if not (Result is TRtcFunctionGroup) then
Result:=nil;
except
Result:=nil;
end;
end;
procedure TRtcServerModule.SetFunctionGroup(const Value: TRtcFunctionGroup);
begin
FFunctions:=Value;
end;
function TRtcServerModule.GetModuleFileName: string;
begin
Result:=FModuleFileName;
end;
procedure TRtcServerModule.SetModuleFileName(const Value: string);
begin
if FModuleFileName<>Value then
begin
FModuleFileName:=Value;
if FModuleFileName<>'' then
begin
// FileName has to start with '/'
if Copy(FModuleFileName,1,1)<>'/' then
FModuleFileName:='/'+FModuleFileName;
end;
end;
end;
function TRtcServerModule.GetModuleHost: string;
begin
Result:=FModuleHost;
end;
procedure TRtcServerModule.SetModuleHost(const Value: string);
begin
if FModuleHost<>Value then
// Convert to uppercase now, so we don't have to do it on every request.
FModuleHost:=UpperCase(Value);
end;
function TRtcServerModule.GetCrypt(Session: TRtcSession): TRtcCryptServer;
begin
if not assigned(Session) then
Result:=nil
else
Result:=TRtcCryptServer(Session.Obj[ModuleHost+ModuleFileName+':$Crypt$']);
end;
procedure TRtcServerModule.NewCrypt(Session: TRtcSession);
begin
if TRtcCryptServer(Session.Obj[ModuleHost+ModuleFileName+':$Crypt$'])<>nil then
TRtcCryptServer(Session.Obj[ModuleHost+ModuleFileName+':$Crypt$']).Init
else
Session.Obj[ModuleHost+ModuleFileName+':$Crypt$']:=TRtcCryptServer.Create;
end;
procedure TRtcServerModule.DelCrypt(Session: TRtcSession);
begin
if assigned(Session) then
if TRtcCryptServer(Session.Obj[ModuleHost+ModuleFileName+':$Crypt$'])<>nil then
begin
TRtcCryptServer(Session.Obj[ModuleHost+ModuleFileName+':$Crypt$']).Free;
Session.Obj[ModuleHost+ModuleFileName+':$Crypt$']:=nil;
end;
end;
procedure TRtcServerModule.SetAutoEncrypt(const Value: integer);
begin
if Value<0 then
raise Exception.Create('Negative values not allowed for EncryptionKey.');
FAutoEncrypt := Value;
if FAutoEncrypt>0 then
FAutoSessions:=True
else
FForceEncrypt:=False;
end;
procedure TRtcServerModule.SetAutoSessions(const Value: boolean);
begin
if not Value and (FAutoEncrypt>0) then
raise Exception.Create('Set EncryptionKey to 0 before setting AutoSessions to False.');
FAutoSessions := Value;
if not FAutoSessions then
begin
FAutoEncrypt:=0;
FForceEncrypt:=False;
end;
end;
procedure TRtcServerModule.SetForceEncrypt(const Value: boolean);
begin
FForceEncrypt := Value;
if FForceEncrypt then
begin
FAutoSessions:=True;
if FAutoEncrypt=0 then
FAutoEncrypt:=16;
end;
end;
{$IFDEF COMPRESS}
procedure TRtcServerModule.SetCompress(const Value: TRtcCompressLevel);
begin
FCompress := Value;
end;
{$ENDIF}
procedure TRtcServerModule.SetAutoSessionsLock(const Value: TRtcSessionLockType);
begin
FAutoSessionsLock := Value;
end;
{ TRtcCryptServer }
destructor TRtcCryptServer.Destroy;
begin
Init;
inherited;
end;
procedure TRtcCryptServer.Init;
begin
HaveHello:=False;
HaveStart:=False;
ControlKey:='';
ClientHello:='';
ServerHello:='';
ClientKey:='';
ServerKey:='';
ControlCounter:=0;
if assigned(Read) then
begin
Read.Free;
Read:=nil;
end;
if assigned(Write) then
begin
Write.Free;
Write:=nil;
end;
end;
procedure TRtcCryptServer.Kill;
begin
Free;
end;
{ TRtcDelayedCall }
type
TRtcDelayedCallJob=class(TRtcJob)
public
Conn:TRtcConnection;
constructor Create(Con: TRtcConnection);
procedure Run(Thr:TRtcThread); override;
procedure Kill; override;
end;
var
List:tBinList;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?