6f8632a2f43c3239e6b3e0198a651ccee2188b6d
[reactos.git] / 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 "config.h"
30
31 #ifdef HAVE_CORESERVICES_CORESERVICES_H
32 #define GetCurrentThread MacGetCurrentThread
33 #define LoadResource MacLoadResource
34 #include <CoreServices/CoreServices.h>
35 #undef GetCurrentThread
36 #undef LoadResource
37 #undef DPRINTF
38 #endif
39
40 #include "winsock2.h"
41 #include "ws2ipdef.h"
42
43 #include <string.h>
44 #include <stdarg.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <ctype.h>
48 #include <assert.h>
49
50 #include "windef.h"
51 #include "winbase.h"
52 #include "winreg.h"
53 #include "winuser.h"
54 #include "wininet.h"
55 #include "winnls.h"
56 #include "wine/debug.h"
57 #include "winerror.h"
58 #define NO_SHLWAPI_STREAM
59 #include "shlwapi.h"
60
61 #include "wine/exception.h"
62
63 #include "internet.h"
64 #include "resource.h"
65
66 #include "wine/unicode.h"
67
68 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
69
70 typedef struct
71 {
72 DWORD dwError;
73 CHAR response[MAX_REPLY_LEN];
74 } WITHREADERROR, *LPWITHREADERROR;
75
76 static DWORD g_dwTlsErrIndex = TLS_OUT_OF_INDEXES;
77 HMODULE WININET_hModule;
78
79 static CRITICAL_SECTION WININET_cs;
80 static CRITICAL_SECTION_DEBUG WININET_cs_debug =
81 {
82 0, 0, &WININET_cs,
83 { &WININET_cs_debug.ProcessLocksList, &WININET_cs_debug.ProcessLocksList },
84 0, 0, { (DWORD_PTR)(__FILE__ ": WININET_cs") }
85 };
86 static CRITICAL_SECTION WININET_cs = { &WININET_cs_debug, -1, 0, 0, 0, 0 };
87
88 static object_header_t **handle_table;
89 static UINT_PTR next_handle;
90 static UINT_PTR handle_table_size;
91
92 typedef struct
93 {
94 DWORD proxyEnabled;
95 LPWSTR proxy;
96 LPWSTR proxyBypass;
97 LPWSTR proxyUsername;
98 LPWSTR proxyPassword;
99 } proxyinfo_t;
100
101 static ULONG max_conns = 2, max_1_0_conns = 4;
102 static ULONG connect_timeout = 60000;
103
104 static const WCHAR szInternetSettings[] =
105 { 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
106 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
107 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s',0 };
108 static const WCHAR szProxyServer[] = { 'P','r','o','x','y','S','e','r','v','e','r', 0 };
109 static const WCHAR szProxyEnable[] = { 'P','r','o','x','y','E','n','a','b','l','e', 0 };
110 static const WCHAR szProxyOverride[] = { 'P','r','o','x','y','O','v','e','r','r','i','d','e', 0 };
111
112 void *alloc_object(object_header_t *parent, const object_vtbl_t *vtbl, size_t size)
113 {
114 UINT_PTR handle = 0, num;
115 object_header_t *ret;
116 object_header_t **p;
117 BOOL res = TRUE;
118
119 ret = heap_alloc_zero(size);
120 if(!ret)
121 return NULL;
122
123 list_init(&ret->children);
124
125 EnterCriticalSection( &WININET_cs );
126
127 if(!handle_table_size) {
128 num = 16;
129 p = heap_alloc_zero(sizeof(handle_table[0]) * num);
130 if(p) {
131 handle_table = p;
132 handle_table_size = num;
133 next_handle = 1;
134 }else {
135 res = FALSE;
136 }
137 }else if(next_handle == handle_table_size) {
138 num = handle_table_size * 2;
139 p = heap_realloc_zero(handle_table, sizeof(handle_table[0]) * num);
140 if(p) {
141 handle_table = p;
142 handle_table_size = num;
143 }else {
144 res = FALSE;
145 }
146 }
147
148 if(res) {
149 handle = next_handle;
150 if(handle_table[handle])
151 ERR("handle isn't free but should be\n");
152 handle_table[handle] = ret;
153 ret->valid_handle = TRUE;
154
155 while(next_handle < handle_table_size && handle_table[next_handle])
156 next_handle++;
157 }
158
159 LeaveCriticalSection( &WININET_cs );
160
161 if(!res) {
162 heap_free(ret);
163 return NULL;
164 }
165
166 ret->vtbl = vtbl;
167 ret->refs = 1;
168 ret->hInternet = (HINTERNET)handle;
169
170 if(parent) {
171 ret->lpfnStatusCB = parent->lpfnStatusCB;
172 ret->dwInternalFlags = parent->dwInternalFlags & INET_CALLBACKW;
173 }
174
175 return ret;
176 }
177
178 object_header_t *WININET_AddRef( object_header_t *info )
179 {
180 ULONG refs = InterlockedIncrement(&info->refs);
181 TRACE("%p -> refcount = %d\n", info, refs );
182 return info;
183 }
184
185 object_header_t *get_handle_object( HINTERNET hinternet )
186 {
187 object_header_t *info = NULL;
188 UINT_PTR handle = (UINT_PTR) hinternet;
189
190 EnterCriticalSection( &WININET_cs );
191
192 if(handle > 0 && handle < handle_table_size && handle_table[handle] && handle_table[handle]->valid_handle)
193 info = WININET_AddRef(handle_table[handle]);
194
195 LeaveCriticalSection( &WININET_cs );
196
197 TRACE("handle %ld -> %p\n", handle, info);
198
199 return info;
200 }
201
202 static void invalidate_handle(object_header_t *info)
203 {
204 object_header_t *child, *next;
205
206 if(!info->valid_handle)
207 return;
208 info->valid_handle = FALSE;
209
210 /* Free all children as native does */
211 LIST_FOR_EACH_ENTRY_SAFE( child, next, &info->children, object_header_t, entry )
212 {
213 TRACE("invalidating child handle %p for parent %p\n", child->hInternet, info);
214 invalidate_handle( child );
215 }
216
217 WININET_Release(info);
218 }
219
220 BOOL WININET_Release( object_header_t *info )
221 {
222 ULONG refs = InterlockedDecrement(&info->refs);
223 TRACE( "object %p refcount = %d\n", info, refs );
224 if( !refs )
225 {
226 invalidate_handle(info);
227 if ( info->vtbl->CloseConnection )
228 {
229 TRACE( "closing connection %p\n", info);
230 info->vtbl->CloseConnection( info );
231 }
232 /* Don't send a callback if this is a session handle created with InternetOpenUrl */
233 if ((info->htype != WH_HHTTPSESSION && info->htype != WH_HFTPSESSION)
234 || !(info->dwInternalFlags & INET_OPENURL))
235 {
236 INTERNET_SendCallback(info, info->dwContext,
237 INTERNET_STATUS_HANDLE_CLOSING, &info->hInternet,
238 sizeof(HINTERNET));
239 }
240 TRACE( "destroying object %p\n", info);
241 if ( info->htype != WH_HINIT )
242 list_remove( &info->entry );
243 info->vtbl->Destroy( info );
244
245 if(info->hInternet) {
246 UINT_PTR handle = (UINT_PTR)info->hInternet;
247
248 EnterCriticalSection( &WININET_cs );
249
250 handle_table[handle] = NULL;
251 if(next_handle > handle)
252 next_handle = handle;
253
254 LeaveCriticalSection( &WININET_cs );
255 }
256
257 heap_free(info);
258 }
259 return TRUE;
260 }
261
262 /***********************************************************************
263 * DllMain [Internal] Initializes the internal 'WININET.DLL'.
264 *
265 * PARAMS
266 * hinstDLL [I] handle to the DLL's instance
267 * fdwReason [I]
268 * lpvReserved [I] reserved, must be NULL
269 *
270 * RETURNS
271 * Success: TRUE
272 * Failure: FALSE
273 */
274
275 BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
276 {
277 TRACE("%p,%x,%p\n", hinstDLL, fdwReason, lpvReserved);
278
279 switch (fdwReason) {
280 case DLL_PROCESS_ATTACH:
281
282 g_dwTlsErrIndex = TlsAlloc();
283
284 if (g_dwTlsErrIndex == TLS_OUT_OF_INDEXES)
285 return FALSE;
286
287 if(!init_urlcache())
288 {
289 TlsFree(g_dwTlsErrIndex);
290 return FALSE;
291 }
292
293 WININET_hModule = hinstDLL;
294 break;
295
296 case DLL_THREAD_ATTACH:
297 break;
298
299 case DLL_THREAD_DETACH:
300 if (g_dwTlsErrIndex != TLS_OUT_OF_INDEXES)
301 {
302 heap_free(TlsGetValue(g_dwTlsErrIndex));
303 }
304 break;
305
306 case DLL_PROCESS_DETACH:
307 if (lpvReserved) break;
308 collect_connections(COLLECT_CLEANUP);
309 NETCON_unload();
310 free_urlcache();
311 free_cookie();
312
313 if (g_dwTlsErrIndex != TLS_OUT_OF_INDEXES)
314 {
315 heap_free(TlsGetValue(g_dwTlsErrIndex));
316 TlsFree(g_dwTlsErrIndex);
317 }
318 break;
319 }
320 return TRUE;
321 }
322
323 /***********************************************************************
324 * DllInstall (WININET.@)
325 */
326 HRESULT WINAPI DllInstall(BOOL bInstall, LPCWSTR cmdline)
327 {
328 FIXME("(%x %s): stub\n", bInstall, debugstr_w(cmdline));
329 return S_OK;
330 }
331
332 /***********************************************************************
333 * INTERNET_SaveProxySettings
334 *
335 * Stores the proxy settings given by lpwai into the registry
336 *
337 * RETURNS
338 * ERROR_SUCCESS if no error, or error code on fail
339 */
340 static LONG INTERNET_SaveProxySettings( proxyinfo_t *lpwpi )
341 {
342 HKEY key;
343 LONG ret;
344
345 if ((ret = RegOpenKeyW( HKEY_CURRENT_USER, szInternetSettings, &key )))
346 return ret;
347
348 if ((ret = RegSetValueExW( key, szProxyEnable, 0, REG_DWORD, (BYTE*)&lpwpi->proxyEnabled, sizeof(DWORD))))
349 {
350 RegCloseKey( key );
351 return ret;
352 }
353
354 if (lpwpi->proxy)
355 {
356 if ((ret = RegSetValueExW( key, szProxyServer, 0, REG_SZ, (BYTE*)lpwpi->proxy, sizeof(WCHAR) * (lstrlenW(lpwpi->proxy) + 1))))
357 {
358 RegCloseKey( key );
359 return ret;
360 }
361 }
362 else
363 {
364 if ((ret = RegDeleteValueW( key, szProxyServer )) && ret != ERROR_FILE_NOT_FOUND)
365 {
366 RegCloseKey( key );
367 return ret;
368 }
369 }
370
371 RegCloseKey(key);
372 return ERROR_SUCCESS;
373 }
374
375 /***********************************************************************
376 * INTERNET_FindProxyForProtocol
377 *
378 * Searches the proxy string for a proxy of the given protocol.
379 * Returns the found proxy, or the default proxy if none of the given
380 * protocol is found.
381 *
382 * PARAMETERS
383 * szProxy [In] proxy string to search
384 * proto [In] protocol to search for, e.g. "http"
385 * foundProxy [Out] found proxy
386 * foundProxyLen [In/Out] length of foundProxy buffer, in WCHARs
387 *
388 * RETURNS
389 * TRUE if a proxy is found, FALSE if not. If foundProxy is too short,
390 * *foundProxyLen is set to the required size in WCHARs, including the
391 * NULL terminator, and the last error is set to ERROR_INSUFFICIENT_BUFFER.
392 */
393 WCHAR *INTERNET_FindProxyForProtocol(LPCWSTR szProxy, LPCWSTR proto)
394 {
395 WCHAR *ret = NULL;
396 const WCHAR *ptr;
397
398 TRACE("(%s, %s)\n", debugstr_w(szProxy), debugstr_w(proto));
399
400 /* First, look for the specified protocol (proto=scheme://host:port) */
401 for (ptr = szProxy; ptr && *ptr; )
402 {
403 LPCWSTR end, equal;
404
405 if (!(end = strchrW(ptr, ' ')))
406 end = ptr + strlenW(ptr);
407 if ((equal = strchrW(ptr, '=')) && equal < end &&
408 equal - ptr == strlenW(proto) &&
409 !strncmpiW(proto, ptr, strlenW(proto)))
410 {
411 ret = heap_strndupW(equal + 1, end - equal - 1);
412 TRACE("found proxy for %s: %s\n", debugstr_w(proto), debugstr_w(ret));
413 return ret;
414 }
415 if (*end == ' ')
416 ptr = end + 1;
417 else
418 ptr = end;
419 }
420
421 /* It wasn't found: look for no protocol */
422 for (ptr = szProxy; ptr && *ptr; )
423 {
424 LPCWSTR end;
425
426 if (!(end = strchrW(ptr, ' ')))
427 end = ptr + strlenW(ptr);
428 if (!strchrW(ptr, '='))
429 {
430 ret = heap_strndupW(ptr, end - ptr);
431 TRACE("found proxy for %s: %s\n", debugstr_w(proto), debugstr_w(ret));
432 return ret;
433 }
434 if (*end == ' ')
435 ptr = end + 1;
436 else
437 ptr = end;
438 }
439
440 return NULL;
441 }
442
443 /***********************************************************************
444 * InternetInitializeAutoProxyDll (WININET.@)
445 *
446 * Setup the internal proxy
447 *
448 * PARAMETERS
449 * dwReserved
450 *
451 * RETURNS
452 * FALSE on failure
453 *
454 */
455 BOOL WINAPI InternetInitializeAutoProxyDll(DWORD dwReserved)
456 {
457 FIXME("STUB\n");
458 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
459 return FALSE;
460 }
461
462 /***********************************************************************
463 * DetectAutoProxyUrl (WININET.@)
464 *
465 * Auto detect the proxy url
466 *
467 * RETURNS
468 * FALSE on failure
469 *
470 */
471 BOOL WINAPI DetectAutoProxyUrl(LPSTR lpszAutoProxyUrl,
472 DWORD dwAutoProxyUrlLength, DWORD dwDetectFlags)
473 {
474 FIXME("STUB\n");
475 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
476 return FALSE;
477 }
478
479 static void FreeProxyInfo( proxyinfo_t *lpwpi )
480 {
481 heap_free(lpwpi->proxy);
482 heap_free(lpwpi->proxyBypass);
483 heap_free(lpwpi->proxyUsername);
484 heap_free(lpwpi->proxyPassword);
485 }
486
487 static proxyinfo_t *global_proxy;
488
489 static void free_global_proxy( void )
490 {
491 EnterCriticalSection( &WININET_cs );
492 if (global_proxy)
493 {
494 FreeProxyInfo( global_proxy );
495 heap_free( global_proxy );
496 }
497 LeaveCriticalSection( &WININET_cs );
498 }
499
500 static BOOL parse_proxy_url( proxyinfo_t *info, const WCHAR *url )
501 {
502 static const WCHAR fmt[] = {'%','.','*','s',':','%','u',0};
503 URL_COMPONENTSW uc = {sizeof(uc)};
504
505 uc.dwHostNameLength = 1;
506 uc.dwUserNameLength = 1;
507 uc.dwPasswordLength = 1;
508
509 if (!InternetCrackUrlW( url, 0, 0, &uc )) return FALSE;
510 if (!uc.dwHostNameLength)
511 {
512 if (!(info->proxy = heap_strdupW( url ))) return FALSE;
513 info->proxyUsername = NULL;
514 info->proxyPassword = NULL;
515 return TRUE;
516 }
517 if (!(info->proxy = heap_alloc( (uc.dwHostNameLength + 12) * sizeof(WCHAR) ))) return FALSE;
518 sprintfW( info->proxy, fmt, uc.dwHostNameLength, uc.lpszHostName, uc.nPort );
519
520 if (!uc.dwUserNameLength) info->proxyUsername = NULL;
521 else if (!(info->proxyUsername = heap_strndupW( uc.lpszUserName, uc.dwUserNameLength )))
522 {
523 heap_free( info->proxy );
524 return FALSE;
525 }
526 if (!uc.dwPasswordLength) info->proxyPassword = NULL;
527 else if (!(info->proxyPassword = heap_strndupW( uc.lpszPassword, uc.dwPasswordLength )))
528 {
529 heap_free( info->proxyUsername );
530 heap_free( info->proxy );
531 return FALSE;
532 }
533 return TRUE;
534 }
535
536 /***********************************************************************
537 * INTERNET_LoadProxySettings
538 *
539 * Loads proxy information from process-wide global settings, the registry,
540 * or the environment into lpwpi.
541 *
542 * The caller should call FreeProxyInfo when done with lpwpi.
543 *
544 * FIXME:
545 * The proxy may be specified in the form 'http=proxy.my.org'
546 * Presumably that means there can be ftp=ftpproxy.my.org too.
547 */
548 static LONG INTERNET_LoadProxySettings( proxyinfo_t *lpwpi )
549 {
550 HKEY key;
551 DWORD type, len;
552 LPCSTR envproxy;
553 LONG ret;
554
555 memset( lpwpi, 0, sizeof(*lpwpi) );
556
557 EnterCriticalSection( &WININET_cs );
558 if (global_proxy)
559 {
560 lpwpi->proxyEnabled = global_proxy->proxyEnabled;
561 lpwpi->proxy = heap_strdupW( global_proxy->proxy );
562 lpwpi->proxyBypass = heap_strdupW( global_proxy->proxyBypass );
563 }
564 LeaveCriticalSection( &WININET_cs );
565
566 if ((ret = RegOpenKeyW( HKEY_CURRENT_USER, szInternetSettings, &key )))
567 {
568 FreeProxyInfo( lpwpi );
569 return ret;
570 }
571
572 len = sizeof(DWORD);
573 if (RegQueryValueExW( key, szProxyEnable, NULL, &type, (BYTE *)&lpwpi->proxyEnabled, &len ) || type != REG_DWORD)
574 {
575 lpwpi->proxyEnabled = 0;
576 if((ret = RegSetValueExW( key, szProxyEnable, 0, REG_DWORD, (BYTE *)&lpwpi->proxyEnabled, sizeof(DWORD) )))
577 {
578 FreeProxyInfo( lpwpi );
579 RegCloseKey( key );
580 return ret;
581 }
582 }
583
584 if (!(envproxy = getenv( "http_proxy" )) || lpwpi->proxyEnabled)
585 {
586 /* figure out how much memory the proxy setting takes */
587 if (!RegQueryValueExW( key, szProxyServer, NULL, &type, NULL, &len ) && len && (type == REG_SZ))
588 {
589 LPWSTR szProxy, p;
590 static const WCHAR szHttp[] = {'h','t','t','p','=',0};
591
592 if (!(szProxy = heap_alloc(len)))
593 {
594 RegCloseKey( key );
595 FreeProxyInfo( lpwpi );
596 return ERROR_OUTOFMEMORY;
597 }
598 RegQueryValueExW( key, szProxyServer, NULL, &type, (BYTE*)szProxy, &len );
599
600 /* find the http proxy, and strip away everything else */
601 p = strstrW( szProxy, szHttp );
602 if (p)
603 {
604 p += lstrlenW( szHttp );
605 lstrcpyW( szProxy, p );
606 }
607 p = strchrW( szProxy, ';' );
608 if (p) *p = 0;
609
610 FreeProxyInfo( lpwpi );
611 lpwpi->proxy = szProxy;
612 lpwpi->proxyBypass = NULL;
613
614 TRACE("http proxy (from registry) = %s\n", debugstr_w(lpwpi->proxy));
615 }
616 else
617 {
618 TRACE("No proxy server settings in registry.\n");
619 FreeProxyInfo( lpwpi );
620 lpwpi->proxy = NULL;
621 lpwpi->proxyBypass = NULL;
622 }
623 }
624 else if (envproxy)
625 {
626 WCHAR *envproxyW;
627
628 len = MultiByteToWideChar( CP_UNIXCP, 0, envproxy, -1, NULL, 0 );
629 if (!(envproxyW = heap_alloc(len * sizeof(WCHAR))))
630 {
631 RegCloseKey( key );
632 return ERROR_OUTOFMEMORY;
633 }
634 MultiByteToWideChar( CP_UNIXCP, 0, envproxy, -1, envproxyW, len );
635
636 FreeProxyInfo( lpwpi );
637 if (parse_proxy_url( lpwpi, envproxyW ))
638 {
639 TRACE("http proxy (from environment) = %s\n", debugstr_w(lpwpi->proxy));
640 lpwpi->proxyEnabled = 1;
641 lpwpi->proxyBypass = NULL;
642 }
643 else
644 {
645 WARN("failed to parse http_proxy value %s\n", debugstr_w(envproxyW));
646 lpwpi->proxyEnabled = 0;
647 lpwpi->proxy = NULL;
648 lpwpi->proxyBypass = NULL;
649 }
650 heap_free( envproxyW );
651 }
652
653 if (lpwpi->proxyEnabled)
654 {
655 TRACE("Proxy is enabled.\n");
656
657 if (!(envproxy = getenv( "no_proxy" )))
658 {
659 /* figure out how much memory the proxy setting takes */
660 if (!RegQueryValueExW( key, szProxyOverride, NULL, &type, NULL, &len ) && len && (type == REG_SZ))
661 {
662 LPWSTR szProxy;
663
664 if (!(szProxy = heap_alloc(len)))
665 {
666 RegCloseKey( key );
667 return ERROR_OUTOFMEMORY;
668 }
669 RegQueryValueExW( key, szProxyOverride, NULL, &type, (BYTE*)szProxy, &len );
670
671 heap_free( lpwpi->proxyBypass );
672 lpwpi->proxyBypass = szProxy;
673
674 TRACE("http proxy bypass (from registry) = %s\n", debugstr_w(lpwpi->proxyBypass));
675 }
676 else
677 {
678 heap_free( lpwpi->proxyBypass );
679 lpwpi->proxyBypass = NULL;
680
681 TRACE("No proxy bypass server settings in registry.\n");
682 }
683 }
684 else
685 {
686 WCHAR *envproxyW;
687
688 len = MultiByteToWideChar( CP_UNIXCP, 0, envproxy, -1, NULL, 0 );
689 if (!(envproxyW = heap_alloc(len * sizeof(WCHAR))))
690 {
691 RegCloseKey( key );
692 return ERROR_OUTOFMEMORY;
693 }
694 MultiByteToWideChar( CP_UNIXCP, 0, envproxy, -1, envproxyW, len );
695
696 heap_free( lpwpi->proxyBypass );
697 lpwpi->proxyBypass = envproxyW;
698
699 TRACE("http proxy bypass (from environment) = %s\n", debugstr_w(lpwpi->proxyBypass));
700 }
701 }
702 else TRACE("Proxy is disabled.\n");
703
704 RegCloseKey( key );
705 return ERROR_SUCCESS;
706 }
707
708 /***********************************************************************
709 * INTERNET_ConfigureProxy
710 */
711 static BOOL INTERNET_ConfigureProxy( appinfo_t *lpwai )
712 {
713 proxyinfo_t wpi;
714
715 if (INTERNET_LoadProxySettings( &wpi ))
716 return FALSE;
717
718 if (wpi.proxyEnabled)
719 {
720 TRACE("http proxy = %s bypass = %s\n", debugstr_w(wpi.proxy), debugstr_w(wpi.proxyBypass));
721
722 lpwai->accessType = INTERNET_OPEN_TYPE_PROXY;
723 lpwai->proxy = wpi.proxy;
724 lpwai->proxyBypass = wpi.proxyBypass;
725 lpwai->proxyUsername = wpi.proxyUsername;
726 lpwai->proxyPassword = wpi.proxyPassword;
727 return TRUE;
728 }
729
730 lpwai->accessType = INTERNET_OPEN_TYPE_DIRECT;
731 FreeProxyInfo(&wpi);
732 return FALSE;
733 }
734
735 /***********************************************************************
736 * dump_INTERNET_FLAGS
737 *
738 * Helper function to TRACE the internet flags.
739 *
740 * RETURNS
741 * None
742 *
743 */
744 static void dump_INTERNET_FLAGS(DWORD dwFlags)
745 {
746 #define FE(x) { x, #x }
747 static const wininet_flag_info flag[] = {
748 FE(INTERNET_FLAG_RELOAD),
749 FE(INTERNET_FLAG_RAW_DATA),
750 FE(INTERNET_FLAG_EXISTING_CONNECT),
751 FE(INTERNET_FLAG_ASYNC),
752 FE(INTERNET_FLAG_PASSIVE),
753 FE(INTERNET_FLAG_NO_CACHE_WRITE),
754 FE(INTERNET_FLAG_MAKE_PERSISTENT),
755 FE(INTERNET_FLAG_FROM_CACHE),
756 FE(INTERNET_FLAG_SECURE),
757 FE(INTERNET_FLAG_KEEP_CONNECTION),
758 FE(INTERNET_FLAG_NO_AUTO_REDIRECT),
759 FE(INTERNET_FLAG_READ_PREFETCH),
760 FE(INTERNET_FLAG_NO_COOKIES),
761 FE(INTERNET_FLAG_NO_AUTH),
762 FE(INTERNET_FLAG_CACHE_IF_NET_FAIL),
763 FE(INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP),
764 FE(INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTPS),
765 FE(INTERNET_FLAG_IGNORE_CERT_DATE_INVALID),
766 FE(INTERNET_FLAG_IGNORE_CERT_CN_INVALID),
767 FE(INTERNET_FLAG_RESYNCHRONIZE),
768 FE(INTERNET_FLAG_HYPERLINK),
769 FE(INTERNET_FLAG_NO_UI),
770 FE(INTERNET_FLAG_PRAGMA_NOCACHE),
771 FE(INTERNET_FLAG_CACHE_ASYNC),
772 FE(INTERNET_FLAG_FORMS_SUBMIT),
773 FE(INTERNET_FLAG_NEED_FILE),
774 FE(INTERNET_FLAG_TRANSFER_ASCII),
775 FE(INTERNET_FLAG_TRANSFER_BINARY)
776 };
777 #undef FE
778 unsigned int i;
779
780 for (i = 0; i < (sizeof(flag) / sizeof(flag[0])); i++) {
781 if (flag[i].val & dwFlags) {
782 TRACE(" %s", flag[i].name);
783 dwFlags &= ~flag[i].val;
784 }
785 }
786 if (dwFlags)
787 TRACE(" Unknown flags (%08x)\n", dwFlags);
788 else
789 TRACE("\n");
790 }
791
792 /***********************************************************************
793 * INTERNET_CloseHandle (internal)
794 *
795 * Close internet handle
796 *
797 */
798 static VOID APPINFO_Destroy(object_header_t *hdr)
799 {
800 appinfo_t *lpwai = (appinfo_t*)hdr;
801
802 TRACE("%p\n",lpwai);
803
804 heap_free(lpwai->agent);
805 heap_free(lpwai->proxy);
806 heap_free(lpwai->proxyBypass);
807 heap_free(lpwai->proxyUsername);
808 heap_free(lpwai->proxyPassword);
809 }
810
811 static DWORD APPINFO_QueryOption(object_header_t *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
812 {
813 appinfo_t *ai = (appinfo_t*)hdr;
814
815 switch(option) {
816 case INTERNET_OPTION_HANDLE_TYPE:
817 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
818
819 if (*size < sizeof(ULONG))
820 return ERROR_INSUFFICIENT_BUFFER;
821
822 *size = sizeof(DWORD);
823 *(DWORD*)buffer = INTERNET_HANDLE_TYPE_INTERNET;
824 return ERROR_SUCCESS;
825
826 case INTERNET_OPTION_USER_AGENT: {
827 DWORD bufsize;
828
829 TRACE("INTERNET_OPTION_USER_AGENT\n");
830
831 bufsize = *size;
832
833 if (unicode) {
834 DWORD len = ai->agent ? strlenW(ai->agent) : 0;
835
836 *size = (len + 1) * sizeof(WCHAR);
837 if(!buffer || bufsize < *size)
838 return ERROR_INSUFFICIENT_BUFFER;
839
840 if (ai->agent)
841 strcpyW(buffer, ai->agent);
842 else
843 *(WCHAR *)buffer = 0;
844 /* If the buffer is copied, the returned length doesn't include
845 * the NULL terminator.
846 */
847 *size = len;
848 }else {
849 if (ai->agent)
850 *size = WideCharToMultiByte(CP_ACP, 0, ai->agent, -1, NULL, 0, NULL, NULL);
851 else
852 *size = 1;
853 if(!buffer || bufsize < *size)
854 return ERROR_INSUFFICIENT_BUFFER;
855
856 if (ai->agent)
857 WideCharToMultiByte(CP_ACP, 0, ai->agent, -1, buffer, *size, NULL, NULL);
858 else
859 *(char *)buffer = 0;
860 /* If the buffer is copied, the returned length doesn't include
861 * the NULL terminator.
862 */
863 *size -= 1;
864 }
865
866 return ERROR_SUCCESS;
867 }
868
869 case INTERNET_OPTION_PROXY:
870 if(!size) return ERROR_INVALID_PARAMETER;
871 if (unicode) {
872 INTERNET_PROXY_INFOW *pi = (INTERNET_PROXY_INFOW *)buffer;
873 DWORD proxyBytesRequired = 0, proxyBypassBytesRequired = 0;
874 LPWSTR proxy, proxy_bypass;
875
876 if (ai->proxy)
877 proxyBytesRequired = (lstrlenW(ai->proxy) + 1) * sizeof(WCHAR);
878 if (ai->proxyBypass)
879 proxyBypassBytesRequired = (lstrlenW(ai->proxyBypass) + 1) * sizeof(WCHAR);
880 if (!pi || *size < sizeof(INTERNET_PROXY_INFOW) + proxyBytesRequired + proxyBypassBytesRequired)
881 {
882 *size = sizeof(INTERNET_PROXY_INFOW) + proxyBytesRequired + proxyBypassBytesRequired;
883 return ERROR_INSUFFICIENT_BUFFER;
884 }
885 proxy = (LPWSTR)((LPBYTE)buffer + sizeof(INTERNET_PROXY_INFOW));
886 proxy_bypass = (LPWSTR)((LPBYTE)buffer + sizeof(INTERNET_PROXY_INFOW) + proxyBytesRequired);
887
888 pi->dwAccessType = ai->accessType;
889 pi->lpszProxy = NULL;
890 pi->lpszProxyBypass = NULL;
891 if (ai->proxy) {
892 lstrcpyW(proxy, ai->proxy);
893 pi->lpszProxy = proxy;
894 }
895
896 if (ai->proxyBypass) {
897 lstrcpyW(proxy_bypass, ai->proxyBypass);
898 pi->lpszProxyBypass = proxy_bypass;
899 }
900
901 *size = sizeof(INTERNET_PROXY_INFOW) + proxyBytesRequired + proxyBypassBytesRequired;
902 return ERROR_SUCCESS;
903 }else {
904 INTERNET_PROXY_INFOA *pi = (INTERNET_PROXY_INFOA *)buffer;
905 DWORD proxyBytesRequired = 0, proxyBypassBytesRequired = 0;
906 LPSTR proxy, proxy_bypass;
907
908 if (ai->proxy)
909 proxyBytesRequired = WideCharToMultiByte(CP_ACP, 0, ai->proxy, -1, NULL, 0, NULL, NULL);
910 if (ai->proxyBypass)
911 proxyBypassBytesRequired = WideCharToMultiByte(CP_ACP, 0, ai->proxyBypass, -1,
912 NULL, 0, NULL, NULL);
913 if (!pi || *size < sizeof(INTERNET_PROXY_INFOA) + proxyBytesRequired + proxyBypassBytesRequired)
914 {
915 *size = sizeof(INTERNET_PROXY_INFOA) + proxyBytesRequired + proxyBypassBytesRequired;
916 return ERROR_INSUFFICIENT_BUFFER;
917 }
918 proxy = (LPSTR)((LPBYTE)buffer + sizeof(INTERNET_PROXY_INFOA));
919 proxy_bypass = (LPSTR)((LPBYTE)buffer + sizeof(INTERNET_PROXY_INFOA) + proxyBytesRequired);
920
921 pi->dwAccessType = ai->accessType;
922 pi->lpszProxy = NULL;
923 pi->lpszProxyBypass = NULL;
924 if (ai->proxy) {
925 WideCharToMultiByte(CP_ACP, 0, ai->proxy, -1, proxy, proxyBytesRequired, NULL, NULL);
926 pi->lpszProxy = proxy;
927 }
928
929 if (ai->proxyBypass) {
930 WideCharToMultiByte(CP_ACP, 0, ai->proxyBypass, -1, proxy_bypass,
931 proxyBypassBytesRequired, NULL, NULL);
932 pi->lpszProxyBypass = proxy_bypass;
933 }
934
935 *size = sizeof(INTERNET_PROXY_INFOA) + proxyBytesRequired + proxyBypassBytesRequired;
936 return ERROR_SUCCESS;
937 }
938
939 case INTERNET_OPTION_CONNECT_TIMEOUT:
940 TRACE("INTERNET_OPTION_CONNECT_TIMEOUT\n");
941
942 if (*size < sizeof(ULONG))
943 return ERROR_INSUFFICIENT_BUFFER;
944
945 *(ULONG*)buffer = ai->connect_timeout;
946 *size = sizeof(ULONG);
947
948 return ERROR_SUCCESS;
949 }
950
951 return INET_QueryOption(hdr, option, buffer, size, unicode);
952 }
953
954 static DWORD APPINFO_SetOption(object_header_t *hdr, DWORD option, void *buf, DWORD size)
955 {
956 appinfo_t *ai = (appinfo_t*)hdr;
957
958 switch(option) {
959 case INTERNET_OPTION_CONNECT_TIMEOUT:
960 TRACE("INTERNET_OPTION_CONNECT_TIMEOUT\n");
961
962 if(size != sizeof(connect_timeout))
963 return ERROR_INTERNET_BAD_OPTION_LENGTH;
964 if(!*(ULONG*)buf)
965 return ERROR_BAD_ARGUMENTS;
966
967 ai->connect_timeout = *(ULONG*)buf;
968 return ERROR_SUCCESS;
969 case INTERNET_OPTION_USER_AGENT:
970 heap_free(ai->agent);
971 if (!(ai->agent = heap_strdupW(buf))) return ERROR_OUTOFMEMORY;
972 return ERROR_SUCCESS;
973 }
974
975 return INET_SetOption(hdr, option, buf, size);
976 }
977
978 static const object_vtbl_t APPINFOVtbl = {
979 APPINFO_Destroy,
980 NULL,
981 APPINFO_QueryOption,
982 APPINFO_SetOption,
983 NULL,
984 NULL,
985 NULL
986 };
987
988
989 /***********************************************************************
990 * InternetOpenW (WININET.@)
991 *
992 * Per-application initialization of wininet
993 *
994 * RETURNS
995 * HINTERNET on success
996 * NULL on failure
997 *
998 */
999 HINTERNET WINAPI InternetOpenW(LPCWSTR lpszAgent, DWORD dwAccessType,
1000 LPCWSTR lpszProxy, LPCWSTR lpszProxyBypass, DWORD dwFlags)
1001 {
1002 appinfo_t *lpwai = NULL;
1003
1004 #ifdef __REACTOS__
1005 init_winsock();
1006 #endif
1007 if (TRACE_ON(wininet)) {
1008 #define FE(x) { x, #x }
1009 static const wininet_flag_info access_type[] = {
1010 FE(INTERNET_OPEN_TYPE_PRECONFIG),
1011 FE(INTERNET_OPEN_TYPE_DIRECT),
1012 FE(INTERNET_OPEN_TYPE_PROXY),
1013 FE(INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY)
1014 };
1015 #undef FE
1016 DWORD i;
1017 const char *access_type_str = "Unknown";
1018
1019 TRACE("(%s, %i, %s, %s, %i)\n", debugstr_w(lpszAgent), dwAccessType,
1020 debugstr_w(lpszProxy), debugstr_w(lpszProxyBypass), dwFlags);
1021 for (i = 0; i < (sizeof(access_type) / sizeof(access_type[0])); i++) {
1022 if (access_type[i].val == dwAccessType) {
1023 access_type_str = access_type[i].name;
1024 break;
1025 }
1026 }
1027 TRACE(" access type : %s\n", access_type_str);
1028 TRACE(" flags :");
1029 dump_INTERNET_FLAGS(dwFlags);
1030 }
1031
1032 /* Clear any error information */
1033 INTERNET_SetLastError(0);
1034
1035 if((dwAccessType == INTERNET_OPEN_TYPE_PROXY) && !lpszProxy) {
1036 SetLastError(ERROR_INVALID_PARAMETER);
1037 return NULL;
1038 }
1039
1040 lpwai = alloc_object(NULL, &APPINFOVtbl, sizeof(appinfo_t));
1041 if (!lpwai) {
1042 SetLastError(ERROR_OUTOFMEMORY);
1043 return NULL;
1044 }
1045
1046 lpwai->hdr.htype = WH_HINIT;
1047 lpwai->hdr.dwFlags = dwFlags;
1048 lpwai->accessType = dwAccessType;
1049 lpwai->proxyUsername = NULL;
1050 lpwai->proxyPassword = NULL;
1051 lpwai->connect_timeout = connect_timeout;
1052
1053 lpwai->agent = heap_strdupW(lpszAgent);
1054 if(dwAccessType == INTERNET_OPEN_TYPE_PRECONFIG)
1055 INTERNET_ConfigureProxy( lpwai );
1056 else if(dwAccessType == INTERNET_OPEN_TYPE_PROXY) {
1057 lpwai->proxy = heap_strdupW(lpszProxy);
1058 lpwai->proxyBypass = heap_strdupW(lpszProxyBypass);
1059 }
1060
1061 TRACE("returning %p\n", lpwai);
1062
1063 return lpwai->hdr.hInternet;
1064 }
1065
1066
1067 /***********************************************************************
1068 * InternetOpenA (WININET.@)
1069 *
1070 * Per-application initialization of wininet
1071 *
1072 * RETURNS
1073 * HINTERNET on success
1074 * NULL on failure
1075 *
1076 */
1077 HINTERNET WINAPI InternetOpenA(LPCSTR lpszAgent, DWORD dwAccessType,
1078 LPCSTR lpszProxy, LPCSTR lpszProxyBypass, DWORD dwFlags)
1079 {
1080 WCHAR *szAgent, *szProxy, *szBypass;
1081 HINTERNET rc;
1082
1083 TRACE("(%s, 0x%08x, %s, %s, 0x%08x)\n", debugstr_a(lpszAgent),
1084 dwAccessType, debugstr_a(lpszProxy), debugstr_a(lpszProxyBypass), dwFlags);
1085
1086 szAgent = heap_strdupAtoW(lpszAgent);
1087 szProxy = heap_strdupAtoW(lpszProxy);
1088 szBypass = heap_strdupAtoW(lpszProxyBypass);
1089
1090 rc = InternetOpenW(szAgent, dwAccessType, szProxy, szBypass, dwFlags);
1091
1092 heap_free(szAgent);
1093 heap_free(szProxy);
1094 heap_free(szBypass);
1095 return rc;
1096 }
1097
1098 /***********************************************************************
1099 * InternetGetLastResponseInfoA (WININET.@)
1100 *
1101 * Return last wininet error description on the calling thread
1102 *
1103 * RETURNS
1104 * TRUE on success of writing to buffer
1105 * FALSE on failure
1106 *
1107 */
1108 BOOL WINAPI InternetGetLastResponseInfoA(LPDWORD lpdwError,
1109 LPSTR lpszBuffer, LPDWORD lpdwBufferLength)
1110 {
1111 LPWITHREADERROR lpwite = TlsGetValue(g_dwTlsErrIndex);
1112
1113 TRACE("\n");
1114
1115 if (lpwite)
1116 {
1117 *lpdwError = lpwite->dwError;
1118 if (lpwite->dwError)
1119 {
1120 memcpy(lpszBuffer, lpwite->response, *lpdwBufferLength);
1121 *lpdwBufferLength = strlen(lpszBuffer);
1122 }
1123 else
1124 *lpdwBufferLength = 0;
1125 }
1126 else
1127 {
1128 *lpdwError = 0;
1129 *lpdwBufferLength = 0;
1130 }
1131
1132 return TRUE;
1133 }
1134
1135 /***********************************************************************
1136 * InternetGetLastResponseInfoW (WININET.@)
1137 *
1138 * Return last wininet error description on the calling thread
1139 *
1140 * RETURNS
1141 * TRUE on success of writing to buffer
1142 * FALSE on failure
1143 *
1144 */
1145 BOOL WINAPI InternetGetLastResponseInfoW(LPDWORD lpdwError,
1146 LPWSTR lpszBuffer, LPDWORD lpdwBufferLength)
1147 {
1148 LPWITHREADERROR lpwite = TlsGetValue(g_dwTlsErrIndex);
1149
1150 TRACE("\n");
1151
1152 if (lpwite)
1153 {
1154 *lpdwError = lpwite->dwError;
1155 if (lpwite->dwError)
1156 {
1157 memcpy(lpszBuffer, lpwite->response, *lpdwBufferLength);
1158 *lpdwBufferLength = lstrlenW(lpszBuffer);
1159 }
1160 else
1161 *lpdwBufferLength = 0;
1162 }
1163 else
1164 {
1165 *lpdwError = 0;
1166 *lpdwBufferLength = 0;
1167 }
1168
1169 return TRUE;
1170 }
1171
1172 /***********************************************************************
1173 * InternetGetConnectedState (WININET.@)
1174 *
1175 * Return connected state
1176 *
1177 * RETURNS
1178 * TRUE if connected
1179 * if lpdwStatus is not null, return the status (off line,
1180 * modem, lan...) in it.
1181 * FALSE if not connected
1182 */
1183 BOOL WINAPI InternetGetConnectedState(LPDWORD lpdwStatus, DWORD dwReserved)
1184 {
1185 TRACE("(%p, 0x%08x)\n", lpdwStatus, dwReserved);
1186
1187 return InternetGetConnectedStateExW(lpdwStatus, NULL, 0, dwReserved);
1188 }
1189
1190
1191 /***********************************************************************
1192 * InternetGetConnectedStateExW (WININET.@)
1193 *
1194 * Return connected state
1195 *
1196 * PARAMS
1197 *
1198 * lpdwStatus [O] Flags specifying the status of the internet connection.
1199 * lpszConnectionName [O] Pointer to buffer to receive the friendly name of the internet connection.
1200 * dwNameLen [I] Size of the buffer, in characters.
1201 * dwReserved [I] Reserved. Must be set to 0.
1202 *
1203 * RETURNS
1204 * TRUE if connected
1205 * if lpdwStatus is not null, return the status (off line,
1206 * modem, lan...) in it.
1207 * FALSE if not connected
1208 *
1209 * NOTES
1210 * If the system has no available network connections, an empty string is
1211 * stored in lpszConnectionName. If there is a LAN connection, a localized
1212 * "LAN Connection" string is stored. Presumably, if only a dial-up
1213 * connection is available then the name of the dial-up connection is
1214 * returned. Why any application, other than the "Internet Settings" CPL,
1215 * would want to use this function instead of the simpler InternetGetConnectedStateW
1216 * function is beyond me.
1217 */
1218 BOOL WINAPI InternetGetConnectedStateExW(LPDWORD lpdwStatus, LPWSTR lpszConnectionName,
1219 DWORD dwNameLen, DWORD dwReserved)
1220 {
1221 TRACE("(%p, %p, %d, 0x%08x)\n", lpdwStatus, lpszConnectionName, dwNameLen, dwReserved);
1222
1223 /* Must be zero */
1224 if(dwReserved)
1225 return FALSE;
1226
1227 if (lpdwStatus) {
1228 WARN("always returning LAN connection.\n");
1229 *lpdwStatus = INTERNET_CONNECTION_LAN;
1230 }
1231
1232 /* When the buffer size is zero LoadStringW fills the buffer with a pointer to
1233 * the resource, avoid it as we must not change the buffer in this case */
1234 if(lpszConnectionName && dwNameLen) {
1235 *lpszConnectionName = '\0';
1236 LoadStringW(WININET_hModule, IDS_LANCONNECTION, lpszConnectionName, dwNameLen);
1237 }
1238
1239 return TRUE;
1240 }
1241
1242
1243 /***********************************************************************
1244 * InternetGetConnectedStateExA (WININET.@)
1245 */
1246 BOOL WINAPI InternetGetConnectedStateExA(LPDWORD lpdwStatus, LPSTR lpszConnectionName,
1247 DWORD dwNameLen, DWORD dwReserved)
1248 {
1249 LPWSTR lpwszConnectionName = NULL;
1250 BOOL rc;
1251
1252 TRACE("(%p, %p, %d, 0x%08x)\n", lpdwStatus, lpszConnectionName, dwNameLen, dwReserved);
1253
1254 if (lpszConnectionName && dwNameLen > 0)
1255 lpwszConnectionName = heap_alloc(dwNameLen * sizeof(WCHAR));
1256
1257 rc = InternetGetConnectedStateExW(lpdwStatus,lpwszConnectionName, dwNameLen,
1258 dwReserved);
1259 if (rc && lpwszConnectionName)
1260 WideCharToMultiByte(CP_ACP,0,lpwszConnectionName,-1,lpszConnectionName,
1261 dwNameLen, NULL, NULL);
1262
1263 heap_free(lpwszConnectionName);
1264 return rc;
1265 }
1266
1267
1268 /***********************************************************************
1269 * InternetConnectW (WININET.@)
1270 *
1271 * Open a ftp, gopher or http session
1272 *
1273 * RETURNS
1274 * HINTERNET a session handle on success
1275 * NULL on failure
1276 *
1277 */
1278 HINTERNET WINAPI InternetConnectW(HINTERNET hInternet,
1279 LPCWSTR lpszServerName, INTERNET_PORT nServerPort,
1280 LPCWSTR lpszUserName, LPCWSTR lpszPassword,
1281 DWORD dwService, DWORD dwFlags, DWORD_PTR dwContext)
1282 {
1283 appinfo_t *hIC;
1284 HINTERNET rc = NULL;
1285 DWORD res = ERROR_SUCCESS;
1286
1287 TRACE("(%p, %s, %u, %s, %p, %u, %x, %lx)\n", hInternet, debugstr_w(lpszServerName),
1288 nServerPort, debugstr_w(lpszUserName), lpszPassword, 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 static BOOL set_url_component(WCHAR **component, DWORD *component_length, const WCHAR *value, DWORD len)
1453 {
1454 TRACE("%s (%d)\n", debugstr_wn(value, len), len);
1455
1456 if (!*component_length)
1457 return TRUE;
1458
1459 if (!*component) {
1460 *(const WCHAR**)component = value;
1461 *component_length = len;
1462 return TRUE;
1463 }
1464
1465 if (*component_length < len+1) {
1466 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1467 return FALSE;
1468 }
1469
1470 *component_length = len;
1471 if(len)
1472 memcpy(*component, value, len*sizeof(WCHAR));
1473 (*component)[len] = 0;
1474 return TRUE;
1475 }
1476
1477 static BOOL set_url_component_WtoA(const WCHAR *comp_w, DWORD length, const WCHAR *url_w, char **comp, DWORD *ret_length,
1478 const char *url_a)
1479 {
1480 size_t size, ret_size = *ret_length;
1481
1482 if (!*ret_length)
1483 return TRUE;
1484 size = WideCharToMultiByte(CP_ACP, 0, comp_w, length, NULL, 0, NULL, NULL);
1485
1486 if (!*comp) {
1487 *comp = comp_w ? (char*)url_a + WideCharToMultiByte(CP_ACP, 0, url_w, comp_w-url_w, NULL, 0, NULL, NULL) : NULL;
1488 *ret_length = size;
1489 return TRUE;
1490 }
1491
1492 if (size+1 > ret_size) {
1493 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1494 *ret_length = size+1;
1495 return FALSE;
1496 }
1497
1498 *ret_length = size;
1499 WideCharToMultiByte(CP_ACP, 0, comp_w, length, *comp, ret_size-1, NULL, NULL);
1500 (*comp)[size] = 0;
1501 return TRUE;
1502 }
1503
1504 static BOOL set_url_component_AtoW(const char *comp_a, DWORD len_a, WCHAR **comp_w, DWORD *len_w, WCHAR **buf)
1505 {
1506 *len_w = len_a;
1507
1508 if(!comp_a) {
1509 *comp_w = NULL;
1510 return TRUE;
1511 }
1512
1513 if(!(*comp_w = *buf = heap_alloc(len_a*sizeof(WCHAR)))) {
1514 SetLastError(ERROR_OUTOFMEMORY);
1515 return FALSE;
1516 }
1517
1518 return TRUE;
1519 }
1520
1521 /***********************************************************************
1522 * InternetCrackUrlA (WININET.@)
1523 *
1524 * See InternetCrackUrlW.
1525 */
1526 BOOL WINAPI InternetCrackUrlA(const char *url, DWORD url_length, DWORD flags, URL_COMPONENTSA *ret_comp)
1527 {
1528 WCHAR *host = NULL, *user = NULL, *pass = NULL, *path = NULL, *scheme = NULL, *extra = NULL;
1529 URL_COMPONENTSW comp;
1530 WCHAR *url_w = NULL;
1531 BOOL ret;
1532
1533 TRACE("(%s %u %x %p)\n", url_length ? debugstr_an(url, url_length) : debugstr_a(url), url_length, flags, ret_comp);
1534
1535 if (!url || !*url || !ret_comp || ret_comp->dwStructSize != sizeof(URL_COMPONENTSA)) {
1536 SetLastError(ERROR_INVALID_PARAMETER);
1537 return FALSE;
1538 }
1539
1540 comp.dwStructSize = sizeof(comp);
1541
1542 ret = set_url_component_AtoW(ret_comp->lpszHostName, ret_comp->dwHostNameLength,
1543 &comp.lpszHostName, &comp.dwHostNameLength, &host)
1544 && set_url_component_AtoW(ret_comp->lpszUserName, ret_comp->dwUserNameLength,
1545 &comp.lpszUserName, &comp.dwUserNameLength, &user)
1546 && set_url_component_AtoW(ret_comp->lpszPassword, ret_comp->dwPasswordLength,
1547 &comp.lpszPassword, &comp.dwPasswordLength, &pass)
1548 && set_url_component_AtoW(ret_comp->lpszUrlPath, ret_comp->dwUrlPathLength,
1549 &comp.lpszUrlPath, &comp.dwUrlPathLength, &path)
1550 && set_url_component_AtoW(ret_comp->lpszScheme, ret_comp->dwSchemeLength,
1551 &comp.lpszScheme, &comp.dwSchemeLength, &scheme)
1552 && set_url_component_AtoW(ret_comp->lpszExtraInfo, ret_comp->dwExtraInfoLength,
1553 &comp.lpszExtraInfo, &comp.dwExtraInfoLength, &extra);
1554
1555 if(ret && !(url_w = heap_strndupAtoW(url, url_length ? url_length : -1, &url_length))) {
1556 SetLastError(ERROR_OUTOFMEMORY);
1557 ret = FALSE;
1558 }
1559
1560 if (ret && (ret = InternetCrackUrlW(url_w, url_length, flags, &comp))) {
1561 ret_comp->nScheme = comp.nScheme;
1562 ret_comp->nPort = comp.nPort;
1563
1564 ret = set_url_component_WtoA(comp.lpszHostName, comp.dwHostNameLength, url_w,
1565 &ret_comp->lpszHostName, &ret_comp->dwHostNameLength, url)
1566 && set_url_component_WtoA(comp.lpszUserName, comp.dwUserNameLength, url_w,
1567 &ret_comp->lpszUserName, &ret_comp->dwUserNameLength, url)
1568 && set_url_component_WtoA(comp.lpszPassword, comp.dwPasswordLength, url_w,
1569 &ret_comp->lpszPassword, &ret_comp->dwPasswordLength, url)
1570 && set_url_component_WtoA(comp.lpszUrlPath, comp.dwUrlPathLength, url_w,
1571 &ret_comp->lpszUrlPath, &ret_comp->dwUrlPathLength, url)
1572 && set_url_component_WtoA(comp.lpszScheme, comp.dwSchemeLength, url_w,
1573 &ret_comp->lpszScheme, &ret_comp->dwSchemeLength, url)
1574 && set_url_component_WtoA(comp.lpszExtraInfo, comp.dwExtraInfoLength, url_w,
1575 &ret_comp->lpszExtraInfo, &ret_comp->dwExtraInfoLength, url);
1576
1577 if(ret)
1578 TRACE("%s: scheme(%s) host(%s) path(%s) extra(%s)\n", debugstr_a(url),
1579 debugstr_an(ret_comp->lpszScheme, ret_comp->dwSchemeLength),
1580 debugstr_an(ret_comp->lpszHostName, ret_comp->dwHostNameLength),
1581 debugstr_an(ret_comp->lpszUrlPath, ret_comp->dwUrlPathLength),
1582 debugstr_an(ret_comp->lpszExtraInfo, ret_comp->dwExtraInfoLength));
1583 }
1584
1585 heap_free(host);
1586 heap_free(user);
1587 heap_free(pass);
1588 heap_free(path);
1589 heap_free(scheme);
1590 heap_free(extra);
1591 heap_free(url_w);
1592 return ret;
1593 }
1594
1595 static const WCHAR url_schemes[][7] =
1596 {
1597 {'f','t','p',0},
1598 {'g','o','p','h','e','r',0},
1599 {'h','t','t','p',0},
1600 {'h','t','t','p','s',0},
1601 {'f','i','l','e',0},
1602 {'n','e','w','s',0},
1603 {'m','a','i','l','t','o',0},
1604 {'r','e','s',0},
1605 };
1606
1607 /***********************************************************************
1608 * GetInternetSchemeW (internal)
1609 *
1610 * Get scheme of url
1611 *
1612 * RETURNS
1613 * scheme on success
1614 * INTERNET_SCHEME_UNKNOWN on failure
1615 *
1616 */
1617 static INTERNET_SCHEME GetInternetSchemeW(LPCWSTR lpszScheme, DWORD nMaxCmp)
1618 {
1619 int i;
1620
1621 TRACE("%s %d\n",debugstr_wn(lpszScheme, nMaxCmp), nMaxCmp);
1622
1623 if(lpszScheme==NULL)
1624 return INTERNET_SCHEME_UNKNOWN;
1625
1626 for (i = 0; i < sizeof(url_schemes)/sizeof(url_schemes[0]); i++)
1627 if (!strncmpiW(lpszScheme, url_schemes[i], nMaxCmp))
1628 return INTERNET_SCHEME_FIRST + i;
1629
1630 return INTERNET_SCHEME_UNKNOWN;
1631 }
1632
1633 /***********************************************************************
1634 * InternetCrackUrlW (WININET.@)
1635 *
1636 * Break up URL into its components
1637 *
1638 * RETURNS
1639 * TRUE on success
1640 * FALSE on failure
1641 */
1642 BOOL WINAPI InternetCrackUrlW(const WCHAR *lpszUrl, DWORD dwUrlLength, DWORD dwFlags, URL_COMPONENTSW *lpUC)
1643 {
1644 /*
1645 * RFC 1808
1646 * <protocol>:[//<net_loc>][/path][;<params>][?<query>][#<fragment>]
1647 *
1648 */
1649 LPCWSTR lpszParam = NULL;
1650 BOOL found_colon = FALSE;
1651 LPCWSTR lpszap;
1652 LPCWSTR lpszcp = NULL, lpszNetLoc;
1653
1654 TRACE("(%s %u %x %p)\n",
1655 lpszUrl ? debugstr_wn(lpszUrl, dwUrlLength ? dwUrlLength : strlenW(lpszUrl)) : "(null)",
1656 dwUrlLength, dwFlags, lpUC);
1657
1658 if (!lpszUrl || !*lpszUrl || !lpUC)
1659 {
1660 SetLastError(ERROR_INVALID_PARAMETER);
1661 return FALSE;
1662 }
1663 if (!dwUrlLength) dwUrlLength = strlenW(lpszUrl);
1664
1665 if (dwFlags & ICU_DECODE)
1666 {
1667 WCHAR *url_tmp, *buffer;
1668 DWORD len = dwUrlLength + 1;
1669 BOOL ret;
1670
1671 if (!(url_tmp = heap_strndupW(lpszUrl, dwUrlLength)))
1672 {
1673 SetLastError(ERROR_OUTOFMEMORY);
1674 return FALSE;
1675 }
1676
1677 buffer = url_tmp;
1678 ret = InternetCanonicalizeUrlW(url_tmp, buffer, &len, ICU_DECODE | ICU_NO_ENCODE);
1679 if (!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER)
1680 {
1681 buffer = heap_alloc(len * sizeof(WCHAR));
1682 if (!buffer)
1683 {
1684 SetLastError(ERROR_OUTOFMEMORY);
1685 heap_free(url_tmp);
1686 return FALSE;
1687 }
1688 ret = InternetCanonicalizeUrlW(url_tmp, buffer, &len, ICU_DECODE | ICU_NO_ENCODE);
1689 }
1690 if (ret)
1691 ret = InternetCrackUrlW(buffer, len, dwFlags & ~ICU_DECODE, lpUC);
1692
1693 if (buffer != url_tmp) heap_free(buffer);
1694 heap_free(url_tmp);
1695 return ret;
1696 }
1697 lpszap = lpszUrl;
1698
1699 /* Determine if the URI is absolute. */
1700 while (lpszap - lpszUrl < dwUrlLength)
1701 {
1702 if (isalnumW(*lpszap) || *lpszap == '+' || *lpszap == '.' || *lpszap == '-')
1703 {
1704 lpszap++;
1705 continue;
1706 }
1707 if (*lpszap == ':')
1708 {
1709 found_colon = TRUE;
1710 lpszcp = lpszap;
1711 }
1712 else
1713 {
1714 lpszcp = lpszUrl; /* Relative url */
1715 }
1716
1717 break;
1718 }
1719
1720 if(!found_colon){
1721 SetLastError(ERROR_INTERNET_UNRECOGNIZED_SCHEME);
1722 return FALSE;
1723 }
1724
1725 lpUC->nScheme = INTERNET_SCHEME_UNKNOWN;
1726 lpUC->nPort = INTERNET_INVALID_PORT_NUMBER;
1727
1728 /* Parse <params> */
1729 lpszParam = memchrW(lpszap, '?', dwUrlLength - (lpszap - lpszUrl));
1730 if(!lpszParam)
1731 lpszParam = memchrW(lpszap, '#', dwUrlLength - (lpszap - lpszUrl));
1732
1733 if(!set_url_component(&lpUC->lpszExtraInfo, &lpUC->dwExtraInfoLength,
1734 lpszParam, lpszParam ? dwUrlLength-(lpszParam-lpszUrl) : 0))
1735 return FALSE;
1736
1737
1738 /* Get scheme first. */
1739 lpUC->nScheme = GetInternetSchemeW(lpszUrl, lpszcp - lpszUrl);
1740 if(!set_url_component(&lpUC->lpszScheme, &lpUC->dwSchemeLength, lpszUrl, lpszcp - lpszUrl))
1741 return FALSE;
1742
1743 /* Eat ':' in protocol. */
1744 lpszcp++;
1745
1746 /* double slash indicates the net_loc portion is present */
1747 if ((lpszcp[0] == '/') && (lpszcp[1] == '/'))
1748 {
1749 lpszcp += 2;
1750
1751 lpszNetLoc = memchrW(lpszcp, '/', dwUrlLength - (lpszcp - lpszUrl));
1752 if (lpszParam)
1753 {
1754 if (lpszNetLoc)
1755 lpszNetLoc = min(lpszNetLoc, lpszParam);
1756 else
1757 lpszNetLoc = lpszParam;
1758 }
1759 else if (!lpszNetLoc)
1760 lpszNetLoc = lpszcp + dwUrlLength-(lpszcp-lpszUrl);
1761
1762 /* Parse net-loc */
1763 if (lpszNetLoc)
1764 {
1765 LPCWSTR lpszHost;
1766 LPCWSTR lpszPort;
1767
1768 /* [<user>[<:password>]@]<host>[:<port>] */
1769 /* First find the user and password if they exist */
1770
1771 lpszHost = memchrW(lpszcp, '@', dwUrlLength - (lpszcp - lpszUrl));
1772 if (lpszHost == NULL || lpszHost > lpszNetLoc)
1773 {
1774 /* username and password not specified. */
1775 set_url_component(&lpUC->lpszUserName, &lpUC->dwUserNameLength, NULL, 0);
1776 set_url_component(&lpUC->lpszPassword, &lpUC->dwPasswordLength, NULL, 0);
1777 }
1778 else /* Parse out username and password */
1779 {
1780 LPCWSTR lpszUser = lpszcp;
1781 LPCWSTR lpszPasswd = lpszHost;
1782
1783 while (lpszcp < lpszHost)
1784 {
1785 if (*lpszcp == ':')
1786 lpszPasswd = lpszcp;
1787
1788 lpszcp++;
1789 }
1790
1791 if(!set_url_component(&lpUC->lpszUserName, &lpUC->dwUserNameLength, lpszUser, lpszPasswd - lpszUser))
1792 return FALSE;
1793
1794 if (lpszPasswd != lpszHost)
1795 lpszPasswd++;
1796 if(!set_url_component(&lpUC->lpszPassword, &lpUC->dwPasswordLength,
1797 lpszPasswd == lpszHost ? NULL : lpszPasswd, lpszHost - lpszPasswd))
1798 return FALSE;
1799
1800 lpszcp++; /* Advance to beginning of host */
1801 }
1802
1803 /* Parse <host><:port> */
1804
1805 lpszHost = lpszcp;
1806 lpszPort = lpszNetLoc;
1807
1808 /* special case for res:// URLs: there is no port here, so the host is the
1809 entire string up to the first '/' */
1810 if(lpUC->nScheme==INTERNET_SCHEME_RES)
1811 {
1812 if(!set_url_component(&lpUC->lpszHostName, &lpUC->dwHostNameLength, lpszHost, lpszPort - lpszHost))
1813 return FALSE;
1814 lpszcp=lpszNetLoc;
1815 }
1816 else
1817 {
1818 while (lpszcp < lpszNetLoc)
1819 {
1820 if (*lpszcp == ':')
1821 lpszPort = lpszcp;
1822
1823 lpszcp++;
1824 }
1825
1826 /* If the scheme is "file" and the host is just one letter, it's not a host */
1827 if(lpUC->nScheme==INTERNET_SCHEME_FILE && lpszPort <= lpszHost+1)
1828 {
1829 lpszcp=lpszHost;
1830 set_url_component(&lpUC->lpszHostName, &lpUC->dwHostNameLength, NULL, 0);
1831 }
1832 else
1833 {
1834 if(!set_url_component(&lpUC->lpszHostName, &lpUC->dwHostNameLength, lpszHost, lpszPort - lpszHost))
1835 return FALSE;
1836 if (lpszPort != lpszNetLoc)
1837 lpUC->nPort = atoiW(++lpszPort);
1838 else switch (lpUC->nScheme)
1839 {
1840 case INTERNET_SCHEME_HTTP:
1841 lpUC->nPort = INTERNET_DEFAULT_HTTP_PORT;
1842 break;
1843 case INTERNET_SCHEME_HTTPS:
1844 lpUC->nPort = INTERNET_DEFAULT_HTTPS_PORT;
1845 break;
1846 case INTERNET_SCHEME_FTP:
1847 lpUC->nPort = INTERNET_DEFAULT_FTP_PORT;
1848 break;
1849 case INTERNET_SCHEME_GOPHER:
1850 lpUC->nPort = INTERNET_DEFAULT_GOPHER_PORT;
1851 break;
1852 default:
1853 break;
1854 }
1855 }
1856 }
1857 }
1858 }
1859 else
1860 {
1861 set_url_component(&lpUC->lpszUserName, &lpUC->dwUserNameLength, NULL, 0);
1862 set_url_component(&lpUC->lpszPassword, &lpUC->dwPasswordLength, NULL, 0);
1863 set_url_component(&lpUC->lpszHostName, &lpUC->dwHostNameLength, NULL, 0);
1864 }
1865
1866 /* Here lpszcp points to:
1867 *
1868 * <protocol>:[//<net_loc>][/path][;<params>][?<query>][#<fragment>]
1869 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1870 */
1871 if (lpszcp != 0 && lpszcp - lpszUrl < dwUrlLength && (!lpszParam || lpszcp <= lpszParam))
1872 {
1873 DWORD len;
1874
1875 /* Only truncate the parameter list if it's already been saved
1876 * in lpUC->lpszExtraInfo.
1877 */
1878 if (lpszParam && lpUC->dwExtraInfoLength && lpUC->lpszExtraInfo)
1879 len = lpszParam - lpszcp;
1880 else
1881 {
1882 /* Leave the parameter list in lpszUrlPath. Strip off any trailing
1883 * newlines if necessary.
1884 */
1885 LPWSTR lpsznewline = memchrW(lpszcp, '\n', dwUrlLength - (lpszcp - lpszUrl));
1886 if (lpsznewline != NULL)
1887 len = lpsznewline - lpszcp;
1888 else
1889 len = dwUrlLength-(lpszcp-lpszUrl);
1890 }
1891 if (lpUC->dwUrlPathLength && lpUC->lpszUrlPath &&
1892 lpUC->nScheme == INTERNET_SCHEME_FILE)
1893 {
1894 WCHAR tmppath[MAX_PATH];
1895 if (*lpszcp == '/')
1896 {
1897 len = MAX_PATH;
1898 PathCreateFromUrlW(lpszUrl, tmppath, &len, 0);
1899 }
1900 else
1901 {
1902 WCHAR *iter;
1903 memcpy(tmppath, lpszcp, len * sizeof(WCHAR));
1904 tmppath[len] = '\0';
1905
1906 iter = tmppath;
1907 while (*iter) {
1908 if (*iter == '/')
1909 *iter = '\\';
1910 ++iter;
1911 }
1912 }
1913 /* if ends in \. or \.. append a backslash */
1914 if (tmppath[len - 1] == '.' &&
1915 (tmppath[len - 2] == '\\' ||
1916 (tmppath[len - 2] == '.' && tmppath[len - 3] == '\\')))
1917 {
1918 if (len < MAX_PATH - 1)
1919 {
1920 tmppath[len] = '\\';
1921 tmppath[len+1] = '\0';
1922 ++len;
1923 }
1924 }
1925 if(!set_url_component(&lpUC->lpszUrlPath, &lpUC->dwUrlPathLength, tmppath, len))
1926 return FALSE;
1927 }
1928 else if(!set_url_component(&lpUC->lpszUrlPath, &lpUC->dwUrlPathLength, lpszcp, len))
1929 return FALSE;
1930 }
1931 else
1932 {
1933 set_url_component(&lpUC->lpszUrlPath, &lpUC->dwUrlPathLength, lpszcp, 0);
1934 }
1935
1936 TRACE("%s: scheme(%s) host(%s) path(%s) extra(%s)\n", debugstr_wn(lpszUrl,dwUrlLength),
1937 debugstr_wn(lpUC->lpszScheme,lpUC->dwSchemeLength),
1938 debugstr_wn(lpUC->lpszHostName,lpUC->dwHostNameLength),
1939 debugstr_wn(lpUC->lpszUrlPath,lpUC->dwUrlPathLength),
1940 debugstr_wn(lpUC->lpszExtraInfo,lpUC->dwExtraInfoLength));
1941
1942 return TRUE;
1943 }
1944
1945 /***********************************************************************
1946 * InternetAttemptConnect (WININET.@)
1947 *
1948 * Attempt to make a connection to the internet
1949 *
1950 * RETURNS
1951 * ERROR_SUCCESS on success
1952 * Error value on failure
1953 *
1954 */
1955 DWORD WINAPI InternetAttemptConnect(DWORD dwReserved)
1956 {
1957 FIXME("Stub\n");
1958 return ERROR_SUCCESS;
1959 }
1960
1961
1962 /***********************************************************************
1963 * convert_url_canonicalization_flags
1964 *
1965 * Helper for InternetCanonicalizeUrl
1966 *
1967 * PARAMS
1968 * dwFlags [I] Flags suitable for InternetCanonicalizeUrl
1969 *
1970 * RETURNS
1971 * Flags suitable for UrlCanonicalize
1972 */
1973 static DWORD convert_url_canonicalization_flags(DWORD dwFlags)
1974 {
1975 DWORD dwUrlFlags = URL_WININET_COMPATIBILITY | URL_ESCAPE_UNSAFE;
1976
1977 if (dwFlags & ICU_BROWSER_MODE) dwUrlFlags |= URL_BROWSER_MODE;
1978 if (dwFlags & ICU_DECODE) dwUrlFlags |= URL_UNESCAPE;
1979 if (dwFlags & ICU_ENCODE_PERCENT) dwUrlFlags |= URL_ESCAPE_PERCENT;
1980 if (dwFlags & ICU_ENCODE_SPACES_ONLY) dwUrlFlags |= URL_ESCAPE_SPACES_ONLY;
1981 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
1982 if (dwFlags & ICU_NO_ENCODE) dwUrlFlags ^= URL_ESCAPE_UNSAFE;
1983 if (dwFlags & ICU_NO_META) dwUrlFlags |= URL_NO_META;
1984
1985 return dwUrlFlags;
1986 }
1987
1988 /***********************************************************************
1989 * InternetCanonicalizeUrlA (WININET.@)
1990 *
1991 * Escape unsafe characters and spaces
1992 *
1993 * RETURNS
1994 * TRUE on success
1995 * FALSE on failure
1996 *
1997 */
1998 BOOL WINAPI InternetCanonicalizeUrlA(LPCSTR lpszUrl, LPSTR lpszBuffer,
1999 LPDWORD lpdwBufferLength, DWORD dwFlags)
2000 {
2001 HRESULT hr;
2002
2003 TRACE("(%s, %p, %p, 0x%08x) buffer length: %d\n", debugstr_a(lpszUrl), lpszBuffer,
2004 lpdwBufferLength, dwFlags, lpdwBufferLength ? *lpdwBufferLength : -1);
2005
2006 dwFlags = convert_url_canonicalization_flags(dwFlags);
2007 hr = UrlCanonicalizeA(lpszUrl, lpszBuffer, lpdwBufferLength, dwFlags);
2008 if (hr == E_POINTER) SetLastError(ERROR_INSUFFICIENT_BUFFER);
2009 if (hr == E_INVALIDARG) SetLastError(ERROR_INVALID_PARAMETER);
2010
2011 return hr == S_OK;
2012 }
2013
2014 /***********************************************************************
2015 * InternetCanonicalizeUrlW (WININET.@)
2016 *
2017 * Escape unsafe characters and spaces
2018 *
2019 * RETURNS
2020 * TRUE on success
2021 * FALSE on failure
2022 *
2023 */
2024 BOOL WINAPI InternetCanonicalizeUrlW(LPCWSTR lpszUrl, LPWSTR lpszBuffer,
2025 LPDWORD lpdwBufferLength, DWORD dwFlags)
2026 {
2027 HRESULT hr;
2028
2029 TRACE("(%s, %p, %p, 0x%08x) buffer length: %d\n", debugstr_w(lpszUrl), lpszBuffer,
2030 lpdwBufferLength, dwFlags, lpdwBufferLength ? *lpdwBufferLength : -1);
2031
2032 dwFlags = convert_url_canonicalization_flags(dwFlags);
2033 hr = UrlCanonicalizeW(lpszUrl, lpszBuffer, lpdwBufferLength, dwFlags);
2034 if (hr == E_POINTER) SetLastError(ERROR_INSUFFICIENT_BUFFER);
2035 if (hr == E_INVALIDARG) SetLastError(ERROR_INVALID_PARAMETER);
2036
2037 return hr == S_OK;
2038 }
2039
2040 /* #################################################### */
2041
2042 static INTERNET_STATUS_CALLBACK set_status_callback(
2043 object_header_t *lpwh, INTERNET_STATUS_CALLBACK callback, BOOL unicode)
2044 {
2045 INTERNET_STATUS_CALLBACK ret;
2046
2047 if (unicode) lpwh->dwInternalFlags |= INET_CALLBACKW;
2048 else lpwh->dwInternalFlags &= ~INET_CALLBACKW;
2049
2050 ret = lpwh->lpfnStatusCB;
2051 lpwh->lpfnStatusCB = callback;
2052
2053 return ret;
2054 }
2055
2056 /***********************************************************************
2057 * InternetSetStatusCallbackA (WININET.@)
2058 *
2059 * Sets up a callback function which is called as progress is made
2060 * during an operation.
2061 *
2062 * RETURNS
2063 * Previous callback or NULL on success
2064 * INTERNET_INVALID_STATUS_CALLBACK on failure
2065 *
2066 */
2067 INTERNET_STATUS_CALLBACK WINAPI InternetSetStatusCallbackA(
2068 HINTERNET hInternet ,INTERNET_STATUS_CALLBACK lpfnIntCB)
2069 {
2070 INTERNET_STATUS_CALLBACK retVal;
2071 object_header_t *lpwh;
2072
2073 TRACE("%p\n", hInternet);
2074
2075 if (!(lpwh = get_handle_object(hInternet)))
2076 return INTERNET_INVALID_STATUS_CALLBACK;
2077
2078 retVal = set_status_callback(lpwh, lpfnIntCB, FALSE);
2079
2080 WININET_Release( lpwh );
2081 return retVal;
2082 }
2083
2084 /***********************************************************************
2085 * InternetSetStatusCallbackW (WININET.@)
2086 *
2087 * Sets up a callback function which is called as progress is made
2088 * during an operation.
2089 *
2090 * RETURNS
2091 * Previous callback or NULL on success
2092 * INTERNET_INVALID_STATUS_CALLBACK on failure
2093 *
2094 */
2095 INTERNET_STATUS_CALLBACK WINAPI InternetSetStatusCallbackW(
2096 HINTERNET hInternet ,INTERNET_STATUS_CALLBACK lpfnIntCB)
2097 {
2098 INTERNET_STATUS_CALLBACK retVal;
2099 object_header_t *lpwh;
2100
2101 TRACE("%p\n", hInternet);
2102
2103 if (!(lpwh = get_handle_object(hInternet)))
2104 return INTERNET_INVALID_STATUS_CALLBACK;
2105
2106 retVal = set_status_callback(lpwh, lpfnIntCB, TRUE);
2107
2108 WININET_Release( lpwh );
2109 return retVal;
2110 }
2111
2112 /***********************************************************************
2113 * InternetSetFilePointer (WININET.@)
2114 */
2115 DWORD WINAPI InternetSetFilePointer(HINTERNET hFile, LONG lDistanceToMove,
2116 PVOID pReserved, DWORD dwMoveContext, DWORD_PTR dwContext)
2117 {
2118 FIXME("(%p %d %p %d %lx): stub\n", hFile, lDistanceToMove, pReserved, dwMoveContext, dwContext);
2119 return FALSE;
2120 }
2121
2122 /***********************************************************************
2123 * InternetWriteFile (WININET.@)
2124 *
2125 * Write data to an open internet file
2126 *
2127 * RETURNS
2128 * TRUE on success
2129 * FALSE on failure
2130 *
2131 */
2132 BOOL WINAPI InternetWriteFile(HINTERNET hFile, LPCVOID lpBuffer,
2133 DWORD dwNumOfBytesToWrite, LPDWORD lpdwNumOfBytesWritten)
2134 {
2135 object_header_t *lpwh;
2136 BOOL res;
2137
2138 TRACE("(%p %p %d %p)\n", hFile, lpBuffer, dwNumOfBytesToWrite, lpdwNumOfBytesWritten);
2139
2140 lpwh = get_handle_object( hFile );
2141 if (!lpwh) {
2142 WARN("Invalid handle\n");
2143 SetLastError(ERROR_INVALID_HANDLE);
2144 return FALSE;
2145 }
2146
2147 if(lpwh->vtbl->WriteFile) {
2148 res = lpwh->vtbl->WriteFile(lpwh, lpBuffer, dwNumOfBytesToWrite, lpdwNumOfBytesWritten);
2149 }else {
2150 WARN("No Writefile method.\n");
2151 res = ERROR_INVALID_HANDLE;
2152 }
2153
2154 WININET_Release( lpwh );
2155
2156 if(res != ERROR_SUCCESS)
2157 SetLastError(res);
2158 return res == ERROR_SUCCESS;
2159 }
2160
2161
2162 /***********************************************************************
2163 * InternetReadFile (WININET.@)
2164 *
2165 * Read data from an open internet file
2166 *
2167 * RETURNS
2168 * TRUE on success
2169 * FALSE on failure
2170 *
2171 */
2172 BOOL WINAPI InternetReadFile(HINTERNET hFile, LPVOID lpBuffer,
2173 DWORD dwNumOfBytesToRead, LPDWORD pdwNumOfBytesRead)
2174 {
2175 object_header_t *hdr;
2176 DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2177
2178 TRACE("%p %p %d %p\n", hFile, lpBuffer, dwNumOfBytesToRead, pdwNumOfBytesRead);
2179
2180 hdr = get_handle_object(hFile);
2181 if (!hdr) {
2182 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
2183 return FALSE;
2184 }
2185
2186 if(hdr->vtbl->ReadFile) {
2187 res = hdr->vtbl->ReadFile(hdr, lpBuffer, dwNumOfBytesToRead, pdwNumOfBytesRead, 0, 0);
2188 if(res == ERROR_IO_PENDING)
2189 *pdwNumOfBytesRead = 0;
2190 }
2191
2192 WININET_Release(hdr);
2193
2194 TRACE("-- %s (%u) (bytes read: %d)\n", res == ERROR_SUCCESS ? "TRUE": "FALSE", res,
2195 pdwNumOfBytesRead ? *pdwNumOfBytesRead : -1);
2196
2197 if(res != ERROR_SUCCESS)
2198 SetLastError(res);
2199 return res == ERROR_SUCCESS;
2200 }
2201
2202 /***********************************************************************
2203 * InternetReadFileExA (WININET.@)
2204 *
2205 * Read data from an open internet file
2206 *
2207 * PARAMS
2208 * hFile [I] Handle returned by InternetOpenUrl or HttpOpenRequest.
2209 * lpBuffersOut [I/O] Buffer.
2210 * dwFlags [I] Flags. See notes.
2211 * dwContext [I] Context for callbacks.
2212 *
2213 * RETURNS
2214 * TRUE on success
2215 * FALSE on failure
2216 *
2217 * NOTES
2218 * The parameter dwFlags include zero or more of the following flags:
2219 *|IRF_ASYNC - Makes the call asynchronous.
2220 *|IRF_SYNC - Makes the call synchronous.
2221 *|IRF_USE_CONTEXT - Forces dwContext to be used.
2222 *|IRF_NO_WAIT - Don't block if the data is not available, just return what is available.
2223 *
2224 * However, in testing IRF_USE_CONTEXT seems to have no effect - dwContext isn't used.
2225 *
2226 * SEE
2227 * InternetOpenUrlA(), HttpOpenRequestA()
2228 */
2229 BOOL WINAPI InternetReadFileExA(HINTERNET hFile, LPINTERNET_BUFFERSA lpBuffersOut,
2230 DWORD dwFlags, DWORD_PTR dwContext)
2231 {
2232 object_header_t *hdr;
2233 DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2234
2235 TRACE("(%p %p 0x%x 0x%lx)\n", hFile, lpBuffersOut, dwFlags, dwContext);
2236
2237 if (lpBuffersOut->dwStructSize != sizeof(*lpBuffersOut)) {
2238 SetLastError(ERROR_INVALID_PARAMETER);
2239 return FALSE;
2240 }
2241
2242 hdr = get_handle_object(hFile);
2243 if (!hdr) {
2244 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
2245 return FALSE;
2246 }
2247
2248 if(hdr->vtbl->ReadFile)
2249 res = hdr->vtbl->ReadFile(hdr, lpBuffersOut->lpvBuffer, lpBuffersOut->dwBufferLength,
2250 &lpBuffersOut->dwBufferLength, dwFlags, dwContext);
2251
2252 WININET_Release(hdr);
2253
2254 TRACE("-- %s (%u, bytes read: %d)\n", res == ERROR_SUCCESS ? "TRUE": "FALSE",
2255 res, lpBuffersOut->dwBufferLength);
2256
2257 if(res != ERROR_SUCCESS)
2258 SetLastError(res);
2259 return res == ERROR_SUCCESS;
2260 }
2261
2262 /***********************************************************************
2263 * InternetReadFileExW (WININET.@)
2264 * SEE
2265 * InternetReadFileExA()
2266 */
2267 BOOL WINAPI InternetReadFileExW(HINTERNET hFile, LPINTERNET_BUFFERSW lpBuffer,
2268 DWORD dwFlags, DWORD_PTR dwContext)
2269 {
2270 object_header_t *hdr;
2271 DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2272
2273 TRACE("(%p %p 0x%x 0x%lx)\n", hFile, lpBuffer, dwFlags, dwContext);
2274
2275 if (!lpBuffer || lpBuffer->dwStructSize != sizeof(*lpBuffer)) {
2276 SetLastError(ERROR_INVALID_PARAMETER);
2277 return FALSE;
2278 }
2279
2280 hdr = get_handle_object(hFile);
2281 if (!hdr) {
2282 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
2283 return FALSE;
2284 }
2285
2286 if(hdr->vtbl->ReadFile)
2287 res = hdr->vtbl->ReadFile(hdr, lpBuffer->lpvBuffer, lpBuffer->dwBufferLength, &lpBuffer->dwBufferLength,
2288 dwFlags, dwContext);
2289
2290 WININET_Release(hdr);
2291
2292 TRACE("-- %s (%u, bytes read: %d)\n", res == ERROR_SUCCESS ? "TRUE": "FALSE",
2293 res, lpBuffer->dwBufferLength);
2294
2295 if(res != ERROR_SUCCESS)
2296 SetLastError(res);
2297 return res == ERROR_SUCCESS;
2298 }
2299
2300 static WCHAR *get_proxy_autoconfig_url(void)
2301 {
2302 #if defined(MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
2303
2304 CFDictionaryRef settings = CFNetworkCopySystemProxySettings();
2305 WCHAR *ret = NULL;
2306 SIZE_T len;
2307 const void *ref;
2308
2309 if (!settings) return NULL;
2310
2311 if (!(ref = CFDictionaryGetValue( settings, kCFNetworkProxiesProxyAutoConfigURLString )))
2312 {
2313 CFRelease( settings );
2314 return NULL;
2315 }
2316 len = CFStringGetLength( ref );
2317 if (len)
2318 ret = heap_alloc( (len+1) * sizeof(WCHAR) );
2319 if (ret)
2320 {
2321 CFStringGetCharacters( ref, CFRangeMake(0, len), ret );
2322 ret[len] = 0;
2323 }
2324 TRACE( "returning %s\n", debugstr_w(ret) );
2325 CFRelease( settings );
2326 return ret;
2327 #else
2328 static int once;
2329 if (!once++) FIXME( "no support on this platform\n" );
2330 return NULL;
2331 #endif
2332 }
2333
2334 static DWORD query_global_option(DWORD option, void *buffer, DWORD *size, BOOL unicode)
2335 {
2336 /* FIXME: This function currently handles more options than it should. Options requiring
2337 * proper handles should be moved to proper functions */
2338 switch(option) {
2339 case INTERNET_OPTION_HTTP_VERSION:
2340 if (*size < sizeof(HTTP_VERSION_INFO))
2341 return ERROR_INSUFFICIENT_BUFFER;
2342
2343 /*
2344 * Presently hardcoded to 1.1
2345 */
2346 ((HTTP_VERSION_INFO*)buffer)->dwMajorVersion = 1;
2347 ((HTTP_VERSION_INFO*)buffer)->dwMinorVersion = 1;
2348 *size = sizeof(HTTP_VERSION_INFO);
2349
2350 return ERROR_SUCCESS;
2351
2352 case INTERNET_OPTION_CONNECTED_STATE:
2353 FIXME("INTERNET_OPTION_CONNECTED_STATE: semi-stub\n");
2354
2355 if (*size < sizeof(ULONG))
2356 return ERROR_INSUFFICIENT_BUFFER;
2357
2358 *(ULONG*)buffer = INTERNET_STATE_CONNECTED;
2359 *size = sizeof(ULONG);
2360
2361 return ERROR_SUCCESS;
2362
2363 case INTERNET_OPTION_PROXY: {
2364 appinfo_t ai;
2365 BOOL ret;
2366
2367 TRACE("Getting global proxy info\n");
2368 memset(&ai, 0, sizeof(appinfo_t));
2369 INTERNET_ConfigureProxy(&ai);
2370
2371 ret = APPINFO_QueryOption(&ai.hdr, INTERNET_OPTION_PROXY, buffer, size, unicode); /* FIXME */
2372 APPINFO_Destroy(&ai.hdr);
2373 return ret;
2374 }
2375
2376 case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2377 TRACE("INTERNET_OPTION_MAX_CONNS_PER_SERVER\n");
2378
2379 if (*size < sizeof(ULONG))
2380 return ERROR_INSUFFICIENT_BUFFER;
2381
2382 *(ULONG*)buffer = max_conns;
2383 *size = sizeof(ULONG);
2384
2385 return ERROR_SUCCESS;
2386
2387 case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2388 TRACE("INTERNET_OPTION_MAX_CONNS_1_0_SERVER\n");
2389
2390 if (*size < sizeof(ULONG))
2391 return ERROR_INSUFFICIENT_BUFFER;
2392
2393 *(ULONG*)buffer = max_1_0_conns;
2394 *size = sizeof(ULONG);
2395
2396 return ERROR_SUCCESS;
2397
2398 case INTERNET_OPTION_SECURITY_FLAGS:
2399 FIXME("INTERNET_OPTION_SECURITY_FLAGS: Stub\n");
2400 return ERROR_SUCCESS;
2401
2402 case INTERNET_OPTION_VERSION: {
2403 static const INTERNET_VERSION_INFO info = { 1, 2 };
2404
2405 TRACE("INTERNET_OPTION_VERSION\n");
2406
2407 if (*size < sizeof(INTERNET_VERSION_INFO))
2408 return ERROR_INSUFFICIENT_BUFFER;
2409
2410 memcpy(buffer, &info, sizeof(info));
2411 *size = sizeof(info);
2412
2413 return ERROR_SUCCESS;
2414 }
2415
2416 case INTERNET_OPTION_PER_CONNECTION_OPTION: {
2417 WCHAR *url;
2418 INTERNET_PER_CONN_OPTION_LISTW *con = buffer;
2419 INTERNET_PER_CONN_OPTION_LISTA *conA = buffer;
2420 DWORD res = ERROR_SUCCESS, i;
2421 proxyinfo_t pi;
2422 LONG ret;
2423
2424 TRACE("Getting global proxy info\n");
2425 if((ret = INTERNET_LoadProxySettings(&pi)))
2426 return ret;
2427
2428 #ifdef __REACTOS__
2429 WARN("INTERNET_OPTION_PER_CONNECTION_OPTION stub\n");
2430 #else
2431 FIXME("INTERNET_OPTION_PER_CONNECTION_OPTION stub\n");
2432 #endif
2433
2434 if (*size < sizeof(INTERNET_PER_CONN_OPTION_LISTW)) {
2435 FreeProxyInfo(&pi);
2436 return ERROR_INSUFFICIENT_BUFFER;
2437 }
2438
2439 url = get_proxy_autoconfig_url();
2440
2441 for (i = 0; i < con->dwOptionCount; i++) {
2442 INTERNET_PER_CONN_OPTIONW *optionW = con->pOptions + i;
2443 INTERNET_PER_CONN_OPTIONA *optionA = conA->pOptions + i;
2444
2445 switch (optionW->dwOption) {
2446 case INTERNET_PER_CONN_FLAGS:
2447 if(pi.proxyEnabled)
2448 optionW->Value.dwValue = PROXY_TYPE_PROXY;
2449 else
2450 optionW->Value.dwValue = PROXY_TYPE_DIRECT;
2451 if (url)
2452 /* native includes PROXY_TYPE_DIRECT even if PROXY_TYPE_PROXY is set */
2453 optionW->Value.dwValue |= PROXY_TYPE_DIRECT|PROXY_TYPE_AUTO_PROXY_URL;
2454 break;
2455
2456 case INTERNET_PER_CONN_PROXY_SERVER:
2457 if (unicode)
2458 optionW->Value.pszValue = heap_strdupW(pi.proxy);
2459 else
2460 optionA->Value.pszValue = heap_strdupWtoA(pi.proxy);
2461 break;
2462
2463 case INTERNET_PER_CONN_PROXY_BYPASS:
2464 if (unicode)
2465 optionW->Value.pszValue = heap_strdupW(pi.proxyBypass);
2466 else
2467 optionA->Value.pszValue = heap_strdupWtoA(pi.proxyBypass);
2468 break;
2469
2470 case INTERNET_PER_CONN_AUTOCONFIG_URL:
2471 if (!url)
2472 optionW->Value.pszValue = NULL;
2473 else if (unicode)
2474 optionW->Value.pszValue = heap_strdupW(url);
2475 else
2476 optionA->Value.pszValue = heap_strdupWtoA(url);
2477 break;
2478
2479 case INTERNET_PER_CONN_AUTODISCOVERY_FLAGS:
2480 optionW->Value.dwValue = AUTO_PROXY_FLAG_ALWAYS_DETECT;
2481 break;
2482
2483 case INTERNET_PER_CONN_AUTOCONFIG_SECONDARY_URL:
2484 case INTERNET_PER_CONN_AUTOCONFIG_RELOAD_DELAY_MINS:
2485 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_TIME:
2486 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_URL:
2487 FIXME("Unhandled dwOption %d\n", optionW->dwOption);
2488 memset(&optionW->Value, 0, sizeof(optionW->Value));
2489 break;
2490
2491 #ifdef __REACTOS__
2492 case INTERNET_PER_CONN_FLAGS_UI:
2493 WARN("Unhandled dwOption %d\n", optionW->dwOption);
2494 break;
2495
2496 #endif
2497 default:
2498 FIXME("Unknown dwOption %d\n", optionW->dwOption);
2499 res = ERROR_INVALID_PARAMETER;
2500 break;
2501 }
2502 }
2503 heap_free(url);
2504 FreeProxyInfo(&pi);
2505
2506 return res;
2507 }
2508 case INTERNET_OPTION_REQUEST_FLAGS:
2509 case INTERNET_OPTION_USER_AGENT:
2510 *size = 0;
2511 return ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2512 case INTERNET_OPTION_POLICY:
2513 return ERROR_INVALID_PARAMETER;
2514 case INTERNET_OPTION_CONNECT_TIMEOUT:
2515 TRACE("INTERNET_OPTION_CONNECT_TIMEOUT\n");
2516
2517 if (*size < sizeof(ULONG))
2518 return ERROR_INSUFFICIENT_BUFFER;
2519
2520 *(ULONG*)buffer = connect_timeout;
2521 *size = sizeof(ULONG);
2522
2523 return ERROR_SUCCESS;
2524 }
2525
2526 FIXME("Stub for %d\n", option);
2527 return ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2528 }
2529
2530 DWORD INET_QueryOption(object_header_t *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
2531 {
2532 switch(option) {
2533 case INTERNET_OPTION_CONTEXT_VALUE:
2534 if (!size)
2535 return ERROR_INVALID_PARAMETER;
2536
2537 if (*size < sizeof(DWORD_PTR)) {
2538 *size = sizeof(DWORD_PTR);
2539 return ERROR_INSUFFICIENT_BUFFER;
2540 }
2541 if (!buffer)
2542 return ERROR_INVALID_PARAMETER;
2543
2544 *(DWORD_PTR *)buffer = hdr->dwContext;
2545 *size = sizeof(DWORD_PTR);
2546 return ERROR_SUCCESS;
2547
2548 case INTERNET_OPTION_REQUEST_FLAGS:
2549 WARN("INTERNET_OPTION_REQUEST_FLAGS\n");
2550 *size = sizeof(DWORD);
2551 return ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2552
2553 case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2554 case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2555 WARN("Called on global option %u\n", option);
2556 return ERROR_INTERNET_INVALID_OPERATION;
2557 }
2558
2559 /* FIXME: we shouldn't call it here */
2560 return query_global_option(option, buffer, size, unicode);
2561 }
2562
2563 /***********************************************************************
2564 * InternetQueryOptionW (WININET.@)
2565 *
2566 * Queries an options on the specified handle
2567 *
2568 * RETURNS
2569 * TRUE on success
2570 * FALSE on failure
2571 *
2572 */
2573 BOOL WINAPI InternetQueryOptionW(HINTERNET hInternet, DWORD dwOption,
2574 LPVOID lpBuffer, LPDWORD lpdwBufferLength)
2575 {
2576 object_header_t *hdr;
2577 DWORD res = ERROR_INVALID_HANDLE;
2578
2579 TRACE("%p %d %p %p\n", hInternet, dwOption, lpBuffer, lpdwBufferLength);
2580
2581 if(hInternet) {
2582 hdr = get_handle_object(hInternet);
2583 if (hdr) {
2584 res = hdr->vtbl->QueryOption(hdr, dwOption, lpBuffer, lpdwBufferLength, TRUE);
2585 WININET_Release(hdr);
2586 }
2587 }else {
2588 res = query_global_option(dwOption, lpBuffer, lpdwBufferLength, TRUE);
2589 }
2590
2591 if(res != ERROR_SUCCESS)
2592 SetLastError(res);
2593 return res == ERROR_SUCCESS;
2594 }
2595
2596 /***********************************************************************
2597 * InternetQueryOptionA (WININET.@)
2598 *
2599 * Queries an options on the specified handle
2600 *
2601 * RETURNS
2602 * TRUE on success
2603 * FALSE on failure
2604 *
2605 */
2606 BOOL WINAPI InternetQueryOptionA(HINTERNET hInternet, DWORD dwOption,
2607 LPVOID lpBuffer, LPDWORD lpdwBufferLength)
2608 {
2609 object_header_t *hdr;
2610 DWORD res = ERROR_INVALID_HANDLE;
2611
2612 TRACE("%p %d %p %p\n", hInternet, dwOption, lpBuffer, lpdwBufferLength);
2613
2614 if(hInternet) {
2615 hdr = get_handle_object(hInternet);
2616 if (hdr) {
2617 res = hdr->vtbl->QueryOption(hdr, dwOption, lpBuffer, lpdwBufferLength, FALSE);
2618 WININET_Release(hdr);
2619 }
2620 }else {
2621 res = query_global_option(dwOption, lpBuffer, lpdwBufferLength, FALSE);
2622 }
2623
2624 if(res != ERROR_SUCCESS)
2625 SetLastError(res);
2626 return res == ERROR_SUCCESS;
2627 }
2628
2629 DWORD INET_SetOption(object_header_t *hdr, DWORD option, void *buf, DWORD size)
2630 {
2631 switch(option) {
2632 case INTERNET_OPTION_CALLBACK:
2633 WARN("Not settable option %u\n", option);
2634 return ERROR_INTERNET_OPTION_NOT_SETTABLE;
2635 case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2636 case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2637 WARN("Called on global option %u\n", option);
2638 return ERROR_INTERNET_INVALID_OPERATION;
2639 }
2640
2641 return ERROR_INTERNET_INVALID_OPTION;
2642 }
2643
2644 static DWORD set_global_option(DWORD option, void *buf, DWORD size)
2645 {
2646 switch(option) {
2647 case INTERNET_OPTION_CALLBACK:
2648 WARN("Not global option %u\n", option);
2649 return ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2650
2651 case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2652 TRACE("INTERNET_OPTION_MAX_CONNS_PER_SERVER\n");
2653
2654 if(size != sizeof(max_conns))
2655 return ERROR_INTERNET_BAD_OPTION_LENGTH;
2656 if(!*(ULONG*)buf)
2657 return ERROR_BAD_ARGUMENTS;
2658
2659 max_conns = *(ULONG*)buf;
2660 return ERROR_SUCCESS;
2661
2662 case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2663 TRACE("INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER\n");
2664
2665 if(size != sizeof(max_1_0_conns))
2666 return ERROR_INTERNET_BAD_OPTION_LENGTH;
2667 if(!*(ULONG*)buf)
2668 return ERROR_BAD_ARGUMENTS;
2669
2670 max_1_0_conns = *(ULONG*)buf;
2671 return ERROR_SUCCESS;
2672
2673 case INTERNET_OPTION_CONNECT_TIMEOUT:
2674 TRACE("INTERNET_OPTION_CONNECT_TIMEOUT\n");
2675
2676 if(size != sizeof(connect_timeout))
2677 return ERROR_INTERNET_BAD_OPTION_LENGTH;
2678 if(!*(ULONG*)buf)
2679 return ERROR_BAD_ARGUMENTS;
2680
2681 connect_timeout = *(ULONG*)buf;
2682 return ERROR_SUCCESS;
2683
2684 case INTERNET_OPTION_SETTINGS_CHANGED:
2685 FIXME("INTERNETOPTION_SETTINGS_CHANGED semi-stub\n");
2686 collect_connections(COLLECT_CONNECTIONS);
2687 return ERROR_SUCCESS;
2688
2689 case INTERNET_OPTION_SUPPRESS_BEHAVIOR:
2690 FIXME("INTERNET_OPTION_SUPPRESS_BEHAVIOR stub\n");
2691
2692 if(size != sizeof(ULONG))
2693 return ERROR_INTERNET_BAD_OPTION_LENGTH;
2694
2695 FIXME("%08x\n", *(ULONG*)buf);
2696 return ERROR_SUCCESS;
2697 }
2698
2699 return ERROR_INTERNET_INVALID_OPTION;
2700 }
2701
2702 /***********************************************************************
2703 * InternetSetOptionW (WININET.@)
2704 *
2705 * Sets an options on the specified handle
2706 *
2707 * RETURNS
2708 * TRUE on success
2709 * FALSE on failure
2710 *
2711 */
2712 BOOL WINAPI InternetSetOptionW(HINTERNET hInternet, DWORD dwOption,
2713 LPVOID lpBuffer, DWORD dwBufferLength)
2714 {
2715 object_header_t *lpwhh;
2716 BOOL ret = TRUE;
2717 DWORD res;
2718
2719 TRACE("(%p %d %p %d)\n", hInternet, dwOption, lpBuffer, dwBufferLength);
2720
2721 lpwhh = (object_header_t*) get_handle_object( hInternet );
2722 if(lpwhh)
2723 res = lpwhh->vtbl->SetOption(lpwhh, dwOption, lpBuffer, dwBufferLength);
2724 else
2725 res = set_global_option(dwOption, lpBuffer, dwBufferLength);
2726
2727 if(res != ERROR_INTERNET_INVALID_OPTION) {
2728 if(lpwhh)
2729 WININET_Release(lpwhh);
2730
2731 if(res != ERROR_SUCCESS)
2732 SetLastError(res);
2733
2734 return res == ERROR_SUCCESS;
2735 }
2736
2737 switch (dwOption)
2738 {
2739 case INTERNET_OPTION_HTTP_VERSION:
2740 {
2741 HTTP_VERSION_INFO* pVersion=(HTTP_VERSION_INFO*)lpBuffer;
2742 FIXME("Option INTERNET_OPTION_HTTP_VERSION(%d,%d): STUB\n",pVersion->dwMajorVersion,pVersion->dwMinorVersion);
2743 }
2744 break;
2745 case INTERNET_OPTION_ERROR_MASK:
2746 {
2747 if(!lpwhh) {
2748 SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2749 return FALSE;
2750 } else if(*(ULONG*)lpBuffer & (~(INTERNET_ERROR_MASK_INSERT_CDROM|
2751 INTERNET_ERROR_MASK_COMBINED_SEC_CERT|
2752 INTERNET_ERROR_MASK_LOGIN_FAILURE_DISPLAY_ENTITY_BODY))) {
2753 SetLastError(ERROR_INVALID_PARAMETER);
2754 ret = FALSE;
2755 } else if(dwBufferLength != sizeof(ULONG)) {
2756 SetLastError(ERROR_INTERNET_BAD_OPTION_LENGTH);
2757 ret = FALSE;
2758 } else
2759 TRACE("INTERNET_OPTION_ERROR_MASK: %x\n", *(ULONG*)lpBuffer);
2760 lpwhh->ErrorMask = *(ULONG*)lpBuffer;
2761 }
2762 break;
2763 case INTERNET_OPTION_PROXY:
2764 {
2765 INTERNET_PROXY_INFOW *info = lpBuffer;
2766
2767 if (!lpBuffer || dwBufferLength < sizeof(INTERNET_PROXY_INFOW))
2768 {
2769 SetLastError(ERROR_INVALID_PARAMETER);
2770 return FALSE;
2771 }
2772 if (!hInternet)
2773 {
2774 EnterCriticalSection( &WININET_cs );
2775 free_global_proxy();
2776 global_proxy = heap_alloc( sizeof(proxyinfo_t) );
2777 if (global_proxy)
2778 {
2779 if (info->dwAccessType == INTERNET_OPEN_TYPE_PROXY)
2780 {
2781 global_proxy->proxyEnabled = 1;
2782 global_proxy->proxy = heap_strdupW( info->lpszProxy );
2783 global_proxy->proxyBypass = heap_strdupW( info->lpszProxyBypass );
2784 }
2785 else
2786 {
2787 global_proxy->proxyEnabled = 0;
2788 global_proxy->proxy = global_proxy->proxyBypass = NULL;
2789 }
2790 }
2791 LeaveCriticalSection( &WININET_cs );
2792 }
2793 else
2794 {
2795 /* In general, each type of object should handle
2796 * INTERNET_OPTION_PROXY directly. This FIXME ensures it doesn't
2797 * get silently dropped.
2798 */
2799 FIXME("INTERNET_OPTION_PROXY unimplemented\n");
2800 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2801 ret = FALSE;
2802 }
2803 break;
2804 }
2805 case INTERNET_OPTION_CODEPAGE:
2806 {
2807 ULONG codepage = *(ULONG *)lpBuffer;
2808 FIXME("Option INTERNET_OPTION_CODEPAGE (%d): STUB\n", codepage);
2809 }
2810 break;
2811 case INTERNET_OPTION_REQUEST_PRIORITY:
2812 {
2813 ULONG priority = *(ULONG *)lpBuffer;
2814 FIXME("Option INTERNET_OPTION_REQUEST_PRIORITY (%d): STUB\n", priority);
2815 }
2816 break;
2817 case INTERNET_OPTION_CONNECT_TIMEOUT:
2818 {
2819 ULONG connecttimeout = *(ULONG *)lpBuffer;
2820 FIXME("Option INTERNET_OPTION_CONNECT_TIMEOUT (%d): STUB\n", connecttimeout);
2821 }
2822 break;
2823 case INTERNET_OPTION_DATA_RECEIVE_TIMEOUT:
2824 {
2825 ULONG receivetimeout = *(ULONG *)lpBuffer;
2826 FIXME("Option INTERNET_OPTION_DATA_RECEIVE_TIMEOUT (%d): STUB\n", receivetimeout);
2827 }
2828 break;
2829 case INTERNET_OPTION_RESET_URLCACHE_SESSION:
2830 FIXME("Option INTERNET_OPTION_RESET_URLCACHE_SESSION: STUB\n");
2831 break;
2832 case INTERNET_OPTION_END_BROWSER_SESSION:
2833 FIXME("Option INTERNET_OPTION_END_BROWSER_SESSION: semi-stub\n");
2834 free_cookie();
2835 break;
2836 case INTERNET_OPTION_CONNECTED_STATE:
2837 FIXME("Option INTERNET_OPTION_CONNECTED_STATE: STUB\n");
2838 break;
2839 case INTERNET_OPTION_DISABLE_PASSPORT_AUTH:
2840 TRACE("Option INTERNET_OPTION_DISABLE_PASSPORT_AUTH: harmless stub, since not enabled\n");
2841 break;
2842 case INTERNET_OPTION_SEND_TIMEOUT:
2843 case INTERNET_OPTION_RECEIVE_TIMEOUT:
2844 case INTERNET_OPTION_DATA_SEND_TIMEOUT:
2845 {
2846 ULONG timeout = *(ULONG *)lpBuffer;
2847 FIXME("INTERNET_OPTION_SEND/RECEIVE_TIMEOUT/DATA_SEND_TIMEOUT %d\n", timeout);
2848 break;
2849 }
2850 case INTERNET_OPTION_CONNECT_RETRIES:
2851 {
2852 ULONG retries = *(ULONG *)lpBuffer;
2853 FIXME("INTERNET_OPTION_CONNECT_RETRIES %d\n", retries);
2854 break;
2855 }
2856 case INTERNET_OPTION_CONTEXT_VALUE:
2857 {
2858 if (!lpwhh)
2859 {
2860 SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2861 return FALSE;
2862 }
2863 if (!lpBuffer || dwBufferLength != sizeof(DWORD_PTR))
2864 {
2865 SetLastError(ERROR_INVALID_PARAMETER);
2866 ret = FALSE;
2867 }
2868 else
2869 lpwhh->dwContext = *(DWORD_PTR *)lpBuffer;
2870 break;
2871 }
2872 case INTERNET_OPTION_SECURITY_FLAGS:
2873 FIXME("Option INTERNET_OPTION_SECURITY_FLAGS; STUB\n");
2874 break;
2875 case INTERNET_OPTION_DISABLE_AUTODIAL:
2876 FIXME("Option INTERNET_OPTION_DISABLE_AUTODIAL; STUB\n");
2877 break;
2878 case INTERNET_OPTION_HTTP_DECODING:
2879 {
2880 if (!lpwhh)
2881 {
2882 SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2883 return FALSE;
2884 }
2885 if (!lpBuffer || dwBufferLength != sizeof(BOOL))
2886 {
2887 SetLastError(ERROR_INVALID_PARAMETER);
2888 ret = FALSE;
2889 }
2890 else
2891 lpwhh->decoding = *(BOOL *)lpBuffer;
2892 break;
2893 }
2894 case INTERNET_OPTION_COOKIES_3RD_PARTY:
2895 FIXME("INTERNET_OPTION_COOKIES_3RD_PARTY; STUB\n");
2896 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2897 ret = FALSE;
2898 break;
2899 case INTERNET_OPTION_SEND_UTF8_SERVERNAME_TO_PROXY:
2900 FIXME("INTERNET_OPTION_SEND_UTF8_SERVERNAME_TO_PROXY; STUB\n");
2901 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2902 ret = FALSE;
2903 break;
2904 case INTERNET_OPTION_CODEPAGE_PATH:
2905 FIXME("INTERNET_OPTION_CODEPAGE_PATH; STUB\n");
2906 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2907 ret = FALSE;
2908 break;
2909 case INTERNET_OPTION_CODEPAGE_EXTRA:
2910 FIXME("INTERNET_OPTION_CODEPAGE_EXTRA; STUB\n");
2911 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2912 ret = FALSE;
2913 break;
2914 case INTERNET_OPTION_IDN:
2915 FIXME("INTERNET_OPTION_IDN; STUB\n");
2916 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2917 ret = FALSE;
2918 break;
2919 case INTERNET_OPTION_POLICY:
2920 SetLastError(ERROR_INVALID_PARAMETER);
2921 ret = FALSE;
2922 break;
2923 case INTERNET_OPTION_PER_CONNECTION_OPTION: {
2924 INTERNET_PER_CONN_OPTION_LISTW *con = lpBuffer;
2925 LONG res;
2926 unsigned int i;
2927 proxyinfo_t pi;
2928
2929 if (INTERNET_LoadProxySettings(&pi)) return FALSE;
2930
2931 for (i = 0; i < con->dwOptionCount; i++) {
2932 INTERNET_PER_CONN_OPTIONW *option = con->pOptions + i;
2933
2934 switch (option->dwOption) {
2935 case INTERNET_PER_CONN_PROXY_SERVER:
2936 heap_free(pi.proxy);
2937 pi.proxy = heap_strdupW(option->Value.pszValue);
2938 break;
2939
2940 case INTERNET_PER_CONN_FLAGS:
2941 if(option->Value.dwValue & PROXY_TYPE_PROXY)
2942 pi.proxyEnabled = 1;
2943 else
2944 {
2945 if(option->Value.dwValue != PROXY_TYPE_DIRECT)
2946 FIXME("Unhandled flags: 0x%x\n", option->Value.dwValue);
2947 pi.proxyEnabled = 0;
2948 }
2949 break;
2950
2951 case INTERNET_PER_CONN_PROXY_BYPASS:
2952 heap_free(pi.proxyBypass);
2953 pi.proxyBypass = heap_strdupW(option->Value.pszValue);
2954 break;
2955
2956 case INTERNET_PER_CONN_AUTOCONFIG_URL:
2957 case INTERNET_PER_CONN_AUTODISCOVERY_FLAGS:
2958 case INTERNET_PER_CONN_AUTOCONFIG_SECONDARY_URL:
2959 case INTERNET_PER_CONN_AUTOCONFIG_RELOAD_DELAY_MINS:
2960 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_TIME:
2961 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_URL:
2962 FIXME("Unhandled dwOption %d\n", option->dwOption);
2963 break;
2964
2965 default:
2966 FIXME("Unknown dwOption %d\n", option->dwOption);
2967 SetLastError(ERROR_INVALID_PARAMETER);
2968 break;
2969 }
2970 }
2971
2972 if ((res = INTERNET_SaveProxySettings(&pi)))
2973 SetLastError(res);
2974
2975 FreeProxyInfo(&pi);
2976
2977 ret = (res == ERROR_SUCCESS);
2978 break;
2979 }
2980 case INTERNET_OPTION_SETTINGS_CHANGED:
2981 FIXME("INTERNET_OPTION_SETTINGS_CHANGED; STUB\n");
2982 break;
2983 case INTERNET_OPTION_REFRESH:
2984 FIXME("INTERNET_OPTION_REFRESH; STUB\n");
2985 break;
2986 default:
2987 FIXME("Option %d STUB\n",dwOption);
2988 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2989 ret = FALSE;
2990 break;
2991 }
2992
2993 if(lpwhh)
2994 WININET_Release( lpwhh );
2995
2996 return ret;
2997 }
2998
2999
3000 /***********************************************************************
3001 * InternetSetOptionA (WININET.@)
3002 *
3003 * Sets an options on the specified handle.
3004 *
3005 * RETURNS
3006 * TRUE on success
3007 * FALSE on failure
3008 *
3009 */
3010 BOOL WINAPI InternetSetOptionA(HINTERNET hInternet, DWORD dwOption,
3011 LPVOID lpBuffer, DWORD dwBufferLength)
3012 {
3013 LPVOID wbuffer;
3014 DWORD wlen;
3015 BOOL r;
3016
3017 switch( dwOption )
3018 {
3019 case INTERNET_OPTION_PROXY:
3020 {
3021 LPINTERNET_PROXY_INFOA pi = (LPINTERNET_PROXY_INFOA) lpBuffer;
3022 LPINTERNET_PROXY_INFOW piw;
3023 DWORD proxlen, prbylen;
3024 LPWSTR prox, prby;
3025
3026 proxlen = MultiByteToWideChar( CP_ACP, 0, pi->lpszProxy, -1, NULL, 0);
3027 prbylen= MultiByteToWideChar( CP_ACP, 0, pi->lpszProxyBypass, -1, NULL, 0);
3028 wlen = sizeof(*piw) + proxlen + prbylen;
3029 wbuffer = heap_alloc(wlen*sizeof(WCHAR) );
3030 piw = (LPINTERNET_PROXY_INFOW) wbuffer;
3031 piw->dwAccessType = pi->dwAccessType;
3032 prox = (LPWSTR) &piw[1];
3033 prby = &prox[proxlen+1];
3034 MultiByteToWideChar( CP_ACP, 0, pi->lpszProxy, -1, prox, proxlen);
3035 MultiByteToWideChar( CP_ACP, 0, pi->lpszProxyBypass, -1, prby, prbylen);
3036 piw->lpszProxy = prox;
3037 piw->lpszProxyBypass = prby;
3038 }
3039 break;
3040 case INTERNET_OPTION_USER_AGENT:
3041 case INTERNET_OPTION_USERNAME:
3042 case INTERNET_OPTION_PASSWORD:
3043 case INTERNET_OPTION_PROXY_USERNAME:
3044 case INTERNET_OPTION_PROXY_PASSWORD:
3045 wlen = MultiByteToWideChar( CP_ACP, 0, lpBuffer, -1, NULL, 0 );
3046 if (!(wbuffer = heap_alloc( wlen * sizeof(WCHAR) ))) return ERROR_OUTOFMEMORY;
3047 MultiByteToWideChar( CP_ACP, 0, lpBuffer, -1, wbuffer, wlen );
3048 break;
3049 case INTERNET_OPTION_PER_CONNECTION_OPTION: {
3050 unsigned int i;
3051 INTERNET_PER_CONN_OPTION_LISTW *listW;
3052 INTERNET_PER_CONN_OPTION_LISTA *listA = lpBuffer;
3053 wlen = sizeof(INTERNET_PER_CONN_OPTION_LISTW);
3054 wbuffer = heap_alloc(wlen);
3055 listW = wbuffer;
3056
3057 listW->dwSize = sizeof(INTERNET_PER_CONN_OPTION_LISTW);
3058 if (listA->pszConnection)
3059 {
3060 wlen = MultiByteToWideChar( CP_ACP, 0, listA->pszConnection, -1, NULL, 0 );
3061 listW->pszConnection = heap_alloc(wlen*sizeof(WCHAR));
3062 MultiByteToWideChar( CP_ACP, 0, listA->pszConnection, -1, listW->pszConnection, wlen );
3063 }
3064 else
3065 listW->pszConnection = NULL;
3066 listW->dwOptionCount = listA->dwOptionCount;
3067 listW->dwOptionError = listA->dwOptionError;
3068 listW->pOptions = heap_alloc(sizeof(INTERNET_PER_CONN_OPTIONW) * listA->dwOptionCount);
3069
3070 for (i = 0; i < listA->dwOptionCount; ++i) {
3071 INTERNET_PER_CONN_OPTIONA *optA = listA->pOptions + i;
3072 INTERNET_PER_CONN_OPTIONW *optW = listW->pOptions + i;
3073
3074 optW->dwOption = optA->dwOption;
3075
3076 switch (optA->dwOption) {
3077 case INTERNET_PER_CONN_AUTOCONFIG_URL:
3078 case INTERNET_PER_CONN_PROXY_BYPASS:
3079 case INTERNET_PER_CONN_PROXY_SERVER:
3080 case INTERNET_PER_CONN_AUTOCONFIG_SECONDARY_URL:
3081 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_URL:
3082 if (optA->Value.pszValue)
3083 {
3084 wlen = MultiByteToWideChar( CP_ACP, 0, optA->Value.pszValue, -1, NULL, 0 );
3085 optW->Value.pszValue = heap_alloc(wlen*sizeof(WCHAR));
3086 MultiByteToWideChar( CP_ACP, 0, optA->Value.pszValue, -1, optW->Value.pszValue, wlen );
3087 }
3088 else
3089 optW->Value.pszValue = NULL;
3090 break;
3091 case INTERNET_PER_CONN_AUTODISCOVERY_FLAGS:
3092 case INTERNET_PER_CONN_FLAGS:
3093 case INTERNET_PER_CONN_AUTOCONFIG_RELOAD_DELAY_MINS:
3094 optW->Value.dwValue = optA->Value.dwValue;
3095 break;
3096 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_TIME:
3097 optW->Value.ftValue = optA->Value.ftValue;
3098 break;
3099 default:
3100 WARN("Unknown PER_CONN dwOption: %d, guessing at conversion to Wide\n", optA->dwOption);
3101 optW->Value.dwValue = optA->Value.dwValue;
3102 break;
3103 }
3104 }
3105 }
3106 break;
3107 default:
3108 wbuffer = lpBuffer;
3109 wlen = dwBufferLength;
3110 }
3111
3112 r = InternetSetOptionW(hInternet,dwOption, wbuffer, wlen);
3113
3114 if( lpBuffer != wbuffer )
3115 {
3116 if (dwOption == INTERNET_OPTION_PER_CONNECTION_OPTION)
3117 {
3118 INTERNET_PER_CONN_OPTION_LISTW *list = wbuffer;
3119 unsigned int i;
3120 for (i = 0; i < list->dwOptionCount; ++i) {
3121 INTERNET_PER_CONN_OPTIONW *opt = list->pOptions + i;
3122 switch (opt->dwOption) {
3123 case INTERNET_PER_CONN_AUTOCONFIG_URL:
3124 case INTERNET_PER_CONN_PROXY_BYPASS:
3125 case INTERNET_PER_CONN_PROXY_SERVER:
3126 case INTERNET_PER_CONN_AUTOCONFIG_SECONDARY_URL:
3127 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_URL:
3128 heap_free( opt->Value.pszValue );
3129 break;
3130 default:
3131 break;
3132 }
3133 }
3134 heap_free( list->pOptions );
3135 }
3136 heap_free( wbuffer );
3137 }
3138
3139 return r;
3140 }
3141
3142
3143 /***********************************************************************
3144 * InternetSetOptionExA (WININET.@)
3145 */
3146 BOOL WINAPI InternetSetOptionExA(HINTERNET hInternet, DWORD dwOption,
3147 LPVOID lpBuffer, DWORD dwBufferLength, DWORD dwFlags)
3148 {
3149 FIXME("Flags %08x ignored\n", dwFlags);
3150 return InternetSetOptionA( hInternet, dwOption, lpBuffer, dwBufferLength );
3151 }
3152
3153 /***********************************************************************
3154 * InternetSetOptionExW (WININET.@)
3155 */
3156 BOOL WINAPI InternetSetOptionExW(HINTERNET hInternet, DWORD dwOption,
3157 LPVOID lpBuffer, DWORD dwBufferLength, DWORD dwFlags)
3158 {
3159 FIXME("Flags %08x ignored\n", dwFlags);
3160 if( dwFlags & ~ISO_VALID_FLAGS )
3161 {
3162 SetLastError( ERROR_INVALID_PARAMETER );
3163 return FALSE;
3164 }
3165 return InternetSetOptionW( hInternet, dwOption, lpBuffer, dwBufferLength );
3166 }
3167
3168 static const WCHAR WININET_wkday[7][4] =
3169 { { 'S','u','n', 0 }, { 'M','o','n', 0 }, { 'T','u','e', 0 }, { 'W','e','d', 0 },
3170 { 'T','h','u', 0 }, { 'F','r','i', 0 }, { 'S','a','t', 0 } };
3171 static const WCHAR WININET_month[12][4] =
3172 { { 'J','a','n', 0 }, { 'F','e','b', 0 }, { 'M','a','r', 0 }, { 'A','p','r', 0 },
3173 { 'M','a','y', 0 }, { 'J','u','n', 0 }, { 'J','u','l', 0 }, { 'A','u','g', 0 },
3174 { 'S','e','p', 0 }, { 'O','c','t', 0 }, { 'N','o','v', 0 }, { 'D','e','c', 0 } };
3175
3176 /***********************************************************************
3177 * InternetTimeFromSystemTimeA (WININET.@)
3178 */
3179 BOOL WINAPI InternetTimeFromSystemTimeA( const SYSTEMTIME* time, DWORD format, LPSTR string, DWORD size )
3180 {
3181 BOOL ret;
3182 WCHAR stringW[INTERNET_RFC1123_BUFSIZE];
3183
3184 TRACE( "%p 0x%08x %p 0x%08x\n", time, format, string, size );
3185
3186 if (!time || !string || format != INTERNET_RFC1123_FORMAT)
3187 {
3188 SetLastError(ERROR_INVALID_PARAMETER);
3189 return FALSE;
3190 }
3191
3192 if (size < INTERNET_RFC1123_BUFSIZE * sizeof(*string))
3193 {
3194 SetLastError(ERROR_INSUFFICIENT_BUFFER);
3195 return FALSE;
3196 }
3197
3198 ret = InternetTimeFromSystemTimeW( time, format, stringW, sizeof(stringW) );
3199 if (ret) WideCharToMultiByte( CP_ACP, 0, stringW, -1, string, size, NULL, NULL );
3200
3201 return ret;
3202 }
3203
3204 /***********************************************************************
3205 * InternetTimeFromSystemTimeW (WININET.@)
3206 */
3207 BOOL WINAPI InternetTimeFromSystemTimeW( const SYSTEMTIME* time, DWORD format, LPWSTR string, DWORD size )
3208 {
3209 static const WCHAR date[] =
3210 { '%','s',',',' ','%','0','2','d',' ','%','s',' ','%','4','d',' ','%','0',
3211 '2','d',':','%','0','2','d',':','%','0','2','d',' ','G','M','T', 0 };
3212
3213 TRACE( "%p 0x%08x %p 0x%08x\n", time, format, string, size );
3214
3215 if (!time || !string || format != INTERNET_RFC1123_FORMAT)
3216 {
3217 SetLastError(ERROR_INVALID_PARAMETER);
3218 return FALSE;
3219 }
3220
3221 if (size < INTERNET_RFC1123_BUFSIZE * sizeof(*string))
3222 {
3223 SetLastError(ERROR_INSUFFICIENT_BUFFER);
3224 return FALSE;
3225 }
3226
3227 sprintfW( string, date,
3228 WININET_wkday[time->wDayOfWeek],
3229 time->wDay,
3230 WININET_month[time->wMonth - 1],
3231 time->wYear,
3232 time->wHour,
3233 time->wMinute,
3234 time->wSecond );
3235
3236 return TRUE;
3237 }
3238
3239 /***********************************************************************
3240 * InternetTimeToSystemTimeA (WININET.@)
3241 */
3242 BOOL WINAPI InternetTimeToSystemTimeA( LPCSTR string, SYSTEMTIME* time, DWORD reserved )
3243 {
3244 BOOL ret = FALSE;
3245 WCHAR *stringW;
3246
3247 TRACE( "%s %p 0x%08x\n", debugstr_a(string), time, reserved );
3248
3249 stringW = heap_strdupAtoW(string);
3250 if (stringW)
3251 {
3252 ret = InternetTimeToSystemTimeW( stringW, time, reserved );
3253 heap_free( stringW );
3254 }
3255 return ret;
3256 }
3257
3258 /***********************************************************************
3259 * InternetTimeToSystemTimeW (WININET.@)
3260 */
3261 BOOL WINAPI InternetTimeToSystemTimeW( LPCWSTR string, SYSTEMTIME* time, DWORD reserved )
3262 {
3263 unsigned int i;
3264 const WCHAR *s = string;
3265 WCHAR *end;
3266
3267 TRACE( "%s %p 0x%08x\n", debugstr_w(string), time, reserved );
3268
3269 if (!string || !time) return FALSE;
3270
3271 /* Windows does this too */
3272 GetSystemTime( time );
3273
3274 /* Convert an RFC1123 time such as 'Fri, 07 Jan 2005 12:06:35 GMT' into
3275 * a SYSTEMTIME structure.
3276 */
3277
3278 while (*s && !isalphaW( *s )) s++;
3279 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
3280 time->wDayOfWeek = 7;
3281
3282 for (i = 0; i < 7; i++)
3283 {
3284 if (toupperW( WININET_wkday[i][0] ) == toupperW( s[0] ) &&
3285 toupperW( WININET_wkday[i][1] ) == toupperW( s[1] ) &&
3286 toupperW( WININET_wkday[i][2] ) == toupperW( s[2] ) )
3287 {
3288 time->wDayOfWeek = i;
3289 break;
3290 }
3291 }
3292
3293 if (time->wDayOfWeek > 6) return TRUE;
3294 while (*s && !isdigitW( *s )) s++;
3295 time->wDay = strtolW( s, &end, 10 );
3296 s = end;
3297
3298 while (*s && !isalphaW( *s )) s++;
3299 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
3300 time->wMonth = 0;
3301
3302 for (i = 0; i < 12; i++)
3303 {
3304 if (toupperW( WININET_month[i][0]) == toupperW( s[0] ) &&
3305 toupperW( WININET_month[i][1]) == toupperW( s[1] ) &&
3306 toupperW( WININET_month[i][2]) == toupperW( s[2] ) )
3307 {
3308 time->wMonth = i + 1;
3309 break;
3310 }
3311 }
3312 if (time->wMonth == 0) return TRUE;
3313
3314 while (*s && !isdigitW( *s )) s++;
3315 if (*s == '\0') return TRUE;
3316 time->wYear = strtolW( s, &end, 10 );
3317 s = end;
3318
3319 while (*s && !isdigitW( *s )) s++;
3320 if (*s == '\0') return TRUE;
3321 time->wHour = strtolW( s, &end, 10 );
3322 s = end;
3323
3324 while (*s && !isdigitW( *s )) s++;
3325 if (*s == '\0') return TRUE;
3326 time->wMinute = strtolW( s, &end, 10 );
3327 s = end;
3328
3329 while (*s && !isdigitW( *s )) s++;
3330 if (*s == '\0') return TRUE;
3331 time->wSecond = strtolW( s, &end, 10 );
3332 s = end;
3333
3334 time->wMilliseconds = 0;
3335 return TRUE;
3336 }
3337
3338 /***********************************************************************
3339 * InternetCheckConnectionW (WININET.@)
3340 *
3341 * Pings a requested host to check internet connection
3342 *
3343 * RETURNS
3344 * TRUE on success and FALSE on failure. If a failure then
3345 * ERROR_NOT_CONNECTED is placed into GetLastError
3346 *
3347 */
3348 BOOL WINAPI InternetCheckConnectionW( LPCWSTR lpszUrl, DWORD dwFlags, DWORD dwReserved )
3349 {
3350 /*
3351 * this is a kludge which runs the resident ping program and reads the output.
3352 *
3353 * Anyone have a better idea?
3354 */
3355
3356 BOOL rc = FALSE;
3357 static const CHAR ping[] = "ping -c 1 ";
3358 static const CHAR redirect[] = " >/dev/null 2>/dev/null";
3359 WCHAR *host;
3360 DWORD len, host_len;
3361 INTERNET_PORT port;
3362 int status = -1;
3363
3364 FIXME("(%s %x %x)\n", debugstr_w(lpszUrl), dwFlags, dwReserved);
3365
3366 /*
3367 * Crack or set the Address
3368 */
3369 if (lpszUrl == NULL)
3370 {
3371 /*
3372 * According to the doc we are supposed to use the ip for the next
3373 * server in the WnInet internal server database. I have
3374 * no idea what that is or how to get it.
3375 *
3376 * So someone needs to implement this.
3377 */
3378 FIXME("Unimplemented with URL of NULL\n");
3379 return TRUE;
3380 }
3381 else
3382 {
3383 URL_COMPONENTSW components = {sizeof(components)};
3384
3385 components.dwHostNameLength = 1;
3386
3387 if (!InternetCrackUrlW(lpszUrl,0,0,&components))
3388 goto End;
3389
3390 host = components.lpszHostName;
3391 host_len = components.dwHostNameLength;
3392 port = components.nPort;
3393 TRACE("host name: %s port: %d\n",debugstr_wn(host, host_len), port);
3394 }
3395
3396 if (dwFlags & FLAG_ICC_FORCE_CONNECTION)
3397 {
3398 struct sockaddr_storage saddr;
3399 int sa_len = sizeof(saddr);
3400 WCHAR *host_z;
3401 int fd;
3402 BOOL b;
3403
3404 host_z = heap_strndupW(host, host_len);
3405 if (!host_z)
3406 return FALSE;
3407
3408 b = GetAddress(host_z, port, (struct sockaddr *)&saddr, &sa_len, NULL);
3409 heap_free(host_z);
3410 if(!b)
3411 goto End;
3412 init_winsock();
3413 fd = socket(saddr.ss_family, SOCK_STREAM, 0);
3414 if (fd != -1)
3415 {
3416 if (connect(fd, (struct sockaddr *)&saddr, sa_len) == 0)
3417 rc = TRUE;
3418 closesocket(fd);
3419 }
3420 }
3421 else
3422 {
3423 /*
3424 * Build our ping command
3425 */
3426 char *command;
3427
3428 len = WideCharToMultiByte(CP_UNIXCP, 0, host, host_len, NULL, 0, NULL, NULL);
3429 command = heap_alloc(strlen(ping)+len+strlen(redirect)+1);
3430 strcpy(command, ping);
3431 WideCharToMultiByte(CP_UNIXCP, 0, host, host_len, command+sizeof(ping)-1, len, NULL, NULL);
3432 strcpy(command+sizeof(ping)-1+len, redirect);
3433
3434 TRACE("Ping command is : %s\n",command);
3435
3436 status = system(command);
3437 heap_free( command );
3438
3439 TRACE("Ping returned a code of %i\n",status);
3440
3441 /* Ping return code of 0 indicates success */
3442 if (status == 0)
3443 rc = TRUE;
3444 }
3445
3446 End:
3447 if (rc == FALSE)
3448 INTERNET_SetLastError(ERROR_NOT_CONNECTED);
3449
3450 return rc;
3451 }
3452
3453
3454 /***********************************************************************
3455 * InternetCheckConnectionA (WININET.@)
3456 *
3457 * Pings a requested host to check internet connection
3458 *
3459 * RETURNS
3460 * TRUE on success and FALSE on failure. If a failure then
3461 * ERROR_NOT_CONNECTED is placed into GetLastError
3462 *
3463 */
3464 BOOL WINAPI InternetCheckConnectionA(LPCSTR lpszUrl, DWORD dwFlags, DWORD dwReserved)
3465 {
3466 WCHAR *url = NULL;
3467 BOOL rc;
3468
3469 if(lpszUrl) {
3470 url = heap_strdupAtoW(lpszUrl);
3471 if(!url)
3472 return FALSE;
3473 }
3474
3475 rc = InternetCheckConnectionW(url, dwFlags, dwReserved);
3476
3477 heap_free(url);
3478 return rc;
3479 }
3480
3481
3482 /**********************************************************
3483 * INTERNET_InternetOpenUrlW (internal)
3484 *
3485 * Opens an URL
3486 *
3487 * RETURNS
3488 * handle of connection or NULL on failure
3489 */
3490 static HINTERNET INTERNET_InternetOpenUrlW(appinfo_t *hIC, LPCWSTR lpszUrl,
3491 LPCWSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext)
3492 {
3493 URL_COMPONENTSW urlComponents = { sizeof(urlComponents) };
3494 WCHAR *host, *user = NULL, *pass = NULL, *path;
3495 HINTERNET client = NULL, client1 = NULL;
3496 DWORD res;
3497
3498 TRACE("(%p, %s, %s, %08x, %08x, %08lx)\n", hIC, debugstr_w(lpszUrl), debugstr_w(lpszHeaders),
3499 dwHeadersLength, dwFlags, dwContext);
3500
3501 urlComponents.dwHostNameLength = 1;
3502 urlComponents.dwUserNameLength = 1;
3503 urlComponents.dwPasswordLength = 1;
3504 urlComponents.dwUrlPathLength = 1;
3505 urlComponents.dwExtraInfoLength = 1;
3506 if(!InternetCrackUrlW(lpszUrl, strlenW(lpszUrl), 0, &urlComponents))
3507 return NULL;
3508
3509 if ((urlComponents.nScheme == INTERNET_SCHEME_HTTP || urlComponents.nScheme == INTERNET_SCHEME_HTTPS) &&
3510 urlComponents.dwExtraInfoLength)
3511 {
3512 assert(urlComponents.lpszUrlPath + urlComponents.dwUrlPathLength == urlComponents.lpszExtraInfo);
3513 urlComponents.dwUrlPathLength += urlComponents.dwExtraInfoLength;
3514 }
3515
3516 host = heap_strndupW(urlComponents.lpszHostName, urlComponents.dwHostNameLength);
3517 path = heap_strndupW(urlComponents.lpszUrlPath, urlComponents.dwUrlPathLength);
3518 if(urlComponents.dwUserNameLength)
3519 user = heap_strndupW(urlComponents.lpszUserName, urlComponents.dwUserNameLength);
3520 if(urlComponents.dwPasswordLength)
3521 pass = heap_strndupW(urlComponents.lpszPassword, urlComponents.dwPasswordLength);
3522
3523 switch(urlComponents.nScheme) {
3524 case INTERNET_SCHEME_FTP:
3525 client = FTP_Connect(hIC, host, urlComponents.nPort,
3526 user, pass, dwFlags, dwContext, INET_OPENURL);
3527 if(client == NULL)
3528 break;
3529 client1 = FtpOpenFileW(client, path, GENERIC_READ, dwFlags, dwContext);
3530 if(client1 == NULL) {
3531 InternetCloseHandle(client);
3532 break;
3533 }
3534 break;
3535
3536 case INTERNET_SCHEME_HTTP:
3537 case INTERNET_SCHEME_HTTPS: {
3538 static const WCHAR szStars[] = { '*','/','*', 0 };
3539 LPCWSTR accept[2] = { szStars, NULL };
3540
3541 if (urlComponents.nScheme == INTERNET_SCHEME_HTTPS) dwFlags |= INTERNET_FLAG_SECURE;
3542
3543 /* FIXME: should use pointers, not handles, as handles are not thread-safe */
3544 res = HTTP_Connect(hIC, host, urlComponents.nPort,
3545 user, pass, dwFlags, dwContext, INET_OPENURL, &client);
3546 if(res != ERROR_SUCCESS) {
3547 INTERNET_SetLastError(res);
3548 break;
3549 }
3550
3551 client1 = HttpOpenRequestW(client, NULL, path, NULL, NULL, accept, dwFlags, dwContext);
3552 if(client1 == NULL) {
3553 InternetCloseHandle(client);
3554 break;
3555 }
3556 HttpAddRequestHeadersW(client1, lpszHeaders, dwHeadersLength, HTTP_ADDREQ_FLAG_ADD);
3557 if (!HttpSendRequestW(client1, NULL, 0, NULL, 0) &&
3558 GetLastError() != ERROR_IO_PENDING) {
3559 InternetCloseHandle(client1);
3560 client1 = NULL;
3561 break;
3562 }
3563 }
3564 case INTERNET_SCHEME_GOPHER:
3565 /* gopher doesn't seem to be implemented in wine, but it's supposed
3566 * to be supported by InternetOpenUrlA. */
3567 default:
3568 SetLastError(ERROR_INTERNET_UNRECOGNIZED_SCHEME);
3569 break;
3570 }
3571
3572 TRACE(" %p <--\n", client1);
3573
3574 heap_free(host);
3575 heap_free(path);
3576 heap_free(user);
3577 heap_free(pass);
3578 return client1;
3579 }
3580
3581 /**********************************************************
3582 * InternetOpenUrlW (WININET.@)
3583 *
3584 * Opens an URL
3585 *
3586 * RETURNS
3587 * handle of connection or NULL on failure
3588 */
3589 typedef struct {
3590 task_header_t hdr;
3591 WCHAR *url;
3592 WCHAR *headers;
3593 DWORD headers_len;
3594 DWORD flags;
3595 DWORD_PTR context;
3596 } open_url_task_t;
3597
3598 static void AsyncInternetOpenUrlProc(task_header_t *hdr)
3599 {
3600 open_url_task_t *task = (open_url_task_t*)hdr;
3601
3602 TRACE("%p\n", task->hdr.hdr);
3603
3604 INTERNET_InternetOpenUrlW((appinfo_t*)task->hdr.hdr, task->url, task->headers,
3605 task->headers_len, task->flags, task->context);
3606 heap_free(task->url);
3607 heap_free(task->headers);
3608 }
3609
3610 HINTERNET WINAPI InternetOpenUrlW(HINTERNET hInternet, LPCWSTR lpszUrl,
3611 LPCWSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext)
3612 {
3613 HINTERNET ret = NULL;
3614 appinfo_t *hIC = NULL;
3615
3616 if (TRACE_ON(wininet)) {
3617 TRACE("(%p, %s, %s, %08x, %08x, %08lx)\n", hInternet, debugstr_w(lpszUrl), debugstr_w(lpszHeaders),
3618 dwHeadersLength, dwFlags, dwContext);
3619 TRACE(" flags :");
3620 dump_INTERNET_FLAGS(dwFlags);
3621 }
3622
3623 if (!lpszUrl)
3624 {
3625 SetLastError(ERROR_INVALID_PARAMETER);
3626 goto lend;
3627 }
3628
3629 hIC = (appinfo_t*)get_handle_object( hInternet );
3630 if (NULL == hIC || hIC->hdr.htype != WH_HINIT) {
3631 SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
3632 goto lend;
3633 }
3634
3635 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC) {
3636 open_url_task_t *task;
3637
3638 task = alloc_async_task(&hIC->hdr, AsyncInternetOpenUrlProc, sizeof(*task));
3639 task->url = heap_strdupW(lpszUrl);
3640 task->headers = heap_strdupW(lpszHeaders);
3641 task->headers_len = dwHeadersLength;
3642 task->flags = dwFlags;
3643 task->context = dwContext;
3644
3645 INTERNET_AsyncCall(&task->hdr);
3646 SetLastError(ERROR_IO_PENDING);
3647 } else {
3648 ret = INTERNET_InternetOpenUrlW(hIC, lpszUrl, lpszHeaders, dwHeadersLength, dwFlags, dwContext);
3649 }
3650
3651 lend:
3652 if( hIC )
3653 WININET_Release( &hIC->hdr );
3654 TRACE(" %p <--\n", ret);
3655
3656 return ret;
3657 }
3658
3659 /**********************************************************
3660 * InternetOpenUrlA (WININET.@)
3661 *
3662 * Opens an URL
3663 *
3664 * RETURNS
3665 * handle of connection or NULL on failure
3666 */
3667 HINTERNET WINAPI InternetOpenUrlA(HINTERNET hInternet, LPCSTR lpszUrl,
3668 LPCSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext)
3669 {
3670 HINTERNET rc = NULL;
3671 LPWSTR szUrl = NULL;
3672 WCHAR *headers = NULL;
3673
3674 TRACE("\n");
3675
3676 if(lpszUrl) {
3677 szUrl = heap_strdupAtoW(lpszUrl);
3678 if(!szUrl)
3679 return NULL;
3680 }
3681
3682 if(lpszHeaders) {
3683 headers = heap_strndupAtoW(lpszHeaders, dwHeadersLength, &dwHeadersLength);
3684 if(!headers) {
3685 heap_free(szUrl);
3686 return NULL;
3687 }
3688 }
3689
3690 rc = InternetOpenUrlW(hInternet, szUrl, headers, dwHeadersLength, dwFlags, dwContext);
3691
3692 heap_free(szUrl);
3693 heap_free(headers);
3694 return rc;
3695 }
3696
3697
3698 static LPWITHREADERROR INTERNET_AllocThreadError(void)
3699 {
3700 LPWITHREADERROR lpwite = heap_alloc(sizeof(*lpwite));
3701
3702 if (lpwite)
3703 {
3704 lpwite->dwError = 0;
3705 lpwite->response[0] = '\0';
3706 }
3707
3708 if (!TlsSetValue(g_dwTlsErrIndex, lpwite))
3709 {
3710 heap_free(lpwite);
3711 return NULL;
3712 }
3713 return lpwite;
3714 }
3715
3716
3717 /***********************************************************************
3718 * INTERNET_SetLastError (internal)
3719 *
3720 * Set last thread specific error
3721 *
3722 * RETURNS