HTTP v1.13.1
Loading...
Searching...
No Matches
HTTPUtility.h
Go to the documentation of this file.
1#pragma once
2
3#include <ostream>
4#include <unordered_map>
5#include <string>
6#include <optional>
7
8#ifdef HTTP_DLL
9#ifdef __LINUX__
10#define HTTP_API __attribute__((visibility("default")))
11#else
12#define HTTP_API __declspec(dllexport)
13#endif
14#define HTTP_API_FUNCTION extern "C" HTTP_API
15#define JSON_DLL
16#else
17#define HTTP_API
18#define HTTP_API_FUNCTION
19#endif // HTTP_DLL
20
21namespace web
22{
24 enum class ResponseCodes
25 {
26 Continue = 100,
29 ok = 200,
30 created,
38 IMUsed = 226,
39 multipleChoices = 300,
41 found,
47 badRequest = 400,
57 gone,
69 locked,
71 upgradeRequired = 426,
75 retryWith = 449,
90 unknownError = 520,
97 };
98
105 inline std::ostream& operator << (std::ostream& stream, ResponseCodes responseCode)
106 {
107 return stream << static_cast<int>(responseCode);
108 }
109
116 inline bool operator == (int code, ResponseCodes otherCode)
117 {
118 return code == static_cast<int>(otherCode);
119 }
120
126
132 HTTP_API_FUNCTION std::string encodeUrl(std::string_view data);
133
139 HTTP_API_FUNCTION std::string decodeUrl(std::string_view data);
140
147 template<typename T> requires (std::same_as<T, ResponseCodes> || std::convertible_to<T, int>)
148 std::string getMessageFromCode(const T& code);
149
150 HTTP_API_FUNCTION std::string __getMessageFromCode(int code);
151
154 {
155 size_t operator () (const std::string& value) const;
156 };
157
160 {
161 bool operator () (const std::string& left, const std::string& right) const;
162 };
163
168 {
169 private:
170 std::string name;
171 std::optional<std::string> fileName;
172 std::optional<std::string> contentType;
173 std::string data;
174
175 public:
176 Multipart(std::string_view data);
177
178 Multipart(std::string_view name, const std::optional<std::string>& fileName, const std::optional<std::string>& contentType, std::string&& data);
179
180 const std::string& getName() const;
181
182 const std::optional<std::string>& getFileName() const;
183
184 const std::optional<std::string>& getContentType() const;
185
186 const std::string& getData() const;
187
188 ~Multipart() = default;
189 };
190
194 using HeadersMap = std::unordered_map<std::string, std::string, InsensitiveStringHash, InsensitiveStringEqual>;
195}
196
197namespace web
198{
199 template<typename T> requires (std::same_as<T, ResponseCodes> || std::convertible_to<T, int>)
200 std::string getMessageFromCode(const T& code)
201 {
202 return __getMessageFromCode(static_cast<int>(code));
203 }
204}
#define HTTP_API_FUNCTION
Definition HTTPUtility.h:18
#define HTTP_API
Definition HTTPUtility.h:17
Defines each part of multipart/form-data.
Multipart(std::string_view data)
~Multipart()=default
Multipart(std::string_view name, const std::optional< std::string > &fileName, const std::optional< std::string > &contentType, std::string &&data)
std::unordered_map< std::string, std::string, InsensitiveStringHash, InsensitiveStringEqual > HeadersMap
Case insensitive unordered_map.
ResponseCodes
Response codes.
Definition HTTPUtility.h:25
std::string getMessageFromCode(const T &code)
Get response message from response code.
string __getMessageFromCode(int code)
ostream & operator<<(ostream &outputStream, const HTTPBuilder &builder)
string getHTTPLibraryVersion()
Get version of HTTP library.
string decodeUrl(string_view data)
string encodeUrl(string_view data)
bool operator==(int code, ResponseCodes otherCode)
Compare operator for ResponseCodes.
Custom equal for headers.
Custom hashing for headers with case insensitive.