Skip to content

Commit

Permalink
Fix: Request.GetPostData() throws UnicodeEncodeError (#517).
Browse files Browse the repository at this point in the history
  • Loading branch information
cztomczak committed Jan 11, 2020
1 parent e025351 commit 8f1484c
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 5 deletions.
7 changes: 5 additions & 2 deletions src/cefpython.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,11 @@ IF PY_MAJOR_VERSION == 2:
from urllib import pathname2url as urllib_pathname2url
# noinspection PyUnresolvedReferences
from urllib import urlencode as urllib_urlencode
from urllib import quote as urlparse_quote
ELSE:
# noinspection PyUnresolvedReferences
from urllib import parse as urlparse
from urllib.parse import quote as urlparse_quote
# noinspection PyUnresolvedReferences
from urllib.request import pathname2url as urllib_pathname2url
# noinspection PyUnresolvedReferences
Expand Down Expand Up @@ -1027,7 +1029,8 @@ cpdef LoadCrlSetsFile(py_string path):
CefLoadCRLSetsFile(PyToCefStringValue(path))

cpdef GetDataUrl(data, mediatype="html"):
html = data.encode("utf-8", "replace")
b64 = base64.b64encode(html).decode("utf-8", "replace")
if PY_MAJOR_VERSION >= 3:
data = data.encode("utf-8", "replace")
b64 = base64.b64encode(data).decode("utf-8", "replace")
ret = "data:text/html;base64,{data}".format(data=b64)
return ret
12 changes: 10 additions & 2 deletions src/request.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,16 @@ cdef class PyRequest:
retMultipart.append(pyData)
else:
# Content-Type: application/x-www-form-urlencoded
retUrlEncoded.update(urlparse.parse_qsl(qs=pyData,
quoted = urlparse_quote(pyData, safe="=")
retUrlEncoded.update(urlparse.parse_qsl(qs=quoted,
keep_blank_values=True))
if PY_MAJOR_VERSION >= 3:
retUrlEncoded_copy = copy.deepcopy(retUrlEncoded)
retUrlEncoded = dict()
for key in retUrlEncoded_copy:
retUrlEncoded[key.encode("utf-8", "replace")] =\
retUrlEncoded_copy[key].encode(
"utf-8", "replace")
elif postDataElement.get().GetType() == cef_types.PDE_TYPE_FILE:
pyFile = CefToPyBytes(postDataElement.get().GetFile())
retMultipart.append(b"@"+pyFile)
Expand Down Expand Up @@ -156,7 +164,7 @@ cdef class PyRequest:
postData.get().AddElement(postDataElement)
self.GetCefRequest().get().SetPostData(postData)
elif type(pyPostData) == dict:
pyElement = urllib_urlencode(pyPostData).encode()
pyElement = urllib_urlencode(pyPostData).encode("utf-8", "replace")
postDataElement = CefPostDataElement_Create()
postDataElement.get().SetToBytes(len(pyElement), <char*>pyElement)
postData.get().AddElement(postDataElement)
Expand Down
43 changes: 43 additions & 0 deletions unittests/issue517.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# coding=utf8
from cefpython3 import cefpython as cef
import sys

html = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
window.onload = function() {
fetch('http://127.0.0.1:8000', {
method: 'POST',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
body: 'key=' + encodeURI('🍣 asd'),
}).then().catch();
}
</script>
</body>
</html>
"""


class RequestHandler:
def GetResourceHandler(self, browser, frame, request):
print(request.GetPostData())
return None

def main():
sys.excepthook = cef.ExceptHook
cef.Initialize()
browser = cef.CreateBrowserSync(url=cef.GetDataUrl(html))
browser.SetClientHandler(RequestHandler())
cef.MessageLoop()
del browser
cef.Shutdown()


if __name__ == '__main__':
main()
2 changes: 1 addition & 1 deletion unittests/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ def test_main(self):
req_file = os.path.dirname(os.path.abspath(__file__))
req_file = os.path.join(req_file, "main_test.py")
if sys.version_info.major > 2:
req_file = req_file.encode()
req_file = req_file.encode("utf-8")
req_data = [b"--key=value", b"@"+req_file]
req.SetMethod("POST")
req.SetPostData(req_data)
Expand Down

1 comment on commit 8f1484c

@cztomczak
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also fixes an issue with cef.GetDataUrl() function in Python 2 when passing utf-8 characters.

Please sign in to comment.