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
25PROMEKI_NAMESPACE_BEGIN
26
63class HttpRequest {
64 PROMEKI_SHARED_FINAL(HttpRequest)
65 public:
67 using Ptr = SharedPtr<HttpRequest>;
68
70 using List = ::promeki::List<HttpRequest>;
71
73 using PtrList = ::promeki::List<Ptr>;
74
76 static const String DefaultHttpVersion;
77
79 HttpRequest() = default;
80
82 const HttpMethod &method() const { return _method; }
83
85 void setMethod(const HttpMethod &m) { _method = m; }
86
95 const Url &url() const { return _url; }
96
98 void setUrl(const Url &u) { _url = u; }
99
101 const String &path() const { return _url.path(); }
102
104 String queryValue(const String &key, const String &defaultValue = String()) const {
105 return _url.queryValue(key, defaultValue);
106 }
107
109 const HttpHeaders &headers() const { return _headers; }
110
112 HttpHeaders &headers() { return _headers; }
113
115 void setHeaders(const HttpHeaders &h) { _headers = h; }
116
118 String header(const String &name, const String &defaultValue = String()) const {
119 return _headers.value(name, defaultValue);
120 }
121
123 const Buffer &body() const { return _body; }
124
126 void setBody(const Buffer &body) { _body = body; }
127
134 void setBody(const String &text);
135
144 String bodyAsString() const;
145
154 JsonObject bodyAsJson(Error *err = nullptr) const;
155
164 JsonArray bodyAsJsonArray(Error *err = nullptr) const;
165
172 void setBody(const JsonObject &obj);
173
175 void setBody(const JsonArray &arr);
176
178 const HashMap<String, String> &pathParams() const { return _pathParams; }
179
181 void setPathParams(const HashMap<String, String> &params) { _pathParams = params; }
182
188 String pathParam(const String &name, const String &defaultValue = String()) const {
189 return _pathParams.value(name, defaultValue);
190 }
191
199 const String &httpVersion() const { return _httpVersion; }
200
202 void setHttpVersion(const String &v) { _httpVersion = v; }
203
211 const String &peerAddress() const { return _peerAddress; }
212
214 void setPeerAddress(const String &s) { _peerAddress = s; }
215
217 bool operator==(const HttpRequest &other) const;
218
220 bool operator!=(const HttpRequest &other) const { return !(*this == other); }
221
222 private:
223 HttpMethod _method;
224 Url _url;
225 HttpHeaders _headers;
226 Buffer _body;
227 HashMap<String, String> _pathParams;
228 String _httpVersion = DefaultHttpVersion;
229 String _peerAddress;
230};
231
232PROMEKI_NAMESPACE_END
233
234#endif // PROMEKI_ENABLE_HTTP