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"
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 = lpwhr->lpHttpSession;
74 hIC = lpwhs->lpAppInfo;
78 lstrcpynW(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) ) )
112 ERR("proxy response wrong? (%s)\n", debugstr_w(szBuf));
122 q = strrchrW( p, '"' );
131 /***********************************************************************
132 * WININET_GetSetPassword
134 static BOOL WININET_GetSetPassword( HWND hdlg, LPCWSTR szServer,
135 LPCWSTR szRealm, BOOL bSet )
137 WCHAR szResource[0x80], szUserPass[0x40];
139 HWND hUserItem, hPassItem;
140 DWORD r, dwMagic = 19;
143 static const WCHAR szColon[] = { ':',0 };
144 static const WCHAR szbs[] = { '/', 0 };
146 hUserItem = GetDlgItem( hdlg, IDC_USERNAME );
147 hPassItem = GetDlgItem( hdlg, IDC_PASSWORD );
149 /* now try fetch the username and password */
150 lstrcpyW( szResource, szServer);
151 lstrcatW( szResource, szbs);
152 lstrcatW( szResource, szRealm);
155 * WNetCachePassword is only concerned with the length
156 * of the data stored (which we tell it) and it does
157 * not use strlen() internally so we can add WCHAR data
158 * instead of ASCII data and get it back the same way.
163 GetWindowTextW( hUserItem, szUserPass,
164 (sizeof szUserPass-1)/sizeof(WCHAR) );
165 lstrcatW(szUserPass, szColon);
166 u_len = strlenW( szUserPass );
167 GetWindowTextW( hPassItem, szUserPass+u_len,
168 (sizeof szUserPass)/sizeof(WCHAR)-u_len );
170 r_len = (strlenW( szResource ) + 1)*sizeof(WCHAR);
171 u_len = (strlenW( szUserPass ) + 1)*sizeof(WCHAR);
172 r = WNetCachePassword( (CHAR*)szResource, r_len,
173 (CHAR*)szUserPass, u_len, dwMagic, 0 );
175 return ( r == WN_SUCCESS );
178 sz = sizeof szUserPass;
179 r_len = (strlenW( szResource ) + 1)*sizeof(WCHAR);
180 r = WNetGetCachedPassword( (CHAR*)szResource, r_len,
181 (CHAR*)szUserPass, &sz, dwMagic );
182 if( r != WN_SUCCESS )
185 p = strchrW( szUserPass, ':' );
189 SetWindowTextW( hUserItem, szUserPass );
190 SetWindowTextW( hPassItem, p+1 );
196 /***********************************************************************
197 * WININET_SetProxyAuthorization
199 static BOOL WININET_SetProxyAuthorization( HINTERNET hRequest,
200 LPWSTR username, LPWSTR password )
202 LPWININETHTTPREQW lpwhr;
203 LPWININETHTTPSESSIONW lpwhs;
204 LPWININETAPPINFOW hIC;
207 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hRequest );
211 lpwhs = lpwhr->lpHttpSession;
212 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
214 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
218 hIC = lpwhs->lpAppInfo;
220 p = HeapAlloc( GetProcessHeap(), 0, (strlenW( username ) + 1)*sizeof(WCHAR) );
224 lstrcpyW( p, username );
225 hIC->lpszProxyUsername = p;
227 p = HeapAlloc( GetProcessHeap(), 0, (strlenW( password ) + 1)*sizeof(WCHAR) );
231 lstrcpyW( p, password );
232 hIC->lpszProxyPassword = p;
237 /***********************************************************************
238 * WININET_ProxyPasswordDialog
240 static INT_PTR WINAPI WININET_ProxyPasswordDialog(
241 HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
244 struct WININET_ErrorDlgParams *params;
245 WCHAR szRealm[0x80], szServer[0x80];
247 if( uMsg == WM_INITDIALOG )
249 TRACE("WM_INITDIALOG (%08lx)\n", lParam);
251 /* save the parameter list */
252 params = (struct WININET_ErrorDlgParams*) lParam;
253 SetWindowLongPtrW( hdlg, GWLP_USERDATA, lParam );
255 /* extract the Realm from the proxy response and show it */
256 if( WININET_GetAuthRealm( params->hRequest,
257 szRealm, sizeof szRealm/sizeof(WCHAR)) )
259 hitem = GetDlgItem( hdlg, IDC_REALM );
260 SetWindowTextW( hitem, szRealm );
263 /* extract the name of the proxy server */
264 if( WININET_GetProxyServer( params->hRequest,
265 szServer, sizeof szServer/sizeof(WCHAR)) )
267 hitem = GetDlgItem( hdlg, IDC_PROXY );
268 SetWindowTextW( hitem, szServer );
271 WININET_GetSetPassword( hdlg, szServer, szRealm, FALSE );
276 params = (struct WININET_ErrorDlgParams*)
277 GetWindowLongPtrW( hdlg, GWLP_USERDATA );
284 WCHAR username[0x20], password[0x20];
287 hitem = GetDlgItem( hdlg, IDC_USERNAME );
289 GetWindowTextW( hitem, username, sizeof username/sizeof(WCHAR) );
292 hitem = GetDlgItem( hdlg, IDC_PASSWORD );
294 GetWindowTextW( hitem, password, sizeof password/sizeof(WCHAR) );
296 hitem = GetDlgItem( hdlg, IDC_SAVEPASSWORD );
298 SendMessageW( hitem, BM_GETSTATE, 0, 0 ) &&
299 WININET_GetAuthRealm( params->hRequest,
300 szRealm, sizeof szRealm/sizeof(WCHAR)) &&
301 WININET_GetProxyServer( params->hRequest,
302 szServer, sizeof szServer/sizeof(WCHAR)) )
304 WININET_GetSetPassword( hdlg, szServer, szRealm, TRUE );
306 WININET_SetProxyAuthorization( params->hRequest, username, password );
308 EndDialog( hdlg, ERROR_INTERNET_FORCE_RETRY );
311 if( wParam == IDCANCEL )
313 EndDialog( hdlg, 0 );
321 /***********************************************************************
322 * WININET_GetConnectionStatus
324 static INT WININET_GetConnectionStatus( HINTERNET hRequest )
326 WCHAR szStatus[0x20];
327 DWORD sz, index, dwStatus;
329 TRACE("%p\n", hRequest );
331 sz = sizeof szStatus;
333 if( !HttpQueryInfoW( hRequest, HTTP_QUERY_STATUS_CODE,
334 szStatus, &sz, &index))
336 dwStatus = atoiW( szStatus );
338 TRACE("request %p status = %d\n", hRequest, dwStatus );
344 /***********************************************************************
347 DWORD WINAPI InternetErrorDlg(HWND hWnd, HINTERNET hRequest,
348 DWORD dwError, DWORD dwFlags, LPVOID* lppvData)
350 struct WININET_ErrorDlgParams params;
351 HMODULE hwininet = GetModuleHandleA( "wininet.dll" );
354 TRACE("%p %p %d %08x %p\n", hWnd, hRequest, dwError, dwFlags, lppvData);
357 params.hRequest = hRequest;
358 params.dwError = dwError;
359 params.dwFlags = dwFlags;
360 params.lppvData = lppvData;
365 if( !(dwFlags & FLAGS_ERROR_UI_FILTER_FOR_ERRORS ) )
367 dwStatus = WININET_GetConnectionStatus( hRequest );
368 if( HTTP_STATUS_PROXY_AUTH_REQ != dwStatus )
369 return ERROR_SUCCESS;
370 return DialogBoxParamW( hwininet, MAKEINTRESOURCEW( IDD_PROXYDLG ),
371 hWnd, WININET_ProxyPasswordDialog, (LPARAM) ¶ms );
373 case ERROR_INTERNET_INCORRECT_PASSWORD:
374 return DialogBoxParamW( hwininet, MAKEINTRESOURCEW( IDD_PROXYDLG ),
375 hWnd, WININET_ProxyPasswordDialog, (LPARAM) ¶ms );
377 case ERROR_INTERNET_HTTP_TO_HTTPS_ON_REDIR:
378 case ERROR_INTERNET_INVALID_CA:
379 case ERROR_INTERNET_POST_IS_NON_SECURE:
380 case ERROR_INTERNET_SEC_CERT_CN_INVALID:
381 case ERROR_INTERNET_SEC_CERT_DATE_INVALID:
382 FIXME("Need to display dialog for error %d\n", dwError);
383 return ERROR_SUCCESS;
385 return ERROR_INVALID_PARAMETER;