TTKMusicPlayer  4.2.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
qhttpserver.cpp
Go to the documentation of this file.
1 #include "qhttpserver.h"
2 
3 #include <QTcpServer>
4 #include <QTcpSocket>
5 #include <QVariant>
6 
7 #include "qhttpconnection.h"
8 
9 QHash<int, QString> STATUS_CODES;
10 
14 class QHttpServerPrivate : public TTKPrivate<QHttpServer>
15 {
16 public:
17  QHttpServerPrivate() noexcept;
18 
19  bool listen(const QHostAddress &address, quint16 port);
20  void create();
21  void close();
22  void free();
23 
24  QTcpServer *m_tcpServer;
25 
26 };
27 
29  : m_tcpServer(nullptr)
30 {
31 
32 }
33 
34 bool QHttpServerPrivate::listen(const QHostAddress &address, quint16 port)
35 {
37  m_tcpServer = new QTcpServer(q);
38 
39  const bool couldBindToPort = m_tcpServer->listen(address, port);
40  if(couldBindToPort)
41  {
42  QObject::connect(m_tcpServer, SIGNAL(newConnection()), q, SLOT(newConnection()));
43  }
44  else
45  {
46  free();
47  }
48  return couldBindToPort;
49 }
50 
52 {
54  while(m_tcpServer->hasPendingConnections())
55  {
56  QHttpConnection *connection = new QHttpConnection(m_tcpServer->nextPendingConnection(), q);
57  QObject::connect(connection, SIGNAL(newRequest(QHttpRequest*, QHttpResponse*)), q, SIGNAL(newRequest(QHttpRequest*, QHttpResponse*)));
58  }
59 }
60 
62 {
63  if(m_tcpServer)
64  {
65  m_tcpServer->close();
66  }
67 }
68 
70 {
71  delete m_tcpServer;
72  m_tcpServer = nullptr;
73 }
74 
75 
76 
77 QHttpServer::QHttpServer(QObject *parent)
78  : QObject(parent)
79 {
81 #define STATUS_CODE(num, reason) STATUS_CODES.insert(num, reason);
82  STATUS_CODE(100, "Continue")
83  STATUS_CODE(101, "Switching Protocols")
84  STATUS_CODE(102, "Processing") // RFC 2518) obsoleted by RFC 4918
85  STATUS_CODE(200, "OK")
86  STATUS_CODE(201, "Created")
87  STATUS_CODE(202, "Accepted")
88  STATUS_CODE(203, "Non-Authoritative Information")
89  STATUS_CODE(204, "No Content")
90  STATUS_CODE(205, "Reset Content")
91  STATUS_CODE(206, "Partial Content")
92  STATUS_CODE(207, "Multi-Status") // RFC 4918
93  STATUS_CODE(300, "Multiple Choices")
94  STATUS_CODE(301, "Moved Permanently")
95  STATUS_CODE(302, "Moved Temporarily")
96  STATUS_CODE(303, "See Other")
97  STATUS_CODE(304, "Not Modified")
98  STATUS_CODE(305, "Use Proxy")
99  STATUS_CODE(307, "Temporary Redirect")
100  STATUS_CODE(400, "Bad Request")
101  STATUS_CODE(401, "Unauthorized")
102  STATUS_CODE(402, "Payment Required")
103  STATUS_CODE(403, "Forbidden")
104  STATUS_CODE(404, "Not Found")
105  STATUS_CODE(405, "Method Not Allowed")
106  STATUS_CODE(406, "Not Acceptable")
107  STATUS_CODE(407, "Proxy Authentication Required")
108  STATUS_CODE(408, "Request Time-out")
109  STATUS_CODE(409, "Conflict")
110  STATUS_CODE(410, "Gone")
111  STATUS_CODE(411, "Length Required")
112  STATUS_CODE(412, "Precondition Failed")
113  STATUS_CODE(413, "Request Entity Too Large")
114  STATUS_CODE(414, "Request-URI Too Large")
115  STATUS_CODE(415, "Unsupported Media Type")
116  STATUS_CODE(416, "Requested Range Not Satisfiable")
117  STATUS_CODE(417, "Expectation Failed")
118  STATUS_CODE(418, "I\"m a teapot") // RFC 2324
119  STATUS_CODE(422, "Unprocessable Entity") // RFC 4918
120  STATUS_CODE(423, "Locked") // RFC 4918
121  STATUS_CODE(424, "Failed Dependency") // RFC 4918
122  STATUS_CODE(425, "Unordered Collection") // RFC 4918
123  STATUS_CODE(426, "Upgrade Required") // RFC 2817
124  STATUS_CODE(500, "Internal Server Error")
125  STATUS_CODE(501, "Not Implemented")
126  STATUS_CODE(502, "Bad Gateway")
127  STATUS_CODE(503, "Service Unavailable")
128  STATUS_CODE(504, "Gateway Time-out")
129  STATUS_CODE(505, "HTTP Version not supported")
130  STATUS_CODE(506, "Variant Also Negotiates") // RFC 2295
131  STATUS_CODE(507, "Insufficient Storage") // RFC 4918
132  STATUS_CODE(509, "Bandwidth Limit Exceeded")
133  STATUS_CODE(510, "Not Extended") // RFC 2774
134 }
135 
137 {
139  d->create();
140 }
141 
142 bool QHttpServer::listen(const QHostAddress &address, quint16 port)
143 {
145  return d->listen(address, port);
146 }
147 
148 bool QHttpServer::listen(quint16 port)
149 {
150  return listen(QHostAddress::Any, port);
151 }
152 
154 {
156  d->close();
157 }
The class of the http response.
Definition: qhttpresponse.h:29
The class of the http connection.
void close()
Stop the server and listening for new connections.
bool listen(const QHostAddress &address, quint16 port)
Definition: qhttpserver.cpp:34
QHttpServerPrivate() noexcept
Definition: qhttpserver.cpp:28
#define STATUS_CODE(num, reason)
The class of the http request.
Definition: qhttprequest.h:32
QHash< int, QString > STATUS_CODES
Maps status codes to string reason phrases.
Definition: qhttpserver.cpp:9
The class of the http server.
Definition: qhttpserver.h:37
The class of the http server private.
Definition: qhttpserver.cpp:14
bool listen(const QHostAddress &address=QHostAddress::Any, quint16 port=80)
Start the server by bounding to the given address and port.
QTcpServer * m_tcpServer
Definition: qhttpserver.cpp:24
QHttpServer(QObject *parent=nullptr)
Construct a new HTTP Server.
Definition: qhttpserver.cpp:77
void newConnection()
#define TTK_INIT_PRIVATE(Class)
Definition: ttkprivate.h:33
The class of the ttk private base.
Definition: ttkprivate.h:48
#define TTK_Q(Class)
Definition: ttkprivate.h:42
#define TTK_D(Class)
Definition: ttkprivate.h:41