You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

407 lines
14 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: inetstl/network/session.hpp (originally MIntSess.h)
  3. *
  4. * Purpose: Contains the basic_session class.
  5. *
  6. * Created: 30th April 1999
  7. * Updated: 10th August 2009
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 1999-2009, Matthew Wilson and Synesis Software
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright notice, this
  18. * list of conditions and the following disclaimer.
  19. * - Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
  23. * any contributors may be used to endorse or promote products derived from
  24. * this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  30. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  32. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. * ////////////////////////////////////////////////////////////////////// */
  39. /** \file inetstl/network/session.hpp
  40. *
  41. * \brief [C++ only] Definition of the inetstl::basic_session class
  42. * template
  43. * (\ref group__library__network "Network" Library).
  44. */
  45. #ifndef INETSTL_INCL_INETSTL_NETWORK_HPP_SESSION
  46. #define INETSTL_INCL_INETSTL_NETWORK_HPP_SESSION
  47. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  48. # define INETSTL_VER_INETSTL_NETWORK_HPP_SESSION_MAJOR 5
  49. # define INETSTL_VER_INETSTL_NETWORK_HPP_SESSION_MINOR 1
  50. # define INETSTL_VER_INETSTL_NETWORK_HPP_SESSION_REVISION 3
  51. # define INETSTL_VER_INETSTL_NETWORK_HPP_SESSION_EDIT 61
  52. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  53. /* /////////////////////////////////////////////////////////////////////////
  54. * Includes
  55. */
  56. #ifndef INETSTL_INCL_INETSTL_H_INETSTL
  57. # include <inetstl/inetstl.h>
  58. #endif /* !INETSTL_INCL_INETSTL_H_INETSTL */
  59. #ifndef INETSTL_OS_IS_WINDOWS
  60. # error This file is currently compatible only with the Win32/Win64 API
  61. #endif /* !INETSTL_OS_IS_WINDOWS */
  62. #ifndef INETSTL_INCL_INETSTL_FILESYSTEM_HPP_FILESYSTEM_TRAITS
  63. # include <inetstl/filesystem/filesystem_traits.hpp>
  64. #endif /* !INETSTL_INCL_INETSTL_FILESYSTEM_HPP_FILESYSTEM_TRAITS */
  65. #ifdef STLSOFT_CF_EXCEPTION_SUPPORT
  66. # ifndef INETSTL_INCL_INETSTL_ERROR_HPP_EXCEPTIONS
  67. # include <inetstl/error/exceptions.hpp> // for throw_internet_exception_policy
  68. # endif /* !INETSTL_INCL_INETSTL_ERROR_HPP_EXCEPTIONS */
  69. #else /* ? STLSOFT_CF_EXCEPTION_SUPPORT */
  70. # ifndef STLSOFT_INCL_STLSOFT_ERROR_HPP_EXCEPTIONS
  71. # include <stlsoft/error/exceptions.hpp> // for stlsoft::null_exception_policy
  72. # endif /* !STLSOFT_INCL_STLSOFT_ERROR_HPP_EXCEPTIONS */
  73. #endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
  74. /* /////////////////////////////////////////////////////////////////////////
  75. * Namespace
  76. */
  77. #ifndef _INETSTL_NO_NAMESPACE
  78. # if defined(_STLSOFT_NO_NAMESPACE) || \
  79. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  80. /* There is no stlsoft namespace, so must define ::inetstl */
  81. namespace inetstl
  82. {
  83. # else
  84. /* Define stlsoft::inetstl_project */
  85. namespace stlsoft
  86. {
  87. namespace inetstl_project
  88. {
  89. # endif /* _STLSOFT_NO_NAMESPACE */
  90. #endif /* !_INETSTL_NO_NAMESPACE */
  91. /* /////////////////////////////////////////////////////////////////////////
  92. * Classes
  93. */
  94. /** \brief Represents an internet session
  95. *
  96. * \ingroup group__library__network
  97. *
  98. * \note A session is required for WinInet, as it represents an initialisation of the WinInet libraries
  99. */
  100. template< ss_typename_param_k C
  101. #ifdef STLSOFT_CF_EXCEPTION_SUPPORT
  102. , ss_typename_param_k X = throw_internet_exception_policy
  103. #else /* ? STLSOFT_CF_EXCEPTION_SUPPORT */
  104. , ss_typename_param_k X = stlsoft_ns_qual(null_exception_policy)
  105. #endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
  106. , ss_typename_param_k T = filesystem_traits<C>
  107. >
  108. class basic_session
  109. {
  110. public:
  111. typedef C char_type;
  112. typedef X exception_policy_type;
  113. typedef ss_typename_param_k exception_policy_type::thrown_type thrown_type;
  114. typedef T traits_type;
  115. typedef basic_session<C, X, T> class_type;
  116. typedef HINTERNET resource_type;
  117. // Construction
  118. public:
  119. /// Default constructor
  120. ///
  121. /// \note A session is created with default arguments
  122. ///
  123. /// \note If the session fails to initialise, the exception_policy_type function-call operator is called. If the
  124. /// policy throws an exception, then that exception is thrown. If the policy is "null exception" policy type, then
  125. /// the session object will be constructed in a closed state.
  126. basic_session();
  127. /// Create a session with the given arguments
  128. ///
  129. /// \param pcszAgent The name of the user agent, e.g. "Acme Internet Browser"
  130. /// \param accessType The type of access required, e.g. INTERNET_OPEN_TYPE_DIRECT, INTERNET_OPEN_TYPE_PRECONFIG. Defaults to INTERNET_OPEN_TYPE_PRECONFIG
  131. /// \param pcszProxyName The name of the proxy server to use. Defaults to NULL
  132. /// \param pcszProxyBypass List of names not be routed by the proxy. Defaults to NULL
  133. /// \param flags Flags altering behaviour of the session initialisation.
  134. ///
  135. /// \note If the session fails to initialise, the exception_policy_type function-call operator is called. If the
  136. /// policy throws an exception, then that exception is thrown. If the policy is "null exception" policy type, then
  137. /// the session object will be constructed in a closed state.
  138. ss_explicit_k basic_session( char_type const* pcszAgent
  139. , is_dword_t accessType = INTERNET_OPEN_TYPE_PRECONFIG
  140. , char_type const* pcszProxyName = NULL
  141. , char_type const* pcszProxyBypass = NULL
  142. , is_dword_t flags = 0);
  143. /// Closes the session, if open
  144. ~basic_session() stlsoft_throw_0();
  145. // Operations
  146. public:
  147. /// Open a session with the given arguments
  148. ///
  149. /// \param pcszAgent The name of the user agent, e.g. "Acme Internet Browser"
  150. /// \param accessType The type of access required, e.g. INTERNET_OPEN_TYPE_DIRECT, INTERNET_OPEN_TYPE_PRECONFIG. Defaults to INTERNET_OPEN_TYPE_PRECONFIG
  151. /// \param pcszProxyName The name of the proxy server to use. Defaults to NULL
  152. /// \param pcszProxyBypass List of names not be routed by the proxy. Defaults to NULL
  153. /// \param flags Flags altering behaviour of the session initialisation.
  154. ///
  155. /// \note If the session fails to initialise, the exception_policy_type function-call operator is called. If the
  156. /// policy throws an exception, then that exception is thrown. If the policy is "null exception" policy type, then
  157. /// the session object will be left in a closed state.
  158. is_bool_t open( char_type const* pcszAgent
  159. , is_dword_t accessType = INTERNET_OPEN_TYPE_PRECONFIG
  160. , char_type const* pcszProxyName = NULL
  161. , char_type const* pcszProxyBypass = NULL
  162. , is_dword_t flags = 0);
  163. /// Closes the session, if open
  164. void close();
  165. /// Removes the session from this object, and returns the underlying WinInet handle to the
  166. /// caller, who must take responsibility for releasing it.
  167. HINTERNET detach();
  168. // State
  169. public:
  170. /// Indicates whether the object is managing an open session
  171. is_bool_t is_open() const;
  172. /// The underlying WinInet handle
  173. HINTERNET get() const;
  174. /// Implicit conversion operator to a WinInet handle
  175. ///
  176. /// \deprecated This will be removed in a future release
  177. operator HINTERNET ();
  178. // Implementation
  179. private:
  180. static char_type const *null_string_();
  181. // Members
  182. private:
  183. HINTERNET m_hConn;
  184. // Not to be implemented
  185. private:
  186. basic_session(class_type const&);
  187. class_type& operator =(class_type const&);
  188. };
  189. /* /////////////////////////////////////////////////////////////////////////
  190. * Typedefs for commonly encountered types
  191. */
  192. /** \brief Specialisation of the basic_session template for the ANSI character type \c char
  193. *
  194. * \ingroup group__library__network
  195. *
  196. */
  197. typedef basic_session<is_char_a_t> session_a;
  198. /** \brief Specialisation of the basic_session template for the Unicode character type \c wchar_t
  199. *
  200. * \ingroup group__library__network
  201. *
  202. */
  203. typedef basic_session<is_char_w_t> session_w;
  204. /** \brief Specialisation of the basic_session template for the Win32 character type \c TCHAR
  205. *
  206. * \ingroup group__library__network
  207. *
  208. */
  209. typedef basic_session<TCHAR> session;
  210. /* ////////////////////////////////////////////////////////////////////// */
  211. template< ss_typename_param_k C
  212. , ss_typename_param_k X
  213. , ss_typename_param_k T
  214. >
  215. inline /* static */ ss_typename_type_ret_k basic_session<C, X, T>::char_type const* basic_session<C, X, T>::null_string_()
  216. {
  217. static char_type s_null[1] = { 0 };
  218. return s_null;
  219. }
  220. template< ss_typename_param_k C
  221. , ss_typename_param_k X
  222. , ss_typename_param_k T
  223. >
  224. inline basic_session<C, X, T>::basic_session()
  225. : m_hConn(traits_type::internet_open(null_string_(), INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0))
  226. {
  227. if(NULL == m_hConn)
  228. {
  229. exception_policy_type()("Failed to create session", ::GetLastError());
  230. }
  231. }
  232. template< ss_typename_param_k C
  233. , ss_typename_param_k X
  234. , ss_typename_param_k T
  235. >
  236. inline basic_session<C, X, T>::basic_session( char_type const* pcszAgent
  237. , is_dword_t accessType
  238. , char_type const* pcszProxyName
  239. , char_type const* pcszProxyBypass
  240. , is_dword_t flags)
  241. : m_hConn(traits_type::internet_open(pcszAgent, accessType, pcszProxyName, pcszProxyBypass, flags))
  242. {
  243. if(NULL == m_hConn)
  244. {
  245. exception_policy_type()("Failed to create session", ::GetLastError());
  246. }
  247. }
  248. template< ss_typename_param_k C
  249. , ss_typename_param_k X
  250. , ss_typename_param_k T
  251. >
  252. inline basic_session<C, X, T>::~basic_session() stlsoft_throw_0()
  253. {
  254. if(NULL != m_hConn)
  255. {
  256. ::InternetCloseHandle(m_hConn);
  257. }
  258. }
  259. template< ss_typename_param_k C
  260. , ss_typename_param_k X
  261. , ss_typename_param_k T
  262. >
  263. inline is_bool_t basic_session<C, X, T>::open( char_type const* pcszAgent
  264. , is_dword_t accessType
  265. , char_type const* pcszProxyName
  266. , char_type const* pcszProxyBypass
  267. , is_dword_t flags)
  268. {
  269. is_bool_t bRet;
  270. if(is_open())
  271. {
  272. bRet = false;
  273. }
  274. else
  275. {
  276. m_hConn = traits_type::internet_open(pcszAgent, accessType, pcszProxyName, pcszProxyBypass, flags);
  277. if(NULL == m_hConn)
  278. {
  279. exception_policy_type()("Failed to create session", ::GetLastError());
  280. bRet = false;
  281. }
  282. else
  283. {
  284. bRet = true;
  285. }
  286. }
  287. return bRet;
  288. }
  289. template< ss_typename_param_k C
  290. , ss_typename_param_k X
  291. , ss_typename_param_k T
  292. >
  293. inline void basic_session<C, X, T>::close()
  294. {
  295. if(NULL != m_hConn)
  296. {
  297. ::InternetCloseHandle(m_hConn);
  298. m_hConn = NULL;
  299. }
  300. }
  301. template< ss_typename_param_k C
  302. , ss_typename_param_k X
  303. , ss_typename_param_k T
  304. >
  305. inline HINTERNET basic_session<C, X, T>::detach()
  306. {
  307. INETSTL_MESSAGE_ASSERT("Attempting to detach from an empty session", is_open());
  308. HINTERNET hConn = m_hConn;
  309. m_hConn = NULL;
  310. return hConn;
  311. }
  312. template< ss_typename_param_k C
  313. , ss_typename_param_k X
  314. , ss_typename_param_k T
  315. >
  316. inline is_bool_t basic_session<C, X, T>::is_open() const
  317. {
  318. return NULL != m_hConn;
  319. }
  320. template< ss_typename_param_k C
  321. , ss_typename_param_k X
  322. , ss_typename_param_k T
  323. >
  324. inline HINTERNET basic_session<C, X, T>::get() const
  325. {
  326. return m_hConn;
  327. }
  328. template< ss_typename_param_k C
  329. , ss_typename_param_k X
  330. , ss_typename_param_k T
  331. >
  332. inline basic_session<C, X, T>::operator HINTERNET ()
  333. {
  334. return m_hConn;
  335. }
  336. /* ////////////////////////////////////////////////////////////////////// */
  337. template< ss_typename_param_k C
  338. , ss_typename_param_k X
  339. , ss_typename_param_k T
  340. >
  341. inline HINTERNET get_handle(basic_session<C, X, T> &s)
  342. {
  343. return s;
  344. }
  345. /* ////////////////////////////////////////////////////////////////////// */
  346. #ifndef _INETSTL_NO_NAMESPACE
  347. # if defined(_STLSOFT_NO_NAMESPACE) || \
  348. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  349. } // namespace inetstl
  350. # else
  351. } // namespace inetstl_project
  352. } // namespace stlsoft
  353. # endif /* _STLSOFT_NO_NAMESPACE */
  354. #endif /* !_INETSTL_NO_NAMESPACE */
  355. /* ////////////////////////////////////////////////////////////////////// */
  356. #endif /* INETSTL_INCL_INETSTL_NETWORK_HPP_SESSION */
  357. /* ///////////////////////////// end of file //////////////////////////// */