QxHttpServer.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /****************************************************************************
  2. **
  3. ** https://www.qxorm.com/
  4. ** Copyright (C) 2013 Lionel Marty (contact@qxorm.com)
  5. **
  6. ** This file is part of the QxOrm library
  7. **
  8. ** This software is provided 'as-is', without any express or implied
  9. ** warranty. In no event will the authors be held liable for any
  10. ** damages arising from the use of this software
  11. **
  12. ** Commercial Usage
  13. ** Licensees holding valid commercial QxOrm licenses may use this file in
  14. ** accordance with the commercial license agreement provided with the
  15. ** Software or, alternatively, in accordance with the terms contained in
  16. ** a written agreement between you and Lionel Marty
  17. **
  18. ** GNU General Public License Usage
  19. ** Alternatively, this file may be used under the terms of the GNU
  20. ** General Public License version 3.0 as published by the Free Software
  21. ** Foundation and appearing in the file 'license.gpl3.txt' included in the
  22. ** packaging of this file. Please review the following information to
  23. ** ensure the GNU General Public License version 3.0 requirements will be
  24. ** met : http://www.gnu.org/copyleft/gpl.html
  25. **
  26. ** If you are unsure which license is appropriate for your use, or
  27. ** if you have questions regarding the use of this file, please contact :
  28. ** contact@qxorm.com
  29. **
  30. ****************************************************************************/
  31. #ifdef Q_MOC_RUN
  32. #include <QxCommon/QxConfig.h> // Need to include this file for the 'moc' process
  33. #endif // Q_MOC_RUN
  34. #ifdef _QX_ENABLE_QT_NETWORK
  35. #ifndef _QX_HTTP_SERVER_H_
  36. #define _QX_HTTP_SERVER_H_
  37. #ifdef _MSC_VER
  38. #pragma once
  39. #endif
  40. /*!
  41. * \file QxHttpServer.h
  42. * \author Lionel Marty
  43. * \ingroup QxHttpServer
  44. * \brief HTTP server which manages connections in a multi-threaded environment (support SSL/TLS, persistent connection, etc...) : https://www.qxorm.com/qxorm_en/manual.html#manual_96
  45. */
  46. #ifdef _QX_NO_PRECOMPILED_HEADER
  47. #ifndef Q_MOC_RUN
  48. #include <QxPrecompiled.h> // Need to include precompiled header for the generated moc file
  49. #endif // Q_MOC_RUN
  50. #endif // _QX_NO_PRECOMPILED_HEADER
  51. #include <QxHttpServer/QxHttpRequest.h>
  52. #include <QxHttpServer/QxHttpResponse.h>
  53. #include <QxHttpServer/QxHttpTransaction.h>
  54. #ifndef Q_MOC_RUN
  55. #include <QxService/QxTransaction.h>
  56. #include <QxService/QxConnect.h>
  57. #include <QxService/QxServer.h>
  58. #endif // Q_MOC_RUN
  59. #if (QT_VERSION >= 0x050000)
  60. class QAbstractEventDispatcher;
  61. #endif // (QT_VERSION >= 0x050000)
  62. namespace qx {
  63. /*!
  64. * \ingroup QxHttpServer
  65. * \brief qx::QxHttpServer : HTTP server which manages connections in a multi-threaded environment (support SSL/TLS, persistent connection, etc...) : https://www.qxorm.com/qxorm_en/manual.html#manual_96
  66. */
  67. class QX_DLL_EXPORT QxHttpServer : public QObject
  68. {
  69. Q_OBJECT
  70. public:
  71. typedef std::function<void (qx::QxHttpRequest &, qx::QxHttpResponse &)> type_fct_custom_request_handler;
  72. private:
  73. struct QxHttpServerImpl;
  74. std::unique_ptr<QxHttpServerImpl> m_pImpl; //!< Private implementation idiom
  75. public:
  76. QxHttpServer(QObject * parent = NULL);
  77. virtual ~QxHttpServer();
  78. Q_INVOKABLE void startServer();
  79. Q_INVOKABLE void stopServer();
  80. void setCustomRequestHandler(type_fct_custom_request_handler fct);
  81. QxHttpServer & dispatch(const QString & command, const QString & path, type_fct_custom_request_handler fct, long position = -1);
  82. void beforeDispatching(type_fct_custom_request_handler fct);
  83. void afterDispatching(type_fct_custom_request_handler fct);
  84. void clearDispatcher();
  85. #if (QT_VERSION >= 0x050000)
  86. void setEventDispatcher(QAbstractEventDispatcher * pEventDispatcher);
  87. #endif // (QT_VERSION >= 0x050000)
  88. static void buildResponseStaticFile(qx::QxHttpRequest & request, qx::QxHttpResponse & response, const QString & serverPath, qlonglong chunkedSize = 0);
  89. static void buildResponseQxRestApi(qx::QxHttpRequest & request, qx::QxHttpResponse & response);
  90. Q_SIGNALS:
  91. void error(const QString & err, qx::QxHttpTransaction_ptr transaction);
  92. void transactionStarted(qx::QxHttpTransaction_ptr transaction);
  93. void transactionFinished(qx::QxHttpTransaction_ptr transaction);
  94. void serverStatusChanged(bool bIsRunning);
  95. private Q_SLOTS:
  96. void onError(const QString & err, qx::service::QxTransaction_ptr transaction);
  97. void onServerIsRunning(bool bIsRunning, qx::service::QxServer * pServer);
  98. void onTransactionStarted(qx::service::QxTransaction_ptr transaction);
  99. void onTransactionFinished(qx::service::QxTransaction_ptr transaction);
  100. void onCustomRequestHandler(qx::service::QxTransaction_ptr transaction);
  101. };
  102. typedef std::shared_ptr<QxHttpServer> QxHttpServer_ptr;
  103. } // namespace qx
  104. typedef qx::QxHttpServer qx_http_server;
  105. #endif // _QX_HTTP_SERVER_H_
  106. #endif // _QX_ENABLE_QT_NETWORK