Results 1 to 6 of 6

Thread: QNetworkAccessManager can't handle multiple cookies?

  1. #1
    Join Date
    May 2009
    Posts
    14
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default QNetworkAccessManager can't handle multiple cookies?

    Hi all,

    I'm wondering if anybody else sees this problem.

    When I use QNetworkAccessManager to handle HTTP connections, I have noticed that if the server issues several Set-Cookie headers that only the first one is added to the cookie jar.

    If I dump the headers from the QNetworkReply I see that "Set-Cookie" shows all of the cookies that are received from the server, but if I do this immediately after:

    Qt Code:
    1. QList<QNetworkCookie> cookieList = qvariant_cast<QList<QNetworkCookie> >(reply->header(QNetworkRequest::SetCookieHeader));
    2. int count = cookieList.count();
    To copy to clipboard, switch view to plain text mode 

    "count" is always 1. And then if I access the cookie jar for the corresponding URL it only returns 1 cookie. I have a feeling that because it's several headers with the same name it's combining them all into one header and thus one cookie.

    I'm going to see if I can track this down in the sources, but just wondering in the meantime if this works correctly for anybody else?

    Thanks
    Last edited by krippy2k; 7th June 2009 at 19:05.

  2. #2
    Join Date
    May 2009
    Posts
    14
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QNetworkAccessManager can't handle multiple cookies?

    Alright, I see what the problem is here. When multiple headers are added with the same name they are concatenated into a single header and separated with a new line '\n'. Then the static method QNetworkCookie::parseCookies is supposed to split the cookies up into separate cookies, but that method is broken.

    From what I can tell that method thinks that cookies are separated by commas rather than newlines, because I see this case:

    Qt Code:
    1. case ',':
    2. // end of the cookie
    3. endOfCookie = true;
    4. break;
    To copy to clipboard, switch view to plain text mode 

    At first glance it would seem that just changing that ',' to '\n' would fix it, but no. What happens is everytime it encounters a ";" it thinks that a new field of the cookie is going to follow, and it jumps ahead to the next non-whitespace character after that, and starts the name of the next cookie as a new field in this cookie.

    It would probably take a refactor of the parseCookies method to fix it correctly along the lines of what they're trying to do, but I found a temporary fix that works, and is probably a better solution anyway.

    Near the beginning of this method is this:

    Qt Code:
    1. int position = 0;
    2. const int length = cookieString.length();
    3. while (position < length) {
    To copy to clipboard, switch view to plain text mode 

    If we change that to this:
    Qt Code:
    1. QList<QByteArray> cookieStrings = cookieString.split('\n');
    2. foreach( QByteArray cookieString, cookieStrings )
    3. {
    4. int position = 0;
    5. const int length = cookieString.length();
    To copy to clipboard, switch view to plain text mode 

    It will first break the set of cookies up into separate QByteArrays, and then process them individually so it doesn't have to worry about trying to find the separator character each time through.

    I'm going to file a bug report after this, but figured I would post it in here in case anybody else runs into this problem.

    Cheers

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QNetworkAccessManager can't handle multiple cookies?

    From what I know this is fixed in Qt 4.5.2
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    May 2009
    Posts
    14
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QNetworkAccessManager can't handle multiple cookies?

    Ahh okay. Is there a 4.5.2 release or is that soon to come?

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QNetworkAccessManager can't handle multiple cookies?

    It's not yet released. But if you download the latest snapshot from the repository, the fix should already be there.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    May 2009
    Posts
    14
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QNetworkAccessManager can't handle multiple cookies?

    Okay, thanks a bunch. I did not even realize there were daily snapshots available. I'll be sure to test everything with the latest from now on.

Similar Threads

  1. How to put custom handle image in QSlider using code?
    By montylee in forum Qt Programming
    Replies: 6
    Last Post: 29th January 2009, 19:38
  2. OS/X how to get physical display handle
    By hvengel in forum Qt Programming
    Replies: 4
    Last Post: 3rd January 2009, 19:51
  3. QSlider custom handle image not displayed
    By planglois in forum Qt Programming
    Replies: 1
    Last Post: 5th September 2008, 13:49
  4. Multiple project files in a single directory
    By jogeshwarakundi in forum Qt for Embedded and Mobile
    Replies: 5
    Last Post: 17th July 2008, 07:03
  5. how to corss compile for windows in Linux
    By safknw in forum Qt Programming
    Replies: 24
    Last Post: 13th May 2006, 05:23

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.