OSDN Git Service

Initial commit
[wordring-tm/wordring-tm.git] / http / httpserver.h
1 #ifndef HTTPSERVER_H
2 #define HTTPSERVER_H
3
4 #include "httprequest.h"
5
6 #include <QTcpServer>
7 #include <QList>
8 #include <QMap>
9 #include <QSharedPointer>
10
11 class HttpServer;
12 class HttpHandler;
13 class HttpModule;
14
15 class HttpConnection : public QObject
16 {
17         Q_OBJECT
18
19         enum : int { LoopLimit = 100, };
20
21 public:
22         HttpConnection(HttpServer *server, QTcpSocket *socket);
23         ~HttpConnection();\
24
25 signals:
26         /*! ハンドラのattach()と接続されます。 */
27         void attach(QSharedPointer<HttpRequest> request);
28
29 private:
30         void setup_connections(QTcpSocket *socket);
31         void delete_connections(QTcpSocket *socket);
32         void setup_connections(HttpHandler *handler);
33         void delete_connections(HttpHandler *handler);
34
35         QTcpSocket* socket();
36
37 private slots:
38         // QTcpSocket -------------------------------------------------------------
39         void onReadyRead();
40
41         // Handler ----------------------------------------------------------------
42         void onWriteSocket(QByteArray bytes);
43         void onFlushSocket();
44         void onCloseSocket();
45 private:
46         void begin_response();
47 private slots:
48         void end_response();
49
50 private:
51         HttpServer *m_server;
52         HttpRequestParser m_parser;
53
54         QList<QSharedPointer<HttpRequest> > m_requests;
55         QSharedPointer<HttpRequest> m_request;
56
57         HttpHandler *m_handler;
58         QString m_debug;
59 };
60
61 class HttpServer : public QObject
62 {
63         Q_OBJECT
64         friend class HttpConnection;
65
66 public:
67         HttpServer(QObject *parent);
68
69         bool listen(QList<quint16> ports, QHostAddress const &address = QHostAddress::LocalHost);
70         bool listen(quint16 port, QHostAddress const &address = QHostAddress::LocalHost);
71         quint16 port() const;
72
73         void install(HttpModule *module);
74
75 signals:
76
77 public slots:
78         void onNewConnection();
79
80 private:
81         HttpHandler* create_handler(QByteArray method, QByteArray url);
82         void destroy_handler(HttpHandler *handler);
83
84 private:
85         quint16 m_port;
86         QTcpServer *m_server;
87
88         QMap<HttpHandler*, HttpModule*> m_handlers;
89         QList<HttpModule*> m_modules;
90
91         HttpHandler *m_handler;
92 };
93
94
95 #endif // HTTPSERVER_H