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