2016-10-26 2 views
0

Possible duplicate QuestionQt QUrlQuery extrait jeton QByteArray

Je générer un lien de réinitialisation/jeton qui sera envoyé par courriel au client comme suit.

QByteArray token; 
    token.resize(crypto_pwhash_SALTBYTES); 
    randombytes_buf(token.data(), crypto_pwhash_SALTBYTES); 

    QUrlQuery token_url("http://localhost:8080/reset/staff"); 
    token_url.addQueryItem("token", token); 
    token_url.query(QUrl::FullyEncoded).toUtf8(); 

Cela semble produire la sortie correcte:

http://localhost:8080/reset/staff&token=pc%22%EF%BF%BD%C6%9Fsktx%EF%BF%BD!%06t%5C%0B 

Pour authentifier la demande de remise à zéro contre le hachage stocké j'ai besoin du QByteArray jeton.

QByteArray path = request.getPath(); 
qDebug() << path; 

QUrlQuery token_url(path); 
QString token(token_url.queryItemValue("token")); 
qDebug() << token; 

qDebug() << QUrl::fromPercentEncoding(path); 

La sortie dont l'URL est collé dans firefox charabia

"/reset/staff&token=pc\"\xEF\xBF\xBD\xC6\x9Fsktx\xEF\xBF\xBD!\x06t\\\x0B" 
"pc\"�Ɵsktx�!%06t\\%0B" 
"/reset/staff&token=pc\"�Ɵsktx�!\u0006t\\\u000B" 

Je comprends que je fais probablement un chien unsigned char converstion.

Quelle est la méthode élégante pour faire passer QBytearrays dans QUrlQuery?

J'ai vu des programmeurs écrire leurs propres analyseurs d'URL, mais cela semble excessif.

Répondre

0

[RESOLU] Le problème était avec QtWebApps HttpRequest :: decodeRequestParams() l'URL était à double décodé plus QUrlQuery.query() ne fonctionne pas calme comme on peut s'y attendre.

K.I.S.S Coder votre URL manuellement

QByteArray url("http://localhost:8080/reset/staff?"); 
url.append("&token="+token.toPercentEncoding()); 

qDebug() << url; 

À l'extrémité de réception

split the query from "?" 
then split at "&" into key=value pairs 
then split at "=" 
finally QByteArray::fromPercentEncoding(key) 
finally QByteArray::fromPercentEncoding(value) 

Je suis ouvert à des améliorations si quelqu'un a une solution plus élégante.