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 http_request_t *lpwhr;
66 http_session_t *lpwhs = NULL;
67 appinfo_t *hIC = NULL;
71 lpwhr = (http_request_t*) WININET_GetObject( hRequest );
75 lpwhs = lpwhr->lpHttpSession;
79 hIC = lpwhs->lpAppInfo;
83 lstrcpynW(szBuf, hIC->lpszProxy, sz);
85 /* FIXME: perhaps it would be better to use InternetCrackUrl here */
86 p = strchrW(szBuf, ':');
93 WININET_Release( &lpwhr->hdr );
97 /***********************************************************************
98 * WININET_GetAuthRealm
100 * Determine the name of the (basic) Authentication realm
102 static BOOL WININET_GetAuthRealm( HINTERNET hRequest, LPWSTR szBuf, DWORD sz )
106 static const WCHAR szRealm[] = { 'r','e','a','l','m','=',0 };
108 /* extract the Realm from the proxy response and show it */
110 if( !HttpQueryInfoW( hRequest, HTTP_QUERY_PROXY_AUTHENTICATE,
111 szBuf, &sz, &index) )
115 * FIXME: maybe we should check that we're
116 * dealing with 'Basic' Authentication
118 p = strchrW( szBuf, ' ' );
119 if( !p || strncmpW( p+1, szRealm, strlenW(szRealm) ) )
121 ERR("proxy response wrong? (%s)\n", debugstr_w(szBuf));
131 q = strrchrW( p, '"' );
140 /***********************************************************************
141 * WININET_GetSetPassword
143 static BOOL WININET_GetSetPassword( HWND hdlg, LPCWSTR szServer,
144 LPCWSTR szRealm, BOOL bSet )
146 WCHAR szResource[0x80], szUserPass[0x40];
148 HWND hUserItem, hPassItem;
149 DWORD r, dwMagic = 19;
152 static const WCHAR szColon[] = { ':',0 };
153 static const WCHAR szbs[] = { '/', 0 };
155 hUserItem = GetDlgItem( hdlg, IDC_USERNAME );
156 hPassItem = GetDlgItem( hdlg, IDC_PASSWORD );
158 /* now try fetch the username and password */
159 lstrcpyW( szResource, szServer);
160 lstrcatW( szResource, szbs);
161 lstrcatW( szResource, szRealm);
164 * WNetCachePassword is only concerned with the length
165 * of the data stored (which we tell it) and it does
166 * not use strlen() internally so we can add WCHAR data
167 * instead of ASCII data and get it back the same way.
172 GetWindowTextW( hUserItem, szUserPass,
173 (sizeof szUserPass-1)/sizeof(WCHAR) );
174 lstrcatW(szUserPass, szColon);
175 u_len = strlenW( szUserPass );
176 GetWindowTextW( hPassItem, szUserPass+u_len,
177 (sizeof szUserPass)/sizeof(WCHAR)-u_len );
179 r_len = (strlenW( szResource ) + 1)*sizeof(WCHAR);
180 u_len = (strlenW( szUserPass ) + 1)*sizeof(WCHAR);
181 r = WNetCachePassword( (CHAR*)szResource, r_len,
182 (CHAR*)szUserPass, u_len, dwMagic, 0 );
184 return ( r == WN_SUCCESS );
187 sz = sizeof szUserPass;
188 r_len = (strlenW( szResource ) + 1)*sizeof(WCHAR);
189 r = WNetGetCachedPassword( (CHAR*)szResource, r_len,
190 (CHAR*)szUserPass, &sz, dwMagic );
191 if( r != WN_SUCCESS )
194 p = strchrW( szUserPass, ':' );
198 SetWindowTextW( hUserItem, szUserPass );
199 SetWindowTextW( hPassItem, p+1 );
205 /***********************************************************************
206 * WININET_SetProxyAuthorization
208 static BOOL WININET_SetProxyAuthorization( HINTERNET hRequest,
209 LPWSTR username, LPWSTR password )
211 http_request_t *lpwhr;
212 http_session_t *lpwhs;
217 lpwhr = (http_request_t*) WININET_GetObject( hRequest );
221 lpwhs = lpwhr->lpHttpSession;
222 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
224 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
228 hIC = lpwhs->lpAppInfo;
230 p = heap_strdupW(username);
234 HeapFree(GetProcessHeap(), 0, hIC->lpszProxyUsername);
235 hIC->lpszProxyUsername = p;
237 p = heap_strdupW(password);
241 HeapFree(GetProcessHeap(), 0, hIC->lpszProxyPassword);
242 hIC->lpszProxyPassword = p;
247 WININET_Release( &lpwhr->hdr );
251 /***********************************************************************
252 * WININET_ProxyPasswordDialog
254 static INT_PTR WINAPI WININET_ProxyPasswordDialog(
255 HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
258 struct WININET_ErrorDlgParams *params;
259 WCHAR szRealm[0x80], szServer[0x80];
261 if( uMsg == WM_INITDIALOG )
263 TRACE("WM_INITDIALOG (%08lx)\n", lParam);
265 /* save the parameter list */
266 params = (struct WININET_ErrorDlgParams*) lParam;
267 SetWindowLongPtrW( hdlg, GWLP_USERDATA, lParam );
269 /* extract the Realm from the proxy response and show it */
270 if( WININET_GetAuthRealm( params->hRequest,
271 szRealm, sizeof szRealm/sizeof(WCHAR)) )
273 hitem = GetDlgItem( hdlg, IDC_REALM );
274 SetWindowTextW( hitem, szRealm );
277 /* extract the name of the proxy server */
278 if( WININET_GetProxyServer( params->hRequest,
279 szServer, sizeof szServer/sizeof(WCHAR)) )
281 hitem = GetDlgItem( hdlg, IDC_PROXY );
282 SetWindowTextW( hitem, szServer );
285 WININET_GetSetPassword( hdlg, szServer, szRealm, FALSE );
290 params = (struct WININET_ErrorDlgParams*)
291 GetWindowLongPtrW( hdlg, GWLP_USERDATA );
298 WCHAR username[0x20], password[0x20];
301 hitem = GetDlgItem( hdlg, IDC_USERNAME );
303 GetWindowTextW( hitem, username, sizeof username/sizeof(WCHAR) );
306 hitem = GetDlgItem( hdlg, IDC_PASSWORD );
308 GetWindowTextW( hitem, password, sizeof password/sizeof(WCHAR) );
310 hitem = GetDlgItem( hdlg, IDC_SAVEPASSWORD );
312 SendMessageW( hitem, BM_GETSTATE, 0, 0 ) &&
313 WININET_GetAuthRealm( params->hRequest,
314 szRealm, sizeof szRealm/sizeof(WCHAR)) &&
315 WININET_GetProxyServer( params->hRequest,
316 szServer, sizeof szServer/sizeof(WCHAR)) )
318 WININET_GetSetPassword( hdlg, szServer, szRealm, TRUE );
320 WININET_SetProxyAuthorization( params->hRequest, username, password );
322 EndDialog( hdlg, ERROR_INTERNET_FORCE_RETRY );
325 if( wParam == IDCANCEL )
327 EndDialog( hdlg, 0 );
335 /***********************************************************************
336 * WININET_GetConnectionStatus
338 static INT WININET_GetConnectionStatus( HINTERNET hRequest )
340 WCHAR szStatus[0x20];
341 DWORD sz, index, dwStatus;
343 TRACE("%p\n", hRequest );
345 sz = sizeof szStatus;
347 if( !HttpQueryInfoW( hRequest, HTTP_QUERY_STATUS_CODE,
348 szStatus, &sz, &index))
350 dwStatus = atoiW( szStatus );
352 TRACE("request %p status = %d\n", hRequest, dwStatus );
358 /***********************************************************************
361 DWORD WINAPI InternetErrorDlg(HWND hWnd, HINTERNET hRequest,
362 DWORD dwError, DWORD dwFlags, LPVOID* lppvData)
364 struct WININET_ErrorDlgParams params;
365 HMODULE hwininet = GetModuleHandleA( "wininet.dll" );
368 TRACE("%p %p %d %08x %p\n", hWnd, hRequest, dwError, dwFlags, lppvData);
371 params.hRequest = hRequest;
372 params.dwError = dwError;
373 params.dwFlags = dwFlags;
374 params.lppvData = lppvData;
379 if( !(dwFlags & FLAGS_ERROR_UI_FILTER_FOR_ERRORS ) )
381 dwStatus = WININET_GetConnectionStatus( hRequest );
382 if( HTTP_STATUS_PROXY_AUTH_REQ != dwStatus )
383 return ERROR_SUCCESS;
384 return DialogBoxParamW( hwininet, MAKEINTRESOURCEW( IDD_PROXYDLG ),
385 hWnd, WININET_ProxyPasswordDialog, (LPARAM) ¶ms );
387 case ERROR_INTERNET_INCORRECT_PASSWORD:
388 return DialogBoxParamW( hwininet, MAKEINTRESOURCEW( IDD_PROXYDLG ),
389 hWnd, WININET_ProxyPasswordDialog, (LPARAM) ¶ms );
391 case ERROR_INTERNET_HTTP_TO_HTTPS_ON_REDIR:
392 case ERROR_INTERNET_INVALID_CA:
393 case ERROR_INTERNET_POST_IS_NON_SECURE:
394 case ERROR_INTERNET_SEC_CERT_CN_INVALID:
395 case ERROR_INTERNET_SEC_CERT_DATE_INVALID:
396 FIXME("Need to display dialog for error %d\n", dwError);
397 return ERROR_SUCCESS;
399 return ERROR_INVALID_PARAMETER;