ResponseCode MuHttpServer::HandleRequest(HttpRequest *pHttpRequest, HttpResponse *pHttpResponse) { debug(pHttpRequest); if (!mAllowNonLocal && pHttpRequest->RemoteAddress() != "127.0.0.1") { QString errorString = QString("<html><body><h1>Forbidden</h1>" "Connection from %1 has been blocked. Only localhost connections are allowed.</body></html>") .arg(QString::fromStdString(pHttpRequest->RemoteAddress())); pHttpResponse->SetBody(errorString.toAscii(), errorString.size()); warning(errorString); return HTTPRESPONSECODE_403_FORBIDDEN; } QString uri = QString::fromStdString(pHttpRequest->Uri()).split("?")[0]; if (pHttpRequest->Method() == REQUESTMETHOD_GET || pHttpRequest->Method() == REQUESTMETHOD_POST) { QString contentType = pHttpRequest->Headers("content-type").c_str(); QString body = pHttpRequest->Body().c_str(); Response rsp = (pHttpRequest->Method() == REQUESTMETHOD_GET) ? getRequest(uri, contentType, body, pHttpRequest->FormValues()) : postRequest(uri, contentType, body, pHttpRequest->FormValues()); if (rsp.mCode != HTTPRESPONSECODE_INVALID && rsp.mEncoding.isValid()) { switch (rsp.mEncoding) { case Response::ASCII: pHttpResponse->SetBody(rsp.mBody.toAscii(), rsp.mBody.toAscii().length()); break; case Response::UTF8: pHttpResponse->SetBody(rsp.mBody.toUtf8(), rsp.mBody.toUtf8().length()); break; case Response::OCTET: pHttpResponse->SetBody(rsp.mOctets.constData(), rsp.mOctets.length()); break; } QMapIterator<QString,QString> headerIt(rsp.mHeaders); while (headerIt.hasNext()) { headerIt.next(); pHttpResponse->SetHeader(headerIt.key().toStdString(), headerIt.value().toStdString()); } return rsp.mCode; } } // default to responding with an internal server error std::string errorString = "<html><body><h1>Internal server error</h1>An unknown error occured.</body></html>"; pHttpResponse->SetBody(errorString.c_str(), errorString.size()); warning(QString::fromStdString(errorString)); return HTTPRESPONSECODE_500_INTERNALSERVERERROR; }
QScriptValue Web::download(const QString &urlString, const QScriptValue &options) { mData.clear(); if(mFileValue.isValid()) { if(Code::File *file = qobject_cast<Code::File*>(mFileValue.toQObject())) mFile = file->file(); else mFile = new QFile(mFileValue.toString(), this); mCloseFile = false; if(!mFile->isOpen()) { if(!mFile->open(QIODevice::WriteOnly)) { throwError("OpenFileError", tr("Unable to open the destination file")); return thisObject(); } mCloseFile = true; } } QUrl url(urlString); if(url.scheme() == QString()) url = QUrl("http://" + urlString, QUrl::TolerantMode); #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)) QUrlQuery urlQuery; #endif QNetworkRequest request; QScriptValueIterator it(options); Method method = Get; QByteArray postData; while(it.hasNext()) { it.next(); if(it.name() == "rawHeaders") { QScriptValueIterator headerIt(it.value()); while(headerIt.hasNext()) { headerIt.next(); request.setRawHeader(headerIt.name().toUtf8(), headerIt.value().toString().toUtf8()); } } else if(it.name() == "method") { method = static_cast<Method>(it.value().toInt32()); } else if(it.name() == "postData") { #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)) QScriptValueIterator postDataIt(it.value()); QUrlQuery postDataParameters; while(postDataIt.hasNext()) { postDataIt.next(); postDataParameters.addQueryItem(postDataIt.name(), postDataIt.value().toString()); } postData = postDataParameters.toString(QUrl::FullyEncoded).toLatin1(); #else QScriptValueIterator postDataIt(it.value()); QUrl postDataParameters; while(postDataIt.hasNext()) { postDataIt.next(); postDataParameters.addQueryItem(postDataIt.name(), postDataIt.value().toString()); } postData = postDataParameters.encodedQuery(); #endif } else if(it.name() == "query") { QScriptValueIterator queryIt(it.value()); while(queryIt.hasNext()) { queryIt.next(); #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)) urlQuery.addQueryItem(queryIt.name(), queryIt.value().toString()); #else url.addQueryItem(queryIt.name(), queryIt.value().toString()); #endif } } else if(it.name() == "user") { mUser = it.value().toString(); } else if(it.name() == "password") { mPassword = it.value().toString(); } } #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)) url.setQuery(urlQuery); #endif request.setUrl(url); switch(method) { case Post: mNetworkReply = mNetworkAccessManager->post(request, postData); break; case Get: default: mNetworkReply = mNetworkAccessManager->get(request); break; } QObject::connect(mNetworkReply, SIGNAL(finished()), this, SLOT(finished())); QObject::connect(mNetworkReply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(downloadProgress(qint64,qint64))); QObject::connect(mNetworkReply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(error())); QObject::connect(mNetworkReply, SIGNAL(readyRead()), this, SLOT(readyRead())); mIsDownloading = true; return thisObject(); }