Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cWinHttp.h
Go to the documentation of this file.
1 //
4 // Wrap HTTP, Proxy and SSL into a single API.
5 // Similar to OpenSSL and CInternetSession for MFC.
6 //
7 
8 #ifndef _INC_cWinHttp_H
9 #define _INC_cWinHttp_H
10 #ifndef NO_PRAGMA_ONCE
11 #pragma once
12 #endif
13 
14 #include "../Net/cNetAddr.h"
15 #include "../Http/cHttpCommon.h"
16 #include "GrayCore/include/cFile.h"
17 
18 #ifdef _WIN32
19 //#define USE_WinHttp // use Winhttp.dll and Winhttp.lib
20 #endif
21 
22 #ifdef USE_WinHttp
23 #include <WinHttp.h>
24 
25 namespace GrayLib
26 {
27  class cWinHttpSession;
28  class cWinHttpConnection;
29 
30  class GRAYLIB_LINK cWinHttpBase // : public cHandlePtr<>
31  {
36 
37  private:
38  HINTERNET m_h;
39 
40  protected:
41  cWinHttpBase()
42  : m_h(INVALID_HANDLE_VALUE)
43  {
44  }
45  ~cWinHttpBase()
46  {
47  }
48  void Close();
49 
50  void AttachHandle(HINTERNET h)
51  {
52  if (m_h != h)
53  {
54  Close();
55  m_h = h;
56  }
57  }
58 
59  public:
60  bool isValidHandle() const
61  {
62  return m_h != HANDLE_NULL && m_h != INVALID_HANDLE_VALUE ;
63  }
64  HINTERNET get_Handle() const
65  {
66  return m_h;
67  }
68  virtual cWinHttpSession* GetSession() const = 0;
69  };
70 
71  //**************************************************************************
72 
73  class GRAYLIB_LINK cWinHttpRequest
74  : public cWinHttpBase
75  , public cRefBase // , public cStream
76  {
79  typedef cWinHttpBase SUPER_t;
80  public:
81  cWinHttpRequest(cWinHttpConnection* pConnection, HTTPVERB_TYPE nVerb, LPCWSTR pstrObjectName,
82  IN LPCWSTR pwszReferrer, IN LPCWSTR FAR * ppwszAcceptTypes, DWORD dwFlags = 0);
83  virtual cWinHttpSession* GetSession() const;
84 
85  bool SendRequest();
86  bool ReceiveResponse();
87 
88  public:
89  cWinHttpConnection* m_pConnection; // my parent connection.
90  };
91  typedef cRefPtr<cWinHttpRequest> cWinHttpRequestPtr;
92 
93  //**************************************************************************
94 
95  class GRAYLIB_LINK cWinHttpConnection
96  : public cWinHttpBase
97  , public cRefBase
98  {
100  // Base class for a Managed connection to the Internet.
101 
102  typedef cWinHttpBase SUPER_t;
103  public:
104  cWinHttpConnection(cWinHttpSession* pSession,
105  LPCWSTR pstrServer, NET_PORT_t nPort = k_NET_PORT_INVALID,
106  DWORD dwFlags = 0);
107  ~cWinHttpConnection();
108 
109  cWinHttpRequestPtr OpenRequest(HTTPVERB_TYPE nVerb, LPCWSTR pstrObjectName, IN LPCWSTR pwszReferrer, IN LPCWSTR FAR * ppwszAcceptTypes, DWORD dwFlags = 0);
110 
111  virtual cWinHttpSession* GetSession() const
112  {
113  // MFC like
114  return m_pSession; // get my parent.
115  }
116  CString GetServerName() const
117  {
118  // MFC like
119  return m_strServerName;
120  }
121 
122  protected:
123  cWinHttpSession* m_pSession; // my parent session.
124  CString m_strServerName; // the server URL I'm connected to.
125  NET_PORT_t m_wPort;
126  };
127 
128  typedef cRefPtr<cWinHttpConnection> cWinHttpConnectionPtr;
129 
130  //**************************************************************************
131 
132  class GRAYLIB_LINK cWinHttpSession : public cWinHttpBase
133  {
135  // A session can contain connections which then can be queried for files.
136  // All status callbacks come via the cWinHttpSession.
137  // Add Normal sockets and UDP to go across the available proxy/tunnel.
138  typedef cWinHttpBase SUPER_t;
139  friend class cWinHttpRequest;
140  friend class cWinHttpSession;
141 
142  public:
143  /* explicit */ cWinHttpSession(LPCWSTR pstrAgent = nullptr,
144  DWORD dwAccessType = WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
145  IN LPCWSTR pwszProxyName = WINHTTP_NO_PROXY_NAME,
146  IN LPCWSTR pwszProxyBypass = WINHTTP_NO_PROXY_BYPASS,
147  IN DWORD dwFlags = 0);
148  ~cWinHttpSession();
149 
150  cWinHttpConnectionPtr GetHttpConnection(LPCWSTR pstrServer,
151  NET_PORT_t nPort = k_NET_PORT_INVALID, DWORD dwFlags = 0);
152 
153  bool EnableStatusCallback(bool bEnable = true);
154 
155  virtual cWinHttpSession* GetSession() const
156  {
157  return const_cast<cWinHttpSession*>(this);
158  }
159 
160  // Overridables
161  virtual void OnStatusCallback(cWinHttpBase* pContext, DWORD dwInternetStatus,
162  LPVOID lpvStatusInformation, DWORD dwStatusInformationLength);
163 
164  UNITTEST2_PREDEF(cWinHttp );
165 
166  protected:
167  // WINHTTP_STATUS_CALLBACK
168  static void CALLBACK OnStatusCallbackStatic(IN HINTERNET hInternet,
169  IN DWORD_PTR dwContext,
170  IN DWORD dwInternetStatus,
171  IN LPVOID lpvStatusInformation OPTIONAL,
172  IN DWORD dwStatusInformationLength);
173 
174  public:
175  bool m_bCallbackEnabled;
176  };
177 
178  class GRAYLIB_LINK cWinHttpCopier
179  : public cWinHttpSession
180  , public IFileCopier
181  {
184  typedef cWinHttpSession SUPER_t;
185  public:
186  cWinHttpCopier();
187  ~cWinHttpCopier();
188 
189  // IFileCopier
190  virtual cStringA get_ConnectName() const override;
191  virtual HRESULT RequestFile(const FILECHAR_t* pszSrcName, const FILECHAR_t* pszDestPath, IStreamProgressCallback* pProgress, FILE_SIZE_t nOffsetStart, FILE_SIZE_t* pnRequestSizeEst) override;
192  virtual HRESULT SendFile(const FILECHAR_t* pszSrcPath, const FILECHAR_t* pszDestName, IStreamProgressCallback* pProgress, FILE_SIZE_t nOffsetStart, FILE_SIZE_t nSizet) override;
193 
194  virtual void OnStatusCallback(cWinHttpBase* pContext, DWORD dwInternetStatus,
195  LPVOID lpvStatusInformation, DWORD dwStatusInformationLength);
196  };
197 };
198 #endif
199 #endif
#define GRAYLIB_LINK
Definition: GrayLibBase.h:35
INT32 HRESULT
_WIN32 style error codes. INT32
Definition: SysTypes.h:465
#define HANDLE_NULL
Invalid OS handle for _WIN32. Not invalid OS handle for linux.
Definition: cOSHandle.h:21
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)
char FILECHAR_t
a UTF8 char in a file name. like TCHAR
Definition: FileName.h:22
cStringT< char > cStringA
Definition: cString.h:635
cString CString
Definition: cString.h:639
UINT64 FILE_SIZE_t
similar to STREAM_POS_t size_t
Definition: cFileStatus.h:31