Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/include/qhttpengine/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,14 @@ class QHTTPENGINE_EXPORT Socket : public QIODevice
*/
void setHeaders(const HeaderMap &headers);

/**
* @brief Set the SSEEnabled flag
*
* Causes calls to write methods on the socket to leave connection open
* It will also not provide content length to prevent clients from closing connections
*/
void setSSEEnabled(bool enabled = false);

/**
* @brief Write response headers to the socket
*
Expand Down Expand Up @@ -367,6 +375,7 @@ class QHTTPENGINE_EXPORT Socket : public QIODevice

SocketPrivate *const d;
friend class SocketPrivate;
bool sseEnabled;
};

}
Expand Down
33 changes: 23 additions & 10 deletions src/src/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ void SocketPrivate::readData()

Socket::Socket(QTcpSocket *socket, QObject *parent)
: QIODevice(parent),
d(new SocketPrivate(this, socket))
d(new SocketPrivate(this, socket)),
sseEnabled(false)
{
// The device is initially open for both reading and writing
setOpenMode(QIODevice::ReadWrite);
Expand Down Expand Up @@ -301,6 +302,11 @@ void Socket::setHeaders(const HeaderMap &headers)
d->responseHeaders = headers;
}

void Socket::setSSEEnabled(bool enabled)
{
sseEnabled = enabled;
}

void Socket::writeHeaders()
{
// Use a QByteArray for building the header so that we can later determine
Expand Down Expand Up @@ -335,7 +341,7 @@ void Socket::writeRedirect(const QByteArray &path, bool permanent)
setStatusCode(permanent ? MovedPermanently : Found);
setHeader("Location", path);
writeHeaders();
close();
if (!sseEnabled) close();
}

void Socket::writeError(int statusCode, const QByteArray &statusReason)
Expand All @@ -348,23 +354,30 @@ void Socket::writeError(int statusCode, const QByteArray &statusReason)
.arg(d->responseStatusReason.constData())
.arg(QHTTPENGINE_VERSION)
.toUtf8();

if (!sseEnabled)
{
setHeader("Content-Length", QByteArray::number(data.length()));
setHeader("Content-Type", "text/html");

setHeader("Content-Length", QByteArray::number(data.length()));
setHeader("Content-Type", "text/html");
writeHeaders();
}

writeHeaders();
write(data);
close();
if (!sseEnabled) close();
}

void Socket::writeJson(const QJsonDocument &document, int statusCode)
{
QByteArray data = document.toJson();
setStatusCode(statusCode);
setHeader("Content-Length", QByteArray::number(data.length()));
setHeader("Content-Type", "application/json");
if (!sseEnabled)
{
setStatusCode(statusCode);
setHeader("Content-Length", QByteArray::number(data.length()));
setHeader("Content-Type", "application/json");
}
write(data);
close();
if (!sseEnabled) close();
}

qint64 Socket::readData(char *data, qint64 maxlen)
Expand Down