-
Notifications
You must be signed in to change notification settings - Fork 24
/
response.cpp
164 lines (140 loc) · 5.03 KB
/
response.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright 2016 Cutehacks AS. All rights reserved.
// License can be found in the LICENSE file.
#include <QtCore/QRegExp>
#include <QtCore/QTextCodec>
#include <QtNetwork/QNetworkReply>
#include <QtQml/QQmlEngine>
#include <QJsonObject>
#include "response.h"
#include "serialization.h"
#include "duperagent.h"
namespace com { namespace cutehacks { namespace duperagent {
ResponsePrototype::ResponsePrototype(QQmlEngine *engine, QNetworkReply *reply, int responseType) : QObject(0),
m_engine(engine),
m_reply(reply)
{
QString type = m_reply->header(QNetworkRequest::ContentTypeHeader).toString();
QRegExp charsetRegexp(".*charset=(.*)[\\s]*", Qt::CaseInsensitive, QRegExp::RegExp2);
if (charsetRegexp.exactMatch(type)) {
m_charset = charsetRegexp.capturedTexts().at(1);
}
if (m_reply->isReadable()) {
QByteArray data = m_reply->readAll();
QTextCodec *text = QTextCodec::codecForName(m_charset.toLatin1());
m_text = text ? text->makeDecoder()->toUnicode(data) : QString::fromUtf8(data);
switch (responseType)
{
case ResponseType::Text:
{
m_body = QJSValue(m_text);
break;
}
case ResponseType::Json:
{
if (!type.contains("application/json")) {
JsonCodec json(m_engine);
m_body = json.parse("{}");
break;
}
// TODO: add error handling
JsonCodec json(m_engine);
m_body = json.parse(data);
break;
}
case ResponseType::Blob:
{
m_body = m_engine->toScriptValue<QByteArray>(data);
break;
}
case ResponseType::ArrayBuffer:
{
m_body = m_engine->toScriptValue<QByteArray>(data);
break;
}
default:
{
if (type.contains("application/json")) {
// TODO: add error handling
JsonCodec json(m_engine);
m_body = json.parse(data);
// } else if (type.contains("application/x-www-form-urlencoded")) {
// TODO: Implement parsing of form-urlencoded
// } else if (type.contains("multipart/form-data")) {
// TODO: Implement parsing of form-data
} else if (type.contains("image/")) {
m_body = QString("data:%1;base64,%2")
.arg(type)
.arg(QString::fromLatin1(data.toBase64()));
} else {
m_body = QJSValue(m_text);
}
break;
}
}
}
m_header = m_engine->newObject();
QList<QNetworkReply::RawHeaderPair>::const_iterator it = m_reply->rawHeaderPairs().cbegin();
for(; it != m_reply->rawHeaderPairs().cend(); it++) {
m_header.setProperty(
QString::fromUtf8((*it).first).toLower(),
QString::fromUtf8((*it).second));
}
m_engine->setObjectOwnership(this, QQmlEngine::JavaScriptOwnership);
}
ResponsePrototype::~ResponsePrototype()
{
delete m_reply;
}
int ResponsePrototype::statusType() const {
return statusCode() / 100;
}
int ResponsePrototype::statusCode() const {
QVariant var = m_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
bool ok;
int i = var.toInt(&ok);
return ok ? i : 0;
}
bool ResponsePrototype::typeEquals(int type) const
{
return statusType() == type;
}
bool ResponsePrototype::statusEquals(int code) const
{
QVariant var = m_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
bool ok;
int i = var.toInt(&ok);
return ok && i == code;
}
bool ResponsePrototype::info() const { return typeEquals(1); }
bool ResponsePrototype::ok() const { return typeEquals(2); }
bool ResponsePrototype::clientError() const { return typeEquals(4); }
bool ResponsePrototype::serverError() const { return typeEquals(5); }
bool ResponsePrototype::error() const { return typeEquals(4) || typeEquals(5); }
bool ResponsePrototype::accepted() const { return statusEquals(202); }
bool ResponsePrototype::noContent() const { return statusEquals(204); }
bool ResponsePrototype::badRequest() const { return statusEquals(400); }
bool ResponsePrototype::unauthorized() const { return statusEquals(401); }
bool ResponsePrototype::notAcceptable() const { return statusEquals(406); }
bool ResponsePrototype::notFound() const { return statusEquals(404); }
bool ResponsePrototype::forbidden() const { return statusEquals(403); }
bool ResponsePrototype::fromCache() const
{
return m_reply->attribute(QNetworkRequest::SourceIsFromCacheAttribute).toBool();
}
QString ResponsePrototype::text() const
{
return m_text;
}
QString ResponsePrototype::charset() const
{
return m_charset;
}
QJSValue ResponsePrototype::body() const
{
return m_body;
}
QJSValue ResponsePrototype::header() const
{
return m_header;
}
} } }