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