Skip to content

Commit

Permalink
Merge pull request #336 from anderbelluno/dev
Browse files Browse the repository at this point in the history
Ajustes uRESTDWFphttpBase
  • Loading branch information
mobius1qwe committed May 7, 2023
2 parents 3e86758 + e87fefa commit 2712bac
Showing 1 changed file with 45 additions and 42 deletions.
87 changes: 45 additions & 42 deletions CORE/Source/Sockets/Fphttp/uRESTDWFphttpBase.pas
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ interface
uRESTDWComponentEvents, uRESTDWBasicTypes, uRESTDWJSONObject, uRESTDWBasic,
uRESTDWBasicDB, uRESTDWParams, uRESTDWBasicClass, uRESTDWAbout,
uRESTDWConsts, uRESTDWDataUtils, uRESTDWTools, uRESTDWAuthenticators,
fphttpserver, HTTPDefs, fpwebclient, base64, opensslsockets;
fphttpserver, HTTPDefs, fpwebclient, base64, opensslsockets, httpprotocol;

Type

Expand Down Expand Up @@ -278,59 +278,62 @@ procedure TRESTDWFphttpServicePooler.ExecRequest(Sender: TObject;
vRedirect //Redirect
) then
begin
SetReplyCORS;
AResponse.WWWAuthenticate := vAuthRealm;
AResponse.ContentType := vContentType;
If Encoding = esUtf8 Then
AResponse.AcceptCharset := 'utf-8'
Else
AResponse.AcceptCharset := 'ansi';
AResponse.Code := StatusCode;
If (vResponseString <> '') Or
(ErrorMessage <> '') Then
Begin
If Assigned(ResultStream) Then
FreeAndNil(ResultStream);
If (vResponseString <> '') Then
ResultStream := TStringStream.Create(vResponseString)
Else
ResultStream := TStringStream.Create(ErrorMessage);
End;


For I := 0 To vResponseHeader.Count -1 Do
AResponse.CustomHeaders.AddPair(vResponseHeader.Names [I],
vResponseHeader.Values[vResponseHeader.Names[I]]);
If vResponseHeader.Count > 0 Then
AResponse.SendHeaders;
//AResponse.WriteContent;

If Assigned(ResultStream) Then
Begin
AResponse.ContentStream := ResultStream;
AResponse.SendContent; //SendContent é necessário para devolver o conteúdo
AResponse.ContentStream := Nil;
AResponse.ContentLength := ResultStream.Size;
End;
SetReplyCORS;
AResponse.SetHeader(hhWWWAuthenticate, 'Basic realm="API"'); // Aqui estava o vAuthRealm, mas no caso do FPHTTP precisa ser diferente do Indy
AResponse.ContentType := vContentType;

If Encoding = esUtf8 Then
AResponse.AcceptCharset := 'utf-8'
else
AResponse.AcceptCharset := 'ansi';

AResponse.Code := StatusCode;



if (vResponseString <> '') Or (ErrorMessage <> '') Then
begin
if Assigned(ResultStream) then
FreeAndNil(ResultStream);

if (vResponseString <> '') then
ResultStream := TStringStream.Create(vResponseString)
else
ResultStream := TStringStream.Create(ErrorMessage);
end;


for I := 0 To vResponseHeader.Count -1 Do
AResponse.CustomHeaders.AddPair(vResponseHeader.Names [I], vResponseHeader.Values[vResponseHeader.Names[I]]);

if vResponseHeader.Count > 0 then
AResponse.SendHeaders;
//AResponse.WriteContent;

if Assigned(ResultStream) then
begin
AResponse.ContentStream := ResultStream;
AResponse.SendContent; //SendContent é necessário para devolver o conteúdo
AResponse.ContentStream := Nil;
AResponse.ContentLength := ResultStream.Size;
end;
end
else
begin
SetReplyCORS;
AResponse.WWWAuthenticate := vAuthRealm;
AResponse.SetHeader(hhWWWAuthenticate, 'Basic realm="API"');
AResponse.Code := StatusCode;

If ErrorMessage <> '' Then
AResponse.Content := ErrorMessage
Else
Begin
if ErrorMessage <> '' Then
AResponse.Content := ErrorMessage
else
begin
AResponse.FreeContentStream := True;
AResponse.ContentStream := ResultStream;
AResponse.ContentStream.Position := 0;
AResponse.ContentLength := -1;
End;
end;
end;

finally
DestroyComponents;
end;
Expand Down

0 comments on commit 2712bac

Please sign in to comment.