http.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #pragma once
  2. #include "conn.h"
  3. #include "options.h"
  4. #include <util/thread/pool.h>
  5. #include <library/cpp/http/io/stream.h>
  6. #include <util/memory/blob.h>
  7. #include <util/generic/ptr.h>
  8. #include <util/generic/vector.h>
  9. #include <util/system/atomic.h>
  10. class IThreadFactory;
  11. class TClientRequest;
  12. class TClientConnection;
  13. class THttpServer {
  14. friend class TClientRequest;
  15. friend class TClientConnection;
  16. public:
  17. class ICallBack {
  18. public:
  19. struct TFailLogData {
  20. int failstate;
  21. TString url;
  22. };
  23. virtual ~ICallBack() {
  24. }
  25. virtual void OnFailRequest(int /*failstate*/) {
  26. }
  27. virtual void OnFailRequestEx(const TFailLogData& d) {
  28. OnFailRequest(d.failstate);
  29. }
  30. virtual void OnException() {
  31. }
  32. virtual void OnMaxConn() {
  33. }
  34. virtual TClientRequest* CreateClient() = 0;
  35. virtual void OnListenStart() {
  36. }
  37. virtual void OnListenStop() {
  38. }
  39. virtual void OnWait() {
  40. }
  41. virtual void* CreateThreadSpecificResource() {
  42. return nullptr;
  43. }
  44. virtual void DestroyThreadSpecificResource(void*) {
  45. }
  46. };
  47. typedef THttpServerOptions TOptions;
  48. typedef TSimpleSharedPtr<IThreadPool> TMtpQueueRef;
  49. THttpServer(ICallBack* cb, const TOptions& options = TOptions(), IThreadFactory* pool = nullptr);
  50. THttpServer(ICallBack* cb, TMtpQueueRef mainWorkers, TMtpQueueRef failWorkers, const TOptions& options = TOptions());
  51. virtual ~THttpServer();
  52. bool Start();
  53. // shutdown a.s.a.p.
  54. void Stop();
  55. // graceful shutdown with serving all already open connections
  56. void Shutdown();
  57. void Wait();
  58. int GetErrorCode();
  59. const char* GetError();
  60. void RestartRequestThreads(ui32 nTh, ui32 maxQS);
  61. const TOptions& Options() const noexcept;
  62. i64 GetClientCount() const;
  63. class TImpl;
  64. size_t GetRequestQueueSize() const;
  65. size_t GetFailQueueSize() const;
  66. const IThreadPool& GetRequestQueue() const;
  67. const IThreadPool& GetFailQueue() const;
  68. static TAtomicBase AcceptReturnsInvalidSocketCounter();
  69. private:
  70. bool MaxRequestsReached() const;
  71. private:
  72. THolder<TImpl> Impl_;
  73. };
  74. /**
  75. * @deprecated Use TRequestReplier instead
  76. */
  77. class TClientRequest: public IObjectInQueue {
  78. friend class THttpServer::TImpl;
  79. public:
  80. TClientRequest();
  81. ~TClientRequest() override;
  82. inline THttpInput& Input() noexcept {
  83. return *HttpConn_->Input();
  84. }
  85. inline THttpOutput& Output() noexcept {
  86. return *HttpConn_->Output();
  87. }
  88. THttpServer* HttpServ() const noexcept;
  89. const TSocket& Socket() const noexcept;
  90. NAddr::IRemoteAddrRef GetListenerSockAddrRef() const noexcept;
  91. TInstant AcceptMoment() const noexcept;
  92. bool IsLocal() const;
  93. bool CheckLoopback();
  94. void ProcessFailRequest(int failstate);
  95. void ReleaseConnection();
  96. void ResetConnection();
  97. private:
  98. /*
  99. * Processes the request after 'connection' been created and 'Headers' been read
  100. * Returns 'false' if the processing must be continued by the next handler,
  101. * 'true' otherwise ('this' will be deleted)
  102. */
  103. virtual bool Reply(void* ThreadSpecificResource);
  104. void Process(void* ThreadSpecificResource) override;
  105. public:
  106. TVector<std::pair<TString, TString>> ParsedHeaders;
  107. TString RequestString;
  108. private:
  109. THolder<TClientConnection> Conn_;
  110. THolder<THttpServerConn> HttpConn_;
  111. };
  112. class TRequestReplier: public TClientRequest {
  113. public:
  114. TRequestReplier();
  115. ~TRequestReplier() override;
  116. struct TReplyParams {
  117. void* ThreadSpecificResource;
  118. THttpInput& Input;
  119. THttpOutput& Output;
  120. };
  121. /*
  122. * Processes the request after 'connection' been created and 'Headers' been read
  123. * Returns 'false' if the processing must be continued by the next handler,
  124. * 'true' otherwise ('this' will be deleted)
  125. */
  126. virtual bool DoReply(const TReplyParams& params) = 0;
  127. private:
  128. bool Reply(void* threadSpecificResource) final;
  129. using TClientRequest::Input;
  130. using TClientRequest::Output;
  131. };
  132. bool TryToBindAddresses(const THttpServerOptions& options, const std::function<void(TSocket)>* callbackOnBoundAddress = nullptr);