Browse Source

Merge pull request #15831 from Chocobo1/server

Improvements for WebAPI server
adaptive-webui-19844
Chocobo1 3 years ago committed by GitHub
parent
commit
87e1a14a4b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 13
      src/base/http/connection.cpp
  2. 4
      src/base/http/connection.h
  3. 18
      src/base/http/responsegenerator.cpp

13
src/base/http/connection.cpp

@ -45,8 +45,18 @@ Connection::Connection(QTcpSocket *socket, IRequestHandler *requestHandler, QObj
, m_requestHandler(requestHandler) , m_requestHandler(requestHandler)
{ {
m_socket->setParent(this); m_socket->setParent(this);
// reset timer when there are activity
m_idleTimer.start();
connect(m_socket, &QIODevice::readyRead, this, [this]()
{
m_idleTimer.start();
read();
});
connect(m_socket, &QIODevice::bytesWritten, this, [this]()
{
m_idleTimer.start(); m_idleTimer.start();
connect(m_socket, &QTcpSocket::readyRead, this, &Connection::read); });
} }
Connection::~Connection() Connection::~Connection()
@ -56,7 +66,6 @@ Connection::~Connection()
void Connection::read() void Connection::read()
{ {
m_idleTimer.restart();
m_receivedData.append(m_socket->readAll()); m_receivedData.append(m_socket->readAll());
while (!m_receivedData.isEmpty()) while (!m_receivedData.isEmpty())

4
src/base/http/connection.h

@ -52,11 +52,9 @@ namespace Http
bool hasExpired(qint64 timeout) const; bool hasExpired(qint64 timeout) const;
bool isClosed() const; bool isClosed() const;
private slots:
void read();
private: private:
static bool acceptsGzipEncoding(QString codings); static bool acceptsGzipEncoding(QString codings);
void read();
void sendResponse(const Response &response) const; void sendResponse(const Response &response) const;
QTcpSocket *m_socket; QTcpSocket *m_socket;

18
src/base/http/responsegenerator.cpp

@ -42,19 +42,23 @@ QByteArray Http::toByteArray(Response response)
response.headers[HEADER_DATE] = httpDate(); response.headers[HEADER_DATE] = httpDate();
QByteArray buf; QByteArray buf;
buf.reserve(10 * 1024); buf.reserve(1024 + response.content.length());
// Status Line // Status Line
buf += QString("HTTP/%1 %2 %3") buf.append("HTTP/1.1 ") // TODO: depends on request
.arg("1.1", // TODO: depends on request .append(QByteArray::number(response.status.code))
QString::number(response.status.code), .append(' ')
response.status.text) .append(response.status.text.toLatin1())
.toLatin1()
.append(CRLF); .append(CRLF);
// Header Fields // Header Fields
for (auto i = response.headers.constBegin(); i != response.headers.constEnd(); ++i) for (auto i = response.headers.constBegin(); i != response.headers.constEnd(); ++i)
buf += QString::fromLatin1("%1: %2").arg(i.key(), i.value()).toLatin1().append(CRLF); {
buf.append(i.key().toLatin1())
.append(": ")
.append(i.value().toLatin1())
.append(CRLF);
}
// the first empty line // the first empty line
buf += CRLF; buf += CRLF;

Loading…
Cancel
Save