qhttpserverconnection.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /** HTTP connection class.
  2. * https://github.com/azadkuh/qhttp
  3. *
  4. * @author amir zamani
  5. * @version 2.0.0
  6. * @date 2014-07-11
  7. */
  8. #ifndef QHTTPSERVER_CONNECTION_HPP
  9. #define QHTTPSERVER_CONNECTION_HPP
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #include "qhttpfwd.hpp"
  12. #include <QObject>
  13. ///////////////////////////////////////////////////////////////////////////////
  14. namespace qhttp {
  15. namespace server {
  16. ///////////////////////////////////////////////////////////////////////////////
  17. /** a HTTP connection in the server.
  18. * this class controls the HTTP connetion and handles life cycle and the memory management
  19. * of QHttpRequest and QHttpResponse instances autoamtically.
  20. */
  21. class QHTTP_API QHttpConnection : public QObject
  22. {
  23. Q_OBJECT
  24. public:
  25. virtual ~QHttpConnection();
  26. /** set an optional timer event to close the connection. */
  27. void setTimeOut(quint32 miliSeconds);
  28. /** forcefully kills (closes) a connection. */
  29. void killConnection();
  30. /** optionally set a handler for connection class.
  31. * @note if you set this handler, the newRequest() signal won't be emitted.
  32. */
  33. void onHandler(const TServerHandler& handler);
  34. /** returns the backend type of the connection. */
  35. TBackend backendType() const;
  36. /** returns connected socket if the backend() == ETcpSocket. */
  37. QTcpSocket* tcpSocket() const;
  38. /** returns connected socket if the backend() == ELocalSocket. */
  39. QLocalSocket* localSocket() const;
  40. /** creates a new QHttpConnection based on arguments. */
  41. static
  42. QHttpConnection* create(qintptr sokDescriptor, TBackend backendType, QObject* parent) {
  43. QHttpConnection* conn = new QHttpConnection(parent);
  44. conn->setSocketDescriptor(sokDescriptor, backendType);
  45. return conn;
  46. }
  47. signals:
  48. /** emitted when a pair of HTTP request and response are ready to interact.
  49. * @param req incoming request by the client.
  50. * @param res outgoing response to the client.
  51. */
  52. void newRequest(QHttpRequest* req, QHttpResponse* res);
  53. /** emitted when the tcp/local socket, disconnects. */
  54. void disconnected();
  55. protected:
  56. explicit QHttpConnection(QObject *parent);
  57. explicit QHttpConnection(QHttpConnectionPrivate&, QObject *);
  58. void setSocketDescriptor(qintptr sokDescriptor, TBackend backendType);
  59. void timerEvent(QTimerEvent*) override;
  60. Q_DISABLE_COPY(QHttpConnection)
  61. Q_DECLARE_PRIVATE(QHttpConnection)
  62. QScopedPointer<QHttpConnectionPrivate> d_ptr;
  63. friend class QHttpServer;
  64. };
  65. ///////////////////////////////////////////////////////////////////////////////
  66. } // namespace server
  67. } // namespace qhttp
  68. ///////////////////////////////////////////////////////////////////////////////
  69. #endif // #define QHTTPSERVER_CONNECTION_HPP