工具项目

HttpServer.h 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright: JessMA Open Source (ldcsaa@gmail.com)
  3. *
  4. * Author : Bruce Liang
  5. * Website : http://www.jessma.org
  6. * Project : https://github.com/ldcsaa
  7. * Blog : http://www.cnblogs.com/ldcsaa
  8. * Wiki : http://www.oschina.net/p/hp-socket
  9. * QQ Group : 75375912, 44636872
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. */
  23. #pragma once
  24. #include "TcpServer.h"
  25. #include "HttpHelper.h"
  26. #include "../Common/Src/Thread.h"
  27. #ifdef _HTTP_SUPPORT
  28. template<class T, USHORT default_port> class CHttpServerT : public IComplexHttpResponder, public T
  29. {
  30. protected:
  31. typedef CThread<CHttpServerT> CCleanThread;
  32. friend class CCleanThread;
  33. typedef CHttpObjPoolT<TRUE, CHttpServerT, TSocketObj> CHttpObjPool;
  34. typedef THttpObjT<CHttpServerT, TSocketObj> THttpObj;
  35. friend struct THttpObj;
  36. public:
  37. virtual BOOL Start(LPCTSTR lpszBindAddress, USHORT usPort);
  38. virtual BOOL SendResponse(CONNID dwConnID, USHORT usStatusCode, LPCSTR lpszDesc = nullptr, const THeader lpHeaders[] = nullptr, int iHeaderCount = 0, const BYTE* pData = nullptr, int iLength = 0);
  39. virtual BOOL SendLocalFile(CONNID dwConnID, LPCSTR lpszFileName, USHORT usStatusCode = HSC_OK, LPCSTR lpszDesc = nullptr, const THeader lpHeaders[] = nullptr, int iHeaderCount = 0);
  40. virtual BOOL Release(CONNID dwConnID);
  41. virtual BOOL SendWSMessage(CONNID dwConnID, BOOL bFinal, BYTE iReserved, BYTE iOperationCode, const BYTE lpszMask[4] = nullptr, BYTE* pData = nullptr, int iLength = 0, ULONGLONG ullBodyLen = 0);
  42. public:
  43. virtual void SetLocalVersion(EnHttpVersion enLocalVersion) {m_enLocalVersion = enLocalVersion;}
  44. virtual void SetReleaseDelay(DWORD dwReleaseDelay) {m_dwReleaseDelay = dwReleaseDelay;}
  45. virtual EnHttpVersion GetLocalVersion () {return m_enLocalVersion;}
  46. virtual DWORD GetReleaseDelay () {return m_dwReleaseDelay;}
  47. virtual BOOL IsUpgrade(CONNID dwConnID);
  48. virtual BOOL IsKeepAlive(CONNID dwConnID);
  49. virtual USHORT GetVersion(CONNID dwConnID);
  50. virtual LPCSTR GetHost(CONNID dwConnID);
  51. virtual ULONGLONG GetContentLength(CONNID dwConnID);
  52. virtual LPCSTR GetContentType(CONNID dwConnID);
  53. virtual LPCSTR GetContentEncoding(CONNID dwConnID);
  54. virtual LPCSTR GetTransferEncoding(CONNID dwConnID);
  55. virtual EnHttpUpgradeType GetUpgradeType(CONNID dwConnID);
  56. virtual USHORT GetParseErrorCode(CONNID dwConnID, LPCSTR* lpszErrorDesc = nullptr);
  57. virtual BOOL GetHeader(CONNID dwConnID, LPCSTR lpszName, LPCSTR* lpszValue);
  58. virtual BOOL GetHeaders(CONNID dwConnID, LPCSTR lpszName, LPCSTR lpszValue[], DWORD& dwCount);
  59. virtual BOOL GetAllHeaders(CONNID dwConnID, THeader lpHeaders[], DWORD& dwCount);
  60. virtual BOOL GetAllHeaderNames(CONNID dwConnID, LPCSTR lpszName[], DWORD& dwCount);
  61. virtual BOOL GetCookie(CONNID dwConnID, LPCSTR lpszName, LPCSTR* lpszValue);
  62. virtual BOOL GetAllCookies(CONNID dwConnID, TCookie lpCookies[], DWORD& dwCount);
  63. virtual USHORT GetUrlFieldSet(CONNID dwConnID);
  64. virtual LPCSTR GetUrlField(CONNID dwConnID, EnHttpUrlField enField);
  65. virtual LPCSTR GetMethod(CONNID dwConnID);
  66. virtual BOOL GetWSMessageState(CONNID dwConnID, BOOL* lpbFinal, BYTE* lpiReserved, BYTE* lpiOperationCode, LPCBYTE* lpszMask, ULONGLONG* lpullBodyLen, ULONGLONG* lpullBodyRemain);
  67. private:
  68. virtual BOOL CheckParams();
  69. virtual void PrepareStart();
  70. virtual EnHandleResult DoFireAccept(TSocketObj* pSocketObj);
  71. virtual EnHandleResult DoFireHandShake(TSocketObj* pSocketObj);
  72. virtual EnHandleResult DoFireReceive(TSocketObj* pSocketObj, const BYTE* pData, int iLength);
  73. virtual EnHandleResult DoFireClose(TSocketObj* pSocketObj, EnSocketOperation enOperation, int iErrorCode);
  74. virtual EnHandleResult DoFireShutdown();
  75. EnHandleResult DoFireSuperReceive(TSocketObj* pSocketObj, const BYTE* pData, int iLength)
  76. {return __super::DoFireReceive(pSocketObj, pData, iLength);}
  77. EnHttpParseResult FireMessageBegin(TSocketObj* pSocketObj)
  78. {return m_pListener->OnMessageBegin((IHttpServer*)this, pSocketObj->connID);}
  79. EnHttpParseResult FireRequestLine(TSocketObj* pSocketObj, LPCSTR lpszMethod, LPCSTR lpszUrl)
  80. {return m_pListener->OnRequestLine((IHttpServer*)this, pSocketObj->connID, lpszMethod, lpszUrl);}
  81. EnHttpParseResult FireStatusLine(TSocketObj* pSocketObj, USHORT usStatusCode, LPCSTR lpszDesc)
  82. {return m_pListener->OnStatusLine((IHttpServer*)this, pSocketObj->connID, usStatusCode, lpszDesc);}
  83. EnHttpParseResult FireHeader(TSocketObj* pSocketObj, LPCSTR lpszName, LPCSTR lpszValue)
  84. {return m_pListener->OnHeader((IHttpServer*)this, pSocketObj->connID, lpszName, lpszValue);}
  85. EnHttpParseResult FireHeadersComplete(TSocketObj* pSocketObj)
  86. {return m_pListener->OnHeadersComplete((IHttpServer*)this, pSocketObj->connID);}
  87. EnHttpParseResult FireBody(TSocketObj* pSocketObj, const BYTE* pData, int iLength)
  88. {return m_pListener->OnBody((IHttpServer*)this, pSocketObj->connID, pData, iLength);}
  89. EnHttpParseResult FireChunkHeader(TSocketObj* pSocketObj, int iLength)
  90. {return m_pListener->OnChunkHeader((IHttpServer*)this, pSocketObj->connID, iLength);}
  91. EnHttpParseResult FireChunkComplete(TSocketObj* pSocketObj)
  92. {return m_pListener->OnChunkComplete((IHttpServer*)this, pSocketObj->connID);}
  93. EnHttpParseResult FireMessageComplete(TSocketObj* pSocketObj)
  94. {return m_pListener->OnMessageComplete((IHttpServer*)this, pSocketObj->connID);}
  95. EnHttpParseResult FireUpgrade(TSocketObj* pSocketObj, EnHttpUpgradeType enUpgradeType)
  96. {return m_pListener->OnUpgrade((IHttpServer*)this, pSocketObj->connID, enUpgradeType);}
  97. EnHttpParseResult FireParseError(TSocketObj* pSocketObj, int iErrorCode, LPCSTR lpszErrorDesc)
  98. {return m_pListener->OnParseError((IHttpServer*)this, pSocketObj->connID, iErrorCode, lpszErrorDesc);}
  99. EnHandleResult FireWSMessageHeader(TSocketObj* pSocketObj, BOOL bFinal, BYTE iReserved, BYTE iOperationCode, const BYTE lpszMask[4], ULONGLONG ullBodyLen)
  100. {return m_pListener->OnWSMessageHeader((IHttpServer*)this, pSocketObj->connID, bFinal, iReserved, iOperationCode, lpszMask, ullBodyLen);}
  101. EnHandleResult FireWSMessageBody(TSocketObj* pSocketObj, const BYTE* pData, int iLength)
  102. {return m_pListener->OnWSMessageBody((IHttpServer*)this, pSocketObj->connID, pData, iLength);}
  103. EnHandleResult FireWSMessageComplete(TSocketObj* pSocketObj)
  104. {return m_pListener->OnWSMessageComplete((IHttpServer*)this, pSocketObj->connID);}
  105. inline THttpObj* FindHttpObj(CONNID dwConnID);
  106. inline THttpObj* FindHttpObj(TSocketObj* pSocketObj);
  107. CCookieMgr* GetCookieMgr() {return nullptr;}
  108. LPCSTR GetRemoteDomain(TSocketObj* pSocketObj) {return nullptr;}
  109. private:
  110. void KillDyingConnection();
  111. void ReleaseDyingConnection();
  112. UINT CleanerThreadProc(PVOID pv = nullptr);
  113. void WaitForCleanerThreadEnd();
  114. public:
  115. CHttpServerT(IHttpServerListener* pListener)
  116. : T (pListener)
  117. , m_pListener (pListener)
  118. , m_enLocalVersion (DEFAULT_HTTP_VERSION)
  119. , m_dwReleaseDelay (DEFAULT_HTTP_RELEASE_DELAY)
  120. {
  121. }
  122. virtual ~CHttpServerT()
  123. {
  124. Stop();
  125. }
  126. private:
  127. IHttpServerListener* m_pListener;
  128. CEvt m_evCleaner;
  129. CCleanThread m_thCleaner;
  130. EnHttpVersion m_enLocalVersion;
  131. DWORD m_dwReleaseDelay;
  132. CCASQueue<TDyingConnection> m_lsDyingQueue;
  133. CHttpObjPool m_objPool;
  134. };
  135. // ------------------------------------------------------------------------------------------------------------- //
  136. typedef CHttpServerT<CTcpServer, HTTP_DEFAULT_PORT> CHttpServer;
  137. #ifdef _SSL_SUPPORT
  138. #include "SSLServer.h"
  139. typedef CHttpServerT<CSSLServer, HTTPS_DEFAULT_PORT> CHttpsServer;
  140. #endif
  141. #endif