| 1 |
{%MainUnit castledownload.pas} |
| 2 |
{ |
| 3 |
Copyright 2013-2024 Michalis Kamburelis. |
| 4 |
|
| 5 |
This file is part of "Castle Game Engine". |
| 6 |
|
| 7 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 8 |
included in this distribution, for details about the copyright. |
| 9 |
|
| 10 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
---------------------------------------------------------------------------- |
| 15 |
} |
| 16 |
|
| 17 |
{$ifdef read_interface} |
| 18 |
|
| 19 |
type |
| 20 |
{ See @link(TCastleDownload.Status). } |
| 21 |
TDownloadStatus = (dsNotStarted, dsDownloading, dsError, dsSuccess); |
| 22 |
|
| 23 |
{ See @link(TCastleDownload.HttpMethod). } |
| 24 |
THttpMethod = (hmGet, hmPost, hmPut, hmDelete, hmOptions, hmHead); |
| 25 |
|
| 26 |
{ Implement this class, and pass to RegisterUrlProtocol, |
| 27 |
to read protocols asynchronously (such that TCastleDownload can read |
| 28 |
them asynchronously). |
| 29 |
|
| 30 |
@bold(There is no point in using this class |
| 31 |
unless you want to handle custom URL protocols). |
| 32 |
If you just want to download asynchronously from standard URLs, |
| 33 |
like file or http, then use TCastleDownload class. } |
| 34 |
TUrlAsynchronousReader = class |
| 35 |
protected |
| 36 |
{ Call this from @link(Start) overrides in descendants that access the network. |
| 37 |
Call this after they called "inherited Start" which may change ForceSynchronous. |
| 38 |
|
| 39 |
This returns @false if |
| 40 |
|
| 41 |
- It seems that we do synchronous download |
| 42 |
(maybe because we are called from Download(), |
| 43 |
or maybe we don't have thread support). |
| 44 |
|
| 45 |
- And EnableBlockingDownloads is @false. |
| 46 |
|
| 47 |
IOW, this makes sure we honor EnableBlockingDownloads. |
| 48 |
|
| 49 |
When it sets @false, it also sets Status and ErrorMessage. |
| 50 |
The caller @link(Start) should just Exit if this returns @false. } |
| 51 |
function CheckNetworkAccess: Boolean; |
| 52 |
public |
| 53 |
{ Fields set before Start is called. Should not be modified by this instance after Start. |
| 54 |
@groupBegin } |
| 55 |
Url: String; |
| 56 |
ForceSynchronous: Boolean; |
| 57 |
HttpMethod: THttpMethod; |
| 58 |
HttpPostData, HttpHeadersKeys, HttpHeadersValues: TStrings; |
| 59 |
HttpRequestBody: TMemoryStream; |
| 60 |
{ @groupEnd } |
| 61 |
|
| 62 |
{ Fields that should be updated during downloading. |
| 63 |
They will set TCastleDownload properties of the same name. |
| 64 |
@groupBegin } |
| 65 |
Status: TDownloadStatus; |
| 66 |
DownloadedBytes, TotalBytes: Int64; |
| 67 |
Contents: TStream; //< Not owned by this instance, TCastleDownload takes care of freeing this. |
| 68 |
ErrorMessage: String; //< When Status = dsError, download failed with this error. |
| 69 |
MimeType: String; //< May be updated at any point, even when dsDownloading, or only when dsSuccess. |
| 70 |
HttpResponseCode: Integer; // In case of HTTP(S) requests, HTTP response code. |
| 71 |
HttpResponseHeaders: TStrings; // In case of HTTP(S) requests, this must contain HTTP response headers. |
| 72 |
FinalUrl: String; |
| 73 |
{ @groupEnd } |
| 74 |
|
| 75 |
{ Start downloading, called when @link(TCastleDownload.Start) is called. |
| 76 |
|
| 77 |
@italic(Note for descendant implementors): |
| 78 |
Exceptions raised inside this will make an actual exception from |
| 79 |
@link(TCastleDownload.Start). |
| 80 |
In case of normal download failure, e.g. file (or HTTP resource) doesn't exist |
| 81 |
(so it is not some bug in code), this method should set Status to dsError |
| 82 |
and not raise an exception. } |
| 83 |
procedure Start; virtual; |
| 84 |
|
| 85 |
{ Called continuously to push the download further. |
| 86 |
|
| 87 |
@italic(Note for descendant implementors): |
| 88 |
Exceptions raised inside this will make an actual exception (from |
| 89 |
undefined place, for now ApplicationProperties._Update, called by TCastleWindow and TCastleControl). |
| 90 |
In case of normal download failure, e.g. file (or HTTP resource) doesn't exist |
| 91 |
(so it is not some bug in code), this method should set Status to dsError |
| 92 |
and not raise an exception. } |
| 93 |
procedure Update; virtual; |
| 94 |
end; |
| 95 |
|
| 96 |
TUrlAsynchronousReaderClass = class of TUrlAsynchronousReader; |
| 97 |
|
| 98 |
{ Event called when @link(Download) function wants to download URL with this protocol. |
| 99 |
Use with @link(RegisterUrlProtocol). } |
| 100 |
TUrlReadEvent = function ( |
| 101 |
const Url: String; out MimeType: string): TStream of object; |
| 102 |
|
| 103 |
{ Event called when @link(URLSaveStream) function wants to save URL with this protocol. |
| 104 |
Use with @link(RegisterUrlProtocol). } |
| 105 |
TUrlWriteEvent = function(const Url: String): TStream of object; |
| 106 |
|
| 107 |
EProtocolAlreadyRegistered = class(Exception); |
| 108 |
|
| 109 |
{ Register how we can load and/or save the URLs with given protocol. |
| 110 |
One (or even both) of given events (ReadEvent, WriteEvent) can be @nil. |
| 111 |
|
| 112 |
@param ReadEvent Used when using synchronous reading by @link(Download). |
| 113 |
|
| 114 |
@param WriteEvent Used when using synchronus writing by @link(URLSaveStream). |
| 115 |
|
| 116 |
@param AsynchronousReader Used when using asynchronous reading by @link(TCastleDownload). |
| 117 |
|
| 118 |
@raises(EProtocolAlreadyRegistered If the protocol handlers are already registered.) } |
| 119 |
procedure RegisterUrlProtocol(const Protocol: String; |
| 120 |
const ReadEvent: TUrlReadEvent; |
| 121 |
const WriteEvent: TUrlWriteEvent; |
| 122 |
const AsynchronousReader: TUrlAsynchronousReaderClass = nil; |
| 123 |
const DetectMimeTypeFromExtension: Boolean = true); |
| 124 |
|
| 125 |
{ Is given protocol name registered with @link(RegisterUrlProtocol). } |
| 126 |
function RegisteredUrlProtocol(const Protocol: String): Boolean; |
| 127 |
|
| 128 |
{ Unregister protocol, reverting the @link(RegisterUrlProtocol). } |
| 129 |
procedure UnregisterUrlProtocol(const Protocol: String); |
| 130 |
|
| 131 |
{$endif} |
| 132 |
|
| 133 |
{$ifdef read_implementation} |
| 134 |
|
| 135 |
procedure TUrlAsynchronousReader.Start; |
| 136 |
begin |
| 137 |
Status := dsDownloading; |
| 138 |
TotalBytes := -1; |
| 139 |
|
| 140 |
{ ForceSynchronous allows to make synchronous Download work even when |
| 141 |
- binary has no thread support linked (e.g. no CThreads linked on Unix). |
| 142 |
- OS has no thread support. |
| 143 |
Moreover TCastleDownload can "downgrade" to working synchronously |
| 144 |
(instead of just failing) when OS never supports threads. |
| 145 |
|
| 146 |
Outside code may set ForceSynchronous, but we can override it to true. } |
| 147 |
{$ifndef CASTLE_SUPPORTS_THREADING} |
| 148 |
ForceSynchronous := true; |
| 149 |
{$endif} |
| 150 |
end; |
| 151 |
|
| 152 |
function TUrlAsynchronousReader.CheckNetworkAccess: Boolean; |
| 153 |
begin |
| 154 |
Result := true; |
| 155 |
|
| 156 |
{ The main purpose of this is to disable Download from blocking. |
| 157 |
Additionally, it also disables TCastleDownload from "downgrading" to blocking, |
| 158 |
when OS doesn't support threads. } |
| 159 |
if ForceSynchronous and (not EnableBlockingDownloads) then |
| 160 |
begin |
| 161 |
Status := dsError; |
| 162 |
ErrorMessage := 'Downloading network resources (from "http" or "https" protocols) is not enabled. Set global EnableBlockingDownloads:=true to enable.'; |
| 163 |
Exit(false); |
| 164 |
end; |
| 165 |
end; |
| 166 |
|
| 167 |
procedure TUrlAsynchronousReader.Update; |
| 168 |
begin |
| 169 |
end; |
| 170 |
|
| 171 |
{ registering URL protocols -------------------------------------------------- } |
| 172 |
|
| 173 |
type |
| 174 |
{ Event called when @link(UriMimeType) function wants to get MIME type from given URL. |
| 175 |
Use with @link(RegisterUrlProtocol). } |
| 176 |
TUrlMimeTypeEvent = function(const Url: String; out Gzipped: Boolean): String of object; |
| 177 |
|
| 178 |
TRegisteredProtocol = class |
| 179 |
Protocol: String; |
| 180 |
ReadEvent: TUrlReadEvent; |
| 181 |
WriteEvent: TUrlWriteEvent; |
| 182 |
AsynchronousReader: TUrlAsynchronousReaderClass; |
| 183 |
MimeTypeEvent: TUrlMimeTypeEvent; |
| 184 |
end; |
| 185 |
|
| 186 |
TRegisteredProtocols = class({$ifdef FPC}specialize{$endif} TObjectList<TRegisteredProtocol>) |
| 187 |
{ Find TRegisteredProtocol with given Protocol, case-insensitive. |
| 188 |
Returns TRegisteredProtocol, or @nil if not found. } |
| 189 |
function Find(Protocol: String): TRegisteredProtocol; |
| 190 |
|
| 191 |
procedure Add(const Protocol: String; |
| 192 |
const ReadEvent: TUrlReadEvent; |
| 193 |
const WriteEvent: TUrlWriteEvent; |
| 194 |
const AsynchronousReader: TUrlAsynchronousReaderClass; |
| 195 |
const MimeTypeEvent: TUrlMimeTypeEvent); reintroduce; |
| 196 |
end; |
| 197 |
|
| 198 |
function TRegisteredProtocols.Find(Protocol: String): TRegisteredProtocol; |
| 199 |
begin |
| 200 |
// to compare ignoring case, entries on TRegisteredProtocol.Protocol are already lower-cased |
| 201 |
Protocol := LowerCase(Protocol); |
| 202 |
|
| 203 |
for Result in Self do |
| 204 |
if Result.Protocol = Protocol then |
| 205 |
Exit; |
| 206 |
Result := nil; |
| 207 |
end; |
| 208 |
|
| 209 |
procedure TRegisteredProtocols.Add(const Protocol: String; |
| 210 |
const ReadEvent: TUrlReadEvent; |
| 211 |
const WriteEvent: TUrlWriteEvent; |
| 212 |
const AsynchronousReader: TUrlAsynchronousReaderClass; |
| 213 |
const MimeTypeEvent: TUrlMimeTypeEvent); |
| 214 |
var |
| 215 |
P: TRegisteredProtocol; |
| 216 |
begin |
| 217 |
if Find(Protocol) <> nil then |
| 218 |
raise EProtocolAlreadyRegistered.CreateFmt('URL protocol "%s" is already registered', [Protocol]); |
| 219 |
// We allow Protocol = '', as it is used internally |
| 220 |
if (Protocol <> '') and |
| 221 |
(not URIValidProtocol(Protocol)) then |
| 222 |
raise Exception.CreateFmt('URL protocol name "%s" is invalid (see URIValidProtocol for the requirements on protocol name)', [Protocol]); |
| 223 |
P := TRegisteredProtocol.Create; |
| 224 |
// lowercase Protocol, to later compare in TRegisteredProtocols.Find ignoring case |
| 225 |
P.Protocol := LowerCase(Protocol); |
| 226 |
P.ReadEvent := ReadEvent; |
| 227 |
P.WriteEvent := WriteEvent; |
| 228 |
P.AsynchronousReader := AsynchronousReader; |
| 229 |
P.MimeTypeEvent := MimeTypeEvent; |
| 230 |
inherited Add(P); |
| 231 |
end; |
| 232 |
|
| 233 |
var |
| 234 |
FRegisteredProtocols: TRegisteredProtocols; |
| 235 |
|
| 236 |
function RegisteredProtocols: TRegisteredProtocols; |
| 237 |
begin |
| 238 |
{ initialize FRegisteredProtocols on-demand } |
| 239 |
if FRegisteredProtocols = nil then |
| 240 |
begin |
| 241 |
FRegisteredProtocols := TRegisteredProtocols.Create(true); |
| 242 |
|
| 243 |
// register default protocols handled in this unit |
| 244 |
|
| 245 |
{$if defined(ANDROID)} |
| 246 |
// http(s) using Android service |
| 247 |
FRegisteredProtocols.Add('http', nil, nil, THttpAndroidReader, |
| 248 |
{$ifdef FPC}@{$endif} TUrlFile{$ifdef FPC}(nil){$endif}.MimeType); |
| 249 |
FRegisteredProtocols.Add('https', nil, nil, THttpAndroidReader, |
| 250 |
{$ifdef FPC}@{$endif} TUrlFile{$ifdef FPC}(nil){$endif}.MimeType); |
| 251 |
|
| 252 |
{$elseif defined(HAS_FP_HTTP_CLIENT)} |
| 253 |
// http(s) using FPC FpHttpClient |
| 254 |
FRegisteredProtocols.Add('http', nil, nil, TFpHttpClientReader, |
| 255 |
{$ifdef FPC}@{$endif} TUrlFile{$ifdef FPC}(nil){$endif}.MimeType); |
| 256 |
FRegisteredProtocols.Add('https', nil, nil, TFpHttpClientReader, |
| 257 |
{$ifdef FPC}@{$endif} TUrlFile{$ifdef FPC}(nil){$endif}.MimeType); |
| 258 |
|
| 259 |
{$elseif defined(DELPHI)} |
| 260 |
// http(s) using Delphi System.Net.HttpClient or Indy |
| 261 |
FRegisteredProtocols.Add('http', nil, nil, TDelphiHttpReader, |
| 262 |
TUrlFile.MimeType); |
| 263 |
FRegisteredProtocols.Add('https', nil, nil, TDelphiHttpReader, |
| 264 |
TUrlFile.MimeType); |
| 265 |
{$endif} |
| 266 |
|
| 267 |
FRegisteredProtocols.Add('', |
| 268 |
{$ifdef FPC}@{$endif} TUrlFile{$ifdef FPC}(nil){$endif}.Read, |
| 269 |
{$ifdef FPC}@{$endif} TUrlFile{$ifdef FPC}(nil){$endif}.Write, nil, |
| 270 |
{$ifdef FPC}@{$endif} TUrlFile{$ifdef FPC}(nil){$endif}.MimeType); |
| 271 |
FRegisteredProtocols.Add('file', |
| 272 |
{$ifdef FPC}@{$endif} TUrlFile{$ifdef FPC}(nil){$endif}.Read, |
| 273 |
{$ifdef FPC}@{$endif} TUrlFile{$ifdef FPC}(nil){$endif}.Write, nil, |
| 274 |
{$ifdef FPC}@{$endif} TUrlFile{$ifdef FPC}(nil){$endif}.MimeType); |
| 275 |
{$if defined(ANDROID)} |
| 276 |
FRegisteredProtocols.Add('assets', |
| 277 |
{$ifdef FPC}@{$endif} TUrlCastleAndroidAssets{$ifdef FPC}(nil){$endif}.Read, nil, nil, |
| 278 |
{$ifdef FPC}@{$endif} TUrlFile{$ifdef FPC}(nil){$endif}.MimeType); |
| 279 |
FRegisteredProtocols.Add('castle-android-assets', |
| 280 |
{$ifdef FPC}@{$endif} TUrlCastleAndroidAssets{$ifdef FPC}(nil){$endif}.Read, nil, nil, |
| 281 |
{$ifdef FPC}@{$endif} TUrlFile{$ifdef FPC}(nil){$endif}.MimeType); |
| 282 |
{$endif} |
| 283 |
FRegisteredProtocols.Add('castle-data', nil, nil, nil, |
| 284 |
{$ifdef FPC}@{$endif} TUrlFile{$ifdef FPC}(nil){$endif}.MimeType); |
| 285 |
FRegisteredProtocols.Add('data', |
| 286 |
{$ifdef FPC}@{$endif} TUrlData{$ifdef FPC}(nil){$endif}.Read, nil, nil, |
| 287 |
{$ifdef FPC}@{$endif} TUrlData{$ifdef FPC}(nil){$endif}.MimeType); |
| 288 |
FRegisteredProtocols.Add('ecmascript', |
| 289 |
{$ifdef FPC}@{$endif} TUrlEcmaScript{$ifdef FPC}(nil){$endif}.Read, nil, nil, |
| 290 |
{$ifdef FPC}@{$endif} TUrlEcmaScript{$ifdef FPC}(nil){$endif}.MimeType); |
| 291 |
FRegisteredProtocols.Add('javascript', |
| 292 |
{$ifdef FPC}@{$endif} TUrlEcmaScript{$ifdef FPC}(nil){$endif}.Read, nil, nil, |
| 293 |
{$ifdef FPC}@{$endif} TUrlEcmaScript{$ifdef FPC}(nil){$endif}.MimeType); |
| 294 |
FRegisteredProtocols.Add('castlescript', |
| 295 |
{$ifdef FPC}@{$endif} TUrlCastleScript{$ifdef FPC}(nil){$endif}.Read, nil, nil, |
| 296 |
{$ifdef FPC}@{$endif} TUrlCastleScript{$ifdef FPC}(nil){$endif}.MimeType); |
| 297 |
FRegisteredProtocols.Add('kambiscript', |
| 298 |
{$ifdef FPC}@{$endif} TUrlCastleScript{$ifdef FPC}(nil){$endif}.Read, nil, nil, |
| 299 |
{$ifdef FPC}@{$endif} TUrlCastleScript{$ifdef FPC}(nil){$endif}.MimeType); |
| 300 |
FRegisteredProtocols.Add('compiled', |
| 301 |
{$ifdef FPC}@{$endif} TUrlCompiled{$ifdef FPC}(nil){$endif}.Read, nil, nil, |
| 302 |
{$ifdef FPC}@{$endif} TUrlCompiled{$ifdef FPC}(nil){$endif}.MimeType); |
| 303 |
end; |
| 304 |
Result := FRegisteredProtocols; |
| 305 |
end; |
| 306 |
|
| 307 |
procedure RegisterUrlProtocol(const Protocol: String; |
| 308 |
const ReadEvent: TUrlReadEvent; |
| 309 |
const WriteEvent: TUrlWriteEvent; |
| 310 |
const AsynchronousReader: TUrlAsynchronousReaderClass = nil; |
| 311 |
const DetectMimeTypeFromExtension: Boolean = true); |
| 312 |
var |
| 313 |
MimeTypeEvent: TUrlMimeTypeEvent; |
| 314 |
begin |
| 315 |
if DetectMimeTypeFromExtension then |
| 316 |
MimeTypeEvent := {$ifdef FPC}@{$endif} TUrlFile{$ifdef FPC}(nil){$endif}.MimeType |
| 317 |
else |
| 318 |
MimeTypeEvent := nil; |
| 319 |
RegisteredProtocols.Add(Protocol, ReadEvent, WriteEvent, AsynchronousReader, MimeTypeEvent); |
| 320 |
end; |
| 321 |
|
| 322 |
function RegisteredUrlProtocol(const Protocol: String): Boolean; |
| 323 |
begin |
| 324 |
Result := RegisteredProtocols.Find(Protocol) <> nil; |
| 325 |
end; |
| 326 |
|
| 327 |
procedure UnregisterUrlProtocol(const Protocol: String); |
| 328 |
var |
| 329 |
P: TRegisteredProtocol; |
| 330 |
begin |
| 331 |
P := RegisteredProtocols.Find(Protocol); |
| 332 |
if P <> nil then |
| 333 |
RegisteredProtocols.Remove(P); |
| 334 |
end; |
| 335 |
|
| 336 |
{$endif} |