summaryrefslogtreecommitdiffstats
path: root/tests/auto/httpserver/proxy_server.cpp
blob: 3384153118d50dff9ed3c3e5f7847e6d1866326c (plain)
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
// Copyright (C) 2019 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "proxy_server.h"
#include <QDataStream>
#include <QTcpSocket>
#include <QDebug>

ProxyServer::ProxyServer(QObject *parent) : QObject(parent)
{
    connect(&m_server, &QTcpServer::newConnection, this, &ProxyServer::handleNewConnection);
}

void ProxyServer::setPort(int port)
{
    m_port = port;
}

void ProxyServer::setCredentials(const QByteArray &user, const QByteArray password)
{
    m_auth.append(user);
    m_auth.append(':');
    m_auth.append(password);
    m_auth = m_auth.toBase64();
    m_authenticate = true;
}

void ProxyServer::setCookie(const QByteArray &cookie)
{
    m_cookie.append(QByteArrayLiteral("Cookie: "));
    m_cookie.append(cookie);
}


bool ProxyServer::isListening()
{
    return m_server.isListening();
}

void ProxyServer::run()
{
    if (!m_server.listen(QHostAddress::LocalHost, m_port))
        qFatal("Could not start the test server");
}

void ProxyServer::handleNewConnection()
{
    // do one connection at the time
    Q_ASSERT(m_data.isEmpty());
    QTcpSocket *socket = m_server.nextPendingConnection();
    Q_ASSERT(socket);
    connect(socket, &QAbstractSocket::disconnected, socket, &QObject::deleteLater);
    connect(socket, &QAbstractSocket::readyRead, this, &ProxyServer::handleReadReady);
}

void ProxyServer::handleReadReady()
{
    QTcpSocket *socket = qobject_cast<QTcpSocket*>(sender());
    Q_ASSERT(socket);

    m_data.append(socket->readAll());

    if (!m_data.endsWith("\r\n\r\n"))
        return;

    if (m_authenticate && !m_data.contains(QByteArrayLiteral("Proxy-Authorization: Basic"))) {
        socket->write("HTTP/1.1 407 Proxy Authentication Required\nProxy-Authenticate: "
                      "Basic realm=\"Proxy requires authentication\"\r\n"
                      "content-length: 0\r\n"
                      "\r\n");
        return;
    }

    if (m_authenticate && m_data.contains(m_auth)) {
        emit authenticationSuccess();
    }

    if (m_data.contains(m_cookie)) {
        emit cookieMatch();
    }
    m_data.clear();
    emit requestReceived();
}

#include "moc_proxy_server.cpp"