Sync with trunk head (part 1 of 2)
[reactos.git] / dll / win32 / wininet / dialogs.c
index dd5aa31..2c4a527 100644 (file)
 #include "config.h"
 #include "wine/port.h"
 
+#if defined(__MINGW32__) || defined (_MSC_VER)
+#include <ws2tcpip.h>
+#endif
+
 #include <stdarg.h>
 
 #include "windef.h"
@@ -58,22 +62,23 @@ struct WININET_ErrorDlgParams
  */
 static BOOL WININET_GetProxyServer( HINTERNET hRequest, LPWSTR szBuf, DWORD sz )
 {
-    LPWININETHTTPREQW lpwhr;
-    LPWININETHTTPSESSIONW lpwhs = NULL;
-    LPWININETAPPINFOW hIC = NULL;
+    http_request_t *lpwhr;
+    http_session_t *lpwhs = NULL;
+    appinfo_t *hIC = NULL;
+    BOOL ret = FALSE;
     LPWSTR p;
 
-    lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hRequest );
+    lpwhr = (http_request_t*) WININET_GetObject( hRequest );
     if (NULL == lpwhr)
-       return FALSE;
+        return FALSE;
 
     lpwhs = lpwhr->lpHttpSession;
     if (NULL == lpwhs)
-       return FALSE;
+        goto done;
 
     hIC = lpwhs->lpAppInfo;
     if (NULL == hIC)
-       return FALSE;
+        goto done;
 
     lstrcpynW(szBuf, hIC->lpszProxy, sz);
 
@@ -82,7 +87,39 @@ static BOOL WININET_GetProxyServer( HINTERNET hRequest, LPWSTR szBuf, DWORD sz )
     if (p)
         *p = 0;
 
-    return TRUE;
+    ret = TRUE;
+
+done:
+    WININET_Release( &lpwhr->hdr );
+    return ret;
+}
+
+/***********************************************************************
+ *         WININET_GetServer
+ *
+ *  Determine the name of the web server
+ */
+static BOOL WININET_GetServer( HINTERNET hRequest, LPWSTR szBuf, DWORD sz )
+{
+    http_request_t *lpwhr;
+    http_session_t *lpwhs = NULL;
+    BOOL ret = FALSE;
+
+    lpwhr = (http_request_t*) WININET_GetObject( hRequest );
+    if (NULL == lpwhr)
+        return FALSE;
+
+    lpwhs = lpwhr->lpHttpSession;
+    if (NULL == lpwhs)
+        goto done;
+
+    lstrcpynW(szBuf, lpwhs->lpszHostName, sz);
+
+    ret = TRUE;
+
+done:
+    WININET_Release( &lpwhr->hdr );
+    return ret;
 }
 
 /***********************************************************************
@@ -90,16 +127,20 @@ static BOOL WININET_GetProxyServer( HINTERNET hRequest, LPWSTR szBuf, DWORD sz )
  *
  *  Determine the name of the (basic) Authentication realm
  */
-static BOOL WININET_GetAuthRealm( HINTERNET hRequest, LPWSTR szBuf, DWORD sz )
+static BOOL WININET_GetAuthRealm( HINTERNET hRequest, LPWSTR szBuf, DWORD sz, BOOL proxy )
 {
     LPWSTR p, q;
-    DWORD index;
+    DWORD index, query;
     static const WCHAR szRealm[] = { 'r','e','a','l','m','=',0 };
 
-    /* extract the Realm from the proxy response and show it */
+    if (proxy)
+        query = HTTP_QUERY_PROXY_AUTHENTICATE;
+    else
+        query = HTTP_QUERY_WWW_AUTHENTICATE;
+
+    /* extract the Realm from the response and show it */
     index = 0;
-    if( !HttpQueryInfoW( hRequest, HTTP_QUERY_PROXY_AUTHENTICATE,
-                         szBuf, &sz, &index) )
+    if( !HttpQueryInfoW( hRequest, query, szBuf, &sz, &index) )
         return FALSE;
 
     /*
@@ -109,11 +150,10 @@ static BOOL WININET_GetAuthRealm( HINTERNET hRequest, LPWSTR szBuf, DWORD sz )
     p = strchrW( szBuf, ' ' );
     if( !p || strncmpW( p+1, szRealm, strlenW(szRealm) ) )
     {
-        ERR("proxy response wrong? (%s)\n", debugstr_w(szBuf));
+        ERR("response wrong? (%s)\n", debugstr_w(szBuf));
         return FALSE;
     }
 
-
     /* remove quotes */
     p += 7;
     if( *p == '"' )
@@ -194,44 +234,62 @@ static BOOL WININET_GetSetPassword( HWND hdlg, LPCWSTR szServer,
 }
 
 /***********************************************************************
- *         WININET_SetProxyAuthorization
+ *         WININET_SetAuthorization
  */
-static BOOL WININET_SetProxyAuthorization( HINTERNET hRequest,
-                                         LPWSTR username, LPWSTR password )
+static BOOL WININET_SetAuthorization( HINTERNET hRequest, LPWSTR username,
+                                      LPWSTR password, BOOL proxy )
 {
-    LPWININETHTTPREQW lpwhr;
-    LPWININETHTTPSESSIONW lpwhs;
-    LPWININETAPPINFOW hIC;
-    LPWSTR p;
+    http_request_t *lpwhr;
+    http_session_t *lpwhs;
+    BOOL ret = FALSE;
+    LPWSTR p, q;
 
-    lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hRequest );
+    lpwhr = (http_request_t*) WININET_GetObject( hRequest );
     if( !lpwhr )
-       return FALSE;
-        
+        return FALSE;
+
     lpwhs = lpwhr->lpHttpSession;
     if (NULL == lpwhs ||  lpwhs->hdr.htype != WH_HHTTPSESSION)
     {
         INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
-       return FALSE;
+        goto done;
     }
 
-    hIC = lpwhs->lpAppInfo;
-
-    p = HeapAlloc( GetProcessHeap(), 0, (strlenW( username ) + 1)*sizeof(WCHAR) );
+    p = heap_strdupW(username);
     if( !p )
-        return FALSE;
-    
-    lstrcpyW( p, username );
-    hIC->lpszProxyUsername = p;
+        goto done;
 
-    p = HeapAlloc( GetProcessHeap(), 0, (strlenW( password ) + 1)*sizeof(WCHAR) );
-    if( !p )
-        return FALSE;
-    
-    lstrcpyW( p, password );
-    hIC->lpszProxyPassword = p;
+    q = heap_strdupW(password);
+    if( !q )
+    {
+        HeapFree(GetProcessHeap(), 0, username);
+        goto done;
+    }
 
-    return TRUE;
+    if (proxy)
+    {
+        appinfo_t *hIC = lpwhs->lpAppInfo;
+
+        HeapFree(GetProcessHeap(), 0, hIC->lpszProxyUsername);
+        hIC->lpszProxyUsername = p;
+
+        HeapFree(GetProcessHeap(), 0, hIC->lpszProxyPassword);
+        hIC->lpszProxyPassword = q;
+    }
+    else
+    {
+        HeapFree(GetProcessHeap(), 0, lpwhs->lpszUserName);
+        lpwhs->lpszUserName = p;
+
+        HeapFree(GetProcessHeap(), 0, lpwhs->lpszPassword);
+        lpwhs->lpszPassword = q;
+    }
+
+    ret = TRUE;
+
+done:
+    WININET_Release( &lpwhr->hdr );
+    return ret;
 }
 
 /***********************************************************************
@@ -254,7 +312,7 @@ static INT_PTR WINAPI WININET_ProxyPasswordDialog(
 
         /* extract the Realm from the proxy response and show it */
         if( WININET_GetAuthRealm( params->hRequest,
-                                  szRealm, sizeof szRealm/sizeof(WCHAR)) )
+                                  szRealm, sizeof szRealm/sizeof(WCHAR), TRUE ) )
         {
             hitem = GetDlgItem( hdlg, IDC_REALM );
             SetWindowTextW( hitem, szRealm );
@@ -297,13 +355,97 @@ static INT_PTR WINAPI WININET_ProxyPasswordDialog(
             if( hitem &&
                 SendMessageW( hitem, BM_GETSTATE, 0, 0 ) &&
                 WININET_GetAuthRealm( params->hRequest,
-                                  szRealm, sizeof szRealm/sizeof(WCHAR)) &&
+                                  szRealm, sizeof szRealm/sizeof(WCHAR), TRUE ) &&
                 WININET_GetProxyServer( params->hRequest, 
                                     szServer, sizeof szServer/sizeof(WCHAR)) )
             {
                 WININET_GetSetPassword( hdlg, szServer, szRealm, TRUE );
             }
-            WININET_SetProxyAuthorization( params->hRequest, username, password );
+            WININET_SetAuthorization( params->hRequest, username, password, TRUE );
+
+            EndDialog( hdlg, ERROR_INTERNET_FORCE_RETRY );
+            return TRUE;
+        }
+        if( wParam == IDCANCEL )
+        {
+            EndDialog( hdlg, 0 );
+            return TRUE;
+        }
+        break;
+    }
+    return FALSE;
+}
+
+/***********************************************************************
+ *         WININET_PasswordDialog
+ */
+static INT_PTR WINAPI WININET_PasswordDialog(
+    HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
+{
+    HWND hitem;
+    struct WININET_ErrorDlgParams *params;
+    WCHAR szRealm[0x80], szServer[0x80];
+
+    if( uMsg == WM_INITDIALOG )
+    {
+        TRACE("WM_INITDIALOG (%08lx)\n", lParam);
+
+        /* save the parameter list */
+        params = (struct WININET_ErrorDlgParams*) lParam;
+        SetWindowLongPtrW( hdlg, GWLP_USERDATA, lParam );
+
+        /* extract the Realm from the response and show it */
+        if( WININET_GetAuthRealm( params->hRequest,
+                                  szRealm, sizeof szRealm/sizeof(WCHAR), FALSE ) )
+        {
+            hitem = GetDlgItem( hdlg, IDC_REALM );
+            SetWindowTextW( hitem, szRealm );
+        }
+
+        /* extract the name of the server */
+        if( WININET_GetServer( params->hRequest,
+                               szServer, sizeof szServer/sizeof(WCHAR)) )
+        {
+            hitem = GetDlgItem( hdlg, IDC_SERVER );
+            SetWindowTextW( hitem, szServer );
+        }
+
+        WININET_GetSetPassword( hdlg, szServer, szRealm, FALSE );
+
+        return TRUE;
+    }
+
+    params = (struct WININET_ErrorDlgParams*)
+                 GetWindowLongPtrW( hdlg, GWLP_USERDATA );
+
+    switch( uMsg )
+    {
+    case WM_COMMAND:
+        if( wParam == IDOK )
+        {
+            WCHAR username[0x20], password[0x20];
+
+            username[0] = 0;
+            hitem = GetDlgItem( hdlg, IDC_USERNAME );
+            if( hitem )
+                GetWindowTextW( hitem, username, sizeof username/sizeof(WCHAR) );
+
+            password[0] = 0;
+            hitem = GetDlgItem( hdlg, IDC_PASSWORD );
+            if( hitem )
+                GetWindowTextW( hitem, password, sizeof password/sizeof(WCHAR) );
+
+            hitem = GetDlgItem( hdlg, IDC_SAVEPASSWORD );
+            if( hitem &&
+                SendMessageW( hitem, BM_GETSTATE, 0, 0 ) &&
+                WININET_GetAuthRealm( params->hRequest,
+                                  szRealm, sizeof szRealm/sizeof(WCHAR), FALSE ) &&
+                WININET_GetServer( params->hRequest,
+                                   szServer, sizeof szServer/sizeof(WCHAR)) )
+            {
+                WININET_GetSetPassword( hdlg, szServer, szRealm, TRUE );
+            }
+            WININET_SetAuthorization( params->hRequest, username, password, FALSE );
 
             EndDialog( hdlg, ERROR_INTERNET_FORCE_RETRY );
             return TRUE;
@@ -362,17 +504,23 @@ DWORD WINAPI InternetErrorDlg(HWND hWnd, HINTERNET hRequest,
     switch( dwError )
     {
     case ERROR_SUCCESS:
-        if( !(dwFlags & FLAGS_ERROR_UI_FILTER_FOR_ERRORS ) )
+    case ERROR_INTERNET_INCORRECT_PASSWORD:
+        if( !dwError && !(dwFlags & FLAGS_ERROR_UI_FILTER_FOR_ERRORS ) )
             return 0;
-        dwStatus = WININET_GetConnectionStatus( hRequest );
-        if( HTTP_STATUS_PROXY_AUTH_REQ != dwStatus )
-            return ERROR_SUCCESS;
-        return DialogBoxParamW( hwininet, MAKEINTRESOURCEW( IDD_PROXYDLG ),
-                    hWnd, WININET_ProxyPasswordDialog, (LPARAM) &params );
 
-    case ERROR_INTERNET_INCORRECT_PASSWORD:
-        return DialogBoxParamW( hwininet, MAKEINTRESOURCEW( IDD_PROXYDLG ),
-                    hWnd, WININET_ProxyPasswordDialog, (LPARAM) &params );
+        dwStatus = WININET_GetConnectionStatus( hRequest );
+        switch (dwStatus)
+        {
+        case HTTP_STATUS_PROXY_AUTH_REQ:
+            return DialogBoxParamW( hwininet, MAKEINTRESOURCEW( IDD_PROXYDLG ),
+                                    hWnd, WININET_ProxyPasswordDialog, (LPARAM) &params );
+        case HTTP_STATUS_DENIED:
+            return DialogBoxParamW( hwininet, MAKEINTRESOURCEW( IDD_AUTHDLG ),
+                                    hWnd, WININET_PasswordDialog, (LPARAM) &params );
+        default:
+            WARN("unhandled status %u\n", dwStatus);
+            return 0;
+        }
 
     case ERROR_INTERNET_HTTP_TO_HTTPS_ON_REDIR:
     case ERROR_INTERNET_INVALID_CA: