[3/3] winhttp: Implement IWinHttpRequest::SetCredentials.

Hans Leidekker hans at codeweavers.com
Sat Jul 23 02:52:58 CDT 2011


---
 dlls/winhttp/request.c       |   24 ++++++++++++++++++++++--
 dlls/winhttp/tests/winhttp.c |   29 ++++++++++++++++++++++++++++-
 2 files changed, 50 insertions(+), 3 deletions(-)

diff --git a/dlls/winhttp/request.c b/dlls/winhttp/request.c
index 65e0aa7..d170573 100644
--- a/dlls/winhttp/request.c
+++ b/dlls/winhttp/request.c
@@ -2361,8 +2361,28 @@ static HRESULT WINAPI winhttp_request_SetCredentials(
     BSTR password,
     HTTPREQUEST_SETCREDENTIALS_FLAGS flags )
 {
-    FIXME("\n");
-    return E_NOTIMPL;
+    struct winhttp_request *request = impl_from_IWinHttpRequest( iface );
+    DWORD target, scheme = WINHTTP_AUTH_SCHEME_BASIC; /* FIXME: query supported schemes */
+
+    TRACE("%p, %s, %p\n", request, debugstr_w(username), password);
+
+    if (request->state < REQUEST_STATE_OPEN)
+    {
+        return HRESULT_FROM_WIN32( ERROR_WINHTTP_CANNOT_CALL_BEFORE_OPEN );
+    }
+    switch (flags)
+    {
+    case HTTPREQUEST_SETCREDENTIALS_FOR_SERVER:
+        target = WINHTTP_AUTH_TARGET_SERVER;
+        break;
+    case HTTPREQUEST_SETCREDENTIALS_FOR_PROXY:
+        target = WINHTTP_AUTH_TARGET_PROXY;
+        break;
+    default:
+        return E_INVALIDARG;
+    }
+    if (WinHttpSetCredentials( request->hrequest, target, scheme, username, password, NULL )) return S_OK;
+    return HRESULT_FROM_WIN32( GetLastError() );
 }
 
 static HRESULT WINAPI winhttp_request_Open(
diff --git a/dlls/winhttp/tests/winhttp.c b/dlls/winhttp/tests/winhttp.c
index 7dfd1cb..77623a4 100644
--- a/dlls/winhttp/tests/winhttp.c
+++ b/dlls/winhttp/tests/winhttp.c
@@ -2100,13 +2100,15 @@ static void test_credentials(void)
 
 static void test_IWinHttpRequest(void)
 {
+    static const WCHAR usernameW[] = {'u','s','e','r','n','a','m','e',0};
+    static const WCHAR passwordW[] = {'p','a','s','s','w','o','r','d',0};
     static const WCHAR url1W[] = {'h','t','t','p',':','/','/','w','i','n','e','h','q','.','o','r','g',0};
     static const WCHAR url2W[] = {'w','i','n','e','h','q','.','o','r','g',0};
     static const WCHAR method1W[] = {'G','E','T',0};
     static const WCHAR method2W[] = {'I','N','V','A','L','I','D',0};
     HRESULT hr;
     IWinHttpRequest *req;
-    BSTR method, url, response = NULL, status_text = NULL;
+    BSTR method, url, username, password, response = NULL, status_text = NULL;
     VARIANT async, empty, timeout, body;
     VARIANT_BOOL succeeded;
     LONG status;
@@ -2189,6 +2191,9 @@ static void test_IWinHttpRequest(void)
     hr = IWinHttpRequest_SetTimeouts( req, 10000, 10000, 10000, 10000 );
     ok( hr == S_OK, "got %08x\n", hr );
 
+    hr = IWinHttpRequest_SetCredentials( req, NULL, NULL, 0xdeadbeef );
+    ok( hr == HRESULT_FROM_WIN32( ERROR_WINHTTP_CANNOT_CALL_BEFORE_OPEN ), "got %08x\n", hr );
+
     SysFreeString( method );
     method = SysAllocString( method1W );
     SysFreeString( url );
@@ -2205,6 +2210,20 @@ static void test_IWinHttpRequest(void)
     hr = IWinHttpRequest_SetTimeouts( req, 10000, 10000, 10000, 10000 );
     ok( hr == S_OK, "got %08x\n", hr );
 
+    hr = IWinHttpRequest_SetCredentials( req, NULL, NULL, 0xdeadbeef );
+    ok( hr == E_INVALIDARG, "got %08x\n", hr );
+
+    username = SysAllocString( usernameW );
+    hr = IWinHttpRequest_SetCredentials( req, username, NULL, 0xdeadbeef );
+    ok( hr == E_INVALIDARG, "got %08x\n", hr );
+
+    password = SysAllocString( passwordW );
+    hr = IWinHttpRequest_SetCredentials( req, username, password, 0xdeadbeef );
+    ok( hr == E_INVALIDARG, "got %08x\n", hr );
+
+    hr = IWinHttpRequest_SetCredentials( req, username, password, HTTPREQUEST_SETCREDENTIALS_FOR_SERVER );
+    ok( hr == S_OK, "got %08x\n", hr );
+
     hr = IWinHttpRequest_Send( req, empty );
     ok( hr == S_OK, "got %08x\n", hr );
 
@@ -2221,6 +2240,9 @@ static void test_IWinHttpRequest(void)
     trace("%s\n", wine_dbgstr_w(status_text));
     SysFreeString( status_text );
 
+    hr = IWinHttpRequest_SetCredentials( req, username, password, HTTPREQUEST_SETCREDENTIALS_FOR_SERVER );
+    ok( hr == S_OK, "got %08x\n", hr );
+
     VariantInit( &timeout );
     V_VT( &timeout ) = VT_I4;
     V_I4( &timeout ) = 10;
@@ -2234,6 +2256,9 @@ static void test_IWinHttpRequest(void)
     ok( hr == S_OK, "got %08x\n", hr );
     SysFreeString( status_text );
 
+    hr = IWinHttpRequest_SetCredentials( req, username, password, HTTPREQUEST_SETCREDENTIALS_FOR_SERVER );
+    ok( hr == S_OK, "got %08x\n", hr );
+
     hr = IWinHttpRequest_Send( req, empty );
     ok( hr == S_OK, "got %08x\n", hr );
 
@@ -2262,6 +2287,8 @@ static void test_IWinHttpRequest(void)
 
     SysFreeString( method );
     SysFreeString( url );
+    SysFreeString( username );
+    SysFreeString( password );
     CoUninitialize();
 }
 
-- 
1.7.4.1






More information about the wine-patches mailing list