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 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
|
(* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is TurboPower Internet Professional
*
* The Initial Developer of the Original Code is
* TurboPower Software
*
* Portions created by the Initial Developer are Copyright (C) 2000-2002
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* ***** END LICENSE BLOCK ***** *)
(* Part of Ipbroker.pas allowing to use local files Armin <diehl@nordrhein.de> Jun 2006 *)
unit Ipfilebroker;
{$I ipdefine.inc}
interface
{$IFDEF IP_LAZARUS}
uses Classes, SysUtils, LResources, Graphics, LCLProc, LazFileUtils, LazUTF8,
ipconst, iputils, iphtml, ipmsg;
{$ELSE}
uses
Windows, SysUtils, Graphics, Classes, Dialogs, ShellApi,
IpConst, IpUtils, {IpSock, IpCache,} IpHtml, {IpHttp,} IpMsg, IpStrms{, IpFtp};
{$ENDIF}
const
IP_DEFAULT_SCHEME : string = 'HTTP';
{$IFDEF IP_LAZARUS}
function expandLocalHtmlFileName (URL : string) : string;
{$ENDIF}
type
TIpGetHtmlDataEvent =
procedure(Sender : TObject; const URL : string; const PostData : TIpFormDataEntity; var Stream : TStream) of object;
TIpGetImageDataEvent =
procedure(Sender : TIpHtmlNode; const URL : string; var Picture : TPicture) of object;
TIpLeaveHtmlDocumentEvent =
procedure(Sender : TIpHtml) of object;
TIpCheckURLEvent =
procedure(Sender : TObject; const URL : string; var Available :Boolean; var ContentType : string) of object;
TIpReportReferenceEvent =
procedure(Sender : TObject; const URL : string) of object;
TIpExternalResourceEvent =
procedure(Sender : TObject; const URL : string) of object;
TIpCanHandleEvent =
function(Sender : TObject; const URL : string) : Boolean of object;
TIpCustomHtmlDataProvider = class(TIpAbstractHtmlDataProvider)
private
FProtocols : TStrings;
FGetHtml : TIpGetHtmlDataEvent;
FGetImage : TIpGetImageDataEvent;
FLeave : TIpLeaveHtmlDocumentEvent;
FCheckURL : TIpCheckURLEvent;
FReportReference : TIpReportReferenceEvent;
FCanHandle : TIpCanHandleEvent;
function GetProtocols : TStrings;
procedure SetProtocols(const Value : TStrings);
protected
// Nothing
public
constructor Create(AOwner : TComponent); override;
destructor Destroy; override;
function DoGetHtmlStream(const URL : string; PostData : TIpFormDataEntity) : TStream; override;
function DoCheckURL(const URL : string; var ContentType : string) : Boolean; override;
procedure DoLeave(Html : TIpHtml); override;
procedure DoReference(const URL : string); override;
procedure DoGetImage(Sender : TIpHtmlNode; const URL : string; var Picture : TPicture); override;
function GetHtmlStream(const URL : string; PostData : TIpFormDataEntity) : TStream; virtual;
function CheckURL(const URL : string; var ContentType : string) : Boolean; virtual;
procedure Leave(Html : TIpHtml); virtual;
procedure Reference(const URL : string); virtual;
procedure GetImage(Sender : TIpHtmlNode; const URL : string; var Picture : TPicture); virtual;
function CanHandle(const URL : string) : Boolean; override;
function BuildURL(const Old, New : string) : string; override;
property HandledProtocols : TStrings read GetProtocols write SetProtocols;
property OnCanHandle : TIpCanHandleEvent read FCanHandle write FCanhandle;
property OnGetHtml : TIpGetHtmlDataEvent read FGetHtml write FGetHtml;
property OnGetImage : TIpGetImageDataEvent read FGetImage write FGetImage;
property OnLeave : TIpLeaveHtmlDocumentEvent read FLeave write FLeave;
property OnCheckURL : TIpCheckURLEvent read FCheckURL write FCheckURL;
property OnReportReference : TIpReportReferenceEvent read FReportReference write FReportReference;
published
// Nothing
end;
TIpHtmlDataProvider = class(TIpCustomHtmlDataProvider)
public
published
property HandledProtocols;
property OnCanHandle;
property OnGetHtml;
property OnGetImage;
property OnLeave;
property OnCheckURL;
property OnReportReference;
end;
{ TIpFileDataProvider }
TIpFileDataProvider = class(TIpCustomHtmlDataProvider)
private
FOldURL : string;
public
constructor Create(AOwner : TComponent); override;
function GetHtmlStream(const URL : string; PostData : TIpFormDataEntity) : TStream; override;
{$IFDEF IP_LAZARUS}
function DoGetStream(const URL: string): TStream; override;
{$ENDIF}
function CheckURL(const URL : string; var ContentType : string) : Boolean; override;
procedure Leave(Html : TIpHtml); override;
procedure Reference(const URL : string); override;
procedure GetImage(Sender : TIpHtmlNode; const URL : string; var Picture : TPicture); override;
function CanHandle(const URL : string) : Boolean; override;
end;
procedure Register;
implementation
{$IFDEF IP_LAZARUS}
function expandLocalHtmlFileName (URL : string) : string;
begin
if pos ('FILE://', ansiuppercase(URL)) = 0 then
result := 'file://'+DOSToNetPath(ExpandFileNameUTF8(URL))
else
result := URL;
end;
{$ENDIF}
{ TIpCustomHtmlDataProvider }
constructor TIpCustomHtmlDataProvider.Create(AOwner : TComponent);
begin
inherited Create(AOwner);
FProtocols := TStringList.Create;
end;
destructor TIpCustomHtmlDataProvider.Destroy;
begin
FProtocols.Free;
inherited Destroy;
end;
function TIpCustomHtmlDataProvider.BuildURL(const Old,
New : string) : string;
begin
Result := IpUtils.BuildURL(Old, New);
{$IFDEF IP_LAZARUS}
//DebugLn('TIpCustomHtmlDataProvider.BuildURL Old="',old,'" new="',New,'"');
{$ENDIF}
end;
function TIpCustomHtmlDataProvider.CanHandle(const URL : string) : Boolean;
var
AddrRec : TIpAddrRec;
begin
Initialize(AddrRec);
if Assigned(FCanHandle) then begin
Result := FCanHandle(self, URL);
end
else begin
Result := False;
IpParseURL(URL, AddrRec);
if AddrRec.Scheme = '' then begin
if FProtocols.Count > 1 then
AddrRec.Scheme := FProtocols[1]
else
AddrRec.Scheme := IP_DEFAULT_SCHEME;
end;
if FProtocols.IndexOf(UpperCase(AddrRec.Scheme)) > -1 then
Result := True
end;
Initialize(AddrRec);
Finalize(AddrRec);
end;
function TIpCustomHtmlDataProvider.CheckURL(const URL : string;
var ContentType : string) : Boolean;
begin
ContentType := '';
Result := False;
end;
function TIpCustomHtmlDataProvider.DoCheckURL(const URL : string;
var ContentType : string) : Boolean;
begin
Result := False;
if Assigned(FCheckURL) then
FCheckURL(Self, URL, Result, ContentType)
else
Result := CheckURL(URL, ContentType);
end;
procedure TIpCustomHtmlDataProvider.DoGetImage(Sender : TIpHtmlNode;
const URL : string; var Picture : TPicture);
begin
if Assigned(OnGetImage) then begin
OnGetImage(Sender, URL, Picture)
end
else
GetImage(Sender, URL, Picture);
if (Picture <> nil) then begin
if not (Picture is TPicture) then
raise Exception.Create(ProviderUnknownPicture);
end;
end;
procedure TIpCustomHtmlDataProvider.DoLeave;
begin
if assigned(FLeave) then
FLeave(Html)
else
Leave(Html);
end;
procedure TIpCustomHtmlDataProvider.DoReference(const URL : string);
begin
if assigned(FReportReference) then
FReportReference(Self, URL)
else
Reference(URL);
end;
function TIpCustomHtmlDataProvider.DoGetHtmlStream(const URL : string;
PostData : TIpFormDataEntity) : TStream;
begin
Result := nil;
if Assigned(FGetHtml) then
FGetHtml(Self, URL, PostData, Result)
else
Result := GetHtmlStream(URL, PostData);
end;
function TIpCustomHtmlDataProvider.GetHtmlStream(const URL : string;
PostData : TIpFormDataEntity) : TStream;
begin
{ return defaults }
Result := nil;
end;
procedure TIpCustomHtmlDataProvider.GetImage(Sender : TIpHtmlNode;
const URL : string; var Picture : TPicture);
begin
{ return defaults }
Picture := nil;
end;
function TIpCustomHtmlDataProvider.GetProtocols : TStrings;
begin
Result := FProtocols;
end;
procedure TIpCustomHtmlDataProvider.Leave(Html : TIpHtml);
begin
{ do nothing }
end;
procedure TIpCustomHtmlDataProvider.Reference(const URL : string);
begin
{ do nothing }
end;
procedure TIpCustomHtmlDataProvider.SetProtocols(const Value : TStrings);
begin
FProtocols.Assign(Value);
end;
{ TIpFileDataProvider }
constructor TIpFileDataProvider.Create(AOwner : TComponent);
begin
inherited Create(AOwner);
HandledProtocols.Add('FILE');
end;
function TIpFileDataProvider.CanHandle(const URL : string) : Boolean;
var
FileAddrRec : TIpAddrRec;
ContentType, FN : string;
begin
Initialize(FileAddrRec);
{$IFDEF IP_LAZARUS}
//DebugLn('TIpFileDataProvider.CanHandle('+URL+')');
{$ENDIF}
FN := BuildURL(FOldURL, URL);
IpParseURL(FN, FileAddrRec);
FN := NetToDosPath(FileAddrRec.Path);
{$IFDEF IP_LAZARUS}
//DebugLn('TIpFileDataProvider.CanHandle FN="'+FN+'"');
{$ENDIF}
ContentType := UpperCase(GetLocalContent(FN));
Result := (FileExistsUTF8(FN)) and ((Pos('TEXT/HTML', ContentType) > 0) or
(Pos('IMAGE/', ContentType) > 0));
Finalize(FileAddrRec);
end;
function TIpFileDataProvider.CheckURL(const URL : string;
var ContentType : string) : Boolean;
var
FileAddrRec : TIpAddrRec;
FN : string;
begin
Initialize(FileAddrRec);
IpParseURL(URL, FileAddrRec);
FN := NetToDosPath(FileAddrRec.Path);
Result := FileExistsUTF8(FN);
ContentType := GetLocalContent(FN);
Finalize(FileAddrRec);
end;
function TIpFileDataProvider.GetHtmlStream(const URL : string;
PostData : TIpFormDataEntity) : TStream;
var
FileAddrRec : TIpAddrRec;
FN : string;
begin
Initialize(FileAddrRec);
IpParseURL(URL, FileAddrRec);
FN := NetToDosPath(FileAddrRec.Path);
Result := TMemoryStream.Create;
TMemoryStream(Result).LoadFromFile(UTF8ToSys(FN));
FOldURL := URL;
Finalize(FileAddrRec);
end;
{$IFDEF IP_LAZARUS}
function TIpFileDataProvider.DoGetStream(const URL: string): TStream;
var
FileAddrRec : TIpAddrRec;
FN : string;
begin
Initialize(FileAddrRec);
IpParseURL(URL, FileAddrRec);
FN := NetToDosPath(FileAddrRec.Path);
Result := TMemoryStream.Create;
try
TMemoryStream(Result).LoadFromFile(UTF8ToSys(FN));
except
Result.Free;
Result:=nil;
end;
Finalize(FileAddrRec);
end;
{$ENDIF}
procedure TIpFileDataProvider.GetImage(Sender : TIpHtmlNode;
const URL : string; var Picture : TPicture);
var
FileAddrRec : TIpAddrRec;
Content, FN : string;
begin
Initialize(FileAddrRec);
Picture := nil;
IpParseURL(URL, FileAddrRec);
FN := NetToDosPath(FileAddrRec.Path);
Content := UpperCase(GetLocalContent(FN));
if Pos('IMAGE/', Content) > 0 then begin
try
Picture := TPicture.Create;
Picture.LoadFromFile(FN);
except
on EInvalidGraphic do begin
Picture.Free;
Picture := nil;
end;
end;
end;
Finalize(FileAddrRec);
end;
procedure TIpFileDataProvider.Leave(Html : TIpHtml);
begin
inherited Leave(Html);
{ Do Nothing }
end;
procedure TIpFileDataProvider.Reference(const URL : string);
begin
inherited Reference(URL);
{ Do Nothing }
end;
procedure Register;
begin
RegisterComponents('IPro', [TIpFileDataProvider, TIpHTMLDataProvider]);
end;
end.
|