[PVR][Estuary] Timer settings dialog: Show client name in timer type selection dialog...
[xbmc.git] / xbmc / utils / HttpRangeUtils.h
blob7e0b66de1bf7a033ae4a0150f4daf9092bca0b51
1 /*
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.
7 */
9 #pragma once
11 #include <stdint.h>
12 #include <string>
13 #include <vector>
15 class CHttpRange
17 public:
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;
36 protected:
37 uint64_t m_first = 1;
38 uint64_t m_last = 0;
41 typedef std::vector<CHttpRange> HttpRanges;
43 class CHttpResponseRange : public CHttpRange
45 public:
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;
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;
62 protected:
63 const void* m_data;
66 typedef std::vector<CHttpResponseRange> HttpResponseRanges;
68 class CHttpRanges final
70 public:
71 CHttpRanges();
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);
89 void Clear();
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);
97 protected:
98 void SortAndCleanup();
100 HttpRanges m_ranges;
103 class HttpRangeUtils
105 public:
107 * \brief Generates a valid Content-Range HTTP header value for the given HTTP
108 * range definition.
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
117 * range properties.
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
129 * responses.
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
147 * content.
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
180 * responses.
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