Make Unicode const strings static so they are not copied to the stack
[wine/hacks.git] / dlls / wininet / dialogs.c
blob9ca229066176a1499eda2dc4a71e468f756dc3cd
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
23 #include <stdarg.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winuser.h"
28 #include "winreg.h"
29 #include "wininet.h"
30 #include "winnetwk.h"
31 #include "winnls.h"
32 #include "wine/debug.h"
33 #include "winerror.h"
34 #define NO_SHLWAPI_STREAM
35 #include "shlwapi.h"
37 #include "internet.h"
39 #include "wine/unicode.h"
41 #include "resource.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
45 struct WININET_ErrorDlgParams
47 HWND hWnd;
48 HINTERNET hRequest;
49 DWORD dwError;
50 DWORD dwFlags;
51 LPVOID* lppvData;
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;
64 LPWSTR p;
66 if (NULL == lpwhr)
67 return FALSE;
69 lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
70 if (NULL == lpwhs)
71 return FALSE;
73 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
74 if (NULL == hIC)
75 return FALSE;
77 strncpyW(szBuf, hIC->lpszProxy, sz);
79 /* FIXME: perhaps it would be better to use InternetCrackUrl here */
80 p = strchrW(szBuf, ':');
81 if(*p)
82 *p = 0;
84 return TRUE;
87 /***********************************************************************
88 * WININET_GetAuthRealm
90 * Determine the name of the (basic) Authentication realm
92 static BOOL WININET_GetAuthRealm( HINTERNET hRequest, LPWSTR szBuf, DWORD sz )
94 LPWSTR p, q;
95 DWORD index;
96 static const WCHAR szRealm[] = { 'r','e','a','l','m','=',0 };
98 /* extract the Realm from the proxy response and show it */
99 index = 0;
100 if( !HttpQueryInfoW( hRequest, HTTP_QUERY_PROXY_AUTHENTICATE,
101 szBuf, &sz, &index) )
102 return FALSE;
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) ) )
111 /* remove quotes */
112 p += 7;
113 if( *p == '"' )
115 p++;
116 q = strrchrW( p, '"' );
117 if( q )
118 *q = 0;
122 strcpyW( szBuf, p );
124 return TRUE;
127 /***********************************************************************
128 * WININET_GetSetPassword
130 static BOOL WININET_GetSetPassword( HWND hdlg, LPCWSTR szServer,
131 LPCWSTR szRealm, BOOL bSet )
133 WCHAR szResource[0x80], szUserPass[0x40];
134 LPWSTR p;
135 HWND hUserItem, hPassItem;
136 DWORD r, dwMagic = 19;
137 UINT r_len, u_len;
138 WORD sz;
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.
156 if( bSet )
158 szUserPass[0] = 0;
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 )
179 return FALSE;
181 p = strchrW( szUserPass, ':' );
182 if( p )
184 *p = 0;
185 SetWindowTextW( hUserItem, szUserPass );
186 SetWindowTextW( hPassItem, p+1 );
189 return TRUE;
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;
201 LPWSTR p;
203 lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
204 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
206 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
207 return FALSE;
210 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
212 p = HeapAlloc( GetProcessHeap(), 0, (strlenW( username ) + 1)*sizeof(WCHAR) );
213 if( !p )
214 return FALSE;
216 lstrcpyW( p, username );
217 hIC->lpszProxyUsername = p;
219 p = HeapAlloc( GetProcessHeap(), 0, (strlenW( password ) + 1)*sizeof(WCHAR) );
220 if( !p )
221 return FALSE;
223 lstrcpyW( p, password );
224 hIC->lpszProxyPassword = p;
226 return TRUE;
229 /***********************************************************************
230 * WININET_ProxyPasswordDialog
232 static INT_PTR WINAPI WININET_ProxyPasswordDialog(
233 HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
235 HWND hitem;
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 );
265 return TRUE;
268 params = (struct WININET_ErrorDlgParams*)
269 GetWindowLongW( hdlg, GWL_USERDATA );
271 switch( uMsg )
273 case WM_COMMAND:
274 if( wParam == IDOK )
276 LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW) params->hRequest;
277 WCHAR username[0x20], password[0x20];
279 username[0] = 0;
280 hitem = GetDlgItem( hdlg, IDC_USERNAME );
281 if( hitem )
282 GetWindowTextW( hitem, username, sizeof username/sizeof(WCHAR) );
284 password[0] = 0;
285 hitem = GetDlgItem( hdlg, IDC_PASSWORD );
286 if( hitem )
287 GetWindowTextW( hitem, password, sizeof password/sizeof(WCHAR) );
289 hitem = GetDlgItem( hdlg, IDC_SAVEPASSWORD );
290 if( hitem &&
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 );
302 return TRUE;
304 if( wParam == IDCANCEL )
306 EndDialog( hdlg, 0 );
307 return TRUE;
309 break;
311 return FALSE;
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;
325 index = 0;
326 if( !HttpQueryInfoW( hRequest, HTTP_QUERY_STATUS_CODE,
327 szStatus, &sz, &index))
328 return -1;
329 dwStatus = atoiW( szStatus );
331 TRACE("request %p status = %ld\n", hRequest, dwStatus );
333 return dwStatus;
337 /***********************************************************************
338 * InternetErrorDlg
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" );
345 INT dwStatus;
347 TRACE("%p %p %ld %08lx %p\n", hWnd, hRequest, dwError, dwFlags, lppvData);
349 params.hWnd = hWnd;
350 params.hRequest = hRequest;
351 params.dwError = dwError;
352 params.dwFlags = dwFlags;
353 params.lppvData = lppvData;
355 switch( dwError )
357 case ERROR_SUCCESS:
358 if( !(dwFlags & FLAGS_ERROR_UI_FILTER_FOR_ERRORS ) )
359 return 0;
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) &params );
366 case ERROR_INTERNET_INCORRECT_PASSWORD:
367 return DialogBoxParamW( hwininet, MAKEINTRESOURCEW( IDD_PROXYDLG ),
368 hWnd, WININET_ProxyPasswordDialog, (LPARAM) &params );
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;