C++ Web Framework  3.0
response.h
1 /*
2  Copyright 2017 Herik Lima de Castro and Marcelo Medeiros Eler
3  Distributed under MIT license, or public domain if desired and
4  recognized in your jurisdiction.
5  See file LICENSE for detail.
6 */
7 
8 #ifndef RESPONSE_H
9 #define RESPONSE_H
10 
11 #include <QFile>
12 #include <QTcpSocket>
13 #include <QTextStream>
14 #include <QJsonDocument>
15 #include <QNetworkCookie>
16 #include "constants.h"
17 #include "cppwebframework_global.h"
18 
19 CWF_BEGIN_NAMESPACE
20 
21 class Configuration;
25 class CPPWEBFRAMEWORKSHARED_EXPORT Response
26 {
27  QTcpSocket *socket;
28  const Configuration &configuration;
29  int statusCode = Response::SC_OK;
30  QByteArray content;
31  QByteArray statusText = HTTP::OK;
32  QMap<QByteArray, QByteArray> headers;
33  QVector<QNetworkCookie> cookies;
34 public:
35  Response(QTcpSocket &socket, const Configuration &configuration);
36 
37  ~Response() noexcept {}
38 
39  void write(const QJsonObject &json, bool writeContentType = true);
40 
41  void write(const QJsonArray &array, bool writeContentType = true);
42 
43  void write(QByteArray &&data);
44 
45  void write(const QByteArray &data, bool flush = true);
46 
47  void sendError(int sc, const QByteArray &msg);
48 
49  void flushBuffer();
50 
51  inline int getBufferSize() const noexcept { return content.size(); }
52 
53  inline void addHeader(const QByteArray &name, const QByteArray &value) noexcept { headers.insert(name, value); }
54 
55  inline void addCookie(const QNetworkCookie &cookie) noexcept { cookies.push_back(cookie); }
56 
57  void setStatus(int statusCode, const QByteArray &description);
58 
59  void sendRedirect(const QByteArray &url);
60 
61  static const int SC_CONTINUE;
62 
63  static const int SC_SWITCHING_PROTOCOLS;
64 
65  static const int SC_OK;
66 
67  static const int SC_CREATED;
68 
69  static const int SC_ACCEPTED;
70 
71  static const int SC_NON_AUTHORITATIVE_INFORMATION;
72 
73  static const int SC_NO_CONTENT;
74 
75  static const int SC_RESET_CONTENT;
76 
77  static const int SC_PARTIAL_CONTENT;
78 
79  static const int SC_MULTIPLE_CHOICES;
80 
81  static const int SC_MOVED_PERMANENTLY;
82 
83  static const int SC_MOVED_TEMPORARILY;
84 
85  static const int SC_FOUND;
86 
87  static const int SC_SEE_OTHER;
88 
89  static const int SC_NOT_MODIFIED;
90 
91  static const int SC_USE_PROXY;
92 
93  static const int SC_TEMPORARY_REDIRECT;
94 
95  static const int SC_BAD_REQUEST;
96 
97  static const int SC_UNAUTHORIZED;
98 
99  static const int SC_PAYMENT_REQUIRED;
100 
101  static const int SC_FORBIDDEN;
102 
103  static const int SC_NOT_FOUND;
104 
105  static const int SC_METHOD_NOT_ALLOWED;
106 
107  static const int SC_NOT_ACCEPTABLE;
108 
109  static const int SC_PROXY_AUTHENTICATION_REQUIRED;
110 
111  static const int SC_REQUEST_TIMEOUT;
112 
113  static const int SC_CONFLICT;
114 
115  static const int SC_GONE;
116 
117  static const int SC_LENGTH_REQUIRED;
118 
119  static const int SC_PRECONDITION_FAILED;
120 
121  static const int SC_REQUEST_ENTITY_TOO_LARGE;
122 
123  static const int SC_REQUEST_URI_TOO_LONG;
124 
125  static const int SC_UNSUPPORTED_MEDIA_TYPE;
126 
127  static const int SC_REQUESTED_RANGE_NOT_SATISFIABLE;
128 
129  static const int SC_EXPECTATION_FAILED;
130 
131  static const int SC_INTERNAL_SERVER_ERROR;
132 
133  static const int SC_NOT_IMPLEMENTED;
134 
135  static const int SC_BAD_GATEWAY;
136 
137  static const int SC_SERVICE_UNAVAILABLE;
138 
139  static const int SC_GATEWAY_TIMEOUT;
140 
141  static const int SC_HTTP_VERSION_NOT_SUPPORTED;
142 };
143 
144 CWF_END_NAMESPACE
145 
146 #endif // RESPONSE_H
All classes of C++ Web Framework are contained within the namespace CWF.
Definition: configuration.h:24
The Response class is responsable to response a Http request.
Definition: response.h:25