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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
24 #if defined(__MINGW32__) || defined (_MSC_VER)
36 #include "wine/debug.h"
38 #define NO_SHLWAPI_STREAM
43 #include "wine/unicode.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
49 struct WININET_ErrorDlgParams
58 /***********************************************************************
59 * WININET_GetProxyServer
61 * Determine the name of the proxy server the request is using
63 static BOOL WININET_GetProxyServer( HINTERNET hRequest, LPWSTR szBuf, DWORD sz )
65 LPWININETHTTPREQW lpwhr;
66 LPWININETHTTPSESSIONW lpwhs = NULL;
67 LPWININETAPPINFOW hIC = NULL;
70 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hRequest );
74 lpwhs = lpwhr->lpHttpSession;
78 hIC = lpwhs->lpAppInfo;
82 lstrcpynW(szBuf, hIC->lpszProxy, sz);
84 /* FIXME: perhaps it would be better to use InternetCrackUrl here */
85 p = strchrW(szBuf, ':');
92 /***********************************************************************
93 * WININET_GetAuthRealm
95 * Determine the name of the (basic) Authentication realm
97 static BOOL WININET_GetAuthRealm( HINTERNET hRequest, LPWSTR szBuf, DWORD sz )
101 static const WCHAR szRealm[] = { 'r','e','a','l','m','=',0 };
103 /* extract the Realm from the proxy response and show it */
105 if( !HttpQueryInfoW( hRequest, HTTP_QUERY_PROXY_AUTHENTICATE,
106 szBuf, &sz, &index) )
110 * FIXME: maybe we should check that we're
111 * dealing with 'Basic' Authentication
113 p = strchrW( szBuf, ' ' );
114 if( !p || strncmpW( p+1, szRealm, strlenW(szRealm) ) )
116 ERR("proxy response wrong? (%s)\n", debugstr_w(szBuf));
126 q = strrchrW( p, '"' );
135 /***********************************************************************
136 * WININET_GetSetPassword
138 static BOOL WININET_GetSetPassword( HWND hdlg, LPCWSTR szServer,
139 LPCWSTR szRealm, BOOL bSet )
141 WCHAR szResource[0x80], szUserPass[0x40];
143 HWND hUserItem, hPassItem;
144 DWORD r, dwMagic = 19;
147 static const WCHAR szColon[] = { ':',0 };
148 static const WCHAR szbs[] = { '/', 0 };
150 hUserItem = GetDlgItem( hdlg, IDC_USERNAME );
151 hPassItem = GetDlgItem( hdlg, IDC_PASSWORD );
153 /* now try fetch the username and password */
154 lstrcpyW( szResource, szServer);
155 lstrcatW( szResource, szbs);
156 lstrcatW( szResource, szRealm);
159 * WNetCachePassword is only concerned with the length
160 * of the data stored (which we tell it) and it does
161 * not use strlen() internally so we can add WCHAR data
162 * instead of ASCII data and get it back the same way.
167 GetWindowTextW( hUserItem, szUserPass,
168 (sizeof szUserPass-1)/sizeof(WCHAR) );
169 lstrcatW(szUserPass, szColon);
170 u_len = strlenW( szUserPass );
171 GetWindowTextW( hPassItem, szUserPass+u_len,
172 (sizeof szUserPass)/sizeof(WCHAR)-u_len );
174 r_len = (strlenW( szResource ) + 1)*sizeof(WCHAR);
175 u_len = (strlenW( szUserPass ) + 1)*sizeof(WCHAR);
176 r = WNetCachePassword( (CHAR*)szResource, r_len,
177 (CHAR*)szUserPass, u_len, dwMagic, 0 );
179 return ( r == WN_SUCCESS );
182 sz = sizeof szUserPass;
183 r_len = (strlenW( szResource ) + 1)*sizeof(WCHAR);
184 r = WNetGetCachedPassword( (CHAR*)szResource, r_len,
185 (CHAR*)szUserPass, &sz, dwMagic );
186 if( r != WN_SUCCESS )
189 p = strchrW( szUserPass, ':' );
193 SetWindowTextW( hUserItem, szUserPass );
194 SetWindowTextW( hPassItem, p+1 );
200 /***********************************************************************
201 * WININET_SetProxyAuthorization
203 static BOOL WININET_SetProxyAuthorization( HINTERNET hRequest,
204 LPWSTR username, LPWSTR password )
206 LPWININETHTTPREQW lpwhr;
207 LPWININETHTTPSESSIONW lpwhs;
208 LPWININETAPPINFOW hIC;
211 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hRequest );
215 lpwhs = lpwhr->lpHttpSession;
216 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
218 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
222 hIC = lpwhs->lpAppInfo;
224 p = HeapAlloc( GetProcessHeap(), 0, (strlenW( username ) + 1)*sizeof(WCHAR) );
228 lstrcpyW( p, username );
229 hIC->lpszProxyUsername = p;
231 p = HeapAlloc( GetProcessHeap(), 0, (strlenW( password ) + 1)*sizeof(WCHAR) );
235 lstrcpyW( p, password );
236 hIC->lpszProxyPassword = p;
241 /***********************************************************************
242 * WININET_ProxyPasswordDialog
244 static INT_PTR WINAPI WININET_ProxyPasswordDialog(
245 HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
248 struct WININET_ErrorDlgParams *params;
249 WCHAR szRealm[0x80], szServer[0x80];
251 if( uMsg == WM_INITDIALOG )
253 TRACE("WM_INITDIALOG (%08lx)\n", lParam);
255 /* save the parameter list */
256 params = (struct WININET_ErrorDlgParams*) lParam;
257 SetWindowLongPtrW( hdlg, GWLP_USERDATA, lParam );
259 /* extract the Realm from the proxy response and show it */
260 if( WININET_GetAuthRealm( params->hRequest,
261 szRealm, sizeof szRealm/sizeof(WCHAR)) )
263 hitem = GetDlgItem( hdlg, IDC_REALM );
264 SetWindowTextW( hitem, szRealm );
267 /* extract the name of the proxy server */
268 if( WININET_GetProxyServer( params->hRequest,
269 szServer, sizeof szServer/sizeof(WCHAR)) )
271 hitem = GetDlgItem( hdlg, IDC_PROXY );
272 SetWindowTextW( hitem, szServer );
275 WININET_GetSetPassword( hdlg, szServer, szRealm, FALSE );
280 params = (struct WININET_ErrorDlgParams*)
281 GetWindowLongPtrW( hdlg, GWLP_USERDATA );
288 WCHAR username[0x20], password[0x20];
291 hitem = GetDlgItem( hdlg, IDC_USERNAME );
293 GetWindowTextW( hitem, username, sizeof username/sizeof(WCHAR) );
296 hitem = GetDlgItem( hdlg, IDC_PASSWORD );
298 GetWindowTextW( hitem, password, sizeof password/sizeof(WCHAR) );
300 hitem = GetDlgItem( hdlg, IDC_SAVEPASSWORD );
302 SendMessageW( hitem, BM_GETSTATE, 0, 0 ) &&
303 WININET_GetAuthRealm( params->hRequest,
304 szRealm, sizeof szRealm/sizeof(WCHAR)) &&
305 WININET_GetProxyServer( params->hRequest,
306 szServer, sizeof szServer/sizeof(WCHAR)) )
308 WININET_GetSetPassword( hdlg, szServer, szRealm, TRUE );
310 WININET_SetProxyAuthorization( params->hRequest, username, password );
312 EndDialog( hdlg, ERROR_INTERNET_FORCE_RETRY );
315 if( wParam == IDCANCEL )
317 EndDialog( hdlg, 0 );
325 /***********************************************************************
326 * WININET_GetConnectionStatus
328 static INT WININET_GetConnectionStatus( HINTERNET hRequest )
330 WCHAR szStatus[0x20];
331 DWORD sz, index, dwStatus;
333 TRACE("%p\n", hRequest );
335 sz = sizeof szStatus;
337 if( !HttpQueryInfoW( hRequest, HTTP_QUERY_STATUS_CODE,
338 szStatus, &sz, &index))
340 dwStatus = atoiW( szStatus );
342 TRACE("request %p status = %d\n", hRequest, dwStatus );
348 /***********************************************************************
351 DWORD WINAPI InternetErrorDlg(HWND hWnd, HINTERNET hRequest,
352 DWORD dwError, DWORD dwFlags, LPVOID* lppvData)
354 struct WININET_ErrorDlgParams params;
355 HMODULE hwininet = GetModuleHandleA( "wininet.dll" );
358 TRACE("%p %p %d %08x %p\n", hWnd, hRequest, dwError, dwFlags, lppvData);
361 params.hRequest = hRequest;
362 params.dwError = dwError;
363 params.dwFlags = dwFlags;
364 params.lppvData = lppvData;
369 if( !(dwFlags & FLAGS_ERROR_UI_FILTER_FOR_ERRORS ) )
371 dwStatus = WININET_GetConnectionStatus( hRequest );
372 if( HTTP_STATUS_PROXY_AUTH_REQ != dwStatus )
373 return ERROR_SUCCESS;
374 return DialogBoxParamW( hwininet, MAKEINTRESOURCEW( IDD_PROXYDLG ),
375 hWnd, WININET_ProxyPasswordDialog, (LPARAM) ¶ms );
377 case ERROR_INTERNET_INCORRECT_PASSWORD:
378 return DialogBoxParamW( hwininet, MAKEINTRESOURCEW( IDD_PROXYDLG ),
379 hWnd, WININET_ProxyPasswordDialog, (LPARAM) ¶ms );
381 case ERROR_INTERNET_HTTP_TO_HTTPS_ON_REDIR:
382 case ERROR_INTERNET_INVALID_CA:
383 case ERROR_INTERNET_POST_IS_NON_SECURE:
384 case ERROR_INTERNET_SEC_CERT_CN_INVALID:
385 case ERROR_INTERNET_SEC_CERT_DATE_INVALID:
386 FIXME("Need to display dialog for error %d\n", dwError);
387 return ERROR_SUCCESS;
389 return ERROR_INVALID_PARAMETER;