工具项目

HttpClient.h 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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 "TcpClient.h"
  25. #include "HttpHelper.h"
  26. #ifdef _HTTP_SUPPORT
  27. template<class R, class T, USHORT default_port> class CHttpClientT : public R, public T
  28. {
  29. protected:
  30. typedef THttpObjT<CHttpClientT, IHttpClient> THttpObj;
  31. friend struct THttpObj;
  32. public:
  33. virtual BOOL SendRequest(LPCSTR lpszMethod, LPCSTR lpszPath, const THeader lpHeaders[] = nullptr, int iHeaderCount = 0, const BYTE* pBody = nullptr, int iLength = 0);
  34. virtual BOOL SendLocalFile(LPCSTR lpszFileName, LPCSTR lpszMethod, LPCSTR lpszPath, const THeader lpHeaders[] = nullptr, int iHeaderCount = 0);
  35. virtual BOOL SendPost(LPCSTR lpszPath, const THeader lpHeaders[], int iHeaderCount, const BYTE* pBody, int iLength)
  36. {return SendRequest(HTTP_METHOD_POST, lpszPath, lpHeaders, iHeaderCount, pBody, iLength);}
  37. virtual BOOL SendPut(LPCSTR lpszPath, const THeader lpHeaders[], int iHeaderCount, const BYTE* pBody, int iLength)
  38. {return SendRequest(HTTP_METHOD_PUT, lpszPath, lpHeaders, iHeaderCount, pBody, iLength);}
  39. virtual BOOL SendPatch(LPCSTR lpszPath, const THeader lpHeaders[], int iHeaderCount, const BYTE* pBody, int iLength)
  40. {return SendRequest(HTTP_METHOD_PATCH, lpszPath, lpHeaders, iHeaderCount, pBody, iLength);}
  41. virtual BOOL SendGet(LPCSTR lpszPath, const THeader lpHeaders[] = nullptr, int iHeaderCount = 0)
  42. {return SendRequest(HTTP_METHOD_GET, lpszPath, lpHeaders, iHeaderCount);}
  43. virtual BOOL SendDelete(LPCSTR lpszPath, const THeader lpHeaders[] = nullptr, int iHeaderCount = 0)
  44. {return SendRequest(HTTP_METHOD_DELETE, lpszPath, lpHeaders, iHeaderCount);}
  45. virtual BOOL SendHead(LPCSTR lpszPath, const THeader lpHeaders[] = nullptr, int iHeaderCount = 0)
  46. {return SendRequest(HTTP_METHOD_HEAD, lpszPath, lpHeaders, iHeaderCount);}
  47. virtual BOOL SendTrace(LPCSTR lpszPath, const THeader lpHeaders[] = nullptr, int iHeaderCount = 0)
  48. {return SendRequest(HTTP_METHOD_TRACE, lpszPath, lpHeaders, iHeaderCount);}
  49. virtual BOOL SendOptions(LPCSTR lpszPath, const THeader lpHeaders[] = nullptr, int iHeaderCount = 0)
  50. {return SendRequest(HTTP_METHOD_OPTIONS, lpszPath, lpHeaders, iHeaderCount);}
  51. virtual BOOL SendConnect(LPCSTR lpszHost, const THeader lpHeaders[] = nullptr, int iHeaderCount = 0)
  52. {return SendRequest(HTTP_METHOD_CONNECT, lpszHost, lpHeaders, iHeaderCount);}
  53. virtual BOOL SendWSMessage(BOOL bFinal, BYTE iReserved, BYTE iOperationCode, const BYTE lpszMask[4] = nullptr, BYTE* pData = nullptr, int iLength = 0, ULONGLONG ullBodyLen = 0);
  54. public:
  55. virtual void SetUseCookie(BOOL bUseCookie) {m_pCookieMgr = bUseCookie ? &g_CookieMgr : nullptr;}
  56. virtual BOOL IsUseCookie() {return m_pCookieMgr != nullptr;}
  57. virtual void SetLocalVersion(EnHttpVersion enLocalVersion) {m_enLocalVersion = enLocalVersion;}
  58. virtual EnHttpVersion GetLocalVersion() {return m_enLocalVersion;}
  59. virtual BOOL IsUpgrade()
  60. {return m_objHttp.IsUpgrade();}
  61. virtual BOOL IsKeepAlive()
  62. {return m_objHttp.IsKeepAlive();}
  63. virtual USHORT GetVersion()
  64. {return m_objHttp.GetVersion();}
  65. virtual ULONGLONG GetContentLength()
  66. {return m_objHttp.GetContentLength();}
  67. virtual LPCSTR GetContentType()
  68. {return m_objHttp.GetContentType();}
  69. virtual LPCSTR GetContentEncoding()
  70. {return m_objHttp.GetContentEncoding();}
  71. virtual LPCSTR GetTransferEncoding()
  72. {return m_objHttp.GetTransferEncoding();}
  73. virtual EnHttpUpgradeType GetUpgradeType()
  74. {return m_objHttp.GetUpgradeType();}
  75. virtual USHORT GetParseErrorCode(LPCSTR* lpszErrorDesc = nullptr)
  76. {return m_objHttp.GetParseErrorCode(lpszErrorDesc);}
  77. virtual BOOL GetHeader(LPCSTR lpszName, LPCSTR* lpszValue)
  78. {return m_objHttp.GetHeader(lpszName, lpszValue);}
  79. virtual BOOL GetHeaders(LPCSTR lpszName, LPCSTR lpszValue[], DWORD& dwCount)
  80. {return m_objHttp.GetHeaders(lpszName, lpszValue, dwCount);}
  81. virtual BOOL GetAllHeaders(THeader lpHeaders[], DWORD& dwCount)
  82. {return m_objHttp.GetAllHeaders(lpHeaders, dwCount);}
  83. virtual BOOL GetAllHeaderNames(LPCSTR lpszName[], DWORD& dwCount)
  84. {return m_objHttp.GetAllHeaderNames(lpszName, dwCount);}
  85. virtual BOOL GetCookie(LPCSTR lpszName, LPCSTR* lpszValue)
  86. {return m_objHttp.GetCookie(lpszName, lpszValue);}
  87. virtual BOOL GetAllCookies(TCookie lpCookies[], DWORD& dwCount)
  88. {return m_objHttp.GetAllCookies(lpCookies, dwCount);}
  89. virtual USHORT GetStatusCode()
  90. {return m_objHttp.GetStatusCode();}
  91. virtual BOOL GetWSMessageState(BOOL* lpbFinal, BYTE* lpiReserved, BYTE* lpiOperationCode, LPCBYTE* lpszMask, ULONGLONG* lpullBodyLen, ULONGLONG* lpullBodyRemain)
  92. {return m_objHttp.GetWSMessageState(lpbFinal, lpiReserved, lpiOperationCode, lpszMask, lpullBodyLen, lpullBodyRemain);}
  93. private:
  94. virtual BOOL CheckParams();
  95. virtual EnHandleResult DoFireReceive(ITcpClient* pSender, const BYTE* pData, int iLength)
  96. {ASSERT(pSender == this); return m_objHttp.Execute(pData, iLength);}
  97. EnHandleResult DoFireSuperReceive(IHttpClient* pSender, const BYTE* pData, int iLength)
  98. {ASSERT(pSender == (IHttpClient*)this); return __super::DoFireReceive(pSender, pData, iLength);}
  99. virtual EnHandleResult DoFireClose(ITcpClient* pSender, EnSocketOperation enOperation, int iErrorCode)
  100. {
  101. ASSERT(pSender == (IHttpClient*)this);
  102. m_objHttp.CheckBodyIdentityEof();
  103. return __super::DoFireClose(pSender, enOperation, iErrorCode);
  104. }
  105. virtual void Reset()
  106. {
  107. m_objHttp.Reset();
  108. __super::Reset();
  109. }
  110. EnHttpParseResult FireMessageBegin(IHttpClient* pSender)
  111. {return m_pListener->OnMessageBegin(pSender, pSender->GetConnectionID());}
  112. EnHttpParseResult FireRequestLine(IHttpClient* pSender, LPCSTR lpszMethod, LPCSTR lpszUrl)
  113. {return m_pListener->OnRequestLine(pSender, pSender->GetConnectionID(), lpszMethod, lpszUrl);}
  114. EnHttpParseResult FireStatusLine(IHttpClient* pSender, USHORT usStatusCode, LPCSTR lpszDesc)
  115. {return m_pListener->OnStatusLine(pSender, pSender->GetConnectionID(), usStatusCode, lpszDesc);}
  116. EnHttpParseResult FireHeader(IHttpClient* pSender, LPCSTR lpszName, LPCSTR lpszValue)
  117. {return m_pListener->OnHeader(pSender, pSender->GetConnectionID(), lpszName, lpszValue);}
  118. EnHttpParseResult FireHeadersComplete(IHttpClient* pSender)
  119. {return m_pListener->OnHeadersComplete(pSender, pSender->GetConnectionID());}
  120. EnHttpParseResult FireBody(IHttpClient* pSender, const BYTE* pData, int iLength)
  121. {return m_pListener->OnBody(pSender, pSender->GetConnectionID(), pData, iLength);}
  122. EnHttpParseResult FireChunkHeader(IHttpClient* pSender, int iLength)
  123. {return m_pListener->OnChunkHeader(pSender, pSender->GetConnectionID(), iLength);}
  124. EnHttpParseResult FireChunkComplete(IHttpClient* pSender)
  125. {return m_pListener->OnChunkComplete(pSender, pSender->GetConnectionID());}
  126. EnHttpParseResult FireMessageComplete(IHttpClient* pSender)
  127. {return m_pListener->OnMessageComplete(pSender, pSender->GetConnectionID());}
  128. EnHttpParseResult FireUpgrade(IHttpClient* pSender, EnHttpUpgradeType enUpgradeType)
  129. {return m_pListener->OnUpgrade(pSender, pSender->GetConnectionID(), enUpgradeType);}
  130. EnHttpParseResult FireParseError(IHttpClient* pSender, int iErrorCode, LPCSTR lpszErrorDesc)
  131. {return m_pListener->OnParseError(pSender, pSender->GetConnectionID(), iErrorCode, lpszErrorDesc);}
  132. EnHandleResult FireWSMessageHeader(IHttpClient* pSender, BOOL bFinal, BYTE iReserved, BYTE iOperationCode, const BYTE lpszMask[4], ULONGLONG ullBodyLen)
  133. {return m_pListener->OnWSMessageHeader(pSender, pSender->GetConnectionID(), bFinal, iReserved, iOperationCode, lpszMask, ullBodyLen);}
  134. EnHandleResult FireWSMessageBody(IHttpClient* pSender, const BYTE* pData, int iLength)
  135. {return m_pListener->OnWSMessageBody(pSender, pSender->GetConnectionID(), pData, iLength);}
  136. EnHandleResult FireWSMessageComplete(IHttpClient* pSender)
  137. {return m_pListener->OnWSMessageComplete(pSender, pSender->GetConnectionID());}
  138. CCookieMgr* GetCookieMgr() {return m_pCookieMgr;}
  139. LPCSTR GetRemoteDomain(IHttpClient* pSender) {LPCSTR lpszDomain; GetRemoteHost(&lpszDomain); return lpszDomain;}
  140. public:
  141. CHttpClientT(IHttpClientListener* pListener)
  142. : T (pListener)
  143. , m_pListener (pListener)
  144. , m_pCookieMgr (&g_CookieMgr)
  145. , m_enLocalVersion (DEFAULT_HTTP_VERSION)
  146. , m_objHttp (FALSE, this, (IHttpClient*)this)
  147. {
  148. }
  149. virtual ~CHttpClientT()
  150. {
  151. Stop();
  152. }
  153. protected:
  154. THttpObj m_objHttp;
  155. private:
  156. IHttpClientListener* m_pListener;
  157. CCookieMgr* m_pCookieMgr;
  158. EnHttpVersion m_enLocalVersion;
  159. };
  160. // ------------------------------------------------------------------------------------------------------------- //
  161. template<class T, USHORT default_port> class CHttpSyncClientT : public CHttpClientT<IHttpSyncRequester, T, default_port>, private CHttpClientListener
  162. {
  163. public:
  164. virtual BOOL Start(LPCTSTR lpszRemoteAddress, USHORT usPort, BOOL bAsyncConnect = TRUE, LPCTSTR lpszBindAddress = nullptr);
  165. public:
  166. virtual BOOL SendRequest(LPCSTR lpszMethod, LPCSTR lpszPath, const THeader lpHeaders[] = nullptr, int iHeaderCount = 0, const BYTE* pBody = nullptr, int iLength = 0);
  167. virtual BOOL SendWSMessage(BOOL bFinal, BYTE iReserved, BYTE iOperationCode, const BYTE lpszMask[4] = nullptr, BYTE* pData = nullptr, int iLength = 0, ULONGLONG ullBodyLen = 0);
  168. public:
  169. virtual BOOL IsUpgrade()
  170. {return m_pHttpObj->IsUpgrade();}
  171. virtual BOOL IsKeepAlive()
  172. {return m_pHttpObj->IsKeepAlive();}
  173. virtual USHORT GetVersion()
  174. {return m_pHttpObj->GetVersion();}
  175. virtual ULONGLONG GetContentLength()
  176. {return m_pHttpObj->GetContentLength();}
  177. virtual LPCSTR GetContentType()
  178. {return m_pHttpObj->GetContentType();}
  179. virtual LPCSTR GetContentEncoding()
  180. {return m_pHttpObj->GetContentEncoding();}
  181. virtual LPCSTR GetTransferEncoding()
  182. {return m_pHttpObj->GetTransferEncoding();}
  183. virtual EnHttpUpgradeType GetUpgradeType()
  184. {return m_pHttpObj->GetUpgradeType();}
  185. virtual USHORT GetParseErrorCode(LPCSTR* lpszErrorDesc = nullptr)
  186. {return m_pHttpObj->GetParseErrorCode(lpszErrorDesc);}
  187. virtual BOOL GetHeader(LPCSTR lpszName, LPCSTR* lpszValue)
  188. {return m_pHttpObj->GetHeader(lpszName, lpszValue);}
  189. virtual BOOL GetHeaders(LPCSTR lpszName, LPCSTR lpszValue[], DWORD& dwCount)
  190. {return m_pHttpObj->GetHeaders(lpszName, lpszValue, dwCount);}
  191. virtual BOOL GetAllHeaders(THeader lpHeaders[], DWORD& dwCount)
  192. {return m_pHttpObj->GetAllHeaders(lpHeaders, dwCount);}
  193. virtual BOOL GetAllHeaderNames(LPCSTR lpszName[], DWORD& dwCount)
  194. {return m_pHttpObj->GetAllHeaderNames(lpszName, dwCount);}
  195. virtual BOOL GetCookie(LPCSTR lpszName, LPCSTR* lpszValue)
  196. {return m_pHttpObj->GetCookie(lpszName, lpszValue);}
  197. virtual BOOL GetAllCookies(TCookie lpCookies[], DWORD& dwCount)
  198. {return m_pHttpObj->GetAllCookies(lpCookies, dwCount);}
  199. virtual USHORT GetStatusCode()
  200. {return m_pHttpObj->GetStatusCode();}
  201. virtual BOOL GetWSMessageState(BOOL* lpbFinal, BYTE* lpiReserved, BYTE* lpiOperationCode, LPCBYTE* lpszMask, ULONGLONG* lpullBodyLen, ULONGLONG* lpullBodyRemain)
  202. {return m_pHttpObj->GetWSMessageState(lpbFinal, lpiReserved, lpiOperationCode, lpszMask, lpullBodyLen, lpullBodyRemain);}
  203. public:
  204. virtual BOOL OpenUrl(LPCSTR lpszMethod, LPCSTR lpszUrl, const THeader lpHeaders[] = nullptr, int iHeaderCount = 0, const BYTE* pBody = nullptr, int iLength = 0, BOOL bForceReconnect = FALSE);
  205. virtual BOOL CleanupRequestResult();
  206. public:
  207. virtual BOOL GetResponseBody (LPCBYTE* lpszBody, int* iLength);
  208. virtual void SetConnectTimeout (DWORD dwConnectTimeout) {m_dwConnectTimeout = dwConnectTimeout;}
  209. virtual void SetRequestTimeout (DWORD dwRequestTimeout) {m_dwRequestTimeout = dwRequestTimeout;}
  210. virtual DWORD GetConnectTimeout () {return m_dwConnectTimeout;}
  211. virtual DWORD GetRequestTimeout () {return m_dwRequestTimeout;}
  212. private:
  213. virtual EnHandleResult OnHandShake(ITcpClient* pSender, CONNID dwConnID);
  214. virtual EnHandleResult OnClose(ITcpClient* pSender, CONNID dwConnID, EnSocketOperation enOperation, int iErrorCode);
  215. virtual EnHttpParseResult OnBody(IHttpClient* pSender, CONNID dwConnID, const BYTE* pData, int iLength);
  216. virtual EnHttpParseResult OnMessageComplete(IHttpClient* pSender, CONNID dwConnID);
  217. virtual EnHttpParseResult OnUpgrade(IHttpClient* pSender, CONNID dwConnID, EnHttpUpgradeType enUpgradeType);
  218. virtual EnHttpParseResult OnParseError(IHttpClient* pSender, CONNID dwConnID, int iErrorCode, LPCSTR lpszErrorDesc);
  219. virtual EnHandleResult OnWSMessageBody(IHttpClient* pSender, CONNID dwConnID, const BYTE* pData, int iLength);
  220. virtual EnHandleResult OnWSMessageComplete(IHttpClient* pSender, CONNID dwConnID);
  221. virtual EnHandleResult OnPrepareConnect(ITcpClient* pSender, CONNID dwConnID, SOCKET socket);
  222. virtual EnHandleResult OnConnect(ITcpClient* pSender, CONNID dwConnID);
  223. virtual EnHandleResult OnSend(ITcpClient* pSender, CONNID dwConnID, const BYTE* pData, int iLength);
  224. virtual EnHttpParseResult OnMessageBegin(IHttpClient* pSender, CONNID dwConnID);
  225. virtual EnHttpParseResult OnStatusLine(IHttpClient* pSender, CONNID dwConnID, USHORT usStatusCode, LPCSTR lpszDesc);
  226. virtual EnHttpParseResult OnHeader(IHttpClient* pSender, CONNID dwConnID, LPCSTR lpszName, LPCSTR lpszValue);
  227. virtual EnHttpParseResult OnHeadersComplete(IHttpClient* pSender, CONNID dwConnID);
  228. virtual EnHttpParseResult OnChunkHeader(IHttpClient* pSender, CONNID dwConnID, int iLength);
  229. virtual EnHttpParseResult OnChunkComplete(IHttpClient* pSender, CONNID dwConnID);
  230. virtual EnHandleResult OnWSMessageHeader(IHttpClient* pSender, CONNID dwConnID, BOOL bFinal, BYTE iReserved, BYTE iOperationCode, const BYTE lpszMask[4], ULONGLONG ullBodyLen);
  231. private:
  232. void SetRequestEvent(EnHttpSyncRequestProgress enProgress, BOOL bCopyHttpObj = TRUE);
  233. public:
  234. CHttpSyncClientT(IHttpClientListener* pListener = nullptr)
  235. : CHttpClientT (this)
  236. , m_enProgress (HSRP_DONE)
  237. , m_objHttp2 (FALSE, this, (IHttpClient*)this)
  238. , m_pHttpObj (nullptr)
  239. , m_pListener2 (pListener)
  240. , m_dwConnectTimeout (DEFAULT_HTTP_SYNC_CONNECT_TIMEOUT)
  241. , m_dwRequestTimeout (DEFAULT_HTTP_SYNC_REQUEST_TIMEOUT)
  242. {
  243. }
  244. virtual ~CHttpSyncClientT()
  245. {
  246. Stop();
  247. }
  248. private:
  249. DWORD m_dwConnectTimeout;
  250. DWORD m_dwRequestTimeout;
  251. CEvt m_evWait;
  252. THttpObj m_objHttp2;
  253. THttpObj* m_pHttpObj;
  254. IHttpClientListener* m_pListener2;
  255. EnHttpSyncRequestProgress m_enProgress;
  256. CBufferPtrT<BYTE, 16 * 1024> m_szBuffer;
  257. };
  258. // ------------------------------------------------------------------------------------------------------------- //
  259. typedef CHttpClientT<IHttpRequester, CTcpClient, HTTP_DEFAULT_PORT> CHttpClient;
  260. typedef CHttpSyncClientT<CTcpClient, HTTP_DEFAULT_PORT> CHttpSyncClient;
  261. #ifdef _SSL_SUPPORT
  262. #include "SSLClient.h"
  263. typedef CHttpClientT<IHttpRequester, CSSLClient, HTTPS_DEFAULT_PORT> CHttpsClient;
  264. typedef CHttpSyncClientT<CSSLClient, HTTPS_DEFAULT_PORT> CHttpsSyncClient;
  265. #endif
  266. #endif