forked from goldendict/goldendict
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy patharticle_urlschemehandler.cc
More file actions
194 lines (165 loc) · 7.05 KB
/
article_urlschemehandler.cc
File metadata and controls
194 lines (165 loc) · 7.05 KB
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
/* This file is (c) 2022 Igor Kushnir <igorkuo@gmail.com>
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
#ifndef USE_QTWEBKIT
#include "article_urlschemehandler.hh"
#include "article_netmgr.hh"
#include "gddebug.hh"
#include <QByteArray>
#include <QMetaEnum>
#include <QMimeDatabase>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QPointer>
#include <QWebEngineProfile>
#include <QWebEngineUrlRequestJob>
#include <QWebEngineUrlScheme>
#include <array>
#include <utility>
namespace {
/// @return the names of GoldenDict's custom URL schemes handled by ArticleUrlSchemeHandler.
auto const & handledUrlSchemeNames()
{
// Note that the remaining custom URL scheme names - "bword", "gdprg" and "gdtts" - do not
// belong in this list, because they are handled only in ArticleView::linkHovered() and
// ArticleView::openLink(). ArticleNetworkAccessManager does not know about these schemes.
static std::array< QByteArray, 7 > const names {
QByteArrayLiteral( "bres" ),
QByteArrayLiteral( "gdau" ),
QByteArrayLiteral( "gdlookup" ),
QByteArrayLiteral( "gdpicture" ),
QByteArrayLiteral( "gdvideo" ),
QByteArrayLiteral( "gico" ),
QByteArrayLiteral( "qrcx" )
};
return names;
}
QNetworkRequest networkRequestFromJob( QWebEngineUrlRequestJob & job )
{
QNetworkRequest request( job.requestUrl() );
auto const headers = job.requestHeaders();
for( auto it = headers.constKeyValueBegin(), end = headers.constKeyValueEnd(); it != end; ++it )
request.setRawHeader( it->first, it->second );
return request;
}
QWebEngineUrlRequestJob::Error jobErrorFromNetworkError( QNetworkReply::NetworkError networkError )
{
switch( networkError )
{
case QNetworkReply::HostNotFoundError:
case QNetworkReply::ProxyNotFoundError:
case QNetworkReply::ContentNotFoundError:
case QNetworkReply::ContentGoneError:
return QWebEngineUrlRequestJob::UrlNotFound;
case QNetworkReply::ProtocolUnknownError:
case QNetworkReply::ProtocolInvalidOperationError:
return QWebEngineUrlRequestJob::UrlInvalid;
case QNetworkReply::OperationCanceledError:
return QWebEngineUrlRequestJob::RequestAborted;
case QNetworkReply::ConnectionRefusedError:
case QNetworkReply::BackgroundRequestNotAllowedError:
case QNetworkReply::ProxyConnectionRefusedError:
case QNetworkReply::ProxyAuthenticationRequiredError:
case QNetworkReply::ContentAccessDenied:
case QNetworkReply::ContentOperationNotPermittedError:
case QNetworkReply::AuthenticationRequiredError:
return QWebEngineUrlRequestJob::RequestDenied;
default:
return QWebEngineUrlRequestJob::RequestFailed;
}
}
void reportError( QNetworkReply::NetworkError networkError, QNetworkReply const & reply, QWebEngineUrlRequestJob * job )
{
gdWarning( "Error while requesting URL %s: %s (%s)", qUtf8Printable( reply.url().toString() ),
qUtf8Printable( reply.errorString() ),
QMetaEnum::fromType< decltype( networkError ) >().valueToKey( networkError ) );
if( job )
job->fail( jobErrorFromNetworkError( networkError ) );
}
bool respondToUrlRequest( QWebEngineUrlRequestJob * job, QNetworkReply * reply )
{
Q_ASSERT( job );
Q_ASSERT( reply );
auto const networkError = reply->error();
if( networkError != QNetworkReply::NoError )
{
reportError( networkError, *reply, job );
return false;
}
auto contentType = reply->header( QNetworkRequest::ContentTypeHeader ).toByteArray();
if( contentType.isEmpty() )
{
// GoldenDict does not always set the Content-Type header => fall back to guessing the MIME type from the URL.
contentType = QMimeDatabase().mimeTypeForUrl( reply->url() ).name().toUtf8();
}
job->reply( contentType, reply );
return true;
}
} // unnamed namespace
void registerArticleUrlSchemes()
{
// When unhandled URL schemes - "gdprg", "gdtts", "bword" - are registered, a blank page is
// loaded when the user clicks on a link that points to such an URL => don't register them.
for( auto const & name : handledUrlSchemeNames() )
{
QWebEngineUrlScheme scheme( name );
scheme.setSyntax( QWebEngineUrlScheme::Syntax::Host );
// Register dictionary schemes as local to make file:// and qrc:// links work in articles.
scheme.setFlags( QWebEngineUrlScheme::SecureScheme
| QWebEngineUrlScheme::LocalScheme
| QWebEngineUrlScheme::LocalAccessAllowed );
QWebEngineUrlScheme::registerScheme( scheme );
}
}
ArticleUrlSchemeHandler::ArticleUrlSchemeHandler( ArticleNetworkAccessManager & netMgr ) :
QWebEngineUrlSchemeHandler( &netMgr ),
articleNetMgr( netMgr )
{
}
void ArticleUrlSchemeHandler::install( QWebEngineProfile & profile )
{
for( auto const & name : handledUrlSchemeNames() )
profile.installUrlSchemeHandler( name, this );
}
void ArticleUrlSchemeHandler::requestStarted( QWebEngineUrlRequestJob * job )
{
if( job->requestMethod() != "GET" )
{
gdWarning( "Unsupported custom scheme request method: %s. Initiator: %s. URL: %s.",
job->requestMethod().constData(), qUtf8Printable( job->initiator().toString() ),
qUtf8Printable( job->requestUrl().toString() ) );
job->fail( QWebEngineUrlRequestJob::UrlInvalid );
return;
}
auto * const reply = articleNetMgr.get( networkRequestFromJob( *job ) );
// Respond to the request immediately in order to show topmost articles as
// soon as they become available, without waiting for all articles to load.
// job uses the QIODevice interface of reply in another thread. *reply can be one of the following:
// - a qrc:// QNetworkAccessManager's QNetworkReply;
// - QNetworkAccessManager's QNetworkReply for a GoldenDict's custom URL scheme (an error page);
// - ArticleResourceReply;
// - BlockedNetworkReply.
// Hopefully the QNetworkAccessManager's replies support such usage. ArticleResourceReply is designed
// to be thread-safe in this scenario. BlockedNetworkReply is trivial and obviously thread-safe.
if( !respondToUrlRequest( job, reply ) )
{
// A network error occurred and respondToUrlRequest() handled it.
// Destroy the no longer needed reply as soon as it finishes.
connect( reply, &QNetworkReply::finished, reply, &QObject::deleteLater );
return;
}
QPointer< QWebEngineUrlRequestJob > jobPointer( job );
connect( reply, &QNetworkReply::errorOccurred, reply, [ reply, jobPointer ]( QNetworkReply::NetworkError code ) {
reportError( code, *reply, jobPointer );
} );
// Deliberately don't use job as context in this connection: if job is destroyed
// before reply is finished, reply would be leaked. Using reply as context does
// not impact behavior, but silences Clazy checker connect-3arg-lambda (level1).
connect( reply, &QNetworkReply::finished, reply, [ reply, jobPointer = std::move( jobPointer ) ] {
// At this point reply is no longer written to and can be safely destroyed once job ends reading from it.
if( jobPointer )
connect( jobPointer, &QObject::destroyed, reply, &QObject::deleteLater );
else
reply->deleteLater();
} );
}
#endif // USE_QTWEBKIT