httpconnectionhandler.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. @file
  3. @author Stefan Frings
  4. */
  5. #ifndef HTTPCONNECTIONHANDLER_H
  6. #define HTTPCONNECTIONHANDLER_H
  7. #ifndef QT_NO_SSL
  8. #include <QSslConfiguration>
  9. #endif
  10. #include <QTcpSocket>
  11. #include <QSettings>
  12. #include <QTimer>
  13. #include <QThread>
  14. #include "httpglobal.h"
  15. #include "httprequest.h"
  16. #include "httprequesthandler.h"
  17. namespace stefanfrings {
  18. /** Alias type definition, for compatibility to different Qt versions */
  19. #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
  20. typedef qintptr tSocketDescriptor;
  21. #else
  22. typedef int tSocketDescriptor;
  23. #endif
  24. /** Alias for QSslConfiguration if OpenSSL is not supported */
  25. #ifdef QT_NO_SSL
  26. #define QSslConfiguration QObject
  27. #endif
  28. /**
  29. The connection handler accepts incoming connections and dispatches incoming requests to to a
  30. request mapper. Since HTTP clients can send multiple requests before waiting for the response,
  31. the incoming requests are queued and processed one after the other.
  32. <p>
  33. Example for the required configuration settings:
  34. <code><pre>
  35. readTimeout=60000
  36. maxRequestSize=16000
  37. maxMultiPartSize=1000000
  38. </pre></code>
  39. <p>
  40. The readTimeout value defines the maximum time to wait for a complete HTTP request.
  41. <p>
  42. MaxRequestSize is the maximum size of a HTTP request. In case of
  43. multipart/form-data requests (also known as file-upload), the maximum
  44. size of the body must not exceed maxMultiPartSize.
  45. */
  46. class DECLSPEC HttpConnectionHandler : public QObject {
  47. Q_OBJECT
  48. Q_DISABLE_COPY(HttpConnectionHandler)
  49. public:
  50. /**
  51. Constructor.
  52. @param settings Configuration settings of the HTTP webserver
  53. @param requestHandler Handler that will process each incoming HTTP request
  54. @param sslConfiguration SSL (HTTPS) will be used if not NULL
  55. */
  56. HttpConnectionHandler(const QSettings* settings, HttpRequestHandler* requestHandler,
  57. const QSslConfiguration* sslConfiguration=nullptr);
  58. /** Destructor */
  59. virtual ~HttpConnectionHandler();
  60. /** Returns true, if this handler is in use. */
  61. bool isBusy();
  62. /** Mark this handler as busy */
  63. void setBusy();
  64. private:
  65. /** Configuration settings */
  66. const QSettings* settings;
  67. /** TCP socket of the current connection */
  68. QTcpSocket* socket;
  69. /** The thread that processes events of this connection */
  70. QThread* thread;
  71. /** Time for read timeout detection */
  72. QTimer readTimer;
  73. /** Storage for the current incoming HTTP request */
  74. HttpRequest* currentRequest;
  75. /** Dispatches received requests to services */
  76. HttpRequestHandler* requestHandler;
  77. /** This shows the busy-state from a very early time */
  78. bool busy;
  79. /** Configuration for SSL */
  80. const QSslConfiguration* sslConfiguration;
  81. /** Create SSL or TCP socket */
  82. void createSocket();
  83. public slots:
  84. /**
  85. Received from from the listener, when the handler shall start processing a new connection.
  86. @param socketDescriptor references the accepted connection.
  87. */
  88. void handleConnection(const tSocketDescriptor socketDescriptor);
  89. private slots:
  90. /** Received from the socket when a read-timeout occured */
  91. void readTimeout();
  92. /** Received from the socket when incoming data can be read */
  93. void read();
  94. /** Received from the socket when a connection has been closed */
  95. void disconnected();
  96. /** Cleanup after the thread is closed */
  97. void thread_done();
  98. };
  99. } // end of namespace
  100. #endif // HTTPCONNECTIONHANDLER_H