delphimvcframework/sources/MVCFramework.Middleware.CORS.pas

42 lines
1.1 KiB
ObjectPascal
Raw Normal View History

2014-10-03 11:40:57 +02:00
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.