Merge commit '547fec74b9dcbf32887aad4fdd6348685c654ef2' into feature_filters

# Conflicts:
#	sources/MVCFramework.Filters.CORS.pas
#	sources/MVCFramework.pas
#	unittests/general/Several/LiveServerTestU.pas
This commit is contained in:
Daniele Teti 2024-02-05 16:30:02 +01:00
parent d1b14eb24a
commit 8b0d370a85
3 changed files with 32 additions and 18 deletions

View File

@ -1,4 +1,4 @@
// *************************************************************************** // ***************************************************************************
// //
// Delphi MVC Framework // Delphi MVC Framework
// //
@ -170,7 +170,12 @@ type
/// <remarks>Using events gives you a lot of flexibility to add functionality to normal htmx responses.</remarks> /// <remarks>Using events gives you a lot of flexibility to add functionality to normal htmx responses.</remarks>
/// <param name="Names">A collection of the names of the javscript events to be triggered</param> /// <param name="Names">A collection of the names of the javscript events to be triggered</param>
/// <param name="After">The timing of the event</param> /// <param name="After">The timing of the event</param>
function HXTriggerClientEvents(Names: TArray<string>; After: TClientEventType = etReceived): TMVCWebResponse; function HXTriggerClientEvents(Names: TArray<string>; After: TClientEventType = etReceived): TMVCWebResponse; overload;
/// <summary>Allows you to trigger a collection of client side events.</summary>
/// <remarks>Using events gives you a lot of flexibility to add functionality to normal htmx responses.</remarks>
/// <param name="EventsDescriptors">A JSON object with events descriptors (https://htmx.org/headers/hx-trigger/)</param>
function HXTriggerClientEvents(EventsDescriptors: TJSONObject; After: TClientEventType = etReceived): TMVCWebResponse; overload;
/// <summary>Allows you to trigger a client side event with parameters.</summary> /// <summary>Allows you to trigger a client side event with parameters.</summary>
/// <remarks>Using events gives you a lot of flexibility to add functionality to normal htmx responses.</remarks> /// <remarks>Using events gives you a lot of flexibility to add functionality to normal htmx responses.</remarks>
@ -392,19 +397,19 @@ end;
function THTMXResponseHelper.HXTriggerClientEvent(Name: string; Params: TValue; After: TClientEventType): TMVCWebResponse; function THTMXResponseHelper.HXTriggerClientEvent(Name: string; Params: TValue; After: TClientEventType): TMVCWebResponse;
var var
ser: TMVCJsonDataObjectsSerializer; lSer: TMVCJsonDataObjectsSerializer;
Data: TJsonObject; lData: TJsonObject;
begin begin
if not Params.IsEmpty then if not Params.IsEmpty then
begin begin
Data := TJsonObject.Create; lData := TJsonObject.Create;
ser := TMVCJsonDataObjectsSerializer.Create; lSer := TMVCJsonDataObjectsSerializer.Create;
try try
ser.TValueToJSONObjectProperty(Data, Name, Params, stdefault, [], []); lSer.TValueToJSONObjectProperty(lData, Name, Params, stdefault, [], []);
SetCustomHeader(ClientEventTypes[After], Data.ToJSON); SetCustomHeader(ClientEventTypes[After], lData.ToJSON);
finally finally
ser.Free; lSer.Free;
Data.Free; lData.Free;
end; end;
end end
else else
@ -413,6 +418,18 @@ begin
Result := Self; Result := Self;
end; end;
function THTMXResponseHelper.HXTriggerClientEvents(EventsDescriptors: TJSONObject;
After: TClientEventType): TMVCWebResponse;
begin
if EventsDescriptors = nil then
begin
Exit(Self);
end;
SetCustomHeader(ClientEventTypes[After], EventsDescriptors.ToJSON(true));
Result := Self;
end;
function THTMXResponseHelper.HXTriggerClientEvents(Names: TArray<string>; After: TClientEventType): TMVCWebResponse; function THTMXResponseHelper.HXTriggerClientEvents(Names: TArray<string>; After: TClientEventType): TMVCWebResponse;
var var
Value: string; Value: string;

View File

@ -1,4 +1,4 @@
// *************************************************************************** // ***************************************************************************
// //
// Delphi MVC Framework // Delphi MVC Framework
// //
@ -345,7 +345,7 @@ begin
end; end;
TMVCStaticContents.SendFile(AFileName, lContentType, AContext); TMVCStaticContents.SendFile(AFileName, lContentType, AContext);
Result := True; Result := True;
Log(TLogLevel.levDebug, AContext.Request.HTTPMethodAsString + ':' + LogI(AContext.Request.HTTPMethodAsString + ':' +
AContext.Request.PathInfo + ' [' + AContext.Request.ClientIp + '] -> ' + AContext.Request.PathInfo + ' [' + AContext.Request.ClientIp + '] -> ' +
ClassName + ' - ' + IntToStr(AContext.Response.StatusCode) + ' ' + ClassName + ' - ' + IntToStr(AContext.Response.StatusCode) + ' ' +
AContext.Response.ReasonString); AContext.Response.ReasonString);

View File

@ -2456,15 +2456,15 @@ begin
case RouterLogState of case RouterLogState of
rlsRouteFound: rlsRouteFound:
begin begin
Log(TLogLevel.levNormal, Context.Request.HTTPMethodAsString + ':' + LogI(Context.Request.HTTPMethodAsString + ':' +
Context.Request.PathInfo + ' [' + Context.Request.ClientIp + '] -> ' + Context.Request.PathInfo + ' [' + Context.Request.ClientIp + '] -> ' +
Sender.GetQualifiedActionName + ' - ' + IntToStr(Context.Response.StatusCode) + ' ' + Sender.GetQualifiedActionName + ' - ' + IntToStr(Context.Response.StatusCode) + ' ' +
Context.Response.ReasonString); Context.Response.ReasonString);
end; end;
rlsRouteNotFound: rlsRouteNotFound:
begin begin
Log(TLogLevel.levNormal, Context.Request.HTTPMethodAsString + ':' + LogW(Context.Request.HTTPMethodAsString + ':' +
Context.Request.PathInfo + ' [' + Context.Request.ClientIp + '] -> {NOT FOUND} - ' + Context.Request.PathInfo + ' [' + Context.Request.ClientIp + '] -> {ROUTE NOT FOUND} - ' +
IntToStr(Context.Response.StatusCode) + ' ' + Context.Response.ReasonString); IntToStr(Context.Response.StatusCode) + ' ' + Context.Response.ReasonString);
end; end;
else else
@ -2789,9 +2789,6 @@ begin
lContext.Response.StatusCode := http_status.NotFound; lContext.Response.StatusCode := http_status.NotFound;
lContext.Response.ReasonString := 'Not Found'; lContext.Response.ReasonString := 'Not Found';
fOnRouterLog(lRouter, rlsRouteNotFound, lContext); fOnRouterLog(lRouter, rlsRouteNotFound, lContext);
raise EMVCException.Create(lContext.Response.ReasonString,
lContext.Request.HTTPMethodAsString + ' ' + lContext.Request.PathInfo, 0,
http_status.NotFound);
end end
else else
begin begin