cutelyst  4.9.0
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
protocol.cpp
1 /*
2  * SPDX-FileCopyrightText: (C) 2016-2018 Daniel Nicoletti <dantti12@gmail.com>
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 #include "protocol.h"
6 
7 #include "server.h"
8 #include "socket.h"
9 
10 #include <Cutelyst/Server/cutelyst_server_export.h>
11 
12 #include <QBuffer>
13 #include <QLoggingCategory>
14 #include <QTemporaryFile>
15 
16 Q_LOGGING_CATEGORY(CUTELYST_SERVER_PROTO, "cutelyst.server.proto", QtWarningMsg)
17 Q_LOGGING_CATEGORY(CUTELYST_SERVER_STATS, "cutelyst.server.stats", QtWarningMsg)
18 
19 using namespace Cutelyst;
20 
21 ProtocolData::ProtocolData(Cutelyst::Socket *_sock, int bufferSize)
22  : sock(_sock)
23  , io(dynamic_cast<QIODevice *>(_sock))
24  , buffer(new char[bufferSize])
25 {
26 }
27 
28 ProtocolData::~ProtocolData()
29 {
30  delete[] buffer;
31 }
32 
33 Cutelyst::Protocol::Protocol(Cutelyst::Server *server)
34  : m_postBufferSize{qMax(static_cast<qint64>(32), server->postBufferingBufsize())}
35  , m_postBuffering{server->postBuffering()}
36  , m_postBuffer{new char[server->postBufferingBufsize()]}
37  , m_bufferSize{server->bufferSize()}
38  , useStats{CUTELYST_SERVER_STATS().isDebugEnabled()}
39 {
40 }
41 
42 Cutelyst::Protocol::~Protocol()
43 {
44  delete[] m_postBuffer;
45 }
46 
47 Cutelyst::Protocol::Type Cutelyst::Protocol::type() const
48 {
49  return Protocol::Type::Unknown;
50 }
51 
52 QIODevice *Cutelyst::Protocol::createBody(qint64 contentLength) const
53 {
54  QIODevice *body;
55  if (m_postBuffering && contentLength > m_postBuffering) {
56  auto temp = new QTemporaryFile;
57  if (!temp->open()) {
58  qCWarning(CUTELYST_SERVER_PROTO)
59  << "Failed to open temporary file to store post" << temp->errorString();
60  // On error close connection immediately
61  return nullptr;
62  }
63  body = temp;
64  } else if (m_postBuffering && contentLength <= m_postBuffering) {
65  auto buffer = new QBuffer;
66  buffer->open(QIODevice::ReadWrite);
67  buffer->buffer().reserve(int(contentLength));
68  body = buffer;
69  } else {
70  // Unbuffered
71  auto buffer = new QBuffer;
72  buffer->open(QIODevice::ReadWrite);
73  buffer->buffer().reserve(int(contentLength));
74  body = buffer;
75  }
76  return body;
77 }
78 
79 #include "moc_protocol.cpp"
QString errorString() const const
Implements a web server.
Definition: server.h:59
virtual bool open(OpenMode flags) override
The Cutelyst namespace holds all public Cutelyst API.