Debian Codegraph @

thirdparty/kQOAuth/kqoauthauthreplyserver.cpp

raw
1 /**
2 * KQOAuth - An OAuth authentication library for Qt.
3 *
4 * Author: Johan Paul ([email protected])
5 * http://www.johanpaul.com
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * KQOAuth is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with KQOAuth. If not, see <http://www.gnu.org/licenses/>.
19 */
20 #include <QTcpSocket>
21 #include <QStringList>
22 #include <QUrl>
23 #if QT_VERSION >= 0x050000
24 #include <QUrlQuery>
25 #endif
26
27 #include "kqoauthauthreplyserver.h"
28 #include "kqoauthauthreplyserver_p.h"
29
30 KQOAuthAuthReplyServerPrivate::KQOAuthAuthReplyServerPrivate(KQOAuthAuthReplyServer *parent):
31 q_ptr(parent)
32 {
33
34 }
35
36 KQOAuthAuthReplyServerPrivate::~KQOAuthAuthReplyServerPrivate()
37 {
38
39 }
40
41 void KQOAuthAuthReplyServerPrivate::onIncomingConnection() {
42 Q_Q(KQOAuthAuthReplyServer);
43
44 socket = q->nextPendingConnection();
45 connect(socket, SIGNAL(readyRead()),
46 this, SLOT(onBytesReady()), Qt::UniqueConnection);
47 }
48
49 void KQOAuthAuthReplyServerPrivate::onBytesReady() {
50 Q_Q(KQOAuthAuthReplyServer);
51
52 QByteArray reply;
53 QByteArray content;
54 content.append("<HTML></HTML>");
55
56 reply.append("HTTP/1.0 200 OK \r\n");
57 reply.append("Content-Type: text/html; charset=\"utf-8\"\r\n");
58 reply.append(QString("Content-Length: %1\r\n").arg(content.size()));
59 reply.append("\r\n");
60 reply.append(content);
61 socket->write(reply);
62
63 QByteArray data = socket->readAll();
64 QMultiMap<QString, QString> queryParams = parseQueryParams(&data);
65
66 socket->disconnectFromHost();
67 q->close();
68 emit q->verificationReceived(queryParams);
69 }
70
71 QMultiMap<QString, QString> KQOAuthAuthReplyServerPrivate::parseQueryParams(QByteArray *data) {
72 QString splitGetLine = QString(*data).split("\r\n").first(); // Retrieve the first line with query params.
73 splitGetLine.remove("GET "); // Clean the line from GET
74 splitGetLine.remove("HTTP/1.1"); // From HTTP
75 splitGetLine.remove("\r\n"); // And from rest.
76 splitGetLine.prepend("http://localhost"); // Now, make it a URL
77
78 QUrl getTokenUrl(splitGetLine);
79 #if QT_VERSION < 0x050000
80 QList< QPair<QString, QString> > tokens = getTokenUrl.queryItems(); // Ask QUrl to do our work.
81 #else
82 QList< QPair<QString, QString> > tokens = QUrlQuery(getTokenUrl.query()).queryItems(); // Ask QUrl to do our work.
83 #endif
84
85 QMultiMap<QString, QString> queryParams;
86 QPair<QString, QString> tokenPair;
87 foreach (tokenPair, tokens) {
88 queryParams.insert(tokenPair.first.trimmed(), tokenPair.second.trimmed());
89 }
90
91 return queryParams;
92 }
93
94
95
96 KQOAuthAuthReplyServer::KQOAuthAuthReplyServer(QObject *parent) :
97 QTcpServer(parent),
98 d_ptr( new KQOAuthAuthReplyServerPrivate(this) )
99 {
100 Q_D(KQOAuthAuthReplyServer);
101
102 connect(this, SIGNAL(newConnection()),
103 d, SLOT(onIncomingConnection()));
104 }
105
106 KQOAuthAuthReplyServer::~KQOAuthAuthReplyServer()
107 {
108 delete d_ptr;
109 }
110
111