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"
Code:
{
QStringList cookielist
= responseHeader.
allValues("set-cookie");
for (int i = 0; i < cookielist.size(); ++i) {
QString cokeline
= cookielist.
at(i
);
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();
}
Re: QHttpResponseHeader encode & Save COOKIE value %20
You can iterate through the string and convert all %xx matches to their hexadecimal value.
Re: QHttpResponseHeader encode & Save COOKIE value %20
Quote:
Originally Posted by
wysota
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 ...
Code:
#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))
{
char *newstrings = lop.data();
char *resulter = spc_decode_url(newstrings);
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;
}
Re: QHttpResponseHeader encode & Save COOKIE value %20
Quote:
Originally Posted by
patrik08
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
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().
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.
Re: QHttpResponseHeader encode & Save COOKIE value %20
Quote:
Originally Posted by
wysota
Is like this better?
Code:
{
/*
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 notaccept
= "%60|%5D|%5B|%7E|%5E|%5C|%7C|%7D|%7B";
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;
}
Re: QHttpResponseHeader encode & Save COOKIE value %20
I'd prefer something like that (not tested):
Code:
int pos = 0;
while((pos=rx.indexIn(stringToDecode, pos))!=-1){
uint val = rx.cap(1).toUInt(0, 16);
}
After this "stringToDecode" should contain the converted string.
Re: QHttpResponseHeader encode & Save COOKIE value %20
Quote:
Originally Posted by
wysota
I'd prefer something like that (not tested):
Code:
int pos = 0;
while((pos=rx.indexIn(stringToDecode, pos))!=-1){
uint val = rx.cap(1).toUInt(0, 16);
}
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)
Code:
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
}