This repository has been archived by the owner on Feb 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BCLDiag.cpp
230 lines (202 loc) · 8.21 KB
/
BCLDiag.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
#include "BCLDiag.hpp"
#include <QDialogButtonBox>
#include <QHBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QMessageBox>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QPushButton>
BCLDiag::BCLDiag(QWidget *parent)
: QDialog(parent),
m_checkingIp(false)
{
m_keyLineEdit = new QLineEdit();
m_statusLabel = new QLabel("Please enter your BCL Auth Key:");
m_connectButton = new QPushButton("Connect");
m_quitButton = new QPushButton("Quit");
m_buttonBox = new QDialogButtonBox;
m_buttonBox->addButton(m_connectButton, QDialogButtonBox::ActionRole);
m_buttonBox->addButton(m_quitButton, QDialogButtonBox::RejectRole);
connect(m_keyLineEdit, SIGNAL(textChanged(QString)), this, SLOT(enableConnectButton()));
connect(m_connectButton, SIGNAL(clicked()), this, SLOT(connectClicked()));
connect(m_quitButton, SIGNAL(clicked()), this, SLOT(close()));
QHBoxLayout *topLayout = new QHBoxLayout;
topLayout->addWidget(m_keyLineEdit);
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(m_statusLabel);
mainLayout->addLayout(topLayout);
mainLayout->addWidget(m_buttonBox);
setLayout(mainLayout);
setWindowTitle("BCL Auth Test (v1.1)");
setWindowFlags(Qt::Dialog | Qt::WindowTitleHint);
setMinimumWidth(300);
m_keyLineEdit->setFocus();
enableConnectButton();
}
void BCLDiag::startRequest(const QUrl url)
{
QNetworkRequest request = QNetworkRequest(url);
request.setRawHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
request.setRawHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.56 Safari/537.17");
m_reply = m_qnam.get(request);
connect(m_reply, SIGNAL(finished()), this, SLOT(httpFinished()));
}
void BCLDiag::connectClicked()
{
m_key = m_keyLineEdit->text();
bool validAuthKey = QRegExp("[A-Za-z0-9]{32}").exactMatch(m_key);
if (!validAuthKey) {
QString errorMsg;
if (QRegExp("[A-Za-z0-9]{32}").exactMatch(m_key.trimmed())) {
m_key = m_key.trimmed();
validAuthKey = true;
m_keyLineEdit->setText(m_key);
errorMsg = QString("Your key has extra whitespace at the beggining or end. The key can only contain 32 non-whitespace alphanumeric characters.\n\nContinuing with fixed auth key: \"%1\"").arg(m_key);
} else if (m_key.length() != 32) {
errorMsg = QString("Your key is %1 character%2. The key can only contain 32 non-whitespace alphanumeric characters.").arg(QString::number(m_key.length()), m_key.length() == 1 ? "" : "s");
} else {
errorMsg = "The key can only contain 32 non-whitespace alphanumeric characters.";
}
m_statusLabel->setText("Malformed Auth Key");
QMessageBox::warning(this, "Malformed Auth Key", errorMsg);
if (!validAuthKey) return;
}
if (!m_checkingIp) {
m_statusLabel->setText("Checking IP...");
m_checkingIp = true;
m_url = "http://axelstudios.com/ip.php";
} else {
m_statusLabel->setText("Connecting...");
m_checkingIp = false;
m_url = QString("http://bcl.nrel.gov/api/search/?api_version=1.1&show_rows=0&oauth_consumer_key=%1").arg(m_key);
m_originalUrl = m_url;
}
m_connectButton->setEnabled(false);
m_httpRequestAborted = false;
startRequest(m_url);
}
void BCLDiag::cancelRequest()
{
m_statusLabel->setText("Canceled.");
m_httpRequestAborted = true;
m_reply->abort();
m_connectButton->setEnabled(true);
}
void BCLDiag::httpFinished()
{
QString response = m_reply->readAll();
if (m_httpRequestAborted) {
m_reply->deleteLater();
m_checkingIp = false;
return;
}
if (m_checkingIp) {
QString octet("(?:[0-1]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])");
bool validIp = QRegExp(QString("^%1\\.%1\\.%1\\.%1$").arg(octet)).exactMatch(response);
m_ip = validIp ? response : "";
m_reply->deleteLater();
m_reply = 0;
connectClicked();
return;
}
QVariant statusCode = m_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
QString errorString = m_reply->errorString();
QString url = m_reply->url().toString();
QString detailedText = QString("BCL Key: \"%1\"\n").arg(m_key);
if (!m_ip.isEmpty()) {
detailedText += QString("IP Address: %1\n").arg(m_ip);
}
if (url != m_originalUrl.toString()) {
detailedText += QString("URL Accessed: %1\n").arg(url);
}
if (m_reply->error() != QNetworkReply::NoError) {
detailedText += QString("QNetworkReply Error %1: %2\n").arg(QString::number(m_reply->error()), errorString);
}
detailedText += QString("HTTP Status: %1\n\n").arg(!statusCode.isNull() ? statusCode.toString() + " " + m_reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString() : "N/A");
if (m_reply->rawHeaderList().size()) {
detailedText += "Response Headers:\n";
Q_FOREACH(QNetworkReply::RawHeaderPair header, m_reply->rawHeaderPairs()) {
detailedText += QString("%1: %2\n").arg(QString(header.first), QString(header.second));
}
} else {
detailedText += "Response Headers: N/A\n";
}
if (response.length()) {
detailedText += QString("\nResponse Body:\n%1").arg(response);
} else {
detailedText += "\nResponse Body: N/A";
}
QVariant redirectionTarget = m_reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
if (m_reply->error() != QNetworkReply::NoError) {
switch(m_reply->error()) {
case QNetworkReply::AuthenticationRequiredError:
m_statusLabel->setText("Invalid Auth Key");
break;
case QNetworkReply::ConnectionRefusedError:
m_statusLabel->setText("Network Error: Connection Refused");
break;
case QNetworkReply::UnknownContentError:
m_statusLabel->setText("Network Error: Unknown Content. Verify the User-Agent header");
break;
case QNetworkReply::HostNotFoundError:
if (m_ip.isEmpty()) {
m_statusLabel->setText("Network Error: Offline. Check your internet connection");
} else {
m_statusLabel->setText("Network Error: Host Not Found");
}
break;
case QNetworkReply::UnknownNetworkError:
if (errorString.startsWith("Error creating SSL context")) {
m_statusLabel->setText("DLL Error: SSL unavailable");
} else {
m_statusLabel->setText("Network Error: Unknown Network Error");
}
break;
default:
m_statusLabel->setText(QString("Network Error: %1%2").arg(!statusCode.isNull() ? statusCode.toString() + " - " : "", errorString));
}
QMessageBox msgBox(QMessageBox::Warning, "Connection Failed", QString("Connection failed: %1.").arg(m_statusLabel->text()), QMessageBox::NoButton, this);
msgBox.setDetailedText(detailedText);
QSpacerItem* horizontalSpacer = new QSpacerItem(478, 0, QSizePolicy::Minimum, QSizePolicy::Expanding);
QGridLayout* layout = (QGridLayout*)msgBox.layout();
layout->addItem(horizontalSpacer, layout->rowCount(), 0, 1, layout->columnCount());
msgBox.exec();
enableConnectButton();
} else if (!redirectionTarget.isNull()) {
QUrl newUrl = m_url.resolved(redirectionTarget.toUrl());
if (QMessageBox::question(this,
"Connection Redirect",
QString("Redirect to %1?").arg(newUrl.toString()),
QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) {
m_url = newUrl;
m_reply->deleteLater();
startRequest(m_url);
return;
} else {
cancelRequest();
return;
}
} else {
QString acceptableResponse("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<results><keywords> </keywords><show_rows>0</show_rows><filters></filters><result/></results>\n");
if (response == acceptableResponse) {
m_statusLabel->setText("Success!");
} else {
m_statusLabel->setText("Unrecognized Response.");
QMessageBox msgBox(QMessageBox::Warning,
"Unrecognized Response",
"Server returned OK, but the response body is unrecognized",
QMessageBox::NoButton,
this);
msgBox.setDetailedText(detailedText);
msgBox.exec();
}
enableConnectButton();
}
m_reply->deleteLater();
m_reply = 0;
}
void BCLDiag::enableConnectButton()
{
m_connectButton->setEnabled(!m_keyLineEdit->text().isEmpty());
}