📄 httpsrv.pas
字号:
{*_* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Author: Fran鏾is PIETTE
Description: THttpServer implement the HTTP server protocol, that is a
web server kernel.
You must implement sucurity yourself, specially, check the URL or
document path for valid values in your application. Be sure to
check for '..\', '.\', drive designation and UNC.
Do the check in OnGetDocument and similar event handlers.
Creation: Oct 10, 1999
Version: 1.33
EMail: francois.piette@overbyte.be http://www.overbyte.be
francois.piette@rtfm.be http://www.rtfm.be/fpiette
francois.piette@pophost.eunet.be
Support: Use the mailing list twsocket@elists.org
Follow "support" link at http://www.overbyte.be for subscription.
Legal issues: Copyright (C) 1999-2005 by Fran鏾is PIETTE
Rue de Grady 24, 4053 Embourg, Belgium. Fax: +32-4-365.74.56
<francois.piette@overbyte.be>
This software is provided 'as-is', without any express or
implied warranty. In no event will the author be held liable
for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it
and redistribute it freely, subject to the following
restrictions:
1. The origin of this software must not be misrepresented,
you must not claim that you wrote the original software.
If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but is
not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
4. You must register this software by sending a picture postcard
to the author. Use a nice stamp and mention your name, street
address, EMail address and any comment you like to say.
History:
Nov 12, 1999 Beta 3 Added Linger properties
Apr 23, 2000 Beta 4 Added Delphi 1 compatibility
Made everything public in THttpConnection because BCB has problems
when deriving a component from Delphi and protected functions.
Oct 29, 2000 Beta 5 Added Client[] property and IsClient() method.
Nov 11, 2000 Beta 6 Added code from Sven <schmidts@cdesign.de> to set
Last-Modified header line. Need some more changes !
Nov 12, 2000 Beta 7 Finished Last-Modified implementation.
Corrected TriggerServerStopped.
Jun 18, 2001 V1.01 Check if still connected before sending any data.
Jul 31, 2001 V1.02 Handle URL encoded document file (for example when there is
a space in the file name). Bug reported by Stian Gr鴑land
<joepezt@berzerk.net>.
Better handling of non existant documents or documents with invalid
file name.
Jan 13, 2002 V1.03 Changed SetPort() to SetPortValue() to avoid a conflict with
BCB6 which has a macro to replace SetPort with SetPortA !
Apr 15, 2002 V1.04 In SendDocument, avoid calling Send(nil, 0) when a document
has to be sent. Bug and fix by Adam Laforge <goodge@goodge.ca>.
Jul 06, 2002 V1.05 Thomas Smyth <smythtp@netscape.net> fixed
ExtractURLEncodedValue which had problem with names beginning with
same sequence of chars.
Oct 12, 2002 V1.06 Added THttpConnection.PostedDataReceived to be called from
OnPostedData event handler when application has received all data
from client.
Added read/only property SrcVersion with component source version
Jan 05, 2003 V1.07 Fixed isxdigit
Feb 11, 2003 V1.08 Enhanced HTTP1.1 handling (persitant connexions)
Aug 28, 2003 V1.09 Stanislav Korotky <editor@tbsoft.ru> added the
OnHttpRequestDone event and related stuff.
Oct 25, 2003 V1.10 Added a space before 'GMT' in Lats-Modified header line.
Oct 31, 2003 V1.11 Changed ProcessRequest so that empty FPath doesn't cause
any problem.
Dec 17, 2003 V1.12 Added THttpConnection.FServer field
Jan 03, 2004 V1.13 Added directory list feature, Options property, utility
functions TextToHtmlText, TranslateChar, UnixPathToDosPath,
DosPathToUnixPath, IsDirectory, UrlDecode, AbsolutisePath.
Removed useless units in the uses clause.
Added hoAllowDirList and hoAllowOutsideRoot options.
Added '.' in non encoded chars in UrlEncode
Added hg403 flag and Answer403 response.
Jan 12, 2004 V1.14 "David A. G." <davidag@arnet.com.ar> added
- Basic authorization support (Authorization header line)
- Icon support "image/x-icon"
- Property MaxClients
- Simple WAP documents support
Ivan Turcan <iturcan@drake-it.com>Added is Content-Range support
by Http 1.1 specs. Still buggy ! Need to check end of range.
Jan 15, 2004 V1.15 Ivan Turcan code fixed.
Conditional compilation with "UseInt64ForHttpRange" symbol to
allow using 64 bit integer for HTTP ranges
Jan 16, 2004 V1.16 SendStream reset FDocstream position to start of stream.
Added SendHeader method to make things more clear.
David A.G. Fixed a bug related to sending last stream chunk.
Jan 17, 2004 V1.18 David A. G. <davidag@arnet.com.ar> implemented multi-host
feature. That is added properties RequestHostName and
RequestHostPort as well as event OnBeforeProcessRequest. From this
event you can check RequestHostName and change DocDir and
DefaultDoc appropriately.
Added a CRLF at the end of the line generated by MakeCookie.
Jan 26, 2004 V1.19 Reordered uses for FPC compatibility.
Added RequestCookies with the cookies included in the request.
Mar 27, 2004 V1.20 Added THttpConnection.AnswerPage method.
Apr 09, 2004 V1.21 Added method AnswerStream.
Renamed Answer() to AnswerString() to make it similar to
AnswerPage and AnswerStream.
May 11, 2004 V1.22 Added text/css check in DocumentToContentType
May 15, 2004 V1.23 Jack (jlist@softhome.net) added better support for HTTP/1.0
persitant connection
May 30, 2004 V1.24 Enhanced byte-ranges support. Thanks to Andreas Hofmann
<andreas.hofmann@docware.de> for his code which has been optimized
and revised to work with older Delphi versions.
Jun 06, 2004 V1.25 Andreas Hofmann fixed a memory leak in the byte-ranges code
implemented in V1.24. He also replaced THttpPartStreamRecord by an
object THttpPartStream.
Jun 20, 2004 V1.26 THttpRangeList.Clear is not virtual in D3. (A. Garrels)
Jul 18, 2004 V1.27 David A. G. <davidag@arnet.com.ar> revised keep-alive
handling. See SendDocument and ConnectionDataSent.
Aug 30, 2004 V1.28 Added support function RemoveHtmlSpecialChars which replace
characters used as delimiter in HTML by their equivalent code:
'<', '>', '&', '''', '"' are replaced by
'<', '>', '&', ''', '"'
Added THttpSrv.SetDocDir (setter function for DocDir property) so
that FDocDir is never terminated by a '\'.
Changed THttpConnection.ProcessRequest to convert FPath to DOS
type path.
Sep 12, 2004 V1.29 Made WSocketServer runtime property available.
Jan 08, 2005 V1.30 In TextToHtmlText, replace multiple spaces by a single space
followed by the required number of non-breaking-spaces ( )
Replace TAB by a non-breaking-space.
Added methods HtmlPageProducerToString and HtmlPageProducerToStream
which ease the generation of dynamic pages from several templates.
Jan 14, 2005 V1.31 Added function HtmlPageProducerSetTagPrefix which allows
to change the delimiter used in the various HtmlPageProducer
functions. You can now change the '#' to something line 'ics:'
which is better for XHTML validation tools provided you have a
namespace defining the '<ics:somename>' tags. By default the
delimiter is still '#' and no change is needed in the existing
code. The HtmlPageProducer functions also accept a better form
of the ending TABLE_ROWS delimiter (for XHTML compatibility). You
can now use "</ics:TABLE_ROWS>" ("ics:" being whatever you selected
with HtmlPageProducerSetTagPrefix and begin '#' by default).
Mar 07, 2005 V1.32 Added Answer501 method and use it to reply to unimplemented
methods as suggested by M. Terrisse mterrisse@index-education.fr
Mar 13, 2005 V1.33 Changed define symbol for stream supporting 64 bits seek.
Now use STREAM64 symbol. See ICSDEFS.INC where it is defined.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
unit HttpSrv;
{$B-} { Enable partial boolean evaluation }
{$T-} { Untyped pointers }
{$X+} { Enable extended syntax }
{$I ICSDEFS.INC}
{$IFDEF DELPHI6_UP}
{$WARN SYMBOL_PLATFORM OFF}
{$WARN SYMBOL_LIBRARY OFF}
{$WARN SYMBOL_DEPRECATED OFF}
{$ENDIF}
{$IFDEF COMPILER2_UP} { Not for Delphi 1 }
{$H+} { Use long strings }
{$J+} { Allow typed constant to be modified }
{$ENDIF}
{$IFDEF BCB3_UP}
{$ObjExportAll On}
{$ENDIF}
{ DEFINE USE_ZLIB} { Experimental code, doesn't work yet }
interface
uses
Messages,
{$IFDEF USEWINDOWS}
Windows,
{$ELSE}
WinTypes, WinProcs,
{$ENDIF}
SysUtils, Classes,
{ You must define USE_SSL so that SSL code is included in the component. }
{ To be able to compile the component, you must have the SSL related files }
{ which are _NOT_ freeware. See http://www.overbyte.be for details. }
{$IFDEF USE_SSL}
IcsSSLEAY, IcsLIBEAY,
{$ENDIF}
{$IFDEF USE_ZLIB}
dZLib, zDeflate, ZLibh,
{$ENDIF}
WSocket, WSocketS;
const
THttpServerVersion = 133;
CopyRight : String = ' THttpServer (c) 1999-2005 F. Piette V1.33 ';
WM_HTTP_DONE = WM_USER + 40;
type
THttpServer = class;
THttpConnection = class;
THttpConnectionClass = class of THttpConnection;
THttpDirEntry = class;
TStringIndex = class;
THttpGetFlag = (hgSendDoc, hgSendStream, hgWillSendMySelf,
hg404, hg403, hg401, hgAcceptData, hgSendDirList);
THttpSendType = (httpSendHead, httpSendDoc);
THttpGetEvent = procedure (Sender : TObject;
Client : TObject;
var Flags : THttpGetFlag) of object;
THttpGetConnEvent = procedure (Sender : TObject;
var Flags : THttpGetFlag) of object;
THttpConnectEvent = procedure (Sender : TObject;
Client : TObject;
Error : Word) of object;
THttpPostedDataEvent = procedure (Sender : TObject;
Client : TObject;
Error : Word) of object;
THttpRequestDoneEvent= procedure (Sender : TObject;
Client : TObject) of object;
THttpBeforeProcessEvent= procedure (Sender : TObject; {DAVID}
Client : TObject) of object;
THttpFilterDirEntry = procedure (Sender : TObject;
Client : TObject;
DirEntry : THttpDirEntry) of object;
THttpGetRowDataEvent = procedure (Sender : TObject;
const TableName : String;
Row : Integer;
TagData : TStringIndex;
var More : Boolean;
UserData : TObject) of object;
THttpConnectionState = (hcRequest, hcHeader, hcPostedData);
THttpOption = (hoAllowDirList, hoAllowOutsideRoot);
THttpOptions = set of THttpOption;
{$IFDEF UseInt64ForHttpRange}
THttpRangeInt = Int64;
{$ELSE}
THttpRangeInt = LongInt; { Limited to 2GB size }
{$ENDIF}
{ANDREAS one byte-range}
THttpRange = class( TObject)
private
FRangeTo : THttpRangeInt;
FRangeFrom : THttpRangeInt;
procedure SetRangeFrom(const Value: THttpRangeInt);
procedure SetRangeTo(const Value: THttpRangeInt);
public
constructor Create;
procedure Assign(Source: THttpRange);
function GetContentRangeString(CompleteDocSize : THttpRangeInt): String;
property RangeFrom : THttpRangeInt read FRangeFrom write SetRangeFrom;
property RangeTo : THttpRangeInt read FRangeTo write SetRangeTo;
end;
{ANDREAS list of byte-ranges}
THttpRangeList = class(TList)
private
function GetItems(NIndex: Integer): THttpRange;
procedure SetItems(NIndex: Integer; const Value: THttpRange);
public
destructor Destroy; override;
procedure Clear; {$IFDEF DELPHI4_UP}override; {$ENDIF}
procedure Assign(Source: THttpRangeList);
function CreateRangeStream(SourceStream : TStream;
ContentString : String;
CompleteDocSize : THttpRangeInt): TStream;
function Valid: Boolean;
procedure InitFromString(AStr: String);
property Items[NIndex: Integer]: THttpRange read GetItems
write SetItems;
end;
THttpPartStream = class(TObject)
Stream : TStream;
StartPos : THttpRangeInt;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -