This repository has been archived by the owner on Jan 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathurl_request_asar_job.cc
349 lines (300 loc) · 10.4 KB
/
url_request_asar_job.cc
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
// Copyright (c) 2014 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "atom/browser/net/asar/url_request_asar_job.h"
#include <string>
#include <utility>
#include <vector>
#include "atom/common/asar/archive.h"
#include "atom/common/asar/asar_util.h"
#include "atom/common/atom_constants.h"
#include "base/bind.h"
#include "base/files/file_util.h"
#include "base/strings/string_util.h"
#include "base/synchronization/lock.h"
#include "base/task_runner.h"
#include "net/base/file_stream.h"
#include "net/base/filename_util.h"
#include "net/base/io_buffer.h"
#include "net/base/load_flags.h"
#include "net/base/mime_util.h"
#include "net/base/net_errors.h"
#include "net/filter/gzip_source_stream.h"
#include "net/filter/source_stream.h"
#include "net/http/http_util.h"
#include "net/url_request/url_request_status.h"
#if defined(OS_WIN)
#include "base/win/shortcut.h"
#endif
namespace asar {
namespace {
void Initialize(
const base::FilePath& full_path,
std::shared_ptr<Archive>& archive, // NOLINT
base::FilePath* file_path,
Archive::FileInfo* file_info,
URLRequestAsarJob::JobType* type) {
// Determine whether it is an asar file.
base::FilePath asar_path, relative_path;
if (!GetAsarArchivePath(full_path, &asar_path, &relative_path)) {
*file_path = full_path;
*type = URLRequestAsarJob::TYPE_FILE;
return;
}
archive =
GetOrCreateAsarArchive(asar_path);
if (!archive || !archive->GetFileInfo(relative_path, file_info)) {
*type = URLRequestAsarJob::TYPE_ERROR;
return;
}
if (file_info->unpacked) {
archive->CopyFileOut(relative_path, file_path);
*type = URLRequestAsarJob::TYPE_FILE;
return;
}
*file_path = relative_path;
*type = URLRequestAsarJob::TYPE_ASAR;
}
} // namespace
URLRequestAsarJob::FileMetaInfo::FileMetaInfo()
: file_size(0),
mime_type_result(false),
file_exists(false),
is_directory(false) {
}
URLRequestAsarJob::URLRequestAsarJob(
net::URLRequest* request,
net::NetworkDelegate* network_delegate,
const scoped_refptr<base::TaskRunner> file_task_runner)
: net::URLRequestJob(request, network_delegate),
type_(TYPE_ERROR),
remaining_bytes_(0),
seek_offset_(0),
range_parse_result_(net::OK),
file_task_runner_(file_task_runner),
weak_ptr_factory_(this) {
net::FileURLToFilePath(request->url(), &full_path_);
}
URLRequestAsarJob::~URLRequestAsarJob() {}
void URLRequestAsarJob::InitializeAsarJob() {
stream_.reset(new net::FileStream(file_task_runner_));
}
void URLRequestAsarJob::InitializeFileJob() {
stream_.reset(new net::FileStream(file_task_runner_));
}
void URLRequestAsarJob::Start() {
file_task_runner_->PostTaskAndReply(
FROM_HERE,
base::Bind(&Initialize,
full_path_, std::ref(archive_), &file_path_, &file_info_, &type_),
base::Bind(&URLRequestAsarJob::DidInitialize,
weak_ptr_factory_.GetWeakPtr()));
}
void URLRequestAsarJob::DidInitialize() {
if (type_ == TYPE_ASAR) {
InitializeAsarJob();
int flags = base::File::FLAG_OPEN |
base::File::FLAG_READ |
base::File::FLAG_ASYNC;
int rv = stream_->Open(archive_->path(), flags,
base::Bind(&URLRequestAsarJob::DidOpen,
weak_ptr_factory_.GetWeakPtr()));
if (rv != net::ERR_IO_PENDING)
DidOpen(rv);
} else if (type_ == TYPE_FILE) {
InitializeFileJob();
auto* meta_info = new FileMetaInfo();
file_task_runner_->PostTaskAndReply(
FROM_HERE,
base::Bind(&URLRequestAsarJob::FetchMetaInfo, file_path_,
base::Unretained(meta_info)),
base::Bind(&URLRequestAsarJob::DidFetchMetaInfo,
weak_ptr_factory_.GetWeakPtr(),
base::Owned(meta_info)));
} else {
NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED,
net::ERR_FILE_NOT_FOUND));
}
}
void URLRequestAsarJob::Kill() {
stream_.reset();
weak_ptr_factory_.InvalidateWeakPtrs();
URLRequestJob::Kill();
}
int URLRequestAsarJob::ReadRawData(net::IOBuffer* dest, int dest_size) {
if (remaining_bytes_ < dest_size)
dest_size = static_cast<int>(remaining_bytes_);
// If we should copy zero bytes because |remaining_bytes_| is zero, short
// circuit here.
if (!dest_size)
return 0;
int rv = stream_->Read(dest,
dest_size,
base::Bind(&URLRequestAsarJob::DidRead,
weak_ptr_factory_.GetWeakPtr(),
base::RetainedRef(dest)));
if (rv >= 0) {
remaining_bytes_ -= rv;
DCHECK_GE(remaining_bytes_, 0);
}
return rv;
}
bool URLRequestAsarJob::IsRedirectResponse(GURL* location,
int* http_status_code) {
if (type_ != TYPE_FILE)
return false;
#if defined(OS_WIN)
// Follow a Windows shortcut.
// We just resolve .lnk file, ignore others.
if (!base::LowerCaseEqualsASCII(file_path_.Extension(), ".lnk"))
return false;
base::FilePath new_path = file_path_;
bool resolved;
resolved = base::win::ResolveShortcut(new_path, &new_path, NULL);
// If shortcut is not resolved succesfully, do not redirect.
if (!resolved)
return false;
*location = net::FilePathToFileURL(new_path);
*http_status_code = 301;
return true;
#else
return false;
#endif
}
std::unique_ptr<net::SourceStream> URLRequestAsarJob::SetUpSourceStream() {
std::unique_ptr<net::SourceStream> source =
URLRequestJob::SetUpSourceStream();
if (!base::LowerCaseEqualsASCII(file_path_.Extension(), ".svgz"))
return source;
return net::GzipSourceStream::Create(std::move(source),
net::SourceStream::TYPE_GZIP);
}
bool URLRequestAsarJob::GetMimeType(std::string* mime_type) const {
if (type_ == TYPE_ASAR) {
return net::GetMimeTypeFromFile(file_path_, mime_type);
} else {
if (meta_info_.mime_type_result) {
*mime_type = meta_info_.mime_type;
return true;
}
return false;
}
}
void URLRequestAsarJob::SetExtraRequestHeaders(
const net::HttpRequestHeaders& headers) {
std::string range_header;
if (headers.GetHeader(net::HttpRequestHeaders::kRange, &range_header)) {
// This job only cares about the Range header. This method stashes the value
// for later use in DidOpen(), which is responsible for some of the range
// validation as well. NotifyStartError is not legal to call here since
// the job has not started.
std::vector<net::HttpByteRange> ranges;
if (net::HttpUtil::ParseRangeHeader(range_header, &ranges)) {
if (ranges.size() == 1) {
byte_range_ = ranges[0];
} else {
range_parse_result_ = net::ERR_REQUEST_RANGE_NOT_SATISFIABLE;
}
}
}
}
int URLRequestAsarJob::GetResponseCode() const {
// Request Job gets created only if path exists.
return 200;
}
void URLRequestAsarJob::GetResponseInfo(net::HttpResponseInfo* info) {
std::string status("HTTP/1.1 200 OK");
auto* headers = new net::HttpResponseHeaders(status);
info->headers = headers;
}
void URLRequestAsarJob::FetchMetaInfo(const base::FilePath& file_path,
FileMetaInfo* meta_info) {
base::File::Info file_info;
meta_info->file_exists = base::GetFileInfo(file_path, &file_info);
if (meta_info->file_exists) {
meta_info->file_size = file_info.size;
meta_info->is_directory = file_info.is_directory;
}
// On Windows GetMimeTypeFromFile() goes to the registry. Thus it should be
// done in WorkerPool.
meta_info->mime_type_result =
net::GetMimeTypeFromFile(file_path, &meta_info->mime_type);
}
void URLRequestAsarJob::DidFetchMetaInfo(const FileMetaInfo* meta_info) {
meta_info_ = *meta_info;
if (!meta_info_.file_exists || meta_info_.is_directory) {
DidOpen(net::ERR_FILE_NOT_FOUND);
return;
}
int flags = base::File::FLAG_OPEN |
base::File::FLAG_READ |
base::File::FLAG_ASYNC;
int rv = stream_->Open(file_path_, flags,
base::Bind(&URLRequestAsarJob::DidOpen,
weak_ptr_factory_.GetWeakPtr()));
if (rv != net::ERR_IO_PENDING)
DidOpen(rv);
}
void URLRequestAsarJob::DidOpen(int result) {
if (result != net::OK) {
NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED,
result));
return;
}
if (range_parse_result_ != net::OK) {
NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED,
range_parse_result_));
return;
}
int64_t file_size, read_offset;
if (type_ == TYPE_ASAR) {
file_size = file_info_.size;
read_offset = file_info_.offset;
} else {
file_size = meta_info_.file_size;
read_offset = 0;
}
if (!byte_range_.ComputeBounds(file_size)) {
NotifyStartError(
net::URLRequestStatus(net::URLRequestStatus::FAILED,
net::ERR_REQUEST_RANGE_NOT_SATISFIABLE));
return;
}
remaining_bytes_ = byte_range_.last_byte_position() -
byte_range_.first_byte_position() + 1;
seek_offset_ = byte_range_.first_byte_position() + read_offset;
if (remaining_bytes_ > 0 && seek_offset_ != 0) {
int rv = stream_->Seek(seek_offset_,
base::Bind(&URLRequestAsarJob::DidSeek,
weak_ptr_factory_.GetWeakPtr()));
if (rv != net::ERR_IO_PENDING) {
// stream_->Seek() failed, so pass an intentionally erroneous value
// into DidSeek().
DidSeek(-1);
}
} else {
// We didn't need to call stream_->Seek() at all, so we pass to DidSeek()
// the value that would mean seek success. This way we skip the code
// handling seek failure.
DidSeek(seek_offset_);
}
}
void URLRequestAsarJob::DidSeek(int64_t result) {
if (result != seek_offset_) {
NotifyStartError(
net::URLRequestStatus(net::URLRequestStatus::FAILED,
net::ERR_REQUEST_RANGE_NOT_SATISFIABLE));
return;
}
set_expected_content_size(remaining_bytes_);
NotifyHeadersComplete();
}
void URLRequestAsarJob::DidRead(scoped_refptr<net::IOBuffer> buf, int result) {
if (result >= 0) {
remaining_bytes_ -= result;
DCHECK_GE(remaining_bytes_, 0);
}
buf = nullptr;
ReadRawDataComplete(result);
}
} // namespace asar