[email protected] | d9f7662 | 2011-05-19 23:34:09 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | fc48db8 | 2009-04-28 21:23:39 | [diff] [blame] | 2 | // 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] | fc48db8 | 2009-04-28 21:23:39 | [diff] [blame] | 7 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 8 | #include <stdint.h> |
9 | |||||
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 10 | #include <string> |
11 | |||||
[email protected] | 172da1b | 2011-08-12 15:52:26 | [diff] [blame] | 12 | #include "net/base/net_export.h" |
[email protected] | fc48db8 | 2009-04-28 21:23:39 | [diff] [blame] | 13 | |
14 | namespace net { | ||||
15 | |||||
16 | // A container class that represents a "range" specified for range request | ||||
[email protected] | ca0c2eb4 | 2014-06-11 15:40:50 | [diff] [blame] | 17 | // specified by RFC 7233 Section 2.1. |
18 | // https://tools.ietf.org/html/rfc7233#section-2.1 | ||||
[email protected] | 172da1b | 2011-08-12 15:52:26 | [diff] [blame] | 19 | class NET_EXPORT HttpByteRange { |
[email protected] | fc48db8 | 2009-04-28 21:23:39 | [diff] [blame] | 20 | public: |
21 | HttpByteRange(); | ||||
22 | |||||
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 23 | // Convenience constructors. |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 24 | 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] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 28 | |
[email protected] | fc48db8 | 2009-04-28 21:23:39 | [diff] [blame] | 29 | // Since this class is POD, we use constructor, assignment operator |
30 | // and destructor provided by compiler. | ||||
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 31 | 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] | fc48db8 | 2009-04-28 21:23:39 | [diff] [blame] | 33 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 34 | 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] | fc48db8 | 2009-04-28 21:23:39 | [diff] [blame] | 36 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 37 | int64_t suffix_length() const { return suffix_length_; } |
38 | void set_suffix_length(int64_t value) { suffix_length_ = value; } | ||||
[email protected] | fc48db8 | 2009-04-28 21:23:39 | [diff] [blame] | 39 | |
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] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 50 | // 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] | fc48db8 | 2009-04-28 21:23:39 | [diff] [blame] | 54 | // 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 Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 61 | bool ComputeBounds(int64_t size); |
[email protected] | fc48db8 | 2009-04-28 21:23:39 | [diff] [blame] | 62 | |
63 | private: | ||||
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 64 | int64_t first_byte_position_; |
65 | int64_t last_byte_position_; | ||||
66 | int64_t suffix_length_; | ||||
[email protected] | fc48db8 | 2009-04-28 21:23:39 | [diff] [blame] | 67 | bool has_computed_bounds_; |
68 | }; | ||||
69 | |||||
70 | } // namespace net | ||||
71 | |||||
72 | #endif // NET_HTTP_HTTP_BYTE_RANGE_H_ |