OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "nacl_io/mount_node_http.h" | 5 #include "nacl_io/mount_node_http.h" |
6 | 6 |
7 #include <assert.h> | 7 #include <assert.h> |
8 #include <errno.h> | 8 #include <errno.h> |
9 #include <stdio.h> | 9 #include <stdio.h> |
10 #include <string.h> | 10 #include <string.h> |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
144 return EIO; | 144 return EIO; |
145 } | 145 } |
146 | 146 |
147 } // namespace | 147 } // namespace |
148 | 148 |
149 void MountNodeHttp::SetCachedSize(off_t size) { | 149 void MountNodeHttp::SetCachedSize(off_t size) { |
150 has_cached_size_ = true; | 150 has_cached_size_ = true; |
151 stat_.st_size = size; | 151 stat_.st_size = size; |
152 } | 152 } |
153 | 153 |
154 Error MountNodeHttp::FSync() { return ENOSYS; } | 154 Error MountNodeHttp::FSync() { return EACCES; } |
155 | 155 |
156 Error MountNodeHttp::GetDents(size_t offs, | 156 Error MountNodeHttp::GetDents(size_t offs, |
157 struct dirent* pdir, | 157 struct dirent* pdir, |
158 size_t count, | 158 size_t count, |
159 int* out_bytes) { | 159 int* out_bytes) { |
160 *out_bytes = 0; | 160 *out_bytes = 0; |
161 return ENOSYS; | 161 return EACCES; |
162 } | 162 } |
163 | 163 |
164 Error MountNodeHttp::GetStat(struct stat* stat) { | 164 Error MountNodeHttp::GetStat(struct stat* stat) { |
165 AUTO_LOCK(node_lock_); | 165 AUTO_LOCK(node_lock_); |
166 | 166 |
167 // Assume we need to 'HEAD' if we do not know the size, otherwise, assume | 167 // Assume we need to 'HEAD' if we do not know the size, otherwise, assume |
168 // that the information is constant. We can add a timeout if needed. | 168 // that the information is constant. We can add a timeout if needed. |
169 MountHttp* mount = static_cast<MountHttp*>(mount_); | 169 MountHttp* mount = static_cast<MountHttp*>(mount_); |
170 if (stat_.st_size == 0 || !mount->cache_stat_) { | 170 if (stat_.st_size == 0 || !mount->cache_stat_) { |
171 StringMap_t headers; | 171 StringMap_t headers; |
(...skipping 14 matching lines...) Expand all Loading... | |
186 | 186 |
187 ScopedResource scoped_loader(mount_->ppapi(), loader); | 187 ScopedResource scoped_loader(mount_->ppapi(), loader); |
188 ScopedResource scoped_request(mount_->ppapi(), request); | 188 ScopedResource scoped_request(mount_->ppapi(), request); |
189 ScopedResource scoped_response(mount_->ppapi(), response); | 189 ScopedResource scoped_response(mount_->ppapi(), response); |
190 | 190 |
191 size_t entity_length; | 191 size_t entity_length; |
192 if (ParseContentLength(response_headers, &entity_length)) { | 192 if (ParseContentLength(response_headers, &entity_length)) { |
193 SetCachedSize(static_cast<off_t>(entity_length)); | 193 SetCachedSize(static_cast<off_t>(entity_length)); |
194 } else if (cache_content_ && !has_cached_size_) { | 194 } else if (cache_content_ && !has_cached_size_) { |
195 error = DownloadToCache(); | 195 error = DownloadToCache(); |
196 // TODO(binji): this error should not be dropped, but it requires a bit | 196 if (error) |
197 // of a refactor of the tests. See crbug.com/245431 | 197 return error; |
198 // if (error) | |
199 // return error; | |
200 } else { | 198 } else { |
201 // Don't use SetCachedSize here -- it is actually unknown. | 199 // Don't use SetCachedSize here -- it is actually unknown. |
202 stat_.st_size = 0; | 200 stat_.st_size = 0; |
203 } | 201 } |
204 | 202 |
205 stat_.st_atime = 0; // TODO(binji): Use "Last-Modified". | 203 stat_.st_atime = 0; // TODO(binji): Use "Last-Modified". |
206 stat_.st_mtime = 0; | 204 stat_.st_mtime = 0; |
207 stat_.st_ctime = 0; | 205 stat_.st_ctime = 0; |
208 | 206 |
209 stat_.st_mode |= S_IFREG; | 207 stat_.st_mode |= S_IFREG; |
(...skipping 19 matching lines...) Expand all Loading... | |
229 if (error) | 227 if (error) |
230 return error; | 228 return error; |
231 } | 229 } |
232 | 230 |
233 return ReadPartialFromCache(attr.offs, buf, count, out_bytes); | 231 return ReadPartialFromCache(attr.offs, buf, count, out_bytes); |
234 } | 232 } |
235 | 233 |
236 return DownloadPartial(attr.offs, buf, count, out_bytes); | 234 return DownloadPartial(attr.offs, buf, count, out_bytes); |
237 } | 235 } |
238 | 236 |
239 Error MountNodeHttp::FTruncate(off_t size) { return ENOSYS; } | 237 Error MountNodeHttp::FTruncate(off_t size) { return EACCES; } |
240 | 238 |
241 Error MountNodeHttp::Write(const HandleAttr& attr, | 239 Error MountNodeHttp::Write(const HandleAttr& attr, |
242 const void* buf, | 240 const void* buf, |
243 size_t count, | 241 size_t count, |
244 int* out_bytes) { | 242 int* out_bytes) { |
245 // TODO(binji): support POST? | 243 // TODO(binji): support POST? |
246 *out_bytes = 0; | 244 *out_bytes = 0; |
247 return ENOSYS; | 245 return EACCES; |
248 } | 246 } |
249 | 247 |
250 Error MountNodeHttp::GetSize(size_t* out_size) { | 248 Error MountNodeHttp::GetSize(size_t* out_size) { |
251 *out_size = 0; | 249 *out_size = 0; |
252 | 250 |
253 // TODO(binji): This value should be cached properly; i.e. obey the caching | 251 // TODO(binji): This value should be cached properly; i.e. obey the caching |
254 // headers returned by the server. | 252 // headers returned by the server. |
255 AUTO_LOCK(node_lock_); | 253 AUTO_LOCK(node_lock_); |
256 if (!has_cached_size_) { | 254 if (!has_cached_size_) { |
257 // Even if DownloadToCache fails, the best result we can return is what | 255 // Even if DownloadToCache fails, the best result we can return is what |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
335 uint32_t response_headers_length; | 333 uint32_t response_headers_length; |
336 const char* response_headers_str = | 334 const char* response_headers_str = |
337 var_interface->VarToUtf8(response_headers_var, &response_headers_length); | 335 var_interface->VarToUtf8(response_headers_var, &response_headers_length); |
338 | 336 |
339 *out_loader = loader.Release(); | 337 *out_loader = loader.Release(); |
340 *out_request = request.Release(); | 338 *out_request = request.Release(); |
341 *out_response = response.Release(); | 339 *out_response = response.Release(); |
342 *out_response_headers = | 340 *out_response_headers = |
343 ParseHeaders(response_headers_str, response_headers_length); | 341 ParseHeaders(response_headers_str, response_headers_length); |
344 | 342 |
343 var_interface->Release(response_headers_var); | |
344 | |
345 return 0; | 345 return 0; |
346 } | 346 } |
347 | 347 |
348 Error MountNodeHttp::DownloadToCache() { | 348 Error MountNodeHttp::DownloadToCache() { |
349 StringMap_t headers; | 349 StringMap_t headers; |
350 PP_Resource loader; | 350 PP_Resource loader; |
351 PP_Resource request; | 351 PP_Resource request; |
352 PP_Resource response; | 352 PP_Resource response; |
353 int32_t statuscode; | 353 int32_t statuscode; |
354 StringMap_t response_headers; | 354 StringMap_t response_headers; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
402 | 402 |
403 cached_data_.resize(total_bytes_read + bytes_to_read); | 403 cached_data_.resize(total_bytes_read + bytes_to_read); |
404 } | 404 } |
405 } | 405 } |
406 | 406 |
407 Error MountNodeHttp::ReadPartialFromCache(size_t offs, | 407 Error MountNodeHttp::ReadPartialFromCache(size_t offs, |
408 void* buf, | 408 void* buf, |
409 int count, | 409 int count, |
410 int* out_bytes) { | 410 int* out_bytes) { |
411 *out_bytes = 0; | 411 *out_bytes = 0; |
412 size_t size = cached_data_.size(); | |
412 | 413 |
413 if (offs > cached_data_.size()) | 414 if (offs + count > size) |
414 return EINVAL; | 415 count = size - offs; |
Sam Clegg
2013/11/20 20:00:54
So this function can no longer fail? Might be wor
binji
2013/11/20 20:34:17
Hm. I like that it has the same signature as the r
| |
415 | 416 |
416 count = std::min(count, static_cast<int>(cached_data_.size() - offs)); | 417 if (count <= 0) |
418 return 0; | |
419 | |
417 memcpy(buf, &cached_data_.data()[offs], count); | 420 memcpy(buf, &cached_data_.data()[offs], count); |
418 | |
419 *out_bytes = count; | 421 *out_bytes = count; |
420 return 0; | 422 return 0; |
421 } | 423 } |
422 | 424 |
423 Error MountNodeHttp::DownloadPartial(size_t offs, | 425 Error MountNodeHttp::DownloadPartial(size_t offs, |
424 void* buf, | 426 void* buf, |
425 size_t count, | 427 size_t count, |
426 int* out_bytes) { | 428 int* out_bytes) { |
427 *out_bytes = 0; | 429 *out_bytes = 0; |
428 | 430 |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
542 bytes_to_read -= bytes_read; | 544 bytes_to_read -= bytes_read; |
543 out_buffer += bytes_read; | 545 out_buffer += bytes_read; |
544 } | 546 } |
545 | 547 |
546 *out_bytes = count; | 548 *out_bytes = count; |
547 return 0; | 549 return 0; |
548 } | 550 } |
549 | 551 |
550 } // namespace nacl_io | 552 } // namespace nacl_io |
551 | 553 |
OLD | NEW |