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
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 = (LPWININETHTTPREQW) hRequest;
62 LPWININETHTTPSESSIONW lpwhs = NULL;
63 LPWININETAPPINFOW hIC = NULL;
69 lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
73 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
77 strncpyW(szBuf, hIC->lpszProxy, sz);
79 /* FIXME: perhaps it would be better to use InternetCrackUrl here */
80 p = strchrW(szBuf, ':');
87 /***********************************************************************
88 * WININET_GetAuthRealm
90 * Determine the name of the (basic) Authentication realm
92 static BOOL WININET_GetAuthRealm( HINTERNET hRequest, LPWSTR szBuf, DWORD sz )
96 WCHAR szRealm[] = { 'r','e','a','l','m','=',0 };
98 /* extract the Realm from the proxy response and show it */
100 if( !HttpQueryInfoW( hRequest, HTTP_QUERY_PROXY_AUTHENTICATE,
101 szBuf, &sz, &index) )
105 * FIXME: maybe we should check that we're
106 * dealing with 'Basic' Authentication
108 p = strchrW( szBuf, ' ' );
109 if( p && !strncmpW( p+1, szRealm, strlenW(szRealm) ) )
116 q = strrchrW( p, '"' );
127 /***********************************************************************
128 * WININET_GetSetPassword
130 static BOOL WININET_GetSetPassword( HWND hdlg, LPCWSTR szServer,
131 LPCWSTR szRealm, BOOL bSet )
133 WCHAR szResource[0x80], szUserPass[0x40];
135 HWND hUserItem, hPassItem;
136 DWORD r, dwMagic = 19;
139 WCHAR szColon[] = { ':',0 }, szbs[] = { '/', 0 };
141 hUserItem = GetDlgItem( hdlg, IDC_USERNAME );
142 hPassItem = GetDlgItem( hdlg, IDC_PASSWORD );
144 /* now try fetch the username and password */
145 lstrcpyW( szResource, szServer);
146 lstrcatW( szResource, szbs);
147 lstrcatW( szResource, szRealm);
150 * WNetCachePassword is only concerned with the length
151 * of the data stored (which we tell it) and it does
152 * not use strlen() internally so we can add WCHAR data
153 * instead of ASCII data and get it back the same way.
158 GetWindowTextW( hUserItem, szUserPass,
159 (sizeof szUserPass-1)/sizeof(WCHAR) );
160 lstrcatW(szUserPass, szColon);
161 u_len = strlenW( szUserPass );
162 GetWindowTextW( hPassItem, szUserPass+u_len,
163 (sizeof szUserPass)/sizeof(WCHAR)-u_len );
165 r_len = (strlenW( szResource ) + 1)*sizeof(WCHAR);
166 u_len = (strlenW( szUserPass ) + 1)*sizeof(WCHAR);
167 r = WNetCachePassword( (CHAR*)szResource, r_len,
168 (CHAR*)szUserPass, u_len, dwMagic, 0 );
170 return ( r == WN_SUCCESS );
173 sz = sizeof szUserPass;
174 r_len = (strlenW( szResource ) + 1)*sizeof(WCHAR);
175 r = WNetGetCachedPassword( (CHAR*)szResource, r_len,
176 (CHAR*)szUserPass, &sz, dwMagic );
177 if( r != WN_SUCCESS )
180 p = strchrW( szUserPass, ':' );
184 SetWindowTextW( hUserItem, szUserPass );
185 SetWindowTextW( hPassItem, p+1 );
191 /***********************************************************************
192 * WININET_SetProxyAuthorization
194 static BOOL WININET_SetProxyAuthorization( HINTERNET hRequest,
195 LPWSTR username, LPWSTR password )
197 LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW) hRequest;
198 LPWININETHTTPSESSIONW lpwhs;
199 LPWININETAPPINFOW hIC;
202 lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
203 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
205 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
209 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
211 p = HeapAlloc( GetProcessHeap(), 0, (strlenW( username ) + 1)*sizeof(WCHAR) );
215 lstrcpyW( p, username );
216 hIC->lpszProxyUsername = p;
218 p = HeapAlloc( GetProcessHeap(), 0, (strlenW( password ) + 1)*sizeof(WCHAR) );
222 lstrcpyW( p, password );
223 hIC->lpszProxyPassword = p;
228 /***********************************************************************
229 * WININET_ProxyPasswordDialog
231 static INT_PTR WINAPI WININET_ProxyPasswordDialog(
232 HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
235 struct WININET_ErrorDlgParams *params;
236 WCHAR szRealm[0x80], szServer[0x80];
238 if( uMsg == WM_INITDIALOG )
240 TRACE("WM_INITDIALOG (%08lx)\n", lParam);
242 /* save the parameter list */
243 params = (struct WININET_ErrorDlgParams*) lParam;
244 SetWindowLongW( hdlg, GWL_USERDATA, lParam );
246 /* extract the Realm from the proxy response and show it */
247 if( WININET_GetAuthRealm( params->hRequest,
248 szRealm, sizeof szRealm/sizeof(WCHAR)) )
250 hitem = GetDlgItem( hdlg, IDC_REALM );
251 SetWindowTextW( hitem, szRealm );
254 /* extract the name of the proxy server */
255 if( WININET_GetProxyServer( params->hRequest,
256 szServer, sizeof szServer/sizeof(WCHAR)) )
258 hitem = GetDlgItem( hdlg, IDC_PROXY );
259 SetWindowTextW( hitem, szServer );
262 WININET_GetSetPassword( hdlg, szServer, szRealm, FALSE );
267 params = (struct WININET_ErrorDlgParams*)
268 GetWindowLongW( hdlg, GWL_USERDATA );
275 LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW) params->hRequest;
276 WCHAR username[0x20], password[0x20];
279 hitem = GetDlgItem( hdlg, IDC_USERNAME );
281 GetWindowTextW( hitem, username, sizeof username/sizeof(WCHAR) );
284 hitem = GetDlgItem( hdlg, IDC_PASSWORD );
286 GetWindowTextW( hitem, password, sizeof password/sizeof(WCHAR) );
288 hitem = GetDlgItem( hdlg, IDC_SAVEPASSWORD );
290 SendMessageW( hitem, BM_GETSTATE, 0, 0 ) &&
291 WININET_GetAuthRealm( params->hRequest,
292 szRealm, sizeof szRealm/sizeof(WCHAR)) &&
293 WININET_GetProxyServer( params->hRequest,
294 szServer, sizeof szServer/sizeof(WCHAR)) )
296 WININET_GetSetPassword( hdlg, szServer, szRealm, TRUE );
298 WININET_SetProxyAuthorization( lpwhr, username, password );
300 EndDialog( hdlg, ERROR_INTERNET_FORCE_RETRY );
303 if( wParam == IDCANCEL )
305 EndDialog( hdlg, 0 );
313 /***********************************************************************
314 * WININET_GetConnectionStatus
316 static INT WININET_GetConnectionStatus( HINTERNET hRequest )
318 WCHAR szStatus[0x20];
319 DWORD sz, index, dwStatus;
321 TRACE("%p\n", hRequest );
323 sz = sizeof szStatus;
325 if( !HttpQueryInfoW( hRequest, HTTP_QUERY_STATUS_CODE,
326 szStatus, &sz, &index))
328 dwStatus = atoiW( szStatus );
330 TRACE("request %p status = %ld\n", hRequest, dwStatus );
336 /***********************************************************************
339 DWORD WINAPI InternetErrorDlg(HWND hWnd, HINTERNET hRequest,
340 DWORD dwError, DWORD dwFlags, LPVOID* lppvData)
342 struct WININET_ErrorDlgParams params;
343 HMODULE hwininet = GetModuleHandleA( "wininet.dll" );
346 TRACE("%p %p %ld %08lx %p\n", hWnd, hRequest, dwError, dwFlags, lppvData);
349 params.hRequest = hRequest;
350 params.dwError = dwError;
351 params.dwFlags = dwFlags;
352 params.lppvData = lppvData;
357 if( !(dwFlags & FLAGS_ERROR_UI_FILTER_FOR_ERRORS ) )
359 dwStatus = WININET_GetConnectionStatus( hRequest );
360 if( HTTP_STATUS_PROXY_AUTH_REQ != dwStatus )
361 return ERROR_SUCCESS;
362 return DialogBoxParamW( hwininet, MAKEINTRESOURCEW( IDD_PROXYDLG ),
363 hWnd, WININET_ProxyPasswordDialog, (LPARAM) ¶ms );
365 case ERROR_INTERNET_INCORRECT_PASSWORD:
366 return DialogBoxParamW( hwininet, MAKEINTRESOURCEW( IDD_PROXYDLG ),
367 hWnd, WININET_ProxyPasswordDialog, (LPARAM) ¶ms );
369 case ERROR_INTERNET_HTTP_TO_HTTPS_ON_REDIR:
370 case ERROR_INTERNET_INVALID_CA:
371 case ERROR_INTERNET_POST_IS_NON_SECURE:
372 case ERROR_INTERNET_SEC_CERT_CN_INVALID:
373 case ERROR_INTERNET_SEC_CERT_DATE_INVALID:
374 FIXME("Need to display dialog for error %ld\n", dwError);
375 return ERROR_SUCCESS;
377 return ERROR_INVALID_PARAMETER;