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
, LPSTR szBuf
, DWORD sz
)
61 LPWININETHTTPREQA lpwhr
= (LPWININETHTTPREQA
) hRequest
;
62 LPWININETHTTPSESSIONA lpwhs
= NULL
;
63 LPWININETAPPINFOA hIC
= NULL
;
69 lpwhs
= (LPWININETHTTPSESSIONA
) lpwhr
->hdr
.lpwhparent
;
73 hIC
= (LPWININETAPPINFOA
) lpwhs
->hdr
.lpwhparent
;
77 strncpy(szBuf
, hIC
->lpszProxy
, sz
);
79 /* FIXME: perhaps it would be better to use InternetCrackUrl here */
80 p
= strchr(szBuf
, ':');
87 /***********************************************************************
88 * WININET_GetAuthRealm
90 * Determine the name of the (basic) Authentication realm
92 static BOOL
WININET_GetAuthRealm( HINTERNET hRequest
, LPSTR szBuf
, DWORD sz
)
97 /* extract the Realm from the proxy response and show it */
99 if( !HttpQueryInfoA( hRequest
, HTTP_QUERY_PROXY_AUTHENTICATE
,
100 szBuf
, &sz
, &index
) )
104 * FIXME: maybe we should check that we're
105 * dealing with 'Basic' Authentication
107 p
= strchr( szBuf
, ' ' );
108 if( p
&& !strncmp( p
+1, "realm=", 6 ) )
115 q
= strrchr( p
, '"' );
126 /***********************************************************************
127 * WININET_GetSetPassword
129 static BOOL
WININET_GetSetPassword( HWND hdlg
, LPCSTR szServer
,
130 LPCSTR szRealm
, BOOL bSet
)
132 CHAR szResource
[0x80], szUserPass
[0x40];
134 HWND hUserItem
, hPassItem
;
135 DWORD r
, dwMagic
= 19;
139 hUserItem
= GetDlgItem( hdlg
, IDC_USERNAME
);
140 hPassItem
= GetDlgItem( hdlg
, IDC_PASSWORD
);
142 /* now try fetch the username and password */
143 strcpy( szResource
, szServer
);
144 strcat( szResource
, "/");
145 strcat( szResource
, szRealm
);
150 GetWindowTextA( hUserItem
, szUserPass
, sizeof szUserPass
-1 );
151 strcat(szUserPass
, ":");
152 len
= strlen( szUserPass
);
153 GetWindowTextA( hPassItem
, szUserPass
+len
, sizeof szUserPass
-len
);
155 r
= WNetCachePassword( szResource
, strlen( szResource
) + 1,
156 szUserPass
, strlen( szUserPass
) + 1, dwMagic
, 0 );
158 return ( r
== WN_SUCCESS
);
161 sz
= sizeof szUserPass
;
162 r
= WNetGetCachedPassword( szResource
, strlen( szResource
) + 1,
163 szUserPass
, &sz
, dwMagic
);
164 if( r
!= WN_SUCCESS
)
167 p
= strchr( szUserPass
, ':' );
171 SetWindowTextA( hUserItem
, szUserPass
);
172 SetWindowTextA( hPassItem
, p
+1 );
178 /***********************************************************************
179 * WININET_SetProxyAuthorization
181 static BOOL
WININET_SetProxyAuthorization( HINTERNET hRequest
,
182 LPSTR username
, LPSTR password
)
184 LPWININETHTTPREQA lpwhr
= (LPWININETHTTPREQA
) hRequest
;
185 LPWININETHTTPSESSIONA lpwhs
;
186 LPWININETAPPINFOA hIC
;
189 lpwhs
= (LPWININETHTTPSESSIONA
) lpwhr
->hdr
.lpwhparent
;
190 if (NULL
== lpwhs
|| lpwhs
->hdr
.htype
!= WH_HHTTPSESSION
)
192 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
196 hIC
= (LPWININETAPPINFOA
) lpwhs
->hdr
.lpwhparent
;
198 p
= HeapAlloc( GetProcessHeap(), 0, strlen( username
) + 1 );
202 strcpy( p
, username
);
203 hIC
->lpszProxyUsername
= p
;
205 p
= HeapAlloc( GetProcessHeap(), 0, strlen( password
) + 1 );
209 strcpy( p
, password
);
210 hIC
->lpszProxyPassword
= p
;
215 /***********************************************************************
216 * WININET_ProxyPasswordDialog
218 static INT_PTR WINAPI
WININET_ProxyPasswordDialog(
219 HWND hdlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
222 struct WININET_ErrorDlgParams
*params
;
223 CHAR szRealm
[0x80], szServer
[0x80];
225 if( uMsg
== WM_INITDIALOG
)
227 TRACE("WM_INITDIALOG (%08lx)\n", lParam
);
229 /* save the parameter list */
230 params
= (struct WININET_ErrorDlgParams
*) lParam
;
231 SetWindowLongW( hdlg
, GWL_USERDATA
, lParam
);
233 /* extract the Realm from the proxy response and show it */
234 if( WININET_GetAuthRealm( params
->hRequest
,
235 szRealm
, sizeof szRealm
) )
237 hitem
= GetDlgItem( hdlg
, IDC_REALM
);
238 SetWindowTextA( hitem
, szRealm
);
241 /* extract the name of the proxy server */
242 if( WININET_GetProxyServer( params
->hRequest
,
243 szServer
, sizeof szServer
) )
245 hitem
= GetDlgItem( hdlg
, IDC_PROXY
);
246 SetWindowTextA( hitem
, szServer
);
249 WININET_GetSetPassword( hdlg
, szServer
, szRealm
, FALSE
);
254 params
= (struct WININET_ErrorDlgParams
*)
255 GetWindowLongW( hdlg
, GWL_USERDATA
);
262 LPWININETHTTPREQA lpwhr
= (LPWININETHTTPREQA
) params
->hRequest
;
263 CHAR username
[0x20], password
[0x20];
266 hitem
= GetDlgItem( hdlg
, IDC_USERNAME
);
268 GetWindowTextA( hitem
, username
, sizeof username
);
271 hitem
= GetDlgItem( hdlg
, IDC_PASSWORD
);
273 GetWindowTextA( hitem
, password
, sizeof password
);
275 hitem
= GetDlgItem( hdlg
, IDC_SAVEPASSWORD
);
277 SendMessageA( hitem
, BM_GETSTATE
, 0, 0 ) &&
278 WININET_GetAuthRealm( params
->hRequest
,
279 szRealm
, sizeof szRealm
) &&
280 WININET_GetProxyServer( params
->hRequest
,
281 szServer
, sizeof szServer
) )
283 WININET_GetSetPassword( hdlg
, szServer
, szRealm
, TRUE
);
285 WININET_SetProxyAuthorization( lpwhr
, username
, password
);
287 EndDialog( hdlg
, ERROR_INTERNET_FORCE_RETRY
);
290 if( wParam
== IDCANCEL
)
292 EndDialog( hdlg
, 0 );
300 /***********************************************************************
301 * WININET_GetConnectionStatus
303 static INT
WININET_GetConnectionStatus( HINTERNET hRequest
)
306 DWORD sz
, index
, dwStatus
;
308 TRACE("%p\n", hRequest
);
310 sz
= sizeof szStatus
;
312 if( !HttpQueryInfoA( hRequest
, HTTP_QUERY_STATUS_CODE
,
313 szStatus
, &sz
, &index
))
315 dwStatus
= atoi( szStatus
);
317 TRACE("request %p status = %ld\n", hRequest
, dwStatus
);
323 /***********************************************************************
326 DWORD WINAPI
InternetErrorDlg(HWND hWnd
, HINTERNET hRequest
,
327 DWORD dwError
, DWORD dwFlags
, LPVOID
* lppvData
)
329 struct WININET_ErrorDlgParams params
;
330 HMODULE hwininet
= GetModuleHandleA( "wininet.dll" );
333 TRACE("%p %p %ld %08lx %p\n", hWnd
, hRequest
, dwError
, dwFlags
, lppvData
);
336 params
.hRequest
= hRequest
;
337 params
.dwError
= dwError
;
338 params
.dwFlags
= dwFlags
;
339 params
.lppvData
= lppvData
;
344 if( !(dwFlags
& FLAGS_ERROR_UI_FILTER_FOR_ERRORS
) )
346 dwStatus
= WININET_GetConnectionStatus( hRequest
);
347 if( HTTP_STATUS_PROXY_AUTH_REQ
!= dwStatus
)
348 return ERROR_SUCCESS
;
349 return DialogBoxParamW( hwininet
, MAKEINTRESOURCEW( IDD_PROXYDLG
),
350 hWnd
, WININET_ProxyPasswordDialog
, (LPARAM
) ¶ms
);
352 case ERROR_INTERNET_INCORRECT_PASSWORD
:
353 return DialogBoxParamW( hwininet
, MAKEINTRESOURCEW( IDD_PROXYDLG
),
354 hWnd
, WININET_ProxyPasswordDialog
, (LPARAM
) ¶ms
);
356 case ERROR_INTERNET_HTTP_TO_HTTPS_ON_REDIR
:
357 case ERROR_INTERNET_INVALID_CA
:
358 case ERROR_INTERNET_POST_IS_NON_SECURE
:
359 case ERROR_INTERNET_SEC_CERT_CN_INVALID
:
360 case ERROR_INTERNET_SEC_CERT_DATE_INVALID
:
361 FIXME("Need to display dialog for error %ld\n", dwError
);
362 return ERROR_SUCCESS
;
364 return ERROR_INVALID_PARAMETER
;