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