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 static const 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 static const WCHAR szColon
[] = { ':',0 };
140 static const WCHAR szbs
[] = { '/', 0 };
142 hUserItem
= GetDlgItem( hdlg
, IDC_USERNAME
);
143 hPassItem
= GetDlgItem( hdlg
, IDC_PASSWORD
);
145 /* now try fetch the username and password */
146 lstrcpyW( szResource
, szServer
);
147 lstrcatW( szResource
, szbs
);
148 lstrcatW( szResource
, szRealm
);
151 * WNetCachePassword is only concerned with the length
152 * of the data stored (which we tell it) and it does
153 * not use strlen() internally so we can add WCHAR data
154 * instead of ASCII data and get it back the same way.
159 GetWindowTextW( hUserItem
, szUserPass
,
160 (sizeof szUserPass
-1)/sizeof(WCHAR
) );
161 lstrcatW(szUserPass
, szColon
);
162 u_len
= strlenW( szUserPass
);
163 GetWindowTextW( hPassItem
, szUserPass
+u_len
,
164 (sizeof szUserPass
)/sizeof(WCHAR
)-u_len
);
166 r_len
= (strlenW( szResource
) + 1)*sizeof(WCHAR
);
167 u_len
= (strlenW( szUserPass
) + 1)*sizeof(WCHAR
);
168 r
= WNetCachePassword( (CHAR
*)szResource
, r_len
,
169 (CHAR
*)szUserPass
, u_len
, dwMagic
, 0 );
171 return ( r
== WN_SUCCESS
);
174 sz
= sizeof szUserPass
;
175 r_len
= (strlenW( szResource
) + 1)*sizeof(WCHAR
);
176 r
= WNetGetCachedPassword( (CHAR
*)szResource
, r_len
,
177 (CHAR
*)szUserPass
, &sz
, dwMagic
);
178 if( r
!= WN_SUCCESS
)
181 p
= strchrW( szUserPass
, ':' );
185 SetWindowTextW( hUserItem
, szUserPass
);
186 SetWindowTextW( hPassItem
, p
+1 );
192 /***********************************************************************
193 * WININET_SetProxyAuthorization
195 static BOOL
WININET_SetProxyAuthorization( HINTERNET hRequest
,
196 LPWSTR username
, LPWSTR password
)
198 LPWININETHTTPREQW lpwhr
= (LPWININETHTTPREQW
) hRequest
;
199 LPWININETHTTPSESSIONW lpwhs
;
200 LPWININETAPPINFOW hIC
;
203 lpwhs
= (LPWININETHTTPSESSIONW
) lpwhr
->hdr
.lpwhparent
;
204 if (NULL
== lpwhs
|| lpwhs
->hdr
.htype
!= WH_HHTTPSESSION
)
206 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
210 hIC
= (LPWININETAPPINFOW
) lpwhs
->hdr
.lpwhparent
;
212 p
= HeapAlloc( GetProcessHeap(), 0, (strlenW( username
) + 1)*sizeof(WCHAR
) );
216 lstrcpyW( p
, username
);
217 hIC
->lpszProxyUsername
= p
;
219 p
= HeapAlloc( GetProcessHeap(), 0, (strlenW( password
) + 1)*sizeof(WCHAR
) );
223 lstrcpyW( p
, password
);
224 hIC
->lpszProxyPassword
= p
;
229 /***********************************************************************
230 * WININET_ProxyPasswordDialog
232 static INT_PTR WINAPI
WININET_ProxyPasswordDialog(
233 HWND hdlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
236 struct WININET_ErrorDlgParams
*params
;
237 WCHAR szRealm
[0x80], szServer
[0x80];
239 if( uMsg
== WM_INITDIALOG
)
241 TRACE("WM_INITDIALOG (%08lx)\n", lParam
);
243 /* save the parameter list */
244 params
= (struct WININET_ErrorDlgParams
*) lParam
;
245 SetWindowLongW( hdlg
, GWL_USERDATA
, lParam
);
247 /* extract the Realm from the proxy response and show it */
248 if( WININET_GetAuthRealm( params
->hRequest
,
249 szRealm
, sizeof szRealm
/sizeof(WCHAR
)) )
251 hitem
= GetDlgItem( hdlg
, IDC_REALM
);
252 SetWindowTextW( hitem
, szRealm
);
255 /* extract the name of the proxy server */
256 if( WININET_GetProxyServer( params
->hRequest
,
257 szServer
, sizeof szServer
/sizeof(WCHAR
)) )
259 hitem
= GetDlgItem( hdlg
, IDC_PROXY
);
260 SetWindowTextW( hitem
, szServer
);
263 WININET_GetSetPassword( hdlg
, szServer
, szRealm
, FALSE
);
268 params
= (struct WININET_ErrorDlgParams
*)
269 GetWindowLongW( hdlg
, GWL_USERDATA
);
276 LPWININETHTTPREQW lpwhr
= (LPWININETHTTPREQW
) params
->hRequest
;
277 WCHAR username
[0x20], password
[0x20];
280 hitem
= GetDlgItem( hdlg
, IDC_USERNAME
);
282 GetWindowTextW( hitem
, username
, sizeof username
/sizeof(WCHAR
) );
285 hitem
= GetDlgItem( hdlg
, IDC_PASSWORD
);
287 GetWindowTextW( hitem
, password
, sizeof password
/sizeof(WCHAR
) );
289 hitem
= GetDlgItem( hdlg
, IDC_SAVEPASSWORD
);
291 SendMessageW( hitem
, BM_GETSTATE
, 0, 0 ) &&
292 WININET_GetAuthRealm( params
->hRequest
,
293 szRealm
, sizeof szRealm
/sizeof(WCHAR
)) &&
294 WININET_GetProxyServer( params
->hRequest
,
295 szServer
, sizeof szServer
/sizeof(WCHAR
)) )
297 WININET_GetSetPassword( hdlg
, szServer
, szRealm
, TRUE
);
299 WININET_SetProxyAuthorization( lpwhr
, username
, password
);
301 EndDialog( hdlg
, ERROR_INTERNET_FORCE_RETRY
);
304 if( wParam
== IDCANCEL
)
306 EndDialog( hdlg
, 0 );
314 /***********************************************************************
315 * WININET_GetConnectionStatus
317 static INT
WININET_GetConnectionStatus( HINTERNET hRequest
)
319 WCHAR szStatus
[0x20];
320 DWORD sz
, index
, dwStatus
;
322 TRACE("%p\n", hRequest
);
324 sz
= sizeof szStatus
;
326 if( !HttpQueryInfoW( hRequest
, HTTP_QUERY_STATUS_CODE
,
327 szStatus
, &sz
, &index
))
329 dwStatus
= atoiW( szStatus
);
331 TRACE("request %p status = %ld\n", hRequest
, dwStatus
);
337 /***********************************************************************
340 DWORD WINAPI
InternetErrorDlg(HWND hWnd
, HINTERNET hRequest
,
341 DWORD dwError
, DWORD dwFlags
, LPVOID
* lppvData
)
343 struct WININET_ErrorDlgParams params
;
344 HMODULE hwininet
= GetModuleHandleA( "wininet.dll" );
347 TRACE("%p %p %ld %08lx %p\n", hWnd
, hRequest
, dwError
, dwFlags
, lppvData
);
350 params
.hRequest
= hRequest
;
351 params
.dwError
= dwError
;
352 params
.dwFlags
= dwFlags
;
353 params
.lppvData
= lppvData
;
358 if( !(dwFlags
& FLAGS_ERROR_UI_FILTER_FOR_ERRORS
) )
360 dwStatus
= WININET_GetConnectionStatus( hRequest
);
361 if( HTTP_STATUS_PROXY_AUTH_REQ
!= dwStatus
)
362 return ERROR_SUCCESS
;
363 return DialogBoxParamW( hwininet
, MAKEINTRESOURCEW( IDD_PROXYDLG
),
364 hWnd
, WININET_ProxyPasswordDialog
, (LPARAM
) ¶ms
);
366 case ERROR_INTERNET_INCORRECT_PASSWORD
:
367 return DialogBoxParamW( hwininet
, MAKEINTRESOURCEW( IDD_PROXYDLG
),
368 hWnd
, WININET_ProxyPasswordDialog
, (LPARAM
) ¶ms
);
370 case ERROR_INTERNET_HTTP_TO_HTTPS_ON_REDIR
:
371 case ERROR_INTERNET_INVALID_CA
:
372 case ERROR_INTERNET_POST_IS_NON_SECURE
:
373 case ERROR_INTERNET_SEC_CERT_CN_INVALID
:
374 case ERROR_INTERNET_SEC_CERT_DATE_INVALID
:
375 FIXME("Need to display dialog for error %ld\n", dwError
);
376 return ERROR_SUCCESS
;
378 return ERROR_INVALID_PARAMETER
;