2022-05-08 13:21:30 +03:00
|
|
|
/* This file is part of the KDE libraries
|
|
|
|
Copyright (C) 2022 Ivailo Monev <xakepa10@gmail.com>
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Library General Public
|
|
|
|
License version 2, as published by the Free Software Foundation.
|
|
|
|
|
|
|
|
This library 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
|
|
|
|
Library General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Library General Public License
|
|
|
|
along with this library; see the file COPYING.LIB. If not, write to
|
|
|
|
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef KHTTP_H
|
|
|
|
#define KHTTP_H
|
|
|
|
|
|
|
|
#include "khttp_export.h"
|
|
|
|
|
|
|
|
#include <QObject>
|
|
|
|
#include <QHostAddress>
|
|
|
|
|
|
|
|
typedef QMap<QByteArray,QByteArray> KHTTPHeaders;
|
|
|
|
|
|
|
|
class KHTTPPrivate;
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Class to serve data over HTTP(S).
|
|
|
|
|
|
|
|
@since 4.21
|
|
|
|
@warning the API is subject to change
|
|
|
|
*/
|
|
|
|
class KHTTP_EXPORT KHTTP : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
/*!
|
|
|
|
@brief Contructs object with @p parent
|
|
|
|
*/
|
|
|
|
KHTTP(QObject *parent = nullptr);
|
|
|
|
~KHTTP();
|
|
|
|
|
|
|
|
/*!
|
|
|
|
@brief Sets @p keydata and @p certdata to be used for TLS/SSL handshake, if the key
|
|
|
|
requires password it must also be provided as @p password.
|
|
|
|
@note HTTP requests to the server address will not be redirected, clients must request
|
|
|
|
HTTPS address. For example if TLS/SSL certificate is set "http://foo.bar" will not be
|
2022-05-09 13:53:24 +03:00
|
|
|
accessible (no data is send) however "https://foo.bar" will be, unless external means are
|
2022-05-08 13:21:30 +03:00
|
|
|
used to redirect the request. This is the case only when non-standard ports are used, if
|
|
|
|
HTTP server runs on port 80 and HTTPS server runs on port 443 then both are accessible but
|
2022-05-09 13:53:24 +03:00
|
|
|
clients will most likely be making requests to the HTTP server on port 80.
|
2022-05-08 13:21:30 +03:00
|
|
|
*/
|
|
|
|
bool setCertificate(const QByteArray &keydata, const QByteArray &certdata, const QByteArray &password = QByteArray());
|
|
|
|
/*!
|
|
|
|
@brief Sets @p username and @p password to be used for authentication with @p message as
|
2022-05-09 13:53:24 +03:00
|
|
|
content to be send to clients when authentication fails.
|
2022-05-10 14:51:42 +03:00
|
|
|
@note The authentication method used is basic.
|
2022-05-08 13:21:30 +03:00
|
|
|
*/
|
|
|
|
bool setAuthenticate(const QByteArray &username, const QByteArray &password, const QString &message);
|
|
|
|
|
2022-05-10 14:51:42 +03:00
|
|
|
/*!
|
|
|
|
@brief Starts serving data for requests at @p address on @p port.
|
|
|
|
@note If port is 0 (the default) then a random port is chosen.
|
|
|
|
*/
|
2022-05-08 13:21:30 +03:00
|
|
|
bool start(const QHostAddress &address = QHostAddress::Any, quint16 port = 0);
|
|
|
|
bool stop();
|
2022-05-10 00:40:14 +03:00
|
|
|
|
|
|
|
/*!
|
|
|
|
@brief Returns human-readable description of the error that occured, if @p start() returns
|
|
|
|
@p false for example it may be used along with @p KMessageBox to notify the user about the
|
|
|
|
error.
|
|
|
|
*/
|
2022-05-08 13:21:30 +03:00
|
|
|
QString errorString() const;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
/*!
|
|
|
|
@brief Reimplement this method to send back data to clients when @p url is requested.
|
2022-05-09 01:40:07 +03:00
|
|
|
@p outdata is the content, @p outhttpstatus is a standard HTTP status (e.g. 404) and
|
2022-05-10 14:51:42 +03:00
|
|
|
@p outheaders is map of additional headers to be send (e.g. "Content-Type"). Either
|
|
|
|
@p outdata must be non-empty or @p outfilepath must be pointing to a file, the other
|
|
|
|
arguments (@p outhttpstatus and @p outheaders) are optional.
|
|
|
|
@note Prefer @p outfilepath over @p outdata for serving files, Large-file support is
|
2022-05-10 15:31:01 +03:00
|
|
|
transparent.
|
|
|
|
@link https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
|
2022-05-10 14:51:42 +03:00
|
|
|
@link https://en.wikipedia.org/wiki/Large-file_support
|
2022-05-08 13:21:30 +03:00
|
|
|
*/
|
2022-05-10 14:51:42 +03:00
|
|
|
virtual void respond(
|
|
|
|
const QByteArray &url,
|
|
|
|
QByteArray *outdata, ushort *outhttpstatus, KHTTPHeaders *outheaders, QString *outfilepath
|
|
|
|
) = 0;
|
2022-05-08 13:21:30 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
friend KHTTPPrivate;
|
|
|
|
Q_DISABLE_COPY(KHTTP);
|
|
|
|
KHTTPPrivate *d;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // KHTTP_H
|