1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // For loading files, we make use of overlapped i/o to ensure that reading from
6 // the filesystem (e.g., a network filesystem) does not block the calling
7 // thread. An alternative approach would be to use a background thread or pool
8 // of threads, but it seems better to leverage the operating system's ability
9 // to do background file reads for us.
11 // Since overlapped reads require a 'static' buffer for the duration of the
12 // asynchronous read, the URLRequestFileJob keeps a buffer as a member var. In
13 // URLRequestFileJob::Read, data is simply copied from the object's buffer into
14 // the given buffer. If there is no data to copy, the URLRequestFileJob
15 // attempts to read more from the file to fill its buffer. If reading from the
16 // file does not complete synchronously, then the URLRequestFileJob waits for a
17 // signal from the OS that the overlapped read has completed. It does so by
18 // leveraging the MessageLoop::WatchObject API.
20 #include "net/url_request/url_request_file_job.h"
22 #include "base/bind.h"
23 #include "base/compiler_specific.h"
24 #include "base/file_util.h"
25 #include "base/message_loop/message_loop.h"
26 #include "base/strings/string_util.h"
27 #include "base/synchronization/lock.h"
28 #include "base/task_runner.h"
29 #include "base/threading/thread_restrictions.h"
30 #include "build/build_config.h"
31 #include "net/base/file_stream.h"
32 #include "net/base/io_buffer.h"
33 #include "net/base/load_flags.h"
34 #include "net/base/mime_util.h"
35 #include "net/base/net_errors.h"
36 #include "net/base/net_util.h"
37 #include "net/filter/filter.h"
38 #include "net/http/http_util.h"
39 #include "net/url_request/url_request_error_job.h"
40 #include "net/url_request/url_request_file_dir_job.h"
44 #include "base/win/shortcut.h"
49 URLRequestFileJob::FileMetaInfo::FileMetaInfo()
51 mime_type_result(false),
56 URLRequestFileJob::URLRequestFileJob(
58 NetworkDelegate
* network_delegate
,
59 const base::FilePath
& file_path
,
60 const scoped_refptr
<base::TaskRunner
>& file_task_runner
)
61 : URLRequestJob(request
, network_delegate
),
62 file_path_(file_path
),
63 stream_(new FileStream(NULL
, file_task_runner
)),
64 file_task_runner_(file_task_runner
),
66 weak_ptr_factory_(this) {}
68 void URLRequestFileJob::Start() {
69 FileMetaInfo
* meta_info
= new FileMetaInfo();
70 file_task_runner_
->PostTaskAndReply(
72 base::Bind(&URLRequestFileJob::FetchMetaInfo
, file_path_
,
73 base::Unretained(meta_info
)),
74 base::Bind(&URLRequestFileJob::DidFetchMetaInfo
,
75 weak_ptr_factory_
.GetWeakPtr(),
76 base::Owned(meta_info
)));
79 void URLRequestFileJob::Kill() {
81 weak_ptr_factory_
.InvalidateWeakPtrs();
83 URLRequestJob::Kill();
86 bool URLRequestFileJob::ReadRawData(IOBuffer
* dest
, int dest_size
,
88 DCHECK_NE(dest_size
, 0);
90 DCHECK_GE(remaining_bytes_
, 0);
92 if (remaining_bytes_
< dest_size
)
93 dest_size
= static_cast<int>(remaining_bytes_
);
95 // If we should copy zero bytes because |remaining_bytes_| is zero, short
102 int rv
= stream_
->Read(dest
, dest_size
,
103 base::Bind(&URLRequestFileJob::DidRead
,
104 weak_ptr_factory_
.GetWeakPtr()));
106 // Data is immediately available.
108 remaining_bytes_
-= rv
;
109 DCHECK_GE(remaining_bytes_
, 0);
113 // Otherwise, a read error occured. We may just need to wait...
114 if (rv
== ERR_IO_PENDING
) {
115 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING
, 0));
117 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED
, rv
));
122 bool URLRequestFileJob::IsRedirectResponse(GURL
* location
,
123 int* http_status_code
) {
124 if (meta_info_
.is_directory
) {
125 // This happens when we discovered the file is a directory, so needs a
126 // slash at the end of the path.
127 std::string new_path
= request_
->url().path();
128 new_path
.push_back('/');
129 GURL::Replacements replacements
;
130 replacements
.SetPathStr(new_path
);
132 *location
= request_
->url().ReplaceComponents(replacements
);
133 *http_status_code
= 301; // simulate a permanent redirect
138 // Follow a Windows shortcut.
139 // We just resolve .lnk file, ignore others.
140 if (!LowerCaseEqualsASCII(file_path_
.Extension(), ".lnk"))
143 base::FilePath new_path
= file_path_
;
145 resolved
= base::win::ResolveShortcut(new_path
, &new_path
, NULL
);
147 // If shortcut is not resolved succesfully, do not redirect.
151 *location
= FilePathToFileURL(new_path
);
152 *http_status_code
= 301;
159 Filter
* URLRequestFileJob::SetupFilter() const {
160 // Bug 9936 - .svgz files needs to be decompressed.
161 return LowerCaseEqualsASCII(file_path_
.Extension(), ".svgz")
162 ? Filter::GZipFactory() : NULL
;
165 bool URLRequestFileJob::GetMimeType(std::string
* mime_type
) const {
167 if (meta_info_
.mime_type_result
) {
168 *mime_type
= meta_info_
.mime_type
;
174 void URLRequestFileJob::SetExtraRequestHeaders(
175 const HttpRequestHeaders
& headers
) {
176 std::string range_header
;
177 if (headers
.GetHeader(HttpRequestHeaders::kRange
, &range_header
)) {
178 // We only care about "Range" header here.
179 std::vector
<HttpByteRange
> ranges
;
180 if (HttpUtil::ParseRangeHeader(range_header
, &ranges
)) {
181 if (ranges
.size() == 1) {
182 byte_range_
= ranges
[0];
184 // We don't support multiple range requests in one single URL request,
185 // because we need to do multipart encoding here.
186 // TODO(hclam): decide whether we want to support multiple range
188 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED
,
189 ERR_REQUEST_RANGE_NOT_SATISFIABLE
));
195 URLRequestFileJob::~URLRequestFileJob() {
198 void URLRequestFileJob::FetchMetaInfo(const base::FilePath
& file_path
,
199 FileMetaInfo
* meta_info
) {
200 base::File::Info file_info
;
201 meta_info
->file_exists
= base::GetFileInfo(file_path
, &file_info
);
202 if (meta_info
->file_exists
) {
203 meta_info
->file_size
= file_info
.size
;
204 meta_info
->is_directory
= file_info
.is_directory
;
206 // On Windows GetMimeTypeFromFile() goes to the registry. Thus it should be
207 // done in WorkerPool.
208 meta_info
->mime_type_result
= GetMimeTypeFromFile(file_path
,
209 &meta_info
->mime_type
);
212 void URLRequestFileJob::DidFetchMetaInfo(const FileMetaInfo
* meta_info
) {
213 meta_info_
= *meta_info
;
215 // We use URLRequestFileJob to handle files as well as directories without
217 // If a directory does not exist, we return ERR_FILE_NOT_FOUND. Otherwise,
218 // we will append trailing slash and redirect to FileDirJob.
219 // A special case is "\" on Windows. We should resolve as invalid.
220 // However, Windows resolves "\" to "C:\", thus reports it as existent.
221 // So what happens is we append it with trailing slash and redirect it to
222 // FileDirJob where it is resolved as invalid.
223 if (!meta_info_
.file_exists
) {
224 DidOpen(ERR_FILE_NOT_FOUND
);
227 if (meta_info_
.is_directory
) {
232 int flags
= base::PLATFORM_FILE_OPEN
|
233 base::PLATFORM_FILE_READ
|
234 base::PLATFORM_FILE_ASYNC
;
235 int rv
= stream_
->Open(file_path_
, flags
,
236 base::Bind(&URLRequestFileJob::DidOpen
,
237 weak_ptr_factory_
.GetWeakPtr()));
238 if (rv
!= ERR_IO_PENDING
)
242 void URLRequestFileJob::DidOpen(int result
) {
244 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED
, result
));
248 if (!byte_range_
.ComputeBounds(meta_info_
.file_size
)) {
249 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED
,
250 ERR_REQUEST_RANGE_NOT_SATISFIABLE
));
254 remaining_bytes_
= byte_range_
.last_byte_position() -
255 byte_range_
.first_byte_position() + 1;
256 DCHECK_GE(remaining_bytes_
, 0);
258 if (remaining_bytes_
> 0 && byte_range_
.first_byte_position() != 0) {
259 int rv
= stream_
->Seek(FROM_BEGIN
, byte_range_
.first_byte_position(),
260 base::Bind(&URLRequestFileJob::DidSeek
,
261 weak_ptr_factory_
.GetWeakPtr()));
262 if (rv
!= ERR_IO_PENDING
) {
263 // stream_->Seek() failed, so pass an intentionally erroneous value
268 // We didn't need to call stream_->Seek() at all, so we pass to DidSeek()
269 // the value that would mean seek success. This way we skip the code
270 // handling seek failure.
271 DidSeek(byte_range_
.first_byte_position());
275 void URLRequestFileJob::DidSeek(int64 result
) {
276 if (result
!= byte_range_
.first_byte_position()) {
277 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED
,
278 ERR_REQUEST_RANGE_NOT_SATISFIABLE
));
282 set_expected_content_size(remaining_bytes_
);
283 NotifyHeadersComplete();
286 void URLRequestFileJob::DidRead(int result
) {
288 SetStatus(URLRequestStatus()); // Clear the IO_PENDING status
289 } else if (result
== 0) {
290 NotifyDone(URLRequestStatus());
292 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED
, result
));
295 remaining_bytes_
-= result
;
296 DCHECK_GE(remaining_bytes_
, 0);
298 NotifyReadComplete(result
);