CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/FileLoaders/HTTPFileLoader.cpp
Views: 1401
1
// Copyright (c) 2012- PPSSPP Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
#include <algorithm>
19
20
#include "Common/Log.h"
21
#include "Common/StringUtils.h"
22
#include "Core/Config.h"
23
#include "Core/FileLoaders/HTTPFileLoader.h"
24
25
HTTPFileLoader::HTTPFileLoader(const ::Path &filename)
26
: url_(filename.ToString()), progress_(&cancel_), filename_(filename) {
27
}
28
29
void HTTPFileLoader::Prepare() {
30
std::call_once(preparedFlag_, [this](){
31
client_.SetUserAgent(StringFromFormat("PPSSPP/%s", PPSSPP_GIT_VERSION));
32
33
std::vector<std::string> responseHeaders;
34
Url resourceURL = url_;
35
int redirectsLeft = 20;
36
while (redirectsLeft > 0) {
37
responseHeaders.clear();
38
int code = SendHEAD(resourceURL, responseHeaders);
39
if (code == -400) {
40
// Already reported the error.
41
return;
42
}
43
44
if (code == 301 || code == 302 || code == 303 || code == 307 || code == 308) {
45
Disconnect();
46
47
std::string redirectURL;
48
if (http::GetHeaderValue(responseHeaders, "Location", &redirectURL)) {
49
Url url(resourceURL);
50
url = url.Relative(redirectURL);
51
52
if (url.ToString() == url_.ToString() || url.ToString() == resourceURL.ToString()) {
53
ERROR_LOG(Log::Loader, "HTTP request failed, hit a redirect loop");
54
latestError_ = "Could not connect (redirect loop)";
55
return;
56
}
57
58
resourceURL = url;
59
redirectsLeft--;
60
continue;
61
}
62
63
// No Location header?
64
ERROR_LOG(Log::Loader, "HTTP request failed, invalid redirect");
65
latestError_ = "Could not connect (invalid response)";
66
return;
67
}
68
69
if (code != 200) {
70
// Leave size at 0, invalid.
71
ERROR_LOG(Log::Loader, "HTTP request failed, got %03d for %s", code, filename_.c_str());
72
latestError_ = "Could not connect (invalid response)";
73
Disconnect();
74
return;
75
}
76
77
// We got a good, non-redirect response.
78
redirectsLeft = 0;
79
url_ = resourceURL;
80
}
81
82
// TODO: Expire cache via ETag, etc.
83
bool acceptsRange = false;
84
for (std::string header : responseHeaders) {
85
if (startsWithNoCase(header, "Content-Length:")) {
86
size_t size_pos = header.find_first_of(' ');
87
if (size_pos != header.npos) {
88
size_pos = header.find_first_not_of(' ', size_pos);
89
}
90
if (size_pos != header.npos) {
91
filesize_ = atoll(&header[size_pos]);
92
}
93
}
94
if (startsWithNoCase(header, "Accept-Ranges:")) {
95
std::string lowerHeader = header;
96
std::transform(lowerHeader.begin(), lowerHeader.end(), lowerHeader.begin(), tolower);
97
// TODO: Delimited.
98
if (lowerHeader.find("bytes") != lowerHeader.npos) {
99
acceptsRange = true;
100
}
101
}
102
}
103
104
// TODO: Keepalive instead.
105
Disconnect();
106
107
if (!acceptsRange) {
108
WARN_LOG(Log::Loader, "HTTP server did not advertise support for range requests.");
109
}
110
if (filesize_ == 0) {
111
ERROR_LOG(Log::Loader, "Could not determine file size for %s", filename_.c_str());
112
}
113
114
// If we didn't end up with a filesize_ (e.g. chunked response), give up. File invalid.
115
});
116
}
117
118
int HTTPFileLoader::SendHEAD(const Url &url, std::vector<std::string> &responseHeaders) {
119
if (!url.Valid()) {
120
ERROR_LOG(Log::Loader, "HTTP request failed, invalid URL: '%s'", url.ToString().c_str());
121
latestError_ = "Invalid URL";
122
return -400;
123
}
124
125
if (!client_.Resolve(url.Host().c_str(), url.Port())) {
126
ERROR_LOG(Log::Loader, "HTTP request failed, unable to resolve: |%s| port %d", url.Host().c_str(), url.Port());
127
latestError_ = "Could not connect (name not resolved)";
128
return -400;
129
}
130
131
double timeout = 20.0;
132
133
client_.SetDataTimeout(timeout);
134
Connect(10.0);
135
if (!connected_) {
136
ERROR_LOG(Log::Loader, "HTTP request failed, failed to connect: %s port %d (resource: '%s')", url.Host().c_str(), url.Port(), url.Resource().c_str());
137
latestError_ = "Could not connect (refused to connect)";
138
return -400;
139
}
140
141
http::RequestParams req(url.Resource(), "*/*");
142
int err = client_.SendRequest("HEAD", req, nullptr, &progress_);
143
if (err < 0) {
144
ERROR_LOG(Log::Loader, "HTTP request failed, failed to send request: %s port %d", url.Host().c_str(), url.Port());
145
latestError_ = "Could not connect (could not request data)";
146
Disconnect();
147
return -400;
148
}
149
150
net::Buffer readbuf;
151
return client_.ReadResponseHeaders(&readbuf, responseHeaders, &progress_);
152
}
153
154
HTTPFileLoader::~HTTPFileLoader() {
155
Disconnect();
156
}
157
158
bool HTTPFileLoader::Exists() {
159
Prepare();
160
return url_.Valid() && filesize_ > 0;
161
}
162
163
bool HTTPFileLoader::ExistsFast() {
164
return url_.Valid();
165
}
166
167
bool HTTPFileLoader::IsDirectory() {
168
// Only files.
169
return false;
170
}
171
172
s64 HTTPFileLoader::FileSize() {
173
Prepare();
174
return filesize_;
175
}
176
177
Path HTTPFileLoader::GetPath() const {
178
return filename_;
179
}
180
181
size_t HTTPFileLoader::ReadAt(s64 absolutePos, size_t bytes, void *data, Flags flags) {
182
Prepare();
183
std::lock_guard<std::mutex> guard(readAtMutex_);
184
185
s64 absoluteEnd = std::min(absolutePos + (s64)bytes, filesize_);
186
if (absolutePos >= filesize_ || bytes == 0) {
187
// Read outside of the file or no read at all, just fail immediately.
188
return 0;
189
}
190
191
Connect(10.0);
192
if (!connected_) {
193
return 0;
194
}
195
196
char requestHeaders[4096];
197
// Note that the Range header is *inclusive*.
198
snprintf(requestHeaders, sizeof(requestHeaders),
199
"Range: bytes=%lld-%lld\r\n", absolutePos, absoluteEnd - 1);
200
201
http::RequestParams req(url_.Resource(), "*/*");
202
int err = client_.SendRequest("GET", req, requestHeaders, &progress_);
203
if (err < 0) {
204
latestError_ = "Invalid response reading data";
205
Disconnect();
206
return 0;
207
}
208
209
net::Buffer readbuf;
210
std::vector<std::string> responseHeaders;
211
int code = client_.ReadResponseHeaders(&readbuf, responseHeaders, &progress_);
212
if (code != 206) {
213
ERROR_LOG(Log::Loader, "HTTP server did not respond with range, received code=%03d", code);
214
latestError_ = "Invalid response reading data";
215
Disconnect();
216
return 0;
217
}
218
219
// TODO: Expire cache via ETag, etc.
220
// We don't support multipart/byteranges responses.
221
bool supportedResponse = false;
222
for (std::string header : responseHeaders) {
223
if (startsWithNoCase(header, "Content-Range:")) {
224
// TODO: More correctness. Whitespace can be missing or different.
225
s64 first = -1, last = -1, total = -1;
226
std::string lowerHeader = header;
227
std::transform(lowerHeader.begin(), lowerHeader.end(), lowerHeader.begin(), tolower);
228
if (sscanf(lowerHeader.c_str(), "content-range: bytes %lld-%lld/%lld", &first, &last, &total) >= 2) {
229
if (first == absolutePos && last == absoluteEnd - 1) {
230
supportedResponse = true;
231
} else {
232
ERROR_LOG(Log::Loader, "Unexpected HTTP range: got %lld-%lld, wanted %lld-%lld.", first, last, absolutePos, absoluteEnd - 1);
233
}
234
} else {
235
ERROR_LOG(Log::Loader, "Unexpected HTTP range response: %s", header.c_str());
236
}
237
}
238
}
239
240
// TODO: Would be nice to read directly.
241
net::Buffer output;
242
int res = client_.ReadResponseEntity(&readbuf, responseHeaders, &output, &progress_);
243
if (res != 0) {
244
ERROR_LOG(Log::Loader, "Unable to read HTTP response entity: %d", res);
245
// Let's take anything we got anyway. Not worse than returning nothing?
246
}
247
248
// TODO: Keepalive instead.
249
Disconnect();
250
251
if (!supportedResponse) {
252
ERROR_LOG(Log::Loader, "HTTP server did not respond with the range we wanted.");
253
latestError_ = "Invalid response reading data";
254
return 0;
255
}
256
257
size_t readBytes = output.size();
258
output.Take(readBytes, (char *)data);
259
filepos_ = absolutePos + readBytes;
260
return readBytes;
261
}
262
263
void HTTPFileLoader::Connect(double timeout) {
264
if (!connected_) {
265
cancel_ = false;
266
connected_ = client_.Connect(3, timeout, &cancel_);
267
}
268
}
269
270