4cbda9beab67e7afdae3e611656e50e46ff50428
[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 "internet.h"
30
31 typedef struct
32 {
33 DWORD dwError;
34 CHAR response[MAX_REPLY_LEN];
35 } WITHREADERROR, *LPWITHREADERROR;
36
37 static DWORD g_dwTlsErrIndex = TLS_OUT_OF_INDEXES;
38 HMODULE WININET_hModule;
39
40 static CRITICAL_SECTION WININET_cs;
41 static CRITICAL_SECTION_DEBUG WININET_cs_debug =
42 {
43 0, 0, &WININET_cs,
44 { &WININET_cs_debug.ProcessLocksList, &WININET_cs_debug.ProcessLocksList },
45 0, 0, { (DWORD_PTR)(__FILE__ ": WININET_cs") }
46 };
47 static CRITICAL_SECTION WININET_cs = { &WININET_cs_debug, -1, 0, 0, 0, 0 };
48
49 static object_header_t **handle_table;
50 static UINT_PTR next_handle;
51 static UINT_PTR handle_table_size;
52
53 typedef struct
54 {
55 DWORD proxyEnabled;
56 LPWSTR proxy;
57 LPWSTR proxyBypass;
58 LPWSTR proxyUsername;
59 LPWSTR proxyPassword;
60 } proxyinfo_t;
61
62 static ULONG max_conns = 2, max_1_0_conns = 4;
63 static ULONG connect_timeout = 60000;
64
65 static const WCHAR szInternetSettings[] =
66 { 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
67 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
68 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s',0 };
69 static const WCHAR szProxyServer[] = { 'P','r','o','x','y','S','e','r','v','e','r', 0 };
70 static const WCHAR szProxyEnable[] = { 'P','r','o','x','y','E','n','a','b','l','e', 0 };
71 static const WCHAR szProxyOverride[] = { 'P','r','o','x','y','O','v','e','r','r','i','d','e', 0 };
72
73 void *alloc_object(object_header_t *parent, const object_vtbl_t *vtbl, size_t size)
74 {
75 UINT_PTR handle = 0, num;
76 object_header_t *ret;
77 object_header_t **p;
78 BOOL res = TRUE;
79
80 ret = heap_alloc_zero(size);
81 if(!ret)
82 return NULL;
83
84 list_init(&ret->children);
85
86 EnterCriticalSection( &WININET_cs );
87
88 if(!handle_table_size) {
89 num = 16;
90 p = heap_alloc_zero(sizeof(handle_table[0]) * num);
91 if(p) {
92 handle_table = p;
93 handle_table_size = num;
94 next_handle = 1;
95 }else {
96 res = FALSE;
97 }
98 }else if(next_handle == handle_table_size) {
99 num = handle_table_size * 2;
100 p = heap_realloc_zero(handle_table, sizeof(handle_table[0]) * num);
101 if(p) {
102 handle_table = p;
103 handle_table_size = num;
104 }else {
105 res = FALSE;
106 }
107 }
108
109 if(res) {
110 handle = next_handle;
111 if(handle_table[handle])
112 ERR("handle isn't free but should be\n");
113 handle_table[handle] = ret;
114 ret->valid_handle = TRUE;
115
116 while(next_handle < handle_table_size && handle_table[next_handle])
117 next_handle++;
118 }
119
120 LeaveCriticalSection( &WININET_cs );
121
122 if(!res) {
123 heap_free(ret);
124 return NULL;
125 }
126
127 ret->vtbl = vtbl;
128 ret->refs = 1;
129 ret->hInternet = (HINTERNET)handle;
130
131 if(parent) {
132 ret->lpfnStatusCB = parent->lpfnStatusCB;
133 ret->dwInternalFlags = parent->dwInternalFlags & INET_CALLBACKW;
134 }
135
136 return ret;
137 }
138
139 object_header_t *WININET_AddRef( object_header_t *info )
140 {
141 ULONG refs = InterlockedIncrement(&info->refs);
142 TRACE("%p -> refcount = %d\n", info, refs );
143 return info;
144 }
145
146 object_header_t *get_handle_object( HINTERNET hinternet )
147 {
148 object_header_t *info = NULL;
149 UINT_PTR handle = (UINT_PTR) hinternet;
150
151 EnterCriticalSection( &WININET_cs );
152
153 if(handle > 0 && handle < handle_table_size && handle_table[handle] && handle_table[handle]->valid_handle)
154 info = WININET_AddRef(handle_table[handle]);
155
156 LeaveCriticalSection( &WININET_cs );
157
158 TRACE("handle %ld -> %p\n", handle, info);
159
160 return info;
161 }
162
163 static void invalidate_handle(object_header_t *info)
164 {
165 object_header_t *child, *next;
166
167 if(!info->valid_handle)
168 return;
169 info->valid_handle = FALSE;
170
171 /* Free all children as native does */
172 LIST_FOR_EACH_ENTRY_SAFE( child, next, &info->children, object_header_t, entry )
173 {
174 TRACE("invalidating child handle %p for parent %p\n", child->hInternet, info);
175 invalidate_handle( child );
176 }
177
178 WININET_Release(info);
179 }
180
181 BOOL WININET_Release( object_header_t *info )
182 {
183 ULONG refs = InterlockedDecrement(&info->refs);
184 TRACE( "object %p refcount = %d\n", info, refs );
185 if( !refs )
186 {
187 invalidate_handle(info);
188 if ( info->vtbl->CloseConnection )
189 {
190 TRACE( "closing connection %p\n", info);
191 info->vtbl->CloseConnection( info );
192 }
193 /* Don't send a callback if this is a session handle created with InternetOpenUrl */
194 if ((info->htype != WH_HHTTPSESSION && info->htype != WH_HFTPSESSION)
195 || !(info->dwInternalFlags & INET_OPENURL))
196 {
197 INTERNET_SendCallback(info, info->dwContext,
198 INTERNET_STATUS_HANDLE_CLOSING, &info->hInternet,
199 sizeof(HINTERNET));
200 }
201 TRACE( "destroying object %p\n", info);
202 if ( info->htype != WH_HINIT )
203 list_remove( &info->entry );
204 info->vtbl->Destroy( info );
205
206 if(info->hInternet) {
207 UINT_PTR handle = (UINT_PTR)info->hInternet;
208
209 EnterCriticalSection( &WININET_cs );
210
211 handle_table[handle] = NULL;
212 if(next_handle > handle)
213 next_handle = handle;
214
215 LeaveCriticalSection( &WININET_cs );
216 }
217
218 heap_free(info);
219 }
220 return TRUE;
221 }
222
223 /***********************************************************************
224 * DllMain [Internal] Initializes the internal 'WININET.DLL'.
225 *
226 * PARAMS
227 * hinstDLL [I] handle to the DLL's instance
228 * fdwReason [I]
229 * lpvReserved [I] reserved, must be NULL
230 *
231 * RETURNS
232 * Success: TRUE
233 * Failure: FALSE
234 */
235
236 BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
237 {
238 TRACE("%p,%x,%p\n", hinstDLL, fdwReason, lpvReserved);
239
240 switch (fdwReason) {
241 case DLL_PROCESS_ATTACH:
242
243 g_dwTlsErrIndex = TlsAlloc();
244
245 if (g_dwTlsErrIndex == TLS_OUT_OF_INDEXES)
246 return FALSE;
247
248 if(!init_urlcache())
249 {
250 TlsFree(g_dwTlsErrIndex);
251 return FALSE;
252 }
253
254 WININET_hModule = hinstDLL;
255 break;
256
257 case DLL_THREAD_ATTACH:
258 break;
259
260 case DLL_THREAD_DETACH:
261 if (g_dwTlsErrIndex != TLS_OUT_OF_INDEXES)
262 {
263 heap_free(TlsGetValue(g_dwTlsErrIndex));
264 }
265 break;
266
267 case DLL_PROCESS_DETACH:
268 if (lpvReserved) break;
269 collect_connections(COLLECT_CLEANUP);
270 NETCON_unload();
271 free_urlcache();
272 free_cookie();
273
274 if (g_dwTlsErrIndex != TLS_OUT_OF_INDEXES)
275 {
276 heap_free(TlsGetValue(g_dwTlsErrIndex));
277 TlsFree(g_dwTlsErrIndex);
278 }
279 break;
280 }
281 return TRUE;
282 }
283
284 /***********************************************************************
285 * DllInstall (WININET.@)
286 */
287 HRESULT WINAPI DllInstall(BOOL bInstall, LPCWSTR cmdline)
288 {
289 FIXME("(%x %s): stub\n", bInstall, debugstr_w(cmdline));
290 return S_OK;
291 }
292
293 /***********************************************************************
294 * INTERNET_SaveProxySettings
295 *
296 * Stores the proxy settings given by lpwai into the registry
297 *
298 * RETURNS
299 * ERROR_SUCCESS if no error, or error code on fail
300 */
301 static LONG INTERNET_SaveProxySettings( proxyinfo_t *lpwpi )
302 {
303 HKEY key;
304 LONG ret;
305
306 if ((ret = RegOpenKeyW( HKEY_CURRENT_USER, szInternetSettings, &key )))
307 return ret;
308
309 if ((ret = RegSetValueExW( key, szProxyEnable, 0, REG_DWORD, (BYTE*)&lpwpi->proxyEnabled, sizeof(DWORD))))
310 {
311 RegCloseKey( key );
312 return ret;
313 }
314
315 if (lpwpi->proxy)
316 {
317 if ((ret = RegSetValueExW( key, szProxyServer, 0, REG_SZ, (BYTE*)lpwpi->proxy, sizeof(WCHAR) * (lstrlenW(lpwpi->proxy) + 1))))
318 {
319 RegCloseKey( key );
320 return ret;
321 }
322 }
323 else
324 {
325 if ((ret = RegDeleteValueW( key, szProxyServer )) && ret != ERROR_FILE_NOT_FOUND)
326 {
327 RegCloseKey( key );
328 return ret;
329 }
330 }
331
332 RegCloseKey(key);
333 return ERROR_SUCCESS;
334 }
335
336 /***********************************************************************
337 * INTERNET_FindProxyForProtocol
338 *
339 * Searches the proxy string for a proxy of the given protocol.
340 * Returns the found proxy, or the default proxy if none of the given
341 * protocol is found.
342 *
343 * PARAMETERS
344 * szProxy [In] proxy string to search
345 * proto [In] protocol to search for, e.g. "http"
346 * foundProxy [Out] found proxy
347 * foundProxyLen [In/Out] length of foundProxy buffer, in WCHARs
348 *
349 * RETURNS
350 * TRUE if a proxy is found, FALSE if not. If foundProxy is too short,
351 * *foundProxyLen is set to the required size in WCHARs, including the
352 * NULL terminator, and the last error is set to ERROR_INSUFFICIENT_BUFFER.
353 */
354 WCHAR *INTERNET_FindProxyForProtocol(LPCWSTR szProxy, LPCWSTR proto)
355 {
356 WCHAR *ret = NULL;
357 const WCHAR *ptr;
358
359 TRACE("(%s, %s)\n", debugstr_w(szProxy), debugstr_w(proto));
360
361 /* First, look for the specified protocol (proto=scheme://host:port) */
362 for (ptr = szProxy; ptr && *ptr; )
363 {
364 LPCWSTR end, equal;
365
366 if (!(end = strchrW(ptr, ' ')))
367 end = ptr + strlenW(ptr);
368 if ((equal = strchrW(ptr, '=')) && equal < end &&
369 equal - ptr == strlenW(proto) &&
370 !strncmpiW(proto, ptr, strlenW(proto)))
371 {
372 ret = heap_strndupW(equal + 1, end - equal - 1);
373 TRACE("found proxy for %s: %s\n", debugstr_w(proto), debugstr_w(ret));
374 return ret;
375 }
376 if (*end == ' ')
377 ptr = end + 1;
378 else
379 ptr = end;
380 }
381
382 /* It wasn't found: look for no protocol */
383 for (ptr = szProxy; ptr && *ptr; )
384 {
385 LPCWSTR end;
386
387 if (!(end = strchrW(ptr, ' ')))
388 end = ptr + strlenW(ptr);
389 if (!strchrW(ptr, '='))
390 {
391 ret = heap_strndupW(ptr, end - ptr);
392 TRACE("found proxy for %s: %s\n", debugstr_w(proto), debugstr_w(ret));
393 return ret;
394 }
395 if (*end == ' ')
396 ptr = end + 1;
397 else
398 ptr = end;
399 }
400
401 return NULL;
402 }
403
404 /***********************************************************************
405 * InternetInitializeAutoProxyDll (WININET.@)
406 *
407 * Setup the internal proxy
408 *
409 * PARAMETERS
410 * dwReserved
411 *
412 * RETURNS
413 * FALSE on failure
414 *
415 */
416 BOOL WINAPI InternetInitializeAutoProxyDll(DWORD dwReserved)
417 {
418 FIXME("STUB\n");
419 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
420 return FALSE;
421 }
422
423 /***********************************************************************
424 * DetectAutoProxyUrl (WININET.@)
425 *
426 * Auto detect the proxy url
427 *
428 * RETURNS
429 * FALSE on failure
430 *
431 */
432 BOOL WINAPI DetectAutoProxyUrl(LPSTR lpszAutoProxyUrl,
433 DWORD dwAutoProxyUrlLength, DWORD dwDetectFlags)
434 {
435 FIXME("STUB\n");
436 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
437 return FALSE;
438 }
439
440 static void FreeProxyInfo( proxyinfo_t *lpwpi )
441 {
442 heap_free(lpwpi->proxy);
443 heap_free(lpwpi->proxyBypass);
444 heap_free(lpwpi->proxyUsername);
445 heap_free(lpwpi->proxyPassword);
446 }
447
448 static proxyinfo_t *global_proxy;
449
450 static void free_global_proxy( void )
451 {
452 EnterCriticalSection( &WININET_cs );
453 if (global_proxy)
454 {
455 FreeProxyInfo( global_proxy );
456 heap_free( global_proxy );
457 }
458 LeaveCriticalSection( &WININET_cs );
459 }
460
461 static BOOL parse_proxy_url( proxyinfo_t *info, const WCHAR *url )
462 {
463 static const WCHAR fmt[] = {'%','.','*','s',':','%','u',0};
464 URL_COMPONENTSW uc = {sizeof(uc)};
465
466 uc.dwHostNameLength = 1;
467 uc.dwUserNameLength = 1;
468 uc.dwPasswordLength = 1;
469
470 if (!InternetCrackUrlW( url, 0, 0, &uc )) return FALSE;
471 if (!uc.dwHostNameLength)
472 {
473 if (!(info->proxy = heap_strdupW( url ))) return FALSE;
474 info->proxyUsername = NULL;
475 info->proxyPassword = NULL;
476 return TRUE;
477 }
478 if (!(info->proxy = heap_alloc( (uc.dwHostNameLength + 12) * sizeof(WCHAR) ))) return FALSE;
479 sprintfW( info->proxy, fmt, uc.dwHostNameLength, uc.lpszHostName, uc.nPort );
480
481 if (!uc.dwUserNameLength) info->proxyUsername = NULL;
482 else if (!(info->proxyUsername = heap_strndupW( uc.lpszUserName, uc.dwUserNameLength )))
483 {
484 heap_free( info->proxy );
485 return FALSE;
486 }
487 if (!uc.dwPasswordLength) info->proxyPassword = NULL;
488 else if (!(info->proxyPassword = heap_strndupW( uc.lpszPassword, uc.dwPasswordLength )))
489 {
490 heap_free( info->proxyUsername );
491 heap_free( info->proxy );
492 return FALSE;
493 }
494 return TRUE;
495 }
496
497 /***********************************************************************
498 * INTERNET_LoadProxySettings
499 *
500 * Loads proxy information from process-wide global settings, the registry,
501 * or the environment into lpwpi.
502 *
503 * The caller should call FreeProxyInfo when done with lpwpi.
504 *
505 * FIXME:
506 * The proxy may be specified in the form 'http=proxy.my.org'
507 * Presumably that means there can be ftp=ftpproxy.my.org too.
508 */
509 static LONG INTERNET_LoadProxySettings( proxyinfo_t *lpwpi )
510 {
511 HKEY key;
512 DWORD type, len;
513 LPCSTR envproxy;
514 LONG ret;
515
516 memset( lpwpi, 0, sizeof(*lpwpi) );
517
518 EnterCriticalSection( &WININET_cs );
519 if (global_proxy)
520 {
521 lpwpi->proxyEnabled = global_proxy->proxyEnabled;
522 lpwpi->proxy = heap_strdupW( global_proxy->proxy );
523 lpwpi->proxyBypass = heap_strdupW( global_proxy->proxyBypass );
524 }
525 LeaveCriticalSection( &WININET_cs );
526
527 if ((ret = RegOpenKeyW( HKEY_CURRENT_USER, szInternetSettings, &key )))
528 {
529 FreeProxyInfo( lpwpi );
530 return ret;
531 }
532
533 len = sizeof(DWORD);
534 if (RegQueryValueExW( key, szProxyEnable, NULL, &type, (BYTE *)&lpwpi->proxyEnabled, &len ) || type != REG_DWORD)
535 {
536 lpwpi->proxyEnabled = 0;
537 if((ret = RegSetValueExW( key, szProxyEnable, 0, REG_DWORD, (BYTE *)&lpwpi->proxyEnabled, sizeof(DWORD) )))
538 {
539 FreeProxyInfo( lpwpi );
540 RegCloseKey( key );
541 return ret;
542 }
543 }
544
545 if (!(envproxy = getenv( "http_proxy" )) || lpwpi->proxyEnabled)
546 {
547 /* figure out how much memory the proxy setting takes */
548 if (!RegQueryValueExW( key, szProxyServer, NULL, &type, NULL, &len ) && len && (type == REG_SZ))
549 {
550 LPWSTR szProxy, p;
551 static const WCHAR szHttp[] = {'h','t','t','p','=',0};
552
553 if (!(szProxy = heap_alloc(len)))
554 {
555 RegCloseKey( key );
556 FreeProxyInfo( lpwpi );
557 return ERROR_OUTOFMEMORY;
558 }
559 RegQueryValueExW( key, szProxyServer, NULL, &type, (BYTE*)szProxy, &len );
560
561 /* find the http proxy, and strip away everything else */
562 p = strstrW( szProxy, szHttp );
563 if (p)
564 {
565 p += lstrlenW( szHttp );
566 lstrcpyW( szProxy, p );
567 }
568 p = strchrW( szProxy, ';' );
569 if (p) *p = 0;
570
571 FreeProxyInfo( lpwpi );
572 lpwpi->proxy = szProxy;
573 lpwpi->proxyBypass = NULL;
574
575 TRACE("http proxy (from registry) = %s\n", debugstr_w(lpwpi->proxy));
576 }
577 else
578 {
579 TRACE("No proxy server settings in registry.\n");
580 FreeProxyInfo( lpwpi );
581 lpwpi->proxy = NULL;
582 lpwpi->proxyBypass = NULL;
583 }
584 }
585 else if (envproxy)
586 {
587 WCHAR *envproxyW;
588
589 len = MultiByteToWideChar( CP_UNIXCP, 0, envproxy, -1, NULL, 0 );
590 if (!(envproxyW = heap_alloc(len * sizeof(WCHAR))))
591 {
592 RegCloseKey( key );
593 return ERROR_OUTOFMEMORY;
594 }
595 MultiByteToWideChar( CP_UNIXCP, 0, envproxy, -1, envproxyW, len );
596
597 FreeProxyInfo( lpwpi );
598 if (parse_proxy_url( lpwpi, envproxyW ))
599 {
600 TRACE("http proxy (from environment) = %s\n", debugstr_w(lpwpi->proxy));
601 lpwpi->proxyEnabled = 1;
602 lpwpi->proxyBypass = NULL;
603 }
604 else
605 {
606 WARN("failed to parse http_proxy value %s\n", debugstr_w(envproxyW));
607 lpwpi->proxyEnabled = 0;
608 lpwpi->proxy = NULL;
609 lpwpi->proxyBypass = NULL;
610 }
611 heap_free( envproxyW );
612 }
613
614 if (lpwpi->proxyEnabled)
615 {
616 TRACE("Proxy is enabled.\n");
617
618 if (!(envproxy = getenv( "no_proxy" )))
619 {
620 /* figure out how much memory the proxy setting takes */
621 if (!RegQueryValueExW( key, szProxyOverride, NULL, &type, NULL, &len ) && len && (type == REG_SZ))
622 {
623 LPWSTR szProxy;
624
625 if (!(szProxy = heap_alloc(len)))
626 {
627 RegCloseKey( key );
628 return ERROR_OUTOFMEMORY;
629 }
630 RegQueryValueExW( key, szProxyOverride, NULL, &type, (BYTE*)szProxy, &len );
631
632 heap_free( lpwpi->proxyBypass );
633 lpwpi->proxyBypass = szProxy;
634
635 TRACE("http proxy bypass (from registry) = %s\n", debugstr_w(lpwpi->proxyBypass));
636 }
637 else
638 {
639 heap_free( lpwpi->proxyBypass );
640 lpwpi->proxyBypass = NULL;
641
642 TRACE("No proxy bypass server settings in registry.\n");
643 }
644 }
645 else
646 {
647 WCHAR *envproxyW;
648
649 len = MultiByteToWideChar( CP_UNIXCP, 0, envproxy, -1, NULL, 0 );
650 if (!(envproxyW = heap_alloc(len * sizeof(WCHAR))))
651 {
652 RegCloseKey( key );
653 return ERROR_OUTOFMEMORY;
654 }
655 MultiByteToWideChar( CP_UNIXCP, 0, envproxy, -1, envproxyW, len );
656
657 heap_free( lpwpi->proxyBypass );
658 lpwpi->proxyBypass = envproxyW;
659
660 TRACE("http proxy bypass (from environment) = %s\n", debugstr_w(lpwpi->proxyBypass));
661 }
662 }
663 else TRACE("Proxy is disabled.\n");
664
665 RegCloseKey( key );
666 return ERROR_SUCCESS;
667 }
668
669 /***********************************************************************
670 * INTERNET_ConfigureProxy
671 */
672 static BOOL INTERNET_ConfigureProxy( appinfo_t *lpwai )
673 {
674 proxyinfo_t wpi;
675
676 if (INTERNET_LoadProxySettings( &wpi ))
677 return FALSE;
678
679 if (wpi.proxyEnabled)
680 {
681 TRACE("http proxy = %s bypass = %s\n", debugstr_w(wpi.proxy), debugstr_w(wpi.proxyBypass));
682
683 lpwai->accessType = INTERNET_OPEN_TYPE_PROXY;
684 lpwai->proxy = wpi.proxy;
685 lpwai->proxyBypass = wpi.proxyBypass;
686 lpwai->proxyUsername = wpi.proxyUsername;
687 lpwai->proxyPassword = wpi.proxyPassword;
688 return TRUE;
689 }
690
691 lpwai->accessType = INTERNET_OPEN_TYPE_DIRECT;
692 FreeProxyInfo(&wpi);
693 return FALSE;
694 }
695
696 /***********************************************************************
697 * dump_INTERNET_FLAGS
698 *
699 * Helper function to TRACE the internet flags.
700 *
701 * RETURNS
702 * None
703 *
704 */
705 static void dump_INTERNET_FLAGS(DWORD dwFlags)
706 {
707 #define FE(x) { x, #x }
708 static const wininet_flag_info flag[] = {
709 FE(INTERNET_FLAG_RELOAD),
710 FE(INTERNET_FLAG_RAW_DATA),
711 FE(INTERNET_FLAG_EXISTING_CONNECT),
712 FE(INTERNET_FLAG_ASYNC),
713 FE(INTERNET_FLAG_PASSIVE),
714 FE(INTERNET_FLAG_NO_CACHE_WRITE),
715 FE(INTERNET_FLAG_MAKE_PERSISTENT),
716 FE(INTERNET_FLAG_FROM_CACHE),
717 FE(INTERNET_FLAG_SECURE),
718 FE(INTERNET_FLAG_KEEP_CONNECTION),
719 FE(INTERNET_FLAG_NO_AUTO_REDIRECT),
720 FE(INTERNET_FLAG_READ_PREFETCH),
721 FE(INTERNET_FLAG_NO_COOKIES),
722 FE(INTERNET_FLAG_NO_AUTH),
723 FE(INTERNET_FLAG_CACHE_IF_NET_FAIL),
724 FE(INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP),
725 FE(INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTPS),
726 FE(INTERNET_FLAG_IGNORE_CERT_DATE_INVALID),
727 FE(INTERNET_FLAG_IGNORE_CERT_CN_INVALID),
728 FE(INTERNET_FLAG_RESYNCHRONIZE),
729 FE(INTERNET_FLAG_HYPERLINK),
730 FE(INTERNET_FLAG_NO_UI),
731 FE(INTERNET_FLAG_PRAGMA_NOCACHE),
732 FE(INTERNET_FLAG_CACHE_ASYNC),
733 FE(INTERNET_FLAG_FORMS_SUBMIT),
734 FE(INTERNET_FLAG_NEED_FILE),
735 FE(INTERNET_FLAG_TRANSFER_ASCII),
736 FE(INTERNET_FLAG_TRANSFER_BINARY)
737 };
738 #undef FE
739 unsigned int i;
740
741 for (i = 0; i < (sizeof(flag) / sizeof(flag[0])); i++) {
742 if (flag[i].val & dwFlags) {
743 TRACE(" %s", flag[i].name);
744 dwFlags &= ~flag[i].val;
745 }
746 }
747 if (dwFlags)
748 TRACE(" Unknown flags (%08x)\n", dwFlags);
749 else
750 TRACE("\n");
751 }
752
753 /***********************************************************************
754 * INTERNET_CloseHandle (internal)
755 *
756 * Close internet handle
757 *
758 */
759 static VOID APPINFO_Destroy(object_header_t *hdr)
760 {
761 appinfo_t *lpwai = (appinfo_t*)hdr;
762
763 TRACE("%p\n",lpwai);
764
765 heap_free(lpwai->agent);
766 heap_free(lpwai->proxy);
767 heap_free(lpwai->proxyBypass);
768 heap_free(lpwai->proxyUsername);
769 heap_free(lpwai->proxyPassword);
770 }
771
772 static DWORD APPINFO_QueryOption(object_header_t *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
773 {
774 appinfo_t *ai = (appinfo_t*)hdr;
775
776 switch(option) {
777 case INTERNET_OPTION_HANDLE_TYPE:
778 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
779
780 if (*size < sizeof(ULONG))
781 return ERROR_INSUFFICIENT_BUFFER;
782
783 *size = sizeof(DWORD);
784 *(DWORD*)buffer = INTERNET_HANDLE_TYPE_INTERNET;
785 return ERROR_SUCCESS;
786
787 case INTERNET_OPTION_USER_AGENT: {
788 DWORD bufsize;
789
790 TRACE("INTERNET_OPTION_USER_AGENT\n");
791
792 bufsize = *size;
793
794 if (unicode) {
795 DWORD len = ai->agent ? strlenW(ai->agent) : 0;
796
797 *size = (len + 1) * sizeof(WCHAR);
798 if(!buffer || bufsize < *size)
799 return ERROR_INSUFFICIENT_BUFFER;
800
801 if (ai->agent)
802 strcpyW(buffer, ai->agent);
803 else
804 *(WCHAR *)buffer = 0;
805 /* If the buffer is copied, the returned length doesn't include
806 * the NULL terminator.
807 */
808 *size = len;
809 }else {
810 if (ai->agent)
811 *size = WideCharToMultiByte(CP_ACP, 0, ai->agent, -1, NULL, 0, NULL, NULL);
812 else
813 *size = 1;
814 if(!buffer || bufsize < *size)
815 return ERROR_INSUFFICIENT_BUFFER;
816
817 if (ai->agent)
818 WideCharToMultiByte(CP_ACP, 0, ai->agent, -1, buffer, *size, NULL, NULL);
819 else
820 *(char *)buffer = 0;
821 /* If the buffer is copied, the returned length doesn't include
822 * the NULL terminator.
823 */
824 *size -= 1;
825 }
826
827 return ERROR_SUCCESS;
828 }
829
830 case INTERNET_OPTION_PROXY:
831 if(!size) return ERROR_INVALID_PARAMETER;
832 if (unicode) {
833 INTERNET_PROXY_INFOW *pi = (INTERNET_PROXY_INFOW *)buffer;
834 DWORD proxyBytesRequired = 0, proxyBypassBytesRequired = 0;
835 LPWSTR proxy, proxy_bypass;
836
837 if (ai->proxy)
838 proxyBytesRequired = (lstrlenW(ai->proxy) + 1) * sizeof(WCHAR);
839 if (ai->proxyBypass)
840 proxyBypassBytesRequired = (lstrlenW(ai->proxyBypass) + 1) * sizeof(WCHAR);
841 if (!pi || *size < sizeof(INTERNET_PROXY_INFOW) + proxyBytesRequired + proxyBypassBytesRequired)
842 {
843 *size = sizeof(INTERNET_PROXY_INFOW) + proxyBytesRequired + proxyBypassBytesRequired;
844 return ERROR_INSUFFICIENT_BUFFER;
845 }
846 proxy = (LPWSTR)((LPBYTE)buffer + sizeof(INTERNET_PROXY_INFOW));
847 proxy_bypass = (LPWSTR)((LPBYTE)buffer + sizeof(INTERNET_PROXY_INFOW) + proxyBytesRequired);
848
849 pi->dwAccessType = ai->accessType;
850 pi->lpszProxy = NULL;
851 pi->lpszProxyBypass = NULL;
852 if (ai->proxy) {
853 lstrcpyW(proxy, ai->proxy);
854 pi->lpszProxy = proxy;
855 }
856
857 if (ai->proxyBypass) {
858 lstrcpyW(proxy_bypass, ai->proxyBypass);
859 pi->lpszProxyBypass = proxy_bypass;
860 }
861
862 *size = sizeof(INTERNET_PROXY_INFOW) + proxyBytesRequired + proxyBypassBytesRequired;
863 return ERROR_SUCCESS;
864 }else {
865 INTERNET_PROXY_INFOA *pi = (INTERNET_PROXY_INFOA *)buffer;
866 DWORD proxyBytesRequired = 0, proxyBypassBytesRequired = 0;
867 LPSTR proxy, proxy_bypass;
868
869 if (ai->proxy)
870 proxyBytesRequired = WideCharToMultiByte(CP_ACP, 0, ai->proxy, -1, NULL, 0, NULL, NULL);
871 if (ai->proxyBypass)
872 proxyBypassBytesRequired = WideCharToMultiByte(CP_ACP, 0, ai->proxyBypass, -1,
873 NULL, 0, NULL, NULL);
874 if (!pi || *size < sizeof(INTERNET_PROXY_INFOA) + proxyBytesRequired + proxyBypassBytesRequired)
875 {
876 *size = sizeof(INTERNET_PROXY_INFOA) + proxyBytesRequired + proxyBypassBytesRequired;
877 return ERROR_INSUFFICIENT_BUFFER;
878 }
879 proxy = (LPSTR)((LPBYTE)buffer + sizeof(INTERNET_PROXY_INFOA));
880 proxy_bypass = (LPSTR)((LPBYTE)buffer + sizeof(INTERNET_PROXY_INFOA) + proxyBytesRequired);
881
882 pi->dwAccessType = ai->accessType;
883 pi->lpszProxy = NULL;
884 pi->lpszProxyBypass = NULL;
885 if (ai->proxy) {
886 WideCharToMultiByte(CP_ACP, 0, ai->proxy, -1, proxy, proxyBytesRequired, NULL, NULL);
887 pi->lpszProxy = proxy;
888 }
889
890 if (ai->proxyBypass) {
891 WideCharToMultiByte(CP_ACP, 0, ai->proxyBypass, -1, proxy_bypass,
892 proxyBypassBytesRequired, NULL, NULL);
893 pi->lpszProxyBypass = proxy_bypass;
894 }
895
896 *size = sizeof(INTERNET_PROXY_INFOA) + proxyBytesRequired + proxyBypassBytesRequired;
897 return ERROR_SUCCESS;
898 }
899
900 case INTERNET_OPTION_CONNECT_TIMEOUT:
901 TRACE("INTERNET_OPTION_CONNECT_TIMEOUT\n");
902
903 if (*size < sizeof(ULONG))
904 return ERROR_INSUFFICIENT_BUFFER;
905
906 *(ULONG*)buffer = ai->connect_timeout;
907 *size = sizeof(ULONG);
908
909 return ERROR_SUCCESS;
910 }
911
912 return INET_QueryOption(hdr, option, buffer, size, unicode);
913 }
914
915 static DWORD APPINFO_SetOption(object_header_t *hdr, DWORD option, void *buf, DWORD size)
916 {
917 appinfo_t *ai = (appinfo_t*)hdr;
918
919 switch(option) {
920 case INTERNET_OPTION_CONNECT_TIMEOUT:
921 TRACE("INTERNET_OPTION_CONNECT_TIMEOUT\n");
922
923 if(size != sizeof(connect_timeout))
924 return ERROR_INTERNET_BAD_OPTION_LENGTH;
925 if(!*(ULONG*)buf)
926 return ERROR_BAD_ARGUMENTS;
927
928 ai->connect_timeout = *(ULONG*)buf;
929 return ERROR_SUCCESS;
930 case INTERNET_OPTION_USER_AGENT:
931 heap_free(ai->agent);
932 if (!(ai->agent = heap_strdupW(buf))) return ERROR_OUTOFMEMORY;
933 return ERROR_SUCCESS;
934 }
935
936 return INET_SetOption(hdr, option, buf, size);
937 }
938
939 static const object_vtbl_t APPINFOVtbl = {
940 APPINFO_Destroy,
941 NULL,
942 APPINFO_QueryOption,
943 APPINFO_SetOption,
944 NULL,
945 NULL,
946 NULL
947 };
948
949
950 /***********************************************************************
951 * InternetOpenW (WININET.@)
952 *
953 * Per-application initialization of wininet
954 *
955 * RETURNS
956 * HINTERNET on success
957 * NULL on failure
958 *
959 */
960 HINTERNET WINAPI InternetOpenW(LPCWSTR lpszAgent, DWORD dwAccessType,
961 LPCWSTR lpszProxy, LPCWSTR lpszProxyBypass, DWORD dwFlags)
962 {
963 appinfo_t *lpwai = NULL;
964
965 #ifdef __REACTOS__
966 init_winsock();
967 #endif
968 if (TRACE_ON(wininet)) {
969 #define FE(x) { x, #x }
970 static const wininet_flag_info access_type[] = {
971 FE(INTERNET_OPEN_TYPE_PRECONFIG),
972 FE(INTERNET_OPEN_TYPE_DIRECT),
973 FE(INTERNET_OPEN_TYPE_PROXY),
974 FE(INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY)
975 };
976 #undef FE
977 DWORD i;
978 const char *access_type_str = "Unknown";
979
980 TRACE("(%s, %i, %s, %s, %i)\n", debugstr_w(lpszAgent), dwAccessType,
981 debugstr_w(lpszProxy), debugstr_w(lpszProxyBypass), dwFlags);
982 for (i = 0; i < (sizeof(access_type) / sizeof(access_type[0])); i++) {
983 if (access_type[i].val == dwAccessType) {
984 access_type_str = access_type[i].name;
985 break;
986 }
987 }
988 TRACE(" access type : %s\n", access_type_str);
989 TRACE(" flags :");
990 dump_INTERNET_FLAGS(dwFlags);
991 }
992
993 /* Clear any error information */
994 INTERNET_SetLastError(0);
995
996 if((dwAccessType == INTERNET_OPEN_TYPE_PROXY) && !lpszProxy) {
997 SetLastError(ERROR_INVALID_PARAMETER);
998 return NULL;
999 }
1000
1001 lpwai = alloc_object(NULL, &APPINFOVtbl, sizeof(appinfo_t));
1002 if (!lpwai) {
1003 SetLastError(ERROR_OUTOFMEMORY);
1004 return NULL;
1005 }
1006
1007 lpwai->hdr.htype = WH_HINIT;
1008 lpwai->hdr.dwFlags = dwFlags;
1009 lpwai->accessType = dwAccessType;
1010 lpwai->proxyUsername = NULL;
1011 lpwai->proxyPassword = NULL;
1012 lpwai->connect_timeout = connect_timeout;
1013
1014 lpwai->agent = heap_strdupW(lpszAgent);
1015 if(dwAccessType == INTERNET_OPEN_TYPE_PRECONFIG)
1016 INTERNET_ConfigureProxy( lpwai );
1017 else if(dwAccessType == INTERNET_OPEN_TYPE_PROXY) {
1018 lpwai->proxy = heap_strdupW(lpszProxy);
1019 lpwai->proxyBypass = heap_strdupW(lpszProxyBypass);
1020 }
1021
1022 TRACE("returning %p\n", lpwai);
1023
1024 return lpwai->hdr.hInternet;
1025 }
1026
1027
1028 /***********************************************************************
1029 * InternetOpenA (WININET.@)
1030 *
1031 * Per-application initialization of wininet
1032 *
1033 * RETURNS
1034 * HINTERNET on success
1035 * NULL on failure
1036 *
1037 */
1038 HINTERNET WINAPI InternetOpenA(LPCSTR lpszAgent, DWORD dwAccessType,
1039 LPCSTR lpszProxy, LPCSTR lpszProxyBypass, DWORD dwFlags)
1040 {
1041 WCHAR *szAgent, *szProxy, *szBypass;
1042 HINTERNET rc;
1043
1044 TRACE("(%s, 0x%08x, %s, %s, 0x%08x)\n", debugstr_a(lpszAgent),
1045 dwAccessType, debugstr_a(lpszProxy), debugstr_a(lpszProxyBypass), dwFlags);
1046
1047 szAgent = heap_strdupAtoW(lpszAgent);
1048 szProxy = heap_strdupAtoW(lpszProxy);
1049 szBypass = heap_strdupAtoW(lpszProxyBypass);
1050
1051 rc = InternetOpenW(szAgent, dwAccessType, szProxy, szBypass, dwFlags);
1052
1053 heap_free(szAgent);
1054 heap_free(szProxy);
1055 heap_free(szBypass);
1056 return rc;
1057 }
1058
1059 /***********************************************************************
1060 * InternetGetLastResponseInfoA (WININET.@)
1061 *
1062 * Return last wininet error description on the calling thread
1063 *
1064 * RETURNS
1065 * TRUE on success of writing to buffer
1066 * FALSE on failure
1067 *
1068 */
1069 BOOL WINAPI InternetGetLastResponseInfoA(LPDWORD lpdwError,
1070 LPSTR lpszBuffer, LPDWORD lpdwBufferLength)
1071 {
1072 LPWITHREADERROR lpwite = TlsGetValue(g_dwTlsErrIndex);
1073
1074 TRACE("\n");
1075
1076 if (lpwite)
1077 {
1078 *lpdwError = lpwite->dwError;
1079 if (lpwite->dwError)
1080 {
1081 memcpy(lpszBuffer, lpwite->response, *lpdwBufferLength);
1082 *lpdwBufferLength = strlen(lpszBuffer);
1083 }
1084 else
1085 *lpdwBufferLength = 0;
1086 }
1087 else
1088 {
1089 *lpdwError = 0;
1090 *lpdwBufferLength = 0;
1091 }
1092
1093 return TRUE;
1094 }
1095
1096 /***********************************************************************
1097 * InternetGetLastResponseInfoW (WININET.@)
1098 *
1099 * Return last wininet error description on the calling thread
1100 *
1101 * RETURNS
1102 * TRUE on success of writing to buffer
1103 * FALSE on failure
1104 *
1105 */
1106 BOOL WINAPI InternetGetLastResponseInfoW(LPDWORD lpdwError,
1107 LPWSTR lpszBuffer, LPDWORD lpdwBufferLength)
1108 {
1109 LPWITHREADERROR lpwite = TlsGetValue(g_dwTlsErrIndex);
1110
1111 TRACE("\n");
1112
1113 if (lpwite)
1114 {
1115 *lpdwError = lpwite->dwError;
1116 if (lpwite->dwError)
1117 {
1118 memcpy(lpszBuffer, lpwite->response, *lpdwBufferLength);
1119 *lpdwBufferLength = lstrlenW(lpszBuffer);
1120 }
1121 else
1122 *lpdwBufferLength = 0;
1123 }
1124 else
1125 {
1126 *lpdwError = 0;
1127 *lpdwBufferLength = 0;
1128 }
1129
1130 return TRUE;
1131 }
1132
1133 /***********************************************************************
1134 * InternetGetConnectedState (WININET.@)
1135 *
1136 * Return connected state
1137 *
1138 * RETURNS
1139 * TRUE if connected
1140 * if lpdwStatus is not null, return the status (off line,
1141 * modem, lan...) in it.
1142 * FALSE if not connected
1143 */
1144 BOOL WINAPI InternetGetConnectedState(LPDWORD lpdwStatus, DWORD dwReserved)
1145 {
1146 TRACE("(%p, 0x%08x)\n", lpdwStatus, dwReserved);
1147
1148 return InternetGetConnectedStateExW(lpdwStatus, NULL, 0, dwReserved);
1149 }
1150
1151
1152 /***********************************************************************
1153 * InternetGetConnectedStateExW (WININET.@)
1154 *
1155 * Return connected state
1156 *
1157 * PARAMS
1158 *
1159 * lpdwStatus [O] Flags specifying the status of the internet connection.
1160 * lpszConnectionName [O] Pointer to buffer to receive the friendly name of the internet connection.
1161 * dwNameLen [I] Size of the buffer, in characters.
1162 * dwReserved [I] Reserved. Must be set to 0.
1163 *
1164 * RETURNS
1165 * TRUE if connected
1166 * if lpdwStatus is not null, return the status (off line,
1167 * modem, lan...) in it.
1168 * FALSE if not connected
1169 *
1170 * NOTES
1171 * If the system has no available network connections, an empty string is
1172 * stored in lpszConnectionName. If there is a LAN connection, a localized
1173 * "LAN Connection" string is stored. Presumably, if only a dial-up
1174 * connection is available then the name of the dial-up connection is
1175 * returned. Why any application, other than the "Internet Settings" CPL,
1176 * would want to use this function instead of the simpler InternetGetConnectedStateW
1177 * function is beyond me.
1178 */
1179 BOOL WINAPI InternetGetConnectedStateExW(LPDWORD lpdwStatus, LPWSTR lpszConnectionName,
1180 DWORD dwNameLen, DWORD dwReserved)
1181 {
1182 TRACE("(%p, %p, %d, 0x%08x)\n", lpdwStatus, lpszConnectionName, dwNameLen, dwReserved);
1183
1184 /* Must be zero */
1185 if(dwReserved)
1186 return FALSE;
1187
1188 if (lpdwStatus) {
1189 WARN("always returning LAN connection.\n");
1190 *lpdwStatus = INTERNET_CONNECTION_LAN;
1191 }
1192
1193 /* When the buffer size is zero LoadStringW fills the buffer with a pointer to
1194 * the resource, avoid it as we must not change the buffer in this case */
1195 if(lpszConnectionName && dwNameLen) {
1196 *lpszConnectionName = '\0';
1197 LoadStringW(WININET_hModule, IDS_LANCONNECTION, lpszConnectionName, dwNameLen);
1198 }
1199
1200 return TRUE;
1201 }
1202
1203
1204 /***********************************************************************
1205 * InternetGetConnectedStateExA (WININET.@)
1206 */
1207 BOOL WINAPI InternetGetConnectedStateExA(LPDWORD lpdwStatus, LPSTR lpszConnectionName,
1208 DWORD dwNameLen, DWORD dwReserved)
1209 {
1210 LPWSTR lpwszConnectionName = NULL;
1211 BOOL rc;
1212
1213 TRACE("(%p, %p, %d, 0x%08x)\n", lpdwStatus, lpszConnectionName, dwNameLen, dwReserved);
1214
1215 if (lpszConnectionName && dwNameLen > 0)
1216 lpwszConnectionName = heap_alloc(dwNameLen * sizeof(WCHAR));
1217
1218 rc = InternetGetConnectedStateExW(lpdwStatus,lpwszConnectionName, dwNameLen,
1219 dwReserved);
1220 if (rc && lpwszConnectionName)
1221 WideCharToMultiByte(CP_ACP,0,lpwszConnectionName,-1,lpszConnectionName,
1222 dwNameLen, NULL, NULL);
1223
1224 heap_free(lpwszConnectionName);
1225 return rc;
1226 }
1227
1228
1229 /***********************************************************************
1230 * InternetConnectW (WININET.@)
1231 *
1232 * Open a ftp, gopher or http session
1233 *
1234 * RETURNS
1235 * HINTERNET a session handle on success
1236 * NULL on failure
1237 *
1238 */
1239 HINTERNET WINAPI InternetConnectW(HINTERNET hInternet,
1240 LPCWSTR lpszServerName, INTERNET_PORT nServerPort,
1241 LPCWSTR lpszUserName, LPCWSTR lpszPassword,
1242 DWORD dwService, DWORD dwFlags, DWORD_PTR dwContext)
1243 {
1244 appinfo_t *hIC;
1245 HINTERNET rc = NULL;
1246 DWORD res = ERROR_SUCCESS;
1247
1248 TRACE("(%p, %s, %u, %s, %p, %u, %x, %lx)\n", hInternet, debugstr_w(lpszServerName),
1249 nServerPort, debugstr_w(lpszUserName), lpszPassword, dwService, dwFlags, dwContext);
1250
1251 if (!lpszServerName)
1252 {
1253 SetLastError(ERROR_INVALID_PARAMETER);
1254 return NULL;
1255 }
1256
1257 hIC = (appinfo_t*)get_handle_object( hInternet );
1258 if ( (hIC == NULL) || (hIC->hdr.htype != WH_HINIT) )
1259 {
1260 res = ERROR_INVALID_HANDLE;
1261 goto lend;
1262 }
1263
1264 switch (dwService)
1265 {
1266 case INTERNET_SERVICE_FTP:
1267 rc = FTP_Connect(hIC, lpszServerName, nServerPort,
1268 lpszUserName, lpszPassword, dwFlags, dwContext, 0);
1269 if(!rc)
1270 res = INTERNET_GetLastError();
1271 break;
1272
1273 case INTERNET_SERVICE_HTTP:
1274 res = HTTP_Connect(hIC, lpszServerName, nServerPort,
1275 lpszUserName, lpszPassword, dwFlags, dwContext, 0, &rc);
1276 break;
1277
1278 case INTERNET_SERVICE_GOPHER:
1279 default:
1280 break;
1281 }
1282 lend:
1283 if( hIC )
1284 WININET_Release( &hIC->hdr );
1285
1286 TRACE("returning %p\n", rc);
1287 SetLastError(res);
1288 return rc;
1289 }
1290
1291
1292 /***********************************************************************
1293 * InternetConnectA (WININET.@)
1294 *
1295 * Open a ftp, gopher or http session
1296 *
1297 * RETURNS
1298 * HINTERNET a session handle on success
1299 * NULL on failure
1300 *
1301 */
1302 HINTERNET WINAPI InternetConnectA(HINTERNET hInternet,
1303 LPCSTR lpszServerName, INTERNET_PORT nServerPort,
1304 LPCSTR lpszUserName, LPCSTR lpszPassword,
1305 DWORD dwService, DWORD dwFlags, DWORD_PTR dwContext)
1306 {
1307 HINTERNET rc = NULL;
1308 LPWSTR szServerName;
1309 LPWSTR szUserName;
1310 LPWSTR szPassword;
1311
1312 szServerName = heap_strdupAtoW(lpszServerName);
1313 szUserName = heap_strdupAtoW(lpszUserName);
1314 szPassword = heap_strdupAtoW(lpszPassword);
1315
1316 rc = InternetConnectW(hInternet, szServerName, nServerPort,
1317 szUserName, szPassword, dwService, dwFlags, dwContext);
1318
1319 heap_free(szServerName);
1320 heap_free(szUserName);
1321 heap_free(szPassword);
1322 return rc;
1323 }
1324
1325
1326 /***********************************************************************
1327 * InternetFindNextFileA (WININET.@)
1328 *
1329 * Continues a file search from a previous call to FindFirstFile
1330 *
1331 * RETURNS
1332 * TRUE on success
1333 * FALSE on failure
1334 *
1335 */
1336 BOOL WINAPI InternetFindNextFileA(HINTERNET hFind, LPVOID lpvFindData)
1337 {
1338 BOOL ret;
1339 WIN32_FIND_DATAW fd;
1340
1341 ret = InternetFindNextFileW(hFind, lpvFindData?&fd:NULL);
1342 if(lpvFindData)
1343 WININET_find_data_WtoA(&fd, (LPWIN32_FIND_DATAA)lpvFindData);
1344 return ret;
1345 }
1346
1347 /***********************************************************************
1348 * InternetFindNextFileW (WININET.@)
1349 *
1350 * Continues a file search from a previous call to FindFirstFile
1351 *
1352 * RETURNS
1353 * TRUE on success
1354 * FALSE on failure
1355 *
1356 */
1357 BOOL WINAPI InternetFindNextFileW(HINTERNET hFind, LPVOID lpvFindData)
1358 {
1359 object_header_t *hdr;
1360 DWORD res;
1361
1362 TRACE("\n");
1363
1364 hdr = get_handle_object(hFind);
1365 if(!hdr) {
1366 WARN("Invalid handle\n");
1367 SetLastError(ERROR_INVALID_HANDLE);
1368 return FALSE;
1369 }
1370
1371 if(hdr->vtbl->FindNextFileW) {
1372 res = hdr->vtbl->FindNextFileW(hdr, lpvFindData);
1373 }else {
1374 WARN("Handle doesn't support NextFile\n");
1375 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
1376 }
1377
1378 WININET_Release(hdr);
1379
1380 if(res != ERROR_SUCCESS)
1381 SetLastError(res);
1382 return res == ERROR_SUCCESS;
1383 }
1384
1385 /***********************************************************************
1386 * InternetCloseHandle (WININET.@)
1387 *
1388 * Generic close handle function
1389 *
1390 * RETURNS
1391 * TRUE on success
1392 * FALSE on failure
1393 *
1394 */
1395 BOOL WINAPI InternetCloseHandle(HINTERNET hInternet)
1396 {
1397 object_header_t *obj;
1398
1399 TRACE("%p\n", hInternet);
1400
1401 obj = get_handle_object( hInternet );
1402 if (!obj) {
1403 SetLastError(ERROR_INVALID_HANDLE);
1404 return FALSE;
1405 }
1406
1407 invalidate_handle(obj);
1408 WININET_Release(obj);
1409
1410 return TRUE;
1411 }
1412
1413 static BOOL set_url_component(WCHAR **component, DWORD *component_length, const WCHAR *value, DWORD len)
1414 {
1415 TRACE("%s (%d)\n", debugstr_wn(value, len), len);
1416
1417 if (!*component_length)
1418 return TRUE;
1419
1420 if (!*component) {
1421 *(const WCHAR**)component = value;
1422 *component_length = len;
1423 return TRUE;
1424 }
1425
1426 if (*component_length < len+1) {
1427 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1428 return FALSE;
1429 }
1430
1431 *component_length = len;
1432 if(len)
1433 memcpy(*component, value, len*sizeof(WCHAR));
1434 (*component)[len] = 0;
1435 return TRUE;
1436 }
1437
1438 static BOOL set_url_component_WtoA(const WCHAR *comp_w, DWORD length, const WCHAR *url_w, char **comp, DWORD *ret_length,
1439 const char *url_a)
1440 {
1441 size_t size, ret_size = *ret_length;
1442
1443 if (!*ret_length)
1444 return TRUE;
1445 size = WideCharToMultiByte(CP_ACP, 0, comp_w, length, NULL, 0, NULL, NULL);
1446
1447 if (!*comp) {
1448 *comp = comp_w ? (char*)url_a + WideCharToMultiByte(CP_ACP, 0, url_w, comp_w-url_w, NULL, 0, NULL, NULL) : NULL;
1449 *ret_length = size;
1450 return TRUE;
1451 }
1452
1453 if (size+1 > ret_size) {
1454 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1455 *ret_length = size+1;
1456 return FALSE;
1457 }
1458
1459 *ret_length = size;
1460 WideCharToMultiByte(CP_ACP, 0, comp_w, length, *comp, ret_size-1, NULL, NULL);
1461 (*comp)[size] = 0;
1462 return TRUE;
1463 }
1464
1465 static BOOL set_url_component_AtoW(const char *comp_a, DWORD len_a, WCHAR **comp_w, DWORD *len_w, WCHAR **buf)
1466 {
1467 *len_w = len_a;
1468
1469 if(!comp_a) {
1470 *comp_w = NULL;
1471 return TRUE;
1472 }
1473
1474 if(!(*comp_w = *buf = heap_alloc(len_a*sizeof(WCHAR)))) {
1475 SetLastError(ERROR_OUTOFMEMORY);
1476 return FALSE;
1477 }
1478
1479 return TRUE;
1480 }
1481
1482 /***********************************************************************
1483 * InternetCrackUrlA (WININET.@)
1484 *
1485 * See InternetCrackUrlW.
1486 */
1487 BOOL WINAPI InternetCrackUrlA(const char *url, DWORD url_length, DWORD flags, URL_COMPONENTSA *ret_comp)
1488 {
1489 WCHAR *host = NULL, *user = NULL, *pass = NULL, *path = NULL, *scheme = NULL, *extra = NULL;
1490 URL_COMPONENTSW comp;
1491 WCHAR *url_w = NULL;
1492 BOOL ret;
1493
1494 TRACE("(%s %u %x %p)\n", url_length ? debugstr_an(url, url_length) : debugstr_a(url), url_length, flags, ret_comp);
1495
1496 if (!url || !*url || !ret_comp || ret_comp->dwStructSize != sizeof(URL_COMPONENTSA)) {
1497 SetLastError(ERROR_INVALID_PARAMETER);
1498 return FALSE;
1499 }
1500
1501 comp.dwStructSize = sizeof(comp);
1502
1503 ret = set_url_component_AtoW(ret_comp->lpszHostName, ret_comp->dwHostNameLength,
1504 &comp.lpszHostName, &comp.dwHostNameLength, &host)
1505 && set_url_component_AtoW(ret_comp->lpszUserName, ret_comp->dwUserNameLength,
1506 &comp.lpszUserName, &comp.dwUserNameLength, &user)
1507 && set_url_component_AtoW(ret_comp->lpszPassword, ret_comp->dwPasswordLength,
1508 &comp.lpszPassword, &comp.dwPasswordLength, &pass)
1509 && set_url_component_AtoW(ret_comp->lpszUrlPath, ret_comp->dwUrlPathLength,
1510 &comp.lpszUrlPath, &comp.dwUrlPathLength, &path)
1511 && set_url_component_AtoW(ret_comp->lpszScheme, ret_comp->dwSchemeLength,
1512 &comp.lpszScheme, &comp.dwSchemeLength, &scheme)
1513 && set_url_component_AtoW(ret_comp->lpszExtraInfo, ret_comp->dwExtraInfoLength,
1514 &comp.lpszExtraInfo, &comp.dwExtraInfoLength, &extra);
1515
1516 if(ret && !(url_w = heap_strndupAtoW(url, url_length ? url_length : -1, &url_length))) {
1517 SetLastError(ERROR_OUTOFMEMORY);
1518 ret = FALSE;
1519 }
1520
1521 if (ret && (ret = InternetCrackUrlW(url_w, url_length, flags, &comp))) {
1522 ret_comp->nScheme = comp.nScheme;
1523 ret_comp->nPort = comp.nPort;
1524
1525 ret = set_url_component_WtoA(comp.lpszHostName, comp.dwHostNameLength, url_w,
1526 &ret_comp->lpszHostName, &ret_comp->dwHostNameLength, url)
1527 && set_url_component_WtoA(comp.lpszUserName, comp.dwUserNameLength, url_w,
1528 &ret_comp->lpszUserName, &ret_comp->dwUserNameLength, url)
1529 && set_url_component_WtoA(comp.lpszPassword, comp.dwPasswordLength, url_w,
1530 &ret_comp->lpszPassword, &ret_comp->dwPasswordLength, url)
1531 && set_url_component_WtoA(comp.lpszUrlPath, comp.dwUrlPathLength, url_w,
1532 &ret_comp->lpszUrlPath, &ret_comp->dwUrlPathLength, url)
1533 && set_url_component_WtoA(comp.lpszScheme, comp.dwSchemeLength, url_w,
1534 &ret_comp->lpszScheme, &ret_comp->dwSchemeLength, url)
1535 && set_url_component_WtoA(comp.lpszExtraInfo, comp.dwExtraInfoLength, url_w,
1536 &ret_comp->lpszExtraInfo, &ret_comp->dwExtraInfoLength, url);
1537
1538 if(ret)
1539 TRACE("%s: scheme(%s) host(%s) path(%s) extra(%s)\n", debugstr_a(url),
1540 debugstr_an(ret_comp->lpszScheme, ret_comp->dwSchemeLength),
1541 debugstr_an(ret_comp->lpszHostName, ret_comp->dwHostNameLength),
1542 debugstr_an(ret_comp->lpszUrlPath, ret_comp->dwUrlPathLength),
1543 debugstr_an(ret_comp->lpszExtraInfo, ret_comp->dwExtraInfoLength));
1544 }
1545
1546 heap_free(host);
1547 heap_free(user);
1548 heap_free(pass);
1549 heap_free(path);
1550 heap_free(scheme);
1551 heap_free(extra);
1552 heap_free(url_w);
1553 return ret;
1554 }
1555
1556 static const WCHAR url_schemes[][7] =
1557 {
1558 {'f','t','p',0},
1559 {'g','o','p','h','e','r',0},
1560 {'h','t','t','p',0},
1561 {'h','t','t','p','s',0},
1562 {'f','i','l','e',0},
1563 {'n','e','w','s',0},
1564 {'m','a','i','l','t','o',0},
1565 {'r','e','s',0},
1566 };
1567
1568 /***********************************************************************
1569 * GetInternetSchemeW (internal)
1570 *
1571 * Get scheme of url
1572 *
1573 * RETURNS
1574 * scheme on success
1575 * INTERNET_SCHEME_UNKNOWN on failure
1576 *
1577 */
1578 static INTERNET_SCHEME GetInternetSchemeW(LPCWSTR lpszScheme, DWORD nMaxCmp)
1579 {
1580 int i;
1581
1582 TRACE("%s %d\n",debugstr_wn(lpszScheme, nMaxCmp), nMaxCmp);
1583
1584 if(lpszScheme==NULL)
1585 return INTERNET_SCHEME_UNKNOWN;
1586
1587 for (i = 0; i < sizeof(url_schemes)/sizeof(url_schemes[0]); i++)
1588 if (!strncmpiW(lpszScheme, url_schemes[i], nMaxCmp))
1589 return INTERNET_SCHEME_FIRST + i;
1590
1591 return INTERNET_SCHEME_UNKNOWN;
1592 }
1593
1594 /***********************************************************************
1595 * InternetCrackUrlW (WININET.@)
1596 *
1597 * Break up URL into its components
1598 *
1599 * RETURNS
1600 * TRUE on success
1601 * FALSE on failure
1602 */
1603 BOOL WINAPI InternetCrackUrlW(const WCHAR *lpszUrl, DWORD dwUrlLength, DWORD dwFlags, URL_COMPONENTSW *lpUC)
1604 {
1605 /*
1606 * RFC 1808
1607 * <protocol>:[//<net_loc>][/path][;<params>][?<query>][#<fragment>]
1608 *
1609 */
1610 LPCWSTR lpszParam = NULL;
1611 BOOL found_colon = FALSE;
1612 LPCWSTR lpszap;
1613 LPCWSTR lpszcp = NULL, lpszNetLoc;
1614
1615 TRACE("(%s %u %x %p)\n",
1616 lpszUrl ? debugstr_wn(lpszUrl, dwUrlLength ? dwUrlLength : strlenW(lpszUrl)) : "(null)",
1617 dwUrlLength, dwFlags, lpUC);
1618
1619 if (!lpszUrl || !*lpszUrl || !lpUC)
1620 {
1621 SetLastError(ERROR_INVALID_PARAMETER);
1622 return FALSE;
1623 }
1624 if (!dwUrlLength) dwUrlLength = strlenW(lpszUrl);
1625
1626 if (dwFlags & ICU_DECODE)
1627 {
1628 WCHAR *url_tmp, *buffer;
1629 DWORD len = dwUrlLength + 1;
1630 BOOL ret;
1631
1632 if (!(url_tmp = heap_strndupW(lpszUrl, dwUrlLength)))
1633 {
1634 SetLastError(ERROR_OUTOFMEMORY);
1635 return FALSE;
1636 }
1637
1638 buffer = url_tmp;
1639 ret = InternetCanonicalizeUrlW(url_tmp, buffer, &len, ICU_DECODE | ICU_NO_ENCODE);
1640 if (!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER)
1641 {
1642 buffer = heap_alloc(len * sizeof(WCHAR));
1643 if (!buffer)
1644 {
1645 SetLastError(ERROR_OUTOFMEMORY);
1646 heap_free(url_tmp);
1647 return FALSE;
1648 }
1649 ret = InternetCanonicalizeUrlW(url_tmp, buffer, &len, ICU_DECODE | ICU_NO_ENCODE);
1650 }
1651 if (ret)
1652 ret = InternetCrackUrlW(buffer, len, dwFlags & ~ICU_DECODE, lpUC);
1653
1654 if (buffer != url_tmp) heap_free(buffer);
1655 heap_free(url_tmp);
1656 return ret;
1657 }
1658 lpszap = lpszUrl;
1659
1660 /* Determine if the URI is absolute. */
1661 while (lpszap - lpszUrl < dwUrlLength)
1662 {
1663 if (isalnumW(*lpszap) || *lpszap == '+' || *lpszap == '.' || *lpszap == '-')
1664 {
1665 lpszap++;
1666 continue;
1667 }
1668 if (*lpszap == ':')
1669 {
1670 found_colon = TRUE;
1671 lpszcp = lpszap;
1672 }
1673 else
1674 {
1675 lpszcp = lpszUrl; /* Relative url */
1676 }
1677
1678 break;
1679 }
1680
1681 if(!found_colon){
1682 SetLastError(ERROR_INTERNET_UNRECOGNIZED_SCHEME);
1683 return FALSE;
1684 }
1685
1686 lpUC->nScheme = INTERNET_SCHEME_UNKNOWN;
1687 lpUC->nPort = INTERNET_INVALID_PORT_NUMBER;
1688
1689 /* Parse <params> */
1690 lpszParam = memchrW(lpszap, '?', dwUrlLength - (lpszap - lpszUrl));
1691 if(!lpszParam)
1692 lpszParam = memchrW(lpszap, '#', dwUrlLength - (lpszap - lpszUrl));
1693
1694 if(!set_url_component(&lpUC->lpszExtraInfo, &lpUC->dwExtraInfoLength,
1695 lpszParam, lpszParam ? dwUrlLength-(lpszParam-lpszUrl) : 0))
1696 return FALSE;
1697
1698
1699 /* Get scheme first. */
1700 lpUC->nScheme = GetInternetSchemeW(lpszUrl, lpszcp - lpszUrl);
1701 if(!set_url_component(&lpUC->lpszScheme, &lpUC->dwSchemeLength, lpszUrl, lpszcp - lpszUrl))
1702 return FALSE;
1703
1704 /* Eat ':' in protocol. */
1705 lpszcp++;
1706
1707 /* double slash indicates the net_loc portion is present */
1708 if ((lpszcp[0] == '/') && (lpszcp[1] == '/'))
1709 {
1710 lpszcp += 2;
1711
1712 lpszNetLoc = memchrW(lpszcp, '/', dwUrlLength - (lpszcp - lpszUrl));
1713 if (lpszParam)
1714 {
1715 if (lpszNetLoc)
1716 lpszNetLoc = min(lpszNetLoc, lpszParam);
1717 else
1718 lpszNetLoc = lpszParam;
1719 }
1720 else if (!lpszNetLoc)
1721 lpszNetLoc = lpszcp + dwUrlLength-(lpszcp-lpszUrl);
1722
1723 /* Parse net-loc */
1724 if (lpszNetLoc)
1725 {
1726 LPCWSTR lpszHost;
1727 LPCWSTR lpszPort;
1728
1729 /* [<user>[<:password>]@]<host>[:<port>] */
1730 /* First find the user and password if they exist */
1731
1732 lpszHost = memchrW(lpszcp, '@', dwUrlLength - (lpszcp - lpszUrl));
1733 if (lpszHost == NULL || lpszHost > lpszNetLoc)
1734 {
1735 /* username and password not specified. */
1736 set_url_component(&lpUC->lpszUserName, &lpUC->dwUserNameLength, NULL, 0);
1737 set_url_component(&lpUC->lpszPassword, &lpUC->dwPasswordLength, NULL, 0);
1738 }
1739 else /* Parse out username and password */
1740 {
1741 LPCWSTR lpszUser = lpszcp;
1742 LPCWSTR lpszPasswd = lpszHost;
1743
1744 while (lpszcp < lpszHost)
1745 {
1746 if (*lpszcp == ':')
1747 lpszPasswd = lpszcp;
1748
1749 lpszcp++;
1750 }
1751
1752 if(!set_url_component(&lpUC->lpszUserName, &lpUC->dwUserNameLength, lpszUser, lpszPasswd - lpszUser))
1753 return FALSE;
1754
1755 if (lpszPasswd != lpszHost)
1756 lpszPasswd++;
1757 if(!set_url_component(&lpUC->lpszPassword, &lpUC->dwPasswordLength,
1758 lpszPasswd == lpszHost ? NULL : lpszPasswd, lpszHost - lpszPasswd))
1759 return FALSE;
1760
1761 lpszcp++; /* Advance to beginning of host */
1762 }
1763
1764 /* Parse <host><:port> */
1765
1766 lpszHost = lpszcp;
1767 lpszPort = lpszNetLoc;
1768
1769 /* special case for res:// URLs: there is no port here, so the host is the
1770 entire string up to the first '/' */
1771 if(lpUC->nScheme==INTERNET_SCHEME_RES)
1772 {
1773 if(!set_url_component(&lpUC->lpszHostName, &lpUC->dwHostNameLength, lpszHost, lpszPort - lpszHost))
1774 return FALSE;
1775 lpszcp=lpszNetLoc;
1776 }
1777 else
1778 {
1779 while (lpszcp < lpszNetLoc)
1780 {
1781 if (*lpszcp == ':')
1782 lpszPort = lpszcp;
1783
1784 lpszcp++;
1785 }
1786
1787 /* If the scheme is "file" and the host is just one letter, it's not a host */
1788 if(lpUC->nScheme==INTERNET_SCHEME_FILE && lpszPort <= lpszHost+1)
1789 {
1790 lpszcp=lpszHost;
1791 set_url_component(&lpUC->lpszHostName, &lpUC->dwHostNameLength, NULL, 0);
1792 }
1793 else
1794 {
1795 if(!set_url_component(&lpUC->lpszHostName, &lpUC->dwHostNameLength, lpszHost, lpszPort - lpszHost))
1796 return FALSE;
1797 if (lpszPort != lpszNetLoc)
1798 lpUC->nPort = atoiW(++lpszPort);
1799 else switch (lpUC->nScheme)
1800 {
1801 case INTERNET_SCHEME_HTTP:
1802 lpUC->nPort = INTERNET_DEFAULT_HTTP_PORT;
1803 break;
1804 case INTERNET_SCHEME_HTTPS:
1805 lpUC->nPort = INTERNET_DEFAULT_HTTPS_PORT;
1806 break;
1807 case INTERNET_SCHEME_FTP:
1808 lpUC->nPort = INTERNET_DEFAULT_FTP_PORT;
1809 break;
1810 case INTERNET_SCHEME_GOPHER:
1811 lpUC->nPort = INTERNET_DEFAULT_GOPHER_PORT;
1812 break;
1813 default:
1814 break;
1815 }
1816 }
1817 }
1818 }
1819 }
1820 else
1821 {
1822 set_url_component(&lpUC->lpszUserName, &lpUC->dwUserNameLength, NULL, 0);
1823 set_url_component(&lpUC->lpszPassword, &lpUC->dwPasswordLength, NULL, 0);
1824 set_url_component(&lpUC->lpszHostName, &lpUC->dwHostNameLength, NULL, 0);
1825 }
1826
1827 /* Here lpszcp points to:
1828 *
1829 * <protocol>:[//<net_loc>][/path][;<params>][?<query>][#<fragment>]
1830 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1831 */
1832 if (lpszcp != 0 && lpszcp - lpszUrl < dwUrlLength && (!lpszParam || lpszcp <= lpszParam))
1833 {
1834 DWORD len;
1835
1836 /* Only truncate the parameter list if it's already been saved
1837 * in lpUC->lpszExtraInfo.
1838 */
1839 if (lpszParam && lpUC->dwExtraInfoLength && lpUC->lpszExtraInfo)
1840 len = lpszParam - lpszcp;
1841 else
1842 {
1843 /* Leave the parameter list in lpszUrlPath. Strip off any trailing
1844 * newlines if necessary.
1845 */
1846 LPWSTR lpsznewline = memchrW(lpszcp, '\n', dwUrlLength - (lpszcp - lpszUrl));
1847 if (lpsznewline != NULL)
1848 len = lpsznewline - lpszcp;
1849 else
1850 len = dwUrlLength-(lpszcp-lpszUrl);
1851 }
1852 if (lpUC->dwUrlPathLength && lpUC->lpszUrlPath &&
1853 lpUC->nScheme == INTERNET_SCHEME_FILE)
1854 {
1855 WCHAR tmppath[MAX_PATH];
1856 if (*lpszcp == '/')
1857 {
1858 len = MAX_PATH;
1859 PathCreateFromUrlW(lpszUrl, tmppath, &len, 0);
1860 }
1861 else
1862 {
1863 WCHAR *iter;
1864 memcpy(tmppath, lpszcp, len * sizeof(WCHAR));
1865 tmppath[len] = '\0';
1866
1867 iter = tmppath;
1868 while (*iter) {
1869 if (*iter == '/')
1870 *iter = '\\';
1871 ++iter;
1872 }
1873 }
1874 /* if ends in \. or \.. append a backslash */
1875 if (tmppath[len - 1] == '.' &&
1876 (tmppath[len - 2] == '\\' ||
1877 (tmppath[len - 2] == '.' && tmppath[len - 3] == '\\')))
1878 {
1879 if (len < MAX_PATH - 1)
1880 {
1881 tmppath[len] = '\\';
1882 tmppath[len+1] = '\0';
1883 ++len;
1884 }
1885 }
1886 if(!set_url_component(&lpUC->lpszUrlPath, &lpUC->dwUrlPathLength, tmppath, len))
1887 return FALSE;
1888 }
1889 else if(!set_url_component(&lpUC->lpszUrlPath, &lpUC->dwUrlPathLength, lpszcp, len))
1890 return FALSE;
1891 }
1892 else
1893 {
1894 set_url_component(&lpUC->lpszUrlPath, &lpUC->dwUrlPathLength, lpszcp, 0);
1895 }
1896
1897 TRACE("%s: scheme(%s) host(%s) path(%s) extra(%s)\n", debugstr_wn(lpszUrl,dwUrlLength),
1898 debugstr_wn(lpUC->lpszScheme,lpUC->dwSchemeLength),
1899 debugstr_wn(lpUC->lpszHostName,lpUC->dwHostNameLength),
1900 debugstr_wn(lpUC->lpszUrlPath,lpUC->dwUrlPathLength),
1901 debugstr_wn(lpUC->lpszExtraInfo,lpUC->dwExtraInfoLength));
1902
1903 return TRUE;
1904 }
1905
1906 /***********************************************************************
1907 * InternetAttemptConnect (WININET.@)
1908 *
1909 * Attempt to make a connection to the internet
1910 *
1911 * RETURNS
1912 * ERROR_SUCCESS on success
1913 * Error value on failure
1914 *
1915 */
1916 DWORD WINAPI InternetAttemptConnect(DWORD dwReserved)
1917 {
1918 FIXME("Stub\n");
1919 return ERROR_SUCCESS;
1920 }
1921
1922
1923 /***********************************************************************
1924 * convert_url_canonicalization_flags
1925 *
1926 * Helper for InternetCanonicalizeUrl
1927 *
1928 * PARAMS
1929 * dwFlags [I] Flags suitable for InternetCanonicalizeUrl
1930 *
1931 * RETURNS
1932 * Flags suitable for UrlCanonicalize
1933 */
1934 static DWORD convert_url_canonicalization_flags(DWORD dwFlags)
1935 {
1936 DWORD dwUrlFlags = URL_WININET_COMPATIBILITY | URL_ESCAPE_UNSAFE;
1937
1938 if (dwFlags & ICU_BROWSER_MODE) dwUrlFlags |= URL_BROWSER_MODE;
1939 if (dwFlags & ICU_DECODE) dwUrlFlags |= URL_UNESCAPE;
1940 if (dwFlags & ICU_ENCODE_PERCENT) dwUrlFlags |= URL_ESCAPE_PERCENT;
1941 if (dwFlags & ICU_ENCODE_SPACES_ONLY) dwUrlFlags |= URL_ESCAPE_SPACES_ONLY;
1942 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
1943 if (dwFlags & ICU_NO_ENCODE) dwUrlFlags ^= URL_ESCAPE_UNSAFE;
1944 if (dwFlags & ICU_NO_META) dwUrlFlags |= URL_NO_META;
1945
1946 return dwUrlFlags;
1947 }
1948
1949 /***********************************************************************
1950 * InternetCanonicalizeUrlA (WININET.@)
1951 *
1952 * Escape unsafe characters and spaces
1953 *
1954 * RETURNS
1955 * TRUE on success
1956 * FALSE on failure
1957 *
1958 */
1959 BOOL WINAPI InternetCanonicalizeUrlA(LPCSTR lpszUrl, LPSTR lpszBuffer,
1960 LPDWORD lpdwBufferLength, DWORD dwFlags)
1961 {
1962 HRESULT hr;
1963
1964 TRACE("(%s, %p, %p, 0x%08x) buffer length: %d\n", debugstr_a(lpszUrl), lpszBuffer,
1965 lpdwBufferLength, dwFlags, lpdwBufferLength ? *lpdwBufferLength : -1);
1966
1967 dwFlags = convert_url_canonicalization_flags(dwFlags);
1968 hr = UrlCanonicalizeA(lpszUrl, lpszBuffer, lpdwBufferLength, dwFlags);
1969 if (hr == E_POINTER) SetLastError(ERROR_INSUFFICIENT_BUFFER);
1970 if (hr == E_INVALIDARG) SetLastError(ERROR_INVALID_PARAMETER);
1971
1972 return hr == S_OK;
1973 }
1974
1975 /***********************************************************************
1976 * InternetCanonicalizeUrlW (WININET.@)
1977 *
1978 * Escape unsafe characters and spaces
1979 *
1980 * RETURNS
1981 * TRUE on success
1982 * FALSE on failure
1983 *
1984 */
1985 BOOL WINAPI InternetCanonicalizeUrlW(LPCWSTR lpszUrl, LPWSTR lpszBuffer,
1986 LPDWORD lpdwBufferLength, DWORD dwFlags)
1987 {
1988 HRESULT hr;
1989
1990 TRACE("(%s, %p, %p, 0x%08x) buffer length: %d\n", debugstr_w(lpszUrl), lpszBuffer,
1991 lpdwBufferLength, dwFlags, lpdwBufferLength ? *lpdwBufferLength : -1);
1992
1993 dwFlags = convert_url_canonicalization_flags(dwFlags);
1994 hr = UrlCanonicalizeW(lpszUrl, lpszBuffer, lpdwBufferLength, dwFlags);
1995 if (hr == E_POINTER) SetLastError(ERROR_INSUFFICIENT_BUFFER);
1996 if (hr == E_INVALIDARG) SetLastError(ERROR_INVALID_PARAMETER);
1997
1998 return hr == S_OK;
1999 }
2000
2001 /* #################################################### */
2002
2003 static INTERNET_STATUS_CALLBACK set_status_callback(
2004 object_header_t *lpwh, INTERNET_STATUS_CALLBACK callback, BOOL unicode)
2005 {
2006 INTERNET_STATUS_CALLBACK ret;
2007
2008 if (unicode) lpwh->dwInternalFlags |= INET_CALLBACKW;
2009 else lpwh->dwInternalFlags &= ~INET_CALLBACKW;
2010
2011 ret = lpwh->lpfnStatusCB;
2012 lpwh->lpfnStatusCB = callback;
2013
2014 return ret;
2015 }
2016
2017 /***********************************************************************
2018 * InternetSetStatusCallbackA (WININET.@)
2019 *
2020 * Sets up a callback function which is called as progress is made
2021 * during an operation.
2022 *
2023 * RETURNS
2024 * Previous callback or NULL on success
2025 * INTERNET_INVALID_STATUS_CALLBACK on failure
2026 *
2027 */
2028 INTERNET_STATUS_CALLBACK WINAPI InternetSetStatusCallbackA(
2029 HINTERNET hInternet ,INTERNET_STATUS_CALLBACK lpfnIntCB)
2030 {
2031 INTERNET_STATUS_CALLBACK retVal;
2032 object_header_t *lpwh;
2033
2034 TRACE("%p\n", hInternet);
2035
2036 if (!(lpwh = get_handle_object(hInternet)))
2037 return INTERNET_INVALID_STATUS_CALLBACK;
2038
2039 retVal = set_status_callback(lpwh, lpfnIntCB, FALSE);
2040
2041 WININET_Release( lpwh );
2042 return retVal;
2043 }
2044
2045 /***********************************************************************
2046 * InternetSetStatusCallbackW (WININET.@)
2047 *
2048 * Sets up a callback function which is called as progress is made
2049 * during an operation.
2050 *
2051 * RETURNS
2052 * Previous callback or NULL on success
2053 * INTERNET_INVALID_STATUS_CALLBACK on failure
2054 *
2055 */
2056 INTERNET_STATUS_CALLBACK WINAPI InternetSetStatusCallbackW(
2057 HINTERNET hInternet ,INTERNET_STATUS_CALLBACK lpfnIntCB)
2058 {
2059 INTERNET_STATUS_CALLBACK retVal;
2060 object_header_t *lpwh;
2061
2062 TRACE("%p\n", hInternet);
2063
2064 if (!(lpwh = get_handle_object(hInternet)))
2065 return INTERNET_INVALID_STATUS_CALLBACK;
2066
2067 retVal = set_status_callback(lpwh, lpfnIntCB, TRUE);
2068
2069 WININET_Release( lpwh );
2070 return retVal;
2071 }
2072
2073 /***********************************************************************
2074 * InternetSetFilePointer (WININET.@)
2075 */
2076 DWORD WINAPI InternetSetFilePointer(HINTERNET hFile, LONG lDistanceToMove,
2077 PVOID pReserved, DWORD dwMoveContext, DWORD_PTR dwContext)
2078 {
2079 FIXME("(%p %d %p %d %lx): stub\n", hFile, lDistanceToMove, pReserved, dwMoveContext, dwContext);
2080 return FALSE;
2081 }
2082
2083 /***********************************************************************
2084 * InternetWriteFile (WININET.@)
2085 *
2086 * Write data to an open internet file
2087 *
2088 * RETURNS
2089 * TRUE on success
2090 * FALSE on failure
2091 *
2092 */
2093 BOOL WINAPI InternetWriteFile(HINTERNET hFile, LPCVOID lpBuffer,
2094 DWORD dwNumOfBytesToWrite, LPDWORD lpdwNumOfBytesWritten)
2095 {
2096 object_header_t *lpwh;
2097 BOOL res;
2098
2099 TRACE("(%p %p %d %p)\n", hFile, lpBuffer, dwNumOfBytesToWrite, lpdwNumOfBytesWritten);
2100
2101 lpwh = get_handle_object( hFile );
2102 if (!lpwh) {
2103 WARN("Invalid handle\n");
2104 SetLastError(ERROR_INVALID_HANDLE);
2105 return FALSE;
2106 }
2107
2108 if(lpwh->vtbl->WriteFile) {
2109 res = lpwh->vtbl->WriteFile(lpwh, lpBuffer, dwNumOfBytesToWrite, lpdwNumOfBytesWritten);
2110 }else {
2111 WARN("No Writefile method.\n");
2112 res = ERROR_INVALID_HANDLE;
2113 }
2114
2115 WININET_Release( lpwh );
2116
2117 if(res != ERROR_SUCCESS)
2118 SetLastError(res);
2119 return res == ERROR_SUCCESS;
2120 }
2121
2122
2123 /***********************************************************************
2124 * InternetReadFile (WININET.@)
2125 *
2126 * Read data from an open internet file
2127 *
2128 * RETURNS
2129 * TRUE on success
2130 * FALSE on failure
2131 *
2132 */
2133 BOOL WINAPI InternetReadFile(HINTERNET hFile, LPVOID lpBuffer,
2134 DWORD dwNumOfBytesToRead, LPDWORD pdwNumOfBytesRead)
2135 {
2136 object_header_t *hdr;
2137 DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2138
2139 TRACE("%p %p %d %p\n", hFile, lpBuffer, dwNumOfBytesToRead, pdwNumOfBytesRead);
2140
2141 hdr = get_handle_object(hFile);
2142 if (!hdr) {
2143 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
2144 return FALSE;
2145 }
2146
2147 if(hdr->vtbl->ReadFile) {
2148 res = hdr->vtbl->ReadFile(hdr, lpBuffer, dwNumOfBytesToRead, pdwNumOfBytesRead, 0, 0);
2149 if(res == ERROR_IO_PENDING)
2150 *pdwNumOfBytesRead = 0;
2151 }
2152
2153 WININET_Release(hdr);
2154
2155 TRACE("-- %s (%u) (bytes read: %d)\n", res == ERROR_SUCCESS ? "TRUE": "FALSE", res,
2156 pdwNumOfBytesRead ? *pdwNumOfBytesRead : -1);
2157
2158 if(res != ERROR_SUCCESS)
2159 SetLastError(res);
2160 return res == ERROR_SUCCESS;
2161 }
2162
2163 /***********************************************************************
2164 * InternetReadFileExA (WININET.@)
2165 *
2166 * Read data from an open internet file
2167 *
2168 * PARAMS
2169 * hFile [I] Handle returned by InternetOpenUrl or HttpOpenRequest.
2170 * lpBuffersOut [I/O] Buffer.
2171 * dwFlags [I] Flags. See notes.
2172 * dwContext [I] Context for callbacks.
2173 *
2174 * RETURNS
2175 * TRUE on success
2176 * FALSE on failure
2177 *
2178 * NOTES
2179 * The parameter dwFlags include zero or more of the following flags:
2180 *|IRF_ASYNC - Makes the call asynchronous.
2181 *|IRF_SYNC - Makes the call synchronous.
2182 *|IRF_USE_CONTEXT - Forces dwContext to be used.
2183 *|IRF_NO_WAIT - Don't block if the data is not available, just return what is available.
2184 *
2185 * However, in testing IRF_USE_CONTEXT seems to have no effect - dwContext isn't used.
2186 *
2187 * SEE
2188 * InternetOpenUrlA(), HttpOpenRequestA()
2189 */
2190 BOOL WINAPI InternetReadFileExA(HINTERNET hFile, LPINTERNET_BUFFERSA lpBuffersOut,
2191 DWORD dwFlags, DWORD_PTR dwContext)
2192 {
2193 object_header_t *hdr;
2194 DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2195
2196 TRACE("(%p %p 0x%x 0x%lx)\n", hFile, lpBuffersOut, dwFlags, dwContext);
2197
2198 if (lpBuffersOut->dwStructSize != sizeof(*lpBuffersOut)) {
2199 SetLastError(ERROR_INVALID_PARAMETER);
2200 return FALSE;
2201 }
2202
2203 hdr = get_handle_object(hFile);
2204 if (!hdr) {
2205 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
2206 return FALSE;
2207 }
2208
2209 if(hdr->vtbl->ReadFile)
2210 res = hdr->vtbl->ReadFile(hdr, lpBuffersOut->lpvBuffer, lpBuffersOut->dwBufferLength,
2211 &lpBuffersOut->dwBufferLength, dwFlags, dwContext);
2212
2213 WININET_Release(hdr);
2214
2215 TRACE("-- %s (%u, bytes read: %d)\n", res == ERROR_SUCCESS ? "TRUE": "FALSE",
2216 res, lpBuffersOut->dwBufferLength);
2217
2218 if(res != ERROR_SUCCESS)
2219 SetLastError(res);
2220 return res == ERROR_SUCCESS;
2221 }
2222
2223 /***********************************************************************
2224 * InternetReadFileExW (WININET.@)
2225 * SEE
2226 * InternetReadFileExA()
2227 */
2228 BOOL WINAPI InternetReadFileExW(HINTERNET hFile, LPINTERNET_BUFFERSW lpBuffer,
2229 DWORD dwFlags, DWORD_PTR dwContext)
2230 {
2231 object_header_t *hdr;
2232 DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2233
2234 TRACE("(%p %p 0x%x 0x%lx)\n", hFile, lpBuffer, dwFlags, dwContext);
2235
2236 if (!lpBuffer || lpBuffer->dwStructSize != sizeof(*lpBuffer)) {
2237 SetLastError(ERROR_INVALID_PARAMETER);
2238 return FALSE;
2239 }
2240
2241 hdr = get_handle_object(hFile);
2242 if (!hdr) {
2243 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
2244 return FALSE;
2245 }
2246
2247 if(hdr->vtbl->ReadFile)
2248 res = hdr->vtbl->ReadFile(hdr, lpBuffer->lpvBuffer, lpBuffer->dwBufferLength, &lpBuffer->dwBufferLength,
2249 dwFlags, dwContext);
2250
2251 WININET_Release(hdr);
2252
2253 TRACE("-- %s (%u, bytes read: %d)\n", res == ERROR_SUCCESS ? "TRUE": "FALSE",
2254 res, lpBuffer->dwBufferLength);
2255
2256 if(res != ERROR_SUCCESS)
2257 SetLastError(res);
2258 return res == ERROR_SUCCESS;
2259 }
2260
2261 static WCHAR *get_proxy_autoconfig_url(void)
2262 {
2263 #if defined(MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
2264
2265 CFDictionaryRef settings = CFNetworkCopySystemProxySettings();
2266 WCHAR *ret = NULL;
2267 SIZE_T len;
2268 const void *ref;
2269
2270 if (!settings) return NULL;
2271
2272 if (!(ref = CFDictionaryGetValue( settings, kCFNetworkProxiesProxyAutoConfigURLString )))
2273 {
2274 CFRelease( settings );
2275 return NULL;
2276 }
2277 len = CFStringGetLength( ref );
2278 if (len)
2279 ret = heap_alloc( (len+1) * sizeof(WCHAR) );
2280 if (ret)
2281 {
2282 CFStringGetCharacters( ref, CFRangeMake(0, len), ret );
2283 ret[len] = 0;
2284 }
2285 TRACE( "returning %s\n", debugstr_w(ret) );
2286 CFRelease( settings );
2287 return ret;
2288 #else
2289 static int once;
2290 if (!once++) FIXME( "no support on this platform\n" );
2291 return NULL;
2292 #endif
2293 }
2294
2295 static DWORD query_global_option(DWORD option, void *buffer, DWORD *size, BOOL unicode)
2296 {
2297 /* FIXME: This function currently handles more options than it should. Options requiring
2298 * proper handles should be moved to proper functions */
2299 switch(option) {
2300 case INTERNET_OPTION_HTTP_VERSION:
2301 if (*size < sizeof(HTTP_VERSION_INFO))
2302 return ERROR_INSUFFICIENT_BUFFER;
2303
2304 /*
2305 * Presently hardcoded to 1.1
2306 */
2307 ((HTTP_VERSION_INFO*)buffer)->dwMajorVersion = 1;
2308 ((HTTP_VERSION_INFO*)buffer)->dwMinorVersion = 1;
2309 *size = sizeof(HTTP_VERSION_INFO);
2310
2311 return ERROR_SUCCESS;
2312
2313 case INTERNET_OPTION_CONNECTED_STATE:
2314 FIXME("INTERNET_OPTION_CONNECTED_STATE: semi-stub\n");
2315
2316 if (*size < sizeof(ULONG))
2317 return ERROR_INSUFFICIENT_BUFFER;
2318
2319 *(ULONG*)buffer = INTERNET_STATE_CONNECTED;
2320 *size = sizeof(ULONG);
2321
2322 return ERROR_SUCCESS;
2323
2324 case INTERNET_OPTION_PROXY: {
2325 appinfo_t ai;
2326 BOOL ret;
2327
2328 TRACE("Getting global proxy info\n");
2329 memset(&ai, 0, sizeof(appinfo_t));
2330 INTERNET_ConfigureProxy(&ai);
2331
2332 ret = APPINFO_QueryOption(&ai.hdr, INTERNET_OPTION_PROXY, buffer, size, unicode); /* FIXME */
2333 APPINFO_Destroy(&ai.hdr);
2334 return ret;
2335 }
2336
2337 case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2338 TRACE("INTERNET_OPTION_MAX_CONNS_PER_SERVER\n");
2339
2340 if (*size < sizeof(ULONG))
2341 return ERROR_INSUFFICIENT_BUFFER;
2342
2343 *(ULONG*)buffer = max_conns;
2344 *size = sizeof(ULONG);
2345
2346 return ERROR_SUCCESS;
2347
2348 case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2349 TRACE("INTERNET_OPTION_MAX_CONNS_1_0_SERVER\n");
2350
2351 if (*size < sizeof(ULONG))
2352 return ERROR_INSUFFICIENT_BUFFER;
2353
2354 *(ULONG*)buffer = max_1_0_conns;
2355 *size = sizeof(ULONG);
2356
2357 return ERROR_SUCCESS;
2358
2359 case INTERNET_OPTION_SECURITY_FLAGS:
2360 FIXME("INTERNET_OPTION_SECURITY_FLAGS: Stub\n");
2361 return ERROR_SUCCESS;
2362
2363 case INTERNET_OPTION_VERSION: {
2364 static const INTERNET_VERSION_INFO info = { 1, 2 };
2365
2366 TRACE("INTERNET_OPTION_VERSION\n");
2367
2368 if (*size < sizeof(INTERNET_VERSION_INFO))
2369 return ERROR_INSUFFICIENT_BUFFER;
2370
2371 memcpy(buffer, &info, sizeof(info));
2372 *size = sizeof(info);
2373
2374 return ERROR_SUCCESS;
2375 }
2376
2377 case INTERNET_OPTION_PER_CONNECTION_OPTION: {
2378 WCHAR *url;
2379 INTERNET_PER_CONN_OPTION_LISTW *con = buffer;
2380 INTERNET_PER_CONN_OPTION_LISTA *conA = buffer;
2381 DWORD res = ERROR_SUCCESS, i;
2382 proxyinfo_t pi;
2383 LONG ret;
2384
2385 TRACE("Getting global proxy info\n");
2386 if((ret = INTERNET_LoadProxySettings(&pi)))
2387 return ret;
2388
2389 #ifdef __REACTOS__
2390 WARN("INTERNET_OPTION_PER_CONNECTION_OPTION stub\n");
2391 #else
2392 FIXME("INTERNET_OPTION_PER_CONNECTION_OPTION stub\n");
2393 #endif
2394
2395 if (*size < sizeof(INTERNET_PER_CONN_OPTION_LISTW)) {
2396 FreeProxyInfo(&pi);
2397 return ERROR_INSUFFICIENT_BUFFER;
2398 }
2399
2400 url = get_proxy_autoconfig_url();
2401
2402 for (i = 0; i < con->dwOptionCount; i++) {
2403 INTERNET_PER_CONN_OPTIONW *optionW = con->pOptions + i;
2404 INTERNET_PER_CONN_OPTIONA *optionA = conA->pOptions + i;
2405
2406 switch (optionW->dwOption) {
2407 case INTERNET_PER_CONN_FLAGS:
2408 if(pi.proxyEnabled)
2409 optionW->Value.dwValue = PROXY_TYPE_PROXY;
2410 else
2411 optionW->Value.dwValue = PROXY_TYPE_DIRECT;
2412 if (url)
2413 /* native includes PROXY_TYPE_DIRECT even if PROXY_TYPE_PROXY is set */
2414 optionW->Value.dwValue |= PROXY_TYPE_DIRECT|PROXY_TYPE_AUTO_PROXY_URL;
2415 break;
2416
2417 case INTERNET_PER_CONN_PROXY_SERVER:
2418 if (unicode)
2419 optionW->Value.pszValue = heap_strdupW(pi.proxy);
2420 else
2421 optionA->Value.pszValue = heap_strdupWtoA(pi.proxy);
2422 break;
2423
2424 case INTERNET_PER_CONN_PROXY_BYPASS:
2425 if (unicode)
2426 optionW->Value.pszValue = heap_strdupW(pi.proxyBypass);
2427 else
2428 optionA->Value.pszValue = heap_strdupWtoA(pi.proxyBypass);
2429 break;
2430
2431 case INTERNET_PER_CONN_AUTOCONFIG_URL:
2432 if (!url)
2433 optionW->Value.pszValue = NULL;
2434 else if (unicode)
2435 optionW->Value.pszValue = heap_strdupW(url);
2436 else
2437 optionA->Value.pszValue = heap_strdupWtoA(url);
2438 break;
2439
2440 case INTERNET_PER_CONN_AUTODISCOVERY_FLAGS:
2441 optionW->Value.dwValue = AUTO_PROXY_FLAG_ALWAYS_DETECT;
2442 break;
2443
2444 case INTERNET_PER_CONN_AUTOCONFIG_SECONDARY_URL:
2445 case INTERNET_PER_CONN_AUTOCONFIG_RELOAD_DELAY_MINS:
2446 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_TIME:
2447 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_URL:
2448 FIXME("Unhandled dwOption %d\n", optionW->dwOption);
2449 memset(&optionW->Value, 0, sizeof(optionW->Value));
2450 break;
2451
2452 #ifdef __REACTOS__
2453 case INTERNET_PER_CONN_FLAGS_UI:
2454 WARN("Unhandled dwOption %d\n", optionW->dwOption);
2455 break;
2456
2457 #endif
2458 default:
2459 FIXME("Unknown dwOption %d\n", optionW->dwOption);
2460 res = ERROR_INVALID_PARAMETER;
2461 break;
2462 }
2463 }
2464 heap_free(url);
2465 FreeProxyInfo(&pi);
2466
2467 return res;
2468 }
2469 case INTERNET_OPTION_REQUEST_FLAGS:
2470 case INTERNET_OPTION_USER_AGENT:
2471 *size = 0;
2472 return ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2473 case INTERNET_OPTION_POLICY:
2474 return ERROR_INVALID_PARAMETER;
2475 case INTERNET_OPTION_CONNECT_TIMEOUT:
2476 TRACE("INTERNET_OPTION_CONNECT_TIMEOUT\n");
2477
2478 if (*size < sizeof(ULONG))
2479 return ERROR_INSUFFICIENT_BUFFER;
2480
2481 *(ULONG*)buffer = connect_timeout;
2482 *size = sizeof(ULONG);
2483
2484 return ERROR_SUCCESS;
2485 }
2486
2487 FIXME("Stub for %d\n", option);
2488 return ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2489 }
2490
2491 DWORD INET_QueryOption(object_header_t *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
2492 {
2493 switch(option) {
2494 case INTERNET_OPTION_CONTEXT_VALUE:
2495 if (!size)
2496 return ERROR_INVALID_PARAMETER;
2497
2498 if (*size < sizeof(DWORD_PTR)) {
2499 *size = sizeof(DWORD_PTR);
2500 return ERROR_INSUFFICIENT_BUFFER;
2501 }
2502 if (!buffer)
2503 return ERROR_INVALID_PARAMETER;
2504
2505 *(DWORD_PTR *)buffer = hdr->dwContext;
2506 *size = sizeof(DWORD_PTR);
2507 return ERROR_SUCCESS;
2508
2509 case INTERNET_OPTION_REQUEST_FLAGS:
2510 WARN("INTERNET_OPTION_REQUEST_FLAGS\n");
2511 *size = sizeof(DWORD);
2512 return ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2513
2514 case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2515 case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2516 WARN("Called on global option %u\n", option);
2517 return ERROR_INTERNET_INVALID_OPERATION;
2518 }
2519
2520 /* FIXME: we shouldn't call it here */
2521 return query_global_option(option, buffer, size, unicode);
2522 }
2523
2524 /***********************************************************************
2525 * InternetQueryOptionW (WININET.@)
2526 *
2527 * Queries an options on the specified handle
2528 *
2529 * RETURNS
2530 * TRUE on success
2531 * FALSE on failure
2532 *
2533 */
2534 BOOL WINAPI InternetQueryOptionW(HINTERNET hInternet, DWORD dwOption,
2535 LPVOID lpBuffer, LPDWORD lpdwBufferLength)
2536 {
2537 object_header_t *hdr;
2538 DWORD res = ERROR_INVALID_HANDLE;
2539
2540 TRACE("%p %d %p %p\n", hInternet, dwOption, lpBuffer, lpdwBufferLength);
2541
2542 if(hInternet) {
2543 hdr = get_handle_object(hInternet);
2544 if (hdr) {
2545 res = hdr->vtbl->QueryOption(hdr, dwOption, lpBuffer, lpdwBufferLength, TRUE);
2546 WININET_Release(hdr);
2547 }
2548 }else {
2549 res = query_global_option(dwOption, lpBuffer, lpdwBufferLength, TRUE);
2550 }
2551
2552 if(res != ERROR_SUCCESS)
2553 SetLastError(res);
2554 return res == ERROR_SUCCESS;
2555 }
2556
2557 /***********************************************************************
2558 * InternetQueryOptionA (WININET.@)
2559 *
2560 * Queries an options on the specified handle
2561 *
2562 * RETURNS
2563 * TRUE on success
2564 * FALSE on failure
2565 *
2566 */
2567 BOOL WINAPI InternetQueryOptionA(HINTERNET hInternet, DWORD dwOption,
2568 LPVOID lpBuffer, LPDWORD lpdwBufferLength)
2569 {
2570 object_header_t *hdr;
2571 DWORD res = ERROR_INVALID_HANDLE;
2572
2573 TRACE("%p %d %p %p\n", hInternet, dwOption, lpBuffer, lpdwBufferLength);
2574
2575 if(hInternet) {
2576 hdr = get_handle_object(hInternet);
2577 if (hdr) {
2578 res = hdr->vtbl->QueryOption(hdr, dwOption, lpBuffer, lpdwBufferLength, FALSE);
2579 WININET_Release(hdr);
2580 }
2581 }else {
2582 res = query_global_option(dwOption, lpBuffer, lpdwBufferLength, FALSE);
2583 }
2584
2585 if(res != ERROR_SUCCESS)
2586 SetLastError(res);
2587 return res == ERROR_SUCCESS;
2588 }
2589
2590 DWORD INET_SetOption(object_header_t *hdr, DWORD option, void *buf, DWORD size)
2591 {
2592 switch(option) {
2593 case INTERNET_OPTION_CALLBACK:
2594 WARN("Not settable option %u\n", option);
2595 return ERROR_INTERNET_OPTION_NOT_SETTABLE;
2596 case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2597 case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2598 WARN("Called on global option %u\n", option);
2599 return ERROR_INTERNET_INVALID_OPERATION;
2600 }
2601
2602 return ERROR_INTERNET_INVALID_OPTION;
2603 }
2604
2605 static DWORD set_global_option(DWORD option, void *buf, DWORD size)
2606 {
2607 switch(option) {
2608 case INTERNET_OPTION_CALLBACK:
2609 WARN("Not global option %u\n", option);
2610 return ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2611
2612 case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2613 TRACE("INTERNET_OPTION_MAX_CONNS_PER_SERVER\n");
2614
2615 if(size != sizeof(max_conns))
2616 return ERROR_INTERNET_BAD_OPTION_LENGTH;
2617 if(!*(ULONG*)buf)
2618 return ERROR_BAD_ARGUMENTS;
2619
2620 max_conns = *(ULONG*)buf;
2621 return ERROR_SUCCESS;
2622
2623 case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2624 TRACE("INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER\n");
2625
2626 if(size != sizeof(max_1_0_conns))
2627 return ERROR_INTERNET_BAD_OPTION_LENGTH;
2628 if(!*(ULONG*)buf)
2629 return ERROR_BAD_ARGUMENTS;
2630
2631 max_1_0_conns = *(ULONG*)buf;
2632 return ERROR_SUCCESS;
2633
2634 case INTERNET_OPTION_CONNECT_TIMEOUT:
2635 TRACE("INTERNET_OPTION_CONNECT_TIMEOUT\n");
2636
2637 if(size != sizeof(connect_timeout))
2638 return ERROR_INTERNET_BAD_OPTION_LENGTH;
2639 if(!*(ULONG*)buf)
2640 return ERROR_BAD_ARGUMENTS;
2641
2642 connect_timeout = *(ULONG*)buf;
2643 return ERROR_SUCCESS;
2644
2645 case INTERNET_OPTION_SETTINGS_CHANGED:
2646 FIXME("INTERNETOPTION_SETTINGS_CHANGED semi-stub\n");
2647 collect_connections(COLLECT_CONNECTIONS);
2648 return ERROR_SUCCESS;
2649
2650 case INTERNET_OPTION_SUPPRESS_BEHAVIOR:
2651 FIXME("INTERNET_OPTION_SUPPRESS_BEHAVIOR stub\n");
2652
2653 if(size != sizeof(ULONG))
2654 return ERROR_INTERNET_BAD_OPTION_LENGTH;
2655
2656 FIXME("%08x\n", *(ULONG*)buf);
2657 return ERROR_SUCCESS;
2658 }
2659
2660 return ERROR_INTERNET_INVALID_OPTION;
2661 }
2662
2663 /***********************************************************************
2664 * InternetSetOptionW (WININET.@)
2665 *
2666 * Sets an options on the specified handle
2667 *
2668 * RETURNS
2669 * TRUE on success
2670 * FALSE on failure
2671 *
2672 */
2673 BOOL WINAPI InternetSetOptionW(HINTERNET hInternet, DWORD dwOption,
2674 LPVOID lpBuffer, DWORD dwBufferLength)
2675 {
2676 object_header_t *lpwhh;
2677 BOOL ret = TRUE;
2678 DWORD res;
2679
2680 TRACE("(%p %d %p %d)\n", hInternet, dwOption, lpBuffer, dwBufferLength);
2681
2682 lpwhh = (object_header_t*) get_handle_object( hInternet );
2683 if(lpwhh)
2684 res = lpwhh->vtbl->SetOption(lpwhh, dwOption, lpBuffer, dwBufferLength);
2685 else
2686 res = set_global_option(dwOption, lpBuffer, dwBufferLength);
2687
2688 if(res != ERROR_INTERNET_INVALID_OPTION) {
2689 if(lpwhh)
2690 WININET_Release(lpwhh);
2691
2692 if(res != ERROR_SUCCESS)
2693 SetLastError(res);
2694
2695 return res == ERROR_SUCCESS;
2696 }
2697
2698 switch (dwOption)
2699 {
2700 case INTERNET_OPTION_HTTP_VERSION:
2701 {
2702 HTTP_VERSION_INFO* pVersion=(HTTP_VERSION_INFO*)lpBuffer;
2703 FIXME("Option INTERNET_OPTION_HTTP_VERSION(%d,%d): STUB\n",pVersion->dwMajorVersion,pVersion->dwMinorVersion);
2704 }
2705 break;
2706 case INTERNET_OPTION_ERROR_MASK:
2707 {
2708 if(!lpwhh) {
2709 SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2710 return FALSE;
2711 } else if(*(ULONG*)lpBuffer & (~(INTERNET_ERROR_MASK_INSERT_CDROM|
2712 INTERNET_ERROR_MASK_COMBINED_SEC_CERT|
2713 INTERNET_ERROR_MASK_LOGIN_FAILURE_DISPLAY_ENTITY_BODY))) {
2714 SetLastError(ERROR_INVALID_PARAMETER);
2715 ret = FALSE;
2716 } else if(dwBufferLength != sizeof(ULONG)) {
2717 SetLastError(ERROR_INTERNET_BAD_OPTION_LENGTH);
2718 ret = FALSE;
2719 } else
2720 TRACE("INTERNET_OPTION_ERROR_MASK: %x\n", *(ULONG*)lpBuffer);
2721 lpwhh->ErrorMask = *(ULONG*)lpBuffer;
2722 }
2723 break;
2724 case INTERNET_OPTION_PROXY:
2725 {
2726 INTERNET_PROXY_INFOW *info = lpBuffer;
2727
2728 if (!lpBuffer || dwBufferLength < sizeof(INTERNET_PROXY_INFOW))
2729 {
2730 SetLastError(ERROR_INVALID_PARAMETER);
2731 return FALSE;
2732 }
2733 if (!hInternet)
2734 {
2735 EnterCriticalSection( &WININET_cs );
2736 free_global_proxy();
2737 global_proxy = heap_alloc( sizeof(proxyinfo_t) );
2738 if (global_proxy)
2739 {
2740 if (info->dwAccessType == INTERNET_OPEN_TYPE_PROXY)
2741 {
2742 global_proxy->proxyEnabled = 1;
2743 global_proxy->proxy = heap_strdupW( info->lpszProxy );
2744 global_proxy->proxyBypass = heap_strdupW( info->lpszProxyBypass );
2745 }
2746 else
2747 {
2748 global_proxy->proxyEnabled = 0;
2749 global_proxy->proxy = global_proxy->proxyBypass = NULL;
2750 }
2751 }
2752 LeaveCriticalSection( &WININET_cs );
2753 }
2754 else
2755 {
2756 /* In general, each type of object should handle
2757 * INTERNET_OPTION_PROXY directly. This FIXME ensures it doesn't
2758 * get silently dropped.
2759 */
2760 FIXME("INTERNET_OPTION_PROXY unimplemented\n");
2761 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2762 ret = FALSE;
2763 }
2764 break;
2765 }
2766 case INTERNET_OPTION_CODEPAGE:
2767 {
2768 ULONG codepage = *(ULONG *)lpBuffer;
2769 FIXME("Option INTERNET_OPTION_CODEPAGE (%d): STUB\n", codepage);
2770 }
2771 break;
2772 case INTERNET_OPTION_REQUEST_PRIORITY:
2773 {
2774 ULONG priority = *(ULONG *)lpBuffer;
2775 FIXME("Option INTERNET_OPTION_REQUEST_PRIORITY (%d): STUB\n", priority);
2776 }
2777 break;
2778 case INTERNET_OPTION_CONNECT_TIMEOUT:
2779 {
2780 ULONG connecttimeout = *(ULONG *)lpBuffer;
2781 FIXME("Option INTERNET_OPTION_CONNECT_TIMEOUT (%d): STUB\n", connecttimeout);
2782 }
2783 break;
2784 case INTERNET_OPTION_DATA_RECEIVE_TIMEOUT:
2785 {
2786 ULONG receivetimeout = *(ULONG *)lpBuffer;
2787 FIXME("Option INTERNET_OPTION_DATA_RECEIVE_TIMEOUT (%d): STUB\n", receivetimeout);
2788 }
2789 break;
2790 case INTERNET_OPTION_RESET_URLCACHE_SESSION:
2791 FIXME("Option INTERNET_OPTION_RESET_URLCACHE_SESSION: STUB\n");
2792 break;
2793 case INTERNET_OPTION_END_BROWSER_SESSION:
2794 FIXME("Option INTERNET_OPTION_END_BROWSER_SESSION: semi-stub\n");
2795 free_cookie();
2796 break;
2797 case INTERNET_OPTION_CONNECTED_STATE:
2798 FIXME("Option INTERNET_OPTION_CONNECTED_STATE: STUB\n");
2799 break;
2800 case INTERNET_OPTION_DISABLE_PASSPORT_AUTH:
2801 TRACE("Option INTERNET_OPTION_DISABLE_PASSPORT_AUTH: harmless stub, since not enabled\n");
2802 break;
2803 case INTERNET_OPTION_SEND_TIMEOUT:
2804 case INTERNET_OPTION_RECEIVE_TIMEOUT:
2805 case INTERNET_OPTION_DATA_SEND_TIMEOUT:
2806 {
2807 ULONG timeout = *(ULONG *)lpBuffer;
2808 FIXME("INTERNET_OPTION_SEND/RECEIVE_TIMEOUT/DATA_SEND_TIMEOUT %d\n", timeout);
2809 break;
2810 }
2811 case INTERNET_OPTION_CONNECT_RETRIES:
2812 {
2813 ULONG retries = *(ULONG *)lpBuffer;
2814 FIXME("INTERNET_OPTION_CONNECT_RETRIES %d\n", retries);
2815 break;
2816 }
2817 case INTERNET_OPTION_CONTEXT_VALUE:
2818 {
2819 if (!lpwhh)
2820 {
2821 SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2822 return FALSE;
2823 }
2824 if (!lpBuffer || dwBufferLength != sizeof(DWORD_PTR))
2825 {
2826 SetLastError(ERROR_INVALID_PARAMETER);
2827 ret = FALSE;
2828 }
2829 else
2830 lpwhh->dwContext = *(DWORD_PTR *)lpBuffer;
2831 break;
2832 }
2833 case INTERNET_OPTION_SECURITY_FLAGS:
2834 FIXME("Option INTERNET_OPTION_SECURITY_FLAGS; STUB\n");
2835 break;
2836 case INTERNET_OPTION_DISABLE_AUTODIAL:
2837 FIXME("Option INTERNET_OPTION_DISABLE_AUTODIAL; STUB\n");
2838 break;
2839 case INTERNET_OPTION_HTTP_DECODING:
2840 {
2841 if (!lpwhh)
2842 {
2843 SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2844 return FALSE;
2845 }
2846 if (!lpBuffer || dwBufferLength != sizeof(BOOL))
2847 {
2848 SetLastError(ERROR_INVALID_PARAMETER);
2849 ret = FALSE;
2850 }
2851 else
2852 lpwhh->decoding = *(BOOL *)lpBuffer;
2853 break;
2854 }
2855 case INTERNET_OPTION_COOKIES_3RD_PARTY:
2856 FIXME("INTERNET_OPTION_COOKIES_3RD_PARTY; STUB\n");
2857 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2858 ret = FALSE;
2859 break;
2860 case INTERNET_OPTION_SEND_UTF8_SERVERNAME_TO_PROXY:
2861 FIXME("INTERNET_OPTION_SEND_UTF8_SERVERNAME_TO_PROXY; STUB\n");
2862 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2863 ret = FALSE;
2864 break;
2865 case INTERNET_OPTION_CODEPAGE_PATH:
2866 FIXME("INTERNET_OPTION_CODEPAGE_PATH; STUB\n");
2867 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2868 ret = FALSE;
2869 break;
2870 case INTERNET_OPTION_CODEPAGE_EXTRA:
2871 FIXME("INTERNET_OPTION_CODEPAGE_EXTRA; STUB\n");
2872 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2873 ret = FALSE;
2874 break;
2875 case INTERNET_OPTION_IDN:
2876 FIXME("INTERNET_OPTION_IDN; STUB\n");
2877 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2878 ret = FALSE;
2879 break;
2880 case INTERNET_OPTION_POLICY:
2881 SetLastError(ERROR_INVALID_PARAMETER);
2882 ret = FALSE;
2883 break;
2884 case INTERNET_OPTION_PER_CONNECTION_OPTION: {
2885 INTERNET_PER_CONN_OPTION_LISTW *con = lpBuffer;
2886 LONG res;
2887 unsigned int i;
2888 proxyinfo_t pi;
2889
2890 if (INTERNET_LoadProxySettings(&pi)) return FALSE;
2891
2892 for (i = 0; i < con->dwOptionCount; i++) {
2893 INTERNET_PER_CONN_OPTIONW *option = con->pOptions + i;
2894
2895 switch (option->dwOption) {
2896 case INTERNET_PER_CONN_PROXY_SERVER:
2897 heap_free(pi.proxy);
2898 pi.proxy = heap_strdupW(option->Value.pszValue);
2899 break;
2900
2901 case INTERNET_PER_CONN_FLAGS:
2902 if(option->Value.dwValue & PROXY_TYPE_PROXY)
2903 pi.proxyEnabled = 1;
2904 else
2905 {
2906 if(option->Value.dwValue != PROXY_TYPE_DIRECT)
2907 FIXME("Unhandled flags: 0x%x\n", option->Value.dwValue);
2908 pi.proxyEnabled = 0;
2909 }
2910 break;
2911
2912 case INTERNET_PER_CONN_PROXY_BYPASS:
2913 heap_free(pi.proxyBypass);
2914 pi.proxyBypass = heap_strdupW(option->Value.pszValue);
2915 break;
2916
2917 case INTERNET_PER_CONN_AUTOCONFIG_URL:
2918 case INTERNET_PER_CONN_AUTODISCOVERY_FLAGS:
2919 case INTERNET_PER_CONN_AUTOCONFIG_SECONDARY_URL:
2920 case INTERNET_PER_CONN_AUTOCONFIG_RELOAD_DELAY_MINS:
2921 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_TIME:
2922 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_URL:
2923 FIXME("Unhandled dwOption %d\n", option->dwOption);
2924 break;
2925
2926 default:
2927 FIXME("Unknown dwOption %d\n", option->dwOption);
2928 SetLastError(ERROR_INVALID_PARAMETER);
2929 break;
2930 }
2931 }
2932
2933 if ((res = INTERNET_SaveProxySettings(&pi)))
2934 SetLastError(res);
2935
2936 FreeProxyInfo(&pi);
2937
2938 ret = (res == ERROR_SUCCESS);
2939 break;
2940 }
2941 case INTERNET_OPTION_SETTINGS_CHANGED:
2942 FIXME("INTERNET_OPTION_SETTINGS_CHANGED; STUB\n");
2943 break;
2944 case INTERNET_OPTION_REFRESH:
2945 FIXME("INTERNET_OPTION_REFRESH; STUB\n");
2946 break;
2947 default:
2948 FIXME("Option %d STUB\n",dwOption);
2949 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2950 ret = FALSE;
2951 break;
2952 }
2953
2954 if(lpwhh)
2955 WININET_Release( lpwhh );
2956
2957 return ret;
2958 }
2959
2960
2961 /***********************************************************************
2962 * InternetSetOptionA (WININET.@)
2963 *
2964 * Sets an options on the specified handle.
2965 *
2966 * RETURNS
2967 * TRUE on success
2968 * FALSE on failure
2969 *
2970 */
2971 BOOL WINAPI InternetSetOptionA(HINTERNET hInternet, DWORD dwOption,
2972 LPVOID lpBuffer, DWORD dwBufferLength)
2973 {
2974 LPVOID wbuffer;
2975 DWORD wlen;
2976 BOOL r;
2977
2978 switch( dwOption )
2979 {
2980 case INTERNET_OPTION_PROXY:
2981 {
2982 LPINTERNET_PROXY_INFOA pi = (LPINTERNET_PROXY_INFOA) lpBuffer;
2983 LPINTERNET_PROXY_INFOW piw;
2984 DWORD proxlen, prbylen;
2985 LPWSTR prox, prby;
2986
2987 proxlen = MultiByteToWideChar( CP_ACP, 0, pi->lpszProxy, -1, NULL, 0);
2988 prbylen= MultiByteToWideChar( CP_ACP, 0, pi->lpszProxyBypass, -1, NULL, 0);
2989 wlen = sizeof(*piw) + proxlen + prbylen;
2990 wbuffer = heap_alloc(wlen*sizeof(WCHAR) );
2991 piw = (LPINTERNET_PROXY_INFOW) wbuffer;
2992 piw->dwAccessType = pi->dwAccessType;
2993 prox = (LPWSTR) &piw[1];
2994 prby = &prox[proxlen+1];
2995 MultiByteToWideChar( CP_ACP, 0, pi->lpszProxy, -1, prox, proxlen);
2996 MultiByteToWideChar( CP_ACP, 0, pi->lpszProxyBypass, -1, prby, prbylen);
2997 piw->lpszProxy = prox;
2998 piw->lpszProxyBypass = prby;
2999 }
3000 break;
3001 case INTERNET_OPTION_USER_AGENT:
3002 case INTERNET_OPTION_USERNAME:
3003 case INTERNET_OPTION_PASSWORD:
3004 case INTERNET_OPTION_PROXY_USERNAME:
3005 case INTERNET_OPTION_PROXY_PASSWORD:
3006 wlen = MultiByteToWideChar( CP_ACP, 0, lpBuffer, -1, NULL, 0 );
3007 if (!(wbuffer = heap_alloc( wlen * sizeof(WCHAR) ))) return ERROR_OUTOFMEMORY;
3008 MultiByteToWideChar( CP_ACP, 0, lpBuffer, -1, wbuffer, wlen );
3009 break;
3010 case INTERNET_OPTION_PER_CONNECTION_OPTION: {
3011 unsigned int i;
3012 INTERNET_PER_CONN_OPTION_LISTW *listW;
3013 INTERNET_PER_CONN_OPTION_LISTA *listA = lpBuffer;
3014 wlen = sizeof(INTERNET_PER_CONN_OPTION_LISTW);
3015 wbuffer = heap_alloc(wlen);
3016 listW = wbuffer;
3017
3018 listW->dwSize = sizeof(INTERNET_PER_CONN_OPTION_LISTW);
3019 if (listA->pszConnection)
3020 {
3021 wlen = MultiByteToWideChar( CP_ACP, 0, listA->pszConnection, -1, NULL, 0 );
3022 listW->pszConnection = heap_alloc(wlen*sizeof(WCHAR));
3023 MultiByteToWideChar( CP_ACP, 0, listA->pszConnection, -1, listW->pszConnection, wlen );
3024 }
3025 else
3026 listW->pszConnection = NULL;
3027 listW->dwOptionCount = listA->dwOptionCount;
3028 listW->dwOptionError = listA->dwOptionError;
3029 listW->pOptions = heap_alloc(sizeof(INTERNET_PER_CONN_OPTIONW) * listA->dwOptionCount);
3030
3031 for (i = 0; i < listA->dwOptionCount; ++i) {
3032 INTERNET_PER_CONN_OPTIONA *optA = listA->pOptions + i;
3033 INTERNET_PER_CONN_OPTIONW *optW = listW->pOptions + i;
3034
3035 optW->dwOption = optA->dwOption;
3036
3037 switch (optA->dwOption) {
3038 case INTERNET_PER_CONN_AUTOCONFIG_URL:
3039 case INTERNET_PER_CONN_PROXY_BYPASS:
3040 case INTERNET_PER_CONN_PROXY_SERVER:
3041 case INTERNET_PER_CONN_AUTOCONFIG_SECONDARY_URL:
3042 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_URL:
3043 if (optA->Value.pszValue)
3044 {
3045 wlen = MultiByteToWideChar( CP_ACP, 0, optA->Value.pszValue, -1, NULL, 0 );
3046 optW->Value.pszValue = heap_alloc(wlen*sizeof(WCHAR));
3047 MultiByteToWideChar( CP_ACP, 0, optA->Value.pszValue, -1, optW->Value.pszValue, wlen );
3048 }
3049 else
3050 optW->Value.pszValue = NULL;
3051 break;
3052 case INTERNET_PER_CONN_AUTODISCOVERY_FLAGS:
3053 case INTERNET_PER_CONN_FLAGS:
3054 case INTERNET_PER_CONN_AUTOCONFIG_RELOAD_DELAY_MINS:
3055 optW->Value.dwValue = optA->Value.dwValue;
3056 break;
3057 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_TIME:
3058 optW->Value.ftValue = optA->Value.ftValue;
3059 break;
3060 default:
3061 WARN("Unknown PER_CONN dwOption: %d, guessing at conversion to Wide\n", optA->dwOption);
3062 optW->Value.dwValue = optA->Value.dwValue;
3063 break;
3064 }
3065 }
3066 }
3067 break;
3068 default:
3069 wbuffer = lpBuffer;
3070 wlen = dwBufferLength;
3071 }
3072
3073 r = InternetSetOptionW(hInternet,dwOption, wbuffer, wlen);
3074
3075 if( lpBuffer != wbuffer )
3076 {
3077 if (dwOption == INTERNET_OPTION_PER_CONNECTION_OPTION)
3078 {
3079 INTERNET_PER_CONN_OPTION_LISTW *list = wbuffer;
3080 unsigned int i;
3081 for (i = 0; i < list->dwOptionCount; ++i) {
3082 INTERNET_PER_CONN_OPTIONW *opt = list->pOptions + i;
3083 switch (opt->dwOption) {
3084 case INTERNET_PER_CONN_AUTOCONFIG_URL:
3085 case INTERNET_PER_CONN_PROXY_BYPASS:
3086 case INTERNET_PER_CONN_PROXY_SERVER:
3087 case INTERNET_PER_CONN_AUTOCONFIG_SECONDARY_URL:
3088 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_URL:
3089 heap_free( opt->Value.pszValue );
3090 break;
3091 default:
3092 break;
3093 }
3094 }
3095 heap_free( list->pOptions );
3096 }
3097 heap_free( wbuffer );
3098 }
3099
3100 return r;
3101 }
3102
3103
3104 /***********************************************************************
3105 * InternetSetOptionExA (WININET.@)
3106 */
3107 BOOL WINAPI InternetSetOptionExA(HINTERNET hInternet, DWORD dwOption,
3108 LPVOID lpBuffer, DWORD dwBufferLength, DWORD dwFlags)
3109 {
3110 FIXME("Flags %08x ignored\n", dwFlags);
3111 return InternetSetOptionA( hInternet, dwOption, lpBuffer, dwBufferLength );
3112 }
3113
3114 /***********************************************************************
3115 * InternetSetOptionExW (WININET.@)
3116 */
3117 BOOL WINAPI InternetSetOptionExW(HINTERNET hInternet, DWORD dwOption,
3118 LPVOID lpBuffer, DWORD dwBufferLength, DWORD dwFlags)
3119 {
3120 FIXME("Flags %08x ignored\n", dwFlags);
3121 if( dwFlags & ~ISO_VALID_FLAGS )
3122 {
3123 SetLastError( ERROR_INVALID_PARAMETER );
3124 return FALSE;
3125 }
3126 return InternetSetOptionW( hInternet, dwOption, lpBuffer, dwBufferLength );
3127 }
3128
3129 static const WCHAR WININET_wkday[7][4] =
3130 { { 'S','u','n', 0 }, { 'M','o','n', 0 }, { 'T','u','e', 0 }, { 'W','e','d', 0 },
3131 { 'T','h','u', 0 }, { 'F','r','i', 0 }, { 'S','a','t', 0 } };
3132 static const WCHAR WININET_month[12][4] =
3133 { { 'J','a','n', 0 }, { 'F','e','b', 0 }, { 'M','a','r', 0 }, { 'A','p','r', 0 },
3134 { 'M','a','y', 0 }, { 'J','u','n', 0 }, { 'J','u','l', 0 }, { 'A','u','g', 0 },
3135 { 'S','e','p', 0 }, { 'O','c','t', 0 }, { 'N','o','v', 0 }, { 'D','e','c', 0 } };
3136
3137 /***********************************************************************
3138 * InternetTimeFromSystemTimeA (WININET.@)
3139 */
3140 BOOL WINAPI InternetTimeFromSystemTimeA( const SYSTEMTIME* time, DWORD format, LPSTR string, DWORD size )
3141 {
3142 BOOL ret;
3143 WCHAR stringW[INTERNET_RFC1123_BUFSIZE];
3144
3145 TRACE( "%p 0x%08x %p 0x%08x\n", time, format, string, size );
3146
3147 if (!time || !string || format != INTERNET_RFC1123_FORMAT)
3148 {
3149 SetLastError(ERROR_INVALID_PARAMETER);
3150 return FALSE;
3151 }
3152
3153 if (size < INTERNET_RFC1123_BUFSIZE * sizeof(*string))
3154 {
3155 SetLastError(ERROR_INSUFFICIENT_BUFFER);
3156 return FALSE;
3157 }
3158
3159 ret = InternetTimeFromSystemTimeW( time, format, stringW, sizeof(stringW) );
3160 if (ret) WideCharToMultiByte( CP_ACP, 0, stringW, -1, string, size, NULL, NULL );
3161
3162 return ret;
3163 }
3164
3165 /***********************************************************************
3166 * InternetTimeFromSystemTimeW (WININET.@)
3167 */
3168 BOOL WINAPI InternetTimeFromSystemTimeW( const SYSTEMTIME* time, DWORD format, LPWSTR string, DWORD size )
3169 {
3170 static const WCHAR date[] =
3171 { '%','s',',',' ','%','0','2','d',' ','%','s',' ','%','4','d',' ','%','0',
3172 '2','d',':','%','0','2','d',':','%','0','2','d',' ','G','M','T', 0 };
3173
3174 TRACE( "%p 0x%08x %p 0x%08x\n", time, format, string, size );
3175
3176 if (!time || !string || format != INTERNET_RFC1123_FORMAT)
3177 {
3178 SetLastError(ERROR_INVALID_PARAMETER);
3179 return FALSE;
3180 }
3181
3182 if (size < INTERNET_RFC1123_BUFSIZE * sizeof(*string))
3183 {
3184 SetLastError(ERROR_INSUFFICIENT_BUFFER);
3185 return FALSE;
3186 }
3187
3188 sprintfW( string, date,
3189 WININET_wkday[time->wDayOfWeek],
3190 time->wDay,
3191 WININET_month[time->wMonth - 1],
3192 time->wYear,
3193 time->wHour,
3194 time->wMinute,
3195 time->wSecond );
3196
3197 return TRUE;
3198 }
3199
3200 /***********************************************************************
3201 * InternetTimeToSystemTimeA (WININET.@)
3202 */
3203 BOOL WINAPI InternetTimeToSystemTimeA( LPCSTR string, SYSTEMTIME* time, DWORD reserved )
3204 {
3205 BOOL ret = FALSE;
3206 WCHAR *stringW;
3207
3208 TRACE( "%s %p 0x%08x\n", debugstr_a(string), time, reserved );
3209
3210 stringW = heap_strdupAtoW(string);
3211 if (stringW)
3212 {
3213 ret = InternetTimeToSystemTimeW( stringW, time, reserved );
3214 heap_free( stringW );
3215 }
3216 return ret;
3217 }
3218
3219 /***********************************************************************
3220 * InternetTimeToSystemTimeW (WININET.@)
3221 */
3222 BOOL WINAPI InternetTimeToSystemTimeW( LPCWSTR string, SYSTEMTIME* time, DWORD reserved )
3223 {
3224 unsigned int i;
3225 const WCHAR *s = string;
3226 WCHAR *end;
3227
3228 TRACE( "%s %p 0x%08x\n", debugstr_w(string), time, reserved );
3229
3230 if (!string || !time) return FALSE;
3231
3232 /* Windows does this too */
3233 GetSystemTime( time );
3234
3235 /* Convert an RFC1123 time such as 'Fri, 07 Jan 2005 12:06:35 GMT' into
3236 * a SYSTEMTIME structure.
3237 */
3238
3239 while (*s && !isalphaW( *s )) s++;
3240 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
3241 time->wDayOfWeek = 7;
3242
3243 for (i = 0; i < 7; i++)
3244 {
3245 if (toupperW( WININET_wkday[i][0] ) == toupperW( s[0] ) &&
3246 toupperW( WININET_wkday[i][1] ) == toupperW( s[1] ) &&
3247 toupperW( WININET_wkday[i][2] ) == toupperW( s[2] ) )
3248 {
3249 time->wDayOfWeek = i;
3250 break;
3251 }
3252 }
3253
3254 if (time->wDayOfWeek > 6) return TRUE;
3255 while (*s && !isdigitW( *s )) s++;
3256 time->wDay = strtolW( s, &end, 10 );
3257 s = end;
3258
3259 while (*s && !isalphaW( *s )) s++;
3260 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
3261 time->wMonth = 0;
3262
3263 for (i = 0; i < 12; i++)
3264 {
3265 if (toupperW( WININET_month[i][0]) == toupperW( s[0] ) &&
3266 toupperW( WININET_month[i][1]) == toupperW( s[1] ) &&
3267 toupperW( WININET_month[i][2]) == toupperW( s[2] ) )
3268 {
3269 time->wMonth = i + 1;
3270 break;
3271 }
3272 }
3273 if (time->wMonth == 0) return TRUE;
3274
3275 while (*s && !isdigitW( *s )) s++;
3276 if (*s == '\0') return TRUE;
3277 time->wYear = strtolW( s, &end, 10 );
3278 s = end;
3279
3280 while (*s && !isdigitW( *s )) s++;
3281 if (*s == '\0') return TRUE;
3282 time->wHour = strtolW( s, &end, 10 );
3283 s = end;
3284
3285 while (*s && !isdigitW( *s )) s++;
3286 if (*s == '\0') return TRUE;
3287 time->wMinute = strtolW( s, &end, 10 );
3288 s = end;
3289
3290 while (*s && !isdigitW( *s )) s++;
3291 if (*s == '\0') return TRUE;
3292 time->wSecond = strtolW( s, &end, 10 );
3293 s = end;
3294
3295 time->wMilliseconds = 0;
3296 return TRUE;
3297 }
3298
3299 /***********************************************************************
3300 * InternetCheckConnectionW (WININET.@)
3301 *
3302 * Pings a requested host to check internet connection
3303 *
3304 * RETURNS
3305 * TRUE on success and FALSE on failure. If a failure then
3306 * ERROR_NOT_CONNECTED is placed into GetLastError
3307 *
3308 */
3309 BOOL WINAPI InternetCheckConnectionW( LPCWSTR lpszUrl, DWORD dwFlags, DWORD dwReserved )
3310 {
3311 /*
3312 * this is a kludge which runs the resident ping program and reads the output.
3313 *
3314 * Anyone have a better idea?
3315 */
3316
3317 BOOL rc = FALSE;
3318 static const CHAR ping[] = "ping -c 1 ";
3319 static const CHAR redirect[] = " >/dev/null 2>/dev/null";
3320 WCHAR *host;
3321 DWORD len, host_len;
3322 INTERNET_PORT port;
3323 int status = -1;
3324
3325 FIXME("(%s %x %x)\n", debugstr_w(lpszUrl), dwFlags, dwReserved);
3326
3327 /*
3328 * Crack or set the Address
3329 */
3330 if (lpszUrl == NULL)
3331 {
3332 /*
3333 * According to the doc we are supposed to use the ip for the next
3334 * server in the WnInet internal server database. I have
3335 * no idea what that is or how to get it.
3336 *
3337 * So someone needs to implement this.
3338 */
3339 FIXME("Unimplemented with URL of NULL\n");
3340 return TRUE;
3341 }
3342 else
3343 {
3344 URL_COMPONENTSW components = {sizeof(components)};
3345
3346 components.dwHostNameLength = 1;
3347
3348 if (!InternetCrackUrlW(lpszUrl,0,0,&components))
3349 goto End;
3350
3351 host = components.lpszHostName;
3352 host_len = components.dwHostNameLength;
3353 port = components.nPort;
3354 TRACE("host name: %s port: %d\n",debugstr_wn(host, host_len), port);
3355 }
3356
3357 if (dwFlags & FLAG_ICC_FORCE_CONNECTION)
3358 {
3359 struct sockaddr_storage saddr;
3360 int sa_len = sizeof(saddr);
3361 WCHAR *host_z;
3362 int fd;
3363 BOOL b;
3364
3365 host_z = heap_strndupW(host, host_len);
3366 if (!host_z)
3367 return FALSE;
3368
3369 b = GetAddress(host_z, port, (struct sockaddr *)&saddr, &sa_len, NULL);
3370 heap_free(host_z);
3371 if(!b)
3372 goto End;
3373 init_winsock();
3374 fd = socket(saddr.ss_family, SOCK_STREAM, 0);
3375 if (fd != -1)
3376 {
3377 if (connect(fd, (struct sockaddr *)&saddr, sa_len) == 0)
3378 rc = TRUE;
3379 closesocket(fd);
3380 }
3381 }
3382 else
3383 {
3384 /*
3385 * Build our ping command
3386 */
3387 char *command;
3388
3389 len = WideCharToMultiByte(CP_UNIXCP, 0, host, host_len, NULL, 0, NULL, NULL);
3390 command = heap_alloc(strlen(ping)+len+strlen(redirect)+1);
3391 strcpy(command, ping);
3392 WideCharToMultiByte(CP_UNIXCP, 0, host, host_len, command+sizeof(ping)-1, len, NULL, NULL);
3393 strcpy(command+sizeof(ping)-1+len, redirect);
3394
3395 TRACE("Ping command is : %s\n",command);
3396
3397 status = system(command);
3398 heap_free( command );
3399
3400 TRACE("Ping returned a code of %i\n",status);
3401
3402 /* Ping return code of 0 indicates success */
3403 if (status == 0)
3404 rc = TRUE;
3405 }
3406
3407 End:
3408 if (rc == FALSE)
3409 INTERNET_SetLastError(ERROR_NOT_CONNECTED);
3410
3411 return rc;
3412 }
3413
3414
3415 /***********************************************************************
3416 * InternetCheckConnectionA (WININET.@)
3417 *
3418 * Pings a requested host to check internet connection
3419 *
3420 * RETURNS
3421 * TRUE on success and FALSE on failure. If a failure then
3422 * ERROR_NOT_CONNECTED is placed into GetLastError
3423 *
3424 */
3425 BOOL WINAPI InternetCheckConnectionA(LPCSTR lpszUrl, DWORD dwFlags, DWORD dwReserved)
3426 {
3427 WCHAR *url = NULL;
3428 BOOL rc;
3429
3430 if(lpszUrl) {
3431 url = heap_strdupAtoW(lpszUrl);
3432 if(!url)
3433 return FALSE;
3434 }
3435
3436 rc = InternetCheckConnectionW(url, dwFlags, dwReserved);
3437
3438 heap_free(url);
3439 return rc;
3440 }
3441
3442
3443 /**********************************************************
3444 * INTERNET_InternetOpenUrlW (internal)
3445 *
3446 * Opens an URL
3447 *
3448 * RETURNS
3449 * handle of connection or NULL on failure
3450 */
3451 static HINTERNET INTERNET_InternetOpenUrlW(appinfo_t *hIC, LPCWSTR lpszUrl,
3452 LPCWSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext)
3453 {
3454 URL_COMPONENTSW urlComponents = { sizeof(urlComponents) };
3455 WCHAR *host, *user = NULL, *pass = NULL, *path;
3456 HINTERNET client = NULL, client1 = NULL;
3457 DWORD res;
3458
3459 TRACE("(%p, %s, %s, %08x, %08x, %08lx)\n", hIC, debugstr_w(lpszUrl), debugstr_w(lpszHeaders),
3460 dwHeadersLength, dwFlags, dwContext);
3461
3462 urlComponents.dwHostNameLength = 1;
3463 urlComponents.dwUserNameLength = 1;
3464 urlComponents.dwPasswordLength = 1;
3465 urlComponents.dwUrlPathLength = 1;
3466 urlComponents.dwExtraInfoLength = 1;
3467 if(!InternetCrackUrlW(lpszUrl, strlenW(lpszUrl), 0, &urlComponents))
3468 return NULL;
3469
3470 if ((urlComponents.nScheme == INTERNET_SCHEME_HTTP || urlComponents.nScheme == INTERNET_SCHEME_HTTPS) &&
3471 urlComponents.dwExtraInfoLength)
3472 {
3473 assert(urlComponents.lpszUrlPath + urlComponents.dwUrlPathLength == urlComponents.lpszExtraInfo);
3474 urlComponents.dwUrlPathLength += urlComponents.dwExtraInfoLength;
3475 }
3476
3477 host = heap_strndupW(urlComponents.lpszHostName, urlComponents.dwHostNameLength);
3478 path = heap_strndupW(urlComponents.lpszUrlPath, urlComponents.dwUrlPathLength);
3479 if(urlComponents.dwUserNameLength)
3480 user = heap_strndupW(urlComponents.lpszUserName, urlComponents.dwUserNameLength);
3481 if(urlComponents.dwPasswordLength)
3482 pass = heap_strndupW(urlComponents.lpszPassword, urlComponents.dwPasswordLength);
3483
3484 switch(urlComponents.nScheme) {
3485 case INTERNET_SCHEME_FTP:
3486 client = FTP_Connect(hIC, host, urlComponents.nPort,
3487 user, pass, dwFlags, dwContext, INET_OPENURL);
3488 if(client == NULL)
3489 break;
3490 client1 = FtpOpenFileW(client, path, GENERIC_READ, dwFlags, dwContext);
3491 if(client1 == NULL) {
3492 InternetCloseHandle(client);
3493 break;
3494 }
3495 break;
3496
3497 case INTERNET_SCHEME_HTTP:
3498 case INTERNET_SCHEME_HTTPS: {
3499 static const WCHAR szStars[] = { '*','/','*', 0 };
3500 LPCWSTR accept[2] = { szStars, NULL };
3501
3502 if (urlComponents.nScheme == INTERNET_SCHEME_HTTPS) dwFlags |= INTERNET_FLAG_SECURE;
3503
3504 /* FIXME: should use pointers, not handles, as handles are not thread-safe */
3505 res = HTTP_Connect(hIC, host, urlComponents.nPort,
3506 user, pass, dwFlags, dwContext, INET_OPENURL, &client);
3507 if(res != ERROR_SUCCESS) {
3508 INTERNET_SetLastError(res);
3509 break;
3510 }
3511
3512 client1 = HttpOpenRequestW(client, NULL, path, NULL, NULL, accept, dwFlags, dwContext);
3513 if(client1 == NULL) {
3514 InternetCloseHandle(client);
3515 break;
3516 }
3517 HttpAddRequestHeadersW(client1, lpszHeaders, dwHeadersLength, HTTP_ADDREQ_FLAG_ADD);
3518 if (!HttpSendRequestW(client1, NULL, 0, NULL, 0) &&
3519 GetLastError() != ERROR_IO_PENDING) {
3520 InternetCloseHandle(client1);
3521 client1 = NULL;
3522 break;
3523 }
3524 }
3525 case INTERNET_SCHEME_GOPHER:
3526 /* gopher doesn't seem to be implemented in wine, but it's supposed
3527 * to be supported by InternetOpenUrlA. */
3528 default:
3529 SetLastError(ERROR_INTERNET_UNRECOGNIZED_SCHEME);
3530 break;
3531 }
3532
3533 TRACE(" %p <--\n", client1);
3534
3535 heap_free(host);
3536 heap_free(path);
3537 heap_free(user);
3538 heap_free(pass);
3539 return client1;
3540 }
3541
3542 /**********************************************************
3543 * InternetOpenUrlW (WININET.@)
3544 *
3545 * Opens an URL
3546 *
3547 * RETURNS
3548 * handle of connection or NULL on failure
3549 */
3550 typedef struct {
3551 task_header_t hdr;
3552 WCHAR *url;
3553 WCHAR *headers;
3554 DWORD headers_len;
3555 DWORD flags;
3556 DWORD_PTR context;
3557 } open_url_task_t;
3558
3559 static void AsyncInternetOpenUrlProc(task_header_t *hdr)
3560 {
3561 open_url_task_t *task = (open_url_task_t*)hdr;
3562
3563 TRACE("%p\n", task->hdr.hdr);
3564
3565 INTERNET_InternetOpenUrlW((appinfo_t*)task->hdr.hdr, task->url, task->headers,
3566 task->headers_len, task->flags, task->context);
3567 heap_free(task->url);
3568 heap_free(task->headers);
3569 }
3570
3571 HINTERNET WINAPI InternetOpenUrlW(HINTERNET hInternet, LPCWSTR lpszUrl,
3572 LPCWSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext)
3573 {
3574 HINTERNET ret = NULL;
3575 appinfo_t *hIC = NULL;
3576
3577 if (TRACE_ON(wininet)) {
3578 TRACE("(%p, %s, %s, %08x, %08x, %08lx)\n", hInternet, debugstr_w(lpszUrl), debugstr_w(lpszHeaders),
3579 dwHeadersLength, dwFlags, dwContext);
3580 TRACE(" flags :");
3581 dump_INTERNET_FLAGS(dwFlags);
3582 }
3583
3584 if (!lpszUrl)
3585 {
3586 SetLastError(ERROR_INVALID_PARAMETER);
3587 goto lend;
3588 }
3589
3590 hIC = (appinfo_t*)get_handle_object( hInternet );
3591 if (NULL == hIC || hIC->hdr.htype != WH_HINIT) {
3592 SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
3593 goto lend;
3594 }
3595
3596 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC) {
3597 open_url_task_t *task;
3598
3599 task = alloc_async_task(&hIC->hdr, AsyncInternetOpenUrlProc, sizeof(*task));
3600 task->url = heap_strdupW(lpszUrl);
3601 task->headers = heap_strdupW(lpszHeaders);
3602 task->headers_len = dwHeadersLength;
3603 task->flags = dwFlags;
3604 task->context = dwContext;
3605
3606 INTERNET_AsyncCall(&task->hdr);
3607 SetLastError(ERROR_IO_PENDING);
3608 } else {
3609 ret = INTERNET_InternetOpenUrlW(hIC, lpszUrl, lpszHeaders, dwHeadersLength, dwFlags, dwContext);
3610 }
3611
3612 lend:
3613 if( hIC )
3614 WININET_Release( &hIC->hdr );
3615 TRACE(" %p <--\n", ret);
3616
3617 return ret;
3618 }
3619
3620 /**********************************************************
3621 * InternetOpenUrlA (WININET.@)
3622 *
3623 * Opens an URL
3624 *
3625 * RETURNS
3626 * handle of connection or NULL on failure
3627 */
3628 HINTERNET WINAPI InternetOpenUrlA(HINTERNET hInternet, LPCSTR lpszUrl,
3629 LPCSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext)
3630 {
3631 HINTERNET rc = NULL;
3632 LPWSTR szUrl = NULL;
3633 WCHAR *headers = NULL;
3634
3635 TRACE("\n");
3636
3637 if(lpszUrl) {
3638 szUrl = heap_strdupAtoW(lpszUrl);
3639 if(!szUrl)
3640 return NULL;
3641 }
3642
3643 if(lpszHeaders) {
3644 headers = heap_strndupAtoW(lpszHeaders, dwHeadersLength, &dwHeadersLength);
3645 if(!headers) {
3646 heap_free(szUrl);
3647 return NULL;
3648 }
3649 }
3650
3651 rc = InternetOpenUrlW(hInternet, szUrl, headers, dwHeadersLength, dwFlags, dwContext);
3652
3653 heap_free(szUrl);
3654 heap_free(headers);
3655 return rc;
3656 }
3657
3658
3659 static LPWITHREADERROR INTERNET_AllocThreadError(void)
3660 {
3661 LPWITHREADERROR lpwite = heap_alloc(sizeof(*lpwite));
3662
3663 if (lpwite)
3664 {
3665 lpwite->dwError = 0;
3666 lpwite->response[0] = '\0';
3667 }
3668
3669 if (!TlsSetValue(g_dwTlsErrIndex, lpwite))
3670 {
3671 heap_free(lpwite);
3672 return NULL;
3673 }
3674 return lpwite;
3675 }
3676
3677
3678 /***********************************************************************
3679 * INTERNET_SetLastError (internal)
3680 *
3681 * Set last thread specific error
3682 *
3683 * RETURNS
3684 *
3685 */
3686 void INTERNET_SetLastError(DWORD dwError)
3687 {
3688 LPWITHREADERROR lpwite = TlsGetValue(g_dwTlsErrIndex);
3689
3690 if (!lpwite)
3691 lpwite = INTERNET_AllocThreadError();
3692
3693 SetLastError(dwError);
3694 if(lpwite)
3695 lpwite->dwError = dwError;
3696 }
3697
3698
3699 /***********************************************************************
3700 * INTERNET_GetLastError (internal)
3701 *
3702 * Get last thread specific error
3703 *
3704 * RETURNS
3705 *
3706 */
3707 DWORD INTERNET_GetLastError(void)
3708 {
3709 LPWITHREADERROR lpwite = TlsGetValue(g_dwTlsErrIndex);
3710 if (!lpwite) return 0;
3711 /* TlsGetValue clears last error, so set it again here */
3712 SetLastError(lpwite->dwError);
3713 return lpwite->dwError;
3714 }
3715
3716
3717 /***********************************************************************
3718 * INTERNET_WorkerThreadFunc (internal)
3719 *
3720 * Worker thread execution function
3721 *
3722 * RETURNS
3723 *
3724 */
3725 static DWORD CALLBACK INTERNET_WorkerThreadFunc(LPVOID lpvParam)
3726 {
3727 task_header_t *task = lpvParam;
3728
3729 TRACE("\n");
3730
3731 task->proc(task);
3732 WININET_Release(task->hdr);
3733 heap_free(task);
3734
3735 if (g_dwTlsErrIndex != TLS_OUT_OF_INDEXES)
3736 {
3737 heap_free(TlsGetValue(g_dwTlsErrIndex));
3738 TlsSetValue(g_dwTlsErrIndex, NULL);
3739 }
3740 return TRUE;
3741 }
3742
3743 void *alloc_async_task(object_header_t *hdr, async_task_proc_t proc, size_t size)
3744 {
3745 task_header_t *task;
3746
3747 task = heap_alloc(size);
3748 if(!task)
3749 return NULL;
3750
3751 task->hdr = WININET_AddRef(hdr);
3752 task->proc = proc;
3753 return task;
3754 }
3755
3756 /***********************************************************************
3757 * INTERNET_AsyncCall (internal)
3758 *
3759 * Retrieves work request from queue
3760 *
3761 * RETURNS
3762 *
3763 */
3764 DWORD INTERNET_AsyncCall(task_header_t *task)
3765 {
3766 BOOL bSuccess;
3767
3768 TRACE("\n");
3769
3770 bSuccess = QueueUserWorkItem(INTERNET_WorkerThreadFunc, task, WT_EXECUTELONGFUNCTION);
3771 if (!bSuccess)
3772 {
3773 heap_free(task);
3774 return ERROR_INTERNET_ASYNC_THREAD_FAILED;
3775 }
3776 return ERROR_SUCCESS;
3777 }
3778
3779
3780 /***********************************************************************
3781 * INTERNET_GetResponseBuffer (internal)
3782 *
3783 * RETURNS
3784 *
3785 */
3786 LPSTR INTERNET_GetResponseBuffer(void)
3787 {
3788 LPWITHREADERROR lpwite = TlsGetValue(g_dwTlsErrIndex);
3789 if (!lpwite)
3790 lpwite = INTERNET_AllocThreadError();
3791 TRACE("\n");
3792 return lpwite->response;
3793 }
3794
3795 /**********************************************************
3796 * InternetQueryDataAvailable (WININET.@)
3797 *
3798 * Determines how much data is available to be read.
3799 *
3800 * RETURNS
3801 * TRUE on success, FALSE if an error occurred. If
3802 * INTERNET_FLAG_ASYNC was specified in InternetOpen, and
3803 * no data is presently available, FALSE is returned with
3804 * the last error ERROR_IO_PENDING; a callback with status
3805 * INTERNET_STATUS_REQUEST_COMPLETE will be sent when more
3806 * data is available.
3807 */
3808 BOOL WINAPI InternetQueryDataAvailable( HINTERNET hFile,
3809 LPDWORD lpdwNumberOfBytesAvailable,
3810 DWORD dwFlags, DWORD_PTR dwContext)
3811 {
3812 object_header_t *hdr;
3813 DWORD res;
3814
3815 TRACE("(%p %p %x %lx)\n", hFile, lpdwNumberOfBytesAvailable, dwFlags, dwContext);
3816
3817 hdr = get_handle_object( hFile );
3818 if (!hdr) {
3819 SetLastError(ERROR_INVALID_HANDLE);
3820 return FALSE;
3821 }
3822
3823 if(hdr->vtbl->QueryDataAvailable) {
3824 res = hdr->vtbl->QueryDataAvailable(hdr, lpdwNumberOfBytesAvailable, dwFlags, dwContext);
3825 }else {
3826 WARN("wrong handle\n");
3827 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
3828 }
3829
3830 WININET_Release(hdr);
3831
3832 if(res != ERROR_SUCCESS)
3833 SetLastError(res);
3834 return res == ERROR_SUCCESS;
3835 }
3836
3837 DWORD create_req_file(const WCHAR *file_name, req_file_t **ret)
3838 {
3839 req_file_t *req_file;
3840
3841 req_file = heap_alloc_zero(sizeof(*req_file));
3842 if(!req_file)
3843 return ERROR_NOT_ENOUGH_MEMORY;
3844
3845 req_file->ref = 1;
3846
3847 req_file->file_name = heap_strdupW(file_name);
3848 if(!req_file->file_name) {
3849 heap_free(req_file);
3850 return ERROR_NOT_ENOUGH_MEMORY;
3851 }
3852
3853 req_file->file_handle = CreateFileW(req_file->file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
3854 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
3855 if(req_file->file_handle == INVALID_HANDLE_VALUE) {
3856 req_file_release(req_file);
3857 return GetLastError();
3858 }
3859
3860 *ret = req_file;
3861 return ERROR_SUCCESS;
3862 }
3863
3864 void req_file_release(req_file_t *req_file)
3865 {
3866 if(InterlockedDecrement(&req_file->ref))
3867 return;
3868
3869 if(!req_file->is_committed)
3870 DeleteFileW(req_file->file_name);
3871 if(req_file->file_handle && req_file->file_handle != INVALID_HANDLE_VALUE)
3872 CloseHandle(req_file->file_handle);
3873 heap_free(req_file->file_name);
3874 heap_free(req_file->url);
3875 heap_free(req_file);
3876 }
3877
3878 /***********************************************************************
3879 * InternetLockRequestFile (WININET.@)
3880 */
3881 BOOL WINAPI InternetLockRequestFile(HINTERNET hInternet, HANDLE *lphLockReqHandle)
3882 {
3883 req_file_t *req_file = NULL;
3884 object_header_t *hdr;
3885 DWORD res;
3886
3887 TRACE("(%p %p)\n", hInternet, lphLockReqHandle);
3888
3889 hdr = get_handle_object(hInternet);
3890 if (!hdr) {
3891 SetLastError(ERROR_INVALID_HANDLE);
3892 return FALSE;
3893 }
3894
3895 if(hdr->vtbl->LockRequestFile) {
3896 res = hdr->vtbl->LockRequestFile(hdr, &req_file);
3897 }else {
3898 WARN("wrong handle\n");
3899 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
3900 }
3901
3902 WININET_Release(hdr);
3903
3904 *lphLockReqHandle = req_file;
3905 if(res != ERROR_SUCCESS)
3906 SetLastError(res);
3907 return res == ERROR_SUCCESS;
3908 }
3909
3910 BOOL WINAPI InternetUnlockRequestFile(HANDLE hLockHandle)
3911 {
3912 TRACE("(%p)\n", hLockHandle);
3913
3914 req_file_release(hLockHandle);
3915 return TRUE;
3916 }
3917
3918
3919 /***********************************************************************
3920 * InternetAutodial (WININET.@)
3921 *
3922 * On windows this function is supposed to dial the default internet
3923 * connection. We don't want to have Wine dial out to the internet so
3924 * we return TRUE by default. It might be nice to check if we are connected.
3925 *
3926 * RETURNS
3927 * TRUE on success
3928 * FALSE on failure
3929 *
3930 */
3931 BOOL WINAPI InternetAutodial(DWORD dwFlags, HWND hwndParent)
3932 {
3933 FIXME("STUB\n");
3934
3935 /* Tell that we are connected to the internet. */
3936 return TRUE;
3937 }
3938
3939 /***********************************************************************
3940 * InternetAutodialHangup (WININET.@)
3941 *
3942 * Hangs up a connection made with InternetAutodial
3943 *
3944 * PARAM
3945 * dwReserved
3946 * RETURNS
3947 * TRUE on success
3948 * FALSE on failure
3949 *
3950 */
3951 BOOL WINAPI InternetAutodialHangup(DWORD dwReserved)
3952 {
3953 FIXME("STUB\n");
3954
3955 /* we didn't dial, we don't disconnect */
3956 return TRUE;
3957 }
3958
3959 /***********************************************************************
3960 * InternetCombineUrlA (WININET.@)
3961 *
3962 * Combine a base URL with a relative URL
3963 *
3964 * RETURNS
3965 * TRUE on success
3966 * FALSE on failure
3967 *
3968 */
3969
3970 BOOL WINAPI InternetCombineUrlA(LPCSTR lpszBaseUrl, LPCSTR lpszRelativeUrl,
3971 LPSTR lpszBuffer, LPDWORD lpdwBufferLength,
3972 DWORD dwFlags)
3973 {
3974 HRESULT hr=S_OK;
3975
3976 TRACE("(%s, %s, %p, %p, 0x%08x)\n", debugstr_a(lpszBaseUrl), debugstr_a(lpszRelativeUrl), lpszBuffer, lpdwBufferLength, dwFlags);
3977
3978 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
3979 dwFlags ^= ICU_NO_ENCODE;
3980 hr=UrlCombineA(lpszBaseUrl,lpszRelativeUrl,lpszBuffer,lpdwBufferLength,dwFlags);
3981
3982 return (hr==S_OK);
3983 }
3984
3985 /***********************************************************************
3986 * InternetCombineUrlW (WININET.@)
3987 *
3988 * Combine a base URL with a relative URL
3989 *
3990 * RETURNS
3991 * TRUE on success
3992 * FALSE on failure
3993 *
3994 */
3995
3996 BOOL WINAPI InternetCombineUrlW(LPCWSTR lpszBaseUrl, LPCWSTR lpszRelativeUrl,
3997 LPWSTR lpszBuffer, LPDWORD lpdwBufferLength,
3998 DWORD dwFlags)
3999 {
4000 HRESULT hr=S_OK;
4001
4002 TRACE("(%s, %s, %p, %p, 0x%08x)\n", debugstr_w(lpszBaseUrl), debugstr_w(lpszRelativeUrl), lpszBuffer, lpdwBufferLength, dwFlags);
4003
4004 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
4005 dwFlags ^= ICU_NO_ENCODE;
4006 hr=UrlCombineW(lpszBaseUrl,lpszRelativeUrl,lpszBuffer,lpdwBufferLength,dwFlags);
4007
4008 return (hr==S_OK);
4009 }
4010
4011 /* max port num is 65535 => 5 digits */
4012 #define MAX_WORD_DIGITS 5
4013
4014 #define URL_GET_COMP_LENGTH(url, component) ((url)->dw##component##Length ? \
4015 (url)->dw##component##Length : strlenW((url)->lpsz##component))
4016 #define URL_GET_COMP_LENGTHA(url, component) ((url)->dw##component##Length ? \
4017 (url)->dw##component##Length : strlen((url)->lpsz##component))
4018
4019 static BOOL url_uses_default_port(INTERNET_SCHEME nScheme, INTERNET_PORT nPort)
4020 {
4021 if ((nScheme == INTERNET_SCHEME_HTTP) &&
4022 (nPort == INTERNET_DEFAULT_HTTP_PORT))
4023 return TRUE;
4024 if ((nScheme == INTERNET_SCHEME_HTTPS) &&
4025 (nPort == INTERNET_DEFAULT_HTTPS_PORT))
4026 return TRUE;
4027 if ((nScheme == INTERNET_SCHEME_FTP) &&
4028 (nPort == INTERNET_DEFAULT_FTP_PORT))
4029 return TRUE;
4030 if ((nScheme == INTERNET_SCHEME_GOPHER) &&
4031 (nPort == INTERNET_DEFAULT_GOPHER_PORT))
4032 return TRUE;
4033
4034 if (nPort == INTERNET_INVALID_PORT_NUMBER)
4035 return TRUE;
4036
4037 return FALSE;
4038 }
4039
4040 /* opaque urls do not fit into the standard url hierarchy and don't have
4041 * two following slashes */
4042 static inline BOOL scheme_is_opaque(INTERNET_SCHEME nScheme)
4043 {
4044 return (nScheme != INTERNET_SCHEME_FTP) &&
4045 (nScheme != INTERNET_SCHEME_GOPHER) &&
4046 (nScheme != INTERNET_SCHEME_HTTP) &&
4047 (nScheme != INTERNET_SCHEME_HTTPS) &&
4048 (nScheme != INTERNET_SCHEME_FILE);
4049 }
4050
4051 static LPCWSTR INTERNET_GetSchemeString(INTERNET_SCHEME scheme)
4052 {
4053 int index;
4054 if (scheme < INTERNET_SCHEME_FIRST)
4055 return NULL;
4056 index = scheme - INTERNET_SCHEME_FIRST;
4057 if (index >= sizeof(url_schemes)/sizeof(url_schemes[0]))
4058 return NULL;
4059 return (LPCWSTR)url_schemes[index];
4060 }
4061
4062 /* we can calculate using ansi strings because we're just
4063 * calculating string length, not size
4064 */
4065 static BOOL calc_url_length(LPURL_COMPONENTSW lpUrlComponents,
4066 LPDWORD lpdwUrlLength)
4067 {
4068 INTERNET_SCHEME nScheme;
4069
4070 *lpdwUrlLength = 0;
4071
4072 if (lpUrlComponents->lpszScheme)
4073 {
4074 DWORD dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, Scheme);
4075 *lpdwUrlLength += dwLen;
4076 nScheme = GetInternetSchemeW(lpUrlComponents->lpszScheme, dwLen);
4077 }
4078 else
4079 {
4080 LPCWSTR scheme;
4081
4082 nScheme = lpUrlComponents->nScheme;
4083
4084 if (nScheme == INTERNET_SCHEME_DEFAULT)
4085 nScheme = INTERNET_SCHEME_HTTP;
4086 scheme = INTERNET_GetSchemeString(nScheme);
4087 *lpdwUrlLength += strlenW(scheme);
4088 }
4089
4090 (*lpdwUrlLength)++; /* ':' */
4091 if (!scheme_is_opaque(nScheme) || lpUrlComponents->lpszHostName)
4092 *lpdwUrlLength += strlen("//");
4093
4094 if (lpUrlComponents->lpszUserName)
4095 {
4096 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, UserName);
4097 *lpdwUrlLength += strlen("@");
4098 }
4099 else
4100 {
4101 if (lpUrlComponents->lpszPassword)
4102 {
4103 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
4104 return FALSE;
4105 }
4106 }
4107
4108 if (lpUrlComponents->lpszPassword)
4109 {
4110 *lpdwUrlLength += strlen(":");
4111 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, Password);
4112 }
4113
4114 if (lpUrlComponents->lpszHostName)
4115 {
4116 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, HostName);
4117
4118 if (!url_uses_default_port(nScheme, lpUrlComponents->nPort))
4119 {
4120 char szPort[MAX_WORD_DIGITS+1];
4121
4122 *lpdwUrlLength += sprintf(szPort, "%d", lpUrlComponents->nPort);
4123 *lpdwUrlLength += strlen(":");
4124 }
4125
4126 if (lpUrlComponents->lpszUrlPath && *lpUrlComponents->lpszUrlPath != '/')
4127 (*lpdwUrlLength)++; /* '/' */
4128 }
4129
4130 if (lpUrlComponents->lpszUrlPath)
4131 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, UrlPath);
4132
4133 if (lpUrlComponents->lpszExtraInfo)
4134 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, ExtraInfo);
4135
4136 return TRUE;
4137 }
4138
4139 static void convert_urlcomp_atow(LPURL_COMPONENTSA lpUrlComponents, LPURL_COMPONENTSW urlCompW)
4140 {
4141 INT len;
4142
4143 ZeroMemory(urlCompW, sizeof(URL_COMPONENTSW));
4144
4145 urlCompW->dwStructSize = sizeof(URL_COMPONENTSW);
4146 urlCompW->dwSchemeLength = lpUrlComponents->dwSchemeLength;
4147 urlCompW->nScheme = lpUrlComponents->nScheme;
4148 urlCompW->dwHostNameLength = lpUrlComponents->dwHostNameLength;
4149 urlCompW->nPort = lpUrlComponents->nPort;
4150 urlCompW->dwUserNameLength = lpUrlComponents->dwUserNameLength;
4151 urlCompW->dwPasswordLength = lpUrlComponents->dwPasswordLength;
4152 urlCompW->dwUrlPathLength = lpUrlComponents->dwUrlPathLength;
4153 urlCompW->dwExtraInfoLength = lpUrlComponents->dwExtraInfoLength;
4154
4155 if (lpUrlComponents->lpszScheme)
4156 {
4157 len = URL_GET_COMP_LENGTHA(lpUrlComponents, Scheme) + 1;
4158 urlCompW->lpszScheme = heap_alloc(len * sizeof(WCHAR));
4159 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszScheme,
4160 -1, urlCompW->lpszScheme, len);
4161 }
4162
4163 if (lpUrlComponents->lpszHostName)
4164 {
4165 len = URL_GET_COMP_LENGTHA(lpUrlComponents, HostName) + 1;
4166 urlCompW->lpszHostName = heap_alloc(len * sizeof(WCHAR));
4167 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszHostName,
4168 -1, urlCompW->lpszHostName, len);
4169 }
4170
4171 if (lpUrlComponents->lpszUserName)
4172 {
4173 len = URL_GET_COMP_LENGTHA(lpUrlComponents, UserName) + 1;
4174 urlCompW->lpszUserName = heap_alloc(len * sizeof(WCHAR));
4175 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszUserName,
4176 -1, urlCompW->lpszUserName, len);
4177 }
4178
4179 if (lpUrlComponents->lpszPassword)
4180 {
4181 len = URL_GET_COMP_LENGTHA(lpUrlComponents, Password) + 1;
4182 urlCompW->lpszPassword = heap_alloc(len * sizeof(WCHAR));
4183 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszPassword,
4184 -1, urlCompW->lpszPassword, len);
4185 }
4186
4187 if (lpUrlComponents->lpszUrlPath)
4188 {
4189 len = URL_GET_COMP_LENGTHA(lpUrlComponents, UrlPath) + 1;
4190 urlCompW->lpszUrlPath = heap_alloc(len * sizeof(WCHAR));
4191 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszUrlPath,
4192 -1, urlCompW->lpszUrlPath, len);
4193 }
4194
4195 if (lpUrlComponents->lpszExtraInfo)
4196 {
4197 len = URL_GET_COMP_LENGTHA(lpUrlComponents, ExtraInfo) + 1;
4198 urlCompW->lpszExtraInfo = heap_alloc(len * sizeof(WCHAR));
4199 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszExtraInfo,
4200 -1, urlCompW->lpszExtraInfo, len);
4201 }
4202 }
4203
4204 /***********************************************************************
4205 * InternetCreateUrlA (WININET.@)
4206 *
4207 * See InternetCreateUrlW.
4208 */
4209 BOOL WINAPI InternetCreateUrlA(LPURL_COMPONENTSA lpUrlComponents, DWORD dwFlags,
4210 LPSTR lpszUrl, LPDWORD lpdwUrlLength)
4211 {
4212 BOOL ret;
4213 LPWSTR urlW = NULL;
4214 URL_COMPONENTSW urlCompW;
4215
4216 TRACE("(%p,%d,%p,%p)\n", lpUrlComponents, dwFlags, lpszUrl, lpdwUrlLength);
4217
4218 if (!lpUrlComponents || lpUrlComponents->dwStructSize != sizeof(URL_COMPONENTSW) || !lpdwUrlLength)
4219 {
4220 SetLastError(ERROR_INVALID_PARAMETER);
4221 return FALSE;
4222 }
4223
4224 convert_urlcomp_atow(lpUrlComponents, &urlCompW);
4225
4226 if (lpszUrl)
4227 urlW = heap_alloc(*lpdwUrlLength * sizeof(WCHAR));
4228
4229 ret = InternetCreateUrlW(&urlCompW, dwFlags, urlW, lpdwUrlLength);
4230
4231 if (!ret && (GetLastError() == ERROR_INSUFFICIENT_BUFFER))
4232 *lpdwUrlLength /= sizeof(WCHAR);
4233
4234 /* on success, lpdwUrlLength points to the size of urlW in WCHARS
4235 * minus one, so add one to leave room for NULL terminator
4236 */
4237 if (ret)
4238 WideCharToMultiByte(CP_ACP, 0, urlW, -1, lpszUrl, *lpdwUrlLength + 1, NULL, NULL);
4239
4240 heap_free(urlCompW.lpszScheme);
4241 heap_free(urlCompW.lpszHostName);
4242 heap_free(urlCompW.lpszUserName);
4243 heap_free(urlCompW.lpszPassword);
4244 heap_free(urlCompW.lpszUrlPath);
4245 heap_free(urlCompW.lpszExtraInfo);
4246 heap_free(urlW);
4247 return ret;
4248 }
4249
4250 /***********************************************************************
4251 * InternetCreateUrlW (WININET.@)
4252 *
4253 * Creates a URL from its component parts.
4254 *
4255 * PARAMS
4256 * lpUrlComponents [I] URL Components.
4257 * dwFlags [I] Flags. See notes.
4258 * lpszUrl [I] Buffer in which to store the created URL.
4259 * lpdwUrlLength [I/O] On input, the length of the buffer pointed to by
4260 * lpszUrl in characters. On output, the number of bytes
4261 * required to store the URL including terminator.
4262 *
4263 * NOTES
4264 *
4265 * The dwFlags parameter can be zero or more of the following:
4266 *|ICU_ESCAPE - Generates escape sequences for unsafe characters in the path and extra info of the URL.
4267 *
4268 * RETURNS
4269 * TRUE on success
4270 * FALSE on failure
4271 *
4272 */
4273 BOOL WINAPI InternetCreateUrlW(LPURL_COMPONENTSW lpUrlComponents, DWORD dwFlags,
4274 LPWSTR lpszUrl, LPDWORD lpdwUrlLength)
4275 {
4276 DWORD dwLen;
4277 INTERNET_SCHEME nScheme;
4278
4279 static const WCHAR slashSlashW[] = {'/','/'};
4280 static const WCHAR fmtW[] = {'%','u',0};
4281
4282 TRACE("(%p,%d,%p,%p)\n", lpUrlComponents, dwFlags, lpszUrl, lpdwUrlLength);
4283
4284 if (!lpUrlComponents || lpUrlComponents->dwStructSize != sizeof(URL_COMPONENTSW) || !lpdwUrlLength)
4285 {
4286 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
4287 return FALSE;
4288 }
4289
4290 if (!calc_url_length(lpUrlComponents, &dwLen))
4291 return FALSE;
4292
4293 if (!lpszUrl || *lpdwUrlLength < dwLen)
4294 {
4295 *lpdwUrlLength = (dwLen + 1) * sizeof(WCHAR);
4296 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
4297 return FALSE;
4298 }
4299
4300 *lpdwUrlLength = dwLen;
4301 lpszUrl[0] = 0x00;
4302
4303 dwLen = 0;
4304
4305 if (lpUrlComponents->lpszScheme)
4306 {
4307 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, Scheme);
4308 memcpy(lpszUrl, lpUrlComponents->lpszScheme, dwLen * sizeof(WCHAR));
4309 lpszUrl += dwLen;
4310
4311 nScheme = GetInternetSchemeW(lpUrlComponents->lpszScheme, dwLen);
4312 }
4313 else
4314 {
4315 LPCWSTR scheme;
4316 nScheme = lpUrlComponents->nScheme;
4317
4318 if (nScheme == INTERNET_SCHEME_DEFAULT)
4319 nScheme = INTERNET_SCHEME_HTTP;
4320
4321 scheme = INTERNET_GetSchemeString(nScheme);
4322 dwLen = strlenW(scheme);
4323 memcpy(lpszUrl, scheme, dwLen * sizeof(WCHAR));
4324 lpszUrl += dwLen;
4325 }
4326
4327 /* all schemes are followed by at least a colon */
4328 *lpszUrl = ':';
4329 lpszUrl++;
4330
4331 if (!scheme_is_opaque(nScheme) || lpUrlComponents->lpszHostName)
4332 {
4333 memcpy(lpszUrl, slashSlashW, sizeof(slashSlashW));
4334 lpszUrl += sizeof(slashSlashW)/sizeof(slashSlashW[0]);
4335 }
4336
4337 if (lpUrlComponents->lpszUserName)
4338 {
4339 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, UserName);
4340 memcpy(lpszUrl, lpUrlComponents->lpszUserName, dwLen * sizeof(WCHAR));
4341 lpszUrl += dwLen;
4342
4343 if (lpUrlComponents->lpszPassword)
4344 {
4345 *lpszUrl = ':';
4346 lpszUrl++;
4347
4348 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, Password);
4349 memcpy(lpszUrl, lpUrlComponents->lpszPassword, dwLen * sizeof(WCHAR));
4350 lpszUrl += dwLen;
4351 }
4352
4353 *lpszUrl = '@';
4354 lpszUrl++;
4355 }
4356
4357 if (lpUrlComponents->lpszHostName)
4358 {
4359 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, HostName);
4360 memcpy(lpszUrl, lpUrlComponents->lpszHostName, dwLen * sizeof(WCHAR));
4361 lpszUrl += dwLen;
4362
4363 if (!url_uses_default_port(nScheme, lpUrlComponents->nPort))
4364 {
4365 *lpszUrl = ':';
4366 lpszUrl++;
4367 lpszUrl += sprintfW(lpszUrl, fmtW, lpUrlComponents->nPort);
4368 }
4369
4370 /* add slash between hostname and path if necessary */
4371 if (lpUrlComponents->lpszUrlPath && *lpUrlComponents->lpszUrlPath != '/')
4372 {
4373 *lpszUrl = '/';
4374 lpszUrl++;
4375 }
4376 }
4377
4378 if (lpUrlComponents->lpszUrlPath)
4379 {
4380 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, UrlPath);
4381 memcpy(lpszUrl, lpUrlComponents->lpszUrlPath, dwLen * sizeof(WCHAR));
4382 lpszUrl += dwLen;
4383 }
4384
4385 if (lpUrlComponents->lpszExtraInfo)
4386 {
4387 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, ExtraInfo);
4388 memcpy(lpszUrl, lpUrlComponents->lpszExtraInfo, dwLen * sizeof(WCHAR));
4389 lpszUrl += dwLen;
4390 }
4391
4392 *lpszUrl = '\0';
4393
4394 return TRUE;
4395 }
4396
4397 /***********************************************************************
4398 * InternetConfirmZoneCrossingA (WININET.@)
4399 *
4400 */
4401 DWORD WINAPI InternetConfirmZoneCrossingA( HWND hWnd, LPSTR szUrlPrev, LPSTR szUrlNew, BOOL bPost )
4402 {
4403 FIXME("(%p, %s, %s, %x) stub\n", hWnd, debugstr_a(szUrlPrev), debugstr_a(szUrlNew), bPost);
4404 return ERROR_SUCCESS;
4405 }
4406
4407 /***********************************************************************
4408 * InternetConfirmZoneCrossingW (WININET.@)
4409 *
4410 */
4411 DWORD WINAPI InternetConfirmZoneCrossingW( HWND hWnd, LPWSTR szUrlPrev, LPWSTR szUrlNew, BOOL bPost )
4412 {
4413 FIXME("(%p, %s, %s, %x) stub\n", hWnd, debugstr_w(szUrlPrev), debugstr_w(szUrlNew), bPost);
4414 return ERROR_SUCCESS;
4415 }
4416
4417 static DWORD zone_preference = 3;
4418
4419 /***********************************************************************
4420 * PrivacySetZonePreferenceW (WININET.@)
4421 */
4422 DWORD WINAPI PrivacySetZonePreferenceW( DWORD zone, DWORD type, DWORD template, LPCWSTR preference )
4423 {
4424 FIXME( "%x %x %x %s: stub\n", zone, type, template, debugstr_w(preference) );
4425
4426 zone_preference = template;
4427 return 0;
4428 }
4429
4430 /***********************************************************************
4431 * PrivacyGetZonePreferenceW (WININET.@)
4432 */
4433 DWORD WINAPI PrivacyGetZonePreferenceW( DWORD zone, DWORD type, LPDWORD template,
4434 LPWSTR preference, LPDWORD length )
4435 {
4436 FIXME( "%x %x %p %p %p: stub\n", zone, type, template, preference, length );
4437
4438 if (template) *template = zone_preference;
4439 return 0;
4440 }
4441
4442 /***********************************************************************
4443 * InternetGetSecurityInfoByURLA (WININET.@)
4444 */
4445 BOOL WINAPI InternetGetSecurityInfoByURLA(LPSTR lpszURL, PCCERT_CHAIN_CONTEXT *ppCertChain, DWORD *pdwSecureFlags)
4446 {
4447 WCHAR *url;
4448 BOOL res;
4449
4450 TRACE("(%s %p %p)\n", debugstr_a(lpszURL), ppCertChain, pdwSecureFlags);
4451
4452 url = heap_strdupAtoW(lpszURL);
4453 if(!url)
4454 return FALSE;
4455
4456 res = InternetGetSecurityInfoByURLW(url, ppCertChain, pdwSecureFlags);
4457 heap_free(url);
4458 return res;
4459 }
4460
4461 /***********************************************************************
4462 * InternetGetSecurityInfoByURLW (WININET.@)
4463 */
4464 BOOL WINAPI InternetGetSecurityInfoByURLW(LPCWSTR lpszURL, PCCERT_CHAIN_CONTEXT *ppCertChain, DWORD *pdwSecureFlags)
4465 {
4466 URL_COMPONENTSW url = {sizeof(url)};
4467 server_t *server;
4468 BOOL res;
4469
4470 TRACE("(%s %p %p)\n", debugstr_w(lpszURL), ppCertChain, pdwSecureFlags);
4471
4472 url.dwHostNameLength = 1;
4473 res = InternetCrackUrlW(lpszURL, 0, 0, &url);
4474 if(!res || url.nScheme != INTERNET_SCHEME_HTTPS) {
4475 SetLastError(ERROR_INTERNET_ITEM_NOT_FOUND);
4476 return FALSE;
4477 }
4478
4479 server = get_server(substr(url.lpszHostName, url.dwHostNameLength), url.nPort, TRUE, FALSE);
4480 if(!server) {
4481 SetLastError(ERROR_INTERNET_ITEM_NOT_FOUND);
4482 return FALSE;
4483 }
4484
4485 if(server->cert_chain) {
4486 const CERT_CHAIN_CONTEXT *chain_dup;
4487
4488 chain_dup = CertDuplicateCertificateChain(server->cert_chain);
4489 if(chain_dup) {
4490 *ppCertChain = chain_dup;
4491 *pdwSecureFlags = server->security_flags & _SECURITY_ERROR_FLAGS_MASK;
4492 }else {
4493 res = FALSE;
4494 }
4495 }else {
4496 SetLastError(ERROR_INTERNET_ITEM_NOT_FOUND);
4497 res = FALSE;
4498 }
4499
4500 server_release(server);
4501 return res;
4502 }
4503
4504 DWORD WINAPI InternetDialA( HWND hwndParent, LPSTR lpszConnectoid, DWORD dwFlags,
4505 DWORD_PTR* lpdwConnection, DWORD dwReserved )
4506 {
4507 FIXME("(%p, %p, 0x%08x, %p, 0x%08x) stub\n", hwndParent, lpszConnectoid, dwFlags,
4508 lpdwConnection, dwReserved);
4509 return ERROR_SUCCESS;
4510 }
4511
4512 DWORD WINAPI InternetDialW( HWND hwndParent, LPWSTR lpszConnectoid, DWORD dwFlags,
4513 DWORD_PTR* lpdwConnection, DWORD dwReserved )
4514 {
4515 FIXME("(%p, %p, 0x%08x, %p, 0x%08x) stub\n", hwndParent, lpszConnectoid, dwFlags,
4516 lpdwConnection, dwReserved);
4517 return ERROR_SUCCESS;
4518 }
4519
4520 BOOL WINAPI InternetGoOnlineA( LPSTR lpszURL, HWND hwndParent, DWORD dwReserved )
4521 {
4522 FIXME("(%s, %p, 0x%08x) stub\n", debugstr_a(lpszURL), hwndParent, dwReserved);
4523 return TRUE;
4524 }
4525
4526 BOOL WINAPI InternetGoOnlineW( LPWSTR lpszURL, HWND hwndParent, DWORD dwReserved )
4527 {
4528 FIXME("(%s, %p, 0x%08x) stub\n", debugstr_w(lpszURL), hwndParent, dwReserved);
4529 return TRUE;
4530 }
4531
4532 DWORD WINAPI InternetHangUp( DWORD_PTR dwConnection, DWORD dwReserved )
4533 {
4534 FIXME("(0x%08lx, 0x%08x) stub\n", dwConnection, dwReserved);
4535 return ERROR_SUCCESS;
4536 }
4537
4538 BOOL WINAPI CreateMD5SSOHash( PWSTR pszChallengeInfo, PWSTR pwszRealm, PWSTR pwszTarget,
4539 PBYTE pbHexHash )
4540 {
4541 FIXME("(%s, %s, %s, %p) stub\n", debugstr_w(pszChallengeInfo), debugstr_w(pwszRealm),
4542 debugstr_w(pwszTarget), pbHexHash);
4543 return FALSE;
4544 }
4545
4546 BOOL WINAPI ResumeSuspendedDownload( HINTERNET hInternet, DWORD dwError )
4547 {
4548 FIXME("(%p, 0x%08x) stub\n", hInternet, dwError);
4549 return FALSE;
4550 }
4551
4552 BOOL WINAPI InternetQueryFortezzaStatus(DWORD *a, DWORD_PTR b)
4553 {
4554 FIXME("(%p, %08lx) stub\n", a, b);
4555 return FALSE;
4556 }
4557
4558 DWORD WINAPI ShowClientAuthCerts(HWND parent)
4559 {
4560 FIXME("%p: stub\n", parent);
4561 return 0;
4562 }