2 * Copyright (C) 2015-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
18 CHttpRange() = default;
19 CHttpRange(uint64_t firstPosition
, uint64_t lastPosition
);
20 virtual ~CHttpRange() = default;
22 bool operator<(const CHttpRange
&other
) const;
23 bool operator==(const CHttpRange
&other
) const;
24 bool operator!=(const CHttpRange
&other
) const;
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
; }
31 virtual uint64_t GetLength() const;
32 virtual void SetLength(uint64_t length
);
34 virtual bool IsValid() const;
41 typedef std::vector
<CHttpRange
> HttpRanges
;
43 class CHttpResponseRange
: public CHttpRange
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;
52 bool operator==(const CHttpResponseRange
&other
) const;
53 bool operator!=(const CHttpResponseRange
&other
) const;
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
);
60 bool IsValid() const override
;
66 typedef std::vector
<CHttpResponseRange
> HttpResponseRanges
;
68 class CHttpRanges final
72 explicit CHttpRanges(const HttpRanges
& httpRanges
);
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(); }
81 bool GetFirstPosition(uint64_t& position
) const;
82 bool GetLastPosition(uint64_t& position
) const;
83 uint64_t GetLength() const;
85 bool GetTotalRange(CHttpRange
& range
) const;
87 void Add(const CHttpRange
& range
);
88 void Remove(size_t index
);
91 HttpRanges::const_iterator
Begin() const { return m_ranges
.begin(); }
92 HttpRanges::const_iterator
End() const { return m_ranges
.end(); }
94 bool Parse(const std::string
& header
);
95 bool Parse(const std::string
& header
, uint64_t totalLength
);
98 void SortAndCleanup();
107 * \brief Generates a valid Content-Range HTTP header value for the given HTTP
110 * \param range HTTP range definition used to generate the Content-Range HTTP header
111 * \return Content-Range HTTP header value
113 static std::string
GenerateContentRangeHeaderValue(const CHttpRange
* range
);
116 * \brief Generates a valid Content-Range HTTP header value for the given HTTP
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
124 static std::string
GenerateContentRangeHeaderValue(uint64_t start
, uint64_t end
, uint64_t total
);
126 #ifdef HAS_WEB_SERVER
128 * \brief Generates a multipart boundary that can be used in ranged HTTP
131 * \return Multipart boundary that can be used in ranged HTTP responses
133 static std::string
GenerateMultipartBoundary();
136 * \brief Generates the multipart/byteranges Content-Type HTTP header value
137 * containing the given multipart boundary for a ranged HTTP response.
139 * \param multipartBoundary Multipart boundary to be used in the ranged HTTP response
140 * \return multipart/byteranges Content-Type HTTP header value
142 static std::string
GenerateMultipartBoundaryContentType(const std::string
& multipartBoundary
);
145 * \brief Generates a multipart boundary including the Content-Type HTTP
146 * header value with the (actual) given content type of the original
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
153 static std::string
GenerateMultipartBoundaryWithHeader(const std::string
& multipartBoundary
, const std::string
& contentType
);
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.
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
165 static std::string
GenerateMultipartBoundaryWithHeader(const std::string
& multipartBoundary
, const std::string
& contentType
, const CHttpRange
* range
);
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.
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
176 static std::string
GenerateMultipartBoundaryWithHeader(const std::string
& multipartBoundaryWithContentType
, const CHttpRange
* range
);
179 * \brief Generates a multipart boundary end that can be used in ranged HTTP
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
185 static std::string
GenerateMultipartBoundaryEnd(const std::string
& multipartBoundary
);
186 #endif // HAS_WEB_SERVER