18 HttpConnection::~HttpConnection() {
20 for (
int i=0; i<m_requests.size(); i++) {
27 m_peerAddress =
socket->peerAddress();
37 m_timeout =
new QTimer(
this);
38 m_timeout->setInterval(timeout);
39 m_timeout->setSingleShot(
true);
41 connect (m_timeout, SIGNAL(timeout()),
42 this, SLOT(slotTimeout()));
44 connect(m_socket, SIGNAL(readyRead()),
45 this, SLOT(slotRead()));
47 connect(m_socket, SIGNAL(disconnected()),
48 this, SLOT(slotDisconnected()));
52 return m_parent->settings();
56 void HttpConnection::startTimeout() {
57 if (m_timeout->interval() == 0) {
66 m_peerCertificate = crt;
71 #if QT_VERSION > 0x050000 72 QDateTime now = QDateTime::currentDateTime();
73 if (now > m_peerCertificate.expiryDate()) {
return QString(); }
74 if (now < m_peerCertificate.effectiveDate()) {
return QString(); }
75 if (m_peerCertificate.isBlacklisted()) {
return QString(); }
76 QStringList cn = m_peerCertificate.subjectInfo(QSslCertificate::CommonName);
77 if (cn.isEmpty()) {
return QString(); }
80 if (!m_peerCertificate.isValid()) {
return QString(); }
81 return m_peerCertificate.subjectInfo(QSslCertificate::CommonName);
91 m_socket->disconnectFromHost();
98 void HttpConnection::slotTimeout() {
100 m_socket->write(
"HTTP/1.1 408 request timeout\r\n");
101 m_socket->write(
"Connection: close\r\n");
102 m_socket->write(
"\r\n");
103 m_socket->write(
"408 request timeout\r\n");
104 m_socket->disconnectFromHost();
109 void HttpConnection::slotDisconnected() {
115 QTimer::singleShot(3000,
this, SLOT(deleteLater()));
128 void HttpConnection::slotRead() {
133 m_requests << m_request;
136 while (m_socket->bytesAvailable()
139 m_request->readFromSocket(m_socket);
146 m_socket->write(
"HTTP/1.1 413 entity too large\r\n");
147 m_socket->write(
"Connection: close\r\n\r\n");
148 m_socket->write(
"\r\n");
149 m_socket->write(
"413 entity too large\r\n");
150 m_socket->disconnectFromHost();
157 bool disconnect = (m_request->header(
"Connection").toLower() ==
"close");
161 m_handler->service(m_request,
response);
171 m_socket->disconnectFromHost();
183 void HttpConnection::deleteRequest() {
188 QVariant HttpConnection::webStatus()
const {
190 for (
int i=0; i<m_requests.size(); i++) {
193 data[
"object"] = QString(
"0x%1").arg((quint64)
this, 8, 16, QChar(
'0'));
194 data[
"path"] = (request!=NULL) ? request->
path() : QVariant();
195 data[
"time"] = (request!=NULL) ? request->
datetime() : QVariant();
196 data[
"method"] = (request!=NULL) ? request->
method() : QVariant();
197 data[
"status"] = (request!=NULL) ? request->
statusString() : QVariant();
198 data[
"connection"] = (
isConnected()) ?
"connected" :
"disconnected";
const HttpSettings * settings() const
Returns pointer to settings used in the class.
QString statusString() const
Returns current status of the request as a string.
QString path() const
Returns path of the request (/files/index.html)
QTcpSocket * socket() const
Returns the socket.
General single-threaded, event-driven HTTP server.
int timeout() const
Returns timeout for opened connection from clients.
Response to HTTP request - headers, cookies and body.
Configuration of the http server instance.
HttpConnection(HttpServer *parent, QTcpSocket *socket)
Constructor is called automatically when new request arrived.
virtual HttpRequestHandler * requestHandler(HttpConnection *)
Creates new request handler and returs pointer to it.
bool isConnected() const
Returns true if the connection is in connected state.
HttpResponse * response()
Returns new instance of class HttpResponse.
void close()
Closes the connection.
const QDateTime & datetime() const
Returns date and time of the request.
Processes HTTP request, parses headers, body and files sent by HTTP protocol.
void setPeerCertificate(const QSslCertificate &)
Sets peer's certificate.
QString method() const
Returns used HTTP method of the request (GET, PUT, POST or DELETE)
Namespace of HTTP server.
QString commonName() const
Returns CommonName field from certificate.