Debian Codegraph @

src/librssguard/network-web/httpserver.h

raw symbols SCIP
1 // For license of this file, see <project-root-folder>/LICENSE.md.
2
3 #ifndef HTTPSERVER_H
4 #define HTTPSERVER_H
5
6 #include <QHostAddress>
7 #include <QObject>
8 #include <QTcpServer>
9 #include <QTcpSocket>
10 #include <QUrl>
11
12 class HttpServer : public QObject {
13 Q_OBJECT
14
15 public:
16 explicit HttpServer(QObject* parent = nullptr);
17 virtual ~HttpServer();
18
19 bool isListening() const;
20
21 // Stops server and clear all connections.
22 void stop();
23
24 // Returns listening portnumber.
25 quint16 listenPort() const;
26
27 // Returns listening IP address, usually something like "127.0.0.1".
28 QHostAddress listenAddress() const;
29
30 // Returns full URL string.
31 QString listenAddressPort() const;
32
33 // Sets full URL string, for example "http://localhost:123456".
34 void setListenAddressPort(const QString& full_uri, bool start_handler);
35
36 protected:
37 struct HttpHeader {
38 QString m_name;
39 QString m_value;
40 };
41
42 QByteArray generateHttpAnswer(int http_code, const QList<HttpHeader>& headers, const QByteArray& body = {}) const;
43
44 struct HttpRequest {
45 bool readMethod(QTcpSocket* socket);
46 bool readUrl(QTcpSocket* socket);
47 bool readStatus(QTcpSocket* socket);
48 bool readHeader(QTcpSocket* socket);
49
50 enum class State {
51 ReadingMethod,
52 ReadingUrl,
53 ReadingStatus,
54 ReadingHeader,
55 ReadingBody,
56 AllDone
57 } m_state = State::ReadingMethod;
58
59 enum class Method {
60 Unknown,
61 Head,
62 Get,
63 Put,
64 Post,
65 Delete,
66 Options
67 } m_method = Method::Unknown;
68
69 QString m_address;
70 quint16 m_port = 0;
71 QByteArray m_fragment;
72 QUrl m_url;
73 QPair<quint8, quint8> m_version;
74 QMap<QByteArray, QByteArray> m_headers;
75 };
76
77 virtual void answerClient(QTcpSocket* socket, const HttpRequest& request) = 0;
78
79 private slots:
80 void clientConnected();
81
82 private:
83 void readReceivedData(QTcpSocket* socket);
84
85 private:
86 QMap<QTcpSocket*, HttpRequest> m_connectedClients;
87 QTcpServer m_httpServer;
88 QHostAddress m_listenAddress;
89 quint16 m_listenPort;
90 QString m_listenAddressPort;
91 };
92
93 #endif // HTTPSERVER_H