Skip to content

Commit 2854d73

Browse files
committed
reduced dependency on QNetworkAccessManager
1 parent d78971d commit 2854d73

File tree

12 files changed

+71
-193
lines changed

12 files changed

+71
-193
lines changed

README.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -453,18 +453,14 @@ It is intercepted inside PEB and is not passed to the underlying operating syste
453453
Please note that the window from where this URL was called will be closed immediately without any check for unsaved user data in HTML forms. Window-closing URL was created for [asynchronous window-closing JavaScript functions](#warning-for-unsaved-user-input-before-closing-a-window).
454454

455455
## Local File Types
456-
All file types not listed here are unsupported. If they are linked from local pages, they will be opened using the default application of the operating system.
457-
458456
PEB is case-insensitive for all local filename extensions with the exception of the start page.
459457
All local files can have multi-dotted names.
460458

461459
Perl scripts are usually recognized by PEB using the ``.pl`` filename extension.
460+
462461
Perl scripts without filename extensions are recognized using a Perl shebang line like:
463462
``#!/usr/bin/perl`` or ``#!/usr/bin/env perl``
464-
465-
No shebang line can change the Perl distribution used by PEB. Shebang arguments are not honored by PEB.
466-
467-
PEB finds Perl interpreter at application startup and uses shebang line only to detect Perl scripts without filename extension.
463+
Shebang interpreter path and arguments are not honored by PEB.
468464

469465
All other supported local file types are recognized using the following filename extensions:
470466
* **CSS files:** ``.css``

resources/app/index.html

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,6 @@
8080
Open /root
8181
</a>
8282
</li>
83-
84-
<li>
85-
<a href="http://local-pseudodomain/nonexistent.html">
86-
Nonexistent File
87-
</a>
88-
</li>
89-
90-
<li>
91-
<a href="http://local-pseudodomain/unsupported.txt">
92-
Unsupported File
93-
</a>
94-
</li>
9583
</ul>
9684
</li>
9785

@@ -102,7 +90,7 @@
10290
</a>
10391
<ul class="dropdown-menu" role="menu">
10492
<li>
105-
<a href="http://local-pseudodomain/interactive-script.html" target="_blank">
93+
<a href="interactive.html" target="_blank">
10694
Interactive Script
10795
</a>
10896
</li>
File renamed without changes.

resources/app/unsupported.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/access-manager.h

Lines changed: 23 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
#define ACCESSMANAGER_H
2020

2121
#include <QApplication>
22-
#include <QDesktopServices>
2322
#include <QDesktopWidget>
2423
#include <QDir>
2524
#include <QMimeDatabase>
@@ -45,27 +44,7 @@ class QAccessManager : public QNetworkAccessManager
4544
const QNetworkRequest &request,
4645
QIODevice *outgoingData = 0)
4746
{
48-
// ==============================
49-
// Start page redirection:
50-
// ==============================
51-
if ((operation == GetOperation) and
52-
request.url().host() ==
53-
qApp->property("pseudoDomain").toString() and
54-
request.url().fileName().length() == 0) {
55-
QNetworkRequest startPageRequest;
56-
startPageRequest
57-
.setUrl(QUrl(qApp->property("startPage").toString()));
58-
59-
return QAccessManager::createRequest
60-
(QAccessManager::GetOperation,
61-
QNetworkRequest(startPageRequest));
62-
}
63-
64-
// ==============================
65-
// GET requests to the browser pseudodomain:
66-
// ==============================
67-
if (operation == GetOperation and
68-
request.url().host() ==
47+
if (request.url().host() ==
6948
qApp->property("pseudoDomain").toString() and
7049
(!request.url().path().contains(".function"))) {
7150

@@ -74,127 +53,50 @@ class QAccessManager : public QNetworkAccessManager
7453
((qApp->property("application").toString())
7554
+ request.url().path());
7655

77-
// Check if file exists:
78-
QFile file(fullFilePath);
79-
if (file.exists()) {
80-
// Get the MIME type of the local file:
81-
QMimeDatabase mimeDatabase;
82-
QMimeType type = mimeDatabase.mimeTypeForFile(fullFilePath);
83-
// qDebug() << "MIME type:" << type.name();
84-
QString mimeType = type.name();
85-
86-
// Handle local Perl scripts:
87-
if (mimeType == "application/x-perl") {
88-
QByteArray emptyPostDataArray;
89-
emit handleScriptSignal(
90-
request.url(), emptyPostDataArray);
91-
92-
QLocalReply *reply = new QLocalReply(request.url(),
93-
emptyString,
94-
emptyString);
95-
return reply;
96-
}
56+
// Get the MIME type of the local file:
57+
QMimeDatabase mimeDatabase;
58+
QMimeType type = mimeDatabase.mimeTypeForFile(fullFilePath);
59+
QString mimeType = type.name();
9760

98-
// Handle other supported local files:
99-
if (mimeType == "text/html" or
100-
mimeType == "text/xml" or
101-
mimeType == "text/css" or
102-
mimeType == "application/javascript" or
103-
mimeType == "application/json" or
104-
mimeType == "image/gif" or
105-
mimeType == "image/jpeg" or
106-
mimeType == "image/png" or
107-
mimeType == "image/svg+xml" or
108-
mimeType == "application/vnd.ms-fontobject" or
109-
mimeType == "application/x-font-ttf" or
110-
mimeType == "application/font-sfnt" or
111-
mimeType.contains("application/font-woff")) {
112-
113-
qDebug() << "Local link requested:"
114-
<< request.url().toString();
115-
116-
QFileReader *resourceReader =
117-
new QFileReader(QString(fullFilePath));
118-
QString fileContents = resourceReader->fileContents;
119-
120-
QLocalReply *reply = new QLocalReply(request.url(),
121-
fileContents,
122-
mimeType);
123-
return reply;
124-
} else {
125-
qDebug() << "File type not supported:" << fullFilePath;
61+
// Handle local Perl scripts:
62+
if (mimeType == "application/x-perl") {
63+
QByteArray postDataArray;
12664

127-
QDesktopServices::openUrl(
128-
QUrl::fromLocalFile(fullFilePath));
65+
// GET requests to the browser pseudodomain:
66+
if (operation == GetOperation) {
67+
emit handleScriptSignal(request.url(), postDataArray);
12968

130-
QLocalReply *reply = new QLocalReply(request.url(),
131-
emptyString,
132-
emptyString);
69+
QLocalReply *reply = new QLocalReply(request.url());
13370
return reply;
13471
}
135-
} else {
136-
qDebug() << "File not found:" << fullFilePath;
137-
138-
QFileReader *resourceReader =
139-
new QFileReader(QString(":/html/error.html"));
140-
QString htmlErrorContents = resourceReader->fileContents;
141-
142-
QString errorMessage =
143-
"<div><br>File not found:<br>" +
144-
fullFilePath + "<br><br>" +
145-
"<a href='http://local-pseudodomain/'>start page</a>" +
146-
"</div>";
147-
htmlErrorContents.replace("ERROR_MESSAGE", errorMessage);
148-
149-
QString mimeType = "text/html";
150-
151-
QLocalReply *reply = new QLocalReply(request.url(),
152-
htmlErrorContents,
153-
mimeType);
154-
return reply;
155-
}
156-
}
15772

158-
// ==============================
159-
// POST requests to the browser pseudodomain:
160-
// ==============================
161-
if (operation == PostOperation and
162-
request.url().host() ==
163-
qApp->property("pseudoDomain").toString()) {
73+
// POST requests to the browser pseudodomain:
74+
if (operation == PostOperation) {
75+
if (outgoingData) {
76+
QByteArray postDataArray = outgoingData->readAll();
77+
emit handleScriptSignal(request.url(),
78+
postDataArray);
79+
}
16480

165-
if (outgoingData) {
166-
QByteArray postDataArray = outgoingData->readAll();
167-
emit handleScriptSignal(request.url(), postDataArray);
81+
QLocalReply *reply = new QLocalReply(request.url());
82+
return reply;
83+
}
16884
}
169-
170-
QLocalReply *reply = new QLocalReply(request.url(),
171-
emptyString,
172-
emptyString);
173-
return reply;
17485
}
17586

176-
// ==============================
17787
// Window closing URL:
178-
// ==============================
17988
if (operation == GetOperation and
18089
request.url().fileName() == "close-window.function") {
18190
emit closeWindowSignal();
18291

183-
QLocalReply *reply = new QLocalReply(request.url(),
184-
emptyString,
185-
emptyString);
92+
QLocalReply *reply = new QLocalReply(request.url());
18693
return reply;
18794
}
18895

189-
qDebug() << "Link requested:" << request.url().toString();
190-
19196
return QNetworkAccessManager::createRequest
19297
(QNetworkAccessManager::GetOperation,
19398
QNetworkRequest(request));
19499
}
195-
196-
private:
197-
QString emptyString;
198100
};
199101

200102
#endif // ACCESSMANAGER_H

src/local-reply.cpp

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ struct QLocalReplyPrivate
2828
int offset;
2929
};
3030

31-
QLocalReply::QLocalReply(const QUrl &url,
32-
const QString &data,
33-
const QString &mime)
31+
QLocalReply::QLocalReply(const QUrl &url)
3432
: QNetworkReply()
3533
{
3634
setFinished(true);
@@ -41,23 +39,9 @@ QLocalReply::QLocalReply(const QUrl &url,
4139

4240
setUrl(url);
4341

44-
if (data.length() > 0) {
45-
setHeader(QNetworkRequest::ContentLengthHeader,
46-
QVariant(reply->data.size()));
47-
setHeader(QNetworkRequest::LastModifiedHeader,
48-
QVariant(QDateTime::currentDateTimeUtc()));
49-
setHeader(QNetworkRequest::ContentTypeHeader, mime);
50-
}
51-
5242
QTimer::singleShot(0, this, SIGNAL(metaDataChanged()));
5343

54-
if (data.length() > 0) {
55-
setAttribute(QNetworkRequest::HttpStatusCodeAttribute, 200);
56-
setAttribute(QNetworkRequest::HttpReasonPhraseAttribute, "OK");
57-
reply->data = data.toUtf8();
58-
} else {
59-
setAttribute(QNetworkRequest::HttpStatusCodeAttribute, 204);
60-
}
44+
setAttribute(QNetworkRequest::HttpStatusCodeAttribute, 204);
6145

6246
QTimer::singleShot(0, this, SIGNAL(readyRead()));
6347
QTimer::singleShot(0, this, SIGNAL(finished()));

src/local-reply.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class QLocalReply : public QNetworkReply
2929

3030
public:
3131
QLocalReply(
32-
const QUrl &url, const QString &data, const QString &mime);
32+
const QUrl &url);
3333
~QLocalReply();
3434

3535
void abort();

src/page.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ QPage::QPage()
3535

3636
QWebSettings::globalSettings()->
3737
setAttribute(QWebSettings::JavaEnabled, false);
38-
QWebSettings::globalSettings()->
39-
setAttribute(QWebSettings::LocalContentCanAccessRemoteUrls, false);
4038
QWebSettings::globalSettings()->
4139
setAttribute(QWebSettings::PluginsEnabled, false);
4240

@@ -48,12 +46,14 @@ QPage::QPage()
4846
setAttribute(QWebSettings::JavascriptEnabled, true);
4947
QWebSettings::globalSettings()->
5048
setAttribute(QWebSettings::JavascriptCanOpenWindows, true);
49+
QWebSettings::globalSettings()->
50+
setAttribute(QWebSettings::LinksIncludedInFocusChain, true);
51+
QWebSettings::globalSettings()->
52+
setAttribute(QWebSettings::LocalContentCanAccessRemoteUrls, true);
5153
QWebSettings::globalSettings()->
5254
setAttribute(QWebSettings::PrivateBrowsingEnabled, true);
5355
QWebSettings::globalSettings()->
5456
setAttribute(QWebSettings::SpatialNavigationEnabled, true);
55-
QWebSettings::globalSettings()->
56-
setAttribute(QWebSettings::LinksIncludedInFocusChain, true);
5757
QWebSettings::globalSettings()->
5858
setAttribute(QWebSettings::XSSAuditingEnabled, true);
5959

src/page.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,22 @@
1818
#ifndef PAGE_H
1919
#define PAGE_H
2020

21-
#include <QInputDialog>
21+
2222
#include <QJsonDocument>
2323
#include <QJsonObject>
2424
#include <QMessageBox>
25-
#include <QNetworkReply>
2625
#include <QRegularExpression>
2726
#include <QTimer>
2827
#include <QUrl>
2928
#include <QUrlQuery>
3029
#include <QWebPage>
3130
#include <QWebFrame>
32-
3331
#include <QWebSecurityOrigin>
3432

33+
#include <QInputDialog>
34+
35+
#include <QNetworkReply>
36+
3537
#include "file-reader.h"
3638
#include "script-handler.h"
3739

@@ -278,8 +280,6 @@ public slots:
278280
"\"); null";
279281

280282
mainFrame()->evaluateJavaScript(inodeSelectedJavaScript);
281-
282-
qDebug() << "User selected inode:" << userSelectedInodesFormatted;
283283
}
284284
}
285285

@@ -516,6 +516,10 @@ public slots:
516516
QApplication::applicationVersion()
517517
.toLatin1());
518518

519+
aboutPageContents
520+
.replace("START_PAGE",
521+
qApp->property("startPage").toString());
522+
519523
frame->setHtml(aboutPageContents);
520524

521525
return false;
@@ -702,7 +706,6 @@ public slots:
702706
}
703707

704708
private:
705-
QString emptyString;
706709
bool windowCloseRequested;
707710

708711
QString alertTitle;

src/resources/html/about.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
<a href="#" onclick="window.open('https://github.com/ddmitov/perl-executing-browser'); return false;">
4848
https://github.com/ddmitov/perl-executing-browser</a><br>
4949
<br>
50-
<a href="http://local-pseudodomain/">start page</a>
50+
<a href="START_PAGE">start page</a>
5151
</body>
5252

5353
</html>

0 commit comments

Comments
 (0)