📄 httpprot.pas
字号:
{*_* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Author: Fran鏾is PIETTE
Creation: November 23, 1997
Version: 1.86
Description: THttpCli is an implementation for the HTTP protocol
RFC 1945 (V1.0), and some of RFC 2068 (V1.1)
Credit: This component was based on a freeware from by Andreas
Hoerstemeier and used with his permission.
andy@hoerstemeier.de http://www.hoerstemeier.com/index.htm
EMail: http://www.overbyte.be http://www.rtfm.be/fpiette
francois.piette@overbyte.be francois.piette@rtfm.be
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) 1997-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.
Quick Reference:
HTTP component can retrieve documents or files using HTTP protocol; that is
connect to a HTTP server also known as a webserver. It can also trigger a
CGI/ISAPI/NSAPI script and post data using either GET or POST method.
Syntax of an URL: protocol://[user[:password]@]server[:port]/path
Path can include data: question mark followed by URL encoded data.
HTTP component is either asynchonous (non-blocking) or synchonous (blocking).
Highest performance is when using asynchonous operation. This is the
recommended way to use HTTP component.
To request several URL simultaneously, use asynchronous operation and as much
HTTP components as you wants to request URLs. All requests will be executed
simultaneously without using multi-threading and without blocking your app.
Methods:
GetASync Asynchronous, non-blocking Get
Retrieve document or file specified by URL, without blocking.
OnRequestDone event trigered when finished. Use HTTP GET
method (data contained in URL)
PostASync Asynchronous, non-blocking Post
Retrieve document or file specified by URL, without blocking.
OnRequestDone event trigered when finished. Use HTTP POST
method (data contained in request stream)
HeadASync Asynchronous, non-blocking Head
Retrieve document or file header specified by URL, without
blocking. OnRequestDone event trigered when finished. Use HTTP
HEAD method.
Get Synchronous, blocking Get. Same as GetAsync, but blocks until
finished.
Post Synchronous, blocking Post. Same as PostAsync, but blocks until
finished.
Head Synchronous, blocking Head. Same as HeadAsync, but blocks until
finished.
Abort Immediately close communication.
Updates:
11/29/97 RcvdStream and SendStream properties moved to public section
11/30/97 Document name bug corrected
12/02/97 Removed bug occuring with terminating slash in docname
12/03/97 Added properties RcvdCount and SentCount to easily add a progress
bar feature (On receive, the ContentLength is initialized with the
value from the header. Update the progress bar in the OnDocData event,
or the OnSendData event).
Added the OnSendBegin, OnSendData and OnSendEnd events.
12/07/97 Corrected Head function to work as expected. Thanks to
R. Barry Jones <rbjones@therightside.demon.co.uk
29/12/97 V0.96 Added ModifiedSince property as following proposition made by
Aw Kong Koy" <infomap@tm.net.my>.
30/12/97 V0.97 Added a Cookie property to send cookies
11/01/98 V0.98 Added WSocket read-only property which enable to access the
socket component used internally. For example to close it to abort
a connection.
13/01/98 V0.99 Added MultiThreaaded property to tell the component that it is
working in a thread and should take care of it.
15/01/98 V1.00 Completely revised internal working to make it work properly
with winsock 2. The TimeOut property is gone.
Changed OnAnswerLine event to OnHeaderData to be more consistent.
Replaced AnswserLine property by readonly LastResponse property.
Added OnRequestDone event. Added GetAsync, PostAsync, HeadAsync
asynchronous, non-blocking methods. Added Abort procedure.
16/01/98 V1.01 Corrected a bug which let some data be lost when receiving
(thanks to Fulvio J. Castelli <fulvio@rocketship.com>)
Added test for HTTP/1.1 response in header.
31/01/98 V1.02 Added an intermediate message posting for the OnRequestDone
event. Thanks to Ed Hochman <ed@mbhsys.com> for his help.
Added an intermediate PostMessage to set the component to ready state.
04/02/98 V1.03 Added some code to better handle DocName (truncating at the
first question mark).
05/02/98 V1.04 Deferred login after a relocation, using WM_HTTP_LOGIN message.
Added workarounf to support faulty webservers which sent only a single
LF in header lines. Submitted by Alwin Hoogerdijk <alwin@lostboys.nl>
15/03/98 V1.05 Enlarge buffers from 2048 to 8192 bytes (not for D1)
01/04/98 V1.06 Adapted for BCB V3
13/04/98 V1.07 Made RcvdHeader property readonly and cleared the content at the
start of a request.
Protected Abort method from calling when component is ready.
Ignore any exception triggered by CancelDnsLookup in Abort method.
14/04/98 V1.08 Corrected a relocation bug occuring with relative path
26/04/98 V1.09 Added OnLocationChange event
30/04/98 V1.10 Added ProxyUsername and ProxyPassword. Suggested by
Myers, Mike <MikeMy@crt.com>.
26/05/98 V1.11 Corrected relocation problem when used with ASP webpages
09/07/98 V1.12 Adapted for Delphi 4
Checked argument length in SendCommand
19/09/98 V1.13 Added support for HTML document without header
Added OnSessionConnected event, httpConnected state and
httpDnsLookupDone state.
Corrected a problem with automatic relocation. The relocation
message was included in data, resulting in wrong document data.
Added two new events: OnRequestHeaderBegin and OnRequestHeaderEnd.
They replace the OnHeaderBegin and OnHeaderEnd events that where
called for both request header (to web server) and response
header (from web server)
22/11/98 V1.14 Added a Location property than gives the new location in
case of page relocation. Suggested by Jon Robertson <touri@pobox.com>
21/12/98 V1.15 Set ContentLength equal to -1 at start of command.
31/01/99 V1.16 Added HostName property
01/02/99 V1.17 Port was lost in DoRequestAsync when using a proxy.
Thanks to David Wright <wrightd@gamespy.com> for his help.
Report Dns lookup error and session connect error in OnrequestDOne
event handler as suggested by Jack Olivera <jack@token.nl>.
14/03/99 V1.18 Added OnCookie event.
16/03/99 V1.19 Added Accept property.
Added a default value to Agent property.
Changed OnCookie event signature (not fully implemented yet !).
07/05/99 V1.20 Added code to support Content Ranges by Jon Robertson
<touri@pobox.com>.
24/07/99 V1.21 Yet another change in relocation code.
Aug 20, 1999 V1.22 Changed conditional compilation so that default is same
as latest compiler (currently Delphi 4, Bcb 4). Should be ok for
Delphi 5. Added Sleep(0) in sync wait loop to reduce CPU usage.
Added DnsResult property as suggested by Heedong Lim
<hdlim@dcenlp.chungbuk.ac.kr>. This property is accessible from
OnStateChange when state is httpDnsLookupDone.
Triggered OnDocData after writing to the stream.
Sep 25, 1999 V1.23 Yet another change in relocation code when using proxy
Francois Demers <fdemers@videotron.ca> found that some webserver
do not insert a space after colon in header line. Corrected
code to handle it correctly.
Cleared ContentType before issuing request.
Oct 02, 1999 V1.24 added AcceptRanges property. Thanks to Werner Lehmann
<wl@bwl.uni-kiel.de>
Oct 30, 1999 V1.25 change parameter in OnCommand event from const to var to
allow changing header line, including deleting or adding before
or after a given line sent by the component.
Nov 26, 1999 V1.26 Yet another relocation fix !
Jun 23, 2000 V1.27 Fixed a bug in ParseURL where hostname is followed by a '?'
(that is no path but a query).
Jul 22, 2000 V1.28 Handle exception during DnsLookup from the login procedure.
Suggested by Robert Penz <robert.penz@outertech.com>
Sep 17, 2000 V1.29 Eugene Mayevski <Mayevski@eldos.org> added support for
NOFORMS.
Jun 18, 2001 V1.30 Use AllocateHWnd and DeallocateHWnd from wsocket.
Renamed property WSocket to CtrlSocket (this require code change
in user application too).
Jul 25, 2001 V1.31 Danny Heijl <Danny.Heijl@cevi.be> found that ISA proxy adds
an extra space to the Content-length header so we need a trim
to extract numeric value.
Ran Margalit <ran@margalit.com> found some server sending
empty document (Content-Length = 0) which crashed the component.
Added a check for that case when header is finished.
Andrew N.Silich" <silich@rambler.ru> found we need to handle
handle relative path using "../" and "./" when relocating. Thanks
for his code which was a good starting point.
Jul 28, 2001 V1.32 Sahat Bun <sahat@operamail.com> suggested to change POST to
GET when a relocation occurs.
Created InternalClear procedure as suggested by Frank Plagge
<frank@plagge.net>.
When relocation, clear FRcvdHeader. If port not specified, then
use port 80. By Alexander O.Kazachkin <kao@inreco.ru>
Jul 30, 2001 V1.33 Corected a few glitches with Delphi 1
Aug 18, 2001 V1.34 Corrected a bug in relocation logic: when server send only a
header, with no document at all, relocation was not occuring and
OnHeaderEnd event was not triggered.
Corrected a bug in document name when a CGI was invoked (a '?'
found in the URL). Now, ignore everything after '?' which is CGI
parameter.
Sep 09, 2001 V1.35 Beat Boegli <leeloo999@bluewin.ch> added LocalAddr property
for multihomed hosts.
Sep 29, 2001 V1.36 Alexander Alexishin <sancho@han.kherson.ua> corrected
ParseUrl to handle the case where http:// is not at start of url:
'first.domain.com/cgi-bin/serv?url=http://second.domain.com'
Yet another relocation code change.
Oct 28, 2001 V1.37 Corrected SocketSessionClosed which called
LocationSessionClosed when it was not needed.
Nov 10, 2001 V1.38 Fixed a bug where component was trying to connect to proxy
using default port instead on specified port after a relocation.
Corrected a bug when relocating to a relative path. Current path
was not taken into account !
Mar 06, 2002 V1.39 Fixed a bug in relocation when content-length was 0: no
relocation occured ! (Just check for relocation before checking
content length empty).
Mar 12, 2002 V1.40 Added UrlEncode and UrlDecode utility functions.
Mar 30, 2002 V1.41 Export a few utility functions: IsDigit, IsXDigit, XDigit,
htoin and htoi2.
Apr 14, 2002 V1.42 Paolo S. Asioli <paolo.asioli@libero.it> found a bug in
relocation code where new user/pass are specified.
On relocation, change DocName according to the relocation.
When DocName has no extension and ContentType is text/html the
add extension .htm (could be expanded do other content type)
Apr 20, 2002 V1.43 Added Socks code from Eugene Mayevski <mayevski@eldos.org>
Apr 21, 2002 V1.44 In LocationSessionClosed, clear status variables from
previous operation.
Sep 06, 2002 V1.45 Made a few more methods virtual.
Sep 10, 2002 V1.46 Added AcceptLanguage property.
Sep 11, 2002 V1.47 Wilfried Mestdagh <wilfried@mestdagh.biz> added
OnBeforeHeaderSend event to help add/remove/change header lines.
He also corrected SocketSessionClosed to report error code.
Feb 08, 2003 V1.48 Implemented more HTTP/1.1 features notably TCP session
persistance.
Feb 22, 2003 V1.49 Corrected a bug related to document length computation.
Thanks to Dav999 <transmaster@ifrance.com> who found a
reproductible case with perso.wanadoo.fr webserver.
Apr 27, 2003 V1.50 OnLocationChange was not called when a relocation occured
and server handle HTTP/1.1 and new location on same server.
May 01, 2003 V1.51 Location and URL properties where incorrect after relocation
to same HTTP/1.1 server.
Change POST to GET after relocation to same HTTP/1.1 server.
May 09, 2003 V1.52 Implemented PUT method
May 31, 2003 V1.53 Corrected a problem with relocation when a proxy was used
and a relative path was given.
Aug 21, 2003 V1.54 Removed HTTPCliDeallocateHWnd virtual attribute for BCB
because of a bug in BCB preventing linking any derived
component with a HWND argument in a virtual method.
With help from Steven S. Showers and Stanislav Korotky.
Nov 26, 2003 V1.55 Implemented OnDataPush event for server push handling.
Added OnSessionClosed event.
Corrected ParseUrl to correctly handle protocol specified in
uppercase. Thanks to "Nu Conteaza" <osfp@personal.ro>.
Implemented OnDataPush2 event. Not the same as OnDataPush:
OnDataPush: Need to call Receive to get data
OnDataPush2: Data already received and delivered in LastResponse
Dec 28, 2003 V1.56 Implemented TransferEncoding = chunked
Moved code for relocation after document receive so that even in
case of a relocation, any available document is preserved.
Thanks to Steve Endicott <Endi@pacbell.net> for his help.
Jan 09, 2004 V1.57 Fixed a relocation not done when end of document is in the
same data packet as the last header line. With help of S. Endicott.
Steve Endicott <Endi@pacbell.net> implemented "Connection: close"
header line.
Jan 12, 2004 V1.58 "Ted T鴕aasen" <Ted@extreme.no> added proprety
FollowRelocation (default to TRUE) to have the component follow
relocation or just ignore them.
Jan 15, 2004 V1.59 Set FRcvdCount to zero in StartRelocation (S. Endicott).
Started to implement NTLM authentication. Doesn't work yet !
Jan 26, 2004 V1.60 Reordered uses clause for FPC compatibility.
Feb 16, 2004 V1.61 Fixed GetHeaderLineNext to start relocation at the right
moment. See annotation "16/02/2004".
Mar 12, 2004 Fixed GetHeaderLineNext to check for StatusCode < 200, 204 and
304 in order to not wait for body.
Thanks to Csonka Tibor <bee@rawbite.ro> for finding a test case.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -