libpromeki 1.0.0-alpha
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
httpresponse.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 <functional>
14#include <promeki/function.h>
15#include <promeki/namespace.h>
16#include <promeki/string.h>
17#include <promeki/buffer.h>
18#include <promeki/sharedptr.h>
19#include <promeki/list.h>
20#include <promeki/iodevice.h>
21#include <promeki/httpstatus.h>
22#include <promeki/httpheaders.h>
23#include <promeki/json.h>
24
25PROMEKI_NAMESPACE_BEGIN
26
27class TcpSocket;
28
71class HttpResponse {
72 PROMEKI_SHARED_FINAL(HttpResponse)
73 public:
75 using Ptr = SharedPtr<HttpResponse>;
76
78 using List = ::promeki::List<HttpResponse>;
79
81 using PtrList = ::promeki::List<Ptr>;
82
84 static const String DefaultHttpVersion;
85
87 HttpResponse() = default;
88
90 const HttpStatus &status() const { return _status; }
91
93 void setStatus(const HttpStatus &s) {
94 _status = s;
95 _customReason.clear();
96 }
97
105 void setStatus(int code) {
106 _status = HttpStatus{code};
107 _customReason.clear();
108 }
109
117 String reasonPhrase() const;
118
120 void setReasonPhrase(const String &s) { _customReason = s; }
121
123 const HttpHeaders &headers() const { return _headers; }
124
126 HttpHeaders &headers() { return _headers; }
127
129 void setHeaders(const HttpHeaders &h) { _headers = h; }
130
132 void setHeader(const String &name, const String &value) { _headers.set(name, value); }
133
135 void addHeader(const String &name, const String &value) { _headers.add(name, value); }
136
138 const Buffer &body() const { return _body; }
139
146 bool hasBodyStream() const { return _bodyStream.isValid(); }
147
155 IODevice *bodyStream() const {
156 return _bodyStream.isValid() ? const_cast<IODevice *>(_bodyStream.ptr()) : nullptr;
157 }
158
166 const IODevice::Shared &bodyStreamShared() const { return _bodyStream; }
167
176 int64_t bodyStreamLength() const { return _bodyStreamLength; }
177
188 IODevice::Shared takeBodyStream();
189
191 void setBody(const Buffer &body);
192
194 void setBody(const String &text);
195
203 void setText(const String &text);
204
208 void setHtml(const String &html);
209
213 void setBinary(const Buffer &body, const String &mimeType);
214
221 void setJson(const JsonObject &obj);
222
224 void setJson(const JsonArray &arr);
225
227 void setJsonPretty(const JsonObject &obj, unsigned int indent = 2);
228
230 void setJsonPretty(const JsonArray &arr, unsigned int indent = 2);
231
245 void setBodyStream(IODevice::Shared device, int64_t length = -1, const String &mimeType = String());
246
248 bool isSuccess() const { return _status.isSuccess(); }
250 bool isRedirect() const { return _status.isRedirect(); }
252 bool isError() const { return _status.isError(); }
253
255 const String &httpVersion() const { return _httpVersion; }
256
258 void setHttpVersion(const String &v) { _httpVersion = v; }
259
260 // ============================================================
261 // Convenience factories
262 // ============================================================
263
265 static HttpResponse ok(const JsonObject &obj);
266
268 static HttpResponse ok(const String &text);
269
277 static HttpResponse notFound(const String &message = String());
278
280 static HttpResponse badRequest(const String &message = String());
281
283 static HttpResponse methodNotAllowed(const String &allow);
284
286 static HttpResponse internalError(const String &message = String());
287
289 static HttpResponse noContent();
290
291 // ============================================================
292 // Protocol upgrade hook
293 // ============================================================
294
310 using UpgradeHook = Function<void(TcpSocket *socket)>;
311
313 void setUpgradeHook(UpgradeHook hook) { _upgradeHook = std::move(hook); }
314
316 const UpgradeHook &upgradeHook() const { return _upgradeHook; }
317
318 private:
319 HttpStatus _status = HttpStatus::Ok;
320 String _customReason;
321 HttpHeaders _headers;
322 Buffer _body;
323 IODevice::Shared _bodyStream;
324 int64_t _bodyStreamLength = -1;
325 String _httpVersion = DefaultHttpVersion;
326 UpgradeHook _upgradeHook;
327};
328
329PROMEKI_NAMESPACE_END
330
331#endif // PROMEKI_ENABLE_HTTP