cztomczak / cefpython

Python bindings for the Chromium Embedded Framework (CEF)
Other
3.03k stars 469 forks source link

WebRequestClient and ResourceHandler data tpye errorBUG #629

Open ctlddey opened 2 years ago

ctlddey commented 2 years ago

` def OnDownloadData(self, web_request:PyRequest, data:bytes): print("OnDownloadData()", type(data), data)

    self._data += data

OnDownloadData() <class 'str'> �PNG  �[ ����b�U嘶Н�;�%%��C'��� ѡ�ތ��L�~pR��h�c������� ���� l�p�u�ŅI�F��F�v���:D�? �w�@�@��o� U�D��-��4&S�&(������ ������f�����{�֒� gܰB�wR�.��"f�R�s�'<E�@�H_wͺg�~���� D�M"�]��(���v�~�i�%�� � �8%M6�� v7�HBir���X�T ��

The api document say the data should be bytes, but I recieved is string,so the web page is broken

ctlddey commented 2 years ago

env: python 3.7.9 X86 win10 X64 cefpython 66.1

karolyjozsa commented 2 years ago

I was to raise this issue, then found this. I wonder whether there is a work-around (e.g. somehow patching the WebRequest) so that we can use it before the bug is officially fixed. Thanks

cztomczak commented 2 years ago

This seems to be fixed via https://github.com/cztomczak/cefpython/commit/5679f28cec18a57a56e298da2927aac8d8f83ad6#diff-e4a5fba8f9cbe6d805afd50840dceeed893bf658eee2f58f4ce075d4c4853b2a . You need to build from sources to apply fix. See the quick build instructions in docs.

cztomczak commented 2 years ago

This fix appeared after the 66.1 release, so you need to pull latest changes from master.

karolyjozsa commented 2 years ago

I followed the steps in quick build (I also had to install Windows SDK), but it seems to look explicitly for Win7 SDK and does not work on Win10. Also, I need to run my program on Win10 and Win7 as well, but I can only build on Win10, so probably I need to wait for the new release, I cannot build it myself.

karolyjozsa commented 2 years ago

I suspect that even if I could build it from master, I would get stuck in ResourceHandler.ReadResponse, because its data_out[0] has str type, and should also be bytes. Or does it get fixed automatically by commit 5679f28?

cztomczak commented 2 years ago

ReadResponse data_out[0] is bytes.

karolyjozsa commented 2 years ago

When I put this print into my ReadResponse method, It states def ReadResponse(self, data_out, bytes_to_read, bytes_read_out, callback): print(f"ReadResponse(f"{type(data_out[0])}")

And I might have found the reason in handlers/resource_handler.pyx, line 153: dataOut = [""] This initiates the list with an empty string, instead of an empty bytes. It should be: dataOut = [b""]

cztomczak commented 2 years ago

You can change it to bytes, you set it.

karolyjozsa commented 2 years ago

I managed to install Win7 SDK on Win10 and trying to build it from master, but the 66.1 zip package seems to be missing the "lib" folder. Linking "subprocess" fails with not finding the "libcef.lib". Could you please make it available?

cztomczak commented 2 years ago

You need to download v66-upstream binaries.

cztomczak commented 2 years ago

This should be explained in Build instructions document. The v66-upstream is a GitHub release tag.

karolyjozsa commented 2 years ago

I managed to compile it with the lib as well as the bin copied over from v66-upstream, thanks. I had to modify resource_handler.pyx, line 165: if dataOut[0] and IsString(dataOut[0]): --> if dataOut[0]: Data is always bytes, not str, i.e. the IsString condition practically would prevent the data to be copied and returned in ResourceHandler_ReadResponse()

cztomczak commented 2 years ago

Are you sure? IsString name be misleading, I looked at source and it should return true for both bytes and string.

See https://github.com/cztomczak/cefpython/blob/d2c72528addad8ef41a2a460946f32fe425ed183/src/utils.pyx#L28

karolyjozsa commented 2 years ago

Indeed, IsString() returns True on a bytes object. Well, I see still no point having this check as the data must always be bytes. Unfortunately, the code still does not work in Python3. I checked that the data is right now after it has been corrected to bytes, but it must go wrong somewhere after the ReadResponse(), because the web page still does not display correctly. Can it be caused by the memcpy(), the (&cefBytesRead)[0], or somewhere after ResourceHandler_ReadResponse()?

cztomczak commented 2 years ago

I'm not sure. You would need to debug.

cztomczak commented 2 years ago

These PR/issues suggest it should work fine in Python 3 with the commit fix: https://github.com/cztomczak/cefpython/issues/620 https://github.com/cztomczak/cefpython/pull/621 https://github.com/cztomczak/cefpython/commit/5679f28cec18a57a56e298da2927aac8d8f83ad6

cztomczak commented 2 years ago

Make sure you have this commit applied.