Create a branch for console restructuration work.
[reactos.git] / dll / win32 / wininet / internet.c
1 /*
2 * Wininet
3 *
4 * Copyright 1999 Corel Corporation
5 * Copyright 2002 CodeWeavers Inc.
6 * Copyright 2002 Jaco Greeff
7 * Copyright 2002 TransGaming Technologies Inc.
8 * Copyright 2004 Mike McCormack for CodeWeavers
9 *
10 * Ulrich Czekalla
11 * Aric Stewart
12 * David Hammerton
13 *
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 */
28
29 #include "internet.h"
30
31 #define RESPONSE_TIMEOUT 30
32
33 typedef struct
34 {
35 DWORD dwError;
36 CHAR response[MAX_REPLY_LEN];
37 } WITHREADERROR, *LPWITHREADERROR;
38
39 static DWORD g_dwTlsErrIndex = TLS_OUT_OF_INDEXES;
40 HMODULE WININET_hModule;
41
42 static CRITICAL_SECTION WININET_cs;
43 static CRITICAL_SECTION_DEBUG WININET_cs_debug =
44 {
45 0, 0, &WININET_cs,
46 { &WININET_cs_debug.ProcessLocksList, &WININET_cs_debug.ProcessLocksList },
47 0, 0, { (DWORD_PTR)(__FILE__ ": WININET_cs") }
48 };
49 static CRITICAL_SECTION WININET_cs = { &WININET_cs_debug, -1, 0, 0, 0, 0 };
50
51 static object_header_t **handle_table;
52 static UINT_PTR next_handle;
53 static UINT_PTR handle_table_size;
54
55 typedef struct
56 {
57 DWORD proxyEnabled;
58 LPWSTR proxy;
59 LPWSTR proxyBypass;
60 } 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) > 0;
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 FALSE;
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_PROXY_BYPASS:
2874 heap_free(pi.proxyBypass);
2875 pi.proxyBypass = heap_strdupW(option->Value.pszValue);
2876 break;
2877
2878 case INTERNET_PER_CONN_AUTOCONFIG_URL:
2879 case INTERNET_PER_CONN_AUTODISCOVERY_FLAGS:
2880 case INTERNET_PER_CONN_AUTOCONFIG_SECONDARY_URL:
2881 case INTERNET_PER_CONN_AUTOCONFIG_RELOAD_DELAY_MINS:
2882 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_TIME:
2883 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_URL:
2884 FIXME("Unhandled dwOption %d\n", option->dwOption);
2885 break;
2886
2887 default:
2888 FIXME("Unknown dwOption %d\n", option->dwOption);
2889 SetLastError(ERROR_INVALID_PARAMETER);
2890 break;
2891 }
2892 }
2893
2894 if ((res = INTERNET_SaveProxySettings(&pi)))
2895 SetLastError(res);
2896
2897 FreeProxyInfo(&pi);
2898
2899 ret = (res == ERROR_SUCCESS);
2900 break;
2901 }
2902 default:
2903 FIXME("Option %d STUB\n",dwOption);
2904 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2905 ret = FALSE;
2906 break;
2907 }
2908
2909 if(lpwhh)
2910 WININET_Release( lpwhh );
2911
2912 return ret;
2913 }
2914
2915
2916 /***********************************************************************
2917 * InternetSetOptionA (WININET.@)
2918 *
2919 * Sets an options on the specified handle.
2920 *
2921 * RETURNS
2922 * TRUE on success
2923 * FALSE on failure
2924 *
2925 */
2926 BOOL WINAPI InternetSetOptionA(HINTERNET hInternet, DWORD dwOption,
2927 LPVOID lpBuffer, DWORD dwBufferLength)
2928 {
2929 LPVOID wbuffer;
2930 DWORD wlen;
2931 BOOL r;
2932
2933 switch( dwOption )
2934 {
2935 case INTERNET_OPTION_PROXY:
2936 {
2937 LPINTERNET_PROXY_INFOA pi = (LPINTERNET_PROXY_INFOA) lpBuffer;
2938 LPINTERNET_PROXY_INFOW piw;
2939 DWORD proxlen, prbylen;
2940 LPWSTR prox, prby;
2941
2942 proxlen = MultiByteToWideChar( CP_ACP, 0, pi->lpszProxy, -1, NULL, 0);
2943 prbylen= MultiByteToWideChar( CP_ACP, 0, pi->lpszProxyBypass, -1, NULL, 0);
2944 wlen = sizeof(*piw) + proxlen + prbylen;
2945 wbuffer = heap_alloc(wlen*sizeof(WCHAR) );
2946 piw = (LPINTERNET_PROXY_INFOW) wbuffer;
2947 piw->dwAccessType = pi->dwAccessType;
2948 prox = (LPWSTR) &piw[1];
2949 prby = &prox[proxlen+1];
2950 MultiByteToWideChar( CP_ACP, 0, pi->lpszProxy, -1, prox, proxlen);
2951 MultiByteToWideChar( CP_ACP, 0, pi->lpszProxyBypass, -1, prby, prbylen);
2952 piw->lpszProxy = prox;
2953 piw->lpszProxyBypass = prby;
2954 }
2955 break;
2956 case INTERNET_OPTION_USER_AGENT:
2957 case INTERNET_OPTION_USERNAME:
2958 case INTERNET_OPTION_PASSWORD:
2959 case INTERNET_OPTION_PROXY_USERNAME:
2960 case INTERNET_OPTION_PROXY_PASSWORD:
2961 wlen = MultiByteToWideChar( CP_ACP, 0, lpBuffer, -1, NULL, 0 );
2962 if (!(wbuffer = heap_alloc( wlen * sizeof(WCHAR) ))) return ERROR_OUTOFMEMORY;
2963 MultiByteToWideChar( CP_ACP, 0, lpBuffer, -1, wbuffer, wlen );
2964 break;
2965 case INTERNET_OPTION_PER_CONNECTION_OPTION: {
2966 unsigned int i;
2967 INTERNET_PER_CONN_OPTION_LISTW *listW;
2968 INTERNET_PER_CONN_OPTION_LISTA *listA = lpBuffer;
2969 wlen = sizeof(INTERNET_PER_CONN_OPTION_LISTW);
2970 wbuffer = heap_alloc(wlen);
2971 listW = wbuffer;
2972
2973 listW->dwSize = sizeof(INTERNET_PER_CONN_OPTION_LISTW);
2974 if (listA->pszConnection)
2975 {
2976 wlen = MultiByteToWideChar( CP_ACP, 0, listA->pszConnection, -1, NULL, 0 );
2977 listW->pszConnection = heap_alloc(wlen*sizeof(WCHAR));
2978 MultiByteToWideChar( CP_ACP, 0, listA->pszConnection, -1, listW->pszConnection, wlen );
2979 }
2980 else
2981 listW->pszConnection = NULL;
2982 listW->dwOptionCount = listA->dwOptionCount;
2983 listW->dwOptionError = listA->dwOptionError;
2984 listW->pOptions = heap_alloc(sizeof(INTERNET_PER_CONN_OPTIONW) * listA->dwOptionCount);
2985
2986 for (i = 0; i < listA->dwOptionCount; ++i) {
2987 INTERNET_PER_CONN_OPTIONA *optA = listA->pOptions + i;
2988 INTERNET_PER_CONN_OPTIONW *optW = listW->pOptions + i;
2989
2990 optW->dwOption = optA->dwOption;
2991
2992 switch (optA->dwOption) {
2993 case INTERNET_PER_CONN_AUTOCONFIG_URL:
2994 case INTERNET_PER_CONN_PROXY_BYPASS:
2995 case INTERNET_PER_CONN_PROXY_SERVER:
2996 case INTERNET_PER_CONN_AUTOCONFIG_SECONDARY_URL:
2997 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_URL:
2998 if (optA->Value.pszValue)
2999 {
3000 wlen = MultiByteToWideChar( CP_ACP, 0, optA->Value.pszValue, -1, NULL, 0 );
3001 optW->Value.pszValue = heap_alloc(wlen*sizeof(WCHAR));
3002 MultiByteToWideChar( CP_ACP, 0, optA->Value.pszValue, -1, optW->Value.pszValue, wlen );
3003 }
3004 else
3005 optW->Value.pszValue = NULL;
3006 break;
3007 case INTERNET_PER_CONN_AUTODISCOVERY_FLAGS:
3008 case INTERNET_PER_CONN_FLAGS:
3009 case INTERNET_PER_CONN_AUTOCONFIG_RELOAD_DELAY_MINS:
3010 optW->Value.dwValue = optA->Value.dwValue;
3011 break;
3012 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_TIME:
3013 optW->Value.ftValue = optA->Value.ftValue;
3014 break;
3015 default:
3016 WARN("Unknown PER_CONN dwOption: %d, guessing at conversion to Wide\n", optA->dwOption);
3017 optW->Value.dwValue = optA->Value.dwValue;
3018 break;
3019 }
3020 }
3021 }
3022 break;
3023 default:
3024 wbuffer = lpBuffer;
3025 wlen = dwBufferLength;
3026 }
3027
3028 r = InternetSetOptionW(hInternet,dwOption, wbuffer, wlen);
3029
3030 if( lpBuffer != wbuffer )
3031 {
3032 if (dwOption == INTERNET_OPTION_PER_CONNECTION_OPTION)
3033 {
3034 INTERNET_PER_CONN_OPTION_LISTW *list = wbuffer;
3035 unsigned int i;
3036 for (i = 0; i < list->dwOptionCount; ++i) {
3037 INTERNET_PER_CONN_OPTIONW *opt = list->pOptions + i;
3038 switch (opt->dwOption) {
3039 case INTERNET_PER_CONN_AUTOCONFIG_URL:
3040 case INTERNET_PER_CONN_PROXY_BYPASS:
3041 case INTERNET_PER_CONN_PROXY_SERVER:
3042 case INTERNET_PER_CONN_AUTOCONFIG_SECONDARY_URL:
3043 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_URL:
3044 heap_free( opt->Value.pszValue );
3045 break;
3046 default:
3047 break;
3048 }
3049 }
3050 heap_free( list->pOptions );
3051 }
3052 heap_free( wbuffer );
3053 }
3054
3055 return r;
3056 }
3057
3058
3059 /***********************************************************************
3060 * InternetSetOptionExA (WININET.@)
3061 */
3062 BOOL WINAPI InternetSetOptionExA(HINTERNET hInternet, DWORD dwOption,
3063 LPVOID lpBuffer, DWORD dwBufferLength, DWORD dwFlags)
3064 {
3065 FIXME("Flags %08x ignored\n", dwFlags);
3066 return InternetSetOptionA( hInternet, dwOption, lpBuffer, dwBufferLength );
3067 }
3068
3069 /***********************************************************************
3070 * InternetSetOptionExW (WININET.@)
3071 */
3072 BOOL WINAPI InternetSetOptionExW(HINTERNET hInternet, DWORD dwOption,
3073 LPVOID lpBuffer, DWORD dwBufferLength, DWORD dwFlags)
3074 {
3075 FIXME("Flags %08x ignored\n", dwFlags);
3076 if( dwFlags & ~ISO_VALID_FLAGS )
3077 {
3078 SetLastError( ERROR_INVALID_PARAMETER );
3079 return FALSE;
3080 }
3081 return InternetSetOptionW( hInternet, dwOption, lpBuffer, dwBufferLength );
3082 }
3083
3084 static const WCHAR WININET_wkday[7][4] =
3085 { { 'S','u','n', 0 }, { 'M','o','n', 0 }, { 'T','u','e', 0 }, { 'W','e','d', 0 },
3086 { 'T','h','u', 0 }, { 'F','r','i', 0 }, { 'S','a','t', 0 } };
3087 static const WCHAR WININET_month[12][4] =
3088 { { 'J','a','n', 0 }, { 'F','e','b', 0 }, { 'M','a','r', 0 }, { 'A','p','r', 0 },
3089 { 'M','a','y', 0 }, { 'J','u','n', 0 }, { 'J','u','l', 0 }, { 'A','u','g', 0 },
3090 { 'S','e','p', 0 }, { 'O','c','t', 0 }, { 'N','o','v', 0 }, { 'D','e','c', 0 } };
3091
3092 /***********************************************************************
3093 * InternetTimeFromSystemTimeA (WININET.@)
3094 */
3095 BOOL WINAPI InternetTimeFromSystemTimeA( const SYSTEMTIME* time, DWORD format, LPSTR string, DWORD size )
3096 {
3097 BOOL ret;
3098 WCHAR stringW[INTERNET_RFC1123_BUFSIZE];
3099
3100 TRACE( "%p 0x%08x %p 0x%08x\n", time, format, string, size );
3101
3102 if (!time || !string || format != INTERNET_RFC1123_FORMAT)
3103 {
3104 SetLastError(ERROR_INVALID_PARAMETER);
3105 return FALSE;
3106 }
3107
3108 if (size < INTERNET_RFC1123_BUFSIZE * sizeof(*string))
3109 {
3110 SetLastError(ERROR_INSUFFICIENT_BUFFER);
3111 return FALSE;
3112 }
3113
3114 ret = InternetTimeFromSystemTimeW( time, format, stringW, sizeof(stringW) );
3115 if (ret) WideCharToMultiByte( CP_ACP, 0, stringW, -1, string, size, NULL, NULL );
3116
3117 return ret;
3118 }
3119
3120 /***********************************************************************
3121 * InternetTimeFromSystemTimeW (WININET.@)
3122 */
3123 BOOL WINAPI InternetTimeFromSystemTimeW( const SYSTEMTIME* time, DWORD format, LPWSTR string, DWORD size )
3124 {
3125 static const WCHAR date[] =
3126 { '%','s',',',' ','%','0','2','d',' ','%','s',' ','%','4','d',' ','%','0',
3127 '2','d',':','%','0','2','d',':','%','0','2','d',' ','G','M','T', 0 };
3128
3129 TRACE( "%p 0x%08x %p 0x%08x\n", time, format, string, size );
3130
3131 if (!time || !string || format != INTERNET_RFC1123_FORMAT)
3132 {
3133 SetLastError(ERROR_INVALID_PARAMETER);
3134 return FALSE;
3135 }
3136
3137 if (size < INTERNET_RFC1123_BUFSIZE * sizeof(*string))
3138 {
3139 SetLastError(ERROR_INSUFFICIENT_BUFFER);
3140 return FALSE;
3141 }
3142
3143 sprintfW( string, date,
3144 WININET_wkday[time->wDayOfWeek],
3145 time->wDay,
3146 WININET_month[time->wMonth - 1],
3147 time->wYear,
3148 time->wHour,
3149 time->wMinute,
3150 time->wSecond );
3151
3152 return TRUE;
3153 }
3154
3155 /***********************************************************************
3156 * InternetTimeToSystemTimeA (WININET.@)
3157 */
3158 BOOL WINAPI InternetTimeToSystemTimeA( LPCSTR string, SYSTEMTIME* time, DWORD reserved )
3159 {
3160 BOOL ret = FALSE;
3161 WCHAR *stringW;
3162
3163 TRACE( "%s %p 0x%08x\n", debugstr_a(string), time, reserved );
3164
3165 stringW = heap_strdupAtoW(string);
3166 if (stringW)
3167 {
3168 ret = InternetTimeToSystemTimeW( stringW, time, reserved );
3169 heap_free( stringW );
3170 }
3171 return ret;
3172 }
3173
3174 /***********************************************************************
3175 * InternetTimeToSystemTimeW (WININET.@)
3176 */
3177 BOOL WINAPI InternetTimeToSystemTimeW( LPCWSTR string, SYSTEMTIME* time, DWORD reserved )
3178 {
3179 unsigned int i;
3180 const WCHAR *s = string;
3181 WCHAR *end;
3182
3183 TRACE( "%s %p 0x%08x\n", debugstr_w(string), time, reserved );
3184
3185 if (!string || !time) return FALSE;
3186
3187 /* Windows does this too */
3188 GetSystemTime( time );
3189
3190 /* Convert an RFC1123 time such as 'Fri, 07 Jan 2005 12:06:35 GMT' into
3191 * a SYSTEMTIME structure.
3192 */
3193
3194 while (*s && !isalphaW( *s )) s++;
3195 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
3196 time->wDayOfWeek = 7;
3197
3198 for (i = 0; i < 7; i++)
3199 {
3200 if (toupperW( WININET_wkday[i][0] ) == toupperW( s[0] ) &&
3201 toupperW( WININET_wkday[i][1] ) == toupperW( s[1] ) &&
3202 toupperW( WININET_wkday[i][2] ) == toupperW( s[2] ) )
3203 {
3204 time->wDayOfWeek = i;
3205 break;
3206 }
3207 }
3208
3209 if (time->wDayOfWeek > 6) return TRUE;
3210 while (*s && !isdigitW( *s )) s++;
3211 time->wDay = strtolW( s, &end, 10 );
3212 s = end;
3213
3214 while (*s && !isalphaW( *s )) s++;
3215 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
3216 time->wMonth = 0;
3217
3218 for (i = 0; i < 12; i++)
3219 {
3220 if (toupperW( WININET_month[i][0]) == toupperW( s[0] ) &&
3221 toupperW( WININET_month[i][1]) == toupperW( s[1] ) &&
3222 toupperW( WININET_month[i][2]) == toupperW( s[2] ) )
3223 {
3224 time->wMonth = i + 1;
3225 break;
3226 }
3227 }
3228 if (time->wMonth == 0) return TRUE;
3229
3230 while (*s && !isdigitW( *s )) s++;
3231 if (*s == '\0') return TRUE;
3232 time->wYear = strtolW( s, &end, 10 );
3233 s = end;
3234
3235 while (*s && !isdigitW( *s )) s++;
3236 if (*s == '\0') return TRUE;
3237 time->wHour = strtolW( s, &end, 10 );
3238 s = end;
3239
3240 while (*s && !isdigitW( *s )) s++;
3241 if (*s == '\0') return TRUE;
3242 time->wMinute = strtolW( s, &end, 10 );
3243 s = end;
3244
3245 while (*s && !isdigitW( *s )) s++;
3246 if (*s == '\0') return TRUE;
3247 time->wSecond = strtolW( s, &end, 10 );
3248 s = end;
3249
3250 time->wMilliseconds = 0;
3251 return TRUE;
3252 }
3253
3254 /***********************************************************************
3255 * InternetCheckConnectionW (WININET.@)
3256 *
3257 * Pings a requested host to check internet connection
3258 *
3259 * RETURNS
3260 * TRUE on success and FALSE on failure. If a failure then
3261 * ERROR_NOT_CONNECTED is placed into GetLastError
3262 *
3263 */
3264 BOOL WINAPI InternetCheckConnectionW( LPCWSTR lpszUrl, DWORD dwFlags, DWORD dwReserved )
3265 {
3266 /*
3267 * this is a kludge which runs the resident ping program and reads the output.
3268 *
3269 * Anyone have a better idea?
3270 */
3271
3272 BOOL rc = FALSE;
3273 static const CHAR ping[] = "ping -c 1 ";
3274 static const CHAR redirect[] = " >/dev/null 2>/dev/null";
3275 CHAR *command = NULL;
3276 WCHAR hostW[INTERNET_MAX_HOST_NAME_LENGTH];
3277 DWORD len;
3278 INTERNET_PORT port;
3279 int status = -1;
3280
3281 FIXME("\n");
3282
3283 /*
3284 * Crack or set the Address
3285 */
3286 if (lpszUrl == NULL)
3287 {
3288 /*
3289 * According to the doc we are supposed to use the ip for the next
3290 * server in the WnInet internal server database. I have
3291 * no idea what that is or how to get it.
3292 *
3293 * So someone needs to implement this.
3294 */
3295 FIXME("Unimplemented with URL of NULL\n");
3296 return TRUE;
3297 }
3298 else
3299 {
3300 URL_COMPONENTSW components;
3301
3302 ZeroMemory(&components,sizeof(URL_COMPONENTSW));
3303 components.lpszHostName = (LPWSTR)hostW;
3304 components.dwHostNameLength = INTERNET_MAX_HOST_NAME_LENGTH;
3305
3306 if (!InternetCrackUrlW(lpszUrl,0,0,&components))
3307 goto End;
3308
3309 TRACE("host name : %s\n",debugstr_w(components.lpszHostName));
3310 port = components.nPort;
3311 TRACE("port: %d\n", port);
3312 }
3313
3314 if (dwFlags & FLAG_ICC_FORCE_CONNECTION)
3315 {
3316 struct sockaddr_storage saddr;
3317 socklen_t sa_len = sizeof(saddr);
3318 int fd;
3319
3320 if (!GetAddress(hostW, port, (struct sockaddr *)&saddr, &sa_len))
3321 goto End;
3322 fd = socket(saddr.ss_family, SOCK_STREAM, 0);
3323 if (fd != -1)
3324 {
3325 if (connect(fd, (struct sockaddr *)&saddr, sa_len) == 0)
3326 rc = TRUE;
3327 close(fd);
3328 }
3329 }
3330 else
3331 {
3332 /*
3333 * Build our ping command
3334 */
3335 len = WideCharToMultiByte(CP_UNIXCP, 0, hostW, -1, NULL, 0, NULL, NULL);
3336 command = heap_alloc(strlen(ping)+len+strlen(redirect));
3337 strcpy(command,ping);
3338 WideCharToMultiByte(CP_UNIXCP, 0, hostW, -1, command+strlen(ping), len, NULL, NULL);
3339 strcat(command,redirect);
3340
3341 TRACE("Ping command is : %s\n",command);
3342
3343 status = system(command);
3344
3345 TRACE("Ping returned a code of %i\n",status);
3346
3347 /* Ping return code of 0 indicates success */
3348 if (status == 0)
3349 rc = TRUE;
3350 }
3351
3352 End:
3353 heap_free( command );
3354 if (rc == FALSE)
3355 INTERNET_SetLastError(ERROR_NOT_CONNECTED);
3356
3357 return rc;
3358 }
3359
3360
3361 /***********************************************************************
3362 * InternetCheckConnectionA (WININET.@)
3363 *
3364 * Pings a requested host to check internet connection
3365 *
3366 * RETURNS
3367 * TRUE on success and FALSE on failure. If a failure then
3368 * ERROR_NOT_CONNECTED is placed into GetLastError
3369 *
3370 */
3371 BOOL WINAPI InternetCheckConnectionA(LPCSTR lpszUrl, DWORD dwFlags, DWORD dwReserved)
3372 {
3373 WCHAR *url = NULL;
3374 BOOL rc;
3375
3376 if(lpszUrl) {
3377 url = heap_strdupAtoW(lpszUrl);
3378 if(!url)
3379 return FALSE;
3380 }
3381
3382 rc = InternetCheckConnectionW(url, dwFlags, dwReserved);
3383
3384 heap_free(url);
3385 return rc;
3386 }
3387
3388
3389 /**********************************************************
3390 * INTERNET_InternetOpenUrlW (internal)
3391 *
3392 * Opens an URL
3393 *
3394 * RETURNS
3395 * handle of connection or NULL on failure
3396 */
3397 static HINTERNET INTERNET_InternetOpenUrlW(appinfo_t *hIC, LPCWSTR lpszUrl,
3398 LPCWSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext)
3399 {
3400 URL_COMPONENTSW urlComponents;
3401 WCHAR protocol[INTERNET_MAX_SCHEME_LENGTH];
3402 WCHAR hostName[INTERNET_MAX_HOST_NAME_LENGTH];
3403 WCHAR userName[INTERNET_MAX_USER_NAME_LENGTH];
3404 WCHAR password[INTERNET_MAX_PASSWORD_LENGTH];
3405 WCHAR path[INTERNET_MAX_PATH_LENGTH];
3406 WCHAR extra[1024];
3407 HINTERNET client = NULL, client1 = NULL;
3408 DWORD res;
3409
3410 TRACE("(%p, %s, %s, %08x, %08x, %08lx)\n", hIC, debugstr_w(lpszUrl), debugstr_w(lpszHeaders),
3411 dwHeadersLength, dwFlags, dwContext);
3412
3413 urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
3414 urlComponents.lpszScheme = protocol;
3415 urlComponents.dwSchemeLength = INTERNET_MAX_SCHEME_LENGTH;
3416 urlComponents.lpszHostName = hostName;
3417 urlComponents.dwHostNameLength = INTERNET_MAX_HOST_NAME_LENGTH;
3418 urlComponents.lpszUserName = userName;
3419 urlComponents.dwUserNameLength = INTERNET_MAX_USER_NAME_LENGTH;
3420 urlComponents.lpszPassword = password;
3421 urlComponents.dwPasswordLength = INTERNET_MAX_PASSWORD_LENGTH;
3422 urlComponents.lpszUrlPath = path;
3423 urlComponents.dwUrlPathLength = INTERNET_MAX_PATH_LENGTH;
3424 urlComponents.lpszExtraInfo = extra;
3425 urlComponents.dwExtraInfoLength = 1024;
3426 if(!InternetCrackUrlW(lpszUrl, strlenW(lpszUrl), 0, &urlComponents))
3427 return NULL;
3428 switch(urlComponents.nScheme) {
3429 case INTERNET_SCHEME_FTP:
3430 if(urlComponents.nPort == 0)
3431 urlComponents.nPort = INTERNET_DEFAULT_FTP_PORT;
3432 client = FTP_Connect(hIC, hostName, urlComponents.nPort,
3433 userName, password, dwFlags, dwContext, INET_OPENURL);
3434 if(client == NULL)
3435 break;
3436 client1 = FtpOpenFileW(client, path, GENERIC_READ, dwFlags, dwContext);
3437 if(client1 == NULL) {
3438 InternetCloseHandle(client);
3439 break;
3440 }
3441 break;
3442
3443 case INTERNET_SCHEME_HTTP:
3444 case INTERNET_SCHEME_HTTPS: {
3445 static const WCHAR szStars[] = { '*','/','*', 0 };
3446 LPCWSTR accept[2] = { szStars, NULL };
3447 if(urlComponents.nPort == 0) {
3448 if(urlComponents.nScheme == INTERNET_SCHEME_HTTP)
3449 urlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
3450 else
3451 urlComponents.nPort = INTERNET_DEFAULT_HTTPS_PORT;
3452 }
3453 if (urlComponents.nScheme == INTERNET_SCHEME_HTTPS) dwFlags |= INTERNET_FLAG_SECURE;
3454
3455 /* FIXME: should use pointers, not handles, as handles are not thread-safe */
3456 res = HTTP_Connect(hIC, hostName, urlComponents.nPort,
3457 userName, password, dwFlags, dwContext, INET_OPENURL, &client);
3458 if(res != ERROR_SUCCESS) {
3459 INTERNET_SetLastError(res);
3460 break;
3461 }
3462
3463 if (urlComponents.dwExtraInfoLength) {
3464 WCHAR *path_extra;
3465 DWORD len = urlComponents.dwUrlPathLength + urlComponents.dwExtraInfoLength + 1;
3466
3467 if (!(path_extra = heap_alloc(len * sizeof(WCHAR))))
3468 {
3469 InternetCloseHandle(client);
3470 break;
3471 }
3472 strcpyW(path_extra, urlComponents.lpszUrlPath);
3473 strcatW(path_extra, urlComponents.lpszExtraInfo);
3474 client1 = HttpOpenRequestW(client, NULL, path_extra, NULL, NULL, accept, dwFlags, dwContext);
3475 heap_free(path_extra);
3476 }
3477 else
3478 client1 = HttpOpenRequestW(client, NULL, path, NULL, NULL, accept, dwFlags, dwContext);
3479
3480 if(client1 == NULL) {
3481 InternetCloseHandle(client);
3482 break;
3483 }
3484 HttpAddRequestHeadersW(client1, lpszHeaders, dwHeadersLength, HTTP_ADDREQ_FLAG_ADD);
3485 if (!HttpSendRequestW(client1, NULL, 0, NULL, 0) &&
3486 GetLastError() != ERROR_IO_PENDING) {
3487 InternetCloseHandle(client1);
3488 client1 = NULL;
3489 break;
3490 }
3491 }
3492 case INTERNET_SCHEME_GOPHER:
3493 /* gopher doesn't seem to be implemented in wine, but it's supposed
3494 * to be supported by InternetOpenUrlA. */
3495 default:
3496 SetLastError(ERROR_INTERNET_UNRECOGNIZED_SCHEME);
3497 break;
3498 }
3499
3500 TRACE(" %p <--\n", client1);
3501
3502 return client1;
3503 }
3504
3505 /**********************************************************
3506 * InternetOpenUrlW (WININET.@)
3507 *
3508 * Opens an URL
3509 *
3510 * RETURNS
3511 * handle of connection or NULL on failure
3512 */
3513 typedef struct {
3514 task_header_t hdr;
3515 WCHAR *url;
3516 WCHAR *headers;
3517 DWORD headers_len;
3518 DWORD flags;
3519 DWORD_PTR context;
3520 } open_url_task_t;
3521
3522 static void AsyncInternetOpenUrlProc(task_header_t *hdr)
3523 {
3524 open_url_task_t *task = (open_url_task_t*)hdr;
3525
3526 TRACE("%p\n", task->hdr.hdr);
3527
3528 INTERNET_InternetOpenUrlW((appinfo_t*)task->hdr.hdr, task->url, task->headers,
3529 task->headers_len, task->flags, task->context);
3530 heap_free(task->url);
3531 heap_free(task->headers);
3532 }
3533
3534 HINTERNET WINAPI InternetOpenUrlW(HINTERNET hInternet, LPCWSTR lpszUrl,
3535 LPCWSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext)
3536 {
3537 HINTERNET ret = NULL;
3538 appinfo_t *hIC = NULL;
3539
3540 if (TRACE_ON(wininet)) {
3541 TRACE("(%p, %s, %s, %08x, %08x, %08lx)\n", hInternet, debugstr_w(lpszUrl), debugstr_w(lpszHeaders),
3542 dwHeadersLength, dwFlags, dwContext);
3543 TRACE(" flags :");
3544 dump_INTERNET_FLAGS(dwFlags);
3545 }
3546
3547 if (!lpszUrl)
3548 {
3549 SetLastError(ERROR_INVALID_PARAMETER);
3550 goto lend;
3551 }
3552
3553 hIC = (appinfo_t*)get_handle_object( hInternet );
3554 if (NULL == hIC || hIC->hdr.htype != WH_HINIT) {
3555 SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
3556 goto lend;
3557 }
3558
3559 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC) {
3560 open_url_task_t *task;
3561
3562 task = alloc_async_task(&hIC->hdr, AsyncInternetOpenUrlProc, sizeof(*task));
3563 task->url = heap_strdupW(lpszUrl);
3564 task->headers = heap_strdupW(lpszHeaders);
3565 task->headers_len = dwHeadersLength;
3566 task->flags = dwFlags;
3567 task->context = dwContext;
3568
3569 INTERNET_AsyncCall(&task->hdr);
3570 SetLastError(ERROR_IO_PENDING);
3571 } else {
3572 ret = INTERNET_InternetOpenUrlW(hIC, lpszUrl, lpszHeaders, dwHeadersLength, dwFlags, dwContext);
3573 }
3574
3575 lend:
3576 if( hIC )
3577 WININET_Release( &hIC->hdr );
3578 TRACE(" %p <--\n", ret);
3579
3580 return ret;
3581 }
3582
3583 /**********************************************************
3584 * InternetOpenUrlA (WININET.@)
3585 *
3586 * Opens an URL
3587 *
3588 * RETURNS
3589 * handle of connection or NULL on failure
3590 */
3591 HINTERNET WINAPI InternetOpenUrlA(HINTERNET hInternet, LPCSTR lpszUrl,
3592 LPCSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext)
3593 {
3594 HINTERNET rc = NULL;
3595 DWORD lenHeaders = 0;
3596 LPWSTR szUrl = NULL;
3597 LPWSTR szHeaders = NULL;
3598
3599 TRACE("\n");
3600
3601 if(lpszUrl) {
3602 szUrl = heap_strdupAtoW(lpszUrl);
3603 if(!szUrl)
3604 return NULL;
3605 }
3606
3607 if(lpszHeaders) {
3608 lenHeaders = MultiByteToWideChar(CP_ACP, 0, lpszHeaders, dwHeadersLength, NULL, 0 );
3609 szHeaders = heap_alloc(lenHeaders*sizeof(WCHAR));
3610 if(!szHeaders) {
3611 heap_free(szUrl);
3612 return NULL;
3613 }
3614 MultiByteToWideChar(CP_ACP, 0, lpszHeaders, dwHeadersLength, szHeaders, lenHeaders);
3615 }
3616
3617 rc = InternetOpenUrlW(hInternet, szUrl, szHeaders,
3618 lenHeaders, dwFlags, dwContext);
3619
3620 heap_free(szUrl);
3621 heap_free(szHeaders);
3622 return rc;
3623 }
3624
3625
3626 static LPWITHREADERROR INTERNET_AllocThreadError(void)
3627 {
3628 LPWITHREADERROR lpwite = heap_alloc(sizeof(*lpwite));
3629
3630 if (lpwite)
3631 {
3632 lpwite->dwError = 0;
3633 lpwite->response[0] = '\0';
3634 }
3635
3636 if (!TlsSetValue(g_dwTlsErrIndex, lpwite))
3637 {
3638 heap_free(lpwite);
3639 return NULL;
3640 }
3641 return lpwite;
3642 }
3643
3644
3645 /***********************************************************************
3646 * INTERNET_SetLastError (internal)
3647 *
3648 * Set last thread specific error
3649 *
3650 * RETURNS
3651 *
3652 */
3653 void INTERNET_SetLastError(DWORD dwError)
3654 {
3655 LPWITHREADERROR lpwite = TlsGetValue(g_dwTlsErrIndex);
3656
3657 if (!lpwite)
3658 lpwite = INTERNET_AllocThreadError();
3659
3660 SetLastError(dwError);
3661 if(lpwite)
3662 lpwite->dwError = dwError;
3663 }
3664
3665
3666 /***********************************************************************
3667 * INTERNET_GetLastError (internal)
3668 *
3669 * Get last thread specific error
3670 *
3671 * RETURNS
3672 *
3673 */
3674 DWORD INTERNET_GetLastError(void)
3675 {
3676 LPWITHREADERROR lpwite = TlsGetValue(g_dwTlsErrIndex);
3677 if (!lpwite) return 0;
3678 /* TlsGetValue clears last error, so set it again here */
3679 SetLastError(lpwite->dwError);
3680 return lpwite->dwError;
3681 }
3682
3683
3684 /***********************************************************************
3685 * INTERNET_WorkerThreadFunc (internal)
3686 *
3687 * Worker thread execution function
3688 *
3689 * RETURNS
3690 *
3691 */
3692 static DWORD CALLBACK INTERNET_WorkerThreadFunc(LPVOID lpvParam)
3693 {
3694 task_header_t *task = lpvParam;
3695
3696 TRACE("\n");
3697
3698 task->proc(task);
3699 WININET_Release(task->hdr);
3700 heap_free(task);
3701
3702 if (g_dwTlsErrIndex != TLS_OUT_OF_INDEXES)
3703 {
3704 heap_free(TlsGetValue(g_dwTlsErrIndex));
3705 TlsSetValue(g_dwTlsErrIndex, NULL);
3706 }
3707 return TRUE;
3708 }
3709
3710 void *alloc_async_task(object_header_t *hdr, async_task_proc_t proc, size_t size)
3711 {
3712 task_header_t *task;
3713
3714 task = heap_alloc(size);
3715 if(!task)
3716 return NULL;
3717
3718 task->hdr = WININET_AddRef(hdr);
3719 task->proc = proc;
3720 return task;
3721 }
3722
3723 /***********************************************************************
3724 * INTERNET_AsyncCall (internal)
3725 *
3726 * Retrieves work request from queue
3727 *
3728 * RETURNS
3729 *
3730 */
3731 DWORD INTERNET_AsyncCall(task_header_t *task)
3732 {
3733 BOOL bSuccess;
3734
3735 TRACE("\n");
3736
3737 bSuccess = QueueUserWorkItem(INTERNET_WorkerThreadFunc, task, WT_EXECUTELONGFUNCTION);
3738 if (!bSuccess)
3739 {
3740 heap_free(task);
3741 return ERROR_INTERNET_ASYNC_THREAD_FAILED;
3742 }
3743 return ERROR_SUCCESS;
3744 }
3745
3746
3747 /***********************************************************************
3748 * INTERNET_GetResponseBuffer (internal)
3749 *
3750 * RETURNS
3751 *
3752 */
3753 LPSTR INTERNET_GetResponseBuffer(void)
3754 {
3755 LPWITHREADERROR lpwite = TlsGetValue(g_dwTlsErrIndex);
3756 if (!lpwite)
3757 lpwite = INTERNET_AllocThreadError();
3758 TRACE("\n");
3759 return lpwite->response;
3760 }
3761
3762 /***********************************************************************
3763 * INTERNET_GetNextLine (internal)
3764 *
3765 * Parse next line in directory string listing
3766 *
3767 * RETURNS
3768 * Pointer to beginning of next line
3769 * NULL on failure
3770 *
3771 */
3772
3773 LPSTR INTERNET_GetNextLine(INT nSocket, LPDWORD dwLen)
3774 {
3775 // ReactOS: use select instead of poll
3776 fd_set infd;
3777 struct timeval tv;
3778 BOOL bSuccess = FALSE;
3779 INT nRecv = 0;
3780 LPSTR lpszBuffer = INTERNET_GetResponseBuffer();
3781
3782 TRACE("\n");
3783
3784 FD_ZERO(&infd);
3785 FD_SET(nSocket,&infd);
3786 tv.tv_sec = RESPONSE_TIMEOUT;
3787 tv.tv_usec = 0;
3788
3789 while (nRecv < MAX_REPLY_LEN)
3790 {
3791 if (select(0, &infd, NULL, NULL, &tv) > 0)
3792 {
3793 if (recv(nSocket, &lpszBuffer[nRecv], 1, 0) <= 0)
3794 {
3795 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
3796 goto lend;
3797 }
3798
3799 if (lpszBuffer[nRecv] == '\n')
3800 {
3801 bSuccess = TRUE;
3802 break;
3803 }
3804 if (lpszBuffer[nRecv] != '\r')
3805 nRecv++;
3806 }
3807 else
3808 {
3809 INTERNET_SetLastError(ERROR_INTERNET_TIMEOUT);
3810 goto lend;
3811 }
3812 }
3813
3814 lend:
3815 if (bSuccess)
3816 {
3817 lpszBuffer[nRecv] = '\0';
3818 *dwLen = nRecv - 1;
3819 TRACE(":%d %s\n", nRecv, lpszBuffer);
3820 return lpszBuffer;
3821 }
3822 else
3823 {
3824 return NULL;
3825 }
3826 }
3827
3828 /**********************************************************
3829 * InternetQueryDataAvailable (WININET.@)
3830 *
3831 * Determines how much data is available to be read.
3832 *
3833 * RETURNS
3834 * TRUE on success, FALSE if an error occurred. If
3835 * INTERNET_FLAG_ASYNC was specified in InternetOpen, and
3836 * no data is presently available, FALSE is returned with
3837 * the last error ERROR_IO_PENDING; a callback with status
3838 * INTERNET_STATUS_REQUEST_COMPLETE will be sent when more
3839 * data is available.
3840 */
3841 BOOL WINAPI InternetQueryDataAvailable( HINTERNET hFile,
3842 LPDWORD lpdwNumberOfBytesAvailable,
3843 DWORD dwFlags, DWORD_PTR dwContext)
3844 {
3845 object_header_t *hdr;
3846 DWORD res;
3847
3848 TRACE("(%p %p %x %lx)\n", hFile, lpdwNumberOfBytesAvailable, dwFlags, dwContext);
3849
3850 hdr = get_handle_object( hFile );
3851 if (!hdr) {
3852 SetLastError(ERROR_INVALID_HANDLE);
3853 return FALSE;
3854 }
3855
3856 if(hdr->vtbl->QueryDataAvailable) {
3857 res = hdr->vtbl->QueryDataAvailable(hdr, lpdwNumberOfBytesAvailable, dwFlags, dwContext);
3858 }else {
3859 WARN("wrong handle\n");
3860 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
3861 }
3862
3863 WININET_Release(hdr);
3864
3865 if(res != ERROR_SUCCESS)
3866 SetLastError(res);
3867 return res == ERROR_SUCCESS;
3868 }
3869
3870 DWORD create_req_file(const WCHAR *file_name, req_file_t **ret)
3871 {
3872 req_file_t *req_file;
3873
3874 req_file = heap_alloc_zero(sizeof(*req_file));
3875 if(!req_file)
3876 return ERROR_NOT_ENOUGH_MEMORY;
3877
3878 req_file->ref = 1;
3879
3880 req_file->file_name = heap_strdupW(file_name);
3881 if(!req_file->file_name) {
3882 heap_free(req_file);
3883 return ERROR_NOT_ENOUGH_MEMORY;
3884 }
3885
3886 req_file->file_handle = CreateFileW(req_file->file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
3887 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
3888 if(req_file->file_handle == INVALID_HANDLE_VALUE) {
3889 req_file_release(req_file);
3890 return GetLastError();
3891 }
3892
3893 *ret = req_file;
3894 return ERROR_SUCCESS;
3895 }
3896
3897 void req_file_release(req_file_t *req_file)
3898 {
3899 if(InterlockedDecrement(&req_file->ref))
3900 return;
3901
3902 if(!req_file->is_committed)
3903 DeleteFileW(req_file->file_name);
3904 if(req_file->file_handle && req_file->file_handle != INVALID_HANDLE_VALUE)
3905 CloseHandle(req_file->file_handle);
3906 heap_free(req_file->file_name);
3907 heap_free(req_file);
3908 }
3909
3910 /***********************************************************************
3911 * InternetLockRequestFile (WININET.@)
3912 */
3913 BOOL WINAPI InternetLockRequestFile(HINTERNET hInternet, HANDLE *lphLockReqHandle)
3914 {
3915 req_file_t *req_file = NULL;
3916 object_header_t *hdr;
3917 DWORD res;
3918
3919 TRACE("(%p %p)\n", hInternet, lphLockReqHandle);
3920
3921 hdr = get_handle_object(hInternet);
3922 if (!hdr) {
3923 SetLastError(ERROR_INVALID_HANDLE);
3924 return FALSE;
3925 }
3926
3927 if(hdr->vtbl->LockRequestFile) {
3928 res = hdr->vtbl->LockRequestFile(hdr, &req_file);
3929 }else {
3930 WARN("wrong handle\n");
3931 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
3932 }
3933
3934 WININET_Release(hdr);
3935
3936 *lphLockReqHandle = req_file;
3937 if(res != ERROR_SUCCESS)
3938 SetLastError(res);
3939 return res == ERROR_SUCCESS;
3940 }
3941
3942 BOOL WINAPI InternetUnlockRequestFile(HANDLE hLockHandle)
3943 {
3944 TRACE("(%p)\n", hLockHandle);
3945
3946 req_file_release(hLockHandle);
3947 return TRUE;
3948 }
3949
3950
3951 /***********************************************************************
3952 * InternetAutodial (WININET.@)
3953 *
3954 * On windows this function is supposed to dial the default internet
3955 * connection. We don't want to have Wine dial out to the internet so
3956 * we return TRUE by default. It might be nice to check if we are connected.
3957 *
3958 * RETURNS
3959 * TRUE on success
3960 * FALSE on failure
3961 *
3962 */
3963 BOOL WINAPI InternetAutodial(DWORD dwFlags, HWND hwndParent)
3964 {
3965 FIXME("STUB\n");
3966
3967 /* Tell that we are connected to the internet. */
3968 return TRUE;
3969 }
3970
3971 /***********************************************************************
3972 * InternetAutodialHangup (WININET.@)
3973 *
3974 * Hangs up a connection made with InternetAutodial
3975 *
3976 * PARAM
3977 * dwReserved
3978 * RETURNS
3979 * TRUE on success
3980 * FALSE on failure
3981 *
3982 */
3983 BOOL WINAPI InternetAutodialHangup(DWORD dwReserved)
3984 {
3985 FIXME("STUB\n");
3986
3987 /* we didn't dial, we don't disconnect */
3988 return TRUE;
3989 }
3990
3991 /***********************************************************************
3992 * InternetCombineUrlA (WININET.@)
3993 *
3994 * Combine a base URL with a relative URL
3995 *
3996 * RETURNS
3997 * TRUE on success
3998 * FALSE on failure
3999 *
4000 */
4001
4002 BOOL WINAPI InternetCombineUrlA(LPCSTR lpszBaseUrl, LPCSTR lpszRelativeUrl,
4003 LPSTR lpszBuffer, LPDWORD lpdwBufferLength,
4004 DWORD dwFlags)
4005 {
4006 HRESULT hr=S_OK;
4007
4008 TRACE("(%s, %s, %p, %p, 0x%08x)\n", debugstr_a(lpszBaseUrl), debugstr_a(lpszRelativeUrl), lpszBuffer, lpdwBufferLength, dwFlags);
4009
4010 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
4011 dwFlags ^= ICU_NO_ENCODE;
4012 hr=UrlCombineA(lpszBaseUrl,lpszRelativeUrl,lpszBuffer,lpdwBufferLength,dwFlags);
4013
4014 return (hr==S_OK);
4015 }
4016
4017 /***********************************************************************
4018 * InternetCombineUrlW (WININET.@)
4019 *
4020 * Combine a base URL with a relative URL
4021 *
4022 * RETURNS
4023 * TRUE on success
4024 * FALSE on failure
4025 *
4026 */
4027
4028 BOOL WINAPI InternetCombineUrlW(LPCWSTR lpszBaseUrl, LPCWSTR lpszRelativeUrl,
4029 LPWSTR lpszBuffer, LPDWORD lpdwBufferLength,
4030 DWORD dwFlags)
4031 {
4032 HRESULT hr=S_OK;
4033
4034 TRACE("(%s, %s, %p, %p, 0x%08x)\n", debugstr_w(lpszBaseUrl), debugstr_w(lpszRelativeUrl), lpszBuffer, lpdwBufferLength, dwFlags);
4035
4036 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
4037 dwFlags ^= ICU_NO_ENCODE;
4038 hr=UrlCombineW(lpszBaseUrl,lpszRelativeUrl,lpszBuffer,lpdwBufferLength,dwFlags);
4039
4040 return (hr==S_OK);
4041 }
4042
4043 /* max port num is 65535 => 5 digits */
4044 #define MAX_WORD_DIGITS 5
4045
4046 #define URL_GET_COMP_LENGTH(url, component) ((url)->dw##component##Length ? \
4047 (url)->dw##component##Length : strlenW((url)->lpsz##component))
4048 #define URL_GET_COMP_LENGTHA(url, component) ((url)->dw##component##Length ? \
4049 (url)->dw##component##Length : strlen((url)->lpsz##component))
4050
4051 static BOOL url_uses_default_port(INTERNET_SCHEME nScheme, INTERNET_PORT nPort)
4052 {
4053 if ((nScheme == INTERNET_SCHEME_HTTP) &&
4054 (nPort == INTERNET_DEFAULT_HTTP_PORT))
4055 return TRUE;
4056 if ((nScheme == INTERNET_SCHEME_HTTPS) &&
4057 (nPort == INTERNET_DEFAULT_HTTPS_PORT))
4058 return TRUE;
4059 if ((nScheme == INTERNET_SCHEME_FTP) &&
4060 (nPort == INTERNET_DEFAULT_FTP_PORT))
4061 return TRUE;
4062 if ((nScheme == INTERNET_SCHEME_GOPHER) &&
4063 (nPort == INTERNET_DEFAULT_GOPHER_PORT))
4064 return TRUE;
4065
4066 if (nPort == INTERNET_INVALID_PORT_NUMBER)
4067 return TRUE;
4068
4069 return FALSE;
4070 }
4071
4072 /* opaque urls do not fit into the standard url hierarchy and don't have
4073 * two following slashes */
4074 static inline BOOL scheme_is_opaque(INTERNET_SCHEME nScheme)
4075 {
4076 return (nScheme != INTERNET_SCHEME_FTP) &&
4077 (nScheme != INTERNET_SCHEME_GOPHER) &&
4078 (nScheme != INTERNET_SCHEME_HTTP) &&
4079 (nScheme != INTERNET_SCHEME_HTTPS) &&
4080 (nScheme != INTERNET_SCHEME_FILE);
4081 }
4082
4083 static LPCWSTR INTERNET_GetSchemeString(INTERNET_SCHEME scheme)
4084 {
4085 int index;
4086 if (scheme < INTERNET_SCHEME_FIRST)
4087 return NULL;
4088 index = scheme - INTERNET_SCHEME_FIRST;
4089 if (index >= sizeof(url_schemes)/sizeof(url_schemes[0]))
4090 return NULL;
4091 return (LPCWSTR)url_schemes[index];
4092 }
4093
4094 /* we can calculate using ansi strings because we're just
4095 * calculating string length, not size
4096 */
4097 static BOOL calc_url_length(LPURL_COMPONENTSW lpUrlComponents,
4098 LPDWORD lpdwUrlLength)
4099 {
4100 INTERNET_SCHEME nScheme;
4101
4102 *lpdwUrlLength = 0;
4103
4104 if (lpUrlComponents->lpszScheme)
4105 {
4106 DWORD dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, Scheme);
4107 *lpdwUrlLength += dwLen;
4108 nScheme = GetInternetSchemeW(lpUrlComponents->lpszScheme, dwLen);
4109 }
4110 else
4111 {
4112 LPCWSTR scheme;
4113
4114 nScheme = lpUrlComponents->nScheme;
4115
4116 if (nScheme == INTERNET_SCHEME_DEFAULT)
4117 nScheme = INTERNET_SCHEME_HTTP;
4118 scheme = INTERNET_GetSchemeString(nScheme);
4119 *lpdwUrlLength += strlenW(scheme);
4120 }
4121
4122 (*lpdwUrlLength)++; /* ':' */
4123 if (!scheme_is_opaque(nScheme) || lpUrlComponents->lpszHostName)
4124 *lpdwUrlLength += strlen("//");
4125
4126 if (lpUrlComponents->lpszUserName)
4127 {
4128 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, UserName);
4129 *lpdwUrlLength += strlen("@");
4130 }
4131 else
4132 {
4133 if (lpUrlComponents->lpszPassword)
4134 {
4135 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
4136 return FALSE;
4137 }
4138 }
4139
4140 if (lpUrlComponents->lpszPassword)
4141 {
4142 *lpdwUrlLength += strlen(":");
4143 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, Password);
4144 }
4145
4146 if (lpUrlComponents->lpszHostName)
4147 {
4148 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, HostName);
4149
4150 if (!url_uses_default_port(nScheme, lpUrlComponents->nPort))
4151 {
4152 char szPort[MAX_WORD_DIGITS+1];
4153
4154 sprintf(szPort, "%d", lpUrlComponents->nPort);
4155 *lpdwUrlLength += strlen(szPort);
4156 *lpdwUrlLength += strlen(":");
4157 }
4158
4159 if (lpUrlComponents->lpszUrlPath && *lpUrlComponents->lpszUrlPath != '/')
4160 (*lpdwUrlLength)++; /* '/' */
4161 }
4162
4163 if (lpUrlComponents->lpszUrlPath)
4164 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, UrlPath);
4165
4166 if (lpUrlComponents->lpszExtraInfo)
4167 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, ExtraInfo);
4168
4169 return TRUE;
4170 }
4171
4172 static void convert_urlcomp_atow(LPURL_COMPONENTSA lpUrlComponents, LPURL_COMPONENTSW urlCompW)
4173 {
4174 INT len;
4175
4176 ZeroMemory(urlCompW, sizeof(URL_COMPONENTSW));
4177
4178 urlCompW->dwStructSize = sizeof(URL_COMPONENTSW);
4179 urlCompW->dwSchemeLength = lpUrlComponents->dwSchemeLength;
4180 urlCompW->nScheme = lpUrlComponents->nScheme;
4181 urlCompW->dwHostNameLength = lpUrlComponents->dwHostNameLength;
4182 urlCompW->nPort = lpUrlComponents->nPort;
4183 urlCompW->dwUserNameLength = lpUrlComponents->dwUserNameLength;
4184 urlCompW->dwPasswordLength = lpUrlComponents->dwPasswordLength;
4185 urlCompW->dwUrlPathLength = lpUrlComponents->dwUrlPathLength;
4186 urlCompW->dwExtraInfoLength = lpUrlComponents->dwExtraInfoLength;
4187
4188 if (lpUrlComponents->lpszScheme)
4189 {
4190 len = URL_GET_COMP_LENGTHA(lpUrlComponents, Scheme) + 1;
4191 urlCompW->lpszScheme = heap_alloc(len * sizeof(WCHAR));
4192 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszScheme,
4193 -1, urlCompW->lpszScheme, len);
4194 }
4195
4196 if (lpUrlComponents->lpszHostName)
4197 {
4198 len = URL_GET_COMP_LENGTHA(lpUrlComponents, HostName) + 1;
4199 urlCompW->lpszHostName = heap_alloc(len * sizeof(WCHAR));
4200 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszHostName,
4201 -1, urlCompW->lpszHostName, len);
4202 }
4203
4204 if (lpUrlComponents->lpszUserName)
4205 {
4206 len = URL_GET_COMP_LENGTHA(lpUrlComponents, UserName) + 1;
4207 urlCompW->lpszUserName = heap_alloc(len * sizeof(WCHAR));
4208 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszUserName,
4209 -1, urlCompW->lpszUserName, len);
4210 }
4211
4212 if (lpUrlComponents->lpszPassword)
4213 {
4214 len = URL_GET_COMP_LENGTHA(lpUrlComponents, Password) + 1;
4215 urlCompW->lpszPassword = heap_alloc(len * sizeof(WCHAR));
4216 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszPassword,
4217 -1, urlCompW->lpszPassword, len);
4218 }
4219
4220 if (lpUrlComponents->lpszUrlPath)
4221 {
4222 len = URL_GET_COMP_LENGTHA(lpUrlComponents, UrlPath) + 1;
4223 urlCompW->lpszUrlPath = heap_alloc(len * sizeof(WCHAR));
4224 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszUrlPath,
4225 -1, urlCompW->lpszUrlPath, len);
4226 }
4227
4228 if (lpUrlComponents->lpszExtraInfo)
4229 {
4230 len = URL_GET_COMP_LENGTHA(lpUrlComponents, ExtraInfo) + 1;
4231 urlCompW->lpszExtraInfo = heap_alloc(len * sizeof(WCHAR));
4232 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszExtraInfo,
4233 -1, urlCompW->lpszExtraInfo, len);
4234 }
4235 }
4236
4237 /***********************************************************************
4238 * InternetCreateUrlA (WININET.@)
4239 *
4240 * See InternetCreateUrlW.
4241 */
4242 BOOL WINAPI InternetCreateUrlA(LPURL_COMPONENTSA lpUrlComponents, DWORD dwFlags,
4243 LPSTR lpszUrl, LPDWORD lpdwUrlLength)
4244 {
4245 BOOL ret;
4246 LPWSTR urlW = NULL;
4247 URL_COMPONENTSW urlCompW;
4248
4249 TRACE("(%p,%d,%p,%p)\n", lpUrlComponents, dwFlags, lpszUrl, lpdwUrlLength);
4250
4251 if (!lpUrlComponents || lpUrlComponents->dwStructSize != sizeof(URL_COMPONENTSW) || !lpdwUrlLength)
4252 {
4253 SetLastError(ERROR_INVALID_PARAMETER);
4254 return FALSE;
4255 }
4256
4257 convert_urlcomp_atow(lpUrlComponents, &urlCompW);
4258
4259 if (lpszUrl)
4260 urlW = heap_alloc(*lpdwUrlLength * sizeof(WCHAR));
4261
4262 ret = InternetCreateUrlW(&urlCompW, dwFlags, urlW, lpdwUrlLength);
4263
4264 if (!ret && (GetLastError() == ERROR_INSUFFICIENT_BUFFER))
4265 *lpdwUrlLength /= sizeof(WCHAR);
4266
4267 /* on success, lpdwUrlLength points to the size of urlW in WCHARS
4268 * minus one, so add one to leave room for NULL terminator
4269 */
4270 if (ret)
4271 WideCharToMultiByte(CP_ACP, 0, urlW, -1, lpszUrl, *lpdwUrlLength + 1, NULL, NULL);
4272
4273 heap_free(urlCompW.lpszScheme);
4274 heap_free(urlCompW.lpszHostName);
4275 heap_free(urlCompW.lpszUserName);
4276 heap_free(urlCompW.lpszPassword);
4277 heap_free(urlCompW.lpszUrlPath);
4278 heap_free(urlCompW.lpszExtraInfo);
4279 heap_free(urlW);
4280 return ret;
4281 }
4282
4283 /***********************************************************************
4284 * InternetCreateUrlW (WININET.@)
4285 *
4286 * Creates a URL from its component parts.
4287 *
4288 * PARAMS
4289 * lpUrlComponents [I] URL Components.
4290 * dwFlags [I] Flags. See notes.
4291 * lpszUrl [I] Buffer in which to store the created URL.
4292 * lpdwUrlLength [I/O] On input, the length of the buffer pointed to by
4293 * lpszUrl in characters. On output, the number of bytes
4294 * required to store the URL including terminator.
4295 *
4296 * NOTES
4297 *
4298 * The dwFlags parameter can be zero or more of the following:
4299 *|ICU_ESCAPE - Generates escape sequences for unsafe characters in the path and extra info of the URL.
4300 *
4301 * RETURNS
4302 * TRUE on success
4303 * FALSE on failure
4304 *
4305 */
4306 BOOL WINAPI InternetCreateUrlW(LPURL_COMPONENTSW lpUrlComponents, DWORD dwFlags,
4307 LPWSTR lpszUrl, LPDWORD lpdwUrlLength)
4308 {
4309 DWORD dwLen;
4310 INTERNET_SCHEME nScheme;
4311
4312 static const WCHAR slashSlashW[] = {'/','/'};
4313 static const WCHAR fmtW[] = {'%','u',0};
4314
4315 TRACE("(%p,%d,%p,%p)\n", lpUrlComponents, dwFlags, lpszUrl, lpdwUrlLength);
4316
4317 if (!lpUrlComponents || lpUrlComponents->dwStructSize != sizeof(URL_COMPONENTSW) || !lpdwUrlLength)
4318 {
4319 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
4320 return FALSE;
4321 }
4322
4323 if (!calc_url_length(lpUrlComponents, &dwLen))
4324 return FALSE;
4325
4326 if (!lpszUrl || *lpdwUrlLength < dwLen)
4327 {
4328 *lpdwUrlLength = (dwLen + 1) * sizeof(WCHAR);
4329 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
4330 return FALSE;
4331 }
4332
4333 *lpdwUrlLength = dwLen;
4334 lpszUrl[0] = 0x00;
4335
4336 dwLen = 0;
4337
4338 if (lpUrlComponents->lpszScheme)
4339 {
4340 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, Scheme);
4341 memcpy(lpszUrl, lpUrlComponents->lpszScheme, dwLen * sizeof(WCHAR));
4342 lpszUrl += dwLen;
4343
4344 nScheme = GetInternetSchemeW(lpUrlComponents->lpszScheme, dwLen);
4345 }
4346 else
4347 {
4348 LPCWSTR scheme;
4349 nScheme = lpUrlComponents->nScheme;
4350
4351 if (nScheme == INTERNET_SCHEME_DEFAULT)
4352 nScheme = INTERNET_SCHEME_HTTP;
4353
4354 scheme = INTERNET_GetSchemeString(nScheme);
4355 dwLen = strlenW(scheme);
4356 memcpy(lpszUrl, scheme, dwLen * sizeof(WCHAR));
4357 lpszUrl += dwLen;
4358 }
4359
4360 /* all schemes are followed by at least a colon */
4361 *lpszUrl = ':';
4362 lpszUrl++;
4363
4364 if (!scheme_is_opaque(nScheme) || lpUrlComponents->lpszHostName)
4365 {
4366 memcpy(lpszUrl, slashSlashW, sizeof(slashSlashW));
4367 lpszUrl += sizeof(slashSlashW)/sizeof(slashSlashW[0]);
4368 }
4369
4370 if (lpUrlComponents->lpszUserName)
4371 {
4372 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, UserName);
4373 memcpy(lpszUrl, lpUrlComponents->lpszUserName, dwLen * sizeof(WCHAR));
4374 lpszUrl += dwLen;
4375
4376 if (lpUrlComponents->lpszPassword)
4377 {
4378 *lpszUrl = ':';
4379 lpszUrl++;
4380
4381 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, Password);
4382 memcpy(lpszUrl, lpUrlComponents->lpszPassword, dwLen * sizeof(WCHAR));
4383 lpszUrl += dwLen;
4384 }
4385
4386 *lpszUrl = '@';
4387 lpszUrl++;
4388 }
4389
4390 if (lpUrlComponents->lpszHostName)
4391 {
4392 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, HostName);
4393 memcpy(lpszUrl, lpUrlComponents->lpszHostName, dwLen * sizeof(WCHAR));
4394 lpszUrl += dwLen;
4395
4396 if (!url_uses_default_port(nScheme, lpUrlComponents->nPort))
4397 {
4398 WCHAR szPort[MAX_WORD_DIGITS+1];
4399
4400 sprintfW(szPort, fmtW, lpUrlComponents->nPort);
4401 *lpszUrl = ':';
4402 lpszUrl++;
4403 dwLen = strlenW(szPort);
4404 memcpy(lpszUrl, szPort, dwLen * sizeof(WCHAR));
4405 lpszUrl += dwLen;
4406 }
4407
4408 /* add slash between hostname and path if necessary */
4409 if (lpUrlComponents->lpszUrlPath && *lpUrlComponents->lpszUrlPath != '/')
4410 {
4411 *lpszUrl = '/';
4412 lpszUrl++;
4413 }
4414 }
4415
4416 if (lpUrlComponents->lpszUrlPath)
4417 {
4418 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, UrlPath);
4419 memcpy(lpszUrl, lpUrlComponents->lpszUrlPath, dwLen * sizeof(WCHAR));
4420 lpszUrl += dwLen;
4421 }
4422
4423 if (lpUrlComponents->lpszExtraInfo)
4424 {
4425 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, ExtraInfo);
4426 memcpy(lpszUrl, lpUrlComponents->lpszExtraInfo, dwLen * sizeof(WCHAR));
4427 lpszUrl += dwLen;
4428 }
4429
4430 *lpszUrl = '\0';
4431
4432 return TRUE;
4433 }
4434
4435 /***********************************************************************
4436 * InternetConfirmZoneCrossingA (WININET.@)
4437 *
4438 */
4439 DWORD WINAPI InternetConfirmZoneCrossingA( HWND hWnd, LPSTR szUrlPrev, LPSTR szUrlNew, BOOL bPost )
4440 {
4441 FIXME("(%p, %s, %s, %x) stub\n", hWnd, debugstr_a(szUrlPrev), debugstr_a(szUrlNew), bPost);
4442 return ERROR_SUCCESS;
4443 }
4444
4445 /***********************************************************************
4446 * InternetConfirmZoneCrossingW (WININET.@)
4447 *
4448 */
4449 DWORD WINAPI InternetConfirmZoneCrossingW( HWND hWnd, LPWSTR szUrlPrev, LPWSTR szUrlNew, BOOL bPost )
4450 {
4451 FIXME("(%p, %s, %s, %x) stub\n", hWnd, debugstr_w(szUrlPrev), debugstr_w(szUrlNew), bPost);
4452 return ERROR_SUCCESS;
4453 }
4454
4455 static DWORD zone_preference = 3;
4456
4457 /***********************************************************************
4458 * PrivacySetZonePreferenceW (WININET.@)
4459 */
4460 DWORD WINAPI PrivacySetZonePreferenceW( DWORD zone, DWORD type, DWORD template, LPCWSTR preference )
4461 {
4462 FIXME( "%x %x %x %s: stub\n", zone, type, template, debugstr_w(preference) );
4463
4464 zone_preference = template;
4465 return 0;
4466 }
4467
4468 /***********************************************************************
4469 * PrivacyGetZonePreferenceW (WININET.@)
4470 */
4471 DWORD WINAPI PrivacyGetZonePreferenceW( DWORD zone, DWORD type, LPDWORD template,
4472 LPWSTR preference, LPDWORD length )
4473 {
4474 FIXME( "%x %x %p %p %p: stub\n", zone, type, template, preference, length );
4475
4476 if (template) *template = zone_preference;
4477 return 0;
4478 }
4479
4480 /***********************************************************************
4481 * InternetGetSecurityInfoByURLA (WININET.@)
4482 */
4483 BOOL WINAPI InternetGetSecurityInfoByURLA(LPSTR lpszURL, PCCERT_CHAIN_CONTEXT *ppCertChain, DWORD *pdwSecureFlags)
4484 {
4485 WCHAR *url;
4486 BOOL res;
4487
4488 TRACE("(%s %p %p)\n", debugstr_a(lpszURL), ppCertChain, pdwSecureFlags);
4489
4490 url = heap_strdupAtoW(lpszURL);
4491 if(!url)
4492 return FALSE;
4493
4494 res = InternetGetSecurityInfoByURLW(url, ppCertChain, pdwSecureFlags);
4495 heap_free(url);
4496 return res;
4497 }
4498
4499 /***********************************************************************
4500 * InternetGetSecurityInfoByURLW (WININET.@)
4501 */
4502 BOOL WINAPI InternetGetSecurityInfoByURLW(LPCWSTR lpszURL, PCCERT_CHAIN_CONTEXT *ppCertChain, DWORD *pdwSecureFlags)
4503 {
4504 WCHAR hostname[INTERNET_MAX_HOST_NAME_LENGTH];
4505 URL_COMPONENTSW url = {sizeof(url)};
4506 server_t *server;
4507 BOOL res = FALSE;
4508
4509 TRACE("(%s %p %p)\n", debugstr_w(lpszURL), ppCertChain, pdwSecureFlags);
4510
4511 url.lpszHostName = hostname;
4512 url.dwHostNameLength = sizeof(hostname)/sizeof(WCHAR);
4513
4514 res = InternetCrackUrlW(lpszURL, 0, 0, &url);
4515 if(!res || url.nScheme != INTERNET_SCHEME_HTTPS) {
4516 SetLastError(ERROR_INTERNET_ITEM_NOT_FOUND);
4517 return FALSE;
4518 }
4519
4520 server = get_server(hostname, url.nPort, TRUE, FALSE);
4521 if(!server) {
4522 SetLastError(ERROR_INTERNET_ITEM_NOT_FOUND);
4523 return FALSE;
4524 }
4525
4526 if(server->cert_chain) {
4527 const CERT_CHAIN_CONTEXT *chain_dup;
4528
4529 chain_dup = CertDuplicateCertificateChain(server->cert_chain);
4530 if(chain_dup) {
4531 *ppCertChain = chain_dup;
4532 *pdwSecureFlags = server->security_flags & _SECURITY_ERROR_FLAGS_MASK;
4533 }else {
4534 res = FALSE;
4535 }
4536 }else {
4537 SetLastError(ERROR_INTERNET_ITEM_NOT_FOUND);
4538 res = FALSE;
4539 }
4540
4541 server_release(server);
4542 return res;
4543 }
4544
4545 DWORD WINAPI InternetDialA( HWND hwndParent, LPSTR lpszConnectoid, DWORD dwFlags,
4546 DWORD_PTR* lpdwConnection, DWORD dwReserved )
4547 {
4548 FIXME("(%p, %p, 0x%08x, %p, 0x%08x) stub\n", hwndParent, lpszConnectoid, dwFlags,
4549 lpdwConnection, dwReserved);
4550 return ERROR_SUCCESS;
4551 }
4552
4553 DWORD WINAPI InternetDialW( HWND hwndParent, LPWSTR lpszConnectoid, DWORD dwFlags,
4554 DWORD_PTR* lpdwConnection, DWORD dwReserved )
4555 {
4556 FIXME("(%p, %p, 0x%08x, %p, 0x%08x) stub\n", hwndParent, lpszConnectoid, dwFlags,
4557 lpdwConnection, dwReserved);
4558 return ERROR_SUCCESS;
4559 }
4560
4561 BOOL WINAPI InternetGoOnlineA( LPSTR lpszURL, HWND hwndParent, DWORD dwReserved )
4562 {
4563 FIXME("(%s, %p, 0x%08x) stub\n", debugstr_a(lpszURL), hwndParent, dwReserved);
4564 return TRUE;
4565 }
4566
4567 BOOL WINAPI InternetGoOnlineW( LPWSTR lpszURL, HWND hwndParent, DWORD dwReserved )
4568 {
4569 FIXME("(%s, %p, 0x%08x) stub\n", debugstr_w(lpszURL), hwndParent, dwReserved);
4570 return TRUE;
4571 }
4572
4573 DWORD WINAPI InternetHangUp( DWORD_PTR dwConnection, DWORD dwReserved )
4574 {
4575 FIXME("(0x%08lx, 0x%08x) stub\n", dwConnection, dwReserved);
4576 return ERROR_SUCCESS;
4577 }
4578
4579 BOOL WINAPI CreateMD5SSOHash( PWSTR pszChallengeInfo, PWSTR pwszRealm, PWSTR pwszTarget,
4580 PBYTE pbHexHash )
4581 {
4582 FIXME("(%s, %s, %s, %p) stub\n", debugstr_w(pszChallengeInfo), debugstr_w(pwszRealm),
4583 debugstr_w(pwszTarget), pbHexHash);
4584 return FALSE;
4585 }
4586
4587 BOOL WINAPI ResumeSuspendedDownload( HINTERNET hInternet, DWORD dwError )
4588 {
4589 FIXME("(%p, 0x%08x) stub\n", hInternet, dwError);
4590 return FALSE;
4591 }
4592
4593 BOOL WINAPI InternetQueryFortezzaStatus(DWORD *a, DWORD_PTR b)
4594 {
4595 FIXME("(%p, %08lx) stub\n", a, b);
4596 return FALSE;
4597 }
4598
4599 DWORD WINAPI ShowClientAuthCerts(HWND parent)
4600 {
4601 FIXME("%p: stub\n", parent);
4602 return 0;
4603 }