f60d29aa458f85da051c0a9da3956aa273537109
[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 )))
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(lpwai->proxy), debugstr_w(lpwai->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 if (TRACE_ON(wininet)) {
1000 #define FE(x) { x, #x }
1001 static const wininet_flag_info access_type[] = {
1002 FE(INTERNET_OPEN_TYPE_PRECONFIG),
1003 FE(INTERNET_OPEN_TYPE_DIRECT),
1004 FE(INTERNET_OPEN_TYPE_PROXY),
1005 FE(INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY)
1006 };
1007 #undef FE
1008 DWORD i;
1009 const char *access_type_str = "Unknown";
1010
1011 TRACE("(%s, %i, %s, %s, %i)\n", debugstr_w(lpszAgent), dwAccessType,
1012 debugstr_w(lpszProxy), debugstr_w(lpszProxyBypass), dwFlags);
1013 for (i = 0; i < (sizeof(access_type) / sizeof(access_type[0])); i++) {
1014 if (access_type[i].val == dwAccessType) {
1015 access_type_str = access_type[i].name;
1016 break;
1017 }
1018 }
1019 TRACE(" access type : %s\n", access_type_str);
1020 TRACE(" flags :");
1021 dump_INTERNET_FLAGS(dwFlags);
1022 }
1023
1024 /* Clear any error information */
1025 INTERNET_SetLastError(0);
1026
1027 if((dwAccessType == INTERNET_OPEN_TYPE_PROXY) && !lpszProxy) {
1028 SetLastError(ERROR_INVALID_PARAMETER);
1029 return NULL;
1030 }
1031
1032 lpwai = alloc_object(NULL, &APPINFOVtbl, sizeof(appinfo_t));
1033 if (!lpwai) {
1034 SetLastError(ERROR_OUTOFMEMORY);
1035 return NULL;
1036 }
1037
1038 lpwai->hdr.htype = WH_HINIT;
1039 lpwai->hdr.dwFlags = dwFlags;
1040 lpwai->accessType = dwAccessType;
1041 lpwai->proxyUsername = NULL;
1042 lpwai->proxyPassword = NULL;
1043 lpwai->connect_timeout = connect_timeout;
1044
1045 lpwai->agent = heap_strdupW(lpszAgent);
1046 if(dwAccessType == INTERNET_OPEN_TYPE_PRECONFIG)
1047 INTERNET_ConfigureProxy( lpwai );
1048 else if(dwAccessType == INTERNET_OPEN_TYPE_PROXY) {
1049 lpwai->proxy = heap_strdupW(lpszProxy);
1050 lpwai->proxyBypass = heap_strdupW(lpszProxyBypass);
1051 }
1052
1053 TRACE("returning %p\n", lpwai);
1054
1055 return lpwai->hdr.hInternet;
1056 }
1057
1058
1059 /***********************************************************************
1060 * InternetOpenA (WININET.@)
1061 *
1062 * Per-application initialization of wininet
1063 *
1064 * RETURNS
1065 * HINTERNET on success
1066 * NULL on failure
1067 *
1068 */
1069 HINTERNET WINAPI InternetOpenA(LPCSTR lpszAgent, DWORD dwAccessType,
1070 LPCSTR lpszProxy, LPCSTR lpszProxyBypass, DWORD dwFlags)
1071 {
1072 WCHAR *szAgent, *szProxy, *szBypass;
1073 HINTERNET rc;
1074
1075 TRACE("(%s, 0x%08x, %s, %s, 0x%08x)\n", debugstr_a(lpszAgent),
1076 dwAccessType, debugstr_a(lpszProxy), debugstr_a(lpszProxyBypass), dwFlags);
1077
1078 szAgent = heap_strdupAtoW(lpszAgent);
1079 szProxy = heap_strdupAtoW(lpszProxy);
1080 szBypass = heap_strdupAtoW(lpszProxyBypass);
1081
1082 rc = InternetOpenW(szAgent, dwAccessType, szProxy, szBypass, dwFlags);
1083
1084 heap_free(szAgent);
1085 heap_free(szProxy);
1086 heap_free(szBypass);
1087 return rc;
1088 }
1089
1090 /***********************************************************************
1091 * InternetGetLastResponseInfoA (WININET.@)
1092 *
1093 * Return last wininet error description on the calling thread
1094 *
1095 * RETURNS
1096 * TRUE on success of writing to buffer
1097 * FALSE on failure
1098 *
1099 */
1100 BOOL WINAPI InternetGetLastResponseInfoA(LPDWORD lpdwError,
1101 LPSTR lpszBuffer, LPDWORD lpdwBufferLength)
1102 {
1103 LPWITHREADERROR lpwite = TlsGetValue(g_dwTlsErrIndex);
1104
1105 TRACE("\n");
1106
1107 if (lpwite)
1108 {
1109 *lpdwError = lpwite->dwError;
1110 if (lpwite->dwError)
1111 {
1112 memcpy(lpszBuffer, lpwite->response, *lpdwBufferLength);
1113 *lpdwBufferLength = strlen(lpszBuffer);
1114 }
1115 else
1116 *lpdwBufferLength = 0;
1117 }
1118 else
1119 {
1120 *lpdwError = 0;
1121 *lpdwBufferLength = 0;
1122 }
1123
1124 return TRUE;
1125 }
1126
1127 /***********************************************************************
1128 * InternetGetLastResponseInfoW (WININET.@)
1129 *
1130 * Return last wininet error description on the calling thread
1131 *
1132 * RETURNS
1133 * TRUE on success of writing to buffer
1134 * FALSE on failure
1135 *
1136 */
1137 BOOL WINAPI InternetGetLastResponseInfoW(LPDWORD lpdwError,
1138 LPWSTR lpszBuffer, LPDWORD lpdwBufferLength)
1139 {
1140 LPWITHREADERROR lpwite = TlsGetValue(g_dwTlsErrIndex);
1141
1142 TRACE("\n");
1143
1144 if (lpwite)
1145 {
1146 *lpdwError = lpwite->dwError;
1147 if (lpwite->dwError)
1148 {
1149 memcpy(lpszBuffer, lpwite->response, *lpdwBufferLength);
1150 *lpdwBufferLength = lstrlenW(lpszBuffer);
1151 }
1152 else
1153 *lpdwBufferLength = 0;
1154 }
1155 else
1156 {
1157 *lpdwError = 0;
1158 *lpdwBufferLength = 0;
1159 }
1160
1161 return TRUE;
1162 }
1163
1164 /***********************************************************************
1165 * InternetGetConnectedState (WININET.@)
1166 *
1167 * Return connected state
1168 *
1169 * RETURNS
1170 * TRUE if connected
1171 * if lpdwStatus is not null, return the status (off line,
1172 * modem, lan...) in it.
1173 * FALSE if not connected
1174 */
1175 BOOL WINAPI InternetGetConnectedState(LPDWORD lpdwStatus, DWORD dwReserved)
1176 {
1177 TRACE("(%p, 0x%08x)\n", lpdwStatus, dwReserved);
1178
1179 if (lpdwStatus) {
1180 WARN("always returning LAN connection.\n");
1181 *lpdwStatus = INTERNET_CONNECTION_LAN;
1182 }
1183 return TRUE;
1184 }
1185
1186
1187 /***********************************************************************
1188 * InternetGetConnectedStateExW (WININET.@)
1189 *
1190 * Return connected state
1191 *
1192 * PARAMS
1193 *
1194 * lpdwStatus [O] Flags specifying the status of the internet connection.
1195 * lpszConnectionName [O] Pointer to buffer to receive the friendly name of the internet connection.
1196 * dwNameLen [I] Size of the buffer, in characters.
1197 * dwReserved [I] Reserved. Must be set to 0.
1198 *
1199 * RETURNS
1200 * TRUE if connected
1201 * if lpdwStatus is not null, return the status (off line,
1202 * modem, lan...) in it.
1203 * FALSE if not connected
1204 *
1205 * NOTES
1206 * If the system has no available network connections, an empty string is
1207 * stored in lpszConnectionName. If there is a LAN connection, a localized
1208 * "LAN Connection" string is stored. Presumably, if only a dial-up
1209 * connection is available then the name of the dial-up connection is
1210 * returned. Why any application, other than the "Internet Settings" CPL,
1211 * would want to use this function instead of the simpler InternetGetConnectedStateW
1212 * function is beyond me.
1213 */
1214 BOOL WINAPI InternetGetConnectedStateExW(LPDWORD lpdwStatus, LPWSTR lpszConnectionName,
1215 DWORD dwNameLen, DWORD dwReserved)
1216 {
1217 TRACE("(%p, %p, %d, 0x%08x)\n", lpdwStatus, lpszConnectionName, dwNameLen, dwReserved);
1218
1219 /* Must be zero */
1220 if(dwReserved)
1221 return FALSE;
1222
1223 if (lpdwStatus) {
1224 WARN("always returning LAN connection.\n");
1225 *lpdwStatus = INTERNET_CONNECTION_LAN;
1226 }
1227
1228 /* When the buffer size is zero LoadStringW fills the buffer with a pointer to
1229 * the resource, avoid it as we must not change the buffer in this case */
1230 if(lpszConnectionName && dwNameLen) {
1231 *lpszConnectionName = '\0';
1232 LoadStringW(WININET_hModule, IDS_LANCONNECTION, lpszConnectionName, dwNameLen);
1233 }
1234
1235 return TRUE;
1236 }
1237
1238
1239 /***********************************************************************
1240 * InternetGetConnectedStateExA (WININET.@)
1241 */
1242 BOOL WINAPI InternetGetConnectedStateExA(LPDWORD lpdwStatus, LPSTR lpszConnectionName,
1243 DWORD dwNameLen, DWORD dwReserved)
1244 {
1245 LPWSTR lpwszConnectionName = NULL;
1246 BOOL rc;
1247
1248 TRACE("(%p, %p, %d, 0x%08x)\n", lpdwStatus, lpszConnectionName, dwNameLen, dwReserved);
1249
1250 if (lpszConnectionName && dwNameLen > 0)
1251 lpwszConnectionName = heap_alloc(dwNameLen * sizeof(WCHAR));
1252
1253 rc = InternetGetConnectedStateExW(lpdwStatus,lpwszConnectionName, dwNameLen,
1254 dwReserved);
1255 if (rc && lpwszConnectionName)
1256 WideCharToMultiByte(CP_ACP,0,lpwszConnectionName,-1,lpszConnectionName,
1257 dwNameLen, NULL, NULL);
1258
1259 heap_free(lpwszConnectionName);
1260 return rc;
1261 }
1262
1263
1264 /***********************************************************************
1265 * InternetConnectW (WININET.@)
1266 *
1267 * Open a ftp, gopher or http session
1268 *
1269 * RETURNS
1270 * HINTERNET a session handle on success
1271 * NULL on failure
1272 *
1273 */
1274 HINTERNET WINAPI InternetConnectW(HINTERNET hInternet,
1275 LPCWSTR lpszServerName, INTERNET_PORT nServerPort,
1276 LPCWSTR lpszUserName, LPCWSTR lpszPassword,
1277 DWORD dwService, DWORD dwFlags, DWORD_PTR dwContext)
1278 {
1279 appinfo_t *hIC;
1280 HINTERNET rc = NULL;
1281 DWORD res = ERROR_SUCCESS;
1282
1283 TRACE("(%p, %s, %i, %s, %s, %i, %x, %lx)\n", hInternet, debugstr_w(lpszServerName),
1284 nServerPort, debugstr_w(lpszUserName), debugstr_w(lpszPassword),
1285 dwService, dwFlags, dwContext);
1286
1287 if (!lpszServerName)
1288 {
1289 SetLastError(ERROR_INVALID_PARAMETER);
1290 return NULL;
1291 }
1292
1293 hIC = (appinfo_t*)get_handle_object( hInternet );
1294 if ( (hIC == NULL) || (hIC->hdr.htype != WH_HINIT) )
1295 {
1296 res = ERROR_INVALID_HANDLE;
1297 goto lend;
1298 }
1299
1300 switch (dwService)
1301 {
1302 case INTERNET_SERVICE_FTP:
1303 rc = FTP_Connect(hIC, lpszServerName, nServerPort,
1304 lpszUserName, lpszPassword, dwFlags, dwContext, 0);
1305 if(!rc)
1306 res = INTERNET_GetLastError();
1307 break;
1308
1309 case INTERNET_SERVICE_HTTP:
1310 res = HTTP_Connect(hIC, lpszServerName, nServerPort,
1311 lpszUserName, lpszPassword, dwFlags, dwContext, 0, &rc);
1312 break;
1313
1314 case INTERNET_SERVICE_GOPHER:
1315 default:
1316 break;
1317 }
1318 lend:
1319 if( hIC )
1320 WININET_Release( &hIC->hdr );
1321
1322 TRACE("returning %p\n", rc);
1323 SetLastError(res);
1324 return rc;
1325 }
1326
1327
1328 /***********************************************************************
1329 * InternetConnectA (WININET.@)
1330 *
1331 * Open a ftp, gopher or http session
1332 *
1333 * RETURNS
1334 * HINTERNET a session handle on success
1335 * NULL on failure
1336 *
1337 */
1338 HINTERNET WINAPI InternetConnectA(HINTERNET hInternet,
1339 LPCSTR lpszServerName, INTERNET_PORT nServerPort,
1340 LPCSTR lpszUserName, LPCSTR lpszPassword,
1341 DWORD dwService, DWORD dwFlags, DWORD_PTR dwContext)
1342 {
1343 HINTERNET rc = NULL;
1344 LPWSTR szServerName;
1345 LPWSTR szUserName;
1346 LPWSTR szPassword;
1347
1348 szServerName = heap_strdupAtoW(lpszServerName);
1349 szUserName = heap_strdupAtoW(lpszUserName);
1350 szPassword = heap_strdupAtoW(lpszPassword);
1351
1352 rc = InternetConnectW(hInternet, szServerName, nServerPort,
1353 szUserName, szPassword, dwService, dwFlags, dwContext);
1354
1355 heap_free(szServerName);
1356 heap_free(szUserName);
1357 heap_free(szPassword);
1358 return rc;
1359 }
1360
1361
1362 /***********************************************************************
1363 * InternetFindNextFileA (WININET.@)
1364 *
1365 * Continues a file search from a previous call to FindFirstFile
1366 *
1367 * RETURNS
1368 * TRUE on success
1369 * FALSE on failure
1370 *
1371 */
1372 BOOL WINAPI InternetFindNextFileA(HINTERNET hFind, LPVOID lpvFindData)
1373 {
1374 BOOL ret;
1375 WIN32_FIND_DATAW fd;
1376
1377 ret = InternetFindNextFileW(hFind, lpvFindData?&fd:NULL);
1378 if(lpvFindData)
1379 WININET_find_data_WtoA(&fd, (LPWIN32_FIND_DATAA)lpvFindData);
1380 return ret;
1381 }
1382
1383 /***********************************************************************
1384 * InternetFindNextFileW (WININET.@)
1385 *
1386 * Continues a file search from a previous call to FindFirstFile
1387 *
1388 * RETURNS
1389 * TRUE on success
1390 * FALSE on failure
1391 *
1392 */
1393 BOOL WINAPI InternetFindNextFileW(HINTERNET hFind, LPVOID lpvFindData)
1394 {
1395 object_header_t *hdr;
1396 DWORD res;
1397
1398 TRACE("\n");
1399
1400 hdr = get_handle_object(hFind);
1401 if(!hdr) {
1402 WARN("Invalid handle\n");
1403 SetLastError(ERROR_INVALID_HANDLE);
1404 return FALSE;
1405 }
1406
1407 if(hdr->vtbl->FindNextFileW) {
1408 res = hdr->vtbl->FindNextFileW(hdr, lpvFindData);
1409 }else {
1410 WARN("Handle doesn't support NextFile\n");
1411 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
1412 }
1413
1414 WININET_Release(hdr);
1415
1416 if(res != ERROR_SUCCESS)
1417 SetLastError(res);
1418 return res == ERROR_SUCCESS;
1419 }
1420
1421 /***********************************************************************
1422 * InternetCloseHandle (WININET.@)
1423 *
1424 * Generic close handle function
1425 *
1426 * RETURNS
1427 * TRUE on success
1428 * FALSE on failure
1429 *
1430 */
1431 BOOL WINAPI InternetCloseHandle(HINTERNET hInternet)
1432 {
1433 object_header_t *obj;
1434
1435 TRACE("%p\n", hInternet);
1436
1437 obj = get_handle_object( hInternet );
1438 if (!obj) {
1439 SetLastError(ERROR_INVALID_HANDLE);
1440 return FALSE;
1441 }
1442
1443 invalidate_handle(obj);
1444 WININET_Release(obj);
1445
1446 return TRUE;
1447 }
1448
1449
1450 /***********************************************************************
1451 * ConvertUrlComponentValue (Internal)
1452 *
1453 * Helper function for InternetCrackUrlA
1454 *
1455 */
1456 static void ConvertUrlComponentValue(LPSTR* lppszComponent, LPDWORD dwComponentLen,
1457 LPWSTR lpwszComponent, DWORD dwwComponentLen,
1458 LPCSTR lpszStart, LPCWSTR lpwszStart)
1459 {
1460 TRACE("%p %d %p %d %p %p\n", *lppszComponent, *dwComponentLen, lpwszComponent, dwwComponentLen, lpszStart, lpwszStart);
1461 if (*dwComponentLen != 0)
1462 {
1463 DWORD nASCIILength=WideCharToMultiByte(CP_ACP,0,lpwszComponent,dwwComponentLen,NULL,0,NULL,NULL);
1464 if (*lppszComponent == NULL)
1465 {
1466 if (lpwszComponent)
1467 {
1468 int offset = WideCharToMultiByte(CP_ACP, 0, lpwszStart, lpwszComponent-lpwszStart, NULL, 0, NULL, NULL);
1469 *lppszComponent = (LPSTR)lpszStart + offset;
1470 }
1471 else
1472 *lppszComponent = NULL;
1473
1474 *dwComponentLen = nASCIILength;
1475 }
1476 else
1477 {
1478 DWORD ncpylen = min((*dwComponentLen)-1, nASCIILength);
1479 WideCharToMultiByte(CP_ACP,0,lpwszComponent,dwwComponentLen,*lppszComponent,ncpylen+1,NULL,NULL);
1480 (*lppszComponent)[ncpylen]=0;
1481 *dwComponentLen = ncpylen;
1482 }
1483 }
1484 }
1485
1486
1487 /***********************************************************************
1488 * InternetCrackUrlA (WININET.@)
1489 *
1490 * See InternetCrackUrlW.
1491 */
1492 BOOL WINAPI InternetCrackUrlA(LPCSTR lpszUrl, DWORD dwUrlLength, DWORD dwFlags,
1493 LPURL_COMPONENTSA lpUrlComponents)
1494 {
1495 DWORD nLength;
1496 URL_COMPONENTSW UCW;
1497 BOOL ret = FALSE;
1498 WCHAR *lpwszUrl, *hostname = NULL, *username = NULL, *password = NULL, *path = NULL,
1499 *scheme = NULL, *extra = NULL;
1500
1501 TRACE("(%s %u %x %p)\n",
1502 lpszUrl ? debugstr_an(lpszUrl, dwUrlLength ? dwUrlLength : strlen(lpszUrl)) : "(null)",
1503 dwUrlLength, dwFlags, lpUrlComponents);
1504
1505 if (!lpszUrl || !*lpszUrl || !lpUrlComponents ||
1506 lpUrlComponents->dwStructSize != sizeof(URL_COMPONENTSA))
1507 {
1508 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1509 return FALSE;
1510 }
1511
1512 if(dwUrlLength<=0)
1513 dwUrlLength=-1;
1514 nLength=MultiByteToWideChar(CP_ACP,0,lpszUrl,dwUrlLength,NULL,0);
1515
1516 /* if dwUrlLength=-1 then nLength includes null but length to
1517 InternetCrackUrlW should not include it */
1518 if (dwUrlLength == -1) nLength--;
1519
1520 lpwszUrl = heap_alloc((nLength + 1) * sizeof(WCHAR));
1521 MultiByteToWideChar(CP_ACP,0,lpszUrl,dwUrlLength,lpwszUrl,nLength + 1);
1522 lpwszUrl[nLength] = '\0';
1523
1524 memset(&UCW,0,sizeof(UCW));
1525 UCW.dwStructSize = sizeof(URL_COMPONENTSW);
1526 if (lpUrlComponents->dwHostNameLength)
1527 {
1528 UCW.dwHostNameLength = lpUrlComponents->dwHostNameLength;
1529 if (lpUrlComponents->lpszHostName)
1530 {
1531 hostname = heap_alloc(UCW.dwHostNameLength * sizeof(WCHAR));
1532 UCW.lpszHostName = hostname;
1533 }
1534 }
1535 if (lpUrlComponents->dwUserNameLength)
1536 {
1537 UCW.dwUserNameLength = lpUrlComponents->dwUserNameLength;
1538 if (lpUrlComponents->lpszUserName)
1539 {
1540 username = heap_alloc(UCW.dwUserNameLength * sizeof(WCHAR));
1541 UCW.lpszUserName = username;
1542 }
1543 }
1544 if (lpUrlComponents->dwPasswordLength)
1545 {
1546 UCW.dwPasswordLength = lpUrlComponents->dwPasswordLength;
1547 if (lpUrlComponents->lpszPassword)
1548 {
1549 password = heap_alloc(UCW.dwPasswordLength * sizeof(WCHAR));
1550 UCW.lpszPassword = password;
1551 }
1552 }
1553 if (lpUrlComponents->dwUrlPathLength)
1554 {
1555 UCW.dwUrlPathLength = lpUrlComponents->dwUrlPathLength;
1556 if (lpUrlComponents->lpszUrlPath)
1557 {
1558 path = heap_alloc(UCW.dwUrlPathLength * sizeof(WCHAR));
1559 UCW.lpszUrlPath = path;
1560 }
1561 }
1562 if (lpUrlComponents->dwSchemeLength)
1563 {
1564 UCW.dwSchemeLength = lpUrlComponents->dwSchemeLength;
1565 if (lpUrlComponents->lpszScheme)
1566 {
1567 scheme = heap_alloc(UCW.dwSchemeLength * sizeof(WCHAR));
1568 UCW.lpszScheme = scheme;
1569 }
1570 }
1571 if (lpUrlComponents->dwExtraInfoLength)
1572 {
1573 UCW.dwExtraInfoLength = lpUrlComponents->dwExtraInfoLength;
1574 if (lpUrlComponents->lpszExtraInfo)
1575 {
1576 extra = heap_alloc(UCW.dwExtraInfoLength * sizeof(WCHAR));
1577 UCW.lpszExtraInfo = extra;
1578 }
1579 }
1580 if ((ret = InternetCrackUrlW(lpwszUrl, nLength, dwFlags, &UCW)))
1581 {
1582 ConvertUrlComponentValue(&lpUrlComponents->lpszHostName, &lpUrlComponents->dwHostNameLength,
1583 UCW.lpszHostName, UCW.dwHostNameLength, lpszUrl, lpwszUrl);
1584 ConvertUrlComponentValue(&lpUrlComponents->lpszUserName, &lpUrlComponents->dwUserNameLength,
1585 UCW.lpszUserName, UCW.dwUserNameLength, lpszUrl, lpwszUrl);
1586 ConvertUrlComponentValue(&lpUrlComponents->lpszPassword, &lpUrlComponents->dwPasswordLength,
1587 UCW.lpszPassword, UCW.dwPasswordLength, lpszUrl, lpwszUrl);
1588 ConvertUrlComponentValue(&lpUrlComponents->lpszUrlPath, &lpUrlComponents->dwUrlPathLength,
1589 UCW.lpszUrlPath, UCW.dwUrlPathLength, lpszUrl, lpwszUrl);
1590 ConvertUrlComponentValue(&lpUrlComponents->lpszScheme, &lpUrlComponents->dwSchemeLength,
1591 UCW.lpszScheme, UCW.dwSchemeLength, lpszUrl, lpwszUrl);
1592 ConvertUrlComponentValue(&lpUrlComponents->lpszExtraInfo, &lpUrlComponents->dwExtraInfoLength,
1593 UCW.lpszExtraInfo, UCW.dwExtraInfoLength, lpszUrl, lpwszUrl);
1594
1595 lpUrlComponents->nScheme = UCW.nScheme;
1596 lpUrlComponents->nPort = UCW.nPort;
1597
1598 TRACE("%s: scheme(%s) host(%s) path(%s) extra(%s)\n", debugstr_a(lpszUrl),
1599 debugstr_an(lpUrlComponents->lpszScheme, lpUrlComponents->dwSchemeLength),
1600 debugstr_an(lpUrlComponents->lpszHostName, lpUrlComponents->dwHostNameLength),
1601 debugstr_an(lpUrlComponents->lpszUrlPath, lpUrlComponents->dwUrlPathLength),
1602 debugstr_an(lpUrlComponents->lpszExtraInfo, lpUrlComponents->dwExtraInfoLength));
1603 }
1604 heap_free(lpwszUrl);
1605 heap_free(hostname);
1606 heap_free(username);
1607 heap_free(password);
1608 heap_free(path);
1609 heap_free(scheme);
1610 heap_free(extra);
1611 return ret;
1612 }
1613
1614 static const WCHAR url_schemes[][7] =
1615 {
1616 {'f','t','p',0},
1617 {'g','o','p','h','e','r',0},
1618 {'h','t','t','p',0},
1619 {'h','t','t','p','s',0},
1620 {'f','i','l','e',0},
1621 {'n','e','w','s',0},
1622 {'m','a','i','l','t','o',0},
1623 {'r','e','s',0},
1624 };
1625
1626 /***********************************************************************
1627 * GetInternetSchemeW (internal)
1628 *
1629 * Get scheme of url
1630 *
1631 * RETURNS
1632 * scheme on success
1633 * INTERNET_SCHEME_UNKNOWN on failure
1634 *
1635 */
1636 static INTERNET_SCHEME GetInternetSchemeW(LPCWSTR lpszScheme, DWORD nMaxCmp)
1637 {
1638 int i;
1639
1640 TRACE("%s %d\n",debugstr_wn(lpszScheme, nMaxCmp), nMaxCmp);
1641
1642 if(lpszScheme==NULL)
1643 return INTERNET_SCHEME_UNKNOWN;
1644
1645 for (i = 0; i < sizeof(url_schemes)/sizeof(url_schemes[0]); i++)
1646 if (!strncmpiW(lpszScheme, url_schemes[i], nMaxCmp))
1647 return INTERNET_SCHEME_FIRST + i;
1648
1649 return INTERNET_SCHEME_UNKNOWN;
1650 }
1651
1652 /***********************************************************************
1653 * SetUrlComponentValueW (Internal)
1654 *
1655 * Helper function for InternetCrackUrlW
1656 *
1657 * PARAMS
1658 * lppszComponent [O] Holds the returned string
1659 * dwComponentLen [I] Holds the size of lppszComponent
1660 * [O] Holds the length of the string in lppszComponent without '\0'
1661 * lpszStart [I] Holds the string to copy from
1662 * len [I] Holds the length of lpszStart without '\0'
1663 *
1664 * RETURNS
1665 * TRUE on success
1666 * FALSE on failure
1667 *
1668 */
1669 static BOOL SetUrlComponentValueW(LPWSTR* lppszComponent, LPDWORD dwComponentLen, LPCWSTR lpszStart, DWORD len)
1670 {
1671 TRACE("%s (%d)\n", debugstr_wn(lpszStart,len), len);
1672
1673 if ( (*dwComponentLen == 0) && (*lppszComponent == NULL) )
1674 return FALSE;
1675
1676 if (*dwComponentLen != 0 || *lppszComponent == NULL)
1677 {
1678 if (*lppszComponent == NULL)
1679 {
1680 *lppszComponent = (LPWSTR)lpszStart;
1681 *dwComponentLen = len;
1682 }
1683 else
1684 {
1685 DWORD ncpylen = min((*dwComponentLen)-1, len);
1686 memcpy(*lppszComponent, lpszStart, ncpylen*sizeof(WCHAR));
1687 (*lppszComponent)[ncpylen] = '\0';
1688 *dwComponentLen = ncpylen;
1689 }
1690 }
1691
1692 return TRUE;
1693 }
1694
1695 /***********************************************************************
1696 * InternetCrackUrlW (WININET.@)
1697 *
1698 * Break up URL into its components
1699 *
1700 * RETURNS
1701 * TRUE on success
1702 * FALSE on failure
1703 */
1704 BOOL WINAPI InternetCrackUrlW(LPCWSTR lpszUrl_orig, DWORD dwUrlLength_orig, DWORD dwFlags,
1705 LPURL_COMPONENTSW lpUC)
1706 {
1707 /*
1708 * RFC 1808
1709 * <protocol>:[//<net_loc>][/path][;<params>][?<query>][#<fragment>]
1710 *
1711 */
1712 LPCWSTR lpszParam = NULL;
1713 BOOL found_colon = FALSE;
1714 LPCWSTR lpszap, lpszUrl = lpszUrl_orig;
1715 LPCWSTR lpszcp = NULL, lpszNetLoc;
1716 LPWSTR lpszUrl_decode = NULL;
1717 DWORD dwUrlLength = dwUrlLength_orig;
1718
1719 TRACE("(%s %u %x %p)\n",
1720 lpszUrl ? debugstr_wn(lpszUrl, dwUrlLength ? dwUrlLength : strlenW(lpszUrl)) : "(null)",
1721 dwUrlLength, dwFlags, lpUC);
1722
1723 if (!lpszUrl_orig || !*lpszUrl_orig || !lpUC)
1724 {
1725 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1726 return FALSE;
1727 }
1728 if (!dwUrlLength) dwUrlLength = strlenW(lpszUrl);
1729
1730 if (dwFlags & ICU_DECODE)
1731 {
1732 WCHAR *url_tmp;
1733 DWORD len = dwUrlLength + 1;
1734
1735 if (!(url_tmp = heap_alloc(len * sizeof(WCHAR))))
1736 {
1737 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1738 return FALSE;
1739 }
1740 memcpy(url_tmp, lpszUrl_orig, dwUrlLength * sizeof(WCHAR));
1741 url_tmp[dwUrlLength] = 0;
1742 if (!(lpszUrl_decode = heap_alloc(len * sizeof(WCHAR))))
1743 {
1744 heap_free(url_tmp);
1745 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1746 return FALSE;
1747 }
1748 if (InternetCanonicalizeUrlW(url_tmp, lpszUrl_decode, &len, ICU_DECODE | ICU_NO_ENCODE))
1749 {
1750 dwUrlLength = len;
1751 lpszUrl = lpszUrl_decode;
1752 }
1753 heap_free(url_tmp);
1754 }
1755 lpszap = lpszUrl;
1756
1757 /* Determine if the URI is absolute. */
1758 while (lpszap - lpszUrl < dwUrlLength)
1759 {
1760 if (isalnumW(*lpszap) || *lpszap == '+' || *lpszap == '.' || *lpszap == '-')
1761 {
1762 lpszap++;
1763 continue;
1764 }
1765 if (*lpszap == ':')
1766 {
1767 found_colon = TRUE;
1768 lpszcp = lpszap;
1769 }
1770 else
1771 {
1772 lpszcp = lpszUrl; /* Relative url */
1773 }
1774
1775 break;
1776 }
1777
1778 if(!found_colon){
1779 SetLastError(ERROR_INTERNET_UNRECOGNIZED_SCHEME);
1780 return FALSE;
1781 }
1782
1783 lpUC->nScheme = INTERNET_SCHEME_UNKNOWN;
1784 lpUC->nPort = INTERNET_INVALID_PORT_NUMBER;
1785
1786 /* Parse <params> */
1787 lpszParam = memchrW(lpszap, '?', dwUrlLength - (lpszap - lpszUrl));
1788 if(!lpszParam)
1789 lpszParam = memchrW(lpszap, '#', dwUrlLength - (lpszap - lpszUrl));
1790
1791 SetUrlComponentValueW(&lpUC->lpszExtraInfo, &lpUC->dwExtraInfoLength,
1792 lpszParam, lpszParam ? dwUrlLength-(lpszParam-lpszUrl) : 0);
1793
1794
1795 /* Get scheme first. */
1796 lpUC->nScheme = GetInternetSchemeW(lpszUrl, lpszcp - lpszUrl);
1797 SetUrlComponentValueW(&lpUC->lpszScheme, &lpUC->dwSchemeLength,
1798 lpszUrl, lpszcp - lpszUrl);
1799
1800 /* Eat ':' in protocol. */
1801 lpszcp++;
1802
1803 /* double slash indicates the net_loc portion is present */
1804 if ((lpszcp[0] == '/') && (lpszcp[1] == '/'))
1805 {
1806 lpszcp += 2;
1807
1808 lpszNetLoc = memchrW(lpszcp, '/', dwUrlLength - (lpszcp - lpszUrl));
1809 if (lpszParam)
1810 {
1811 if (lpszNetLoc)
1812 lpszNetLoc = min(lpszNetLoc, lpszParam);
1813 else
1814 lpszNetLoc = lpszParam;
1815 }
1816 else if (!lpszNetLoc)
1817 lpszNetLoc = lpszcp + dwUrlLength-(lpszcp-lpszUrl);
1818
1819 /* Parse net-loc */
1820 if (lpszNetLoc)
1821 {
1822 LPCWSTR lpszHost;
1823 LPCWSTR lpszPort;
1824
1825 /* [<user>[<:password>]@]<host>[:<port>] */
1826 /* First find the user and password if they exist */
1827
1828 lpszHost = memchrW(lpszcp, '@', dwUrlLength - (lpszcp - lpszUrl));
1829 if (lpszHost == NULL || lpszHost > lpszNetLoc)
1830 {
1831 /* username and password not specified. */
1832 SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength, NULL, 0);
1833 SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength, NULL, 0);
1834 }
1835 else /* Parse out username and password */
1836 {
1837 LPCWSTR lpszUser = lpszcp;
1838 LPCWSTR lpszPasswd = lpszHost;
1839
1840 while (lpszcp < lpszHost)
1841 {
1842 if (*lpszcp == ':')
1843 lpszPasswd = lpszcp;
1844
1845 lpszcp++;
1846 }
1847
1848 SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength,
1849 lpszUser, lpszPasswd - lpszUser);
1850
1851 if (lpszPasswd != lpszHost)
1852 lpszPasswd++;
1853 SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength,
1854 lpszPasswd == lpszHost ? NULL : lpszPasswd,
1855 lpszHost - lpszPasswd);
1856
1857 lpszcp++; /* Advance to beginning of host */
1858 }
1859
1860 /* Parse <host><:port> */
1861
1862 lpszHost = lpszcp;
1863 lpszPort = lpszNetLoc;
1864
1865 /* special case for res:// URLs: there is no port here, so the host is the
1866 entire string up to the first '/' */
1867 if(lpUC->nScheme==INTERNET_SCHEME_RES)
1868 {
1869 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength,
1870 lpszHost, lpszPort - lpszHost);
1871 lpszcp=lpszNetLoc;
1872 }
1873 else
1874 {
1875 while (lpszcp < lpszNetLoc)
1876 {
1877 if (*lpszcp == ':')
1878 lpszPort = lpszcp;
1879
1880 lpszcp++;
1881 }
1882
1883 /* If the scheme is "file" and the host is just one letter, it's not a host */
1884 if(lpUC->nScheme==INTERNET_SCHEME_FILE && lpszPort <= lpszHost+1)
1885 {
1886 lpszcp=lpszHost;
1887 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength,
1888 NULL, 0);
1889 }
1890 else
1891 {
1892 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength,
1893 lpszHost, lpszPort - lpszHost);
1894 if (lpszPort != lpszNetLoc)
1895 lpUC->nPort = atoiW(++lpszPort);
1896 else switch (lpUC->nScheme)
1897 {
1898 case INTERNET_SCHEME_HTTP:
1899 lpUC->nPort = INTERNET_DEFAULT_HTTP_PORT;
1900 break;
1901 case INTERNET_SCHEME_HTTPS:
1902 lpUC->nPort = INTERNET_DEFAULT_HTTPS_PORT;
1903 break;
1904 case INTERNET_SCHEME_FTP:
1905 lpUC->nPort = INTERNET_DEFAULT_FTP_PORT;
1906 break;
1907 case INTERNET_SCHEME_GOPHER:
1908 lpUC->nPort = INTERNET_DEFAULT_GOPHER_PORT;
1909 break;
1910 default:
1911 break;
1912 }
1913 }
1914 }
1915 }
1916 }
1917 else
1918 {
1919 SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength, NULL, 0);
1920 SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength, NULL, 0);
1921 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength, NULL, 0);
1922 }
1923
1924 /* Here lpszcp points to:
1925 *
1926 * <protocol>:[//<net_loc>][/path][;<params>][?<query>][#<fragment>]
1927 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1928 */
1929 if (lpszcp != 0 && lpszcp - lpszUrl < dwUrlLength && (!lpszParam || lpszcp <= lpszParam))
1930 {
1931 DWORD len;
1932
1933 /* Only truncate the parameter list if it's already been saved
1934 * in lpUC->lpszExtraInfo.
1935 */
1936 if (lpszParam && lpUC->dwExtraInfoLength && lpUC->lpszExtraInfo)
1937 len = lpszParam - lpszcp;
1938 else
1939 {
1940 /* Leave the parameter list in lpszUrlPath. Strip off any trailing
1941 * newlines if necessary.
1942 */
1943 LPWSTR lpsznewline = memchrW(lpszcp, '\n', dwUrlLength - (lpszcp - lpszUrl));
1944 if (lpsznewline != NULL)
1945 len = lpsznewline - lpszcp;
1946 else
1947 len = dwUrlLength-(lpszcp-lpszUrl);
1948 }
1949 if (lpUC->dwUrlPathLength && lpUC->lpszUrlPath &&
1950 lpUC->nScheme == INTERNET_SCHEME_FILE)
1951 {
1952 WCHAR tmppath[MAX_PATH];
1953 if (*lpszcp == '/')
1954 {
1955 len = MAX_PATH;
1956 PathCreateFromUrlW(lpszUrl_orig, tmppath, &len, 0);
1957 }
1958 else
1959 {
1960 WCHAR *iter;
1961 memcpy(tmppath, lpszcp, len * sizeof(WCHAR));
1962 tmppath[len] = '\0';
1963
1964 iter = tmppath;
1965 while (*iter) {
1966 if (*iter == '/')
1967 *iter = '\\';
1968 ++iter;
1969 }
1970 }
1971 /* if ends in \. or \.. append a backslash */
1972 if (tmppath[len - 1] == '.' &&
1973 (tmppath[len - 2] == '\\' ||
1974 (tmppath[len - 2] == '.' && tmppath[len - 3] == '\\')))
1975 {
1976 if (len < MAX_PATH - 1)
1977 {
1978 tmppath[len] = '\\';
1979 tmppath[len+1] = '\0';
1980 ++len;
1981 }
1982 }
1983 SetUrlComponentValueW(&lpUC->lpszUrlPath, &lpUC->dwUrlPathLength,
1984 tmppath, len);
1985 }
1986 else
1987 SetUrlComponentValueW(&lpUC->lpszUrlPath, &lpUC->dwUrlPathLength,
1988 lpszcp, len);
1989 }
1990 else
1991 {
1992 if (lpUC->lpszUrlPath && (lpUC->dwUrlPathLength > 0))
1993 lpUC->lpszUrlPath[0] = 0;
1994 lpUC->dwUrlPathLength = 0;
1995 }
1996
1997 TRACE("%s: scheme(%s) host(%s) path(%s) extra(%s)\n", debugstr_wn(lpszUrl,dwUrlLength),
1998 debugstr_wn(lpUC->lpszScheme,lpUC->dwSchemeLength),
1999 debugstr_wn(lpUC->lpszHostName,lpUC->dwHostNameLength),
2000 debugstr_wn(lpUC->lpszUrlPath,lpUC->dwUrlPathLength),
2001 debugstr_wn(lpUC->lpszExtraInfo,lpUC->dwExtraInfoLength));
2002
2003 heap_free( lpszUrl_decode );
2004 return TRUE;
2005 }
2006
2007 /***********************************************************************
2008 * InternetAttemptConnect (WININET.@)
2009 *
2010 * Attempt to make a connection to the internet
2011 *
2012 * RETURNS
2013 * ERROR_SUCCESS on success
2014 * Error value on failure
2015 *
2016 */
2017 DWORD WINAPI InternetAttemptConnect(DWORD dwReserved)
2018 {
2019 FIXME("Stub\n");
2020 return ERROR_SUCCESS;
2021 }
2022
2023
2024 /***********************************************************************
2025 * convert_url_canonicalization_flags
2026 *
2027 * Helper for InternetCanonicalizeUrl
2028 *
2029 * PARAMS
2030 * dwFlags [I] Flags suitable for InternetCanonicalizeUrl
2031 *
2032 * RETURNS
2033 * Flags suitable for UrlCanonicalize
2034 */
2035 static DWORD convert_url_canonicalization_flags(DWORD dwFlags)
2036 {
2037 DWORD dwUrlFlags = URL_WININET_COMPATIBILITY | URL_ESCAPE_UNSAFE;
2038
2039 if (dwFlags & ICU_BROWSER_MODE) dwUrlFlags |= URL_BROWSER_MODE;
2040 if (dwFlags & ICU_DECODE) dwUrlFlags |= URL_UNESCAPE;
2041 if (dwFlags & ICU_ENCODE_PERCENT) dwUrlFlags |= URL_ESCAPE_PERCENT;
2042 if (dwFlags & ICU_ENCODE_SPACES_ONLY) dwUrlFlags |= URL_ESCAPE_SPACES_ONLY;
2043 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
2044 if (dwFlags & ICU_NO_ENCODE) dwUrlFlags ^= URL_ESCAPE_UNSAFE;
2045 if (dwFlags & ICU_NO_META) dwUrlFlags |= URL_NO_META;
2046
2047 return dwUrlFlags;
2048 }
2049
2050 /***********************************************************************
2051 * InternetCanonicalizeUrlA (WININET.@)
2052 *
2053 * Escape unsafe characters and spaces
2054 *
2055 * RETURNS
2056 * TRUE on success
2057 * FALSE on failure
2058 *
2059 */
2060 BOOL WINAPI InternetCanonicalizeUrlA(LPCSTR lpszUrl, LPSTR lpszBuffer,
2061 LPDWORD lpdwBufferLength, DWORD dwFlags)
2062 {
2063 HRESULT hr;
2064
2065 TRACE("(%s, %p, %p, 0x%08x) buffer length: %d\n", debugstr_a(lpszUrl), lpszBuffer,
2066 lpdwBufferLength, dwFlags, lpdwBufferLength ? *lpdwBufferLength : -1);
2067
2068 dwFlags = convert_url_canonicalization_flags(dwFlags);
2069 hr = UrlCanonicalizeA(lpszUrl, lpszBuffer, lpdwBufferLength, dwFlags);
2070 if (hr == E_POINTER) SetLastError(ERROR_INSUFFICIENT_BUFFER);
2071 if (hr == E_INVALIDARG) SetLastError(ERROR_INVALID_PARAMETER);
2072
2073 return hr == S_OK;
2074 }
2075
2076 /***********************************************************************
2077 * InternetCanonicalizeUrlW (WININET.@)
2078 *
2079 * Escape unsafe characters and spaces
2080 *
2081 * RETURNS
2082 * TRUE on success
2083 * FALSE on failure
2084 *
2085 */
2086 BOOL WINAPI InternetCanonicalizeUrlW(LPCWSTR lpszUrl, LPWSTR lpszBuffer,
2087 LPDWORD lpdwBufferLength, DWORD dwFlags)
2088 {
2089 HRESULT hr;
2090
2091 TRACE("(%s, %p, %p, 0x%08x) buffer length: %d\n", debugstr_w(lpszUrl), lpszBuffer,
2092 lpdwBufferLength, dwFlags, lpdwBufferLength ? *lpdwBufferLength : -1);
2093
2094 dwFlags = convert_url_canonicalization_flags(dwFlags);
2095 hr = UrlCanonicalizeW(lpszUrl, lpszBuffer, lpdwBufferLength, dwFlags);
2096 if (hr == E_POINTER) SetLastError(ERROR_INSUFFICIENT_BUFFER);
2097 if (hr == E_INVALIDARG) SetLastError(ERROR_INVALID_PARAMETER);
2098
2099 return hr == S_OK;
2100 }
2101
2102 /* #################################################### */
2103
2104 static INTERNET_STATUS_CALLBACK set_status_callback(
2105 object_header_t *lpwh, INTERNET_STATUS_CALLBACK callback, BOOL unicode)
2106 {
2107 INTERNET_STATUS_CALLBACK ret;
2108
2109 if (unicode) lpwh->dwInternalFlags |= INET_CALLBACKW;
2110 else lpwh->dwInternalFlags &= ~INET_CALLBACKW;
2111
2112 ret = lpwh->lpfnStatusCB;
2113 lpwh->lpfnStatusCB = callback;
2114
2115 return ret;
2116 }
2117
2118 /***********************************************************************
2119 * InternetSetStatusCallbackA (WININET.@)
2120 *
2121 * Sets up a callback function which is called as progress is made
2122 * during an operation.
2123 *
2124 * RETURNS
2125 * Previous callback or NULL on success
2126 * INTERNET_INVALID_STATUS_CALLBACK on failure
2127 *
2128 */
2129 INTERNET_STATUS_CALLBACK WINAPI InternetSetStatusCallbackA(
2130 HINTERNET hInternet ,INTERNET_STATUS_CALLBACK lpfnIntCB)
2131 {
2132 INTERNET_STATUS_CALLBACK retVal;
2133 object_header_t *lpwh;
2134
2135 TRACE("%p\n", hInternet);
2136
2137 if (!(lpwh = get_handle_object(hInternet)))
2138 return INTERNET_INVALID_STATUS_CALLBACK;
2139
2140 retVal = set_status_callback(lpwh, lpfnIntCB, FALSE);
2141
2142 WININET_Release( lpwh );
2143 return retVal;
2144 }
2145
2146 /***********************************************************************
2147 * InternetSetStatusCallbackW (WININET.@)
2148 *
2149 * Sets up a callback function which is called as progress is made
2150 * during an operation.
2151 *
2152 * RETURNS
2153 * Previous callback or NULL on success
2154 * INTERNET_INVALID_STATUS_CALLBACK on failure
2155 *
2156 */
2157 INTERNET_STATUS_CALLBACK WINAPI InternetSetStatusCallbackW(
2158 HINTERNET hInternet ,INTERNET_STATUS_CALLBACK lpfnIntCB)
2159 {
2160 INTERNET_STATUS_CALLBACK retVal;
2161 object_header_t *lpwh;
2162
2163 TRACE("%p\n", hInternet);
2164
2165 if (!(lpwh = get_handle_object(hInternet)))
2166 return INTERNET_INVALID_STATUS_CALLBACK;
2167
2168 retVal = set_status_callback(lpwh, lpfnIntCB, TRUE);
2169
2170 WININET_Release( lpwh );
2171 return retVal;
2172 }
2173
2174 /***********************************************************************
2175 * InternetSetFilePointer (WININET.@)
2176 */
2177 DWORD WINAPI InternetSetFilePointer(HINTERNET hFile, LONG lDistanceToMove,
2178 PVOID pReserved, DWORD dwMoveContext, DWORD_PTR dwContext)
2179 {
2180 FIXME("(%p %d %p %d %lx): stub\n", hFile, lDistanceToMove, pReserved, dwMoveContext, dwContext);
2181 return FALSE;
2182 }
2183
2184 /***********************************************************************
2185 * InternetWriteFile (WININET.@)
2186 *
2187 * Write data to an open internet file
2188 *
2189 * RETURNS
2190 * TRUE on success
2191 * FALSE on failure
2192 *
2193 */
2194 BOOL WINAPI InternetWriteFile(HINTERNET hFile, LPCVOID lpBuffer,
2195 DWORD dwNumOfBytesToWrite, LPDWORD lpdwNumOfBytesWritten)
2196 {
2197 object_header_t *lpwh;
2198 BOOL res;
2199
2200 TRACE("(%p %p %d %p)\n", hFile, lpBuffer, dwNumOfBytesToWrite, lpdwNumOfBytesWritten);
2201
2202 lpwh = get_handle_object( hFile );
2203 if (!lpwh) {
2204 WARN("Invalid handle\n");
2205 SetLastError(ERROR_INVALID_HANDLE);
2206 return FALSE;
2207 }
2208
2209 if(lpwh->vtbl->WriteFile) {
2210 res = lpwh->vtbl->WriteFile(lpwh, lpBuffer, dwNumOfBytesToWrite, lpdwNumOfBytesWritten);
2211 }else {
2212 WARN("No Writefile method.\n");
2213 res = ERROR_INVALID_HANDLE;
2214 }
2215
2216 WININET_Release( lpwh );
2217
2218 if(res != ERROR_SUCCESS)
2219 SetLastError(res);
2220 return res == ERROR_SUCCESS;
2221 }
2222
2223
2224 /***********************************************************************
2225 * InternetReadFile (WININET.@)
2226 *
2227 * Read data from an open internet file
2228 *
2229 * RETURNS
2230 * TRUE on success
2231 * FALSE on failure
2232 *
2233 */
2234 BOOL WINAPI InternetReadFile(HINTERNET hFile, LPVOID lpBuffer,
2235 DWORD dwNumOfBytesToRead, LPDWORD pdwNumOfBytesRead)
2236 {
2237 object_header_t *hdr;
2238 DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2239
2240 TRACE("%p %p %d %p\n", hFile, lpBuffer, dwNumOfBytesToRead, pdwNumOfBytesRead);
2241
2242 hdr = get_handle_object(hFile);
2243 if (!hdr) {
2244 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
2245 return FALSE;
2246 }
2247
2248 if(hdr->vtbl->ReadFile)
2249 res = hdr->vtbl->ReadFile(hdr, lpBuffer, dwNumOfBytesToRead, pdwNumOfBytesRead);
2250
2251 WININET_Release(hdr);
2252
2253 TRACE("-- %s (%u) (bytes read: %d)\n", res == ERROR_SUCCESS ? "TRUE": "FALSE", res,
2254 pdwNumOfBytesRead ? *pdwNumOfBytesRead : -1);
2255
2256 if(res != ERROR_SUCCESS)
2257 SetLastError(res);
2258 return res == ERROR_SUCCESS;
2259 }
2260
2261 /***********************************************************************
2262 * InternetReadFileExA (WININET.@)
2263 *
2264 * Read data from an open internet file
2265 *
2266 * PARAMS
2267 * hFile [I] Handle returned by InternetOpenUrl or HttpOpenRequest.
2268 * lpBuffersOut [I/O] Buffer.
2269 * dwFlags [I] Flags. See notes.
2270 * dwContext [I] Context for callbacks.
2271 *
2272 * RETURNS
2273 * TRUE on success
2274 * FALSE on failure
2275 *
2276 * NOTES
2277 * The parameter dwFlags include zero or more of the following flags:
2278 *|IRF_ASYNC - Makes the call asynchronous.
2279 *|IRF_SYNC - Makes the call synchronous.
2280 *|IRF_USE_CONTEXT - Forces dwContext to be used.
2281 *|IRF_NO_WAIT - Don't block if the data is not available, just return what is available.
2282 *
2283 * However, in testing IRF_USE_CONTEXT seems to have no effect - dwContext isn't used.
2284 *
2285 * SEE
2286 * InternetOpenUrlA(), HttpOpenRequestA()
2287 */
2288 BOOL WINAPI InternetReadFileExA(HINTERNET hFile, LPINTERNET_BUFFERSA lpBuffersOut,
2289 DWORD dwFlags, DWORD_PTR dwContext)
2290 {
2291 object_header_t *hdr;
2292 DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2293
2294 TRACE("(%p %p 0x%x 0x%lx)\n", hFile, lpBuffersOut, dwFlags, dwContext);
2295
2296 if (lpBuffersOut->dwStructSize != sizeof(*lpBuffersOut)) {
2297 SetLastError(ERROR_INVALID_PARAMETER);
2298 return FALSE;
2299 }
2300
2301 hdr = get_handle_object(hFile);
2302 if (!hdr) {
2303 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
2304 return FALSE;
2305 }
2306
2307 if(hdr->vtbl->ReadFileEx)
2308 res = hdr->vtbl->ReadFileEx(hdr, lpBuffersOut->lpvBuffer, lpBuffersOut->dwBufferLength,
2309 &lpBuffersOut->dwBufferLength, dwFlags, dwContext);
2310
2311 WININET_Release(hdr);
2312
2313 TRACE("-- %s (%u, bytes read: %d)\n", res == ERROR_SUCCESS ? "TRUE": "FALSE",
2314 res, lpBuffersOut->dwBufferLength);
2315
2316 if(res != ERROR_SUCCESS)
2317 SetLastError(res);
2318 return res == ERROR_SUCCESS;
2319 }
2320
2321 /***********************************************************************
2322 * InternetReadFileExW (WININET.@)
2323 * SEE
2324 * InternetReadFileExA()
2325 */
2326 BOOL WINAPI InternetReadFileExW(HINTERNET hFile, LPINTERNET_BUFFERSW lpBuffer,
2327 DWORD dwFlags, DWORD_PTR dwContext)
2328 {
2329 object_header_t *hdr;
2330 DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2331
2332 TRACE("(%p %p 0x%x 0x%lx)\n", hFile, lpBuffer, dwFlags, dwContext);
2333
2334 if (lpBuffer->dwStructSize != sizeof(*lpBuffer)) {
2335 SetLastError(ERROR_INVALID_PARAMETER);
2336 return FALSE;
2337 }
2338
2339 hdr = get_handle_object(hFile);
2340 if (!hdr) {
2341 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
2342 return FALSE;
2343 }
2344
2345 if(hdr->vtbl->ReadFileEx)
2346 res = hdr->vtbl->ReadFileEx(hdr, lpBuffer->lpvBuffer, lpBuffer->dwBufferLength, &lpBuffer->dwBufferLength,
2347 dwFlags, dwContext);
2348
2349 WININET_Release(hdr);
2350
2351 TRACE("-- %s (%u, bytes read: %d)\n", res == ERROR_SUCCESS ? "TRUE": "FALSE",
2352 res, lpBuffer->dwBufferLength);
2353
2354 if(res != ERROR_SUCCESS)
2355 SetLastError(res);
2356 return res == ERROR_SUCCESS;
2357 }
2358
2359 static BOOL get_proxy_autoconfig_url( char *buf, DWORD buflen )
2360 {
2361 #if defined(MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
2362
2363 CFDictionaryRef settings = CFNetworkCopySystemProxySettings();
2364 const void *ref;
2365 BOOL ret = FALSE;
2366
2367 if (!settings) return FALSE;
2368
2369 if (!(ref = CFDictionaryGetValue( settings, kCFNetworkProxiesProxyAutoConfigURLString )))
2370 {
2371 CFRelease( settings );
2372 return FALSE;
2373 }
2374 if (CFStringGetCString( ref, buf, buflen, kCFStringEncodingASCII ))
2375 {
2376 TRACE( "returning %s\n", debugstr_a(buf) );
2377 ret = TRUE;
2378 }
2379 CFRelease( settings );
2380 return ret;
2381 #else
2382 FIXME( "no support on this platform\n" );
2383 return FALSE;
2384 #endif
2385 }
2386
2387 static DWORD query_global_option(DWORD option, void *buffer, DWORD *size, BOOL unicode)
2388 {
2389 /* FIXME: This function currently handles more options than it should. Options requiring
2390 * proper handles should be moved to proper functions */
2391 switch(option) {
2392 case INTERNET_OPTION_HTTP_VERSION:
2393 if (*size < sizeof(HTTP_VERSION_INFO))
2394 return ERROR_INSUFFICIENT_BUFFER;
2395
2396 /*
2397 * Presently hardcoded to 1.1
2398 */
2399 ((HTTP_VERSION_INFO*)buffer)->dwMajorVersion = 1;
2400 ((HTTP_VERSION_INFO*)buffer)->dwMinorVersion = 1;
2401 *size = sizeof(HTTP_VERSION_INFO);
2402
2403 return ERROR_SUCCESS;
2404
2405 case INTERNET_OPTION_CONNECTED_STATE:
2406 FIXME("INTERNET_OPTION_CONNECTED_STATE: semi-stub\n");
2407
2408 if (*size < sizeof(ULONG))
2409 return ERROR_INSUFFICIENT_BUFFER;
2410
2411 *(ULONG*)buffer = INTERNET_STATE_CONNECTED;
2412 *size = sizeof(ULONG);
2413
2414 return ERROR_SUCCESS;
2415
2416 case INTERNET_OPTION_PROXY: {
2417 appinfo_t ai;
2418 BOOL ret;
2419
2420 TRACE("Getting global proxy info\n");
2421 memset(&ai, 0, sizeof(appinfo_t));
2422 INTERNET_ConfigureProxy(&ai);
2423
2424 ret = APPINFO_QueryOption(&ai.hdr, INTERNET_OPTION_PROXY, buffer, size, unicode); /* FIXME */
2425 APPINFO_Destroy(&ai.hdr);
2426 return ret;
2427 }
2428
2429 case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2430 TRACE("INTERNET_OPTION_MAX_CONNS_PER_SERVER\n");
2431
2432 if (*size < sizeof(ULONG))
2433 return ERROR_INSUFFICIENT_BUFFER;
2434
2435 *(ULONG*)buffer = max_conns;
2436 *size = sizeof(ULONG);
2437
2438 return ERROR_SUCCESS;
2439
2440 case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2441 TRACE("INTERNET_OPTION_MAX_CONNS_1_0_SERVER\n");
2442
2443 if (*size < sizeof(ULONG))
2444 return ERROR_INSUFFICIENT_BUFFER;
2445
2446 *(ULONG*)buffer = max_1_0_conns;
2447 *size = sizeof(ULONG);
2448
2449 return ERROR_SUCCESS;
2450
2451 case INTERNET_OPTION_SECURITY_FLAGS:
2452 FIXME("INTERNET_OPTION_SECURITY_FLAGS: Stub\n");
2453 return ERROR_SUCCESS;
2454
2455 case INTERNET_OPTION_VERSION: {
2456 static const INTERNET_VERSION_INFO info = { 1, 2 };
2457
2458 TRACE("INTERNET_OPTION_VERSION\n");
2459
2460 if (*size < sizeof(INTERNET_VERSION_INFO))
2461 return ERROR_INSUFFICIENT_BUFFER;
2462
2463 memcpy(buffer, &info, sizeof(info));
2464 *size = sizeof(info);
2465
2466 return ERROR_SUCCESS;
2467 }
2468
2469 case INTERNET_OPTION_PER_CONNECTION_OPTION: {
2470 char url[INTERNET_MAX_URL_LENGTH + 1];
2471 INTERNET_PER_CONN_OPTION_LISTW *con = buffer;
2472 INTERNET_PER_CONN_OPTION_LISTA *conA = buffer;
2473 DWORD res = ERROR_SUCCESS, i;
2474 proxyinfo_t pi;
2475 BOOL have_url;
2476 LONG ret;
2477
2478 TRACE("Getting global proxy info\n");
2479 if((ret = INTERNET_LoadProxySettings(&pi)))
2480 return ret;
2481
2482 have_url = get_proxy_autoconfig_url(url, sizeof(url));
2483
2484 FIXME("INTERNET_OPTION_PER_CONNECTION_OPTION stub\n");
2485
2486 if (*size < sizeof(INTERNET_PER_CONN_OPTION_LISTW)) {
2487 FreeProxyInfo(&pi);
2488 return ERROR_INSUFFICIENT_BUFFER;
2489 }
2490
2491 for (i = 0; i < con->dwOptionCount; i++) {
2492 INTERNET_PER_CONN_OPTIONW *optionW = con->pOptions + i;
2493 INTERNET_PER_CONN_OPTIONA *optionA = conA->pOptions + i;
2494
2495 switch (optionW->dwOption) {
2496 case INTERNET_PER_CONN_FLAGS:
2497 if(pi.proxyEnabled)
2498 optionW->Value.dwValue = PROXY_TYPE_PROXY;
2499 else
2500 optionW->Value.dwValue = PROXY_TYPE_DIRECT;
2501 if (have_url)
2502 /* native includes PROXY_TYPE_DIRECT even if PROXY_TYPE_PROXY is set */
2503 optionW->Value.dwValue |= PROXY_TYPE_DIRECT|PROXY_TYPE_AUTO_PROXY_URL;
2504 break;
2505
2506 case INTERNET_PER_CONN_PROXY_SERVER:
2507 if (unicode)
2508 optionW->Value.pszValue = heap_strdupW(pi.proxy);
2509 else
2510 optionA->Value.pszValue = heap_strdupWtoA(pi.proxy);
2511 break;
2512
2513 case INTERNET_PER_CONN_PROXY_BYPASS:
2514 if (unicode)
2515 optionW->Value.pszValue = heap_strdupW(pi.proxyBypass);
2516 else
2517 optionA->Value.pszValue = heap_strdupWtoA(pi.proxyBypass);
2518 break;
2519
2520 case INTERNET_PER_CONN_AUTOCONFIG_URL:
2521 if (!have_url)
2522 optionW->Value.pszValue = NULL;
2523 else if (unicode)
2524 optionW->Value.pszValue = heap_strdupAtoW(url);
2525 else
2526 optionA->Value.pszValue = heap_strdupA(url);
2527 break;
2528
2529 case INTERNET_PER_CONN_AUTODISCOVERY_FLAGS:
2530 optionW->Value.dwValue = AUTO_PROXY_FLAG_ALWAYS_DETECT;
2531 break;
2532
2533 case INTERNET_PER_CONN_AUTOCONFIG_SECONDARY_URL:
2534 case INTERNET_PER_CONN_AUTOCONFIG_RELOAD_DELAY_MINS:
2535 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_TIME:
2536 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_URL:
2537 FIXME("Unhandled dwOption %d\n", optionW->dwOption);
2538 memset(&optionW->Value, 0, sizeof(optionW->Value));
2539 break;
2540
2541 default:
2542 FIXME("Unknown dwOption %d\n", optionW->dwOption);
2543 res = ERROR_INVALID_PARAMETER;
2544 break;
2545 }
2546 }
2547 FreeProxyInfo(&pi);
2548
2549 return res;
2550 }
2551 case INTERNET_OPTION_REQUEST_FLAGS:
2552 case INTERNET_OPTION_USER_AGENT:
2553 *size = 0;
2554 return ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2555 case INTERNET_OPTION_POLICY:
2556 return ERROR_INVALID_PARAMETER;
2557 case INTERNET_OPTION_CONNECT_TIMEOUT:
2558 TRACE("INTERNET_OPTION_CONNECT_TIMEOUT\n");
2559
2560 if (*size < sizeof(ULONG))
2561 return ERROR_INSUFFICIENT_BUFFER;
2562
2563 *(ULONG*)buffer = connect_timeout;
2564 *size = sizeof(ULONG);
2565
2566 return ERROR_SUCCESS;
2567 }
2568
2569 FIXME("Stub for %d\n", option);
2570 return ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2571 }
2572
2573 DWORD INET_QueryOption(object_header_t *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
2574 {
2575 switch(option) {
2576 case INTERNET_OPTION_CONTEXT_VALUE:
2577 if (!size)
2578 return ERROR_INVALID_PARAMETER;
2579
2580 if (*size < sizeof(DWORD_PTR)) {
2581 *size = sizeof(DWORD_PTR);
2582 return ERROR_INSUFFICIENT_BUFFER;
2583 }
2584 if (!buffer)
2585 return ERROR_INVALID_PARAMETER;
2586
2587 *(DWORD_PTR *)buffer = hdr->dwContext;
2588 *size = sizeof(DWORD_PTR);
2589 return ERROR_SUCCESS;
2590
2591 case INTERNET_OPTION_REQUEST_FLAGS:
2592 WARN("INTERNET_OPTION_REQUEST_FLAGS\n");
2593 *size = sizeof(DWORD);
2594 return ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2595
2596 case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2597 case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2598 WARN("Called on global option %u\n", option);
2599 return ERROR_INTERNET_INVALID_OPERATION;
2600 }
2601
2602 /* FIXME: we shouldn't call it here */
2603 return query_global_option(option, buffer, size, unicode);
2604 }
2605
2606 /***********************************************************************
2607 * InternetQueryOptionW (WININET.@)
2608 *
2609 * Queries an options on the specified handle
2610 *
2611 * RETURNS
2612 * TRUE on success
2613 * FALSE on failure
2614 *
2615 */
2616 BOOL WINAPI InternetQueryOptionW(HINTERNET hInternet, DWORD dwOption,
2617 LPVOID lpBuffer, LPDWORD lpdwBufferLength)
2618 {
2619 object_header_t *hdr;
2620 DWORD res = ERROR_INVALID_HANDLE;
2621
2622 TRACE("%p %d %p %p\n", hInternet, dwOption, lpBuffer, lpdwBufferLength);
2623
2624 if(hInternet) {
2625 hdr = get_handle_object(hInternet);
2626 if (hdr) {
2627 res = hdr->vtbl->QueryOption(hdr, dwOption, lpBuffer, lpdwBufferLength, TRUE);
2628 WININET_Release(hdr);
2629 }
2630 }else {
2631 res = query_global_option(dwOption, lpBuffer, lpdwBufferLength, TRUE);
2632 }
2633
2634 if(res != ERROR_SUCCESS)
2635 SetLastError(res);
2636 return res == ERROR_SUCCESS;
2637 }
2638
2639 /***********************************************************************
2640 * InternetQueryOptionA (WININET.@)
2641 *
2642 * Queries an options on the specified handle
2643 *
2644 * RETURNS
2645 * TRUE on success
2646 * FALSE on failure
2647 *
2648 */
2649 BOOL WINAPI InternetQueryOptionA(HINTERNET hInternet, DWORD dwOption,
2650 LPVOID lpBuffer, LPDWORD lpdwBufferLength)
2651 {
2652 object_header_t *hdr;
2653 DWORD res = ERROR_INVALID_HANDLE;
2654
2655 TRACE("%p %d %p %p\n", hInternet, dwOption, lpBuffer, lpdwBufferLength);
2656
2657 if(hInternet) {
2658 hdr = get_handle_object(hInternet);
2659 if (hdr) {
2660 res = hdr->vtbl->QueryOption(hdr, dwOption, lpBuffer, lpdwBufferLength, FALSE);
2661 WININET_Release(hdr);
2662 }
2663 }else {
2664 res = query_global_option(dwOption, lpBuffer, lpdwBufferLength, FALSE);
2665 }
2666
2667 if(res != ERROR_SUCCESS)
2668 SetLastError(res);
2669 return res == ERROR_SUCCESS;
2670 }
2671
2672 DWORD INET_SetOption(object_header_t *hdr, DWORD option, void *buf, DWORD size)
2673 {
2674 switch(option) {
2675 case INTERNET_OPTION_CALLBACK:
2676 WARN("Not settable option %u\n", option);
2677 return ERROR_INTERNET_OPTION_NOT_SETTABLE;
2678 case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2679 case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2680 WARN("Called on global option %u\n", option);
2681 return ERROR_INTERNET_INVALID_OPERATION;
2682 }
2683
2684 return ERROR_INTERNET_INVALID_OPTION;
2685 }
2686
2687 static DWORD set_global_option(DWORD option, void *buf, DWORD size)
2688 {
2689 switch(option) {
2690 case INTERNET_OPTION_CALLBACK:
2691 WARN("Not global option %u\n", option);
2692 return ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2693
2694 case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2695 TRACE("INTERNET_OPTION_MAX_CONNS_PER_SERVER\n");
2696
2697 if(size != sizeof(max_conns))
2698 return ERROR_INTERNET_BAD_OPTION_LENGTH;
2699 if(!*(ULONG*)buf)
2700 return ERROR_BAD_ARGUMENTS;
2701
2702 max_conns = *(ULONG*)buf;
2703 return ERROR_SUCCESS;
2704
2705 case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2706 TRACE("INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER\n");
2707
2708 if(size != sizeof(max_1_0_conns))
2709 return ERROR_INTERNET_BAD_OPTION_LENGTH;
2710 if(!*(ULONG*)buf)
2711 return ERROR_BAD_ARGUMENTS;
2712
2713 max_1_0_conns = *(ULONG*)buf;
2714 return ERROR_SUCCESS;
2715
2716 case INTERNET_OPTION_CONNECT_TIMEOUT:
2717 TRACE("INTERNET_OPTION_CONNECT_TIMEOUT\n");
2718
2719 if(size != sizeof(connect_timeout))
2720 return ERROR_INTERNET_BAD_OPTION_LENGTH;
2721 if(!*(ULONG*)buf)
2722 return ERROR_BAD_ARGUMENTS;
2723
2724 connect_timeout = *(ULONG*)buf;
2725 return ERROR_SUCCESS;
2726
2727 case INTERNET_OPTION_SETTINGS_CHANGED:
2728 FIXME("INTERNETOPTION_SETTINGS_CHANGED semi-stub\n");
2729 collect_connections(COLLECT_CONNECTIONS);
2730 return ERROR_SUCCESS;
2731 }
2732
2733 return ERROR_INTERNET_INVALID_OPTION;
2734 }
2735
2736 /***********************************************************************
2737 * InternetSetOptionW (WININET.@)
2738 *
2739 * Sets an options on the specified handle
2740 *
2741 * RETURNS
2742 * TRUE on success
2743 * FALSE on failure
2744 *
2745 */
2746 BOOL WINAPI InternetSetOptionW(HINTERNET hInternet, DWORD dwOption,
2747 LPVOID lpBuffer, DWORD dwBufferLength)
2748 {
2749 object_header_t *lpwhh;
2750 BOOL ret = TRUE;
2751 DWORD res;
2752
2753 TRACE("(%p %d %p %d)\n", hInternet, dwOption, lpBuffer, dwBufferLength);
2754
2755 lpwhh = (object_header_t*) get_handle_object( hInternet );
2756 if(lpwhh)
2757 res = lpwhh->vtbl->SetOption(lpwhh, dwOption, lpBuffer, dwBufferLength);
2758 else
2759 res = set_global_option(dwOption, lpBuffer, dwBufferLength);
2760
2761 if(res != ERROR_INTERNET_INVALID_OPTION) {
2762 if(lpwhh)
2763 WININET_Release(lpwhh);
2764
2765 if(res != ERROR_SUCCESS)
2766 SetLastError(res);
2767
2768 return res == ERROR_SUCCESS;
2769 }
2770
2771 switch (dwOption)
2772 {
2773 case INTERNET_OPTION_HTTP_VERSION:
2774 {
2775 HTTP_VERSION_INFO* pVersion=(HTTP_VERSION_INFO*)lpBuffer;
2776 FIXME("Option INTERNET_OPTION_HTTP_VERSION(%d,%d): STUB\n",pVersion->dwMajorVersion,pVersion->dwMinorVersion);
2777 }
2778 break;
2779 case INTERNET_OPTION_ERROR_MASK:
2780 {
2781 if(!lpwhh) {
2782 SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2783 return FALSE;
2784 } else if(*(ULONG*)lpBuffer & (~(INTERNET_ERROR_MASK_INSERT_CDROM|
2785 INTERNET_ERROR_MASK_COMBINED_SEC_CERT|
2786 INTERNET_ERROR_MASK_LOGIN_FAILURE_DISPLAY_ENTITY_BODY))) {
2787 SetLastError(ERROR_INVALID_PARAMETER);
2788 ret = FALSE;
2789 } else if(dwBufferLength != sizeof(ULONG)) {
2790 SetLastError(ERROR_INTERNET_BAD_OPTION_LENGTH);
2791 ret = FALSE;
2792 } else
2793 TRACE("INTERNET_OPTION_ERROR_MASK: %x\n", *(ULONG*)lpBuffer);
2794 lpwhh->ErrorMask = *(ULONG*)lpBuffer;
2795 }
2796 break;
2797 case INTERNET_OPTION_PROXY:
2798 {
2799 INTERNET_PROXY_INFOW *info = lpBuffer;
2800
2801 if (!lpBuffer || dwBufferLength < sizeof(INTERNET_PROXY_INFOW))
2802 {
2803 SetLastError(ERROR_INVALID_PARAMETER);
2804 return FALSE;
2805 }
2806 if (!hInternet)
2807 {
2808 EnterCriticalSection( &WININET_cs );
2809 free_global_proxy();
2810 global_proxy = heap_alloc( sizeof(proxyinfo_t) );
2811 if (global_proxy)
2812 {
2813 if (info->dwAccessType == INTERNET_OPEN_TYPE_PROXY)
2814 {
2815 global_proxy->proxyEnabled = 1;
2816 global_proxy->proxy = heap_strdupW( info->lpszProxy );
2817 global_proxy->proxyBypass = heap_strdupW( info->lpszProxyBypass );
2818 }
2819 else
2820 {
2821 global_proxy->proxyEnabled = 0;
2822 global_proxy->proxy = global_proxy->proxyBypass = NULL;
2823 }
2824 }
2825 LeaveCriticalSection( &WININET_cs );
2826 }
2827 else
2828 {
2829 /* In general, each type of object should handle
2830 * INTERNET_OPTION_PROXY directly. This FIXME ensures it doesn't
2831 * get silently dropped.
2832 */
2833 FIXME("INTERNET_OPTION_PROXY unimplemented\n");
2834 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2835 ret = FALSE;
2836 }
2837 break;
2838 }
2839 case INTERNET_OPTION_CODEPAGE:
2840 {
2841 ULONG codepage = *(ULONG *)lpBuffer;
2842 FIXME("Option INTERNET_OPTION_CODEPAGE (%d): STUB\n", codepage);
2843 }
2844 break;
2845 case INTERNET_OPTION_REQUEST_PRIORITY:
2846 {
2847 ULONG priority = *(ULONG *)lpBuffer;
2848 FIXME("Option INTERNET_OPTION_REQUEST_PRIORITY (%d): STUB\n", priority);
2849 }
2850 break;
2851 case INTERNET_OPTION_CONNECT_TIMEOUT:
2852 {
2853 ULONG connecttimeout = *(ULONG *)lpBuffer;
2854 FIXME("Option INTERNET_OPTION_CONNECT_TIMEOUT (%d): STUB\n", connecttimeout);
2855 }
2856 break;
2857 case INTERNET_OPTION_DATA_RECEIVE_TIMEOUT:
2858 {
2859 ULONG receivetimeout = *(ULONG *)lpBuffer;
2860 FIXME("Option INTERNET_OPTION_DATA_RECEIVE_TIMEOUT (%d): STUB\n", receivetimeout);
2861 }
2862 break;
2863 case INTERNET_OPTION_RESET_URLCACHE_SESSION:
2864 FIXME("Option INTERNET_OPTION_RESET_URLCACHE_SESSION: STUB\n");
2865 break;
2866 case INTERNET_OPTION_END_BROWSER_SESSION:
2867 FIXME("Option INTERNET_OPTION_END_BROWSER_SESSION: STUB\n");
2868 break;
2869 case INTERNET_OPTION_CONNECTED_STATE:
2870 FIXME("Option INTERNET_OPTION_CONNECTED_STATE: STUB\n");
2871 break;
2872 case INTERNET_OPTION_DISABLE_PASSPORT_AUTH:
2873 TRACE("Option INTERNET_OPTION_DISABLE_PASSPORT_AUTH: harmless stub, since not enabled\n");
2874 break;
2875 case INTERNET_OPTION_SEND_TIMEOUT:
2876 case INTERNET_OPTION_RECEIVE_TIMEOUT:
2877 case INTERNET_OPTION_DATA_SEND_TIMEOUT:
2878 {
2879 ULONG timeout = *(ULONG *)lpBuffer;
2880 FIXME("INTERNET_OPTION_SEND/RECEIVE_TIMEOUT/DATA_SEND_TIMEOUT %d\n", timeout);
2881 break;
2882 }
2883 case INTERNET_OPTION_CONNECT_RETRIES:
2884 {
2885 ULONG retries = *(ULONG *)lpBuffer;
2886 FIXME("INTERNET_OPTION_CONNECT_RETRIES %d\n", retries);
2887 break;
2888 }
2889 case INTERNET_OPTION_CONTEXT_VALUE:
2890 {
2891 if (!lpwhh)
2892 {
2893 SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2894 return FALSE;
2895 }
2896 if (!lpBuffer || dwBufferLength != sizeof(DWORD_PTR))
2897 {
2898 SetLastError(ERROR_INVALID_PARAMETER);
2899 ret = FALSE;
2900 }
2901 else
2902 lpwhh->dwContext = *(DWORD_PTR *)lpBuffer;
2903 break;
2904 }
2905 case INTERNET_OPTION_SECURITY_FLAGS:
2906 FIXME("Option INTERNET_OPTION_SECURITY_FLAGS; STUB\n");
2907 break;
2908 case INTERNET_OPTION_DISABLE_AUTODIAL:
2909 FIXME("Option INTERNET_OPTION_DISABLE_AUTODIAL; STUB\n");
2910 break;
2911 case INTERNET_OPTION_HTTP_DECODING:
2912 FIXME("INTERNET_OPTION_HTTP_DECODING; STUB\n");
2913 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2914 ret = FALSE;
2915 break;
2916 case INTERNET_OPTION_COOKIES_3RD_PARTY:
2917 FIXME("INTERNET_OPTION_COOKIES_3RD_PARTY; STUB\n");
2918 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2919 ret = FALSE;
2920 break;
2921 case INTERNET_OPTION_SEND_UTF8_SERVERNAME_TO_PROXY:
2922 FIXME("INTERNET_OPTION_SEND_UTF8_SERVERNAME_TO_PROXY; STUB\n");
2923 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2924 ret = FALSE;
2925 break;
2926 case INTERNET_OPTION_CODEPAGE_PATH:
2927 FIXME("INTERNET_OPTION_CODEPAGE_PATH; STUB\n");
2928 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2929 ret = FALSE;
2930 break;
2931 case INTERNET_OPTION_CODEPAGE_EXTRA:
2932 FIXME("INTERNET_OPTION_CODEPAGE_EXTRA; STUB\n");
2933 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2934 ret = FALSE;
2935 break;
2936 case INTERNET_OPTION_IDN:
2937 FIXME("INTERNET_OPTION_IDN; STUB\n");
2938 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2939 ret = FALSE;
2940 break;
2941 case INTERNET_OPTION_POLICY:
2942 SetLastError(ERROR_INVALID_PARAMETER);
2943 ret = FALSE;
2944 break;
2945 case INTERNET_OPTION_PER_CONNECTION_OPTION: {
2946 INTERNET_PER_CONN_OPTION_LISTW *con = lpBuffer;
2947 LONG res;
2948 unsigned int i;
2949 proxyinfo_t pi;
2950
2951 if (INTERNET_LoadProxySettings(&pi)) return FALSE;
2952
2953 for (i = 0; i < con->dwOptionCount; i++) {
2954 INTERNET_PER_CONN_OPTIONW *option = con->pOptions + i;
2955
2956 switch (option->dwOption) {
2957 case INTERNET_PER_CONN_PROXY_SERVER:
2958 heap_free(pi.proxy);
2959 pi.proxy = heap_strdupW(option->Value.pszValue);
2960 break;
2961
2962 case INTERNET_PER_CONN_FLAGS:
2963 if(option->Value.dwValue & PROXY_TYPE_PROXY)
2964 pi.proxyEnabled = 1;
2965 else
2966 {
2967 if(option->Value.dwValue != PROXY_TYPE_DIRECT)
2968 FIXME("Unhandled flags: 0x%x\n", option->Value.dwValue);
2969 pi.proxyEnabled = 0;
2970 }
2971 break;
2972
2973 case INTERNET_PER_CONN_PROXY_BYPASS:
2974 heap_free(pi.proxyBypass);
2975 pi.proxyBypass = heap_strdupW(option->Value.pszValue);
2976 break;
2977
2978 case INTERNET_PER_CONN_AUTOCONFIG_URL:
2979 case INTERNET_PER_CONN_AUTODISCOVERY_FLAGS:
2980 case INTERNET_PER_CONN_AUTOCONFIG_SECONDARY_URL:
2981 case INTERNET_PER_CONN_AUTOCONFIG_RELOAD_DELAY_MINS:
2982 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_TIME:
2983 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_URL:
2984 FIXME("Unhandled dwOption %d\n", option->dwOption);
2985 break;
2986
2987 default:
2988 FIXME("Unknown dwOption %d\n", option->dwOption);
2989 SetLastError(ERROR_INVALID_PARAMETER);
2990 break;
2991 }
2992 }
2993
2994 if ((res = INTERNET_SaveProxySettings(&pi)))
2995 SetLastError(res);
2996
2997 FreeProxyInfo(&pi);
2998
2999 ret = (res == ERROR_SUCCESS);
3000 break;
3001 }
3002 default:
3003 FIXME("Option %d STUB\n",dwOption);
3004 SetLastError(ERROR_INTERNET_INVALID_OPTION);
3005 ret = FALSE;
3006 break;
3007 }
3008
3009 if(lpwhh)
3010 WININET_Release( lpwhh );
3011
3012 return ret;
3013 }
3014
3015
3016 /***********************************************************************
3017 * InternetSetOptionA (WININET.@)
3018 *
3019 * Sets an options on the specified handle.
3020 *
3021 * RETURNS
3022 * TRUE on success
3023 * FALSE on failure
3024 *
3025 */
3026 BOOL WINAPI InternetSetOptionA(HINTERNET hInternet, DWORD dwOption,
3027 LPVOID lpBuffer, DWORD dwBufferLength)
3028 {
3029 LPVOID wbuffer;
3030 DWORD wlen;
3031 BOOL r;
3032
3033 switch( dwOption )
3034 {
3035 case INTERNET_OPTION_PROXY:
3036 {
3037 LPINTERNET_PROXY_INFOA pi = (LPINTERNET_PROXY_INFOA) lpBuffer;
3038 LPINTERNET_PROXY_INFOW piw;
3039 DWORD proxlen, prbylen;
3040 LPWSTR prox, prby;
3041
3042 proxlen = MultiByteToWideChar( CP_ACP, 0, pi->lpszProxy, -1, NULL, 0);
3043 prbylen= MultiByteToWideChar( CP_ACP, 0, pi->lpszProxyBypass, -1, NULL, 0);
3044 wlen = sizeof(*piw) + proxlen + prbylen;
3045 wbuffer = heap_alloc(wlen*sizeof(WCHAR) );
3046 piw = (LPINTERNET_PROXY_INFOW) wbuffer;
3047 piw->dwAccessType = pi->dwAccessType;
3048 prox = (LPWSTR) &piw[1];
3049 prby = &prox[proxlen+1];
3050 MultiByteToWideChar( CP_ACP, 0, pi->lpszProxy, -1, prox, proxlen);
3051 MultiByteToWideChar( CP_ACP, 0, pi->lpszProxyBypass, -1, prby, prbylen);
3052 piw->lpszProxy = prox;
3053 piw->lpszProxyBypass = prby;
3054 }
3055 break;
3056 case INTERNET_OPTION_USER_AGENT:
3057 case INTERNET_OPTION_USERNAME:
3058 case INTERNET_OPTION_PASSWORD:
3059 case INTERNET_OPTION_PROXY_USERNAME:
3060 case INTERNET_OPTION_PROXY_PASSWORD:
3061 wlen = MultiByteToWideChar( CP_ACP, 0, lpBuffer, -1, NULL, 0 );
3062 if (!(wbuffer = heap_alloc( wlen * sizeof(WCHAR) ))) return ERROR_OUTOFMEMORY;
3063 MultiByteToWideChar( CP_ACP, 0, lpBuffer, -1, wbuffer, wlen );
3064 break;
3065 case INTERNET_OPTION_PER_CONNECTION_OPTION: {
3066 unsigned int i;
3067 INTERNET_PER_CONN_OPTION_LISTW *listW;
3068 INTERNET_PER_CONN_OPTION_LISTA *listA = lpBuffer;
3069 wlen = sizeof(INTERNET_PER_CONN_OPTION_LISTW);
3070 wbuffer = heap_alloc(wlen);
3071 listW = wbuffer;
3072
3073 listW->dwSize = sizeof(INTERNET_PER_CONN_OPTION_LISTW);
3074 if (listA->pszConnection)
3075 {
3076 wlen = MultiByteToWideChar( CP_ACP, 0, listA->pszConnection, -1, NULL, 0 );
3077 listW->pszConnection = heap_alloc(wlen*sizeof(WCHAR));
3078 MultiByteToWideChar( CP_ACP, 0, listA->pszConnection, -1, listW->pszConnection, wlen );
3079 }
3080 else
3081 listW->pszConnection = NULL;
3082 listW->dwOptionCount = listA->dwOptionCount;
3083 listW->dwOptionError = listA->dwOptionError;
3084 listW->pOptions = heap_alloc(sizeof(INTERNET_PER_CONN_OPTIONW) * listA->dwOptionCount);
3085
3086 for (i = 0; i < listA->dwOptionCount; ++i) {
3087 INTERNET_PER_CONN_OPTIONA *optA = listA->pOptions + i;
3088 INTERNET_PER_CONN_OPTIONW *optW = listW->pOptions + i;
3089
3090 optW->dwOption = optA->dwOption;
3091
3092 switch (optA->dwOption) {
3093 case INTERNET_PER_CONN_AUTOCONFIG_URL:
3094 case INTERNET_PER_CONN_PROXY_BYPASS:
3095 case INTERNET_PER_CONN_PROXY_SERVER:
3096 case INTERNET_PER_CONN_AUTOCONFIG_SECONDARY_URL:
3097 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_URL:
3098 if (optA->Value.pszValue)
3099 {
3100 wlen = MultiByteToWideChar( CP_ACP, 0, optA->Value.pszValue, -1, NULL, 0 );
3101 optW->Value.pszValue = heap_alloc(wlen*sizeof(WCHAR));
3102 MultiByteToWideChar( CP_ACP, 0, optA->Value.pszValue, -1, optW->Value.pszValue, wlen );
3103 }
3104 else
3105 optW->Value.pszValue = NULL;
3106 break;
3107 case INTERNET_PER_CONN_AUTODISCOVERY_FLAGS:
3108 case INTERNET_PER_CONN_FLAGS:
3109 case INTERNET_PER_CONN_AUTOCONFIG_RELOAD_DELAY_MINS:
3110 optW->Value.dwValue = optA->Value.dwValue;
3111 break;
3112 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_TIME:
3113 optW->Value.ftValue = optA->Value.ftValue;
3114 break;
3115 default:
3116 WARN("Unknown PER_CONN dwOption: %d, guessing at conversion to Wide\n", optA->dwOption);
3117 optW->Value.dwValue = optA->Value.dwValue;
3118 break;
3119 }
3120 }
3121 }
3122 break;
3123 default:
3124 wbuffer = lpBuffer;
3125 wlen = dwBufferLength;
3126 }
3127
3128 r = InternetSetOptionW(hInternet,dwOption, wbuffer, wlen);
3129
3130 if( lpBuffer != wbuffer )
3131 {
3132 if (dwOption == INTERNET_OPTION_PER_CONNECTION_OPTION)
3133 {
3134 INTERNET_PER_CONN_OPTION_LISTW *list = wbuffer;
3135 unsigned int i;
3136 for (i = 0; i < list->dwOptionCount; ++i) {
3137 INTERNET_PER_CONN_OPTIONW *opt = list->pOptions + i;
3138 switch (opt->dwOption) {
3139 case INTERNET_PER_CONN_AUTOCONFIG_URL:
3140 case INTERNET_PER_CONN_PROXY_BYPASS:
3141 case INTERNET_PER_CONN_PROXY_SERVER:
3142 case INTERNET_PER_CONN_AUTOCONFIG_SECONDARY_URL:
3143 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_URL:
3144 heap_free( opt->Value.pszValue );
3145 break;
3146 default:
3147 break;
3148 }
3149 }
3150 heap_free( list->pOptions );
3151 }
3152 heap_free( wbuffer );
3153 }
3154
3155 return r;
3156 }
3157
3158
3159 /***********************************************************************
3160 * InternetSetOptionExA (WININET.@)
3161 */
3162 BOOL WINAPI InternetSetOptionExA(HINTERNET hInternet, DWORD dwOption,
3163 LPVOID lpBuffer, DWORD dwBufferLength, DWORD dwFlags)
3164 {