void HTTPConnection::respond(const char* code, const QByteArray& content, const char* contentType, const Headers& headers) { _socket->write("HTTP/1.1 "); _socket->write(code); _socket->write("\r\n"); int csize = content.size(); for (Headers::const_iterator it = headers.constBegin(), end = headers.constEnd(); it != end; it++) { _socket->write(it.key()); _socket->write(": "); _socket->write(it.value()); _socket->write("\r\n"); } if (csize > 0) { _socket->write("Content-Length: "); _socket->write(QByteArray::number(csize)); _socket->write("\r\n"); _socket->write("Content-Type: "); _socket->write(contentType); _socket->write("\r\n"); } _socket->write("Connection: close\r\n\r\n"); if (csize > 0) { _socket->write(content); } // make sure we receive no further read notifications _socket->disconnect(SIGNAL(readyRead()), this); _socket->disconnectFromHost(); }
void HTTPConnection::respondWithStatusAndHeaders(const char* code, const char* contentType, const Headers& headers, qint64 contentLength) { _socket->write("HTTP/1.1 "); _socket->write(code); _socket->write("\r\n"); for (Headers::const_iterator it = headers.constBegin(), end = headers.constEnd(); it != end; it++) { _socket->write(it.key()); _socket->write(": "); _socket->write(it.value()); _socket->write("\r\n"); } if (contentLength > 0) { _socket->write("Content-Length: "); _socket->write(QByteArray::number(contentLength)); _socket->write("\r\n"); _socket->write("Content-Type: "); _socket->write(contentType); _socket->write("\r\n"); } _socket->write("Connection: close\r\n\r\n"); }
bool uWSGI::finalizeHeaders(Context *ctx) { struct wsgi_request *wsgi_req = static_cast<wsgi_request*>(ctx->request()->engineData()); Response *res = ctx->res(); QByteArray status = statusCode(res->status()); if (uwsgi_response_prepare_headers(wsgi_req, status.data(), status.size())) { return false; } if (!Engine::finalizeHeaders(ctx)) { return false; } const Headers headers = res->headers(); auto it = headers.constBegin(); auto endIt = headers.constEnd(); while (it != endIt) { QByteArray key = camelCaseHeader(it.key()).toLatin1(); QByteArray value = it.value().toLatin1(); if (uwsgi_response_add_header(wsgi_req, key.data(), key.size(), value.data(), value.size())) { return false; } ++it; } return true; }