4 * Copyright 2003 Mike McCormack for CodeWeavers Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #include "wine/debug.h"
34 #define NO_SHLWAPI_STREAM
39 #include "wine/unicode.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
45 struct WININET_ErrorDlgParams
54 /***********************************************************************
55 * WININET_GetProxyServer
57 * Determine the name of the proxy server the request is using
59 static BOOL WININET_GetProxyServer( HINTERNET hRequest, LPWSTR szBuf, DWORD sz )
61 LPWININETHTTPREQW lpwhr;
62 LPWININETHTTPSESSIONW lpwhs = NULL;
63 LPWININETAPPINFOW hIC = NULL;
66 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hRequest );
70 lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
74 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
78 strncpyW(szBuf, hIC->lpszProxy, sz);
80 /* FIXME: perhaps it would be better to use InternetCrackUrl here */
81 p = strchrW(szBuf, ':');
88 /***********************************************************************
89 * WININET_GetAuthRealm
91 * Determine the name of the (basic) Authentication realm
93 static BOOL WININET_GetAuthRealm( HINTERNET hRequest, LPWSTR szBuf, DWORD sz )
97 static const WCHAR szRealm[] = { 'r','e','a','l','m','=',0 };
99 /* extract the Realm from the proxy response and show it */
101 if( !HttpQueryInfoW( hRequest, HTTP_QUERY_PROXY_AUTHENTICATE,
102 szBuf, &sz, &index) )
106 * FIXME: maybe we should check that we're
107 * dealing with 'Basic' Authentication
109 p = strchrW( szBuf, ' ' );
110 if( p && !strncmpW( p+1, szRealm, strlenW(szRealm) ) )
117 q = strrchrW( p, '"' );
128 /***********************************************************************
129 * WININET_GetSetPassword
131 static BOOL WININET_GetSetPassword( HWND hdlg, LPCWSTR szServer,
132 LPCWSTR szRealm, BOOL bSet )
134 WCHAR szResource[0x80], szUserPass[0x40];
136 HWND hUserItem, hPassItem;
137 DWORD r, dwMagic = 19;
140 static const WCHAR szColon[] = { ':',0 };
141 static const WCHAR szbs[] = { '/', 0 };
143 hUserItem = GetDlgItem( hdlg, IDC_USERNAME );
144 hPassItem = GetDlgItem( hdlg, IDC_PASSWORD );
146 /* now try fetch the username and password */
147 lstrcpyW( szResource, szServer);
148 lstrcatW( szResource, szbs);
149 lstrcatW( szResource, szRealm);
152 * WNetCachePassword is only concerned with the length
153 * of the data stored (which we tell it) and it does
154 * not use strlen() internally so we can add WCHAR data
155 * instead of ASCII data and get it back the same way.
160 GetWindowTextW( hUserItem, szUserPass,
161 (sizeof szUserPass-1)/sizeof(WCHAR) );
162 lstrcatW(szUserPass, szColon);
163 u_len = strlenW( szUserPass );
164 GetWindowTextW( hPassItem, szUserPass+u_len,
165 (sizeof szUserPass)/sizeof(WCHAR)-u_len );
167 r_len = (strlenW( szResource ) + 1)*sizeof(WCHAR);
168 u_len = (strlenW( szUserPass ) + 1)*sizeof(WCHAR);
169 r = WNetCachePassword( (CHAR*)szResource, r_len,
170 (CHAR*)szUserPass, u_len, dwMagic, 0 );
172 return ( r == WN_SUCCESS );
175 sz = sizeof szUserPass;
176 r_len = (strlenW( szResource ) + 1)*sizeof(WCHAR);
177 r = WNetGetCachedPassword( (CHAR*)szResource, r_len,
178 (CHAR*)szUserPass, &sz, dwMagic );
179 if( r != WN_SUCCESS )
182 p = strchrW( szUserPass, ':' );
186 SetWindowTextW( hUserItem, szUserPass );
187 SetWindowTextW( hPassItem, p+1 );
193 /***********************************************************************
194 * WININET_SetProxyAuthorization
196 static BOOL WININET_SetProxyAuthorization( HINTERNET hRequest,
197 LPWSTR username, LPWSTR password )
199 LPWININETHTTPREQW lpwhr;
200 LPWININETHTTPSESSIONW lpwhs;
201 LPWININETAPPINFOW hIC;
204 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hRequest );
208 lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
209 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
211 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
215 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
217 p = HeapAlloc( GetProcessHeap(), 0, (strlenW( username ) + 1)*sizeof(WCHAR) );
221 lstrcpyW( p, username );
222 hIC->lpszProxyUsername = p;
224 p = HeapAlloc( GetProcessHeap(), 0, (strlenW( password ) + 1)*sizeof(WCHAR) );
228 lstrcpyW( p, password );
229 hIC->lpszProxyPassword = p;
234 /***********************************************************************
235 * WININET_ProxyPasswordDialog
237 static INT_PTR WINAPI WININET_ProxyPasswordDialog(
238 HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
241 struct WININET_ErrorDlgParams *params;
242 WCHAR szRealm[0x80], szServer[0x80];
244 if( uMsg == WM_INITDIALOG )
246 TRACE("WM_INITDIALOG (%08lx)\n", lParam);
248 /* save the parameter list */
249 params = (struct WININET_ErrorDlgParams*) lParam;
250 SetWindowLongW( hdlg, GWL_USERDATA, lParam );
252 /* extract the Realm from the proxy response and show it */
253 if( WININET_GetAuthRealm( params->hRequest,
254 szRealm, sizeof szRealm/sizeof(WCHAR)) )
256 hitem = GetDlgItem( hdlg, IDC_REALM );
257 SetWindowTextW( hitem, szRealm );
260 /* extract the name of the proxy server */
261 if( WININET_GetProxyServer( params->hRequest,
262 szServer, sizeof szServer/sizeof(WCHAR)) )
264 hitem = GetDlgItem( hdlg, IDC_PROXY );
265 SetWindowTextW( hitem, szServer );
268 WININET_GetSetPassword( hdlg, szServer, szRealm, FALSE );
273 params = (struct WININET_ErrorDlgParams*)
274 GetWindowLongW( hdlg, GWL_USERDATA );
281 WCHAR username[0x20], password[0x20];
284 hitem = GetDlgItem( hdlg, IDC_USERNAME );
286 GetWindowTextW( hitem, username, sizeof username/sizeof(WCHAR) );
289 hitem = GetDlgItem( hdlg, IDC_PASSWORD );
291 GetWindowTextW( hitem, password, sizeof password/sizeof(WCHAR) );
293 hitem = GetDlgItem( hdlg, IDC_SAVEPASSWORD );
295 SendMessageW( hitem, BM_GETSTATE, 0, 0 ) &&
296 WININET_GetAuthRealm( params->hRequest,
297 szRealm, sizeof szRealm/sizeof(WCHAR)) &&
298 WININET_GetProxyServer( params->hRequest,
299 szServer, sizeof szServer/sizeof(WCHAR)) )
301 WININET_GetSetPassword( hdlg, szServer, szRealm, TRUE );
303 WININET_SetProxyAuthorization( params->hRequest, username, password );
305 EndDialog( hdlg, ERROR_INTERNET_FORCE_RETRY );
308 if( wParam == IDCANCEL )
310 EndDialog( hdlg, 0 );
318 /***********************************************************************
319 * WININET_GetConnectionStatus
321 static INT WININET_GetConnectionStatus( HINTERNET hRequest )
323 WCHAR szStatus[0x20];
324 DWORD sz, index, dwStatus;
326 TRACE("%p\n", hRequest );
328 sz = sizeof szStatus;
330 if( !HttpQueryInfoW( hRequest, HTTP_QUERY_STATUS_CODE,
331 szStatus, &sz, &index))
333 dwStatus = atoiW( szStatus );
335 TRACE("request %p status = %ld\n", hRequest, dwStatus );
341 /***********************************************************************
344 DWORD WINAPI InternetErrorDlg(HWND hWnd, HINTERNET hRequest,
345 DWORD dwError, DWORD dwFlags, LPVOID* lppvData)
347 struct WININET_ErrorDlgParams params;
348 HMODULE hwininet = GetModuleHandleA( "wininet.dll" );
351 TRACE("%p %p %ld %08lx %p\n", hWnd, hRequest, dwError, dwFlags, lppvData);
354 params.hRequest = hRequest;
355 params.dwError = dwError;
356 params.dwFlags = dwFlags;
357 params.lppvData = lppvData;
362 if( !(dwFlags & FLAGS_ERROR_UI_FILTER_FOR_ERRORS ) )
364 dwStatus = WININET_GetConnectionStatus( hRequest );
365 if( HTTP_STATUS_PROXY_AUTH_REQ != dwStatus )
366 return ERROR_SUCCESS;
367 return DialogBoxParamW( hwininet, MAKEINTRESOURCEW( IDD_PROXYDLG ),
368 hWnd, WININET_ProxyPasswordDialog, (LPARAM) ¶ms );
370 case ERROR_INTERNET_INCORRECT_PASSWORD:
371 return DialogBoxParamW( hwininet, MAKEINTRESOURCEW( IDD_PROXYDLG ),
372 hWnd, WININET_ProxyPasswordDialog, (LPARAM) ¶ms );
374 case ERROR_INTERNET_HTTP_TO_HTTPS_ON_REDIR:
375 case ERROR_INTERNET_INVALID_CA:
376 case ERROR_INTERNET_POST_IS_NON_SECURE:
377 case ERROR_INTERNET_SEC_CERT_CN_INVALID:
378 case ERROR_INTERNET_SEC_CERT_DATE_INVALID:
379 FIXME("Need to display dialog for error %ld\n", dwError);
380 return ERROR_SUCCESS;
382 return ERROR_INVALID_PARAMETER;