Commit 927618d5 authored by Thomas BOUTROUE's avatar Thomas BOUTROUE

Avoid flushing TCP sockets too often

Use string builders when possible
parent 304f2c55
Pipeline #983 passed with stage
in 17 seconds
......@@ -11,7 +11,9 @@
#include <QStringList>
#include <QDateTime>
const QByteArray & QtHttpClientWrapper::CRLF = QByteArrayLiteral ("\r\n");
const char QtHttpClientWrapper::SPACE (' ');
const char QtHttpClientWrapper::COLON (':');
const QByteArray QtHttpClientWrapper::CRLF ("\r\n");
QtHttpClientWrapper::QtHttpClientWrapper (QTcpSocket * sock, QtHttpServer * parent)
: QObject (parent)
......@@ -147,17 +149,16 @@ void QtHttpClientWrapper::onClientDataReceived (void) {
void QtHttpClientWrapper::onReplySendHeadersRequested (void) {
if (QtHttpReply * reply = qobject_cast<QtHttpReply *> (sender ())) {
QByteArray data;
// HTTP Version + Status Code + Status Msg
data.append (QtHttpServer::HTTP_VERSION);
data.append (SPACE);
data.append (QByteArray::number (reply->getStatusCode ()));
data.append (SPACE);
data.append (QtHttpReply::getStatusTextForCode (reply->getStatusCode ()));
data.append (CRLF);
QByteArray data = (QtHttpServer::HTTP_VERSION %
SPACE %
QByteArray::number (reply->getStatusCode ()) %
SPACE %
QtHttpReply::getStatusTextForCode (reply->getStatusCode ()) %
CRLF);
// Header name: header value
if (reply->useChunked ()) {
static const QByteArray & CHUNKED = QByteArrayLiteral ("chunked");
static const QByteArray CHUNKED ("chunked");
reply->addHeader (QtHttpHeader::TransferEncoding, CHUNKED);
}
else {
......@@ -165,16 +166,16 @@ void QtHttpClientWrapper::onReplySendHeadersRequested (void) {
}
const QList<QByteArray> & headersList = reply->getHeadersList ();
foreach (const QByteArray & header, headersList) {
data.append (header);
data.append (COLON);
data.append (SPACE);
data.append (reply->getHeader (header));
data.append (CRLF);
data += (header %
COLON %
SPACE %
reply->getHeader (header) %
CRLF);
}
// empty line
data.append (CRLF);
data += CRLF;
m_sockClient->write (data);
m_sockClient->flush ();
//m_sockClient->flush ();
}
}
......@@ -189,7 +190,7 @@ void QtHttpClientWrapper::onReplySendDataRequested (void) {
}
// write to socket
m_sockClient->write (data);
m_sockClient->flush ();
//m_sockClient->flush ();
}
}
......@@ -203,12 +204,14 @@ QtHttpClientWrapper::ParsingStatus QtHttpClientWrapper::sendReplyToClient (QtHtt
}
else {
// last chunk
m_sockClient->write ("0" % CRLF % CRLF);
m_sockClient->flush ();
m_sockClient->write ("0");
m_sockClient->write (CRLF);
m_sockClient->write (CRLF);
//m_sockClient->flush ();
}
}
if (m_currentRequest != Q_NULLPTR) {
static const QByteArray & CLOSE = QByteArrayLiteral ("close");
static const QByteArray CLOSE ("close");
if (m_currentRequest->getHeader (QtHttpHeader::Connection).toLower () == CLOSE) {
// must close connection after this request
m_sockClient->close ();
......
......@@ -3,6 +3,7 @@
#include <QObject>
#include <QString>
#include <QByteArray>
class QTcpSocket;
......@@ -17,9 +18,9 @@ public:
explicit QtHttpClientWrapper (QTcpSocket * sock, QtHttpServer * parent);
virtual ~QtHttpClientWrapper (void);
static const char SPACE = ' ';
static const char COLON = ':';
static const QByteArray & CRLF;
static const char SPACE;
static const char COLON;
static const QByteArray CRLF;
enum ParsingStatus {
ParsingError = -1,
......
......@@ -6,7 +6,7 @@
#include <QDebug>
const QString & QtHttpServer::HTTP_VERSION = QStringLiteral ("HTTP/1.1");
const QByteArray QtHttpServer::HTTP_VERSION ("HTTP/1.1");
QtHttpServerWrapper::QtHttpServerWrapper (QObject * parent)
: QTcpServer (parent)
......
......@@ -40,7 +40,7 @@ class QtHttpServer : public QObject {
public:
explicit QtHttpServer (QObject * parent = Q_NULLPTR);
static const QString & HTTP_VERSION;
static const QByteArray HTTP_VERSION;
typedef void (QSslSocket::* SslErrorSignal) (const QList<QSslError> &);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment