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