25ae508062e514eaaea9ee8a360ea4ee9709bddf
[googlelatitude] / libkqoauth / kqoauthauthreplyserver.cpp
1 /**
2  * KQOAuth - An OAuth authentication library for Qt.
3  *
4  * Author: Johan Paul (johan.paul@d-pointer.com)
5  *         http://www.d-pointer.com
6  *
7  *  KQOAuth is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU Lesser General Public License as published by
9  *  the Free Software Foundation, either version 3 of the License, or
10  *  (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
24 #include "kqoauthauthreplyserver.h"
25 #include "kqoauthauthreplyserver_p.h"
26
27 KQOAuthAuthReplyServerPrivate::KQOAuthAuthReplyServerPrivate(KQOAuthAuthReplyServer *parent):
28     q_ptr(parent)
29 {
30
31 }
32
33 KQOAuthAuthReplyServerPrivate::~KQOAuthAuthReplyServerPrivate()
34 {
35
36 }
37
38 void KQOAuthAuthReplyServerPrivate::onIncomingConnection() {
39     Q_Q(KQOAuthAuthReplyServer);
40
41     socket = q->nextPendingConnection();
42     connect(socket, SIGNAL(readyRead()),
43             this, SLOT(onBytesReady()), Qt::UniqueConnection);
44 }
45
46 void KQOAuthAuthReplyServerPrivate::onBytesReady() {
47     Q_Q(KQOAuthAuthReplyServer);
48     
49     QByteArray reply;
50     QByteArray content;
51     content.append("<HTML></HTML>");
52
53     reply.append("HTTP/1.0 200 OK \r\n");
54     reply.append("Content-Type: text/html; charset=\"utf-8\"\r\n");
55     reply.append(QString("Content-Length: %1\r\n").arg(content.size()));
56     reply.append("\r\n");
57     reply.append(content);
58     socket->write(reply);
59     
60     QByteArray data = socket->readAll();
61     QMultiMap<QString, QString> queryParams = parseQueryParams(&data);
62
63     socket->disconnectFromHost();
64     q->close();
65     emit q->verificationReceived(queryParams);
66 }
67
68 QMultiMap<QString, QString> KQOAuthAuthReplyServerPrivate::parseQueryParams(QByteArray *data) {
69     QString splitGetLine = QString(*data).split("\r\n").first();   // Retrieve the first line with query params.
70     splitGetLine.remove("GET ");                                   // Clean the line from GET
71     splitGetLine.remove("HTTP/1.1");                               // From HTTP
72     splitGetLine.remove("\r\n");                                   // And from rest.
73     splitGetLine.prepend("http://localhost");                      // Now, make it a URL
74
75     QUrl getTokenUrl(splitGetLine);
76     QList< QPair<QString, QString> > tokens = getTokenUrl.queryItems();  // Ask QUrl to do our work.
77
78     QMultiMap<QString, QString> queryParams;
79     QPair<QString, QString> tokenPair;
80     foreach (tokenPair, tokens) {
81         queryParams.insert(tokenPair.first.trimmed(), tokenPair.second.trimmed());
82     }
83
84     return queryParams;
85 }
86
87
88
89 KQOAuthAuthReplyServer::KQOAuthAuthReplyServer(QObject *parent) :
90     QTcpServer(parent),
91     d_ptr( new KQOAuthAuthReplyServerPrivate(this) )
92 {
93     Q_D(KQOAuthAuthReplyServer);
94
95     connect(this, SIGNAL(newConnection()),
96             d, SLOT(onIncomingConnection()));
97 }
98
99 KQOAuthAuthReplyServer::~KQOAuthAuthReplyServer()
100 {
101     delete d_ptr;
102 }
103
104