📄 httpprot.pas
字号:
procedure Register;
procedure ReplaceExt(var FName : String; const newExt : String);
function EncodeLine(Encoding : THttpEncoding;
SrcData : PChar; Size : Integer):String;
function EncodeStr(Encoding : THttpEncoding; const Value : String) : String;
function RFC1123_Date(aDate : TDateTime) : String;
function RFC1123_StrToDate(aDate : String) : TDateTime;
implementation
const
bin2uue : String = '`!"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_';
bin2b64 : String = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
uue2bin : String = ' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_ ';
b642bin : String = '~~~~~~~~~~~^~~~_TUVWXYZ[\]~~~|~~~ !"#$%&''()*+,-./0123456789~~~~~~:;<=>?@ABCDEFGHIJKLMNOPQRS';
linesize = 45;
function GetBaseUrl(const Url : String) : String; forward;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure Register;
begin
RegisterComponents('FPiette',
[THttpCli
{$IFDEF USE_SSL}
, TSslHttpCli
{$ENDIF}
]);
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{$IFDEF DELPHI1}
function TrimRight(Str : String) : String;
var
i : Integer;
begin
i := Length(Str);
while (i > 0) and (Str[i] in [' ', #9]) do
i := i - 1;
Result := Copy(Str, 1, i);
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function TrimLeft(Str : String) : String;
var
i : Integer;
begin
if Str[1] <> ' ' then
Result := Str
else begin
i := 1;
while (i <= Length(Str)) and (Str[i] = ' ') do
i := i + 1;
Result := Copy(Str, i, Length(Str) - i + 1);
end;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function Trim(Str : String) : String;
begin
Result := TrimLeft(TrimRight(Str));
end;
{$ENDIF}
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{$IFNDEF DELPHI5_UP}
function StrToIntDef(const S: String; const Default: Integer): Integer;
begin
try
Result := StrToInt(S);
except
Result := Default;
end;
end;
{$ENDIF}
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{$IFDEF DEBUG_OUTPUT}
procedure OutputDebugString(const Msg : String);
begin
if IsConsole then
WriteLn(Msg);
end;
{$ENDIF}
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
constructor EHttpException.Create(const Msg : String; ErrCode : Word);
begin
inherited Create(Msg);
ErrorCode := ErrCode;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{$IFDEF DELPHI1}
procedure SetLength(var S: string; NewLength: Integer);
begin
S[0] := chr(NewLength);
end;
{$ENDIF}
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
const
RFC1123_StrWeekDay : String = 'MonTueWedThuFriSatSun';
RFC1123_StrMonth : String = 'JanFebMarAprMayJunJulAugSepOctNovDec';
{ We cannot use Delphi own function because the date must be specified in }
{ english and Delphi use the current language. }
function RFC1123_Date(aDate : TDateTime) : String;
var
Year, Month, Day : Word;
Hour, Min, Sec, MSec : Word;
DayOfWeek : Word;
begin
DecodeDate(aDate, Year, Month, Day);
DecodeTime(aDate, Hour, Min, Sec, MSec);
DayOfWeek := ((Trunc(aDate) - 2) mod 7);
Result := Copy(RFC1123_StrWeekDay, 1 + DayOfWeek * 3, 3) + ', ' +
Format('%2.2d %s %4.4d %2.2d:%2.2d:%2.2d',
[Day, Copy(RFC1123_StrMonth, 1 + 3 * (Month - 1), 3),
Year, Hour, Min, Sec]);
end;
{ Bug: time zone is ignored !! }
function RFC1123_StrToDate(aDate : String) : TDateTime;
var
Year, Month, Day : Word;
Hour, Min, Sec : Word;
begin
{ Fri, 30 Jul 2004 10:10:35 GMT }
Day := StrToIntDef(Copy(aDate, 6, 2), 0);
Month := (Pos(Copy(aDate, 9, 3), RFC1123_StrMonth) + 2) div 3;
Year := StrToIntDef(Copy(aDate, 13, 4), 0);
Hour := StrToIntDef(Copy(aDate, 18, 2), 0);
Min := StrToIntDef(Copy(aDate, 21, 2), 0);
Sec := StrToIntDef(Copy(aDate, 24, 2), 0);
Result := EncodeDate(Year, Month, Day);
Result := Result + EncodeTime(Hour, Min, Sec, 0);
end;
{$IFDEF NOFORMS}
{ This function is a callback function. It means that it is called by }
{ windows. This is the very low level message handler procedure setup to }
{ handle the message sent by windows (winsock) to handle messages. }
function HTTPCliWindowProc(
ahWnd : HWND;
auMsg : Integer;
awParam : WPARAM;
alParam : LPARAM): Integer; stdcall;
var
Obj : TObject;
MsgRec : TMessage;
begin
{ At window creation asked windows to store a pointer to our object }
Obj := TObject(GetWindowLong(ahWnd, 0));
{ If the pointer doesn't represent a TCustomFtpCli, just call the default procedure}
if not (Obj is THTTPCli) then
Result := DefWindowProc(ahWnd, auMsg, awParam, alParam)
else begin
{ Delphi use a TMessage type to pass parameter to his own kind of }
{ windows procedure. So we are doing the same... }
MsgRec.Msg := auMsg;
MsgRec.wParam := awParam;
MsgRec.lParam := alParam;
{ May be a try/except around next line is needed. Not sure ! }
THTTPCli(Obj).WndProc(MsgRec);
Result := MsgRec.Result;
end;
end;
{$ENDIF}
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function THttpCli.HTTPCliAllocateHWnd(Method: TWndMethod) : HWND;
begin
{$IFDEF NOFORMS}
Result := XSocketAllocateHWnd(Self);
SetWindowLong(Result, GWL_WNDPROC, LongInt(@HTTPCliWindowProc));
{$ELSE}
Result := WSocket.AllocateHWnd(Method);
{$ENDIF}
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure THttpCli.HTTPCliDeallocateHWnd(WHandle : HWND);
begin
{$IFDEF NOFORMS}
XSocketDeallocateHWnd(WHandle);
{$ELSE}
WSocket.DeallocateHWnd(WHandle);
{$ENDIF}
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
constructor THttpCli.Create(Aowner:TComponent);
begin
inherited Create(AOwner);
FWindowHandle := HTTPCliAllocateHWnd(WndProc);
FProxyPort := DefaultProxyPort;
FRequestVer := '1.0';
FContentPost := 'application/x-www-form-urlencoded';
FAccept := 'image/gif, image/x-xbitmap, ' +
'image/jpeg, image/pjpeg, */*';
FAgent := 'Mozilla/4.0 (compatible; ICS)';
FDoAuthor := TStringlist.Create;
FRcvdHeader := TStringList.Create;
FReqStream := TMemoryStream.Create;
FState := httpReady;
FLocalAddr := '0.0.0.0';
FFollowRelocation := True; {TT 29 sept 2003}
CreateSocket;
FCtrlSocket.OnSessionClosed := SocketSessionClosed;
FCtrlSocket.OnDataAvailable := SocketDataAvailable;
FCtrlSocket.OnSessionConnected := SocketSessionConnected;
FCtrlSocket.OnDataSent := SocketDataSent;
FCtrlSocket.OnDnsLookupDone := SocketDNSLookupDone;
FCtrlSocket.OnSocksError := DoSocksError;
FCtrlSocket.OnSocksConnected := DoSocksConnected;
FCtrlSocket.OnError := SocketErrorTransfer;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
destructor THttpCli.Destroy;
begin
FDoAuthor.Free;
FCtrlSocket.Free;
FRcvdHeader.Free;
FReqStream.Free;
HTTPCliDeAllocateHWnd(FWindowHandle);
inherited Destroy;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure THttpCli.CreateSocket;
begin
FCtrlSocket := TWSocket.Create(Self);
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure THttpCli.WndProc(var MsgRec: TMessage);
begin
with MsgRec do begin
case Msg of
WM_HTTP_REQUEST_DONE : WMHttpRequestDone(MsgRec);
WM_HTTP_SET_READY : WMHttpSetReady(MsgRec);
WM_HTTP_LOGIN : WMHttpLogin(MsgRec);
else
Result := DefWindowProc(Handle, Msg, wParam, lParam);
end;
end;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure THTTPCli.DoSocksConnected(Sender: TObject; ErrCode: Word);
begin
if Assigned(FOnSocksConnected) then
FOnSocksConnected(Sender, ErrCode);
end;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -