forked from CPsoftBE/BackupOfCromis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cromis.MVC.IndyEngine.pas
210 lines (176 loc) · 6.64 KB
/
Cromis.MVC.IndyEngine.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
unit Cromis.MVC.IndyEngine;
interface
uses
SysUtils, Classes,
// cromis units
Cromis.MVC.Sessions, Cromis.MVC.Routing, Cromis.MVC.Common, Cromis.StringUtils,
// indy units and the HTTPApp unit
idHttpServer, idContext, IdCustomHTTPServer, IDCookie, HTTPApp;
type
TOnGetSessionID = procedure(const Request: TIdHTTPRequestInfo;
var SessionClass: TClass;
var ID: string) of Object;
TOnSetSessionID = procedure(const Response: TIdHTTPResponseInfo;
const Session: IMVCSession) of Object;
IHTTPRequest = Interface(IInterface)
['{11F5ADED-1B02-48A6-89D5-AEA4C29CD229}']
function GetRequest: TIdHTTPRequestInfo;
function GetResponse: TIdHTTPResponseInfo;
property Request: TIdHTTPRequestInfo read GetRequest;
property Response: TIdHTTPResponseInfo read GetResponse;
end;
TMVCIndyEngine = class
private
FOnMVCRoute: TRouteProc;
FHTTPServer: TIdHTTPServer;
FOnGetSessionID: TOnGetSessionID;
FOnSetSessionID: TOnSetSessionID;
FMVCSessionManager: TMVCSessionManager;
procedure OnCommandGet(AContext: TIdContext;
ARequestInfo: TIdHTTPRequestInfo;
AResponseInfo: TIdHTTPResponseInfo);
procedure DoOnMVCRoute(const Sender: IBaseRoute; const HttpRequest: IInterface);
function FillMVCData(const HttpRequest: IHTTPRequest; const Session: IMVCSession): IMVCData;
public
constructor Create;
destructor Destroy; override;
procedure FinalizeEngine;
procedure InitializeEngine;
function GetMimeType(const FileName: string): string;
property HTTPServer: TIdHTTPServer read FHTTPServer;
property OnMVCRoute: TRouteProc read FOnMVCRoute write FOnMVCRoute;
property SessionManager: TMVCSessionManager read FMVCSessionManager;
property OnGetSessionID: TOnGetSessionID read FOnGetSessionID write FOnGetSessionID;
property OnSetSessionID: TOnSetSessionID read FOnSetSessionID write FOnSetSessionID;
end;
implementation
type
THTTPRequest = class(TInterfacedObject, IHTTPRequest)
private
FRequest: TIdHTTPRequestInfo;
FResponse: TIdHTTPResponseInfo;
function GetRequest: TIdHTTPRequestInfo;
function GetResponse: TIdHTTPResponseInfo;
public
constructor Create(const Request: TIdHTTPRequestInfo;
const Response: TIdHTTPResponseInfo);
property Response: TIdHTTPResponseInfo read GetResponse;
property Request: TIdHTTPRequestInfo read GetRequest;
end;
{ TMVCIndyEngine }
constructor TMVCIndyEngine.Create;
begin
FOnMVCRoute := DoOnMVCRoute;
// create and initialize HTTP server
FHTTPServer := TIdHTTPServer.Create;
FHTTPServer.OnCommandGet := OnCommandGet;
FHTTPServer.ParseParams := False;
// create the session manager object
FMVCSessionManager := TMVCSessionManager.Create;
end;
destructor TMVCIndyEngine.Destroy;
begin
FreeAndNil(FMVCSessionManager);
FreeAndNil(FHTTPServer);
inherited;
end;
function TMVCIndyEngine.FillMVCData(const HttpRequest: IHTTPRequest; const Session: IMVCSession): IMVCData;
var
I: Integer;
Params: TStringList;
begin
Result := AcquireMVCData;
Params := TStringList.Create;
try
Params.StrictDelimiter := True;
Params.Delimiter := '&';
// parse the parameters and store them into temporary string list
Params.DelimitedText := UTF8ToString(HTTPDecode(UTF8Encode(HttpRequest.Request.UnparsedParams)));
for I := 0 to Params.Count - 1 do
Result.Values.Ensure(Params.Names[I]).AsString := Params.ValueFromIndex[I];
finally
Params.Free;
end;
end;
procedure TMVCIndyEngine.FinalizeEngine;
begin
FHTTPServer.Active := False;
WebRouting.ClearAllRoutes;
end;
function TMVCIndyEngine.GetMimeType(const FileName: string): string;
begin
Result := FHTTPServer.MIMETable.GetFileMIMEType(FileName);
end;
procedure TMVCIndyEngine.InitializeEngine;
begin
FHTTPServer.Active := True;
end;
procedure TMVCIndyEngine.OnCommandGet(AContext: TIdContext;
ARequestInfo: TIdHTTPRequestInfo;
AResponseInfo: TIdHTTPResponseInfo);
var
WebRoute: IBaseRoute;
HTTPRequest: IHTTPRequest;
begin
HTTPRequest := THTTPRequest.Create(ARequestInfo, AResponseInfo);
WebRoute := WebRouting.FindRoute(ARequestInfo.Document);
case WebRoute.RouteType of
rtBase: raise Exception.Create('BaseRoute interface is not allowed');
rtCustom: ICustomRoute(WebRoute).RouteProc(WebRoute, HTTPRequest);
rtMVC: FOnMVCRoute(WebRoute, HTTPRequest);
end;
end;
procedure TMVCIndyEngine.DoOnMVCRoute(const Sender: IBaseRoute; const HttpRequest: IInterface);
var
Session: IMVCSession;
SessiondID: string;
SessionClass: TClass;
RedirectURL: string;
TempRequest: IHTTPRequest;
begin
TempRequest := IHTTPRequest(HttpRequest);
// dispatch the request to MVC and wait for result
FOnGetSessionID(TempRequest.Request, SessionClass, SessiondID);
Session := FMVCSessionManager.AcquireSession(SessiondID, SessionClass);
Session.InitializeRequest;
try
Session.DispatchRequest(IMVCRoute(Sender), FillMVCData(TempRequest, Session));
if Session.SessionData.DispatchResult = drForbiden then
begin
TempRequest.Response.ResponseNo := 401;
Exit;
end
else if Session.SessionData.DispatchResult = drRedirectToLogin then
begin
RedirectURL := Format('/%s/%s', [Session.AuthenticationRoute.Controller,
Session.AuthenticationRoute.AuthCommand]);
TempRequest.Response.Redirect(RedirectURL);
Exit;
end;
// if all is ok then send content back
TempRequest.Response.ContentStream := TMemoryStream.Create;
Session.SessionData.SessionContent.Data.Seek(0, soFromBeginning);
TempRequest.Response.ContentStream.CopyFrom(Session.SessionData.SessionContent.Data, 0);
TempRequest.Response.ContentType := Session.SessionData.SessionContent.Encoding;
TempRequest.Response.ResponseNo := Session.SessionData.SessionContent.ExitCode;
FOnSetSessionID(TempRequest.Response, Session);
finally
Session.FinalizeRequest;
end;
end;
{ THTTPRequest }
constructor THTTPRequest.Create(const Request: TIdHTTPRequestInfo;
const Response: TIdHTTPResponseInfo);
begin
FRequest := Request;
FResponse := Response;
end;
function THTTPRequest.GetRequest: TIdHTTPRequestInfo;
begin
Result := FRequest;
end;
function THTTPRequest.GetResponse: TIdHTTPResponseInfo;
begin
Result := FResponse;
end;
end.