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.

429 lines
15 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: inetstl/network/connection.hpp (originally MIntConn.h)
  3. *
  4. * Purpose: Contains the basic_connection 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/connection.hpp
  40. *
  41. * \brief [C++ only] Definition of the inetstl::basic_connection class
  42. * template
  43. * (\ref group__library__network "Network" Library).
  44. */
  45. #ifndef INETSTL_INCL_INETSTL_NETWORK_HPP_CONNECTION
  46. #define INETSTL_INCL_INETSTL_NETWORK_HPP_CONNECTION
  47. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  48. # define INETSTL_VER_INETSTL_NETWORK_HPP_CONNECTION_MAJOR 5
  49. # define INETSTL_VER_INETSTL_NETWORK_HPP_CONNECTION_MINOR 1
  50. # define INETSTL_VER_INETSTL_NETWORK_HPP_CONNECTION_REVISION 3
  51. # define INETSTL_VER_INETSTL_NETWORK_HPP_CONNECTION_EDIT 67
  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_NETWORK_HPP_SESSION
  63. # include <inetstl/network/session.hpp>
  64. #endif /* !INETSTL_INCL_INETSTL_NETWORK_HPP_SESSION */
  65. #ifndef INETSTL_INCL_INETSTL_FILESYSTEM_HPP_FILESYSTEM_TRAITS
  66. # include <inetstl/filesystem/filesystem_traits.hpp>
  67. #endif /* !INETSTL_INCL_INETSTL_FILESYSTEM_HPP_FILESYSTEM_TRAITS */
  68. #ifdef STLSOFT_CF_EXCEPTION_SUPPORT
  69. # ifndef INETSTL_INCL_INETSTL_ERROR_HPP_EXCEPTIONS
  70. # include <inetstl/error/exceptions.hpp> // for throw_internet_exception_policy
  71. # endif /* !INETSTL_INCL_INETSTL_ERROR_HPP_EXCEPTIONS */
  72. #else /* ? STLSOFT_CF_EXCEPTION_SUPPORT */
  73. # ifndef STLSOFT_INCL_STLSOFT_ERROR_HPP_EXCEPTIONS
  74. # include <stlsoft/error/exceptions.hpp> // for stlsoft::null_exception_policy
  75. # endif /* !STLSOFT_INCL_STLSOFT_ERROR_HPP_EXCEPTIONS */
  76. #endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
  77. /* /////////////////////////////////////////////////////////////////////////
  78. * Namespace
  79. */
  80. #ifndef _INETSTL_NO_NAMESPACE
  81. # if defined(_STLSOFT_NO_NAMESPACE) || \
  82. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  83. /* There is no stlsoft namespace, so must define ::inetstl */
  84. namespace inetstl
  85. {
  86. # else
  87. /* Define stlsoft::inetstl_project */
  88. namespace stlsoft
  89. {
  90. namespace inetstl_project
  91. {
  92. # endif /* _STLSOFT_NO_NAMESPACE */
  93. #endif /* !_INETSTL_NO_NAMESPACE */
  94. /* /////////////////////////////////////////////////////////////////////////
  95. * Classes
  96. */
  97. /** \brief Represents an internet connection
  98. *
  99. * \ingroup group__library__network
  100. *
  101. */
  102. template< ss_typename_param_k C
  103. #ifdef STLSOFT_CF_EXCEPTION_SUPPORT
  104. , ss_typename_param_k X = throw_internet_exception_policy
  105. #else /* ? STLSOFT_CF_EXCEPTION_SUPPORT */
  106. , ss_typename_param_k X = stlsoft_ns_qual(null_exception_policy)
  107. #endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
  108. , ss_typename_param_k T = filesystem_traits<C>
  109. >
  110. class basic_connection
  111. {
  112. public:
  113. typedef C char_type;
  114. typedef X exception_policy_type;
  115. typedef ss_typename_param_k exception_policy_type::thrown_type thrown_type;
  116. typedef T traits_type;
  117. typedef basic_connection<C, X, T> class_type;
  118. typedef HINTERNET resource_type;
  119. // Construction
  120. public:
  121. /// Default constructor
  122. ///
  123. /// \note No connection exists. Use connect() to open a connection
  124. basic_connection();
  125. /// Create a connection with the given arguments
  126. ///
  127. /// \param hsess A WinInet handle to the current session, obtained via \c InternetOpen() or the basic_session class
  128. /// \param server The name of the server to which to connect. May not be NULL
  129. /// \param port The number of the port to connect on
  130. /// \param userName The username for the connection. May be NULL
  131. /// \param password The password for the connection. May be NULL
  132. /// \param service The service to access. Either INTERNET_SERVICE_FTP or INTERNET_SERVICE_HTTP
  133. /// \param flags Service specific flags, e.g. INTERNET_FLAG_PASSIVE
  134. /// \param context Context used for callback functions
  135. ///
  136. /// \note If the connection fails to open, the exception_policy_type function-call operator is called. If the
  137. /// policy throws an exception, then that exception is thrown. If the policy is "null exception" policy type, then
  138. /// the connection object will be constructed in a closed state.
  139. ss_explicit_k basic_connection( HINTERNET hsess
  140. , char_type const* server
  141. , INTERNET_PORT port
  142. , char_type const* userName
  143. , char_type const* password
  144. , is_dword_t service
  145. , is_dword_t flags
  146. , is_dword_t context = 0);
  147. /// Closes the connection, if open
  148. ~basic_connection() stlsoft_throw_0();
  149. // Operations
  150. public:
  151. /// Opens a connection with the given arguments
  152. ///
  153. /// \param hsess A WinInet handle to the current session, obtained via \c InternetOpen() or the basic_session class
  154. /// \param server The name of the server to which to connect. May not be NULL
  155. /// \param port The number of the port to connect on
  156. /// \param userName The username for the connection. May be NULL
  157. /// \param password The password for the connection. May be NULL
  158. /// \param service The service to access. Either INTERNET_SERVICE_FTP or INTERNET_SERVICE_HTTP
  159. /// \param flags Service specific flags, e.g. INTERNET_FLAG_PASSIVE
  160. /// \param context Context used for callback functions
  161. ///
  162. /// \note If the connection fails to open, the exception_policy_type function-call operator is called. If the
  163. /// policy throws an exception, then that exception is thrown. If the policy is "null exception" policy type, then
  164. /// the connection object will be left in a closed state.
  165. is_bool_t connect( HINTERNET hsess
  166. , char_type const* server
  167. , INTERNET_PORT port
  168. , char_type const* userName
  169. , char_type const* password
  170. , is_dword_t service
  171. , is_dword_t flags
  172. , is_dword_t context = 0);
  173. /// Closes the connection, if open
  174. void close();
  175. /// Removes the connection from this object, and returns the underlying WinInet handle to the
  176. /// caller, who must take responsibility for releasing it.
  177. HINTERNET detach();
  178. // State
  179. public:
  180. /// Indicates whether the object is managing an open connection
  181. is_bool_t is_connected() const;
  182. /// The last Win32/WinInet error associated with this connection object
  183. is_dword_t last_error() const;
  184. /// The underlying WinInet handle
  185. HINTERNET get() const;
  186. /// Implicit conversion operator to a WinInet handle
  187. ///
  188. /// \deprecated This will be removed in a future release
  189. operator HINTERNET ();
  190. // Implementation
  191. private:
  192. static char_type const *null_string_();
  193. // Members
  194. private:
  195. HINTERNET m_hConn;
  196. is_dword_t m_lastError;
  197. };
  198. /* /////////////////////////////////////////////////////////////////////////
  199. * Typedefs for commonly encountered types
  200. */
  201. /** \brief Specialisation of the basic_connection template for the ANSI character type \c char
  202. *
  203. * \ingroup group__library__network
  204. *
  205. */
  206. typedef basic_connection<is_char_a_t> connection_a;
  207. /** \brief Specialisation of the basic_connection template for the Unicode character type \c wchar_t
  208. *
  209. * \ingroup group__library__network
  210. *
  211. */
  212. typedef basic_connection<is_char_w_t> connection_w;
  213. /** \brief Specialisation of the basic_connection template for the Win32 character type \c TCHAR
  214. *
  215. * \ingroup group__library__network
  216. *
  217. */
  218. typedef basic_connection<TCHAR> connection;
  219. /* ////////////////////////////////////////////////////////////////////// */
  220. template< ss_typename_param_k C
  221. , ss_typename_param_k X
  222. , ss_typename_param_k T
  223. >
  224. inline /* static */ ss_typename_type_ret_k basic_connection<C, X, T>::char_type const* basic_connection<C, X, T>::null_string_()
  225. {
  226. static char_type s_null[1] = { 0 };
  227. return s_null;
  228. }
  229. template< ss_typename_param_k C
  230. , ss_typename_param_k X
  231. , ss_typename_param_k T
  232. >
  233. inline basic_connection<C, X, T>::basic_connection()
  234. : m_hConn(NULL)
  235. , m_lastError(ERROR_SUCCESS)
  236. {}
  237. template< ss_typename_param_k C
  238. , ss_typename_param_k X
  239. , ss_typename_param_k T
  240. >
  241. inline basic_connection<C, X, T>::basic_connection( HINTERNET hsess
  242. , char_type const* server
  243. , INTERNET_PORT port
  244. , char_type const* userName
  245. , char_type const* password
  246. , is_dword_t service
  247. , is_dword_t flags
  248. , is_dword_t context /* = 0 */)
  249. : m_hConn(traits_type::internet_connect(hsess, server, port, userName, password, service, flags, context))
  250. , m_lastError(::GetLastError())
  251. {
  252. if(NULL == m_hConn)
  253. {
  254. exception_policy_type()("Failed to open connection", m_lastError);
  255. }
  256. }
  257. template< ss_typename_param_k C
  258. , ss_typename_param_k X
  259. , ss_typename_param_k T
  260. >
  261. inline basic_connection<C, X, T>::~basic_connection() stlsoft_throw_0()
  262. {
  263. if(NULL != m_hConn)
  264. {
  265. traits_type::close_connection(m_hConn);
  266. }
  267. }
  268. template< ss_typename_param_k C
  269. , ss_typename_param_k X
  270. , ss_typename_param_k T
  271. >
  272. inline is_bool_t basic_connection<C, X, T>::connect(HINTERNET hsess
  273. , char_type const* server
  274. , INTERNET_PORT port
  275. , char_type const* userName
  276. , char_type const* password
  277. , is_dword_t service
  278. , is_dword_t flags
  279. , is_dword_t context /* = 0 */)
  280. {
  281. is_bool_t bRet;
  282. if(is_connected())
  283. {
  284. bRet = false;
  285. }
  286. else
  287. {
  288. m_hConn = traits_type::internet_connect(hsess, server, port, userName, password, service, flags, context);
  289. m_lastError = ::GetLastError();
  290. if(NULL == m_hConn)
  291. {
  292. exception_policy_type()("Failed to open connection", m_lastError);
  293. bRet = false;
  294. }
  295. else
  296. {
  297. bRet = true;
  298. }
  299. }
  300. return bRet;
  301. }
  302. template< ss_typename_param_k C
  303. , ss_typename_param_k X
  304. , ss_typename_param_k T
  305. >
  306. inline void basic_connection<C, X, T>::close()
  307. {
  308. if(NULL != m_hConn)
  309. {
  310. traits_type::close_connection(m_hConn);
  311. m_hConn = NULL;
  312. }
  313. }
  314. template< ss_typename_param_k C
  315. , ss_typename_param_k X
  316. , ss_typename_param_k T
  317. >
  318. inline HINTERNET basic_connection<C, X, T>::detach()
  319. {
  320. INETSTL_MESSAGE_ASSERT("Attempting to detach from an empty connection", is_connected());
  321. HINTERNET hConn = m_hConn;
  322. m_hConn = NULL;
  323. return hConn;
  324. }
  325. template< ss_typename_param_k C
  326. , ss_typename_param_k X
  327. , ss_typename_param_k T
  328. >
  329. inline is_bool_t basic_connection<C, X, T>::is_connected() const
  330. {
  331. return NULL != m_hConn;
  332. }
  333. template< ss_typename_param_k C
  334. , ss_typename_param_k X
  335. , ss_typename_param_k T
  336. >
  337. inline is_dword_t basic_connection<C, X, T>::last_error() const
  338. {
  339. return m_lastError;
  340. }
  341. template< ss_typename_param_k C
  342. , ss_typename_param_k X
  343. , ss_typename_param_k T
  344. >
  345. inline HINTERNET basic_connection<C, X, T>::get() const
  346. {
  347. return m_hConn;
  348. }
  349. template< ss_typename_param_k C
  350. , ss_typename_param_k X
  351. , ss_typename_param_k T
  352. >
  353. inline basic_connection<C, X, T>::operator HINTERNET ()
  354. {
  355. return m_hConn;
  356. }
  357. /* ////////////////////////////////////////////////////////////////////// */
  358. template< ss_typename_param_k C
  359. , ss_typename_param_k X
  360. , ss_typename_param_k T
  361. >
  362. inline HINTERNET get_handle(basic_connection<C, X, T> &s)
  363. {
  364. return s;
  365. }
  366. /* ////////////////////////////////////////////////////////////////////// */
  367. #ifndef _INETSTL_NO_NAMESPACE
  368. # if defined(_STLSOFT_NO_NAMESPACE) || \
  369. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  370. } // namespace inetstl
  371. # else
  372. } // namespace inetstl_project
  373. } // namespace stlsoft
  374. # endif /* _STLSOFT_NO_NAMESPACE */
  375. #endif /* !_INETSTL_NO_NAMESPACE */
  376. /* ////////////////////////////////////////////////////////////////////// */
  377. #endif /* INETSTL_INCL_INETSTL_NETWORK_HPP_CONNECTION */
  378. /* ///////////////////////////// end of file //////////////////////////// */