File: kqoauthauthreplyserver.cpp

package info (click to toggle)
musescore 2.3.2%2Bdfsg1-1~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports-sloppy
  • size: 204,200 kB
  • sloc: cpp: 267,218; xml: 177,839; ansic: 140,048; sh: 6,921; python: 6,867; perl: 423; makefile: 332; awk: 142; pascal: 74; sed: 3
file content (111 lines) | stat: -rw-r--r-- 3,532 bytes parent folder | download | duplicates (17)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/**
 * KQOAuth - An OAuth authentication library for Qt.
 *
 * Author: Johan Paul (johan.paul@gmail.com)
 *         http://www.johanpaul.com
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  KQOAuth is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with KQOAuth.  If not, see <http://www.gnu.org/licenses/>.
 */
#include <QTcpSocket>
#include <QStringList>
#include <QUrl>
#if QT_VERSION >= 0x050000
#include <QUrlQuery>
#endif

#include "kqoauthauthreplyserver.h"
#include "kqoauthauthreplyserver_p.h"

KQOAuthAuthReplyServerPrivate::KQOAuthAuthReplyServerPrivate(KQOAuthAuthReplyServer *parent):
    q_ptr(parent)
{

}

KQOAuthAuthReplyServerPrivate::~KQOAuthAuthReplyServerPrivate()
{

}

void KQOAuthAuthReplyServerPrivate::onIncomingConnection() {
    Q_Q(KQOAuthAuthReplyServer);

    socket = q->nextPendingConnection();
    connect(socket, SIGNAL(readyRead()),
            this, SLOT(onBytesReady()), Qt::UniqueConnection);
}

void KQOAuthAuthReplyServerPrivate::onBytesReady() {
    Q_Q(KQOAuthAuthReplyServer);
    
    QByteArray reply;
    QByteArray content;
    content.append("<HTML></HTML>");

    reply.append("HTTP/1.0 200 OK \r\n");
    reply.append("Content-Type: text/html; charset=\"utf-8\"\r\n");
    reply.append(QString("Content-Length: %1\r\n").arg(content.size()));
    reply.append("\r\n");
    reply.append(content);
    socket->write(reply);
    
    QByteArray data = socket->readAll();
    QMultiMap<QString, QString> queryParams = parseQueryParams(&data);

    socket->disconnectFromHost();
    q->close();
    emit q->verificationReceived(queryParams);
}

QMultiMap<QString, QString> KQOAuthAuthReplyServerPrivate::parseQueryParams(QByteArray *data) {
    QString splitGetLine = QString(*data).split("\r\n").first();   // Retrieve the first line with query params.
    splitGetLine.remove("GET ");                                   // Clean the line from GET
    splitGetLine.remove("HTTP/1.1");                               // From HTTP
    splitGetLine.remove("\r\n");                                   // And from rest.
    splitGetLine.prepend("http://localhost");                      // Now, make it a URL

    QUrl getTokenUrl(splitGetLine);
#if QT_VERSION < 0x050000
    QList< QPair<QString, QString> > tokens = getTokenUrl.queryItems();  // Ask QUrl to do our work.
#else
    QList< QPair<QString, QString> > tokens = QUrlQuery(getTokenUrl.query()).queryItems();  // Ask QUrl to do our work.
#endif

    QMultiMap<QString, QString> queryParams;
    QPair<QString, QString> tokenPair;
    foreach (tokenPair, tokens) {
        queryParams.insert(tokenPair.first.trimmed(), tokenPair.second.trimmed());
    }

    return queryParams;
}



KQOAuthAuthReplyServer::KQOAuthAuthReplyServer(QObject *parent) :
    QTcpServer(parent),
    d_ptr( new KQOAuthAuthReplyServerPrivate(this) )
{
    Q_D(KQOAuthAuthReplyServer);

    connect(this, SIGNAL(newConnection()),
            d, SLOT(onIncomingConnection()));
}

KQOAuthAuthReplyServer::~KQOAuthAuthReplyServer()
{
    delete d_ptr;
}