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
22 #include "wine/port.h"
33 #include "wine/debug.h"
35 #define NO_SHLWAPI_STREAM
40 #include "wine/unicode.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
46 struct WININET_ErrorDlgParams
55 /***********************************************************************
56 * WININET_GetProxyServer
58 * Determine the name of the proxy server the request is using
60 static BOOL WININET_GetProxyServer( HINTERNET hRequest, LPWSTR szBuf, DWORD sz )
62 LPWININETHTTPREQW lpwhr;
63 LPWININETHTTPSESSIONW lpwhs = NULL;
64 LPWININETAPPINFOW hIC = NULL;
67 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hRequest );
71 lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
75 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
79 lstrcpynW(szBuf, hIC->lpszProxy, sz);
81 /* FIXME: perhaps it would be better to use InternetCrackUrl here */
82 p = strchrW(szBuf, ':');
89 /***********************************************************************
90 * WININET_GetAuthRealm
92 * Determine the name of the (basic) Authentication realm
94 static BOOL WININET_GetAuthRealm( HINTERNET hRequest, LPWSTR szBuf, DWORD sz )
98 static const WCHAR szRealm[] = { 'r','e','a','l','m','=',0 };
100 /* extract the Realm from the proxy response and show it */
102 if( !HttpQueryInfoW( hRequest, HTTP_QUERY_PROXY_AUTHENTICATE,
103 szBuf, &sz, &index) )
107 * FIXME: maybe we should check that we're
108 * dealing with 'Basic' Authentication
110 p = strchrW( szBuf, ' ' );
111 if( p && !strncmpW( p+1, szRealm, strlenW(szRealm) ) )
118 q = strrchrW( p, '"' );
129 /***********************************************************************
130 * WININET_GetSetPassword
132 static BOOL WININET_GetSetPassword( HWND hdlg, LPCWSTR szServer,
133 LPCWSTR szRealm, BOOL bSet )
135 WCHAR szResource[0x80], szUserPass[0x40];
137 HWND hUserItem, hPassItem;
138 DWORD r, dwMagic = 19;
141 static const WCHAR szColon[] = { ':',0 };
142 static const WCHAR szbs[] = { '/', 0 };
144 hUserItem = GetDlgItem( hdlg, IDC_USERNAME );
145 hPassItem = GetDlgItem( hdlg, IDC_PASSWORD );
147 /* now try fetch the username and password */
148 lstrcpyW( szResource, szServer);
149 lstrcatW( szResource, szbs);
150 lstrcatW( szResource, szRealm);
153 * WNetCachePassword is only concerned with the length
154 * of the data stored (which we tell it) and it does
155 * not use strlen() internally so we can add WCHAR data
156 * instead of ASCII data and get it back the same way.
161 GetWindowTextW( hUserItem, szUserPass,
162 (sizeof szUserPass-1)/sizeof(WCHAR) );
163 lstrcatW(szUserPass, szColon);
164 u_len = strlenW( szUserPass );
165 GetWindowTextW( hPassItem, szUserPass+u_len,
166 (sizeof szUserPass)/sizeof(WCHAR)-u_len );
168 r_len = (strlenW( szResource ) + 1)*sizeof(WCHAR);
169 u_len = (strlenW( szUserPass ) + 1)*sizeof(WCHAR);
170 r = WNetCachePassword( (CHAR*)szResource, r_len,
171 (CHAR*)szUserPass, u_len, dwMagic, 0 );
173 return ( r == WN_SUCCESS );
176 sz = sizeof szUserPass;
177 r_len = (strlenW( szResource ) + 1)*sizeof(WCHAR);
178 r = WNetGetCachedPassword( (CHAR*)szResource, r_len,
179 (CHAR*)szUserPass, &sz, dwMagic );
180 if( r != WN_SUCCESS )
183 p = strchrW( szUserPass, ':' );
187 SetWindowTextW( hUserItem, szUserPass );
188 SetWindowTextW( hPassItem, p+1 );
194 /***********************************************************************
195 * WININET_SetProxyAuthorization
197 static BOOL WININET_SetProxyAuthorization( HINTERNET hRequest,
198 LPWSTR username, LPWSTR password )
200 LPWININETHTTPREQW lpwhr;
201 LPWININETHTTPSESSIONW lpwhs;
202 LPWININETAPPINFOW hIC;
205 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hRequest );
209 lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
210 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
212 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
216 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
218 p = HeapAlloc( GetProcessHeap(), 0, (strlenW( username ) + 1)*sizeof(WCHAR) );
222 lstrcpyW( p, username );
223 hIC->lpszProxyUsername = p;
225 p = HeapAlloc( GetProcessHeap(), 0, (strlenW( password ) + 1)*sizeof(WCHAR) );
229 lstrcpyW( p, password );
230 hIC->lpszProxyPassword = p;
235 /***********************************************************************
236 * WININET_ProxyPasswordDialog
238 static INT_PTR WINAPI WININET_ProxyPasswordDialog(
239 HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
242 struct WININET_ErrorDlgParams *params;
243 WCHAR szRealm[0x80], szServer[0x80];
245 if( uMsg == WM_INITDIALOG )
247 TRACE("WM_INITDIALOG (%08lx)\n", lParam);
249 /* save the parameter list */
250 params = (struct WININET_ErrorDlgParams*) lParam;
251 SetWindowLongPtrW( hdlg, GWLP_USERDATA, lParam );
253 /* extract the Realm from the proxy response and show it */
254 if( WININET_GetAuthRealm( params->hRequest,
255 szRealm, sizeof szRealm/sizeof(WCHAR)) )
257 hitem = GetDlgItem( hdlg, IDC_REALM );
258 SetWindowTextW( hitem, szRealm );
261 /* extract the name of the proxy server */
262 if( WININET_GetProxyServer( params->hRequest,
263 szServer, sizeof szServer/sizeof(WCHAR)) )
265 hitem = GetDlgItem( hdlg, IDC_PROXY );
266 SetWindowTextW( hitem, szServer );
269 WININET_GetSetPassword( hdlg, szServer, szRealm, FALSE );
274 params = (struct WININET_ErrorDlgParams*)
275 GetWindowLongPtrW( hdlg, GWLP_USERDATA );
282 WCHAR username[0x20], password[0x20];
285 hitem = GetDlgItem( hdlg, IDC_USERNAME );
287 GetWindowTextW( hitem, username, sizeof username/sizeof(WCHAR) );
290 hitem = GetDlgItem( hdlg, IDC_PASSWORD );
292 GetWindowTextW( hitem, password, sizeof password/sizeof(WCHAR) );
294 hitem = GetDlgItem( hdlg, IDC_SAVEPASSWORD );
296 SendMessageW( hitem, BM_GETSTATE, 0, 0 ) &&
297 WININET_GetAuthRealm( params->hRequest,
298 szRealm, sizeof szRealm/sizeof(WCHAR)) &&
299 WININET_GetProxyServer( params->hRequest,
300 szServer, sizeof szServer/sizeof(WCHAR)) )
302 WININET_GetSetPassword( hdlg, szServer, szRealm, TRUE );
304 WININET_SetProxyAuthorization( params->hRequest, username, password );
306 EndDialog( hdlg, ERROR_INTERNET_FORCE_RETRY );
309 if( wParam == IDCANCEL )
311 EndDialog( hdlg, 0 );
319 /***********************************************************************
320 * WININET_GetConnectionStatus
322 static INT WININET_GetConnectionStatus( HINTERNET hRequest )
324 WCHAR szStatus[0x20];
325 DWORD sz, index, dwStatus;
327 TRACE("%p\n", hRequest );
329 sz = sizeof szStatus;
331 if( !HttpQueryInfoW( hRequest, HTTP_QUERY_STATUS_CODE,
332 szStatus, &sz, &index))
334 dwStatus = atoiW( szStatus );
336 TRACE("request %p status = %ld\n", hRequest, dwStatus );
342 /***********************************************************************
345 DWORD WINAPI InternetErrorDlg(HWND hWnd, HINTERNET hRequest,
346 DWORD dwError, DWORD dwFlags, LPVOID* lppvData)
348 struct WININET_ErrorDlgParams params;
349 HMODULE hwininet = GetModuleHandleA( "wininet.dll" );
352 TRACE("%p %p %ld %08lx %p\n", hWnd, hRequest, dwError, dwFlags, lppvData);
355 params.hRequest = hRequest;
356 params.dwError = dwError;
357 params.dwFlags = dwFlags;
358 params.lppvData = lppvData;
363 if( !(dwFlags & FLAGS_ERROR_UI_FILTER_FOR_ERRORS ) )
365 dwStatus = WININET_GetConnectionStatus( hRequest );
366 if( HTTP_STATUS_PROXY_AUTH_REQ != dwStatus )
367 return ERROR_SUCCESS;
368 return DialogBoxParamW( hwininet, MAKEINTRESOURCEW( IDD_PROXYDLG ),
369 hWnd, WININET_ProxyPasswordDialog, (LPARAM) ¶ms );
371 case ERROR_INTERNET_INCORRECT_PASSWORD:
372 return DialogBoxParamW( hwininet, MAKEINTRESOURCEW( IDD_PROXYDLG ),
373 hWnd, WININET_ProxyPasswordDialog, (LPARAM) ¶ms );
375 case ERROR_INTERNET_HTTP_TO_HTTPS_ON_REDIR:
376 case ERROR_INTERNET_INVALID_CA:
377 case ERROR_INTERNET_POST_IS_NON_SECURE:
378 case ERROR_INTERNET_SEC_CERT_CN_INVALID:
379 case ERROR_INTERNET_SEC_CERT_DATE_INVALID:
380 FIXME("Need to display dialog for error %ld\n", dwError);
381 return ERROR_SUCCESS;
383 return ERROR_INVALID_PARAMETER;