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
3 // found in the LICENSE file.
7 #include "net/http/http_byte_range.h"
11 const int64 kPositionNotSpecified
= -1;
17 HttpByteRange::HttpByteRange()
18 : first_byte_position_(kPositionNotSpecified
),
19 last_byte_position_(kPositionNotSpecified
),
20 suffix_length_(kPositionNotSpecified
),
21 has_computed_bounds_(false) {
24 bool HttpByteRange::IsSuffixByteRange() const {
25 return suffix_length_
!= kPositionNotSpecified
;
28 bool HttpByteRange::HasFirstBytePosition() const {
29 return first_byte_position_
!= kPositionNotSpecified
;
32 bool HttpByteRange::HasLastBytePosition() const {
33 return last_byte_position_
!= kPositionNotSpecified
;
36 bool HttpByteRange::IsValid() const {
37 if (suffix_length_
> 0)
39 return (first_byte_position_
>= 0 &&
40 (last_byte_position_
== kPositionNotSpecified
||
41 last_byte_position_
>= first_byte_position_
));
44 bool HttpByteRange::ComputeBounds(int64 size
) {
47 if (has_computed_bounds_
)
49 has_computed_bounds_
= true;
52 if (!HasFirstBytePosition() &&
53 !HasLastBytePosition() &&
54 !IsSuffixByteRange()) {
55 first_byte_position_
= 0;
56 last_byte_position_
= size
- 1;
61 if (IsSuffixByteRange()) {
62 first_byte_position_
= size
- std::min(size
, suffix_length_
);
63 last_byte_position_
= size
- 1;
66 if (first_byte_position_
< size
) {
67 if (HasLastBytePosition())
68 last_byte_position_
= std::min(size
- 1, last_byte_position_
);
70 last_byte_position_
= size
- 1;