TTKMusicPlayer  3.7.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:
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 {
36  m_tcpServer = new QTcpServer(ttk_q());
37  bool couldBindToPort = m_tcpServer->listen(address, port);
38  if(couldBindToPort)
39  {
40  QObject::connect(m_tcpServer, SIGNAL(newConnection()), ttk_q(), SLOT(newConnection()));
41  }
42  else
43  {
44  free();
45  }
46  return couldBindToPort;
47 }
48 
50 {
51  while(m_tcpServer->hasPendingConnections())
52  {
53  QHttpConnection *connection = new QHttpConnection(m_tcpServer->nextPendingConnection(), ttk_q());
54  QObject::connect(connection, SIGNAL(newRequest(QHttpRequest *, QHttpResponse *)), ttk_q(),
55  SIGNAL(newRequest(QHttpRequest *, QHttpResponse *)));
56  }
57 }
58 
60 {
61  if(m_tcpServer)
62  {
63  m_tcpServer->close();
64  }
65 }
66 
68 {
69  delete m_tcpServer;
70  m_tcpServer = nullptr;
71 }
72 
73 
74 
75 QHttpServer::QHttpServer(QObject *parent)
76  : QObject(parent)
77 {
79 #define STATUS_CODE(num, reason) STATUS_CODES.insert(num, reason);
80  STATUS_CODE(100, "Continue")
81  STATUS_CODE(101, "Switching Protocols")
82  STATUS_CODE(102, "Processing") // RFC 2518) obsoleted by RFC 4918
83  STATUS_CODE(200, "OK")
84  STATUS_CODE(201, "Created")
85  STATUS_CODE(202, "Accepted")
86  STATUS_CODE(203, "Non-Authoritative Information")
87  STATUS_CODE(204, "No Content")
88  STATUS_CODE(205, "Reset Content")
89  STATUS_CODE(206, "Partial Content")
90  STATUS_CODE(207, "Multi-Status") // RFC 4918
91  STATUS_CODE(300, "Multiple Choices")
92  STATUS_CODE(301, "Moved Permanently")
93  STATUS_CODE(302, "Moved Temporarily")
94  STATUS_CODE(303, "See Other")
95  STATUS_CODE(304, "Not Modified")
96  STATUS_CODE(305, "Use Proxy")
97  STATUS_CODE(307, "Temporary Redirect")
98  STATUS_CODE(400, "Bad Request")
99  STATUS_CODE(401, "Unauthorized")
100  STATUS_CODE(402, "Payment Required")
101  STATUS_CODE(403, "Forbidden")
102  STATUS_CODE(404, "Not Found")
103  STATUS_CODE(405, "Method Not Allowed")
104  STATUS_CODE(406, "Not Acceptable")
105  STATUS_CODE(407, "Proxy Authentication Required")
106  STATUS_CODE(408, "Request Time-out")
107  STATUS_CODE(409, "Conflict")
108  STATUS_CODE(410, "Gone")
109  STATUS_CODE(411, "Length Required")
110  STATUS_CODE(412, "Precondition Failed")
111  STATUS_CODE(413, "Request Entity Too Large")
112  STATUS_CODE(414, "Request-URI Too Large")
113  STATUS_CODE(415, "Unsupported Media Type")
114  STATUS_CODE(416, "Requested Range Not Satisfiable")
115  STATUS_CODE(417, "Expectation Failed")
116  STATUS_CODE(418, "I\"m a teapot") // RFC 2324
117  STATUS_CODE(422, "Unprocessable Entity") // RFC 4918
118  STATUS_CODE(423, "Locked") // RFC 4918
119  STATUS_CODE(424, "Failed Dependency") // RFC 4918
120  STATUS_CODE(425, "Unordered Collection") // RFC 4918
121  STATUS_CODE(426, "Upgrade Required") // RFC 2817
122  STATUS_CODE(500, "Internal Server Error")
123  STATUS_CODE(501, "Not Implemented")
124  STATUS_CODE(502, "Bad Gateway")
125  STATUS_CODE(503, "Service Unavailable")
126  STATUS_CODE(504, "Gateway Time-out")
127  STATUS_CODE(505, "HTTP Version not supported")
128  STATUS_CODE(506, "Variant Also Negotiates") // RFC 2295
129  STATUS_CODE(507, "Insufficient Storage") // RFC 4918
130  STATUS_CODE(509, "Bandwidth Limit Exceeded")
131  STATUS_CODE(510, "Not Extended") // RFC 2774
132 }
133 
135 {
137  d->create();
138 }
139 
140 bool QHttpServer::listen(const QHostAddress &address, quint16 port)
141 {
143  return d->listen(address, port);
144 }
145 
146 bool QHttpServer::listen(quint16 port)
147 {
148  return listen(QHostAddress::Any, port);
149 }
150 
152 {
154  d->close();
155 }
QHttpServer * ttk_q() const
Definition: ttkprivate.h:77
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
#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:75
void newConnection()
#define TTK_INIT_PRIVATE(Class)
Definition: ttkprivate.h:33
The class of the ttk private base.
Definition: ttkprivate.h:48
#define TTK_D(Class)
Definition: ttkprivate.h:41