cppwebserver.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. Copyright 2017 Herik Lima de Castro and Marcelo Medeiros Eler
  3. Distributed under MIT license, or public domain if desired and
  4. recognized in your jurisdiction.
  5. See file LICENSE for detail.
  6. */
  7. #ifndef CPPWEBSERVER_H
  8. #define CPPWEBSERVER_H
  9. #include <QTcpServer>
  10. #include <QSslKey>
  11. #include <QSslCertificate>
  12. #include <QHttpMultiPart>
  13. #include <QThreadPool>
  14. #include <QTimer>
  15. #include <atomic>
  16. #include "qmapthreadsafety.h"
  17. #include "httpreadrequest.h"
  18. #include "controller.h"
  19. #include "session.h"
  20. #include "filter.h"
  21. #include "controller.h"
  22. #include "configuration.h"
  23. #include "request.h"
  24. #include "response.h"
  25. #include "cppwebframework_global.h"
  26. CWF_BEGIN_NAMESPACE
  27. /**
  28. * @brief The CppWebServer class is a HTTP server, responsable to receive and dispatch the requisitions.
  29. */
  30. class CPPWEBFRAMEWORKSHARED_EXPORT CppWebServer : public QTcpServer
  31. {
  32. Q_OBJECT
  33. private:
  34. Configuration configuration;
  35. Filter *filter;
  36. QTimer *timer;
  37. QThreadPool pool;
  38. QMapThreadSafety<QString, Controller *> urlController;
  39. QMapThreadSafety<QString, Session *> sessions;
  40. QSslConfiguration *ssl = nullptr;
  41. const int sleepTime = 10;
  42. QAtomicInteger<qint8> block = 0;
  43. public:
  44. /**
  45. * @brief Load SSL configuration, configure the thread pool and the filter.
  46. * @param Filter *filter : Install a filter for requests on the server. Optional.
  47. */
  48. explicit CppWebServer(const Configuration &configuration, Filter *filter = nullptr);
  49. /**
  50. * @brief Destroys all controllers and sessions.
  51. */
  52. ~CppWebServer() override;
  53. /**
  54. * @brief Hitches a url to a Controller.
  55. * @param const QString &url : Url name.
  56. * it will not be possible to map the controllers through the addController function, instead everything should be handled inside the Filter.
  57. */
  58. template<typename CONTROLLER>
  59. void addController(const QString &url) noexcept
  60. {
  61. static_assert(std::is_base_of<Controller, CONTROLLER>::value, "CONTROLLER must be a descendant of Controller");
  62. urlController.insert(url, new CONTROLLER);
  63. }
  64. protected:
  65. /**
  66. * @brief incomingConnection
  67. * @param qintptr socketfd : Socket descriptor.
  68. */
  69. void incomingConnection(qintptr socketfd) override;
  70. /**
  71. * @brief Clean expired sessions on Server.
  72. */
  73. void doClean();
  74. };
  75. CWF_END_NAMESPACE
  76. #endif // CPPWEBSERVER_H