libpromeki 1.0.0-alpha
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
httprequest.h
Go to the documentation of this file.
1
8#pragma once
9
10
11#include <promeki/config.h>
12#if PROMEKI_ENABLE_HTTP
13#include <promeki/namespace.h>
14#include <promeki/string.h>
15#include <promeki/buffer.h>
16#include <promeki/url.h>
17#include <promeki/hashmap.h>
18#include <promeki/sharedptr.h>
19#include <promeki/list.h>
20#include <promeki/error.h>
21#include <promeki/httpmethod.h>
22#include <promeki/httpheaders.h>
23#include <promeki/json.h>
24#include <promeki/function.h>
25
26PROMEKI_NAMESPACE_BEGIN
27
64class HttpRequest {
65 PROMEKI_SHARED_FINAL(HttpRequest)
66 public:
68 using Ptr = SharedPtr<HttpRequest>;
69
71 using List = ::promeki::List<HttpRequest>;
72
74 using PtrList = ::promeki::List<Ptr>;
75
77 static const String DefaultHttpVersion;
78
80 HttpRequest() = default;
81
83 const HttpMethod &method() const { return _method; }
84
86 void setMethod(const HttpMethod &m) { _method = m; }
87
96 const Url &url() const { return _url; }
97
99 void setUrl(const Url &u) { _url = u; }
100
102 const String &path() const { return _url.path(); }
103
105 String queryValue(const String &key, const String &defaultValue = String()) const {
106 return _url.queryValue(key, defaultValue);
107 }
108
110 const HttpHeaders &headers() const { return _headers; }
111
113 HttpHeaders &headers() { return _headers; }
114
116 void setHeaders(const HttpHeaders &h) { _headers = h; }
117
119 String header(const String &name, const String &defaultValue = String()) const {
120 return _headers.value(name, defaultValue);
121 }
122
124 const Buffer &body() const { return _body; }
125
127 void setBody(const Buffer &body) { _body = body; }
128
135 void setBody(const String &text);
136
145 String bodyAsString() const;
146
155 JsonObject bodyAsJson(Error *err = nullptr) const;
156
165 JsonArray bodyAsJsonArray(Error *err = nullptr) const;
166
173 void setBody(const JsonObject &obj);
174
176 void setBody(const JsonArray &arr);
177
195 using BodySink = ::promeki::Function<Error(const void *data, size_t len)>;
196
213 using ProgressCallback = ::promeki::Function<bool(int64_t received, int64_t total)>;
214
216 const BodySink &bodySink() const { return _bodySink; }
217
225 void setBodySink(BodySink sink) { _bodySink = std::move(sink); }
226
228 const ProgressCallback &progressCallback() const { return _progressCallback; }
229
236 void setProgressCallback(ProgressCallback cb) { _progressCallback = std::move(cb); }
237
239 const HashMap<String, String> &pathParams() const { return _pathParams; }
240
242 void setPathParams(const HashMap<String, String> &params) { _pathParams = params; }
243
249 String pathParam(const String &name, const String &defaultValue = String()) const {
250 return _pathParams.value(name, defaultValue);
251 }
252
260 const String &httpVersion() const { return _httpVersion; }
261
263 void setHttpVersion(const String &v) { _httpVersion = v; }
264
272 const String &peerAddress() const { return _peerAddress; }
273
275 void setPeerAddress(const String &s) { _peerAddress = s; }
276
278 bool operator==(const HttpRequest &other) const;
279
281 bool operator!=(const HttpRequest &other) const { return !(*this == other); }
282
283 private:
284 HttpMethod _method;
285 Url _url;
286 HttpHeaders _headers;
287 Buffer _body;
288 HashMap<String, String> _pathParams;
289 String _httpVersion = DefaultHttpVersion;
290 String _peerAddress;
291 BodySink _bodySink;
292 ProgressCallback _progressCallback;
293};
294
295PROMEKI_NAMESPACE_END
296
297#endif // PROMEKI_ENABLE_HTTP