WebBrowser/Network/NetworkUrlInterceptor.py

Sun, 03 Feb 2019 16:10:39 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sun, 03 Feb 2019 16:10:39 +0100
changeset 6695
0a51887c13cd
parent 6645
ad476851d7e0
permissions
-rw-r--r--

Web Browser (NG):
- added a site info widget that is show when the site icon in the URL entry is clicked
- added code to register custom protocol handlers (upon request of the user via the site info widget)

4825
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
6645
ad476851d7e0 Updated copyright for 2019.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6281
diff changeset
3 # Copyright (c) 2016 - 2019 Detlev Offenbach <detlev@die-offenbachs.de>
4825
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 """
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 Module implementing a class to handle URL requests before they get processed
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 by QtWebEngine.
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9 """
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
10
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
11 from __future__ import unicode_literals
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
12
6281
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
13 from PyQt5.QtCore import QMutex, QMutexLocker, QUrl
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
14 from PyQt5.QtWebEngineCore import QWebEngineUrlRequestInterceptor, \
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
15 QWebEngineUrlRequestInfo
4825
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16
4906
939ff20f712d Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4857
diff changeset
17 from ..WebBrowserPage import WebBrowserPage
939ff20f712d Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4857
diff changeset
18
4825
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19 import Preferences
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22 class NetworkUrlInterceptor(QWebEngineUrlRequestInterceptor):
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23 """
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
24 Class implementing an URL request handler.
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25 """
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26 def __init__(self, parent=None):
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27 """
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28 Constructor
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30 @param parent reference to the parent object
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
31 @type QObject
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32 """
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33 super(NetworkUrlInterceptor, self).__init__(parent)
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35 self.__interceptors = []
6131
1ecd0c53e117 Changed the URL interceptor to properly lock the mutex.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
36 self.__mutex = QMutex()
4825
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
38 self.__loadSettings()
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
40 def interceptRequest(self, info):
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41 """
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42 Public method handling an URL request.
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44 @param info URL request information
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45 @type QWebEngineUrlRequestInfo
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 """
6131
1ecd0c53e117 Changed the URL interceptor to properly lock the mutex.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
47 locker = QMutexLocker(self.__mutex) # __IGNORE_WARNING__
1ecd0c53e117 Changed the URL interceptor to properly lock the mutex.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
48
4825
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
49 # Do Not Track feature
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
50 if self.__doNotTrack:
4857
8dba5fb92f12 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4826
diff changeset
51 info.setHttpHeader(b"DNT", b"1")
8dba5fb92f12 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4826
diff changeset
52 info.setHttpHeader(b"X-Do-Not-Track", b"1")
4825
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
53
6277
e22952cec018 Added a TODO marker to improve the Referer header handling in the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6131
diff changeset
54 # Send referrer header?
e22952cec018 Added a TODO marker to improve the Referer header handling in the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6131
diff changeset
55 if info.requestUrl().host() not in \
4825
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
56 Preferences.getWebBrowser("SendRefererWhitelist"):
6277
e22952cec018 Added a TODO marker to improve the Referer header handling in the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6131
diff changeset
57 self.__setRefererHeader(info)
4825
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
58
4906
939ff20f712d Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4857
diff changeset
59 # User Agents header
939ff20f712d Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4857
diff changeset
60 userAgent = WebBrowserPage.userAgentForUrl(info.requestUrl())
939ff20f712d Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4857
diff changeset
61 info.setHttpHeader(b"User-Agent", userAgent.encode())
939ff20f712d Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4857
diff changeset
62
4825
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
63 for interceptor in self.__interceptors:
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
64 interceptor.interceptRequest(info)
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
65
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
66 def installUrlInterceptor(self, interceptor):
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
67 """
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
68 Public method to install an URL interceptor.
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
69
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
70 @param interceptor URL interceptor to be installed
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
71 @type UrlInterceptor
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
72 """
6131
1ecd0c53e117 Changed the URL interceptor to properly lock the mutex.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
73 locker = QMutexLocker(self.__mutex) # __IGNORE_WARNING__
1ecd0c53e117 Changed the URL interceptor to properly lock the mutex.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
74
4825
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
75 if interceptor not in self.__interceptors:
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
76 self.__interceptors.append(interceptor)
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
77
4917
682750cc7bd5 Corrected some code style issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4906
diff changeset
78 def removeUrlInterceptor(self, interceptor):
4825
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
79 """
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
80 Public method to remove an URL interceptor.
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
81
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
82 @param interceptor URL interceptor to be removed
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
83 @type UrlInterceptor
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
84 """
6131
1ecd0c53e117 Changed the URL interceptor to properly lock the mutex.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
85 locker = QMutexLocker(self.__mutex) # __IGNORE_WARNING__
1ecd0c53e117 Changed the URL interceptor to properly lock the mutex.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
86
4825
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
87 if interceptor in self.__interceptors:
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
88 self.__interceptors.remove(interceptor)
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
89
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
90 def __loadSettings(self):
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
91 """
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
92 Private method to load the Network Manager settings.
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
93 """
6131
1ecd0c53e117 Changed the URL interceptor to properly lock the mutex.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
94 locker = QMutexLocker(self.__mutex) # __IGNORE_WARNING__
1ecd0c53e117 Changed the URL interceptor to properly lock the mutex.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
95
4825
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
96 self.__doNotTrack = Preferences.getWebBrowser("DoNotTrack")
6281
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
97 self.__sendReferer = Preferences.getWebBrowser("RefererSendReferer")
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
98 self.__refererDefaultPolicy = \
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
99 Preferences.getWebBrowser("RefererDefaultPolicy")
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
100 self.__refererTrimmingPolicy = \
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
101 Preferences.getWebBrowser("RefererTrimmingPolicy")
4825
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
102
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
103 def preferencesChanged(self):
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
104 """
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
105 Public slot to handle a change of preferences.
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
106 """
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
107 self.__loadSettings()
6281
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
108
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
109 def __setRefererHeader(self, info):
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
110 """
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
111 Private method to set the 'Referer' header depending on the configured
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
112 rule set.
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
113
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
114 @param info URL request information
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
115 @type QWebEngineUrlRequestInfo
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
116 @see <a href="https://wiki.mozilla.org/Security/Referrer">
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
117 Mozilla Referrer</a>
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
118 @see <a href="https://www.w3.org/TR/referrer-policy/">
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
119 W3C Referrer Policy</a>
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
120 """
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
121 # 1. SendReferer:
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
122 # 0 = never
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
123 # 1 = only on click (NavigationTypeLink)
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
124 # 2 = always (default)
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
125 # 2. RefererTrimmingPolicy:
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
126 # 0 = send full URL (no trimming) (default)
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
127 # 1 = send the URL without its query string
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
128 # 2 = only send the origin (ensure trailing /)
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
129 # 3. RefererDefaultPolicy:
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
130 # set the default referrer policy (which can be overriden by
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
131 # the site)
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
132 # 0 = no-referrer
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
133 # 1 = same-origin
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
134 # 2 = strict-origin-when-cross-origin
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
135 # 3 = no-referrer-when-downgrade (default)
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
136 # see: https://wiki.mozilla.org/Security/Referrer
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
137 # see: https://www.w3.org/TR/referrer-policy/
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
138
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
139 if self.__sendReferer == 0:
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
140 # never send referer header
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
141 info.setHttpHeader(b"Referer", b"")
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
142 elif (self.__sendReferer == 1 and
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
143 info.navigationType() !=
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
144 QWebEngineUrlRequestInfo.NavigationTypeLink):
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
145 # send referer header only on click
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
146 info.setHttpHeader(b"Referer", b"")
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
147 else:
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
148 # send referer header always applying further policies
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
149 url = info.firstPartyUrl()
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
150 reqUrl = info.requestUrl()
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
151 if self.__refererDefaultPolicy == 0:
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
152 # no-referrer
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
153 refererUrl = b""
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
154 elif self.__refererDefaultPolicy == 1:
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
155 # same-origin
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
156 if self.__sameOrigin(url, reqUrl):
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
157 refererUrl = self.__trimmedReferer(url)
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
158 else:
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
159 refererUrl = b""
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
160 elif self.__refererDefaultPolicy == 2:
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
161 # strict-origin-when-cross-origin
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
162 if self.__sameOrigin(url, reqUrl):
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
163 refererUrl = self.__trimmedReferer(url)
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
164 elif url.scheme() in ("https", "wss"):
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
165 if self.__potentiallyTrustworthy(url):
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
166 refererUrl = self.__refererOrigin(url)
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
167 else:
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
168 refererUrl = b""
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
169 else:
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
170 refererUrl = self.__refererOrigin(url)
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
171 else:
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
172 # no-referrer-when-downgrade
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
173 if url.scheme() in ("https", "wss") and \
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
174 not self.__potentiallyTrustworthy(url):
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
175 refererUrl = b""
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
176 else:
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
177 refererUrl = self.__trimmedReferer(url)
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
178
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
179 info.setHttpHeader(b"Referer", refererUrl)
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
180
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
181 def __sameOrigin(self, url1, url2):
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
182 """
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
183 Private method to test the "same origin" policy.
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
184
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
185 @param url1 first URL for the test
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
186 @type QUrl
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
187 @param url2 second URL for the test
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
188 @type QUrl
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
189 @return flag indicating that both URLs have the same origin
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
190 @rtype bool
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
191 """
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
192 origin1 = url1.url(QUrl.RemoveUserInfo | QUrl.RemovePath)
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
193 origin2 = url2.url(QUrl.RemoveUserInfo | QUrl.RemovePath)
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
194
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
195 return origin1 == origin2
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
196
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
197 def __potentiallyTrustworthy(self, url):
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
198 """
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
199 Private method to check, if the given URL is potentially trustworthy.
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
200
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
201 @param url URL to be checked
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
202 @type QUrl
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
203 @return flag indicating a potentially trustworthy URL
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
204 @rtype bool
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
205 """
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
206 if url.scheme() == "data":
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
207 return False
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
208
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
209 if url.toString() in ("about:blank", "about:srcdoc"):
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
210 return True
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
211
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
212 origin = url.adjusted(QUrl.RemoveUserInfo | QUrl.RemovePath)
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
213
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
214 if origin.isEmpty() or origin.scheme() == "":
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
215 return False
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
216 if origin.scheme() in ("https", "wss"):
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
217 return True
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
218 if origin.host().startswith("127.") or origin.host().endswith(":1"):
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
219 return True
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
220 if origin.host() == "localhost" or \
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
221 origin.host().endswith(".localhost"):
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
222 return True
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
223 if origin.scheme() == "file":
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
224 return True
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
225 if origin.scheme() in ("qrc", "qthelp", "eric"):
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
226 return True
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
227
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
228 return False
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
229
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
230 def __trimmedReferer(self, url):
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
231 """
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
232 Private method to generate the trimmed referer header URL.
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
233
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
234 @param url URL to be trimmed as a referer header
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
235 @type QUrl
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
236 @return trimmed referer header URL
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
237 @rtype QByteArray or bytes
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
238 """
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
239 if self.__refererTrimmingPolicy == 0:
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
240 # send full URL (no trimming) (default)
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
241 refererUrl = url.toEncoded(
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
242 QUrl.RemoveUserInfo | QUrl.RemoveFragment)
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
243 elif self.__refererTrimmingPolicy == 1:
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
244 # send the URL without its query string
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
245 refererUrl = url.toEncoded(
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
246 QUrl.RemoveUserInfo | QUrl.RemoveFragment |
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
247 QUrl.RemoveQuery)
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
248 else:
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
249 # only send the origin (ensure trailing /)
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
250 refererUrl = self.__refererOrigin(url)
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
251
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
252 return refererUrl
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
253
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
254 def __refererOrigin(self, url):
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
255 """
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
256 Private method to generate an origin referer header URL.
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
257
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
258 @param url URL to generate the header from
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
259 @type QUrl
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
260 @return origin referer header URL
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
261 @rtype QByteArray or bytes
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
262 """
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
263 referer = url.toEncoded(QUrl.RemoveUserInfo | QUrl.RemovePath)
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
264 if not referer.endsWith(b"/"):
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
265 referer += b"/"
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
266
9f7bbfd6545d WebBrowser, NetworkUrlInterceptor: added code to control the sending of the "Referer" header (similiar to Firefox)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6277
diff changeset
267 return referer

eric ide

mercurial