Showing the changes to libraries/embedded-webserver/src/HTTPConnection.cpp (1 of 2 files). Show all
Unified · Split
| @@ -133,57 +133,33 @@ QList<FormData> HTTPConnection::parseFormData() const { · 132 unmodified lines | |||
| 133 | } | 133 | } |
| 134 | 134 | ||
| 135 | void HTTPConnection::respond(const char* code, const QByteArray& content, const char* contentType, const Headers& headers) { | 135 | void HTTPConnection::respond(const char* code, const QByteArray& content, const char* contentType, const Headers& headers) { |
| 136 | QByteArray data(content); | 136 | respondWithStatusAndHeaders(code, contentType, headers, content.size()); |
| 137 | auto device { std::unique_ptr<QBuffer>(new QBuffer()) }; | 137 | |
| 138 | device->setBuffer(new QByteArray(content)); | 138 | _socket->write(content); |
| 139 | if (device->open(QIODevice::ReadOnly)) { | 139 | |
| 140 | respond(code, std::move(device), contentType, headers); | 140 | _socket->disconnectFromHost(); |
| 141 | } else { | 141 | |
| 142 | qCritical() << "Error opening QBuffer to respond to " << _requestUrl.path(); | 142 | // make sure we receive no further read notifications |
| 143 | } | 143 | disconnect(_socket, &QTcpSocket::readyRead, this, nullptr); |
| 144 | } | 144 | } |
| 145 | 145 | ||
| 146 | void HTTPConnection::respond(const char* code, std::unique_ptr<QIODevice> device, const char* contentType, const Headers& headers) { | 146 | void HTTPConnection::respond(const char* code, std::unique_ptr<QIODevice> device, const char* contentType, const Headers& headers) { |
| 147 | _responseDevice = std::move(device); | 147 | _responseDevice = std::move(device); |
| 148 | 148 | ||
| 149 | _socket->write("HTTP/1.1 "); | ||
| 150 | |||
| 151 | if (_responseDevice->isSequential()) { | 149 | if (_responseDevice->isSequential()) { |
| 152 | qWarning() << "Error responding to HTTPConnection: sequential IO devices not supported"; | 150 | qWarning() << "Error responding to HTTPConnection: sequential IO devices not supported"; |
| 153 | _socket->write(StatusCode500); | 151 | respondWithStatusAndHeaders(StatusCode500, contentType, headers, 0); |
| 154 | _socket->write("\r\n"); | ||
| 155 | _socket->disconnect(SIGNAL(readyRead()), this); | 152 | _socket->disconnect(SIGNAL(readyRead()), this); |
| 156 | _socket->disconnectFromHost(); | 153 | _socket->disconnectFromHost(); |
| 157 | return; | 154 | return; |
| 158 | } | 155 | } |
| 159 | 156 | ||
| 160 | _socket->write(code); | 157 | int totalToBeWritten = _responseDevice->size(); |
| 161 | _socket->write("\r\n"); | 158 | respondWithStatusAndHeaders(code, contentType, headers, totalToBeWritten); |
| 162 | |||
| 163 | for (Headers::const_iterator it = headers.constBegin(), end = headers.constEnd(); | ||
| 164 | it != end; it++) { | ||
| 165 | _socket->write(it.key()); | ||
| 166 | _socket->write(": "); | ||
| 167 | _socket->write(it.value()); | ||
| 168 | _socket->write("\r\n"); | ||
| 169 | } | ||
| 170 | |||
| 171 | int csize = _responseDevice->size(); | ||
| 172 | if (csize > 0) { | ||
| 173 | _socket->write("Content-Length: "); | ||
| 174 | _socket->write(QByteArray::number(csize)); | ||
| 175 | _socket->write("\r\n"); | ||
| 176 | |||
| 177 | _socket->write("Content-Type: "); | ||
| 178 | _socket->write(contentType); | ||
| 179 | _socket->write("\r\n"); | ||
| 180 | } | ||
| 181 | _socket->write("Connection: close\r\n\r\n"); | ||
| 182 | 159 | ||
| 183 | if (_responseDevice->atEnd()) { | 160 | if (_responseDevice->atEnd()) { |
| 184 | _socket->disconnectFromHost(); | 161 | _socket->disconnectFromHost(); |
| 185 | } else { | 162 | } else { |
| 186 | int totalToBeWritten = csize; | ||
| 187 | connect(_socket, &QTcpSocket::bytesWritten, this, [this, totalToBeWritten](size_t bytes) mutable { | 163 | connect(_socket, &QTcpSocket::bytesWritten, this, [this, totalToBeWritten](size_t bytes) mutable { |
| 188 | constexpr size_t HTTP_RESPONSE_CHUNK_SIZE = 1024 * 10; | 164 | constexpr size_t HTTP_RESPONSE_CHUNK_SIZE = 1024 * 10; |
| 189 | if (!_responseDevice->atEnd()) { | 165 | if (!_responseDevice->atEnd()) { |
| @@ -201,6 +177,32 @@ void HTTPConnection::respond(const char* code, std::unique_ptr<QIODevice> device · 11 unmodified lines | |||
| 201 | disconnect(_socket, &QTcpSocket::readyRead, this, nullptr); | 177 | disconnect(_socket, &QTcpSocket::readyRead, this, nullptr); |
| 202 | } | 178 | } |
| 203 | 179 | ||
| 180 | void HTTPConnection::respondWithStatusAndHeaders(const char* code, const char* contentType, const Headers& headers, int64_t contentLength) { | ||
| 181 | _socket->write("HTTP/1.1 "); | ||
| 182 | |||
| 183 | _socket->write(code); | ||
| 184 | _socket->write("\r\n"); | ||
| 185 | |||
| 186 | for (Headers::const_iterator it = headers.constBegin(), end = headers.constEnd(); | ||
| 187 | it != end; it++) { | ||
| 188 | _socket->write(it.key()); | ||
| 189 | _socket->write(": "); | ||
| 190 | _socket->write(it.value()); | ||
| 191 | _socket->write("\r\n"); | ||
| 192 | } | ||
| 193 | |||
| 194 | if (contentLength > 0) { | ||
| 195 | _socket->write("Content-Length: "); | ||
| 196 | _socket->write(QByteArray::number(contentLength)); | ||
| 197 | _socket->write("\r\n"); | ||
| 198 | |||
| 199 | _socket->write("Content-Type: "); | ||
| 200 | _socket->write(contentType); | ||
| 201 | _socket->write("\r\n"); | ||
| 202 | } | ||
| 203 | _socket->write("Connection: close\r\n\r\n"); | ||
| 204 | } | ||
| 205 | |||
| 204 | void HTTPConnection::readRequest() { | 206 | void HTTPConnection::readRequest() { |
| 205 | if (!_socket->canReadLine()) { | 207 | if (!_socket->canReadLine()) { |
| 206 | return; | 208 | return; |