工具项目

HttpAgent.h 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 "TcpAgent.h"
  25. #include "HttpHelper.h"
  26. #ifdef _HTTP_SUPPORT
  27. template<class T, USHORT default_port> class CHttpAgentT : public IComplexHttpRequester, public T
  28. {
  29. protected:
  30. typedef CHttpObjPoolT<FALSE, CHttpAgentT, TSocketObj> CHttpObjPool;
  31. typedef THttpObjT<CHttpAgentT, TSocketObj> THttpObj;
  32. friend struct THttpObj;
  33. public:
  34. virtual BOOL SendRequest(CONNID dwConnID, LPCSTR lpszMethod, LPCSTR lpszPath, const THeader lpHeaders[] = nullptr, int iHeaderCount = 0, const BYTE* pBody = nullptr, int iLength = 0);
  35. virtual BOOL SendLocalFile(CONNID dwConnID, LPCSTR lpszFileName, LPCSTR lpszMethod, LPCSTR lpszPath, const THeader lpHeaders[] = nullptr, int iHeaderCount = 0);
  36. virtual BOOL SendPost(CONNID dwConnID, LPCSTR lpszPath, const THeader lpHeaders[], int iHeaderCount, const BYTE* pBody, int iLength)
  37. {return SendRequest(dwConnID, HTTP_METHOD_POST, lpszPath, lpHeaders, iHeaderCount, pBody, iLength);}
  38. virtual BOOL SendPut(CONNID dwConnID, LPCSTR lpszPath, const THeader lpHeaders[], int iHeaderCount, const BYTE* pBody, int iLength)
  39. {return SendRequest(dwConnID, HTTP_METHOD_PUT, lpszPath, lpHeaders, iHeaderCount, pBody, iLength);}
  40. virtual BOOL SendPatch(CONNID dwConnID, LPCSTR lpszPath, const THeader lpHeaders[], int iHeaderCount, const BYTE* pBody, int iLength)
  41. {return SendRequest(dwConnID, HTTP_METHOD_PATCH, lpszPath, lpHeaders, iHeaderCount, pBody, iLength);}
  42. virtual BOOL SendGet(CONNID dwConnID, LPCSTR lpszPath, const THeader lpHeaders[] = nullptr, int iHeaderCount = 0)
  43. {return SendRequest(dwConnID, HTTP_METHOD_GET, lpszPath, lpHeaders, iHeaderCount);}
  44. virtual BOOL SendDelete(CONNID dwConnID, LPCSTR lpszPath, const THeader lpHeaders[] = nullptr, int iHeaderCount = 0)
  45. {return SendRequest(dwConnID, HTTP_METHOD_DELETE, lpszPath, lpHeaders, iHeaderCount);}
  46. virtual BOOL SendHead(CONNID dwConnID, LPCSTR lpszPath, const THeader lpHeaders[] = nullptr, int iHeaderCount = 0)
  47. {return SendRequest(dwConnID, HTTP_METHOD_HEAD, lpszPath, lpHeaders, iHeaderCount);}
  48. virtual BOOL SendTrace(CONNID dwConnID, LPCSTR lpszPath, const THeader lpHeaders[] = nullptr, int iHeaderCount = 0)
  49. {return SendRequest(dwConnID, HTTP_METHOD_TRACE, lpszPath, lpHeaders, iHeaderCount);}
  50. virtual BOOL SendOptions(CONNID dwConnID, LPCSTR lpszPath, const THeader lpHeaders[] = nullptr, int iHeaderCount = 0)
  51. {return SendRequest(dwConnID, HTTP_METHOD_OPTIONS, lpszPath, lpHeaders, iHeaderCount);}
  52. virtual BOOL SendConnect(CONNID dwConnID, LPCSTR lpszHost, const THeader lpHeaders[] = nullptr, int iHeaderCount = 0)
  53. {return SendRequest(dwConnID, HTTP_METHOD_CONNECT, lpszHost, lpHeaders, iHeaderCount);}
  54. 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);
  55. public:
  56. virtual void SetUseCookie(BOOL bUseCookie) {m_pCookieMgr = bUseCookie ? &g_CookieMgr : nullptr;}
  57. virtual BOOL IsUseCookie() {return m_pCookieMgr != nullptr;}
  58. virtual void SetLocalVersion(EnHttpVersion enLocalVersion) {m_enLocalVersion = enLocalVersion;}
  59. virtual EnHttpVersion GetLocalVersion() {return m_enLocalVersion;}
  60. virtual BOOL IsUpgrade(CONNID dwConnID);
  61. virtual BOOL IsKeepAlive(CONNID dwConnID);
  62. virtual USHORT GetVersion(CONNID dwConnID);
  63. virtual ULONGLONG GetContentLength(CONNID dwConnID);
  64. virtual LPCSTR GetContentType(CONNID dwConnID);
  65. virtual LPCSTR GetContentEncoding(CONNID dwConnID);
  66. virtual LPCSTR GetTransferEncoding(CONNID dwConnID);
  67. virtual EnHttpUpgradeType GetUpgradeType(CONNID dwConnID);
  68. virtual USHORT GetParseErrorCode(CONNID dwConnID, LPCSTR* lpszErrorDesc = nullptr);
  69. virtual BOOL GetHeader(CONNID dwConnID, LPCSTR lpszName, LPCSTR* lpszValue);
  70. virtual BOOL GetHeaders(CONNID dwConnID, LPCSTR lpszName, LPCSTR lpszValue[], DWORD& dwCount);
  71. virtual BOOL GetAllHeaders(CONNID dwConnID, THeader lpHeaders[], DWORD& dwCount);
  72. virtual BOOL GetAllHeaderNames(CONNID dwConnID, LPCSTR lpszName[], DWORD& dwCount);
  73. virtual BOOL GetCookie(CONNID dwConnID, LPCSTR lpszName, LPCSTR* lpszValue);
  74. virtual BOOL GetAllCookies(CONNID dwConnID, TCookie lpCookies[], DWORD& dwCount);
  75. virtual USHORT GetStatusCode(CONNID dwConnID);
  76. virtual BOOL GetWSMessageState(CONNID dwConnID, BOOL* lpbFinal, BYTE* lpiReserved, BYTE* lpiOperationCode, LPCBYTE* lpszMask, ULONGLONG* lpullBodyLen, ULONGLONG* lpullBodyRemain);
  77. private:
  78. virtual BOOL CheckParams();
  79. virtual void PrepareStart();
  80. virtual EnHandleResult DoFireConnect(TSocketObj* pSocketObj);
  81. virtual EnHandleResult DoFireHandShake(TSocketObj* pSocketObj);
  82. virtual EnHandleResult DoFireReceive(TSocketObj* pSocketObj, const BYTE* pData, int iLength);
  83. virtual EnHandleResult DoFireClose(TSocketObj* pSocketObj, EnSocketOperation enOperation, int iErrorCode);
  84. virtual EnHandleResult DoFireShutdown();
  85. EnHandleResult DoFireSuperReceive(TSocketObj* pSocketObj, const BYTE* pData, int iLength)
  86. {return __super::DoFireReceive(pSocketObj, pData, iLength);}
  87. EnHttpParseResult FireMessageBegin(TSocketObj* pSocketObj)
  88. {return m_pListener->OnMessageBegin((IHttpAgent*)this, pSocketObj->connID);}
  89. EnHttpParseResult FireRequestLine(TSocketObj* pSocketObj, LPCSTR lpszMethod, LPCSTR lpszUrl)
  90. {return m_pListener->OnRequestLine((IHttpAgent*)this, pSocketObj->connID, lpszMethod, lpszUrl);}
  91. EnHttpParseResult FireStatusLine(TSocketObj* pSocketObj, USHORT usStatusCode, LPCSTR lpszDesc)
  92. {return m_pListener->OnStatusLine((IHttpAgent*)this, pSocketObj->connID, usStatusCode, lpszDesc);}
  93. EnHttpParseResult FireHeader(TSocketObj* pSocketObj, LPCSTR lpszName, LPCSTR lpszValue)
  94. {return m_pListener->OnHeader((IHttpAgent*)this, pSocketObj->connID, lpszName, lpszValue);}
  95. EnHttpParseResult FireHeadersComplete(TSocketObj* pSocketObj)
  96. {return m_pListener->OnHeadersComplete((IHttpAgent*)this, pSocketObj->connID);}
  97. EnHttpParseResult FireBody(TSocketObj* pSocketObj, const BYTE* pData, int iLength)
  98. {return m_pListener->OnBody((IHttpAgent*)this, pSocketObj->connID, pData, iLength);}
  99. EnHttpParseResult FireChunkHeader(TSocketObj* pSocketObj, int iLength)
  100. {return m_pListener->OnChunkHeader((IHttpAgent*)this, pSocketObj->connID, iLength);}
  101. EnHttpParseResult FireChunkComplete(TSocketObj* pSocketObj)
  102. {return m_pListener->OnChunkComplete((IHttpAgent*)this, pSocketObj->connID);}
  103. EnHttpParseResult FireMessageComplete(TSocketObj* pSocketObj)
  104. {return m_pListener->OnMessageComplete((IHttpAgent*)this, pSocketObj->connID);}
  105. EnHttpParseResult FireUpgrade(TSocketObj* pSocketObj, EnHttpUpgradeType enUpgradeType)
  106. {return m_pListener->OnUpgrade((IHttpAgent*)this, pSocketObj->connID, enUpgradeType);}
  107. EnHttpParseResult FireParseError(TSocketObj* pSocketObj, int iErrorCode, LPCSTR lpszErrorDesc)
  108. {return m_pListener->OnParseError((IHttpAgent*)this, pSocketObj->connID, iErrorCode, lpszErrorDesc);}
  109. EnHandleResult FireWSMessageHeader(TSocketObj* pSocketObj, BOOL bFinal, BYTE iReserved, BYTE iOperationCode, const BYTE lpszMask[4], ULONGLONG ullBodyLen)
  110. {return m_pListener->OnWSMessageHeader((IHttpAgent*)this, pSocketObj->connID, bFinal, iReserved, iOperationCode, lpszMask, ullBodyLen);}
  111. EnHandleResult FireWSMessageBody(TSocketObj* pSocketObj, const BYTE* pData, int iLength)
  112. {return m_pListener->OnWSMessageBody((IHttpAgent*)this, pSocketObj->connID, pData, iLength);}
  113. EnHandleResult FireWSMessageComplete(TSocketObj* pSocketObj)
  114. {return m_pListener->OnWSMessageComplete((IHttpAgent*)this, pSocketObj->connID);}
  115. inline THttpObj* FindHttpObj(CONNID dwConnID);
  116. inline THttpObj* FindHttpObj(TSocketObj* pSocketObj);
  117. CCookieMgr* GetCookieMgr() {return m_pCookieMgr;}
  118. LPCSTR GetRemoteDomain(TSocketObj* pSocketObj) {LPCSTR lpszDomain; pSocketObj->GetRemoteHost(&lpszDomain); return lpszDomain;}
  119. public:
  120. CHttpAgentT(IHttpAgentListener* pListener)
  121. : T (pListener)
  122. , m_pListener (pListener)
  123. , m_pCookieMgr (&g_CookieMgr)
  124. , m_enLocalVersion (DEFAULT_HTTP_VERSION)
  125. {
  126. }
  127. virtual ~CHttpAgentT()
  128. {
  129. Stop();
  130. }
  131. private:
  132. IHttpAgentListener* m_pListener;
  133. CCookieMgr* m_pCookieMgr;
  134. EnHttpVersion m_enLocalVersion;
  135. CHttpObjPool m_objPool;
  136. };
  137. // ------------------------------------------------------------------------------------------------------------- //
  138. typedef CHttpAgentT<CTcpAgent, HTTP_DEFAULT_PORT> CHttpAgent;
  139. #ifdef _SSL_SUPPORT
  140. #include "SSLAgent.h"
  141. typedef CHttpAgentT<CSSLAgent, HTTPS_DEFAULT_PORT> CHttpsAgent;
  142. #endif
  143. #endif