Skip to content

Commit

Permalink
Fixed error "Response already sent" when isolating a request from the…
Browse files Browse the repository at this point in the history
… main event loop. (Fix #30)
  • Loading branch information
silvioclecio committed May 10, 2021
1 parent b8edf85 commit 75a0d6b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
17 changes: 14 additions & 3 deletions Source/BrookHTTPRequest.pas
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(* _ _
(* _ _
* | |__ _ __ ___ ___ | | __
* | '_ \| '__/ _ \ / _ \| |/ /
* | |_) | | | (_) | (_) | <
Expand Down Expand Up @@ -73,6 +73,7 @@ TBrookHTTPRequest = class(TBrookHandledPersistent)
FVersion: string;
FMethod: string;
FPath: string;
FIsIsolated: Boolean;
FIsUploading: Boolean;
FClient: Pointer;
FTLSSession: Pointer;
Expand All @@ -82,6 +83,7 @@ TBrookHTTPRequest = class(TBrookHandledPersistent)
function GetContentType: string; {$IFNDEF DEBUG}inline;{$ENDIF}
function GetReferer: string; {$IFNDEF DEBUG}inline;{$ENDIF}
function GetUserAgent: string; {$IFNDEF DEBUG}inline;{$ENDIF}
procedure SetIsIsolated(AValue: Boolean); {$IFNDEF DEBUG}inline;{$ENDIF}
protected
class procedure DoRequestIsolatedProcCallback(Acls: Pcvoid;
Areq: Psg_httpreq; Ares: Psg_httpres); cdecl; static;
Expand Down Expand Up @@ -165,6 +167,9 @@ TBrookHTTPRequest = class(TBrookHandledPersistent)
property Referer: string read GetReferer;
{ Contains the levels of the path component. }
property Paths: TArray<string> read GetPaths;
{ Checks if the request was isolated from the main event loop to an own
dedicated thread. }
property IsIsolated: Boolean read FIsIsolated;
{ Checks if the client is uploading data. }
property IsUploading: Boolean read FIsUploading;
{ List of the uploaded files. }
Expand Down Expand Up @@ -297,8 +302,7 @@ class procedure TBrookHTTPRequest.DoRequestIsolatedAnonymousProcCallback(

{$ENDIF}

class function TBrookHTTPRequest.CreateRequest(
AHandle: Pointer): TBrookHTTPRequest;
class function TBrookHTTPRequest.CreateRequest(AHandle: Pointer): TBrookHTTPRequest;
begin
Result := TBrookHTTPRequest.Create(AHandle);
end;
Expand Down Expand Up @@ -376,6 +380,11 @@ function TBrookHTTPRequest.GetHandle: Pointer;
Result := FHandle;
end;

procedure TBrookHTTPRequest.SetIsIsolated(AValue: Boolean);
begin
FIsIsolated := AValue;
end;

function TBrookHTTPRequest.GetIP: string;
var
P: array[0..45] of cchar;
Expand Down Expand Up @@ -441,6 +450,7 @@ procedure TBrookHTTPRequest.Isolate(AProc: TBrookHTTPRequestIsolatedProc;
VHolder: TBrookHTTPReqIsolatedProcHolder<TBrookHTTPRequestIsolatedProc>;
begin
SgLib.Check;
SetIsIsolated(True);
VHolder := TBrookHTTPReqIsolatedProcHolder<
TBrookHTTPRequestIsolatedProc>.Create(AProc, AUserData);
try
Expand All @@ -461,6 +471,7 @@ procedure TBrookHTTPRequest.Isolate(
TBrookHTTPRequestIsolatedAnonymousProc>;
begin
SgLib.Check;
SetIsIsolated(True);
VHolder := TBrookHTTPReqIsolatedProcHolder<
TBrookHTTPRequestIsolatedAnonymousProc>.Create(AProc, AUserData);
try
Expand Down
4 changes: 2 additions & 2 deletions Source/BrookHTTPServer.pas
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(* _ _
(* _ _
* | |__ _ __ ___ ___ | | __
* | '_ \| '__/ _ \ / _ \| |/ /
* | |_) | | | (_) | (_) | <
Expand Down Expand Up @@ -517,7 +517,7 @@ class procedure TBrookHTTPServer.DoRequestCallback(Acls: Pcvoid;
finally
VSrv.Unlock;
end;
if VRes.IsEmpty then
if VRes.IsEmpty and (not VReq.IsIsolated) then
VRes.SendEmpty;
end;
finally
Expand Down

2 comments on commit 75a0d6b

@KevinLeeMorris
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am the original reporting person. I have tested this fix. It works as intended.

@silvioprog
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am the original reporting person. I have tested this fix. It works as intended.

It was an important fix. Thanks for reporting! 👍

Please sign in to comment.