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 return ENOMEM; | |
566 buffer_len_ = kBytesToRead; | |
binji
2014/08/07 17:52:52
should set buffer_len_ to 0 if !buffer_?
Sam Clegg
2014/08/08 09:26:37
Done.
| |
561 | 567 |
562 while (true) { | 568 while (true) { |
563 int bytes_read; | 569 int bytes_read; |
564 Error error = | 570 Error error = |
565 ReadResponseToBuffer(loader, buffer_.data(), kBytesToRead, &bytes_read); | 571 ReadResponseToBuffer(loader, buffer_, kBytesToRead, &bytes_read); |
566 if (error) | 572 if (error) |
567 return error; | 573 return error; |
568 | 574 |
569 *out_bytes += bytes_read; | 575 *out_bytes += bytes_read; |
570 | 576 |
571 if (bytes_read < kBytesToRead) | 577 if (bytes_read < kBytesToRead) |
572 return 0; | 578 return 0; |
573 } | 579 } |
574 } | 580 } |
575 | 581 |
(...skipping 21 matching lines...) Expand all Loading... | |
597 return 0; | 603 return 0; |
598 } | 604 } |
599 } | 605 } |
600 } | 606 } |
601 | 607 |
602 Error HttpFsNode::ReadResponseToTemp(const ScopedResource& loader, | 608 Error HttpFsNode::ReadResponseToTemp(const ScopedResource& loader, |
603 int count, | 609 int count, |
604 int* out_bytes) { | 610 int* out_bytes) { |
605 *out_bytes = 0; | 611 *out_bytes = 0; |
606 | 612 |
607 if (buffer_.size() < static_cast<size_t>(count)) | 613 if (buffer_len_ < count) { |
608 buffer_.resize(std::min(count, MAX_READ_BUFFER_SIZE)); | 614 int new_len = std::min(count, MAX_READ_BUFFER_SIZE); |
615 buffer_ = (char*)realloc(buffer_, new_len); | |
616 assert(buffer_); | |
617 if (!buffer_) | |
618 return ENOMEM; | |
619 buffer_len_ = new_len; | |
binji
2014/08/07 17:52:52
same here?
Sam Clegg
2014/08/08 09:26:37
Done.
| |
620 } | |
609 | 621 |
610 int bytes_left = count; | 622 int bytes_left = count; |
611 while (bytes_left > 0) { | 623 while (bytes_left > 0) { |
612 int bytes_to_read = | 624 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; | 625 int bytes_read; |
615 Error error = ReadResponseToBuffer( | 626 Error error = ReadResponseToBuffer( |
616 loader, buffer_.data(), bytes_to_read, &bytes_read); | 627 loader, buffer_, bytes_to_read, &bytes_read); |
617 if (error) | 628 if (error) |
618 return error; | 629 return error; |
619 | 630 |
620 if (bytes_read == 0) | 631 if (bytes_read == 0) |
621 return 0; | 632 return 0; |
622 | 633 |
623 bytes_left -= bytes_read; | 634 bytes_left -= bytes_read; |
624 *out_bytes += bytes_read; | 635 *out_bytes += bytes_read; |
625 } | 636 } |
626 | 637 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
658 assert(bytes_read <= bytes_to_read); | 669 assert(bytes_read <= bytes_to_read); |
659 bytes_to_read -= bytes_read; | 670 bytes_to_read -= bytes_read; |
660 out_buffer += bytes_read; | 671 out_buffer += bytes_read; |
661 } | 672 } |
662 | 673 |
663 *out_bytes = count; | 674 *out_bytes = count; |
664 return 0; | 675 return 0; |
665 } | 676 } |
666 | 677 |
667 } // namespace nacl_io | 678 } // namespace nacl_io |
OLD | NEW |