Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cWinINet.h
Go to the documentation of this file.
1 //
6 //
7 
8 #ifndef _INC_cWinINet_H
9 #define _INC_cWinINet_H
10 #ifndef NO_PRAGMA_ONCE
11 #pragma once
12 #endif
13 
14 #include "cNetAddr.h"
15 #include "cNetURL.h"
16 #include "../Http/cHttpCommon.h"
17 #include "GrayCore/include/cFile.h"
20 
21 #ifdef _WIN32
22 #define USE_WinInet
23 #endif
24 
25 #ifdef USE_WinInet
26 #include <WinInet.h>
27 
28 #ifdef _MFC_VER
29 #include <afxinet.h>
30 #endif
31 
32 namespace GrayLib
33 {
34  UNITTEST2_PREDEF(cWinINet);
35 
36  class cWinINetSession;
37  class cWinINetConnection;
38 
39  class GRAYLIB_LINK cWinINetHandle // : public cHandlePtr<HINTERNET>
40  {
47 
48  private:
49  HINTERNET m_h;
50 
51  public:
52  cWinINetHandle(HINTERNET h = HANDLE_NULL) noexcept
53  : m_h(h)
54  {
55  }
56  ~cWinINetHandle()
57  {
58  Close();
59  }
60 
61  void Close();
62 
63  HINTERNET get_Handle() const noexcept
64  {
65  return m_h;
66  }
67  bool isValidHandle() const noexcept
68  {
69  return m_h != HANDLE_NULL ;
70  }
71 
72  void AttachHandle(HINTERNET h)
73  {
74  if (m_h != h)
75  {
76  Close();
77  m_h = h;
78  }
79  }
80 
81  bool QueryOption(DWORD dwOption, void* pData, OUT DWORD* pdwDataSize)
82  {
85  return ::InternetQueryOption(get_Handle(), dwOption, pData, pdwDataSize);
86  }
87  bool SetOption(DWORD dwOption, void* pData, DWORD dwDataSize)
88  {
90  return ::InternetSetOption(get_Handle(), dwOption, pData, dwDataSize);
91  }
92  };
93 
94  class GRAYLIB_LINK cWinINetBase : public cWinINetHandle
95  {
98 
99  friend class cWinINetSession;
100  typedef cWinINetHandle SUPER_t;
101 
102  public:
103  virtual cWinINetBase* GetParent() const noexcept = 0;
104  virtual cWinINetSession* GetSession() const noexcept = 0;
105 
106  protected:
107  cWinINetBase() noexcept
108  {
109  }
110  virtual ~cWinINetBase()
111  {
113  ASSERT(!isValidHandle());
114  }
115  virtual void Close()
116  {
117  SUPER_t::Close();
118  }
119  };
120 
121  //**************************************************************************
122 
123  class GRAYLIB_LINK cWinINetFile
124  : public cWinINetBase
125  , public cRefBase
126  , public cStream
127  {
131  typedef cWinINetBase SUPER_t;
132 
133  public:
134  cRefPtr<cWinINetConnection> m_pConnection;
135 
136  protected:
137  cWinINetFile(cWinINetConnection* pConnection);
138  virtual ~cWinINetFile();
139 
140  public:
141  virtual cWinINetBase* GetParent() const noexcept;
142  virtual cWinINetSession* GetSession() const noexcept;
143 
144  virtual HRESULT ReadX(void* pData, size_t nDataSize) override;
145  virtual HRESULT WriteX(const void* pData, size_t nDataSize) override;
146  };
147 
148  class GRAYLIB_LINK cHttpRequest : public cWinINetFile
149  {
152  typedef cWinINetFile SUPER_t;
153  public:
154  cHttpRequest(cWinINetConnection* pConnection, HTTPVERB_TYPE nVerb, const GChar_t* pstrObjectName,
155  const GChar_t* pszReferrer = nullptr,
156  const GChar_t** ppszAcceptTypes = nullptr,
157  DWORD dwFlags = INTERNET_FLAG_EXISTING_CONNECT | INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_NO_UI);
158  virtual ~cHttpRequest()
159  {
160  Close();
161  }
162 
163  bool AddRequestHeaders(const GChar_t* pstrHeaders, StrLen_t dwHeadersLen, DWORD dwModifiers);
164  bool SendRequest(const GChar_t* pstrHeaders = nullptr, StrLen_t dwHeadersLen = 0, LPVOID lpOptional = nullptr, DWORD dwOptionalLen = 0);
165  bool SendRequestEx(LPINTERNET_BUFFERS lpBuffersIn);
166  // void AddRequestHeaders(const GChar_t* pszHeaders);
167  bool QueryInfoStatusCode(DWORD& dwStatusCode);
168  bool QueryInfo(DWORD dwInfoLevel, cString& str, DWORD* pdwIndex = nullptr) const;
169  };
170  typedef cRefPtr<cHttpRequest> cHttpRequestPtr;
171 
172  class GRAYLIB_LINK cFtpRequest : public cWinINetFile
173  {
176  typedef cWinINetFile SUPER_t;
177  public:
178  cFtpRequest(cWinINetConnection* pConnection) noexcept
179  : cWinINetFile(pConnection)
180  {
181  }
182  virtual ~cFtpRequest()
183  {
184  Close();
185  }
186  };
187 
188  //**************************************************************************
189 
190  class GRAYLIB_LINK cWinINetConnection
191  : public cWinINetBase
192  , public cRefBase
193  {
197 
198  typedef cWinINetBase SUPER_t;
199  friend class cWinINetSession;
200 
201  protected:
202  cWinINetSession* m_pSession;
203  int m_eConnectState;
204 
205  CString m_strServerName;
206  NET_PORT_t m_wPort;
207 
208  public:
209  cWinINetConnection(cWinINetSession* pSession, const GChar_t* pstrServer,
210  NET_PORT_t nPort = k_NET_PORT_INVALID);
211  virtual ~cWinINetConnection();
212 
213  virtual cWinINetBase* GetParent() const noexcept override;
214  virtual cWinINetSession* GetSession() const noexcept override
215  {
217  return m_pSession; // get my parent.
218  }
219  CString GetServerName() const noexcept
220  {
222  return m_strServerName;
223  }
224  bool isConnected() const noexcept;
225  };
226 
227  class GRAYLIB_LINK cHttpConnection : public cWinINetConnection
228  {
232  typedef cWinINetConnection SUPER_t;
233  public:
234  cHttpConnection(cWinINetSession* pSession,
235  const GChar_t* pstrServer, NET_PORT_t nPort = k_NET_PORT_INVALID,
236  const GChar_t* pstrUserName = nullptr, const GChar_t* pstrPassword = nullptr);
237  virtual ~cHttpConnection()
238  {
239  Close();
240  }
241 
242  cHttpRequestPtr OpenRequest(HTTPVERB_TYPE nVerb, const GChar_t* pstrObjectName,
243  const GChar_t* pstrReferer = nullptr,
244  const GChar_t** ppstrAcceptTypes = nullptr,
245  DWORD dwFlags = INTERNET_FLAG_EXISTING_CONNECT | INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_NO_UI);
246  };
247  typedef cRefPtr<cHttpConnection> cHttpConnectionPtr;
248 
249  class GRAYLIB_LINK cFtpConnection : public cWinINetConnection
250  {
253  typedef cWinINetFile SUPER_t;
254  public:
255  cFtpConnection(cWinINetSession* pSession,
256  const GChar_t* pstrServer)
257  : cWinINetConnection(pSession, pstrServer)
258  {}
259  virtual ~cFtpConnection()
260  {
261  Close();
262  }
263  };
264 
265  //**************************************************************************
266 
267  class GRAYLIB_LINK cWinINetSession : public cWinINetBase
268  {
274 
275  typedef cWinINetBase SUPER_t;
276  friend class cWinINetConnection;
277  friend class cWinINetFile;
278 
279  public:
280  bool m_bCallbackEnabled;
281 
282  private:
283  static void CALLBACK OnStatusCallbackStatic(HINTERNET hInternet, DWORD_PTR dwContext, DWORD dwInternetStatus,
284  LPVOID lpvStatusInformation, DWORD dwStatusInformationLength); // static
285 
286  public:
287  /* explicit */ cWinINetSession(const GChar_t* pstrAgent = nullptr,
288  DWORD dwAccessType = PRE_CONFIG_INTERNET_ACCESS,
289  const GChar_t* pstrProxyName = nullptr,
290  const GChar_t* pstrProxyBypass = nullptr,
291  DWORD dwFlags = 0);
292  virtual ~cWinINetSession();
293 
294  cHttpConnectionPtr GetHttpConnection(const GChar_t* pstrServer,
295  NET_PORT_t nPort = k_NET_PORT_INVALID,
296  const GChar_t* pstrUserName = nullptr, const GChar_t* pstrPassword = nullptr);
297 
298  virtual cWinINetBase* GetParent() const noexcept override
299  {
300  return nullptr;
301  }
302  virtual cWinINetSession* GetSession() const noexcept override
303  {
304  return const_cast<cWinINetSession*>(this);
305  }
306 
307  bool EnableStatusCallback(bool bEnable = true);
308  virtual void Close() override;
309 
310  static HRESULT GRAYCALL GetProxyConfig(cString& rsProxy, cString& rsProxyBypass);
311 
312  // Overridables
313  virtual void OnStatusCallback(cWinINetBase* pContext, DWORD dwInternetStatus,
314  LPVOID lpvStatusInformation, DWORD dwStatusInformationLength);
315 
316  UNITTEST_FRIEND(cWinINet);
317  };
318 
319  class GRAYLIB_LINK cWinINetCopier
320  : public cWinINetSession
321  , public IFileCopier
322  {
325  typedef cWinINetSession SUPER_t;
326 
327  public:
328  // cRefPtr<cWinINetConnection> m_pConnection; //!< the connection to the HTTP/FTP server.
329  cRefPtr<cHttpConnection> m_pConnection;
330  cNetURL m_url;
331 
332  public:
333  cWinINetCopier(const GChar_t* pszAgent = _GT(GRAY_NAMES), DWORD dwAccessType = PRE_CONFIG_INTERNET_ACCESS);
334  virtual ~cWinINetCopier();
335 
336  bool isConnected() const
337  {
338  if (m_pConnection == nullptr)
339  return false;
340  return m_pConnection->isConnected();
341  }
342 
343  void Close();
344 
345  // IFileCopier
346  virtual cStringA get_ConnectName() const override;
347  virtual HRESULT Connect(const FILECHAR_t* pszDevice) override;
348  virtual HRESULT RequestFile(const FILECHAR_t* pszSrcName, const FILECHAR_t* pszDestPath, IStreamProgressCallback* pProgress, FILE_SIZE_t nOffsetStart, FILE_SIZE_t* pnRequestSizeEst) override;
349  virtual HRESULT SendFile(const FILECHAR_t* pszSrcPath, const FILECHAR_t* pszDestName, IStreamProgressCallback* pProgress, FILE_SIZE_t nOffsetStart, FILE_SIZE_t nSize) override;
350 
351  // cWinINetSession
352  virtual void OnStatusCallback(cWinINetBase* pContext, DWORD dwInternetStatus,
353  LPVOID lpvStatusInformation, DWORD dwStatusInformationLength) override;
354  };
355 }
356 #endif
357 #endif
#define GRAYCALL
declare calling convention for static functions so everyone knows the arg passing scheme....
Definition: GrayCore.h:36
#define GRAY_NAMES
Use GRAYNAME for string.
Definition: GrayCore.h:34
#define GRAYLIB_LINK
Definition: GrayLibBase.h:35
#define _GT(x)
like _T(x) macro for static text.
Definition: StrConst.h:27
INT32 HRESULT
_WIN32 style error codes. INT32
Definition: SysTypes.h:465
#define ASSERT(exp)
Definition: cDebugAssert.h:87
#define HANDLE_NULL
Invalid OS handle for _WIN32. Not invalid OS handle for linux.
Definition: cOSHandle.h:21
#define UNITTEST_FRIEND(n)
Define this in the class body to be unit tested. Allow the unit test to access private/protected stuf...
Definition: cUnitTestDecl.h:17
Definition: cMesh.h:22
WORD NET_PORT_t
WinINet calls ports NET_PORT_t INTERNET_PORT = a service.
Definition: cNetPort.h:21
HTTPVERB_TYPE
Definition: cHttpCommon.h:36
UNITTEST2_PREDEF(cQuadtree)
int StrLen_t
the length of a string in chars (bytes for UTF8, wchar_t for UNICODE). or offset in characters....
Definition: StrConst.h:32
char FILECHAR_t
a UTF8 char in a file name. like TCHAR
Definition: FileName.h:22
cStringT< char > cStringA
Definition: cString.h:635
char GChar_t
My version of TCHAR, _TCHAR.
Definition: StrConst.h:26
cString CString
Definition: cString.h:639
UINT64 FILE_SIZE_t
similar to STREAM_POS_t size_t
Definition: cFileStatus.h:31
cStringT< GChar_t > cString
Definition: cString.h:636