OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 <algorithm> | 5 #include <algorithm> |
6 | 6 |
| 7 #include "base/format_macros.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/strings/stringprintf.h" |
7 #include "net/http/http_byte_range.h" | 10 #include "net/http/http_byte_range.h" |
8 | 11 |
9 namespace { | 12 namespace { |
10 | 13 |
11 const int64 kPositionNotSpecified = -1; | 14 const int64 kPositionNotSpecified = -1; |
12 | 15 |
13 } // namespace | 16 } // namespace |
14 | 17 |
15 namespace net { | 18 namespace net { |
16 | 19 |
17 HttpByteRange::HttpByteRange() | 20 HttpByteRange::HttpByteRange() |
18 : first_byte_position_(kPositionNotSpecified), | 21 : first_byte_position_(kPositionNotSpecified), |
19 last_byte_position_(kPositionNotSpecified), | 22 last_byte_position_(kPositionNotSpecified), |
20 suffix_length_(kPositionNotSpecified), | 23 suffix_length_(kPositionNotSpecified), |
21 has_computed_bounds_(false) { | 24 has_computed_bounds_(false) { |
22 } | 25 } |
23 | 26 |
| 27 // static |
| 28 HttpByteRange HttpByteRange::Bounded(int64 first_byte_position, |
| 29 int64 last_byte_position) { |
| 30 HttpByteRange range; |
| 31 range.set_first_byte_position(first_byte_position); |
| 32 range.set_last_byte_position(last_byte_position); |
| 33 return range; |
| 34 } |
| 35 |
| 36 // static |
| 37 HttpByteRange HttpByteRange::RightUnbounded(int64 first_byte_position) { |
| 38 HttpByteRange range; |
| 39 range.set_first_byte_position(first_byte_position); |
| 40 return range; |
| 41 } |
| 42 |
| 43 // static |
| 44 HttpByteRange HttpByteRange::Suffix(int64 suffix_length) { |
| 45 HttpByteRange range; |
| 46 range.set_suffix_length(suffix_length); |
| 47 return range; |
| 48 } |
| 49 |
24 bool HttpByteRange::IsSuffixByteRange() const { | 50 bool HttpByteRange::IsSuffixByteRange() const { |
25 return suffix_length_ != kPositionNotSpecified; | 51 return suffix_length_ != kPositionNotSpecified; |
26 } | 52 } |
27 | 53 |
28 bool HttpByteRange::HasFirstBytePosition() const { | 54 bool HttpByteRange::HasFirstBytePosition() const { |
29 return first_byte_position_ != kPositionNotSpecified; | 55 return first_byte_position_ != kPositionNotSpecified; |
30 } | 56 } |
31 | 57 |
32 bool HttpByteRange::HasLastBytePosition() const { | 58 bool HttpByteRange::HasLastBytePosition() const { |
33 return last_byte_position_ != kPositionNotSpecified; | 59 return last_byte_position_ != kPositionNotSpecified; |
34 } | 60 } |
35 | 61 |
36 bool HttpByteRange::IsValid() const { | 62 bool HttpByteRange::IsValid() const { |
37 if (suffix_length_ > 0) | 63 if (suffix_length_ > 0) |
38 return true; | 64 return true; |
39 return (first_byte_position_ >= 0 && | 65 return (first_byte_position_ >= 0 && |
40 (last_byte_position_ == kPositionNotSpecified || | 66 (last_byte_position_ == kPositionNotSpecified || |
41 last_byte_position_ >= first_byte_position_)); | 67 last_byte_position_ >= first_byte_position_)); |
42 } | 68 } |
43 | 69 |
| 70 std::string HttpByteRange::PrintHeader() const { |
| 71 DCHECK(IsValid()); |
| 72 |
| 73 if (IsSuffixByteRange()) { |
| 74 return base::StringPrintf("bytes=-%" PRId64, suffix_length()); |
| 75 } else if (HasLastBytePosition()) { |
| 76 DCHECK(HasFirstBytePosition()); |
| 77 return base::StringPrintf("bytes=%" PRId64 "-%" PRId64, |
| 78 first_byte_position(), last_byte_position()); |
| 79 } else { |
| 80 DCHECK(HasFirstBytePosition()); |
| 81 return base::StringPrintf("bytes=%" PRId64 "-", first_byte_position()); |
| 82 } |
| 83 } |
| 84 |
44 bool HttpByteRange::ComputeBounds(int64 size) { | 85 bool HttpByteRange::ComputeBounds(int64 size) { |
45 if (size < 0) | 86 if (size < 0) |
46 return false; | 87 return false; |
47 if (has_computed_bounds_) | 88 if (has_computed_bounds_) |
48 return false; | 89 return false; |
49 has_computed_bounds_ = true; | 90 has_computed_bounds_ = true; |
50 | 91 |
51 // Empty values. | 92 // Empty values. |
52 if (!HasFirstBytePosition() && | 93 if (!HasFirstBytePosition() && |
53 !HasLastBytePosition() && | 94 !HasLastBytePosition() && |
(...skipping 13 matching lines...) Expand all Loading... |
67 if (HasLastBytePosition()) | 108 if (HasLastBytePosition()) |
68 last_byte_position_ = std::min(size - 1, last_byte_position_); | 109 last_byte_position_ = std::min(size - 1, last_byte_position_); |
69 else | 110 else |
70 last_byte_position_ = size - 1; | 111 last_byte_position_ = size - 1; |
71 return true; | 112 return true; |
72 } | 113 } |
73 return false; | 114 return false; |
74 } | 115 } |
75 | 116 |
76 } // namespace net | 117 } // namespace net |
OLD | NEW |