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"
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 = lpwhr->lpHttpSession;
75 hIC = lpwhs->lpAppInfo;
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) ) )
113 ERR("proxy response wrong? (%s)\n", debugstr_w(szBuf));
123 q = strrchrW( p, '"' );
132 /***********************************************************************
133 * WININET_GetSetPassword
135 static BOOL WININET_GetSetPassword( HWND hdlg, LPCWSTR szServer,
136 LPCWSTR szRealm, BOOL bSet )
138 WCHAR szResource[0x80], szUserPass[0x40];
140 HWND hUserItem, hPassItem;
141 DWORD r, dwMagic = 19;
144 static const WCHAR szColon[] = { ':',0 };
145 static const WCHAR szbs[] = { '/', 0 };
147 hUserItem = GetDlgItem( hdlg, IDC_USERNAME );
148 hPassItem = GetDlgItem( hdlg, IDC_PASSWORD );
150 /* now try fetch the username and password */
151 lstrcpyW( szResource, szServer);
152 lstrcatW( szResource, szbs);
153 lstrcatW( szResource, szRealm);
156 * WNetCachePassword is only concerned with the length
157 * of the data stored (which we tell it) and it does
158 * not use strlen() internally so we can add WCHAR data
159 * instead of ASCII data and get it back the same way.
164 GetWindowTextW( hUserItem, szUserPass,
165 (sizeof szUserPass-1)/sizeof(WCHAR) );
166 lstrcatW(szUserPass, szColon);
167 u_len = strlenW( szUserPass );
168 GetWindowTextW( hPassItem, szUserPass+u_len,
169 (sizeof szUserPass)/sizeof(WCHAR)-u_len );
171 r_len = (strlenW( szResource ) + 1)*sizeof(WCHAR);
172 u_len = (strlenW( szUserPass ) + 1)*sizeof(WCHAR);
173 r = WNetCachePassword( (CHAR*)szResource, r_len,
174 (CHAR*)szUserPass, u_len, dwMagic, 0 );
176 return ( r == WN_SUCCESS );
179 sz = sizeof szUserPass;
180 r_len = (strlenW( szResource ) + 1)*sizeof(WCHAR);
181 r = WNetGetCachedPassword( (CHAR*)szResource, r_len,
182 (CHAR*)szUserPass, &sz, dwMagic );
183 if( r != WN_SUCCESS )
186 p = strchrW( szUserPass, ':' );
190 SetWindowTextW( hUserItem, szUserPass );
191 SetWindowTextW( hPassItem, p+1 );
197 /***********************************************************************
198 * WININET_SetProxyAuthorization
200 static BOOL WININET_SetProxyAuthorization( HINTERNET hRequest,
201 LPWSTR username, LPWSTR password )
203 LPWININETHTTPREQW lpwhr;
204 LPWININETHTTPSESSIONW lpwhs;
205 LPWININETAPPINFOW hIC;
208 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hRequest );
212 lpwhs = lpwhr->lpHttpSession;
213 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
215 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
219 hIC = lpwhs->lpAppInfo;
221 p = HeapAlloc( GetProcessHeap(), 0, (strlenW( username ) + 1)*sizeof(WCHAR) );
225 lstrcpyW( p, username );
226 hIC->lpszProxyUsername = p;
228 p = HeapAlloc( GetProcessHeap(), 0, (strlenW( password ) + 1)*sizeof(WCHAR) );
232 lstrcpyW( p, password );
233 hIC->lpszProxyPassword = p;
238 /***********************************************************************
239 * WININET_ProxyPasswordDialog
241 static INT_PTR WINAPI WININET_ProxyPasswordDialog(
242 HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
245 struct WININET_ErrorDlgParams *params;
246 WCHAR szRealm[0x80], szServer[0x80];
248 if( uMsg == WM_INITDIALOG )
250 TRACE("WM_INITDIALOG (%08lx)\n", lParam);
252 /* save the parameter list */
253 params = (struct WININET_ErrorDlgParams*) lParam;
254 SetWindowLongPtrW( hdlg, GWLP_USERDATA, lParam );
256 /* extract the Realm from the proxy response and show it */
257 if( WININET_GetAuthRealm( params->hRequest,
258 szRealm, sizeof szRealm/sizeof(WCHAR)) )
260 hitem = GetDlgItem( hdlg, IDC_REALM );
261 SetWindowTextW( hitem, szRealm );
264 /* extract the name of the proxy server */
265 if( WININET_GetProxyServer( params->hRequest,
266 szServer, sizeof szServer/sizeof(WCHAR)) )
268 hitem = GetDlgItem( hdlg, IDC_PROXY );
269 SetWindowTextW( hitem, szServer );
272 WININET_GetSetPassword( hdlg, szServer, szRealm, FALSE );
277 params = (struct WININET_ErrorDlgParams*)
278 GetWindowLongPtrW( hdlg, GWLP_USERDATA );
285 WCHAR username[0x20], password[0x20];
288 hitem = GetDlgItem( hdlg, IDC_USERNAME );
290 GetWindowTextW( hitem, username, sizeof username/sizeof(WCHAR) );
293 hitem = GetDlgItem( hdlg, IDC_PASSWORD );
295 GetWindowTextW( hitem, password, sizeof password/sizeof(WCHAR) );
297 hitem = GetDlgItem( hdlg, IDC_SAVEPASSWORD );
299 SendMessageW( hitem, BM_GETSTATE, 0, 0 ) &&
300 WININET_GetAuthRealm( params->hRequest,
301 szRealm, sizeof szRealm/sizeof(WCHAR)) &&
302 WININET_GetProxyServer( params->hRequest,
303 szServer, sizeof szServer/sizeof(WCHAR)) )
305 WININET_GetSetPassword( hdlg, szServer, szRealm, TRUE );
307 WININET_SetProxyAuthorization( params->hRequest, username, password );
309 EndDialog( hdlg, ERROR_INTERNET_FORCE_RETRY );
312 if( wParam == IDCANCEL )
314 EndDialog( hdlg, 0 );
322 /***********************************************************************
323 * WININET_GetConnectionStatus
325 static INT WININET_GetConnectionStatus( HINTERNET hRequest )
327 WCHAR szStatus[0x20];
328 DWORD sz, index, dwStatus;
330 TRACE("%p\n", hRequest );
332 sz = sizeof szStatus;
334 if( !HttpQueryInfoW( hRequest, HTTP_QUERY_STATUS_CODE,
335 szStatus, &sz, &index))
337 dwStatus = atoiW( szStatus );
339 TRACE("request %p status = %d\n", hRequest, dwStatus );
345 /***********************************************************************
348 DWORD WINAPI InternetErrorDlg(HWND hWnd, HINTERNET hRequest,
349 DWORD dwError, DWORD dwFlags, LPVOID* lppvData)
351 struct WININET_ErrorDlgParams params;
352 HMODULE hwininet = GetModuleHandleA( "wininet.dll" );
355 TRACE("%p %p %d %08x %p\n", hWnd, hRequest, dwError, dwFlags, lppvData);
358 params.hRequest = hRequest;
359 params.dwError = dwError;
360 params.dwFlags = dwFlags;
361 params.lppvData = lppvData;
366 if( !(dwFlags & FLAGS_ERROR_UI_FILTER_FOR_ERRORS ) )
368 dwStatus = WININET_GetConnectionStatus( hRequest );
369 if( HTTP_STATUS_PROXY_AUTH_REQ != dwStatus )
370 return ERROR_SUCCESS;
371 return DialogBoxParamW( hwininet, MAKEINTRESOURCEW( IDD_PROXYDLG ),
372 hWnd, WININET_ProxyPasswordDialog, (LPARAM) ¶ms );
374 case ERROR_INTERNET_INCORRECT_PASSWORD:
375 return DialogBoxParamW( hwininet, MAKEINTRESOURCEW( IDD_PROXYDLG ),
376 hWnd, WININET_ProxyPasswordDialog, (LPARAM) ¶ms );
378 case ERROR_INTERNET_HTTP_TO_HTTPS_ON_REDIR:
379 case ERROR_INTERNET_INVALID_CA:
380 case ERROR_INTERNET_POST_IS_NON_SECURE:
381 case ERROR_INTERNET_SEC_CERT_CN_INVALID:
382 case ERROR_INTERNET_SEC_CERT_DATE_INVALID:
383 FIXME("Need to display dialog for error %d\n", dwError);
384 return ERROR_SUCCESS;
386 return ERROR_INVALID_PARAMETER;