| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 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/httpfs/http_fs_node.h" | 5 #include "nacl_io/httpfs/http_fs_node.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 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 221 | 221 |
| 222 *out_size = stat_.st_size; | 222 *out_size = stat_.st_size; |
| 223 return 0; | 223 return 0; |
| 224 } | 224 } |
| 225 | 225 |
| 226 HttpFsNode::HttpFsNode(Filesystem* filesystem, | 226 HttpFsNode::HttpFsNode(Filesystem* filesystem, |
| 227 const std::string& url, | 227 const std::string& url, |
| 228 bool cache_content) | 228 bool cache_content) |
| 229 : Node(filesystem), | 229 : Node(filesystem), |
| 230 url_(url), | 230 url_(url), |
| 231 buffer_(NULL), |
| 232 buffer_len_(0), |
| 231 cache_content_(cache_content), | 233 cache_content_(cache_content), |
| 232 has_cached_size_(false) { | 234 has_cached_size_(false) { |
| 233 } | 235 } |
| 234 | 236 |
| 235 void HttpFsNode::SetMode(int mode) { | 237 void HttpFsNode::SetMode(int mode) { |
| 236 stat_.st_mode = mode; | 238 stat_.st_mode = mode; |
| 237 } | 239 } |
| 238 | 240 |
| 239 Error HttpFsNode::GetStat_Locked(struct stat* stat) { | 241 Error HttpFsNode::GetStat_Locked(struct stat* stat) { |
| 240 // Assume we need to 'HEAD' if we do not know the size, otherwise, assume | 242 // Assume we need to 'HEAD' if we do not know the size, otherwise, assume |
| (...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 550 } | 552 } |
| 551 | 553 |
| 552 return ReadEntireResponseToTemp(loader, out_bytes); | 554 return ReadEntireResponseToTemp(loader, out_bytes); |
| 553 } | 555 } |
| 554 | 556 |
| 555 Error HttpFsNode::ReadEntireResponseToTemp(const ScopedResource& loader, | 557 Error HttpFsNode::ReadEntireResponseToTemp(const ScopedResource& loader, |
| 556 off_t* out_bytes) { | 558 off_t* out_bytes) { |
| 557 *out_bytes = 0; | 559 *out_bytes = 0; |
| 558 | 560 |
| 559 const int kBytesToRead = MAX_READ_BUFFER_SIZE; | 561 const int kBytesToRead = MAX_READ_BUFFER_SIZE; |
| 560 buffer_.resize(kBytesToRead); | 562 buffer_ = (char*)realloc(buffer_, kBytesToRead); |
| 563 assert(buffer_); |
| 564 if (!buffer_) { |
| 565 buffer_len_ = 0; |
| 566 return ENOMEM; |
| 567 } |
| 568 buffer_len_ = kBytesToRead; |
| 561 | 569 |
| 562 while (true) { | 570 while (true) { |
| 563 int bytes_read; | 571 int bytes_read; |
| 564 Error error = | 572 Error error = |
| 565 ReadResponseToBuffer(loader, buffer_.data(), kBytesToRead, &bytes_read); | 573 ReadResponseToBuffer(loader, buffer_, kBytesToRead, &bytes_read); |
| 566 if (error) | 574 if (error) |
| 567 return error; | 575 return error; |
| 568 | 576 |
| 569 *out_bytes += bytes_read; | 577 *out_bytes += bytes_read; |
| 570 | 578 |
| 571 if (bytes_read < kBytesToRead) | 579 if (bytes_read < kBytesToRead) |
| 572 return 0; | 580 return 0; |
| 573 } | 581 } |
| 574 } | 582 } |
| 575 | 583 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 597 return 0; | 605 return 0; |
| 598 } | 606 } |
| 599 } | 607 } |
| 600 } | 608 } |
| 601 | 609 |
| 602 Error HttpFsNode::ReadResponseToTemp(const ScopedResource& loader, | 610 Error HttpFsNode::ReadResponseToTemp(const ScopedResource& loader, |
| 603 int count, | 611 int count, |
| 604 int* out_bytes) { | 612 int* out_bytes) { |
| 605 *out_bytes = 0; | 613 *out_bytes = 0; |
| 606 | 614 |
| 607 if (buffer_.size() < static_cast<size_t>(count)) | 615 if (buffer_len_ < count) { |
| 608 buffer_.resize(std::min(count, MAX_READ_BUFFER_SIZE)); | 616 int new_len = std::min(count, MAX_READ_BUFFER_SIZE); |
| 617 buffer_ = (char*)realloc(buffer_, new_len); |
| 618 assert(buffer_); |
| 619 if (!buffer_) { |
| 620 buffer_len_ = 0; |
| 621 return ENOMEM; |
| 622 } |
| 623 buffer_len_ = new_len; |
| 624 } |
| 609 | 625 |
| 610 int bytes_left = count; | 626 int bytes_left = count; |
| 611 while (bytes_left > 0) { | 627 while (bytes_left > 0) { |
| 612 int bytes_to_read = | 628 int bytes_to_read = std::min(bytes_left, buffer_len_); |
| 613 std::min(static_cast<size_t>(bytes_left), buffer_.size()); | |
| 614 int bytes_read; | 629 int bytes_read; |
| 615 Error error = ReadResponseToBuffer( | 630 Error error = ReadResponseToBuffer( |
| 616 loader, buffer_.data(), bytes_to_read, &bytes_read); | 631 loader, buffer_, bytes_to_read, &bytes_read); |
| 617 if (error) | 632 if (error) |
| 618 return error; | 633 return error; |
| 619 | 634 |
| 620 if (bytes_read == 0) | 635 if (bytes_read == 0) |
| 621 return 0; | 636 return 0; |
| 622 | 637 |
| 623 bytes_left -= bytes_read; | 638 bytes_left -= bytes_read; |
| 624 *out_bytes += bytes_read; | 639 *out_bytes += bytes_read; |
| 625 } | 640 } |
| 626 | 641 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 658 assert(bytes_read <= bytes_to_read); | 673 assert(bytes_read <= bytes_to_read); |
| 659 bytes_to_read -= bytes_read; | 674 bytes_to_read -= bytes_read; |
| 660 out_buffer += bytes_read; | 675 out_buffer += bytes_read; |
| 661 } | 676 } |
| 662 | 677 |
| 663 *out_bytes = count; | 678 *out_bytes = count; |
| 664 return 0; | 679 return 0; |
| 665 } | 680 } |
| 666 | 681 |
| 667 } // namespace nacl_io | 682 } // namespace nacl_io |
| OLD | NEW |