forked from nikitayev/extpascal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIdExtHTTPServer.pas
370 lines (324 loc) · 12.3 KB
/
IdExtHTTPServer.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
unit IdExtHTTPServer;
interface
uses
{$IFNDEF MSWINDOWS}cthreads,{$ENDIF}
Classes, IdCustomHTTPServer, IdHTTPServer, ExtPascalClasses, ExtPascalUtils;
type
TIdWebSession = class(TCustomWebSession)
private
procedure SetCustomResponseHeaders;
protected
function CanCallAfterHandleRequest : Boolean; override;
function CanHandleUrlPath : Boolean; override;
procedure DoLogout; override;
procedure DoSetCookie(const Name, ValueRaw : string); override;
class function GetCurrentWebSession : TCustomWebSession; override;
function GetDocumentRoot : string; override;
function GetRequestHeader(const Name : string) : string; override;
function GetWebServer : string; override;
procedure SendResponse(const Msg : string); override;
function TryToServeFile : Boolean; override;
function UploadBlockType(const Buffer : AnsiString; var MarkPos : Integer) : TUploadBlockType; override;
function UploadNeedUnknownBlock : Boolean; override;
public
constructor Create(AOwner : TObject); override;
end;
TWebSession = class(TIdWebSession);
TIdWebApplication = class(TCustomWebApplication)
private
FServer: TIdHTTPServer;
public
constructor Create(ATitle : string; ASessionClass : TCustomWebSessionClass; APort : word = 80;
AMaxIdleMinutes : word = 30; AMaxConns : integer = 1000); reintroduce;
procedure DoRun; override;
end;
var
Application : TIdWebApplication; // Indy web application object
threadvar
CurrentWebSession : TIdWebSession; // current Indy web session object
function CreateWebApplication(const ATitle: string; ASessionClass: TCustomWebSessionClass; APort: Word = 80;
AMaxIdleMinutes : Word = 30; AShutdownAfterLastThreadDown : Boolean = False;
AMaxConns : Integer = 1000): TIdWebApplication;
implementation
uses
{$IFDEF MSWINDOWS}Windows, Messages,{$ENDIF} StrUtils, SysUtils,
IdGlobal, IdGlobalProtocols, IdCookie, IdContext;
function CreateWebApplication(const ATitle : string; ASessionClass: TCustomWebSessionClass; APort: Word = 80;
AMaxIdleMinutes : Word = 30; AShutdownAfterLastThreadDown : Boolean = False;
AMaxConns : Integer = 1000) : TIdWebApplication;
begin
Result := TIdWebApplication.Create(ATitle, ASessionClass, APort, AMaxIdleMinutes, AMaxConns);
end;
type
TIdWebHTTPServer = class(TIdHTTPServer)
protected
procedure InitComponent; override;
procedure CommandGet(AContext : TIdContext; ARequestInfo : TIdHTTPRequestInfo; AResponseInfo : TIdHTTPResponseInfo);
end;
TIdWebHTTPSessionList = class(TIdHTTPDefaultSessionList)
private
FOwner : TIdWebHTTPServer;
public
constructor Create(const AOwner : TIdWebHTTPServer); reintroduce;
function CreateSession(const RemoteIP, SessionID : string) : TIdHTTPSession; override;
end;
TIdWebHTTPSession = class(TIdHTTPSession)
private
FCurrentRequest : TIdHTTPRequestInfo;
FCurrentResponse : TIdHTTPResponseInfo;
FSession : TIdWebSession;
procedure ParseCookies(IdCookies : TIdServerCookies);
public
constructor CreateInitialized(AOwner : TIdHTTPCustomSessionList; const SessionID, RemoteIP : string); override;
destructor Destroy; override;
procedure HandleRequest(ARequest : TIdHTTPRequestInfo; AResponse : TIdHTTPResponseInfo);
end;
{ TIdWebHTTPSessionList }
constructor TIdWebHTTPSessionList.Create(const AOwner : TIdWebHTTPServer); begin
FOwner := AOwner;
inherited Create(AOwner);
end;
function TIdWebHTTPSessionList.CreateSession(const RemoteIP, SessionID : string) : TIdHTTPSession; begin
Result := TIdWebHTTPSession.CreateInitialized(Self, SessionID, RemoteIP);
SessionList.Add(Result);
end;
{ TIdWebHTTPServer }
procedure TIdWebHTTPServer.CommandGet(AContext : TIdContext; ARequestInfo : TIdHTTPRequestInfo; AResponseInfo : TIdHTTPResponseInfo);
var
I : Integer;
begin
if pos('multipart/form-data', ARequestInfo.ContentType) <> 0 then
with TIdWebHTTPSession(ARequestInfo.Session) do begin
I := 0;
FSession.UploadPrepare(ARequestInfo.ContentType, ARequestInfo.UnparsedParams, I);
FSession.UploadWriteFile(ARequestInfo.UnparsedParams, I);
FCurrentResponse := AResponseInfo;
FCurrentResponse.ContentText := FSession.Response;
end;
TIdWebHTTPSession(ARequestInfo.Session).HandleRequest(ARequestInfo, AResponseInfo);
end;
procedure TIdWebHTTPServer.InitComponent; begin
inherited;
SessionState := True;
AutoStartSession := True;
SessionList.Free;
FSessionList := TIdWebHTTPSessionList.Create(Self);
end;
{ TIdWebHTTPSession }
constructor TIdWebHTTPSession.CreateInitialized(AOwner : TIdHTTPCustomSessionList; const SessionID, RemoteIP : string); begin
inherited;
FSession := TIdWebSession(Application.SessionClass.Create(Self));
FSession.FApplication := Application;
FSession.AfterNewSession;
end;
destructor TIdWebHTTPSession.Destroy; begin
FSession.Free;
inherited;
end;
procedure TIdWebHTTPSession.HandleRequest(ARequest : TIdHTTPRequestInfo; AResponse : TIdHTTPResponseInfo); begin
CurrentWebSession := FSession;
FCurrentRequest := ARequest;
FCurrentResponse := AResponse;
ParseCookies(ARequest.Cookies);
if FSession.Cookie['FCGIThread'] = '' then
FSession.SetCookie('FCGIThread', SessionID);
if not FSession.IsUpload then begin
FSession.ContentType := 'text/html';
FSession.Response := '';
FSession.FCustomResponseHeaders.Clear;
FSession.SetQueryText(FCurrentRequest.UnParsedParams, False, False);
end;
with FSession do begin
FPathInfo := FCurrentRequest.Document;
if (FPathInfo <> '') and (FPathInfo[1] = '/') then Delete(FPathInfo, 1, 1);
FScriptName := Query['SCRIPT_NAME'];
if (FScriptName = '') or (FScriptName[Length(FScriptName)] <> '/') then FScriptName := FScriptName + '/';
end;
FSession.HandleRequest(FCurrentRequest.UnParsedParams);
if not FSession.IsUpload then begin
TIdWebSession(FSession).SetCustomResponseHeaders;
if not Assigned(AResponse.ContentStream) and (FSession.Response <> '') and (AResponse.ResponseNo <> 304) then
FCurrentResponse.ContentText := FSession.Response;
end;
FCurrentResponse.ContentType := FSession.ContentType;
FSession.IsDownload := false;
FSession.IsUpload := false;
end;
procedure TIdWebHTTPSession.ParseCookies(IdCookies : TIdServerCookies);
var
I: Integer;
begin
with FSession do begin
FCookies.Clear;
for I := 0 to IdCookies.Count - 1 do
with IdCookies[I] do
FCookies.Values[CookieName] := CookieText;
end;
end;
{ TIdWebApplication }
constructor TIdWebApplication.Create(ATitle : string; ASessionClass : TCustomWebSessionClass;
APort, AMaxIdleMinutes : word; AMaxConns : integer); begin
inherited Create(ATitle, ASessionClass, APort, AMaxIdleMinutes, AMaxConns);
Assert(Assigned(ASessionClass) and ASessionClass.InheritsFrom(TIdWebSession));
FServer := TIdWebHTTPServer.Create(nil);
with TIdWebHTTPServer(FServer) do begin
OnCommandGet := CommandGet;
SessionTimeOut := AMaxIdleMinutes;
MaxConnections := AMaxConns;
ServerSoftware := ATitle;
DefaultPort := APort;
{$IFNDEF MSWINDOWS}
with Bindings do begin
Clear;
Add;
Items[0].SetPeer('127.0.0.1', APort, id_IPV4);
end;
{$ENDIF}
end;
end;
procedure TIdWebApplication.DoRun;
{$IFDEF MSWINDOWS}
var
Msg : TMsg;
Unicode : boolean;
{$ENDIF}
begin
TIdWebHTTPServer(FServer).Startup;
while not Terminated do
{$IFDEF MSWINDOWS}
if PeekMessage(Msg, 0, 0, 0, PM_NOREMOVE) then begin
Unicode := (Msg.hwnd <> 0) and IsWindowUnicode(Msg.hwnd);
if Unicode then
PeekMessageW(Msg, 0, 0, 0, PM_REMOVE)
else
PeekMessage(Msg, 0, 0, 0, PM_REMOVE);
if Msg.Message = WM_QUIT then exit;
TranslateMessage(Msg);
if Unicode then
DispatchMessageW(Msg)
else
DispatchMessage(Msg);
end
else
{$ENDIF}
sleep(10);
end;
{ TIdWebSession }
constructor TIdWebSession.Create(AOwner : TObject); begin
inherited Create(AOwner);
FOwner := AOwner as TIdWebHTTPSession;
end;
function TIdWebSession.CanCallAfterHandleRequest : Boolean; begin
Result := not IsUpload;
end;
function TIdWebSession.CanHandleUrlPath : Boolean; begin
Result := True;
end;
procedure TIdWebSession.DoLogout; begin
inherited;
TIdWebHTTPSession(FOwner).FLastTimeStamp := 0;
end;
procedure TIdWebSession.DoSetCookie(const Name, ValueRaw : string); begin
with TIdWebHTTPSession(FOwner) do begin
with FCurrentResponse.Cookies.Add do begin
CookieName := 'FCGIThread';
CookieText := ValueRaw;
end;
FCurrentRequest.Cookies.AddSrcCookie(ValueRaw);
end;
end;
class function TIdWebSession.GetCurrentWebSession : TCustomWebSession; begin
Result := CurrentWebSession;
end;
function TIdWebSession.GetDocumentRoot : string; begin
Result := '.';
end;
function TIdWebSession.GetRequestHeader(const Name : string): string;
var
HeaderName : string;
begin
HeaderName := Name;
if pos('HTTP_', HeaderName) = 1 then HeaderName := copy(HeaderName, 6, MaxInt);
HeaderName := AnsiReplaceStr(HeaderName, '_', '-');
with TIdWebHTTPSession(FOwner) do
if FCurrentRequest = nil then
Result := ''
else
if FCurrentRequest.RawHeaders = nil then
if FCurrentRequest.CustomHeaders = nil then
Result := ''
else
Result := FCurrentRequest.CustomHeaders.Values[HeaderName]
else
Result := FCurrentRequest.RawHeaders.Values[HeaderName];
end;
function TIdWebSession.GetWebServer : string; begin
Result := 'Embedded'; // or maybe 'Embedded Indy' ?
end;
procedure TIdWebSession.SendResponse(const Msg : string); begin
with TIdWebHTTPSession(FOwner).FCurrentResponse do begin
ContentText := Msg;
WriteContent;
end;
end;
procedure TIdWebSession.SetCustomResponseHeaders;
var
i: Integer;
begin
for i := 0 to FCustomResponseHeaders.Count - 1 do
with TIdWebHTTPSession(FOwner).FCurrentResponse.CustomHeaders do
Values[FCustomResponseHeaders.Names[i]] := FCustomResponseHeaders.ValueFromIndex[i];
end;
function TIdWebSession.TryToServeFile : Boolean;
function CheckIfFileIsModified(FileName : string) : Boolean;
const
FCompareDateFmt = 'yyyymmddhhnnss';
var
FFileDateTime: TDateTime;
begin
Result := True;
with TIdWebHTTPSession(FOwner).FCurrentRequest.RawHeaders do
if (Values['if-Modified-Since'] <> '') then begin
FFileDateTime := GetGMTDateByName(FileName);
Result := not SameText(FormatDateTime(FCompareDateFmt, FFileDateTime),
FormatDateTime(FCompareDateFmt, StrInternetToDateTime(Values['if-Modified-Since'])));
end;
end;
var
FileName : string;
FileDateTime : TDateTime;
begin
FileName := ExtractFilePath(ParamStr(0));
FileName := StringReplace(FileName, ExtractFileDrive(FileName), '', []);
with TIdWebHTTPSession(FOwner).FCurrentRequest do
if (Length(Document) > 1) and (Document[1]in ['/', '\']) then
FileName := FileName + Copy(Document, 2, MaxInt)
else
FileName := FileName + Document;
FileName := ExpandFilename(FileName);
with TIdWebHTTPSession(FOwner).FCurrentResponse do
if FileExists(FileName) then begin
Result := True;
if CheckIfFileIsModified(FileName) then begin
ContentStream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
FreeContentStream := True;
ContentLength := ContentStream.Size;
FileDateTime := GetGMTDateByName(FileName);
LastModified := FileDateTime;
Self.ContentType := DownloadContentType(FileName, 'text/html');
end
else
ResponseNo := 304; // Not Modified, use cache version
end
else begin
ResponseNo := 404; // Not found
Result := False;
end;
end;
function TIdWebSession.UploadBlockType(const Buffer : AnsiString; var MarkPos : Integer) : TUploadBlockType; begin
Result := ubtBegin;
end;
function TIdWebSession.UploadNeedUnknownBlock : Boolean; begin
Result := False;
end;
end.