risoflora / brookframework

Microframework which helps to develop web Pascal applications.
https://risoflora.github.io/brookframework
GNU Lesser General Public License v2.1
238 stars 54 forks source link

SendBytes method fails if content is an empty array #31

Closed alantelles closed 3 years ago

alantelles commented 3 years ago

When using the SendBytes method to send a binary response it fails if ABytes argument is an empty array of bytes.

Steps to reproduce:

Run this program. It's inspired by hellohttpsrv.pas FPC example

program TestBrook;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes
  { you can add units after this },
  BrookHTTPRequest,
  BrookHTTPResponse,
  BrookHTTPServer;

type
  THTTPServer = class(TBrookHTTPServer)
  protected
    procedure DoRequest(ASender: TObject; ARequest: TBrookHTTPRequest;
      AResponse: TBrookHTTPResponse); override;
  end;

procedure THTTPServer.DoRequest(ASender: TObject; ARequest: TBrookHTTPRequest;
  AResponse: TBrookHTTPResponse);

begin

  AResponse.SendBytes([], 0, 'application/octet-stream', 200);
end;

begin
  with THTTPServer.Create(nil) do
  try
    Port := 4000;
    Open;
    if not Active then
      Exit;
    WriteLn('Server running at http://localhost:', Port);
    ReadLn;
  finally
    Free;
  end;
end.

Raised error and/or generated log.

Invalid argument

Environment:

OS, compiler and Brook version to reproduce the problem, e.g.:

PR: https://github.com/risoflora/brookframework/pull/32

silvioprog commented 3 years ago

Fixed by PR #32. Thanks a lot!