ieframe: Moved string and menu resources to ieframe.
[wine/testsucceed.git] / dlls / wininet / internet.c
blobc949f5df6adf81b6ed9c19fdc4eea60d4acfa053
1 /*
2 * Wininet
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
10 * Ulrich Czekalla
11 * Aric Stewart
12 * David Hammerton
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.
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.
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
29 #include "config.h"
30 #include "wine/port.h"
32 #define MAXHOSTNAME 100 /* from http.c */
34 #if defined(__MINGW32__) || defined (_MSC_VER)
35 #include <ws2tcpip.h>
36 #endif
38 #include <string.h>
39 #include <stdarg.h>
40 #include <stdio.h>
41 #include <sys/types.h>
42 #ifdef HAVE_SYS_SOCKET_H
43 # include <sys/socket.h>
44 #endif
45 #ifdef HAVE_POLL_H
46 #include <poll.h>
47 #endif
48 #ifdef HAVE_SYS_POLL_H
49 # include <sys/poll.h>
50 #endif
51 #ifdef HAVE_SYS_TIME_H
52 # include <sys/time.h>
53 #endif
54 #include <stdlib.h>
55 #include <ctype.h>
56 #ifdef HAVE_UNISTD_H
57 # include <unistd.h>
58 #endif
59 #include <assert.h>
61 #include "windef.h"
62 #include "winbase.h"
63 #include "winreg.h"
64 #include "winuser.h"
65 #include "wininet.h"
66 #include "winineti.h"
67 #include "winnls.h"
68 #include "wine/debug.h"
69 #include "winerror.h"
70 #define NO_SHLWAPI_STREAM
71 #include "shlwapi.h"
73 #include "wine/exception.h"
75 #include "internet.h"
76 #include "resource.h"
78 #include "wine/unicode.h"
80 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
82 #define RESPONSE_TIMEOUT 30
84 typedef struct
86 DWORD dwError;
87 CHAR response[MAX_REPLY_LEN];
88 } WITHREADERROR, *LPWITHREADERROR;
90 static DWORD g_dwTlsErrIndex = TLS_OUT_OF_INDEXES;
91 HMODULE WININET_hModule;
93 static CRITICAL_SECTION WININET_cs;
94 static CRITICAL_SECTION_DEBUG WININET_cs_debug =
96 0, 0, &WININET_cs,
97 { &WININET_cs_debug.ProcessLocksList, &WININET_cs_debug.ProcessLocksList },
98 0, 0, { (DWORD_PTR)(__FILE__ ": WININET_cs") }
100 static CRITICAL_SECTION WININET_cs = { &WININET_cs_debug, -1, 0, 0, 0, 0 };
102 static object_header_t **handle_table;
103 static UINT_PTR next_handle;
104 static UINT_PTR handle_table_size;
106 typedef struct
108 DWORD proxyEnabled;
109 LPWSTR proxy;
110 LPWSTR proxyBypass;
111 } proxyinfo_t;
113 static const WCHAR szInternetSettings[] =
114 { 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
115 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
116 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s',0 };
117 static const WCHAR szProxyServer[] = { 'P','r','o','x','y','S','e','r','v','e','r', 0 };
118 static const WCHAR szProxyEnable[] = { 'P','r','o','x','y','E','n','a','b','l','e', 0 };
120 void *alloc_object(object_header_t *parent, const object_vtbl_t *vtbl, size_t size)
122 UINT_PTR handle = 0, num;
123 object_header_t *ret;
124 object_header_t **p;
125 BOOL res = TRUE;
127 ret = heap_alloc_zero(size);
128 if(!ret)
129 return NULL;
131 list_init(&ret->children);
133 EnterCriticalSection( &WININET_cs );
135 if(!handle_table_size) {
136 num = 16;
137 p = heap_alloc_zero(sizeof(handle_table[0]) * num);
138 if(p) {
139 handle_table = p;
140 handle_table_size = num;
141 next_handle = 1;
142 }else {
143 res = FALSE;
145 }else if(next_handle == handle_table_size) {
146 num = handle_table_size * 2;
147 p = heap_realloc_zero(handle_table, sizeof(handle_table[0]) * num);
148 if(p) {
149 handle_table = p;
150 handle_table_size = num;
151 }else {
152 res = FALSE;
156 if(res) {
157 handle = next_handle;
158 if(handle_table[handle])
159 ERR("handle isn't free but should be\n");
160 handle_table[handle] = ret;
161 ret->valid_handle = TRUE;
163 while(handle_table[next_handle] && next_handle < handle_table_size)
164 next_handle++;
167 LeaveCriticalSection( &WININET_cs );
169 if(!res) {
170 heap_free(ret);
171 return NULL;
174 ret->vtbl = vtbl;
175 ret->refs = 1;
176 ret->hInternet = (HINTERNET)handle;
178 if(parent) {
179 ret->lpfnStatusCB = parent->lpfnStatusCB;
180 ret->dwInternalFlags = parent->dwInternalFlags & INET_CALLBACKW;
183 return ret;
186 object_header_t *WININET_AddRef( object_header_t *info )
188 ULONG refs = InterlockedIncrement(&info->refs);
189 TRACE("%p -> refcount = %d\n", info, refs );
190 return info;
193 object_header_t *get_handle_object( HINTERNET hinternet )
195 object_header_t *info = NULL;
196 UINT_PTR handle = (UINT_PTR) hinternet;
198 EnterCriticalSection( &WININET_cs );
200 if(handle > 0 && handle < handle_table_size && handle_table[handle] && handle_table[handle]->valid_handle)
201 info = WININET_AddRef(handle_table[handle]);
203 LeaveCriticalSection( &WININET_cs );
205 TRACE("handle %ld -> %p\n", handle, info);
207 return info;
210 static void invalidate_handle(object_header_t *info)
212 object_header_t *child, *next;
214 if(!info->valid_handle)
215 return;
216 info->valid_handle = FALSE;
218 /* Free all children as native does */
219 LIST_FOR_EACH_ENTRY_SAFE( child, next, &info->children, object_header_t, entry )
221 TRACE("invalidating child handle %p for parent %p\n", child->hInternet, info);
222 invalidate_handle( child );
225 WININET_Release(info);
228 BOOL WININET_Release( object_header_t *info )
230 ULONG refs = InterlockedDecrement(&info->refs);
231 TRACE( "object %p refcount = %d\n", info, refs );
232 if( !refs )
234 invalidate_handle(info);
235 if ( info->vtbl->CloseConnection )
237 TRACE( "closing connection %p\n", info);
238 info->vtbl->CloseConnection( info );
240 /* Don't send a callback if this is a session handle created with InternetOpenUrl */
241 if ((info->htype != WH_HHTTPSESSION && info->htype != WH_HFTPSESSION)
242 || !(info->dwInternalFlags & INET_OPENURL))
244 INTERNET_SendCallback(info, info->dwContext,
245 INTERNET_STATUS_HANDLE_CLOSING, &info->hInternet,
246 sizeof(HINTERNET));
248 TRACE( "destroying object %p\n", info);
249 if ( info->htype != WH_HINIT )
250 list_remove( &info->entry );
251 info->vtbl->Destroy( info );
253 if(info->hInternet) {
254 UINT_PTR handle = (UINT_PTR)info->hInternet;
256 EnterCriticalSection( &WININET_cs );
258 handle_table[handle] = NULL;
259 if(next_handle > handle)
260 next_handle = handle;
262 LeaveCriticalSection( &WININET_cs );
265 heap_free(info);
267 return TRUE;
270 /***********************************************************************
271 * DllMain [Internal] Initializes the internal 'WININET.DLL'.
273 * PARAMS
274 * hinstDLL [I] handle to the DLL's instance
275 * fdwReason [I]
276 * lpvReserved [I] reserved, must be NULL
278 * RETURNS
279 * Success: TRUE
280 * Failure: FALSE
283 BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
285 TRACE("%p,%x,%p\n", hinstDLL, fdwReason, lpvReserved);
287 switch (fdwReason) {
288 case DLL_PROCESS_ATTACH:
290 g_dwTlsErrIndex = TlsAlloc();
292 if (g_dwTlsErrIndex == TLS_OUT_OF_INDEXES)
293 return FALSE;
295 URLCacheContainers_CreateDefaults();
297 WININET_hModule = hinstDLL;
299 case DLL_THREAD_ATTACH:
300 break;
302 case DLL_THREAD_DETACH:
303 if (g_dwTlsErrIndex != TLS_OUT_OF_INDEXES)
305 heap_free(TlsGetValue(g_dwTlsErrIndex));
307 break;
309 case DLL_PROCESS_DETACH:
310 collect_connections(TRUE);
311 NETCON_unload();
312 URLCacheContainers_DeleteAll();
314 if (g_dwTlsErrIndex != TLS_OUT_OF_INDEXES)
316 heap_free(TlsGetValue(g_dwTlsErrIndex));
317 TlsFree(g_dwTlsErrIndex);
319 break;
321 return TRUE;
324 /***********************************************************************
325 * INTERNET_SaveProxySettings
327 * Stores the proxy settings given by lpwai into the registry
329 * RETURNS
330 * ERROR_SUCCESS if no error, or error code on fail
332 static LONG INTERNET_SaveProxySettings( proxyinfo_t *lpwpi )
334 HKEY key;
335 LONG ret;
337 if ((ret = RegOpenKeyW( HKEY_CURRENT_USER, szInternetSettings, &key )))
338 return ret;
340 if ((ret = RegSetValueExW( key, szProxyEnable, 0, REG_DWORD, (BYTE*)&lpwpi->proxyEnabled, sizeof(DWORD))))
342 RegCloseKey( key );
343 return ret;
346 if (lpwpi->proxy)
348 if ((ret = RegSetValueExW( key, szProxyServer, 0, REG_SZ, (BYTE*)lpwpi->proxy, sizeof(WCHAR) * (lstrlenW(lpwpi->proxy) + 1))))
350 RegCloseKey( key );
351 return ret;
354 else
356 if ((ret = RegDeleteValueW( key, szProxyServer )))
358 RegCloseKey( key );
359 return ret;
363 RegCloseKey(key);
364 return ERROR_SUCCESS;
367 /***********************************************************************
368 * INTERNET_FindProxyForProtocol
370 * Searches the proxy string for a proxy of the given protocol.
371 * Returns the found proxy, or the default proxy if none of the given
372 * protocol is found.
374 * PARAMETERS
375 * szProxy [In] proxy string to search
376 * proto [In] protocol to search for, e.g. "http"
377 * foundProxy [Out] found proxy
378 * foundProxyLen [In/Out] length of foundProxy buffer, in WCHARs
380 * RETURNS
381 * TRUE if a proxy is found, FALSE if not. If foundProxy is too short,
382 * *foundProxyLen is set to the required size in WCHARs, including the
383 * NULL terminator, and the last error is set to ERROR_INSUFFICIENT_BUFFER.
385 BOOL INTERNET_FindProxyForProtocol(LPCWSTR szProxy, LPCWSTR proto, WCHAR *foundProxy, DWORD *foundProxyLen)
387 LPCWSTR ptr;
388 BOOL ret = FALSE;
390 TRACE("(%s, %s)\n", debugstr_w(szProxy), debugstr_w(proto));
392 /* First, look for the specified protocol (proto=scheme://host:port) */
393 for (ptr = szProxy; !ret && ptr && *ptr; )
395 LPCWSTR end, equal;
397 if (!(end = strchrW(ptr, ' ')))
398 end = ptr + strlenW(ptr);
399 if ((equal = strchrW(ptr, '=')) && equal < end &&
400 equal - ptr == strlenW(proto) &&
401 !strncmpiW(proto, ptr, strlenW(proto)))
403 if (end - equal > *foundProxyLen)
405 WARN("buffer too short for %s\n",
406 debugstr_wn(equal + 1, end - equal - 1));
407 *foundProxyLen = end - equal;
408 SetLastError(ERROR_INSUFFICIENT_BUFFER);
410 else
412 memcpy(foundProxy, equal + 1, (end - equal) * sizeof(WCHAR));
413 foundProxy[end - equal] = 0;
414 ret = TRUE;
417 if (*end == ' ')
418 ptr = end + 1;
419 else
420 ptr = end;
422 if (!ret)
424 /* It wasn't found: look for no protocol */
425 for (ptr = szProxy; !ret && ptr && *ptr; )
427 LPCWSTR end, equal;
429 if (!(end = strchrW(ptr, ' ')))
430 end = ptr + strlenW(ptr);
431 if (!(equal = strchrW(ptr, '=')))
433 if (end - ptr + 1 > *foundProxyLen)
435 WARN("buffer too short for %s\n",
436 debugstr_wn(ptr, end - ptr));
437 *foundProxyLen = end - ptr + 1;
438 SetLastError(ERROR_INSUFFICIENT_BUFFER);
440 else
442 memcpy(foundProxy, ptr, (end - ptr) * sizeof(WCHAR));
443 foundProxy[end - ptr] = 0;
444 ret = TRUE;
447 if (*end == ' ')
448 ptr = end + 1;
449 else
450 ptr = end;
453 if (ret)
454 TRACE("found proxy for %s: %s\n", debugstr_w(proto),
455 debugstr_w(foundProxy));
456 return ret;
459 /***********************************************************************
460 * InternetInitializeAutoProxyDll (WININET.@)
462 * Setup the internal proxy
464 * PARAMETERS
465 * dwReserved
467 * RETURNS
468 * FALSE on failure
471 BOOL WINAPI InternetInitializeAutoProxyDll(DWORD dwReserved)
473 FIXME("STUB\n");
474 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
475 return FALSE;
478 /***********************************************************************
479 * DetectAutoProxyUrl (WININET.@)
481 * Auto detect the proxy url
483 * RETURNS
484 * FALSE on failure
487 BOOL WINAPI DetectAutoProxyUrl(LPSTR lpszAutoProxyUrl,
488 DWORD dwAutoProxyUrlLength, DWORD dwDetectFlags)
490 FIXME("STUB\n");
491 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
492 return FALSE;
495 static void FreeProxyInfo( proxyinfo_t *lpwpi )
497 heap_free(lpwpi->proxy);
498 heap_free(lpwpi->proxyBypass);
501 static proxyinfo_t *global_proxy;
503 static void free_global_proxy( void )
505 EnterCriticalSection( &WININET_cs );
506 if (global_proxy)
508 FreeProxyInfo( global_proxy );
509 heap_free( global_proxy );
511 LeaveCriticalSection( &WININET_cs );
514 /***********************************************************************
515 * INTERNET_LoadProxySettings
517 * Loads proxy information from process-wide global settings, the registry,
518 * or the environment into lpwpi.
520 * The caller should call FreeProxyInfo when done with lpwpi.
522 * FIXME:
523 * The proxy may be specified in the form 'http=proxy.my.org'
524 * Presumably that means there can be ftp=ftpproxy.my.org too.
526 static LONG INTERNET_LoadProxySettings( proxyinfo_t *lpwpi )
528 HKEY key;
529 DWORD type, len;
530 LPCSTR envproxy;
531 LONG ret;
533 EnterCriticalSection( &WININET_cs );
534 if (global_proxy)
536 lpwpi->proxyEnabled = global_proxy->proxyEnabled;
537 lpwpi->proxy = heap_strdupW( global_proxy->proxy );
538 lpwpi->proxyBypass = heap_strdupW( global_proxy->proxyBypass );
540 LeaveCriticalSection( &WININET_cs );
542 if ((ret = RegOpenKeyW( HKEY_CURRENT_USER, szInternetSettings, &key )))
543 return ret;
545 len = sizeof(DWORD);
546 if (RegQueryValueExW( key, szProxyEnable, NULL, &type, (BYTE *)&lpwpi->proxyEnabled, &len ) || type != REG_DWORD)
548 lpwpi->proxyEnabled = 0;
549 if((ret = RegSetValueExW( key, szProxyEnable, 0, REG_DWORD, (BYTE *)&lpwpi->proxyEnabled, sizeof(DWORD) )))
551 RegCloseKey( key );
552 return ret;
556 if (!(envproxy = getenv( "http_proxy" )) || lpwpi->proxyEnabled)
558 TRACE("Proxy is enabled.\n");
560 /* figure out how much memory the proxy setting takes */
561 if (!RegQueryValueExW( key, szProxyServer, NULL, &type, NULL, &len ) && len && (type == REG_SZ))
563 LPWSTR szProxy, p;
564 static const WCHAR szHttp[] = {'h','t','t','p','=',0};
566 if (!(szProxy = heap_alloc(len)))
568 RegCloseKey( key );
569 return ERROR_OUTOFMEMORY;
571 RegQueryValueExW( key, szProxyServer, NULL, &type, (BYTE*)szProxy, &len );
573 /* find the http proxy, and strip away everything else */
574 p = strstrW( szProxy, szHttp );
575 if (p)
577 p += lstrlenW( szHttp );
578 lstrcpyW( szProxy, p );
580 p = strchrW( szProxy, ' ' );
581 if (p) *p = 0;
583 lpwpi->proxy = szProxy;
585 TRACE("http proxy = %s\n", debugstr_w(lpwpi->proxy));
587 else
589 TRACE("No proxy server settings in registry.\n");
590 lpwpi->proxy = NULL;
593 else if (envproxy)
595 WCHAR *envproxyW;
597 len = MultiByteToWideChar( CP_UNIXCP, 0, envproxy, -1, NULL, 0 );
598 if (!(envproxyW = heap_alloc(len * sizeof(WCHAR))))
599 return ERROR_OUTOFMEMORY;
600 MultiByteToWideChar( CP_UNIXCP, 0, envproxy, -1, envproxyW, len );
602 lpwpi->proxyEnabled = 1;
603 lpwpi->proxy = envproxyW;
605 TRACE("http proxy (from environment) = %s\n", debugstr_w(lpwpi->proxy));
607 RegCloseKey( key );
609 lpwpi->proxyBypass = NULL;
611 return ERROR_SUCCESS;
614 /***********************************************************************
615 * INTERNET_ConfigureProxy
617 static BOOL INTERNET_ConfigureProxy( appinfo_t *lpwai )
619 proxyinfo_t wpi;
621 if (INTERNET_LoadProxySettings( &wpi ))
622 return FALSE;
624 if (wpi.proxyEnabled)
626 lpwai->accessType = INTERNET_OPEN_TYPE_PROXY;
627 lpwai->proxy = wpi.proxy;
628 return TRUE;
631 lpwai->accessType = INTERNET_OPEN_TYPE_DIRECT;
632 return FALSE;
635 /***********************************************************************
636 * dump_INTERNET_FLAGS
638 * Helper function to TRACE the internet flags.
640 * RETURNS
641 * None
644 static void dump_INTERNET_FLAGS(DWORD dwFlags)
646 #define FE(x) { x, #x }
647 static const wininet_flag_info flag[] = {
648 FE(INTERNET_FLAG_RELOAD),
649 FE(INTERNET_FLAG_RAW_DATA),
650 FE(INTERNET_FLAG_EXISTING_CONNECT),
651 FE(INTERNET_FLAG_ASYNC),
652 FE(INTERNET_FLAG_PASSIVE),
653 FE(INTERNET_FLAG_NO_CACHE_WRITE),
654 FE(INTERNET_FLAG_MAKE_PERSISTENT),
655 FE(INTERNET_FLAG_FROM_CACHE),
656 FE(INTERNET_FLAG_SECURE),
657 FE(INTERNET_FLAG_KEEP_CONNECTION),
658 FE(INTERNET_FLAG_NO_AUTO_REDIRECT),
659 FE(INTERNET_FLAG_READ_PREFETCH),
660 FE(INTERNET_FLAG_NO_COOKIES),
661 FE(INTERNET_FLAG_NO_AUTH),
662 FE(INTERNET_FLAG_CACHE_IF_NET_FAIL),
663 FE(INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP),
664 FE(INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTPS),
665 FE(INTERNET_FLAG_IGNORE_CERT_DATE_INVALID),
666 FE(INTERNET_FLAG_IGNORE_CERT_CN_INVALID),
667 FE(INTERNET_FLAG_RESYNCHRONIZE),
668 FE(INTERNET_FLAG_HYPERLINK),
669 FE(INTERNET_FLAG_NO_UI),
670 FE(INTERNET_FLAG_PRAGMA_NOCACHE),
671 FE(INTERNET_FLAG_CACHE_ASYNC),
672 FE(INTERNET_FLAG_FORMS_SUBMIT),
673 FE(INTERNET_FLAG_NEED_FILE),
674 FE(INTERNET_FLAG_TRANSFER_ASCII),
675 FE(INTERNET_FLAG_TRANSFER_BINARY)
677 #undef FE
678 unsigned int i;
680 for (i = 0; i < (sizeof(flag) / sizeof(flag[0])); i++) {
681 if (flag[i].val & dwFlags) {
682 TRACE(" %s", flag[i].name);
683 dwFlags &= ~flag[i].val;
686 if (dwFlags)
687 TRACE(" Unknown flags (%08x)\n", dwFlags);
688 else
689 TRACE("\n");
692 /***********************************************************************
693 * INTERNET_CloseHandle (internal)
695 * Close internet handle
698 static VOID APPINFO_Destroy(object_header_t *hdr)
700 appinfo_t *lpwai = (appinfo_t*)hdr;
702 TRACE("%p\n",lpwai);
704 heap_free(lpwai->agent);
705 heap_free(lpwai->proxy);
706 heap_free(lpwai->proxyBypass);
707 heap_free(lpwai->proxyUsername);
708 heap_free(lpwai->proxyPassword);
711 static DWORD APPINFO_QueryOption(object_header_t *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
713 appinfo_t *ai = (appinfo_t*)hdr;
715 switch(option) {
716 case INTERNET_OPTION_HANDLE_TYPE:
717 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
719 if (*size < sizeof(ULONG))
720 return ERROR_INSUFFICIENT_BUFFER;
722 *size = sizeof(DWORD);
723 *(DWORD*)buffer = INTERNET_HANDLE_TYPE_INTERNET;
724 return ERROR_SUCCESS;
726 case INTERNET_OPTION_USER_AGENT: {
727 DWORD bufsize;
729 TRACE("INTERNET_OPTION_USER_AGENT\n");
731 bufsize = *size;
733 if (unicode) {
734 DWORD len = ai->agent ? strlenW(ai->agent) : 0;
736 *size = (len + 1) * sizeof(WCHAR);
737 if(!buffer || bufsize < *size)
738 return ERROR_INSUFFICIENT_BUFFER;
740 if (ai->agent)
741 strcpyW(buffer, ai->agent);
742 else
743 *(WCHAR *)buffer = 0;
744 /* If the buffer is copied, the returned length doesn't include
745 * the NULL terminator.
747 *size = len * sizeof(WCHAR);
748 }else {
749 if (ai->agent)
750 *size = WideCharToMultiByte(CP_ACP, 0, ai->agent, -1, NULL, 0, NULL, NULL);
751 else
752 *size = 1;
753 if(!buffer || bufsize < *size)
754 return ERROR_INSUFFICIENT_BUFFER;
756 if (ai->agent)
757 WideCharToMultiByte(CP_ACP, 0, ai->agent, -1, buffer, *size, NULL, NULL);
758 else
759 *(char *)buffer = 0;
760 /* If the buffer is copied, the returned length doesn't include
761 * the NULL terminator.
763 *size -= 1;
766 return ERROR_SUCCESS;
769 case INTERNET_OPTION_PROXY:
770 if (unicode) {
771 INTERNET_PROXY_INFOW *pi = (INTERNET_PROXY_INFOW *)buffer;
772 DWORD proxyBytesRequired = 0, proxyBypassBytesRequired = 0;
773 LPWSTR proxy, proxy_bypass;
775 if (ai->proxy)
776 proxyBytesRequired = (lstrlenW(ai->proxy) + 1) * sizeof(WCHAR);
777 if (ai->proxyBypass)
778 proxyBypassBytesRequired = (lstrlenW(ai->proxyBypass) + 1) * sizeof(WCHAR);
779 if (*size < sizeof(INTERNET_PROXY_INFOW) + proxyBytesRequired + proxyBypassBytesRequired)
781 *size = sizeof(INTERNET_PROXY_INFOW) + proxyBytesRequired + proxyBypassBytesRequired;
782 return ERROR_INSUFFICIENT_BUFFER;
784 proxy = (LPWSTR)((LPBYTE)buffer + sizeof(INTERNET_PROXY_INFOW));
785 proxy_bypass = (LPWSTR)((LPBYTE)buffer + sizeof(INTERNET_PROXY_INFOW) + proxyBytesRequired);
787 pi->dwAccessType = ai->accessType;
788 pi->lpszProxy = NULL;
789 pi->lpszProxyBypass = NULL;
790 if (ai->proxy) {
791 lstrcpyW(proxy, ai->proxy);
792 pi->lpszProxy = proxy;
795 if (ai->proxyBypass) {
796 lstrcpyW(proxy_bypass, ai->proxyBypass);
797 pi->lpszProxyBypass = proxy_bypass;
800 *size = sizeof(INTERNET_PROXY_INFOW) + proxyBytesRequired + proxyBypassBytesRequired;
801 return ERROR_SUCCESS;
802 }else {
803 INTERNET_PROXY_INFOA *pi = (INTERNET_PROXY_INFOA *)buffer;
804 DWORD proxyBytesRequired = 0, proxyBypassBytesRequired = 0;
805 LPSTR proxy, proxy_bypass;
807 if (ai->proxy)
808 proxyBytesRequired = WideCharToMultiByte(CP_ACP, 0, ai->proxy, -1, NULL, 0, NULL, NULL);
809 if (ai->proxyBypass)
810 proxyBypassBytesRequired = WideCharToMultiByte(CP_ACP, 0, ai->proxyBypass, -1,
811 NULL, 0, NULL, NULL);
812 if (*size < sizeof(INTERNET_PROXY_INFOA) + proxyBytesRequired + proxyBypassBytesRequired)
814 *size = sizeof(INTERNET_PROXY_INFOA) + proxyBytesRequired + proxyBypassBytesRequired;
815 return ERROR_INSUFFICIENT_BUFFER;
817 proxy = (LPSTR)((LPBYTE)buffer + sizeof(INTERNET_PROXY_INFOA));
818 proxy_bypass = (LPSTR)((LPBYTE)buffer + sizeof(INTERNET_PROXY_INFOA) + proxyBytesRequired);
820 pi->dwAccessType = ai->accessType;
821 pi->lpszProxy = NULL;
822 pi->lpszProxyBypass = NULL;
823 if (ai->proxy) {
824 WideCharToMultiByte(CP_ACP, 0, ai->proxy, -1, proxy, proxyBytesRequired, NULL, NULL);
825 pi->lpszProxy = proxy;
828 if (ai->proxyBypass) {
829 WideCharToMultiByte(CP_ACP, 0, ai->proxyBypass, -1, proxy_bypass,
830 proxyBypassBytesRequired, NULL, NULL);
831 pi->lpszProxyBypass = proxy_bypass;
834 *size = sizeof(INTERNET_PROXY_INFOA) + proxyBytesRequired + proxyBypassBytesRequired;
835 return ERROR_SUCCESS;
839 return INET_QueryOption(hdr, option, buffer, size, unicode);
842 static const object_vtbl_t APPINFOVtbl = {
843 APPINFO_Destroy,
844 NULL,
845 APPINFO_QueryOption,
846 NULL,
847 NULL,
848 NULL,
849 NULL,
850 NULL,
851 NULL
855 /***********************************************************************
856 * InternetOpenW (WININET.@)
858 * Per-application initialization of wininet
860 * RETURNS
861 * HINTERNET on success
862 * NULL on failure
865 HINTERNET WINAPI InternetOpenW(LPCWSTR lpszAgent, DWORD dwAccessType,
866 LPCWSTR lpszProxy, LPCWSTR lpszProxyBypass, DWORD dwFlags)
868 appinfo_t *lpwai = NULL;
870 if (TRACE_ON(wininet)) {
871 #define FE(x) { x, #x }
872 static const wininet_flag_info access_type[] = {
873 FE(INTERNET_OPEN_TYPE_PRECONFIG),
874 FE(INTERNET_OPEN_TYPE_DIRECT),
875 FE(INTERNET_OPEN_TYPE_PROXY),
876 FE(INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY)
878 #undef FE
879 DWORD i;
880 const char *access_type_str = "Unknown";
882 TRACE("(%s, %i, %s, %s, %i)\n", debugstr_w(lpszAgent), dwAccessType,
883 debugstr_w(lpszProxy), debugstr_w(lpszProxyBypass), dwFlags);
884 for (i = 0; i < (sizeof(access_type) / sizeof(access_type[0])); i++) {
885 if (access_type[i].val == dwAccessType) {
886 access_type_str = access_type[i].name;
887 break;
890 TRACE(" access type : %s\n", access_type_str);
891 TRACE(" flags :");
892 dump_INTERNET_FLAGS(dwFlags);
895 /* Clear any error information */
896 INTERNET_SetLastError(0);
898 lpwai = alloc_object(NULL, &APPINFOVtbl, sizeof(appinfo_t));
899 if (!lpwai) {
900 SetLastError(ERROR_OUTOFMEMORY);
901 return NULL;
904 lpwai->hdr.htype = WH_HINIT;
905 lpwai->hdr.dwFlags = dwFlags;
906 lpwai->accessType = dwAccessType;
907 lpwai->proxyUsername = NULL;
908 lpwai->proxyPassword = NULL;
910 lpwai->agent = heap_strdupW(lpszAgent);
911 if(dwAccessType == INTERNET_OPEN_TYPE_PRECONFIG)
912 INTERNET_ConfigureProxy( lpwai );
913 else
914 lpwai->proxy = heap_strdupW(lpszProxy);
915 lpwai->proxyBypass = heap_strdupW(lpszProxyBypass);
917 TRACE("returning %p\n", lpwai);
919 return lpwai->hdr.hInternet;
923 /***********************************************************************
924 * InternetOpenA (WININET.@)
926 * Per-application initialization of wininet
928 * RETURNS
929 * HINTERNET on success
930 * NULL on failure
933 HINTERNET WINAPI InternetOpenA(LPCSTR lpszAgent, DWORD dwAccessType,
934 LPCSTR lpszProxy, LPCSTR lpszProxyBypass, DWORD dwFlags)
936 WCHAR *szAgent, *szProxy, *szBypass;
937 HINTERNET rc;
939 TRACE("(%s, 0x%08x, %s, %s, 0x%08x)\n", debugstr_a(lpszAgent),
940 dwAccessType, debugstr_a(lpszProxy), debugstr_a(lpszProxyBypass), dwFlags);
942 szAgent = heap_strdupAtoW(lpszAgent);
943 szProxy = heap_strdupAtoW(lpszProxy);
944 szBypass = heap_strdupAtoW(lpszProxyBypass);
946 rc = InternetOpenW(szAgent, dwAccessType, szProxy, szBypass, dwFlags);
948 heap_free(szAgent);
949 heap_free(szProxy);
950 heap_free(szBypass);
951 return rc;
954 /***********************************************************************
955 * InternetGetLastResponseInfoA (WININET.@)
957 * Return last wininet error description on the calling thread
959 * RETURNS
960 * TRUE on success of writing to buffer
961 * FALSE on failure
964 BOOL WINAPI InternetGetLastResponseInfoA(LPDWORD lpdwError,
965 LPSTR lpszBuffer, LPDWORD lpdwBufferLength)
967 LPWITHREADERROR lpwite = TlsGetValue(g_dwTlsErrIndex);
969 TRACE("\n");
971 if (lpwite)
973 *lpdwError = lpwite->dwError;
974 if (lpwite->dwError)
976 memcpy(lpszBuffer, lpwite->response, *lpdwBufferLength);
977 *lpdwBufferLength = strlen(lpszBuffer);
979 else
980 *lpdwBufferLength = 0;
982 else
984 *lpdwError = 0;
985 *lpdwBufferLength = 0;
988 return TRUE;
991 /***********************************************************************
992 * InternetGetLastResponseInfoW (WININET.@)
994 * Return last wininet error description on the calling thread
996 * RETURNS
997 * TRUE on success of writing to buffer
998 * FALSE on failure
1001 BOOL WINAPI InternetGetLastResponseInfoW(LPDWORD lpdwError,
1002 LPWSTR lpszBuffer, LPDWORD lpdwBufferLength)
1004 LPWITHREADERROR lpwite = TlsGetValue(g_dwTlsErrIndex);
1006 TRACE("\n");
1008 if (lpwite)
1010 *lpdwError = lpwite->dwError;
1011 if (lpwite->dwError)
1013 memcpy(lpszBuffer, lpwite->response, *lpdwBufferLength);
1014 *lpdwBufferLength = lstrlenW(lpszBuffer);
1016 else
1017 *lpdwBufferLength = 0;
1019 else
1021 *lpdwError = 0;
1022 *lpdwBufferLength = 0;
1025 return TRUE;
1028 /***********************************************************************
1029 * InternetGetConnectedState (WININET.@)
1031 * Return connected state
1033 * RETURNS
1034 * TRUE if connected
1035 * if lpdwStatus is not null, return the status (off line,
1036 * modem, lan...) in it.
1037 * FALSE if not connected
1039 BOOL WINAPI InternetGetConnectedState(LPDWORD lpdwStatus, DWORD dwReserved)
1041 TRACE("(%p, 0x%08x)\n", lpdwStatus, dwReserved);
1043 if (lpdwStatus) {
1044 WARN("always returning LAN connection.\n");
1045 *lpdwStatus = INTERNET_CONNECTION_LAN;
1047 return TRUE;
1051 /***********************************************************************
1052 * InternetGetConnectedStateExW (WININET.@)
1054 * Return connected state
1056 * PARAMS
1058 * lpdwStatus [O] Flags specifying the status of the internet connection.
1059 * lpszConnectionName [O] Pointer to buffer to receive the friendly name of the internet connection.
1060 * dwNameLen [I] Size of the buffer, in characters.
1061 * dwReserved [I] Reserved. Must be set to 0.
1063 * RETURNS
1064 * TRUE if connected
1065 * if lpdwStatus is not null, return the status (off line,
1066 * modem, lan...) in it.
1067 * FALSE if not connected
1069 * NOTES
1070 * If the system has no available network connections, an empty string is
1071 * stored in lpszConnectionName. If there is a LAN connection, a localized
1072 * "LAN Connection" string is stored. Presumably, if only a dial-up
1073 * connection is available then the name of the dial-up connection is
1074 * returned. Why any application, other than the "Internet Settings" CPL,
1075 * would want to use this function instead of the simpler InternetGetConnectedStateW
1076 * function is beyond me.
1078 BOOL WINAPI InternetGetConnectedStateExW(LPDWORD lpdwStatus, LPWSTR lpszConnectionName,
1079 DWORD dwNameLen, DWORD dwReserved)
1081 TRACE("(%p, %p, %d, 0x%08x)\n", lpdwStatus, lpszConnectionName, dwNameLen, dwReserved);
1083 /* Must be zero */
1084 if(dwReserved)
1085 return FALSE;
1087 if (lpdwStatus) {
1088 WARN("always returning LAN connection.\n");
1089 *lpdwStatus = INTERNET_CONNECTION_LAN;
1091 return LoadStringW(WININET_hModule, IDS_LANCONNECTION, lpszConnectionName, dwNameLen);
1095 /***********************************************************************
1096 * InternetGetConnectedStateExA (WININET.@)
1098 BOOL WINAPI InternetGetConnectedStateExA(LPDWORD lpdwStatus, LPSTR lpszConnectionName,
1099 DWORD dwNameLen, DWORD dwReserved)
1101 LPWSTR lpwszConnectionName = NULL;
1102 BOOL rc;
1104 TRACE("(%p, %p, %d, 0x%08x)\n", lpdwStatus, lpszConnectionName, dwNameLen, dwReserved);
1106 if (lpszConnectionName && dwNameLen > 0)
1107 lpwszConnectionName = heap_alloc(dwNameLen * sizeof(WCHAR));
1109 rc = InternetGetConnectedStateExW(lpdwStatus,lpwszConnectionName, dwNameLen,
1110 dwReserved);
1111 if (rc && lpwszConnectionName)
1113 WideCharToMultiByte(CP_ACP,0,lpwszConnectionName,-1,lpszConnectionName,
1114 dwNameLen, NULL, NULL);
1115 heap_free(lpwszConnectionName);
1117 return rc;
1121 /***********************************************************************
1122 * InternetConnectW (WININET.@)
1124 * Open a ftp, gopher or http session
1126 * RETURNS
1127 * HINTERNET a session handle on success
1128 * NULL on failure
1131 HINTERNET WINAPI InternetConnectW(HINTERNET hInternet,
1132 LPCWSTR lpszServerName, INTERNET_PORT nServerPort,
1133 LPCWSTR lpszUserName, LPCWSTR lpszPassword,
1134 DWORD dwService, DWORD dwFlags, DWORD_PTR dwContext)
1136 appinfo_t *hIC;
1137 HINTERNET rc = NULL;
1138 DWORD res = ERROR_SUCCESS;
1140 TRACE("(%p, %s, %i, %s, %s, %i, %i, %lx)\n", hInternet, debugstr_w(lpszServerName),
1141 nServerPort, debugstr_w(lpszUserName), debugstr_w(lpszPassword),
1142 dwService, dwFlags, dwContext);
1144 if (!lpszServerName)
1146 SetLastError(ERROR_INVALID_PARAMETER);
1147 return NULL;
1150 hIC = (appinfo_t*)get_handle_object( hInternet );
1151 if ( (hIC == NULL) || (hIC->hdr.htype != WH_HINIT) )
1153 res = ERROR_INVALID_HANDLE;
1154 goto lend;
1157 switch (dwService)
1159 case INTERNET_SERVICE_FTP:
1160 rc = FTP_Connect(hIC, lpszServerName, nServerPort,
1161 lpszUserName, lpszPassword, dwFlags, dwContext, 0);
1162 if(!rc)
1163 res = INTERNET_GetLastError();
1164 break;
1166 case INTERNET_SERVICE_HTTP:
1167 res = HTTP_Connect(hIC, lpszServerName, nServerPort,
1168 lpszUserName, lpszPassword, dwFlags, dwContext, 0, &rc);
1169 break;
1171 case INTERNET_SERVICE_GOPHER:
1172 default:
1173 break;
1175 lend:
1176 if( hIC )
1177 WININET_Release( &hIC->hdr );
1179 TRACE("returning %p\n", rc);
1180 SetLastError(res);
1181 return rc;
1185 /***********************************************************************
1186 * InternetConnectA (WININET.@)
1188 * Open a ftp, gopher or http session
1190 * RETURNS
1191 * HINTERNET a session handle on success
1192 * NULL on failure
1195 HINTERNET WINAPI InternetConnectA(HINTERNET hInternet,
1196 LPCSTR lpszServerName, INTERNET_PORT nServerPort,
1197 LPCSTR lpszUserName, LPCSTR lpszPassword,
1198 DWORD dwService, DWORD dwFlags, DWORD_PTR dwContext)
1200 HINTERNET rc = NULL;
1201 LPWSTR szServerName;
1202 LPWSTR szUserName;
1203 LPWSTR szPassword;
1205 szServerName = heap_strdupAtoW(lpszServerName);
1206 szUserName = heap_strdupAtoW(lpszUserName);
1207 szPassword = heap_strdupAtoW(lpszPassword);
1209 rc = InternetConnectW(hInternet, szServerName, nServerPort,
1210 szUserName, szPassword, dwService, dwFlags, dwContext);
1212 heap_free(szServerName);
1213 heap_free(szUserName);
1214 heap_free(szPassword);
1215 return rc;
1219 /***********************************************************************
1220 * InternetFindNextFileA (WININET.@)
1222 * Continues a file search from a previous call to FindFirstFile
1224 * RETURNS
1225 * TRUE on success
1226 * FALSE on failure
1229 BOOL WINAPI InternetFindNextFileA(HINTERNET hFind, LPVOID lpvFindData)
1231 BOOL ret;
1232 WIN32_FIND_DATAW fd;
1234 ret = InternetFindNextFileW(hFind, lpvFindData?&fd:NULL);
1235 if(lpvFindData)
1236 WININET_find_data_WtoA(&fd, (LPWIN32_FIND_DATAA)lpvFindData);
1237 return ret;
1240 /***********************************************************************
1241 * InternetFindNextFileW (WININET.@)
1243 * Continues a file search from a previous call to FindFirstFile
1245 * RETURNS
1246 * TRUE on success
1247 * FALSE on failure
1250 BOOL WINAPI InternetFindNextFileW(HINTERNET hFind, LPVOID lpvFindData)
1252 object_header_t *hdr;
1253 DWORD res;
1255 TRACE("\n");
1257 hdr = get_handle_object(hFind);
1258 if(!hdr) {
1259 WARN("Invalid handle\n");
1260 SetLastError(ERROR_INVALID_HANDLE);
1261 return FALSE;
1264 if(hdr->vtbl->FindNextFileW) {
1265 res = hdr->vtbl->FindNextFileW(hdr, lpvFindData);
1266 }else {
1267 WARN("Handle doesn't support NextFile\n");
1268 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
1271 WININET_Release(hdr);
1273 if(res != ERROR_SUCCESS)
1274 SetLastError(res);
1275 return res == ERROR_SUCCESS;
1278 /***********************************************************************
1279 * InternetCloseHandle (WININET.@)
1281 * Generic close handle function
1283 * RETURNS
1284 * TRUE on success
1285 * FALSE on failure
1288 BOOL WINAPI InternetCloseHandle(HINTERNET hInternet)
1290 object_header_t *obj;
1292 TRACE("%p\n", hInternet);
1294 obj = get_handle_object( hInternet );
1295 if (!obj) {
1296 SetLastError(ERROR_INVALID_HANDLE);
1297 return FALSE;
1300 invalidate_handle(obj);
1301 WININET_Release(obj);
1303 return TRUE;
1307 /***********************************************************************
1308 * ConvertUrlComponentValue (Internal)
1310 * Helper function for InternetCrackUrlA
1313 static void ConvertUrlComponentValue(LPSTR* lppszComponent, LPDWORD dwComponentLen,
1314 LPWSTR lpwszComponent, DWORD dwwComponentLen,
1315 LPCSTR lpszStart, LPCWSTR lpwszStart)
1317 TRACE("%p %d %p %d %p %p\n", *lppszComponent, *dwComponentLen, lpwszComponent, dwwComponentLen, lpszStart, lpwszStart);
1318 if (*dwComponentLen != 0)
1320 DWORD nASCIILength=WideCharToMultiByte(CP_ACP,0,lpwszComponent,dwwComponentLen,NULL,0,NULL,NULL);
1321 if (*lppszComponent == NULL)
1323 if (lpwszComponent)
1325 int offset = WideCharToMultiByte(CP_ACP, 0, lpwszStart, lpwszComponent-lpwszStart, NULL, 0, NULL, NULL);
1326 *lppszComponent = (LPSTR)lpszStart + offset;
1328 else
1329 *lppszComponent = NULL;
1331 *dwComponentLen = nASCIILength;
1333 else
1335 DWORD ncpylen = min((*dwComponentLen)-1, nASCIILength);
1336 WideCharToMultiByte(CP_ACP,0,lpwszComponent,dwwComponentLen,*lppszComponent,ncpylen+1,NULL,NULL);
1337 (*lppszComponent)[ncpylen]=0;
1338 *dwComponentLen = ncpylen;
1344 /***********************************************************************
1345 * InternetCrackUrlA (WININET.@)
1347 * See InternetCrackUrlW.
1349 BOOL WINAPI InternetCrackUrlA(LPCSTR lpszUrl, DWORD dwUrlLength, DWORD dwFlags,
1350 LPURL_COMPONENTSA lpUrlComponents)
1352 DWORD nLength;
1353 URL_COMPONENTSW UCW;
1354 BOOL ret = FALSE;
1355 WCHAR *lpwszUrl, *hostname = NULL, *username = NULL, *password = NULL, *path = NULL,
1356 *scheme = NULL, *extra = NULL;
1358 TRACE("(%s %u %x %p)\n",
1359 lpszUrl ? debugstr_an(lpszUrl, dwUrlLength ? dwUrlLength : strlen(lpszUrl)) : "(null)",
1360 dwUrlLength, dwFlags, lpUrlComponents);
1362 if (!lpszUrl || !*lpszUrl || !lpUrlComponents ||
1363 lpUrlComponents->dwStructSize != sizeof(URL_COMPONENTSA))
1365 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1366 return FALSE;
1369 if(dwUrlLength<=0)
1370 dwUrlLength=-1;
1371 nLength=MultiByteToWideChar(CP_ACP,0,lpszUrl,dwUrlLength,NULL,0);
1373 /* if dwUrlLength=-1 then nLength includes null but length to
1374 InternetCrackUrlW should not include it */
1375 if (dwUrlLength == -1) nLength--;
1377 lpwszUrl = heap_alloc((nLength + 1) * sizeof(WCHAR));
1378 MultiByteToWideChar(CP_ACP,0,lpszUrl,dwUrlLength,lpwszUrl,nLength + 1);
1379 lpwszUrl[nLength] = '\0';
1381 memset(&UCW,0,sizeof(UCW));
1382 UCW.dwStructSize = sizeof(URL_COMPONENTSW);
1383 if (lpUrlComponents->dwHostNameLength)
1385 UCW.dwHostNameLength = lpUrlComponents->dwHostNameLength;
1386 if (lpUrlComponents->lpszHostName)
1388 hostname = heap_alloc(UCW.dwHostNameLength * sizeof(WCHAR));
1389 UCW.lpszHostName = hostname;
1392 if (lpUrlComponents->dwUserNameLength)
1394 UCW.dwUserNameLength = lpUrlComponents->dwUserNameLength;
1395 if (lpUrlComponents->lpszUserName)
1397 username = heap_alloc(UCW.dwUserNameLength * sizeof(WCHAR));
1398 UCW.lpszUserName = username;
1401 if (lpUrlComponents->dwPasswordLength)
1403 UCW.dwPasswordLength = lpUrlComponents->dwPasswordLength;
1404 if (lpUrlComponents->lpszPassword)
1406 password = heap_alloc(UCW.dwPasswordLength * sizeof(WCHAR));
1407 UCW.lpszPassword = password;
1410 if (lpUrlComponents->dwUrlPathLength)
1412 UCW.dwUrlPathLength = lpUrlComponents->dwUrlPathLength;
1413 if (lpUrlComponents->lpszUrlPath)
1415 path = heap_alloc(UCW.dwUrlPathLength * sizeof(WCHAR));
1416 UCW.lpszUrlPath = path;
1419 if (lpUrlComponents->dwSchemeLength)
1421 UCW.dwSchemeLength = lpUrlComponents->dwSchemeLength;
1422 if (lpUrlComponents->lpszScheme)
1424 scheme = heap_alloc(UCW.dwSchemeLength * sizeof(WCHAR));
1425 UCW.lpszScheme = scheme;
1428 if (lpUrlComponents->dwExtraInfoLength)
1430 UCW.dwExtraInfoLength = lpUrlComponents->dwExtraInfoLength;
1431 if (lpUrlComponents->lpszExtraInfo)
1433 extra = heap_alloc(UCW.dwExtraInfoLength * sizeof(WCHAR));
1434 UCW.lpszExtraInfo = extra;
1437 if ((ret = InternetCrackUrlW(lpwszUrl, nLength, dwFlags, &UCW)))
1439 ConvertUrlComponentValue(&lpUrlComponents->lpszHostName, &lpUrlComponents->dwHostNameLength,
1440 UCW.lpszHostName, UCW.dwHostNameLength, lpszUrl, lpwszUrl);
1441 ConvertUrlComponentValue(&lpUrlComponents->lpszUserName, &lpUrlComponents->dwUserNameLength,
1442 UCW.lpszUserName, UCW.dwUserNameLength, lpszUrl, lpwszUrl);
1443 ConvertUrlComponentValue(&lpUrlComponents->lpszPassword, &lpUrlComponents->dwPasswordLength,
1444 UCW.lpszPassword, UCW.dwPasswordLength, lpszUrl, lpwszUrl);
1445 ConvertUrlComponentValue(&lpUrlComponents->lpszUrlPath, &lpUrlComponents->dwUrlPathLength,
1446 UCW.lpszUrlPath, UCW.dwUrlPathLength, lpszUrl, lpwszUrl);
1447 ConvertUrlComponentValue(&lpUrlComponents->lpszScheme, &lpUrlComponents->dwSchemeLength,
1448 UCW.lpszScheme, UCW.dwSchemeLength, lpszUrl, lpwszUrl);
1449 ConvertUrlComponentValue(&lpUrlComponents->lpszExtraInfo, &lpUrlComponents->dwExtraInfoLength,
1450 UCW.lpszExtraInfo, UCW.dwExtraInfoLength, lpszUrl, lpwszUrl);
1452 lpUrlComponents->nScheme = UCW.nScheme;
1453 lpUrlComponents->nPort = UCW.nPort;
1455 TRACE("%s: scheme(%s) host(%s) path(%s) extra(%s)\n", debugstr_a(lpszUrl),
1456 debugstr_an(lpUrlComponents->lpszScheme, lpUrlComponents->dwSchemeLength),
1457 debugstr_an(lpUrlComponents->lpszHostName, lpUrlComponents->dwHostNameLength),
1458 debugstr_an(lpUrlComponents->lpszUrlPath, lpUrlComponents->dwUrlPathLength),
1459 debugstr_an(lpUrlComponents->lpszExtraInfo, lpUrlComponents->dwExtraInfoLength));
1461 heap_free(lpwszUrl);
1462 heap_free(hostname);
1463 heap_free(username);
1464 heap_free(password);
1465 heap_free(path);
1466 heap_free(scheme);
1467 heap_free(extra);
1468 return ret;
1471 static const WCHAR url_schemes[][7] =
1473 {'f','t','p',0},
1474 {'g','o','p','h','e','r',0},
1475 {'h','t','t','p',0},
1476 {'h','t','t','p','s',0},
1477 {'f','i','l','e',0},
1478 {'n','e','w','s',0},
1479 {'m','a','i','l','t','o',0},
1480 {'r','e','s',0},
1483 /***********************************************************************
1484 * GetInternetSchemeW (internal)
1486 * Get scheme of url
1488 * RETURNS
1489 * scheme on success
1490 * INTERNET_SCHEME_UNKNOWN on failure
1493 static INTERNET_SCHEME GetInternetSchemeW(LPCWSTR lpszScheme, DWORD nMaxCmp)
1495 int i;
1497 TRACE("%s %d\n",debugstr_wn(lpszScheme, nMaxCmp), nMaxCmp);
1499 if(lpszScheme==NULL)
1500 return INTERNET_SCHEME_UNKNOWN;
1502 for (i = 0; i < sizeof(url_schemes)/sizeof(url_schemes[0]); i++)
1503 if (!strncmpW(lpszScheme, url_schemes[i], nMaxCmp))
1504 return INTERNET_SCHEME_FIRST + i;
1506 return INTERNET_SCHEME_UNKNOWN;
1509 /***********************************************************************
1510 * SetUrlComponentValueW (Internal)
1512 * Helper function for InternetCrackUrlW
1514 * PARAMS
1515 * lppszComponent [O] Holds the returned string
1516 * dwComponentLen [I] Holds the size of lppszComponent
1517 * [O] Holds the length of the string in lppszComponent without '\0'
1518 * lpszStart [I] Holds the string to copy from
1519 * len [I] Holds the length of lpszStart without '\0'
1521 * RETURNS
1522 * TRUE on success
1523 * FALSE on failure
1526 static BOOL SetUrlComponentValueW(LPWSTR* lppszComponent, LPDWORD dwComponentLen, LPCWSTR lpszStart, DWORD len)
1528 TRACE("%s (%d)\n", debugstr_wn(lpszStart,len), len);
1530 if ( (*dwComponentLen == 0) && (*lppszComponent == NULL) )
1531 return FALSE;
1533 if (*dwComponentLen != 0 || *lppszComponent == NULL)
1535 if (*lppszComponent == NULL)
1537 *lppszComponent = (LPWSTR)lpszStart;
1538 *dwComponentLen = len;
1540 else
1542 DWORD ncpylen = min((*dwComponentLen)-1, len);
1543 memcpy(*lppszComponent, lpszStart, ncpylen*sizeof(WCHAR));
1544 (*lppszComponent)[ncpylen] = '\0';
1545 *dwComponentLen = ncpylen;
1549 return TRUE;
1552 /***********************************************************************
1553 * InternetCrackUrlW (WININET.@)
1555 * Break up URL into its components
1557 * RETURNS
1558 * TRUE on success
1559 * FALSE on failure
1561 BOOL WINAPI InternetCrackUrlW(LPCWSTR lpszUrl_orig, DWORD dwUrlLength_orig, DWORD dwFlags,
1562 LPURL_COMPONENTSW lpUC)
1565 * RFC 1808
1566 * <protocol>:[//<net_loc>][/path][;<params>][?<query>][#<fragment>]
1569 LPCWSTR lpszParam = NULL;
1570 BOOL bIsAbsolute = FALSE;
1571 LPCWSTR lpszap, lpszUrl = lpszUrl_orig;
1572 LPCWSTR lpszcp = NULL;
1573 LPWSTR lpszUrl_decode = NULL;
1574 DWORD dwUrlLength = dwUrlLength_orig;
1576 TRACE("(%s %u %x %p)\n",
1577 lpszUrl ? debugstr_wn(lpszUrl, dwUrlLength ? dwUrlLength : strlenW(lpszUrl)) : "(null)",
1578 dwUrlLength, dwFlags, lpUC);
1580 if (!lpszUrl_orig || !*lpszUrl_orig || !lpUC)
1582 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1583 return FALSE;
1585 if (!dwUrlLength) dwUrlLength = strlenW(lpszUrl);
1587 if (dwFlags & ICU_DECODE)
1589 WCHAR *url_tmp;
1590 DWORD len = dwUrlLength + 1;
1592 if (!(url_tmp = heap_alloc(len * sizeof(WCHAR))))
1594 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1595 return FALSE;
1597 memcpy(url_tmp, lpszUrl_orig, dwUrlLength * sizeof(WCHAR));
1598 url_tmp[dwUrlLength] = 0;
1599 if (!(lpszUrl_decode = heap_alloc(len * sizeof(WCHAR))))
1601 heap_free(url_tmp);
1602 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1603 return FALSE;
1605 if (InternetCanonicalizeUrlW(url_tmp, lpszUrl_decode, &len, ICU_DECODE | ICU_NO_ENCODE))
1607 dwUrlLength = len;
1608 lpszUrl = lpszUrl_decode;
1610 heap_free(url_tmp);
1612 lpszap = lpszUrl;
1614 /* Determine if the URI is absolute. */
1615 while (lpszap - lpszUrl < dwUrlLength)
1617 if (isalnumW(*lpszap) || *lpszap == '+' || *lpszap == '.' || *lpszap == '-')
1619 lpszap++;
1620 continue;
1622 if ((*lpszap == ':') && (lpszap - lpszUrl >= 2))
1624 bIsAbsolute = TRUE;
1625 lpszcp = lpszap;
1627 else
1629 lpszcp = lpszUrl; /* Relative url */
1632 break;
1635 lpUC->nScheme = INTERNET_SCHEME_UNKNOWN;
1636 lpUC->nPort = INTERNET_INVALID_PORT_NUMBER;
1638 /* Parse <params> */
1639 lpszParam = memchrW(lpszap, ';', dwUrlLength - (lpszap - lpszUrl));
1640 if(!lpszParam)
1641 lpszParam = memchrW(lpszap, '?', dwUrlLength - (lpszap - lpszUrl));
1642 if(!lpszParam)
1643 lpszParam = memchrW(lpszap, '#', dwUrlLength - (lpszap - lpszUrl));
1645 SetUrlComponentValueW(&lpUC->lpszExtraInfo, &lpUC->dwExtraInfoLength,
1646 lpszParam, lpszParam ? dwUrlLength-(lpszParam-lpszUrl) : 0);
1648 if (bIsAbsolute) /* Parse <protocol>:[//<net_loc>] */
1650 LPCWSTR lpszNetLoc;
1652 /* Get scheme first. */
1653 lpUC->nScheme = GetInternetSchemeW(lpszUrl, lpszcp - lpszUrl);
1654 SetUrlComponentValueW(&lpUC->lpszScheme, &lpUC->dwSchemeLength,
1655 lpszUrl, lpszcp - lpszUrl);
1657 /* Eat ':' in protocol. */
1658 lpszcp++;
1660 /* double slash indicates the net_loc portion is present */
1661 if ((lpszcp[0] == '/') && (lpszcp[1] == '/'))
1663 lpszcp += 2;
1665 lpszNetLoc = memchrW(lpszcp, '/', dwUrlLength - (lpszcp - lpszUrl));
1666 if (lpszParam)
1668 if (lpszNetLoc)
1669 lpszNetLoc = min(lpszNetLoc, lpszParam);
1670 else
1671 lpszNetLoc = lpszParam;
1673 else if (!lpszNetLoc)
1674 lpszNetLoc = lpszcp + dwUrlLength-(lpszcp-lpszUrl);
1676 /* Parse net-loc */
1677 if (lpszNetLoc)
1679 LPCWSTR lpszHost;
1680 LPCWSTR lpszPort;
1682 /* [<user>[<:password>]@]<host>[:<port>] */
1683 /* First find the user and password if they exist */
1685 lpszHost = memchrW(lpszcp, '@', dwUrlLength - (lpszcp - lpszUrl));
1686 if (lpszHost == NULL || lpszHost > lpszNetLoc)
1688 /* username and password not specified. */
1689 SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength, NULL, 0);
1690 SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength, NULL, 0);
1692 else /* Parse out username and password */
1694 LPCWSTR lpszUser = lpszcp;
1695 LPCWSTR lpszPasswd = lpszHost;
1697 while (lpszcp < lpszHost)
1699 if (*lpszcp == ':')
1700 lpszPasswd = lpszcp;
1702 lpszcp++;
1705 SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength,
1706 lpszUser, lpszPasswd - lpszUser);
1708 if (lpszPasswd != lpszHost)
1709 lpszPasswd++;
1710 SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength,
1711 lpszPasswd == lpszHost ? NULL : lpszPasswd,
1712 lpszHost - lpszPasswd);
1714 lpszcp++; /* Advance to beginning of host */
1717 /* Parse <host><:port> */
1719 lpszHost = lpszcp;
1720 lpszPort = lpszNetLoc;
1722 /* special case for res:// URLs: there is no port here, so the host is the
1723 entire string up to the first '/' */
1724 if(lpUC->nScheme==INTERNET_SCHEME_RES)
1726 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength,
1727 lpszHost, lpszPort - lpszHost);
1728 lpszcp=lpszNetLoc;
1730 else
1732 while (lpszcp < lpszNetLoc)
1734 if (*lpszcp == ':')
1735 lpszPort = lpszcp;
1737 lpszcp++;
1740 /* If the scheme is "file" and the host is just one letter, it's not a host */
1741 if(lpUC->nScheme==INTERNET_SCHEME_FILE && lpszPort <= lpszHost+1)
1743 lpszcp=lpszHost;
1744 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength,
1745 NULL, 0);
1747 else
1749 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength,
1750 lpszHost, lpszPort - lpszHost);
1751 if (lpszPort != lpszNetLoc)
1752 lpUC->nPort = atoiW(++lpszPort);
1753 else switch (lpUC->nScheme)
1755 case INTERNET_SCHEME_HTTP:
1756 lpUC->nPort = INTERNET_DEFAULT_HTTP_PORT;
1757 break;
1758 case INTERNET_SCHEME_HTTPS:
1759 lpUC->nPort = INTERNET_DEFAULT_HTTPS_PORT;
1760 break;
1761 case INTERNET_SCHEME_FTP:
1762 lpUC->nPort = INTERNET_DEFAULT_FTP_PORT;
1763 break;
1764 case INTERNET_SCHEME_GOPHER:
1765 lpUC->nPort = INTERNET_DEFAULT_GOPHER_PORT;
1766 break;
1767 default:
1768 break;
1774 else
1776 SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength, NULL, 0);
1777 SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength, NULL, 0);
1778 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength, NULL, 0);
1781 else
1783 SetUrlComponentValueW(&lpUC->lpszScheme, &lpUC->dwSchemeLength, NULL, 0);
1784 SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength, NULL, 0);
1785 SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength, NULL, 0);
1786 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength, NULL, 0);
1789 /* Here lpszcp points to:
1791 * <protocol>:[//<net_loc>][/path][;<params>][?<query>][#<fragment>]
1792 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1794 if (lpszcp != 0 && lpszcp - lpszUrl < dwUrlLength && (!lpszParam || lpszcp <= lpszParam))
1796 DWORD len;
1798 /* Only truncate the parameter list if it's already been saved
1799 * in lpUC->lpszExtraInfo.
1801 if (lpszParam && lpUC->dwExtraInfoLength && lpUC->lpszExtraInfo)
1802 len = lpszParam - lpszcp;
1803 else
1805 /* Leave the parameter list in lpszUrlPath. Strip off any trailing
1806 * newlines if necessary.
1808 LPWSTR lpsznewline = memchrW(lpszcp, '\n', dwUrlLength - (lpszcp - lpszUrl));
1809 if (lpsznewline != NULL)
1810 len = lpsznewline - lpszcp;
1811 else
1812 len = dwUrlLength-(lpszcp-lpszUrl);
1814 if (lpUC->dwUrlPathLength && lpUC->lpszUrlPath &&
1815 lpUC->nScheme == INTERNET_SCHEME_FILE)
1817 WCHAR tmppath[MAX_PATH];
1818 if (*lpszcp == '/')
1820 len = MAX_PATH;
1821 PathCreateFromUrlW(lpszUrl_orig, tmppath, &len, 0);
1823 else
1825 WCHAR *iter;
1826 memcpy(tmppath, lpszcp, len * sizeof(WCHAR));
1827 tmppath[len] = '\0';
1829 iter = tmppath;
1830 while (*iter) {
1831 if (*iter == '/')
1832 *iter = '\\';
1833 ++iter;
1836 /* if ends in \. or \.. append a backslash */
1837 if (tmppath[len - 1] == '.' &&
1838 (tmppath[len - 2] == '\\' ||
1839 (tmppath[len - 2] == '.' && tmppath[len - 3] == '\\')))
1841 if (len < MAX_PATH - 1)
1843 tmppath[len] = '\\';
1844 tmppath[len+1] = '\0';
1845 ++len;
1848 SetUrlComponentValueW(&lpUC->lpszUrlPath, &lpUC->dwUrlPathLength,
1849 tmppath, len);
1851 else
1852 SetUrlComponentValueW(&lpUC->lpszUrlPath, &lpUC->dwUrlPathLength,
1853 lpszcp, len);
1855 else
1857 if (lpUC->lpszUrlPath && (lpUC->dwUrlPathLength > 0))
1858 lpUC->lpszUrlPath[0] = 0;
1859 lpUC->dwUrlPathLength = 0;
1862 TRACE("%s: scheme(%s) host(%s) path(%s) extra(%s)\n", debugstr_wn(lpszUrl,dwUrlLength),
1863 debugstr_wn(lpUC->lpszScheme,lpUC->dwSchemeLength),
1864 debugstr_wn(lpUC->lpszHostName,lpUC->dwHostNameLength),
1865 debugstr_wn(lpUC->lpszUrlPath,lpUC->dwUrlPathLength),
1866 debugstr_wn(lpUC->lpszExtraInfo,lpUC->dwExtraInfoLength));
1868 heap_free( lpszUrl_decode );
1869 return TRUE;
1872 /***********************************************************************
1873 * InternetAttemptConnect (WININET.@)
1875 * Attempt to make a connection to the internet
1877 * RETURNS
1878 * ERROR_SUCCESS on success
1879 * Error value on failure
1882 DWORD WINAPI InternetAttemptConnect(DWORD dwReserved)
1884 FIXME("Stub\n");
1885 return ERROR_SUCCESS;
1889 /***********************************************************************
1890 * InternetCanonicalizeUrlA (WININET.@)
1892 * Escape unsafe characters and spaces
1894 * RETURNS
1895 * TRUE on success
1896 * FALSE on failure
1899 BOOL WINAPI InternetCanonicalizeUrlA(LPCSTR lpszUrl, LPSTR lpszBuffer,
1900 LPDWORD lpdwBufferLength, DWORD dwFlags)
1902 HRESULT hr;
1903 DWORD dwURLFlags = URL_WININET_COMPATIBILITY | URL_ESCAPE_UNSAFE;
1905 TRACE("(%s, %p, %p, 0x%08x) bufferlength: %d\n", debugstr_a(lpszUrl), lpszBuffer,
1906 lpdwBufferLength, dwFlags, lpdwBufferLength ? *lpdwBufferLength : -1);
1908 if(dwFlags & ICU_DECODE)
1910 dwURLFlags |= URL_UNESCAPE;
1911 dwFlags &= ~ICU_DECODE;
1914 if(dwFlags & ICU_ESCAPE)
1916 dwURLFlags |= URL_UNESCAPE;
1917 dwFlags &= ~ICU_ESCAPE;
1920 if(dwFlags & ICU_BROWSER_MODE)
1922 dwURLFlags |= URL_BROWSER_MODE;
1923 dwFlags &= ~ICU_BROWSER_MODE;
1926 if(dwFlags & ICU_NO_ENCODE)
1928 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
1929 dwURLFlags ^= URL_ESCAPE_UNSAFE;
1930 dwFlags &= ~ICU_NO_ENCODE;
1933 if (dwFlags) FIXME("Unhandled flags 0x%08x\n", dwFlags);
1935 hr = UrlCanonicalizeA(lpszUrl, lpszBuffer, lpdwBufferLength, dwURLFlags);
1936 if (hr == E_POINTER) SetLastError(ERROR_INSUFFICIENT_BUFFER);
1937 if (hr == E_INVALIDARG) SetLastError(ERROR_INVALID_PARAMETER);
1939 return (hr == S_OK) ? TRUE : FALSE;
1942 /***********************************************************************
1943 * InternetCanonicalizeUrlW (WININET.@)
1945 * Escape unsafe characters and spaces
1947 * RETURNS
1948 * TRUE on success
1949 * FALSE on failure
1952 BOOL WINAPI InternetCanonicalizeUrlW(LPCWSTR lpszUrl, LPWSTR lpszBuffer,
1953 LPDWORD lpdwBufferLength, DWORD dwFlags)
1955 HRESULT hr;
1956 DWORD dwURLFlags = URL_WININET_COMPATIBILITY | URL_ESCAPE_UNSAFE;
1958 TRACE("(%s, %p, %p, 0x%08x) bufferlength: %d\n", debugstr_w(lpszUrl), lpszBuffer,
1959 lpdwBufferLength, dwFlags, lpdwBufferLength ? *lpdwBufferLength : -1);
1961 if(dwFlags & ICU_DECODE)
1963 dwURLFlags |= URL_UNESCAPE;
1964 dwFlags &= ~ICU_DECODE;
1967 if(dwFlags & ICU_ESCAPE)
1969 dwURLFlags |= URL_UNESCAPE;
1970 dwFlags &= ~ICU_ESCAPE;
1973 if(dwFlags & ICU_BROWSER_MODE)
1975 dwURLFlags |= URL_BROWSER_MODE;
1976 dwFlags &= ~ICU_BROWSER_MODE;
1979 if(dwFlags & ICU_NO_ENCODE)
1981 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
1982 dwURLFlags ^= URL_ESCAPE_UNSAFE;
1983 dwFlags &= ~ICU_NO_ENCODE;
1986 if (dwFlags) FIXME("Unhandled flags 0x%08x\n", dwFlags);
1988 hr = UrlCanonicalizeW(lpszUrl, lpszBuffer, lpdwBufferLength, dwURLFlags);
1989 if (hr == E_POINTER) SetLastError(ERROR_INSUFFICIENT_BUFFER);
1990 if (hr == E_INVALIDARG) SetLastError(ERROR_INVALID_PARAMETER);
1992 return (hr == S_OK) ? TRUE : FALSE;
1995 /* #################################################### */
1997 static INTERNET_STATUS_CALLBACK set_status_callback(
1998 object_header_t *lpwh, INTERNET_STATUS_CALLBACK callback, BOOL unicode)
2000 INTERNET_STATUS_CALLBACK ret;
2002 if (unicode) lpwh->dwInternalFlags |= INET_CALLBACKW;
2003 else lpwh->dwInternalFlags &= ~INET_CALLBACKW;
2005 ret = lpwh->lpfnStatusCB;
2006 lpwh->lpfnStatusCB = callback;
2008 return ret;
2011 /***********************************************************************
2012 * InternetSetStatusCallbackA (WININET.@)
2014 * Sets up a callback function which is called as progress is made
2015 * during an operation.
2017 * RETURNS
2018 * Previous callback or NULL on success
2019 * INTERNET_INVALID_STATUS_CALLBACK on failure
2022 INTERNET_STATUS_CALLBACK WINAPI InternetSetStatusCallbackA(
2023 HINTERNET hInternet ,INTERNET_STATUS_CALLBACK lpfnIntCB)
2025 INTERNET_STATUS_CALLBACK retVal;
2026 object_header_t *lpwh;
2028 TRACE("%p\n", hInternet);
2030 if (!(lpwh = get_handle_object(hInternet)))
2031 return INTERNET_INVALID_STATUS_CALLBACK;
2033 retVal = set_status_callback(lpwh, lpfnIntCB, FALSE);
2035 WININET_Release( lpwh );
2036 return retVal;
2039 /***********************************************************************
2040 * InternetSetStatusCallbackW (WININET.@)
2042 * Sets up a callback function which is called as progress is made
2043 * during an operation.
2045 * RETURNS
2046 * Previous callback or NULL on success
2047 * INTERNET_INVALID_STATUS_CALLBACK on failure
2050 INTERNET_STATUS_CALLBACK WINAPI InternetSetStatusCallbackW(
2051 HINTERNET hInternet ,INTERNET_STATUS_CALLBACK lpfnIntCB)
2053 INTERNET_STATUS_CALLBACK retVal;
2054 object_header_t *lpwh;
2056 TRACE("%p\n", hInternet);
2058 if (!(lpwh = get_handle_object(hInternet)))
2059 return INTERNET_INVALID_STATUS_CALLBACK;
2061 retVal = set_status_callback(lpwh, lpfnIntCB, TRUE);
2063 WININET_Release( lpwh );
2064 return retVal;
2067 /***********************************************************************
2068 * InternetSetFilePointer (WININET.@)
2070 DWORD WINAPI InternetSetFilePointer(HINTERNET hFile, LONG lDistanceToMove,
2071 PVOID pReserved, DWORD dwMoveContext, DWORD_PTR dwContext)
2073 FIXME("stub\n");
2074 return FALSE;
2077 /***********************************************************************
2078 * InternetWriteFile (WININET.@)
2080 * Write data to an open internet file
2082 * RETURNS
2083 * TRUE on success
2084 * FALSE on failure
2087 BOOL WINAPI InternetWriteFile(HINTERNET hFile, LPCVOID lpBuffer,
2088 DWORD dwNumOfBytesToWrite, LPDWORD lpdwNumOfBytesWritten)
2090 object_header_t *lpwh;
2091 BOOL res;
2093 TRACE("(%p %p %d %p)\n", hFile, lpBuffer, dwNumOfBytesToWrite, lpdwNumOfBytesWritten);
2095 lpwh = get_handle_object( hFile );
2096 if (!lpwh) {
2097 WARN("Invalid handle\n");
2098 SetLastError(ERROR_INVALID_HANDLE);
2099 return FALSE;
2102 if(lpwh->vtbl->WriteFile) {
2103 res = lpwh->vtbl->WriteFile(lpwh, lpBuffer, dwNumOfBytesToWrite, lpdwNumOfBytesWritten);
2104 }else {
2105 WARN("No Writefile method.\n");
2106 res = ERROR_INVALID_HANDLE;
2109 WININET_Release( lpwh );
2111 if(res != ERROR_SUCCESS)
2112 SetLastError(res);
2113 return res == ERROR_SUCCESS;
2117 /***********************************************************************
2118 * InternetReadFile (WININET.@)
2120 * Read data from an open internet file
2122 * RETURNS
2123 * TRUE on success
2124 * FALSE on failure
2127 BOOL WINAPI InternetReadFile(HINTERNET hFile, LPVOID lpBuffer,
2128 DWORD dwNumOfBytesToRead, LPDWORD pdwNumOfBytesRead)
2130 object_header_t *hdr;
2131 DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2133 TRACE("%p %p %d %p\n", hFile, lpBuffer, dwNumOfBytesToRead, pdwNumOfBytesRead);
2135 hdr = get_handle_object(hFile);
2136 if (!hdr) {
2137 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
2138 return FALSE;
2141 if(hdr->vtbl->ReadFile)
2142 res = hdr->vtbl->ReadFile(hdr, lpBuffer, dwNumOfBytesToRead, pdwNumOfBytesRead);
2144 WININET_Release(hdr);
2146 TRACE("-- %s (%u) (bytes read: %d)\n", res == ERROR_SUCCESS ? "TRUE": "FALSE", res,
2147 pdwNumOfBytesRead ? *pdwNumOfBytesRead : -1);
2149 if(res != ERROR_SUCCESS)
2150 SetLastError(res);
2151 return res == ERROR_SUCCESS;
2154 /***********************************************************************
2155 * InternetReadFileExA (WININET.@)
2157 * Read data from an open internet file
2159 * PARAMS
2160 * hFile [I] Handle returned by InternetOpenUrl or HttpOpenRequest.
2161 * lpBuffersOut [I/O] Buffer.
2162 * dwFlags [I] Flags. See notes.
2163 * dwContext [I] Context for callbacks.
2165 * RETURNS
2166 * TRUE on success
2167 * FALSE on failure
2169 * NOTES
2170 * The parameter dwFlags include zero or more of the following flags:
2171 *|IRF_ASYNC - Makes the call asynchronous.
2172 *|IRF_SYNC - Makes the call synchronous.
2173 *|IRF_USE_CONTEXT - Forces dwContext to be used.
2174 *|IRF_NO_WAIT - Don't block if the data is not available, just return what is available.
2176 * However, in testing IRF_USE_CONTEXT seems to have no effect - dwContext isn't used.
2178 * SEE
2179 * InternetOpenUrlA(), HttpOpenRequestA()
2181 BOOL WINAPI InternetReadFileExA(HINTERNET hFile, LPINTERNET_BUFFERSA lpBuffersOut,
2182 DWORD dwFlags, DWORD_PTR dwContext)
2184 object_header_t *hdr;
2185 DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2187 TRACE("(%p %p 0x%x 0x%lx)\n", hFile, lpBuffersOut, dwFlags, dwContext);
2189 hdr = get_handle_object(hFile);
2190 if (!hdr) {
2191 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
2192 return FALSE;
2195 if(hdr->vtbl->ReadFileExA)
2196 res = hdr->vtbl->ReadFileExA(hdr, lpBuffersOut, dwFlags, dwContext);
2198 WININET_Release(hdr);
2200 TRACE("-- %s (%u, bytes read: %d)\n", res == ERROR_SUCCESS ? "TRUE": "FALSE",
2201 res, lpBuffersOut->dwBufferLength);
2203 if(res != ERROR_SUCCESS)
2204 SetLastError(res);
2205 return res == ERROR_SUCCESS;
2208 /***********************************************************************
2209 * InternetReadFileExW (WININET.@)
2210 * SEE
2211 * InternetReadFileExA()
2213 BOOL WINAPI InternetReadFileExW(HINTERNET hFile, LPINTERNET_BUFFERSW lpBuffer,
2214 DWORD dwFlags, DWORD_PTR dwContext)
2216 object_header_t *hdr;
2217 DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2219 TRACE("(%p %p 0x%x 0x%lx)\n", hFile, lpBuffer, dwFlags, dwContext);
2221 hdr = get_handle_object(hFile);
2222 if (!hdr) {
2223 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
2224 return FALSE;
2227 if(hdr->vtbl->ReadFileExW)
2228 res = hdr->vtbl->ReadFileExW(hdr, lpBuffer, dwFlags, dwContext);
2230 WININET_Release(hdr);
2232 TRACE("-- %s (%u, bytes read: %d)\n", res == ERROR_SUCCESS ? "TRUE": "FALSE",
2233 res, lpBuffer->dwBufferLength);
2235 if(res != ERROR_SUCCESS)
2236 SetLastError(res);
2237 return res == ERROR_SUCCESS;
2240 DWORD INET_QueryOption(object_header_t *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
2242 static BOOL warn = TRUE;
2244 switch(option) {
2245 case INTERNET_OPTION_REQUEST_FLAGS:
2246 TRACE("INTERNET_OPTION_REQUEST_FLAGS\n");
2248 if (*size < sizeof(ULONG))
2249 return ERROR_INSUFFICIENT_BUFFER;
2251 *(ULONG*)buffer = 4;
2252 *size = sizeof(ULONG);
2254 return ERROR_SUCCESS;
2256 case INTERNET_OPTION_HTTP_VERSION:
2257 if (*size < sizeof(HTTP_VERSION_INFO))
2258 return ERROR_INSUFFICIENT_BUFFER;
2261 * Presently hardcoded to 1.1
2263 ((HTTP_VERSION_INFO*)buffer)->dwMajorVersion = 1;
2264 ((HTTP_VERSION_INFO*)buffer)->dwMinorVersion = 1;
2265 *size = sizeof(HTTP_VERSION_INFO);
2267 return ERROR_SUCCESS;
2269 case INTERNET_OPTION_CONNECTED_STATE:
2270 if (warn) {
2271 FIXME("INTERNET_OPTION_CONNECTED_STATE: semi-stub\n");
2272 warn = FALSE;
2274 if (*size < sizeof(ULONG))
2275 return ERROR_INSUFFICIENT_BUFFER;
2277 *(ULONG*)buffer = INTERNET_STATE_CONNECTED;
2278 *size = sizeof(ULONG);
2280 return ERROR_SUCCESS;
2282 case INTERNET_OPTION_PROXY: {
2283 appinfo_t ai;
2284 BOOL ret;
2286 TRACE("Getting global proxy info\n");
2287 memset(&ai, 0, sizeof(appinfo_t));
2288 INTERNET_ConfigureProxy(&ai);
2290 ret = APPINFO_QueryOption(&ai.hdr, INTERNET_OPTION_PROXY, buffer, size, unicode); /* FIXME */
2291 APPINFO_Destroy(&ai.hdr);
2292 return ret;
2295 case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2296 TRACE("INTERNET_OPTION_MAX_CONNS_PER_SERVER\n");
2298 if (*size < sizeof(ULONG))
2299 return ERROR_INSUFFICIENT_BUFFER;
2301 *(ULONG*)buffer = 2;
2302 *size = sizeof(ULONG);
2304 return ERROR_SUCCESS;
2306 case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2307 TRACE("INTERNET_OPTION_MAX_CONNS_1_0_SERVER\n");
2309 if (*size < sizeof(ULONG))
2310 return ERROR_INSUFFICIENT_BUFFER;
2312 *(ULONG*)buffer = 4;
2313 *size = sizeof(ULONG);
2315 return ERROR_SUCCESS;
2317 case INTERNET_OPTION_SECURITY_FLAGS:
2318 FIXME("INTERNET_OPTION_SECURITY_FLAGS: Stub\n");
2319 return ERROR_SUCCESS;
2321 case INTERNET_OPTION_VERSION: {
2322 static const INTERNET_VERSION_INFO info = { 1, 2 };
2324 TRACE("INTERNET_OPTION_VERSION\n");
2326 if (*size < sizeof(INTERNET_VERSION_INFO))
2327 return ERROR_INSUFFICIENT_BUFFER;
2329 memcpy(buffer, &info, sizeof(info));
2330 *size = sizeof(info);
2332 return ERROR_SUCCESS;
2335 case INTERNET_OPTION_PER_CONNECTION_OPTION: {
2336 INTERNET_PER_CONN_OPTION_LISTW *con = buffer;
2337 INTERNET_PER_CONN_OPTION_LISTA *conA = buffer;
2338 DWORD res = ERROR_SUCCESS, i;
2339 proxyinfo_t pi;
2340 LONG ret;
2342 TRACE("Getting global proxy info\n");
2343 if((ret = INTERNET_LoadProxySettings(&pi)))
2344 return ret;
2346 FIXME("INTERNET_OPTION_PER_CONNECTION_OPTION stub\n");
2348 if (*size < sizeof(INTERNET_PER_CONN_OPTION_LISTW)) {
2349 FreeProxyInfo(&pi);
2350 return ERROR_INSUFFICIENT_BUFFER;
2353 for (i = 0; i < con->dwOptionCount; i++) {
2354 INTERNET_PER_CONN_OPTIONW *optionW = con->pOptions + i;
2355 INTERNET_PER_CONN_OPTIONA *optionA = conA->pOptions + i;
2357 switch (optionW->dwOption) {
2358 case INTERNET_PER_CONN_FLAGS:
2359 if(pi.proxyEnabled)
2360 optionW->Value.dwValue = PROXY_TYPE_PROXY;
2361 else
2362 optionW->Value.dwValue = PROXY_TYPE_DIRECT;
2363 break;
2365 case INTERNET_PER_CONN_PROXY_SERVER:
2366 if (unicode)
2367 optionW->Value.pszValue = heap_strdupW(pi.proxy);
2368 else
2369 optionA->Value.pszValue = heap_strdupWtoA(pi.proxy);
2370 break;
2372 case INTERNET_PER_CONN_PROXY_BYPASS:
2373 if (unicode)
2374 optionW->Value.pszValue = heap_strdupW(pi.proxyBypass);
2375 else
2376 optionA->Value.pszValue = heap_strdupWtoA(pi.proxyBypass);
2377 break;
2379 case INTERNET_PER_CONN_AUTOCONFIG_URL:
2380 case INTERNET_PER_CONN_AUTODISCOVERY_FLAGS:
2381 case INTERNET_PER_CONN_AUTOCONFIG_SECONDARY_URL:
2382 case INTERNET_PER_CONN_AUTOCONFIG_RELOAD_DELAY_MINS:
2383 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_TIME:
2384 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_URL:
2385 FIXME("Unhandled dwOption %d\n", optionW->dwOption);
2386 memset(&optionW->Value, 0, sizeof(optionW->Value));
2387 break;
2389 default:
2390 FIXME("Unknown dwOption %d\n", optionW->dwOption);
2391 res = ERROR_INVALID_PARAMETER;
2392 break;
2395 FreeProxyInfo(&pi);
2397 return res;
2399 case INTERNET_OPTION_USER_AGENT:
2400 return ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2401 case INTERNET_OPTION_POLICY:
2402 return ERROR_INVALID_PARAMETER;
2403 case INTERNET_OPTION_CONTEXT_VALUE:
2405 if (!hdr)
2406 return ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2407 if (!size)
2408 return ERROR_INVALID_PARAMETER;
2410 if (*size < sizeof(DWORD_PTR))
2412 *size = sizeof(DWORD_PTR);
2413 return ERROR_INSUFFICIENT_BUFFER;
2415 if (!buffer)
2416 return ERROR_INVALID_PARAMETER;
2418 *(DWORD_PTR *)buffer = hdr->dwContext;
2419 *size = sizeof(DWORD_PTR);
2420 return ERROR_SUCCESS;
2424 FIXME("Stub for %d\n", option);
2425 return ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2428 /***********************************************************************
2429 * InternetQueryOptionW (WININET.@)
2431 * Queries an options on the specified handle
2433 * RETURNS
2434 * TRUE on success
2435 * FALSE on failure
2438 BOOL WINAPI InternetQueryOptionW(HINTERNET hInternet, DWORD dwOption,
2439 LPVOID lpBuffer, LPDWORD lpdwBufferLength)
2441 object_header_t *hdr;
2442 DWORD res = ERROR_INVALID_HANDLE;
2444 TRACE("%p %d %p %p\n", hInternet, dwOption, lpBuffer, lpdwBufferLength);
2446 if(hInternet) {
2447 hdr = get_handle_object(hInternet);
2448 if (hdr) {
2449 res = hdr->vtbl->QueryOption(hdr, dwOption, lpBuffer, lpdwBufferLength, TRUE);
2450 WININET_Release(hdr);
2452 }else {
2453 res = INET_QueryOption(NULL, dwOption, lpBuffer, lpdwBufferLength, TRUE);
2456 if(res != ERROR_SUCCESS)
2457 SetLastError(res);
2458 return res == ERROR_SUCCESS;
2461 /***********************************************************************
2462 * InternetQueryOptionA (WININET.@)
2464 * Queries an options on the specified handle
2466 * RETURNS
2467 * TRUE on success
2468 * FALSE on failure
2471 BOOL WINAPI InternetQueryOptionA(HINTERNET hInternet, DWORD dwOption,
2472 LPVOID lpBuffer, LPDWORD lpdwBufferLength)
2474 object_header_t *hdr;
2475 DWORD res = ERROR_INVALID_HANDLE;
2477 TRACE("%p %d %p %p\n", hInternet, dwOption, lpBuffer, lpdwBufferLength);
2479 if(hInternet) {
2480 hdr = get_handle_object(hInternet);
2481 if (hdr) {
2482 res = hdr->vtbl->QueryOption(hdr, dwOption, lpBuffer, lpdwBufferLength, FALSE);
2483 WININET_Release(hdr);
2485 }else {
2486 res = INET_QueryOption(NULL, dwOption, lpBuffer, lpdwBufferLength, FALSE);
2489 if(res != ERROR_SUCCESS)
2490 SetLastError(res);
2491 return res == ERROR_SUCCESS;
2495 /***********************************************************************
2496 * InternetSetOptionW (WININET.@)
2498 * Sets an options on the specified handle
2500 * RETURNS
2501 * TRUE on success
2502 * FALSE on failure
2505 BOOL WINAPI InternetSetOptionW(HINTERNET hInternet, DWORD dwOption,
2506 LPVOID lpBuffer, DWORD dwBufferLength)
2508 object_header_t *lpwhh;
2509 BOOL ret = TRUE;
2511 TRACE("(%p %d %p %d)\n", hInternet, dwOption, lpBuffer, dwBufferLength);
2513 lpwhh = (object_header_t*) get_handle_object( hInternet );
2514 if(lpwhh && lpwhh->vtbl->SetOption) {
2515 DWORD res;
2517 res = lpwhh->vtbl->SetOption(lpwhh, dwOption, lpBuffer, dwBufferLength);
2518 if(res != ERROR_INTERNET_INVALID_OPTION) {
2519 WININET_Release( lpwhh );
2521 if(res != ERROR_SUCCESS)
2522 SetLastError(res);
2524 return res == ERROR_SUCCESS;
2528 switch (dwOption)
2530 case INTERNET_OPTION_CALLBACK:
2532 if (!lpwhh)
2534 SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2535 return FALSE;
2537 WININET_Release(lpwhh);
2538 SetLastError(ERROR_INTERNET_OPTION_NOT_SETTABLE);
2539 return FALSE;
2541 case INTERNET_OPTION_HTTP_VERSION:
2543 HTTP_VERSION_INFO* pVersion=(HTTP_VERSION_INFO*)lpBuffer;
2544 FIXME("Option INTERNET_OPTION_HTTP_VERSION(%d,%d): STUB\n",pVersion->dwMajorVersion,pVersion->dwMinorVersion);
2546 break;
2547 case INTERNET_OPTION_ERROR_MASK:
2549 if(!lpwhh) {
2550 SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2551 return FALSE;
2552 } else if(*(ULONG*)lpBuffer & (~(INTERNET_ERROR_MASK_INSERT_CDROM|
2553 INTERNET_ERROR_MASK_COMBINED_SEC_CERT|
2554 INTERNET_ERROR_MASK_LOGIN_FAILURE_DISPLAY_ENTITY_BODY))) {
2555 SetLastError(ERROR_INVALID_PARAMETER);
2556 ret = FALSE;
2557 } else if(dwBufferLength != sizeof(ULONG)) {
2558 SetLastError(ERROR_INTERNET_BAD_OPTION_LENGTH);
2559 ret = FALSE;
2560 } else
2561 lpwhh->ErrorMask = *(ULONG*)lpBuffer;
2563 break;
2564 case INTERNET_OPTION_PROXY:
2566 INTERNET_PROXY_INFOW *info = lpBuffer;
2568 if (!lpBuffer || dwBufferLength < sizeof(INTERNET_PROXY_INFOW))
2570 SetLastError(ERROR_INVALID_PARAMETER);
2571 return FALSE;
2573 if (!hInternet)
2575 EnterCriticalSection( &WININET_cs );
2576 free_global_proxy();
2577 global_proxy = heap_alloc( sizeof(proxyinfo_t) );
2578 if (global_proxy)
2580 if (info->dwAccessType == INTERNET_OPEN_TYPE_PROXY)
2582 global_proxy->proxyEnabled = 1;
2583 global_proxy->proxy = heap_strdupW( info->lpszProxy );
2584 global_proxy->proxyBypass = heap_strdupW( info->lpszProxyBypass );
2586 else
2588 global_proxy->proxyEnabled = 0;
2589 global_proxy->proxy = global_proxy->proxyBypass = NULL;
2592 LeaveCriticalSection( &WININET_cs );
2594 else
2596 /* In general, each type of object should handle
2597 * INTERNET_OPTION_PROXY directly. This FIXME ensures it doesn't
2598 * get silently dropped.
2600 FIXME("INTERNET_OPTION_PROXY unimplemented\n");
2601 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2602 ret = FALSE;
2604 break;
2606 case INTERNET_OPTION_CODEPAGE:
2608 ULONG codepage = *(ULONG *)lpBuffer;
2609 FIXME("Option INTERNET_OPTION_CODEPAGE (%d): STUB\n", codepage);
2611 break;
2612 case INTERNET_OPTION_REQUEST_PRIORITY:
2614 ULONG priority = *(ULONG *)lpBuffer;
2615 FIXME("Option INTERNET_OPTION_REQUEST_PRIORITY (%d): STUB\n", priority);
2617 break;
2618 case INTERNET_OPTION_CONNECT_TIMEOUT:
2620 ULONG connecttimeout = *(ULONG *)lpBuffer;
2621 FIXME("Option INTERNET_OPTION_CONNECT_TIMEOUT (%d): STUB\n", connecttimeout);
2623 break;
2624 case INTERNET_OPTION_DATA_RECEIVE_TIMEOUT:
2626 ULONG receivetimeout = *(ULONG *)lpBuffer;
2627 FIXME("Option INTERNET_OPTION_DATA_RECEIVE_TIMEOUT (%d): STUB\n", receivetimeout);
2629 break;
2630 case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2632 ULONG conns = *(ULONG *)lpBuffer;
2633 FIXME("Option INTERNET_OPTION_MAX_CONNS_PER_SERVER (%d): STUB\n", conns);
2635 break;
2636 case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2638 ULONG conns = *(ULONG *)lpBuffer;
2639 FIXME("Option INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER (%d): STUB\n", conns);
2641 break;
2642 case INTERNET_OPTION_RESET_URLCACHE_SESSION:
2643 FIXME("Option INTERNET_OPTION_RESET_URLCACHE_SESSION: STUB\n");
2644 break;
2645 case INTERNET_OPTION_END_BROWSER_SESSION:
2646 FIXME("Option INTERNET_OPTION_END_BROWSER_SESSION: STUB\n");
2647 break;
2648 case INTERNET_OPTION_CONNECTED_STATE:
2649 FIXME("Option INTERNET_OPTION_CONNECTED_STATE: STUB\n");
2650 break;
2651 case INTERNET_OPTION_DISABLE_PASSPORT_AUTH:
2652 TRACE("Option INTERNET_OPTION_DISABLE_PASSPORT_AUTH: harmless stub, since not enabled\n");
2653 break;
2654 case INTERNET_OPTION_SEND_TIMEOUT:
2655 case INTERNET_OPTION_RECEIVE_TIMEOUT:
2656 case INTERNET_OPTION_DATA_SEND_TIMEOUT:
2658 ULONG timeout = *(ULONG *)lpBuffer;
2659 FIXME("INTERNET_OPTION_SEND/RECEIVE_TIMEOUT/DATA_SEND_TIMEOUT %d\n", timeout);
2660 break;
2662 case INTERNET_OPTION_CONNECT_RETRIES:
2664 ULONG retries = *(ULONG *)lpBuffer;
2665 FIXME("INTERNET_OPTION_CONNECT_RETRIES %d\n", retries);
2666 break;
2668 case INTERNET_OPTION_CONTEXT_VALUE:
2670 if (!lpwhh)
2672 SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2673 return FALSE;
2675 if (!lpBuffer || dwBufferLength != sizeof(DWORD_PTR))
2677 SetLastError(ERROR_INVALID_PARAMETER);
2678 ret = FALSE;
2680 else
2681 lpwhh->dwContext = *(DWORD_PTR *)lpBuffer;
2682 break;
2684 case INTERNET_OPTION_SECURITY_FLAGS:
2685 FIXME("Option INTERNET_OPTION_SECURITY_FLAGS; STUB\n");
2686 break;
2687 case INTERNET_OPTION_DISABLE_AUTODIAL:
2688 FIXME("Option INTERNET_OPTION_DISABLE_AUTODIAL; STUB\n");
2689 break;
2690 case INTERNET_OPTION_HTTP_DECODING:
2691 FIXME("INTERNET_OPTION_HTTP_DECODING; STUB\n");
2692 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2693 ret = FALSE;
2694 break;
2695 case INTERNET_OPTION_COOKIES_3RD_PARTY:
2696 FIXME("INTERNET_OPTION_COOKIES_3RD_PARTY; STUB\n");
2697 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2698 ret = FALSE;
2699 break;
2700 case INTERNET_OPTION_SEND_UTF8_SERVERNAME_TO_PROXY:
2701 FIXME("INTERNET_OPTION_SEND_UTF8_SERVERNAME_TO_PROXY; STUB\n");
2702 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2703 ret = FALSE;
2704 break;
2705 case INTERNET_OPTION_CODEPAGE_PATH:
2706 FIXME("INTERNET_OPTION_CODEPAGE_PATH; STUB\n");
2707 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2708 ret = FALSE;
2709 break;
2710 case INTERNET_OPTION_CODEPAGE_EXTRA:
2711 FIXME("INTERNET_OPTION_CODEPAGE_EXTRA; STUB\n");
2712 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2713 ret = FALSE;
2714 break;
2715 case INTERNET_OPTION_IDN:
2716 FIXME("INTERNET_OPTION_IDN; STUB\n");
2717 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2718 ret = FALSE;
2719 break;
2720 case INTERNET_OPTION_POLICY:
2721 SetLastError(ERROR_INVALID_PARAMETER);
2722 ret = FALSE;
2723 break;
2724 case INTERNET_OPTION_PER_CONNECTION_OPTION: {
2725 INTERNET_PER_CONN_OPTION_LISTW *con = lpBuffer;
2726 LONG res;
2727 int i;
2728 proxyinfo_t pi;
2730 INTERNET_LoadProxySettings(&pi);
2732 for (i = 0; i < con->dwOptionCount; i++) {
2733 INTERNET_PER_CONN_OPTIONW *option = con->pOptions + i;
2735 switch (option->dwOption) {
2736 case INTERNET_PER_CONN_PROXY_SERVER:
2737 heap_free(pi.proxy);
2738 pi.proxy = heap_strdupW(option->Value.pszValue);
2739 break;
2741 case INTERNET_PER_CONN_FLAGS:
2742 if(option->Value.dwValue & PROXY_TYPE_PROXY)
2743 pi.proxyEnabled = 1;
2744 else
2746 if(option->Value.dwValue != PROXY_TYPE_DIRECT)
2747 FIXME("Unhandled flags: 0x%x\n", option->Value.dwValue);
2748 pi.proxyEnabled = 0;
2750 break;
2752 case INTERNET_PER_CONN_AUTOCONFIG_URL:
2753 case INTERNET_PER_CONN_AUTODISCOVERY_FLAGS:
2754 case INTERNET_PER_CONN_AUTOCONFIG_SECONDARY_URL:
2755 case INTERNET_PER_CONN_AUTOCONFIG_RELOAD_DELAY_MINS:
2756 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_TIME:
2757 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_URL:
2758 case INTERNET_PER_CONN_PROXY_BYPASS:
2759 FIXME("Unhandled dwOption %d\n", option->dwOption);
2760 break;
2762 default:
2763 FIXME("Unknown dwOption %d\n", option->dwOption);
2764 SetLastError(ERROR_INVALID_PARAMETER);
2765 break;
2769 if ((res = INTERNET_SaveProxySettings(&pi)))
2770 SetLastError(res);
2772 FreeProxyInfo(&pi);
2774 ret = (res == ERROR_SUCCESS);
2775 break;
2777 default:
2778 FIXME("Option %d STUB\n",dwOption);
2779 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2780 ret = FALSE;
2781 break;
2784 if(lpwhh)
2785 WININET_Release( lpwhh );
2787 return ret;
2791 /***********************************************************************
2792 * InternetSetOptionA (WININET.@)
2794 * Sets an options on the specified handle.
2796 * RETURNS
2797 * TRUE on success
2798 * FALSE on failure
2801 BOOL WINAPI InternetSetOptionA(HINTERNET hInternet, DWORD dwOption,
2802 LPVOID lpBuffer, DWORD dwBufferLength)
2804 LPVOID wbuffer;
2805 DWORD wlen;
2806 BOOL r;
2808 switch( dwOption )
2810 case INTERNET_OPTION_CALLBACK:
2812 object_header_t *lpwh;
2814 if (!(lpwh = get_handle_object(hInternet)))
2816 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2817 return FALSE;
2819 WININET_Release(lpwh);
2820 INTERNET_SetLastError(ERROR_INTERNET_OPTION_NOT_SETTABLE);
2821 return FALSE;
2823 case INTERNET_OPTION_PROXY:
2825 LPINTERNET_PROXY_INFOA pi = (LPINTERNET_PROXY_INFOA) lpBuffer;
2826 LPINTERNET_PROXY_INFOW piw;
2827 DWORD proxlen, prbylen;
2828 LPWSTR prox, prby;
2830 proxlen = MultiByteToWideChar( CP_ACP, 0, pi->lpszProxy, -1, NULL, 0);
2831 prbylen= MultiByteToWideChar( CP_ACP, 0, pi->lpszProxyBypass, -1, NULL, 0);
2832 wlen = sizeof(*piw) + proxlen + prbylen;
2833 wbuffer = heap_alloc(wlen*sizeof(WCHAR) );
2834 piw = (LPINTERNET_PROXY_INFOW) wbuffer;
2835 piw->dwAccessType = pi->dwAccessType;
2836 prox = (LPWSTR) &piw[1];
2837 prby = &prox[proxlen+1];
2838 MultiByteToWideChar( CP_ACP, 0, pi->lpszProxy, -1, prox, proxlen);
2839 MultiByteToWideChar( CP_ACP, 0, pi->lpszProxyBypass, -1, prby, prbylen);
2840 piw->lpszProxy = prox;
2841 piw->lpszProxyBypass = prby;
2843 break;
2844 case INTERNET_OPTION_USER_AGENT:
2845 case INTERNET_OPTION_USERNAME:
2846 case INTERNET_OPTION_PASSWORD:
2847 wlen = MultiByteToWideChar( CP_ACP, 0, lpBuffer, dwBufferLength,
2848 NULL, 0 );
2849 wbuffer = heap_alloc(wlen*sizeof(WCHAR) );
2850 MultiByteToWideChar( CP_ACP, 0, lpBuffer, dwBufferLength,
2851 wbuffer, wlen );
2852 break;
2853 case INTERNET_OPTION_PER_CONNECTION_OPTION: {
2854 int i;
2855 INTERNET_PER_CONN_OPTION_LISTW *listW;
2856 INTERNET_PER_CONN_OPTION_LISTA *listA = lpBuffer;
2857 wlen = sizeof(INTERNET_PER_CONN_OPTION_LISTW);
2858 wbuffer = heap_alloc(wlen);
2859 listW = wbuffer;
2861 listW->dwSize = sizeof(INTERNET_PER_CONN_OPTION_LISTW);
2862 if (listA->pszConnection)
2864 wlen = MultiByteToWideChar( CP_ACP, 0, listA->pszConnection, -1, NULL, 0 );
2865 listW->pszConnection = heap_alloc(wlen*sizeof(WCHAR));
2866 MultiByteToWideChar( CP_ACP, 0, listA->pszConnection, -1, listW->pszConnection, wlen );
2868 else
2869 listW->pszConnection = NULL;
2870 listW->dwOptionCount = listA->dwOptionCount;
2871 listW->dwOptionError = listA->dwOptionError;
2872 listW->pOptions = heap_alloc(sizeof(INTERNET_PER_CONN_OPTIONW) * listA->dwOptionCount);
2874 for (i = 0; i < listA->dwOptionCount; ++i) {
2875 INTERNET_PER_CONN_OPTIONA *optA = listA->pOptions + i;
2876 INTERNET_PER_CONN_OPTIONW *optW = listW->pOptions + i;
2878 optW->dwOption = optA->dwOption;
2880 switch (optA->dwOption) {
2881 case INTERNET_PER_CONN_AUTOCONFIG_URL:
2882 case INTERNET_PER_CONN_PROXY_BYPASS:
2883 case INTERNET_PER_CONN_PROXY_SERVER:
2884 case INTERNET_PER_CONN_AUTOCONFIG_SECONDARY_URL:
2885 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_URL:
2886 if (optA->Value.pszValue)
2888 wlen = MultiByteToWideChar( CP_ACP, 0, optA->Value.pszValue, -1, NULL, 0 );
2889 optW->Value.pszValue = heap_alloc(wlen*sizeof(WCHAR));
2890 MultiByteToWideChar( CP_ACP, 0, optA->Value.pszValue, -1, optW->Value.pszValue, wlen );
2892 else
2893 optW->Value.pszValue = NULL;
2894 break;
2895 case INTERNET_PER_CONN_AUTODISCOVERY_FLAGS:
2896 case INTERNET_PER_CONN_FLAGS:
2897 case INTERNET_PER_CONN_AUTOCONFIG_RELOAD_DELAY_MINS:
2898 optW->Value.dwValue = optA->Value.dwValue;
2899 break;
2900 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_TIME:
2901 optW->Value.ftValue = optA->Value.ftValue;
2902 break;
2903 default:
2904 WARN("Unknown PER_CONN dwOption: %d, guessing at conversion to Wide\n", optA->dwOption);
2905 optW->Value.dwValue = optA->Value.dwValue;
2906 break;
2910 break;
2911 default:
2912 wbuffer = lpBuffer;
2913 wlen = dwBufferLength;
2916 r = InternetSetOptionW(hInternet,dwOption, wbuffer, wlen);
2918 if( lpBuffer != wbuffer )
2920 if (dwOption == INTERNET_OPTION_PER_CONNECTION_OPTION)
2922 INTERNET_PER_CONN_OPTION_LISTW *list = wbuffer;
2923 int i;
2924 for (i = 0; i < list->dwOptionCount; ++i) {
2925 INTERNET_PER_CONN_OPTIONW *opt = list->pOptions + i;
2926 switch (opt->dwOption) {
2927 case INTERNET_PER_CONN_AUTOCONFIG_URL:
2928 case INTERNET_PER_CONN_PROXY_BYPASS:
2929 case INTERNET_PER_CONN_PROXY_SERVER:
2930 case INTERNET_PER_CONN_AUTOCONFIG_SECONDARY_URL:
2931 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_URL:
2932 heap_free( opt->Value.pszValue );
2933 break;
2934 default:
2935 break;
2938 heap_free( list->pOptions );
2940 heap_free( wbuffer );
2943 return r;
2947 /***********************************************************************
2948 * InternetSetOptionExA (WININET.@)
2950 BOOL WINAPI InternetSetOptionExA(HINTERNET hInternet, DWORD dwOption,
2951 LPVOID lpBuffer, DWORD dwBufferLength, DWORD dwFlags)
2953 FIXME("Flags %08x ignored\n", dwFlags);
2954 return InternetSetOptionA( hInternet, dwOption, lpBuffer, dwBufferLength );
2957 /***********************************************************************
2958 * InternetSetOptionExW (WININET.@)
2960 BOOL WINAPI InternetSetOptionExW(HINTERNET hInternet, DWORD dwOption,
2961 LPVOID lpBuffer, DWORD dwBufferLength, DWORD dwFlags)
2963 FIXME("Flags %08x ignored\n", dwFlags);
2964 if( dwFlags & ~ISO_VALID_FLAGS )
2966 SetLastError( ERROR_INVALID_PARAMETER );
2967 return FALSE;
2969 return InternetSetOptionW( hInternet, dwOption, lpBuffer, dwBufferLength );
2972 static const WCHAR WININET_wkday[7][4] =
2973 { { 'S','u','n', 0 }, { 'M','o','n', 0 }, { 'T','u','e', 0 }, { 'W','e','d', 0 },
2974 { 'T','h','u', 0 }, { 'F','r','i', 0 }, { 'S','a','t', 0 } };
2975 static const WCHAR WININET_month[12][4] =
2976 { { 'J','a','n', 0 }, { 'F','e','b', 0 }, { 'M','a','r', 0 }, { 'A','p','r', 0 },
2977 { 'M','a','y', 0 }, { 'J','u','n', 0 }, { 'J','u','l', 0 }, { 'A','u','g', 0 },
2978 { 'S','e','p', 0 }, { 'O','c','t', 0 }, { 'N','o','v', 0 }, { 'D','e','c', 0 } };
2980 /***********************************************************************
2981 * InternetTimeFromSystemTimeA (WININET.@)
2983 BOOL WINAPI InternetTimeFromSystemTimeA( const SYSTEMTIME* time, DWORD format, LPSTR string, DWORD size )
2985 BOOL ret;
2986 WCHAR stringW[INTERNET_RFC1123_BUFSIZE];
2988 TRACE( "%p 0x%08x %p 0x%08x\n", time, format, string, size );
2990 if (!time || !string || format != INTERNET_RFC1123_FORMAT)
2992 SetLastError(ERROR_INVALID_PARAMETER);
2993 return FALSE;
2996 if (size < INTERNET_RFC1123_BUFSIZE * sizeof(*string))
2998 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2999 return FALSE;
3002 ret = InternetTimeFromSystemTimeW( time, format, stringW, sizeof(stringW) );
3003 if (ret) WideCharToMultiByte( CP_ACP, 0, stringW, -1, string, size, NULL, NULL );
3005 return ret;
3008 /***********************************************************************
3009 * InternetTimeFromSystemTimeW (WININET.@)
3011 BOOL WINAPI InternetTimeFromSystemTimeW( const SYSTEMTIME* time, DWORD format, LPWSTR string, DWORD size )
3013 static const WCHAR date[] =
3014 { '%','s',',',' ','%','0','2','d',' ','%','s',' ','%','4','d',' ','%','0',
3015 '2','d',':','%','0','2','d',':','%','0','2','d',' ','G','M','T', 0 };
3017 TRACE( "%p 0x%08x %p 0x%08x\n", time, format, string, size );
3019 if (!time || !string || format != INTERNET_RFC1123_FORMAT)
3021 SetLastError(ERROR_INVALID_PARAMETER);
3022 return FALSE;
3025 if (size < INTERNET_RFC1123_BUFSIZE * sizeof(*string))
3027 SetLastError(ERROR_INSUFFICIENT_BUFFER);
3028 return FALSE;
3031 sprintfW( string, date,
3032 WININET_wkday[time->wDayOfWeek],
3033 time->wDay,
3034 WININET_month[time->wMonth - 1],
3035 time->wYear,
3036 time->wHour,
3037 time->wMinute,
3038 time->wSecond );
3040 return TRUE;
3043 /***********************************************************************
3044 * InternetTimeToSystemTimeA (WININET.@)
3046 BOOL WINAPI InternetTimeToSystemTimeA( LPCSTR string, SYSTEMTIME* time, DWORD reserved )
3048 BOOL ret = FALSE;
3049 WCHAR *stringW;
3051 TRACE( "%s %p 0x%08x\n", debugstr_a(string), time, reserved );
3053 stringW = heap_strdupAtoW(string);
3054 if (stringW)
3056 ret = InternetTimeToSystemTimeW( stringW, time, reserved );
3057 heap_free( stringW );
3059 return ret;
3062 /***********************************************************************
3063 * InternetTimeToSystemTimeW (WININET.@)
3065 BOOL WINAPI InternetTimeToSystemTimeW( LPCWSTR string, SYSTEMTIME* time, DWORD reserved )
3067 unsigned int i;
3068 const WCHAR *s = string;
3069 WCHAR *end;
3071 TRACE( "%s %p 0x%08x\n", debugstr_w(string), time, reserved );
3073 if (!string || !time) return FALSE;
3075 /* Windows does this too */
3076 GetSystemTime( time );
3078 /* Convert an RFC1123 time such as 'Fri, 07 Jan 2005 12:06:35 GMT' into
3079 * a SYSTEMTIME structure.
3082 while (*s && !isalphaW( *s )) s++;
3083 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
3084 time->wDayOfWeek = 7;
3086 for (i = 0; i < 7; i++)
3088 if (toupperW( WININET_wkday[i][0] ) == toupperW( s[0] ) &&
3089 toupperW( WININET_wkday[i][1] ) == toupperW( s[1] ) &&
3090 toupperW( WININET_wkday[i][2] ) == toupperW( s[2] ) )
3092 time->wDayOfWeek = i;
3093 break;
3097 if (time->wDayOfWeek > 6) return TRUE;
3098 while (*s && !isdigitW( *s )) s++;
3099 time->wDay = strtolW( s, &end, 10 );
3100 s = end;
3102 while (*s && !isalphaW( *s )) s++;
3103 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
3104 time->wMonth = 0;
3106 for (i = 0; i < 12; i++)
3108 if (toupperW( WININET_month[i][0]) == toupperW( s[0] ) &&
3109 toupperW( WININET_month[i][1]) == toupperW( s[1] ) &&
3110 toupperW( WININET_month[i][2]) == toupperW( s[2] ) )
3112 time->wMonth = i + 1;
3113 break;
3116 if (time->wMonth == 0) return TRUE;
3118 while (*s && !isdigitW( *s )) s++;
3119 if (*s == '\0') return TRUE;
3120 time->wYear = strtolW( s, &end, 10 );
3121 s = end;
3123 while (*s && !isdigitW( *s )) s++;
3124 if (*s == '\0') return TRUE;
3125 time->wHour = strtolW( s, &end, 10 );
3126 s = end;
3128 while (*s && !isdigitW( *s )) s++;
3129 if (*s == '\0') return TRUE;
3130 time->wMinute = strtolW( s, &end, 10 );
3131 s = end;
3133 while (*s && !isdigitW( *s )) s++;
3134 if (*s == '\0') return TRUE;
3135 time->wSecond = strtolW( s, &end, 10 );
3136 s = end;
3138 time->wMilliseconds = 0;
3139 return TRUE;
3142 /***********************************************************************
3143 * InternetCheckConnectionW (WININET.@)
3145 * Pings a requested host to check internet connection
3147 * RETURNS
3148 * TRUE on success and FALSE on failure. If a failure then
3149 * ERROR_NOT_CONNECTED is placed into GetLastError
3152 BOOL WINAPI InternetCheckConnectionW( LPCWSTR lpszUrl, DWORD dwFlags, DWORD dwReserved )
3155 * this is a kludge which runs the resident ping program and reads the output.
3157 * Anyone have a better idea?
3160 BOOL rc = FALSE;
3161 static const CHAR ping[] = "ping -c 1 ";
3162 static const CHAR redirect[] = " >/dev/null 2>/dev/null";
3163 CHAR *command = NULL;
3164 WCHAR hostW[1024];
3165 DWORD len;
3166 INTERNET_PORT port;
3167 int status = -1;
3169 FIXME("\n");
3172 * Crack or set the Address
3174 if (lpszUrl == NULL)
3177 * According to the doc we are supposed to use the ip for the next
3178 * server in the WnInet internal server database. I have
3179 * no idea what that is or how to get it.
3181 * So someone needs to implement this.
3183 FIXME("Unimplemented with URL of NULL\n");
3184 return TRUE;
3186 else
3188 URL_COMPONENTSW components;
3190 ZeroMemory(&components,sizeof(URL_COMPONENTSW));
3191 components.lpszHostName = (LPWSTR)hostW;
3192 components.dwHostNameLength = 1024;
3194 if (!InternetCrackUrlW(lpszUrl,0,0,&components))
3195 goto End;
3197 TRACE("host name : %s\n",debugstr_w(components.lpszHostName));
3198 port = components.nPort;
3199 TRACE("port: %d\n", port);
3202 if (dwFlags & FLAG_ICC_FORCE_CONNECTION)
3204 struct sockaddr_storage saddr;
3205 socklen_t sa_len = sizeof(saddr);
3206 int fd;
3208 if (!GetAddress(hostW, port, (struct sockaddr *)&saddr, &sa_len))
3209 goto End;
3210 fd = socket(saddr.ss_family, SOCK_STREAM, 0);
3211 if (fd != -1)
3213 if (connect(fd, (struct sockaddr *)&saddr, sa_len) == 0)
3214 rc = TRUE;
3215 close(fd);
3218 else
3221 * Build our ping command
3223 len = WideCharToMultiByte(CP_UNIXCP, 0, hostW, -1, NULL, 0, NULL, NULL);
3224 command = heap_alloc(strlen(ping)+len+strlen(redirect));
3225 strcpy(command,ping);
3226 WideCharToMultiByte(CP_UNIXCP, 0, hostW, -1, command+strlen(ping), len, NULL, NULL);
3227 strcat(command,redirect);
3229 TRACE("Ping command is : %s\n",command);
3231 status = system(command);
3233 TRACE("Ping returned a code of %i\n",status);
3235 /* Ping return code of 0 indicates success */
3236 if (status == 0)
3237 rc = TRUE;
3240 End:
3241 heap_free( command );
3242 if (rc == FALSE)
3243 INTERNET_SetLastError(ERROR_NOT_CONNECTED);
3245 return rc;
3249 /***********************************************************************
3250 * InternetCheckConnectionA (WININET.@)
3252 * Pings a requested host to check internet connection
3254 * RETURNS
3255 * TRUE on success and FALSE on failure. If a failure then
3256 * ERROR_NOT_CONNECTED is placed into GetLastError
3259 BOOL WINAPI InternetCheckConnectionA(LPCSTR lpszUrl, DWORD dwFlags, DWORD dwReserved)
3261 WCHAR *url = NULL;
3262 BOOL rc;
3264 if(lpszUrl) {
3265 url = heap_strdupAtoW(lpszUrl);
3266 if(!url)
3267 return FALSE;
3270 rc = InternetCheckConnectionW(url, dwFlags, dwReserved);
3272 heap_free(url);
3273 return rc;
3277 /**********************************************************
3278 * INTERNET_InternetOpenUrlW (internal)
3280 * Opens an URL
3282 * RETURNS
3283 * handle of connection or NULL on failure
3285 static HINTERNET INTERNET_InternetOpenUrlW(appinfo_t *hIC, LPCWSTR lpszUrl,
3286 LPCWSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext)
3288 URL_COMPONENTSW urlComponents;
3289 WCHAR protocol[32], hostName[MAXHOSTNAME], userName[1024];
3290 WCHAR password[1024], path[2048], extra[1024];
3291 HINTERNET client = NULL, client1 = NULL;
3292 DWORD res;
3294 TRACE("(%p, %s, %s, %08x, %08x, %08lx)\n", hIC, debugstr_w(lpszUrl), debugstr_w(lpszHeaders),
3295 dwHeadersLength, dwFlags, dwContext);
3297 urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
3298 urlComponents.lpszScheme = protocol;
3299 urlComponents.dwSchemeLength = 32;
3300 urlComponents.lpszHostName = hostName;
3301 urlComponents.dwHostNameLength = MAXHOSTNAME;
3302 urlComponents.lpszUserName = userName;
3303 urlComponents.dwUserNameLength = 1024;
3304 urlComponents.lpszPassword = password;
3305 urlComponents.dwPasswordLength = 1024;
3306 urlComponents.lpszUrlPath = path;
3307 urlComponents.dwUrlPathLength = 2048;
3308 urlComponents.lpszExtraInfo = extra;
3309 urlComponents.dwExtraInfoLength = 1024;
3310 if(!InternetCrackUrlW(lpszUrl, strlenW(lpszUrl), 0, &urlComponents))
3311 return NULL;
3312 switch(urlComponents.nScheme) {
3313 case INTERNET_SCHEME_FTP:
3314 if(urlComponents.nPort == 0)
3315 urlComponents.nPort = INTERNET_DEFAULT_FTP_PORT;
3316 client = FTP_Connect(hIC, hostName, urlComponents.nPort,
3317 userName, password, dwFlags, dwContext, INET_OPENURL);
3318 if(client == NULL)
3319 break;
3320 client1 = FtpOpenFileW(client, path, GENERIC_READ, dwFlags, dwContext);
3321 if(client1 == NULL) {
3322 InternetCloseHandle(client);
3323 break;
3325 break;
3327 case INTERNET_SCHEME_HTTP:
3328 case INTERNET_SCHEME_HTTPS: {
3329 static const WCHAR szStars[] = { '*','/','*', 0 };
3330 LPCWSTR accept[2] = { szStars, NULL };
3331 if(urlComponents.nPort == 0) {
3332 if(urlComponents.nScheme == INTERNET_SCHEME_HTTP)
3333 urlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
3334 else
3335 urlComponents.nPort = INTERNET_DEFAULT_HTTPS_PORT;
3337 if (urlComponents.nScheme == INTERNET_SCHEME_HTTPS) dwFlags |= INTERNET_FLAG_SECURE;
3339 /* FIXME: should use pointers, not handles, as handles are not thread-safe */
3340 res = HTTP_Connect(hIC, hostName, urlComponents.nPort,
3341 userName, password, dwFlags, dwContext, INET_OPENURL, &client);
3342 if(res != ERROR_SUCCESS) {
3343 INTERNET_SetLastError(res);
3344 break;
3347 if (urlComponents.dwExtraInfoLength) {
3348 WCHAR *path_extra;
3349 DWORD len = urlComponents.dwUrlPathLength + urlComponents.dwExtraInfoLength + 1;
3351 if (!(path_extra = heap_alloc(len * sizeof(WCHAR))))
3353 InternetCloseHandle(client);
3354 break;
3356 strcpyW(path_extra, urlComponents.lpszUrlPath);
3357 strcatW(path_extra, urlComponents.lpszExtraInfo);
3358 client1 = HttpOpenRequestW(client, NULL, path_extra, NULL, NULL, accept, dwFlags, dwContext);
3359 heap_free(path_extra);
3361 else
3362 client1 = HttpOpenRequestW(client, NULL, path, NULL, NULL, accept, dwFlags, dwContext);
3364 if(client1 == NULL) {
3365 InternetCloseHandle(client);
3366 break;
3368 HttpAddRequestHeadersW(client1, lpszHeaders, dwHeadersLength, HTTP_ADDREQ_FLAG_ADD);
3369 if (!HttpSendRequestW(client1, NULL, 0, NULL, 0) &&
3370 GetLastError() != ERROR_IO_PENDING) {
3371 InternetCloseHandle(client1);
3372 client1 = NULL;
3373 break;
3376 case INTERNET_SCHEME_GOPHER:
3377 /* gopher doesn't seem to be implemented in wine, but it's supposed
3378 * to be supported by InternetOpenUrlA. */
3379 default:
3380 SetLastError(ERROR_INTERNET_UNRECOGNIZED_SCHEME);
3381 break;
3384 TRACE(" %p <--\n", client1);
3386 return client1;
3389 /**********************************************************
3390 * InternetOpenUrlW (WININET.@)
3392 * Opens an URL
3394 * RETURNS
3395 * handle of connection or NULL on failure
3397 static void AsyncInternetOpenUrlProc(WORKREQUEST *workRequest)
3399 struct WORKREQ_INTERNETOPENURLW const *req = &workRequest->u.InternetOpenUrlW;
3400 appinfo_t *hIC = (appinfo_t*) workRequest->hdr;
3402 TRACE("%p\n", hIC);
3404 INTERNET_InternetOpenUrlW(hIC, req->lpszUrl,
3405 req->lpszHeaders, req->dwHeadersLength, req->dwFlags, req->dwContext);
3406 heap_free(req->lpszUrl);
3407 heap_free(req->lpszHeaders);
3410 HINTERNET WINAPI InternetOpenUrlW(HINTERNET hInternet, LPCWSTR lpszUrl,
3411 LPCWSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext)
3413 HINTERNET ret = NULL;
3414 appinfo_t *hIC = NULL;
3416 if (TRACE_ON(wininet)) {
3417 TRACE("(%p, %s, %s, %08x, %08x, %08lx)\n", hInternet, debugstr_w(lpszUrl), debugstr_w(lpszHeaders),
3418 dwHeadersLength, dwFlags, dwContext);
3419 TRACE(" flags :");
3420 dump_INTERNET_FLAGS(dwFlags);
3423 if (!lpszUrl)
3425 SetLastError(ERROR_INVALID_PARAMETER);
3426 goto lend;
3429 hIC = (appinfo_t*)get_handle_object( hInternet );
3430 if (NULL == hIC || hIC->hdr.htype != WH_HINIT) {
3431 SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
3432 goto lend;
3435 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC) {
3436 WORKREQUEST workRequest;
3437 struct WORKREQ_INTERNETOPENURLW *req;
3439 workRequest.asyncproc = AsyncInternetOpenUrlProc;
3440 workRequest.hdr = WININET_AddRef( &hIC->hdr );
3441 req = &workRequest.u.InternetOpenUrlW;
3442 req->lpszUrl = heap_strdupW(lpszUrl);
3443 req->lpszHeaders = heap_strdupW(lpszHeaders);
3444 req->dwHeadersLength = dwHeadersLength;
3445 req->dwFlags = dwFlags;
3446 req->dwContext = dwContext;
3448 INTERNET_AsyncCall(&workRequest);
3450 * This is from windows.
3452 SetLastError(ERROR_IO_PENDING);
3453 } else {
3454 ret = INTERNET_InternetOpenUrlW(hIC, lpszUrl, lpszHeaders, dwHeadersLength, dwFlags, dwContext);
3457 lend:
3458 if( hIC )
3459 WININET_Release( &hIC->hdr );
3460 TRACE(" %p <--\n", ret);
3462 return ret;
3465 /**********************************************************
3466 * InternetOpenUrlA (WININET.@)
3468 * Opens an URL
3470 * RETURNS
3471 * handle of connection or NULL on failure
3473 HINTERNET WINAPI InternetOpenUrlA(HINTERNET hInternet, LPCSTR lpszUrl,
3474 LPCSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext)
3476 HINTERNET rc = NULL;
3477 DWORD lenHeaders = 0;
3478 LPWSTR szUrl = NULL;
3479 LPWSTR szHeaders = NULL;
3481 TRACE("\n");
3483 if(lpszUrl) {
3484 szUrl = heap_strdupAtoW(lpszUrl);
3485 if(!szUrl)
3486 return NULL;
3489 if(lpszHeaders) {
3490 lenHeaders = MultiByteToWideChar(CP_ACP, 0, lpszHeaders, dwHeadersLength, NULL, 0 );
3491 szHeaders = heap_alloc(lenHeaders*sizeof(WCHAR));
3492 if(!szHeaders) {
3493 heap_free(szUrl);
3494 return NULL;
3496 MultiByteToWideChar(CP_ACP, 0, lpszHeaders, dwHeadersLength, szHeaders, lenHeaders);
3499 rc = InternetOpenUrlW(hInternet, szUrl, szHeaders,
3500 lenHeaders, dwFlags, dwContext);
3502 heap_free(szUrl);
3503 heap_free(szHeaders);
3504 return rc;
3508 static LPWITHREADERROR INTERNET_AllocThreadError(void)
3510 LPWITHREADERROR lpwite = heap_alloc(sizeof(*lpwite));
3512 if (lpwite)
3514 lpwite->dwError = 0;
3515 lpwite->response[0] = '\0';
3518 if (!TlsSetValue(g_dwTlsErrIndex, lpwite))
3520 heap_free(lpwite);
3521 return NULL;
3523 return lpwite;
3527 /***********************************************************************
3528 * INTERNET_SetLastError (internal)
3530 * Set last thread specific error
3532 * RETURNS
3535 void INTERNET_SetLastError(DWORD dwError)
3537 LPWITHREADERROR lpwite = TlsGetValue(g_dwTlsErrIndex);
3539 if (!lpwite)
3540 lpwite = INTERNET_AllocThreadError();
3542 SetLastError(dwError);
3543 if(lpwite)
3544 lpwite->dwError = dwError;
3548 /***********************************************************************
3549 * INTERNET_GetLastError (internal)
3551 * Get last thread specific error
3553 * RETURNS
3556 DWORD INTERNET_GetLastError(void)
3558 LPWITHREADERROR lpwite = TlsGetValue(g_dwTlsErrIndex);
3559 if (!lpwite) return 0;
3560 /* TlsGetValue clears last error, so set it again here */
3561 SetLastError(lpwite->dwError);
3562 return lpwite->dwError;
3566 /***********************************************************************
3567 * INTERNET_WorkerThreadFunc (internal)
3569 * Worker thread execution function
3571 * RETURNS
3574 static DWORD CALLBACK INTERNET_WorkerThreadFunc(LPVOID lpvParam)
3576 LPWORKREQUEST lpRequest = lpvParam;
3577 WORKREQUEST workRequest;
3579 TRACE("\n");
3581 workRequest = *lpRequest;
3582 heap_free(lpRequest);
3584 workRequest.asyncproc(&workRequest);
3585 WININET_Release( workRequest.hdr );
3587 if (g_dwTlsErrIndex != TLS_OUT_OF_INDEXES)
3589 heap_free(TlsGetValue(g_dwTlsErrIndex));
3590 TlsSetValue(g_dwTlsErrIndex, NULL);
3592 return TRUE;
3596 /***********************************************************************
3597 * INTERNET_AsyncCall (internal)
3599 * Retrieves work request from queue
3601 * RETURNS
3604 DWORD INTERNET_AsyncCall(LPWORKREQUEST lpWorkRequest)
3606 BOOL bSuccess;
3607 LPWORKREQUEST lpNewRequest;
3609 TRACE("\n");
3611 lpNewRequest = heap_alloc(sizeof(WORKREQUEST));
3612 if (!lpNewRequest)
3613 return ERROR_OUTOFMEMORY;
3615 *lpNewRequest = *lpWorkRequest;
3617 bSuccess = QueueUserWorkItem(INTERNET_WorkerThreadFunc, lpNewRequest, WT_EXECUTELONGFUNCTION);
3618 if (!bSuccess)
3620 heap_free(lpNewRequest);
3621 return ERROR_INTERNET_ASYNC_THREAD_FAILED;
3623 return ERROR_SUCCESS;
3627 /***********************************************************************
3628 * INTERNET_GetResponseBuffer (internal)
3630 * RETURNS
3633 LPSTR INTERNET_GetResponseBuffer(void)
3635 LPWITHREADERROR lpwite = TlsGetValue(g_dwTlsErrIndex);
3636 if (!lpwite)
3637 lpwite = INTERNET_AllocThreadError();
3638 TRACE("\n");
3639 return lpwite->response;
3642 /***********************************************************************
3643 * INTERNET_GetNextLine (internal)
3645 * Parse next line in directory string listing
3647 * RETURNS
3648 * Pointer to beginning of next line
3649 * NULL on failure
3653 LPSTR INTERNET_GetNextLine(INT nSocket, LPDWORD dwLen)
3655 struct pollfd pfd;
3656 BOOL bSuccess = FALSE;
3657 INT nRecv = 0;
3658 LPSTR lpszBuffer = INTERNET_GetResponseBuffer();
3660 TRACE("\n");
3662 pfd.fd = nSocket;
3663 pfd.events = POLLIN;
3665 while (nRecv < MAX_REPLY_LEN)
3667 if (poll(&pfd,1, RESPONSE_TIMEOUT * 1000) > 0)
3669 if (recv(nSocket, &lpszBuffer[nRecv], 1, 0) <= 0)
3671 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
3672 goto lend;
3675 if (lpszBuffer[nRecv] == '\n')
3677 bSuccess = TRUE;
3678 break;
3680 if (lpszBuffer[nRecv] != '\r')
3681 nRecv++;
3683 else
3685 INTERNET_SetLastError(ERROR_INTERNET_TIMEOUT);
3686 goto lend;
3690 lend:
3691 if (bSuccess)
3693 lpszBuffer[nRecv] = '\0';
3694 *dwLen = nRecv - 1;
3695 TRACE(":%d %s\n", nRecv, lpszBuffer);
3696 return lpszBuffer;
3698 else
3700 return NULL;
3704 /**********************************************************
3705 * InternetQueryDataAvailable (WININET.@)
3707 * Determines how much data is available to be read.
3709 * RETURNS
3710 * TRUE on success, FALSE if an error occurred. If
3711 * INTERNET_FLAG_ASYNC was specified in InternetOpen, and
3712 * no data is presently available, FALSE is returned with
3713 * the last error ERROR_IO_PENDING; a callback with status
3714 * INTERNET_STATUS_REQUEST_COMPLETE will be sent when more
3715 * data is available.
3717 BOOL WINAPI InternetQueryDataAvailable( HINTERNET hFile,
3718 LPDWORD lpdwNumberOfBytesAvailble,
3719 DWORD dwFlags, DWORD_PTR dwContext)
3721 object_header_t *hdr;
3722 DWORD res;
3724 TRACE("(%p %p %x %lx)\n", hFile, lpdwNumberOfBytesAvailble, dwFlags, dwContext);
3726 hdr = get_handle_object( hFile );
3727 if (!hdr) {
3728 SetLastError(ERROR_INVALID_HANDLE);
3729 return FALSE;
3732 if(hdr->vtbl->QueryDataAvailable) {
3733 res = hdr->vtbl->QueryDataAvailable(hdr, lpdwNumberOfBytesAvailble, dwFlags, dwContext);
3734 }else {
3735 WARN("wrong handle\n");
3736 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
3739 WININET_Release(hdr);
3741 if(res != ERROR_SUCCESS)
3742 SetLastError(res);
3743 return res == ERROR_SUCCESS;
3747 /***********************************************************************
3748 * InternetLockRequestFile (WININET.@)
3750 BOOL WINAPI InternetLockRequestFile( HINTERNET hInternet, HANDLE
3751 *lphLockReqHandle)
3753 FIXME("STUB\n");
3754 return FALSE;
3757 BOOL WINAPI InternetUnlockRequestFile( HANDLE hLockHandle)
3759 FIXME("STUB\n");
3760 return FALSE;
3764 /***********************************************************************
3765 * InternetAutodial (WININET.@)
3767 * On windows this function is supposed to dial the default internet
3768 * connection. We don't want to have Wine dial out to the internet so
3769 * we return TRUE by default. It might be nice to check if we are connected.
3771 * RETURNS
3772 * TRUE on success
3773 * FALSE on failure
3776 BOOL WINAPI InternetAutodial(DWORD dwFlags, HWND hwndParent)
3778 FIXME("STUB\n");
3780 /* Tell that we are connected to the internet. */
3781 return TRUE;
3784 /***********************************************************************
3785 * InternetAutodialHangup (WININET.@)
3787 * Hangs up a connection made with InternetAutodial
3789 * PARAM
3790 * dwReserved
3791 * RETURNS
3792 * TRUE on success
3793 * FALSE on failure
3796 BOOL WINAPI InternetAutodialHangup(DWORD dwReserved)
3798 FIXME("STUB\n");
3800 /* we didn't dial, we don't disconnect */
3801 return TRUE;
3804 /***********************************************************************
3805 * InternetCombineUrlA (WININET.@)
3807 * Combine a base URL with a relative URL
3809 * RETURNS
3810 * TRUE on success
3811 * FALSE on failure
3815 BOOL WINAPI InternetCombineUrlA(LPCSTR lpszBaseUrl, LPCSTR lpszRelativeUrl,
3816 LPSTR lpszBuffer, LPDWORD lpdwBufferLength,
3817 DWORD dwFlags)
3819 HRESULT hr=S_OK;
3821 TRACE("(%s, %s, %p, %p, 0x%08x)\n", debugstr_a(lpszBaseUrl), debugstr_a(lpszRelativeUrl), lpszBuffer, lpdwBufferLength, dwFlags);
3823 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
3824 dwFlags ^= ICU_NO_ENCODE;
3825 hr=UrlCombineA(lpszBaseUrl,lpszRelativeUrl,lpszBuffer,lpdwBufferLength,dwFlags);
3827 return (hr==S_OK);
3830 /***********************************************************************
3831 * InternetCombineUrlW (WININET.@)
3833 * Combine a base URL with a relative URL
3835 * RETURNS
3836 * TRUE on success
3837 * FALSE on failure
3841 BOOL WINAPI InternetCombineUrlW(LPCWSTR lpszBaseUrl, LPCWSTR lpszRelativeUrl,
3842 LPWSTR lpszBuffer, LPDWORD lpdwBufferLength,
3843 DWORD dwFlags)
3845 HRESULT hr=S_OK;
3847 TRACE("(%s, %s, %p, %p, 0x%08x)\n", debugstr_w(lpszBaseUrl), debugstr_w(lpszRelativeUrl), lpszBuffer, lpdwBufferLength, dwFlags);
3849 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
3850 dwFlags ^= ICU_NO_ENCODE;
3851 hr=UrlCombineW(lpszBaseUrl,lpszRelativeUrl,lpszBuffer,lpdwBufferLength,dwFlags);
3853 return (hr==S_OK);
3856 /* max port num is 65535 => 5 digits */
3857 #define MAX_WORD_DIGITS 5
3859 #define URL_GET_COMP_LENGTH(url, component) ((url)->dw##component##Length ? \
3860 (url)->dw##component##Length : strlenW((url)->lpsz##component))
3861 #define URL_GET_COMP_LENGTHA(url, component) ((url)->dw##component##Length ? \
3862 (url)->dw##component##Length : strlen((url)->lpsz##component))
3864 static BOOL url_uses_default_port(INTERNET_SCHEME nScheme, INTERNET_PORT nPort)
3866 if ((nScheme == INTERNET_SCHEME_HTTP) &&
3867 (nPort == INTERNET_DEFAULT_HTTP_PORT))
3868 return TRUE;
3869 if ((nScheme == INTERNET_SCHEME_HTTPS) &&
3870 (nPort == INTERNET_DEFAULT_HTTPS_PORT))
3871 return TRUE;
3872 if ((nScheme == INTERNET_SCHEME_FTP) &&
3873 (nPort == INTERNET_DEFAULT_FTP_PORT))
3874 return TRUE;
3875 if ((nScheme == INTERNET_SCHEME_GOPHER) &&
3876 (nPort == INTERNET_DEFAULT_GOPHER_PORT))
3877 return TRUE;
3879 if (nPort == INTERNET_INVALID_PORT_NUMBER)
3880 return TRUE;
3882 return FALSE;
3885 /* opaque urls do not fit into the standard url hierarchy and don't have
3886 * two following slashes */
3887 static inline BOOL scheme_is_opaque(INTERNET_SCHEME nScheme)
3889 return (nScheme != INTERNET_SCHEME_FTP) &&
3890 (nScheme != INTERNET_SCHEME_GOPHER) &&
3891 (nScheme != INTERNET_SCHEME_HTTP) &&
3892 (nScheme != INTERNET_SCHEME_HTTPS) &&
3893 (nScheme != INTERNET_SCHEME_FILE);
3896 static LPCWSTR INTERNET_GetSchemeString(INTERNET_SCHEME scheme)
3898 int index;
3899 if (scheme < INTERNET_SCHEME_FIRST)
3900 return NULL;
3901 index = scheme - INTERNET_SCHEME_FIRST;
3902 if (index >= sizeof(url_schemes)/sizeof(url_schemes[0]))
3903 return NULL;
3904 return (LPCWSTR)url_schemes[index];
3907 /* we can calculate using ansi strings because we're just
3908 * calculating string length, not size
3910 static BOOL calc_url_length(LPURL_COMPONENTSW lpUrlComponents,
3911 LPDWORD lpdwUrlLength)
3913 INTERNET_SCHEME nScheme;
3915 *lpdwUrlLength = 0;
3917 if (lpUrlComponents->lpszScheme)
3919 DWORD dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, Scheme);
3920 *lpdwUrlLength += dwLen;
3921 nScheme = GetInternetSchemeW(lpUrlComponents->lpszScheme, dwLen);
3923 else
3925 LPCWSTR scheme;
3927 nScheme = lpUrlComponents->nScheme;
3929 if (nScheme == INTERNET_SCHEME_DEFAULT)
3930 nScheme = INTERNET_SCHEME_HTTP;
3931 scheme = INTERNET_GetSchemeString(nScheme);
3932 *lpdwUrlLength += strlenW(scheme);
3935 (*lpdwUrlLength)++; /* ':' */
3936 if (!scheme_is_opaque(nScheme) || lpUrlComponents->lpszHostName)
3937 *lpdwUrlLength += strlen("//");
3939 if (lpUrlComponents->lpszUserName)
3941 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, UserName);
3942 *lpdwUrlLength += strlen("@");
3944 else
3946 if (lpUrlComponents->lpszPassword)
3948 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
3949 return FALSE;
3953 if (lpUrlComponents->lpszPassword)
3955 *lpdwUrlLength += strlen(":");
3956 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, Password);
3959 if (lpUrlComponents->lpszHostName)
3961 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, HostName);
3963 if (!url_uses_default_port(nScheme, lpUrlComponents->nPort))
3965 char szPort[MAX_WORD_DIGITS+1];
3967 sprintf(szPort, "%d", lpUrlComponents->nPort);
3968 *lpdwUrlLength += strlen(szPort);
3969 *lpdwUrlLength += strlen(":");
3972 if (lpUrlComponents->lpszUrlPath && *lpUrlComponents->lpszUrlPath != '/')
3973 (*lpdwUrlLength)++; /* '/' */
3976 if (lpUrlComponents->lpszUrlPath)
3977 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, UrlPath);
3979 if (lpUrlComponents->lpszExtraInfo)
3980 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, ExtraInfo);
3982 return TRUE;
3985 static void convert_urlcomp_atow(LPURL_COMPONENTSA lpUrlComponents, LPURL_COMPONENTSW urlCompW)
3987 INT len;
3989 ZeroMemory(urlCompW, sizeof(URL_COMPONENTSW));
3991 urlCompW->dwStructSize = sizeof(URL_COMPONENTSW);
3992 urlCompW->dwSchemeLength = lpUrlComponents->dwSchemeLength;
3993 urlCompW->nScheme = lpUrlComponents->nScheme;
3994 urlCompW->dwHostNameLength = lpUrlComponents->dwHostNameLength;
3995 urlCompW->nPort = lpUrlComponents->nPort;
3996 urlCompW->dwUserNameLength = lpUrlComponents->dwUserNameLength;
3997 urlCompW->dwPasswordLength = lpUrlComponents->dwPasswordLength;
3998 urlCompW->dwUrlPathLength = lpUrlComponents->dwUrlPathLength;
3999 urlCompW->dwExtraInfoLength = lpUrlComponents->dwExtraInfoLength;
4001 if (lpUrlComponents->lpszScheme)
4003 len = URL_GET_COMP_LENGTHA(lpUrlComponents, Scheme) + 1;
4004 urlCompW->lpszScheme = heap_alloc(len * sizeof(WCHAR));
4005 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszScheme,
4006 -1, urlCompW->lpszScheme, len);
4009 if (lpUrlComponents->lpszHostName)
4011 len = URL_GET_COMP_LENGTHA(lpUrlComponents, HostName) + 1;
4012 urlCompW->lpszHostName = heap_alloc(len * sizeof(WCHAR));
4013 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszHostName,
4014 -1, urlCompW->lpszHostName, len);
4017 if (lpUrlComponents->lpszUserName)
4019 len = URL_GET_COMP_LENGTHA(lpUrlComponents, UserName) + 1;
4020 urlCompW->lpszUserName = heap_alloc(len * sizeof(WCHAR));
4021 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszUserName,
4022 -1, urlCompW->lpszUserName, len);
4025 if (lpUrlComponents->lpszPassword)
4027 len = URL_GET_COMP_LENGTHA(lpUrlComponents, Password) + 1;
4028 urlCompW->lpszPassword = heap_alloc(len * sizeof(WCHAR));
4029 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszPassword,
4030 -1, urlCompW->lpszPassword, len);
4033 if (lpUrlComponents->lpszUrlPath)
4035 len = URL_GET_COMP_LENGTHA(lpUrlComponents, UrlPath) + 1;
4036 urlCompW->lpszUrlPath = heap_alloc(len * sizeof(WCHAR));
4037 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszUrlPath,
4038 -1, urlCompW->lpszUrlPath, len);
4041 if (lpUrlComponents->lpszExtraInfo)
4043 len = URL_GET_COMP_LENGTHA(lpUrlComponents, ExtraInfo) + 1;
4044 urlCompW->lpszExtraInfo = heap_alloc(len * sizeof(WCHAR));
4045 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszExtraInfo,
4046 -1, urlCompW->lpszExtraInfo, len);
4050 /***********************************************************************
4051 * InternetCreateUrlA (WININET.@)
4053 * See InternetCreateUrlW.
4055 BOOL WINAPI InternetCreateUrlA(LPURL_COMPONENTSA lpUrlComponents, DWORD dwFlags,
4056 LPSTR lpszUrl, LPDWORD lpdwUrlLength)
4058 BOOL ret;
4059 LPWSTR urlW = NULL;
4060 URL_COMPONENTSW urlCompW;
4062 TRACE("(%p,%d,%p,%p)\n", lpUrlComponents, dwFlags, lpszUrl, lpdwUrlLength);
4064 if (!lpUrlComponents || lpUrlComponents->dwStructSize != sizeof(URL_COMPONENTSW) || !lpdwUrlLength)
4066 SetLastError(ERROR_INVALID_PARAMETER);
4067 return FALSE;
4070 convert_urlcomp_atow(lpUrlComponents, &urlCompW);
4072 if (lpszUrl)
4073 urlW = heap_alloc(*lpdwUrlLength * sizeof(WCHAR));
4075 ret = InternetCreateUrlW(&urlCompW, dwFlags, urlW, lpdwUrlLength);
4077 if (!ret && (GetLastError() == ERROR_INSUFFICIENT_BUFFER))
4078 *lpdwUrlLength /= sizeof(WCHAR);
4080 /* on success, lpdwUrlLength points to the size of urlW in WCHARS
4081 * minus one, so add one to leave room for NULL terminator
4083 if (ret)
4084 WideCharToMultiByte(CP_ACP, 0, urlW, -1, lpszUrl, *lpdwUrlLength + 1, NULL, NULL);
4086 heap_free(urlCompW.lpszScheme);
4087 heap_free(urlCompW.lpszHostName);
4088 heap_free(urlCompW.lpszUserName);
4089 heap_free(urlCompW.lpszPassword);
4090 heap_free(urlCompW.lpszUrlPath);
4091 heap_free(urlCompW.lpszExtraInfo);
4092 heap_free(urlW);
4093 return ret;
4096 /***********************************************************************
4097 * InternetCreateUrlW (WININET.@)
4099 * Creates a URL from its component parts.
4101 * PARAMS
4102 * lpUrlComponents [I] URL Components.
4103 * dwFlags [I] Flags. See notes.
4104 * lpszUrl [I] Buffer in which to store the created URL.
4105 * lpdwUrlLength [I/O] On input, the length of the buffer pointed to by
4106 * lpszUrl in characters. On output, the number of bytes
4107 * required to store the URL including terminator.
4109 * NOTES
4111 * The dwFlags parameter can be zero or more of the following:
4112 *|ICU_ESCAPE - Generates escape sequences for unsafe characters in the path and extra info of the URL.
4114 * RETURNS
4115 * TRUE on success
4116 * FALSE on failure
4119 BOOL WINAPI InternetCreateUrlW(LPURL_COMPONENTSW lpUrlComponents, DWORD dwFlags,
4120 LPWSTR lpszUrl, LPDWORD lpdwUrlLength)
4122 DWORD dwLen;
4123 INTERNET_SCHEME nScheme;
4125 static const WCHAR slashSlashW[] = {'/','/'};
4126 static const WCHAR fmtW[] = {'%','u',0};
4128 TRACE("(%p,%d,%p,%p)\n", lpUrlComponents, dwFlags, lpszUrl, lpdwUrlLength);
4130 if (!lpUrlComponents || lpUrlComponents->dwStructSize != sizeof(URL_COMPONENTSW) || !lpdwUrlLength)
4132 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
4133 return FALSE;
4136 if (!calc_url_length(lpUrlComponents, &dwLen))
4137 return FALSE;
4139 if (!lpszUrl || *lpdwUrlLength < dwLen)
4141 *lpdwUrlLength = (dwLen + 1) * sizeof(WCHAR);
4142 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
4143 return FALSE;
4146 *lpdwUrlLength = dwLen;
4147 lpszUrl[0] = 0x00;
4149 dwLen = 0;
4151 if (lpUrlComponents->lpszScheme)
4153 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, Scheme);
4154 memcpy(lpszUrl, lpUrlComponents->lpszScheme, dwLen * sizeof(WCHAR));
4155 lpszUrl += dwLen;
4157 nScheme = GetInternetSchemeW(lpUrlComponents->lpszScheme, dwLen);
4159 else
4161 LPCWSTR scheme;
4162 nScheme = lpUrlComponents->nScheme;
4164 if (nScheme == INTERNET_SCHEME_DEFAULT)
4165 nScheme = INTERNET_SCHEME_HTTP;
4167 scheme = INTERNET_GetSchemeString(nScheme);
4168 dwLen = strlenW(scheme);
4169 memcpy(lpszUrl, scheme, dwLen * sizeof(WCHAR));
4170 lpszUrl += dwLen;
4173 /* all schemes are followed by at least a colon */
4174 *lpszUrl = ':';
4175 lpszUrl++;
4177 if (!scheme_is_opaque(nScheme) || lpUrlComponents->lpszHostName)
4179 memcpy(lpszUrl, slashSlashW, sizeof(slashSlashW));
4180 lpszUrl += sizeof(slashSlashW)/sizeof(slashSlashW[0]);
4183 if (lpUrlComponents->lpszUserName)
4185 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, UserName);
4186 memcpy(lpszUrl, lpUrlComponents->lpszUserName, dwLen * sizeof(WCHAR));
4187 lpszUrl += dwLen;
4189 if (lpUrlComponents->lpszPassword)
4191 *lpszUrl = ':';
4192 lpszUrl++;
4194 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, Password);
4195 memcpy(lpszUrl, lpUrlComponents->lpszPassword, dwLen * sizeof(WCHAR));
4196 lpszUrl += dwLen;
4199 *lpszUrl = '@';
4200 lpszUrl++;
4203 if (lpUrlComponents->lpszHostName)
4205 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, HostName);
4206 memcpy(lpszUrl, lpUrlComponents->lpszHostName, dwLen * sizeof(WCHAR));
4207 lpszUrl += dwLen;
4209 if (!url_uses_default_port(nScheme, lpUrlComponents->nPort))
4211 WCHAR szPort[MAX_WORD_DIGITS+1];
4213 sprintfW(szPort, fmtW, lpUrlComponents->nPort);
4214 *lpszUrl = ':';
4215 lpszUrl++;
4216 dwLen = strlenW(szPort);
4217 memcpy(lpszUrl, szPort, dwLen * sizeof(WCHAR));
4218 lpszUrl += dwLen;
4221 /* add slash between hostname and path if necessary */
4222 if (lpUrlComponents->lpszUrlPath && *lpUrlComponents->lpszUrlPath != '/')
4224 *lpszUrl = '/';
4225 lpszUrl++;
4229 if (lpUrlComponents->lpszUrlPath)
4231 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, UrlPath);
4232 memcpy(lpszUrl, lpUrlComponents->lpszUrlPath, dwLen * sizeof(WCHAR));
4233 lpszUrl += dwLen;
4236 if (lpUrlComponents->lpszExtraInfo)
4238 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, ExtraInfo);
4239 memcpy(lpszUrl, lpUrlComponents->lpszExtraInfo, dwLen * sizeof(WCHAR));
4240 lpszUrl += dwLen;
4243 *lpszUrl = '\0';
4245 return TRUE;
4248 /***********************************************************************
4249 * InternetConfirmZoneCrossingA (WININET.@)
4252 DWORD WINAPI InternetConfirmZoneCrossingA( HWND hWnd, LPSTR szUrlPrev, LPSTR szUrlNew, BOOL bPost )
4254 FIXME("(%p, %s, %s, %x) stub\n", hWnd, debugstr_a(szUrlPrev), debugstr_a(szUrlNew), bPost);
4255 return ERROR_SUCCESS;
4258 /***********************************************************************
4259 * InternetConfirmZoneCrossingW (WININET.@)
4262 DWORD WINAPI InternetConfirmZoneCrossingW( HWND hWnd, LPWSTR szUrlPrev, LPWSTR szUrlNew, BOOL bPost )
4264 FIXME("(%p, %s, %s, %x) stub\n", hWnd, debugstr_w(szUrlPrev), debugstr_w(szUrlNew), bPost);
4265 return ERROR_SUCCESS;
4268 static DWORD zone_preference = 3;
4270 /***********************************************************************
4271 * PrivacySetZonePreferenceW (WININET.@)
4273 DWORD WINAPI PrivacySetZonePreferenceW( DWORD zone, DWORD type, DWORD template, LPCWSTR preference )
4275 FIXME( "%x %x %x %s: stub\n", zone, type, template, debugstr_w(preference) );
4277 zone_preference = template;
4278 return 0;
4281 /***********************************************************************
4282 * PrivacyGetZonePreferenceW (WININET.@)
4284 DWORD WINAPI PrivacyGetZonePreferenceW( DWORD zone, DWORD type, LPDWORD template,
4285 LPWSTR preference, LPDWORD length )
4287 FIXME( "%x %x %p %p %p: stub\n", zone, type, template, preference, length );
4289 if (template) *template = zone_preference;
4290 return 0;
4293 DWORD WINAPI InternetDialA( HWND hwndParent, LPSTR lpszConnectoid, DWORD dwFlags,
4294 DWORD_PTR* lpdwConnection, DWORD dwReserved )
4296 FIXME("(%p, %p, 0x%08x, %p, 0x%08x) stub\n", hwndParent, lpszConnectoid, dwFlags,
4297 lpdwConnection, dwReserved);
4298 return ERROR_SUCCESS;
4301 DWORD WINAPI InternetDialW( HWND hwndParent, LPWSTR lpszConnectoid, DWORD dwFlags,
4302 DWORD_PTR* lpdwConnection, DWORD dwReserved )
4304 FIXME("(%p, %p, 0x%08x, %p, 0x%08x) stub\n", hwndParent, lpszConnectoid, dwFlags,
4305 lpdwConnection, dwReserved);
4306 return ERROR_SUCCESS;
4309 BOOL WINAPI InternetGoOnlineA( LPSTR lpszURL, HWND hwndParent, DWORD dwReserved )
4311 FIXME("(%s, %p, 0x%08x) stub\n", debugstr_a(lpszURL), hwndParent, dwReserved);
4312 return TRUE;
4315 BOOL WINAPI InternetGoOnlineW( LPWSTR lpszURL, HWND hwndParent, DWORD dwReserved )
4317 FIXME("(%s, %p, 0x%08x) stub\n", debugstr_w(lpszURL), hwndParent, dwReserved);
4318 return TRUE;
4321 DWORD WINAPI InternetHangUp( DWORD_PTR dwConnection, DWORD dwReserved )
4323 FIXME("(0x%08lx, 0x%08x) stub\n", dwConnection, dwReserved);
4324 return ERROR_SUCCESS;
4327 BOOL WINAPI CreateMD5SSOHash( PWSTR pszChallengeInfo, PWSTR pwszRealm, PWSTR pwszTarget,
4328 PBYTE pbHexHash )
4330 FIXME("(%s, %s, %s, %p) stub\n", debugstr_w(pszChallengeInfo), debugstr_w(pwszRealm),
4331 debugstr_w(pwszTarget), pbHexHash);
4332 return FALSE;
4335 BOOL WINAPI ResumeSuspendedDownload( HINTERNET hInternet, DWORD dwError )
4337 FIXME("(%p, 0x%08x) stub\n", hInternet, dwError);
4338 return FALSE;
4341 BOOL WINAPI InternetQueryFortezzaStatus(DWORD *a, DWORD_PTR b)
4343 FIXME("(%p, %08lx) stub\n", a, b);
4344 return 0;