blob: 3e82c5fd2a4f5a643d5fdaff189d19165ea53fd0 (
plain)
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
|
{
This file is part of the Free Pascal run time library.
Copyright (c) 1999-2009 by the Free Pascal development team
THTTPApplication class.
See the file COPYING.FPC, included in this distribution,
for details about the copyright.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
**********************************************************************}
{ $define CGIDEBUG}
{$mode objfpc}
{$H+}
unit custhttpapp;
Interface
uses
Classes, SysUtils, httpdefs, custweb, ssockets, fphttpserver;
Type
TCustomHTTPApplication = Class;
TEmbeddedHttpServer = Class(TFPCustomHttpServer)
protected
Property Active;
end;
{ TFCgiHandler }
{ TFPHTTPServerHandler }
TFPHTTPServerHandler = class(TWebHandler)
procedure HTTPHandleRequest(Sender: TObject;
var ARequest: TFPHTTPConnectionRequest;
var AResponse: TFPHTTPConnectionResponse);
Private
FServer: TEmbeddedHTTPServer;
function GetAllowConnect: TConnectQuery;
function GetPort: Word;
function GetQueueSize: Word;
function GetThreaded: Boolean;
procedure SetOnAllowConnect(const AValue: TConnectQuery);
procedure SetPort(const AValue: Word);
procedure SetQueueSize(const AValue: Word);
procedure SetThreaded(const AValue: Boolean);
protected
function WaitForRequest(out ARequest : TRequest; out AResponse : TResponse) : boolean; override;
Function CreateServer : TEmbeddedHttpServer; virtual;
Property HTTPServer : TEmbeddedHttpServer Read FServer;
Public
Procedure Run; override;
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
// Port to listen on.
Property Port : Word Read GetPort Write SetPort Default 80;
// Max connections on queue (for Listen call)
Property QueueSize : Word Read GetQueueSize Write SetQueueSize Default 5;
// Called when deciding whether to accept a connection.
Property OnAllowConnect : TConnectQuery Read GetAllowConnect Write SetOnAllowConnect;
// Use a thread to handle a connection ?
property Threaded : Boolean read GetThreaded Write SetThreaded;
end;
{ TCustomHTTPApplication }
TCustomHTTPApplication = Class(TCustomWebApplication)
private
function GetAllowConnect: TConnectQuery;
function GetPort: Word;
function GetQueueSize: Word;
function GetThreaded: Boolean;
procedure SetOnAllowConnect(const AValue: TConnectQuery);
procedure SetPort(const AValue: Word);
procedure SetQueueSize(const AValue: Word);
procedure SetThreaded(const AValue: Boolean);
protected
function InitializeWebHandler: TWebHandler; override;
Function HTTPHandler : TFPHTTPServerHandler;
Public
Property Port : Word Read GetPort Write SetPort Default 80;
// Max connections on queue (for Listen call)
Property QueueSize : Word Read GetQueueSize Write SetQueueSize Default 5;
// Called when deciding whether to accept a connection.
Property OnAllowConnect : TConnectQuery Read GetAllowConnect Write SetOnAllowConnect;
// Use a thread to handle a connection ?
property Threaded : Boolean read GetThreaded Write SetThreaded;
end;
ResourceString
SNoInputHandle = 'Failed to open input-handle passed from server. Socket Error: %d';
SNoSocket = 'Failed to open socket. Socket Error: %d';
SBindFailed = 'Failed to bind to port %d. Socket Error: %d';
SListenFailed = 'Failed to listen to port %d. Socket Error: %d';
SErrReadingSocket = 'Failed to read data from socket. Error: %d';
SErrReadingHeader = 'Failed to read FastCGI header. Read only %d bytes';
SErrWritingSocket = 'Failed to write data to socket. Error: %d';
Implementation
{$ifdef CGIDEBUG}
uses
dbugintf;
{$endif}
{ TCustomHTTPApplication }
function TCustomHTTPApplication.GetAllowConnect: TConnectQuery;
begin
Result:=HTTPHandler.OnAllowConnect;
end;
function TCustomHTTPApplication.GetPort: Word;
begin
Result:=HTTPHandler.Port;
end;
function TCustomHTTPApplication.GetQueueSize: Word;
begin
Result:=HTTPHandler.QueueSize;
end;
function TCustomHTTPApplication.GetThreaded: Boolean;
begin
Result:=HTTPHandler.Threaded;
end;
procedure TCustomHTTPApplication.SetOnAllowConnect(const AValue: TConnectQuery);
begin
HTTPHandler.OnAllowConnect:=AValue;
end;
procedure TCustomHTTPApplication.SetPort(const AValue: Word);
begin
HTTPHandler.Port:=Avalue;
end;
procedure TCustomHTTPApplication.SetQueueSize(const AValue: Word);
begin
HTTPHandler.QueueSize:=Avalue;
end;
procedure TCustomHTTPApplication.SetThreaded(const AValue: Boolean);
begin
HTTPHandler.Threaded:=Avalue;
end;
function TCustomHTTPApplication.InitializeWebHandler: TWebHandler;
begin
Result:=TFPHTTPServerHandler.Create(Self);
end;
function TCustomHTTPApplication.HTTPHandler: TFPHTTPServerHandler;
begin
Result:=Webhandler as TFPHTTPServerHandler;
end;
{ TFPHTTPServerHandler }
procedure TFPHTTPServerHandler.HTTPHandleRequest(Sender: TObject;
var ARequest: TFPHTTPConnectionRequest;
var AResponse: TFPHTTPConnectionResponse);
begin
// Exceptions are handled by (Do)HandleRequest. It also frees the response/request
DoHandleRequest(ARequest,AResponse);
ARequest:=Nil;
AResponse:=Nil;
If Terminated then
FServer.Active:=False;
if Assigned(OnIdle) then
OnIdle(Self);
end;
function TFPHTTPServerHandler.GetAllowConnect: TConnectQuery;
begin
Result:=FServer.OnAllowConnect;
end;
function TFPHTTPServerHandler.GetPort: Word;
begin
Result:=FServer.Port;
end;
function TFPHTTPServerHandler.GetQueueSize: Word;
begin
Result:=FServer.QueueSize;
end;
function TFPHTTPServerHandler.GetThreaded: Boolean;
begin
Result:=FServer.Threaded;
end;
procedure TFPHTTPServerHandler.SetOnAllowConnect(const AValue: TConnectQuery);
begin
FServer.OnAllowConnect:=Avalue
end;
procedure TFPHTTPServerHandler.SetPort(const AValue: Word);
begin
FServer.Port:=Avalue
end;
procedure TFPHTTPServerHandler.SetQueueSize(const AValue: Word);
begin
FServer.QueueSize:=Avalue
end;
procedure TFPHTTPServerHandler.SetThreaded(const AValue: Boolean);
begin
FServer.Threaded:=AValue;
end;
function TFPHTTPServerHandler.WaitForRequest(out ARequest: TRequest;
out AResponse: TResponse): boolean;
begin
// Result:=inherited WaitForRequest(ARequest, AResponse);
end;
function TFPHTTPServerHandler.CreateServer: TEmbeddedHttpServer;
begin
Result:=TEmbeddedHttpServer.Create(Self);
end;
procedure TFPHTTPServerHandler.Run;
begin
Fserver.Active:=True;
end;
constructor TFPHTTPServerHandler.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FServer:=CreateServer;
FServer.OnRequest:=@HTTPHandleRequest;
end;
destructor TFPHTTPServerHandler.Destroy;
begin
FServer.Active:=False;
FreeAndNil(FServer);
inherited Destroy;
end;
end.
|