PDA

View Full Version : QHttpResponseHeader encode & Save COOKIE value %20



patrik08
29th October 2006, 13:18
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"





void readResponseHeader(const QHttpResponseHeader &responseHeader )
{
QStringList cookielist = responseHeader.allValues("set-cookie");
for (int i = 0; i < cookielist.size(); ++i) {
QString cokeline = cookielist.at(i);
QStringList onlines = cokeline.split("=");
QString cookiename = onlines.at(0);
QString cookievalue = onlines.at(1);
qDebug() << "### entry set-cookie pos=" << i << " name= " << cookiename << " value=" << cookievalue;
}
emit WatReturn(QString("One code=%1 - Other say = %2").arg( responseHeader.statusCode() ).arg( responseHeader.reasonPhrase() ) );
////////qDebug() << "### QHttpResponseHeader " << responseHeader.toString();
}

wysota
29th October 2006, 13:39
You can iterate through the string and convert all %xx matches to their hexadecimal value.

patrik08
29th October 2006, 17:11
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 ...



#include <stdlib.h>
#include <string.h>
#include <ctype.h>


#define SPC_BASE16_TO_10(x) (((x) >= '0' && (x) <= '9') ? ((x) - '0') : \
(toupper((x)) - 'A' + 10))

QString Url_Decode( QString indata )
{
QByteArray lop = indata.toAscii();
char *newstrings = lop.data();
char *resulter = spc_decode_url(newstrings);
QString salt = QString("%1").arg(resulter);
QString resoap = salt.replace("+"," ");
return resoap;
}
/* found http://www.oreillynet.com/pub/a/network/excerpt/spcookbook_chap03/index2.html */
char *spc_decode_url(const char *url) {
char *out, *ptr;
const char *c;

if (!(out = ptr = strdup(url))) return 0;
for (c = url; *c; c++) {
if (*c != '%' || !isxdigit(c[1]) || !isxdigit(c[2])) *ptr++ = *c;
else {
*ptr++ = (SPC_BASE16_TO_10(c[1]) * 16) + (SPC_BASE16_TO_10(c[2]));
c += 2;
}
}
*ptr = 0;
if ( strlen(url) == (ptr - out)); /* does not include null byte */
return out;
}

jacek
29th October 2006, 17:45
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"?


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().

wysota
29th October 2006, 19:52
I guess it would be better to use QRegExp::indexIn(), QRegExp::cap() and QString::replace() instead of that mess.

patrik08
31st October 2006, 10:03
I guess it would be better to use QRegExp::indexIn(), QRegExp::cap() and QString::replace() instead of that mess.

Is like this better?



QString Url_Decode( QString indata )
{
/*
http://www.blooberry.com/indexdot/html/topics/urlencoding.htm
Dollar ("$") 24
Ampersand ("&") 26
Plus ("+") 2B
Comma (",") 2C
Forward slash/Virgule ("/") 2F
Colon (":") 3A
Semi-colon (";") 3B
Equals ("=") 3D
Question mark ("?") 3F
'At' symbol ("@") 40
Left Curly Brace ("{") 7B
Right Curly Brace ("}") 7D
Vertical Bar/Pipe ("|") 7C
Backslash ("\") 5C
Caret ("^") 5E
Tilde ("~") 7E
Left Square Bracket ("[") 5B
Right Square Bracket ("]") 5D
Grave Accent ("`") 60
*/
QString blnull = "";
QString notaccept = "%60|%5D|%5B|%7E|%5E|%5C|%7C|%7D|%7B";
QStringList notallow;
notallow = notaccept.split("|");

for (int i = 0; i < notallow.size(); ++i) {
if ( indata.contains(notallow.at(i)) ) {
return blnull;
}
}

QString spaceout = indata.replace("%20"," ");
spaceout = spaceout.replace("%3A",":");
spaceout = spaceout.replace("%3B",";");
spaceout = spaceout.replace("%3D","=");
spaceout = spaceout.replace("%3F","?");
spaceout = spaceout.replace("%40","@");
spaceout = spaceout.replace("%24","$");
spaceout = spaceout.replace("%2B","+");
spaceout = spaceout.replace("+"," ");
return spaceout;
}

wysota
1st November 2006, 21:53
I'd prefer something like that (not tested):


QString stringToDecode;
QRegExp rx("%(\\d\\d)");
int pos = 0;
while((pos=rx.indexIn(stringToDecode, pos))!=-1){
uint val = rx.cap(1).toUInt(0, 16);
stringToDecode.replace(pos, 3, QString(QChar(val)));
}

After this "stringToDecode" should contain the converted string.

braaja
8th April 2009, 22:22
I'd prefer something like that (not tested):


QString stringToDecode;
QRegExp rx("%(\\d\\d)");
int pos = 0;
while((pos=rx.indexIn(stringToDecode, pos))!=-1){
uint val = rx.cap(1).toUInt(0, 16);
stringToDecode.replace(pos, 3, QString(QChar(val)));
}

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)



QString stringToDecode;
QRegExp rx("%(\\S\\S)");
int pos = 0;
while ((pos = rx.indexIn(stringToDecode, pos)) != -1) {
stringToDecode.replace(pos, 3, QString(QChar(rx.cap(1).toUInt(0, 16))));
pos++; // jumps over currently replaced character
}