http_byte_range.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright (c) 2011 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. #ifndef NET_HTTP_HTTP_BYTE_RANGE_H_
  5. #define NET_HTTP_HTTP_BYTE_RANGE_H_
  6. #include <stdint.h>
  7. #include <string>
  8. #include "net/base/net_export.h"
  9. namespace net {
  10. // A container class that represents a "range" specified for range request
  11. // specified by RFC 7233 Section 2.1.
  12. // https://tools.ietf.org/html/rfc7233#section-2.1
  13. class NET_EXPORT HttpByteRange {
  14. public:
  15. HttpByteRange();
  16. // Convenience constructors.
  17. static HttpByteRange Bounded(int64_t first_byte_position,
  18. int64_t last_byte_position);
  19. static HttpByteRange RightUnbounded(int64_t first_byte_position);
  20. static HttpByteRange Suffix(int64_t suffix_length);
  21. // Since this class is POD, we use constructor, assignment operator
  22. // and destructor provided by compiler.
  23. int64_t first_byte_position() const { return first_byte_position_; }
  24. void set_first_byte_position(int64_t value) { first_byte_position_ = value; }
  25. int64_t last_byte_position() const { return last_byte_position_; }
  26. void set_last_byte_position(int64_t value) { last_byte_position_ = value; }
  27. int64_t suffix_length() const { return suffix_length_; }
  28. void set_suffix_length(int64_t value) { suffix_length_ = value; }
  29. // Returns true if this is a suffix byte range.
  30. bool IsSuffixByteRange() const;
  31. // Returns true if the first byte position is specified in this request.
  32. bool HasFirstBytePosition() const;
  33. // Returns true if the last byte position is specified in this request.
  34. bool HasLastBytePosition() const;
  35. // Returns true if this range is valid.
  36. bool IsValid() const;
  37. // Gets the header string, e.g. "bytes=0-100", "bytes=100-", "bytes=-100".
  38. // Assumes range is valid.
  39. std::string GetHeaderValue() const;
  40. // A method that when given the size in bytes of a file, adjust the internal
  41. // |first_byte_position_| and |last_byte_position_| values according to the
  42. // range specified by this object. If the range specified is invalid with
  43. // regard to the size or |size| is negative, returns false and there will be
  44. // no side effect.
  45. // Returns false if this method is called more than once and there will be
  46. // no side effect.
  47. bool ComputeBounds(int64_t size);
  48. private:
  49. int64_t first_byte_position_;
  50. int64_t last_byte_position_;
  51. int64_t suffix_length_;
  52. bool has_computed_bounds_ = false;
  53. };
  54. } // namespace net
  55. #endif // NET_HTTP_HTTP_BYTE_RANGE_H_