Skip to content

Commit

Permalink
Squashed 'src/cpprestsdk/cpprestsdk/' changes from 122d09549..411a10915
Browse files Browse the repository at this point in the history
411a10915 mint 2.10.19
096a9a1bb Merge pull request microsoft#1782 from microsoft/fix_uri_parsing
006271f6a make Uri.is_host_loopback() only return true for localhost and 127.0.0.1 exactly
9c654889e Remove email list from the readme
31e7feacc Merge pull request microsoft#1718 from dashanji/Fix-typo
e1b6a8e61 Merge pull request microsoft#1711 from Fighter19/pr-fix-safeint3
c5dcbb5bb Merge pull request microsoft#1717 from microsoft/users/GitHubPolicyService/f2ee14d6-8d6b-4313-b754-3880e015f7c3
98ee36dac Fix typo
a57f45918 Microsoft mandatory file
3308d9728 Fix likely typo in SafeInt3.hpp, that results in error with clang 15
07cf58910 Merge pull request microsoft#1429 from NN---/fix/value_int_ctor
3eac925ad Update Release/include/cpprest/json.h
06363bc78 Update Release/include/cpprest/json.h
bfe348779 Merge pull request #1577 from JvdGlind/hidden_visibility_support_macos
0ddc61829 Clarify cpprestsdk level of support
804448058 export http_exception for non Windows builds
d9d7f5ed4 Merge pull request microsoft#1496 from icherniukh/oauth2_client_credentials
8ae5da616 Update oauth2.h
708a5df2b Add support for oauth2 using only client credentials
5408f1dc9 Add constructor from all integer types.

git-subtree-dir: src/cpprestsdk/cpprestsdk
git-subtree-split: 411a109150b270f23c8c97fa4ec9a0a4a98cdecf
  • Loading branch information
Trenly committed Mar 22, 2024
1 parent e650de5 commit 449f1b9
Show file tree
Hide file tree
Showing 18 changed files with 289 additions and 33 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
**cpprestsdk is in maintenance mode and we do not recommend its use in new projects. We will continue to fix critical bugs and address security issues.**

## Welcome!

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.
Expand Down Expand Up @@ -59,7 +61,7 @@ target_link_libraries(main PRIVATE cpprestsdk::cpprest)

Is there a feature missing that you'd like to see, or found a bug that you have a fix for? Or do you have an idea or just interest in helping out in building the library? Let us know and we'd love to work with you. For a good starting point on where we are headed and feature ideas, take a look at our [requested features and bugs](https://github.com/Microsoft/cpprestsdk/issues).

Big or small we'd like to take your [contributions](https://github.com/Microsoft/cpprestsdk/wiki/Make-a-contribution-and-report-issues) back to help improve the C++ Rest SDK for everyone. If interested contact us askcasablanca at Microsoft dot com.
Big or small we'd like to take your [contributions](https://github.com/Microsoft/cpprestsdk/wiki/Make-a-contribution-and-report-issues) back to help improve the C++ Rest SDK for everyone.

## Having Trouble?

Expand Down
2 changes: 1 addition & 1 deletion Release/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ endif()

set(CPPREST_VERSION_MAJOR 2)
set(CPPREST_VERSION_MINOR 10)
set(CPPREST_VERSION_REVISION 18)
set(CPPREST_VERSION_REVISION 19)

enable_testing()

Expand Down
5 changes: 3 additions & 2 deletions Release/include/cpprest/base_uri.h
Original file line number Diff line number Diff line change
Expand Up @@ -296,13 +296,14 @@ class uri
/// A loopback URI is one which refers to a hostname or ip address with meaning only on the local machine.
/// </summary>
/// <remarks>
/// Examples include "localhost", or ip addresses in the loopback range (127.0.0.0/24).
/// Examples include "localhost", or "127.0.0.1". The only URIs for which this method returns true are "127.0.0.1", and "localhost",
/// all other URIs return false
/// </remarks>
/// <returns><c>true</c> if this URI references the local host, <c>false</c> otherwise.</returns>
bool is_host_loopback() const
{
return !is_empty() &&
((host() == _XPLATSTR("localhost")) || (host().size() > 4 && host().substr(0, 4) == _XPLATSTR("127.")));
((host() == _XPLATSTR("localhost")) || (host() == _XPLATSTR("127.0.0.1")));
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Release/include/cpprest/details/SafeInt3.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1574,7 +1574,7 @@ class SafeCastHelper<bool, T, CastToBool>
}

template<typename E>
static void CastThrow(bool b, T& t) SAFEINT_CPP_THROW
static void CastThrow(T t, bool& b) SAFEINT_CPP_THROW
{
b = !!t;
}
Expand Down
8 changes: 8 additions & 0 deletions Release/include/cpprest/details/cpprest_compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,20 @@

#ifdef _NO_ASYNCRTIMP
#define _ASYNCRTIMP
#define _ASYNCRTIMP_TYPEINFO
#else // ^^^ _NO_ASYNCRTIMP ^^^ // vvv !_NO_ASYNCRTIMP vvv
#ifdef _ASYNCRT_EXPORT
#define _ASYNCRTIMP __declspec(dllexport)
#else // ^^^ _ASYNCRT_EXPORT ^^^ // vvv !_ASYNCRT_EXPORT vvv
#define _ASYNCRTIMP __declspec(dllimport)
#endif // _ASYNCRT_EXPORT

#if defined(_WIN32)
#define _ASYNCRTIMP_TYPEINFO
#else // ^^^ _WIN32 ^^^ // vvv !_WIN32 vvv
#define _ASYNCRTIMP_TYPEINFO __attribute__((visibility("default")))
#endif // _WIN32

#endif // _NO_ASYNCRTIMP

#ifdef CASABLANCA_DEPRECATION_NO_WARNINGS
Expand Down
1 change: 1 addition & 0 deletions Release/include/cpprest/details/http_constants.dat
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ DAT(expires_in, "expires_in")
DAT(grant_type, "grant_type")
DAT(redirect_uri, "redirect_uri")
DAT(refresh_token, "refresh_token")
DAT(client_credentials, "client_credentials")
DAT(response_type, "response_type")
DAT(scope, "scope")
DAT(state, "state")
Expand Down
2 changes: 1 addition & 1 deletion Release/include/cpprest/http_msg.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ class header_names
/// <summary>
/// Represents an HTTP error. This class holds an error message and an optional error code.
/// </summary>
class http_exception : public std::exception
class _ASYNCRTIMP_TYPEINFO http_exception : public std::exception
{
public:
/// <summary>
Expand Down
62 changes: 46 additions & 16 deletions Release/include/cpprest/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,25 +100,37 @@ class value
/// Constructor creating a JSON number value
/// </summary>
/// <param name="value">The C++ value to create a JSON value from</param>
_ASYNCRTIMP value(int32_t value);
_ASYNCRTIMP value(int value);

/// <summary>
/// Constructor creating a JSON number value
/// </summary>
/// <param name="value">The C++ value to create a JSON value from</param>
_ASYNCRTIMP value(uint32_t value);
_ASYNCRTIMP value(unsigned value);

/// <summary>
/// Constructor creating a JSON number value
/// </summary>
/// <param name="value">The C++ value to create a JSON value from</param>
_ASYNCRTIMP value(int64_t value);
_ASYNCRTIMP value(long value);

/// <summary>
/// Constructor creating a JSON number value
/// </summary>
/// <param name="value">The C++ value to create a JSON value from</param>
_ASYNCRTIMP value(uint64_t value);
_ASYNCRTIMP value(unsigned long value);

/// <summary>
/// Constructor creating a JSON number value
/// </summary>
/// <param name="value">The C++ value to create a JSON value from</param>
_ASYNCRTIMP value(long long value);

/// <summary>
/// Constructor creating a JSON number value
/// </summary>
/// <param name="value">The C++ value to create a JSON value from</param>
_ASYNCRTIMP value(unsigned long long value);

/// <summary>
/// Constructor creating a JSON number value
Expand Down Expand Up @@ -222,28 +234,42 @@ class value
/// </summary>
/// <param name="value">The C++ value to create a JSON value from</param>
/// <returns>A JSON number value</returns>
static _ASYNCRTIMP value __cdecl number(int32_t value);
static _ASYNCRTIMP value __cdecl number(int value);

/// <summary>
/// Creates a number value
/// </summary>
/// <param name="value">The C++ value to create a JSON value from</param>
/// <returns>A JSON number value</returns>
static _ASYNCRTIMP value __cdecl number(unsigned value);

/// <summary>
/// Creates a number value
/// </summary>
/// <param name="value">The C++ value to create a JSON value from</param>
/// <returns>A JSON number value</returns>
static _ASYNCRTIMP value __cdecl number(long value);

/// <summary>
/// Creates a number value
/// </summary>
/// <param name="value">The C++ value to create a JSON value from</param>
/// <returns>A JSON number value</returns>
static _ASYNCRTIMP value __cdecl number(uint32_t value);
static _ASYNCRTIMP value __cdecl number(unsigned long value);

/// <summary>
/// Creates a number value
/// </summary>
/// <param name="value">The C++ value to create a JSON value from</param>
/// <returns>A JSON number value</returns>
static _ASYNCRTIMP value __cdecl number(int64_t value);
static _ASYNCRTIMP value __cdecl number(long long value);

/// <summary>
/// Creates a number value
/// </summary>
/// <param name="value">The C++ value to create a JSON value from</param>
/// <returns>A JSON number value</returns>
static _ASYNCRTIMP value __cdecl number(uint64_t value);
static _ASYNCRTIMP value __cdecl number(unsigned long long value);

/// <summary>
/// Creates a Boolean value
Expand Down Expand Up @@ -1218,10 +1244,12 @@ class number
// convert to unsigned int64). This helps handling number objects e.g. comparing two numbers.

number(double value) : m_value(value), m_type(double_type) {}
number(int32_t value) : m_intval(value), m_type(value < 0 ? signed_type : unsigned_type) {}
number(uint32_t value) : m_intval(value), m_type(unsigned_type) {}
number(int64_t value) : m_intval(value), m_type(value < 0 ? signed_type : unsigned_type) {}
number(uint64_t value) : m_uintval(value), m_type(unsigned_type) {}
number(int value) : m_intval(value), m_type(value < 0 ? signed_type : unsigned_type) {}
number(unsigned value) : m_intval(value), m_type(unsigned_type) {}
number(long value) : m_intval(value), m_type(value < 0 ? signed_type : unsigned_type) {}
number(unsigned long value) : m_uintval(value), m_type(unsigned_type) {}
number(long long value) : m_intval(value), m_type(value < 0 ? signed_type : unsigned_type) {}
number(unsigned long long value) : m_uintval(value), m_type(unsigned_type) {}

public:
/// <summary>
Expand Down Expand Up @@ -1438,10 +1466,12 @@ class _Number : public _Value
{
public:
_Number(double value) : m_number(value) {}
_Number(int32_t value) : m_number(value) {}
_Number(uint32_t value) : m_number(value) {}
_Number(int64_t value) : m_number(value) {}
_Number(uint64_t value) : m_number(value) {}
_Number(int value) : m_number(value) {}
_Number(unsigned value) : m_number(value) {}
_Number(long value) : m_number(value) {}
_Number(unsigned long value) : m_number(value) {}
_Number(long long value) : m_number(value) {}
_Number(unsigned long long value) : m_number(value) {}

virtual std::unique_ptr<_Value> _copy_value() { return utility::details::make_unique<_Number>(*this); }

Expand Down
15 changes: 15 additions & 0 deletions Release/include/cpprest/oauth2.h
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,21 @@ class oauth2_config
return _request_token(ub);
}

/// <summary>
/// Fetches an access token from the token endpoint using client credentials grant type.
/// The task creates an HTTP request to the token_endpoint() using
/// client authentication as the authorization grant.
/// See: http://tools.ietf.org/html/rfc6749#section-4.4
/// </summary>
/// <returns>Task that fetches token(s) using client credentials.</returns>
pplx::task<void> token_from_client_credentials()
{
uri_builder ub;
ub.append_query(
details::oauth2_strings::grant_type, details::oauth2_strings::client_credentials, false);
return _request_token(ub);
}

/// <summary>
/// Returns enabled state of the configuration.
/// The oauth2_handler will perform OAuth 2.0 authentication only if
Expand Down
2 changes: 1 addition & 1 deletion Release/include/cpprest/producerconsumerstream.h
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ class basic_producer_consumer_buffer : public streams::details::streambuf_state_
// If front block is not empty - we are done
if (m_blocks.front()->rd_chars_left() > 0) break;

// The block has no more data to be read. Relase the block
// The block has no more data to be read. Release the block
m_blocks.pop_front();
}
}
Expand Down
2 changes: 1 addition & 1 deletion Release/include/cpprest/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
*/
#define CPPREST_VERSION_MINOR 10
#define CPPREST_VERSION_MAJOR 2
#define CPPREST_VERSION_REVISION 18
#define CPPREST_VERSION_REVISION 19

#define CPPREST_VERSION (CPPREST_VERSION_MAJOR * 100000 + CPPREST_VERSION_MINOR * 100 + CPPREST_VERSION_REVISION)
37 changes: 29 additions & 8 deletions Release/src/json/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,31 +38,48 @@ web::json::value::value()
{
}

web::json::value::value(int32_t value)
web::json::value::value(int value)
: m_value(utility::details::make_unique<web::json::details::_Number>(value))
#ifdef ENABLE_JSON_VALUE_VISUALIZER
, m_kind(value::Number)
#endif
{
}

web::json::value::value(uint32_t value)
web::json::value::value(unsigned value)
: m_value(utility::details::make_unique<web::json::details::_Number>(value))
#ifdef ENABLE_JSON_VALUE_VISUALIZER
, m_kind(value::Number)
#endif
{
}

web::json::value::value(int64_t value)

web::json::value::value(long value)
: m_value(utility::details::make_unique<web::json::details::_Number>(value))
#ifdef ENABLE_JSON_VALUE_VISUALIZER
, m_kind(value::Number)
#endif
{
}

web::json::value::value(uint64_t value)
web::json::value::value(unsigned long value)
: m_value(utility::details::make_unique<web::json::details::_Number>(value))
#ifdef ENABLE_JSON_VALUE_VISUALIZER
, m_kind(value::Number)
#endif
{
}

web::json::value::value(long long value)
: m_value(utility::details::make_unique<web::json::details::_Number>(value))
#ifdef ENABLE_JSON_VALUE_VISUALIZER
, m_kind(value::Number)
#endif
{
}

web::json::value::value(unsigned long long value)
: m_value(utility::details::make_unique<web::json::details::_Number>(value))
#ifdef ENABLE_JSON_VALUE_VISUALIZER
, m_kind(value::Number)
Expand Down Expand Up @@ -162,13 +179,17 @@ web::json::value web::json::value::null() { return web::json::value(); }

web::json::value web::json::value::number(double value) { return web::json::value(value); }

web::json::value web::json::value::number(int32_t value) { return web::json::value(value); }
web::json::value web::json::value::number(int value) { return web::json::value(value); }

web::json::value web::json::value::number(unsigned value) { return web::json::value(value); }

web::json::value web::json::value::number(long value) { return web::json::value(value); }

web::json::value web::json::value::number(uint32_t value) { return web::json::value(value); }
web::json::value web::json::value::number(unsigned long value) { return web::json::value(value); }

web::json::value web::json::value::number(int64_t value) { return web::json::value(value); }
web::json::value web::json::value::number(long long value) { return web::json::value(value); }

web::json::value web::json::value::number(uint64_t value) { return web::json::value(value); }
web::json::value web::json::value::number(unsigned long long value) { return web::json::value(value); }

web::json::value web::json::value::boolean(bool value) { return web::json::value(value); }

Expand Down
68 changes: 68 additions & 0 deletions Release/tests/functional/http/client/oauth2_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,74 @@ SUITE(oauth2_tests)
VERIFY_ARE_EQUAL(U("done"), m_oauth2_config.token().access_token());
}

TEST_FIXTURE(oauth2_test_setup, oauth2_token_from_client_credentials)
{
VERIFY_IS_FALSE(m_oauth2_config.is_enabled());

m_oauth2_config.set_user_agent(U("test_user_agent"));

// Fetch using HTTP Basic authentication.
{
m_scoped.server()->next_request().then([](test_request* request) {
VERIFY_ARE_EQUAL(request->m_method, methods::POST);

VERIFY_IS_TRUE(is_application_x_www_form_urlencoded(request));

VERIFY_ARE_EQUAL(
U("Basic MTIzQUJDOjQ1NkRFRg=="),
request->m_headers[header_names::authorization]);

VERIFY_ARE_EQUAL(
to_body_data(U("grant_type=client_credentials")),
request->m_body);

VERIFY_ARE_EQUAL(
U("test_user_agent"),
get_request_user_agent(request));

std::map<utility::string_t, utility::string_t> headers;
headers[header_names::content_type] = mime_types::application_json;
request->reply(
status_codes::OK, U(""), headers, "{\"access_token\":\"xyzzy123\",\"token_type\":\"bearer\"}");
});

m_oauth2_config.token_from_client_credentials().wait();
VERIFY_ARE_EQUAL(U("xyzzy123"), m_oauth2_config.token().access_token());
VERIFY_IS_TRUE(m_oauth2_config.is_enabled());
}

// Fetch using client key & secret in request body (x-www-form-urlencoded).
{
m_scoped.server()->next_request().then([](test_request* request) {
VERIFY_IS_TRUE(is_application_x_www_form_urlencoded(request));

VERIFY_ARE_EQUAL(U(""), request->m_headers[header_names::authorization]);

VERIFY_ARE_EQUAL(
to_body_data(U("grant_type=client_credentials&client_id=123ABC&client_secret=456DEF")),
request->m_body);

VERIFY_ARE_EQUAL(U("test_user_agent"), get_request_user_agent(request));

std::map<utility::string_t, utility::string_t> headers;
headers[header_names::content_type] = mime_types::application_json;
request->reply(
status_codes::OK, U(""), headers, "{\"access_token\":\"xyzzy123\",\"token_type\":\"bearer\"}");
});

m_oauth2_config.set_token(oauth2_token()); // Clear token.
VERIFY_IS_FALSE(m_oauth2_config.is_enabled());

m_oauth2_config.set_http_basic_auth(false);
m_oauth2_config.token_from_client_credentials().wait();

VERIFY_ARE_EQUAL(
U("xyzzy123"),
m_oauth2_config.token().access_token());
VERIFY_IS_TRUE(m_oauth2_config.is_enabled());
}
}

TEST_FIXTURE(oauth2_test_setup, oauth2_bearer_token)
{
m_oauth2_config.set_token(oauth2_token(U("12345678")));
Expand Down
Loading

0 comments on commit 449f1b9

Please sign in to comment.