HttpAgent.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright: JessMA Open Source (ldcsaa@gmail.com)
  3. *
  4. * Version : 3.6.1
  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> class CHttpAgentT : public IComplexHttpRequester, public T
  28. {
  29. private:
  30. typedef THttpObjT<CHttpAgentT, TSocketObj> THttpObj;
  31. friend struct THttpObj;
  32. public:
  33. virtual BOOL SendRequest(CONNID dwConnID, LPCSTR lpszMethod, LPCSTR lpszPath, const THeader lpHeaders[] = nullptr, int iHeaderCount = 0, const BYTE* pBody = nullptr, int iLength = 0);
  34. virtual BOOL SendPost(CONNID dwConnID, LPCSTR lpszPath, const THeader lpHeaders[], int iHeaderCount, const BYTE* pBody, int iLength)
  35. {return SendRequest(dwConnID, HTTP_METHOD_POST, lpszPath, lpHeaders, iHeaderCount, pBody, iLength);}
  36. virtual BOOL SendPut(CONNID dwConnID, LPCSTR lpszPath, const THeader lpHeaders[], int iHeaderCount, const BYTE* pBody, int iLength)
  37. {return SendRequest(dwConnID, HTTP_METHOD_PUT, lpszPath, lpHeaders, iHeaderCount, pBody, iLength);}
  38. virtual BOOL SendPatch(CONNID dwConnID, LPCSTR lpszPath, const THeader lpHeaders[], int iHeaderCount, const BYTE* pBody, int iLength)
  39. {return SendRequest(dwConnID, HTTP_METHOD_PATCH, lpszPath, lpHeaders, iHeaderCount, pBody, iLength);}
  40. virtual BOOL SendGet(CONNID dwConnID, LPCSTR lpszPath, const THeader lpHeaders[] = nullptr, int iHeaderCount = 0)
  41. {return SendRequest(dwConnID, HTTP_METHOD_GET, lpszPath, lpHeaders, iHeaderCount);}
  42. virtual BOOL SendDelete(CONNID dwConnID, LPCSTR lpszPath, const THeader lpHeaders[] = nullptr, int iHeaderCount = 0)
  43. {return SendRequest(dwConnID, HTTP_METHOD_DELETE, lpszPath, lpHeaders, iHeaderCount);}
  44. virtual BOOL SendHead(CONNID dwConnID, LPCSTR lpszPath, const THeader lpHeaders[] = nullptr, int iHeaderCount = 0)
  45. {return SendRequest(dwConnID, HTTP_METHOD_HEAD, lpszPath, lpHeaders, iHeaderCount);}
  46. virtual BOOL SendTrace(CONNID dwConnID, LPCSTR lpszPath, const THeader lpHeaders[] = nullptr, int iHeaderCount = 0)
  47. {return SendRequest(dwConnID, HTTP_METHOD_TRACE, lpszPath, lpHeaders, iHeaderCount);}
  48. virtual BOOL SendOptions(CONNID dwConnID, LPCSTR lpszPath, const THeader lpHeaders[] = nullptr, int iHeaderCount = 0)
  49. {return SendRequest(dwConnID, HTTP_METHOD_OPTIONS, lpszPath, lpHeaders, iHeaderCount);}
  50. virtual BOOL SendConnect(CONNID dwConnID, LPCSTR lpszHost, const THeader lpHeaders[] = nullptr, int iHeaderCount = 0)
  51. {return SendRequest(dwConnID, HTTP_METHOD_CONNECT, lpszHost, lpHeaders, iHeaderCount);}
  52. public:
  53. virtual void SetLocalVersion(EnHttpVersion enLocalVersion) {m_enLocalVersion = enLocalVersion;}
  54. virtual EnHttpVersion GetLocalVersion() {return m_enLocalVersion;}
  55. virtual BOOL IsUpgrade(CONNID dwConnID);
  56. virtual BOOL IsKeepAlive(CONNID dwConnID);
  57. virtual USHORT GetVersion(CONNID dwConnID);
  58. virtual ULONGLONG GetContentLength(CONNID dwConnID);
  59. virtual LPCSTR GetContentType(CONNID dwConnID);
  60. virtual EnHttpUpgradeType GetUpgradeType(CONNID dwConnID);
  61. virtual USHORT GetParseErrorCode(CONNID dwConnID, LPCSTR* lpszErrorDesc = nullptr);
  62. virtual BOOL GetHeader(CONNID dwConnID, LPCSTR lpszName, LPCSTR* lpszValue);
  63. virtual BOOL GetHeaders(CONNID dwConnID, LPCSTR lpszName, LPCSTR lpszValue[], DWORD& dwCount);
  64. virtual BOOL GetAllHeaders(CONNID dwConnID, THeader lpHeaders[], DWORD& dwCount);
  65. virtual BOOL GetAllHeaderNames(CONNID dwConnID, LPCSTR lpszName[], DWORD& dwCount);
  66. virtual BOOL GetCookie(CONNID dwConnID, LPCSTR lpszName, LPCSTR* lpszValue);
  67. virtual BOOL GetAllCookies(CONNID dwConnID, TCookie lpCookies[], DWORD& dwCount);
  68. virtual BOOL AddCookie(CONNID dwConnID, LPCSTR lpszName, LPCSTR lpszValue, BOOL bRelpace = TRUE);
  69. virtual BOOL DeleteCookie(CONNID dwConnID, LPCSTR lpszName);
  70. virtual BOOL DeleteAllCookies(CONNID dwConnID);
  71. virtual USHORT GetStatusCode(CONNID dwConnID);
  72. private:
  73. virtual BOOL CheckParams();
  74. virtual EnHandleResult DoFireConnect(TSocketObj* pSocketObj);
  75. virtual EnHandleResult DoFireReceive(TSocketObj* pSocketObj, const BYTE* pData, int iLength);
  76. virtual EnHandleResult DoFireClose(TSocketObj* pSocketObj, EnSocketOperation enOperation, int iErrorCode);
  77. virtual EnHandleResult DoFireShutdown();
  78. EnHandleResult DoFireSuperReceive(TSocketObj* pSocketObj, const BYTE* pData, int iLength)
  79. {return __super::DoFireReceive(pSocketObj, pData, iLength);}
  80. EnHttpParseResult FireMessageBegin(TSocketObj* pSocketObj)
  81. {return m_pListener->OnMessageBegin(pSocketObj->connID);}
  82. EnHttpParseResult FireRequestLine(TSocketObj* pSocketObj, LPCSTR lpszMethod, LPCSTR lpszUrl)
  83. {return m_pListener->OnRequestLine(pSocketObj->connID, lpszMethod, lpszUrl);}
  84. EnHttpParseResult FireStatusLine(TSocketObj* pSocketObj, USHORT usStatusCode, LPCSTR lpszDesc)
  85. {return m_pListener->OnStatusLine(pSocketObj->connID, usStatusCode, lpszDesc);}
  86. EnHttpParseResult FireHeader(TSocketObj* pSocketObj, LPCSTR lpszName, LPCSTR lpszValue)
  87. {return m_pListener->OnHeader(pSocketObj->connID, lpszName, lpszValue);}
  88. EnHttpParseResult FireHeadersComplete(TSocketObj* pSocketObj)
  89. {return m_pListener->OnHeadersComplete(pSocketObj->connID);}
  90. EnHttpParseResult FireBody(TSocketObj* pSocketObj, const BYTE* pData, int iLength)
  91. {return m_pListener->OnBody(pSocketObj->connID, pData, iLength);}
  92. EnHttpParseResult FireChunkHeader(TSocketObj* pSocketObj, int iLength)
  93. {return m_pListener->OnChunkHeader(pSocketObj->connID, iLength);}
  94. EnHttpParseResult FireChunkComplete(TSocketObj* pSocketObj)
  95. {return m_pListener->OnChunkComplete(pSocketObj->connID);}
  96. EnHttpParseResult FireMessageComplete(TSocketObj* pSocketObj)
  97. {return m_pListener->OnMessageComplete(pSocketObj->connID);}
  98. EnHttpParseResult FireUpgrade(TSocketObj* pSocketObj, EnHttpUpgradeType enUpgradeType)
  99. {return m_pListener->OnUpgrade(pSocketObj->connID, enUpgradeType);}
  100. EnHttpParseResult FireParseError(TSocketObj* pSocketObj, int iErrorCode, LPCSTR lpszErrorDesc)
  101. {return m_pListener->OnParseError(pSocketObj->connID, iErrorCode, lpszErrorDesc);}
  102. inline THttpObj* FindHttpObj(CONNID dwConnID);
  103. inline THttpObj* FindHttpObj(TSocketObj* pSocketObj);
  104. public:
  105. CHttpAgentT(IHttpAgentListener* pListener)
  106. : T (pListener)
  107. , m_pListener (pListener)
  108. , m_enLocalVersion (DEFAULT_HTTP_VERSION)
  109. {
  110. }
  111. virtual ~CHttpAgentT()
  112. {
  113. Stop();
  114. }
  115. private:
  116. IHttpAgentListener* m_pListener;
  117. EnHttpVersion m_enLocalVersion;
  118. };
  119. typedef CHttpAgentT<CTcpAgent> CHttpAgent;
  120. #ifdef _SSL_SUPPORT
  121. #include "SSLAgent.h"
  122. typedef CHttpAgentT<CSSLAgent> CHttpsAgent;
  123. #endif