delphimvcframework/sources/MVCFramework.Middleware.CORS.pas
2015-11-15 18:31:08 +01:00

54 lines
1.4 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);
procedure OnBeforeControllerAction(Context: TWebContext;
const AControllerQualifiedClassName: string; const AActionNAme: string;
var Handled: Boolean);
end;
implementation
{ TCORSMiddleware }
procedure TCORSMiddleware.OnAfterControllerAction(Context: TWebContext;
const AActionNAme: string; const Handled: Boolean);
begin
end;
procedure TCORSMiddleware.OnBeforeControllerAction(Context: TWebContext;
const AControllerQualifiedClassName, AActionNAme: string;
var Handled: Boolean);
begin
// do nothing
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.