blob: f83187407b7b09109f44d188aa0277f8da6023b4 [file] [log] [blame]
[email protected]d9f76622011-05-19 23:34:091// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]fc48db82009-04-28 21:23:392// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef NET_HTTP_HTTP_BYTE_RANGE_H_
6#define NET_HTTP_HTTP_BYTE_RANGE_H_
[email protected]fc48db82009-04-28 21:23:397
Avi Drissman13fc8932015-12-20 04:40:468#include <stdint.h>
9
[email protected]b7572ea2013-11-26 20:16:3810#include <string>
11
[email protected]172da1b2011-08-12 15:52:2612#include "net/base/net_export.h"
[email protected]fc48db82009-04-28 21:23:3913
14namespace net {
15
16// A container class that represents a "range" specified for range request
[email protected]ca0c2eb42014-06-11 15:40:5017// specified by RFC 7233 Section 2.1.
18// https://tools.ietf.org/html/rfc7233#section-2.1
[email protected]172da1b2011-08-12 15:52:2619class NET_EXPORT HttpByteRange {
[email protected]fc48db82009-04-28 21:23:3920 public:
21 HttpByteRange();
22
[email protected]b7572ea2013-11-26 20:16:3823 // Convenience constructors.
Avi Drissman13fc8932015-12-20 04:40:4624 static HttpByteRange Bounded(int64_t first_byte_position,
25 int64_t last_byte_position);
26 static HttpByteRange RightUnbounded(int64_t first_byte_position);
27 static HttpByteRange Suffix(int64_t suffix_length);
[email protected]b7572ea2013-11-26 20:16:3828
[email protected]fc48db82009-04-28 21:23:3929 // Since this class is POD, we use constructor, assignment operator
30 // and destructor provided by compiler.
Avi Drissman13fc8932015-12-20 04:40:4631 int64_t first_byte_position() const { return first_byte_position_; }
32 void set_first_byte_position(int64_t value) { first_byte_position_ = value; }
[email protected]fc48db82009-04-28 21:23:3933
Avi Drissman13fc8932015-12-20 04:40:4634 int64_t last_byte_position() const { return last_byte_position_; }
35 void set_last_byte_position(int64_t value) { last_byte_position_ = value; }
[email protected]fc48db82009-04-28 21:23:3936
Avi Drissman13fc8932015-12-20 04:40:4637 int64_t suffix_length() const { return suffix_length_; }
38 void set_suffix_length(int64_t value) { suffix_length_ = value; }
[email protected]fc48db82009-04-28 21:23:3939
40 // Returns true if this is a suffix byte range.
41 bool IsSuffixByteRange() const;
42 // Returns true if the first byte position is specified in this request.
43 bool HasFirstBytePosition() const;
44 // Returns true if the last byte position is specified in this request.
45 bool HasLastBytePosition() const;
46
47 // Returns true if this range is valid.
48 bool IsValid() const;
49
[email protected]b7572ea2013-11-26 20:16:3850 // Gets the header string, e.g. "bytes=0-100", "bytes=100-", "bytes=-100".
51 // Assumes range is valid.
52 std::string GetHeaderValue() const;
53
[email protected]fc48db82009-04-28 21:23:3954 // A method that when given the size in bytes of a file, adjust the internal
55 // |first_byte_position_| and |last_byte_position_| values according to the
56 // range specified by this object. If the range specified is invalid with
57 // regard to the size or |size| is negative, returns false and there will be
58 // no side effect.
59 // Returns false if this method is called more than once and there will be
60 // no side effect.
Avi Drissman13fc8932015-12-20 04:40:4661 bool ComputeBounds(int64_t size);
[email protected]fc48db82009-04-28 21:23:3962
63 private:
Avi Drissman13fc8932015-12-20 04:40:4664 int64_t first_byte_position_;
65 int64_t last_byte_position_;
66 int64_t suffix_length_;
[email protected]fc48db82009-04-28 21:23:3967 bool has_computed_bounds_;
68};
69
70} // namespace net
71
72#endif // NET_HTTP_HTTP_BYTE_RANGE_H_