httpconnectionhandlerpool.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #ifndef HTTPCONNECTIONHANDLERPOOL_H
  2. #define HTTPCONNECTIONHANDLERPOOL_H
  3. #include <QList>
  4. #include <QTimer>
  5. #include <QObject>
  6. #include <QMutex>
  7. #include "httpglobal.h"
  8. #include "httpconnectionhandler.h"
  9. namespace stefanfrings {
  10. /**
  11. Pool of http connection handlers. The size of the pool grows and
  12. shrinks on demand.
  13. <p>
  14. Example for the required configuration settings:
  15. <code><pre>
  16. readTimeout=60000
  17. maxRequestSize=16000
  18. maxMultiPartSize=1000000
  19. minThreads=4
  20. maxThreads=100
  21. cleanupInterval=60000
  22. </pre></code>
  23. <p>
  24. The readTimeout value defines the maximum time to wait for a complete HTTP request.
  25. <p>
  26. MaxRequestSize is the maximum size of a HTTP request. In case of
  27. multipart/form-data requests (also known as file-upload), the maximum
  28. size of the body must not exceed maxMultiPartSize.
  29. <p>
  30. After server start, the size of the thread pool is always 0. Threads
  31. are started on demand when requests come in. The cleanup timer reduces
  32. the number of idle threads slowly by closing one thread in each interval.
  33. But the configured minimum number of threads are kept running.
  34. <p>
  35. Additional settings for SSL (HTTPS):
  36. <code><pre>
  37. sslKeyFile=ssl/server.key
  38. sslCertFile=ssl/server.crt
  39. ;caCertFile=ssl/ca.crt
  40. verifyPeer=false
  41. </pre></code>
  42. For SSL support, you need at least a pair of OpenSSL x509 certificate and an RSA key,
  43. both files in PEM format. To enable verification of the peer (the calling web browser),
  44. you can either use the central certificate store of the operating system, or provide
  45. a CA certificate file in PEM format. The certificates of the peers must have been
  46. derived from the CA certificate.
  47. <p>
  48. Example commands to create these files:
  49. <code><pre>
  50. # Generate CA key
  51. openssl genrsa 2048 > ca.key
  52. # Generate CA certificate
  53. openssl req -new -x509 -nodes -days 365000 -key ca.key -out ca.crt
  54. # Generate a server key and certificate request
  55. openssl req -newkey rsa:2048 -nodes -days 365000 -keyout server.key -out server.req
  56. # Generate a signed server certificate
  57. openssl x509 -req -days 365000 -set_serial 01 -in server.req -out server.crt -CA ca.crt -CAkey ca.key
  58. # Generate a client key and certificate request
  59. openssl req -newkey rsa:2048 -nodes -days 365000 -keyout client.key -out client.req
  60. # Generate a signed client certificate
  61. openssl x509 -req -days 365000 -set_serial 01 -in client.req -out client.crt -CA ca.crt -CAkey ca.key
  62. # Combine client key and certificate into one PKCS12 file
  63. openssl pkcs12 -export -in client.crt -inkey client.key -out client.p12 -certfile ca.crt
  64. # Remove temporary files
  65. rm *.req
  66. </pre></code>
  67. <p>
  68. Please note that a listener with SSL can only handle HTTPS protocol. To support both
  69. HTTP and HTTPS simultaneously, you need to start <b>two</b> listeners on different ports
  70. one with SLL and one without SSL (usually on public ports 80 and 443, or locally on 8080 and 8443).
  71. */
  72. class DECLSPEC HttpConnectionHandlerPool : public QObject {
  73. Q_OBJECT
  74. Q_DISABLE_COPY(HttpConnectionHandlerPool)
  75. public:
  76. /**
  77. Constructor.
  78. @param settings Configuration settings for the HTTP server. Must not be 0.
  79. @param requestHandler The handler that will process each received HTTP request.
  80. */
  81. HttpConnectionHandlerPool(const QSettings* settings, HttpRequestHandler *requestHandler);
  82. /** Destructor */
  83. virtual ~HttpConnectionHandlerPool();
  84. /** Get a free connection handler, or 0 if not available. */
  85. HttpConnectionHandler* getConnectionHandler();
  86. private:
  87. /** Settings for this pool */
  88. const QSettings* settings;
  89. /** Will be assigned to each Connectionhandler during their creation */
  90. HttpRequestHandler* requestHandler;
  91. /** Pool of connection handlers */
  92. QList<HttpConnectionHandler*> pool;
  93. /** Timer to clean-up unused connection handler */
  94. QTimer cleanupTimer;
  95. /** Used to synchronize threads */
  96. QMutex mutex;
  97. /** The SSL configuration (certificate, key and other settings) */
  98. QSslConfiguration* sslConfiguration;
  99. /** Load SSL configuration */
  100. void loadSslConfig();
  101. private slots:
  102. /** Received from the clean-up timer. */
  103. void cleanup();
  104. };
  105. } // end of namespace
  106. #endif // HTTPCONNECTIONHANDLERPOOL_H