From e41d674985c4e6a562c9b53f4686fcbfb8d2b8e8 Mon Sep 17 00:00:00 2001 From: anderbelluno <36983422+anderbelluno@users.noreply.github.com> Date: Tue, 2 May 2023 17:08:44 -0300 Subject: [PATCH] Ajustes internos --- .../Sockets/Fphttp/uRESTDWFphttpBase.pas | 69 +++++++++++++------ 1 file changed, 48 insertions(+), 21 deletions(-) diff --git a/CORE/Source/Sockets/Fphttp/uRESTDWFphttpBase.pas b/CORE/Source/Sockets/Fphttp/uRESTDWFphttpBase.pas index e3a675dd..5ecf38db 100644 --- a/CORE/Source/Sockets/Fphttp/uRESTDWFphttpBase.pas +++ b/CORE/Source/Sockets/Fphttp/uRESTDWFphttpBase.pas @@ -107,7 +107,7 @@ procedure TRESTDWFphttpServicePooler.ExecRequest(Sender: TObject; Var AResponse : TFPHTTPConnectionResponse); var vContentType, - AuthRealm, + vAuthRealm, sCharSet, ErrorMessage, vResponseString : String; @@ -118,6 +118,7 @@ procedure TRESTDWFphttpServicePooler.ExecRequest(Sender: TObject; CORSCustomHeaders : TStringList; ResultStream, ContentStringStream : TStream; + mb : TStringStream; vRedirect : TRedirect; a : String; @@ -192,11 +193,36 @@ procedure TRESTDWFphttpServicePooler.ExecRequest(Sender: TObject; //end; end; + Procedure DestroyComponents; + Begin + if assigned(vResponseHeader)then + FreeAndNil(vResponseHeader); + if assigned(ResultStream)then + FreeAndNil(ResultStream); + if assigned(CORSCustomHeaders)then + FreeAndNil(CORSCustomHeaders); + if assigned(ContentStringStream)then + FreeAndNil(ContentStringStream); + if assigned(HeaderList)then + FreeAndNil(HeaderList); + End; + procedure Redirect(Url: String); begin AResponse.SendRedirect(Url); end; + Procedure WriteError; + Begin + AResponse.Code := StatusCode; + mb := TStringStream.Create(ErrorMessage); + mb.Position := 0; + AResponse.FreeContentStream := True; + AResponse.ContentStream := mb; + AResponse.ContentStream.Position := 0; + AResponse.ContentLength := -1;//mb.Size; + AResponse.SendContent; + End; begin aUserName:= EmptyStr; @@ -211,9 +237,9 @@ procedure TRESTDWFphttpServicePooler.ExecRequest(Sender: TObject; ParseHeader; - //@vRedirect := @Redirect; { #todo -oAnderson : Verificar como funciona o Redirect. } + vRedirect := TRedirect(@Redirect); vContentType := ARequest.ContentType; - AuthRealm := '' ; + vAuthRealm := AResponse.WWWAuthenticate; sCharSet := aRequest.AcceptCharset; ErrorMessage := ''; vResponseString := ''; @@ -241,7 +267,7 @@ procedure TRESTDWFphttpServicePooler.ExecRequest(Sender: TObject; aRequest.CustomHeaders , //Params aRequest.URI , //QueryParams ContentStringStream , //ContentStringStream - AuthRealm , //AuthRealm + vAuthRealm , //AuthRealm sCharSet , //sCharSet ErrorMessage , //ErrorMessage StatusCode , //StatusCode @@ -252,11 +278,9 @@ procedure TRESTDWFphttpServicePooler.ExecRequest(Sender: TObject; vRedirect //Redirect ) then begin - - SetReplyCORS; - //AResponseInfo.AuthRealm := vAuthRealm; - AResponse.ContentType := vContentType; + AResponse.WWWAuthenticate := vAuthRealm; + AResponse.ContentType := vContentType; If Encoding = esUtf8 Then AResponse.AcceptCharset := 'utf-8' Else @@ -273,7 +297,8 @@ procedure TRESTDWFphttpServicePooler.ExecRequest(Sender: TObject; ResultStream := TStringStream.Create(ErrorMessage); End; - For I := 0 To vResponseHeader.Count -1 Do + + For I := 0 To vResponseHeader.Count -1 Do AResponse.CustomHeaders.AddPair(vResponseHeader.Names [I], vResponseHeader.Values[vResponseHeader.Names[I]]); If vResponseHeader.Count > 0 Then @@ -288,24 +313,26 @@ procedure TRESTDWFphttpServicePooler.ExecRequest(Sender: TObject; AResponse.ContentLength := ResultStream.Size; End; - end else begin - a := 'no.'; + SetReplyCORS; + AResponse.WWWAuthenticate := vAuthRealm; + AResponse.Code := StatusCode; + + If ErrorMessage <> '' Then + AResponse.Content := ErrorMessage + Else + Begin + AResponse.FreeContentStream := True; + AResponse.ContentStream := ResultStream; + AResponse.ContentStream.Position := 0; + AResponse.ContentLength := -1; + End; end; finally - if assigned(vResponseHeader)then - FreeAndNil(vResponseHeader); - if assigned(ResultStream)then - FreeAndNil(ResultStream); - if assigned(CORSCustomHeaders)then - FreeAndNil(CORSCustomHeaders); - if assigned(ContentStringStream)then - FreeAndNil(ContentStringStream); - if assigned(HeaderList)then - FreeAndNil(HeaderList); + DestroyComponents; end; end;