14 #include <QStringList> 21 m_connection = parent;
38 QString path = request->
path();
39 QStringList parts = path.split(
"/");
41 if (parts.size() <= 1) {
42 serviceError(request,
response, 404,
"not-found",
"Object not found");
48 switch (parts.size()) {
50 if (request->
method() !=
"GET") {
51 serviceError(request,
response, 405,
"bad-request",
"Method not allowed");
58 if (parts[1] ==
"events") {
59 if (request->
method() !=
"GET") {
60 serviceError(request,
response, 405,
"bad-request",
"Method not allowed");
71 if (parts[1] !=
"events") {
72 QString
id = parts[1];
73 if (request->
method() ==
"GET") {
77 serviceError(request,
response, 404,
"not-found",
"Not found");
83 if (request->
method() ==
"PUT") {
88 if (m_checkId && !data.contains(
"id")) {
89 serviceError(request,
response, 400,
"bad-request",
"Data must contain ID");
93 serviceIdPut(request,
response, data);
96 if (request->
method() ==
"POST") {
101 if (m_checkId && !data.contains(
"id")) {
102 serviceError(request,
response, 400,
"bad-request",
"Data must contain ID");
106 serviceIdPost(request,
response, data);
109 if (request->
method() ==
"DELETE") {
113 serviceError(request,
response, 404,
"not-found",
"Not found");
116 serviceIdDelete(request,
response,
id);
123 if (parts[1] ==
"events" || parts[2] !=
"events") {
124 serviceError(request,
response, 404,
"not-found",
"Not found");
128 if (request->
method() !=
"GET") {
129 serviceError(request,
response, 405,
"bad-request",
"Method not allowed");
134 serviceError(request,
response, 404,
"not-found",
"Not found");
142 serviceIdEvents(request,
response, parts[1]);
146 serviceError(request,
response, 400,
"bad-request",
"Request could not be recognized");
151 void AbstractController::serviceError(
155 const QString& error,
156 const QString& reason) {
161 data[
"error"] = error;
162 data[
"reason"] = reason;
176 const QVariant& data) {
191 void AbstractController::serviceEvent (
194 const QVariant& data) {
200 lresponse->
setHeader(
"Transfer-Encoding",
"chunked");
201 lresponse->
setHeader(
"Content-Type",
"text/event-stream");
202 lresponse->
setHeader(
"Cache-Control",
"no-cache,public");
206 datagram +=
"event: status\n";
207 datagram +=
"data: ";
211 lresponse->
write(datagram);
213 lresponse->flushAndDelete();
217 serviceError(request,
response, 501,
"not-implemeted",
"Method not implemented");
222 serviceError(request,
response, 501,
"not-implemeted",
"Method not implemented");
227 serviceError(request,
response, 501,
"not-implemeted",
"Method not implemented");
232 serviceError(request,
response, 501,
"not-implemeted",
"Method not implemented");
237 serviceError(request,
response, 501,
"not-implemeted",
"Method not implemented");
242 serviceError(request,
response, 501,
"not-implemeted",
"Method not implemented");
247 serviceError(request,
response, 501,
"not-implemeted",
"Method not implemented");
virtual void serviceOK(HobrasoftHttpd::HttpRequest *request, HobrasoftHttpd::HttpResponse *response, const QVariant &data=QVariant())
Sends 200 OK response.
AbstractController(HobrasoftHttpd::HttpConnection *parent)
Constructor.
HttpResponse * response()
Returns new instance of HttpResponse class.
QString path() const
Returns path of the request (/files/index.html)
HttpConnection * connection() const
Returns pointer to parent HttpConnection class.
virtual void service(HobrasoftHttpd::HttpRequest *request, HobrasoftHttpd::HttpResponse *response)
Request processing.
Name space for own application HTTP server.
void flush()
Writes last part of the response and closes the socket when possible.
void setSendHeaders(bool send)
Sets the header sending.
virtual bool exists(const QString &id) const
Check existence of th ID.
Response to HTTP request - headers, cookies and body.
void flushSocket()
Flushed sockets data to network.
void write(const QByteArray &data)
Writes data to response body.
static QVariant data(const QByteArray &json)
Converts json to data.
static QByteArray json(const QVariant &data)
Converts data to json.
bool containsHeader(const QString &name)
Returns true if headers contains specific header.
HttpResponse * response()
Returns new instance of class HttpResponse.
QByteArray body() const
Returns the body of the request.
Processes HTTP request, parses headers, body and files sent by HTTP protocol.
void setStatus(int code, const QString &description=QString())
Set the status code and the description of the response.
void setHeader(const QString &name, const QString &value)
Sets or rewrite one header.
void clearHeaders()
Clears all headers set.
QString method() const
Returns used HTTP method of the request (GET, PUT, POST or DELETE)
Namespace of HTTP server.
One single connection to http server.