2008-04-23 23:26:50 +03:00
|
|
|
{==============================================================================|
|
2008-04-24 10:23:38 +03:00
|
|
|
| Project : Delphree - Synapse | 003.006.004 |
|
2008-04-23 23:26:50 +03:00
|
|
|
|==============================================================================|
|
2008-04-24 09:42:13 +03:00
|
|
|
| Content: HTTP client |
|
2008-04-23 23:26:50 +03:00
|
|
|
|==============================================================================|
|
2008-04-24 10:22:17 +03:00
|
|
|
| Copyright (c)1999-2003, Lukas Gebauer |
|
2008-04-24 10:20:39 +03:00
|
|
|
| All rights reserved. |
|
2008-04-23 23:26:50 +03:00
|
|
|
| |
|
2008-04-24 10:20:39 +03:00
|
|
|
| Redistribution and use in source and binary forms, with or without |
|
|
|
|
| modification, are permitted provided that the following conditions are met: |
|
|
|
|
| |
|
|
|
|
| Redistributions of source code must retain the above copyright notice, this |
|
|
|
|
| list of conditions and the following disclaimer. |
|
|
|
|
| |
|
|
|
|
| Redistributions in binary form must reproduce the above copyright notice, |
|
|
|
|
| this list of conditions and the following disclaimer in the documentation |
|
|
|
|
| and/or other materials provided with the distribution. |
|
|
|
|
| |
|
|
|
|
| Neither the name of Lukas Gebauer nor the names of its contributors may |
|
|
|
|
| be used to endorse or promote products derived from this software without |
|
|
|
|
| specific prior written permission. |
|
|
|
|
| |
|
|
|
|
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
|
|
|
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
|
|
|
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
|
|
|
| ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR |
|
|
|
|
| ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
|
|
|
|
| DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
|
|
|
|
| SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
|
|
|
|
| CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
|
|
|
|
| LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
|
|
|
|
| OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
|
|
|
|
| DAMAGE. |
|
2008-04-23 23:26:50 +03:00
|
|
|
|==============================================================================|
|
|
|
|
| The Initial Developer of the Original Code is Lukas Gebauer (Czech Republic).|
|
2008-04-24 10:22:17 +03:00
|
|
|
| Portions created by Lukas Gebauer are Copyright (c) 1999-2003. |
|
2008-04-23 23:26:50 +03:00
|
|
|
| All Rights Reserved. |
|
|
|
|
|==============================================================================|
|
|
|
|
| Contributor(s): |
|
|
|
|
|==============================================================================|
|
|
|
|
| History: see HISTORY.HTM from distribution package |
|
2008-04-23 23:48:39 +03:00
|
|
|
| (Found at URL: http://www.ararat.cz/synapse/) |
|
2008-04-23 23:26:50 +03:00
|
|
|
|==============================================================================}
|
|
|
|
|
2008-04-24 09:59:26 +03:00
|
|
|
unit HTTPSend;
|
2008-04-23 23:26:50 +03:00
|
|
|
|
|
|
|
interface
|
2008-04-24 10:05:26 +03:00
|
|
|
|
2008-04-23 23:26:50 +03:00
|
|
|
uses
|
2008-04-24 10:05:26 +03:00
|
|
|
SysUtils, Classes,
|
2008-04-24 10:23:38 +03:00
|
|
|
{$IFDEF STREAMSEC}
|
|
|
|
TlsInternalServer, TlsSynaSock,
|
|
|
|
{$ENDIF}
|
2008-04-24 10:05:26 +03:00
|
|
|
blcksock, SynaUtil, SynaCode;
|
2008-04-23 23:26:50 +03:00
|
|
|
|
|
|
|
const
|
2008-04-24 10:05:26 +03:00
|
|
|
cHttpProtocol = '80';
|
2008-04-23 23:26:50 +03:00
|
|
|
|
|
|
|
type
|
2008-04-24 10:05:26 +03:00
|
|
|
TTransferEncoding = (TE_UNKNOWN, TE_IDENTITY, TE_CHUNKED);
|
2008-04-24 09:46:22 +03:00
|
|
|
|
2008-04-24 10:20:39 +03:00
|
|
|
THTTPSend = class(TSynaClient)
|
2008-04-23 23:26:50 +03:00
|
|
|
private
|
2008-04-24 10:23:38 +03:00
|
|
|
{$IFDEF STREAMSEC}
|
|
|
|
FSock: TSsTCPBlockSocket;
|
|
|
|
FTLSServer: TCustomTLSInternalServer;
|
|
|
|
{$ELSE}
|
2008-04-24 10:05:26 +03:00
|
|
|
FSock: TTCPBlockSocket;
|
2008-04-24 10:23:38 +03:00
|
|
|
{$ENDIF}
|
2008-04-24 10:05:26 +03:00
|
|
|
FTransferEncoding: TTransferEncoding;
|
|
|
|
FAliveHost: string;
|
|
|
|
FAlivePort: string;
|
|
|
|
FHeaders: TStringList;
|
|
|
|
FDocument: TMemoryStream;
|
|
|
|
FMimeType: string;
|
|
|
|
FProtocol: string;
|
|
|
|
FKeepAlive: Boolean;
|
2008-04-24 10:23:38 +03:00
|
|
|
FStatus100: Boolean;
|
2008-04-24 10:05:26 +03:00
|
|
|
FProxyHost: string;
|
|
|
|
FProxyPort: string;
|
|
|
|
FProxyUser: string;
|
|
|
|
FProxyPass: string;
|
|
|
|
FResultCode: Integer;
|
|
|
|
FResultString: string;
|
2008-04-24 10:22:17 +03:00
|
|
|
FUserAgent: string;
|
|
|
|
FCookies: TStringList;
|
|
|
|
FDownloadSize: integer;
|
|
|
|
FUploadSize: integer;
|
|
|
|
FRangeStart: integer;
|
|
|
|
FRangeEnd: integer;
|
2008-04-24 10:05:26 +03:00
|
|
|
function ReadUnknown: Boolean;
|
|
|
|
function ReadIdentity(Size: Integer): Boolean;
|
|
|
|
function ReadChunked: Boolean;
|
2008-04-24 10:22:17 +03:00
|
|
|
procedure ParseCookies;
|
2008-04-23 23:26:50 +03:00
|
|
|
public
|
2008-04-24 10:05:26 +03:00
|
|
|
constructor Create;
|
|
|
|
destructor Destroy; override;
|
|
|
|
procedure Clear;
|
|
|
|
procedure DecodeStatus(const Value: string);
|
|
|
|
function HTTPMethod(const Method, URL: string): Boolean;
|
2008-04-24 10:22:17 +03:00
|
|
|
procedure Abort;
|
2008-04-24 10:05:26 +03:00
|
|
|
published
|
2008-04-24 10:22:17 +03:00
|
|
|
property Headers: TStringList read FHeaders;
|
|
|
|
property Cookies: TStringList read FCookies;
|
|
|
|
property Document: TMemoryStream read FDocument;
|
|
|
|
property RangeStart: integer read FRangeStart Write FRangeStart;
|
|
|
|
property RangeEnd: integer read FRangeEnd Write FRangeEnd;
|
2008-04-24 10:05:26 +03:00
|
|
|
property MimeType: string read FMimeType Write FMimeType;
|
|
|
|
property Protocol: string read FProtocol Write FProtocol;
|
|
|
|
property KeepAlive: Boolean read FKeepAlive Write FKeepAlive;
|
2008-04-24 10:23:38 +03:00
|
|
|
property Status100: Boolean read FStatus100 Write FStatus100;
|
2008-04-24 10:05:26 +03:00
|
|
|
property ProxyHost: string read FProxyHost Write FProxyHost;
|
|
|
|
property ProxyPort: string read FProxyPort Write FProxyPort;
|
|
|
|
property ProxyUser: string read FProxyUser Write FProxyUser;
|
|
|
|
property ProxyPass: string read FProxyPass Write FProxyPass;
|
2008-04-24 10:22:17 +03:00
|
|
|
property UserAgent: string read FUserAgent Write FUserAgent;
|
2008-04-24 10:05:26 +03:00
|
|
|
property ResultCode: Integer read FResultCode;
|
|
|
|
property ResultString: string read FResultString;
|
2008-04-24 10:22:17 +03:00
|
|
|
property DownloadSize: integer read FDownloadSize;
|
|
|
|
property UploadSize: integer read FUploadSize;
|
2008-04-24 10:23:38 +03:00
|
|
|
{$IFDEF STREAMSEC}
|
|
|
|
property Sock: TSsTCPBlockSocket read FSock;
|
|
|
|
property TLSServer: TCustomTLSInternalServer read FTLSServer write FTLSServer;
|
|
|
|
{$ELSE}
|
2008-04-24 10:07:45 +03:00
|
|
|
property Sock: TTCPBlockSocket read FSock;
|
2008-04-24 10:23:38 +03:00
|
|
|
{$ENDIF}
|
2008-04-23 23:26:50 +03:00
|
|
|
end;
|
|
|
|
|
2008-04-24 10:05:26 +03:00
|
|
|
function HttpGetText(const URL: string; const Response: TStrings): Boolean;
|
|
|
|
function HttpGetBinary(const URL: string; const Response: TStream): Boolean;
|
|
|
|
function HttpPostBinary(const URL: string; const Data: TStream): Boolean;
|
|
|
|
function HttpPostURL(const URL, URLData: string; const Data: TStream): Boolean;
|
2008-04-24 10:07:45 +03:00
|
|
|
function HttpPostFile(const URL, FieldName, FileName: string;
|
2008-04-24 10:20:39 +03:00
|
|
|
const Data: TStream; const ResultData: TStrings): Boolean;
|
2008-04-23 23:26:50 +03:00
|
|
|
|
|
|
|
implementation
|
|
|
|
|
2008-04-24 10:05:26 +03:00
|
|
|
constructor THTTPSend.Create;
|
2008-04-23 23:26:50 +03:00
|
|
|
begin
|
|
|
|
inherited Create;
|
2008-04-24 10:05:26 +03:00
|
|
|
FHeaders := TStringList.Create;
|
2008-04-24 10:22:17 +03:00
|
|
|
FCookies := TStringList.Create;
|
2008-04-24 10:05:26 +03:00
|
|
|
FDocument := TMemoryStream.Create;
|
2008-04-24 10:23:38 +03:00
|
|
|
{$IFDEF STREAMSEC}
|
|
|
|
FTLSServer := GlobalTLSInternalServer;
|
|
|
|
FSock := TSsTCPBlockSocket.Create;
|
|
|
|
FSock.BlockingRead := True;
|
|
|
|
{$ELSE}
|
2008-04-24 10:05:26 +03:00
|
|
|
FSock := TTCPBlockSocket.Create;
|
2008-04-24 10:23:38 +03:00
|
|
|
{$ENDIF}
|
2008-04-24 10:22:17 +03:00
|
|
|
FSock.ConvertLineEnd := True;
|
2008-04-24 10:05:26 +03:00
|
|
|
FSock.SizeRecvBuffer := 65536;
|
|
|
|
FSock.SizeSendBuffer := 65536;
|
2008-04-24 10:23:38 +03:00
|
|
|
FTimeout := 90000;
|
2008-04-24 10:20:39 +03:00
|
|
|
FTargetPort := cHttpProtocol;
|
2008-04-24 10:05:26 +03:00
|
|
|
FProxyHost := '';
|
|
|
|
FProxyPort := '8080';
|
|
|
|
FProxyUser := '';
|
|
|
|
FProxyPass := '';
|
|
|
|
FAliveHost := '';
|
|
|
|
FAlivePort := '';
|
2008-04-24 10:18:26 +03:00
|
|
|
FProtocol := '1.0';
|
2008-04-24 10:05:26 +03:00
|
|
|
FKeepAlive := True;
|
2008-04-24 10:23:38 +03:00
|
|
|
FStatus100 := False;
|
2008-04-24 10:22:17 +03:00
|
|
|
FUserAgent := 'Mozilla/4.0 (compatible; Synapse)';
|
|
|
|
FDownloadSize := 0;
|
|
|
|
FUploadSize := 0;
|
2008-04-24 09:46:22 +03:00
|
|
|
Clear;
|
2008-04-23 23:26:50 +03:00
|
|
|
end;
|
|
|
|
|
2008-04-24 10:05:26 +03:00
|
|
|
destructor THTTPSend.Destroy;
|
2008-04-23 23:26:50 +03:00
|
|
|
begin
|
2008-04-24 10:05:26 +03:00
|
|
|
FSock.Free;
|
|
|
|
FDocument.Free;
|
2008-04-24 10:22:17 +03:00
|
|
|
FCookies.Free;
|
2008-04-24 10:05:26 +03:00
|
|
|
FHeaders.Free;
|
|
|
|
inherited Destroy;
|
2008-04-23 23:26:50 +03:00
|
|
|
end;
|
|
|
|
|
2008-04-24 09:46:22 +03:00
|
|
|
procedure THTTPSend.Clear;
|
|
|
|
begin
|
2008-04-24 10:22:17 +03:00
|
|
|
FRangeStart := 0;
|
|
|
|
FRangeEnd := 0;
|
2008-04-24 10:05:26 +03:00
|
|
|
FDocument.Clear;
|
|
|
|
FHeaders.Clear;
|
|
|
|
FMimeType := 'text/html';
|
2008-04-24 09:46:22 +03:00
|
|
|
end;
|
|
|
|
|
2008-04-24 10:05:26 +03:00
|
|
|
procedure THTTPSend.DecodeStatus(const Value: string);
|
2008-04-23 23:26:50 +03:00
|
|
|
var
|
2008-04-24 10:05:26 +03:00
|
|
|
s, su: string;
|
2008-04-24 09:46:22 +03:00
|
|
|
begin
|
2008-04-24 10:05:26 +03:00
|
|
|
s := SeparateRight(Value, ' ');
|
|
|
|
su := SeparateLeft(s, ' ');
|
|
|
|
FResultCode := StrToIntDef(su, 0);
|
|
|
|
FResultString := SeparateRight(s, ' ');
|
|
|
|
if FResultString = s then
|
|
|
|
FResultString := '';
|
2008-04-24 09:46:22 +03:00
|
|
|
end;
|
|
|
|
|
2008-04-24 10:05:26 +03:00
|
|
|
function THTTPSend.HTTPMethod(const Method, URL: string): Boolean;
|
2008-04-24 09:46:22 +03:00
|
|
|
var
|
2008-04-24 10:05:26 +03:00
|
|
|
Sending, Receiving: Boolean;
|
|
|
|
status100: Boolean;
|
|
|
|
status100error: string;
|
|
|
|
ToClose: Boolean;
|
|
|
|
Size: Integer;
|
|
|
|
Prot, User, Pass, Host, Port, Path, Para, URI: string;
|
|
|
|
s, su: string;
|
2008-04-24 10:13:22 +03:00
|
|
|
HttpTunnel: Boolean;
|
2008-04-24 10:22:17 +03:00
|
|
|
n: integer;
|
2008-04-23 23:26:50 +03:00
|
|
|
begin
|
2008-04-24 09:46:22 +03:00
|
|
|
{initial values}
|
2008-04-24 10:05:26 +03:00
|
|
|
Result := False;
|
|
|
|
FResultCode := 500;
|
|
|
|
FResultString := '';
|
2008-04-24 10:22:17 +03:00
|
|
|
FDownloadSize := 0;
|
|
|
|
FUploadSize := 0;
|
2008-04-24 10:05:26 +03:00
|
|
|
|
|
|
|
URI := ParseURL(URL, Prot, User, Pass, Host, Port, Path, Para);
|
2008-04-24 10:13:22 +03:00
|
|
|
if UpperCase(Prot) = 'HTTPS' then
|
|
|
|
begin
|
|
|
|
HttpTunnel := FProxyHost <> '';
|
|
|
|
FSock.HTTPTunnelIP := FProxyHost;
|
|
|
|
FSock.HTTPTunnelPort := FProxyPort;
|
|
|
|
FSock.HTTPTunnelUser := FProxyUser;
|
|
|
|
FSock.HTTPTunnelPass := FProxyPass;
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
HttpTunnel := False;
|
|
|
|
FSock.HTTPTunnelIP := '';
|
|
|
|
FSock.HTTPTunnelPort := '';
|
|
|
|
FSock.HTTPTunnelUser := '';
|
|
|
|
FSock.HTTPTunnelPass := '';
|
|
|
|
end;
|
|
|
|
|
2008-04-24 10:05:26 +03:00
|
|
|
Sending := Document.Size > 0;
|
|
|
|
{Headers for Sending data}
|
2008-04-24 10:23:38 +03:00
|
|
|
status100 := FStatus100 and Sending and (FProtocol = '1.1');
|
2008-04-24 10:05:26 +03:00
|
|
|
if status100 then
|
|
|
|
FHeaders.Insert(0, 'Expect: 100-continue');
|
|
|
|
if Sending then
|
|
|
|
begin
|
|
|
|
FHeaders.Insert(0, 'Content-Length: ' + IntToStr(FDocument.Size));
|
|
|
|
if FMimeType <> '' then
|
|
|
|
FHeaders.Insert(0, 'Content-Type: ' + FMimeType);
|
|
|
|
end;
|
2008-04-24 10:22:17 +03:00
|
|
|
{ setting User-agent }
|
|
|
|
if FUserAgent <> '' then
|
|
|
|
FHeaders.Insert(0, 'User-Agent: ' + FUserAgent);
|
|
|
|
{ setting Ranges }
|
|
|
|
if FRangeEnd > 0 then
|
|
|
|
FHeaders.Insert(0, 'Range: bytes=' + IntToStr(FRangeStart) + '-' + IntToStr(FRangeEnd));
|
|
|
|
{ setting Cookies }
|
|
|
|
for n := 0 to FCookies.Count - 1 do
|
|
|
|
FHeaders.Insert(0, 'Cookie: ' + FCookies[n]);
|
2008-04-24 10:05:26 +03:00
|
|
|
{ setting KeepAlives }
|
|
|
|
if not FKeepAlive then
|
|
|
|
FHeaders.Insert(0, 'Connection: close');
|
2008-04-24 10:13:22 +03:00
|
|
|
{ set target servers/proxy, authorizations, etc... }
|
2008-04-24 10:05:26 +03:00
|
|
|
if User <> '' then
|
|
|
|
FHeaders.Insert(0, 'Authorization: Basic ' + EncodeBase64(user + ':' + pass));
|
2008-04-24 10:13:22 +03:00
|
|
|
if (FProxyHost <> '') and (FProxyUser <> '') and not(HttpTunnel) then
|
2008-04-24 10:05:26 +03:00
|
|
|
FHeaders.Insert(0, 'Proxy-Authorization: Basic ' +
|
|
|
|
EncodeBase64(FProxyUser + ':' + FProxyPass));
|
2008-04-24 10:23:38 +03:00
|
|
|
if isIP6(Host) then
|
|
|
|
s := '[' + Host + ']'
|
|
|
|
else
|
|
|
|
s := Host;
|
2008-04-24 10:09:13 +03:00
|
|
|
if Port<>'80' then
|
2008-04-24 10:23:38 +03:00
|
|
|
FHeaders.Insert(0, 'Host: ' + s + ':' + Port)
|
2008-04-24 10:09:13 +03:00
|
|
|
else
|
2008-04-24 10:23:38 +03:00
|
|
|
FHeaders.Insert(0, 'Host: ' + s);
|
2008-04-24 10:13:22 +03:00
|
|
|
if (FProxyHost <> '') and not(HttpTunnel)then
|
2008-04-24 10:23:38 +03:00
|
|
|
URI := Prot + '://' + s + ':' + Port + URI;
|
2008-04-24 10:05:26 +03:00
|
|
|
if URI = '/*' then
|
|
|
|
URI := '*';
|
|
|
|
if FProtocol = '0.9' then
|
|
|
|
FHeaders.Insert(0, UpperCase(Method) + ' ' + URI)
|
|
|
|
else
|
|
|
|
FHeaders.Insert(0, UpperCase(Method) + ' ' + URI + ' HTTP/' + FProtocol);
|
2008-04-24 10:13:22 +03:00
|
|
|
if (FProxyHost <> '') and not(HttpTunnel) then
|
2008-04-24 10:05:26 +03:00
|
|
|
begin
|
2008-04-24 10:20:39 +03:00
|
|
|
FTargetHost := FProxyHost;
|
|
|
|
FTargetPort := FProxyPort;
|
2008-04-24 10:05:26 +03:00
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
2008-04-24 10:20:39 +03:00
|
|
|
FTargetHost := Host;
|
|
|
|
FTargetPort := Port;
|
2008-04-24 10:05:26 +03:00
|
|
|
end;
|
|
|
|
if FHeaders[FHeaders.Count - 1] <> '' then
|
|
|
|
FHeaders.Add('');
|
|
|
|
|
|
|
|
{ connect }
|
2008-04-24 10:20:39 +03:00
|
|
|
if (FAliveHost <> FTargetHost) or (FAlivePort <> FTargetPort) then
|
2008-04-24 10:05:26 +03:00
|
|
|
begin
|
|
|
|
FSock.CloseSocket;
|
2008-04-24 10:20:39 +03:00
|
|
|
FSock.Bind(FIPInterface, cAnyPort);
|
2008-04-24 10:05:26 +03:00
|
|
|
if FSock.LastError <> 0 then
|
|
|
|
Exit;
|
2008-04-24 10:23:38 +03:00
|
|
|
{$IFDEF STREAMSEC}
|
|
|
|
FSock.TLSServer := nil;
|
|
|
|
if UpperCase(Prot) = 'HTTPS' then
|
|
|
|
if assigned(FTLSServer) then
|
|
|
|
FSock.TLSServer := FTLSServer
|
|
|
|
else
|
|
|
|
exit;
|
|
|
|
{$ELSE}
|
|
|
|
FSock.SSLEnabled := UpperCase(Prot) = 'HTTPS';
|
|
|
|
{$ENDIF}
|
|
|
|
if FSock.LastError <> 0 then
|
|
|
|
Exit;
|
2008-04-24 10:20:39 +03:00
|
|
|
FSock.Connect(FTargetHost, FTargetPort);
|
|
|
|
if FSock.LastError <> 0 then
|
|
|
|
Exit;
|
|
|
|
FAliveHost := FTargetHost;
|
|
|
|
FAlivePort := FTargetPort;
|
2008-04-24 10:05:26 +03:00
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
if FSock.CanRead(0) then
|
2008-04-24 09:46:22 +03:00
|
|
|
begin
|
2008-04-24 10:05:26 +03:00
|
|
|
FSock.CloseSocket;
|
2008-04-24 10:20:39 +03:00
|
|
|
FSock.Bind(FIPInterface, cAnyPort);
|
|
|
|
if FSock.LastError <> 0 then
|
|
|
|
Exit;
|
2008-04-24 10:23:38 +03:00
|
|
|
{$IFDEF STREAMSEC}
|
|
|
|
FSock.TLSServer := nil;
|
|
|
|
if UpperCase(Prot) = 'HTTPS' then
|
|
|
|
if assigned(FTLSServer) then
|
|
|
|
FSock.TLSServer := FTLSServer
|
|
|
|
else
|
|
|
|
exit;
|
|
|
|
{$ELSE}
|
|
|
|
FSock.SSLEnabled := UpperCase(Prot) = 'HTTPS';
|
|
|
|
{$ENDIF}
|
|
|
|
if FSock.LastError <> 0 then
|
|
|
|
Exit;
|
2008-04-24 10:20:39 +03:00
|
|
|
FSock.Connect(FTargetHost, FTargetPort);
|
2008-04-24 10:05:26 +03:00
|
|
|
if FSock.LastError <> 0 then
|
|
|
|
Exit;
|
2008-04-24 09:46:22 +03:00
|
|
|
end;
|
2008-04-24 10:05:26 +03:00
|
|
|
end;
|
2008-04-24 09:46:22 +03:00
|
|
|
|
2008-04-24 10:05:26 +03:00
|
|
|
{ send Headers }
|
2008-04-24 10:18:26 +03:00
|
|
|
if FProtocol = '0.9' then
|
|
|
|
FSock.SendString(FHeaders[0] + CRLF)
|
|
|
|
else
|
2008-04-24 10:20:39 +03:00
|
|
|
{$IFDEF LINUX}
|
|
|
|
FSock.SendString(AdjustLineBreaks(FHeaders.Text, tlbsCRLF));
|
|
|
|
{$ELSE}
|
2008-04-24 10:18:26 +03:00
|
|
|
FSock.SendString(FHeaders.Text);
|
2008-04-24 10:20:39 +03:00
|
|
|
{$ENDIF}
|
2008-04-24 10:05:26 +03:00
|
|
|
if FSock.LastError <> 0 then
|
|
|
|
Exit;
|
2008-04-24 09:46:22 +03:00
|
|
|
|
2008-04-24 10:05:26 +03:00
|
|
|
{ reading Status }
|
|
|
|
Status100Error := '';
|
2008-04-24 09:46:22 +03:00
|
|
|
if status100 then
|
2008-04-24 10:05:26 +03:00
|
|
|
begin
|
|
|
|
repeat
|
|
|
|
s := FSock.RecvString(FTimeout);
|
|
|
|
if s <> '' then
|
|
|
|
Break;
|
|
|
|
until FSock.LastError <> 0;
|
|
|
|
DecodeStatus(s);
|
|
|
|
if (FResultCode >= 100) and (FResultCode < 200) then
|
2008-04-24 09:46:22 +03:00
|
|
|
repeat
|
2008-04-24 10:05:26 +03:00
|
|
|
s := FSock.recvstring(FTimeout);
|
|
|
|
if s = '' then
|
|
|
|
Break;
|
|
|
|
until FSock.LastError <> 0
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
Sending := False;
|
|
|
|
Status100Error := s;
|
2008-04-24 09:46:22 +03:00
|
|
|
end;
|
2008-04-24 10:05:26 +03:00
|
|
|
end;
|
|
|
|
|
|
|
|
{ send document }
|
|
|
|
if Sending then
|
|
|
|
begin
|
2008-04-24 10:22:17 +03:00
|
|
|
FUploadSize := FDocument.Size;
|
2008-04-24 10:05:26 +03:00
|
|
|
FSock.SendBuffer(FDocument.Memory, FDocument.Size);
|
|
|
|
if FSock.LastError <> 0 then
|
|
|
|
Exit;
|
|
|
|
end;
|
|
|
|
|
|
|
|
Clear;
|
|
|
|
Size := -1;
|
|
|
|
FTransferEncoding := TE_UNKNOWN;
|
2008-04-24 09:46:22 +03:00
|
|
|
|
2008-04-24 10:05:26 +03:00
|
|
|
{ read status }
|
|
|
|
if Status100Error = '' then
|
|
|
|
begin
|
|
|
|
repeat
|
|
|
|
s := FSock.RecvString(FTimeout);
|
|
|
|
if s <> '' then
|
|
|
|
Break;
|
|
|
|
until FSock.LastError <> 0;
|
|
|
|
if Pos('HTTP/', UpperCase(s)) = 1 then
|
|
|
|
begin
|
|
|
|
FHeaders.Add(s);
|
|
|
|
DecodeStatus(s);
|
|
|
|
end
|
|
|
|
else
|
2008-04-24 09:46:22 +03:00
|
|
|
begin
|
2008-04-24 10:05:26 +03:00
|
|
|
{ old HTTP 0.9 and some buggy servers not send result }
|
|
|
|
s := s + CRLF;
|
|
|
|
FDocument.Write(Pointer(s)^, Length(s));
|
|
|
|
FResultCode := 0;
|
2008-04-24 09:46:22 +03:00
|
|
|
end;
|
2008-04-24 10:05:26 +03:00
|
|
|
end
|
|
|
|
else
|
|
|
|
FHeaders.Add(Status100Error);
|
2008-04-24 09:46:22 +03:00
|
|
|
|
2008-04-24 10:20:39 +03:00
|
|
|
{ if need receive headers, receive and parse it }
|
2008-04-24 10:05:26 +03:00
|
|
|
ToClose := FProtocol <> '1.1';
|
|
|
|
if FHeaders.Count > 0 then
|
2008-04-24 09:46:22 +03:00
|
|
|
repeat
|
2008-04-24 10:05:26 +03:00
|
|
|
s := FSock.RecvString(FTimeout);
|
|
|
|
FHeaders.Add(s);
|
|
|
|
if s = '' then
|
|
|
|
Break;
|
|
|
|
su := UpperCase(s);
|
|
|
|
if Pos('CONTENT-LENGTH:', su) = 1 then
|
|
|
|
begin
|
|
|
|
Size := StrToIntDef(SeparateRight(s, ' '), -1);
|
2008-04-24 10:09:13 +03:00
|
|
|
if Size <> -1 then
|
|
|
|
FTransferEncoding := TE_IDENTITY;
|
2008-04-24 10:05:26 +03:00
|
|
|
end;
|
|
|
|
if Pos('CONTENT-TYPE:', su) = 1 then
|
|
|
|
FMimeType := SeparateRight(s, ' ');
|
|
|
|
if Pos('TRANSFER-ENCODING:', su) = 1 then
|
|
|
|
begin
|
|
|
|
s := SeparateRight(su, ' ');
|
|
|
|
if Pos('CHUNKED', s) > 0 then
|
|
|
|
FTransferEncoding := TE_CHUNKED;
|
|
|
|
end;
|
|
|
|
if Pos('CONNECTION: CLOSE', su) = 1 then
|
|
|
|
ToClose := True;
|
|
|
|
until FSock.LastError <> 0;
|
2008-04-24 09:46:22 +03:00
|
|
|
|
|
|
|
{if need receive response body, read it}
|
2008-04-24 10:05:26 +03:00
|
|
|
Receiving := Method <> 'HEAD';
|
|
|
|
Receiving := Receiving and (FResultCode <> 204);
|
|
|
|
Receiving := Receiving and (FResultCode <> 304);
|
2008-04-24 09:46:22 +03:00
|
|
|
if Receiving then
|
2008-04-24 10:05:26 +03:00
|
|
|
case FTransferEncoding of
|
|
|
|
TE_UNKNOWN:
|
|
|
|
ReadUnknown;
|
|
|
|
TE_IDENTITY:
|
|
|
|
ReadIdentity(Size);
|
|
|
|
TE_CHUNKED:
|
|
|
|
ReadChunked;
|
2008-04-24 09:46:22 +03:00
|
|
|
end;
|
2008-04-24 10:22:17 +03:00
|
|
|
Result := True;
|
2008-04-24 09:46:22 +03:00
|
|
|
|
2008-04-24 10:05:26 +03:00
|
|
|
FDocument.Seek(0, soFromBeginning);
|
2008-04-24 09:46:22 +03:00
|
|
|
if ToClose then
|
2008-04-24 10:05:26 +03:00
|
|
|
begin
|
|
|
|
FSock.CloseSocket;
|
|
|
|
FAliveHost := '';
|
|
|
|
FAlivePort := '';
|
|
|
|
end;
|
2008-04-24 10:22:17 +03:00
|
|
|
ParseCookies;
|
2008-04-24 09:46:22 +03:00
|
|
|
end;
|
|
|
|
|
2008-04-24 10:05:26 +03:00
|
|
|
function THTTPSend.ReadUnknown: Boolean;
|
2008-04-24 09:46:22 +03:00
|
|
|
var
|
2008-04-24 10:05:26 +03:00
|
|
|
s: string;
|
2008-04-24 09:46:22 +03:00
|
|
|
begin
|
2008-04-23 23:26:50 +03:00
|
|
|
repeat
|
2008-04-24 10:09:13 +03:00
|
|
|
s := FSock.RecvPacket(FTimeout);
|
|
|
|
if FSock.LastError = 0 then
|
|
|
|
FDocument.Write(Pointer(s)^, Length(s));
|
2008-04-24 10:05:26 +03:00
|
|
|
until FSock.LastError <> 0;
|
|
|
|
Result := True;
|
2008-04-23 23:26:50 +03:00
|
|
|
end;
|
|
|
|
|
2008-04-24 10:05:26 +03:00
|
|
|
function THTTPSend.ReadIdentity(Size: Integer): Boolean;
|
2008-04-24 09:42:13 +03:00
|
|
|
var
|
2008-04-24 10:22:17 +03:00
|
|
|
x: integer;
|
2008-04-24 09:42:13 +03:00
|
|
|
begin
|
2008-04-24 10:22:17 +03:00
|
|
|
FDownloadSize := Size;
|
|
|
|
FDocument.SetSize(FDocument.Position + Size);
|
|
|
|
x := FSock.RecvBufferEx(IncPoint(FDocument.Memory, FDocument.Position), Size, FTimeout);
|
|
|
|
FDocument.SetSize(FDocument.Position + x);
|
|
|
|
Result := FSock.LastError = 0;
|
2008-04-24 09:42:13 +03:00
|
|
|
end;
|
|
|
|
|
2008-04-24 10:05:26 +03:00
|
|
|
function THTTPSend.ReadChunked: Boolean;
|
2008-04-24 09:46:22 +03:00
|
|
|
var
|
2008-04-24 10:05:26 +03:00
|
|
|
s: string;
|
|
|
|
Size: Integer;
|
2008-04-24 09:46:22 +03:00
|
|
|
begin
|
|
|
|
repeat
|
|
|
|
repeat
|
2008-04-24 10:05:26 +03:00
|
|
|
s := FSock.RecvString(FTimeout);
|
|
|
|
until s <> '';
|
|
|
|
if FSock.LastError <> 0 then
|
|
|
|
Break;
|
|
|
|
s := SeparateLeft(s, ' ');
|
|
|
|
Size := StrToIntDef('$' + s, 0);
|
|
|
|
if Size = 0 then
|
|
|
|
Break;
|
|
|
|
ReadIdentity(Size);
|
|
|
|
until False;
|
|
|
|
Result := FSock.LastError = 0;
|
2008-04-24 09:46:22 +03:00
|
|
|
end;
|
|
|
|
|
2008-04-24 10:22:17 +03:00
|
|
|
procedure THTTPSend.ParseCookies;
|
|
|
|
var
|
|
|
|
n: integer;
|
|
|
|
s: string;
|
|
|
|
sn, sv: string;
|
|
|
|
begin
|
|
|
|
for n := 0 to FHeaders.Count - 1 do
|
|
|
|
if Pos('set-cookie:', lowercase(FHeaders[n])) = 1 then
|
|
|
|
begin
|
|
|
|
s := SeparateRight(FHeaders[n], ':');
|
|
|
|
s := trim(SeparateLeft(s, ';'));
|
|
|
|
sn := trim(SeparateLeft(s, '='));
|
|
|
|
sv := trim(SeparateRight(s, '='));
|
|
|
|
FCookies.Values[sn] := sv;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure THTTPSend.Abort;
|
|
|
|
begin
|
|
|
|
FSock.CloseSocket;
|
|
|
|
end;
|
|
|
|
|
2008-04-23 23:26:50 +03:00
|
|
|
{==============================================================================}
|
|
|
|
|
2008-04-24 10:05:26 +03:00
|
|
|
function HttpGetText(const URL: string; const Response: TStrings): Boolean;
|
2008-04-23 23:26:50 +03:00
|
|
|
var
|
2008-04-24 10:05:26 +03:00
|
|
|
HTTP: THTTPSend;
|
2008-04-23 23:26:50 +03:00
|
|
|
begin
|
2008-04-24 10:05:26 +03:00
|
|
|
HTTP := THTTPSend.Create;
|
2008-04-23 23:26:50 +03:00
|
|
|
try
|
2008-04-24 10:05:26 +03:00
|
|
|
Result := HTTP.HTTPMethod('GET', URL);
|
|
|
|
Response.LoadFromStream(HTTP.Document);
|
2008-04-23 23:26:50 +03:00
|
|
|
finally
|
|
|
|
HTTP.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2008-04-24 10:05:26 +03:00
|
|
|
function HttpGetBinary(const URL: string; const Response: TStream): Boolean;
|
2008-04-24 09:42:13 +03:00
|
|
|
var
|
2008-04-24 10:05:26 +03:00
|
|
|
HTTP: THTTPSend;
|
2008-04-24 09:42:13 +03:00
|
|
|
begin
|
2008-04-24 10:05:26 +03:00
|
|
|
HTTP := THTTPSend.Create;
|
2008-04-24 09:42:13 +03:00
|
|
|
try
|
2008-04-24 10:05:26 +03:00
|
|
|
Result := HTTP.HTTPMethod('GET', URL);
|
|
|
|
Response.Seek(0, soFromBeginning);
|
|
|
|
Response.CopyFrom(HTTP.Document, 0);
|
2008-04-24 09:42:13 +03:00
|
|
|
finally
|
|
|
|
HTTP.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2008-04-24 10:05:26 +03:00
|
|
|
function HttpPostBinary(const URL: string; const Data: TStream): Boolean;
|
2008-04-24 09:42:13 +03:00
|
|
|
var
|
2008-04-24 10:05:26 +03:00
|
|
|
HTTP: THTTPSend;
|
2008-04-24 09:42:13 +03:00
|
|
|
begin
|
2008-04-24 10:05:26 +03:00
|
|
|
HTTP := THTTPSend.Create;
|
2008-04-24 09:42:13 +03:00
|
|
|
try
|
2008-04-24 10:05:26 +03:00
|
|
|
HTTP.Document.CopyFrom(Data, 0);
|
|
|
|
HTTP.MimeType := 'Application/octet-stream';
|
|
|
|
Result := HTTP.HTTPMethod('POST', URL);
|
|
|
|
Data.Seek(0, soFromBeginning);
|
|
|
|
Data.CopyFrom(HTTP.Document, 0);
|
2008-04-24 09:42:13 +03:00
|
|
|
finally
|
|
|
|
HTTP.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2008-04-24 10:05:26 +03:00
|
|
|
function HttpPostURL(const URL, URLData: string; const Data: TStream): Boolean;
|
2008-04-24 10:00:43 +03:00
|
|
|
var
|
2008-04-24 10:05:26 +03:00
|
|
|
HTTP: THTTPSend;
|
2008-04-24 10:00:43 +03:00
|
|
|
begin
|
2008-04-24 10:05:26 +03:00
|
|
|
HTTP := THTTPSend.Create;
|
2008-04-24 10:00:43 +03:00
|
|
|
try
|
2008-04-24 10:05:26 +03:00
|
|
|
HTTP.Document.Write(Pointer(URLData)^, Length(URLData));
|
2008-04-24 10:18:26 +03:00
|
|
|
HTTP.MimeType := 'application/x-www-form-urlencoded';
|
2008-04-24 10:05:26 +03:00
|
|
|
Result := HTTP.HTTPMethod('POST', URL);
|
|
|
|
Data.CopyFrom(HTTP.Document, 0);
|
2008-04-24 10:00:43 +03:00
|
|
|
finally
|
|
|
|
HTTP.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2008-04-24 10:07:45 +03:00
|
|
|
function HttpPostFile(const URL, FieldName, FileName: string;
|
2008-04-24 10:20:39 +03:00
|
|
|
const Data: TStream; const ResultData: TStrings): Boolean;
|
2008-04-24 10:07:45 +03:00
|
|
|
var
|
|
|
|
HTTP: THTTPSend;
|
|
|
|
Bound, s: string;
|
|
|
|
begin
|
2008-04-24 10:18:26 +03:00
|
|
|
Bound := IntToHex(Random(MaxInt), 8) + '_Synapse_boundary';
|
2008-04-24 10:07:45 +03:00
|
|
|
HTTP := THTTPSend.Create;
|
|
|
|
try
|
2008-04-24 10:18:26 +03:00
|
|
|
s := '--' + Bound + CRLF;
|
2008-04-24 10:07:45 +03:00
|
|
|
s := s + 'content-disposition: form-data; name="' + FieldName + '";';
|
|
|
|
s := s + ' filename="' + FileName +'"' + CRLF;
|
|
|
|
s := s + 'Content-Type: Application/octet-string' + CRLF + CRLF;
|
|
|
|
HTTP.Document.Write(Pointer(s)^, Length(s));
|
|
|
|
HTTP.Document.CopyFrom(Data, 0);
|
2008-04-24 10:18:26 +03:00
|
|
|
s := CRLF + '--' + Bound + '--' + CRLF;
|
2008-04-24 10:07:45 +03:00
|
|
|
HTTP.Document.Write(Pointer(s)^, Length(s));
|
2008-04-24 10:22:17 +03:00
|
|
|
HTTP.MimeType := 'multipart/form-data; boundary=' + Bound;
|
2008-04-24 10:07:45 +03:00
|
|
|
Result := HTTP.HTTPMethod('POST', URL);
|
|
|
|
ResultData.LoadFromStream(HTTP.Document);
|
|
|
|
finally
|
|
|
|
HTTP.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2008-04-23 23:26:50 +03:00
|
|
|
end.
|