Skip to content

Commit

Permalink
Improved encoding caching
Browse files Browse the repository at this point in the history
  • Loading branch information
danieleteti committed Dec 7, 2023
1 parent 5324b0e commit b761b9a
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 18 deletions.
1 change: 1 addition & 0 deletions sources/MVCFramework.JSONRPC.Client.pas
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ procedure TMVCJSONRPCExecutor.ClearHTTPHeaders;
function TMVCJSONRPCExecutor.ConfigureHTTPClient(const aConfigProc: TProc<THTTPClient>): IMVCJSONRPCExecutor;
begin
aConfigProc(fHTTP);
Result := Self;
end;

constructor TMVCJSONRPCExecutor.Create(const aURL: string; const aRaiseExceptionOnError: Boolean = True;
Expand Down
58 changes: 45 additions & 13 deletions sources/MVCFramework.pas
Original file line number Diff line number Diff line change
Expand Up @@ -1260,6 +1260,7 @@ implementation
gIsShuttingDown: Boolean = False;
gMVCGlobalActionParamsCache: TMVCStringObjectDictionary<TMVCActionParamCacheItem> = nil;
gHostingFramework: TMVCHostingFrameworkType = hftUnknown;
gEncodingUTF8: TEncoding;


type
Expand Down Expand Up @@ -1440,16 +1441,21 @@ function TMVCWebRequest.Body: string;
{$IF not Defined(BERLINORBETTER)}
lBuffer: TArray<Byte>;
{$ENDIF}
lFreeEncoding: Boolean;
begin
{ DONE -oEzequiel -cRefactoring : Refactoring the method TMVCWebRequest.Body }
if (FBody = EmptyStr) then
begin
if FCharset = EmptyStr then
lEncoding := TEncoding.GetEncoding('UTF-8')
if (FCharset = EmptyStr) or (SameText(FCharset, TMVCCharSet.UTF_8)) then
begin
lFreeEncoding := False;
lEncoding := gEncodingUTF8; //utf-8 is the most used encoding, we have a global instance
end
else
begin
lFreeEncoding := True;
lEncoding := TEncoding.GetEncoding(FCharset);
end;
try

{$IF Defined(BERLINORBETTER)}
FWebRequest.ReadTotalContent; // Otherwise ISAPI Raises "Empty BODY"
FBody := lEncoding.GetString(FWebRequest.RawContent);
Expand All @@ -1459,7 +1465,10 @@ function TMVCWebRequest.Body: string;
FBody := lEncoding.GetString(lBuffer);
{$ENDIF}
finally
lEncoding.Free;
if lFreeEncoding then
begin
lEncoding.Free;
end;
end;
end;
Result := FBody;
Expand Down Expand Up @@ -3999,8 +4008,11 @@ procedure TMVCRenderer.Render(const AStatusCode: Integer);
end;

procedure TMVCRenderer.Render(const AContent: string);
var lContentType: string;
lOutEncoding: TEncoding; lCharset: string;
var
lContentType: string;
lOutEncoding: TEncoding;
lCharset: string;
lFreeEncoding: Boolean;
begin
SplitContentMediaTypeAndCharset(GetContentType, lContentType, lCharset);
if lCharset.IsEmpty then
Expand All @@ -4009,15 +4021,33 @@ procedure TMVCRenderer.Render(const AContent: string);
lContentType := TMVCConstants.DEFAULT_CONTENT_TYPE;
lContentType := BuildContentType(lContentType, lCharset);

lOutEncoding := TEncoding.GetEncoding(lCharset);
lOutEncoding := nil;
if SameText('UTF-8', lCharset) then
begin
lFreeEncoding := False;
end
else
begin
lOutEncoding := TEncoding.GetEncoding(lCharset);
end;
try
if SameText('UTF-8', UpperCase(lCharset)) then
GetContext.Response.SetContentStream(TStringStream.Create(AContent, TEncoding.UTF8),
lContentType)
if not lFreeEncoding then
begin
//utf-8
GetContext
.Response
.SetContentStream(TStringStream.Create(AContent, gEncodingUTF8, False), lContentType)
end
else
begin
GetContext.Response.SetContentStream(TBytesStream.Create(TEncoding.Convert(TEncoding.Default,
lOutEncoding, TEncoding.Default.GetBytes(AContent))), lContentType);
GetContext
.Response
.SetContentStream(
TBytesStream.Create(
TEncoding.Convert(
TEncoding.Default,
lOutEncoding,
TEncoding.Default.GetBytes(AContent))), lContentType);
end;
finally
lOutEncoding.Free;
Expand Down Expand Up @@ -4968,9 +4998,11 @@ initialization
gIsShuttingDown := False;

gMVCGlobalActionParamsCache := TMVCStringObjectDictionary<TMVCActionParamCacheItem>.Create;
gEncodingUTF8 := TEncoding.GetEncoding(TMVCCharSet.UTF_8);

finalization

FreeAndNil(gEncodingUTF8);
FreeAndNil(gMVCGlobalActionParamsCache);


Expand Down
28 changes: 24 additions & 4 deletions unittests/general/Several/LiveServerTestU.pas
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,8 @@ implementation
Vcl.Graphics
{$ENDIF}
, TestConstsU, MVCFramework.Tests.Serializer.Entities,
MVCFramework.Logger, System.IOUtils, MVCFramework.Utils;
MVCFramework.Logger, System.IOUtils, MVCFramework.Utils,
System.Net.HttpClient, System.Net.URLClient;

function GetServer: string;
begin
Expand All @@ -488,7 +489,10 @@ procedure TBaseServerTest.Setup;
begin
inherited;
RESTClient := TMVCRESTClient.New.BaseURL(TEST_SERVER_ADDRESS, 8888);
RESTClient.ReadTimeout(60 * 1000 * 30);
RESTClient
.ReadTimeout(60 * 1000 * 30)
.ProxyServer('localhost')
.ProxyPort(8080);
end;

procedure TBaseServerTest.TearDown;
Expand Down Expand Up @@ -2291,9 +2295,25 @@ procedure TServerTest.TestProducesConsumes03;
res: IMVCRESTResponse;
lContentType: string;
lContentCharset: string;
lVal: String;
lISO8859_1Encoding: TEncoding;
begin
res := RESTClient.Accept(TMVCMediaType.TEXT_PLAIN).Post('/testconsumes/textiso8859_1', 'àèéìòù',
BuildContentType(TMVCMediaType.TEXT_PLAIN, TMVCCharSet.ISO88591));
lISO8859_1Encoding := TEncoding.GetEncoding('iso8859-1');
try
lVal :=
lISO8859_1Encoding
.GetString(
TEncoding.Convert(
TEncoding.Default,
lISO8859_1Encoding,
lISO8859_1Encoding.GetBytes('àèéìòù')
)
);
finally
lISO8859_1Encoding.Free;
end;
res := RESTClient.Accept(TMVCMediaType.TEXT_PLAIN)
.Post('/testconsumes/textiso8859_1', lVal, BuildContentType(TMVCMediaType.TEXT_PLAIN, TMVCCharSet.ISO88591));
Assert.areEqual<Integer>(HTTP_STATUS.OK, res.StatusCode);
// Assert.AreNotEqual('àèéìòù', res.Content, 'non iso8859-1 text is rendered ok whan should not');
SplitContentMediaTypeAndCharset(res.ContentType, lContentType, lContentCharset);
Expand Down
2 changes: 1 addition & 1 deletion unittests/general/Several/TestConstsU.pas
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ interface

const
//TEST_SERVER_ADDRESS = '192.168.56.1';
TEST_SERVER_ADDRESS = '127.0.0.1';
TEST_SERVER_ADDRESS = 'localhost';

implementation

Expand Down

0 comments on commit b761b9a

Please sign in to comment.