mirror of
https://github.com/danieleteti/delphimvcframework.git
synced 2024-11-16 08:15:53 +01:00
42 lines
1.1 KiB
ObjectPascal
42 lines
1.1 KiB
ObjectPascal
unit MVCFramework.Middleware.CORS;
|
|
|
|
interface
|
|
|
|
uses
|
|
MVCFramework;
|
|
|
|
type
|
|
TCORSMiddleware = class(TInterfacedObject, IMVCMiddleware)
|
|
public
|
|
procedure OnBeforeRouting(Context: TWebContext; var Handled: Boolean);
|
|
procedure OnAfterControllerAction(Context: TWebContext;
|
|
const AActionNAme: string; const Handled: Boolean);
|
|
|
|
end;
|
|
|
|
implementation
|
|
|
|
{ TCORSMiddleware }
|
|
|
|
procedure TCORSMiddleware.OnAfterControllerAction(Context: TWebContext;
|
|
const AActionNAme: string; const Handled: Boolean);
|
|
begin
|
|
end;
|
|
|
|
procedure TCORSMiddleware.OnBeforeRouting(Context: TWebContext;
|
|
var Handled: Boolean);
|
|
begin
|
|
Context.Response.RawWebResponse.CustomHeaders.Values['Access-Control-Allow-Origin'] := '*';
|
|
Context.Response.RawWebResponse.CustomHeaders.Values['Access-Control-Allow-Methods'] :=
|
|
'POST, GET, OPTIONS, PUT, DELETE';
|
|
Context.Response.RawWebResponse.CustomHeaders.Values['Access-Control-Allow-Headers'] := 'content-type';
|
|
|
|
if Context.Request.HTTPMethod = httpOPTIONS then
|
|
begin
|
|
Context.Response.StatusCode := 200;
|
|
Handled := True;
|
|
end;
|
|
end;
|
|
|
|
end.
|