httplistener.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**
  2. @file
  3. @author Stefan Frings
  4. */
  5. #ifndef HTTPLISTENER_H
  6. #define HTTPLISTENER_H
  7. #include <QTcpServer>
  8. #include <QSettings>
  9. #include <QBasicTimer>
  10. #include "httpglobal.h"
  11. #include "httpconnectionhandler.h"
  12. #include "httpconnectionhandlerpool.h"
  13. #include "httprequesthandler.h"
  14. namespace stefanfrings {
  15. /**
  16. Listens for incoming TCP connections and and passes all incoming HTTP requests to your implementation of HttpRequestHandler,
  17. which processes the request and generates the response (usually a HTML document).
  18. <p>
  19. Example for the required settings in the config file:
  20. <code><pre>
  21. ;host=192.168.0.100
  22. port=8080
  23. readTimeout=60000
  24. maxRequestSize=16000
  25. maxMultiPartSize=1000000
  26. minThreads=1
  27. maxThreads=10
  28. cleanupInterval=1000
  29. ;sslKeyFile=ssl/server.key
  30. ;sslCertFile=ssl/server.crt
  31. ;caCertFile=ssl/ca.crt
  32. ;verifyPeer=false
  33. </pre></code>
  34. The optional host parameter binds the listener to a specific network interface,
  35. otherwise the server accepts connections from any network interface on the given port.
  36. <p>
  37. The readTimeout value defines the maximum time to wait for a complete HTTP request.
  38. <p>
  39. MaxRequestSize is the maximum size of a HTTP request. In case of
  40. multipart/form-data requests (also known as file-upload), the maximum
  41. size of the body must not exceed maxMultiPartSize.
  42. <p>
  43. After server start, the size of the thread pool is always 0. Threads
  44. are started on demand when requests come in. The cleanup timer reduces
  45. the number of idle threads slowly by closing one thread in each interval.
  46. But the configured minimum number of threads are kept running.
  47. @see HttpConnectionHandlerPool for description of the optional ssl settings
  48. */
  49. class DECLSPEC HttpListener : public QTcpServer {
  50. Q_OBJECT
  51. Q_DISABLE_COPY(HttpListener)
  52. public:
  53. /**
  54. Constructor.
  55. Creates a connection pool and starts listening on the configured host and port.
  56. @param settings Configuration settings, usually stored in an INI file. Must not be 0.
  57. Settings are read from the current group, so the caller must have called settings->beginGroup().
  58. Because the group must not change during runtime, it is recommended to provide a
  59. separate QSettings instance that is not used by other parts of the program.
  60. The HttpListener does not take over ownership of the QSettings instance, so the
  61. caller should destroy it during shutdown.
  62. @param requestHandler Processes each received HTTP request, usually by dispatching to controller classes.
  63. @param parent Parent object.
  64. @warning Ensure to close or delete the listener before deleting the request handler.
  65. */
  66. HttpListener(const QSettings* settings, HttpRequestHandler* requestHandler, QObject* parent=nullptr);
  67. /** Destructor */
  68. virtual ~HttpListener();
  69. /**
  70. Restart listeing after close().
  71. */
  72. void listen();
  73. /**
  74. Closes the listener, waits until all pending requests are processed,
  75. then closes the connection pool.
  76. */
  77. void close();
  78. protected:
  79. /** Serves new incoming connection requests */
  80. void incomingConnection(tSocketDescriptor socketDescriptor);
  81. private:
  82. /** Configuration settings for the HTTP server */
  83. const QSettings* settings;
  84. /** Point to the reuqest handler which processes all HTTP requests */
  85. HttpRequestHandler* requestHandler;
  86. /** Pool of connection handlers */
  87. HttpConnectionHandlerPool* pool;
  88. signals:
  89. /**
  90. Sent to the connection handler to process a new incoming connection.
  91. @param socketDescriptor references the accepted connection.
  92. */
  93. void handleConnection(tSocketDescriptor socketDescriptor);
  94. };
  95. } // end of namespace
  96. #endif // HTTPLISTENER_H