xref: /aosp_15_r20/external/cronet/net/http/http_byte_range.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2011 The Chromium Authors
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_
7 
8 #include <stdint.h>
9 
10 #include <string>
11 
12 #include "net/base/net_export.h"
13 
14 namespace net {
15 
16 // A container class that represents a "range" specified for range request
17 // specified by RFC 7233 Section 2.1.
18 // https://tools.ietf.org/html/rfc7233#section-2.1
19 class NET_EXPORT HttpByteRange {
20  public:
21   HttpByteRange();
22 
23   // Convenience constructors.
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);
28 
29   // Since this class is POD, we use constructor, assignment operator
30   // and destructor provided by compiler.
first_byte_position()31   int64_t first_byte_position() const { return first_byte_position_; }
set_first_byte_position(int64_t value)32   void set_first_byte_position(int64_t value) { first_byte_position_ = value; }
33 
last_byte_position()34   int64_t last_byte_position() const { return last_byte_position_; }
set_last_byte_position(int64_t value)35   void set_last_byte_position(int64_t value) { last_byte_position_ = value; }
36 
suffix_length()37   int64_t suffix_length() const { return suffix_length_; }
set_suffix_length(int64_t value)38   void set_suffix_length(int64_t value) { suffix_length_ = value; }
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 
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 
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.
61   bool ComputeBounds(int64_t size);
62 
63  private:
64   int64_t first_byte_position_;
65   int64_t last_byte_position_;
66   int64_t suffix_length_;
67   bool has_computed_bounds_ = false;
68 };
69 
70 }  // namespace net
71 
72 #endif  // NET_HTTP_HTTP_BYTE_RANGE_H_
73