forked from goldendict/goldendict
-
Notifications
You must be signed in to change notification settings - Fork 0
/
article_netmgr.hh
232 lines (177 loc) · 6.02 KB
/
article_netmgr.hh
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
/* This file is (c) 2008-2012 Konstantin Isakov <[email protected]>
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
#ifndef __ARTICLE_NETMGR_HH_INCLUDED__
#define __ARTICLE_NETMGR_HH_INCLUDED__
#include <QtNetwork>
#if QT_VERSION >= 0x050300 // Qt 5.3+
#include <QWebSecurityOrigin>
#include <QSet>
#include <QMap>
#include <QPair>
#endif
#include "dictionary.hh"
#include "article_maker.hh"
using std::vector;
/// A custom QNetworkAccessManager version which fetches images from the
/// dictionaries when requested.
#if QT_VERSION >= 0x050300 // Qt 5.3+
// White lists for QWebSecurityOrigin
struct SecurityWhiteList
{
QWebSecurityOrigin * origin;
QString originUri;
QSet< QPair< QString, QString > > hostsToAccess;
SecurityWhiteList() :
origin( 0 )
{}
~SecurityWhiteList()
{ swlDelete(); }
SecurityWhiteList( SecurityWhiteList const & swl ) :
origin( 0 )
{ swlCopy( swl ); }
SecurityWhiteList & operator=( SecurityWhiteList const & swl );
QWebSecurityOrigin * setOrigin( QUrl const & url );
private:
void swlCopy( SecurityWhiteList const & swl );
void swlDelete();
};
typedef QMap< QString, SecurityWhiteList > Origins;
// Proxy class for QNetworkReply to remove X-Frame-Options header
// It allow to show websites in <iframe> tag like Qt 4.x
class AllowFrameReply : public QNetworkReply
{
Q_OBJECT
private:
QNetworkReply * baseReply;
QByteArray buffer;
AllowFrameReply();
AllowFrameReply( AllowFrameReply const & );
public:
explicit AllowFrameReply( QNetworkReply * _reply );
~AllowFrameReply()
{ delete baseReply; }
// QNetworkReply virtual functions
void setReadBufferSize( qint64 size );
void close()
{ baseReply->close(); }
// QIODevice virtual functions
qint64 bytesAvailable() const;
bool atEnd() const
{ return baseReply->atEnd(); }
qint64 bytesToWrite() const
{ return baseReply->bytesToWrite(); }
bool canReadLine() const
{ return baseReply->canReadLine(); }
bool isSequential() const
{ return baseReply->isSequential(); }
bool waitForReadyRead( int msecs )
{ return baseReply->waitForReadyRead( msecs ); }
bool waitForBytesWritten( int msecs )
{ return baseReply->waitForBytesWritten( msecs ); }
bool reset()
{ return baseReply->reset(); }
public slots:
// Own AllowFrameReply slots
void applyMetaData();
void applyError( QNetworkReply::NetworkError code );
void readDataFromBase();
// Redirect QNetworkReply slots
virtual void abort()
{ baseReply->abort(); }
virtual void ignoreSslErrors()
{ baseReply->ignoreSslErrors(); }
protected:
// QNetworkReply virtual functions
void ignoreSslErrorsImplementation( const QList< QSslError > & errors )
{ baseReply->ignoreSslErrors( errors ); }
void setSslConfigurationImplementation( const QSslConfiguration & configuration )
{ baseReply->setSslConfiguration( configuration ); }
void sslConfigurationImplementation( QSslConfiguration & configuration ) const
{ configuration = baseReply->sslConfiguration(); }
// QIODevice virtual functions
qint64 readData( char * data, qint64 maxSize );
qint64 readLineData( char * data, qint64 maxSize )
{ return baseReply->readLine( data, maxSize ); }
qint64 writeData( const char * data, qint64 maxSize )
{ return baseReply->write( data, maxSize ); }
};
#endif
class ArticleNetworkAccessManager: public QNetworkAccessManager
{
vector< sptr< Dictionary::Class > > const & dictionaries;
ArticleMaker const & articleMaker;
bool const & disallowContentFromOtherSites;
bool const & hideGoldenDictHeader;
#if QT_VERSION >= 0x050300 // Qt 5.3+
Origins allOrigins;
#endif
public:
ArticleNetworkAccessManager( QObject * parent,
vector< sptr< Dictionary::Class > > const &
dictionaries_,
ArticleMaker const & articleMaker_,
bool const & disallowContentFromOtherSites_,
bool const & hideGoldenDictHeader_ ):
QNetworkAccessManager( parent ), dictionaries( dictionaries_ ),
articleMaker( articleMaker_ ),
disallowContentFromOtherSites( disallowContentFromOtherSites_ ),
hideGoldenDictHeader( hideGoldenDictHeader_ )
{}
/// Tries handling any kind of internal resources referenced by dictionaries.
/// If it succeeds, the result is a dictionary request object. Otherwise, an
/// empty pointer is returned.
/// The function can optionally set the Content-Type header correspondingly.
sptr< Dictionary::DataRequest > getResource( QUrl const & url,
QString & contentType );
protected:
virtual QNetworkReply * createRequest( Operation op,
QNetworkRequest const & req,
QIODevice * outgoingData );
};
class ArticleResourceReply: public QNetworkReply
{
Q_OBJECT
sptr< Dictionary::DataRequest > req;
qint64 alreadyRead;
public:
ArticleResourceReply( QObject * parent,
QNetworkRequest const &,
sptr< Dictionary::DataRequest > const &,
QString const & contentType );
~ArticleResourceReply();
protected:
virtual qint64 bytesAvailable() const;
virtual void abort()
{}
virtual qint64 readData( char * data, qint64 maxSize );
// We use the hackery below to work around the fact that we need to emit
// ready/finish signals after we've been constructed.
signals:
void readyReadSignal();
void finishedSignal();
private slots:
void reqUpdated();
void reqFinished();
void readyReadSlot();
void finishedSlot();
};
class BlockedNetworkReply: public QNetworkReply
{
Q_OBJECT
public:
BlockedNetworkReply( QObject * parent );
virtual qint64 readData( char *, qint64 )
{
return -1;
}
virtual void abort()
{}
protected:
// We use the hackery below to work around the fact that we need to emit
// ready/finish signals after we've been constructed.
signals:
void finishedSignal();
private slots:
void finishedSlot();
};
#endif