Results 1 to 8 of 8

Thread: QHttpResponseHeader encode & Save COOKIE value %20

  1. #1
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QHttpResponseHeader encode & Save COOKIE value %20

    I must save valid cookie to resend at server the next time ....
    but QUrl QUrl::fromEncoded "cookievalue" can not decode ... is not a url...
    i thear other way to become a clean value decoded %20 + ....??
    or i must replace all sign and special char....

    cookie incomming = "Sunday+29th+of+October+2006+02%3A21%3A30+PM"


    Qt Code:
    1. void readResponseHeader(const QHttpResponseHeader &responseHeader )
    2. {
    3. QStringList cookielist = responseHeader.allValues("set-cookie");
    4. for (int i = 0; i < cookielist.size(); ++i) {
    5. QString cokeline = cookielist.at(i);
    6. QStringList onlines = cokeline.split("=");
    7. QString cookiename = onlines.at(0);
    8. QString cookievalue = onlines.at(1);
    9. qDebug() << "### entry set-cookie pos=" << i << " name= " << cookiename << " value=" << cookievalue;
    10. }
    11. emit WatReturn(QString("One code=%1 - Other say = %2").arg( responseHeader.statusCode() ).arg( responseHeader.reasonPhrase() ) );
    12. ////////qDebug() << "### QHttpResponseHeader " << responseHeader.toString();
    13. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    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: QHttpResponseHeader encode & Save COOKIE value %20

    You can iterate through the string and convert all %xx matches to their hexadecimal value.

  3. #3
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QHttpResponseHeader encode & Save COOKIE value %20

    Quote Originally Posted by wysota View Post
    You can iterate through the string and convert all %xx matches to their hexadecimal value.

    So..... i can read cookie on window .... i hope is run on Mac & Linux ...

    Qt Code:
    1. #include <stdlib.h>
    2. #include <string.h>
    3. #include <ctype.h>
    4.  
    5.  
    6. #define SPC_BASE16_TO_10(x) (((x) >= '0' && (x) <= '9') ? ((x) - '0') : \
    7. (toupper((x)) - 'A' + 10))
    8.  
    9. QString Url_Decode( QString indata )
    10. {
    11. QByteArray lop = indata.toAscii();
    12. char *newstrings = lop.data();
    13. char *resulter = spc_decode_url(newstrings);
    14. QString salt = QString("%1").arg(resulter);
    15. QString resoap = salt.replace("+"," ");
    16. return resoap;
    17. }
    18. /* found http://www.oreillynet.com/pub/a/network/excerpt/spcookbook_chap03/index2.html */
    19. char *spc_decode_url(const char *url) {
    20. char *out, *ptr;
    21. const char *c;
    22.  
    23. if (!(out = ptr = strdup(url))) return 0;
    24. for (c = url; *c; c++) {
    25. if (*c != '%' || !isxdigit(c[1]) || !isxdigit(c[2])) *ptr++ = *c;
    26. else {
    27. *ptr++ = (SPC_BASE16_TO_10(c[1]) * 16) + (SPC_BASE16_TO_10(c[2]));
    28. c += 2;
    29. }
    30. }
    31. *ptr = 0;
    32. if ( strlen(url) == (ptr - out)); /* does not include null byte */
    33. return out;
    34. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QHttpResponseHeader encode & Save COOKIE value %20

    Quote Originally Posted by patrik08 View Post
    QByteArray lop = indata.toAscii();
    char *newstrings = lop.data();
    char *resulter = spc_decode_url(newstrings);
    QString salt = QString("%1").arg(resulter);
    QString resoap = salt.replace("+"," ");
    What will happen if indata contains "%2b"?

    Quote Originally Posted by patrik08 View Post
    if ( strlen(url) == (ptr - out)); /* does not include null byte */
    This actually does nothing. Probably you were supposed to add something yourself there.

    And you have a memory leak, since you never free the memory allocated by strdup().

  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: QHttpResponseHeader encode & Save COOKIE value %20

    I guess it would be better to use QRegExp::indexIn(), QRegExp::cap() and QString::replace() instead of that mess.

  6. #6
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QHttpResponseHeader encode & Save COOKIE value %20

    Quote Originally Posted by wysota View Post
    I guess it would be better to use QRegExp::indexIn(), QRegExp::cap() and QString::replace() instead of that mess.
    Is like this better?

    Qt Code:
    1. QString Url_Decode( QString indata )
    2. {
    3. /*
    4. http://www.blooberry.com/indexdot/html/topics/urlencoding.htm
    5. Dollar ("$") 24
    6. Ampersand ("&") 26
    7. Plus ("+") 2B
    8. Comma (",") 2C
    9. Forward slash/Virgule ("/") 2F
    10. Colon (":") 3A
    11. Semi-colon (";") 3B
    12. Equals ("=") 3D
    13. Question mark ("?") 3F
    14. 'At' symbol ("@") 40
    15. Left Curly Brace ("{") 7B
    16. Right Curly Brace ("}") 7D
    17. Vertical Bar/Pipe ("|") 7C
    18. Backslash ("\") 5C
    19. Caret ("^") 5E
    20. Tilde ("~") 7E
    21. Left Square Bracket ("[") 5B
    22. Right Square Bracket ("]") 5D
    23. Grave Accent ("`") 60
    24. */
    25. QString blnull = "";
    26. QString notaccept = "%60|%5D|%5B|%7E|%5E|%5C|%7C|%7D|%7B";
    27. QStringList notallow;
    28. notallow = notaccept.split("|");
    29.  
    30. for (int i = 0; i < notallow.size(); ++i) {
    31. if ( indata.contains(notallow.at(i)) ) {
    32. return blnull;
    33. }
    34. }
    35.  
    36. QString spaceout = indata.replace("%20"," ");
    37. spaceout = spaceout.replace("%3A",":");
    38. spaceout = spaceout.replace("%3B",";");
    39. spaceout = spaceout.replace("%3D","=");
    40. spaceout = spaceout.replace("%3F","?");
    41. spaceout = spaceout.replace("%40","@");
    42. spaceout = spaceout.replace("%24","$");
    43. spaceout = spaceout.replace("%2B","+");
    44. spaceout = spaceout.replace("+"," ");
    45. return spaceout;
    46. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    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: QHttpResponseHeader encode & Save COOKIE value %20

    I'd prefer something like that (not tested):

    Qt Code:
    1. QString stringToDecode;
    2. QRegExp rx("%(\\d\\d)");
    3. int pos = 0;
    4. while((pos=rx.indexIn(stringToDecode, pos))!=-1){
    5. uint val = rx.cap(1).toUInt(0, 16);
    6. stringToDecode.replace(pos, 3, QString(QChar(val)));
    7. }
    To copy to clipboard, switch view to plain text mode 

    After this "stringToDecode" should contain the converted string.

  8. The following user says thank you to wysota for this useful post:

    patrik08 (2nd November 2006)

  9. #8
    Join Date
    Mar 2009
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows
    Wiki edits
    2

    Exclamation Re: QHttpResponseHeader encode & Save COOKIE value %20

    Quote Originally Posted by wysota View Post
    I'd prefer something like that (not tested):

    Qt Code:
    1. QString stringToDecode;
    2. QRegExp rx("%(\\d\\d)");
    3. int pos = 0;
    4. while((pos=rx.indexIn(stringToDecode, pos))!=-1){
    5. uint val = rx.cap(1).toUInt(0, 16);
    6. stringToDecode.replace(pos, 3, QString(QChar(val)));
    7. }
    To copy to clipboard, switch view to plain text mode 

    After this "stringToDecode" should contain the converted string.
    I recommend this code - it eliminates errors caused by previous code (hex A-F are not decimal = unrecognized by previous code, "hi%25there" represents "hi%there" = causes unwanted character creation "%th" - last replaced character has to be jumped over)

    Qt Code:
    1. QString stringToDecode;
    2. QRegExp rx("%(\\S\\S)");
    3. int pos = 0;
    4. while ((pos = rx.indexIn(stringToDecode, pos)) != -1) {
    5. stringToDecode.replace(pos, 3, QString(QChar(rx.cap(1).toUInt(0, 16))));
    6. pos++; // jumps over currently replaced character
    7. }
    To copy to clipboard, switch view to plain text mode 

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.