[WININET] Sync with Wine Staging 1.7.55. CORE-10536
[reactos.git] / reactos / dll / win32 / wininet / internet.c
1 /*
2 * Wininet
3 *
4 * Copyright 1999 Corel Corporation
5 * Copyright 2002 CodeWeavers Inc.
6 * Copyright 2002 Jaco Greeff
7 * Copyright 2002 TransGaming Technologies Inc.
8 * Copyright 2004 Mike McCormack for CodeWeavers
9 *
10 * Ulrich Czekalla
11 * Aric Stewart
12 * David Hammerton
13 *
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 */
28
29 #include "internet.h"
30
31 typedef struct
32 {
33 DWORD dwError;
34 CHAR response[MAX_REPLY_LEN];
35 } WITHREADERROR, *LPWITHREADERROR;
36
37 static DWORD g_dwTlsErrIndex = TLS_OUT_OF_INDEXES;
38 HMODULE WININET_hModule;
39
40 static CRITICAL_SECTION WININET_cs;
41 static CRITICAL_SECTION_DEBUG WININET_cs_debug =
42 {
43 0, 0, &WININET_cs,
44 { &WININET_cs_debug.ProcessLocksList, &WININET_cs_debug.ProcessLocksList },
45 0, 0, { (DWORD_PTR)(__FILE__ ": WININET_cs") }
46 };
47 static CRITICAL_SECTION WININET_cs = { &WININET_cs_debug, -1, 0, 0, 0, 0 };
48
49 static object_header_t **handle_table;
50 static UINT_PTR next_handle;
51 static UINT_PTR handle_table_size;
52
53 typedef struct
54 {
55 DWORD proxyEnabled;
56 LPWSTR proxy;
57 LPWSTR proxyBypass;
58 LPWSTR proxyUsername;
59 LPWSTR proxyPassword;
60 } proxyinfo_t;
61
62 static ULONG max_conns = 2, max_1_0_conns = 4;
63 static ULONG connect_timeout = 60000;
64
65 static const WCHAR szInternetSettings[] =
66 { 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
67 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
68 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s',0 };
69 static const WCHAR szProxyServer[] = { 'P','r','o','x','y','S','e','r','v','e','r', 0 };
70 static const WCHAR szProxyEnable[] = { 'P','r','o','x','y','E','n','a','b','l','e', 0 };
71 static const WCHAR szProxyOverride[] = { 'P','r','o','x','y','O','v','e','r','r','i','d','e', 0 };
72
73 void *alloc_object(object_header_t *parent, const object_vtbl_t *vtbl, size_t size)
74 {
75 UINT_PTR handle = 0, num;
76 object_header_t *ret;
77 object_header_t **p;
78 BOOL res = TRUE;
79
80 ret = heap_alloc_zero(size);
81 if(!ret)
82 return NULL;
83
84 list_init(&ret->children);
85
86 EnterCriticalSection( &WININET_cs );
87
88 if(!handle_table_size) {
89 num = 16;
90 p = heap_alloc_zero(sizeof(handle_table[0]) * num);
91 if(p) {
92 handle_table = p;
93 handle_table_size = num;
94 next_handle = 1;
95 }else {
96 res = FALSE;
97 }
98 }else if(next_handle == handle_table_size) {
99 num = handle_table_size * 2;
100 p = heap_realloc_zero(handle_table, sizeof(handle_table[0]) * num);
101 if(p) {
102 handle_table = p;
103 handle_table_size = num;
104 }else {
105 res = FALSE;
106 }
107 }
108
109 if(res) {
110 handle = next_handle;
111 if(handle_table[handle])
112 ERR("handle isn't free but should be\n");
113 handle_table[handle] = ret;
114 ret->valid_handle = TRUE;
115
116 while(handle_table[next_handle] && next_handle < handle_table_size)
117 next_handle++;
118 }
119
120 LeaveCriticalSection( &WININET_cs );
121
122 if(!res) {
123 heap_free(ret);
124 return NULL;
125 }
126
127 ret->vtbl = vtbl;
128 ret->refs = 1;
129 ret->hInternet = (HINTERNET)handle;
130
131 if(parent) {
132 ret->lpfnStatusCB = parent->lpfnStatusCB;
133 ret->dwInternalFlags = parent->dwInternalFlags & INET_CALLBACKW;
134 }
135
136 return ret;
137 }
138
139 object_header_t *WININET_AddRef( object_header_t *info )
140 {
141 ULONG refs = InterlockedIncrement(&info->refs);
142 TRACE("%p -> refcount = %d\n", info, refs );
143 return info;
144 }
145
146 object_header_t *get_handle_object( HINTERNET hinternet )
147 {
148 object_header_t *info = NULL;
149 UINT_PTR handle = (UINT_PTR) hinternet;
150
151 EnterCriticalSection( &WININET_cs );
152
153 if(handle > 0 && handle < handle_table_size && handle_table[handle] && handle_table[handle]->valid_handle)
154 info = WININET_AddRef(handle_table[handle]);
155
156 LeaveCriticalSection( &WININET_cs );
157
158 TRACE("handle %ld -> %p\n", handle, info);
159
160 return info;
161 }
162
163 static void invalidate_handle(object_header_t *info)
164 {
165 object_header_t *child, *next;
166
167 if(!info->valid_handle)
168 return;
169 info->valid_handle = FALSE;
170
171 /* Free all children as native does */
172 LIST_FOR_EACH_ENTRY_SAFE( child, next, &info->children, object_header_t, entry )
173 {
174 TRACE("invalidating child handle %p for parent %p\n", child->hInternet, info);
175 invalidate_handle( child );
176 }
177
178 WININET_Release(info);
179 }
180
181 BOOL WININET_Release( object_header_t *info )
182 {
183 ULONG refs = InterlockedDecrement(&info->refs);
184 TRACE( "object %p refcount = %d\n", info, refs );
185 if( !refs )
186 {
187 invalidate_handle(info);
188 if ( info->vtbl->CloseConnection )
189 {
190 TRACE( "closing connection %p\n", info);
191 info->vtbl->CloseConnection( info );
192 }
193 /* Don't send a callback if this is a session handle created with InternetOpenUrl */
194 if ((info->htype != WH_HHTTPSESSION && info->htype != WH_HFTPSESSION)
195 || !(info->dwInternalFlags & INET_OPENURL))
196 {
197 INTERNET_SendCallback(info, info->dwContext,
198 INTERNET_STATUS_HANDLE_CLOSING, &info->hInternet,
199 sizeof(HINTERNET));
200 }
201 TRACE( "destroying object %p\n", info);
202 if ( info->htype != WH_HINIT )
203 list_remove( &info->entry );
204 info->vtbl->Destroy( info );
205
206 if(info->hInternet) {
207 UINT_PTR handle = (UINT_PTR)info->hInternet;
208
209 EnterCriticalSection( &WININET_cs );
210
211 handle_table[handle] = NULL;
212 if(next_handle > handle)
213 next_handle = handle;
214
215 LeaveCriticalSection( &WININET_cs );
216 }
217
218 heap_free(info);
219 }
220 return TRUE;
221 }
222
223 /***********************************************************************
224 * DllMain [Internal] Initializes the internal 'WININET.DLL'.
225 *
226 * PARAMS
227 * hinstDLL [I] handle to the DLL's instance
228 * fdwReason [I]
229 * lpvReserved [I] reserved, must be NULL
230 *
231 * RETURNS
232 * Success: TRUE
233 * Failure: FALSE
234 */
235
236 BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
237 {
238 TRACE("%p,%x,%p\n", hinstDLL, fdwReason, lpvReserved);
239
240 switch (fdwReason) {
241 case DLL_PROCESS_ATTACH:
242
243 g_dwTlsErrIndex = TlsAlloc();
244
245 if (g_dwTlsErrIndex == TLS_OUT_OF_INDEXES)
246 return FALSE;
247
248 if(!init_urlcache())
249 {
250 TlsFree(g_dwTlsErrIndex);
251 return FALSE;
252 }
253
254 WININET_hModule = hinstDLL;
255 break;
256
257 case DLL_THREAD_ATTACH:
258 break;
259
260 case DLL_THREAD_DETACH:
261 if (g_dwTlsErrIndex != TLS_OUT_OF_INDEXES)
262 {
263 heap_free(TlsGetValue(g_dwTlsErrIndex));
264 }
265 break;
266
267 case DLL_PROCESS_DETACH:
268 if (lpvReserved) break;
269 collect_connections(COLLECT_CLEANUP);
270 NETCON_unload();
271 free_urlcache();
272 free_cookie();
273
274 if (g_dwTlsErrIndex != TLS_OUT_OF_INDEXES)
275 {
276 heap_free(TlsGetValue(g_dwTlsErrIndex));
277 TlsFree(g_dwTlsErrIndex);
278 }
279 break;
280 }
281 return TRUE;
282 }
283
284 /***********************************************************************
285 * DllInstall (WININET.@)
286 */
287 HRESULT WINAPI DllInstall(BOOL bInstall, LPCWSTR cmdline)
288 {
289 FIXME("(%x %s): stub\n", bInstall, debugstr_w(cmdline));
290 return S_OK;
291 }
292
293 /***********************************************************************
294 * INTERNET_SaveProxySettings
295 *
296 * Stores the proxy settings given by lpwai into the registry
297 *
298 * RETURNS
299 * ERROR_SUCCESS if no error, or error code on fail
300 */
301 static LONG INTERNET_SaveProxySettings( proxyinfo_t *lpwpi )
302 {
303 HKEY key;
304 LONG ret;
305
306 if ((ret = RegOpenKeyW( HKEY_CURRENT_USER, szInternetSettings, &key )))
307 return ret;
308
309 if ((ret = RegSetValueExW( key, szProxyEnable, 0, REG_DWORD, (BYTE*)&lpwpi->proxyEnabled, sizeof(DWORD))))
310 {
311 RegCloseKey( key );
312 return ret;
313 }
314
315 if (lpwpi->proxy)
316 {
317 if ((ret = RegSetValueExW( key, szProxyServer, 0, REG_SZ, (BYTE*)lpwpi->proxy, sizeof(WCHAR) * (lstrlenW(lpwpi->proxy) + 1))))
318 {
319 RegCloseKey( key );
320 return ret;
321 }
322 }
323 else
324 {
325 if ((ret = RegDeleteValueW( key, szProxyServer )) && ret != ERROR_FILE_NOT_FOUND)
326 {
327 RegCloseKey( key );
328 return ret;
329 }
330 }
331
332 RegCloseKey(key);
333 return ERROR_SUCCESS;
334 }
335
336 /***********************************************************************
337 * INTERNET_FindProxyForProtocol
338 *
339 * Searches the proxy string for a proxy of the given protocol.
340 * Returns the found proxy, or the default proxy if none of the given
341 * protocol is found.
342 *
343 * PARAMETERS
344 * szProxy [In] proxy string to search
345 * proto [In] protocol to search for, e.g. "http"
346 * foundProxy [Out] found proxy
347 * foundProxyLen [In/Out] length of foundProxy buffer, in WCHARs
348 *
349 * RETURNS
350 * TRUE if a proxy is found, FALSE if not. If foundProxy is too short,
351 * *foundProxyLen is set to the required size in WCHARs, including the
352 * NULL terminator, and the last error is set to ERROR_INSUFFICIENT_BUFFER.
353 */
354 BOOL INTERNET_FindProxyForProtocol(LPCWSTR szProxy, LPCWSTR proto, WCHAR *foundProxy, DWORD *foundProxyLen)
355 {
356 LPCWSTR ptr;
357 BOOL ret = FALSE;
358
359 TRACE("(%s, %s)\n", debugstr_w(szProxy), debugstr_w(proto));
360
361 /* First, look for the specified protocol (proto=scheme://host:port) */
362 for (ptr = szProxy; !ret && ptr && *ptr; )
363 {
364 LPCWSTR end, equal;
365
366 if (!(end = strchrW(ptr, ' ')))
367 end = ptr + strlenW(ptr);
368 if ((equal = strchrW(ptr, '=')) && equal < end &&
369 equal - ptr == strlenW(proto) &&
370 !strncmpiW(proto, ptr, strlenW(proto)))
371 {
372 if (end - equal > *foundProxyLen)
373 {
374 WARN("buffer too short for %s\n",
375 debugstr_wn(equal + 1, end - equal - 1));
376 *foundProxyLen = end - equal;
377 SetLastError(ERROR_INSUFFICIENT_BUFFER);
378 }
379 else
380 {
381 memcpy(foundProxy, equal + 1, (end - equal) * sizeof(WCHAR));
382 foundProxy[end - equal] = 0;
383 ret = TRUE;
384 }
385 }
386 if (*end == ' ')
387 ptr = end + 1;
388 else
389 ptr = end;
390 }
391 if (!ret)
392 {
393 /* It wasn't found: look for no protocol */
394 for (ptr = szProxy; !ret && ptr && *ptr; )
395 {
396 LPCWSTR end;
397
398 if (!(end = strchrW(ptr, ' ')))
399 end = ptr + strlenW(ptr);
400 if (!strchrW(ptr, '='))
401 {
402 if (end - ptr + 1 > *foundProxyLen)
403 {
404 WARN("buffer too short for %s\n",
405 debugstr_wn(ptr, end - ptr));
406 *foundProxyLen = end - ptr + 1;
407 SetLastError(ERROR_INSUFFICIENT_BUFFER);
408 }
409 else
410 {
411 memcpy(foundProxy, ptr, (end - ptr) * sizeof(WCHAR));
412 foundProxy[end - ptr] = 0;
413 ret = TRUE;
414 }
415 }
416 if (*end == ' ')
417 ptr = end + 1;
418 else
419 ptr = end;
420 }
421 }
422 if (ret)
423 TRACE("found proxy for %s: %s\n", debugstr_w(proto),
424 debugstr_w(foundProxy));
425 return ret;
426 }
427
428 /***********************************************************************
429 * InternetInitializeAutoProxyDll (WININET.@)
430 *
431 * Setup the internal proxy
432 *
433 * PARAMETERS
434 * dwReserved
435 *
436 * RETURNS
437 * FALSE on failure
438 *
439 */
440 BOOL WINAPI InternetInitializeAutoProxyDll(DWORD dwReserved)
441 {
442 FIXME("STUB\n");
443 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
444 return FALSE;
445 }
446
447 /***********************************************************************
448 * DetectAutoProxyUrl (WININET.@)
449 *
450 * Auto detect the proxy url
451 *
452 * RETURNS
453 * FALSE on failure
454 *
455 */
456 BOOL WINAPI DetectAutoProxyUrl(LPSTR lpszAutoProxyUrl,
457 DWORD dwAutoProxyUrlLength, DWORD dwDetectFlags)
458 {
459 FIXME("STUB\n");
460 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
461 return FALSE;
462 }
463
464 static void FreeProxyInfo( proxyinfo_t *lpwpi )
465 {
466 heap_free(lpwpi->proxy);
467 heap_free(lpwpi->proxyBypass);
468 heap_free(lpwpi->proxyUsername);
469 heap_free(lpwpi->proxyPassword);
470 }
471
472 static proxyinfo_t *global_proxy;
473
474 static void free_global_proxy( void )
475 {
476 EnterCriticalSection( &WININET_cs );
477 if (global_proxy)
478 {
479 FreeProxyInfo( global_proxy );
480 heap_free( global_proxy );
481 }
482 LeaveCriticalSection( &WININET_cs );
483 }
484
485 static BOOL parse_proxy_url( proxyinfo_t *info, const WCHAR *url )
486 {
487 static const WCHAR fmt[] = {'%','s',':','%','u',0};
488 WCHAR hostname[INTERNET_MAX_HOST_NAME_LENGTH];
489 WCHAR username[INTERNET_MAX_USER_NAME_LENGTH];
490 WCHAR password[INTERNET_MAX_PASSWORD_LENGTH];
491 URL_COMPONENTSW uc;
492
493 hostname[0] = username[0] = password[0] = 0;
494 memset( &uc, 0, sizeof(uc) );
495 uc.dwStructSize = sizeof(uc);
496 uc.lpszHostName = hostname;
497 uc.dwHostNameLength = INTERNET_MAX_HOST_NAME_LENGTH;
498 uc.lpszUserName = username;
499 uc.dwUserNameLength = INTERNET_MAX_USER_NAME_LENGTH;
500 uc.lpszPassword = password;
501 uc.dwPasswordLength = INTERNET_MAX_PASSWORD_LENGTH;
502
503 if (!InternetCrackUrlW( url, 0, 0, &uc )) return FALSE;
504 if (!hostname[0])
505 {
506 if (!(info->proxy = heap_strdupW( url ))) return FALSE;
507 info->proxyUsername = NULL;
508 info->proxyPassword = NULL;
509 return TRUE;
510 }
511 if (!(info->proxy = heap_alloc( (strlenW(hostname) + 12) * sizeof(WCHAR) ))) return FALSE;
512 sprintfW( info->proxy, fmt, hostname, uc.nPort );
513
514 if (!username[0]) info->proxyUsername = NULL;
515 else if (!(info->proxyUsername = heap_strdupW( username )))
516 {
517 heap_free( info->proxy );
518 return FALSE;
519 }
520 if (!password[0]) info->proxyPassword = NULL;
521 else if (!(info->proxyPassword = heap_strdupW( password )))
522 {
523 heap_free( info->proxyUsername );
524 heap_free( info->proxy );
525 return FALSE;
526 }
527 return TRUE;
528 }
529
530 /***********************************************************************
531 * INTERNET_LoadProxySettings
532 *
533 * Loads proxy information from process-wide global settings, the registry,
534 * or the environment into lpwpi.
535 *
536 * The caller should call FreeProxyInfo when done with lpwpi.
537 *
538 * FIXME:
539 * The proxy may be specified in the form 'http=proxy.my.org'
540 * Presumably that means there can be ftp=ftpproxy.my.org too.
541 */
542 static LONG INTERNET_LoadProxySettings( proxyinfo_t *lpwpi )
543 {
544 HKEY key;
545 DWORD type, len;
546 LPCSTR envproxy;
547 LONG ret;
548
549 memset( lpwpi, 0, sizeof(*lpwpi) );
550
551 EnterCriticalSection( &WININET_cs );
552 if (global_proxy)
553 {
554 lpwpi->proxyEnabled = global_proxy->proxyEnabled;
555 lpwpi->proxy = heap_strdupW( global_proxy->proxy );
556 lpwpi->proxyBypass = heap_strdupW( global_proxy->proxyBypass );
557 }
558 LeaveCriticalSection( &WININET_cs );
559
560 if ((ret = RegOpenKeyW( HKEY_CURRENT_USER, szInternetSettings, &key )))
561 {
562 FreeProxyInfo( lpwpi );
563 return ret;
564 }
565
566 len = sizeof(DWORD);
567 if (RegQueryValueExW( key, szProxyEnable, NULL, &type, (BYTE *)&lpwpi->proxyEnabled, &len ) || type != REG_DWORD)
568 {
569 lpwpi->proxyEnabled = 0;
570 if((ret = RegSetValueExW( key, szProxyEnable, 0, REG_DWORD, (BYTE *)&lpwpi->proxyEnabled, sizeof(DWORD) )))
571 {
572 FreeProxyInfo( lpwpi );
573 RegCloseKey( key );
574 return ret;
575 }
576 }
577
578 if (!(envproxy = getenv( "http_proxy" )) || lpwpi->proxyEnabled)
579 {
580 /* figure out how much memory the proxy setting takes */
581 if (!RegQueryValueExW( key, szProxyServer, NULL, &type, NULL, &len ) && len && (type == REG_SZ))
582 {
583 LPWSTR szProxy, p;
584 static const WCHAR szHttp[] = {'h','t','t','p','=',0};
585
586 if (!(szProxy = heap_alloc(len)))
587 {
588 RegCloseKey( key );
589 FreeProxyInfo( lpwpi );
590 return ERROR_OUTOFMEMORY;
591 }
592 RegQueryValueExW( key, szProxyServer, NULL, &type, (BYTE*)szProxy, &len );
593
594 /* find the http proxy, and strip away everything else */
595 p = strstrW( szProxy, szHttp );
596 if (p)
597 {
598 p += lstrlenW( szHttp );
599 lstrcpyW( szProxy, p );
600 }
601 p = strchrW( szProxy, ';' );
602 if (p) *p = 0;
603
604 FreeProxyInfo( lpwpi );
605 lpwpi->proxy = szProxy;
606 lpwpi->proxyBypass = NULL;
607
608 TRACE("http proxy (from registry) = %s\n", debugstr_w(lpwpi->proxy));
609 }
610 else
611 {
612 TRACE("No proxy server settings in registry.\n");
613 FreeProxyInfo( lpwpi );
614 lpwpi->proxy = NULL;
615 lpwpi->proxyBypass = NULL;
616 }
617 }
618 else if (envproxy)
619 {
620 WCHAR *envproxyW;
621
622 len = MultiByteToWideChar( CP_UNIXCP, 0, envproxy, -1, NULL, 0 );
623 if (!(envproxyW = heap_alloc(len * sizeof(WCHAR))))
624 {
625 RegCloseKey( key );
626 return ERROR_OUTOFMEMORY;
627 }
628 MultiByteToWideChar( CP_UNIXCP, 0, envproxy, -1, envproxyW, len );
629
630 FreeProxyInfo( lpwpi );
631 if (parse_proxy_url( lpwpi, envproxyW ))
632 {
633 TRACE("http proxy (from environment) = %s\n", debugstr_w(lpwpi->proxy));
634 lpwpi->proxyEnabled = 1;
635 lpwpi->proxyBypass = NULL;
636 }
637 else
638 {
639 WARN("failed to parse http_proxy value %s\n", debugstr_w(envproxyW));
640 lpwpi->proxyEnabled = 0;
641 lpwpi->proxy = NULL;
642 lpwpi->proxyBypass = NULL;
643 }
644 heap_free( envproxyW );
645 }
646
647 if (lpwpi->proxyEnabled)
648 {
649 TRACE("Proxy is enabled.\n");
650
651 if (!(envproxy = getenv( "no_proxy" )))
652 {
653 /* figure out how much memory the proxy setting takes */
654 if (!RegQueryValueExW( key, szProxyOverride, NULL, &type, NULL, &len ) && len && (type == REG_SZ))
655 {
656 LPWSTR szProxy;
657
658 if (!(szProxy = heap_alloc(len)))
659 {
660 RegCloseKey( key );
661 return ERROR_OUTOFMEMORY;
662 }
663 RegQueryValueExW( key, szProxyOverride, NULL, &type, (BYTE*)szProxy, &len );
664
665 heap_free( lpwpi->proxyBypass );
666 lpwpi->proxyBypass = szProxy;
667
668 TRACE("http proxy bypass (from registry) = %s\n", debugstr_w(lpwpi->proxyBypass));
669 }
670 else
671 {
672 heap_free( lpwpi->proxyBypass );
673 lpwpi->proxyBypass = NULL;
674
675 TRACE("No proxy bypass server settings in registry.\n");
676 }
677 }
678 else
679 {
680 WCHAR *envproxyW;
681
682 len = MultiByteToWideChar( CP_UNIXCP, 0, envproxy, -1, NULL, 0 );
683 if (!(envproxyW = heap_alloc(len * sizeof(WCHAR))))
684 {
685 RegCloseKey( key );
686 return ERROR_OUTOFMEMORY;
687 }
688 MultiByteToWideChar( CP_UNIXCP, 0, envproxy, -1, envproxyW, len );
689
690 heap_free( lpwpi->proxyBypass );
691 lpwpi->proxyBypass = envproxyW;
692
693 TRACE("http proxy bypass (from environment) = %s\n", debugstr_w(lpwpi->proxyBypass));
694 }
695 }
696 else TRACE("Proxy is disabled.\n");
697
698 RegCloseKey( key );
699 return ERROR_SUCCESS;
700 }
701
702 /***********************************************************************
703 * INTERNET_ConfigureProxy
704 */
705 static BOOL INTERNET_ConfigureProxy( appinfo_t *lpwai )
706 {
707 proxyinfo_t wpi;
708
709 if (INTERNET_LoadProxySettings( &wpi ))
710 return FALSE;
711
712 if (wpi.proxyEnabled)
713 {
714 TRACE("http proxy = %s bypass = %s\n", debugstr_w(wpi.proxy), debugstr_w(wpi.proxyBypass));
715
716 lpwai->accessType = INTERNET_OPEN_TYPE_PROXY;
717 lpwai->proxy = wpi.proxy;
718 lpwai->proxyBypass = wpi.proxyBypass;
719 lpwai->proxyUsername = wpi.proxyUsername;
720 lpwai->proxyPassword = wpi.proxyPassword;
721 return TRUE;
722 }
723
724 lpwai->accessType = INTERNET_OPEN_TYPE_DIRECT;
725 FreeProxyInfo(&wpi);
726 return FALSE;
727 }
728
729 /***********************************************************************
730 * dump_INTERNET_FLAGS
731 *
732 * Helper function to TRACE the internet flags.
733 *
734 * RETURNS
735 * None
736 *
737 */
738 static void dump_INTERNET_FLAGS(DWORD dwFlags)
739 {
740 #define FE(x) { x, #x }
741 static const wininet_flag_info flag[] = {
742 FE(INTERNET_FLAG_RELOAD),
743 FE(INTERNET_FLAG_RAW_DATA),
744 FE(INTERNET_FLAG_EXISTING_CONNECT),
745 FE(INTERNET_FLAG_ASYNC),
746 FE(INTERNET_FLAG_PASSIVE),
747 FE(INTERNET_FLAG_NO_CACHE_WRITE),
748 FE(INTERNET_FLAG_MAKE_PERSISTENT),
749 FE(INTERNET_FLAG_FROM_CACHE),
750 FE(INTERNET_FLAG_SECURE),
751 FE(INTERNET_FLAG_KEEP_CONNECTION),
752 FE(INTERNET_FLAG_NO_AUTO_REDIRECT),
753 FE(INTERNET_FLAG_READ_PREFETCH),
754 FE(INTERNET_FLAG_NO_COOKIES),
755 FE(INTERNET_FLAG_NO_AUTH),
756 FE(INTERNET_FLAG_CACHE_IF_NET_FAIL),
757 FE(INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP),
758 FE(INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTPS),
759 FE(INTERNET_FLAG_IGNORE_CERT_DATE_INVALID),
760 FE(INTERNET_FLAG_IGNORE_CERT_CN_INVALID),
761 FE(INTERNET_FLAG_RESYNCHRONIZE),
762 FE(INTERNET_FLAG_HYPERLINK),
763 FE(INTERNET_FLAG_NO_UI),
764 FE(INTERNET_FLAG_PRAGMA_NOCACHE),
765 FE(INTERNET_FLAG_CACHE_ASYNC),
766 FE(INTERNET_FLAG_FORMS_SUBMIT),
767 FE(INTERNET_FLAG_NEED_FILE),
768 FE(INTERNET_FLAG_TRANSFER_ASCII),
769 FE(INTERNET_FLAG_TRANSFER_BINARY)
770 };
771 #undef FE
772 unsigned int i;
773
774 for (i = 0; i < (sizeof(flag) / sizeof(flag[0])); i++) {
775 if (flag[i].val & dwFlags) {
776 TRACE(" %s", flag[i].name);
777 dwFlags &= ~flag[i].val;
778 }
779 }
780 if (dwFlags)
781 TRACE(" Unknown flags (%08x)\n", dwFlags);
782 else
783 TRACE("\n");
784 }
785
786 /***********************************************************************
787 * INTERNET_CloseHandle (internal)
788 *
789 * Close internet handle
790 *
791 */
792 static VOID APPINFO_Destroy(object_header_t *hdr)
793 {
794 appinfo_t *lpwai = (appinfo_t*)hdr;
795
796 TRACE("%p\n",lpwai);
797
798 heap_free(lpwai->agent);
799 heap_free(lpwai->proxy);
800 heap_free(lpwai->proxyBypass);
801 heap_free(lpwai->proxyUsername);
802 heap_free(lpwai->proxyPassword);
803 }
804
805 static DWORD APPINFO_QueryOption(object_header_t *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
806 {
807 appinfo_t *ai = (appinfo_t*)hdr;
808
809 switch(option) {
810 case INTERNET_OPTION_HANDLE_TYPE:
811 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
812
813 if (*size < sizeof(ULONG))
814 return ERROR_INSUFFICIENT_BUFFER;
815
816 *size = sizeof(DWORD);
817 *(DWORD*)buffer = INTERNET_HANDLE_TYPE_INTERNET;
818 return ERROR_SUCCESS;
819
820 case INTERNET_OPTION_USER_AGENT: {
821 DWORD bufsize;
822
823 TRACE("INTERNET_OPTION_USER_AGENT\n");
824
825 bufsize = *size;
826
827 if (unicode) {
828 DWORD len = ai->agent ? strlenW(ai->agent) : 0;
829
830 *size = (len + 1) * sizeof(WCHAR);
831 if(!buffer || bufsize < *size)
832 return ERROR_INSUFFICIENT_BUFFER;
833
834 if (ai->agent)
835 strcpyW(buffer, ai->agent);
836 else
837 *(WCHAR *)buffer = 0;
838 /* If the buffer is copied, the returned length doesn't include
839 * the NULL terminator.
840 */
841 *size = len;
842 }else {
843 if (ai->agent)
844 *size = WideCharToMultiByte(CP_ACP, 0, ai->agent, -1, NULL, 0, NULL, NULL);
845 else
846 *size = 1;
847 if(!buffer || bufsize < *size)
848 return ERROR_INSUFFICIENT_BUFFER;
849
850 if (ai->agent)
851 WideCharToMultiByte(CP_ACP, 0, ai->agent, -1, buffer, *size, NULL, NULL);
852 else
853 *(char *)buffer = 0;
854 /* If the buffer is copied, the returned length doesn't include
855 * the NULL terminator.
856 */
857 *size -= 1;
858 }
859
860 return ERROR_SUCCESS;
861 }
862
863 case INTERNET_OPTION_PROXY:
864 if(!size) return ERROR_INVALID_PARAMETER;
865 if (unicode) {
866 INTERNET_PROXY_INFOW *pi = (INTERNET_PROXY_INFOW *)buffer;
867 DWORD proxyBytesRequired = 0, proxyBypassBytesRequired = 0;
868 LPWSTR proxy, proxy_bypass;
869
870 if (ai->proxy)
871 proxyBytesRequired = (lstrlenW(ai->proxy) + 1) * sizeof(WCHAR);
872 if (ai->proxyBypass)
873 proxyBypassBytesRequired = (lstrlenW(ai->proxyBypass) + 1) * sizeof(WCHAR);
874 if (!pi || *size < sizeof(INTERNET_PROXY_INFOW) + proxyBytesRequired + proxyBypassBytesRequired)
875 {
876 *size = sizeof(INTERNET_PROXY_INFOW) + proxyBytesRequired + proxyBypassBytesRequired;
877 return ERROR_INSUFFICIENT_BUFFER;
878 }
879 proxy = (LPWSTR)((LPBYTE)buffer + sizeof(INTERNET_PROXY_INFOW));
880 proxy_bypass = (LPWSTR)((LPBYTE)buffer + sizeof(INTERNET_PROXY_INFOW) + proxyBytesRequired);
881
882 pi->dwAccessType = ai->accessType;
883 pi->lpszProxy = NULL;
884 pi->lpszProxyBypass = NULL;
885 if (ai->proxy) {
886 lstrcpyW(proxy, ai->proxy);
887 pi->lpszProxy = proxy;
888 }
889
890 if (ai->proxyBypass) {
891 lstrcpyW(proxy_bypass, ai->proxyBypass);
892 pi->lpszProxyBypass = proxy_bypass;
893 }
894
895 *size = sizeof(INTERNET_PROXY_INFOW) + proxyBytesRequired + proxyBypassBytesRequired;
896 return ERROR_SUCCESS;
897 }else {
898 INTERNET_PROXY_INFOA *pi = (INTERNET_PROXY_INFOA *)buffer;
899 DWORD proxyBytesRequired = 0, proxyBypassBytesRequired = 0;
900 LPSTR proxy, proxy_bypass;
901
902 if (ai->proxy)
903 proxyBytesRequired = WideCharToMultiByte(CP_ACP, 0, ai->proxy, -1, NULL, 0, NULL, NULL);
904 if (ai->proxyBypass)
905 proxyBypassBytesRequired = WideCharToMultiByte(CP_ACP, 0, ai->proxyBypass, -1,
906 NULL, 0, NULL, NULL);
907 if (!pi || *size < sizeof(INTERNET_PROXY_INFOA) + proxyBytesRequired + proxyBypassBytesRequired)
908 {
909 *size = sizeof(INTERNET_PROXY_INFOA) + proxyBytesRequired + proxyBypassBytesRequired;
910 return ERROR_INSUFFICIENT_BUFFER;
911 }
912 proxy = (LPSTR)((LPBYTE)buffer + sizeof(INTERNET_PROXY_INFOA));
913 proxy_bypass = (LPSTR)((LPBYTE)buffer + sizeof(INTERNET_PROXY_INFOA) + proxyBytesRequired);
914
915 pi->dwAccessType = ai->accessType;
916 pi->lpszProxy = NULL;
917 pi->lpszProxyBypass = NULL;
918 if (ai->proxy) {
919 WideCharToMultiByte(CP_ACP, 0, ai->proxy, -1, proxy, proxyBytesRequired, NULL, NULL);
920 pi->lpszProxy = proxy;
921 }
922
923 if (ai->proxyBypass) {
924 WideCharToMultiByte(CP_ACP, 0, ai->proxyBypass, -1, proxy_bypass,
925 proxyBypassBytesRequired, NULL, NULL);
926 pi->lpszProxyBypass = proxy_bypass;
927 }
928
929 *size = sizeof(INTERNET_PROXY_INFOA) + proxyBytesRequired + proxyBypassBytesRequired;
930 return ERROR_SUCCESS;
931 }
932
933 case INTERNET_OPTION_CONNECT_TIMEOUT:
934 TRACE("INTERNET_OPTION_CONNECT_TIMEOUT\n");
935
936 if (*size < sizeof(ULONG))
937 return ERROR_INSUFFICIENT_BUFFER;
938
939 *(ULONG*)buffer = ai->connect_timeout;
940 *size = sizeof(ULONG);
941
942 return ERROR_SUCCESS;
943 }
944
945 return INET_QueryOption(hdr, option, buffer, size, unicode);
946 }
947
948 static DWORD APPINFO_SetOption(object_header_t *hdr, DWORD option, void *buf, DWORD size)
949 {
950 appinfo_t *ai = (appinfo_t*)hdr;
951
952 switch(option) {
953 case INTERNET_OPTION_CONNECT_TIMEOUT:
954 TRACE("INTERNET_OPTION_CONNECT_TIMEOUT\n");
955
956 if(size != sizeof(connect_timeout))
957 return ERROR_INTERNET_BAD_OPTION_LENGTH;
958 if(!*(ULONG*)buf)
959 return ERROR_BAD_ARGUMENTS;
960
961 ai->connect_timeout = *(ULONG*)buf;
962 return ERROR_SUCCESS;
963 case INTERNET_OPTION_USER_AGENT:
964 heap_free(ai->agent);
965 if (!(ai->agent = heap_strdupW(buf))) return ERROR_OUTOFMEMORY;
966 return ERROR_SUCCESS;
967 }
968
969 return INET_SetOption(hdr, option, buf, size);
970 }
971
972 static const object_vtbl_t APPINFOVtbl = {
973 APPINFO_Destroy,
974 NULL,
975 APPINFO_QueryOption,
976 APPINFO_SetOption,
977 NULL,
978 NULL,
979 NULL,
980 NULL
981 };
982
983
984 /***********************************************************************
985 * InternetOpenW (WININET.@)
986 *
987 * Per-application initialization of wininet
988 *
989 * RETURNS
990 * HINTERNET on success
991 * NULL on failure
992 *
993 */
994 HINTERNET WINAPI InternetOpenW(LPCWSTR lpszAgent, DWORD dwAccessType,
995 LPCWSTR lpszProxy, LPCWSTR lpszProxyBypass, DWORD dwFlags)
996 {
997 appinfo_t *lpwai = NULL;
998
999 #ifdef __REACTOS__
1000 init_winsock();
1001 #endif
1002 if (TRACE_ON(wininet)) {
1003 #define FE(x) { x, #x }
1004 static const wininet_flag_info access_type[] = {
1005 FE(INTERNET_OPEN_TYPE_PRECONFIG),
1006 FE(INTERNET_OPEN_TYPE_DIRECT),
1007 FE(INTERNET_OPEN_TYPE_PROXY),
1008 FE(INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY)
1009 };
1010 #undef FE
1011 DWORD i;
1012 const char *access_type_str = "Unknown";
1013
1014 TRACE("(%s, %i, %s, %s, %i)\n", debugstr_w(lpszAgent), dwAccessType,
1015 debugstr_w(lpszProxy), debugstr_w(lpszProxyBypass), dwFlags);
1016 for (i = 0; i < (sizeof(access_type) / sizeof(access_type[0])); i++) {
1017 if (access_type[i].val == dwAccessType) {
1018 access_type_str = access_type[i].name;
1019 break;
1020 }
1021 }
1022 TRACE(" access type : %s\n", access_type_str);
1023 TRACE(" flags :");
1024 dump_INTERNET_FLAGS(dwFlags);
1025 }
1026
1027 /* Clear any error information */
1028 INTERNET_SetLastError(0);
1029
1030 if((dwAccessType == INTERNET_OPEN_TYPE_PROXY) && !lpszProxy) {
1031 SetLastError(ERROR_INVALID_PARAMETER);
1032 return NULL;
1033 }
1034
1035 lpwai = alloc_object(NULL, &APPINFOVtbl, sizeof(appinfo_t));
1036 if (!lpwai) {
1037 SetLastError(ERROR_OUTOFMEMORY);
1038 return NULL;
1039 }
1040
1041 lpwai->hdr.htype = WH_HINIT;
1042 lpwai->hdr.dwFlags = dwFlags;
1043 lpwai->accessType = dwAccessType;
1044 lpwai->proxyUsername = NULL;
1045 lpwai->proxyPassword = NULL;
1046 lpwai->connect_timeout = connect_timeout;
1047
1048 lpwai->agent = heap_strdupW(lpszAgent);
1049 if(dwAccessType == INTERNET_OPEN_TYPE_PRECONFIG)
1050 INTERNET_ConfigureProxy( lpwai );
1051 else if(dwAccessType == INTERNET_OPEN_TYPE_PROXY) {
1052 lpwai->proxy = heap_strdupW(lpszProxy);
1053 lpwai->proxyBypass = heap_strdupW(lpszProxyBypass);
1054 }
1055
1056 TRACE("returning %p\n", lpwai);
1057
1058 return lpwai->hdr.hInternet;
1059 }
1060
1061
1062 /***********************************************************************
1063 * InternetOpenA (WININET.@)
1064 *
1065 * Per-application initialization of wininet
1066 *
1067 * RETURNS
1068 * HINTERNET on success
1069 * NULL on failure
1070 *
1071 */
1072 HINTERNET WINAPI InternetOpenA(LPCSTR lpszAgent, DWORD dwAccessType,
1073 LPCSTR lpszProxy, LPCSTR lpszProxyBypass, DWORD dwFlags)
1074 {
1075 WCHAR *szAgent, *szProxy, *szBypass;
1076 HINTERNET rc;
1077
1078 TRACE("(%s, 0x%08x, %s, %s, 0x%08x)\n", debugstr_a(lpszAgent),
1079 dwAccessType, debugstr_a(lpszProxy), debugstr_a(lpszProxyBypass), dwFlags);
1080
1081 szAgent = heap_strdupAtoW(lpszAgent);
1082 szProxy = heap_strdupAtoW(lpszProxy);
1083 szBypass = heap_strdupAtoW(lpszProxyBypass);
1084
1085 rc = InternetOpenW(szAgent, dwAccessType, szProxy, szBypass, dwFlags);
1086
1087 heap_free(szAgent);
1088 heap_free(szProxy);
1089 heap_free(szBypass);
1090 return rc;
1091 }
1092
1093 /***********************************************************************
1094 * InternetGetLastResponseInfoA (WININET.@)
1095 *
1096 * Return last wininet error description on the calling thread
1097 *
1098 * RETURNS
1099 * TRUE on success of writing to buffer
1100 * FALSE on failure
1101 *
1102 */
1103 BOOL WINAPI InternetGetLastResponseInfoA(LPDWORD lpdwError,
1104 LPSTR lpszBuffer, LPDWORD lpdwBufferLength)
1105 {
1106 LPWITHREADERROR lpwite = TlsGetValue(g_dwTlsErrIndex);
1107
1108 TRACE("\n");
1109
1110 if (lpwite)
1111 {
1112 *lpdwError = lpwite->dwError;
1113 if (lpwite->dwError)
1114 {
1115 memcpy(lpszBuffer, lpwite->response, *lpdwBufferLength);
1116 *lpdwBufferLength = strlen(lpszBuffer);
1117 }
1118 else
1119 *lpdwBufferLength = 0;
1120 }
1121 else
1122 {
1123 *lpdwError = 0;
1124 *lpdwBufferLength = 0;
1125 }
1126
1127 return TRUE;
1128 }
1129
1130 /***********************************************************************
1131 * InternetGetLastResponseInfoW (WININET.@)
1132 *
1133 * Return last wininet error description on the calling thread
1134 *
1135 * RETURNS
1136 * TRUE on success of writing to buffer
1137 * FALSE on failure
1138 *
1139 */
1140 BOOL WINAPI InternetGetLastResponseInfoW(LPDWORD lpdwError,
1141 LPWSTR lpszBuffer, LPDWORD lpdwBufferLength)
1142 {
1143 LPWITHREADERROR lpwite = TlsGetValue(g_dwTlsErrIndex);
1144
1145 TRACE("\n");
1146
1147 if (lpwite)
1148 {
1149 *lpdwError = lpwite->dwError;
1150 if (lpwite->dwError)
1151 {
1152 memcpy(lpszBuffer, lpwite->response, *lpdwBufferLength);
1153 *lpdwBufferLength = lstrlenW(lpszBuffer);
1154 }
1155 else
1156 *lpdwBufferLength = 0;
1157 }
1158 else
1159 {
1160 *lpdwError = 0;
1161 *lpdwBufferLength = 0;
1162 }
1163
1164 return TRUE;
1165 }
1166
1167 /***********************************************************************
1168 * InternetGetConnectedState (WININET.@)
1169 *
1170 * Return connected state
1171 *
1172 * RETURNS
1173 * TRUE if connected
1174 * if lpdwStatus is not null, return the status (off line,
1175 * modem, lan...) in it.
1176 * FALSE if not connected
1177 */
1178 BOOL WINAPI InternetGetConnectedState(LPDWORD lpdwStatus, DWORD dwReserved)
1179 {
1180 TRACE("(%p, 0x%08x)\n", lpdwStatus, dwReserved);
1181
1182 if (lpdwStatus) {
1183 WARN("always returning LAN connection.\n");
1184 *lpdwStatus = INTERNET_CONNECTION_LAN;
1185 }
1186 return TRUE;
1187 }
1188
1189
1190 /***********************************************************************
1191 * InternetGetConnectedStateExW (WININET.@)
1192 *
1193 * Return connected state
1194 *
1195 * PARAMS
1196 *
1197 * lpdwStatus [O] Flags specifying the status of the internet connection.
1198 * lpszConnectionName [O] Pointer to buffer to receive the friendly name of the internet connection.
1199 * dwNameLen [I] Size of the buffer, in characters.
1200 * dwReserved [I] Reserved. Must be set to 0.
1201 *
1202 * RETURNS
1203 * TRUE if connected
1204 * if lpdwStatus is not null, return the status (off line,
1205 * modem, lan...) in it.
1206 * FALSE if not connected
1207 *
1208 * NOTES
1209 * If the system has no available network connections, an empty string is
1210 * stored in lpszConnectionName. If there is a LAN connection, a localized
1211 * "LAN Connection" string is stored. Presumably, if only a dial-up
1212 * connection is available then the name of the dial-up connection is
1213 * returned. Why any application, other than the "Internet Settings" CPL,
1214 * would want to use this function instead of the simpler InternetGetConnectedStateW
1215 * function is beyond me.
1216 */
1217 BOOL WINAPI InternetGetConnectedStateExW(LPDWORD lpdwStatus, LPWSTR lpszConnectionName,
1218 DWORD dwNameLen, DWORD dwReserved)
1219 {
1220 TRACE("(%p, %p, %d, 0x%08x)\n", lpdwStatus, lpszConnectionName, dwNameLen, dwReserved);
1221
1222 /* Must be zero */
1223 if(dwReserved)
1224 return FALSE;
1225
1226 if (lpdwStatus) {
1227 WARN("always returning LAN connection.\n");
1228 *lpdwStatus = INTERNET_CONNECTION_LAN;
1229 }
1230
1231 /* When the buffer size is zero LoadStringW fills the buffer with a pointer to
1232 * the resource, avoid it as we must not change the buffer in this case */
1233 if(lpszConnectionName && dwNameLen) {
1234 *lpszConnectionName = '\0';
1235 LoadStringW(WININET_hModule, IDS_LANCONNECTION, lpszConnectionName, dwNameLen);
1236 }
1237
1238 return TRUE;
1239 }
1240
1241
1242 /***********************************************************************
1243 * InternetGetConnectedStateExA (WININET.@)
1244 */
1245 BOOL WINAPI InternetGetConnectedStateExA(LPDWORD lpdwStatus, LPSTR lpszConnectionName,
1246 DWORD dwNameLen, DWORD dwReserved)
1247 {
1248 LPWSTR lpwszConnectionName = NULL;
1249 BOOL rc;
1250
1251 TRACE("(%p, %p, %d, 0x%08x)\n", lpdwStatus, lpszConnectionName, dwNameLen, dwReserved);
1252
1253 if (lpszConnectionName && dwNameLen > 0)
1254 lpwszConnectionName = heap_alloc(dwNameLen * sizeof(WCHAR));
1255
1256 rc = InternetGetConnectedStateExW(lpdwStatus,lpwszConnectionName, dwNameLen,
1257 dwReserved);
1258 if (rc && lpwszConnectionName)
1259 WideCharToMultiByte(CP_ACP,0,lpwszConnectionName,-1,lpszConnectionName,
1260 dwNameLen, NULL, NULL);
1261
1262 heap_free(lpwszConnectionName);
1263 return rc;
1264 }
1265
1266
1267 /***********************************************************************
1268 * InternetConnectW (WININET.@)
1269 *
1270 * Open a ftp, gopher or http session
1271 *
1272 * RETURNS
1273 * HINTERNET a session handle on success
1274 * NULL on failure
1275 *
1276 */
1277 HINTERNET WINAPI InternetConnectW(HINTERNET hInternet,
1278 LPCWSTR lpszServerName, INTERNET_PORT nServerPort,
1279 LPCWSTR lpszUserName, LPCWSTR lpszPassword,
1280 DWORD dwService, DWORD dwFlags, DWORD_PTR dwContext)
1281 {
1282 appinfo_t *hIC;
1283 HINTERNET rc = NULL;
1284 DWORD res = ERROR_SUCCESS;
1285
1286 TRACE("(%p, %s, %u, %s, %p, %u, %x, %lx)\n", hInternet, debugstr_w(lpszServerName),
1287 nServerPort, debugstr_w(lpszUserName), lpszPassword, dwService, dwFlags, dwContext);
1288
1289 if (!lpszServerName)
1290 {
1291 SetLastError(ERROR_INVALID_PARAMETER);
1292 return NULL;
1293 }
1294
1295 hIC = (appinfo_t*)get_handle_object( hInternet );
1296 if ( (hIC == NULL) || (hIC->hdr.htype != WH_HINIT) )
1297 {
1298 res = ERROR_INVALID_HANDLE;
1299 goto lend;
1300 }
1301
1302 switch (dwService)
1303 {
1304 case INTERNET_SERVICE_FTP:
1305 rc = FTP_Connect(hIC, lpszServerName, nServerPort,
1306 lpszUserName, lpszPassword, dwFlags, dwContext, 0);
1307 if(!rc)
1308 res = INTERNET_GetLastError();
1309 break;
1310
1311 case INTERNET_SERVICE_HTTP:
1312 res = HTTP_Connect(hIC, lpszServerName, nServerPort,
1313 lpszUserName, lpszPassword, dwFlags, dwContext, 0, &rc);
1314 break;
1315
1316 case INTERNET_SERVICE_GOPHER:
1317 default:
1318 break;
1319 }
1320 lend:
1321 if( hIC )
1322 WININET_Release( &hIC->hdr );
1323
1324 TRACE("returning %p\n", rc);
1325 SetLastError(res);
1326 return rc;
1327 }
1328
1329
1330 /***********************************************************************
1331 * InternetConnectA (WININET.@)
1332 *
1333 * Open a ftp, gopher or http session
1334 *
1335 * RETURNS
1336 * HINTERNET a session handle on success
1337 * NULL on failure
1338 *
1339 */
1340 HINTERNET WINAPI InternetConnectA(HINTERNET hInternet,
1341 LPCSTR lpszServerName, INTERNET_PORT nServerPort,
1342 LPCSTR lpszUserName, LPCSTR lpszPassword,
1343 DWORD dwService, DWORD dwFlags, DWORD_PTR dwContext)
1344 {
1345 HINTERNET rc = NULL;
1346 LPWSTR szServerName;
1347 LPWSTR szUserName;
1348 LPWSTR szPassword;
1349
1350 szServerName = heap_strdupAtoW(lpszServerName);
1351 szUserName = heap_strdupAtoW(lpszUserName);
1352 szPassword = heap_strdupAtoW(lpszPassword);
1353
1354 rc = InternetConnectW(hInternet, szServerName, nServerPort,
1355 szUserName, szPassword, dwService, dwFlags, dwContext);
1356
1357 heap_free(szServerName);
1358 heap_free(szUserName);
1359 heap_free(szPassword);
1360 return rc;
1361 }
1362
1363
1364 /***********************************************************************
1365 * InternetFindNextFileA (WININET.@)
1366 *
1367 * Continues a file search from a previous call to FindFirstFile
1368 *
1369 * RETURNS
1370 * TRUE on success
1371 * FALSE on failure
1372 *
1373 */
1374 BOOL WINAPI InternetFindNextFileA(HINTERNET hFind, LPVOID lpvFindData)
1375 {
1376 BOOL ret;
1377 WIN32_FIND_DATAW fd;
1378
1379 ret = InternetFindNextFileW(hFind, lpvFindData?&fd:NULL);
1380 if(lpvFindData)
1381 WININET_find_data_WtoA(&fd, (LPWIN32_FIND_DATAA)lpvFindData);
1382 return ret;
1383 }
1384
1385 /***********************************************************************
1386 * InternetFindNextFileW (WININET.@)
1387 *
1388 * Continues a file search from a previous call to FindFirstFile
1389 *
1390 * RETURNS
1391 * TRUE on success
1392 * FALSE on failure
1393 *
1394 */
1395 BOOL WINAPI InternetFindNextFileW(HINTERNET hFind, LPVOID lpvFindData)
1396 {
1397 object_header_t *hdr;
1398 DWORD res;
1399
1400 TRACE("\n");
1401
1402 hdr = get_handle_object(hFind);
1403 if(!hdr) {
1404 WARN("Invalid handle\n");
1405 SetLastError(ERROR_INVALID_HANDLE);
1406 return FALSE;
1407 }
1408
1409 if(hdr->vtbl->FindNextFileW) {
1410 res = hdr->vtbl->FindNextFileW(hdr, lpvFindData);
1411 }else {
1412 WARN("Handle doesn't support NextFile\n");
1413 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
1414 }
1415
1416 WININET_Release(hdr);
1417
1418 if(res != ERROR_SUCCESS)
1419 SetLastError(res);
1420 return res == ERROR_SUCCESS;
1421 }
1422
1423 /***********************************************************************
1424 * InternetCloseHandle (WININET.@)
1425 *
1426 * Generic close handle function
1427 *
1428 * RETURNS
1429 * TRUE on success
1430 * FALSE on failure
1431 *
1432 */
1433 BOOL WINAPI InternetCloseHandle(HINTERNET hInternet)
1434 {
1435 object_header_t *obj;
1436
1437 TRACE("%p\n", hInternet);
1438
1439 obj = get_handle_object( hInternet );
1440 if (!obj) {
1441 SetLastError(ERROR_INVALID_HANDLE);
1442 return FALSE;
1443 }
1444
1445 invalidate_handle(obj);
1446 WININET_Release(obj);
1447
1448 return TRUE;
1449 }
1450
1451
1452 /***********************************************************************
1453 * ConvertUrlComponentValue (Internal)
1454 *
1455 * Helper function for InternetCrackUrlA
1456 *
1457 */
1458 static void ConvertUrlComponentValue(LPSTR* lppszComponent, LPDWORD dwComponentLen,
1459 LPWSTR lpwszComponent, DWORD dwwComponentLen,
1460 LPCSTR lpszStart, LPCWSTR lpwszStart)
1461 {
1462 TRACE("%p %d %p %d %p %p\n", *lppszComponent, *dwComponentLen, lpwszComponent, dwwComponentLen, lpszStart, lpwszStart);
1463 if (*dwComponentLen != 0)
1464 {
1465 DWORD nASCIILength=WideCharToMultiByte(CP_ACP,0,lpwszComponent,dwwComponentLen,NULL,0,NULL,NULL);
1466 if (*lppszComponent == NULL)
1467 {
1468 if (lpwszComponent)
1469 {
1470 int offset = WideCharToMultiByte(CP_ACP, 0, lpwszStart, lpwszComponent-lpwszStart, NULL, 0, NULL, NULL);
1471 *lppszComponent = (LPSTR)lpszStart + offset;
1472 }
1473 else
1474 *lppszComponent = NULL;
1475
1476 *dwComponentLen = nASCIILength;
1477 }
1478 else
1479 {
1480 DWORD ncpylen = min((*dwComponentLen)-1, nASCIILength);
1481 WideCharToMultiByte(CP_ACP,0,lpwszComponent,dwwComponentLen,*lppszComponent,ncpylen+1,NULL,NULL);
1482 (*lppszComponent)[ncpylen]=0;
1483 *dwComponentLen = ncpylen;
1484 }
1485 }
1486 }
1487
1488
1489 /***********************************************************************
1490 * InternetCrackUrlA (WININET.@)
1491 *
1492 * See InternetCrackUrlW.
1493 */
1494 BOOL WINAPI InternetCrackUrlA(LPCSTR lpszUrl, DWORD dwUrlLength, DWORD dwFlags,
1495 LPURL_COMPONENTSA lpUrlComponents)
1496 {
1497 DWORD nLength;
1498 URL_COMPONENTSW UCW;
1499 BOOL ret = FALSE;
1500 WCHAR *lpwszUrl, *hostname = NULL, *username = NULL, *password = NULL, *path = NULL,
1501 *scheme = NULL, *extra = NULL;
1502
1503 TRACE("(%s %u %x %p)\n",
1504 lpszUrl ? debugstr_an(lpszUrl, dwUrlLength ? dwUrlLength : strlen(lpszUrl)) : "(null)",
1505 dwUrlLength, dwFlags, lpUrlComponents);
1506
1507 if (!lpszUrl || !*lpszUrl || !lpUrlComponents ||
1508 lpUrlComponents->dwStructSize != sizeof(URL_COMPONENTSA))
1509 {
1510 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1511 return FALSE;
1512 }
1513
1514 if(dwUrlLength<=0)
1515 dwUrlLength=-1;
1516 nLength=MultiByteToWideChar(CP_ACP,0,lpszUrl,dwUrlLength,NULL,0);
1517
1518 /* if dwUrlLength=-1 then nLength includes null but length to
1519 InternetCrackUrlW should not include it */
1520 if (dwUrlLength == -1) nLength--;
1521
1522 lpwszUrl = heap_alloc((nLength + 1) * sizeof(WCHAR));
1523 MultiByteToWideChar(CP_ACP,0,lpszUrl,dwUrlLength,lpwszUrl,nLength + 1);
1524 lpwszUrl[nLength] = '\0';
1525
1526 memset(&UCW,0,sizeof(UCW));
1527 UCW.dwStructSize = sizeof(URL_COMPONENTSW);
1528 if (lpUrlComponents->dwHostNameLength)
1529 {
1530 UCW.dwHostNameLength = lpUrlComponents->dwHostNameLength;
1531 if (lpUrlComponents->lpszHostName)
1532 {
1533 hostname = heap_alloc(UCW.dwHostNameLength * sizeof(WCHAR));
1534 UCW.lpszHostName = hostname;
1535 }
1536 }
1537 if (lpUrlComponents->dwUserNameLength)
1538 {
1539 UCW.dwUserNameLength = lpUrlComponents->dwUserNameLength;
1540 if (lpUrlComponents->lpszUserName)
1541 {
1542 username = heap_alloc(UCW.dwUserNameLength * sizeof(WCHAR));
1543 UCW.lpszUserName = username;
1544 }
1545 }
1546 if (lpUrlComponents->dwPasswordLength)
1547 {
1548 UCW.dwPasswordLength = lpUrlComponents->dwPasswordLength;
1549 if (lpUrlComponents->lpszPassword)
1550 {
1551 password = heap_alloc(UCW.dwPasswordLength * sizeof(WCHAR));
1552 UCW.lpszPassword = password;
1553 }
1554 }
1555 if (lpUrlComponents->dwUrlPathLength)
1556 {
1557 UCW.dwUrlPathLength = lpUrlComponents->dwUrlPathLength;
1558 if (lpUrlComponents->lpszUrlPath)
1559 {
1560 path = heap_alloc(UCW.dwUrlPathLength * sizeof(WCHAR));
1561 UCW.lpszUrlPath = path;
1562 }
1563 }
1564 if (lpUrlComponents->dwSchemeLength)
1565 {
1566 UCW.dwSchemeLength = lpUrlComponents->dwSchemeLength;
1567 if (lpUrlComponents->lpszScheme)
1568 {
1569 scheme = heap_alloc(UCW.dwSchemeLength * sizeof(WCHAR));
1570 UCW.lpszScheme = scheme;
1571 }
1572 }
1573 if (lpUrlComponents->dwExtraInfoLength)
1574 {
1575 UCW.dwExtraInfoLength = lpUrlComponents->dwExtraInfoLength;
1576 if (lpUrlComponents->lpszExtraInfo)
1577 {
1578 extra = heap_alloc(UCW.dwExtraInfoLength * sizeof(WCHAR));
1579 UCW.lpszExtraInfo = extra;
1580 }
1581 }
1582 if ((ret = InternetCrackUrlW(lpwszUrl, nLength, dwFlags, &UCW)))
1583 {
1584 ConvertUrlComponentValue(&lpUrlComponents->lpszHostName, &lpUrlComponents->dwHostNameLength,
1585 UCW.lpszHostName, UCW.dwHostNameLength, lpszUrl, lpwszUrl);
1586 ConvertUrlComponentValue(&lpUrlComponents->lpszUserName, &lpUrlComponents->dwUserNameLength,
1587 UCW.lpszUserName, UCW.dwUserNameLength, lpszUrl, lpwszUrl);
1588 ConvertUrlComponentValue(&lpUrlComponents->lpszPassword, &lpUrlComponents->dwPasswordLength,
1589 UCW.lpszPassword, UCW.dwPasswordLength, lpszUrl, lpwszUrl);
1590 ConvertUrlComponentValue(&lpUrlComponents->lpszUrlPath, &lpUrlComponents->dwUrlPathLength,
1591 UCW.lpszUrlPath, UCW.dwUrlPathLength, lpszUrl, lpwszUrl);
1592 ConvertUrlComponentValue(&lpUrlComponents->lpszScheme, &lpUrlComponents->dwSchemeLength,
1593 UCW.lpszScheme, UCW.dwSchemeLength, lpszUrl, lpwszUrl);
1594 ConvertUrlComponentValue(&lpUrlComponents->lpszExtraInfo, &lpUrlComponents->dwExtraInfoLength,
1595 UCW.lpszExtraInfo, UCW.dwExtraInfoLength, lpszUrl, lpwszUrl);
1596
1597 lpUrlComponents->nScheme = UCW.nScheme;
1598 lpUrlComponents->nPort = UCW.nPort;
1599
1600 TRACE("%s: scheme(%s) host(%s) path(%s) extra(%s)\n", debugstr_a(lpszUrl),
1601 debugstr_an(lpUrlComponents->lpszScheme, lpUrlComponents->dwSchemeLength),
1602 debugstr_an(lpUrlComponents->lpszHostName, lpUrlComponents->dwHostNameLength),
1603 debugstr_an(lpUrlComponents->lpszUrlPath, lpUrlComponents->dwUrlPathLength),
1604 debugstr_an(lpUrlComponents->lpszExtraInfo, lpUrlComponents->dwExtraInfoLength));
1605 }
1606 heap_free(lpwszUrl);
1607 heap_free(hostname);
1608 heap_free(username);
1609 heap_free(password);
1610 heap_free(path);
1611 heap_free(scheme);
1612 heap_free(extra);
1613 return ret;
1614 }
1615
1616 static const WCHAR url_schemes[][7] =
1617 {
1618 {'f','t','p',0},
1619 {'g','o','p','h','e','r',0},
1620 {'h','t','t','p',0},
1621 {'h','t','t','p','s',0},
1622 {'f','i','l','e',0},
1623 {'n','e','w','s',0},
1624 {'m','a','i','l','t','o',0},
1625 {'r','e','s',0},
1626 };
1627
1628 /***********************************************************************
1629 * GetInternetSchemeW (internal)
1630 *
1631 * Get scheme of url
1632 *
1633 * RETURNS
1634 * scheme on success
1635 * INTERNET_SCHEME_UNKNOWN on failure
1636 *
1637 */
1638 static INTERNET_SCHEME GetInternetSchemeW(LPCWSTR lpszScheme, DWORD nMaxCmp)
1639 {
1640 int i;
1641
1642 TRACE("%s %d\n",debugstr_wn(lpszScheme, nMaxCmp), nMaxCmp);
1643
1644 if(lpszScheme==NULL)
1645 return INTERNET_SCHEME_UNKNOWN;
1646
1647 for (i = 0; i < sizeof(url_schemes)/sizeof(url_schemes[0]); i++)
1648 if (!strncmpiW(lpszScheme, url_schemes[i], nMaxCmp))
1649 return INTERNET_SCHEME_FIRST + i;
1650
1651 return INTERNET_SCHEME_UNKNOWN;
1652 }
1653
1654 /***********************************************************************
1655 * SetUrlComponentValueW (Internal)
1656 *
1657 * Helper function for InternetCrackUrlW
1658 *
1659 * PARAMS
1660 * lppszComponent [O] Holds the returned string
1661 * dwComponentLen [I] Holds the size of lppszComponent
1662 * [O] Holds the length of the string in lppszComponent without '\0'
1663 * lpszStart [I] Holds the string to copy from
1664 * len [I] Holds the length of lpszStart without '\0'
1665 *
1666 * RETURNS
1667 * TRUE on success
1668 * FALSE on failure
1669 *
1670 */
1671 static BOOL SetUrlComponentValueW(LPWSTR* lppszComponent, LPDWORD dwComponentLen, LPCWSTR lpszStart, DWORD len)
1672 {
1673 TRACE("%s (%d)\n", debugstr_wn(lpszStart,len), len);
1674
1675 if ( (*dwComponentLen == 0) && (*lppszComponent == NULL) )
1676 return FALSE;
1677
1678 if (*dwComponentLen != 0 || *lppszComponent == NULL)
1679 {
1680 if (*lppszComponent == NULL)
1681 {
1682 *lppszComponent = (LPWSTR)lpszStart;
1683 *dwComponentLen = len;
1684 }
1685 else
1686 {
1687 DWORD ncpylen = min((*dwComponentLen)-1, len);
1688 memcpy(*lppszComponent, lpszStart, ncpylen*sizeof(WCHAR));
1689 (*lppszComponent)[ncpylen] = '\0';
1690 *dwComponentLen = ncpylen;
1691 }
1692 }
1693
1694 return TRUE;
1695 }
1696
1697 /***********************************************************************
1698 * InternetCrackUrlW (WININET.@)
1699 *
1700 * Break up URL into its components
1701 *
1702 * RETURNS
1703 * TRUE on success
1704 * FALSE on failure
1705 */
1706 BOOL WINAPI InternetCrackUrlW(LPCWSTR lpszUrl_orig, DWORD dwUrlLength_orig, DWORD dwFlags,
1707 LPURL_COMPONENTSW lpUC)
1708 {
1709 /*
1710 * RFC 1808
1711 * <protocol>:[//<net_loc>][/path][;<params>][?<query>][#<fragment>]
1712 *
1713 */
1714 LPCWSTR lpszParam = NULL;
1715 BOOL found_colon = FALSE;
1716 LPCWSTR lpszap, lpszUrl = lpszUrl_orig;
1717 LPCWSTR lpszcp = NULL, lpszNetLoc;
1718 LPWSTR lpszUrl_decode = NULL;
1719 DWORD dwUrlLength = dwUrlLength_orig;
1720
1721 TRACE("(%s %u %x %p)\n",
1722 lpszUrl ? debugstr_wn(lpszUrl, dwUrlLength ? dwUrlLength : strlenW(lpszUrl)) : "(null)",
1723 dwUrlLength, dwFlags, lpUC);
1724
1725 if (!lpszUrl_orig || !*lpszUrl_orig || !lpUC)
1726 {
1727 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1728 return FALSE;
1729 }
1730 if (!dwUrlLength) dwUrlLength = strlenW(lpszUrl);
1731
1732 if (dwFlags & ICU_DECODE)
1733 {
1734 WCHAR *url_tmp;
1735 DWORD len = dwUrlLength + 1;
1736
1737 if (!(url_tmp = heap_alloc(len * sizeof(WCHAR))))
1738 {
1739 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1740 return FALSE;
1741 }
1742 memcpy(url_tmp, lpszUrl_orig, dwUrlLength * sizeof(WCHAR));
1743 url_tmp[dwUrlLength] = 0;
1744 if (!(lpszUrl_decode = heap_alloc(len * sizeof(WCHAR))))
1745 {
1746 heap_free(url_tmp);
1747 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1748 return FALSE;
1749 }
1750 if (InternetCanonicalizeUrlW(url_tmp, lpszUrl_decode, &len, ICU_DECODE | ICU_NO_ENCODE))
1751 {
1752 dwUrlLength = len;
1753 lpszUrl = lpszUrl_decode;
1754 }
1755 heap_free(url_tmp);
1756 }
1757 lpszap = lpszUrl;
1758
1759 /* Determine if the URI is absolute. */
1760 while (lpszap - lpszUrl < dwUrlLength)
1761 {
1762 if (isalnumW(*lpszap) || *lpszap == '+' || *lpszap == '.' || *lpszap == '-')
1763 {
1764 lpszap++;
1765 continue;
1766 }
1767 if (*lpszap == ':')
1768 {
1769 found_colon = TRUE;
1770 lpszcp = lpszap;
1771 }
1772 else
1773 {
1774 lpszcp = lpszUrl; /* Relative url */
1775 }
1776
1777 break;
1778 }
1779
1780 if(!found_colon){
1781 SetLastError(ERROR_INTERNET_UNRECOGNIZED_SCHEME);
1782 return FALSE;
1783 }
1784
1785 lpUC->nScheme = INTERNET_SCHEME_UNKNOWN;
1786 lpUC->nPort = INTERNET_INVALID_PORT_NUMBER;
1787
1788 /* Parse <params> */
1789 lpszParam = memchrW(lpszap, '?', dwUrlLength - (lpszap - lpszUrl));
1790 if(!lpszParam)
1791 lpszParam = memchrW(lpszap, '#', dwUrlLength - (lpszap - lpszUrl));
1792
1793 SetUrlComponentValueW(&lpUC->lpszExtraInfo, &lpUC->dwExtraInfoLength,
1794 lpszParam, lpszParam ? dwUrlLength-(lpszParam-lpszUrl) : 0);
1795
1796
1797 /* Get scheme first. */
1798 lpUC->nScheme = GetInternetSchemeW(lpszUrl, lpszcp - lpszUrl);
1799 SetUrlComponentValueW(&lpUC->lpszScheme, &lpUC->dwSchemeLength,
1800 lpszUrl, lpszcp - lpszUrl);
1801
1802 /* Eat ':' in protocol. */
1803 lpszcp++;
1804
1805 /* double slash indicates the net_loc portion is present */
1806 if ((lpszcp[0] == '/') && (lpszcp[1] == '/'))
1807 {
1808 lpszcp += 2;
1809
1810 lpszNetLoc = memchrW(lpszcp, '/', dwUrlLength - (lpszcp - lpszUrl));
1811 if (lpszParam)
1812 {
1813 if (lpszNetLoc)
1814 lpszNetLoc = min(lpszNetLoc, lpszParam);
1815 else
1816 lpszNetLoc = lpszParam;
1817 }
1818 else if (!lpszNetLoc)
1819 lpszNetLoc = lpszcp + dwUrlLength-(lpszcp-lpszUrl);
1820
1821 /* Parse net-loc */
1822 if (lpszNetLoc)
1823 {
1824 LPCWSTR lpszHost;
1825 LPCWSTR lpszPort;
1826
1827 /* [<user>[<:password>]@]<host>[:<port>] */
1828 /* First find the user and password if they exist */
1829
1830 lpszHost = memchrW(lpszcp, '@', dwUrlLength - (lpszcp - lpszUrl));
1831 if (lpszHost == NULL || lpszHost > lpszNetLoc)
1832 {
1833 /* username and password not specified. */
1834 SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength, NULL, 0);
1835 SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength, NULL, 0);
1836 }
1837 else /* Parse out username and password */
1838 {
1839 LPCWSTR lpszUser = lpszcp;
1840 LPCWSTR lpszPasswd = lpszHost;
1841
1842 while (lpszcp < lpszHost)
1843 {
1844 if (*lpszcp == ':')
1845 lpszPasswd = lpszcp;
1846
1847 lpszcp++;
1848 }
1849
1850 SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength,
1851 lpszUser, lpszPasswd - lpszUser);
1852
1853 if (lpszPasswd != lpszHost)
1854 lpszPasswd++;
1855 SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength,
1856 lpszPasswd == lpszHost ? NULL : lpszPasswd,
1857 lpszHost - lpszPasswd);
1858
1859 lpszcp++; /* Advance to beginning of host */
1860 }
1861
1862 /* Parse <host><:port> */
1863
1864 lpszHost = lpszcp;
1865 lpszPort = lpszNetLoc;
1866
1867 /* special case for res:// URLs: there is no port here, so the host is the
1868 entire string up to the first '/' */
1869 if(lpUC->nScheme==INTERNET_SCHEME_RES)
1870 {
1871 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength,
1872 lpszHost, lpszPort - lpszHost);
1873 lpszcp=lpszNetLoc;
1874 }
1875 else
1876 {
1877 while (lpszcp < lpszNetLoc)
1878 {
1879 if (*lpszcp == ':')
1880 lpszPort = lpszcp;
1881
1882 lpszcp++;
1883 }
1884
1885 /* If the scheme is "file" and the host is just one letter, it's not a host */
1886 if(lpUC->nScheme==INTERNET_SCHEME_FILE && lpszPort <= lpszHost+1)
1887 {
1888 lpszcp=lpszHost;
1889 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength,
1890 NULL, 0);
1891 }
1892 else
1893 {
1894 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength,
1895 lpszHost, lpszPort - lpszHost);
1896 if (lpszPort != lpszNetLoc)
1897 lpUC->nPort = atoiW(++lpszPort);
1898 else switch (lpUC->nScheme)
1899 {
1900 case INTERNET_SCHEME_HTTP:
1901 lpUC->nPort = INTERNET_DEFAULT_HTTP_PORT;
1902 break;
1903 case INTERNET_SCHEME_HTTPS:
1904 lpUC->nPort = INTERNET_DEFAULT_HTTPS_PORT;
1905 break;
1906 case INTERNET_SCHEME_FTP:
1907 lpUC->nPort = INTERNET_DEFAULT_FTP_PORT;
1908 break;
1909 case INTERNET_SCHEME_GOPHER:
1910 lpUC->nPort = INTERNET_DEFAULT_GOPHER_PORT;
1911 break;
1912 default:
1913 break;
1914 }
1915 }
1916 }
1917 }
1918 }
1919 else
1920 {
1921 SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength, NULL, 0);
1922 SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength, NULL, 0);
1923 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength, NULL, 0);
1924 }
1925
1926 /* Here lpszcp points to:
1927 *
1928 * <protocol>:[//<net_loc>][/path][;<params>][?<query>][#<fragment>]
1929 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1930 */
1931 if (lpszcp != 0 && lpszcp - lpszUrl < dwUrlLength && (!lpszParam || lpszcp <= lpszParam))
1932 {
1933 DWORD len;
1934
1935 /* Only truncate the parameter list if it's already been saved
1936 * in lpUC->lpszExtraInfo.
1937 */
1938 if (lpszParam && lpUC->dwExtraInfoLength && lpUC->lpszExtraInfo)
1939 len = lpszParam - lpszcp;
1940 else
1941 {
1942 /* Leave the parameter list in lpszUrlPath. Strip off any trailing
1943 * newlines if necessary.
1944 */
1945 LPWSTR lpsznewline = memchrW(lpszcp, '\n', dwUrlLength - (lpszcp - lpszUrl));
1946 if (lpsznewline != NULL)
1947 len = lpsznewline - lpszcp;
1948 else
1949 len = dwUrlLength-(lpszcp-lpszUrl);
1950 }
1951 if (lpUC->dwUrlPathLength && lpUC->lpszUrlPath &&
1952 lpUC->nScheme == INTERNET_SCHEME_FILE)
1953 {
1954 WCHAR tmppath[MAX_PATH];
1955 if (*lpszcp == '/')
1956 {
1957 len = MAX_PATH;
1958 PathCreateFromUrlW(lpszUrl_orig, tmppath, &len, 0);
1959 }
1960 else
1961 {
1962 WCHAR *iter;
1963 memcpy(tmppath, lpszcp, len * sizeof(WCHAR));
1964 tmppath[len] = '\0';
1965
1966 iter = tmppath;
1967 while (*iter) {
1968 if (*iter == '/')
1969 *iter = '\\';
1970 ++iter;
1971 }
1972 }
1973 /* if ends in \. or \.. append a backslash */
1974 if (tmppath[len - 1] == '.' &&
1975 (tmppath[len - 2] == '\\' ||
1976 (tmppath[len - 2] == '.' && tmppath[len - 3] == '\\')))
1977 {
1978 if (len < MAX_PATH - 1)
1979 {
1980 tmppath[len] = '\\';
1981 tmppath[len+1] = '\0';
1982 ++len;
1983 }
1984 }
1985 SetUrlComponentValueW(&lpUC->lpszUrlPath, &lpUC->dwUrlPathLength,
1986 tmppath, len);
1987 }
1988 else
1989 SetUrlComponentValueW(&lpUC->lpszUrlPath, &lpUC->dwUrlPathLength,
1990 lpszcp, len);
1991 }
1992 else
1993 {
1994 if (lpUC->lpszUrlPath && (lpUC->dwUrlPathLength > 0))
1995 lpUC->lpszUrlPath[0] = 0;
1996 lpUC->dwUrlPathLength = 0;
1997 }
1998
1999 TRACE("%s: scheme(%s) host(%s) path(%s) extra(%s)\n", debugstr_wn(lpszUrl,dwUrlLength),
2000 debugstr_wn(lpUC->lpszScheme,lpUC->dwSchemeLength),
2001 debugstr_wn(lpUC->lpszHostName,lpUC->dwHostNameLength),
2002 debugstr_wn(lpUC->lpszUrlPath,lpUC->dwUrlPathLength),
2003 debugstr_wn(lpUC->lpszExtraInfo,lpUC->dwExtraInfoLength));
2004
2005 heap_free( lpszUrl_decode );
2006 return TRUE;
2007 }
2008
2009 /***********************************************************************
2010 * InternetAttemptConnect (WININET.@)
2011 *
2012 * Attempt to make a connection to the internet
2013 *
2014 * RETURNS
2015 * ERROR_SUCCESS on success
2016 * Error value on failure
2017 *
2018 */
2019 DWORD WINAPI InternetAttemptConnect(DWORD dwReserved)
2020 {
2021 FIXME("Stub\n");
2022 return ERROR_SUCCESS;
2023 }
2024
2025
2026 /***********************************************************************
2027 * convert_url_canonicalization_flags
2028 *
2029 * Helper for InternetCanonicalizeUrl
2030 *
2031 * PARAMS
2032 * dwFlags [I] Flags suitable for InternetCanonicalizeUrl
2033 *
2034 * RETURNS
2035 * Flags suitable for UrlCanonicalize
2036 */
2037 static DWORD convert_url_canonicalization_flags(DWORD dwFlags)
2038 {
2039 DWORD dwUrlFlags = URL_WININET_COMPATIBILITY | URL_ESCAPE_UNSAFE;
2040
2041 if (dwFlags & ICU_BROWSER_MODE) dwUrlFlags |= URL_BROWSER_MODE;
2042 if (dwFlags & ICU_DECODE) dwUrlFlags |= URL_UNESCAPE;
2043 if (dwFlags & ICU_ENCODE_PERCENT) dwUrlFlags |= URL_ESCAPE_PERCENT;
2044 if (dwFlags & ICU_ENCODE_SPACES_ONLY) dwUrlFlags |= URL_ESCAPE_SPACES_ONLY;
2045 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
2046 if (dwFlags & ICU_NO_ENCODE) dwUrlFlags ^= URL_ESCAPE_UNSAFE;
2047 if (dwFlags & ICU_NO_META) dwUrlFlags |= URL_NO_META;
2048
2049 return dwUrlFlags;
2050 }
2051
2052 /***********************************************************************
2053 * InternetCanonicalizeUrlA (WININET.@)
2054 *
2055 * Escape unsafe characters and spaces
2056 *
2057 * RETURNS
2058 * TRUE on success
2059 * FALSE on failure
2060 *
2061 */
2062 BOOL WINAPI InternetCanonicalizeUrlA(LPCSTR lpszUrl, LPSTR lpszBuffer,
2063 LPDWORD lpdwBufferLength, DWORD dwFlags)
2064 {
2065 HRESULT hr;
2066
2067 TRACE("(%s, %p, %p, 0x%08x) buffer length: %d\n", debugstr_a(lpszUrl), lpszBuffer,
2068 lpdwBufferLength, dwFlags, lpdwBufferLength ? *lpdwBufferLength : -1);
2069
2070 dwFlags = convert_url_canonicalization_flags(dwFlags);
2071 hr = UrlCanonicalizeA(lpszUrl, lpszBuffer, lpdwBufferLength, dwFlags);
2072 if (hr == E_POINTER) SetLastError(ERROR_INSUFFICIENT_BUFFER);
2073 if (hr == E_INVALIDARG) SetLastError(ERROR_INVALID_PARAMETER);
2074
2075 return hr == S_OK;
2076 }
2077
2078 /***********************************************************************
2079 * InternetCanonicalizeUrlW (WININET.@)
2080 *
2081 * Escape unsafe characters and spaces
2082 *
2083 * RETURNS
2084 * TRUE on success
2085 * FALSE on failure
2086 *
2087 */
2088 BOOL WINAPI InternetCanonicalizeUrlW(LPCWSTR lpszUrl, LPWSTR lpszBuffer,
2089 LPDWORD lpdwBufferLength, DWORD dwFlags)
2090 {
2091 HRESULT hr;
2092
2093 TRACE("(%s, %p, %p, 0x%08x) buffer length: %d\n", debugstr_w(lpszUrl), lpszBuffer,
2094 lpdwBufferLength, dwFlags, lpdwBufferLength ? *lpdwBufferLength : -1);
2095
2096 dwFlags = convert_url_canonicalization_flags(dwFlags);
2097 hr = UrlCanonicalizeW(lpszUrl, lpszBuffer, lpdwBufferLength, dwFlags);
2098 if (hr == E_POINTER) SetLastError(ERROR_INSUFFICIENT_BUFFER);
2099 if (hr == E_INVALIDARG) SetLastError(ERROR_INVALID_PARAMETER);
2100
2101 return hr == S_OK;
2102 }
2103
2104 /* #################################################### */
2105
2106 static INTERNET_STATUS_CALLBACK set_status_callback(
2107 object_header_t *lpwh, INTERNET_STATUS_CALLBACK callback, BOOL unicode)
2108 {
2109 INTERNET_STATUS_CALLBACK ret;
2110
2111 if (unicode) lpwh->dwInternalFlags |= INET_CALLBACKW;
2112 else lpwh->dwInternalFlags &= ~INET_CALLBACKW;
2113
2114 ret = lpwh->lpfnStatusCB;
2115 lpwh->lpfnStatusCB = callback;
2116
2117 return ret;
2118 }
2119
2120 /***********************************************************************
2121 * InternetSetStatusCallbackA (WININET.@)
2122 *
2123 * Sets up a callback function which is called as progress is made
2124 * during an operation.
2125 *
2126 * RETURNS
2127 * Previous callback or NULL on success
2128 * INTERNET_INVALID_STATUS_CALLBACK on failure
2129 *
2130 */
2131 INTERNET_STATUS_CALLBACK WINAPI InternetSetStatusCallbackA(
2132 HINTERNET hInternet ,INTERNET_STATUS_CALLBACK lpfnIntCB)
2133 {
2134 INTERNET_STATUS_CALLBACK retVal;
2135 object_header_t *lpwh;
2136
2137 TRACE("%p\n", hInternet);
2138
2139 if (!(lpwh = get_handle_object(hInternet)))
2140 return INTERNET_INVALID_STATUS_CALLBACK;
2141
2142 retVal = set_status_callback(lpwh, lpfnIntCB, FALSE);
2143
2144 WININET_Release( lpwh );
2145 return retVal;
2146 }
2147
2148 /***********************************************************************
2149 * InternetSetStatusCallbackW (WININET.@)
2150 *
2151 * Sets up a callback function which is called as progress is made
2152 * during an operation.
2153 *
2154 * RETURNS
2155 * Previous callback or NULL on success
2156 * INTERNET_INVALID_STATUS_CALLBACK on failure
2157 *
2158 */
2159 INTERNET_STATUS_CALLBACK WINAPI InternetSetStatusCallbackW(
2160 HINTERNET hInternet ,INTERNET_STATUS_CALLBACK lpfnIntCB)
2161 {
2162 INTERNET_STATUS_CALLBACK retVal;
2163 object_header_t *lpwh;
2164
2165 TRACE("%p\n", hInternet);
2166
2167 if (!(lpwh = get_handle_object(hInternet)))
2168 return INTERNET_INVALID_STATUS_CALLBACK;
2169
2170 retVal = set_status_callback(lpwh, lpfnIntCB, TRUE);
2171
2172 WININET_Release( lpwh );
2173 return retVal;
2174 }
2175
2176 /***********************************************************************
2177 * InternetSetFilePointer (WININET.@)
2178 */
2179 DWORD WINAPI InternetSetFilePointer(HINTERNET hFile, LONG lDistanceToMove,
2180 PVOID pReserved, DWORD dwMoveContext, DWORD_PTR dwContext)
2181 {
2182 FIXME("(%p %d %p %d %lx): stub\n", hFile, lDistanceToMove, pReserved, dwMoveContext, dwContext);
2183 return FALSE;
2184 }
2185
2186 /***********************************************************************
2187 * InternetWriteFile (WININET.@)
2188 *
2189 * Write data to an open internet file
2190 *
2191 * RETURNS
2192 * TRUE on success
2193 * FALSE on failure
2194 *
2195 */
2196 BOOL WINAPI InternetWriteFile(HINTERNET hFile, LPCVOID lpBuffer,
2197 DWORD dwNumOfBytesToWrite, LPDWORD lpdwNumOfBytesWritten)
2198 {
2199 object_header_t *lpwh;
2200 BOOL res;
2201
2202 TRACE("(%p %p %d %p)\n", hFile, lpBuffer, dwNumOfBytesToWrite, lpdwNumOfBytesWritten);
2203
2204 lpwh = get_handle_object( hFile );
2205 if (!lpwh) {
2206 WARN("Invalid handle\n");
2207 SetLastError(ERROR_INVALID_HANDLE);
2208 return FALSE;
2209 }
2210
2211 if(lpwh->vtbl->WriteFile) {
2212 res = lpwh->vtbl->WriteFile(lpwh, lpBuffer, dwNumOfBytesToWrite, lpdwNumOfBytesWritten);
2213 }else {
2214 WARN("No Writefile method.\n");
2215 res = ERROR_INVALID_HANDLE;
2216 }
2217
2218 WININET_Release( lpwh );
2219
2220 if(res != ERROR_SUCCESS)
2221 SetLastError(res);
2222 return res == ERROR_SUCCESS;
2223 }
2224
2225
2226 /***********************************************************************
2227 * InternetReadFile (WININET.@)
2228 *
2229 * Read data from an open internet file
2230 *
2231 * RETURNS
2232 * TRUE on success
2233 * FALSE on failure
2234 *
2235 */
2236 BOOL WINAPI InternetReadFile(HINTERNET hFile, LPVOID lpBuffer,
2237 DWORD dwNumOfBytesToRead, LPDWORD pdwNumOfBytesRead)
2238 {
2239 object_header_t *hdr;
2240 DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2241
2242 TRACE("%p %p %d %p\n", hFile, lpBuffer, dwNumOfBytesToRead, pdwNumOfBytesRead);
2243
2244 hdr = get_handle_object(hFile);
2245 if (!hdr) {
2246 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
2247 return FALSE;
2248 }
2249
2250 if(hdr->vtbl->ReadFile)
2251 res = hdr->vtbl->ReadFile(hdr, lpBuffer, dwNumOfBytesToRead, pdwNumOfBytesRead);
2252
2253 WININET_Release(hdr);
2254
2255 TRACE("-- %s (%u) (bytes read: %d)\n", res == ERROR_SUCCESS ? "TRUE": "FALSE", res,
2256 pdwNumOfBytesRead ? *pdwNumOfBytesRead : -1);
2257
2258 if(res != ERROR_SUCCESS)
2259 SetLastError(res);
2260 return res == ERROR_SUCCESS;
2261 }
2262
2263 /***********************************************************************
2264 * InternetReadFileExA (WININET.@)
2265 *
2266 * Read data from an open internet file
2267 *
2268 * PARAMS
2269 * hFile [I] Handle returned by InternetOpenUrl or HttpOpenRequest.
2270 * lpBuffersOut [I/O] Buffer.
2271 * dwFlags [I] Flags. See notes.
2272 * dwContext [I] Context for callbacks.
2273 *
2274 * RETURNS
2275 * TRUE on success
2276 * FALSE on failure
2277 *
2278 * NOTES
2279 * The parameter dwFlags include zero or more of the following flags:
2280 *|IRF_ASYNC - Makes the call asynchronous.
2281 *|IRF_SYNC - Makes the call synchronous.
2282 *|IRF_USE_CONTEXT - Forces dwContext to be used.
2283 *|IRF_NO_WAIT - Don't block if the data is not available, just return what is available.
2284 *
2285 * However, in testing IRF_USE_CONTEXT seems to have no effect - dwContext isn't used.
2286 *
2287 * SEE
2288 * InternetOpenUrlA(), HttpOpenRequestA()
2289 */
2290 BOOL WINAPI InternetReadFileExA(HINTERNET hFile, LPINTERNET_BUFFERSA lpBuffersOut,
2291 DWORD dwFlags, DWORD_PTR dwContext)
2292 {
2293 object_header_t *hdr;
2294 DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2295
2296 TRACE("(%p %p 0x%x 0x%lx)\n", hFile, lpBuffersOut, dwFlags, dwContext);
2297
2298 if (lpBuffersOut->dwStructSize != sizeof(*lpBuffersOut)) {
2299 SetLastError(ERROR_INVALID_PARAMETER);
2300 return FALSE;
2301 }
2302
2303 hdr = get_handle_object(hFile);
2304 if (!hdr) {
2305 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
2306 return FALSE;
2307 }
2308
2309 if(hdr->vtbl->ReadFileEx)
2310 res = hdr->vtbl->ReadFileEx(hdr, lpBuffersOut->lpvBuffer, lpBuffersOut->dwBufferLength,
2311 &lpBuffersOut->dwBufferLength, dwFlags, dwContext);
2312
2313 WININET_Release(hdr);
2314
2315 TRACE("-- %s (%u, bytes read: %d)\n", res == ERROR_SUCCESS ? "TRUE": "FALSE",
2316 res, lpBuffersOut->dwBufferLength);
2317
2318 if(res != ERROR_SUCCESS)
2319 SetLastError(res);
2320 return res == ERROR_SUCCESS;
2321 }
2322
2323 /***********************************************************************
2324 * InternetReadFileExW (WININET.@)
2325 * SEE
2326 * InternetReadFileExA()
2327 */
2328 BOOL WINAPI InternetReadFileExW(HINTERNET hFile, LPINTERNET_BUFFERSW lpBuffer,
2329 DWORD dwFlags, DWORD_PTR dwContext)
2330 {
2331 object_header_t *hdr;
2332 DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2333
2334 TRACE("(%p %p 0x%x 0x%lx)\n", hFile, lpBuffer, dwFlags, dwContext);
2335
2336 if (lpBuffer->dwStructSize != sizeof(*lpBuffer)) {
2337 SetLastError(ERROR_INVALID_PARAMETER);
2338 return FALSE;
2339 }
2340
2341 hdr = get_handle_object(hFile);
2342 if (!hdr) {
2343 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
2344 return FALSE;
2345 }
2346
2347 if(hdr->vtbl->ReadFileEx)
2348 res = hdr->vtbl->ReadFileEx(hdr, lpBuffer->lpvBuffer, lpBuffer->dwBufferLength, &lpBuffer->dwBufferLength,
2349 dwFlags, dwContext);
2350
2351 WININET_Release(hdr);
2352
2353 TRACE("-- %s (%u, bytes read: %d)\n", res == ERROR_SUCCESS ? "TRUE": "FALSE",
2354 res, lpBuffer->dwBufferLength);
2355
2356 if(res != ERROR_SUCCESS)
2357 SetLastError(res);
2358 return res == ERROR_SUCCESS;
2359 }
2360
2361 static BOOL get_proxy_autoconfig_url( char *buf, DWORD buflen )
2362 {
2363 #if defined(MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
2364
2365 CFDictionaryRef settings = CFNetworkCopySystemProxySettings();
2366 const void *ref;
2367 BOOL ret = FALSE;
2368
2369 if (!settings) return FALSE;
2370
2371 if (!(ref = CFDictionaryGetValue( settings, kCFNetworkProxiesProxyAutoConfigURLString )))
2372 {
2373 CFRelease( settings );
2374 return FALSE;
2375 }
2376 if (CFStringGetCString( ref, buf, buflen, kCFStringEncodingASCII ))
2377 {
2378 TRACE( "returning %s\n", debugstr_a(buf) );
2379 ret = TRUE;
2380 }
2381 CFRelease( settings );
2382 return ret;
2383 #else
2384 static int once;
2385 if (!once++) FIXME( "no support on this platform\n" );
2386 return FALSE;
2387 #endif
2388 }
2389
2390 static DWORD query_global_option(DWORD option, void *buffer, DWORD *size, BOOL unicode)
2391 {
2392 /* FIXME: This function currently handles more options than it should. Options requiring
2393 * proper handles should be moved to proper functions */
2394 switch(option) {
2395 case INTERNET_OPTION_HTTP_VERSION:
2396 if (*size < sizeof(HTTP_VERSION_INFO))
2397 return ERROR_INSUFFICIENT_BUFFER;
2398
2399 /*
2400 * Presently hardcoded to 1.1
2401 */
2402 ((HTTP_VERSION_INFO*)buffer)->dwMajorVersion = 1;
2403 ((HTTP_VERSION_INFO*)buffer)->dwMinorVersion = 1;
2404 *size = sizeof(HTTP_VERSION_INFO);
2405
2406 return ERROR_SUCCESS;
2407
2408 case INTERNET_OPTION_CONNECTED_STATE:
2409 FIXME("INTERNET_OPTION_CONNECTED_STATE: semi-stub\n");
2410
2411 if (*size < sizeof(ULONG))
2412 return ERROR_INSUFFICIENT_BUFFER;
2413
2414 *(ULONG*)buffer = INTERNET_STATE_CONNECTED;
2415 *size = sizeof(ULONG);
2416
2417 return ERROR_SUCCESS;
2418
2419 case INTERNET_OPTION_PROXY: {
2420 appinfo_t ai;
2421 BOOL ret;
2422
2423 TRACE("Getting global proxy info\n");
2424 memset(&ai, 0, sizeof(appinfo_t));
2425 INTERNET_ConfigureProxy(&ai);
2426
2427 ret = APPINFO_QueryOption(&ai.hdr, INTERNET_OPTION_PROXY, buffer, size, unicode); /* FIXME */
2428 APPINFO_Destroy(&ai.hdr);
2429 return ret;
2430 }
2431
2432 case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2433 TRACE("INTERNET_OPTION_MAX_CONNS_PER_SERVER\n");
2434
2435 if (*size < sizeof(ULONG))
2436 return ERROR_INSUFFICIENT_BUFFER;
2437
2438 *(ULONG*)buffer = max_conns;
2439 *size = sizeof(ULONG);
2440
2441 return ERROR_SUCCESS;
2442
2443 case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2444 TRACE("INTERNET_OPTION_MAX_CONNS_1_0_SERVER\n");
2445
2446 if (*size < sizeof(ULONG))
2447 return ERROR_INSUFFICIENT_BUFFER;
2448
2449 *(ULONG*)buffer = max_1_0_conns;
2450 *size = sizeof(ULONG);
2451
2452 return ERROR_SUCCESS;
2453
2454 case INTERNET_OPTION_SECURITY_FLAGS:
2455 FIXME("INTERNET_OPTION_SECURITY_FLAGS: Stub\n");
2456 return ERROR_SUCCESS;
2457
2458 case INTERNET_OPTION_VERSION: {
2459 static const INTERNET_VERSION_INFO info = { 1, 2 };
2460
2461 TRACE("INTERNET_OPTION_VERSION\n");
2462
2463 if (*size < sizeof(INTERNET_VERSION_INFO))
2464 return ERROR_INSUFFICIENT_BUFFER;
2465
2466 memcpy(buffer, &info, sizeof(info));
2467 *size = sizeof(info);
2468
2469 return ERROR_SUCCESS;
2470 }
2471
2472 case INTERNET_OPTION_PER_CONNECTION_OPTION: {
2473 char url[INTERNET_MAX_URL_LENGTH + 1];
2474 INTERNET_PER_CONN_OPTION_LISTW *con = buffer;
2475 INTERNET_PER_CONN_OPTION_LISTA *conA = buffer;
2476 DWORD res = ERROR_SUCCESS, i;
2477 proxyinfo_t pi;
2478 BOOL have_url;
2479 LONG ret;
2480
2481 TRACE("Getting global proxy info\n");
2482 if((ret = INTERNET_LoadProxySettings(&pi)))
2483 return ret;
2484
2485 have_url = get_proxy_autoconfig_url(url, sizeof(url));
2486
2487 FIXME("INTERNET_OPTION_PER_CONNECTION_OPTION stub\n");
2488
2489 if (*size < sizeof(INTERNET_PER_CONN_OPTION_LISTW)) {
2490 FreeProxyInfo(&pi);
2491 return ERROR_INSUFFICIENT_BUFFER;
2492 }
2493
2494 for (i = 0; i < con->dwOptionCount; i++) {
2495 INTERNET_PER_CONN_OPTIONW *optionW = con->pOptions + i;
2496 INTERNET_PER_CONN_OPTIONA *optionA = conA->pOptions + i;
2497
2498 switch (optionW->dwOption) {
2499 case INTERNET_PER_CONN_FLAGS:
2500 if(pi.proxyEnabled)
2501 optionW->Value.dwValue = PROXY_TYPE_PROXY;
2502 else
2503 optionW->Value.dwValue = PROXY_TYPE_DIRECT;
2504 if (have_url)
2505 /* native includes PROXY_TYPE_DIRECT even if PROXY_TYPE_PROXY is set */
2506 optionW->Value.dwValue |= PROXY_TYPE_DIRECT|PROXY_TYPE_AUTO_PROXY_URL;
2507 break;
2508
2509 case INTERNET_PER_CONN_PROXY_SERVER:
2510 if (unicode)
2511 optionW->Value.pszValue = heap_strdupW(pi.proxy);
2512 else
2513 optionA->Value.pszValue = heap_strdupWtoA(pi.proxy);
2514 break;
2515
2516 case INTERNET_PER_CONN_PROXY_BYPASS:
2517 if (unicode)
2518 optionW->Value.pszValue = heap_strdupW(pi.proxyBypass);
2519 else
2520 optionA->Value.pszValue = heap_strdupWtoA(pi.proxyBypass);
2521 break;
2522
2523 case INTERNET_PER_CONN_AUTOCONFIG_URL:
2524 if (!have_url)
2525 optionW->Value.pszValue = NULL;
2526 else if (unicode)
2527 optionW->Value.pszValue = heap_strdupAtoW(url);
2528 else
2529 optionA->Value.pszValue = heap_strdupA(url);
2530 break;
2531
2532 case INTERNET_PER_CONN_AUTODISCOVERY_FLAGS:
2533 optionW->Value.dwValue = AUTO_PROXY_FLAG_ALWAYS_DETECT;
2534 break;
2535
2536 case INTERNET_PER_CONN_AUTOCONFIG_SECONDARY_URL:
2537 case INTERNET_PER_CONN_AUTOCONFIG_RELOAD_DELAY_MINS:
2538 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_TIME:
2539 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_URL:
2540 FIXME("Unhandled dwOption %d\n", optionW->dwOption);
2541 memset(&optionW->Value, 0, sizeof(optionW->Value));
2542 break;
2543
2544 default:
2545 FIXME("Unknown dwOption %d\n", optionW->dwOption);
2546 res = ERROR_INVALID_PARAMETER;
2547 break;
2548 }
2549 }
2550 FreeProxyInfo(&pi);
2551
2552 return res;
2553 }
2554 case INTERNET_OPTION_REQUEST_FLAGS:
2555 case INTERNET_OPTION_USER_AGENT:
2556 *size = 0;
2557 return ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2558 case INTERNET_OPTION_POLICY:
2559 return ERROR_INVALID_PARAMETER;
2560 case INTERNET_OPTION_CONNECT_TIMEOUT:
2561 TRACE("INTERNET_OPTION_CONNECT_TIMEOUT\n");
2562
2563 if (*size < sizeof(ULONG))
2564 return ERROR_INSUFFICIENT_BUFFER;
2565
2566 *(ULONG*)buffer = connect_timeout;
2567 *size = sizeof(ULONG);
2568
2569 return ERROR_SUCCESS;
2570 }
2571
2572 FIXME("Stub for %d\n", option);
2573 return ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2574 }
2575
2576 DWORD INET_QueryOption(object_header_t *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
2577 {
2578 switch(option) {
2579 case INTERNET_OPTION_CONTEXT_VALUE:
2580 if (!size)
2581 return ERROR_INVALID_PARAMETER;
2582
2583 if (*size < sizeof(DWORD_PTR)) {
2584 *size = sizeof(DWORD_PTR);
2585 return ERROR_INSUFFICIENT_BUFFER;
2586 }
2587 if (!buffer)
2588 return ERROR_INVALID_PARAMETER;
2589
2590 *(DWORD_PTR *)buffer = hdr->dwContext;
2591 *size = sizeof(DWORD_PTR);
2592 return ERROR_SUCCESS;
2593
2594 case INTERNET_OPTION_REQUEST_FLAGS:
2595 WARN("INTERNET_OPTION_REQUEST_FLAGS\n");
2596 *size = sizeof(DWORD);
2597 return ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2598
2599 case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2600 case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2601 WARN("Called on global option %u\n", option);
2602 return ERROR_INTERNET_INVALID_OPERATION;
2603 }
2604
2605 /* FIXME: we shouldn't call it here */
2606 return query_global_option(option, buffer, size, unicode);
2607 }
2608
2609 /***********************************************************************
2610 * InternetQueryOptionW (WININET.@)
2611 *
2612 * Queries an options on the specified handle
2613 *
2614 * RETURNS
2615 * TRUE on success
2616 * FALSE on failure
2617 *
2618 */
2619 BOOL WINAPI InternetQueryOptionW(HINTERNET hInternet, DWORD dwOption,
2620 LPVOID lpBuffer, LPDWORD lpdwBufferLength)
2621 {
2622 object_header_t *hdr;
2623 DWORD res = ERROR_INVALID_HANDLE;
2624
2625 TRACE("%p %d %p %p\n", hInternet, dwOption, lpBuffer, lpdwBufferLength);
2626
2627 if(hInternet) {
2628 hdr = get_handle_object(hInternet);
2629 if (hdr) {
2630 res = hdr->vtbl->QueryOption(hdr, dwOption, lpBuffer, lpdwBufferLength, TRUE);
2631 WININET_Release(hdr);
2632 }
2633 }else {
2634 res = query_global_option(dwOption, lpBuffer, lpdwBufferLength, TRUE);
2635 }
2636
2637 if(res != ERROR_SUCCESS)
2638 SetLastError(res);
2639 return res == ERROR_SUCCESS;
2640 }
2641
2642 /***********************************************************************
2643 * InternetQueryOptionA (WININET.@)
2644 *
2645 * Queries an options on the specified handle
2646 *
2647 * RETURNS
2648 * TRUE on success
2649 * FALSE on failure
2650 *
2651 */
2652 BOOL WINAPI InternetQueryOptionA(HINTERNET hInternet, DWORD dwOption,
2653 LPVOID lpBuffer, LPDWORD lpdwBufferLength)
2654 {
2655 object_header_t *hdr;
2656 DWORD res = ERROR_INVALID_HANDLE;
2657
2658 TRACE("%p %d %p %p\n", hInternet, dwOption, lpBuffer, lpdwBufferLength);
2659
2660 if(hInternet) {
2661 hdr = get_handle_object(hInternet);
2662 if (hdr) {
2663 res = hdr->vtbl->QueryOption(hdr, dwOption, lpBuffer, lpdwBufferLength, FALSE);
2664 WININET_Release(hdr);
2665 }
2666 }else {
2667 res = query_global_option(dwOption, lpBuffer, lpdwBufferLength, FALSE);
2668 }
2669
2670 if(res != ERROR_SUCCESS)
2671 SetLastError(res);
2672 return res == ERROR_SUCCESS;
2673 }
2674
2675 DWORD INET_SetOption(object_header_t *hdr, DWORD option, void *buf, DWORD size)
2676 {
2677 switch(option) {
2678 case INTERNET_OPTION_CALLBACK:
2679 WARN("Not settable option %u\n", option);
2680 return ERROR_INTERNET_OPTION_NOT_SETTABLE;
2681 case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2682 case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2683 WARN("Called on global option %u\n", option);
2684 return ERROR_INTERNET_INVALID_OPERATION;
2685 }
2686
2687 return ERROR_INTERNET_INVALID_OPTION;
2688 }
2689
2690 static DWORD set_global_option(DWORD option, void *buf, DWORD size)
2691 {
2692 switch(option) {
2693 case INTERNET_OPTION_CALLBACK:
2694 WARN("Not global option %u\n", option);
2695 return ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2696
2697 case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2698 TRACE("INTERNET_OPTION_MAX_CONNS_PER_SERVER\n");
2699
2700 if(size != sizeof(max_conns))
2701 return ERROR_INTERNET_BAD_OPTION_LENGTH;
2702 if(!*(ULONG*)buf)
2703 return ERROR_BAD_ARGUMENTS;
2704
2705 max_conns = *(ULONG*)buf;
2706 return ERROR_SUCCESS;
2707
2708 case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2709 TRACE("INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER\n");
2710
2711 if(size != sizeof(max_1_0_conns))
2712 return ERROR_INTERNET_BAD_OPTION_LENGTH;
2713 if(!*(ULONG*)buf)
2714 return ERROR_BAD_ARGUMENTS;
2715
2716 max_1_0_conns = *(ULONG*)buf;
2717 return ERROR_SUCCESS;
2718
2719 case INTERNET_OPTION_CONNECT_TIMEOUT:
2720 TRACE("INTERNET_OPTION_CONNECT_TIMEOUT\n");
2721
2722 if(size != sizeof(connect_timeout))
2723 return ERROR_INTERNET_BAD_OPTION_LENGTH;
2724 if(!*(ULONG*)buf)
2725 return ERROR_BAD_ARGUMENTS;
2726
2727 connect_timeout = *(ULONG*)buf;
2728 return ERROR_SUCCESS;
2729
2730 case INTERNET_OPTION_SETTINGS_CHANGED:
2731 FIXME("INTERNETOPTION_SETTINGS_CHANGED semi-stub\n");
2732 collect_connections(COLLECT_CONNECTIONS);
2733 return ERROR_SUCCESS;
2734
2735 case INTERNET_OPTION_SUPPRESS_BEHAVIOR:
2736 FIXME("INTERNET_OPTION_SUPPRESS_BEHAVIOR stub\n");
2737
2738 if(size != sizeof(ULONG))
2739 return ERROR_INTERNET_BAD_OPTION_LENGTH;
2740
2741 FIXME("%08x\n", *(ULONG*)buf);
2742 return ERROR_SUCCESS;
2743 }
2744
2745 return ERROR_INTERNET_INVALID_OPTION;
2746 }
2747
2748 /***********************************************************************
2749 * InternetSetOptionW (WININET.@)
2750 *
2751 * Sets an options on the specified handle
2752 *
2753 * RETURNS
2754 * TRUE on success
2755 * FALSE on failure
2756 *
2757 */
2758 BOOL WINAPI InternetSetOptionW(HINTERNET hInternet, DWORD dwOption,
2759 LPVOID lpBuffer, DWORD dwBufferLength)
2760 {
2761 object_header_t *lpwhh;
2762 BOOL ret = TRUE;
2763 DWORD res;
2764
2765 TRACE("(%p %d %p %d)\n", hInternet, dwOption, lpBuffer, dwBufferLength);
2766
2767 lpwhh = (object_header_t*) get_handle_object( hInternet );
2768 if(lpwhh)
2769 res = lpwhh->vtbl->SetOption(lpwhh, dwOption, lpBuffer, dwBufferLength);
2770 else
2771 res = set_global_option(dwOption, lpBuffer, dwBufferLength);
2772
2773 if(res != ERROR_INTERNET_INVALID_OPTION) {
2774 if(lpwhh)
2775 WININET_Release(lpwhh);
2776
2777 if(res != ERROR_SUCCESS)
2778 SetLastError(res);
2779
2780 return res == ERROR_SUCCESS;
2781 }
2782
2783 switch (dwOption)
2784 {
2785 case INTERNET_OPTION_HTTP_VERSION:
2786 {
2787 HTTP_VERSION_INFO* pVersion=(HTTP_VERSION_INFO*)lpBuffer;
2788 FIXME("Option INTERNET_OPTION_HTTP_VERSION(%d,%d): STUB\n",pVersion->dwMajorVersion,pVersion->dwMinorVersion);
2789 }
2790 break;
2791 case INTERNET_OPTION_ERROR_MASK:
2792 {
2793 if(!lpwhh) {
2794 SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2795 return FALSE;
2796 } else if(*(ULONG*)lpBuffer & (~(INTERNET_ERROR_MASK_INSERT_CDROM|
2797 INTERNET_ERROR_MASK_COMBINED_SEC_CERT|
2798 INTERNET_ERROR_MASK_LOGIN_FAILURE_DISPLAY_ENTITY_BODY))) {
2799 SetLastError(ERROR_INVALID_PARAMETER);
2800 ret = FALSE;
2801 } else if(dwBufferLength != sizeof(ULONG)) {
2802 SetLastError(ERROR_INTERNET_BAD_OPTION_LENGTH);
2803 ret = FALSE;
2804 } else
2805 TRACE("INTERNET_OPTION_ERROR_MASK: %x\n", *(ULONG*)lpBuffer);
2806 lpwhh->ErrorMask = *(ULONG*)lpBuffer;
2807 }
2808 break;
2809 case INTERNET_OPTION_PROXY:
2810 {
2811 INTERNET_PROXY_INFOW *info = lpBuffer;
2812
2813 if (!lpBuffer || dwBufferLength < sizeof(INTERNET_PROXY_INFOW))
2814 {
2815 SetLastError(ERROR_INVALID_PARAMETER);
2816 return FALSE;
2817 }
2818 if (!hInternet)
2819 {
2820 EnterCriticalSection( &WININET_cs );
2821 free_global_proxy();
2822 global_proxy = heap_alloc( sizeof(proxyinfo_t) );
2823 if (global_proxy)
2824 {
2825 if (info->dwAccessType == INTERNET_OPEN_TYPE_PROXY)
2826 {
2827 global_proxy->proxyEnabled = 1;
2828 global_proxy->proxy = heap_strdupW( info->lpszProxy );
2829 global_proxy->proxyBypass = heap_strdupW( info->lpszProxyBypass );
2830 }
2831 else
2832 {
2833 global_proxy->proxyEnabled = 0;
2834 global_proxy->proxy = global_proxy->proxyBypass = NULL;
2835 }
2836 }
2837 LeaveCriticalSection( &WININET_cs );
2838 }
2839 else
2840 {
2841 /* In general, each type of object should handle
2842 * INTERNET_OPTION_PROXY directly. This FIXME ensures it doesn't
2843 * get silently dropped.
2844 */
2845 FIXME("INTERNET_OPTION_PROXY unimplemented\n");
2846 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2847 ret = FALSE;
2848 }
2849 break;
2850 }
2851 case INTERNET_OPTION_CODEPAGE:
2852 {
2853 ULONG codepage = *(ULONG *)lpBuffer;
2854 FIXME("Option INTERNET_OPTION_CODEPAGE (%d): STUB\n", codepage);
2855 }
2856 break;
2857 case INTERNET_OPTION_REQUEST_PRIORITY:
2858 {
2859 ULONG priority = *(ULONG *)lpBuffer;
2860 FIXME("Option INTERNET_OPTION_REQUEST_PRIORITY (%d): STUB\n", priority);
2861 }
2862 break;
2863 case INTERNET_OPTION_CONNECT_TIMEOUT:
2864 {
2865 ULONG connecttimeout = *(ULONG *)lpBuffer;
2866 FIXME("Option INTERNET_OPTION_CONNECT_TIMEOUT (%d): STUB\n", connecttimeout);
2867 }
2868 break;
2869 case INTERNET_OPTION_DATA_RECEIVE_TIMEOUT:
2870 {
2871 ULONG receivetimeout = *(ULONG *)lpBuffer;
2872 FIXME("Option INTERNET_OPTION_DATA_RECEIVE_TIMEOUT (%d): STUB\n", receivetimeout);
2873 }
2874 break;
2875 case INTERNET_OPTION_RESET_URLCACHE_SESSION:
2876 FIXME("Option INTERNET_OPTION_RESET_URLCACHE_SESSION: STUB\n");
2877 break;
2878 case INTERNET_OPTION_END_BROWSER_SESSION:
2879 FIXME("Option INTERNET_OPTION_END_BROWSER_SESSION: STUB\n");
2880 break;
2881 case INTERNET_OPTION_CONNECTED_STATE:
2882 FIXME("Option INTERNET_OPTION_CONNECTED_STATE: STUB\n");
2883 break;
2884 case INTERNET_OPTION_DISABLE_PASSPORT_AUTH:
2885 TRACE("Option INTERNET_OPTION_DISABLE_PASSPORT_AUTH: harmless stub, since not enabled\n");
2886 break;
2887 case INTERNET_OPTION_SEND_TIMEOUT:
2888 case INTERNET_OPTION_RECEIVE_TIMEOUT:
2889 case INTERNET_OPTION_DATA_SEND_TIMEOUT:
2890 {
2891 ULONG timeout = *(ULONG *)lpBuffer;
2892 FIXME("INTERNET_OPTION_SEND/RECEIVE_TIMEOUT/DATA_SEND_TIMEOUT %d\n", timeout);
2893 break;
2894 }
2895 case INTERNET_OPTION_CONNECT_RETRIES:
2896 {
2897 ULONG retries = *(ULONG *)lpBuffer;
2898 FIXME("INTERNET_OPTION_CONNECT_RETRIES %d\n", retries);
2899 break;
2900 }
2901 case INTERNET_OPTION_CONTEXT_VALUE:
2902 {
2903 if (!lpwhh)
2904 {
2905 SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2906 return FALSE;
2907 }
2908 if (!lpBuffer || dwBufferLength != sizeof(DWORD_PTR))
2909 {
2910 SetLastError(ERROR_INVALID_PARAMETER);
2911 ret = FALSE;
2912 }
2913 else
2914 lpwhh->dwContext = *(DWORD_PTR *)lpBuffer;
2915 break;
2916 }
2917 case INTERNET_OPTION_SECURITY_FLAGS:
2918 FIXME("Option INTERNET_OPTION_SECURITY_FLAGS; STUB\n");
2919 break;
2920 case INTERNET_OPTION_DISABLE_AUTODIAL:
2921 FIXME("Option INTERNET_OPTION_DISABLE_AUTODIAL; STUB\n");
2922 break;
2923 case INTERNET_OPTION_HTTP_DECODING:
2924 FIXME("INTERNET_OPTION_HTTP_DECODING; STUB\n");
2925 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2926 ret = FALSE;
2927 break;
2928 case INTERNET_OPTION_COOKIES_3RD_PARTY:
2929 FIXME("INTERNET_OPTION_COOKIES_3RD_PARTY; STUB\n");
2930 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2931 ret = FALSE;
2932 break;
2933 case INTERNET_OPTION_SEND_UTF8_SERVERNAME_TO_PROXY:
2934 FIXME("INTERNET_OPTION_SEND_UTF8_SERVERNAME_TO_PROXY; STUB\n");
2935 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2936 ret = FALSE;
2937 break;
2938 case INTERNET_OPTION_CODEPAGE_PATH:
2939 FIXME("INTERNET_OPTION_CODEPAGE_PATH; STUB\n");
2940 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2941 ret = FALSE;
2942 break;
2943 case INTERNET_OPTION_CODEPAGE_EXTRA:
2944 FIXME("INTERNET_OPTION_CODEPAGE_EXTRA; STUB\n");
2945 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2946 ret = FALSE;
2947 break;
2948 case INTERNET_OPTION_IDN:
2949 FIXME("INTERNET_OPTION_IDN; STUB\n");
2950 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2951 ret = FALSE;
2952 break;
2953 case INTERNET_OPTION_POLICY:
2954 SetLastError(ERROR_INVALID_PARAMETER);
2955 ret = FALSE;
2956 break;
2957 case INTERNET_OPTION_PER_CONNECTION_OPTION: {
2958 INTERNET_PER_CONN_OPTION_LISTW *con = lpBuffer;
2959 LONG res;
2960 unsigned int i;
2961 proxyinfo_t pi;
2962
2963 if (INTERNET_LoadProxySettings(&pi)) return FALSE;
2964
2965 for (i = 0; i < con->dwOptionCount; i++) {
2966 INTERNET_PER_CONN_OPTIONW *option = con->pOptions + i;
2967
2968 switch (option->dwOption) {
2969 case INTERNET_PER_CONN_PROXY_SERVER:
2970 heap_free(pi.proxy);
2971 pi.proxy = heap_strdupW(option->Value.pszValue);
2972 break;
2973
2974 case INTERNET_PER_CONN_FLAGS:
2975 if(option->Value.dwValue & PROXY_TYPE_PROXY)
2976 pi.proxyEnabled = 1;
2977 else
2978 {
2979 if(option->Value.dwValue != PROXY_TYPE_DIRECT)
2980 FIXME("Unhandled flags: 0x%x\n", option->Value.dwValue);
2981 pi.proxyEnabled = 0;
2982 }
2983 break;
2984
2985 case INTERNET_PER_CONN_PROXY_BYPASS:
2986 heap_free(pi.proxyBypass);
2987 pi.proxyBypass = heap_strdupW(option->Value.pszValue);
2988 break;
2989
2990 case INTERNET_PER_CONN_AUTOCONFIG_URL:
2991 case INTERNET_PER_CONN_AUTODISCOVERY_FLAGS:
2992 case INTERNET_PER_CONN_AUTOCONFIG_SECONDARY_URL:
2993 case INTERNET_PER_CONN_AUTOCONFIG_RELOAD_DELAY_MINS:
2994 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_TIME:
2995 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_URL:
2996 FIXME("Unhandled dwOption %d\n", option->dwOption);
2997 break;
2998
2999 default:
3000 FIXME("Unknown dwOption %d\n", option->dwOption);
3001 SetLastError(ERROR_INVALID_PARAMETER);
3002 break;
3003 }
3004 }
3005
3006 if ((res = INTERNET_SaveProxySettings(&pi)))
3007 SetLastError(res);
3008
3009 FreeProxyInfo(&pi);
3010
3011 ret = (res == ERROR_SUCCESS);
3012 break;
3013 }
3014 case INTERNET_OPTION_SETTINGS_CHANGED:
3015 FIXME("INTERNET_OPTION_SETTINGS_CHANGED; STUB\n");
3016 break;
3017 case INTERNET_OPTION_REFRESH:
3018 FIXME("INTERNET_OPTION_REFRESH; STUB\n");
3019 break;
3020 default:
3021 FIXME("Option %d STUB\n",dwOption);
3022 SetLastError(ERROR_INTERNET_INVALID_OPTION);
3023 ret = FALSE;
3024 break;
3025 }
3026
3027 if(lpwhh)
3028 WININET_Release( lpwhh );
3029
3030 return ret;
3031 }
3032
3033
3034 /***********************************************************************
3035 * InternetSetOptionA (WININET.@)
3036 *
3037 * Sets an options on the specified handle.
3038 *
3039 * RETURNS
3040 * TRUE on success
3041 * FALSE on failure
3042 *
3043 */
3044 BOOL WINAPI InternetSetOptionA(HINTERNET hInternet, DWORD dwOption,
3045 LPVOID lpBuffer, DWORD dwBufferLength)
3046 {
3047 LPVOID wbuffer;
3048 DWORD wlen;
3049 BOOL r;
3050
3051 switch( dwOption )
3052 {
3053 case INTERNET_OPTION_PROXY:
3054 {
3055 LPINTERNET_PROXY_INFOA pi = (LPINTERNET_PROXY_INFOA) lpBuffer;
3056 LPINTERNET_PROXY_INFOW piw;
3057 DWORD proxlen, prbylen;
3058 LPWSTR prox, prby;
3059
3060 proxlen = MultiByteToWideChar( CP_ACP, 0, pi->lpszProxy, -1, NULL, 0);
3061 prbylen= MultiByteToWideChar( CP_ACP, 0, pi->lpszProxyBypass, -1, NULL, 0);
3062 wlen = sizeof(*piw) + proxlen + prbylen;
3063 wbuffer = heap_alloc(wlen*sizeof(WCHAR) );
3064 piw = (LPINTERNET_PROXY_INFOW) wbuffer;
3065 piw->dwAccessType = pi->dwAccessType;
3066 prox = (LPWSTR) &piw[1];
3067 prby = &prox[proxlen+1];
3068 MultiByteToWideChar( CP_ACP, 0, pi->lpszProxy, -1, prox, proxlen);
3069 MultiByteToWideChar( CP_ACP, 0, pi->lpszProxyBypass, -1, prby, prbylen);
3070 piw->lpszProxy = prox;
3071 piw->lpszProxyBypass = prby;
3072 }
3073 break;
3074 case INTERNET_OPTION_USER_AGENT:
3075 case INTERNET_OPTION_USERNAME:
3076 case INTERNET_OPTION_PASSWORD:
3077 case INTERNET_OPTION_PROXY_USERNAME:
3078 case INTERNET_OPTION_PROXY_PASSWORD:
3079 wlen = MultiByteToWideChar( CP_ACP, 0, lpBuffer, -1, NULL, 0 );
3080 if (!(wbuffer = heap_alloc( wlen * sizeof(WCHAR) ))) return ERROR_OUTOFMEMORY;
3081 MultiByteToWideChar( CP_ACP, 0, lpBuffer, -1, wbuffer, wlen );
3082 break;
3083 case INTERNET_OPTION_PER_CONNECTION_OPTION: {
3084 unsigned int i;
3085 INTERNET_PER_CONN_OPTION_LISTW *listW;
3086 INTERNET_PER_CONN_OPTION_LISTA *listA = lpBuffer;
3087 wlen = sizeof(INTERNET_PER_CONN_OPTION_LISTW);
3088 wbuffer = heap_alloc(wlen);
3089 listW = wbuffer;
3090
3091 listW->dwSize = sizeof(INTERNET_PER_CONN_OPTION_LISTW);
3092 if (listA->pszConnection)
3093 {
3094 wlen = MultiByteToWideChar( CP_ACP, 0, listA->pszConnection, -1, NULL, 0 );
3095 listW->pszConnection = heap_alloc(wlen*sizeof(WCHAR));
3096 MultiByteToWideChar( CP_ACP, 0, listA->pszConnection, -1, listW->pszConnection, wlen );
3097 }
3098 else
3099 listW->pszConnection = NULL;
3100 listW->dwOptionCount = listA->dwOptionCount;
3101 listW->dwOptionError = listA->dwOptionError;
3102 listW->pOptions = heap_alloc(sizeof(INTERNET_PER_CONN_OPTIONW) * listA->dwOptionCount);
3103
3104 for (i = 0; i < listA->dwOptionCount; ++i) {
3105 INTERNET_PER_CONN_OPTIONA *optA = listA->pOptions + i;
3106 INTERNET_PER_CONN_OPTIONW *optW = listW->pOptions + i;
3107
3108 optW->dwOption = optA->dwOption;
3109
3110 switch (optA->dwOption) {
3111 case INTERNET_PER_CONN_AUTOCONFIG_URL:
3112 case INTERNET_PER_CONN_PROXY_BYPASS:
3113 case INTERNET_PER_CONN_PROXY_SERVER:
3114 case INTERNET_PER_CONN_AUTOCONFIG_SECONDARY_URL:
3115 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_URL:
3116 if (optA->Value.pszValue)
3117 {
3118 wlen = MultiByteToWideChar( CP_ACP, 0, optA->Value.pszValue, -1, NULL, 0 );
3119 optW->Value.pszValue = heap_alloc(wlen*sizeof(WCHAR));
3120 MultiByteToWideChar( CP_ACP, 0, optA->Value.pszValue, -1, optW->Value.pszValue, wlen );
3121 }
3122 else
3123 optW->Value.pszValue = NULL;
3124 break;
3125 case INTERNET_PER_CONN_AUTODISCOVERY_FLAGS:
3126 case INTERNET_PER_CONN_FLAGS:
3127 case INTERNET_PER_CONN_AUTOCONFIG_RELOAD_DELAY_MINS:
3128 optW->Value.dwValue = optA->Value.dwValue;
3129 break;
3130 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_TIME:
3131 optW->Value.ftValue = optA->Value.ftValue;
3132 break;
3133 default:
3134 WARN("Unknown PER_CONN dwOption: %d, guessing at conversion to Wide\n", optA->dwOption);
3135 optW->Value.dwValue = optA->Value.dwValue;
3136 break;
3137 }
3138 }
3139 }
3140 break;
3141 default:
3142 wbuffer = lpBuffer;
3143 wlen = dwBufferLength;
3144 }
3145
3146 r = InternetSetOptionW(hInternet,dwOption, wbuffer, wlen);
3147
3148 if( lpBuffer != wbuffer )
3149 {
3150 if (dwOption == INTERNET_OPTION_PER_CONNECTION_OPTION)
3151 {
3152 INTERNET_PER_CONN_OPTION_LISTW *list = wbuffer;
3153 unsigned int i;
3154 for (i = 0; i < list->dwOptionCount; ++i) {
3155 INTERNET_PER_CONN_OPTIONW *opt = list->pOptions + i;
3156 switch (opt->dwOption) {
3157 case INTERNET_PER_CONN_AUTOCONFIG_URL:
3158 case INTERNET_PER_CONN_PROXY_BYPASS:
3159 case INTERNET_PER_CONN_PROXY_S