1 #include "private/qhttpserver_private.hpp"
2 
3 ///////////////////////////////////////////////////////////////////////////////
4 namespace qhttp {
5 namespace server {
6 ///////////////////////////////////////////////////////////////////////////////
7 
QHttpServer(QObject * parent)8 QHttpServer::QHttpServer(QObject *parent)
9     : QObject(parent), d_ptr(new QHttpServerPrivate) {
10 }
11 
QHttpServer(QHttpServerPrivate & dd,QObject * parent)12 QHttpServer::QHttpServer(QHttpServerPrivate &dd, QObject *parent)
13     : QObject(parent), d_ptr(&dd) {
14 }
15 
~QHttpServer()16 QHttpServer::~QHttpServer() {
17     stopListening();
18 }
19 
20 bool
listen(const QString & socketOrPort,const TServerHandler & handler)21 QHttpServer::listen(const QString &socketOrPort, const TServerHandler &handler) {
22     Q_D(QHttpServer);
23 
24     bool isNumber   = false;
25     quint16 tcpPort = socketOrPort.toUShort(&isNumber);
26     if ( isNumber    &&    tcpPort > 0 )
27         return listen(QHostAddress::Any, tcpPort, handler);
28 
29     d->initialize(ELocalSocket, this);
30     d->ihandler = handler;
31     return d->ilocalServer->listen(socketOrPort);
32 }
33 
34 bool
listen(const QHostAddress & address,quint16 port,const qhttp::server::TServerHandler & handler)35 QHttpServer::listen(const QHostAddress& address, quint16 port, const qhttp::server::TServerHandler& handler) {
36     Q_D(QHttpServer);
37 
38     d->initialize(ETcpSocket, this);
39     d->ihandler = handler;
40     return d->itcpServer->listen(address, port);
41 }
42 
43 bool
isListening() const44 QHttpServer::isListening() const {
45     const Q_D(QHttpServer);
46 
47     if ( d->ibackend == ETcpSocket    &&    d->itcpServer )
48         return d->itcpServer->isListening();
49 
50     else if ( d->ibackend == ELocalSocket    &&    d->ilocalServer )
51         return d->ilocalServer->isListening();
52 
53     return false;
54 }
55 
56 void
stopListening()57 QHttpServer::stopListening() {
58     Q_D(QHttpServer);
59 
60     if ( d->itcpServer )
61         d->itcpServer->close();
62 
63     if ( d->ilocalServer ) {
64         d->ilocalServer->close();
65         QLocalServer::removeServer( d->ilocalServer->fullServerName() );
66     }
67 }
68 
69 quint32
timeOut() const70 QHttpServer::timeOut() const {
71     return d_func()->itimeOut;
72 }
73 
74 void
setTimeOut(quint32 newValue)75 QHttpServer::setTimeOut(quint32 newValue) {
76     d_func()->itimeOut = newValue;
77 }
78 
79 TBackend
backendType() const80 QHttpServer::backendType() const {
81     return d_func()->ibackend;
82 }
83 
84 QTcpServer*
tcpServer() const85 QHttpServer::tcpServer() const {
86     return d_func()->itcpServer.data();
87 }
88 
89 QLocalServer*
localServer() const90 QHttpServer::localServer() const {
91     return d_func()->ilocalServer.data();
92 }
93 
94 void
incomingConnection(qintptr handle)95 QHttpServer::incomingConnection(qintptr handle) {
96     QHttpConnection* conn = new QHttpConnection(this);
97     conn->setSocketDescriptor(handle, backendType());
98     conn->setTimeOut(d_func()->itimeOut);
99 
100     emit newConnection(conn);
101 
102     Q_D(QHttpServer);
103     if ( d->ihandler )
104         QObject::connect(conn, &QHttpConnection::newRequest, d->ihandler);
105     else
106         incomingConnection(conn);
107 }
108 
109 void
incomingConnection(QHttpConnection * connection)110 QHttpServer::incomingConnection(QHttpConnection *connection) {
111     QObject::connect(connection,  &QHttpConnection::newRequest,
112                      this,        &QHttpServer::newRequest);
113 }
114 
115 ///////////////////////////////////////////////////////////////////////////////
116 } // namespace server
117 } // namespace qhttp
118 ///////////////////////////////////////////////////////////////////////////////
119