Encoding Error while using SessionAuth tool

Showing 1-8 of 8 messages
Encoding Error while using SessionAuth tool Derek Litz 3/19/12 10:51 AM
I provided a method to override the check_username_and_password
method,  and after doing so upon error I got.

mod_wsgi (pid=7625): Exception occurred processing WSGI script
TypeError: sequence of byte string values expected, value of type
unicode found

I tried using tools.encode.on: True and tools.encode.encoding: 'utf-8'
in my conf, but that did not fix the problem.

I noticed in the tool the response body was being set directly so I
changed line 319 to:

response.body = body
response.body = body.encode('utf-8')

and that fixed the problem.

I noticed https://bitbucket.org/cherrypy/cherrypy/wiki/UpgradeTo32,
which seems like it may be related.  Perhaps someone with better
comprehension of the matters could shed some light for me?

1) Why didn't using tools.encode in my configuration work?  Is it
because the tool is setting the response body directly?
2) Is this a bug, poor implementation (of the tool), or user error?
3) Is there a way for me to fix this problem I'm encountering, besides
rewriting/overriding what I need to in the Session Auth tool?
Re: [cherrypy-users] Encoding Error while using SessionAuth tool Julien Cigar 3/20/12 1:57 AM

Did you already tried with: "tools.encode.text_only = False" ?

--
No trees were killed in the creation of this message.
However, many electrons were terribly inconvenienced.

Re: Encoding Error while using SessionAuth tool Derek Litz 3/20/12 7:30 AM
Just tried it with:

conf = {'/': {
                'tools.encode.on': True,
                'tools.encode.encoding': 'utf-8',
                'tools.encode.text_only': False,
                ...

Still getting the same error from mod_wsgi


 sequence of byte string values expected, value of type unicode found

What does setting text_only option True or False do?
Re: [cherrypy-users] Re: Encoding Error while using SessionAuth tool Julien Cigar 3/20/12 7:33 AM
On 03/20/2012 15:30, Derek Litz wrote:
> Just tried it with:
>
> conf = {'/': {
>                 'tools.encode.on': True,
>                 'tools.encode.encoding': 'utf-8',
>                 'tools.encode.text_only': False,
>                 ...
>
> Still getting the same error from mod_wsgi
>
>      sequence of byte string values expected, value of type unicode found
>
>
> What does setting text_only option True or False do?
>


by default the encoding tool encodes only "text/*", so it may fail with
"application/xml" or ...
I filled a ticket about that some time ago:
https://bitbucket.org/cherrypy/cherrypy/issue/1123/responseencoder-replace-text_only-with


> On Monday, March 19, 2012 12:51:03 PM UTC-5, Derek Litz wrote:
>
>     I provided a method to override the check_username_and_password
>     method,  and after doing so upon error I got.
>
>     mod_wsgi (pid=7625): Exception occurred processing WSGI script
>     TypeError: sequence of byte string values expected, value of type
>     unicode found
>
>     I tried using tools.encode.on: True and tools.encode.encoding:
>     'utf-8'
>     in my conf, but that did not fix the problem.
>
>     I noticed in the tool the response body was being set directly so I
>     changed line 319 to:
>
>     response.body = body
>     response.body = body.encode('utf-8')
>
>     and that fixed the problem.
>
>     I noticed https://bitbucket.org/cherrypy/cherrypy/wiki/UpgradeTo32
>     <https://bitbucket.org/cherrypy/cherrypy/wiki/UpgradeTo32>,

>     which seems like it may be related.  Perhaps someone with better
>     comprehension of the matters could shed some light for me?
>
>     1) Why didn't using tools.encode in my configuration work?  Is it
>     because the tool is setting the response body directly?
>     2) Is this a bug, poor implementation (of the tool), or user error?
>     3) Is there a way for me to fix this problem I'm encountering,
>     besides
>     rewriting/overriding what I need to in the Session Auth tool?
>
> --
> You received this message because you are subscribed to the Google
> Groups "cherrypy-users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/cherrypy-users/-/IDLAc1slNS0J.
> To post to this group, send email to cherryp...@googlegroups.com.
> To unsubscribe from this group, send email to
> cherrypy-user...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/cherrypy-users?hl=en.


--
No trees were killed in the creation of this message.
However, many electrons were terribly inconvenienced.

Re: Encoding Error while using SessionAuth tool Derek Litz 3/20/12 1:36 PM
Ok, I just noticed the tool DOES encode the body line 301 using the function ntob imported from cherrypy._cpcompat.

def login_screen(self, from_page='..', username='', error_msg='', **kwargs):
        return ntob("""<html><body>
Message: %(error_msg)s
<form method="post" action="do_login">
    Login: <input type="text" name="username" value="%(username)s" size="10" /><br />
    Password: <input type="password" name="password" size="10" /><br />
    <input type="hidden" name="from_page" value="%(from_page)s" /><br />
    <input type="submit" />
</form>
</body></html>""" % {'from_page': from_page, 'username': username,
                     'error_msg': error_msg}, "utf-8")

Just doing a quick test I can see the ntob function DOES NOT return a byte string as the documentation states: 'Return the given native string as a byte string in the given encoding.'

ntob('asfd')
Out[90]: 'asfd' <--- BAD

ntob(u'asdf')
Out[91]: u'asdf' <---- BAD

u'asdf'.encode('utf-8')
Out[92]: 'asdf' <--- GOOD

Python3

>>> 'asdfasdf'.encode('utf-8')
b'asdfasdf' <--- GOOD

>>> from cherrypy._cpcompat import basestring, ntob, md5, set
>>> ntob('asdf')
b'asdf' <--- GOOD

So this may only affect python2.x it seems.  Could someone confirm?


On Monday, March 19, 2012 12:51:03 PM UTC-5, Derek Litz wrote:
Re: Encoding Error while using SessionAuth tool Derek Litz 3/20/12 1:42 PM
hmm, ignore the first bad mis-marked it.  Also, I noticed the doc say it takes a 'native' string and encodes it, perhaps that is the issue?  The unicode string is coming from the form submission for username and password.
RE: [cherrypy-users] Re: Encoding Error while using SessionAuth tool fumanchu 3/20/12 2:45 PM

Good catch. That function should probably fail loudly if it receives a non-native string arg. A ticket would be appreciated.

 

 

Robert Brewer

fuma...@aminus.org

 

From: cherryp...@googlegroups.com [mailto:cherryp...@googlegroups.com] On Behalf Of Derek Litz
Sent: Tuesday, March 20, 2012 1:42 PM
To: cherryp...@googlegroups.com
Subject: [cherrypy-users] Re: Encoding Error while using SessionAuth tool

--

You received this message because you are subscribed to the Google Groups "cherrypy-users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/cherrypy-users/-/VT6BM4TJhbsJ.

To post to this group, send email to cherryp...@googlegroups.com.
To unsubscribe from this group, send email to cherrypy-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/cherrypy-users?hl=en.

Re: Encoding Error while using SessionAuth tool Derek Litz 3/20/12 8:06 PM
No, problem. https://bitbucket.org/cherrypy/cherrypy/issue/1132/cherrypy_cpcompatntob-fails-silently-on


On Monday, March 19, 2012 12:51:03 PM UTC-5, Derek Litz wrote: