2016-06-22 17:49:16 +02:00
|
|
|
// ***************************************************************************
|
|
|
|
//
|
|
|
|
// Delphi MVC Framework
|
|
|
|
//
|
2017-01-05 12:44:34 +01:00
|
|
|
// Copyright (c) 2010-2017 Daniele Teti and the DMVCFramework Team
|
2016-06-22 17:49:16 +02:00
|
|
|
//
|
|
|
|
// https://github.com/danieleteti/delphimvcframework
|
|
|
|
//
|
|
|
|
// ***************************************************************************
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
// *************************************************************************** }
|
2015-12-22 12:38:17 +01:00
|
|
|
|
2014-10-03 11:40:57 +02:00
|
|
|
unit MVCFramework.Middleware.CORS;
|
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
{$I dmvcframework.inc}
|
|
|
|
|
2014-10-03 11:40:57 +02:00
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
2017-03-20 19:08:01 +01:00
|
|
|
System.StrUtils,
|
|
|
|
MVCFramework,
|
|
|
|
MVCFramework.Commons;
|
2014-10-03 11:40:57 +02:00
|
|
|
|
|
|
|
type
|
2017-03-20 19:08:01 +01:00
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
TMVCCORSMiddleware = class(TInterfacedObject, IMVCMiddleware)
|
2016-10-12 13:10:41 +02:00
|
|
|
private
|
|
|
|
FAllowedOriginURL: string;
|
|
|
|
FAllowsCredentials: string;
|
2017-03-20 19:08:01 +01:00
|
|
|
protected
|
2017-03-23 18:51:25 +01:00
|
|
|
procedure OnBeforeRouting(
|
|
|
|
AContext: TWebContext;
|
|
|
|
var AHandled: Boolean
|
|
|
|
);
|
2017-03-20 19:08:01 +01:00
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
procedure OnBeforeControllerAction(
|
|
|
|
AContext: TWebContext;
|
|
|
|
const AControllerQualifiedClassName: string;
|
|
|
|
const AActionName: string;
|
|
|
|
var AHandled: Boolean
|
|
|
|
);
|
|
|
|
|
|
|
|
procedure OnAfterControllerAction(
|
|
|
|
AContext: TWebContext;
|
|
|
|
const AActionName: string;
|
|
|
|
const AHandled: Boolean
|
|
|
|
);
|
|
|
|
public
|
|
|
|
constructor Create(const AAllowedOriginURL: string = '*'; const AAllowsCredentials: Boolean = True); virtual;
|
2016-06-22 17:49:16 +02:00
|
|
|
end;
|
2014-10-03 11:40:57 +02:00
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
TCORSMiddleware = TMVCCORSMiddleware;
|
2014-10-03 11:40:57 +02:00
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
implementation
|
2016-10-12 13:10:41 +02:00
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
{ TMVCCORSMiddleware }
|
2014-10-03 11:40:57 +02:00
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
constructor TMVCCORSMiddleware.Create(const AAllowedOriginURL: string; const AAllowsCredentials: Boolean);
|
2016-10-12 13:10:41 +02:00
|
|
|
begin
|
|
|
|
inherited Create;
|
2017-03-23 18:51:25 +01:00
|
|
|
FAllowedOriginURL := AAllowedOriginURL;
|
|
|
|
FAllowsCredentials := IfThen(AAllowsCredentials, 'true', 'false');
|
2016-10-12 13:10:41 +02:00
|
|
|
end;
|
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
procedure TMVCCORSMiddleware.OnAfterControllerAction(AContext: TWebContext;
|
|
|
|
const AActionName: string; const AHandled: Boolean);
|
2015-11-15 18:31:08 +01:00
|
|
|
begin
|
2017-03-23 18:51:25 +01:00
|
|
|
// Implement as needed
|
2015-11-15 18:31:08 +01:00
|
|
|
end;
|
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
procedure TMVCCORSMiddleware.OnBeforeControllerAction(
|
|
|
|
AContext: TWebContext; const AControllerQualifiedClassName,
|
|
|
|
AActionName: string; var AHandled: Boolean);
|
2014-10-03 11:40:57 +02:00
|
|
|
begin
|
2017-03-23 18:51:25 +01:00
|
|
|
// Implement as needed
|
2014-10-03 11:40:57 +02:00
|
|
|
end;
|
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
procedure TMVCCORSMiddleware.OnBeforeRouting(AContext: TWebContext; var AHandled: Boolean);
|
2014-10-03 11:40:57 +02:00
|
|
|
begin
|
2017-03-20 19:08:01 +01:00
|
|
|
AContext.Response.RawWebResponse.CustomHeaders.Values['Access-Control-Allow-Origin'] := FAllowedOriginURL;
|
|
|
|
AContext.Response.RawWebResponse.CustomHeaders.Values['Access-Control-Allow-Methods'] := 'POST, GET, OPTIONS, PUT, DELETE';
|
2017-05-31 08:48:40 +02:00
|
|
|
AContext.Response.RawWebResponse.CustomHeaders.Values['Access-Control-Allow-Headers'] := 'Content-Type, Accept, jwtusername, jwtpassword, authentication';
|
2017-03-20 19:08:01 +01:00
|
|
|
AContext.Response.RawWebResponse.CustomHeaders.Values['Access-Control-Allow-Credentials'] := FAllowsCredentials;
|
|
|
|
if (AContext.Request.HTTPMethod = httpOPTIONS) then
|
2016-06-22 17:49:16 +02:00
|
|
|
begin
|
2017-03-23 18:51:25 +01:00
|
|
|
AContext.Response.StatusCode := HTTP_STATUS.OK;
|
2017-03-20 19:08:01 +01:00
|
|
|
AHandled := True;
|
2016-06-22 17:49:16 +02:00
|
|
|
end;
|
2014-10-03 11:40:57 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|