cefpython icon indicating copy to clipboard operation
cefpython copied to clipboard

WebRequestClient and ResourceHandler data tpye errorBUG

Open ctlddey opened this issue 4 years ago • 18 comments

` 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 avatar Aug 05 '21 08:08 ctlddey

env: python 3.7.9 X86 win10 X64 cefpython 66.1

ctlddey avatar Aug 05 '21 09:08 ctlddey

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

karolyjozsa avatar Sep 01 '21 04:09 karolyjozsa

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 avatar Sep 01 '21 08:09 cztomczak

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

cztomczak avatar Sep 01 '21 08:09 cztomczak

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 avatar Sep 02 '21 08:09 karolyjozsa

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?

karolyjozsa avatar Sep 04 '21 04:09 karolyjozsa

ReadResponse data_out[0] is bytes.

cztomczak avatar Sep 04 '21 14:09 cztomczak

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""]

karolyjozsa avatar Sep 06 '21 07:09 karolyjozsa

You can change it to bytes, you set it.

cztomczak avatar Sep 06 '21 09:09 cztomczak

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?

karolyjozsa avatar Sep 27 '21 07:09 karolyjozsa

You need to download v66-upstream binaries.

cztomczak avatar Sep 27 '21 09:09 cztomczak

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

cztomczak avatar Sep 28 '21 13:09 cztomczak

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()

karolyjozsa avatar Oct 01 '21 08:10 karolyjozsa

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

cztomczak avatar Oct 01 '21 14:10 cztomczak

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()?

karolyjozsa avatar Oct 07 '21 02:10 karolyjozsa

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

cztomczak avatar Oct 07 '21 14:10 cztomczak

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 avatar Oct 07 '21 14:10 cztomczak

Make sure you have this commit applied.

cztomczak avatar Oct 07 '21 14:10 cztomczak