Skip to content

Commit

Permalink
Merge branch 'development' of https://git01.codeplex.com/casablanca i…
Browse files Browse the repository at this point in the history
…nto development
  • Loading branch information
Ognjen Sobajic committed Oct 11, 2013
2 parents 96489f2 + 76156f0 commit b5446af
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ void PrintTable(const http_response &response, bool &refresh)
if ( suppressMoney )
ucout << "'" << name.as_string() << "'" ;
else
fprintf(stdout, "'%s' Balance = $%02g ", name.as_string().c_str(), bet.as_double());
ucout << "'" << name.as_string() << "' Balance = $" << bet.as_double() << " ";

PrintHand(suppressMoney, BJHand::FromJSON(player[HAND]));
ucout << std::endl;
}
Expand Down
50 changes: 50 additions & 0 deletions Release/include/cpprest/http_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@
#ifndef _CASA_HTTP_CLIENT_H
#define _CASA_HTTP_CLIENT_H

#if defined (__cplusplus_winrt)
#define __WRL_NO_DEFAULT_LIB__
#include <wrl.h>
#include <msxml6.h>
namespace web { namespace http{namespace client{
typedef IXMLHTTPRequest2* native_handle;}}}
#else
namespace web { namespace http{namespace client{
typedef void* native_handle;}}}
#endif // __cplusplus_winrt

#include <memory>
#include <limits>
Expand All @@ -53,6 +63,16 @@ namespace web { namespace http
namespace client
{

#ifdef _MS_WINDOWS
namespace details {
#ifdef __cplusplus_winrt
class winrt_client ;
#else
class winhttp_client;
#endif // __cplusplus_winrt
}
#endif // _MS_WINDOWS

/// <summary>
/// credentials represents a set of user credentials (username and password) to be used
/// for the client and proxy authentication
Expand Down Expand Up @@ -130,6 +150,7 @@ class http_client_config
#if !defined(__cplusplus_winrt)
, m_validate_certificates(true)
#endif
,m_set_user_nativehandle_options([](native_handle)->void{})
{
}

Expand Down Expand Up @@ -245,6 +266,15 @@ class http_client_config
}
#endif

/// <summary>
/// Sets a callback to enable custom setting of winhttp options
/// </summary>
/// <param name="callback">A user callback allowing for customization of the request</param>
void set_nativehandle_options(std::function<void(native_handle)> callback)
{
m_set_user_nativehandle_options = callback;
}

private:
web_proxy m_proxy;
http::client::credentials m_credentials;
Expand All @@ -255,9 +285,29 @@ class http_client_config
#if !defined(__cplusplus_winrt)
bool m_validate_certificates;
#endif
std::function<void(native_handle)> m_set_user_nativehandle_options;

utility::seconds m_timeout;
size_t m_chunksize;

#ifdef _MS_WINDOWS
#ifdef __cplusplus_winrt
friend class details::winrt_client;
#else
friend class details::winhttp_client;
#endif // __cplusplus_winrt
#endif // _MS_WINDOWS


/// <summary>
/// Invokes a user callback to allow for customization of the requst
/// </summary>
/// <param name="handle">The internal http_request handle</param>
/// <returns>True if users set WinHttp/IXAMLHttpRequest2 options correctly, false otherwise.</returns>
void call_user_nativehandle_options(native_handle handle) const
{
m_set_user_nativehandle_options(handle);
}
};

/// <summary>
Expand Down
4 changes: 3 additions & 1 deletion Release/setup/CasablancaSetup.wxs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
<?define VSTargetShort = "VS 2012" ?>
<?define VSTargetFileSuffix = "110" ?>
<?define SDK_RegKeyName = "110" ?>
<?define ExtSDKWinVer = "v8.0" ?>
<?elseif $(var.VSTargetVersion) = 12 ?>
<?include VS12\guiddefs.wxi ?>
<?define VSTargetShort = "VS 2013" ?>
<?define VSTargetFileSuffix = "120" ?>
<?define SDK_RegKeyName = "120" ?>
<?define ExtSDKWinVer = "v8.1" ?>
<?endif ?>

<!-- last GUID used:
Expand Down Expand Up @@ -163,7 +165,7 @@
<?if $(var.VSTargetVersion) != 10 ?>
<Directory Id="Microsoft_SDKs" Name="Microsoft SDKs">
<Directory Id="Windows" Name="Windows">
<Directory Id="v8.0" Name="v8.0">
<Directory Id="winver" Name="$(var.ExtSDKWinVer)">
<Directory Id="ExtensionSDKs" Name="ExtensionSDKs">
<Directory Id="CppRESTWindowsSDK" Name="CppRESTWindowsSDK">
<Directory Id="dir_ver" Name="$(var.ProductVersion)">
Expand Down
11 changes: 11 additions & 0 deletions Release/src/http/client/http_win7.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,17 @@ namespace web { namespace http

winhttp_context->m_request_data = &m_request_data;

//call the callback function of user customized options
try
{
client_config().call_user_nativehandle_options(winhttp_context->m_request_handle);
}
catch (...)
{
request->report_exception(std::current_exception());
return;
}

_start_request_send(winhttp_context, content_length);

return;
Expand Down
24 changes: 18 additions & 6 deletions Release/src/http/client/http_win8.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,15 @@ namespace web { namespace http
// Do not read more than the specified read_length
msl::utilities::SafeInt<size_t> safe_count = static_cast<size_t>(cb);
size_t size_to_read = safe_count.Min(m_read_length);

size_t count = m_buffer.getn((uint8_t *)pv, size_to_read).get();

if (count == 0 && size_to_read != 0)
{
*pcbRead = (ULONG)count;
return (HRESULT)STG_E_READFAULT;
}

_ASSERTE(count != static_cast<size_t>(-1));

if (pcbRead != nullptr)
Expand Down Expand Up @@ -162,7 +162,7 @@ namespace web { namespace http

private:
concurrency::streams::streambuf<uint8_t> m_buffer;

request_context *m_request;

// Total count of bytes read
Expand Down Expand Up @@ -303,7 +303,7 @@ namespace web { namespace http
{
IXMLHTTPRequest2 * req = m_request->m_hRequest;
if ( req != nullptr ) req->AddRef();

if (m_request->m_exceptionPtr != nullptr)
m_request->report_exception(m_request->m_exceptionPtr);
else
Expand Down Expand Up @@ -363,7 +363,7 @@ namespace web { namespace http
request->report_error(L"The method string is invalid.");
return;
}

if ( msg.method() == http::methods::TRCE )
{
// Not supported by WinInet. Generate a more specific exception than what WinInet does.
Expand Down Expand Up @@ -490,6 +490,18 @@ namespace web { namespace http
return;
}

//call the callback function of user customized options
try
{
config.call_user_nativehandle_options(xhr);
}
catch (...)
{
request->report_exception(std::current_exception());
xhr->Release();
return;
}

if (content_length == 0)
{
hr = xhr->Send(nullptr, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>$(OutDir);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>Winhttp.lib;httpapi.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|ARM'">
Expand All @@ -103,6 +104,7 @@
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>$(OutDir);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>Winhttp.lib;httpapi.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
Expand All @@ -117,6 +119,7 @@
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>$(OutDir);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>Winhttp.lib;httpapi.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
Expand All @@ -135,6 +138,7 @@
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalLibraryDirectories>$(OutDir);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>Winhttp.lib;httpapi.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|ARM'">
Expand All @@ -153,6 +157,7 @@
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalLibraryDirectories>$(OutDir);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>Winhttp.lib;httpapi.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
Expand All @@ -171,6 +176,7 @@
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalLibraryDirectories>$(OutDir);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>Winhttp.lib;httpapi.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>$(OutDir);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>Winhttp.lib;httpapi.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|ARM'">
Expand All @@ -116,6 +117,7 @@
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>$(OutDir);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>Winhttp.lib;httpapi.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
Expand All @@ -129,6 +131,7 @@
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>$(OutDir);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>Winhttp.lib;httpapi.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
Expand All @@ -146,6 +149,7 @@
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalLibraryDirectories>$(OutDir);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>Winhttp.lib;httpapi.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|ARM'">
Expand All @@ -163,6 +167,7 @@
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalLibraryDirectories>$(OutDir);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>Winhttp.lib;httpapi.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
Expand All @@ -180,6 +185,7 @@
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalLibraryDirectories>$(OutDir);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>Winhttp.lib;httpapi.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
Expand Down
81 changes: 81 additions & 0 deletions Release/tests/Functional/http/client/authentication_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@
****/

#include "stdafx.h"
#include <stdexcept>

#ifdef _MS_WINDOWS
#ifdef __cplusplus_winrt
#define __WRL_NO_DEFAULT_LIB__
#include <wrl.h>
#include <msxml6.h>
#else
#include <windows.h>
#include <winhttp.h>
#endif
#endif

using namespace web;
using namespace utility;
Expand Down Expand Up @@ -409,6 +421,75 @@ TEST_FIXTURE(server_properties, failed_authentication_resend_request_error, "Req
VERIFY_ARE_EQUAL(200, response.status_code());
}

#ifdef __cplusplus_winrt
TEST_FIXTURE(uri_address, set_user_options_winrt)
{
test_http_server::scoped_server scoped(m_uri);
scoped.server()->next_request().then([](test_request *p_request)
{
p_request->reply(status_codes::OK);
});

http_client_config config;
config.set_nativehandle_options([](native_handle handle)->void{
auto hr = handle->SetProperty(XHR_PROP_TIMEOUT, 1000);
if(!SUCCEEDED(hr))
throw std::runtime_error("The Test Exception");
});
http_client client(m_uri, config);
auto response = client.request(methods::GET).get();
VERIFY_ARE_EQUAL(200, response.status_code());
}
#endif // __cplusplus_winrt

#ifdef _MS_WINDOWS
#if !defined(__cplusplus_winrt)
TEST_FIXTURE(server_properties, set_user_options, "Requires", "Server;UserName;Password")
{
load_parameters();

http_client_config config;
config.set_credentials(credentials(m_username, m_password));

config.set_nativehandle_options([&](native_handle handle)->void{
DWORD policy = WINHTTP_AUTOLOGON_SECURITY_LEVEL_LOW;
if (!WinHttpSetOption(handle,
WINHTTP_OPTION_AUTOLOGON_POLICY,
&policy,
sizeof(policy)))
{
throw std::runtime_error("The Test Error");
}
});

http_client client(m_uri, config);

const size_t rawDataSize = 8;

std::vector<unsigned char> data(rawDataSize);
memcpy(&data[0], "raw data", rawDataSize);

http_request request;
request.set_method(methods::POST);
request.set_body(data);

VERIFY_ARE_EQUAL(200, client.request(request).get().status_code());
}
#endif

TEST_FIXTURE(uri_address, set_user_options_exceptions)
{
test_http_server::scoped_server scoped(m_uri);
http_client_config config;
class TestException;
config.set_nativehandle_options([](native_handle handle)->void{
(handle);
throw std::runtime_error("The Test exception");
});
http_client client(m_uri, config);
VERIFY_THROWS(client.request(methods::GET).get(), std::runtime_error);
}
#endif // _MS_WINDOWS
#pragma endregion

// Fix for 522831 AV after failed authentication attempt
Expand Down

0 comments on commit b5446af

Please sign in to comment.