Created
June 7, 2015 05:47
-
-
Save caetanus/e61086358ffcff7913e8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "XMLHttpRequest.h" | |
XMLHttpRequest::XMLHttpRequest(QObject *parent) | |
: QObject(parent), | |
p_status(0), | |
p_readyState(0) | |
{ | |
} | |
void XMLHttpRequest::open(QString method, QString url) | |
{ | |
p_method = method; | |
p_request = Request(new QNetworkRequest(QUrl(url))); | |
// clean state when a new ajax is made. | |
p_status = 0; | |
p_readyState = 0; | |
p_responseText = ""; | |
p_statusText = ""; | |
} | |
void XMLHttpRequest::send(QString data) | |
{ | |
auto bytedata = data.toUtf8(); | |
if (p_method == "POST") | |
{ | |
p_reply = Reply(p_networkmanager.post(*p_request, bytedata)); | |
} | |
else if (p_method == "GET") | |
{ | |
p_reply = Reply(p_networkmanager.get(*p_request)); | |
} | |
else if (p_method == "PUT") | |
{ | |
p_reply = Reply(p_networkmanager.put(*p_request, bytedata)); | |
} | |
else if (p_method == "DELETE") | |
{ | |
p_reply = Reply(p_networkmanager.deleteResource(*p_request)); | |
} | |
connect(&(*p_reply), &QNetworkReply::finished, | |
[this]() | |
{ | |
p_responseText = QString::fromUtf8(p_reply->readAll()); | |
p_status = p_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); | |
p_statusText = QString::fromUtf8(p_reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toByteArray()); | |
p_readyState = 4; | |
if (p_reply->error()) | |
{ | |
p_status = p_reply->error(); | |
p_statusText = p_reply->errorString(); | |
} | |
emit onreadystatechange(); | |
}); | |
} | |
void XMLHttpRequest::send() | |
{ | |
send(""); | |
} | |
QString XMLHttpRequest::getAllResponseHeaders() | |
{ | |
if (!p_readyState) | |
{ | |
return ""; | |
} | |
QStringList output; | |
foreach (auto header, p_reply->rawHeaderPairs()) | |
{ | |
QString stringHeader; | |
stringHeader = QString("%1: %2").arg(QString::fromUtf8(header.first), | |
QString::fromUtf8(header.second)); | |
output << stringHeader; | |
} | |
return output.join("\n"); | |
} | |
QString XMLHttpRequest::getResponseHeader(QString header) | |
{ | |
if (!p_readyState) | |
{ | |
return ""; | |
} | |
return QString::fromUtf8(p_reply->rawHeader(header.toUtf8())); | |
} | |
int XMLHttpRequest::getStatus() | |
{ | |
return p_status; | |
} | |
QString XMLHttpRequest::getStatusText() | |
{ | |
return p_statusText; | |
} | |
int XMLHttpRequest::getReadyState() | |
{ | |
return p_readyState; | |
} | |
QString XMLHttpRequest::getResponseText() | |
{ | |
return p_responseText; | |
} | |
void XMLHttpRequest::setup(QSharedPointer<QScriptEngine> engine) | |
{ | |
engine->globalObject().setProperty("XMLHTTPRequest", | |
engine->newFunction(XMLHttpRequest_ctor)); | |
} | |
QScriptValue XMLHttpRequest_ctor(QScriptContext *context, QScriptEngine *engine) | |
{ | |
return engine->toScriptValue(XMLHttpRequest()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment