-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathglCapsViewerHttp.cpp
302 lines (262 loc) · 8.33 KB
/
glCapsViewerHttp.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
*
* OpenGL hardware capability viewer and database
*
* Server http communication class implementation
*
* Copyright (C) 2011-2015 by Sascha Willems (www.saschawillems.de)
*
* This code is free software, you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 3 as published by the Free Software Foundation.
*
* Please review the following information to ensure the GNU Lesser
* General Public License version 3 requirements will be met:
* http://opensource.org/licenses/lgpl-3.0.html
*
* The code is distributed WITHOUT ANY WARRANTY; without even the
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU LGPL 3.0 for more details.
*
*/
#include "glCapsViewerHttp.h"
#include <QNetworkProxy>
#include <sstream>
#include <QMessageBox>
#include <QEventLoop>
#include <QHttpMultiPart>
#include <QXmlStreamReader>
glCapsViewerHttp::glCapsViewerHttp()
{
}
glCapsViewerHttp::~glCapsViewerHttp()
{
}
/// <summary>
/// Checks if the OpenGL hardware database can be reached
/// </summary>
/// <returns>true if request succeeded, false if not (e.g. wrong proxy, or website down</returns>
bool glCapsViewerHttp::checkServerConnection()
{
manager = new QNetworkAccessManager(NULL);
QUrl qurl(QString::fromStdString(getBaseUrl() + "services/gl_serverstate.php"));
QNetworkReply* reply = manager->get(QNetworkRequest(qurl));
QEventLoop loop;
connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec(QEventLoop::ExcludeUserInputEvents);
return (reply->error() == QNetworkReply::NoError);
}
/// <summary>
/// Execute http get request
/// </summary>
/// <param name="url">url for the http get request</param>
/// <returns>string of the http get request or empty string in case of failure</returns>
string glCapsViewerHttp::httpGet(string url)
{
manager = new QNetworkAccessManager(NULL);
QUrl qurl(QString::fromStdString(url));
QNetworkReply* reply = manager->get(QNetworkRequest(qurl));
QEventLoop loop;
connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();
if (reply->error() == QNetworkReply::NoError)
{
QByteArray bytes = reply->readAll();
QString replyStr(bytes);
delete(manager);
return replyStr.toStdString();
}
else
{
QString err;
err = reply->errorString();
delete(manager);
return "";
}
}
/// <summary>
/// Execute http post
/// </summary>
/// <param name="url">url for the http post</param>
/// <param name="data">string data to post</param>
/// <returns>Server answer</returns>
string glCapsViewerHttp::httpPost(string url, string data)
{
manager = new QNetworkAccessManager(NULL);
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
QHttpPart xmlPart;
xmlPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"data\"; filename=\"glcapsviewerreport.xml\""));
xmlPart.setBody(QString::fromStdString(data).toLatin1());
multiPart->append(xmlPart);
QUrl qurl(QString::fromStdString(url));
QNetworkRequest request(qurl);
QNetworkReply *reply = manager->post(request, multiPart);
multiPart->setParent(reply);
QEventLoop loop;
connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();
if (reply->error() == QNetworkReply::NoError)
{
QByteArray bytes = reply->readAll();
QString replyStr(bytes);
delete(manager);
return replyStr.toStdString();
}
else
{
QString err;
err = reply->errorString();
delete(manager);
return "";
}
}
/// <summary>
/// Encodes an url (or string) to make it comply to RFC2396 by replacing
/// illegal characters
/// Some device descriptions may include a "+" that needs to be replaced (e.g. GeForce 9800 GTX+/PCI/SSE2)
/// </summary>
/// <param name="url">Url (or string) to be encoded</param>
/// <returns></returns>
string glCapsViewerHttp::encodeUrl(string url)
{
QString urlStr(QString::fromStdString(url));
urlStr.replace("+", "%2B");
return urlStr.toStdString();
}
/// <summary>
/// Gets the Id of a report from the online database
/// </summary>
/// <param name="description">Description of the report to get the Id for</param>
/// <returns></returns>
int glCapsViewerHttp::getReportId(string description)
{
string httpReply;
stringstream urlss;
urlss << getBaseUrl() << "gl_checkreport.php?description=" << description;
string url = encodeUrl(urlss.str());
httpReply = httpGet(url);
return (!httpReply.empty()) ? atoi(httpReply.c_str()) : -1;
}
/// <summary>
/// Checks if the report is present in the online database
/// </summary>
/// <param name="description">Description of the report to get the Id for</param>
/// <returns></returns>
bool glCapsViewerHttp::checkReportPresent(string description)
{
int reportID = getReportId(description);
return (reportID > -1) ? true : false;
}
/// <summary>
/// Fechtes the OpenGL capability xml list from the web server
/// </summary>
/// <returns>xml string</returns>
string glCapsViewerHttp::fetchCapsList()
{
string capsXml;
stringstream urlss;
capsXml = httpGet(getBaseUrl() + "/files/capslist.xml");
return capsXml;
}
/// <summary>
/// Fechtes an xml with all report data from the online database
/// </summary>
/// <param name="reportId">id of the report to get the report xml for</param>
/// <returns>xml string</returns>
string glCapsViewerHttp::fetchReport(int reportId)
{
string reportXml;
stringstream urlss;
urlss << getBaseUrl() << "services/gl_getreport.php?reportId=" << reportId;
reportXml = httpGet(urlss.str());
return reportXml;
}
/// <summary>
/// Posts the given xml for a report to the database
/// </summary>
/// <returns>todo</returns>
string glCapsViewerHttp::postReport(string xml)
{
string httpReply;
stringstream urlss;
urlss << getBaseUrl() << "services/gl_convertreport.php";
httpReply = httpPost(urlss.str(), xml);
return httpReply;
}
/// <summary>
/// Posts the given url to the db report update script
/// </summary>
/// <returns>Coma separated list of updated caps</returns>
string glCapsViewerHttp::postReportForUpdate(string xml)
{
string httpReply;
stringstream urlss;
urlss << getBaseUrl() << "services/gl_updatereport.php";
httpReply = httpPost(urlss.str(), xml);
return httpReply;
}
/// <summary>
/// Fetches all available devices from the online database and lists them in the ui
/// </summary>
/// <returns>List of reports as vector</returns>
vector<string> glCapsViewerHttp::fetchDevices()
{
vector<string> deviceList;
string httpReply;
stringstream urlss;
urlss << getBaseUrl() << "services/gl_getdevices.php";
httpReply = httpGet(urlss.str());
if (!httpReply.empty()) {
QXmlStreamReader xmlReader(&httpReply[0]);
while (!xmlReader.atEnd()) {
if ((xmlReader.name() == "device") && (xmlReader.isStartElement())) {
deviceList.push_back(xmlReader.readElementText().toStdString());
}
xmlReader.readNext();
}
}
return deviceList;
}
/// <summary>
/// Fetches all available reports for the given device
/// </summary>
/// <param name="device">Name of the device to select reports for (GL_RENDERER) </param>
/// <returns></returns>
vector<reportInfo> glCapsViewerHttp::fetchDeviceReports(string device)
{
vector<reportInfo> reportList;
string httpReply;
stringstream urlss;
urlss << getBaseUrl() << "services/gl_getdevicereports.php?glrenderer=" << device;
string url;
url = encodeUrl(urlss.str());
httpReply = httpGet(url);
if (!httpReply.empty())
{
QXmlStreamReader xmlReader(&httpReply[0]);
while (!xmlReader.atEnd()) {
if ((xmlReader.name() == "report") && (xmlReader.isStartElement())) {
reportInfo report;
report.device = device;
QXmlStreamAttributes xmlAttribs = xmlReader.attributes();
report.operatingSystem = xmlAttribs.value("os").toString().toStdString();
report.reportId = xmlAttribs.value("id").toInt();
report.version = xmlReader.readElementText().toStdString();
reportList.push_back(report);
}
xmlReader.readNext();
}
};
return reportList;
}
/// <summary>
/// Returns the base url of the OpenGL hardware database
/// </summary>
string glCapsViewerHttp::getBaseUrl()
{
#ifdef DEVDATABASE
return "http://www.delphigl.de/opengldatabase_dev/";
#else
return "http://opengl.gpuinfo.org/";
#endif
}