Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request.SetPostData() and GetPostData don't work. All strings are expected to be byte strings. #228

Closed
stanfea opened this issue Jun 7, 2016 · 14 comments
Labels
Milestone

Comments

@stanfea
Copy link

stanfea commented Jun 7, 2016

Can't seem to get SetPostData() to work, should accept a list or a dict:

>> from cefpython3 import cefpython
>>> req = cefpython.Request.CreateRequest()
>>> req.SetPostData([])
Segmentation fault (core dumped)

Python 2.7.11+
cefpython 31.2-1

@cztomczak
Copy link
Owner

You are setting an empty data. Does the error occur also when setting non-empty data or when not calling SetPostData at all?

@stanfea
Copy link
Author

stanfea commented Jun 7, 2016

I've tried with a dict {'key': 'value'} and with a list ["--data=1"]. It only seg faults when calling SetPostData.
At the moment I'm trying to rebuild cefpython from source to debug.

@cztomczak
Copy link
Owner

You can get stacktrace using gdb, no need to build from source to do that. See:
https://github.com/cztomczak/cefpython/wiki/InternalDevelopment#debug-cef-stack-trace

@cztomczak
Copy link
Owner

cztomczak commented Jun 7, 2016

@stanfea
Copy link
Author

stanfea commented Jun 9, 2016

The cef stacktrace isn't very useful:

Thread 1 "python2.7" received signal SIGSEGV, Segmentation fault.
__pyx_f_14cefpython_py27_9PyRequest_SetPostData (__pyx_v_self=<optimized out>, 
    __pyx_v_pyPostData=[], __pyx_skip_dispatch=<optimized out>)
    at cefpython.cpp:43604
43604   cefpython.cpp: No such file or directory.
(gdb) bt
#0  __pyx_f_14cefpython_py27_9PyRequest_SetPostData (
    __pyx_v_self=<optimized out>, __pyx_v_pyPostData=[], 
    __pyx_skip_dispatch=<optimized out>) at cefpython.cpp:43604
#1  0x00007fffe6531a30 in __pyx_pf_14cefpython_py27_9PyRequest_10SetPostData (
    __pyx_v_pyPostData=<optimized out>, __pyx_v_self=<optimized out>)
    at cefpython.cpp:43968
#2  __pyx_pw_14cefpython_py27_9PyRequest_11SetPostData (
    __pyx_v_self=<optimized out>, __pyx_v_pyPostData=<optimized out>)
    at cefpython.cpp:43946
#3  0x00000000004ca77e in call_function (oparg=<optimized out>, 
    pp_stack=0x7fffffffd690) at ../Python/ceval.c:4338
#4  PyEval_EvalFrameEx () at ../Python/ceval.c:2987
#5  0x00000000004ca39f in fast_function (nk=<optimized out>, 
    na=<optimized out>, n=<optimized out>, pp_stack=0x7fffffffd7e0, 
    func=<function at remote 0x7fffe4939668>) at ../Python/ceval.c:4435
#6  call_function (oparg=<optimized out>, pp_stack=0x7fffffffd7e0)
    at ../Python/ceval.c:4370
#7  PyEval_EvalFrameEx () at ../Python/ceval.c:2987
#8  0x00000000004ca39f in fast_function (nk=<optimized out>, 
    na=<optimized out>, n=<optimized out>, pp_stack=0x7fffffffd930, 
    func=<function at remote 0x7fffe49396e0>) at ../Python/ceval.c:4435
#9  call_function (oparg=<optimized out>, pp_stack=0x7fffffffd930)
    at ../Python/ceval.c:4370
---Type <return> to continue, or q <return> to quit---
#10 PyEval_EvalFrameEx () at ../Python/ceval.c:2987
#11 0x00000000004c2e05 in PyEval_EvalCodeEx () at ../Python/ceval.c:3582
#12 0x00000000004c2ba9 in PyEval_EvalCode (co=<optimized out>, 
    globals=<optimized out>, locals=<optimized out>) at ../Python/ceval.c:669
#13 0x00000000004f20ef in run_mod.lto_priv () at ../Python/pythonrun.c:1376
#14 0x00000000004eca72 in PyRun_FileExFlags () at ../Python/pythonrun.c:1362
#15 0x00000000004eb1f1 in PyRun_SimpleFileExFlags ()
    at ../Python/pythonrun.c:948
#16 0x000000000049e18a in Py_Main () at ../Modules/main.c:640
#17 0x00007ffff7811830 in __libc_start_main (main=0x49daf0 <main>, argc=3, 
    argv=0x7fffffffdd78, init=<optimized out>, fini=<optimized out>, 
    rtld_fini=<optimized out>, stack_end=0x7fffffffdd68)
    at ../csu/libc-start.c:291
#18 0x000000000049da19 in _start ()

Still trying to get cython debugging working.

But is the issue reproduceable on your end?

@cztomczak
Copy link
Owner

The stacktrace at least says something useful - the error is in the cefpython code. It fails on this line:

postData.get().RemoveElements()

https://github.com/cztomczak/cefpython/blob/cefpython31/cefpython/request_cef3.pyx#L116

I looked at upstream CEF code and postdata is set to NULL when it's empty, meaning that postData.get() may return NULL. Thus you can't call RemoveElements() on it.

Looks like these functions (GetPostData, SetPostData) were never really tested.

This code also fails:

req.SetMethod("POST")
req.GetPostData()

Fails on this line:

if postData.get().GetElementCount() == 0:
            return {}

https://github.com/cztomczak/cefpython/blob/cefpython31/cefpython/request_cef3.pyx#L69

Similarly, postData.get() returns NULL, thus segmentation fault when calling GetElementCount().

Fix: always check postData.get() for non-NULL value before calling a method on it.

@cztomczak
Copy link
Owner

Already fixed in my local repo, will commit today. Pasting test code which passes OK x6 after fixes:

from cefpython3 import cefpython as cef

print("Test 1")
req = cef.Request.CreateRequest()
req.SetMethod("POST")
req.SetPostData([])
print("OK")

print("Test 2")
req = cef.Request.CreateRequest()
req.SetMethod("POST")
req.SetPostData(["--key=value"])
print("OK")

print("Test 3")
req = cef.Request.CreateRequest()
req.SetMethod("POST")
req.SetPostData({"key":"value"})
req.GetPostData()
print("OK")

print("Test 4")
req = cef.Request.CreateRequest()
req.SetMethod("POST")
req.GetPostData()
print("OK")

print("Test 5")
req = cef.Request.CreateRequest()
req.SetPostData(["--key=value"])
print("OK")

print("Test 6")
req = cef.Request.CreateRequest()
req.GetPostData()
print("OK")

cztomczak added a commit that referenced this issue Dec 15, 2016
Update to Cython 0.25.2.

Fix Certificate Transparency support causing some SSL sites fail
to load after some time (#279).

Fix ApplicationSettings.cache_path not working (#283).

Fix: Not a clean shutdown in examples, when closing app while browser
is still loading a website (#282).

Fix loading local filesystem urls that contained any of ? & =
characters (#273).

Fix Request.SetPostData and GetPostData segmentation faults (#228).

Add ApplicationSettings.net_security_expiration_enabled.

Update ExecuteJavascript docs with scriptUrl="" and startLine=1
default params. Also worth noting that ExecuteJavascript crashed
in earlier CEF versions when startLine wasn't provided or was <= 0
(Issue #268).

Add a test that measures execution time for calling Python function
from javascript and then js callback.

Add Contributing guidelines / Issue template.

Update automate.py, include ceftests executable.
@cztomczak
Copy link
Owner

Fixed in bfc0a25. v55.2 released. Please test.

@cztomczak cztomczak added this to the v55 milestone Feb 24, 2017
@cztomczak
Copy link
Owner

This issue seems to be still unresolved. req.GetPostData returns an empty dict. Code:

print("Test postdata")
req = cef.Request.CreateRequest()
req.SetMethod("POST")
req.SetPostData({"key":"value"})
print(req.GetPostData())

Reported on the Forum:
https://groups.google.com/d/topic/cefpython/KtFsudPlFHE/discussion

@cztomczak cztomczak reopened this Jun 20, 2017
@cztomczak cztomczak changed the title SetPostData() Seg Fault Request.SetPostData() and GetPostData don't work Jun 20, 2017
@cztomczak cztomczak removed this from the v55 milestone Jun 20, 2017
@ritu1337
Copy link

Tested on v57.0, v55.3 and v55.2.
Also doesn't work from within the RequestHandler.

@cztomczak
Copy link
Owner

The code in question is here:

  1. https://github.com/cztomczak/cefpython/blob/master/src/request.pyx#L65
  2. https://github.com/cztomczak/cefpython/blob/master/src/request.pyx#L117

If someone can analyze the code and find the bug that would be helpful.

@sonya75
Copy link

sonya75 commented Jun 22, 2017

You are missing this line at the end of SetPostData function:-
self.GetCefRequest().get().SetPostData(postData)

@cztomczak
Copy link
Owner

@sonya75 Thanks Sonya, that was it!

Fixed in revision 82ed469.

This time tested properly. Code:

from cefpython3 import cefpython as cef

print("Test 1")
req = cef.Request.CreateRequest()
req.SetMethod("POST")
req.SetPostData(["--key=value"])
print(req.GetPostData())

print("Test 2")
req = cef.Request.CreateRequest()
req.SetMethod("POST")
req.SetPostData({"key":"value"})
print(req.GetPostData())

Output:

Test 1
['--key=value']
Test 2
{'key': 'value'}

The fix was applied to master branch and will be available in the next release (schedule not yet known). If you want this applied now, then apply the fix from the commit above to the "cefpython57" branch and follow the quick instructions from the docs/Build-instructions.md document to build the cefpython module from sources by using prebuilt CEF binaries from GitHub. With these quick instructions building cefpython takes about 10 minutes.

@cztomczak cztomczak added this to the v66 milestone May 30, 2018
@cztomczak cztomczak changed the title Request.SetPostData() and GetPostData don't work Request.SetPostData() and GetPostData don't work. All strings are expected to be byte strings. Aug 13, 2018
@cztomczak
Copy link
Owner

Further fixes for Python 3 in rev 4c5cf09.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants