eric6/WebBrowser/Network/NetworkUrlInterceptor.py

Sat, 27 Feb 2021 12:08:23 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 27 Feb 2021 12:08:23 +0100
changeset 8138
169e65a6787c
parent 7923
91e843545d9a
child 8143
2c730d5fd177
permissions
-rw-r--r--

Shell: added functionality to show a prompt when the main client process has exited (e.g. a script ended).

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
7923
91e843545d9a Updated copyright for 2021.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7781
diff changeset
3 # Copyright (c) 2016 - 2021 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
7774
9eed155411f0 Changed code to our own context manager type mutex locker class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
11 from PyQt5.QtCore import QMutex, QUrl
7268
a28338eaf694 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
12 from PyQt5.QtWebEngineCore import (
a28338eaf694 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
13 QWebEngineUrlRequestInterceptor, QWebEngineUrlRequestInfo
a28338eaf694 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
14 )
4825
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15
7774
9eed155411f0 Changed code to our own context manager type mutex locker class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
16 from E5Utilities.E5MutexLocker import E5MutexLocker
9eed155411f0 Changed code to our own context manager type mutex locker class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
17
4906
939ff20f712d Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4857
diff changeset
18 from ..WebBrowserPage import WebBrowserPage
939ff20f712d Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4857
diff changeset
19
4825
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20 import Preferences
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
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23 class NetworkUrlInterceptor(QWebEngineUrlRequestInterceptor):
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
24 """
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25 Class implementing an URL request handler.
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26 """
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27 def __init__(self, parent=None):
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28 """
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29 Constructor
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
31 @param parent reference to the parent object
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32 @type QObject
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33 """
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34 super(NetworkUrlInterceptor, self).__init__(parent)
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36 self.__interceptors = []
6131
1ecd0c53e117 Changed the URL interceptor to properly lock the mutex.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
37 self.__mutex = QMutex()
4825
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
38
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39 self.__loadSettings()
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
40
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41 def interceptRequest(self, info):
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42 """
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43 Public method handling an URL request.
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45 @param info URL request information
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 @type QWebEngineUrlRequestInfo
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47 """
7774
9eed155411f0 Changed code to our own context manager type mutex locker class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
48 with E5MutexLocker(self.__mutex):
9eed155411f0 Changed code to our own context manager type mutex locker class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
49 # Do Not Track feature
9eed155411f0 Changed code to our own context manager type mutex locker class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
50 if self.__doNotTrack:
9eed155411f0 Changed code to our own context manager type mutex locker class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
51 info.setHttpHeader(b"DNT", b"1")
9eed155411f0 Changed code to our own context manager type mutex locker class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
52 info.setHttpHeader(b"X-Do-Not-Track", b"1")
9eed155411f0 Changed code to our own context manager type mutex locker class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
53
9eed155411f0 Changed code to our own context manager type mutex locker class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
54 # Send referrer header?
9eed155411f0 Changed code to our own context manager type mutex locker class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
55 if info.requestUrl().host() not in Preferences.getWebBrowser(
9eed155411f0 Changed code to our own context manager type mutex locker class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
56 "SendRefererWhitelist"):
9eed155411f0 Changed code to our own context manager type mutex locker class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
57 self.__setRefererHeader(info)
9eed155411f0 Changed code to our own context manager type mutex locker class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
58
9eed155411f0 Changed code to our own context manager type mutex locker class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
59 # User Agents header
9eed155411f0 Changed code to our own context manager type mutex locker class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
60 userAgent = WebBrowserPage.userAgentForUrl(info.requestUrl())
9eed155411f0 Changed code to our own context manager type mutex locker class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
61 info.setHttpHeader(b"User-Agent", userAgent.encode())
9eed155411f0 Changed code to our own context manager type mutex locker class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
62
9eed155411f0 Changed code to our own context manager type mutex locker class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
63 for interceptor in self.__interceptors:
9eed155411f0 Changed code to our own context manager type mutex locker class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
64 interceptor.interceptRequest(info)
4825
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 """
7774
9eed155411f0 Changed code to our own context manager type mutex locker class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
73 with E5MutexLocker(self.__mutex):
9eed155411f0 Changed code to our own context manager type mutex locker class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
74 if interceptor not in self.__interceptors:
9eed155411f0 Changed code to our own context manager type mutex locker class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
75 self.__interceptors.append(interceptor)
4825
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
76
4917
682750cc7bd5 Corrected some code style issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4906
diff changeset
77 def removeUrlInterceptor(self, interceptor):
4825
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
78 """
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
79 Public method to remove an URL interceptor.
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
80
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
81 @param interceptor URL interceptor to be removed
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
82 @type UrlInterceptor
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
83 """
7774
9eed155411f0 Changed code to our own context manager type mutex locker class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
84 with E5MutexLocker(self.__mutex):
9eed155411f0 Changed code to our own context manager type mutex locker class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
85 if interceptor in self.__interceptors:
9eed155411f0 Changed code to our own context manager type mutex locker class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
86 self.__interceptors.remove(interceptor)
4825
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
87
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
88 def __loadSettings(self):
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 Private method to load the Network Manager settings.
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
91 """
7774
9eed155411f0 Changed code to our own context manager type mutex locker class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
92 with E5MutexLocker(self.__mutex):
7775
4a1db75550bd Changed code to not use deprecated 'QRegExp' anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7774
diff changeset
93 self.__doNotTrack = Preferences.getWebBrowser(
4a1db75550bd Changed code to not use deprecated 'QRegExp' anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7774
diff changeset
94 "DoNotTrack")
4a1db75550bd Changed code to not use deprecated 'QRegExp' anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7774
diff changeset
95 self.__sendReferer = Preferences.getWebBrowser(
4a1db75550bd Changed code to not use deprecated 'QRegExp' anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7774
diff changeset
96 "RefererSendReferer")
7774
9eed155411f0 Changed code to our own context manager type mutex locker class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
97 self.__refererDefaultPolicy = Preferences.getWebBrowser(
9eed155411f0 Changed code to our own context manager type mutex locker class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
98 "RefererDefaultPolicy")
9eed155411f0 Changed code to our own context manager type mutex locker class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
99 self.__refererTrimmingPolicy = Preferences.getWebBrowser(
9eed155411f0 Changed code to our own context manager type mutex locker class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
100 "RefererTrimmingPolicy")
4825
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
101
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
102 def preferencesChanged(self):
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
103 """
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
104 Public slot to handle a change of preferences.
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
105 """
b5fb58f3830b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
106 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
107
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 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
109 """
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 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
111 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
112
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 @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
114 @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
115 @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
116 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
117 @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
118 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
119 """
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 # 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
121 # 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
122 # 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
123 # 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
124 # 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
125 # 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
126 # 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
127 # 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
128 # 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
129 # 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
130 # 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
131 # 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
132 # 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
133 # 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
134 # 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
135 # 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
136 # 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
137
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 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
139 # 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
140 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
141 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
142 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
143 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
144 # 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
145 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
146 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
147 # 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
148 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
149 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
150 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
151 # 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
152 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
153 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
154 # 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
155 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
156 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
157 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
158 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
159 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
160 # 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
161 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
162 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
163 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
164 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
165 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
166 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
167 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
168 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
169 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
170 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
171 # no-referrer-when-downgrade
7268
a28338eaf694 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
172 if (
a28338eaf694 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
173 url.scheme() in ("https", "wss") and
a28338eaf694 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
174 not self.__potentiallyTrustworthy(url)
a28338eaf694 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
175 ):
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
176 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
177 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
178 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
179
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 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
181
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 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
183 """
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 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
185
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 @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
187 @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
188 @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
189 @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
190 @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
191 @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
192 """
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 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
194 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
195
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 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
197
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 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
199 """
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 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
201
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 @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
203 @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
204 @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
205 @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
206 """
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 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
208 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
209
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 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
211 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
212
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 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
214
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 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
216 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
217 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
218 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
219 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
220 return True
7268
a28338eaf694 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
221 if (
a28338eaf694 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
222 origin.host() == "localhost" or
a28338eaf694 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
223 origin.host().endswith(".localhost")
a28338eaf694 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
224 ):
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
225 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
226 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
227 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
228 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
229 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
230
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 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
232
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 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
234 """
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 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
236
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 @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
238 @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
239 @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
240 @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
241 """
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 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
243 # 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
244 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
245 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
246 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
247 # 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
248 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
249 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
250 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
251 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
252 # 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
253 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
254
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 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
256
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 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
258 """
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 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
260
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 @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
262 @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
263 @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
264 @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
265 """
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 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
267 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
268 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
269
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
270 return referer

eric ide

mercurial