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.
4 
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.
10 //
11 // Since overlapped reads require a 'static' buffer for the duration of the
12 // asynchronous read, the URLRequestTestJobBackedByFile keeps a buffer as a
13 // member var.  In URLRequestTestJobBackedByFile::Read, data is simply copied
14 // from the object's buffer into the given buffer.  If there is no data to copy,
15 // the URLRequestTestJobBackedByFile attempts to read more from the file to fill
16 // its buffer.  If reading from the file does not complete synchronously, then
17 // the URLRequestTestJobBackedByFile waits for a signal from the OS that the
18 // overlapped read has completed.  It does so by leveraging the
19 // MessageLoop::WatchObject API.
20 
21 #include "net/test/url_request/url_request_test_job_backed_by_file.h"
22 
23 #include "base/bind.h"
24 #include "base/compiler_specific.h"
25 #include "base/files/file_util.h"
26 #include "base/strings/string_util.h"
27 #include "base/strings/stringprintf.h"
28 #include "base/synchronization/lock.h"
29 #include "base/task_runner.h"
30 #include "base/threading/thread_restrictions.h"
31 #include "build/build_config.h"
32 #include "net/base/file_stream.h"
33 #include "net/base/filename_util.h"
34 #include "net/base/io_buffer.h"
35 #include "net/base/load_flags.h"
36 #include "net/base/mime_util.h"
37 #include "net/filter/gzip_source_stream.h"
38 #include "net/filter/source_stream.h"
39 #include "net/http/http_util.h"
40 #include "net/url_request/url_request_error_job.h"
41 #include "url/gurl.h"
42 
43 #if defined(OS_WIN)
44 #include "base/win/shortcut.h"
45 #endif
46 
47 namespace net {
48 
FileMetaInfo()49 URLRequestTestJobBackedByFile::FileMetaInfo::FileMetaInfo()
50     : file_size(0),
51       mime_type_result(false),
52       file_exists(false),
53       is_directory(false) {}
54 
URLRequestTestJobBackedByFile(URLRequest * request,const base::FilePath & file_path,const scoped_refptr<base::TaskRunner> & file_task_runner)55 URLRequestTestJobBackedByFile::URLRequestTestJobBackedByFile(
56     URLRequest* request,
57     const base::FilePath& file_path,
58     const scoped_refptr<base::TaskRunner>& file_task_runner)
59     : URLRequestJob(request),
60       file_path_(file_path),
61       stream_(new FileStream(file_task_runner)),
62       file_task_runner_(file_task_runner),
63       remaining_bytes_(0),
64       range_parse_result_(OK) {}
65 
Start()66 void URLRequestTestJobBackedByFile::Start() {
67   FileMetaInfo* meta_info = new FileMetaInfo();
68   file_task_runner_->PostTaskAndReply(
69       FROM_HERE,
70       base::BindOnce(&URLRequestTestJobBackedByFile::FetchMetaInfo, file_path_,
71                      base::Unretained(meta_info)),
72       base::BindOnce(&URLRequestTestJobBackedByFile::DidFetchMetaInfo,
73                      weak_ptr_factory_.GetWeakPtr(), base::Owned(meta_info)));
74 }
75 
Kill()76 void URLRequestTestJobBackedByFile::Kill() {
77   stream_.reset();
78   weak_ptr_factory_.InvalidateWeakPtrs();
79 
80   URLRequestJob::Kill();
81 }
82 
ReadRawData(IOBuffer * dest,int dest_size)83 int URLRequestTestJobBackedByFile::ReadRawData(IOBuffer* dest, int dest_size) {
84   DCHECK_NE(dest_size, 0);
85   DCHECK_GE(remaining_bytes_, 0);
86 
87   if (remaining_bytes_ < dest_size)
88     dest_size = remaining_bytes_;
89 
90   // If we should copy zero bytes because |remaining_bytes_| is zero, short
91   // circuit here.
92   if (!dest_size)
93     return 0;
94 
95   int rv = stream_->Read(dest, dest_size,
96                          base::BindOnce(&URLRequestTestJobBackedByFile::DidRead,
97                                         weak_ptr_factory_.GetWeakPtr(),
98                                         base::WrapRefCounted(dest)));
99   if (rv >= 0) {
100     remaining_bytes_ -= rv;
101     DCHECK_GE(remaining_bytes_, 0);
102   }
103 
104   return rv;
105 }
106 
GetMimeType(std::string * mime_type) const107 bool URLRequestTestJobBackedByFile::GetMimeType(std::string* mime_type) const {
108   DCHECK(request_);
109   if (meta_info_.mime_type_result) {
110     *mime_type = meta_info_.mime_type;
111     return true;
112   }
113   return false;
114 }
115 
SetExtraRequestHeaders(const HttpRequestHeaders & headers)116 void URLRequestTestJobBackedByFile::SetExtraRequestHeaders(
117     const HttpRequestHeaders& headers) {
118   std::string range_header;
119   if (headers.GetHeader(HttpRequestHeaders::kRange, &range_header)) {
120     // This job only cares about the Range header. This method stashes the value
121     // for later use in DidOpen(), which is responsible for some of the range
122     // validation as well. NotifyStartError is not legal to call here since
123     // the job has not started.
124     std::vector<HttpByteRange> ranges;
125     if (HttpUtil::ParseRangeHeader(range_header, &ranges)) {
126       if (ranges.size() == 1) {
127         byte_range_ = ranges[0];
128       } else {
129         // We don't support multiple range requests in one single URL request,
130         // because we need to do multipart encoding here.
131         // TODO(hclam): decide whether we want to support multiple range
132         // requests.
133         range_parse_result_ = ERR_REQUEST_RANGE_NOT_SATISFIABLE;
134       }
135     }
136   }
137 }
138 
GetResponseInfo(HttpResponseInfo * info)139 void URLRequestTestJobBackedByFile::GetResponseInfo(HttpResponseInfo* info) {
140   if (!serve_mime_type_as_content_type_ || !meta_info_.mime_type_result)
141     return;
142   auto headers =
143       base::MakeRefCounted<net::HttpResponseHeaders>("HTTP/1.1 200 OK");
144   headers->AddHeader(net::HttpRequestHeaders::kContentType,
145                      meta_info_.mime_type);
146   info->headers = headers;
147 }
148 
OnOpenComplete(int result)149 void URLRequestTestJobBackedByFile::OnOpenComplete(int result) {}
150 
OnSeekComplete(int64_t result)151 void URLRequestTestJobBackedByFile::OnSeekComplete(int64_t result) {}
152 
OnReadComplete(IOBuffer * buf,int result)153 void URLRequestTestJobBackedByFile::OnReadComplete(IOBuffer* buf, int result) {}
154 
155 URLRequestTestJobBackedByFile::~URLRequestTestJobBackedByFile() = default;
156 
157 std::unique_ptr<SourceStream>
SetUpSourceStream()158 URLRequestTestJobBackedByFile::SetUpSourceStream() {
159   std::unique_ptr<SourceStream> source = URLRequestJob::SetUpSourceStream();
160   if (!base::LowerCaseEqualsASCII(file_path_.Extension(), ".svgz"))
161     return source;
162 
163   return GzipSourceStream::Create(std::move(source), SourceStream::TYPE_GZIP);
164 }
165 
FetchMetaInfo(const base::FilePath & file_path,FileMetaInfo * meta_info)166 void URLRequestTestJobBackedByFile::FetchMetaInfo(
167     const base::FilePath& file_path,
168     FileMetaInfo* meta_info) {
169   base::File::Info file_info;
170   meta_info->file_exists = base::GetFileInfo(file_path, &file_info);
171   if (meta_info->file_exists) {
172     meta_info->file_size = file_info.size;
173     meta_info->is_directory = file_info.is_directory;
174   }
175   // On Windows GetMimeTypeFromFile() goes to the registry. Thus it should be
176   // done in WorkerPool.
177   meta_info->mime_type_result =
178       GetMimeTypeFromFile(file_path, &meta_info->mime_type);
179   meta_info->absolute_path = base::MakeAbsoluteFilePath(file_path);
180 }
181 
DidFetchMetaInfo(const FileMetaInfo * meta_info)182 void URLRequestTestJobBackedByFile::DidFetchMetaInfo(
183     const FileMetaInfo* meta_info) {
184   meta_info_ = *meta_info;
185 
186   if (!meta_info_.file_exists) {
187     DidOpen(ERR_FILE_NOT_FOUND);
188     return;
189   }
190 
191   // This class is only used for mocking out network requests in test by using a
192   // file as a response body. It doesn't need to support directory listings.
193   if (meta_info_.is_directory) {
194     DidOpen(ERR_INVALID_ARGUMENT);
195     return;
196   }
197 
198   int flags =
199       base::File::FLAG_OPEN | base::File::FLAG_READ | base::File::FLAG_ASYNC;
200   int rv = stream_->Open(file_path_, flags,
201                          base::BindOnce(&URLRequestTestJobBackedByFile::DidOpen,
202                                         weak_ptr_factory_.GetWeakPtr()));
203   if (rv != ERR_IO_PENDING)
204     DidOpen(rv);
205 }
206 
DidOpen(int result)207 void URLRequestTestJobBackedByFile::DidOpen(int result) {
208   OnOpenComplete(result);
209   if (result != OK) {
210     NotifyStartError(result);
211     return;
212   }
213 
214   if (range_parse_result_ != OK ||
215       !byte_range_.ComputeBounds(meta_info_.file_size)) {
216     DidSeek(ERR_REQUEST_RANGE_NOT_SATISFIABLE);
217     return;
218   }
219 
220   remaining_bytes_ =
221       byte_range_.last_byte_position() - byte_range_.first_byte_position() + 1;
222   DCHECK_GE(remaining_bytes_, 0);
223 
224   if (remaining_bytes_ > 0 && byte_range_.first_byte_position() != 0) {
225     int rv =
226         stream_->Seek(byte_range_.first_byte_position(),
227                       base::BindOnce(&URLRequestTestJobBackedByFile::DidSeek,
228                                      weak_ptr_factory_.GetWeakPtr()));
229     if (rv != ERR_IO_PENDING)
230       DidSeek(ERR_REQUEST_RANGE_NOT_SATISFIABLE);
231   } else {
232     // We didn't need to call stream_->Seek() at all, so we pass to DidSeek()
233     // the value that would mean seek success. This way we skip the code
234     // handling seek failure.
235     DidSeek(byte_range_.first_byte_position());
236   }
237 }
238 
DidSeek(int64_t result)239 void URLRequestTestJobBackedByFile::DidSeek(int64_t result) {
240   DCHECK(result < 0 || result == byte_range_.first_byte_position());
241 
242   OnSeekComplete(result);
243   if (result < 0) {
244     NotifyStartError(ERR_REQUEST_RANGE_NOT_SATISFIABLE);
245     return;
246   }
247 
248   set_expected_content_size(remaining_bytes_);
249   NotifyHeadersComplete();
250 }
251 
DidRead(scoped_refptr<IOBuffer> buf,int result)252 void URLRequestTestJobBackedByFile::DidRead(scoped_refptr<IOBuffer> buf,
253                                             int result) {
254   if (result >= 0) {
255     remaining_bytes_ -= result;
256     DCHECK_GE(remaining_bytes_, 0);
257   }
258 
259   OnReadComplete(buf.get(), result);
260   buf = nullptr;
261 
262   ReadRawDataComplete(result);
263 }
264 
265 }  // namespace net
266