wininet: Added INTERNET_OPTION_MAX_CONNS_PER_SERVER and INTERNET_OPTION_MAX_CONNS_PER...
[wine/multimedia.git] / dlls / wininet / internet.c
bloba40d5667622568ec8421e810f5ad4c39743bda2b
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 #if defined(__MINGW32__) || defined (_MSC_VER)
33 #include <ws2tcpip.h>
34 #endif
36 #include <string.h>
37 #include <stdarg.h>
38 #include <stdio.h>
39 #include <sys/types.h>
40 #ifdef HAVE_SYS_SOCKET_H
41 # include <sys/socket.h>
42 #endif
43 #ifdef HAVE_POLL_H
44 #include <poll.h>
45 #endif
46 #ifdef HAVE_SYS_POLL_H
47 # include <sys/poll.h>
48 #endif
49 #ifdef HAVE_SYS_TIME_H
50 # include <sys/time.h>
51 #endif
52 #include <stdlib.h>
53 #include <ctype.h>
54 #ifdef HAVE_UNISTD_H
55 # include <unistd.h>
56 #endif
57 #include <assert.h>
59 #include "windef.h"
60 #include "winbase.h"
61 #include "winreg.h"
62 #include "winuser.h"
63 #include "wininet.h"
64 #include "winineti.h"
65 #include "winnls.h"
66 #include "wine/debug.h"
67 #include "winerror.h"
68 #define NO_SHLWAPI_STREAM
69 #include "shlwapi.h"
71 #include "wine/exception.h"
73 #include "internet.h"
74 #include "resource.h"
76 #include "wine/unicode.h"
78 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
80 #define RESPONSE_TIMEOUT 30
82 typedef struct
84 DWORD dwError;
85 CHAR response[MAX_REPLY_LEN];
86 } WITHREADERROR, *LPWITHREADERROR;
88 static DWORD g_dwTlsErrIndex = TLS_OUT_OF_INDEXES;
89 HMODULE WININET_hModule;
91 static CRITICAL_SECTION WININET_cs;
92 static CRITICAL_SECTION_DEBUG WININET_cs_debug =
94 0, 0, &WININET_cs,
95 { &WININET_cs_debug.ProcessLocksList, &WININET_cs_debug.ProcessLocksList },
96 0, 0, { (DWORD_PTR)(__FILE__ ": WININET_cs") }
98 static CRITICAL_SECTION WININET_cs = { &WININET_cs_debug, -1, 0, 0, 0, 0 };
100 static object_header_t **handle_table;
101 static UINT_PTR next_handle;
102 static UINT_PTR handle_table_size;
104 typedef struct
106 DWORD proxyEnabled;
107 LPWSTR proxy;
108 LPWSTR proxyBypass;
109 } proxyinfo_t;
111 static ULONG max_conns = 2, max_1_0_conns = 4;
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;
298 break;
300 case DLL_THREAD_ATTACH:
301 break;
303 case DLL_THREAD_DETACH:
304 if (g_dwTlsErrIndex != TLS_OUT_OF_INDEXES)
306 heap_free(TlsGetValue(g_dwTlsErrIndex));
308 break;
310 case DLL_PROCESS_DETACH:
311 collect_connections(TRUE);
312 NETCON_unload();
313 URLCacheContainers_DeleteAll();
315 if (g_dwTlsErrIndex != TLS_OUT_OF_INDEXES)
317 heap_free(TlsGetValue(g_dwTlsErrIndex));
318 TlsFree(g_dwTlsErrIndex);
320 break;
322 return TRUE;
325 /***********************************************************************
326 * INTERNET_SaveProxySettings
328 * Stores the proxy settings given by lpwai into the registry
330 * RETURNS
331 * ERROR_SUCCESS if no error, or error code on fail
333 static LONG INTERNET_SaveProxySettings( proxyinfo_t *lpwpi )
335 HKEY key;
336 LONG ret;
338 if ((ret = RegOpenKeyW( HKEY_CURRENT_USER, szInternetSettings, &key )))
339 return ret;
341 if ((ret = RegSetValueExW( key, szProxyEnable, 0, REG_DWORD, (BYTE*)&lpwpi->proxyEnabled, sizeof(DWORD))))
343 RegCloseKey( key );
344 return ret;
347 if (lpwpi->proxy)
349 if ((ret = RegSetValueExW( key, szProxyServer, 0, REG_SZ, (BYTE*)lpwpi->proxy, sizeof(WCHAR) * (lstrlenW(lpwpi->proxy) + 1))))
351 RegCloseKey( key );
352 return ret;
355 else
357 if ((ret = RegDeleteValueW( key, szProxyServer )))
359 RegCloseKey( key );
360 return ret;
364 RegCloseKey(key);
365 return ERROR_SUCCESS;
368 /***********************************************************************
369 * INTERNET_FindProxyForProtocol
371 * Searches the proxy string for a proxy of the given protocol.
372 * Returns the found proxy, or the default proxy if none of the given
373 * protocol is found.
375 * PARAMETERS
376 * szProxy [In] proxy string to search
377 * proto [In] protocol to search for, e.g. "http"
378 * foundProxy [Out] found proxy
379 * foundProxyLen [In/Out] length of foundProxy buffer, in WCHARs
381 * RETURNS
382 * TRUE if a proxy is found, FALSE if not. If foundProxy is too short,
383 * *foundProxyLen is set to the required size in WCHARs, including the
384 * NULL terminator, and the last error is set to ERROR_INSUFFICIENT_BUFFER.
386 BOOL INTERNET_FindProxyForProtocol(LPCWSTR szProxy, LPCWSTR proto, WCHAR *foundProxy, DWORD *foundProxyLen)
388 LPCWSTR ptr;
389 BOOL ret = FALSE;
391 TRACE("(%s, %s)\n", debugstr_w(szProxy), debugstr_w(proto));
393 /* First, look for the specified protocol (proto=scheme://host:port) */
394 for (ptr = szProxy; !ret && ptr && *ptr; )
396 LPCWSTR end, equal;
398 if (!(end = strchrW(ptr, ' ')))
399 end = ptr + strlenW(ptr);
400 if ((equal = strchrW(ptr, '=')) && equal < end &&
401 equal - ptr == strlenW(proto) &&
402 !strncmpiW(proto, ptr, strlenW(proto)))
404 if (end - equal > *foundProxyLen)
406 WARN("buffer too short for %s\n",
407 debugstr_wn(equal + 1, end - equal - 1));
408 *foundProxyLen = end - equal;
409 SetLastError(ERROR_INSUFFICIENT_BUFFER);
411 else
413 memcpy(foundProxy, equal + 1, (end - equal) * sizeof(WCHAR));
414 foundProxy[end - equal] = 0;
415 ret = TRUE;
418 if (*end == ' ')
419 ptr = end + 1;
420 else
421 ptr = end;
423 if (!ret)
425 /* It wasn't found: look for no protocol */
426 for (ptr = szProxy; !ret && ptr && *ptr; )
428 LPCWSTR end, equal;
430 if (!(end = strchrW(ptr, ' ')))
431 end = ptr + strlenW(ptr);
432 if (!(equal = strchrW(ptr, '=')))
434 if (end - ptr + 1 > *foundProxyLen)
436 WARN("buffer too short for %s\n",
437 debugstr_wn(ptr, end - ptr));
438 *foundProxyLen = end - ptr + 1;
439 SetLastError(ERROR_INSUFFICIENT_BUFFER);
441 else
443 memcpy(foundProxy, ptr, (end - ptr) * sizeof(WCHAR));
444 foundProxy[end - ptr] = 0;
445 ret = TRUE;
448 if (*end == ' ')
449 ptr = end + 1;
450 else
451 ptr = end;
454 if (ret)
455 TRACE("found proxy for %s: %s\n", debugstr_w(proto),
456 debugstr_w(foundProxy));
457 return ret;
460 /***********************************************************************
461 * InternetInitializeAutoProxyDll (WININET.@)
463 * Setup the internal proxy
465 * PARAMETERS
466 * dwReserved
468 * RETURNS
469 * FALSE on failure
472 BOOL WINAPI InternetInitializeAutoProxyDll(DWORD dwReserved)
474 FIXME("STUB\n");
475 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
476 return FALSE;
479 /***********************************************************************
480 * DetectAutoProxyUrl (WININET.@)
482 * Auto detect the proxy url
484 * RETURNS
485 * FALSE on failure
488 BOOL WINAPI DetectAutoProxyUrl(LPSTR lpszAutoProxyUrl,
489 DWORD dwAutoProxyUrlLength, DWORD dwDetectFlags)
491 FIXME("STUB\n");
492 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
493 return FALSE;
496 static void FreeProxyInfo( proxyinfo_t *lpwpi )
498 heap_free(lpwpi->proxy);
499 heap_free(lpwpi->proxyBypass);
502 static proxyinfo_t *global_proxy;
504 static void free_global_proxy( void )
506 EnterCriticalSection( &WININET_cs );
507 if (global_proxy)
509 FreeProxyInfo( global_proxy );
510 heap_free( global_proxy );
512 LeaveCriticalSection( &WININET_cs );
515 /***********************************************************************
516 * INTERNET_LoadProxySettings
518 * Loads proxy information from process-wide global settings, the registry,
519 * or the environment into lpwpi.
521 * The caller should call FreeProxyInfo when done with lpwpi.
523 * FIXME:
524 * The proxy may be specified in the form 'http=proxy.my.org'
525 * Presumably that means there can be ftp=ftpproxy.my.org too.
527 static LONG INTERNET_LoadProxySettings( proxyinfo_t *lpwpi )
529 HKEY key;
530 DWORD type, len;
531 LPCSTR envproxy;
532 LONG ret;
534 EnterCriticalSection( &WININET_cs );
535 if (global_proxy)
537 lpwpi->proxyEnabled = global_proxy->proxyEnabled;
538 lpwpi->proxy = heap_strdupW( global_proxy->proxy );
539 lpwpi->proxyBypass = heap_strdupW( global_proxy->proxyBypass );
541 LeaveCriticalSection( &WININET_cs );
543 if ((ret = RegOpenKeyW( HKEY_CURRENT_USER, szInternetSettings, &key )))
544 return ret;
546 len = sizeof(DWORD);
547 if (RegQueryValueExW( key, szProxyEnable, NULL, &type, (BYTE *)&lpwpi->proxyEnabled, &len ) || type != REG_DWORD)
549 lpwpi->proxyEnabled = 0;
550 if((ret = RegSetValueExW( key, szProxyEnable, 0, REG_DWORD, (BYTE *)&lpwpi->proxyEnabled, sizeof(DWORD) )))
552 RegCloseKey( key );
553 return ret;
557 if (!(envproxy = getenv( "http_proxy" )) || lpwpi->proxyEnabled)
559 TRACE("Proxy is enabled.\n");
561 /* figure out how much memory the proxy setting takes */
562 if (!RegQueryValueExW( key, szProxyServer, NULL, &type, NULL, &len ) && len && (type == REG_SZ))
564 LPWSTR szProxy, p;
565 static const WCHAR szHttp[] = {'h','t','t','p','=',0};
567 if (!(szProxy = heap_alloc(len)))
569 RegCloseKey( key );
570 return ERROR_OUTOFMEMORY;
572 RegQueryValueExW( key, szProxyServer, NULL, &type, (BYTE*)szProxy, &len );
574 /* find the http proxy, and strip away everything else */
575 p = strstrW( szProxy, szHttp );
576 if (p)
578 p += lstrlenW( szHttp );
579 lstrcpyW( szProxy, p );
581 p = strchrW( szProxy, ' ' );
582 if (p) *p = 0;
584 lpwpi->proxy = szProxy;
586 TRACE("http proxy = %s\n", debugstr_w(lpwpi->proxy));
588 else
590 TRACE("No proxy server settings in registry.\n");
591 lpwpi->proxy = NULL;
594 else if (envproxy)
596 WCHAR *envproxyW;
598 len = MultiByteToWideChar( CP_UNIXCP, 0, envproxy, -1, NULL, 0 );
599 if (!(envproxyW = heap_alloc(len * sizeof(WCHAR))))
600 return ERROR_OUTOFMEMORY;
601 MultiByteToWideChar( CP_UNIXCP, 0, envproxy, -1, envproxyW, len );
603 lpwpi->proxyEnabled = 1;
604 lpwpi->proxy = envproxyW;
606 TRACE("http proxy (from environment) = %s\n", debugstr_w(lpwpi->proxy));
608 RegCloseKey( key );
610 lpwpi->proxyBypass = NULL;
612 return ERROR_SUCCESS;
615 /***********************************************************************
616 * INTERNET_ConfigureProxy
618 static BOOL INTERNET_ConfigureProxy( appinfo_t *lpwai )
620 proxyinfo_t wpi;
622 if (INTERNET_LoadProxySettings( &wpi ))
623 return FALSE;
625 if (wpi.proxyEnabled)
627 WCHAR proxyurl[INTERNET_MAX_URL_LENGTH];
628 WCHAR username[INTERNET_MAX_USER_NAME_LENGTH];
629 WCHAR password[INTERNET_MAX_PASSWORD_LENGTH];
630 WCHAR hostname[INTERNET_MAX_HOST_NAME_LENGTH];
631 URL_COMPONENTSW UrlComponents;
633 UrlComponents.dwStructSize = sizeof UrlComponents;
634 UrlComponents.dwSchemeLength = 0;
635 UrlComponents.lpszHostName = hostname;
636 UrlComponents.dwHostNameLength = INTERNET_MAX_HOST_NAME_LENGTH;
637 UrlComponents.lpszUserName = username;
638 UrlComponents.dwUserNameLength = INTERNET_MAX_USER_NAME_LENGTH;
639 UrlComponents.lpszPassword = password;
640 UrlComponents.dwPasswordLength = INTERNET_MAX_PASSWORD_LENGTH;
641 UrlComponents.dwUrlPathLength = 0;
642 UrlComponents.dwExtraInfoLength = 0;
644 if(InternetCrackUrlW(wpi.proxy, 0, 0, &UrlComponents))
646 static const WCHAR szFormat[] = { 'h','t','t','p',':','/','/','%','s',':','%','u',0 };
648 if(UrlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
649 UrlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
650 sprintfW(proxyurl, szFormat, hostname, UrlComponents.nPort);
652 lpwai->accessType = INTERNET_OPEN_TYPE_PROXY;
653 lpwai->proxy = heap_strdupW(proxyurl);
654 if (UrlComponents.dwUserNameLength)
656 lpwai->proxyUsername = heap_strdupW(UrlComponents.lpszUserName);
657 lpwai->proxyPassword = heap_strdupW(UrlComponents.lpszPassword);
660 TRACE("http proxy = %s\n", debugstr_w(lpwai->proxy));
661 return TRUE;
663 else
665 TRACE("Failed to parse proxy: %s\n", debugstr_w(wpi.proxy));
666 lpwai->proxy = NULL;
670 lpwai->accessType = INTERNET_OPEN_TYPE_DIRECT;
671 return FALSE;
674 /***********************************************************************
675 * dump_INTERNET_FLAGS
677 * Helper function to TRACE the internet flags.
679 * RETURNS
680 * None
683 static void dump_INTERNET_FLAGS(DWORD dwFlags)
685 #define FE(x) { x, #x }
686 static const wininet_flag_info flag[] = {
687 FE(INTERNET_FLAG_RELOAD),
688 FE(INTERNET_FLAG_RAW_DATA),
689 FE(INTERNET_FLAG_EXISTING_CONNECT),
690 FE(INTERNET_FLAG_ASYNC),
691 FE(INTERNET_FLAG_PASSIVE),
692 FE(INTERNET_FLAG_NO_CACHE_WRITE),
693 FE(INTERNET_FLAG_MAKE_PERSISTENT),
694 FE(INTERNET_FLAG_FROM_CACHE),
695 FE(INTERNET_FLAG_SECURE),
696 FE(INTERNET_FLAG_KEEP_CONNECTION),
697 FE(INTERNET_FLAG_NO_AUTO_REDIRECT),
698 FE(INTERNET_FLAG_READ_PREFETCH),
699 FE(INTERNET_FLAG_NO_COOKIES),
700 FE(INTERNET_FLAG_NO_AUTH),
701 FE(INTERNET_FLAG_CACHE_IF_NET_FAIL),
702 FE(INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP),
703 FE(INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTPS),
704 FE(INTERNET_FLAG_IGNORE_CERT_DATE_INVALID),
705 FE(INTERNET_FLAG_IGNORE_CERT_CN_INVALID),
706 FE(INTERNET_FLAG_RESYNCHRONIZE),
707 FE(INTERNET_FLAG_HYPERLINK),
708 FE(INTERNET_FLAG_NO_UI),
709 FE(INTERNET_FLAG_PRAGMA_NOCACHE),
710 FE(INTERNET_FLAG_CACHE_ASYNC),
711 FE(INTERNET_FLAG_FORMS_SUBMIT),
712 FE(INTERNET_FLAG_NEED_FILE),
713 FE(INTERNET_FLAG_TRANSFER_ASCII),
714 FE(INTERNET_FLAG_TRANSFER_BINARY)
716 #undef FE
717 unsigned int i;
719 for (i = 0; i < (sizeof(flag) / sizeof(flag[0])); i++) {
720 if (flag[i].val & dwFlags) {
721 TRACE(" %s", flag[i].name);
722 dwFlags &= ~flag[i].val;
725 if (dwFlags)
726 TRACE(" Unknown flags (%08x)\n", dwFlags);
727 else
728 TRACE("\n");
731 /***********************************************************************
732 * INTERNET_CloseHandle (internal)
734 * Close internet handle
737 static VOID APPINFO_Destroy(object_header_t *hdr)
739 appinfo_t *lpwai = (appinfo_t*)hdr;
741 TRACE("%p\n",lpwai);
743 heap_free(lpwai->agent);
744 heap_free(lpwai->proxy);
745 heap_free(lpwai->proxyBypass);
746 heap_free(lpwai->proxyUsername);
747 heap_free(lpwai->proxyPassword);
750 static DWORD APPINFO_QueryOption(object_header_t *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
752 appinfo_t *ai = (appinfo_t*)hdr;
754 switch(option) {
755 case INTERNET_OPTION_HANDLE_TYPE:
756 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
758 if (*size < sizeof(ULONG))
759 return ERROR_INSUFFICIENT_BUFFER;
761 *size = sizeof(DWORD);
762 *(DWORD*)buffer = INTERNET_HANDLE_TYPE_INTERNET;
763 return ERROR_SUCCESS;
765 case INTERNET_OPTION_USER_AGENT: {
766 DWORD bufsize;
768 TRACE("INTERNET_OPTION_USER_AGENT\n");
770 bufsize = *size;
772 if (unicode) {
773 DWORD len = ai->agent ? strlenW(ai->agent) : 0;
775 *size = (len + 1) * sizeof(WCHAR);
776 if(!buffer || bufsize < *size)
777 return ERROR_INSUFFICIENT_BUFFER;
779 if (ai->agent)
780 strcpyW(buffer, ai->agent);
781 else
782 *(WCHAR *)buffer = 0;
783 /* If the buffer is copied, the returned length doesn't include
784 * the NULL terminator.
786 *size = len * sizeof(WCHAR);
787 }else {
788 if (ai->agent)
789 *size = WideCharToMultiByte(CP_ACP, 0, ai->agent, -1, NULL, 0, NULL, NULL);
790 else
791 *size = 1;
792 if(!buffer || bufsize < *size)
793 return ERROR_INSUFFICIENT_BUFFER;
795 if (ai->agent)
796 WideCharToMultiByte(CP_ACP, 0, ai->agent, -1, buffer, *size, NULL, NULL);
797 else
798 *(char *)buffer = 0;
799 /* If the buffer is copied, the returned length doesn't include
800 * the NULL terminator.
802 *size -= 1;
805 return ERROR_SUCCESS;
808 case INTERNET_OPTION_PROXY:
809 if(!size) return ERROR_INVALID_PARAMETER;
810 if (unicode) {
811 INTERNET_PROXY_INFOW *pi = (INTERNET_PROXY_INFOW *)buffer;
812 DWORD proxyBytesRequired = 0, proxyBypassBytesRequired = 0;
813 LPWSTR proxy, proxy_bypass;
815 if (ai->proxy)
816 proxyBytesRequired = (lstrlenW(ai->proxy) + 1) * sizeof(WCHAR);
817 if (ai->proxyBypass)
818 proxyBypassBytesRequired = (lstrlenW(ai->proxyBypass) + 1) * sizeof(WCHAR);
819 if (*size < sizeof(INTERNET_PROXY_INFOW) + proxyBytesRequired + proxyBypassBytesRequired)
821 *size = sizeof(INTERNET_PROXY_INFOW) + proxyBytesRequired + proxyBypassBytesRequired;
822 return ERROR_INSUFFICIENT_BUFFER;
824 proxy = (LPWSTR)((LPBYTE)buffer + sizeof(INTERNET_PROXY_INFOW));
825 proxy_bypass = (LPWSTR)((LPBYTE)buffer + sizeof(INTERNET_PROXY_INFOW) + proxyBytesRequired);
827 pi->dwAccessType = ai->accessType;
828 pi->lpszProxy = NULL;
829 pi->lpszProxyBypass = NULL;
830 if (ai->proxy) {
831 lstrcpyW(proxy, ai->proxy);
832 pi->lpszProxy = proxy;
835 if (ai->proxyBypass) {
836 lstrcpyW(proxy_bypass, ai->proxyBypass);
837 pi->lpszProxyBypass = proxy_bypass;
840 *size = sizeof(INTERNET_PROXY_INFOW) + proxyBytesRequired + proxyBypassBytesRequired;
841 return ERROR_SUCCESS;
842 }else {
843 INTERNET_PROXY_INFOA *pi = (INTERNET_PROXY_INFOA *)buffer;
844 DWORD proxyBytesRequired = 0, proxyBypassBytesRequired = 0;
845 LPSTR proxy, proxy_bypass;
847 if (ai->proxy)
848 proxyBytesRequired = WideCharToMultiByte(CP_ACP, 0, ai->proxy, -1, NULL, 0, NULL, NULL);
849 if (ai->proxyBypass)
850 proxyBypassBytesRequired = WideCharToMultiByte(CP_ACP, 0, ai->proxyBypass, -1,
851 NULL, 0, NULL, NULL);
852 if (*size < sizeof(INTERNET_PROXY_INFOA) + proxyBytesRequired + proxyBypassBytesRequired)
854 *size = sizeof(INTERNET_PROXY_INFOA) + proxyBytesRequired + proxyBypassBytesRequired;
855 return ERROR_INSUFFICIENT_BUFFER;
857 proxy = (LPSTR)((LPBYTE)buffer + sizeof(INTERNET_PROXY_INFOA));
858 proxy_bypass = (LPSTR)((LPBYTE)buffer + sizeof(INTERNET_PROXY_INFOA) + proxyBytesRequired);
860 pi->dwAccessType = ai->accessType;
861 pi->lpszProxy = NULL;
862 pi->lpszProxyBypass = NULL;
863 if (ai->proxy) {
864 WideCharToMultiByte(CP_ACP, 0, ai->proxy, -1, proxy, proxyBytesRequired, NULL, NULL);
865 pi->lpszProxy = proxy;
868 if (ai->proxyBypass) {
869 WideCharToMultiByte(CP_ACP, 0, ai->proxyBypass, -1, proxy_bypass,
870 proxyBypassBytesRequired, NULL, NULL);
871 pi->lpszProxyBypass = proxy_bypass;
874 *size = sizeof(INTERNET_PROXY_INFOA) + proxyBytesRequired + proxyBypassBytesRequired;
875 return ERROR_SUCCESS;
879 return INET_QueryOption(hdr, option, buffer, size, unicode);
882 static const object_vtbl_t APPINFOVtbl = {
883 APPINFO_Destroy,
884 NULL,
885 APPINFO_QueryOption,
886 INET_SetOption,
887 NULL,
888 NULL,
889 NULL,
890 NULL,
891 NULL
895 /***********************************************************************
896 * InternetOpenW (WININET.@)
898 * Per-application initialization of wininet
900 * RETURNS
901 * HINTERNET on success
902 * NULL on failure
905 HINTERNET WINAPI InternetOpenW(LPCWSTR lpszAgent, DWORD dwAccessType,
906 LPCWSTR lpszProxy, LPCWSTR lpszProxyBypass, DWORD dwFlags)
908 appinfo_t *lpwai = NULL;
910 if (TRACE_ON(wininet)) {
911 #define FE(x) { x, #x }
912 static const wininet_flag_info access_type[] = {
913 FE(INTERNET_OPEN_TYPE_PRECONFIG),
914 FE(INTERNET_OPEN_TYPE_DIRECT),
915 FE(INTERNET_OPEN_TYPE_PROXY),
916 FE(INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY)
918 #undef FE
919 DWORD i;
920 const char *access_type_str = "Unknown";
922 TRACE("(%s, %i, %s, %s, %i)\n", debugstr_w(lpszAgent), dwAccessType,
923 debugstr_w(lpszProxy), debugstr_w(lpszProxyBypass), dwFlags);
924 for (i = 0; i < (sizeof(access_type) / sizeof(access_type[0])); i++) {
925 if (access_type[i].val == dwAccessType) {
926 access_type_str = access_type[i].name;
927 break;
930 TRACE(" access type : %s\n", access_type_str);
931 TRACE(" flags :");
932 dump_INTERNET_FLAGS(dwFlags);
935 /* Clear any error information */
936 INTERNET_SetLastError(0);
938 lpwai = alloc_object(NULL, &APPINFOVtbl, sizeof(appinfo_t));
939 if (!lpwai) {
940 SetLastError(ERROR_OUTOFMEMORY);
941 return NULL;
944 lpwai->hdr.htype = WH_HINIT;
945 lpwai->hdr.dwFlags = dwFlags;
946 lpwai->accessType = dwAccessType;
947 lpwai->proxyUsername = NULL;
948 lpwai->proxyPassword = NULL;
950 lpwai->agent = heap_strdupW(lpszAgent);
951 if(dwAccessType == INTERNET_OPEN_TYPE_PRECONFIG)
952 INTERNET_ConfigureProxy( lpwai );
953 else
954 lpwai->proxy = heap_strdupW(lpszProxy);
955 lpwai->proxyBypass = heap_strdupW(lpszProxyBypass);
957 TRACE("returning %p\n", lpwai);
959 return lpwai->hdr.hInternet;
963 /***********************************************************************
964 * InternetOpenA (WININET.@)
966 * Per-application initialization of wininet
968 * RETURNS
969 * HINTERNET on success
970 * NULL on failure
973 HINTERNET WINAPI InternetOpenA(LPCSTR lpszAgent, DWORD dwAccessType,
974 LPCSTR lpszProxy, LPCSTR lpszProxyBypass, DWORD dwFlags)
976 WCHAR *szAgent, *szProxy, *szBypass;
977 HINTERNET rc;
979 TRACE("(%s, 0x%08x, %s, %s, 0x%08x)\n", debugstr_a(lpszAgent),
980 dwAccessType, debugstr_a(lpszProxy), debugstr_a(lpszProxyBypass), dwFlags);
982 szAgent = heap_strdupAtoW(lpszAgent);
983 szProxy = heap_strdupAtoW(lpszProxy);
984 szBypass = heap_strdupAtoW(lpszProxyBypass);
986 rc = InternetOpenW(szAgent, dwAccessType, szProxy, szBypass, dwFlags);
988 heap_free(szAgent);
989 heap_free(szProxy);
990 heap_free(szBypass);
991 return rc;
994 /***********************************************************************
995 * InternetGetLastResponseInfoA (WININET.@)
997 * Return last wininet error description on the calling thread
999 * RETURNS
1000 * TRUE on success of writing to buffer
1001 * FALSE on failure
1004 BOOL WINAPI InternetGetLastResponseInfoA(LPDWORD lpdwError,
1005 LPSTR lpszBuffer, LPDWORD lpdwBufferLength)
1007 LPWITHREADERROR lpwite = TlsGetValue(g_dwTlsErrIndex);
1009 TRACE("\n");
1011 if (lpwite)
1013 *lpdwError = lpwite->dwError;
1014 if (lpwite->dwError)
1016 memcpy(lpszBuffer, lpwite->response, *lpdwBufferLength);
1017 *lpdwBufferLength = strlen(lpszBuffer);
1019 else
1020 *lpdwBufferLength = 0;
1022 else
1024 *lpdwError = 0;
1025 *lpdwBufferLength = 0;
1028 return TRUE;
1031 /***********************************************************************
1032 * InternetGetLastResponseInfoW (WININET.@)
1034 * Return last wininet error description on the calling thread
1036 * RETURNS
1037 * TRUE on success of writing to buffer
1038 * FALSE on failure
1041 BOOL WINAPI InternetGetLastResponseInfoW(LPDWORD lpdwError,
1042 LPWSTR lpszBuffer, LPDWORD lpdwBufferLength)
1044 LPWITHREADERROR lpwite = TlsGetValue(g_dwTlsErrIndex);
1046 TRACE("\n");
1048 if (lpwite)
1050 *lpdwError = lpwite->dwError;
1051 if (lpwite->dwError)
1053 memcpy(lpszBuffer, lpwite->response, *lpdwBufferLength);
1054 *lpdwBufferLength = lstrlenW(lpszBuffer);
1056 else
1057 *lpdwBufferLength = 0;
1059 else
1061 *lpdwError = 0;
1062 *lpdwBufferLength = 0;
1065 return TRUE;
1068 /***********************************************************************
1069 * InternetGetConnectedState (WININET.@)
1071 * Return connected state
1073 * RETURNS
1074 * TRUE if connected
1075 * if lpdwStatus is not null, return the status (off line,
1076 * modem, lan...) in it.
1077 * FALSE if not connected
1079 BOOL WINAPI InternetGetConnectedState(LPDWORD lpdwStatus, DWORD dwReserved)
1081 TRACE("(%p, 0x%08x)\n", lpdwStatus, dwReserved);
1083 if (lpdwStatus) {
1084 WARN("always returning LAN connection.\n");
1085 *lpdwStatus = INTERNET_CONNECTION_LAN;
1087 return TRUE;
1091 /***********************************************************************
1092 * InternetGetConnectedStateExW (WININET.@)
1094 * Return connected state
1096 * PARAMS
1098 * lpdwStatus [O] Flags specifying the status of the internet connection.
1099 * lpszConnectionName [O] Pointer to buffer to receive the friendly name of the internet connection.
1100 * dwNameLen [I] Size of the buffer, in characters.
1101 * dwReserved [I] Reserved. Must be set to 0.
1103 * RETURNS
1104 * TRUE if connected
1105 * if lpdwStatus is not null, return the status (off line,
1106 * modem, lan...) in it.
1107 * FALSE if not connected
1109 * NOTES
1110 * If the system has no available network connections, an empty string is
1111 * stored in lpszConnectionName. If there is a LAN connection, a localized
1112 * "LAN Connection" string is stored. Presumably, if only a dial-up
1113 * connection is available then the name of the dial-up connection is
1114 * returned. Why any application, other than the "Internet Settings" CPL,
1115 * would want to use this function instead of the simpler InternetGetConnectedStateW
1116 * function is beyond me.
1118 BOOL WINAPI InternetGetConnectedStateExW(LPDWORD lpdwStatus, LPWSTR lpszConnectionName,
1119 DWORD dwNameLen, DWORD dwReserved)
1121 TRACE("(%p, %p, %d, 0x%08x)\n", lpdwStatus, lpszConnectionName, dwNameLen, dwReserved);
1123 /* Must be zero */
1124 if(dwReserved)
1125 return FALSE;
1127 if (lpdwStatus) {
1128 WARN("always returning LAN connection.\n");
1129 *lpdwStatus = INTERNET_CONNECTION_LAN;
1131 return LoadStringW(WININET_hModule, IDS_LANCONNECTION, lpszConnectionName, dwNameLen);
1135 /***********************************************************************
1136 * InternetGetConnectedStateExA (WININET.@)
1138 BOOL WINAPI InternetGetConnectedStateExA(LPDWORD lpdwStatus, LPSTR lpszConnectionName,
1139 DWORD dwNameLen, DWORD dwReserved)
1141 LPWSTR lpwszConnectionName = NULL;
1142 BOOL rc;
1144 TRACE("(%p, %p, %d, 0x%08x)\n", lpdwStatus, lpszConnectionName, dwNameLen, dwReserved);
1146 if (lpszConnectionName && dwNameLen > 0)
1147 lpwszConnectionName = heap_alloc(dwNameLen * sizeof(WCHAR));
1149 rc = InternetGetConnectedStateExW(lpdwStatus,lpwszConnectionName, dwNameLen,
1150 dwReserved);
1151 if (rc && lpwszConnectionName)
1153 WideCharToMultiByte(CP_ACP,0,lpwszConnectionName,-1,lpszConnectionName,
1154 dwNameLen, NULL, NULL);
1155 heap_free(lpwszConnectionName);
1157 return rc;
1161 /***********************************************************************
1162 * InternetConnectW (WININET.@)
1164 * Open a ftp, gopher or http session
1166 * RETURNS
1167 * HINTERNET a session handle on success
1168 * NULL on failure
1171 HINTERNET WINAPI InternetConnectW(HINTERNET hInternet,
1172 LPCWSTR lpszServerName, INTERNET_PORT nServerPort,
1173 LPCWSTR lpszUserName, LPCWSTR lpszPassword,
1174 DWORD dwService, DWORD dwFlags, DWORD_PTR dwContext)
1176 appinfo_t *hIC;
1177 HINTERNET rc = NULL;
1178 DWORD res = ERROR_SUCCESS;
1180 TRACE("(%p, %s, %i, %s, %s, %i, %i, %lx)\n", hInternet, debugstr_w(lpszServerName),
1181 nServerPort, debugstr_w(lpszUserName), debugstr_w(lpszPassword),
1182 dwService, dwFlags, dwContext);
1184 if (!lpszServerName)
1186 SetLastError(ERROR_INVALID_PARAMETER);
1187 return NULL;
1190 hIC = (appinfo_t*)get_handle_object( hInternet );
1191 if ( (hIC == NULL) || (hIC->hdr.htype != WH_HINIT) )
1193 res = ERROR_INVALID_HANDLE;
1194 goto lend;
1197 switch (dwService)
1199 case INTERNET_SERVICE_FTP:
1200 rc = FTP_Connect(hIC, lpszServerName, nServerPort,
1201 lpszUserName, lpszPassword, dwFlags, dwContext, 0);
1202 if(!rc)
1203 res = INTERNET_GetLastError();
1204 break;
1206 case INTERNET_SERVICE_HTTP:
1207 res = HTTP_Connect(hIC, lpszServerName, nServerPort,
1208 lpszUserName, lpszPassword, dwFlags, dwContext, 0, &rc);
1209 break;
1211 case INTERNET_SERVICE_GOPHER:
1212 default:
1213 break;
1215 lend:
1216 if( hIC )
1217 WININET_Release( &hIC->hdr );
1219 TRACE("returning %p\n", rc);
1220 SetLastError(res);
1221 return rc;
1225 /***********************************************************************
1226 * InternetConnectA (WININET.@)
1228 * Open a ftp, gopher or http session
1230 * RETURNS
1231 * HINTERNET a session handle on success
1232 * NULL on failure
1235 HINTERNET WINAPI InternetConnectA(HINTERNET hInternet,
1236 LPCSTR lpszServerName, INTERNET_PORT nServerPort,
1237 LPCSTR lpszUserName, LPCSTR lpszPassword,
1238 DWORD dwService, DWORD dwFlags, DWORD_PTR dwContext)
1240 HINTERNET rc = NULL;
1241 LPWSTR szServerName;
1242 LPWSTR szUserName;
1243 LPWSTR szPassword;
1245 szServerName = heap_strdupAtoW(lpszServerName);
1246 szUserName = heap_strdupAtoW(lpszUserName);
1247 szPassword = heap_strdupAtoW(lpszPassword);
1249 rc = InternetConnectW(hInternet, szServerName, nServerPort,
1250 szUserName, szPassword, dwService, dwFlags, dwContext);
1252 heap_free(szServerName);
1253 heap_free(szUserName);
1254 heap_free(szPassword);
1255 return rc;
1259 /***********************************************************************
1260 * InternetFindNextFileA (WININET.@)
1262 * Continues a file search from a previous call to FindFirstFile
1264 * RETURNS
1265 * TRUE on success
1266 * FALSE on failure
1269 BOOL WINAPI InternetFindNextFileA(HINTERNET hFind, LPVOID lpvFindData)
1271 BOOL ret;
1272 WIN32_FIND_DATAW fd;
1274 ret = InternetFindNextFileW(hFind, lpvFindData?&fd:NULL);
1275 if(lpvFindData)
1276 WININET_find_data_WtoA(&fd, (LPWIN32_FIND_DATAA)lpvFindData);
1277 return ret;
1280 /***********************************************************************
1281 * InternetFindNextFileW (WININET.@)
1283 * Continues a file search from a previous call to FindFirstFile
1285 * RETURNS
1286 * TRUE on success
1287 * FALSE on failure
1290 BOOL WINAPI InternetFindNextFileW(HINTERNET hFind, LPVOID lpvFindData)
1292 object_header_t *hdr;
1293 DWORD res;
1295 TRACE("\n");
1297 hdr = get_handle_object(hFind);
1298 if(!hdr) {
1299 WARN("Invalid handle\n");
1300 SetLastError(ERROR_INVALID_HANDLE);
1301 return FALSE;
1304 if(hdr->vtbl->FindNextFileW) {
1305 res = hdr->vtbl->FindNextFileW(hdr, lpvFindData);
1306 }else {
1307 WARN("Handle doesn't support NextFile\n");
1308 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
1311 WININET_Release(hdr);
1313 if(res != ERROR_SUCCESS)
1314 SetLastError(res);
1315 return res == ERROR_SUCCESS;
1318 /***********************************************************************
1319 * InternetCloseHandle (WININET.@)
1321 * Generic close handle function
1323 * RETURNS
1324 * TRUE on success
1325 * FALSE on failure
1328 BOOL WINAPI InternetCloseHandle(HINTERNET hInternet)
1330 object_header_t *obj;
1332 TRACE("%p\n", hInternet);
1334 obj = get_handle_object( hInternet );
1335 if (!obj) {
1336 SetLastError(ERROR_INVALID_HANDLE);
1337 return FALSE;
1340 invalidate_handle(obj);
1341 WININET_Release(obj);
1343 return TRUE;
1347 /***********************************************************************
1348 * ConvertUrlComponentValue (Internal)
1350 * Helper function for InternetCrackUrlA
1353 static void ConvertUrlComponentValue(LPSTR* lppszComponent, LPDWORD dwComponentLen,
1354 LPWSTR lpwszComponent, DWORD dwwComponentLen,
1355 LPCSTR lpszStart, LPCWSTR lpwszStart)
1357 TRACE("%p %d %p %d %p %p\n", *lppszComponent, *dwComponentLen, lpwszComponent, dwwComponentLen, lpszStart, lpwszStart);
1358 if (*dwComponentLen != 0)
1360 DWORD nASCIILength=WideCharToMultiByte(CP_ACP,0,lpwszComponent,dwwComponentLen,NULL,0,NULL,NULL);
1361 if (*lppszComponent == NULL)
1363 if (lpwszComponent)
1365 int offset = WideCharToMultiByte(CP_ACP, 0, lpwszStart, lpwszComponent-lpwszStart, NULL, 0, NULL, NULL);
1366 *lppszComponent = (LPSTR)lpszStart + offset;
1368 else
1369 *lppszComponent = NULL;
1371 *dwComponentLen = nASCIILength;
1373 else
1375 DWORD ncpylen = min((*dwComponentLen)-1, nASCIILength);
1376 WideCharToMultiByte(CP_ACP,0,lpwszComponent,dwwComponentLen,*lppszComponent,ncpylen+1,NULL,NULL);
1377 (*lppszComponent)[ncpylen]=0;
1378 *dwComponentLen = ncpylen;
1384 /***********************************************************************
1385 * InternetCrackUrlA (WININET.@)
1387 * See InternetCrackUrlW.
1389 BOOL WINAPI InternetCrackUrlA(LPCSTR lpszUrl, DWORD dwUrlLength, DWORD dwFlags,
1390 LPURL_COMPONENTSA lpUrlComponents)
1392 DWORD nLength;
1393 URL_COMPONENTSW UCW;
1394 BOOL ret = FALSE;
1395 WCHAR *lpwszUrl, *hostname = NULL, *username = NULL, *password = NULL, *path = NULL,
1396 *scheme = NULL, *extra = NULL;
1398 TRACE("(%s %u %x %p)\n",
1399 lpszUrl ? debugstr_an(lpszUrl, dwUrlLength ? dwUrlLength : strlen(lpszUrl)) : "(null)",
1400 dwUrlLength, dwFlags, lpUrlComponents);
1402 if (!lpszUrl || !*lpszUrl || !lpUrlComponents ||
1403 lpUrlComponents->dwStructSize != sizeof(URL_COMPONENTSA))
1405 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1406 return FALSE;
1409 if(dwUrlLength<=0)
1410 dwUrlLength=-1;
1411 nLength=MultiByteToWideChar(CP_ACP,0,lpszUrl,dwUrlLength,NULL,0);
1413 /* if dwUrlLength=-1 then nLength includes null but length to
1414 InternetCrackUrlW should not include it */
1415 if (dwUrlLength == -1) nLength--;
1417 lpwszUrl = heap_alloc((nLength + 1) * sizeof(WCHAR));
1418 MultiByteToWideChar(CP_ACP,0,lpszUrl,dwUrlLength,lpwszUrl,nLength + 1);
1419 lpwszUrl[nLength] = '\0';
1421 memset(&UCW,0,sizeof(UCW));
1422 UCW.dwStructSize = sizeof(URL_COMPONENTSW);
1423 if (lpUrlComponents->dwHostNameLength)
1425 UCW.dwHostNameLength = lpUrlComponents->dwHostNameLength;
1426 if (lpUrlComponents->lpszHostName)
1428 hostname = heap_alloc(UCW.dwHostNameLength * sizeof(WCHAR));
1429 UCW.lpszHostName = hostname;
1432 if (lpUrlComponents->dwUserNameLength)
1434 UCW.dwUserNameLength = lpUrlComponents->dwUserNameLength;
1435 if (lpUrlComponents->lpszUserName)
1437 username = heap_alloc(UCW.dwUserNameLength * sizeof(WCHAR));
1438 UCW.lpszUserName = username;
1441 if (lpUrlComponents->dwPasswordLength)
1443 UCW.dwPasswordLength = lpUrlComponents->dwPasswordLength;
1444 if (lpUrlComponents->lpszPassword)
1446 password = heap_alloc(UCW.dwPasswordLength * sizeof(WCHAR));
1447 UCW.lpszPassword = password;
1450 if (lpUrlComponents->dwUrlPathLength)
1452 UCW.dwUrlPathLength = lpUrlComponents->dwUrlPathLength;
1453 if (lpUrlComponents->lpszUrlPath)
1455 path = heap_alloc(UCW.dwUrlPathLength * sizeof(WCHAR));
1456 UCW.lpszUrlPath = path;
1459 if (lpUrlComponents->dwSchemeLength)
1461 UCW.dwSchemeLength = lpUrlComponents->dwSchemeLength;
1462 if (lpUrlComponents->lpszScheme)
1464 scheme = heap_alloc(UCW.dwSchemeLength * sizeof(WCHAR));
1465 UCW.lpszScheme = scheme;
1468 if (lpUrlComponents->dwExtraInfoLength)
1470 UCW.dwExtraInfoLength = lpUrlComponents->dwExtraInfoLength;
1471 if (lpUrlComponents->lpszExtraInfo)
1473 extra = heap_alloc(UCW.dwExtraInfoLength * sizeof(WCHAR));
1474 UCW.lpszExtraInfo = extra;
1477 if ((ret = InternetCrackUrlW(lpwszUrl, nLength, dwFlags, &UCW)))
1479 ConvertUrlComponentValue(&lpUrlComponents->lpszHostName, &lpUrlComponents->dwHostNameLength,
1480 UCW.lpszHostName, UCW.dwHostNameLength, lpszUrl, lpwszUrl);
1481 ConvertUrlComponentValue(&lpUrlComponents->lpszUserName, &lpUrlComponents->dwUserNameLength,
1482 UCW.lpszUserName, UCW.dwUserNameLength, lpszUrl, lpwszUrl);
1483 ConvertUrlComponentValue(&lpUrlComponents->lpszPassword, &lpUrlComponents->dwPasswordLength,
1484 UCW.lpszPassword, UCW.dwPasswordLength, lpszUrl, lpwszUrl);
1485 ConvertUrlComponentValue(&lpUrlComponents->lpszUrlPath, &lpUrlComponents->dwUrlPathLength,
1486 UCW.lpszUrlPath, UCW.dwUrlPathLength, lpszUrl, lpwszUrl);
1487 ConvertUrlComponentValue(&lpUrlComponents->lpszScheme, &lpUrlComponents->dwSchemeLength,
1488 UCW.lpszScheme, UCW.dwSchemeLength, lpszUrl, lpwszUrl);
1489 ConvertUrlComponentValue(&lpUrlComponents->lpszExtraInfo, &lpUrlComponents->dwExtraInfoLength,
1490 UCW.lpszExtraInfo, UCW.dwExtraInfoLength, lpszUrl, lpwszUrl);
1492 lpUrlComponents->nScheme = UCW.nScheme;
1493 lpUrlComponents->nPort = UCW.nPort;
1495 TRACE("%s: scheme(%s) host(%s) path(%s) extra(%s)\n", debugstr_a(lpszUrl),
1496 debugstr_an(lpUrlComponents->lpszScheme, lpUrlComponents->dwSchemeLength),
1497 debugstr_an(lpUrlComponents->lpszHostName, lpUrlComponents->dwHostNameLength),
1498 debugstr_an(lpUrlComponents->lpszUrlPath, lpUrlComponents->dwUrlPathLength),
1499 debugstr_an(lpUrlComponents->lpszExtraInfo, lpUrlComponents->dwExtraInfoLength));
1501 heap_free(lpwszUrl);
1502 heap_free(hostname);
1503 heap_free(username);
1504 heap_free(password);
1505 heap_free(path);
1506 heap_free(scheme);
1507 heap_free(extra);
1508 return ret;
1511 static const WCHAR url_schemes[][7] =
1513 {'f','t','p',0},
1514 {'g','o','p','h','e','r',0},
1515 {'h','t','t','p',0},
1516 {'h','t','t','p','s',0},
1517 {'f','i','l','e',0},
1518 {'n','e','w','s',0},
1519 {'m','a','i','l','t','o',0},
1520 {'r','e','s',0},
1523 /***********************************************************************
1524 * GetInternetSchemeW (internal)
1526 * Get scheme of url
1528 * RETURNS
1529 * scheme on success
1530 * INTERNET_SCHEME_UNKNOWN on failure
1533 static INTERNET_SCHEME GetInternetSchemeW(LPCWSTR lpszScheme, DWORD nMaxCmp)
1535 int i;
1537 TRACE("%s %d\n",debugstr_wn(lpszScheme, nMaxCmp), nMaxCmp);
1539 if(lpszScheme==NULL)
1540 return INTERNET_SCHEME_UNKNOWN;
1542 for (i = 0; i < sizeof(url_schemes)/sizeof(url_schemes[0]); i++)
1543 if (!strncmpW(lpszScheme, url_schemes[i], nMaxCmp))
1544 return INTERNET_SCHEME_FIRST + i;
1546 return INTERNET_SCHEME_UNKNOWN;
1549 /***********************************************************************
1550 * SetUrlComponentValueW (Internal)
1552 * Helper function for InternetCrackUrlW
1554 * PARAMS
1555 * lppszComponent [O] Holds the returned string
1556 * dwComponentLen [I] Holds the size of lppszComponent
1557 * [O] Holds the length of the string in lppszComponent without '\0'
1558 * lpszStart [I] Holds the string to copy from
1559 * len [I] Holds the length of lpszStart without '\0'
1561 * RETURNS
1562 * TRUE on success
1563 * FALSE on failure
1566 static BOOL SetUrlComponentValueW(LPWSTR* lppszComponent, LPDWORD dwComponentLen, LPCWSTR lpszStart, DWORD len)
1568 TRACE("%s (%d)\n", debugstr_wn(lpszStart,len), len);
1570 if ( (*dwComponentLen == 0) && (*lppszComponent == NULL) )
1571 return FALSE;
1573 if (*dwComponentLen != 0 || *lppszComponent == NULL)
1575 if (*lppszComponent == NULL)
1577 *lppszComponent = (LPWSTR)lpszStart;
1578 *dwComponentLen = len;
1580 else
1582 DWORD ncpylen = min((*dwComponentLen)-1, len);
1583 memcpy(*lppszComponent, lpszStart, ncpylen*sizeof(WCHAR));
1584 (*lppszComponent)[ncpylen] = '\0';
1585 *dwComponentLen = ncpylen;
1589 return TRUE;
1592 /***********************************************************************
1593 * InternetCrackUrlW (WININET.@)
1595 * Break up URL into its components
1597 * RETURNS
1598 * TRUE on success
1599 * FALSE on failure
1601 BOOL WINAPI InternetCrackUrlW(LPCWSTR lpszUrl_orig, DWORD dwUrlLength_orig, DWORD dwFlags,
1602 LPURL_COMPONENTSW lpUC)
1605 * RFC 1808
1606 * <protocol>:[//<net_loc>][/path][;<params>][?<query>][#<fragment>]
1609 LPCWSTR lpszParam = NULL;
1610 BOOL bIsAbsolute = FALSE;
1611 LPCWSTR lpszap, lpszUrl = lpszUrl_orig;
1612 LPCWSTR lpszcp = NULL;
1613 LPWSTR lpszUrl_decode = NULL;
1614 DWORD dwUrlLength = dwUrlLength_orig;
1616 TRACE("(%s %u %x %p)\n",
1617 lpszUrl ? debugstr_wn(lpszUrl, dwUrlLength ? dwUrlLength : strlenW(lpszUrl)) : "(null)",
1618 dwUrlLength, dwFlags, lpUC);
1620 if (!lpszUrl_orig || !*lpszUrl_orig || !lpUC)
1622 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1623 return FALSE;
1625 if (!dwUrlLength) dwUrlLength = strlenW(lpszUrl);
1627 if (dwFlags & ICU_DECODE)
1629 WCHAR *url_tmp;
1630 DWORD len = dwUrlLength + 1;
1632 if (!(url_tmp = heap_alloc(len * sizeof(WCHAR))))
1634 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1635 return FALSE;
1637 memcpy(url_tmp, lpszUrl_orig, dwUrlLength * sizeof(WCHAR));
1638 url_tmp[dwUrlLength] = 0;
1639 if (!(lpszUrl_decode = heap_alloc(len * sizeof(WCHAR))))
1641 heap_free(url_tmp);
1642 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1643 return FALSE;
1645 if (InternetCanonicalizeUrlW(url_tmp, lpszUrl_decode, &len, ICU_DECODE | ICU_NO_ENCODE))
1647 dwUrlLength = len;
1648 lpszUrl = lpszUrl_decode;
1650 heap_free(url_tmp);
1652 lpszap = lpszUrl;
1654 /* Determine if the URI is absolute. */
1655 while (lpszap - lpszUrl < dwUrlLength)
1657 if (isalnumW(*lpszap) || *lpszap == '+' || *lpszap == '.' || *lpszap == '-')
1659 lpszap++;
1660 continue;
1662 if ((*lpszap == ':') && (lpszap - lpszUrl >= 2))
1664 bIsAbsolute = TRUE;
1665 lpszcp = lpszap;
1667 else
1669 lpszcp = lpszUrl; /* Relative url */
1672 break;
1675 lpUC->nScheme = INTERNET_SCHEME_UNKNOWN;
1676 lpUC->nPort = INTERNET_INVALID_PORT_NUMBER;
1678 /* Parse <params> */
1679 lpszParam = memchrW(lpszap, ';', dwUrlLength - (lpszap - lpszUrl));
1680 if(!lpszParam)
1681 lpszParam = memchrW(lpszap, '?', dwUrlLength - (lpszap - lpszUrl));
1682 if(!lpszParam)
1683 lpszParam = memchrW(lpszap, '#', dwUrlLength - (lpszap - lpszUrl));
1685 SetUrlComponentValueW(&lpUC->lpszExtraInfo, &lpUC->dwExtraInfoLength,
1686 lpszParam, lpszParam ? dwUrlLength-(lpszParam-lpszUrl) : 0);
1688 if (bIsAbsolute) /* Parse <protocol>:[//<net_loc>] */
1690 LPCWSTR lpszNetLoc;
1692 /* Get scheme first. */
1693 lpUC->nScheme = GetInternetSchemeW(lpszUrl, lpszcp - lpszUrl);
1694 SetUrlComponentValueW(&lpUC->lpszScheme, &lpUC->dwSchemeLength,
1695 lpszUrl, lpszcp - lpszUrl);
1697 /* Eat ':' in protocol. */
1698 lpszcp++;
1700 /* double slash indicates the net_loc portion is present */
1701 if ((lpszcp[0] == '/') && (lpszcp[1] == '/'))
1703 lpszcp += 2;
1705 lpszNetLoc = memchrW(lpszcp, '/', dwUrlLength - (lpszcp - lpszUrl));
1706 if (lpszParam)
1708 if (lpszNetLoc)
1709 lpszNetLoc = min(lpszNetLoc, lpszParam);
1710 else
1711 lpszNetLoc = lpszParam;
1713 else if (!lpszNetLoc)
1714 lpszNetLoc = lpszcp + dwUrlLength-(lpszcp-lpszUrl);
1716 /* Parse net-loc */
1717 if (lpszNetLoc)
1719 LPCWSTR lpszHost;
1720 LPCWSTR lpszPort;
1722 /* [<user>[<:password>]@]<host>[:<port>] */
1723 /* First find the user and password if they exist */
1725 lpszHost = memchrW(lpszcp, '@', dwUrlLength - (lpszcp - lpszUrl));
1726 if (lpszHost == NULL || lpszHost > lpszNetLoc)
1728 /* username and password not specified. */
1729 SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength, NULL, 0);
1730 SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength, NULL, 0);
1732 else /* Parse out username and password */
1734 LPCWSTR lpszUser = lpszcp;
1735 LPCWSTR lpszPasswd = lpszHost;
1737 while (lpszcp < lpszHost)
1739 if (*lpszcp == ':')
1740 lpszPasswd = lpszcp;
1742 lpszcp++;
1745 SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength,
1746 lpszUser, lpszPasswd - lpszUser);
1748 if (lpszPasswd != lpszHost)
1749 lpszPasswd++;
1750 SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength,
1751 lpszPasswd == lpszHost ? NULL : lpszPasswd,
1752 lpszHost - lpszPasswd);
1754 lpszcp++; /* Advance to beginning of host */
1757 /* Parse <host><:port> */
1759 lpszHost = lpszcp;
1760 lpszPort = lpszNetLoc;
1762 /* special case for res:// URLs: there is no port here, so the host is the
1763 entire string up to the first '/' */
1764 if(lpUC->nScheme==INTERNET_SCHEME_RES)
1766 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength,
1767 lpszHost, lpszPort - lpszHost);
1768 lpszcp=lpszNetLoc;
1770 else
1772 while (lpszcp < lpszNetLoc)
1774 if (*lpszcp == ':')
1775 lpszPort = lpszcp;
1777 lpszcp++;
1780 /* If the scheme is "file" and the host is just one letter, it's not a host */
1781 if(lpUC->nScheme==INTERNET_SCHEME_FILE && lpszPort <= lpszHost+1)
1783 lpszcp=lpszHost;
1784 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength,
1785 NULL, 0);
1787 else
1789 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength,
1790 lpszHost, lpszPort - lpszHost);
1791 if (lpszPort != lpszNetLoc)
1792 lpUC->nPort = atoiW(++lpszPort);
1793 else switch (lpUC->nScheme)
1795 case INTERNET_SCHEME_HTTP:
1796 lpUC->nPort = INTERNET_DEFAULT_HTTP_PORT;
1797 break;
1798 case INTERNET_SCHEME_HTTPS:
1799 lpUC->nPort = INTERNET_DEFAULT_HTTPS_PORT;
1800 break;
1801 case INTERNET_SCHEME_FTP:
1802 lpUC->nPort = INTERNET_DEFAULT_FTP_PORT;
1803 break;
1804 case INTERNET_SCHEME_GOPHER:
1805 lpUC->nPort = INTERNET_DEFAULT_GOPHER_PORT;
1806 break;
1807 default:
1808 break;
1814 else
1816 SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength, NULL, 0);
1817 SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength, NULL, 0);
1818 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength, NULL, 0);
1821 else
1823 SetUrlComponentValueW(&lpUC->lpszScheme, &lpUC->dwSchemeLength, NULL, 0);
1824 SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength, NULL, 0);
1825 SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength, NULL, 0);
1826 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength, NULL, 0);
1829 /* Here lpszcp points to:
1831 * <protocol>:[//<net_loc>][/path][;<params>][?<query>][#<fragment>]
1832 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1834 if (lpszcp != 0 && lpszcp - lpszUrl < dwUrlLength && (!lpszParam || lpszcp <= lpszParam))
1836 DWORD len;
1838 /* Only truncate the parameter list if it's already been saved
1839 * in lpUC->lpszExtraInfo.
1841 if (lpszParam && lpUC->dwExtraInfoLength && lpUC->lpszExtraInfo)
1842 len = lpszParam - lpszcp;
1843 else
1845 /* Leave the parameter list in lpszUrlPath. Strip off any trailing
1846 * newlines if necessary.
1848 LPWSTR lpsznewline = memchrW(lpszcp, '\n', dwUrlLength - (lpszcp - lpszUrl));
1849 if (lpsznewline != NULL)
1850 len = lpsznewline - lpszcp;
1851 else
1852 len = dwUrlLength-(lpszcp-lpszUrl);
1854 if (lpUC->dwUrlPathLength && lpUC->lpszUrlPath &&
1855 lpUC->nScheme == INTERNET_SCHEME_FILE)
1857 WCHAR tmppath[MAX_PATH];
1858 if (*lpszcp == '/')
1860 len = MAX_PATH;
1861 PathCreateFromUrlW(lpszUrl_orig, tmppath, &len, 0);
1863 else
1865 WCHAR *iter;
1866 memcpy(tmppath, lpszcp, len * sizeof(WCHAR));
1867 tmppath[len] = '\0';
1869 iter = tmppath;
1870 while (*iter) {
1871 if (*iter == '/')
1872 *iter = '\\';
1873 ++iter;
1876 /* if ends in \. or \.. append a backslash */
1877 if (tmppath[len - 1] == '.' &&
1878 (tmppath[len - 2] == '\\' ||
1879 (tmppath[len - 2] == '.' && tmppath[len - 3] == '\\')))
1881 if (len < MAX_PATH - 1)
1883 tmppath[len] = '\\';
1884 tmppath[len+1] = '\0';
1885 ++len;
1888 SetUrlComponentValueW(&lpUC->lpszUrlPath, &lpUC->dwUrlPathLength,
1889 tmppath, len);
1891 else
1892 SetUrlComponentValueW(&lpUC->lpszUrlPath, &lpUC->dwUrlPathLength,
1893 lpszcp, len);
1895 else
1897 if (lpUC->lpszUrlPath && (lpUC->dwUrlPathLength > 0))
1898 lpUC->lpszUrlPath[0] = 0;
1899 lpUC->dwUrlPathLength = 0;
1902 TRACE("%s: scheme(%s) host(%s) path(%s) extra(%s)\n", debugstr_wn(lpszUrl,dwUrlLength),
1903 debugstr_wn(lpUC->lpszScheme,lpUC->dwSchemeLength),
1904 debugstr_wn(lpUC->lpszHostName,lpUC->dwHostNameLength),
1905 debugstr_wn(lpUC->lpszUrlPath,lpUC->dwUrlPathLength),
1906 debugstr_wn(lpUC->lpszExtraInfo,lpUC->dwExtraInfoLength));
1908 heap_free( lpszUrl_decode );
1909 return TRUE;
1912 /***********************************************************************
1913 * InternetAttemptConnect (WININET.@)
1915 * Attempt to make a connection to the internet
1917 * RETURNS
1918 * ERROR_SUCCESS on success
1919 * Error value on failure
1922 DWORD WINAPI InternetAttemptConnect(DWORD dwReserved)
1924 FIXME("Stub\n");
1925 return ERROR_SUCCESS;
1929 /***********************************************************************
1930 * InternetCanonicalizeUrlA (WININET.@)
1932 * Escape unsafe characters and spaces
1934 * RETURNS
1935 * TRUE on success
1936 * FALSE on failure
1939 BOOL WINAPI InternetCanonicalizeUrlA(LPCSTR lpszUrl, LPSTR lpszBuffer,
1940 LPDWORD lpdwBufferLength, DWORD dwFlags)
1942 HRESULT hr;
1943 DWORD dwURLFlags = URL_WININET_COMPATIBILITY | URL_ESCAPE_UNSAFE;
1945 TRACE("(%s, %p, %p, 0x%08x) bufferlength: %d\n", debugstr_a(lpszUrl), lpszBuffer,
1946 lpdwBufferLength, dwFlags, lpdwBufferLength ? *lpdwBufferLength : -1);
1948 if(dwFlags & ICU_DECODE)
1950 dwURLFlags |= URL_UNESCAPE;
1951 dwFlags &= ~ICU_DECODE;
1954 if(dwFlags & ICU_ESCAPE)
1956 dwURLFlags |= URL_UNESCAPE;
1957 dwFlags &= ~ICU_ESCAPE;
1960 if(dwFlags & ICU_BROWSER_MODE)
1962 dwURLFlags |= URL_BROWSER_MODE;
1963 dwFlags &= ~ICU_BROWSER_MODE;
1966 if(dwFlags & ICU_NO_ENCODE)
1968 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
1969 dwURLFlags ^= URL_ESCAPE_UNSAFE;
1970 dwFlags &= ~ICU_NO_ENCODE;
1973 if (dwFlags) FIXME("Unhandled flags 0x%08x\n", dwFlags);
1975 hr = UrlCanonicalizeA(lpszUrl, lpszBuffer, lpdwBufferLength, dwURLFlags);
1976 if (hr == E_POINTER) SetLastError(ERROR_INSUFFICIENT_BUFFER);
1977 if (hr == E_INVALIDARG) SetLastError(ERROR_INVALID_PARAMETER);
1979 return (hr == S_OK) ? TRUE : FALSE;
1982 /***********************************************************************
1983 * InternetCanonicalizeUrlW (WININET.@)
1985 * Escape unsafe characters and spaces
1987 * RETURNS
1988 * TRUE on success
1989 * FALSE on failure
1992 BOOL WINAPI InternetCanonicalizeUrlW(LPCWSTR lpszUrl, LPWSTR lpszBuffer,
1993 LPDWORD lpdwBufferLength, DWORD dwFlags)
1995 HRESULT hr;
1996 DWORD dwURLFlags = URL_WININET_COMPATIBILITY | URL_ESCAPE_UNSAFE;
1998 TRACE("(%s, %p, %p, 0x%08x) bufferlength: %d\n", debugstr_w(lpszUrl), lpszBuffer,
1999 lpdwBufferLength, dwFlags, lpdwBufferLength ? *lpdwBufferLength : -1);
2001 if(dwFlags & ICU_DECODE)
2003 dwURLFlags |= URL_UNESCAPE;
2004 dwFlags &= ~ICU_DECODE;
2007 if(dwFlags & ICU_ESCAPE)
2009 dwURLFlags |= URL_UNESCAPE;
2010 dwFlags &= ~ICU_ESCAPE;
2013 if(dwFlags & ICU_BROWSER_MODE)
2015 dwURLFlags |= URL_BROWSER_MODE;
2016 dwFlags &= ~ICU_BROWSER_MODE;
2019 if(dwFlags & ICU_NO_ENCODE)
2021 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
2022 dwURLFlags ^= URL_ESCAPE_UNSAFE;
2023 dwFlags &= ~ICU_NO_ENCODE;
2026 if (dwFlags) FIXME("Unhandled flags 0x%08x\n", dwFlags);
2028 hr = UrlCanonicalizeW(lpszUrl, lpszBuffer, lpdwBufferLength, dwURLFlags);
2029 if (hr == E_POINTER) SetLastError(ERROR_INSUFFICIENT_BUFFER);
2030 if (hr == E_INVALIDARG) SetLastError(ERROR_INVALID_PARAMETER);
2032 return (hr == S_OK) ? TRUE : FALSE;
2035 /* #################################################### */
2037 static INTERNET_STATUS_CALLBACK set_status_callback(
2038 object_header_t *lpwh, INTERNET_STATUS_CALLBACK callback, BOOL unicode)
2040 INTERNET_STATUS_CALLBACK ret;
2042 if (unicode) lpwh->dwInternalFlags |= INET_CALLBACKW;
2043 else lpwh->dwInternalFlags &= ~INET_CALLBACKW;
2045 ret = lpwh->lpfnStatusCB;
2046 lpwh->lpfnStatusCB = callback;
2048 return ret;
2051 /***********************************************************************
2052 * InternetSetStatusCallbackA (WININET.@)
2054 * Sets up a callback function which is called as progress is made
2055 * during an operation.
2057 * RETURNS
2058 * Previous callback or NULL on success
2059 * INTERNET_INVALID_STATUS_CALLBACK on failure
2062 INTERNET_STATUS_CALLBACK WINAPI InternetSetStatusCallbackA(
2063 HINTERNET hInternet ,INTERNET_STATUS_CALLBACK lpfnIntCB)
2065 INTERNET_STATUS_CALLBACK retVal;
2066 object_header_t *lpwh;
2068 TRACE("%p\n", hInternet);
2070 if (!(lpwh = get_handle_object(hInternet)))
2071 return INTERNET_INVALID_STATUS_CALLBACK;
2073 retVal = set_status_callback(lpwh, lpfnIntCB, FALSE);
2075 WININET_Release( lpwh );
2076 return retVal;
2079 /***********************************************************************
2080 * InternetSetStatusCallbackW (WININET.@)
2082 * Sets up a callback function which is called as progress is made
2083 * during an operation.
2085 * RETURNS
2086 * Previous callback or NULL on success
2087 * INTERNET_INVALID_STATUS_CALLBACK on failure
2090 INTERNET_STATUS_CALLBACK WINAPI InternetSetStatusCallbackW(
2091 HINTERNET hInternet ,INTERNET_STATUS_CALLBACK lpfnIntCB)
2093 INTERNET_STATUS_CALLBACK retVal;
2094 object_header_t *lpwh;
2096 TRACE("%p\n", hInternet);
2098 if (!(lpwh = get_handle_object(hInternet)))
2099 return INTERNET_INVALID_STATUS_CALLBACK;
2101 retVal = set_status_callback(lpwh, lpfnIntCB, TRUE);
2103 WININET_Release( lpwh );
2104 return retVal;
2107 /***********************************************************************
2108 * InternetSetFilePointer (WININET.@)
2110 DWORD WINAPI InternetSetFilePointer(HINTERNET hFile, LONG lDistanceToMove,
2111 PVOID pReserved, DWORD dwMoveContext, DWORD_PTR dwContext)
2113 FIXME("(%p %d %p %d %lx): stub\n", hFile, lDistanceToMove, pReserved, dwMoveContext, dwContext);
2114 return FALSE;
2117 /***********************************************************************
2118 * InternetWriteFile (WININET.@)
2120 * Write data to an open internet file
2122 * RETURNS
2123 * TRUE on success
2124 * FALSE on failure
2127 BOOL WINAPI InternetWriteFile(HINTERNET hFile, LPCVOID lpBuffer,
2128 DWORD dwNumOfBytesToWrite, LPDWORD lpdwNumOfBytesWritten)
2130 object_header_t *lpwh;
2131 BOOL res;
2133 TRACE("(%p %p %d %p)\n", hFile, lpBuffer, dwNumOfBytesToWrite, lpdwNumOfBytesWritten);
2135 lpwh = get_handle_object( hFile );
2136 if (!lpwh) {
2137 WARN("Invalid handle\n");
2138 SetLastError(ERROR_INVALID_HANDLE);
2139 return FALSE;
2142 if(lpwh->vtbl->WriteFile) {
2143 res = lpwh->vtbl->WriteFile(lpwh, lpBuffer, dwNumOfBytesToWrite, lpdwNumOfBytesWritten);
2144 }else {
2145 WARN("No Writefile method.\n");
2146 res = ERROR_INVALID_HANDLE;
2149 WININET_Release( lpwh );
2151 if(res != ERROR_SUCCESS)
2152 SetLastError(res);
2153 return res == ERROR_SUCCESS;
2157 /***********************************************************************
2158 * InternetReadFile (WININET.@)
2160 * Read data from an open internet file
2162 * RETURNS
2163 * TRUE on success
2164 * FALSE on failure
2167 BOOL WINAPI InternetReadFile(HINTERNET hFile, LPVOID lpBuffer,
2168 DWORD dwNumOfBytesToRead, LPDWORD pdwNumOfBytesRead)
2170 object_header_t *hdr;
2171 DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2173 TRACE("%p %p %d %p\n", hFile, lpBuffer, dwNumOfBytesToRead, pdwNumOfBytesRead);
2175 hdr = get_handle_object(hFile);
2176 if (!hdr) {
2177 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
2178 return FALSE;
2181 if(hdr->vtbl->ReadFile)
2182 res = hdr->vtbl->ReadFile(hdr, lpBuffer, dwNumOfBytesToRead, pdwNumOfBytesRead);
2184 WININET_Release(hdr);
2186 TRACE("-- %s (%u) (bytes read: %d)\n", res == ERROR_SUCCESS ? "TRUE": "FALSE", res,
2187 pdwNumOfBytesRead ? *pdwNumOfBytesRead : -1);
2189 if(res != ERROR_SUCCESS)
2190 SetLastError(res);
2191 return res == ERROR_SUCCESS;
2194 /***********************************************************************
2195 * InternetReadFileExA (WININET.@)
2197 * Read data from an open internet file
2199 * PARAMS
2200 * hFile [I] Handle returned by InternetOpenUrl or HttpOpenRequest.
2201 * lpBuffersOut [I/O] Buffer.
2202 * dwFlags [I] Flags. See notes.
2203 * dwContext [I] Context for callbacks.
2205 * RETURNS
2206 * TRUE on success
2207 * FALSE on failure
2209 * NOTES
2210 * The parameter dwFlags include zero or more of the following flags:
2211 *|IRF_ASYNC - Makes the call asynchronous.
2212 *|IRF_SYNC - Makes the call synchronous.
2213 *|IRF_USE_CONTEXT - Forces dwContext to be used.
2214 *|IRF_NO_WAIT - Don't block if the data is not available, just return what is available.
2216 * However, in testing IRF_USE_CONTEXT seems to have no effect - dwContext isn't used.
2218 * SEE
2219 * InternetOpenUrlA(), HttpOpenRequestA()
2221 BOOL WINAPI InternetReadFileExA(HINTERNET hFile, LPINTERNET_BUFFERSA lpBuffersOut,
2222 DWORD dwFlags, DWORD_PTR dwContext)
2224 object_header_t *hdr;
2225 DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2227 TRACE("(%p %p 0x%x 0x%lx)\n", hFile, lpBuffersOut, dwFlags, dwContext);
2229 hdr = get_handle_object(hFile);
2230 if (!hdr) {
2231 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
2232 return FALSE;
2235 if(hdr->vtbl->ReadFileExA)
2236 res = hdr->vtbl->ReadFileExA(hdr, lpBuffersOut, dwFlags, dwContext);
2238 WININET_Release(hdr);
2240 TRACE("-- %s (%u, bytes read: %d)\n", res == ERROR_SUCCESS ? "TRUE": "FALSE",
2241 res, lpBuffersOut->dwBufferLength);
2243 if(res != ERROR_SUCCESS)
2244 SetLastError(res);
2245 return res == ERROR_SUCCESS;
2248 /***********************************************************************
2249 * InternetReadFileExW (WININET.@)
2250 * SEE
2251 * InternetReadFileExA()
2253 BOOL WINAPI InternetReadFileExW(HINTERNET hFile, LPINTERNET_BUFFERSW lpBuffer,
2254 DWORD dwFlags, DWORD_PTR dwContext)
2256 object_header_t *hdr;
2257 DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2259 TRACE("(%p %p 0x%x 0x%lx)\n", hFile, lpBuffer, dwFlags, dwContext);
2261 hdr = get_handle_object(hFile);
2262 if (!hdr) {
2263 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
2264 return FALSE;
2267 if(hdr->vtbl->ReadFileExW)
2268 res = hdr->vtbl->ReadFileExW(hdr, lpBuffer, dwFlags, dwContext);
2270 WININET_Release(hdr);
2272 TRACE("-- %s (%u, bytes read: %d)\n", res == ERROR_SUCCESS ? "TRUE": "FALSE",
2273 res, lpBuffer->dwBufferLength);
2275 if(res != ERROR_SUCCESS)
2276 SetLastError(res);
2277 return res == ERROR_SUCCESS;
2280 DWORD INET_QueryOption(object_header_t *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
2282 static BOOL warn = TRUE;
2284 switch(option) {
2285 case INTERNET_OPTION_REQUEST_FLAGS:
2286 TRACE("INTERNET_OPTION_REQUEST_FLAGS\n");
2288 if (*size < sizeof(ULONG))
2289 return ERROR_INSUFFICIENT_BUFFER;
2291 *(ULONG*)buffer = 4;
2292 *size = sizeof(ULONG);
2294 return ERROR_SUCCESS;
2296 case INTERNET_OPTION_HTTP_VERSION:
2297 if (*size < sizeof(HTTP_VERSION_INFO))
2298 return ERROR_INSUFFICIENT_BUFFER;
2301 * Presently hardcoded to 1.1
2303 ((HTTP_VERSION_INFO*)buffer)->dwMajorVersion = 1;
2304 ((HTTP_VERSION_INFO*)buffer)->dwMinorVersion = 1;
2305 *size = sizeof(HTTP_VERSION_INFO);
2307 return ERROR_SUCCESS;
2309 case INTERNET_OPTION_CONNECTED_STATE:
2310 if (warn) {
2311 FIXME("INTERNET_OPTION_CONNECTED_STATE: semi-stub\n");
2312 warn = FALSE;
2314 if (*size < sizeof(ULONG))
2315 return ERROR_INSUFFICIENT_BUFFER;
2317 *(ULONG*)buffer = INTERNET_STATE_CONNECTED;
2318 *size = sizeof(ULONG);
2320 return ERROR_SUCCESS;
2322 case INTERNET_OPTION_PROXY: {
2323 appinfo_t ai;
2324 BOOL ret;
2326 TRACE("Getting global proxy info\n");
2327 memset(&ai, 0, sizeof(appinfo_t));
2328 INTERNET_ConfigureProxy(&ai);
2330 ret = APPINFO_QueryOption(&ai.hdr, INTERNET_OPTION_PROXY, buffer, size, unicode); /* FIXME */
2331 APPINFO_Destroy(&ai.hdr);
2332 return ret;
2335 case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2336 TRACE("INTERNET_OPTION_MAX_CONNS_PER_SERVER\n");
2338 if (*size < sizeof(ULONG))
2339 return ERROR_INSUFFICIENT_BUFFER;
2341 *(ULONG*)buffer = max_conns;
2342 *size = sizeof(ULONG);
2344 return ERROR_SUCCESS;
2346 case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2347 TRACE("INTERNET_OPTION_MAX_CONNS_1_0_SERVER\n");
2349 if (*size < sizeof(ULONG))
2350 return ERROR_INSUFFICIENT_BUFFER;
2352 *(ULONG*)buffer = max_1_0_conns;
2353 *size = sizeof(ULONG);
2355 return ERROR_SUCCESS;
2357 case INTERNET_OPTION_SECURITY_FLAGS:
2358 FIXME("INTERNET_OPTION_SECURITY_FLAGS: Stub\n");
2359 return ERROR_SUCCESS;
2361 case INTERNET_OPTION_VERSION: {
2362 static const INTERNET_VERSION_INFO info = { 1, 2 };
2364 TRACE("INTERNET_OPTION_VERSION\n");
2366 if (*size < sizeof(INTERNET_VERSION_INFO))
2367 return ERROR_INSUFFICIENT_BUFFER;
2369 memcpy(buffer, &info, sizeof(info));
2370 *size = sizeof(info);
2372 return ERROR_SUCCESS;
2375 case INTERNET_OPTION_PER_CONNECTION_OPTION: {
2376 INTERNET_PER_CONN_OPTION_LISTW *con = buffer;
2377 INTERNET_PER_CONN_OPTION_LISTA *conA = buffer;
2378 DWORD res = ERROR_SUCCESS, i;
2379 proxyinfo_t pi;
2380 LONG ret;
2382 TRACE("Getting global proxy info\n");
2383 if((ret = INTERNET_LoadProxySettings(&pi)))
2384 return ret;
2386 FIXME("INTERNET_OPTION_PER_CONNECTION_OPTION stub\n");
2388 if (*size < sizeof(INTERNET_PER_CONN_OPTION_LISTW)) {
2389 FreeProxyInfo(&pi);
2390 return ERROR_INSUFFICIENT_BUFFER;
2393 for (i = 0; i < con->dwOptionCount; i++) {
2394 INTERNET_PER_CONN_OPTIONW *optionW = con->pOptions + i;
2395 INTERNET_PER_CONN_OPTIONA *optionA = conA->pOptions + i;
2397 switch (optionW->dwOption) {
2398 case INTERNET_PER_CONN_FLAGS:
2399 if(pi.proxyEnabled)
2400 optionW->Value.dwValue = PROXY_TYPE_PROXY;
2401 else
2402 optionW->Value.dwValue = PROXY_TYPE_DIRECT;
2403 break;
2405 case INTERNET_PER_CONN_PROXY_SERVER:
2406 if (unicode)
2407 optionW->Value.pszValue = heap_strdupW(pi.proxy);
2408 else
2409 optionA->Value.pszValue = heap_strdupWtoA(pi.proxy);
2410 break;
2412 case INTERNET_PER_CONN_PROXY_BYPASS:
2413 if (unicode)
2414 optionW->Value.pszValue = heap_strdupW(pi.proxyBypass);
2415 else
2416 optionA->Value.pszValue = heap_strdupWtoA(pi.proxyBypass);
2417 break;
2419 case INTERNET_PER_CONN_AUTOCONFIG_URL:
2420 case INTERNET_PER_CONN_AUTODISCOVERY_FLAGS:
2421 case INTERNET_PER_CONN_AUTOCONFIG_SECONDARY_URL:
2422 case INTERNET_PER_CONN_AUTOCONFIG_RELOAD_DELAY_MINS:
2423 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_TIME:
2424 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_URL:
2425 FIXME("Unhandled dwOption %d\n", optionW->dwOption);
2426 memset(&optionW->Value, 0, sizeof(optionW->Value));
2427 break;
2429 default:
2430 FIXME("Unknown dwOption %d\n", optionW->dwOption);
2431 res = ERROR_INVALID_PARAMETER;
2432 break;
2435 FreeProxyInfo(&pi);
2437 return res;
2439 case INTERNET_OPTION_USER_AGENT:
2440 return ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2441 case INTERNET_OPTION_POLICY:
2442 return ERROR_INVALID_PARAMETER;
2443 case INTERNET_OPTION_CONTEXT_VALUE:
2445 if (!hdr)
2446 return ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2447 if (!size)
2448 return ERROR_INVALID_PARAMETER;
2450 if (*size < sizeof(DWORD_PTR))
2452 *size = sizeof(DWORD_PTR);
2453 return ERROR_INSUFFICIENT_BUFFER;
2455 if (!buffer)
2456 return ERROR_INVALID_PARAMETER;
2458 *(DWORD_PTR *)buffer = hdr->dwContext;
2459 *size = sizeof(DWORD_PTR);
2460 return ERROR_SUCCESS;
2464 FIXME("Stub for %d\n", option);
2465 return ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2468 /***********************************************************************
2469 * InternetQueryOptionW (WININET.@)
2471 * Queries an options on the specified handle
2473 * RETURNS
2474 * TRUE on success
2475 * FALSE on failure
2478 BOOL WINAPI InternetQueryOptionW(HINTERNET hInternet, DWORD dwOption,
2479 LPVOID lpBuffer, LPDWORD lpdwBufferLength)
2481 object_header_t *hdr;
2482 DWORD res = ERROR_INVALID_HANDLE;
2484 TRACE("%p %d %p %p\n", hInternet, dwOption, lpBuffer, lpdwBufferLength);
2486 if(hInternet) {
2487 hdr = get_handle_object(hInternet);
2488 if (hdr) {
2489 res = hdr->vtbl->QueryOption(hdr, dwOption, lpBuffer, lpdwBufferLength, TRUE);
2490 WININET_Release(hdr);
2492 }else {
2493 res = INET_QueryOption(NULL, dwOption, lpBuffer, lpdwBufferLength, TRUE);
2496 if(res != ERROR_SUCCESS)
2497 SetLastError(res);
2498 return res == ERROR_SUCCESS;
2501 /***********************************************************************
2502 * InternetQueryOptionA (WININET.@)
2504 * Queries an options on the specified handle
2506 * RETURNS
2507 * TRUE on success
2508 * FALSE on failure
2511 BOOL WINAPI InternetQueryOptionA(HINTERNET hInternet, DWORD dwOption,
2512 LPVOID lpBuffer, LPDWORD lpdwBufferLength)
2514 object_header_t *hdr;
2515 DWORD res = ERROR_INVALID_HANDLE;
2517 TRACE("%p %d %p %p\n", hInternet, dwOption, lpBuffer, lpdwBufferLength);
2519 if(hInternet) {
2520 hdr = get_handle_object(hInternet);
2521 if (hdr) {
2522 res = hdr->vtbl->QueryOption(hdr, dwOption, lpBuffer, lpdwBufferLength, FALSE);
2523 WININET_Release(hdr);
2525 }else {
2526 res = INET_QueryOption(NULL, dwOption, lpBuffer, lpdwBufferLength, FALSE);
2529 if(res != ERROR_SUCCESS)
2530 SetLastError(res);
2531 return res == ERROR_SUCCESS;
2534 DWORD INET_SetOption(object_header_t *hdr, DWORD option, void *buf, DWORD size)
2536 switch(option) {
2537 case INTERNET_OPTION_CALLBACK:
2538 WARN("Not settable option %u\n", option);
2539 return ERROR_INTERNET_OPTION_NOT_SETTABLE;
2540 case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2541 case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2542 WARN("Called on global option %u\n", option);
2543 return ERROR_INTERNET_INVALID_OPERATION;
2546 return ERROR_INTERNET_INVALID_OPTION;
2549 static DWORD set_global_option(DWORD option, void *buf, DWORD size)
2551 switch(option) {
2552 case INTERNET_OPTION_CALLBACK:
2553 WARN("Not global option %u\n", option);
2554 return ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2556 case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2557 TRACE("INTERNET_OPTION_MAX_CONNS_PER_SERVER\n");
2559 if(size != sizeof(max_conns))
2560 return ERROR_INTERNET_BAD_OPTION_LENGTH;
2561 if(!*(ULONG*)buf)
2562 return ERROR_BAD_ARGUMENTS;
2564 max_conns = *(ULONG*)buf;
2565 return ERROR_SUCCESS;
2567 case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2568 TRACE("INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER\n");
2570 if(size != sizeof(max_1_0_conns))
2571 return ERROR_INTERNET_BAD_OPTION_LENGTH;
2572 if(!*(ULONG*)buf)
2573 return ERROR_BAD_ARGUMENTS;
2575 max_1_0_conns = *(ULONG*)buf;
2576 return ERROR_SUCCESS;
2579 return ERROR_INTERNET_INVALID_OPTION;
2582 /***********************************************************************
2583 * InternetSetOptionW (WININET.@)
2585 * Sets an options on the specified handle
2587 * RETURNS
2588 * TRUE on success
2589 * FALSE on failure
2592 BOOL WINAPI InternetSetOptionW(HINTERNET hInternet, DWORD dwOption,
2593 LPVOID lpBuffer, DWORD dwBufferLength)
2595 object_header_t *lpwhh;
2596 BOOL ret = TRUE;
2597 DWORD res;
2599 TRACE("(%p %d %p %d)\n", hInternet, dwOption, lpBuffer, dwBufferLength);
2601 lpwhh = (object_header_t*) get_handle_object( hInternet );
2602 if(lpwhh)
2603 res = lpwhh->vtbl->SetOption(lpwhh, dwOption, lpBuffer, dwBufferLength);
2604 else
2605 res = set_global_option(dwOption, lpBuffer, dwBufferLength);
2607 if(res != ERROR_INTERNET_INVALID_OPTION) {
2608 if(lpwhh)
2609 WININET_Release(lpwhh);
2611 if(res != ERROR_SUCCESS)
2612 SetLastError(res);
2614 return res == ERROR_SUCCESS;
2617 switch (dwOption)
2619 case INTERNET_OPTION_HTTP_VERSION:
2621 HTTP_VERSION_INFO* pVersion=(HTTP_VERSION_INFO*)lpBuffer;
2622 FIXME("Option INTERNET_OPTION_HTTP_VERSION(%d,%d): STUB\n",pVersion->dwMajorVersion,pVersion->dwMinorVersion);
2624 break;
2625 case INTERNET_OPTION_ERROR_MASK:
2627 if(!lpwhh) {
2628 SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2629 return FALSE;
2630 } else if(*(ULONG*)lpBuffer & (~(INTERNET_ERROR_MASK_INSERT_CDROM|
2631 INTERNET_ERROR_MASK_COMBINED_SEC_CERT|
2632 INTERNET_ERROR_MASK_LOGIN_FAILURE_DISPLAY_ENTITY_BODY))) {
2633 SetLastError(ERROR_INVALID_PARAMETER);
2634 ret = FALSE;
2635 } else if(dwBufferLength != sizeof(ULONG)) {
2636 SetLastError(ERROR_INTERNET_BAD_OPTION_LENGTH);
2637 ret = FALSE;
2638 } else
2639 lpwhh->ErrorMask = *(ULONG*)lpBuffer;
2641 break;
2642 case INTERNET_OPTION_PROXY:
2644 INTERNET_PROXY_INFOW *info = lpBuffer;
2646 if (!lpBuffer || dwBufferLength < sizeof(INTERNET_PROXY_INFOW))
2648 SetLastError(ERROR_INVALID_PARAMETER);
2649 return FALSE;
2651 if (!hInternet)
2653 EnterCriticalSection( &WININET_cs );
2654 free_global_proxy();
2655 global_proxy = heap_alloc( sizeof(proxyinfo_t) );
2656 if (global_proxy)
2658 if (info->dwAccessType == INTERNET_OPEN_TYPE_PROXY)
2660 global_proxy->proxyEnabled = 1;
2661 global_proxy->proxy = heap_strdupW( info->lpszProxy );
2662 global_proxy->proxyBypass = heap_strdupW( info->lpszProxyBypass );
2664 else
2666 global_proxy->proxyEnabled = 0;
2667 global_proxy->proxy = global_proxy->proxyBypass = NULL;
2670 LeaveCriticalSection( &WININET_cs );
2672 else
2674 /* In general, each type of object should handle
2675 * INTERNET_OPTION_PROXY directly. This FIXME ensures it doesn't
2676 * get silently dropped.
2678 FIXME("INTERNET_OPTION_PROXY unimplemented\n");
2679 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2680 ret = FALSE;
2682 break;
2684 case INTERNET_OPTION_CODEPAGE:
2686 ULONG codepage = *(ULONG *)lpBuffer;
2687 FIXME("Option INTERNET_OPTION_CODEPAGE (%d): STUB\n", codepage);
2689 break;
2690 case INTERNET_OPTION_REQUEST_PRIORITY:
2692 ULONG priority = *(ULONG *)lpBuffer;
2693 FIXME("Option INTERNET_OPTION_REQUEST_PRIORITY (%d): STUB\n", priority);
2695 break;
2696 case INTERNET_OPTION_CONNECT_TIMEOUT:
2698 ULONG connecttimeout = *(ULONG *)lpBuffer;
2699 FIXME("Option INTERNET_OPTION_CONNECT_TIMEOUT (%d): STUB\n", connecttimeout);
2701 break;
2702 case INTERNET_OPTION_DATA_RECEIVE_TIMEOUT:
2704 ULONG receivetimeout = *(ULONG *)lpBuffer;
2705 FIXME("Option INTERNET_OPTION_DATA_RECEIVE_TIMEOUT (%d): STUB\n", receivetimeout);
2707 break;
2708 case INTERNET_OPTION_RESET_URLCACHE_SESSION:
2709 FIXME("Option INTERNET_OPTION_RESET_URLCACHE_SESSION: STUB\n");
2710 break;
2711 case INTERNET_OPTION_END_BROWSER_SESSION:
2712 FIXME("Option INTERNET_OPTION_END_BROWSER_SESSION: STUB\n");
2713 break;
2714 case INTERNET_OPTION_CONNECTED_STATE:
2715 FIXME("Option INTERNET_OPTION_CONNECTED_STATE: STUB\n");
2716 break;
2717 case INTERNET_OPTION_DISABLE_PASSPORT_AUTH:
2718 TRACE("Option INTERNET_OPTION_DISABLE_PASSPORT_AUTH: harmless stub, since not enabled\n");
2719 break;
2720 case INTERNET_OPTION_SEND_TIMEOUT:
2721 case INTERNET_OPTION_RECEIVE_TIMEOUT:
2722 case INTERNET_OPTION_DATA_SEND_TIMEOUT:
2724 ULONG timeout = *(ULONG *)lpBuffer;
2725 FIXME("INTERNET_OPTION_SEND/RECEIVE_TIMEOUT/DATA_SEND_TIMEOUT %d\n", timeout);
2726 break;
2728 case INTERNET_OPTION_CONNECT_RETRIES:
2730 ULONG retries = *(ULONG *)lpBuffer;
2731 FIXME("INTERNET_OPTION_CONNECT_RETRIES %d\n", retries);
2732 break;
2734 case INTERNET_OPTION_CONTEXT_VALUE:
2736 if (!lpwhh)
2738 SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2739 return FALSE;
2741 if (!lpBuffer || dwBufferLength != sizeof(DWORD_PTR))
2743 SetLastError(ERROR_INVALID_PARAMETER);
2744 ret = FALSE;
2746 else
2747 lpwhh->dwContext = *(DWORD_PTR *)lpBuffer;
2748 break;
2750 case INTERNET_OPTION_SECURITY_FLAGS:
2751 FIXME("Option INTERNET_OPTION_SECURITY_FLAGS; STUB\n");
2752 break;
2753 case INTERNET_OPTION_DISABLE_AUTODIAL:
2754 FIXME("Option INTERNET_OPTION_DISABLE_AUTODIAL; STUB\n");
2755 break;
2756 case INTERNET_OPTION_HTTP_DECODING:
2757 FIXME("INTERNET_OPTION_HTTP_DECODING; STUB\n");
2758 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2759 ret = FALSE;
2760 break;
2761 case INTERNET_OPTION_COOKIES_3RD_PARTY:
2762 FIXME("INTERNET_OPTION_COOKIES_3RD_PARTY; STUB\n");
2763 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2764 ret = FALSE;
2765 break;
2766 case INTERNET_OPTION_SEND_UTF8_SERVERNAME_TO_PROXY:
2767 FIXME("INTERNET_OPTION_SEND_UTF8_SERVERNAME_TO_PROXY; STUB\n");
2768 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2769 ret = FALSE;
2770 break;
2771 case INTERNET_OPTION_CODEPAGE_PATH:
2772 FIXME("INTERNET_OPTION_CODEPAGE_PATH; STUB\n");
2773 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2774 ret = FALSE;
2775 break;
2776 case INTERNET_OPTION_CODEPAGE_EXTRA:
2777 FIXME("INTERNET_OPTION_CODEPAGE_EXTRA; STUB\n");
2778 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2779 ret = FALSE;
2780 break;
2781 case INTERNET_OPTION_IDN:
2782 FIXME("INTERNET_OPTION_IDN; STUB\n");
2783 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2784 ret = FALSE;
2785 break;
2786 case INTERNET_OPTION_POLICY:
2787 SetLastError(ERROR_INVALID_PARAMETER);
2788 ret = FALSE;
2789 break;
2790 case INTERNET_OPTION_PER_CONNECTION_OPTION: {
2791 INTERNET_PER_CONN_OPTION_LISTW *con = lpBuffer;
2792 LONG res;
2793 int i;
2794 proxyinfo_t pi;
2796 INTERNET_LoadProxySettings(&pi);
2798 for (i = 0; i < con->dwOptionCount; i++) {
2799 INTERNET_PER_CONN_OPTIONW *option = con->pOptions + i;
2801 switch (option->dwOption) {
2802 case INTERNET_PER_CONN_PROXY_SERVER:
2803 heap_free(pi.proxy);
2804 pi.proxy = heap_strdupW(option->Value.pszValue);
2805 break;
2807 case INTERNET_PER_CONN_FLAGS:
2808 if(option->Value.dwValue & PROXY_TYPE_PROXY)
2809 pi.proxyEnabled = 1;
2810 else
2812 if(option->Value.dwValue != PROXY_TYPE_DIRECT)
2813 FIXME("Unhandled flags: 0x%x\n", option->Value.dwValue);
2814 pi.proxyEnabled = 0;
2816 break;
2818 case INTERNET_PER_CONN_AUTOCONFIG_URL:
2819 case INTERNET_PER_CONN_AUTODISCOVERY_FLAGS:
2820 case INTERNET_PER_CONN_AUTOCONFIG_SECONDARY_URL:
2821 case INTERNET_PER_CONN_AUTOCONFIG_RELOAD_DELAY_MINS:
2822 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_TIME:
2823 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_URL:
2824 case INTERNET_PER_CONN_PROXY_BYPASS:
2825 FIXME("Unhandled dwOption %d\n", option->dwOption);
2826 break;
2828 default:
2829 FIXME("Unknown dwOption %d\n", option->dwOption);
2830 SetLastError(ERROR_INVALID_PARAMETER);
2831 break;
2835 if ((res = INTERNET_SaveProxySettings(&pi)))
2836 SetLastError(res);
2838 FreeProxyInfo(&pi);
2840 ret = (res == ERROR_SUCCESS);
2841 break;
2843 default:
2844 FIXME("Option %d STUB\n",dwOption);
2845 SetLastError(ERROR_INTERNET_INVALID_OPTION);
2846 ret = FALSE;
2847 break;
2850 if(lpwhh)
2851 WININET_Release( lpwhh );
2853 return ret;
2857 /***********************************************************************
2858 * InternetSetOptionA (WININET.@)
2860 * Sets an options on the specified handle.
2862 * RETURNS
2863 * TRUE on success
2864 * FALSE on failure
2867 BOOL WINAPI InternetSetOptionA(HINTERNET hInternet, DWORD dwOption,
2868 LPVOID lpBuffer, DWORD dwBufferLength)
2870 LPVOID wbuffer;
2871 DWORD wlen;
2872 BOOL r;
2874 switch( dwOption )
2876 case INTERNET_OPTION_PROXY:
2878 LPINTERNET_PROXY_INFOA pi = (LPINTERNET_PROXY_INFOA) lpBuffer;
2879 LPINTERNET_PROXY_INFOW piw;
2880 DWORD proxlen, prbylen;
2881 LPWSTR prox, prby;
2883 proxlen = MultiByteToWideChar( CP_ACP, 0, pi->lpszProxy, -1, NULL, 0);
2884 prbylen= MultiByteToWideChar( CP_ACP, 0, pi->lpszProxyBypass, -1, NULL, 0);
2885 wlen = sizeof(*piw) + proxlen + prbylen;
2886 wbuffer = heap_alloc(wlen*sizeof(WCHAR) );
2887 piw = (LPINTERNET_PROXY_INFOW) wbuffer;
2888 piw->dwAccessType = pi->dwAccessType;
2889 prox = (LPWSTR) &piw[1];
2890 prby = &prox[proxlen+1];
2891 MultiByteToWideChar( CP_ACP, 0, pi->lpszProxy, -1, prox, proxlen);
2892 MultiByteToWideChar( CP_ACP, 0, pi->lpszProxyBypass, -1, prby, prbylen);
2893 piw->lpszProxy = prox;
2894 piw->lpszProxyBypass = prby;
2896 break;
2897 case INTERNET_OPTION_USER_AGENT:
2898 case INTERNET_OPTION_USERNAME:
2899 case INTERNET_OPTION_PASSWORD:
2900 wlen = MultiByteToWideChar( CP_ACP, 0, lpBuffer, dwBufferLength,
2901 NULL, 0 );
2902 wbuffer = heap_alloc(wlen*sizeof(WCHAR) );
2903 MultiByteToWideChar( CP_ACP, 0, lpBuffer, dwBufferLength,
2904 wbuffer, wlen );
2905 break;
2906 case INTERNET_OPTION_PER_CONNECTION_OPTION: {
2907 int i;
2908 INTERNET_PER_CONN_OPTION_LISTW *listW;
2909 INTERNET_PER_CONN_OPTION_LISTA *listA = lpBuffer;
2910 wlen = sizeof(INTERNET_PER_CONN_OPTION_LISTW);
2911 wbuffer = heap_alloc(wlen);
2912 listW = wbuffer;
2914 listW->dwSize = sizeof(INTERNET_PER_CONN_OPTION_LISTW);
2915 if (listA->pszConnection)
2917 wlen = MultiByteToWideChar( CP_ACP, 0, listA->pszConnection, -1, NULL, 0 );
2918 listW->pszConnection = heap_alloc(wlen*sizeof(WCHAR));
2919 MultiByteToWideChar( CP_ACP, 0, listA->pszConnection, -1, listW->pszConnection, wlen );
2921 else
2922 listW->pszConnection = NULL;
2923 listW->dwOptionCount = listA->dwOptionCount;
2924 listW->dwOptionError = listA->dwOptionError;
2925 listW->pOptions = heap_alloc(sizeof(INTERNET_PER_CONN_OPTIONW) * listA->dwOptionCount);
2927 for (i = 0; i < listA->dwOptionCount; ++i) {
2928 INTERNET_PER_CONN_OPTIONA *optA = listA->pOptions + i;
2929 INTERNET_PER_CONN_OPTIONW *optW = listW->pOptions + i;
2931 optW->dwOption = optA->dwOption;
2933 switch (optA->dwOption) {
2934 case INTERNET_PER_CONN_AUTOCONFIG_URL:
2935 case INTERNET_PER_CONN_PROXY_BYPASS:
2936 case INTERNET_PER_CONN_PROXY_SERVER:
2937 case INTERNET_PER_CONN_AUTOCONFIG_SECONDARY_URL:
2938 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_URL:
2939 if (optA->Value.pszValue)
2941 wlen = MultiByteToWideChar( CP_ACP, 0, optA->Value.pszValue, -1, NULL, 0 );
2942 optW->Value.pszValue = heap_alloc(wlen*sizeof(WCHAR));
2943 MultiByteToWideChar( CP_ACP, 0, optA->Value.pszValue, -1, optW->Value.pszValue, wlen );
2945 else
2946 optW->Value.pszValue = NULL;
2947 break;
2948 case INTERNET_PER_CONN_AUTODISCOVERY_FLAGS:
2949 case INTERNET_PER_CONN_FLAGS:
2950 case INTERNET_PER_CONN_AUTOCONFIG_RELOAD_DELAY_MINS:
2951 optW->Value.dwValue = optA->Value.dwValue;
2952 break;
2953 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_TIME:
2954 optW->Value.ftValue = optA->Value.ftValue;
2955 break;
2956 default:
2957 WARN("Unknown PER_CONN dwOption: %d, guessing at conversion to Wide\n", optA->dwOption);
2958 optW->Value.dwValue = optA->Value.dwValue;
2959 break;
2963 break;
2964 default:
2965 wbuffer = lpBuffer;
2966 wlen = dwBufferLength;
2969 r = InternetSetOptionW(hInternet,dwOption, wbuffer, wlen);
2971 if( lpBuffer != wbuffer )
2973 if (dwOption == INTERNET_OPTION_PER_CONNECTION_OPTION)
2975 INTERNET_PER_CONN_OPTION_LISTW *list = wbuffer;
2976 int i;
2977 for (i = 0; i < list->dwOptionCount; ++i) {
2978 INTERNET_PER_CONN_OPTIONW *opt = list->pOptions + i;
2979 switch (opt->dwOption) {
2980 case INTERNET_PER_CONN_AUTOCONFIG_URL:
2981 case INTERNET_PER_CONN_PROXY_BYPASS:
2982 case INTERNET_PER_CONN_PROXY_SERVER:
2983 case INTERNET_PER_CONN_AUTOCONFIG_SECONDARY_URL:
2984 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_URL:
2985 heap_free( opt->Value.pszValue );
2986 break;
2987 default:
2988 break;
2991 heap_free( list->pOptions );
2993 heap_free( wbuffer );
2996 return r;
3000 /***********************************************************************
3001 * InternetSetOptionExA (WININET.@)
3003 BOOL WINAPI InternetSetOptionExA(HINTERNET hInternet, DWORD dwOption,
3004 LPVOID lpBuffer, DWORD dwBufferLength, DWORD dwFlags)
3006 FIXME("Flags %08x ignored\n", dwFlags);
3007 return InternetSetOptionA( hInternet, dwOption, lpBuffer, dwBufferLength );
3010 /***********************************************************************
3011 * InternetSetOptionExW (WININET.@)
3013 BOOL WINAPI InternetSetOptionExW(HINTERNET hInternet, DWORD dwOption,
3014 LPVOID lpBuffer, DWORD dwBufferLength, DWORD dwFlags)
3016 FIXME("Flags %08x ignored\n", dwFlags);
3017 if( dwFlags & ~ISO_VALID_FLAGS )
3019 SetLastError( ERROR_INVALID_PARAMETER );
3020 return FALSE;
3022 return InternetSetOptionW( hInternet, dwOption, lpBuffer, dwBufferLength );
3025 static const WCHAR WININET_wkday[7][4] =
3026 { { 'S','u','n', 0 }, { 'M','o','n', 0 }, { 'T','u','e', 0 }, { 'W','e','d', 0 },
3027 { 'T','h','u', 0 }, { 'F','r','i', 0 }, { 'S','a','t', 0 } };
3028 static const WCHAR WININET_month[12][4] =
3029 { { 'J','a','n', 0 }, { 'F','e','b', 0 }, { 'M','a','r', 0 }, { 'A','p','r', 0 },
3030 { 'M','a','y', 0 }, { 'J','u','n', 0 }, { 'J','u','l', 0 }, { 'A','u','g', 0 },
3031 { 'S','e','p', 0 }, { 'O','c','t', 0 }, { 'N','o','v', 0 }, { 'D','e','c', 0 } };
3033 /***********************************************************************
3034 * InternetTimeFromSystemTimeA (WININET.@)
3036 BOOL WINAPI InternetTimeFromSystemTimeA( const SYSTEMTIME* time, DWORD format, LPSTR string, DWORD size )
3038 BOOL ret;
3039 WCHAR stringW[INTERNET_RFC1123_BUFSIZE];
3041 TRACE( "%p 0x%08x %p 0x%08x\n", time, format, string, size );
3043 if (!time || !string || format != INTERNET_RFC1123_FORMAT)
3045 SetLastError(ERROR_INVALID_PARAMETER);
3046 return FALSE;
3049 if (size < INTERNET_RFC1123_BUFSIZE * sizeof(*string))
3051 SetLastError(ERROR_INSUFFICIENT_BUFFER);
3052 return FALSE;
3055 ret = InternetTimeFromSystemTimeW( time, format, stringW, sizeof(stringW) );
3056 if (ret) WideCharToMultiByte( CP_ACP, 0, stringW, -1, string, size, NULL, NULL );
3058 return ret;
3061 /***********************************************************************
3062 * InternetTimeFromSystemTimeW (WININET.@)
3064 BOOL WINAPI InternetTimeFromSystemTimeW( const SYSTEMTIME* time, DWORD format, LPWSTR string, DWORD size )
3066 static const WCHAR date[] =
3067 { '%','s',',',' ','%','0','2','d',' ','%','s',' ','%','4','d',' ','%','0',
3068 '2','d',':','%','0','2','d',':','%','0','2','d',' ','G','M','T', 0 };
3070 TRACE( "%p 0x%08x %p 0x%08x\n", time, format, string, size );
3072 if (!time || !string || format != INTERNET_RFC1123_FORMAT)
3074 SetLastError(ERROR_INVALID_PARAMETER);
3075 return FALSE;
3078 if (size < INTERNET_RFC1123_BUFSIZE * sizeof(*string))
3080 SetLastError(ERROR_INSUFFICIENT_BUFFER);
3081 return FALSE;
3084 sprintfW( string, date,
3085 WININET_wkday[time->wDayOfWeek],
3086 time->wDay,
3087 WININET_month[time->wMonth - 1],
3088 time->wYear,
3089 time->wHour,
3090 time->wMinute,
3091 time->wSecond );
3093 return TRUE;
3096 /***********************************************************************
3097 * InternetTimeToSystemTimeA (WININET.@)
3099 BOOL WINAPI InternetTimeToSystemTimeA( LPCSTR string, SYSTEMTIME* time, DWORD reserved )
3101 BOOL ret = FALSE;
3102 WCHAR *stringW;
3104 TRACE( "%s %p 0x%08x\n", debugstr_a(string), time, reserved );
3106 stringW = heap_strdupAtoW(string);
3107 if (stringW)
3109 ret = InternetTimeToSystemTimeW( stringW, time, reserved );
3110 heap_free( stringW );
3112 return ret;
3115 /***********************************************************************
3116 * InternetTimeToSystemTimeW (WININET.@)
3118 BOOL WINAPI InternetTimeToSystemTimeW( LPCWSTR string, SYSTEMTIME* time, DWORD reserved )
3120 unsigned int i;
3121 const WCHAR *s = string;
3122 WCHAR *end;
3124 TRACE( "%s %p 0x%08x\n", debugstr_w(string), time, reserved );
3126 if (!string || !time) return FALSE;
3128 /* Windows does this too */
3129 GetSystemTime( time );
3131 /* Convert an RFC1123 time such as 'Fri, 07 Jan 2005 12:06:35 GMT' into
3132 * a SYSTEMTIME structure.
3135 while (*s && !isalphaW( *s )) s++;
3136 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
3137 time->wDayOfWeek = 7;
3139 for (i = 0; i < 7; i++)
3141 if (toupperW( WININET_wkday[i][0] ) == toupperW( s[0] ) &&
3142 toupperW( WININET_wkday[i][1] ) == toupperW( s[1] ) &&
3143 toupperW( WININET_wkday[i][2] ) == toupperW( s[2] ) )
3145 time->wDayOfWeek = i;
3146 break;
3150 if (time->wDayOfWeek > 6) return TRUE;
3151 while (*s && !isdigitW( *s )) s++;
3152 time->wDay = strtolW( s, &end, 10 );
3153 s = end;
3155 while (*s && !isalphaW( *s )) s++;
3156 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
3157 time->wMonth = 0;
3159 for (i = 0; i < 12; i++)
3161 if (toupperW( WININET_month[i][0]) == toupperW( s[0] ) &&
3162 toupperW( WININET_month[i][1]) == toupperW( s[1] ) &&
3163 toupperW( WININET_month[i][2]) == toupperW( s[2] ) )
3165 time->wMonth = i + 1;
3166 break;
3169 if (time->wMonth == 0) return TRUE;
3171 while (*s && !isdigitW( *s )) s++;
3172 if (*s == '\0') return TRUE;
3173 time->wYear = strtolW( s, &end, 10 );
3174 s = end;
3176 while (*s && !isdigitW( *s )) s++;
3177 if (*s == '\0') return TRUE;
3178 time->wHour = strtolW( s, &end, 10 );
3179 s = end;
3181 while (*s && !isdigitW( *s )) s++;
3182 if (*s == '\0') return TRUE;
3183 time->wMinute = strtolW( s, &end, 10 );
3184 s = end;
3186 while (*s && !isdigitW( *s )) s++;
3187 if (*s == '\0') return TRUE;
3188 time->wSecond = strtolW( s, &end, 10 );
3189 s = end;
3191 time->wMilliseconds = 0;
3192 return TRUE;
3195 /***********************************************************************
3196 * InternetCheckConnectionW (WININET.@)
3198 * Pings a requested host to check internet connection
3200 * RETURNS
3201 * TRUE on success and FALSE on failure. If a failure then
3202 * ERROR_NOT_CONNECTED is placed into GetLastError
3205 BOOL WINAPI InternetCheckConnectionW( LPCWSTR lpszUrl, DWORD dwFlags, DWORD dwReserved )
3208 * this is a kludge which runs the resident ping program and reads the output.
3210 * Anyone have a better idea?
3213 BOOL rc = FALSE;
3214 static const CHAR ping[] = "ping -c 1 ";
3215 static const CHAR redirect[] = " >/dev/null 2>/dev/null";
3216 CHAR *command = NULL;
3217 WCHAR hostW[INTERNET_MAX_HOST_NAME_LENGTH];
3218 DWORD len;
3219 INTERNET_PORT port;
3220 int status = -1;
3222 FIXME("\n");
3225 * Crack or set the Address
3227 if (lpszUrl == NULL)
3230 * According to the doc we are supposed to use the ip for the next
3231 * server in the WnInet internal server database. I have
3232 * no idea what that is or how to get it.
3234 * So someone needs to implement this.
3236 FIXME("Unimplemented with URL of NULL\n");
3237 return TRUE;
3239 else
3241 URL_COMPONENTSW components;
3243 ZeroMemory(&components,sizeof(URL_COMPONENTSW));
3244 components.lpszHostName = (LPWSTR)hostW;
3245 components.dwHostNameLength = INTERNET_MAX_HOST_NAME_LENGTH;
3247 if (!InternetCrackUrlW(lpszUrl,0,0,&components))
3248 goto End;
3250 TRACE("host name : %s\n",debugstr_w(components.lpszHostName));
3251 port = components.nPort;
3252 TRACE("port: %d\n", port);
3255 if (dwFlags & FLAG_ICC_FORCE_CONNECTION)
3257 struct sockaddr_storage saddr;
3258 socklen_t sa_len = sizeof(saddr);
3259 int fd;
3261 if (!GetAddress(hostW, port, (struct sockaddr *)&saddr, &sa_len))
3262 goto End;
3263 fd = socket(saddr.ss_family, SOCK_STREAM, 0);
3264 if (fd != -1)
3266 if (connect(fd, (struct sockaddr *)&saddr, sa_len) == 0)
3267 rc = TRUE;
3268 close(fd);
3271 else
3274 * Build our ping command
3276 len = WideCharToMultiByte(CP_UNIXCP, 0, hostW, -1, NULL, 0, NULL, NULL);
3277 command = heap_alloc(strlen(ping)+len+strlen(redirect));
3278 strcpy(command,ping);
3279 WideCharToMultiByte(CP_UNIXCP, 0, hostW, -1, command+strlen(ping), len, NULL, NULL);
3280 strcat(command,redirect);
3282 TRACE("Ping command is : %s\n",command);
3284 status = system(command);
3286 TRACE("Ping returned a code of %i\n",status);
3288 /* Ping return code of 0 indicates success */
3289 if (status == 0)
3290 rc = TRUE;
3293 End:
3294 heap_free( command );
3295 if (rc == FALSE)
3296 INTERNET_SetLastError(ERROR_NOT_CONNECTED);
3298 return rc;
3302 /***********************************************************************
3303 * InternetCheckConnectionA (WININET.@)
3305 * Pings a requested host to check internet connection
3307 * RETURNS
3308 * TRUE on success and FALSE on failure. If a failure then
3309 * ERROR_NOT_CONNECTED is placed into GetLastError
3312 BOOL WINAPI InternetCheckConnectionA(LPCSTR lpszUrl, DWORD dwFlags, DWORD dwReserved)
3314 WCHAR *url = NULL;
3315 BOOL rc;
3317 if(lpszUrl) {
3318 url = heap_strdupAtoW(lpszUrl);
3319 if(!url)
3320 return FALSE;
3323 rc = InternetCheckConnectionW(url, dwFlags, dwReserved);
3325 heap_free(url);
3326 return rc;
3330 /**********************************************************
3331 * INTERNET_InternetOpenUrlW (internal)
3333 * Opens an URL
3335 * RETURNS
3336 * handle of connection or NULL on failure
3338 static HINTERNET INTERNET_InternetOpenUrlW(appinfo_t *hIC, LPCWSTR lpszUrl,
3339 LPCWSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext)
3341 URL_COMPONENTSW urlComponents;
3342 WCHAR protocol[INTERNET_MAX_SCHEME_LENGTH];
3343 WCHAR hostName[INTERNET_MAX_HOST_NAME_LENGTH];
3344 WCHAR userName[INTERNET_MAX_USER_NAME_LENGTH];
3345 WCHAR password[INTERNET_MAX_PASSWORD_LENGTH];
3346 WCHAR path[INTERNET_MAX_PATH_LENGTH];
3347 WCHAR extra[1024];
3348 HINTERNET client = NULL, client1 = NULL;
3349 DWORD res;
3351 TRACE("(%p, %s, %s, %08x, %08x, %08lx)\n", hIC, debugstr_w(lpszUrl), debugstr_w(lpszHeaders),
3352 dwHeadersLength, dwFlags, dwContext);
3354 urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
3355 urlComponents.lpszScheme = protocol;
3356 urlComponents.dwSchemeLength = INTERNET_MAX_SCHEME_LENGTH;
3357 urlComponents.lpszHostName = hostName;
3358 urlComponents.dwHostNameLength = INTERNET_MAX_HOST_NAME_LENGTH;
3359 urlComponents.lpszUserName = userName;
3360 urlComponents.dwUserNameLength = INTERNET_MAX_USER_NAME_LENGTH;
3361 urlComponents.lpszPassword = password;
3362 urlComponents.dwPasswordLength = INTERNET_MAX_PASSWORD_LENGTH;
3363 urlComponents.lpszUrlPath = path;
3364 urlComponents.dwUrlPathLength = INTERNET_MAX_PATH_LENGTH;
3365 urlComponents.lpszExtraInfo = extra;
3366 urlComponents.dwExtraInfoLength = 1024;
3367 if(!InternetCrackUrlW(lpszUrl, strlenW(lpszUrl), 0, &urlComponents))
3368 return NULL;
3369 switch(urlComponents.nScheme) {
3370 case INTERNET_SCHEME_FTP:
3371 if(urlComponents.nPort == 0)
3372 urlComponents.nPort = INTERNET_DEFAULT_FTP_PORT;
3373 client = FTP_Connect(hIC, hostName, urlComponents.nPort,
3374 userName, password, dwFlags, dwContext, INET_OPENURL);
3375 if(client == NULL)
3376 break;
3377 client1 = FtpOpenFileW(client, path, GENERIC_READ, dwFlags, dwContext);
3378 if(client1 == NULL) {
3379 InternetCloseHandle(client);
3380 break;
3382 break;
3384 case INTERNET_SCHEME_HTTP:
3385 case INTERNET_SCHEME_HTTPS: {
3386 static const WCHAR szStars[] = { '*','/','*', 0 };
3387 LPCWSTR accept[2] = { szStars, NULL };
3388 if(urlComponents.nPort == 0) {
3389 if(urlComponents.nScheme == INTERNET_SCHEME_HTTP)
3390 urlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
3391 else
3392 urlComponents.nPort = INTERNET_DEFAULT_HTTPS_PORT;
3394 if (urlComponents.nScheme == INTERNET_SCHEME_HTTPS) dwFlags |= INTERNET_FLAG_SECURE;
3396 /* FIXME: should use pointers, not handles, as handles are not thread-safe */
3397 res = HTTP_Connect(hIC, hostName, urlComponents.nPort,
3398 userName, password, dwFlags, dwContext, INET_OPENURL, &client);
3399 if(res != ERROR_SUCCESS) {
3400 INTERNET_SetLastError(res);
3401 break;
3404 if (urlComponents.dwExtraInfoLength) {
3405 WCHAR *path_extra;
3406 DWORD len = urlComponents.dwUrlPathLength + urlComponents.dwExtraInfoLength + 1;
3408 if (!(path_extra = heap_alloc(len * sizeof(WCHAR))))
3410 InternetCloseHandle(client);
3411 break;
3413 strcpyW(path_extra, urlComponents.lpszUrlPath);
3414 strcatW(path_extra, urlComponents.lpszExtraInfo);
3415 client1 = HttpOpenRequestW(client, NULL, path_extra, NULL, NULL, accept, dwFlags, dwContext);
3416 heap_free(path_extra);
3418 else
3419 client1 = HttpOpenRequestW(client, NULL, path, NULL, NULL, accept, dwFlags, dwContext);
3421 if(client1 == NULL) {
3422 InternetCloseHandle(client);
3423 break;
3425 HttpAddRequestHeadersW(client1, lpszHeaders, dwHeadersLength, HTTP_ADDREQ_FLAG_ADD);
3426 if (!HttpSendRequestW(client1, NULL, 0, NULL, 0) &&
3427 GetLastError() != ERROR_IO_PENDING) {
3428 InternetCloseHandle(client1);
3429 client1 = NULL;
3430 break;
3433 case INTERNET_SCHEME_GOPHER:
3434 /* gopher doesn't seem to be implemented in wine, but it's supposed
3435 * to be supported by InternetOpenUrlA. */
3436 default:
3437 SetLastError(ERROR_INTERNET_UNRECOGNIZED_SCHEME);
3438 break;
3441 TRACE(" %p <--\n", client1);
3443 return client1;
3446 /**********************************************************
3447 * InternetOpenUrlW (WININET.@)
3449 * Opens an URL
3451 * RETURNS
3452 * handle of connection or NULL on failure
3454 static void AsyncInternetOpenUrlProc(WORKREQUEST *workRequest)
3456 struct WORKREQ_INTERNETOPENURLW const *req = &workRequest->u.InternetOpenUrlW;
3457 appinfo_t *hIC = (appinfo_t*) workRequest->hdr;
3459 TRACE("%p\n", hIC);
3461 INTERNET_InternetOpenUrlW(hIC, req->lpszUrl,
3462 req->lpszHeaders, req->dwHeadersLength, req->dwFlags, req->dwContext);
3463 heap_free(req->lpszUrl);
3464 heap_free(req->lpszHeaders);
3467 HINTERNET WINAPI InternetOpenUrlW(HINTERNET hInternet, LPCWSTR lpszUrl,
3468 LPCWSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext)
3470 HINTERNET ret = NULL;
3471 appinfo_t *hIC = NULL;
3473 if (TRACE_ON(wininet)) {
3474 TRACE("(%p, %s, %s, %08x, %08x, %08lx)\n", hInternet, debugstr_w(lpszUrl), debugstr_w(lpszHeaders),
3475 dwHeadersLength, dwFlags, dwContext);
3476 TRACE(" flags :");
3477 dump_INTERNET_FLAGS(dwFlags);
3480 if (!lpszUrl)
3482 SetLastError(ERROR_INVALID_PARAMETER);
3483 goto lend;
3486 hIC = (appinfo_t*)get_handle_object( hInternet );
3487 if (NULL == hIC || hIC->hdr.htype != WH_HINIT) {
3488 SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
3489 goto lend;
3492 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC) {
3493 WORKREQUEST workRequest;
3494 struct WORKREQ_INTERNETOPENURLW *req;
3496 workRequest.asyncproc = AsyncInternetOpenUrlProc;
3497 workRequest.hdr = WININET_AddRef( &hIC->hdr );
3498 req = &workRequest.u.InternetOpenUrlW;
3499 req->lpszUrl = heap_strdupW(lpszUrl);
3500 req->lpszHeaders = heap_strdupW(lpszHeaders);
3501 req->dwHeadersLength = dwHeadersLength;
3502 req->dwFlags = dwFlags;
3503 req->dwContext = dwContext;
3505 INTERNET_AsyncCall(&workRequest);
3507 * This is from windows.
3509 SetLastError(ERROR_IO_PENDING);
3510 } else {
3511 ret = INTERNET_InternetOpenUrlW(hIC, lpszUrl, lpszHeaders, dwHeadersLength, dwFlags, dwContext);
3514 lend:
3515 if( hIC )
3516 WININET_Release( &hIC->hdr );
3517 TRACE(" %p <--\n", ret);
3519 return ret;
3522 /**********************************************************
3523 * InternetOpenUrlA (WININET.@)
3525 * Opens an URL
3527 * RETURNS
3528 * handle of connection or NULL on failure
3530 HINTERNET WINAPI InternetOpenUrlA(HINTERNET hInternet, LPCSTR lpszUrl,
3531 LPCSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext)
3533 HINTERNET rc = NULL;
3534 DWORD lenHeaders = 0;
3535 LPWSTR szUrl = NULL;
3536 LPWSTR szHeaders = NULL;
3538 TRACE("\n");
3540 if(lpszUrl) {
3541 szUrl = heap_strdupAtoW(lpszUrl);
3542 if(!szUrl)
3543 return NULL;
3546 if(lpszHeaders) {
3547 lenHeaders = MultiByteToWideChar(CP_ACP, 0, lpszHeaders, dwHeadersLength, NULL, 0 );
3548 szHeaders = heap_alloc(lenHeaders*sizeof(WCHAR));
3549 if(!szHeaders) {
3550 heap_free(szUrl);
3551 return NULL;
3553 MultiByteToWideChar(CP_ACP, 0, lpszHeaders, dwHeadersLength, szHeaders, lenHeaders);
3556 rc = InternetOpenUrlW(hInternet, szUrl, szHeaders,
3557 lenHeaders, dwFlags, dwContext);
3559 heap_free(szUrl);
3560 heap_free(szHeaders);
3561 return rc;
3565 static LPWITHREADERROR INTERNET_AllocThreadError(void)
3567 LPWITHREADERROR lpwite = heap_alloc(sizeof(*lpwite));
3569 if (lpwite)
3571 lpwite->dwError = 0;
3572 lpwite->response[0] = '\0';
3575 if (!TlsSetValue(g_dwTlsErrIndex, lpwite))
3577 heap_free(lpwite);
3578 return NULL;
3580 return lpwite;
3584 /***********************************************************************
3585 * INTERNET_SetLastError (internal)
3587 * Set last thread specific error
3589 * RETURNS
3592 void INTERNET_SetLastError(DWORD dwError)
3594 LPWITHREADERROR lpwite = TlsGetValue(g_dwTlsErrIndex);
3596 if (!lpwite)
3597 lpwite = INTERNET_AllocThreadError();
3599 SetLastError(dwError);
3600 if(lpwite)
3601 lpwite->dwError = dwError;
3605 /***********************************************************************
3606 * INTERNET_GetLastError (internal)
3608 * Get last thread specific error
3610 * RETURNS
3613 DWORD INTERNET_GetLastError(void)
3615 LPWITHREADERROR lpwite = TlsGetValue(g_dwTlsErrIndex);
3616 if (!lpwite) return 0;
3617 /* TlsGetValue clears last error, so set it again here */
3618 SetLastError(lpwite->dwError);
3619 return lpwite->dwError;
3623 /***********************************************************************
3624 * INTERNET_WorkerThreadFunc (internal)
3626 * Worker thread execution function
3628 * RETURNS
3631 static DWORD CALLBACK INTERNET_WorkerThreadFunc(LPVOID lpvParam)
3633 LPWORKREQUEST lpRequest = lpvParam;
3634 WORKREQUEST workRequest;
3636 TRACE("\n");
3638 workRequest = *lpRequest;
3639 heap_free(lpRequest);
3641 workRequest.asyncproc(&workRequest);
3642 WININET_Release( workRequest.hdr );
3644 if (g_dwTlsErrIndex != TLS_OUT_OF_INDEXES)
3646 heap_free(TlsGetValue(g_dwTlsErrIndex));
3647 TlsSetValue(g_dwTlsErrIndex, NULL);
3649 return TRUE;
3653 /***********************************************************************
3654 * INTERNET_AsyncCall (internal)
3656 * Retrieves work request from queue
3658 * RETURNS
3661 DWORD INTERNET_AsyncCall(LPWORKREQUEST lpWorkRequest)
3663 BOOL bSuccess;
3664 LPWORKREQUEST lpNewRequest;
3666 TRACE("\n");
3668 lpNewRequest = heap_alloc(sizeof(WORKREQUEST));
3669 if (!lpNewRequest)
3670 return ERROR_OUTOFMEMORY;
3672 *lpNewRequest = *lpWorkRequest;
3674 bSuccess = QueueUserWorkItem(INTERNET_WorkerThreadFunc, lpNewRequest, WT_EXECUTELONGFUNCTION);
3675 if (!bSuccess)
3677 heap_free(lpNewRequest);
3678 return ERROR_INTERNET_ASYNC_THREAD_FAILED;
3680 return ERROR_SUCCESS;
3684 /***********************************************************************
3685 * INTERNET_GetResponseBuffer (internal)
3687 * RETURNS
3690 LPSTR INTERNET_GetResponseBuffer(void)
3692 LPWITHREADERROR lpwite = TlsGetValue(g_dwTlsErrIndex);
3693 if (!lpwite)
3694 lpwite = INTERNET_AllocThreadError();
3695 TRACE("\n");
3696 return lpwite->response;
3699 /***********************************************************************
3700 * INTERNET_GetNextLine (internal)
3702 * Parse next line in directory string listing
3704 * RETURNS
3705 * Pointer to beginning of next line
3706 * NULL on failure
3710 LPSTR INTERNET_GetNextLine(INT nSocket, LPDWORD dwLen)
3712 struct pollfd pfd;
3713 BOOL bSuccess = FALSE;
3714 INT nRecv = 0;
3715 LPSTR lpszBuffer = INTERNET_GetResponseBuffer();
3717 TRACE("\n");
3719 pfd.fd = nSocket;
3720 pfd.events = POLLIN;
3722 while (nRecv < MAX_REPLY_LEN)
3724 if (poll(&pfd,1, RESPONSE_TIMEOUT * 1000) > 0)
3726 if (recv(nSocket, &lpszBuffer[nRecv], 1, 0) <= 0)
3728 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
3729 goto lend;
3732 if (lpszBuffer[nRecv] == '\n')
3734 bSuccess = TRUE;
3735 break;
3737 if (lpszBuffer[nRecv] != '\r')
3738 nRecv++;
3740 else
3742 INTERNET_SetLastError(ERROR_INTERNET_TIMEOUT);
3743 goto lend;
3747 lend:
3748 if (bSuccess)
3750 lpszBuffer[nRecv] = '\0';
3751 *dwLen = nRecv - 1;
3752 TRACE(":%d %s\n", nRecv, lpszBuffer);
3753 return lpszBuffer;
3755 else
3757 return NULL;
3761 /**********************************************************
3762 * InternetQueryDataAvailable (WININET.@)
3764 * Determines how much data is available to be read.
3766 * RETURNS
3767 * TRUE on success, FALSE if an error occurred. If
3768 * INTERNET_FLAG_ASYNC was specified in InternetOpen, and
3769 * no data is presently available, FALSE is returned with
3770 * the last error ERROR_IO_PENDING; a callback with status
3771 * INTERNET_STATUS_REQUEST_COMPLETE will be sent when more
3772 * data is available.
3774 BOOL WINAPI InternetQueryDataAvailable( HINTERNET hFile,
3775 LPDWORD lpdwNumberOfBytesAvailble,
3776 DWORD dwFlags, DWORD_PTR dwContext)
3778 object_header_t *hdr;
3779 DWORD res;
3781 TRACE("(%p %p %x %lx)\n", hFile, lpdwNumberOfBytesAvailble, dwFlags, dwContext);
3783 hdr = get_handle_object( hFile );
3784 if (!hdr) {
3785 SetLastError(ERROR_INVALID_HANDLE);
3786 return FALSE;
3789 if(hdr->vtbl->QueryDataAvailable) {
3790 res = hdr->vtbl->QueryDataAvailable(hdr, lpdwNumberOfBytesAvailble, dwFlags, dwContext);
3791 }else {
3792 WARN("wrong handle\n");
3793 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
3796 WININET_Release(hdr);
3798 if(res != ERROR_SUCCESS)
3799 SetLastError(res);
3800 return res == ERROR_SUCCESS;
3804 /***********************************************************************
3805 * InternetLockRequestFile (WININET.@)
3807 BOOL WINAPI InternetLockRequestFile( HINTERNET hInternet, HANDLE
3808 *lphLockReqHandle)
3810 FIXME("STUB\n");
3811 return FALSE;
3814 BOOL WINAPI InternetUnlockRequestFile( HANDLE hLockHandle)
3816 FIXME("STUB\n");
3817 return FALSE;
3821 /***********************************************************************
3822 * InternetAutodial (WININET.@)
3824 * On windows this function is supposed to dial the default internet
3825 * connection. We don't want to have Wine dial out to the internet so
3826 * we return TRUE by default. It might be nice to check if we are connected.
3828 * RETURNS
3829 * TRUE on success
3830 * FALSE on failure
3833 BOOL WINAPI InternetAutodial(DWORD dwFlags, HWND hwndParent)
3835 FIXME("STUB\n");
3837 /* Tell that we are connected to the internet. */
3838 return TRUE;
3841 /***********************************************************************
3842 * InternetAutodialHangup (WININET.@)
3844 * Hangs up a connection made with InternetAutodial
3846 * PARAM
3847 * dwReserved
3848 * RETURNS
3849 * TRUE on success
3850 * FALSE on failure
3853 BOOL WINAPI InternetAutodialHangup(DWORD dwReserved)
3855 FIXME("STUB\n");
3857 /* we didn't dial, we don't disconnect */
3858 return TRUE;
3861 /***********************************************************************
3862 * InternetCombineUrlA (WININET.@)
3864 * Combine a base URL with a relative URL
3866 * RETURNS
3867 * TRUE on success
3868 * FALSE on failure
3872 BOOL WINAPI InternetCombineUrlA(LPCSTR lpszBaseUrl, LPCSTR lpszRelativeUrl,
3873 LPSTR lpszBuffer, LPDWORD lpdwBufferLength,
3874 DWORD dwFlags)
3876 HRESULT hr=S_OK;
3878 TRACE("(%s, %s, %p, %p, 0x%08x)\n", debugstr_a(lpszBaseUrl), debugstr_a(lpszRelativeUrl), lpszBuffer, lpdwBufferLength, dwFlags);
3880 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
3881 dwFlags ^= ICU_NO_ENCODE;
3882 hr=UrlCombineA(lpszBaseUrl,lpszRelativeUrl,lpszBuffer,lpdwBufferLength,dwFlags);
3884 return (hr==S_OK);
3887 /***********************************************************************
3888 * InternetCombineUrlW (WININET.@)
3890 * Combine a base URL with a relative URL
3892 * RETURNS
3893 * TRUE on success
3894 * FALSE on failure
3898 BOOL WINAPI InternetCombineUrlW(LPCWSTR lpszBaseUrl, LPCWSTR lpszRelativeUrl,
3899 LPWSTR lpszBuffer, LPDWORD lpdwBufferLength,
3900 DWORD dwFlags)
3902 HRESULT hr=S_OK;
3904 TRACE("(%s, %s, %p, %p, 0x%08x)\n", debugstr_w(lpszBaseUrl), debugstr_w(lpszRelativeUrl), lpszBuffer, lpdwBufferLength, dwFlags);
3906 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
3907 dwFlags ^= ICU_NO_ENCODE;
3908 hr=UrlCombineW(lpszBaseUrl,lpszRelativeUrl,lpszBuffer,lpdwBufferLength,dwFlags);
3910 return (hr==S_OK);
3913 /* max port num is 65535 => 5 digits */
3914 #define MAX_WORD_DIGITS 5
3916 #define URL_GET_COMP_LENGTH(url, component) ((url)->dw##component##Length ? \
3917 (url)->dw##component##Length : strlenW((url)->lpsz##component))
3918 #define URL_GET_COMP_LENGTHA(url, component) ((url)->dw##component##Length ? \
3919 (url)->dw##component##Length : strlen((url)->lpsz##component))
3921 static BOOL url_uses_default_port(INTERNET_SCHEME nScheme, INTERNET_PORT nPort)
3923 if ((nScheme == INTERNET_SCHEME_HTTP) &&
3924 (nPort == INTERNET_DEFAULT_HTTP_PORT))
3925 return TRUE;
3926 if ((nScheme == INTERNET_SCHEME_HTTPS) &&
3927 (nPort == INTERNET_DEFAULT_HTTPS_PORT))
3928 return TRUE;
3929 if ((nScheme == INTERNET_SCHEME_FTP) &&
3930 (nPort == INTERNET_DEFAULT_FTP_PORT))
3931 return TRUE;
3932 if ((nScheme == INTERNET_SCHEME_GOPHER) &&
3933 (nPort == INTERNET_DEFAULT_GOPHER_PORT))
3934 return TRUE;
3936 if (nPort == INTERNET_INVALID_PORT_NUMBER)
3937 return TRUE;
3939 return FALSE;
3942 /* opaque urls do not fit into the standard url hierarchy and don't have
3943 * two following slashes */
3944 static inline BOOL scheme_is_opaque(INTERNET_SCHEME nScheme)
3946 return (nScheme != INTERNET_SCHEME_FTP) &&
3947 (nScheme != INTERNET_SCHEME_GOPHER) &&
3948 (nScheme != INTERNET_SCHEME_HTTP) &&
3949 (nScheme != INTERNET_SCHEME_HTTPS) &&
3950 (nScheme != INTERNET_SCHEME_FILE);
3953 static LPCWSTR INTERNET_GetSchemeString(INTERNET_SCHEME scheme)
3955 int index;
3956 if (scheme < INTERNET_SCHEME_FIRST)
3957 return NULL;
3958 index = scheme - INTERNET_SCHEME_FIRST;
3959 if (index >= sizeof(url_schemes)/sizeof(url_schemes[0]))
3960 return NULL;
3961 return (LPCWSTR)url_schemes[index];
3964 /* we can calculate using ansi strings because we're just
3965 * calculating string length, not size
3967 static BOOL calc_url_length(LPURL_COMPONENTSW lpUrlComponents,
3968 LPDWORD lpdwUrlLength)
3970 INTERNET_SCHEME nScheme;
3972 *lpdwUrlLength = 0;
3974 if (lpUrlComponents->lpszScheme)
3976 DWORD dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, Scheme);
3977 *lpdwUrlLength += dwLen;
3978 nScheme = GetInternetSchemeW(lpUrlComponents->lpszScheme, dwLen);
3980 else
3982 LPCWSTR scheme;
3984 nScheme = lpUrlComponents->nScheme;
3986 if (nScheme == INTERNET_SCHEME_DEFAULT)
3987 nScheme = INTERNET_SCHEME_HTTP;
3988 scheme = INTERNET_GetSchemeString(nScheme);
3989 *lpdwUrlLength += strlenW(scheme);
3992 (*lpdwUrlLength)++; /* ':' */
3993 if (!scheme_is_opaque(nScheme) || lpUrlComponents->lpszHostName)
3994 *lpdwUrlLength += strlen("//");
3996 if (lpUrlComponents->lpszUserName)
3998 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, UserName);
3999 *lpdwUrlLength += strlen("@");
4001 else
4003 if (lpUrlComponents->lpszPassword)
4005 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
4006 return FALSE;
4010 if (lpUrlComponents->lpszPassword)
4012 *lpdwUrlLength += strlen(":");
4013 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, Password);
4016 if (lpUrlComponents->lpszHostName)
4018 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, HostName);
4020 if (!url_uses_default_port(nScheme, lpUrlComponents->nPort))
4022 char szPort[MAX_WORD_DIGITS+1];
4024 sprintf(szPort, "%d", lpUrlComponents->nPort);
4025 *lpdwUrlLength += strlen(szPort);
4026 *lpdwUrlLength += strlen(":");
4029 if (lpUrlComponents->lpszUrlPath && *lpUrlComponents->lpszUrlPath != '/')
4030 (*lpdwUrlLength)++; /* '/' */
4033 if (lpUrlComponents->lpszUrlPath)
4034 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, UrlPath);
4036 if (lpUrlComponents->lpszExtraInfo)
4037 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, ExtraInfo);
4039 return TRUE;
4042 static void convert_urlcomp_atow(LPURL_COMPONENTSA lpUrlComponents, LPURL_COMPONENTSW urlCompW)
4044 INT len;
4046 ZeroMemory(urlCompW, sizeof(URL_COMPONENTSW));
4048 urlCompW->dwStructSize = sizeof(URL_COMPONENTSW);
4049 urlCompW->dwSchemeLength = lpUrlComponents->dwSchemeLength;
4050 urlCompW->nScheme = lpUrlComponents->nScheme;
4051 urlCompW->dwHostNameLength = lpUrlComponents->dwHostNameLength;
4052 urlCompW->nPort = lpUrlComponents->nPort;
4053 urlCompW->dwUserNameLength = lpUrlComponents->dwUserNameLength;
4054 urlCompW->dwPasswordLength = lpUrlComponents->dwPasswordLength;
4055 urlCompW->dwUrlPathLength = lpUrlComponents->dwUrlPathLength;
4056 urlCompW->dwExtraInfoLength = lpUrlComponents->dwExtraInfoLength;
4058 if (lpUrlComponents->lpszScheme)
4060 len = URL_GET_COMP_LENGTHA(lpUrlComponents, Scheme) + 1;
4061 urlCompW->lpszScheme = heap_alloc(len * sizeof(WCHAR));
4062 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszScheme,
4063 -1, urlCompW->lpszScheme, len);
4066 if (lpUrlComponents->lpszHostName)
4068 len = URL_GET_COMP_LENGTHA(lpUrlComponents, HostName) + 1;
4069 urlCompW->lpszHostName = heap_alloc(len * sizeof(WCHAR));
4070 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszHostName,
4071 -1, urlCompW->lpszHostName, len);
4074 if (lpUrlComponents->lpszUserName)
4076 len = URL_GET_COMP_LENGTHA(lpUrlComponents, UserName) + 1;
4077 urlCompW->lpszUserName = heap_alloc(len * sizeof(WCHAR));
4078 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszUserName,
4079 -1, urlCompW->lpszUserName, len);
4082 if (lpUrlComponents->lpszPassword)
4084 len = URL_GET_COMP_LENGTHA(lpUrlComponents, Password) + 1;
4085 urlCompW->lpszPassword = heap_alloc(len * sizeof(WCHAR));
4086 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszPassword,
4087 -1, urlCompW->lpszPassword, len);
4090 if (lpUrlComponents->lpszUrlPath)
4092 len = URL_GET_COMP_LENGTHA(lpUrlComponents, UrlPath) + 1;
4093 urlCompW->lpszUrlPath = heap_alloc(len * sizeof(WCHAR));
4094 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszUrlPath,
4095 -1, urlCompW->lpszUrlPath, len);
4098 if (lpUrlComponents->lpszExtraInfo)
4100 len = URL_GET_COMP_LENGTHA(lpUrlComponents, ExtraInfo) + 1;
4101 urlCompW->lpszExtraInfo = heap_alloc(len * sizeof(WCHAR));
4102 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszExtraInfo,
4103 -1, urlCompW->lpszExtraInfo, len);
4107 /***********************************************************************
4108 * InternetCreateUrlA (WININET.@)
4110 * See InternetCreateUrlW.
4112 BOOL WINAPI InternetCreateUrlA(LPURL_COMPONENTSA lpUrlComponents, DWORD dwFlags,
4113 LPSTR lpszUrl, LPDWORD lpdwUrlLength)
4115 BOOL ret;
4116 LPWSTR urlW = NULL;
4117 URL_COMPONENTSW urlCompW;
4119 TRACE("(%p,%d,%p,%p)\n", lpUrlComponents, dwFlags, lpszUrl, lpdwUrlLength);
4121 if (!lpUrlComponents || lpUrlComponents->dwStructSize != sizeof(URL_COMPONENTSW) || !lpdwUrlLength)
4123 SetLastError(ERROR_INVALID_PARAMETER);
4124 return FALSE;
4127 convert_urlcomp_atow(lpUrlComponents, &urlCompW);
4129 if (lpszUrl)
4130 urlW = heap_alloc(*lpdwUrlLength * sizeof(WCHAR));
4132 ret = InternetCreateUrlW(&urlCompW, dwFlags, urlW, lpdwUrlLength);
4134 if (!ret && (GetLastError() == ERROR_INSUFFICIENT_BUFFER))
4135 *lpdwUrlLength /= sizeof(WCHAR);
4137 /* on success, lpdwUrlLength points to the size of urlW in WCHARS
4138 * minus one, so add one to leave room for NULL terminator
4140 if (ret)
4141 WideCharToMultiByte(CP_ACP, 0, urlW, -1, lpszUrl, *lpdwUrlLength + 1, NULL, NULL);
4143 heap_free(urlCompW.lpszScheme);
4144 heap_free(urlCompW.lpszHostName);
4145 heap_free(urlCompW.lpszUserName);
4146 heap_free(urlCompW.lpszPassword);
4147 heap_free(urlCompW.lpszUrlPath);
4148 heap_free(urlCompW.lpszExtraInfo);
4149 heap_free(urlW);
4150 return ret;
4153 /***********************************************************************
4154 * InternetCreateUrlW (WININET.@)
4156 * Creates a URL from its component parts.
4158 * PARAMS
4159 * lpUrlComponents [I] URL Components.
4160 * dwFlags [I] Flags. See notes.
4161 * lpszUrl [I] Buffer in which to store the created URL.
4162 * lpdwUrlLength [I/O] On input, the length of the buffer pointed to by
4163 * lpszUrl in characters. On output, the number of bytes
4164 * required to store the URL including terminator.
4166 * NOTES
4168 * The dwFlags parameter can be zero or more of the following:
4169 *|ICU_ESCAPE - Generates escape sequences for unsafe characters in the path and extra info of the URL.
4171 * RETURNS
4172 * TRUE on success
4173 * FALSE on failure
4176 BOOL WINAPI InternetCreateUrlW(LPURL_COMPONENTSW lpUrlComponents, DWORD dwFlags,
4177 LPWSTR lpszUrl, LPDWORD lpdwUrlLength)
4179 DWORD dwLen;
4180 INTERNET_SCHEME nScheme;
4182 static const WCHAR slashSlashW[] = {'/','/'};
4183 static const WCHAR fmtW[] = {'%','u',0};
4185 TRACE("(%p,%d,%p,%p)\n", lpUrlComponents, dwFlags, lpszUrl, lpdwUrlLength);
4187 if (!lpUrlComponents || lpUrlComponents->dwStructSize != sizeof(URL_COMPONENTSW) || !lpdwUrlLength)
4189 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
4190 return FALSE;
4193 if (!calc_url_length(lpUrlComponents, &dwLen))
4194 return FALSE;
4196 if (!lpszUrl || *lpdwUrlLength < dwLen)
4198 *lpdwUrlLength = (dwLen + 1) * sizeof(WCHAR);
4199 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
4200 return FALSE;
4203 *lpdwUrlLength = dwLen;
4204 lpszUrl[0] = 0x00;
4206 dwLen = 0;
4208 if (lpUrlComponents->lpszScheme)
4210 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, Scheme);
4211 memcpy(lpszUrl, lpUrlComponents->lpszScheme, dwLen * sizeof(WCHAR));
4212 lpszUrl += dwLen;
4214 nScheme = GetInternetSchemeW(lpUrlComponents->lpszScheme, dwLen);
4216 else
4218 LPCWSTR scheme;
4219 nScheme = lpUrlComponents->nScheme;
4221 if (nScheme == INTERNET_SCHEME_DEFAULT)
4222 nScheme = INTERNET_SCHEME_HTTP;
4224 scheme = INTERNET_GetSchemeString(nScheme);
4225 dwLen = strlenW(scheme);
4226 memcpy(lpszUrl, scheme, dwLen * sizeof(WCHAR));
4227 lpszUrl += dwLen;
4230 /* all schemes are followed by at least a colon */
4231 *lpszUrl = ':';
4232 lpszUrl++;
4234 if (!scheme_is_opaque(nScheme) || lpUrlComponents->lpszHostName)
4236 memcpy(lpszUrl, slashSlashW, sizeof(slashSlashW));
4237 lpszUrl += sizeof(slashSlashW)/sizeof(slashSlashW[0]);
4240 if (lpUrlComponents->lpszUserName)
4242 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, UserName);
4243 memcpy(lpszUrl, lpUrlComponents->lpszUserName, dwLen * sizeof(WCHAR));
4244 lpszUrl += dwLen;
4246 if (lpUrlComponents->lpszPassword)
4248 *lpszUrl = ':';
4249 lpszUrl++;
4251 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, Password);
4252 memcpy(lpszUrl, lpUrlComponents->lpszPassword, dwLen * sizeof(WCHAR));
4253 lpszUrl += dwLen;
4256 *lpszUrl = '@';
4257 lpszUrl++;
4260 if (lpUrlComponents->lpszHostName)
4262 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, HostName);
4263 memcpy(lpszUrl, lpUrlComponents->lpszHostName, dwLen * sizeof(WCHAR));
4264 lpszUrl += dwLen;
4266 if (!url_uses_default_port(nScheme, lpUrlComponents->nPort))
4268 WCHAR szPort[MAX_WORD_DIGITS+1];
4270 sprintfW(szPort, fmtW, lpUrlComponents->nPort);
4271 *lpszUrl = ':';
4272 lpszUrl++;
4273 dwLen = strlenW(szPort);
4274 memcpy(lpszUrl, szPort, dwLen * sizeof(WCHAR));
4275 lpszUrl += dwLen;
4278 /* add slash between hostname and path if necessary */
4279 if (lpUrlComponents->lpszUrlPath && *lpUrlComponents->lpszUrlPath != '/')
4281 *lpszUrl = '/';
4282 lpszUrl++;
4286 if (lpUrlComponents->lpszUrlPath)
4288 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, UrlPath);
4289 memcpy(lpszUrl, lpUrlComponents->lpszUrlPath, dwLen * sizeof(WCHAR));
4290 lpszUrl += dwLen;
4293 if (lpUrlComponents->lpszExtraInfo)
4295 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, ExtraInfo);
4296 memcpy(lpszUrl, lpUrlComponents->lpszExtraInfo, dwLen * sizeof(WCHAR));
4297 lpszUrl += dwLen;
4300 *lpszUrl = '\0';
4302 return TRUE;
4305 /***********************************************************************
4306 * InternetConfirmZoneCrossingA (WININET.@)
4309 DWORD WINAPI InternetConfirmZoneCrossingA( HWND hWnd, LPSTR szUrlPrev, LPSTR szUrlNew, BOOL bPost )
4311 FIXME("(%p, %s, %s, %x) stub\n", hWnd, debugstr_a(szUrlPrev), debugstr_a(szUrlNew), bPost);
4312 return ERROR_SUCCESS;
4315 /***********************************************************************
4316 * InternetConfirmZoneCrossingW (WININET.@)
4319 DWORD WINAPI InternetConfirmZoneCrossingW( HWND hWnd, LPWSTR szUrlPrev, LPWSTR szUrlNew, BOOL bPost )
4321 FIXME("(%p, %s, %s, %x) stub\n", hWnd, debugstr_w(szUrlPrev), debugstr_w(szUrlNew), bPost);
4322 return ERROR_SUCCESS;
4325 static DWORD zone_preference = 3;
4327 /***********************************************************************
4328 * PrivacySetZonePreferenceW (WININET.@)
4330 DWORD WINAPI PrivacySetZonePreferenceW( DWORD zone, DWORD type, DWORD template, LPCWSTR preference )
4332 FIXME( "%x %x %x %s: stub\n", zone, type, template, debugstr_w(preference) );
4334 zone_preference = template;
4335 return 0;
4338 /***********************************************************************
4339 * PrivacyGetZonePreferenceW (WININET.@)
4341 DWORD WINAPI PrivacyGetZonePreferenceW( DWORD zone, DWORD type, LPDWORD template,
4342 LPWSTR preference, LPDWORD length )
4344 FIXME( "%x %x %p %p %p: stub\n", zone, type, template, preference, length );
4346 if (template) *template = zone_preference;
4347 return 0;
4350 DWORD WINAPI InternetDialA( HWND hwndParent, LPSTR lpszConnectoid, DWORD dwFlags,
4351 DWORD_PTR* lpdwConnection, DWORD dwReserved )
4353 FIXME("(%p, %p, 0x%08x, %p, 0x%08x) stub\n", hwndParent, lpszConnectoid, dwFlags,
4354 lpdwConnection, dwReserved);
4355 return ERROR_SUCCESS;
4358 DWORD WINAPI InternetDialW( HWND hwndParent, LPWSTR lpszConnectoid, DWORD dwFlags,
4359 DWORD_PTR* lpdwConnection, DWORD dwReserved )
4361 FIXME("(%p, %p, 0x%08x, %p, 0x%08x) stub\n", hwndParent, lpszConnectoid, dwFlags,
4362 lpdwConnection, dwReserved);
4363 return ERROR_SUCCESS;
4366 BOOL WINAPI InternetGoOnlineA( LPSTR lpszURL, HWND hwndParent, DWORD dwReserved )
4368 FIXME("(%s, %p, 0x%08x) stub\n", debugstr_a(lpszURL), hwndParent, dwReserved);
4369 return TRUE;
4372 BOOL WINAPI InternetGoOnlineW( LPWSTR lpszURL, HWND hwndParent, DWORD dwReserved )
4374 FIXME("(%s, %p, 0x%08x) stub\n", debugstr_w(lpszURL), hwndParent, dwReserved);
4375 return TRUE;
4378 DWORD WINAPI InternetHangUp( DWORD_PTR dwConnection, DWORD dwReserved )
4380 FIXME("(0x%08lx, 0x%08x) stub\n", dwConnection, dwReserved);
4381 return ERROR_SUCCESS;
4384 BOOL WINAPI CreateMD5SSOHash( PWSTR pszChallengeInfo, PWSTR pwszRealm, PWSTR pwszTarget,
4385 PBYTE pbHexHash )
4387 FIXME("(%s, %s, %s, %p) stub\n", debugstr_w(pszChallengeInfo), debugstr_w(pwszRealm),
4388 debugstr_w(pwszTarget), pbHexHash);
4389 return FALSE;
4392 BOOL WINAPI ResumeSuspendedDownload( HINTERNET hInternet, DWORD dwError )
4394 FIXME("(%p, 0x%08x) stub\n", hInternet, dwError);
4395 return FALSE;
4398 BOOL WINAPI InternetQueryFortezzaStatus(DWORD *a, DWORD_PTR b)
4400 FIXME("(%p, %08lx) stub\n", a, b);
4401 return 0;
4404 DWORD WINAPI ShowClientAuthCerts(HWND parent)
4406 FIXME("%p: stub\n", parent);
4407 return 0;