cutelyst  4.3.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/cutelyst_global.h>
11 
12 #include <QBuffer>
13 #include <QLoggingCategory>
14 #include <QTemporaryFile>
15 
16 Q_LOGGING_CATEGORY(C_SERVER_PROTO, "cutelyst.server.proto", QtWarningMsg)
17 #if defined(CUTELYST_SERVER_EXPORT_STATS)
18 Q_LOGGING_CATEGORY(CUTELYST_STATS, "cutelyst.stats", QtWarningMsg)
19 #else
20 Q_DECLARE_LOGGING_CATEGORY(CUTELYST_STATS)
21 #endif
22 
23 using namespace Cutelyst;
24 
25 ProtocolData::ProtocolData(Cutelyst::Socket *_sock, int bufferSize)
26  : sock(_sock)
27  , io(dynamic_cast<QIODevice *>(_sock))
28  , buffer(new char[bufferSize])
29 {
30 }
31 
32 ProtocolData::~ProtocolData()
33 {
34  delete[] buffer;
35 }
36 
37 Cutelyst::Protocol::Protocol(Cutelyst::Server *server)
38  : m_postBufferSize{qMax(static_cast<qint64>(32), server->postBufferingBufsize())}
39  , m_postBuffering{server->postBuffering()}
40  , m_postBuffer{new char[server->postBufferingBufsize()]}
41  , m_bufferSize{server->bufferSize()}
42  , useStats{CUTELYST_STATS().isDebugEnabled()}
43 {
44 }
45 
46 Cutelyst::Protocol::~Protocol()
47 {
48  delete[] m_postBuffer;
49 }
50 
51 Cutelyst::Protocol::Type Cutelyst::Protocol::type() const
52 {
53  return Protocol::Type::Unknown;
54 }
55 
56 QIODevice *Cutelyst::Protocol::createBody(qint64 contentLength) const
57 {
58  QIODevice *body;
59  if (m_postBuffering && contentLength > m_postBuffering) {
60  auto temp = new QTemporaryFile;
61  if (!temp->open()) {
62  qCWarning(C_SERVER_PROTO)
63  << "Failed to open temporary file to store post" << temp->errorString();
64  // On error close connection immediately
65  return nullptr;
66  }
67  body = temp;
68  } else if (m_postBuffering && contentLength <= m_postBuffering) {
69  auto buffer = new QBuffer;
70  buffer->open(QIODevice::ReadWrite);
71  buffer->buffer().reserve(int(contentLength));
72  body = buffer;
73  } else {
74  // Unbuffered
75  auto buffer = new QBuffer;
76  buffer->open(QIODevice::ReadWrite);
77  buffer->buffer().reserve(int(contentLength));
78  body = buffer;
79  }
80  return body;
81 }
82 
83 #include "moc_protocol.cpp"
QString errorString() const const
Implements a web server.
Definition: server.h:59
virtual bool open(QIODeviceBase::OpenMode flags) override
The Cutelyst namespace holds all public Cutelyst API.