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

It seems that cookies is a bug? ? #802

Closed
wanggp3 opened this issue Feb 25, 2016 · 7 comments
Closed

It seems that cookies is a bug? ? #802

wanggp3 opened this issue Feb 25, 2016 · 7 comments

Comments

@wanggp3
Copy link

wanggp3 commented Feb 25, 2016

url = 'http://www.asos.com/Missguided/Missguided-Longline-Padded-Bomber/Prod/pgeproduct.aspx?iid=6206713'

#You can see that currencyid=2 will decide one currency, here id=2 is USD
cookies = {'asos': 'currencyid=2&topcatid=9057'} 

#in normal way
def fetch1(url,cookies):
    import requests
    r = requests.get(url,cookies=cookies)
    body = r.content  # You can find the value with tag 'product_price_details' is in 68.43$
    #print(body)

def fetch2(url,cookies):
   session = aiohttp.ClientSession(cookies=cookies):
   r = yield from session.get(url)
   body = yield from r.read() # You can find the value with tag 'product_price_detail' isn't in   $
   #print(body) 


if __name__ == '__main__':
    fetch1(url,cookies)#In the requests, when you change currencyid in cookies , you can see different currency, but the aiohtttp..


   loop = asyncio.get_event_loop() 
   loop.run_until_complete(fetch2(url,cookies)) # The aiohttp, the currency doesn't come into effect
   loop.close()

SO,Is That a bug , the cookies ?

@panda73111
Copy link
Contributor

requests sends the cookie value without quotes:
Cookie: asos=currencyid=2&topcatid=9057.
aiohttp uses python's http module and sends the cookie value with quotes:
COOKIE: asos="currencyid=2&topcatid=9057".
The website seems to only accept the first one, but I honestly don't know what the "correct" version is, here.

@asvetlov
Copy link
Member

Quoted cookie value is absolutely correct IFAIK.

@asvetlov
Copy link
Member

Aha.
RFC 6265 explicitly defines cookie value as:

cookie-value      = *cookie-octet / ( DQUOTE *cookie-octet DQUOTE )

So quoting is correct.

As workaround you may pass COOKIE header explicitly: session.get(url, headers={'COOKIE': 'asos=currencyid=2&topcatid=9057'})

Closing the issue.

@wanggp3
Copy link
Author

wanggp3 commented Feb 29, 2016

Thanks all very much!

@nibrag
Copy link

nibrag commented Jun 13, 2016

Maybe we should make quoting optional? For example http://google.com/search does not recognize quoted cookies. I am sure many other web servers also do not understand quoted cookies.

@facuman
Copy link

facuman commented Jul 4, 2016

Another workaround is to define a custom request class (see awful hack below).

class UnquotedClientRequest(aiohttp.ClientRequest):
    unquoted_cookies = False

    def output_unquoted_cookie(self, c, attrs=None, header="Set-Cookie:", sep="\015\012"):
        """Return a string suitable for HTTP."""
        result = []
        items = sorted(c.items())
        for key, value in items:        
            value.coded_value = value.value
            result.append(value.output(attrs, header))

        return sep.join(result)


    def update_cookies(self, cookies):

        """Update request cookies header."""
        if not cookies:
            return

        c = http.cookies.SimpleCookie()
        if hdrs.COOKIE in self.headers:            
            c.load(self.headers.get(hdrs.COOKIE, ''))
            del self.headers[hdrs.COOKIE]

        if isinstance(cookies, dict):
            cookies = cookies.items()

        for name, value in cookies:
            if isinstance(value, http.cookies.Morsel):
                c[value.key] = value.value
            else:
                c[name] = value

        if self.unquoted_cookies:
            self.headers[hdrs.COOKIE] = self.output_unquoted_cookie(c, header='', sep=';').strip()        
        else:            
            self.headers[hdrs.COOKIE] = c.output(header='', sep=';').strip()        


...
MyClientRequest.unquoted_cookies = True
...
with aiohttp.ClientSession(..., request_class=UnquotedClientRequest) as session:
...

Cheers

@lock
Copy link

lock bot commented Oct 29, 2019

This thread has been automatically locked since there has not been
any recent activity after it was closed. Please open a new issue for
related bugs.

If you feel like there's important points made in this discussion,
please include those exceprts into that new issue.

@lock lock bot added the outdated label Oct 29, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 29, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants