xbmc
HttpRangeUtils.h
1 /*
2  * Copyright (C) 2015-2018 Team Kodi
3  * This file is part of Kodi - https://kodi.tv
4  *
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  * See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 #include <stdint.h>
12 #include <string>
13 #include <vector>
14 
16 {
17 public:
18  CHttpRange() = default;
19  CHttpRange(uint64_t firstPosition, uint64_t lastPosition);
20  virtual ~CHttpRange() = default;
21 
22  bool operator<(const CHttpRange &other) const;
23  bool operator==(const CHttpRange &other) const;
24  bool operator!=(const CHttpRange &other) const;
25 
26  virtual uint64_t GetFirstPosition() const { return m_first; }
27  virtual void SetFirstPosition(uint64_t firstPosition) { m_first = firstPosition; }
28  virtual uint64_t GetLastPosition() const { return m_last; }
29  virtual void SetLastPosition(uint64_t lastPosition) { m_last = lastPosition; }
30 
31  virtual uint64_t GetLength() const;
32  virtual void SetLength(uint64_t length);
33 
34  virtual bool IsValid() const;
35 
36 protected:
37  uint64_t m_first = 1;
38  uint64_t m_last = 0;
39 };
40 
41 typedef std::vector<CHttpRange> HttpRanges;
42 
44 {
45 public:
47  CHttpResponseRange(uint64_t firstPosition, uint64_t lastPosition);
48  CHttpResponseRange(const void* data, uint64_t firstPosition, uint64_t lastPosition);
49  CHttpResponseRange(const void* data, uint64_t length);
50  ~CHttpResponseRange() override = default;
51 
52  bool operator==(const CHttpResponseRange &other) const;
53  bool operator!=(const CHttpResponseRange &other) const;
54 
55  const void* GetData() const { return m_data; }
56  void SetData(const void* data) { m_data = data; }
57  void SetData(const void* data, uint64_t length);
58  void SetData(const void* data, uint64_t firstPosition, uint64_t lastPosition);
59 
60  bool IsValid() const override;
61 
62 protected:
63  const void* m_data;
64 };
65 
66 typedef std::vector<CHttpResponseRange> HttpResponseRanges;
67 
68 class CHttpRanges final
69 {
70 public:
71  CHttpRanges();
72  explicit CHttpRanges(const HttpRanges& httpRanges);
73 
74  const HttpRanges& Get() const { return m_ranges; }
75  bool Get(size_t index, CHttpRange& range) const;
76  bool GetFirst(CHttpRange& range) const;
77  bool GetLast(CHttpRange& range) const;
78  size_t Size() const { return m_ranges.size(); }
79  bool IsEmpty() const { return m_ranges.empty(); }
80 
81  bool GetFirstPosition(uint64_t& position) const;
82  bool GetLastPosition(uint64_t& position) const;
83  uint64_t GetLength() const;
84 
85  bool GetTotalRange(CHttpRange& range) const;
86 
87  void Add(const CHttpRange& range);
88  void Remove(size_t index);
89  void Clear();
90 
91  HttpRanges::const_iterator Begin() const { return m_ranges.begin(); }
92  HttpRanges::const_iterator End() const { return m_ranges.end(); }
93 
94  bool Parse(const std::string& header);
95  bool Parse(const std::string& header, uint64_t totalLength);
96 
97 protected:
98  void SortAndCleanup();
99 
100  HttpRanges m_ranges;
101 };
102 
104 {
105 public:
113  static std::string GenerateContentRangeHeaderValue(const CHttpRange* range);
114 
124  static std::string GenerateContentRangeHeaderValue(uint64_t start, uint64_t end, uint64_t total);
125 
126 #ifdef HAS_WEB_SERVER
127 
133  static std::string GenerateMultipartBoundary();
134 
142  static std::string GenerateMultipartBoundaryContentType(const std::string& multipartBoundary);
143 
153  static std::string GenerateMultipartBoundaryWithHeader(const std::string& multipartBoundary, const std::string& contentType);
154 
165  static std::string GenerateMultipartBoundaryWithHeader(const std::string& multipartBoundary, const std::string& contentType, const CHttpRange* range);
166 
176  static std::string GenerateMultipartBoundaryWithHeader(const std::string& multipartBoundaryWithContentType, const CHttpRange* range);
177 
185  static std::string GenerateMultipartBoundaryEnd(const std::string& multipartBoundary);
186 #endif // HAS_WEB_SERVER
187 };
Definition: HttpRangeUtils.h:43
Definition: HttpRangeUtils.h:68
Definition: HttpRangeUtils.h:103
Definition: HttpRangeUtils.h:15