Skip to content

Commit 6cc7f9c

Browse files
committed
Fix issues with urls containing special characters (cztomczak#384). BC BREAK!
Updated Migration-Guide.md document and other documentation.
1 parent b03b78b commit 6cc7f9c

7 files changed

Lines changed: 36 additions & 58 deletions

File tree

api/Browser.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ Returns the focused [Frame](Frame.md) for the browser window.
376376
| name | string |
377377
| __Return__ | Frame |
378378

379-
Returns the [Frame](Frame.md) with the specified name, or NULL if not found.
379+
Returns the [Frame](Frame.md) with the specified name, or NULL if not found.
380380

381381

382382
### GetFrameByIdentifier
@@ -697,6 +697,13 @@ Returns true if window rendering is disabled.
697697

698698
Load url in the main frame.
699699

700+
If the url is a local path it needs to start with the `file://` prefix.
701+
If the url contains special characters it may need proper handling.
702+
Starting with v66.1+ it is required for the app code to encode the url
703+
properly. You can use the `pathlib.PurePath.as_uri` in Python 3
704+
or `urllib.pathname2url` in Python 2 (`urllib.request.pathname2url`
705+
in Python 3) depending on your case.
706+
700707

701708
### Navigate
702709

api/Frame.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,16 @@ Take also a look at a [custom resource handler](ResourceHandler.md).
228228

229229
Load the contents of |value| with the specified dummy |url|. |url|
230230
should have a standard scheme (for example, http scheme) or behaviors like
231-
link clicks and web security restrictions may not behave as expected.
231+
link clicks and web security restrictions may not behave as expected.
232232
LoadString() can be called only after the Renderer process has been created.
233233

234+
If the url is a local path it needs to start with the `file://` prefix.
235+
If the url contains special characters it may need proper handling.
236+
Starting with v66.1+ it is required for the app code to encode the url
237+
properly. You can use the `pathlib.PurePath.as_uri` in Python 3
238+
or `urllib.pathname2url` in Python 2 (`urllib.request.pathname2url`
239+
in Python 3) depending on your case.
240+
234241

235242
### LoadUrl
236243

api/cefpython.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ All parameters are optional.
5959

6060
This function can only be called on the UI thread.
6161

62+
If the url is a local path it needs to start with the `file://` prefix.
63+
If the url contains special characters it may need proper handling.
64+
Starting with v66.1+ it is required for the app code to encode the url
65+
properly. You can use the `pathlib.PurePath.as_uri` in Python 3
66+
or `urllib.pathname2url` in Python 2 (`urllib.request.pathname2url`
67+
in Python 3) depending on your case.
68+
6269
The "window_title" parameter will be used only when parent
6370
window provided in window_info was set to 0. This is for use
6471
with hello_world.py and tutorial.py examples which don't use
@@ -240,7 +247,7 @@ Description from upstream CEF:
240247
| __Return__ | void |
241248

242249
Run the CEF message loop. Use this function instead of an application-
243-
provided message loop to get the best balance between performance and
250+
provided message loop to get the best balance between performance and
244251
CPU usage. This function should only be called on the main application
245252
thread (UI thread) and only if cefpython.Initialize() is called with a
246253
[ApplicationSettings](ApplicationSettings.md).multi_threaded_message_loop

docs/Migration-guide.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ Table of contents:
5050
* [v66+ cef.Request.Flags changed](#v66-cefrequestflags-changed)
5151
* [v66+ RequestHandler.GetCookieManager not getting called in some cases](#v66-requesthandlergetcookiemanager-not-getting-called-in-some-cases)
5252
* [v66+ Changes to Mac apps that integrate into existing message loop (Qt, wxPython)](#v66-changes-to-mac-apps-that-integrate-into-existing-message-loop-qt-wxpython)
53+
* [v66.1+ Navigation urls passed to CreateBrowserSync or LoadUrl methods need to be encoded by app code](#v661-navigation-urls-passed-to-createbrowsersync-or-loadurl-methods-need-to-be-encoded-by-app-code)
5354
* [v67+ Do not call the 'WindowUtils.OnSize' function](#v67-do-not-call-the-windowutilsonsize-function)
5455

5556

@@ -487,12 +488,21 @@ tested of how this change affects performance.
487488

488489
See Issue [#442](../../../issues/442) for more details on the issues.
489490

491+
## v66.1+ Navigation urls passed to CreateBrowserSync or LoadUrl methods need to be encoded by app code
492+
493+
[Issue #384](../../../issues/384) fixes problems with browser failing to load
494+
urls containing certain characters by not encoding the url anymore. From now
495+
on it is required for the app code to encode the url properly. You can use
496+
the `pathlib.PurePath.as_uri` in Python 3 or `urllib.pathname2url` in
497+
Python 2 (`urllib.request.pathname2url` in Python 3) depending on your case.
498+
499+
The `cef.GetNavigateUrl` function was removed from the cefpython3 module.
500+
490501

491502
## v67+ Do not call the 'WindowUtils.OnSize' function
492503

493504
This function can sometimes cause app hanging during window resize.
494505
Call instead the new `WindowUtils`.[UpdateBrowserSize](../api/WindowUtils.md#updatebrowsersize)
495506
function. Except when you use the `pywin32.py` example, in such case
496507
`WindowUtils.OnSize` must be called.
497-
See [Issue #464](../../../issues/464) for more details.
498-
508+
See [Issue #464](../../../issues/464) for more details.

src/cefpython.pyx

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,17 +144,13 @@ IF PY_MAJOR_VERSION == 2:
144144
# noinspection PyUnresolvedReferences
145145
import urlparse
146146
# noinspection PyUnresolvedReferences
147-
from urllib import pathname2url as urllib_pathname2url
148-
# noinspection PyUnresolvedReferences
149147
from urllib import urlencode as urllib_urlencode
150148
from urllib import quote as urlparse_quote
151149
ELSE:
152150
# noinspection PyUnresolvedReferences
153151
from urllib import parse as urlparse
154152
from urllib.parse import quote as urlparse_quote
155153
# noinspection PyUnresolvedReferences
156-
from urllib.request import pathname2url as urllib_pathname2url
157-
# noinspection PyUnresolvedReferences
158154
from urllib.parse import urlencode as urllib_urlencode
159155

160156
# noinspection PyUnresolvedReferences
@@ -739,8 +735,6 @@ def CreateBrowserSync(windowInfo=None,
739735
cdef CefWindowInfo cefWindowInfo
740736
SetCefWindowInfo(cefWindowInfo, windowInfo)
741737

742-
navigateUrl = GetNavigateUrl(navigateUrl)
743-
Debug("navigateUrl: %s" % navigateUrl)
744738
cdef CefString cefNavigateUrl
745739
PyToCefString(navigateUrl, cefNavigateUrl)
746740

src/frame.pyx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,6 @@ cdef class PyFrame:
211211
self.GetCefFrame().get().LoadString(cefValue, cefUrl)
212212

213213
cpdef py_void LoadUrl(self, py_string url):
214-
url = GetNavigateUrl(url)
215214
cdef CefString cefUrl
216215
PyToCefString(url, cefUrl)
217216
self.GetCefFrame().get().LoadURL(cefUrl)

src/utils.pyx

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -72,52 +72,6 @@ cpdef str GetSystemError():
7272
ELSE:
7373
return ""
7474

75-
cpdef str GetNavigateUrl(py_string url):
76-
# Encode local file paths so that CEF can load them correctly:
77-
# | some.html, some/some.html, D:\, /var, file://
78-
if re.search(r"^file:", url, re.I) or \
79-
re.search(r"^[a-zA-Z]:", url) or \
80-
not re.search(r"^[\w-]+:", url):
81-
82-
# Function pathname2url will complain if url starts with "file://".
83-
# CEF may also change local urls to "file:///C:/" - three slashes.
84-
is_file_protocol = False
85-
file_prefix = ""
86-
file_prefixes = ["file:///", "file://"]
87-
for file_prefix in file_prefixes:
88-
if url.startswith(file_prefix):
89-
is_file_protocol = True
90-
# Remove the file:// prefix
91-
url = url[len(file_prefix):]
92-
break
93-
94-
# Need to encode chinese characters in local file paths,
95-
# otherwise CEF will try to encode them by itself. But it
96-
# will fail in doing so. CEF will return the following string:
97-
# >> %EF%BF%97%EF%BF%80%EF%BF%83%EF%BF%A6
98-
# But it should be:
99-
# >> %E6%A1%8C%E9%9D%A2
100-
url = urllib_pathname2url(url)
101-
102-
if is_file_protocol:
103-
url = "%s%s" % (file_prefix, url)
104-
105-
# If it is C:\ then colon was encoded. Decode it back.
106-
url = re.sub(r"^([a-zA-Z])%3A", r"\1:", url)
107-
108-
# Allow hash when loading urls. The pathname2url function
109-
# replaced hashes with "%23" (Issue #114).
110-
url = url.replace("%23", "#")
111-
112-
# Allow more special characters when loading urls. The pathname2url
113-
# function encoded them and need to decode them back here
114-
# Characters: ? & = (Issue #273).
115-
url = url.replace("%3F", "?")
116-
url = url.replace("%26", "&")
117-
url = url.replace("%3D", "=")
118-
119-
return str(url)
120-
12175
cpdef py_bool IsFunctionOrMethod(object valueType):
12276
if (valueType == types.FunctionType
12377
or valueType == types.MethodType

0 commit comments

Comments
 (0)