Skip to content

Commit 2766e0a

Browse files
committed
fix:
- otimização de RALParams para buscar o separador mais rápido - ajuste no webmodule para rota genérica - ajuste no ThreadSafe - ajuste no RALToken pra evitar o warning de "result might not be defined" feat: - nova rota de módulo genérica que aceita métodos independentes - novo método SetBody para request e response para substituir o body totalmente com a função.
1 parent 161d01b commit 2766e0a

7 files changed

Lines changed: 97 additions & 61 deletions

File tree

src/base/RALParams.pas

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,9 @@ procedure TRALParams.AppendParams(ASource: TStrings; AKind: TRALParamKind);
644644
vInt: Integer;
645645
vSeparator: StringRAL;
646646
begin
647-
if ASource.Count > 0 then
647+
if ASource.NameValueSeparator <> '' then
648+
vSeparator := ASource.NameValueSeparator
649+
else if ASource.Count > 0 then
648650
vSeparator := FindHeaderNameSeparator(ASource.Strings[0]);
649651

650652
for vInt := 0 to Pred(ASource.Count) do
@@ -1206,8 +1208,10 @@ function TRALParams.FindHeaderNameSeparator(const ASource: StringRAL): StringRAL
12061208
Engine := Self.GetParam('RALEngine').AsString;
12071209
if SameText(Engine, ENGINESYNOPSE) then
12081210
Result := ': '
1209-
else if SameText(Engine, ENGINEINDY) or SameText(Engine, ENGINEFPHTTP)
1210-
or SameText(Engine, ENGINESAGUI) or SameText(Engine, ENGINENETHTTP) then
1211+
else if SameText(Engine, ENGINEFPHTTP) then
1212+
Result := ':'
1213+
else if SameText(Engine, ENGINEINDY) or SameText(Engine, ENGINESAGUI)
1214+
or SameText(Engine, ENGINENETHTTP) then
12111215
Result := '='
12121216
else
12131217
begin

src/base/RALServer.pas

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ TRALServer = class(TRALComponent)
233233
function CreateRoute(const ARoute: StringRAL; AReplyProc: TRALOnReplyGen;
234234
const ADescription: StringRAL = ''): TRALRoute; overload;
235235
{ Core procedure of the server, every request will pass through here to be
236-
processed into response that will be answered to the client}
236+
processed into response that will be answered to the client }
237237
procedure ProcessCommands(ARequest: TRALRequest; AResponse: TRALResponse);
238238
// Validate requests headers before ProcessCommands
239239
procedure ValidateRequest(ARequest: TRALRequest; AResponse: TRALResponse);
@@ -309,7 +309,9 @@ TRALModuleRoutes = class(TRALComponent)
309309
destructor Destroy; override;
310310
// Shortcut to create route on the server, similar to RALServer's CreateRoute
311311
function CreateRoute(const ARoute: StringRAL; AReplyProc: TRALOnReply;
312-
const ADescription: StringRAL = ''): TRALRoute;
312+
const ADescription: StringRAL = ''): TRALRoute; overload;
313+
function CreateRoute(const ARoute: StringRAL; AReplyProc: TRALOnReplyGen;
314+
const ADescription: StringRAL = ''): TRALRoute; overload;
313315
// Inherited method of RALServer
314316
function CanAnswerRoute(ARequest: TRALRequest; AResponse: TRALResponse): TRALRoute; virtual;
315317
// Inherited method of RALServer
@@ -884,6 +886,15 @@ function TRALModuleRoutes.CreateRoute(const ARoute: StringRAL; AReplyProc: TRALO
884886
Result.Description.Text := ADescription;
885887
end;
886888

889+
function TRALModuleRoutes.CreateRoute(const ARoute: StringRAL;
890+
AReplyProc: TRALOnReplyGen; const ADescription: StringRAL): TRALRoute;
891+
begin
892+
Result := TRALRoute(FRoutes.Add);
893+
Result.Route := ARoute;
894+
Result.OnReplyGen := AReplyProc;
895+
Result.Description.Text := ADescription;
896+
end;
897+
887898
function TRALModuleRoutes.CanAnswerRoute(ARequest: TRALRequest; AResponse: TRALResponse): TRALRoute;
888899
begin
889900
Result := Routes.CanAnswerRoute(ARequest);

src/base/RALWebModule.pas

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ function TRALWebModule.CanAnswerRoute(ARequest: TRALRequest; AResponse: TRALResp
144144

145145
if (Result = nil) and (GetFileRoute(ARequest) <> '') then
146146
Result := FDefaultRoute
147-
else if (Result <> nil) and (not Assigned(Result.OnReply)) then
147+
else if (Result <> nil) and (not Assigned(Result.OnReply) and not Assigned(Result.OnReplyGen)) then
148148
Result.OnReply := {$IFDEF FPC}@{$ENDIF}WebModFile;
149149

150150
//if Result <> nil then begin

src/utils/RALCompressZLib.pas

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ TRALCompressZLib = class(TRALCompress)
2929
implementation
3030

3131
const
32-
GZipHeader: array [0 .. 9] of byte = ($1F, $8B, $08, $00, $00, $00, $00, $00, $00, $00);
32+
GZipHeader: array [0 .. 9] of byte = ($1F, $8B, $08, $00, $00, $00, $00, $00, $04, $FF);
33+
// 1F8B08 = gzip signature | 04 = fastest | FF = source is stream | 00 = fill the gaps
3334

3435
{ TRALCompressZLib }
3536

@@ -116,8 +117,8 @@ procedure TRALCompressZLib.InitDeCompress(AInStream, AOutStream: TStream);
116117
AInStream.Read(vCRCFile, SizeOf(vCRCFile));
117118
AInStream.Read(vFileSize, SizeOf(vFileSize));
118119

119-
AInStream.Size := AInStream.Size - (2 * SizeOf(LongWord));
120-
AInStream.Position := Length(GZipHeader);
120+
AInStream.Size := AInStream.Size - (2 * SizeOf(LongWord));
121+
AInStream.Position := Length(GZipHeader);
121122
end;
122123
{$ELSE}
123124
AInStream.Position := 0;

src/utils/RALCustomObjects.pas

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ TRALHTTPHeaderInfo = class
7272
function HasValidContentEncoding: boolean;
7373
/// Grabs the param either on request or response by its name
7474
function ParamByName(const AParamName: StringRAL): TRALParam;
75+
/// fills the body with the String AContent
76+
procedure SetBody(AContent: StringRAL); overload; virtual;
77+
/// fills the body with the Stream AContent
78+
procedure SetBody(AContent: TStream); overload; virtual;
7579
published
7680
property AcceptCompress: TRALCompressType read GetAcceptCompress;
7781
property AcceptCripto: TRALCriptoType read GetAcceptCripto;
@@ -343,6 +347,18 @@ function TRALHTTPHeaderInfo.ParamByName(const AParamName: StringRAL): TRALParam;
343347
Result := FParams.Get[AParamName];
344348
end;
345349

350+
procedure TRALHTTPHeaderInfo.SetBody(AContent: StringRAL);
351+
begin
352+
Params.ClearParams;
353+
Params.AddValue(AContent, rpkBODY);
354+
end;
355+
356+
procedure TRALHTTPHeaderInfo.SetBody(AContent: TStream);
357+
begin
358+
Params.ClearParams;
359+
Params.AddValue(AContent, rpkBODY);
360+
end;
361+
346362
procedure TRALHTTPHeaderInfo.SetContentCompress(const AValue: TRALCompressType);
347363
begin
348364
FContentEncoding := TRALCompress.CompressToString(AValue);

src/utils/RALThreadSafe.pas

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ function TRALStringListSafe.Exists(const AItem: StringRAL): boolean;
197197
Result := false;
198198
Lock;
199199
try
200-
Result := FValue.IndexOf(AItem) > 0;
200+
Result := FValue.IndexOf(AItem) >= 0;
201201
finally
202202
Unlock;
203203
end;

src/utils/RALToken.pas

Lines changed: 55 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -229,65 +229,69 @@ function TRALDigest.GetHeader: TStringList;
229229
vHa1, vHa2, vAux1, vNC: StringRAL;
230230
vHash: TRALHashBase;
231231
begin
232-
case FParams.Algorithm of
233-
tdaMD5: begin
234-
vHash := TRALMD5.Create;
235-
end;
236-
tdaSHA2_256: begin
237-
vHash := TRALSHA2_32.Create;
238-
TRALSHA2_32(vHash).Version := rsv256;
239-
end;
240-
tdaSHA2_512: begin
241-
vHash := TRALSHA2_64.Create;
242-
TRALSHA2_64(vHash).Version := rsv512_256;
232+
Result := TStringList.Create;
233+
try
234+
case FParams.Algorithm of
235+
tdaMD5: begin
236+
vHash := TRALMD5.Create;
237+
end;
238+
tdaSHA2_256: begin
239+
vHash := TRALSHA2_32.Create;
240+
TRALSHA2_32(vHash).Version := rsv256;
241+
end;
242+
tdaSHA2_512: begin
243+
vHash := TRALSHA2_64.Create;
244+
TRALSHA2_64(vHash).Version := rsv512_256;
245+
end;
243246
end;
244-
end;
245247

246-
try
247-
vNC := Format('%.8d', [FParams.NC]);
248-
FParams.CNonce := vHash.HashAsString(vNC);
248+
try
249+
vNC := Format('%.8d', [FParams.NC]);
250+
FParams.CNonce := vHash.HashAsString(vNC);
249251

250-
vHa1 := Format('%s:%s:%s', [FUserName, FParams.Realm, FPassword]);
251-
vHa1 := vHash.HashAsString(vHa1);
252+
vHa1 := Format('%s:%s:%s', [FUserName, FParams.Realm, FPassword]);
253+
vHa1 := vHash.HashAsString(vHa1);
252254

253-
if FParams.SessAlgorithm then
254-
vHa1 := vHash.HashAsString(Format('%s:%s:%s',
255-
[vHa1, FParams.Nonce, FParams.CNonce]));
255+
if FParams.SessAlgorithm then
256+
vHa1 := vHash.HashAsString(Format('%s:%s:%s',
257+
[vHa1, FParams.Nonce, FParams.CNonce]));
256258

257-
if ((Pos('auth', LowerCase(FParams.Qop)) > 0) and
258-
(Pos('auth-int', LowerCase(FParams.Qop)) = 0)) or
259-
(Trim(FParams.Qop) = '') then
260-
begin
261-
vHa2 := Format('%s:%s', [FMethod, FURL]);
262-
vHa2 := vHash.HashAsString(vHa2);
263-
end
264-
else if (Pos('auth-int', LowerCase(FParams.Qop)) > 0) then
265-
begin
266-
vHa2 := vHash.HashAsString(FEntityBody);
267-
vHa2 := Format('%s:%s:%s', [FMethod, FURL, vHa2]);
268-
vHa2 := vHash.HashAsString(vHa2);
259+
if ((Pos('auth', LowerCase(FParams.Qop)) > 0) and
260+
(Pos('auth-int', LowerCase(FParams.Qop)) = 0)) or
261+
(Trim(FParams.Qop) = '') then
262+
begin
263+
vHa2 := Format('%s:%s', [FMethod, FURL]);
264+
vHa2 := vHash.HashAsString(vHa2);
265+
end
266+
else if (Pos('auth-int', LowerCase(FParams.Qop)) > 0) then
267+
begin
268+
vHa2 := vHash.HashAsString(FEntityBody);
269+
vHa2 := Format('%s:%s:%s', [FMethod, FURL, vHa2]);
270+
vHa2 := vHash.HashAsString(vHa2);
271+
end;
272+
273+
if (Pos('auth', LowerCase(FParams.Qop)) > 0) then
274+
vAux1 := Format('%s:%s:%s:%s:%s:%s', [vHa1, FParams.Nonce, vNC,
275+
FParams.CNonce, FParams.Qop, vHa2])
276+
else
277+
vAux1 := Format('%s:%s:%s', [vHa1, FParams.Nonce, vHa2]);
278+
vAux1 := vHash.HashAsString(vAux1);
279+
finally
280+
FreeAndNil(vHash);
269281
end;
270282

271-
if (Pos('auth', LowerCase(FParams.Qop)) > 0) then
272-
vAux1 := Format('%s:%s:%s:%s:%s:%s', [vHa1, FParams.Nonce, vNC,
273-
FParams.CNonce, FParams.Qop, vHa2])
274-
else
275-
vAux1 := Format('%s:%s:%s', [vHa1, FParams.Nonce, vHa2]);
276-
vAux1 := vHash.HashAsString(vAux1);
277-
finally
278-
FreeAndNil(vHash);
283+
Result.Add('realm=' + FParams.Realm);
284+
Result.Add('username=' + FUserName);
285+
Result.Add('nonce=' + FParams.Nonce);
286+
Result.Add('uri=' + FURL);
287+
Result.Add('qop=' + FParams.Qop);
288+
Result.Add('nc=' + vNC); // nc=00000001,
289+
Result.Add('cnonce=' + FParams.CNonce); // cnonce="0a4f113b",
290+
Result.Add('response=' + vAux1);
291+
Result.Add('opaque=' + FParams.Opaque);
292+
except
293+
Result.Free;
279294
end;
280-
281-
Result := TStringList.Create;
282-
Result.Add('realm=' + FParams.Realm);
283-
Result.Add('username=' + FUserName);
284-
Result.Add('nonce=' + FParams.Nonce);
285-
Result.Add('uri=' + FURL);
286-
Result.Add('qop=' + FParams.Qop);
287-
Result.Add('nc=' + vNC); // nc=00000001,
288-
Result.Add('cnonce=' + FParams.CNonce); // cnonce="0a4f113b",
289-
Result.Add('response=' + vAux1);
290-
Result.Add('opaque=' + FParams.Opaque);
291295
end;
292296

293297
constructor TRALDigest.Create;

0 commit comments

Comments
 (0)