winmm: Remove unused variable and if check (Coverity).
[wine.git] / dlls / wininet / dialogs.c
blobfc473be5b0872ba9a45d539b79eecd9a21dfbd15
1 /*
2 * Wininet
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <stdarg.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "winreg.h"
30 #include "wininet.h"
31 #include "winnetwk.h"
32 #include "winnls.h"
33 #include "wine/debug.h"
34 #include "winerror.h"
35 #define NO_SHLWAPI_STREAM
36 #include "shlwapi.h"
38 #include "internet.h"
40 #include "wine/unicode.h"
42 #include "resource.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
46 struct WININET_ErrorDlgParams
48 HWND hWnd;
49 HINTERNET hRequest;
50 DWORD dwError;
51 DWORD dwFlags;
52 LPVOID* lppvData;
55 /***********************************************************************
56 * WININET_GetProxyServer
58 * Determine the name of the proxy server the request is using
60 static BOOL WININET_GetProxyServer( HINTERNET hRequest, LPWSTR szBuf, DWORD sz )
62 LPWININETHTTPREQW lpwhr;
63 LPWININETHTTPSESSIONW lpwhs = NULL;
64 LPWININETAPPINFOW hIC = NULL;
65 LPWSTR p;
67 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hRequest );
68 if (NULL == lpwhr)
69 return FALSE;
71 lpwhs = lpwhr->lpHttpSession;
72 if (NULL == lpwhs)
73 return FALSE;
75 hIC = lpwhs->lpAppInfo;
76 if (NULL == hIC)
77 return FALSE;
79 lstrcpynW(szBuf, hIC->lpszProxy, sz);
81 /* FIXME: perhaps it would be better to use InternetCrackUrl here */
82 p = strchrW(szBuf, ':');
83 if (p)
84 *p = 0;
86 return TRUE;
89 /***********************************************************************
90 * WININET_GetAuthRealm
92 * Determine the name of the (basic) Authentication realm
94 static BOOL WININET_GetAuthRealm( HINTERNET hRequest, LPWSTR szBuf, DWORD sz )
96 LPWSTR p, q;
97 DWORD index;
98 static const WCHAR szRealm[] = { 'r','e','a','l','m','=',0 };
100 /* extract the Realm from the proxy response and show it */
101 index = 0;
102 if( !HttpQueryInfoW( hRequest, HTTP_QUERY_PROXY_AUTHENTICATE,
103 szBuf, &sz, &index) )
104 return FALSE;
107 * FIXME: maybe we should check that we're
108 * dealing with 'Basic' Authentication
110 p = strchrW( szBuf, ' ' );
111 if( !p || strncmpW( p+1, szRealm, strlenW(szRealm) ) )
113 ERR("proxy response wrong? (%s)\n", debugstr_w(szBuf));
114 return FALSE;
118 /* remove quotes */
119 p += 7;
120 if( *p == '"' )
122 p++;
123 q = strrchrW( p, '"' );
124 if( q )
125 *q = 0;
127 strcpyW( szBuf, p );
129 return TRUE;
132 /***********************************************************************
133 * WININET_GetSetPassword
135 static BOOL WININET_GetSetPassword( HWND hdlg, LPCWSTR szServer,
136 LPCWSTR szRealm, BOOL bSet )
138 WCHAR szResource[0x80], szUserPass[0x40];
139 LPWSTR p;
140 HWND hUserItem, hPassItem;
141 DWORD r, dwMagic = 19;
142 UINT r_len, u_len;
143 WORD sz;
144 static const WCHAR szColon[] = { ':',0 };
145 static const WCHAR szbs[] = { '/', 0 };
147 hUserItem = GetDlgItem( hdlg, IDC_USERNAME );
148 hPassItem = GetDlgItem( hdlg, IDC_PASSWORD );
150 /* now try fetch the username and password */
151 lstrcpyW( szResource, szServer);
152 lstrcatW( szResource, szbs);
153 lstrcatW( szResource, szRealm);
156 * WNetCachePassword is only concerned with the length
157 * of the data stored (which we tell it) and it does
158 * not use strlen() internally so we can add WCHAR data
159 * instead of ASCII data and get it back the same way.
161 if( bSet )
163 szUserPass[0] = 0;
164 GetWindowTextW( hUserItem, szUserPass,
165 (sizeof szUserPass-1)/sizeof(WCHAR) );
166 lstrcatW(szUserPass, szColon);
167 u_len = strlenW( szUserPass );
168 GetWindowTextW( hPassItem, szUserPass+u_len,
169 (sizeof szUserPass)/sizeof(WCHAR)-u_len );
171 r_len = (strlenW( szResource ) + 1)*sizeof(WCHAR);
172 u_len = (strlenW( szUserPass ) + 1)*sizeof(WCHAR);
173 r = WNetCachePassword( (CHAR*)szResource, r_len,
174 (CHAR*)szUserPass, u_len, dwMagic, 0 );
176 return ( r == WN_SUCCESS );
179 sz = sizeof szUserPass;
180 r_len = (strlenW( szResource ) + 1)*sizeof(WCHAR);
181 r = WNetGetCachedPassword( (CHAR*)szResource, r_len,
182 (CHAR*)szUserPass, &sz, dwMagic );
183 if( r != WN_SUCCESS )
184 return FALSE;
186 p = strchrW( szUserPass, ':' );
187 if( p )
189 *p = 0;
190 SetWindowTextW( hUserItem, szUserPass );
191 SetWindowTextW( hPassItem, p+1 );
194 return TRUE;
197 /***********************************************************************
198 * WININET_SetProxyAuthorization
200 static BOOL WININET_SetProxyAuthorization( HINTERNET hRequest,
201 LPWSTR username, LPWSTR password )
203 LPWININETHTTPREQW lpwhr;
204 LPWININETHTTPSESSIONW lpwhs;
205 LPWININETAPPINFOW hIC;
206 LPWSTR p;
208 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hRequest );
209 if( !lpwhr )
210 return FALSE;
212 lpwhs = lpwhr->lpHttpSession;
213 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
215 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
216 return FALSE;
219 hIC = lpwhs->lpAppInfo;
221 p = HeapAlloc( GetProcessHeap(), 0, (strlenW( username ) + 1)*sizeof(WCHAR) );
222 if( !p )
223 return FALSE;
225 lstrcpyW( p, username );
226 hIC->lpszProxyUsername = p;
228 p = HeapAlloc( GetProcessHeap(), 0, (strlenW( password ) + 1)*sizeof(WCHAR) );
229 if( !p )
230 return FALSE;
232 lstrcpyW( p, password );
233 hIC->lpszProxyPassword = p;
235 return TRUE;
238 /***********************************************************************
239 * WININET_ProxyPasswordDialog
241 static INT_PTR WINAPI WININET_ProxyPasswordDialog(
242 HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
244 HWND hitem;
245 struct WININET_ErrorDlgParams *params;
246 WCHAR szRealm[0x80], szServer[0x80];
248 if( uMsg == WM_INITDIALOG )
250 TRACE("WM_INITDIALOG (%08lx)\n", lParam);
252 /* save the parameter list */
253 params = (struct WININET_ErrorDlgParams*) lParam;
254 SetWindowLongPtrW( hdlg, GWLP_USERDATA, lParam );
256 /* extract the Realm from the proxy response and show it */
257 if( WININET_GetAuthRealm( params->hRequest,
258 szRealm, sizeof szRealm/sizeof(WCHAR)) )
260 hitem = GetDlgItem( hdlg, IDC_REALM );
261 SetWindowTextW( hitem, szRealm );
264 /* extract the name of the proxy server */
265 if( WININET_GetProxyServer( params->hRequest,
266 szServer, sizeof szServer/sizeof(WCHAR)) )
268 hitem = GetDlgItem( hdlg, IDC_PROXY );
269 SetWindowTextW( hitem, szServer );
272 WININET_GetSetPassword( hdlg, szServer, szRealm, FALSE );
274 return TRUE;
277 params = (struct WININET_ErrorDlgParams*)
278 GetWindowLongPtrW( hdlg, GWLP_USERDATA );
280 switch( uMsg )
282 case WM_COMMAND:
283 if( wParam == IDOK )
285 WCHAR username[0x20], password[0x20];
287 username[0] = 0;
288 hitem = GetDlgItem( hdlg, IDC_USERNAME );
289 if( hitem )
290 GetWindowTextW( hitem, username, sizeof username/sizeof(WCHAR) );
292 password[0] = 0;
293 hitem = GetDlgItem( hdlg, IDC_PASSWORD );
294 if( hitem )
295 GetWindowTextW( hitem, password, sizeof password/sizeof(WCHAR) );
297 hitem = GetDlgItem( hdlg, IDC_SAVEPASSWORD );
298 if( hitem &&
299 SendMessageW( hitem, BM_GETSTATE, 0, 0 ) &&
300 WININET_GetAuthRealm( params->hRequest,
301 szRealm, sizeof szRealm/sizeof(WCHAR)) &&
302 WININET_GetProxyServer( params->hRequest,
303 szServer, sizeof szServer/sizeof(WCHAR)) )
305 WININET_GetSetPassword( hdlg, szServer, szRealm, TRUE );
307 WININET_SetProxyAuthorization( params->hRequest, username, password );
309 EndDialog( hdlg, ERROR_INTERNET_FORCE_RETRY );
310 return TRUE;
312 if( wParam == IDCANCEL )
314 EndDialog( hdlg, 0 );
315 return TRUE;
317 break;
319 return FALSE;
322 /***********************************************************************
323 * WININET_GetConnectionStatus
325 static INT WININET_GetConnectionStatus( HINTERNET hRequest )
327 WCHAR szStatus[0x20];
328 DWORD sz, index, dwStatus;
330 TRACE("%p\n", hRequest );
332 sz = sizeof szStatus;
333 index = 0;
334 if( !HttpQueryInfoW( hRequest, HTTP_QUERY_STATUS_CODE,
335 szStatus, &sz, &index))
336 return -1;
337 dwStatus = atoiW( szStatus );
339 TRACE("request %p status = %d\n", hRequest, dwStatus );
341 return dwStatus;
345 /***********************************************************************
346 * InternetErrorDlg
348 DWORD WINAPI InternetErrorDlg(HWND hWnd, HINTERNET hRequest,
349 DWORD dwError, DWORD dwFlags, LPVOID* lppvData)
351 struct WININET_ErrorDlgParams params;
352 HMODULE hwininet = GetModuleHandleA( "wininet.dll" );
353 INT dwStatus;
355 TRACE("%p %p %d %08x %p\n", hWnd, hRequest, dwError, dwFlags, lppvData);
357 params.hWnd = hWnd;
358 params.hRequest = hRequest;
359 params.dwError = dwError;
360 params.dwFlags = dwFlags;
361 params.lppvData = lppvData;
363 switch( dwError )
365 case ERROR_SUCCESS:
366 if( !(dwFlags & FLAGS_ERROR_UI_FILTER_FOR_ERRORS ) )
367 return 0;
368 dwStatus = WININET_GetConnectionStatus( hRequest );
369 if( HTTP_STATUS_PROXY_AUTH_REQ != dwStatus )
370 return ERROR_SUCCESS;
371 return DialogBoxParamW( hwininet, MAKEINTRESOURCEW( IDD_PROXYDLG ),
372 hWnd, WININET_ProxyPasswordDialog, (LPARAM) &params );
374 case ERROR_INTERNET_INCORRECT_PASSWORD:
375 return DialogBoxParamW( hwininet, MAKEINTRESOURCEW( IDD_PROXYDLG ),
376 hWnd, WININET_ProxyPasswordDialog, (LPARAM) &params );
378 case ERROR_INTERNET_HTTP_TO_HTTPS_ON_REDIR:
379 case ERROR_INTERNET_INVALID_CA:
380 case ERROR_INTERNET_POST_IS_NON_SECURE:
381 case ERROR_INTERNET_SEC_CERT_CN_INVALID:
382 case ERROR_INTERNET_SEC_CERT_DATE_INVALID:
383 FIXME("Need to display dialog for error %d\n", dwError);
384 return ERROR_SUCCESS;
386 return ERROR_INVALID_PARAMETER;