summaryrefslogtreecommitdiffstats
path: root/xbmc/utils/HttpRangeUtils.h
diff options
context:
space:
mode:
Diffstat (limited to 'xbmc/utils/HttpRangeUtils.h')
-rw-r--r--xbmc/utils/HttpRangeUtils.h187
1 files changed, 187 insertions, 0 deletions
diff --git a/xbmc/utils/HttpRangeUtils.h b/xbmc/utils/HttpRangeUtils.h
new file mode 100644
index 0000000..7e0b66d
--- /dev/null
+++ b/xbmc/utils/HttpRangeUtils.h
@@ -0,0 +1,187 @@
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
15class CHttpRange
16{
17public:
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
36protected:
37 uint64_t m_first = 1;
38 uint64_t m_last = 0;
39};
40
41typedef std::vector<CHttpRange> HttpRanges;
42
43class CHttpResponseRange : public CHttpRange
44{
45public:
46 CHttpResponseRange();
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
62protected:
63 const void* m_data;
64};
65
66typedef std::vector<CHttpResponseRange> HttpResponseRanges;
67
68class CHttpRanges final
69{
70public:
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
97protected:
98 void SortAndCleanup();
99
100 HttpRanges m_ranges;
101};
102
103class HttpRangeUtils
104{
105public:
106 /*!
107 * \brief Generates a valid Content-Range HTTP header value for the given HTTP
108 * range definition.
109 *
110 * \param range HTTP range definition used to generate the Content-Range HTTP header
111 * \return Content-Range HTTP header value
112 */
113 static std::string GenerateContentRangeHeaderValue(const CHttpRange* range);
114
115 /*!
116 * \brief Generates a valid Content-Range HTTP header value for the given HTTP
117 * range properties.
118 *
119 * \param start Start position of the HTTP range
120 * \param end Last/End position of the HTTP range
121 * \param total Total length of original content (not just the range)
122 * \return Content-Range HTTP header value
123 */
124 static std::string GenerateContentRangeHeaderValue(uint64_t start, uint64_t end, uint64_t total);
125
126#ifdef HAS_WEB_SERVER
127 /*!
128 * \brief Generates a multipart boundary that can be used in ranged HTTP
129 * responses.
130 *
131 * \return Multipart boundary that can be used in ranged HTTP responses
132 */
133 static std::string GenerateMultipartBoundary();
134
135 /*!
136 * \brief Generates the multipart/byteranges Content-Type HTTP header value
137 * containing the given multipart boundary for a ranged HTTP response.
138 *
139 * \param multipartBoundary Multipart boundary to be used in the ranged HTTP response
140 * \return multipart/byteranges Content-Type HTTP header value
141 */
142 static std::string GenerateMultipartBoundaryContentType(const std::string& multipartBoundary);
143
144 /*!
145 * \brief Generates a multipart boundary including the Content-Type HTTP
146 * header value with the (actual) given content type of the original
147 * content.
148 *
149 * \param multipartBoundary Multipart boundary to be used in the ranged HTTP response
150 * \param contentType (Actual) Content type of the original content
151 * \return Multipart boundary (including the Content-Type HTTP header) value that can be used in ranged HTTP responses
152 */
153 static std::string GenerateMultipartBoundaryWithHeader(const std::string& multipartBoundary, const std::string& contentType);
154
155 /*!
156 * \brief Generates a multipart boundary including the Content-Type HTTP
157 * header value with the (actual) given content type of the original
158 * content and the Content-Range HTTP header value for the given range.
159 *
160 * \param multipartBoundary Multipart boundary to be used in the ranged HTTP response
161 * \param contentType (Actual) Content type of the original content
162 * \param range HTTP range definition used to generate the Content-Range HTTP header
163 * \return Multipart boundary (including the Content-Type and Content-Range HTTP headers) value that can be used in ranged HTTP responses
164 */
165 static std::string GenerateMultipartBoundaryWithHeader(const std::string& multipartBoundary, const std::string& contentType, const CHttpRange* range);
166
167 /*!
168 * \brief Generates a multipart boundary including the Content-Type HTTP
169 * header value with the (actual) given content type of the original
170 * content and the Content-Range HTTP header value for the given range.
171 *
172 * \param multipartBoundaryWithContentType Multipart boundary (already including the Content-Type HTTP header value) to be used in the ranged HTTP response
173 * \param range HTTP range definition used to generate the Content-Range HTTP header
174 * \return Multipart boundary (including the Content-Type and Content-Range HTTP headers) value that can be used in ranged HTTP responses
175 */
176 static std::string GenerateMultipartBoundaryWithHeader(const std::string& multipartBoundaryWithContentType, const CHttpRange* range);
177
178 /*!
179 * \brief Generates a multipart boundary end that can be used in ranged HTTP
180 * responses.
181 *
182 * \param multipartBoundary Multipart boundary to be used in the ranged HTTP response
183 * \return Multipart boundary end that can be used in a ranged HTTP response
184 */
185 static std::string GenerateMultipartBoundaryEnd(const std::string& multipartBoundary);
186#endif // HAS_WEB_SERVER
187};