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. 4 5#include <algorithm> 6 7#include "net/http/http_byte_range.h" 8 9namespace { 10 11const int64 kPositionNotSpecified = -1; 12 13} // namespace 14 15namespace net { 16 17HttpByteRange::HttpByteRange() 18 : first_byte_position_(kPositionNotSpecified), 19 last_byte_position_(kPositionNotSpecified), 20 suffix_length_(kPositionNotSpecified), 21 has_computed_bounds_(false) { 22} 23 24bool HttpByteRange::IsSuffixByteRange() const { 25 return suffix_length_ != kPositionNotSpecified; 26} 27 28bool HttpByteRange::HasFirstBytePosition() const { 29 return first_byte_position_ != kPositionNotSpecified; 30} 31 32bool HttpByteRange::HasLastBytePosition() const { 33 return last_byte_position_ != kPositionNotSpecified; 34} 35 36bool HttpByteRange::IsValid() const { 37 if (suffix_length_ > 0) 38 return true; 39 return (first_byte_position_ >= 0 && 40 (last_byte_position_ == kPositionNotSpecified || 41 last_byte_position_ >= first_byte_position_)); 42} 43 44bool HttpByteRange::ComputeBounds(int64 size) { 45 if (size < 0) 46 return false; 47 if (has_computed_bounds_) 48 return false; 49 has_computed_bounds_ = true; 50 51 // Empty values. 52 if (!HasFirstBytePosition() && 53 !HasLastBytePosition() && 54 !IsSuffixByteRange()) { 55 first_byte_position_ = 0; 56 last_byte_position_ = size - 1; 57 return true; 58 } 59 if (!IsValid()) 60 return false; 61 if (IsSuffixByteRange()) { 62 first_byte_position_ = size - std::min(size, suffix_length_); 63 last_byte_position_ = size - 1; 64 return true; 65 } 66 if (first_byte_position_ < size) { 67 if (HasLastBytePosition()) 68 last_byte_position_ = std::min(size - 1, last_byte_position_); 69 else 70 last_byte_position_ = size - 1; 71 return true; 72 } 73 return false; 74} 75 76} // namespace net 77