| 1 | /* |
| 2 | * This file is part of libAuthentication |
| 3 | * |
| 4 | * Copyright (C) 2017-2020 Alberto Mardegan <[email protected]> |
| 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public License |
| 8 | * version 2.1 as published by the Free Software Foundation. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public |
| 16 | * License along with this library; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 18 | * 02110-1301 USA |
| 19 | */ |
| 20 | |
| 21 | #include "loopback-server.h" |
| 22 | |
| 23 | #include <QCoreApplication> |
| 24 | #include <QDebug> |
| 25 | #include <QTcpServer> |
| 26 | #include <QTcpSocket> |
| 27 | |
| 28 | using namespace OnlineAccountsPlugin; |
| 29 | |
| 30 | namespace OnlineAccountsPlugin { |
| 31 | |
| 32 | class LoopbackServerPrivate: public QTcpServer |
| 33 | { |
| 34 | Q_OBJECT |
| 35 | Q_DECLARE_PUBLIC(LoopbackServer) |
| 36 | |
| 37 | public: |
| 38 | LoopbackServerPrivate(LoopbackServer *q); |
| 39 | ~LoopbackServerPrivate(); |
| 40 | |
| 41 | bool listen(uint16_t port); |
| 42 | void respond(QTcpSocket *socket); |
| 43 | |
| 44 | private Q_SLOTS: |
| 45 | void onNewConnection(); |
| 46 | void onReadyRead(); |
| 47 | |
| 48 | private: |
| 49 | int m_portIncrementAttempts; |
| 50 | QByteArray m_data; |
| 51 | QByteArray m_servedHtml; |
| 52 | LoopbackServer *q_ptr; |
| 53 | }; |
| 54 | |
| 55 | } // namespace |
| 56 | |
| 57 | LoopbackServerPrivate::LoopbackServerPrivate(LoopbackServer *q): |
| 58 | m_portIncrementAttempts(0), |
| 59 | q_ptr(q) |
| 60 | { |
| 61 | QObject::connect(this, &QTcpServer::newConnection, |
| 62 | this, &LoopbackServerPrivate::onNewConnection); |
| 63 | |
| 64 | m_servedHtml = |
| 65 | QByteArrayLiteral("<html><head><title>") + |
| 66 | qApp->applicationName().toUtf8() + |
| 67 | QByteArrayLiteral("</title></head><body>") + |
| 68 | QObject::tr("Authentication completed; you can close this page " |
| 69 | "and go back to the application."). |
| 70 | toUtf8() + |
| 71 | QByteArrayLiteral("</body></html>"); |
| 72 | } |
| 73 | |
| 74 | LoopbackServerPrivate::~LoopbackServerPrivate() |
| 75 | { |
| 76 | } |
| 77 | |
| 78 | bool LoopbackServerPrivate::listen(uint16_t port) |
| 79 | { |
| 80 | uint16_t attempts = 0; |
| 81 | |
| 82 | do { |
| 83 | bool ok = QTcpServer::listen(QHostAddress::Any, |
| 84 | quint16(port + attempts)); |
| 85 | if (ok) return true; |
| 86 | } while (attempts++ < m_portIncrementAttempts); |
| 87 | |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | void LoopbackServerPrivate::respond(QTcpSocket *socket) |
| 92 | { |
| 93 | QByteArray htmlSize = QString::number(m_servedHtml.size()).toUtf8(); |
| 94 | QByteArray replyMessage = |
| 95 | QByteArrayLiteral("HTTP/1.0 200 OK \r\n" |
| 96 | "Content-Type: text/html; charset=\"utf-8\"\r\n" |
| 97 | "Content-Length: ") + htmlSize + |
| 98 | QByteArrayLiteral("\r\n\r\n") + |
| 99 | m_servedHtml; |
| 100 | |
| 101 | socket->write(replyMessage); |
| 102 | } |
| 103 | |
| 104 | void LoopbackServerPrivate::onNewConnection() |
| 105 | { |
| 106 | QTcpSocket *socket = nextPendingConnection(); |
| 107 | Q_ASSERT(socket); |
| 108 | QObject::connect(socket, &QTcpSocket::disconnected, |
| 109 | socket, &QTcpSocket::deleteLater); |
| 110 | QObject::connect(socket, &QTcpSocket::readyRead, |
| 111 | this, &LoopbackServerPrivate::onReadyRead); |
| 112 | } |
| 113 | |
| 114 | void LoopbackServerPrivate::onReadyRead() |
| 115 | { |
| 116 | Q_Q(LoopbackServer); |
| 117 | |
| 118 | QTcpSocket *socket = qobject_cast<QTcpSocket*>(sender()); |
| 119 | Q_ASSERT(socket); |
| 120 | |
| 121 | m_data += socket->readAll(); |
| 122 | int lineLength = m_data.indexOf('\r'); |
| 123 | if (lineLength < 0) { |
| 124 | /* Not all data has been received; will get more later */ |
| 125 | return; |
| 126 | } |
| 127 | QByteArray firstLine = m_data.left(lineLength); |
| 128 | QList<QByteArray> parts = firstLine.split(' '); |
| 129 | if (parts.count() >= 2) { |
| 130 | QString path = QString::fromUtf8(parts[1]); |
| 131 | QUrl url(QString::fromLatin1("http://localhost:%1%2"). |
| 132 | arg(serverPort()).arg(path)); |
| 133 | Q_EMIT q->visited(url); |
| 134 | } |
| 135 | |
| 136 | respond(socket); |
| 137 | socket->disconnectFromHost(); |
| 138 | } |
| 139 | |
| 140 | LoopbackServer::LoopbackServer(QObject *parent): |
| 141 | QObject(parent), |
| 142 | d_ptr(new LoopbackServerPrivate(this)) |
| 143 | { |
| 144 | } |
| 145 | |
| 146 | LoopbackServer::~LoopbackServer() |
| 147 | { |
| 148 | } |
| 149 | |
| 150 | bool LoopbackServer::listen(uint16_t port) |
| 151 | { |
| 152 | Q_D(LoopbackServer); |
| 153 | |
| 154 | bool ok = d->listen(port); |
| 155 | |
| 156 | Q_EMIT callbackUrlChanged(); |
| 157 | |
| 158 | return ok; |
| 159 | } |
| 160 | |
| 161 | void LoopbackServer::close() |
| 162 | { |
| 163 | Q_D(LoopbackServer); |
| 164 | d->close(); |
| 165 | |
| 166 | Q_EMIT callbackUrlChanged(); |
| 167 | } |
| 168 | |
| 169 | uint16_t LoopbackServer::port() const |
| 170 | { |
| 171 | Q_D(const LoopbackServer); |
| 172 | return d->serverPort(); |
| 173 | } |
| 174 | |
| 175 | void LoopbackServer::setPortIncrementAttempts(int maxAttempts) |
| 176 | { |
| 177 | Q_D(LoopbackServer); |
| 178 | if (d->m_portIncrementAttempts == maxAttempts) return; |
| 179 | d->m_portIncrementAttempts = maxAttempts; |
| 180 | Q_EMIT portIncrementAttemptsChanged(); |
| 181 | } |
| 182 | |
| 183 | int LoopbackServer::portIncrementAttempts() const |
| 184 | { |
| 185 | Q_D(const LoopbackServer); |
| 186 | return d->m_portIncrementAttempts; |
| 187 | } |
| 188 | |
| 189 | void LoopbackServer::setServedHtml(const QByteArray &html) |
| 190 | { |
| 191 | Q_D(LoopbackServer); |
| 192 | d->m_servedHtml = html; |
| 193 | Q_EMIT servedHtmlChanged(); |
| 194 | } |
| 195 | |
| 196 | QByteArray LoopbackServer::servedHtml() const |
| 197 | { |
| 198 | Q_D(const LoopbackServer); |
| 199 | return d->m_servedHtml; |
| 200 | } |
| 201 | |
| 202 | QUrl LoopbackServer::callbackUrl() const |
| 203 | { |
| 204 | Q_D(const LoopbackServer); |
| 205 | if (!d->isListening()) |
| 206 | return QUrl(); |
| 207 | return QUrl(QString::fromLatin1("http://localhost:%1/"). |
| 208 | arg(d->serverPort())); |
| 209 | } |
| 210 | |
| 211 | #include "loopback-server.moc" |