| 1 |
{%MainUnit castledownload.pas} |
| 2 |
{ |
| 3 |
Copyright 2013-2023 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 |
{ Handle http, https URLs using Android service. } |
| 18 |
|
| 19 |
{$ifdef ANDROID} |
| 20 |
|
| 21 |
{$ifdef read_implementation_uses} |
| 22 |
CastleAndroidInternalAssetStream, CastleMessaging, |
| 23 |
{$endif read_implementation_uses} |
| 24 |
|
| 25 |
{$ifdef read_implementation} |
| 26 |
|
| 27 |
{ TUrlHttpAndroid ------------------------------------------------------------ } |
| 28 |
|
| 29 |
type |
| 30 |
{ TUrlAsynchronousReader descendant that implements http(s) downloading on Android. } |
| 31 |
THttpAndroidReader = class(TUrlAsynchronousReader) |
| 32 |
strict private |
| 33 |
class var |
| 34 |
{ Every THttpAndroidReader instance uses a different id, to avoid mixing messages |
| 35 |
(and temporary files) when multiple downloads are running. } |
| 36 |
NextDownloadId: Integer; |
| 37 |
var |
| 38 |
DownloadId: Integer; |
| 39 |
function HandleDownloadMessages(const Received: TCastleStringList; |
| 40 |
const ReceivedStream: TMemoryStream): boolean; |
| 41 |
public |
| 42 |
procedure Start; override; |
| 43 |
destructor Destroy; override; |
| 44 |
end; |
| 45 |
|
| 46 |
procedure THttpAndroidReader.Start; |
| 47 |
|
| 48 |
function HttpMethodName: String; |
| 49 |
begin |
| 50 |
case HttpMethod of |
| 51 |
hmGet: Result := 'GET'; |
| 52 |
hmPost: Result := 'POST'; |
| 53 |
hmPut: Result := 'PUT'; |
| 54 |
hmDelete: Result := 'DELETE'; |
| 55 |
hmOptions: Result := 'OPTIONS'; |
| 56 |
hmHead: Result := 'HEAD'; |
| 57 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 58 |
else raise EInternalError.Create('HttpMethod=?'); |
| 59 |
{$endif} |
| 60 |
end; |
| 61 |
end; |
| 62 |
|
| 63 |
function HttpRequestBodyStr: String; |
| 64 |
begin |
| 65 |
if HttpRequestBody <> nil then |
| 66 |
Result := StreamToString(HttpRequestBody) |
| 67 |
else |
| 68 |
if HttpPostData <> nil then |
| 69 |
Result := HttpPostData.Text |
| 70 |
else |
| 71 |
Result := ''; |
| 72 |
end; |
| 73 |
|
| 74 |
function HttpHeadersStr: String; |
| 75 |
var |
| 76 |
SL: TStringList; |
| 77 |
I: Integer; |
| 78 |
begin |
| 79 |
if HttpHeadersKeys = nil then |
| 80 |
Exit(''); |
| 81 |
Assert((HttpHeadersKeys <> nil) = (HttpHeadersValues <> nil)); |
| 82 |
Assert(HttpHeadersKeys.Count = HttpHeadersValues.Count); |
| 83 |
SL := TStringList.Create; |
| 84 |
try |
| 85 |
for I := 0 to HttpHeadersKeys.Count - 1 do |
| 86 |
begin |
| 87 |
if Pos(':', HttpHeadersKeys[I]) <> 0 then |
| 88 |
raise Exception.CreateFmt('HTTP header key "%s" contains colon (:), this is not allowed', [ |
| 89 |
HttpHeadersKeys[I] |
| 90 |
]); |
| 91 |
if Pos(':', HttpHeadersValues[I]) <> 0 then |
| 92 |
raise Exception.CreateFmt('HTTP header value "%s" contains colon (:), this is not allowed', [ |
| 93 |
HttpHeadersValues[I] |
| 94 |
]); |
| 95 |
SL.Append(HttpHeadersKeys[I] + ':' + HttpHeadersValues[I]); |
| 96 |
end; |
| 97 |
Result := SL.Text; |
| 98 |
finally |
| 99 |
FreeAndNil(SL); |
| 100 |
end; |
| 101 |
end; |
| 102 |
|
| 103 |
begin |
| 104 |
inherited; |
| 105 |
if not CheckNetworkAccess then |
| 106 |
Exit; |
| 107 |
|
| 108 |
DownloadId := NextDownloadId; |
| 109 |
Inc(NextDownloadId); |
| 110 |
|
| 111 |
CheckFileAccessSafe(Url); |
| 112 |
WritelnLog('Network', 'Download service started for "%s", id %d', [ |
| 113 |
UriDisplay(Url), |
| 114 |
DownloadId |
| 115 |
]); |
| 116 |
|
| 117 |
MimeType := UriMimeType(Url); |
| 118 |
|
| 119 |
Messaging.OnReceive.Add({$ifdef FPC}@{$endif} HandleDownloadMessages); |
| 120 |
Messaging.Send(['download-url', IntToStr(DownloadId), Url, HttpMethodName, HttpRequestBodyStr, HttpHeadersStr]); |
| 121 |
Contents := TMemoryStream.Create; |
| 122 |
|
| 123 |
HttpResponseHeaders := TStringList.Create; |
| 124 |
HttpResponseHeaders.NameValueSeparator := ':'; |
| 125 |
end; |
| 126 |
|
| 127 |
function THttpAndroidReader.HandleDownloadMessages(const Received: TCastleStringList; |
| 128 |
const ReceivedStream: TMemoryStream): boolean; |
| 129 |
|
| 130 |
procedure HandleHttpResponseHeaders(const Received: TCastleStringList); |
| 131 |
var |
| 132 |
I: Integer; |
| 133 |
Val, MimeTypeFromResponse: String; |
| 134 |
begin |
| 135 |
{ append to HttpResponseHeaders } |
| 136 |
for I := 2 to Received.Count - 1 do |
| 137 |
HttpResponseHeaders.Append(Received[I]); |
| 138 |
|
| 139 |
{ set TotalBytes from Content-Length response } |
| 140 |
Val := HttpResponseHeaders.Values['Content-Length']; |
| 141 |
if Val <> '' then |
| 142 |
TotalBytes := StrToInt64Def(Val, -1); |
| 143 |
|
| 144 |
{ set MimeType (overrides MIME from URL extension) from Content-Type response } |
| 145 |
Val := HttpResponseHeaders.Values['Content-Type']; |
| 146 |
if Val <> '' then |
| 147 |
begin |
| 148 |
MimeTypeFromResponse := ContentTypeToMimeType(Val); |
| 149 |
if MimeTypeFromResponse <> '' then |
| 150 |
MimeType := MimeTypeFromResponse; |
| 151 |
end; |
| 152 |
end; |
| 153 |
|
| 154 |
begin |
| 155 |
if (Received.Count = 3) and |
| 156 |
(Received[0] = 'download-error') and |
| 157 |
(Received[1] = IntToStr(DownloadId)) then |
| 158 |
begin |
| 159 |
Status := dsError; |
| 160 |
ErrorMessage := Received[2]; |
| 161 |
Result := true; |
| 162 |
end; |
| 163 |
|
| 164 |
if (Received.Count >= 2) and |
| 165 |
(Received[0] = 'download-http-response-headers') and |
| 166 |
(Received[1] = IntToStr(DownloadId)) then |
| 167 |
begin |
| 168 |
HandleHttpResponseHeaders(Received); |
| 169 |
Result := true; |
| 170 |
end; |
| 171 |
|
| 172 |
if (Received.Count = 2) and |
| 173 |
(Received[0] = 'download-progress') and |
| 174 |
(Received[1] = IntToStr(DownloadId)) then |
| 175 |
begin |
| 176 |
// append ReceivedStream to Contents |
| 177 |
ReceivedStream.SaveToStream(Contents); |
| 178 |
DownloadedBytes := Contents.Size; |
| 179 |
Result := true; |
| 180 |
end; |
| 181 |
|
| 182 |
if (Received.Count = 2) and |
| 183 |
(Received[0] = 'download-success') and |
| 184 |
(Received[1] = IntToStr(DownloadId)) then |
| 185 |
begin |
| 186 |
Status := dsSuccess; |
| 187 |
Contents.Position := 0; // rewind |
| 188 |
DownloadedBytes := Contents.Size; |
| 189 |
TotalBytes := Contents.Size; |
| 190 |
Result := true; |
| 191 |
end; |
| 192 |
|
| 193 |
if (Received.Count = 4) and |
| 194 |
(Received[0] = 'download-response-code') and |
| 195 |
(Received[1] = IntToStr(DownloadId)) then |
| 196 |
begin |
| 197 |
HttpResponseCode := StrToInt(Received[2]); |
| 198 |
// WritelnLog('Network', 'HTTP response for "%s" is %d: %s', [ |
| 199 |
// UriDisplay(Url), |
| 200 |
// HttpResponseCode, |
| 201 |
// Received[3] |
| 202 |
// ]); |
| 203 |
Result := true; |
| 204 |
end; |
| 205 |
end; |
| 206 |
|
| 207 |
destructor THttpAndroidReader.Destroy; |
| 208 |
begin |
| 209 |
if Status = dsDownloading then |
| 210 |
Messaging.Send(['download-interrupt', IntToStr(DownloadId)]); |
| 211 |
Messaging.OnReceive.Remove({$ifdef FPC}@{$endif} HandleDownloadMessages); |
| 212 |
inherited; |
| 213 |
end; |
| 214 |
|
| 215 |
{$endif read_implementation} |
| 216 |
|
| 217 |
{$endif ANDROID} |
| 218 |
|