开源的socket服务端客户端,支持C# C++

HttpAgent.h 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright: JessMA Open Source (ldcsaa@gmail.com)
  3. *
  4. * Version : 4.1.3
  5. * Author : Bruce Liang
  6. * Website : http://www.jessma.org
  7. * Project : https://github.com/ldcsaa
  8. * Blog : http://www.cnblogs.com/ldcsaa
  9. * Wiki : http://www.oschina.net/p/hp-socket
  10. * QQ Group : 75375912
  11. *
  12. * Licensed under the Apache License, Version 2.0 (the "License");
  13. * you may not use this file except in compliance with the License.
  14. * You may obtain a copy of the License at
  15. *
  16. * http://www.apache.org/licenses/LICENSE-2.0
  17. *
  18. * Unless required by applicable law or agreed to in writing, software
  19. * distributed under the License is distributed on an "AS IS" BASIS,
  20. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21. * See the License for the specific language governing permissions and
  22. * limitations under the License.
  23. */
  24. #pragma once
  25. #include "TcpAgent.h"
  26. #include "HttpHelper.h"
  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 SetLocalVersion(EnHttpVersion enLocalVersion) {m_enLocalVersion = enLocalVersion;}
  57. virtual EnHttpVersion GetLocalVersion() {return m_enLocalVersion;}
  58. virtual BOOL IsUpgrade(CONNID dwConnID);
  59. virtual BOOL IsKeepAlive(CONNID dwConnID);
  60. virtual USHORT GetVersion(CONNID dwConnID);
  61. virtual ULONGLONG GetContentLength(CONNID dwConnID);
  62. virtual LPCSTR GetContentType(CONNID dwConnID);
  63. virtual LPCSTR GetContentEncoding(CONNID dwConnID);
  64. virtual LPCSTR GetTransferEncoding(CONNID dwConnID);
  65. virtual EnHttpUpgradeType GetUpgradeType(CONNID dwConnID);
  66. virtual USHORT GetParseErrorCode(CONNID dwConnID, LPCSTR* lpszErrorDesc = nullptr);
  67. virtual BOOL GetHeader(CONNID dwConnID, LPCSTR lpszName, LPCSTR* lpszValue);
  68. virtual BOOL GetHeaders(CONNID dwConnID, LPCSTR lpszName, LPCSTR lpszValue[], DWORD& dwCount);
  69. virtual BOOL GetAllHeaders(CONNID dwConnID, THeader lpHeaders[], DWORD& dwCount);
  70. virtual BOOL GetAllHeaderNames(CONNID dwConnID, LPCSTR lpszName[], DWORD& dwCount);
  71. virtual BOOL GetCookie(CONNID dwConnID, LPCSTR lpszName, LPCSTR* lpszValue);
  72. virtual BOOL GetAllCookies(CONNID dwConnID, TCookie lpCookies[], DWORD& dwCount);
  73. virtual BOOL AddCookie(CONNID dwConnID, LPCSTR lpszName, LPCSTR lpszValue, BOOL bRelpace = TRUE);
  74. virtual BOOL DeleteCookie(CONNID dwConnID, LPCSTR lpszName);
  75. virtual BOOL DeleteAllCookies(CONNID dwConnID);
  76. virtual USHORT GetStatusCode(CONNID dwConnID);
  77. virtual BOOL GetWSMessageState(CONNID dwConnID, BOOL* lpbFinal, BYTE* lpiReserved, BYTE* lpiOperationCode, LPCBYTE* lpszMask, ULONGLONG* lpullBodyLen, ULONGLONG* lpullBodyRemain);
  78. private:
  79. virtual BOOL CheckParams();
  80. virtual void PrepareStart();
  81. virtual EnHandleResult DoFireConnect(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. public:
  118. CHttpAgentT(IHttpAgentListener* pListener)
  119. : T (pListener)
  120. , m_pListener (pListener)
  121. , m_enLocalVersion (DEFAULT_HTTP_VERSION)
  122. {
  123. }
  124. virtual ~CHttpAgentT()
  125. {
  126. Stop();
  127. }
  128. private:
  129. IHttpAgentListener* m_pListener;
  130. EnHttpVersion m_enLocalVersion;
  131. CHttpObjPool m_objPool;
  132. };
  133. // ------------------------------------------------------------------------------------------------------------- //
  134. typedef CHttpAgentT<CTcpAgent, HTTP_DEFAULT_PORT> CHttpAgent;
  135. #ifdef _SSL_SUPPORT
  136. #include "SSLAgent.h"
  137. typedef CHttpAgentT<CSSLAgent, HTTPS_DEFAULT_PORT> CHttpsAgent;
  138. #endif