C++ Rest SDK
The C++ REST SDK is a Microsoft project for cloud-based client-server communication in native code using a modern asynchronous C++ API design. This project aims to help C++ developers connect to and interact with services.
web_utilities.h
1 /***
2 * ==++==
3 *
4 * Copyright (c) Microsoft Corporation. All rights reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * ==--==
17 * =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
18 *
19 * utility classes used by the different web:: clients
20 *
21 * =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
22 ****/
23 #pragma once
24 
25 #include "cpprest/asyncrt_utils.h"
26 
27 namespace web
28 {
29 
30 namespace http { namespace client { namespace details {
31 class winhttp_client;
32 class winrt_client;
33 }}}
34 namespace websockets { namespace client { namespace details {
35 class winrt_callback_client;
36 class wspp_callback_client;
37 }}}
38 
39 namespace details
40 {
41 
43 {
44 public:
45  _ASYNCRTIMP void operator()(::utility::string_t *data) const;
46 };
47 typedef std::unique_ptr<::utility::string_t, zero_memory_deleter> plaintext_string;
48 
49 #if defined(_WIN32) && !defined(CPPREST_TARGET_XP)
50 #if defined(__cplusplus_winrt)
51 class winrt_encryption
52 {
53 public:
54  winrt_encryption() {}
55  _ASYNCRTIMP winrt_encryption(const std::wstring &data);
56  _ASYNCRTIMP plaintext_string decrypt() const;
57 private:
59 };
60 #else
61 class win32_encryption
62 {
63 public:
64  win32_encryption() {}
65  _ASYNCRTIMP win32_encryption(const std::wstring &data);
66  _ASYNCRTIMP ~win32_encryption();
67  _ASYNCRTIMP plaintext_string decrypt() const;
68 private:
69  std::vector<char> m_buffer;
70  size_t m_numCharacters;
71 };
72 #endif
73 #endif
74 }
75 
81 {
82 public:
87 
93  credentials(utility::string_t username, const utility::string_t & password) :
94  m_username(std::move(username)),
95  m_password(password)
96  {}
97 
102  const utility::string_t &username() const { return m_username; }
103 
108  CASABLANCA_DEPRECATED("This API is deprecated for security reasons to avoid unnecessary password copies stored in plaintext.")
109  utility::string_t password() const
110  {
111 #if defined(_WIN32) && !defined(CPPREST_TARGET_XP)
112  return utility::string_t(*m_password.decrypt());
113 #else
114  return m_password;
115 #endif
116  }
117 
122  bool is_set() const { return !m_username.empty(); }
123 
124 private:
125  friend class http::client::details::winhttp_client;
126  friend class http::client::details::winrt_client;
127  friend class websockets::client::details::winrt_callback_client;
128  friend class websockets::client::details::wspp_callback_client;
129 
130  details::plaintext_string decrypt() const
131  {
132  // Encryption APIs not supported on XP
133 #if defined(_WIN32) && !defined(CPPREST_TARGET_XP)
134  return m_password.decrypt();
135 #else
136  return details::plaintext_string(new ::utility::string_t(m_password));
137 #endif
138  }
139 
140  ::utility::string_t m_username;
141 
142 #if defined(_WIN32) && !defined(CPPREST_TARGET_XP)
143 #if defined(__cplusplus_winrt)
144  details::winrt_encryption m_password;
145 #else
146  details::win32_encryption m_password;
147 #endif
148 #else
149  ::utility::string_t m_password;
150 #endif
151 };
152 
158 {
159  enum web_proxy_mode_internal{ use_default_, use_auto_discovery_, disabled_, user_provided_ };
160 public:
161  enum web_proxy_mode{ use_default = use_default_, use_auto_discovery = use_auto_discovery_, disabled = disabled_};
162 
166  web_proxy() : m_address(_XPLATSTR("")), m_mode(use_default_) {}
167 
172  web_proxy( web_proxy_mode mode ) : m_address(_XPLATSTR("")), m_mode(static_cast<web_proxy_mode_internal>(mode)) {}
173 
178  web_proxy( uri address ) : m_address(address), m_mode(user_provided_) {}
179 
184  const uri& address() const { return m_address; }
185 
190  const web::credentials& credentials() const { return m_credentials; }
191 
197  if( m_mode == disabled_ )
198  {
199  throw std::invalid_argument("Cannot attach credentials to a disabled proxy");
200  }
201  m_credentials = std::move(cred);
202  }
203 
208  bool is_default() const { return m_mode == use_default_; }
209 
214  bool is_disabled() const { return m_mode == disabled_; }
215 
220  bool is_auto_discovery() const { return m_mode == use_auto_discovery_; }
221 
226  bool is_specified() const { return m_mode == user_provided_; }
227 
228 private:
229  web::uri m_address;
230  web_proxy_mode_internal m_mode;
231  web::credentials m_credentials;
232 };
233 
234 }
const web::credentials & credentials() const
Gets the credentials used for authentication with this proxy.
Definition: web_utilities.h:190
Represents a set of user credentials (user name and password) to be used for authentication.
Definition: web_utilities.h:80
Definition: web_utilities.h:42
A flexible, protocol independent URI implementation.
Definition: base_uri.h:151
web_proxy()
Constructs a proxy with the default settings.
Definition: web_utilities.h:166
bool is_auto_discovery() const
Checks if the auto discovery protocol, WPAD, is to be used.
Definition: web_utilities.h:220
bool is_disabled() const
Checks if using a proxy is disabled.
Definition: web_utilities.h:214
The web namespace contains functionality common to multiple protocols like HTTP and WebSockets...
Definition: base_uri.h:37
bool is_specified() const
Checks if a proxy address is explicitly specified by the user.
Definition: web_utilities.h:226
const uri & address() const
Gets this proxy's URI address. Returns an empty URI if not explicitly set by user.
Definition: web_utilities.h:184
bool is_set() const
Checks if credentials have been set
Definition: web_utilities.h:122
web_proxy(uri address)
Creates a proxy explicitly with provided address.
Definition: web_utilities.h:178
web_proxy represents the concept of the web proxy, which can be auto-discovered, disabled, or specified explicitly by the user.
Definition: web_utilities.h:157
web_proxy(web_proxy_mode mode)
Creates a proxy with specified mode.
Definition: web_utilities.h:172
credentials(utility::string_t username, const utility::string_t &password)
Constructs credentials from given user name and password.
Definition: web_utilities.h:93
void set_credentials(web::credentials cred)
Sets the credentials to use for authentication with this proxy.
Definition: web_utilities.h:196
The Parallel Patterns Library (PPL) task class. A task object represents work that can be executed as...
Definition: pplxtasks.h:176
credentials()
Constructs an empty set of credentials without a user name or password.
Definition: web_utilities.h:86
const utility::string_t & username() const
The user name associated with the credentials.
Definition: web_utilities.h:102
utility::string_t password() const
The password for the user name associated with the credentials.
Definition: web_utilities.h:109
bool is_default() const
Checks if this proxy was constructed with default settings.
Definition: web_utilities.h:208
Various utilities for string conversions and date and time manipulation.
Definition: asyncrt_utils.h:50