push 5c57339901c8d80068fee18365e7574a1e8d922a
[wine/hacks.git] / dlls / wininet / internet.c
blob619674ef3bee747b2fdc02c9755bb288c0e92991
1 /*
2 * Wininet
4 * Copyright 1999 Corel Corporation
5 * Copyright 2002 CodeWeavers Inc.
6 * Copyright 2002 Jaco Greeff
7 * Copyright 2002 TransGaming Technologies Inc.
8 * Copyright 2004 Mike McCormack for CodeWeavers
10 * Ulrich Czekalla
11 * Aric Stewart
12 * David Hammerton
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
29 #include "config.h"
30 #include "wine/port.h"
32 #define MAXHOSTNAME 100 /* from http.c */
34 #if defined(__MINGW32__) || defined (_MSC_VER)
35 #include <ws2tcpip.h>
36 #endif
38 #include <string.h>
39 #include <stdarg.h>
40 #include <stdio.h>
41 #include <sys/types.h>
42 #ifdef HAVE_SYS_SOCKET_H
43 # include <sys/socket.h>
44 #endif
45 #ifdef HAVE_POLL_H
46 #include <poll.h>
47 #endif
48 #ifdef HAVE_SYS_POLL_H
49 # include <sys/poll.h>
50 #endif
51 #ifdef HAVE_SYS_TIME_H
52 # include <sys/time.h>
53 #endif
54 #include <stdlib.h>
55 #include <ctype.h>
56 #ifdef HAVE_UNISTD_H
57 # include <unistd.h>
58 #endif
59 #include <assert.h>
61 #include "windef.h"
62 #include "winbase.h"
63 #include "winreg.h"
64 #include "winuser.h"
65 #include "wininet.h"
66 #include "winineti.h"
67 #include "winnls.h"
68 #include "wine/debug.h"
69 #include "winerror.h"
70 #define NO_SHLWAPI_STREAM
71 #include "shlwapi.h"
73 #include "wine/exception.h"
75 #include "internet.h"
76 #include "resource.h"
78 #include "wine/unicode.h"
80 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
82 #define RESPONSE_TIMEOUT 30
84 typedef struct
86 DWORD dwError;
87 CHAR response[MAX_REPLY_LEN];
88 } WITHREADERROR, *LPWITHREADERROR;
90 static DWORD g_dwTlsErrIndex = TLS_OUT_OF_INDEXES;
91 static HMODULE WININET_hModule;
93 #define HANDLE_CHUNK_SIZE 0x10
95 static CRITICAL_SECTION WININET_cs;
96 static CRITICAL_SECTION_DEBUG WININET_cs_debug =
98 0, 0, &WININET_cs,
99 { &WININET_cs_debug.ProcessLocksList, &WININET_cs_debug.ProcessLocksList },
100 0, 0, { (DWORD_PTR)(__FILE__ ": WININET_cs") }
102 static CRITICAL_SECTION WININET_cs = { &WININET_cs_debug, -1, 0, 0, 0, 0 };
104 static object_header_t **WININET_Handles;
105 static UINT WININET_dwNextHandle;
106 static UINT WININET_dwMaxHandles;
108 HINTERNET WININET_AllocHandle( object_header_t *info )
110 object_header_t **p;
111 UINT handle = 0, num;
113 list_init( &info->children );
115 EnterCriticalSection( &WININET_cs );
116 if( !WININET_dwMaxHandles )
118 num = HANDLE_CHUNK_SIZE;
119 p = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
120 sizeof (*WININET_Handles)* num);
121 if( !p )
122 goto end;
123 WININET_Handles = p;
124 WININET_dwMaxHandles = num;
126 if( WININET_dwMaxHandles == WININET_dwNextHandle )
128 num = WININET_dwMaxHandles + HANDLE_CHUNK_SIZE;
129 p = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
130 WININET_Handles, sizeof (*WININET_Handles)* num);
131 if( !p )
132 goto end;
133 WININET_Handles = p;
134 WININET_dwMaxHandles = num;
137 handle = WININET_dwNextHandle;
138 if( WININET_Handles[handle] )
139 ERR("handle isn't free but should be\n");
140 WININET_Handles[handle] = WININET_AddRef( info );
142 while( WININET_Handles[WININET_dwNextHandle] &&
143 (WININET_dwNextHandle < WININET_dwMaxHandles ) )
144 WININET_dwNextHandle++;
146 end:
147 LeaveCriticalSection( &WININET_cs );
149 return info->hInternet = (HINTERNET) (handle+1);
152 object_header_t *WININET_AddRef( object_header_t *info )
154 ULONG refs = InterlockedIncrement(&info->refs);
155 TRACE("%p -> refcount = %d\n", info, refs );
156 return info;
159 object_header_t *WININET_GetObject( HINTERNET hinternet )
161 object_header_t *info = NULL;
162 UINT handle = (UINT) hinternet;
164 EnterCriticalSection( &WININET_cs );
166 if( (handle > 0) && ( handle <= WININET_dwMaxHandles ) &&
167 WININET_Handles[handle-1] )
168 info = WININET_AddRef( WININET_Handles[handle-1] );
170 LeaveCriticalSection( &WININET_cs );
172 TRACE("handle %d -> %p\n", handle, info);
174 return info;
177 BOOL WININET_Release( object_header_t *info )
179 ULONG refs = InterlockedDecrement(&info->refs);
180 TRACE( "object %p refcount = %d\n", info, refs );
181 if( !refs )
183 if ( info->vtbl->CloseConnection )
185 TRACE( "closing connection %p\n", info);
186 info->vtbl->CloseConnection( info );
188 /* Don't send a callback if this is a session handle created with InternetOpenUrl */
189 if ((info->htype != WH_HHTTPSESSION && info->htype != WH_HFTPSESSION)
190 || !(info->dwInternalFlags & INET_OPENURL))
192 INTERNET_SendCallback(info, info->dwContext,
193 INTERNET_STATUS_HANDLE_CLOSING, &info->hInternet,
194 sizeof(HINTERNET));
196 TRACE( "destroying object %p\n", info);
197 if ( info->htype != WH_HINIT )
198 list_remove( &info->entry );
199 info->vtbl->Destroy( info );
201 return TRUE;
204 BOOL WININET_FreeHandle( HINTERNET hinternet )
206 BOOL ret = FALSE;
207 UINT handle = (UINT) hinternet;
208 object_header_t *info = NULL, *child, *next;
210 EnterCriticalSection( &WININET_cs );
212 if( (handle > 0) && ( handle <= WININET_dwMaxHandles ) )
214 handle--;
215 if( WININET_Handles[handle] )
217 info = WININET_Handles[handle];
218 TRACE( "destroying handle %d for object %p\n", handle+1, info);
219 WININET_Handles[handle] = NULL;
220 ret = TRUE;
224 LeaveCriticalSection( &WININET_cs );
226 /* As on native when the equivalent of WININET_Release is called, the handle
227 * is already invalid, but if a new handle is created at this time it does
228 * not yet get assigned the freed handle number */
229 if( info )
231 /* Free all children as native does */
232 LIST_FOR_EACH_ENTRY_SAFE( child, next, &info->children, object_header_t, entry )
234 TRACE( "freeing child handle %d for parent handle %d\n",
235 (UINT)child->hInternet, handle+1);
236 WININET_FreeHandle( child->hInternet );
238 WININET_Release( info );
241 EnterCriticalSection( &WININET_cs );
243 if( WININET_dwNextHandle > handle && !WININET_Handles[handle] )
244 WININET_dwNextHandle = handle;
246 LeaveCriticalSection( &WININET_cs );
248 return ret;
251 /***********************************************************************
252 * DllMain [Internal] Initializes the internal 'WININET.DLL'.
254 * PARAMS
255 * hinstDLL [I] handle to the DLL's instance
256 * fdwReason [I]
257 * lpvReserved [I] reserved, must be NULL
259 * RETURNS
260 * Success: TRUE
261 * Failure: FALSE
264 BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
266 TRACE("%p,%x,%p\n", hinstDLL, fdwReason, lpvReserved);
268 switch (fdwReason) {
269 case DLL_PROCESS_ATTACH:
271 g_dwTlsErrIndex = TlsAlloc();
273 if (g_dwTlsErrIndex == TLS_OUT_OF_INDEXES)
274 return FALSE;
276 URLCacheContainers_CreateDefaults();
278 WININET_hModule = hinstDLL;
280 case DLL_THREAD_ATTACH:
281 break;
283 case DLL_THREAD_DETACH:
284 if (g_dwTlsErrIndex != TLS_OUT_OF_INDEXES)
286 LPVOID lpwite = TlsGetValue(g_dwTlsErrIndex);
287 HeapFree(GetProcessHeap(), 0, lpwite);
289 break;
291 case DLL_PROCESS_DETACH:
293 NETCON_unload();
295 URLCacheContainers_DeleteAll();
297 if (g_dwTlsErrIndex != TLS_OUT_OF_INDEXES)
299 HeapFree(GetProcessHeap(), 0, TlsGetValue(g_dwTlsErrIndex));
300 TlsFree(g_dwTlsErrIndex);
302 break;
305 return TRUE;
309 /***********************************************************************
310 * InternetInitializeAutoProxyDll (WININET.@)
312 * Setup the internal proxy
314 * PARAMETERS
315 * dwReserved
317 * RETURNS
318 * FALSE on failure
321 BOOL WINAPI InternetInitializeAutoProxyDll(DWORD dwReserved)
323 FIXME("STUB\n");
324 INTERNET_SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
325 return FALSE;
328 /***********************************************************************
329 * DetectAutoProxyUrl (WININET.@)
331 * Auto detect the proxy url
333 * RETURNS
334 * FALSE on failure
337 BOOL WINAPI DetectAutoProxyUrl(LPSTR lpszAutoProxyUrl,
338 DWORD dwAutoProxyUrlLength, DWORD dwDetectFlags)
340 FIXME("STUB\n");
341 INTERNET_SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
342 return FALSE;
346 /***********************************************************************
347 * INTERNET_ConfigureProxy
349 * FIXME:
350 * The proxy may be specified in the form 'http=proxy.my.org'
351 * Presumably that means there can be ftp=ftpproxy.my.org too.
353 static BOOL INTERNET_ConfigureProxy( appinfo_t *lpwai )
355 HKEY key;
356 DWORD type, len, enabled = 0;
357 LPCSTR envproxy;
358 static const WCHAR szInternetSettings[] =
359 { 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
360 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
361 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s',0 };
362 static const WCHAR szProxyServer[] = { 'P','r','o','x','y','S','e','r','v','e','r', 0 };
363 static const WCHAR szProxyEnable[] = { 'P','r','o','x','y','E','n','a','b','l','e', 0 };
365 if (RegOpenKeyW( HKEY_CURRENT_USER, szInternetSettings, &key )) return FALSE;
367 len = sizeof enabled;
368 if (RegQueryValueExW( key, szProxyEnable, NULL, &type, (BYTE *)&enabled, &len ) || type != REG_DWORD)
369 RegSetValueExW( key, szProxyEnable, 0, REG_DWORD, (BYTE *)&enabled, sizeof(REG_DWORD) );
371 if (enabled)
373 TRACE("Proxy is enabled.\n");
375 /* figure out how much memory the proxy setting takes */
376 if (!RegQueryValueExW( key, szProxyServer, NULL, &type, NULL, &len ) && len && (type == REG_SZ))
378 LPWSTR szProxy, p;
379 static const WCHAR szHttp[] = {'h','t','t','p','=',0};
381 if (!(szProxy = HeapAlloc( GetProcessHeap(), 0, len )))
383 RegCloseKey( key );
384 return FALSE;
386 RegQueryValueExW( key, szProxyServer, NULL, &type, (BYTE*)szProxy, &len );
388 /* find the http proxy, and strip away everything else */
389 p = strstrW( szProxy, szHttp );
390 if (p)
392 p += lstrlenW( szHttp );
393 lstrcpyW( szProxy, p );
395 p = strchrW( szProxy, ' ' );
396 if (p) *p = 0;
398 lpwai->dwAccessType = INTERNET_OPEN_TYPE_PROXY;
399 lpwai->lpszProxy = szProxy;
401 TRACE("http proxy = %s\n", debugstr_w(lpwai->lpszProxy));
403 else
404 ERR("Couldn't read proxy server settings from registry.\n");
406 else if ((envproxy = getenv( "http_proxy" )))
408 WCHAR *envproxyW;
410 len = MultiByteToWideChar( CP_UNIXCP, 0, envproxy, -1, NULL, 0 );
411 if (!(envproxyW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR)))) return FALSE;
412 MultiByteToWideChar( CP_UNIXCP, 0, envproxy, -1, envproxyW, len );
414 lpwai->dwAccessType = INTERNET_OPEN_TYPE_PROXY;
415 lpwai->lpszProxy = envproxyW;
417 TRACE("http proxy (from environment) = %s\n", debugstr_w(lpwai->lpszProxy));
418 enabled = 1;
420 if (!enabled)
422 TRACE("Proxy is not enabled.\n");
423 lpwai->dwAccessType = INTERNET_OPEN_TYPE_DIRECT;
425 RegCloseKey( key );
426 return (enabled > 0);
429 /***********************************************************************
430 * dump_INTERNET_FLAGS
432 * Helper function to TRACE the internet flags.
434 * RETURNS
435 * None
438 static void dump_INTERNET_FLAGS(DWORD dwFlags)
440 #define FE(x) { x, #x }
441 static const wininet_flag_info flag[] = {
442 FE(INTERNET_FLAG_RELOAD),
443 FE(INTERNET_FLAG_RAW_DATA),
444 FE(INTERNET_FLAG_EXISTING_CONNECT),
445 FE(INTERNET_FLAG_ASYNC),
446 FE(INTERNET_FLAG_PASSIVE),
447 FE(INTERNET_FLAG_NO_CACHE_WRITE),
448 FE(INTERNET_FLAG_MAKE_PERSISTENT),
449 FE(INTERNET_FLAG_FROM_CACHE),
450 FE(INTERNET_FLAG_SECURE),
451 FE(INTERNET_FLAG_KEEP_CONNECTION),
452 FE(INTERNET_FLAG_NO_AUTO_REDIRECT),
453 FE(INTERNET_FLAG_READ_PREFETCH),
454 FE(INTERNET_FLAG_NO_COOKIES),
455 FE(INTERNET_FLAG_NO_AUTH),
456 FE(INTERNET_FLAG_CACHE_IF_NET_FAIL),
457 FE(INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP),
458 FE(INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTPS),
459 FE(INTERNET_FLAG_IGNORE_CERT_DATE_INVALID),
460 FE(INTERNET_FLAG_IGNORE_CERT_CN_INVALID),
461 FE(INTERNET_FLAG_RESYNCHRONIZE),
462 FE(INTERNET_FLAG_HYPERLINK),
463 FE(INTERNET_FLAG_NO_UI),
464 FE(INTERNET_FLAG_PRAGMA_NOCACHE),
465 FE(INTERNET_FLAG_CACHE_ASYNC),
466 FE(INTERNET_FLAG_FORMS_SUBMIT),
467 FE(INTERNET_FLAG_NEED_FILE),
468 FE(INTERNET_FLAG_TRANSFER_ASCII),
469 FE(INTERNET_FLAG_TRANSFER_BINARY)
471 #undef FE
472 unsigned int i;
474 for (i = 0; i < (sizeof(flag) / sizeof(flag[0])); i++) {
475 if (flag[i].val & dwFlags) {
476 TRACE(" %s", flag[i].name);
477 dwFlags &= ~flag[i].val;
480 if (dwFlags)
481 TRACE(" Unknown flags (%08x)\n", dwFlags);
482 else
483 TRACE("\n");
486 /***********************************************************************
487 * INTERNET_CloseHandle (internal)
489 * Close internet handle
492 static VOID APPINFO_Destroy(object_header_t *hdr)
494 appinfo_t *lpwai = (appinfo_t*)hdr;
496 TRACE("%p\n",lpwai);
498 HeapFree(GetProcessHeap(), 0, lpwai->lpszAgent);
499 HeapFree(GetProcessHeap(), 0, lpwai->lpszProxy);
500 HeapFree(GetProcessHeap(), 0, lpwai->lpszProxyBypass);
501 HeapFree(GetProcessHeap(), 0, lpwai->lpszProxyUsername);
502 HeapFree(GetProcessHeap(), 0, lpwai->lpszProxyPassword);
503 HeapFree(GetProcessHeap(), 0, lpwai);
506 static DWORD APPINFO_QueryOption(object_header_t *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
508 appinfo_t *ai = (appinfo_t*)hdr;
510 switch(option) {
511 case INTERNET_OPTION_HANDLE_TYPE:
512 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
514 if (*size < sizeof(ULONG))
515 return ERROR_INSUFFICIENT_BUFFER;
517 *size = sizeof(DWORD);
518 *(DWORD*)buffer = INTERNET_HANDLE_TYPE_INTERNET;
519 return ERROR_SUCCESS;
521 case INTERNET_OPTION_USER_AGENT: {
522 DWORD bufsize;
524 TRACE("INTERNET_OPTION_USER_AGENT\n");
526 bufsize = *size;
528 if (unicode) {
529 *size = (strlenW(ai->lpszAgent) + 1) * sizeof(WCHAR);
530 if(!buffer || bufsize < *size)
531 return ERROR_INSUFFICIENT_BUFFER;
533 strcpyW(buffer, ai->lpszAgent);
534 }else {
535 *size = WideCharToMultiByte(CP_ACP, 0, ai->lpszAgent, -1, NULL, 0, NULL, NULL);
536 if(!buffer || bufsize < *size)
537 return ERROR_INSUFFICIENT_BUFFER;
539 WideCharToMultiByte(CP_ACP, 0, ai->lpszAgent, -1, buffer, *size, NULL, NULL);
542 return ERROR_SUCCESS;
545 case INTERNET_OPTION_PROXY:
546 if (unicode) {
547 INTERNET_PROXY_INFOW *pi = (INTERNET_PROXY_INFOW *)buffer;
548 DWORD proxyBytesRequired = 0, proxyBypassBytesRequired = 0;
549 LPWSTR proxy, proxy_bypass;
551 if (ai->lpszProxy)
552 proxyBytesRequired = (lstrlenW(ai->lpszProxy) + 1) * sizeof(WCHAR);
553 if (ai->lpszProxyBypass)
554 proxyBypassBytesRequired = (lstrlenW(ai->lpszProxyBypass) + 1) * sizeof(WCHAR);
555 if (*size < sizeof(INTERNET_PROXY_INFOW) + proxyBytesRequired + proxyBypassBytesRequired)
557 *size = sizeof(INTERNET_PROXY_INFOW) + proxyBytesRequired + proxyBypassBytesRequired;
558 return ERROR_INSUFFICIENT_BUFFER;
560 proxy = (LPWSTR)((LPBYTE)buffer + sizeof(INTERNET_PROXY_INFOW));
561 proxy_bypass = (LPWSTR)((LPBYTE)buffer + sizeof(INTERNET_PROXY_INFOW) + proxyBytesRequired);
563 pi->dwAccessType = ai->dwAccessType;
564 pi->lpszProxy = NULL;
565 pi->lpszProxyBypass = NULL;
566 if (ai->lpszProxy) {
567 lstrcpyW(proxy, ai->lpszProxy);
568 pi->lpszProxy = proxy;
571 if (ai->lpszProxyBypass) {
572 lstrcpyW(proxy_bypass, ai->lpszProxyBypass);
573 pi->lpszProxyBypass = proxy_bypass;
576 *size = sizeof(INTERNET_PROXY_INFOW) + proxyBytesRequired + proxyBypassBytesRequired;
577 return ERROR_SUCCESS;
578 }else {
579 INTERNET_PROXY_INFOA *pi = (INTERNET_PROXY_INFOA *)buffer;
580 DWORD proxyBytesRequired = 0, proxyBypassBytesRequired = 0;
581 LPSTR proxy, proxy_bypass;
583 if (ai->lpszProxy)
584 proxyBytesRequired = WideCharToMultiByte(CP_ACP, 0, ai->lpszProxy, -1, NULL, 0, NULL, NULL);
585 if (ai->lpszProxyBypass)
586 proxyBypassBytesRequired = WideCharToMultiByte(CP_ACP, 0, ai->lpszProxyBypass, -1,
587 NULL, 0, NULL, NULL);
588 if (*size < sizeof(INTERNET_PROXY_INFOA) + proxyBytesRequired + proxyBypassBytesRequired)
590 *size = sizeof(INTERNET_PROXY_INFOA) + proxyBytesRequired + proxyBypassBytesRequired;
591 return ERROR_INSUFFICIENT_BUFFER;
593 proxy = (LPSTR)((LPBYTE)buffer + sizeof(INTERNET_PROXY_INFOA));
594 proxy_bypass = (LPSTR)((LPBYTE)buffer + sizeof(INTERNET_PROXY_INFOA) + proxyBytesRequired);
596 pi->dwAccessType = ai->dwAccessType;
597 pi->lpszProxy = NULL;
598 pi->lpszProxyBypass = NULL;
599 if (ai->lpszProxy) {
600 WideCharToMultiByte(CP_ACP, 0, ai->lpszProxy, -1, proxy, proxyBytesRequired, NULL, NULL);
601 pi->lpszProxy = proxy;
604 if (ai->lpszProxyBypass) {
605 WideCharToMultiByte(CP_ACP, 0, ai->lpszProxyBypass, -1, proxy_bypass,
606 proxyBypassBytesRequired, NULL, NULL);
607 pi->lpszProxyBypass = proxy_bypass;
610 *size = sizeof(INTERNET_PROXY_INFOA) + proxyBytesRequired + proxyBypassBytesRequired;
611 return ERROR_SUCCESS;
615 return INET_QueryOption(option, buffer, size, unicode);
618 static const object_vtbl_t APPINFOVtbl = {
619 APPINFO_Destroy,
620 NULL,
621 APPINFO_QueryOption,
622 NULL,
623 NULL,
624 NULL,
625 NULL,
626 NULL,
627 NULL
631 /***********************************************************************
632 * InternetOpenW (WININET.@)
634 * Per-application initialization of wininet
636 * RETURNS
637 * HINTERNET on success
638 * NULL on failure
641 HINTERNET WINAPI InternetOpenW(LPCWSTR lpszAgent, DWORD dwAccessType,
642 LPCWSTR lpszProxy, LPCWSTR lpszProxyBypass, DWORD dwFlags)
644 appinfo_t *lpwai = NULL;
645 HINTERNET handle = NULL;
647 if (TRACE_ON(wininet)) {
648 #define FE(x) { x, #x }
649 static const wininet_flag_info access_type[] = {
650 FE(INTERNET_OPEN_TYPE_PRECONFIG),
651 FE(INTERNET_OPEN_TYPE_DIRECT),
652 FE(INTERNET_OPEN_TYPE_PROXY),
653 FE(INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY)
655 #undef FE
656 DWORD i;
657 const char *access_type_str = "Unknown";
659 TRACE("(%s, %i, %s, %s, %i)\n", debugstr_w(lpszAgent), dwAccessType,
660 debugstr_w(lpszProxy), debugstr_w(lpszProxyBypass), dwFlags);
661 for (i = 0; i < (sizeof(access_type) / sizeof(access_type[0])); i++) {
662 if (access_type[i].val == dwAccessType) {
663 access_type_str = access_type[i].name;
664 break;
667 TRACE(" access type : %s\n", access_type_str);
668 TRACE(" flags :");
669 dump_INTERNET_FLAGS(dwFlags);
672 /* Clear any error information */
673 INTERNET_SetLastError(0);
675 lpwai = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(appinfo_t));
676 if (NULL == lpwai)
678 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
679 goto lend;
682 lpwai->hdr.htype = WH_HINIT;
683 lpwai->hdr.vtbl = &APPINFOVtbl;
684 lpwai->hdr.dwFlags = dwFlags;
685 lpwai->hdr.refs = 1;
686 lpwai->dwAccessType = dwAccessType;
687 lpwai->lpszProxyUsername = NULL;
688 lpwai->lpszProxyPassword = NULL;
690 handle = WININET_AllocHandle( &lpwai->hdr );
691 if( !handle )
693 HeapFree( GetProcessHeap(), 0, lpwai );
694 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
695 goto lend;
698 lpwai->lpszAgent = heap_strdupW(lpszAgent);
699 if(dwAccessType == INTERNET_OPEN_TYPE_PRECONFIG)
700 INTERNET_ConfigureProxy( lpwai );
701 else
702 lpwai->lpszProxy = heap_strdupW(lpszProxy);
703 lpwai->lpszProxyBypass = heap_strdupW(lpszProxyBypass);
705 lend:
706 if( lpwai )
707 WININET_Release( &lpwai->hdr );
709 TRACE("returning %p\n", lpwai);
711 return handle;
715 /***********************************************************************
716 * InternetOpenA (WININET.@)
718 * Per-application initialization of wininet
720 * RETURNS
721 * HINTERNET on success
722 * NULL on failure
725 HINTERNET WINAPI InternetOpenA(LPCSTR lpszAgent, DWORD dwAccessType,
726 LPCSTR lpszProxy, LPCSTR lpszProxyBypass, DWORD dwFlags)
728 WCHAR *szAgent, *szProxy, *szBypass;
729 HINTERNET rc;
731 TRACE("(%s, 0x%08x, %s, %s, 0x%08x)\n", debugstr_a(lpszAgent),
732 dwAccessType, debugstr_a(lpszProxy), debugstr_a(lpszProxyBypass), dwFlags);
734 szAgent = heap_strdupAtoW(lpszAgent);
735 szProxy = heap_strdupAtoW(lpszProxy);
736 szBypass = heap_strdupAtoW(lpszProxyBypass);
738 rc = InternetOpenW(szAgent, dwAccessType, szProxy, szBypass, dwFlags);
740 HeapFree(GetProcessHeap(), 0, szAgent);
741 HeapFree(GetProcessHeap(), 0, szProxy);
742 HeapFree(GetProcessHeap(), 0, szBypass);
744 return rc;
747 /***********************************************************************
748 * InternetGetLastResponseInfoA (WININET.@)
750 * Return last wininet error description on the calling thread
752 * RETURNS
753 * TRUE on success of writing to buffer
754 * FALSE on failure
757 BOOL WINAPI InternetGetLastResponseInfoA(LPDWORD lpdwError,
758 LPSTR lpszBuffer, LPDWORD lpdwBufferLength)
760 LPWITHREADERROR lpwite = TlsGetValue(g_dwTlsErrIndex);
762 TRACE("\n");
764 if (lpwite)
766 *lpdwError = lpwite->dwError;
767 if (lpwite->dwError)
769 memcpy(lpszBuffer, lpwite->response, *lpdwBufferLength);
770 *lpdwBufferLength = strlen(lpszBuffer);
772 else
773 *lpdwBufferLength = 0;
775 else
777 *lpdwError = 0;
778 *lpdwBufferLength = 0;
781 return TRUE;
784 /***********************************************************************
785 * InternetGetLastResponseInfoW (WININET.@)
787 * Return last wininet error description on the calling thread
789 * RETURNS
790 * TRUE on success of writing to buffer
791 * FALSE on failure
794 BOOL WINAPI InternetGetLastResponseInfoW(LPDWORD lpdwError,
795 LPWSTR lpszBuffer, LPDWORD lpdwBufferLength)
797 LPWITHREADERROR lpwite = TlsGetValue(g_dwTlsErrIndex);
799 TRACE("\n");
801 if (lpwite)
803 *lpdwError = lpwite->dwError;
804 if (lpwite->dwError)
806 memcpy(lpszBuffer, lpwite->response, *lpdwBufferLength);
807 *lpdwBufferLength = lstrlenW(lpszBuffer);
809 else
810 *lpdwBufferLength = 0;
812 else
814 *lpdwError = 0;
815 *lpdwBufferLength = 0;
818 return TRUE;
821 /***********************************************************************
822 * InternetGetConnectedState (WININET.@)
824 * Return connected state
826 * RETURNS
827 * TRUE if connected
828 * if lpdwStatus is not null, return the status (off line,
829 * modem, lan...) in it.
830 * FALSE if not connected
832 BOOL WINAPI InternetGetConnectedState(LPDWORD lpdwStatus, DWORD dwReserved)
834 TRACE("(%p, 0x%08x)\n", lpdwStatus, dwReserved);
836 if (lpdwStatus) {
837 WARN("always returning LAN connection.\n");
838 *lpdwStatus = INTERNET_CONNECTION_LAN;
840 return TRUE;
844 /***********************************************************************
845 * InternetGetConnectedStateExW (WININET.@)
847 * Return connected state
849 * PARAMS
851 * lpdwStatus [O] Flags specifying the status of the internet connection.
852 * lpszConnectionName [O] Pointer to buffer to receive the friendly name of the internet connection.
853 * dwNameLen [I] Size of the buffer, in characters.
854 * dwReserved [I] Reserved. Must be set to 0.
856 * RETURNS
857 * TRUE if connected
858 * if lpdwStatus is not null, return the status (off line,
859 * modem, lan...) in it.
860 * FALSE if not connected
862 * NOTES
863 * If the system has no available network connections, an empty string is
864 * stored in lpszConnectionName. If there is a LAN connection, a localized
865 * "LAN Connection" string is stored. Presumably, if only a dial-up
866 * connection is available then the name of the dial-up connection is
867 * returned. Why any application, other than the "Internet Settings" CPL,
868 * would want to use this function instead of the simpler InternetGetConnectedStateW
869 * function is beyond me.
871 BOOL WINAPI InternetGetConnectedStateExW(LPDWORD lpdwStatus, LPWSTR lpszConnectionName,
872 DWORD dwNameLen, DWORD dwReserved)
874 TRACE("(%p, %p, %d, 0x%08x)\n", lpdwStatus, lpszConnectionName, dwNameLen, dwReserved);
876 /* Must be zero */
877 if(dwReserved)
878 return FALSE;
880 if (lpdwStatus) {
881 WARN("always returning LAN connection.\n");
882 *lpdwStatus = INTERNET_CONNECTION_LAN;
884 return LoadStringW(WININET_hModule, IDS_LANCONNECTION, lpszConnectionName, dwNameLen);
888 /***********************************************************************
889 * InternetGetConnectedStateExA (WININET.@)
891 BOOL WINAPI InternetGetConnectedStateExA(LPDWORD lpdwStatus, LPSTR lpszConnectionName,
892 DWORD dwNameLen, DWORD dwReserved)
894 LPWSTR lpwszConnectionName = NULL;
895 BOOL rc;
897 TRACE("(%p, %p, %d, 0x%08x)\n", lpdwStatus, lpszConnectionName, dwNameLen, dwReserved);
899 if (lpszConnectionName && dwNameLen > 0)
900 lpwszConnectionName= HeapAlloc(GetProcessHeap(), 0, dwNameLen * sizeof(WCHAR));
902 rc = InternetGetConnectedStateExW(lpdwStatus,lpwszConnectionName, dwNameLen,
903 dwReserved);
904 if (rc && lpwszConnectionName)
906 WideCharToMultiByte(CP_ACP,0,lpwszConnectionName,-1,lpszConnectionName,
907 dwNameLen, NULL, NULL);
909 HeapFree(GetProcessHeap(),0,lpwszConnectionName);
912 return rc;
916 /***********************************************************************
917 * InternetConnectW (WININET.@)
919 * Open a ftp, gopher or http session
921 * RETURNS
922 * HINTERNET a session handle on success
923 * NULL on failure
926 HINTERNET WINAPI InternetConnectW(HINTERNET hInternet,
927 LPCWSTR lpszServerName, INTERNET_PORT nServerPort,
928 LPCWSTR lpszUserName, LPCWSTR lpszPassword,
929 DWORD dwService, DWORD dwFlags, DWORD_PTR dwContext)
931 appinfo_t *hIC;
932 HINTERNET rc = NULL;
934 TRACE("(%p, %s, %i, %s, %s, %i, %i, %lx)\n", hInternet, debugstr_w(lpszServerName),
935 nServerPort, debugstr_w(lpszUserName), debugstr_w(lpszPassword),
936 dwService, dwFlags, dwContext);
938 if (!lpszServerName)
940 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
941 return NULL;
944 /* Clear any error information */
945 INTERNET_SetLastError(0);
946 hIC = (appinfo_t*)WININET_GetObject( hInternet );
947 if ( (hIC == NULL) || (hIC->hdr.htype != WH_HINIT) )
949 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
950 goto lend;
953 switch (dwService)
955 case INTERNET_SERVICE_FTP:
956 rc = FTP_Connect(hIC, lpszServerName, nServerPort,
957 lpszUserName, lpszPassword, dwFlags, dwContext, 0);
958 break;
960 case INTERNET_SERVICE_HTTP:
961 rc = HTTP_Connect(hIC, lpszServerName, nServerPort,
962 lpszUserName, lpszPassword, dwFlags, dwContext, 0);
963 break;
965 case INTERNET_SERVICE_GOPHER:
966 default:
967 break;
969 lend:
970 if( hIC )
971 WININET_Release( &hIC->hdr );
973 TRACE("returning %p\n", rc);
974 return rc;
978 /***********************************************************************
979 * InternetConnectA (WININET.@)
981 * Open a ftp, gopher or http session
983 * RETURNS
984 * HINTERNET a session handle on success
985 * NULL on failure
988 HINTERNET WINAPI InternetConnectA(HINTERNET hInternet,
989 LPCSTR lpszServerName, INTERNET_PORT nServerPort,
990 LPCSTR lpszUserName, LPCSTR lpszPassword,
991 DWORD dwService, DWORD dwFlags, DWORD_PTR dwContext)
993 HINTERNET rc = NULL;
994 LPWSTR szServerName;
995 LPWSTR szUserName;
996 LPWSTR szPassword;
998 szServerName = heap_strdupAtoW(lpszServerName);
999 szUserName = heap_strdupAtoW(lpszUserName);
1000 szPassword = heap_strdupAtoW(lpszPassword);
1002 rc = InternetConnectW(hInternet, szServerName, nServerPort,
1003 szUserName, szPassword, dwService, dwFlags, dwContext);
1005 HeapFree(GetProcessHeap(), 0, szServerName);
1006 HeapFree(GetProcessHeap(), 0, szUserName);
1007 HeapFree(GetProcessHeap(), 0, szPassword);
1008 return rc;
1012 /***********************************************************************
1013 * InternetFindNextFileA (WININET.@)
1015 * Continues a file search from a previous call to FindFirstFile
1017 * RETURNS
1018 * TRUE on success
1019 * FALSE on failure
1022 BOOL WINAPI InternetFindNextFileA(HINTERNET hFind, LPVOID lpvFindData)
1024 BOOL ret;
1025 WIN32_FIND_DATAW fd;
1027 ret = InternetFindNextFileW(hFind, lpvFindData?&fd:NULL);
1028 if(lpvFindData)
1029 WININET_find_data_WtoA(&fd, (LPWIN32_FIND_DATAA)lpvFindData);
1030 return ret;
1033 /***********************************************************************
1034 * InternetFindNextFileW (WININET.@)
1036 * Continues a file search from a previous call to FindFirstFile
1038 * RETURNS
1039 * TRUE on success
1040 * FALSE on failure
1043 BOOL WINAPI InternetFindNextFileW(HINTERNET hFind, LPVOID lpvFindData)
1045 object_header_t *hdr;
1046 DWORD res;
1048 TRACE("\n");
1050 hdr = WININET_GetObject(hFind);
1051 if(!hdr) {
1052 WARN("Invalid handle\n");
1053 SetLastError(ERROR_INVALID_HANDLE);
1054 return FALSE;
1057 if(hdr->vtbl->FindNextFileW) {
1058 res = hdr->vtbl->FindNextFileW(hdr, lpvFindData);
1059 }else {
1060 WARN("Handle doesn't support NextFile\n");
1061 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
1064 WININET_Release(hdr);
1066 if(res != ERROR_SUCCESS)
1067 SetLastError(res);
1068 return res == ERROR_SUCCESS;
1071 /***********************************************************************
1072 * InternetCloseHandle (WININET.@)
1074 * Generic close handle function
1076 * RETURNS
1077 * TRUE on success
1078 * FALSE on failure
1081 BOOL WINAPI InternetCloseHandle(HINTERNET hInternet)
1083 object_header_t *lpwh;
1085 TRACE("%p\n",hInternet);
1087 lpwh = WININET_GetObject( hInternet );
1088 if (NULL == lpwh)
1090 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
1091 return FALSE;
1094 WININET_Release( lpwh );
1095 WININET_FreeHandle( hInternet );
1097 return TRUE;
1101 /***********************************************************************
1102 * ConvertUrlComponentValue (Internal)
1104 * Helper function for InternetCrackUrlA
1107 static void ConvertUrlComponentValue(LPSTR* lppszComponent, LPDWORD dwComponentLen,
1108 LPWSTR lpwszComponent, DWORD dwwComponentLen,
1109 LPCSTR lpszStart, LPCWSTR lpwszStart)
1111 TRACE("%p %d %p %d %p %p\n", *lppszComponent, *dwComponentLen, lpwszComponent, dwwComponentLen, lpszStart, lpwszStart);
1112 if (*dwComponentLen != 0)
1114 DWORD nASCIILength=WideCharToMultiByte(CP_ACP,0,lpwszComponent,dwwComponentLen,NULL,0,NULL,NULL);
1115 if (*lppszComponent == NULL)
1117 int nASCIIOffset=WideCharToMultiByte(CP_ACP,0,lpwszStart,lpwszComponent-lpwszStart,NULL,0,NULL,NULL);
1118 if (lpwszComponent)
1119 *lppszComponent = (LPSTR)lpszStart+nASCIIOffset;
1120 else
1121 *lppszComponent = NULL;
1122 *dwComponentLen = nASCIILength;
1124 else
1126 DWORD ncpylen = min((*dwComponentLen)-1, nASCIILength);
1127 WideCharToMultiByte(CP_ACP,0,lpwszComponent,dwwComponentLen,*lppszComponent,ncpylen+1,NULL,NULL);
1128 (*lppszComponent)[ncpylen]=0;
1129 *dwComponentLen = ncpylen;
1135 /***********************************************************************
1136 * InternetCrackUrlA (WININET.@)
1138 * See InternetCrackUrlW.
1140 BOOL WINAPI InternetCrackUrlA(LPCSTR lpszUrl, DWORD dwUrlLength, DWORD dwFlags,
1141 LPURL_COMPONENTSA lpUrlComponents)
1143 DWORD nLength;
1144 URL_COMPONENTSW UCW;
1145 BOOL ret = FALSE;
1146 WCHAR *lpwszUrl, *hostname = NULL, *username = NULL, *password = NULL, *path = NULL,
1147 *scheme = NULL, *extra = NULL;
1149 TRACE("(%s %u %x %p)\n",
1150 lpszUrl ? debugstr_an(lpszUrl, dwUrlLength ? dwUrlLength : strlen(lpszUrl)) : "(null)",
1151 dwUrlLength, dwFlags, lpUrlComponents);
1153 if (!lpszUrl || !*lpszUrl || !lpUrlComponents ||
1154 lpUrlComponents->dwStructSize != sizeof(URL_COMPONENTSA))
1156 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1157 return FALSE;
1160 if(dwUrlLength<=0)
1161 dwUrlLength=-1;
1162 nLength=MultiByteToWideChar(CP_ACP,0,lpszUrl,dwUrlLength,NULL,0);
1164 /* if dwUrlLength=-1 then nLength includes null but length to
1165 InternetCrackUrlW should not include it */
1166 if (dwUrlLength == -1) nLength--;
1168 lpwszUrl=HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WCHAR)*nLength);
1169 MultiByteToWideChar(CP_ACP,0,lpszUrl,dwUrlLength,lpwszUrl,nLength);
1171 memset(&UCW,0,sizeof(UCW));
1172 UCW.dwStructSize = sizeof(URL_COMPONENTSW);
1173 if (lpUrlComponents->dwHostNameLength)
1175 UCW.dwHostNameLength = lpUrlComponents->dwHostNameLength;
1176 if (lpUrlComponents->lpszHostName)
1178 hostname = HeapAlloc(GetProcessHeap(), 0, UCW.dwHostNameLength * sizeof(WCHAR));
1179 UCW.lpszHostName = hostname;
1182 if (lpUrlComponents->dwUserNameLength)
1184 UCW.dwUserNameLength = lpUrlComponents->dwUserNameLength;
1185 if (lpUrlComponents->lpszUserName)
1187 username = HeapAlloc(GetProcessHeap(), 0, UCW.dwUserNameLength * sizeof(WCHAR));
1188 UCW.lpszUserName = username;
1191 if (lpUrlComponents->dwPasswordLength)
1193 UCW.dwPasswordLength = lpUrlComponents->dwPasswordLength;
1194 if (lpUrlComponents->lpszPassword)
1196 password = HeapAlloc(GetProcessHeap(), 0, UCW.dwPasswordLength * sizeof(WCHAR));
1197 UCW.lpszPassword = password;
1200 if (lpUrlComponents->dwUrlPathLength)
1202 UCW.dwUrlPathLength = lpUrlComponents->dwUrlPathLength;
1203 if (lpUrlComponents->lpszUrlPath)
1205 path = HeapAlloc(GetProcessHeap(), 0, UCW.dwUrlPathLength * sizeof(WCHAR));
1206 UCW.lpszUrlPath = path;
1209 if (lpUrlComponents->dwSchemeLength)
1211 UCW.dwSchemeLength = lpUrlComponents->dwSchemeLength;
1212 if (lpUrlComponents->lpszScheme)
1214 scheme = HeapAlloc(GetProcessHeap(), 0, UCW.dwSchemeLength * sizeof(WCHAR));
1215 UCW.lpszScheme = scheme;
1218 if (lpUrlComponents->dwExtraInfoLength)
1220 UCW.dwExtraInfoLength = lpUrlComponents->dwExtraInfoLength;
1221 if (lpUrlComponents->lpszExtraInfo)
1223 extra = HeapAlloc(GetProcessHeap(), 0, UCW.dwExtraInfoLength * sizeof(WCHAR));
1224 UCW.lpszExtraInfo = extra;
1227 if ((ret = InternetCrackUrlW(lpwszUrl, nLength, dwFlags, &UCW)))
1229 ConvertUrlComponentValue(&lpUrlComponents->lpszHostName, &lpUrlComponents->dwHostNameLength,
1230 UCW.lpszHostName, UCW.dwHostNameLength, lpszUrl, lpwszUrl);
1231 ConvertUrlComponentValue(&lpUrlComponents->lpszUserName, &lpUrlComponents->dwUserNameLength,
1232 UCW.lpszUserName, UCW.dwUserNameLength, lpszUrl, lpwszUrl);
1233 ConvertUrlComponentValue(&lpUrlComponents->lpszPassword, &lpUrlComponents->dwPasswordLength,
1234 UCW.lpszPassword, UCW.dwPasswordLength, lpszUrl, lpwszUrl);
1235 ConvertUrlComponentValue(&lpUrlComponents->lpszUrlPath, &lpUrlComponents->dwUrlPathLength,
1236 UCW.lpszUrlPath, UCW.dwUrlPathLength, lpszUrl, lpwszUrl);
1237 ConvertUrlComponentValue(&lpUrlComponents->lpszScheme, &lpUrlComponents->dwSchemeLength,
1238 UCW.lpszScheme, UCW.dwSchemeLength, lpszUrl, lpwszUrl);
1239 ConvertUrlComponentValue(&lpUrlComponents->lpszExtraInfo, &lpUrlComponents->dwExtraInfoLength,
1240 UCW.lpszExtraInfo, UCW.dwExtraInfoLength, lpszUrl, lpwszUrl);
1242 lpUrlComponents->nScheme = UCW.nScheme;
1243 lpUrlComponents->nPort = UCW.nPort;
1245 TRACE("%s: scheme(%s) host(%s) path(%s) extra(%s)\n", lpszUrl,
1246 debugstr_an(lpUrlComponents->lpszScheme, lpUrlComponents->dwSchemeLength),
1247 debugstr_an(lpUrlComponents->lpszHostName, lpUrlComponents->dwHostNameLength),
1248 debugstr_an(lpUrlComponents->lpszUrlPath, lpUrlComponents->dwUrlPathLength),
1249 debugstr_an(lpUrlComponents->lpszExtraInfo, lpUrlComponents->dwExtraInfoLength));
1251 HeapFree(GetProcessHeap(), 0, lpwszUrl);
1252 HeapFree(GetProcessHeap(), 0, hostname);
1253 HeapFree(GetProcessHeap(), 0, username);
1254 HeapFree(GetProcessHeap(), 0, password);
1255 HeapFree(GetProcessHeap(), 0, path);
1256 HeapFree(GetProcessHeap(), 0, scheme);
1257 HeapFree(GetProcessHeap(), 0, extra);
1258 return ret;
1261 static const WCHAR url_schemes[][7] =
1263 {'f','t','p',0},
1264 {'g','o','p','h','e','r',0},
1265 {'h','t','t','p',0},
1266 {'h','t','t','p','s',0},
1267 {'f','i','l','e',0},
1268 {'n','e','w','s',0},
1269 {'m','a','i','l','t','o',0},
1270 {'r','e','s',0},
1273 /***********************************************************************
1274 * GetInternetSchemeW (internal)
1276 * Get scheme of url
1278 * RETURNS
1279 * scheme on success
1280 * INTERNET_SCHEME_UNKNOWN on failure
1283 static INTERNET_SCHEME GetInternetSchemeW(LPCWSTR lpszScheme, DWORD nMaxCmp)
1285 int i;
1287 TRACE("%s %d\n",debugstr_wn(lpszScheme, nMaxCmp), nMaxCmp);
1289 if(lpszScheme==NULL)
1290 return INTERNET_SCHEME_UNKNOWN;
1292 for (i = 0; i < sizeof(url_schemes)/sizeof(url_schemes[0]); i++)
1293 if (!strncmpW(lpszScheme, url_schemes[i], nMaxCmp))
1294 return INTERNET_SCHEME_FIRST + i;
1296 return INTERNET_SCHEME_UNKNOWN;
1299 /***********************************************************************
1300 * SetUrlComponentValueW (Internal)
1302 * Helper function for InternetCrackUrlW
1304 * PARAMS
1305 * lppszComponent [O] Holds the returned string
1306 * dwComponentLen [I] Holds the size of lppszComponent
1307 * [O] Holds the length of the string in lppszComponent without '\0'
1308 * lpszStart [I] Holds the string to copy from
1309 * len [I] Holds the length of lpszStart without '\0'
1311 * RETURNS
1312 * TRUE on success
1313 * FALSE on failure
1316 static BOOL SetUrlComponentValueW(LPWSTR* lppszComponent, LPDWORD dwComponentLen, LPCWSTR lpszStart, DWORD len)
1318 TRACE("%s (%d)\n", debugstr_wn(lpszStart,len), len);
1320 if ( (*dwComponentLen == 0) && (*lppszComponent == NULL) )
1321 return FALSE;
1323 if (*dwComponentLen != 0 || *lppszComponent == NULL)
1325 if (*lppszComponent == NULL)
1327 *lppszComponent = (LPWSTR)lpszStart;
1328 *dwComponentLen = len;
1330 else
1332 DWORD ncpylen = min((*dwComponentLen)-1, len);
1333 memcpy(*lppszComponent, lpszStart, ncpylen*sizeof(WCHAR));
1334 (*lppszComponent)[ncpylen] = '\0';
1335 *dwComponentLen = ncpylen;
1339 return TRUE;
1342 /***********************************************************************
1343 * InternetCrackUrlW (WININET.@)
1345 * Break up URL into its components
1347 * RETURNS
1348 * TRUE on success
1349 * FALSE on failure
1351 BOOL WINAPI InternetCrackUrlW(LPCWSTR lpszUrl_orig, DWORD dwUrlLength_orig, DWORD dwFlags,
1352 LPURL_COMPONENTSW lpUC)
1355 * RFC 1808
1356 * <protocol>:[//<net_loc>][/path][;<params>][?<query>][#<fragment>]
1359 LPCWSTR lpszParam = NULL;
1360 BOOL bIsAbsolute = FALSE;
1361 LPCWSTR lpszap, lpszUrl = lpszUrl_orig;
1362 LPCWSTR lpszcp = NULL;
1363 LPWSTR lpszUrl_decode = NULL;
1364 DWORD dwUrlLength = dwUrlLength_orig;
1366 TRACE("(%s %u %x %p)\n",
1367 lpszUrl ? debugstr_wn(lpszUrl, dwUrlLength ? dwUrlLength : strlenW(lpszUrl)) : "(null)",
1368 dwUrlLength, dwFlags, lpUC);
1370 if (!lpszUrl_orig || !*lpszUrl_orig || !lpUC)
1372 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1373 return FALSE;
1375 if (!dwUrlLength) dwUrlLength = strlenW(lpszUrl);
1377 if (dwFlags & ICU_DECODE)
1379 WCHAR *url_tmp;
1380 DWORD len = dwUrlLength + 1;
1382 if (!(url_tmp = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR))))
1384 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1385 return FALSE;
1387 memcpy(url_tmp, lpszUrl_orig, dwUrlLength * sizeof(WCHAR));
1388 url_tmp[dwUrlLength] = 0;
1389 if (!(lpszUrl_decode = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR))))
1391 HeapFree(GetProcessHeap(), 0, url_tmp);
1392 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1393 return FALSE;
1395 if (InternetCanonicalizeUrlW(url_tmp, lpszUrl_decode, &len, ICU_DECODE | ICU_NO_ENCODE))
1397 dwUrlLength = len;
1398 lpszUrl = lpszUrl_decode;
1400 HeapFree(GetProcessHeap(), 0, url_tmp);
1402 lpszap = lpszUrl;
1404 /* Determine if the URI is absolute. */
1405 while (lpszap - lpszUrl < dwUrlLength)
1407 if (isalnumW(*lpszap) || *lpszap == '+' || *lpszap == '.' || *lpszap == '-')
1409 lpszap++;
1410 continue;
1412 if ((*lpszap == ':') && (lpszap - lpszUrl >= 2))
1414 bIsAbsolute = TRUE;
1415 lpszcp = lpszap;
1417 else
1419 lpszcp = lpszUrl; /* Relative url */
1422 break;
1425 lpUC->nScheme = INTERNET_SCHEME_UNKNOWN;
1426 lpUC->nPort = INTERNET_INVALID_PORT_NUMBER;
1428 /* Parse <params> */
1429 lpszParam = memchrW(lpszap, ';', dwUrlLength - (lpszap - lpszUrl));
1430 if(!lpszParam)
1431 lpszParam = memchrW(lpszap, '?', dwUrlLength - (lpszap - lpszUrl));
1432 if(!lpszParam)
1433 lpszParam = memchrW(lpszap, '#', dwUrlLength - (lpszap - lpszUrl));
1435 SetUrlComponentValueW(&lpUC->lpszExtraInfo, &lpUC->dwExtraInfoLength,
1436 lpszParam, lpszParam ? dwUrlLength-(lpszParam-lpszUrl) : 0);
1438 if (bIsAbsolute) /* Parse <protocol>:[//<net_loc>] */
1440 LPCWSTR lpszNetLoc;
1442 /* Get scheme first. */
1443 lpUC->nScheme = GetInternetSchemeW(lpszUrl, lpszcp - lpszUrl);
1444 SetUrlComponentValueW(&lpUC->lpszScheme, &lpUC->dwSchemeLength,
1445 lpszUrl, lpszcp - lpszUrl);
1447 /* Eat ':' in protocol. */
1448 lpszcp++;
1450 /* double slash indicates the net_loc portion is present */
1451 if ((lpszcp[0] == '/') && (lpszcp[1] == '/'))
1453 lpszcp += 2;
1455 lpszNetLoc = memchrW(lpszcp, '/', dwUrlLength - (lpszcp - lpszUrl));
1456 if (lpszParam)
1458 if (lpszNetLoc)
1459 lpszNetLoc = min(lpszNetLoc, lpszParam);
1460 else
1461 lpszNetLoc = lpszParam;
1463 else if (!lpszNetLoc)
1464 lpszNetLoc = lpszcp + dwUrlLength-(lpszcp-lpszUrl);
1466 /* Parse net-loc */
1467 if (lpszNetLoc)
1469 LPCWSTR lpszHost;
1470 LPCWSTR lpszPort;
1472 /* [<user>[<:password>]@]<host>[:<port>] */
1473 /* First find the user and password if they exist */
1475 lpszHost = memchrW(lpszcp, '@', dwUrlLength - (lpszcp - lpszUrl));
1476 if (lpszHost == NULL || lpszHost > lpszNetLoc)
1478 /* username and password not specified. */
1479 SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength, NULL, 0);
1480 SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength, NULL, 0);
1482 else /* Parse out username and password */
1484 LPCWSTR lpszUser = lpszcp;
1485 LPCWSTR lpszPasswd = lpszHost;
1487 while (lpszcp < lpszHost)
1489 if (*lpszcp == ':')
1490 lpszPasswd = lpszcp;
1492 lpszcp++;
1495 SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength,
1496 lpszUser, lpszPasswd - lpszUser);
1498 if (lpszPasswd != lpszHost)
1499 lpszPasswd++;
1500 SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength,
1501 lpszPasswd == lpszHost ? NULL : lpszPasswd,
1502 lpszHost - lpszPasswd);
1504 lpszcp++; /* Advance to beginning of host */
1507 /* Parse <host><:port> */
1509 lpszHost = lpszcp;
1510 lpszPort = lpszNetLoc;
1512 /* special case for res:// URLs: there is no port here, so the host is the
1513 entire string up to the first '/' */
1514 if(lpUC->nScheme==INTERNET_SCHEME_RES)
1516 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength,
1517 lpszHost, lpszPort - lpszHost);
1518 lpszcp=lpszNetLoc;
1520 else
1522 while (lpszcp < lpszNetLoc)
1524 if (*lpszcp == ':')
1525 lpszPort = lpszcp;
1527 lpszcp++;
1530 /* If the scheme is "file" and the host is just one letter, it's not a host */
1531 if(lpUC->nScheme==INTERNET_SCHEME_FILE && (lpszPort-lpszHost)==1)
1533 lpszcp=lpszHost;
1534 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength,
1535 NULL, 0);
1537 else
1539 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength,
1540 lpszHost, lpszPort - lpszHost);
1541 if (lpszPort != lpszNetLoc)
1542 lpUC->nPort = atoiW(++lpszPort);
1543 else switch (lpUC->nScheme)
1545 case INTERNET_SCHEME_HTTP:
1546 lpUC->nPort = INTERNET_DEFAULT_HTTP_PORT;
1547 break;
1548 case INTERNET_SCHEME_HTTPS:
1549 lpUC->nPort = INTERNET_DEFAULT_HTTPS_PORT;
1550 break;
1551 case INTERNET_SCHEME_FTP:
1552 lpUC->nPort = INTERNET_DEFAULT_FTP_PORT;
1553 break;
1554 case INTERNET_SCHEME_GOPHER:
1555 lpUC->nPort = INTERNET_DEFAULT_GOPHER_PORT;
1556 break;
1557 default:
1558 break;
1564 else
1566 SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength, NULL, 0);
1567 SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength, NULL, 0);
1568 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength, NULL, 0);
1571 else
1573 SetUrlComponentValueW(&lpUC->lpszScheme, &lpUC->dwSchemeLength, NULL, 0);
1574 SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength, NULL, 0);
1575 SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength, NULL, 0);
1576 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength, NULL, 0);
1579 /* Here lpszcp points to:
1581 * <protocol>:[//<net_loc>][/path][;<params>][?<query>][#<fragment>]
1582 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1584 if (lpszcp != 0 && lpszcp - lpszUrl < dwUrlLength && (!lpszParam || lpszcp < lpszParam))
1586 INT len;
1588 /* Only truncate the parameter list if it's already been saved
1589 * in lpUC->lpszExtraInfo.
1591 if (lpszParam && lpUC->dwExtraInfoLength && lpUC->lpszExtraInfo)
1592 len = lpszParam - lpszcp;
1593 else
1595 /* Leave the parameter list in lpszUrlPath. Strip off any trailing
1596 * newlines if necessary.
1598 LPWSTR lpsznewline = memchrW(lpszcp, '\n', dwUrlLength - (lpszcp - lpszUrl));
1599 if (lpsznewline != NULL)
1600 len = lpsznewline - lpszcp;
1601 else
1602 len = dwUrlLength-(lpszcp-lpszUrl);
1604 SetUrlComponentValueW(&lpUC->lpszUrlPath, &lpUC->dwUrlPathLength,
1605 lpszcp, len);
1607 else
1609 if (lpUC->lpszUrlPath && (lpUC->dwUrlPathLength > 0))
1610 lpUC->lpszUrlPath[0] = 0;
1611 lpUC->dwUrlPathLength = 0;
1614 TRACE("%s: scheme(%s) host(%s) path(%s) extra(%s)\n", debugstr_wn(lpszUrl,dwUrlLength),
1615 debugstr_wn(lpUC->lpszScheme,lpUC->dwSchemeLength),
1616 debugstr_wn(lpUC->lpszHostName,lpUC->dwHostNameLength),
1617 debugstr_wn(lpUC->lpszUrlPath,lpUC->dwUrlPathLength),
1618 debugstr_wn(lpUC->lpszExtraInfo,lpUC->dwExtraInfoLength));
1620 HeapFree(GetProcessHeap(), 0, lpszUrl_decode );
1621 return TRUE;
1624 /***********************************************************************
1625 * InternetAttemptConnect (WININET.@)
1627 * Attempt to make a connection to the internet
1629 * RETURNS
1630 * ERROR_SUCCESS on success
1631 * Error value on failure
1634 DWORD WINAPI InternetAttemptConnect(DWORD dwReserved)
1636 FIXME("Stub\n");
1637 return ERROR_SUCCESS;
1641 /***********************************************************************
1642 * InternetCanonicalizeUrlA (WININET.@)
1644 * Escape unsafe characters and spaces
1646 * RETURNS
1647 * TRUE on success
1648 * FALSE on failure
1651 BOOL WINAPI InternetCanonicalizeUrlA(LPCSTR lpszUrl, LPSTR lpszBuffer,
1652 LPDWORD lpdwBufferLength, DWORD dwFlags)
1654 HRESULT hr;
1655 DWORD dwURLFlags = URL_WININET_COMPATIBILITY | URL_ESCAPE_UNSAFE;
1657 TRACE("(%s, %p, %p, 0x%08x) bufferlength: %d\n", debugstr_a(lpszUrl), lpszBuffer,
1658 lpdwBufferLength, lpdwBufferLength ? *lpdwBufferLength : -1, dwFlags);
1660 if(dwFlags & ICU_DECODE)
1662 dwURLFlags |= URL_UNESCAPE;
1663 dwFlags &= ~ICU_DECODE;
1666 if(dwFlags & ICU_ESCAPE)
1668 dwURLFlags |= URL_UNESCAPE;
1669 dwFlags &= ~ICU_ESCAPE;
1672 if(dwFlags & ICU_BROWSER_MODE)
1674 dwURLFlags |= URL_BROWSER_MODE;
1675 dwFlags &= ~ICU_BROWSER_MODE;
1678 if(dwFlags & ICU_NO_ENCODE)
1680 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
1681 dwURLFlags ^= URL_ESCAPE_UNSAFE;
1682 dwFlags &= ~ICU_NO_ENCODE;
1685 if (dwFlags) FIXME("Unhandled flags 0x%08x\n", dwFlags);
1687 hr = UrlCanonicalizeA(lpszUrl, lpszBuffer, lpdwBufferLength, dwURLFlags);
1688 if (hr == E_POINTER) SetLastError(ERROR_INSUFFICIENT_BUFFER);
1689 if (hr == E_INVALIDARG) SetLastError(ERROR_INVALID_PARAMETER);
1691 return (hr == S_OK) ? TRUE : FALSE;
1694 /***********************************************************************
1695 * InternetCanonicalizeUrlW (WININET.@)
1697 * Escape unsafe characters and spaces
1699 * RETURNS
1700 * TRUE on success
1701 * FALSE on failure
1704 BOOL WINAPI InternetCanonicalizeUrlW(LPCWSTR lpszUrl, LPWSTR lpszBuffer,
1705 LPDWORD lpdwBufferLength, DWORD dwFlags)
1707 HRESULT hr;
1708 DWORD dwURLFlags = URL_WININET_COMPATIBILITY | URL_ESCAPE_UNSAFE;
1710 TRACE("(%s, %p, %p, 0x%08x) bufferlength: %d\n", debugstr_w(lpszUrl), lpszBuffer,
1711 lpdwBufferLength, dwFlags, lpdwBufferLength ? *lpdwBufferLength : -1);
1713 if(dwFlags & ICU_DECODE)
1715 dwURLFlags |= URL_UNESCAPE;
1716 dwFlags &= ~ICU_DECODE;
1719 if(dwFlags & ICU_ESCAPE)
1721 dwURLFlags |= URL_UNESCAPE;
1722 dwFlags &= ~ICU_ESCAPE;
1725 if(dwFlags & ICU_BROWSER_MODE)
1727 dwURLFlags |= URL_BROWSER_MODE;
1728 dwFlags &= ~ICU_BROWSER_MODE;
1731 if(dwFlags & ICU_NO_ENCODE)
1733 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
1734 dwURLFlags ^= URL_ESCAPE_UNSAFE;
1735 dwFlags &= ~ICU_NO_ENCODE;
1738 if (dwFlags) FIXME("Unhandled flags 0x%08x\n", dwFlags);
1740 hr = UrlCanonicalizeW(lpszUrl, lpszBuffer, lpdwBufferLength, dwURLFlags);
1741 if (hr == E_POINTER) SetLastError(ERROR_INSUFFICIENT_BUFFER);
1742 if (hr == E_INVALIDARG) SetLastError(ERROR_INVALID_PARAMETER);
1744 return (hr == S_OK) ? TRUE : FALSE;
1747 /* #################################################### */
1749 static INTERNET_STATUS_CALLBACK set_status_callback(
1750 object_header_t *lpwh, INTERNET_STATUS_CALLBACK callback, BOOL unicode)
1752 INTERNET_STATUS_CALLBACK ret;
1754 if (unicode) lpwh->dwInternalFlags |= INET_CALLBACKW;
1755 else lpwh->dwInternalFlags &= ~INET_CALLBACKW;
1757 ret = lpwh->lpfnStatusCB;
1758 lpwh->lpfnStatusCB = callback;
1760 return ret;
1763 /***********************************************************************
1764 * InternetSetStatusCallbackA (WININET.@)
1766 * Sets up a callback function which is called as progress is made
1767 * during an operation.
1769 * RETURNS
1770 * Previous callback or NULL on success
1771 * INTERNET_INVALID_STATUS_CALLBACK on failure
1774 INTERNET_STATUS_CALLBACK WINAPI InternetSetStatusCallbackA(
1775 HINTERNET hInternet ,INTERNET_STATUS_CALLBACK lpfnIntCB)
1777 INTERNET_STATUS_CALLBACK retVal;
1778 object_header_t *lpwh;
1780 TRACE("%p\n", hInternet);
1782 if (!(lpwh = WININET_GetObject(hInternet)))
1783 return INTERNET_INVALID_STATUS_CALLBACK;
1785 retVal = set_status_callback(lpwh, lpfnIntCB, FALSE);
1787 WININET_Release( lpwh );
1788 return retVal;
1791 /***********************************************************************
1792 * InternetSetStatusCallbackW (WININET.@)
1794 * Sets up a callback function which is called as progress is made
1795 * during an operation.
1797 * RETURNS
1798 * Previous callback or NULL on success
1799 * INTERNET_INVALID_STATUS_CALLBACK on failure
1802 INTERNET_STATUS_CALLBACK WINAPI InternetSetStatusCallbackW(
1803 HINTERNET hInternet ,INTERNET_STATUS_CALLBACK lpfnIntCB)
1805 INTERNET_STATUS_CALLBACK retVal;
1806 object_header_t *lpwh;
1808 TRACE("%p\n", hInternet);
1810 if (!(lpwh = WININET_GetObject(hInternet)))
1811 return INTERNET_INVALID_STATUS_CALLBACK;
1813 retVal = set_status_callback(lpwh, lpfnIntCB, TRUE);
1815 WININET_Release( lpwh );
1816 return retVal;
1819 /***********************************************************************
1820 * InternetSetFilePointer (WININET.@)
1822 DWORD WINAPI InternetSetFilePointer(HINTERNET hFile, LONG lDistanceToMove,
1823 PVOID pReserved, DWORD dwMoveContext, DWORD_PTR dwContext)
1825 FIXME("stub\n");
1826 return FALSE;
1829 /***********************************************************************
1830 * InternetWriteFile (WININET.@)
1832 * Write data to an open internet file
1834 * RETURNS
1835 * TRUE on success
1836 * FALSE on failure
1839 BOOL WINAPI InternetWriteFile(HINTERNET hFile, LPCVOID lpBuffer,
1840 DWORD dwNumOfBytesToWrite, LPDWORD lpdwNumOfBytesWritten)
1842 object_header_t *lpwh;
1843 BOOL retval = FALSE;
1845 TRACE("(%p %p %d %p)\n", hFile, lpBuffer, dwNumOfBytesToWrite, lpdwNumOfBytesWritten);
1847 lpwh = WININET_GetObject( hFile );
1848 if (!lpwh) {
1849 WARN("Invalid handle\n");
1850 SetLastError(ERROR_INVALID_HANDLE);
1851 return FALSE;
1854 if(lpwh->vtbl->WriteFile) {
1855 retval = lpwh->vtbl->WriteFile(lpwh, lpBuffer, dwNumOfBytesToWrite, lpdwNumOfBytesWritten);
1856 }else {
1857 WARN("No Writefile method.\n");
1858 SetLastError(ERROR_INVALID_HANDLE);
1859 retval = FALSE;
1862 WININET_Release( lpwh );
1864 return retval;
1868 /***********************************************************************
1869 * InternetReadFile (WININET.@)
1871 * Read data from an open internet file
1873 * RETURNS
1874 * TRUE on success
1875 * FALSE on failure
1878 BOOL WINAPI InternetReadFile(HINTERNET hFile, LPVOID lpBuffer,
1879 DWORD dwNumOfBytesToRead, LPDWORD pdwNumOfBytesRead)
1881 object_header_t *hdr;
1882 DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
1884 TRACE("%p %p %d %p\n", hFile, lpBuffer, dwNumOfBytesToRead, pdwNumOfBytesRead);
1886 hdr = WININET_GetObject(hFile);
1887 if (!hdr) {
1888 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
1889 return FALSE;
1892 if(hdr->vtbl->ReadFile)
1893 res = hdr->vtbl->ReadFile(hdr, lpBuffer, dwNumOfBytesToRead, pdwNumOfBytesRead);
1895 WININET_Release(hdr);
1897 TRACE("-- %s (%u) (bytes read: %d)\n", res == ERROR_SUCCESS ? "TRUE": "FALSE", res,
1898 pdwNumOfBytesRead ? *pdwNumOfBytesRead : -1);
1900 if(res != ERROR_SUCCESS)
1901 SetLastError(res);
1902 return res == ERROR_SUCCESS;
1905 /***********************************************************************
1906 * InternetReadFileExA (WININET.@)
1908 * Read data from an open internet file
1910 * PARAMS
1911 * hFile [I] Handle returned by InternetOpenUrl or HttpOpenRequest.
1912 * lpBuffersOut [I/O] Buffer.
1913 * dwFlags [I] Flags. See notes.
1914 * dwContext [I] Context for callbacks.
1916 * RETURNS
1917 * TRUE on success
1918 * FALSE on failure
1920 * NOTES
1921 * The parameter dwFlags include zero or more of the following flags:
1922 *|IRF_ASYNC - Makes the call asynchronous.
1923 *|IRF_SYNC - Makes the call synchronous.
1924 *|IRF_USE_CONTEXT - Forces dwContext to be used.
1925 *|IRF_NO_WAIT - Don't block if the data is not available, just return what is available.
1927 * However, in testing IRF_USE_CONTEXT seems to have no effect - dwContext isn't used.
1929 * SEE
1930 * InternetOpenUrlA(), HttpOpenRequestA()
1932 BOOL WINAPI InternetReadFileExA(HINTERNET hFile, LPINTERNET_BUFFERSA lpBuffersOut,
1933 DWORD dwFlags, DWORD_PTR dwContext)
1935 object_header_t *hdr;
1936 DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
1938 TRACE("(%p %p 0x%x 0x%lx)\n", hFile, lpBuffersOut, dwFlags, dwContext);
1940 hdr = WININET_GetObject(hFile);
1941 if (!hdr) {
1942 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
1943 return FALSE;
1946 if(hdr->vtbl->ReadFileExA)
1947 res = hdr->vtbl->ReadFileExA(hdr, lpBuffersOut, dwFlags, dwContext);
1949 WININET_Release(hdr);
1951 TRACE("-- %s (%u, bytes read: %d)\n", res == ERROR_SUCCESS ? "TRUE": "FALSE",
1952 res, lpBuffersOut->dwBufferLength);
1954 if(res != ERROR_SUCCESS)
1955 SetLastError(res);
1956 return res == ERROR_SUCCESS;
1959 /***********************************************************************
1960 * InternetReadFileExW (WININET.@)
1961 * SEE
1962 * InternetReadFileExA()
1964 BOOL WINAPI InternetReadFileExW(HINTERNET hFile, LPINTERNET_BUFFERSW lpBuffer,
1965 DWORD dwFlags, DWORD_PTR dwContext)
1967 object_header_t *hdr;
1968 DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
1970 TRACE("(%p %p 0x%x 0x%lx)\n", hFile, lpBuffer, dwFlags, dwContext);
1972 hdr = WININET_GetObject(hFile);
1973 if (!hdr) {
1974 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
1975 return FALSE;
1978 if(hdr->vtbl->ReadFileExW)
1979 res = hdr->vtbl->ReadFileExW(hdr, lpBuffer, dwFlags, dwContext);
1981 WININET_Release(hdr);
1983 TRACE("-- %s (%u, bytes read: %d)\n", res == ERROR_SUCCESS ? "TRUE": "FALSE",
1984 res, lpBuffer->dwBufferLength);
1986 if(res != ERROR_SUCCESS)
1987 SetLastError(res);
1988 return res == ERROR_SUCCESS;
1991 DWORD INET_QueryOption(DWORD option, void *buffer, DWORD *size, BOOL unicode)
1993 static BOOL warn = TRUE;
1995 switch(option) {
1996 case INTERNET_OPTION_REQUEST_FLAGS:
1997 TRACE("INTERNET_OPTION_REQUEST_FLAGS\n");
1999 if (*size < sizeof(ULONG))
2000 return ERROR_INSUFFICIENT_BUFFER;
2002 *(ULONG*)buffer = 4;
2003 *size = sizeof(ULONG);
2005 return ERROR_SUCCESS;
2007 case INTERNET_OPTION_HTTP_VERSION:
2008 if (*size < sizeof(HTTP_VERSION_INFO))
2009 return ERROR_INSUFFICIENT_BUFFER;
2012 * Presently hardcoded to 1.1
2014 ((HTTP_VERSION_INFO*)buffer)->dwMajorVersion = 1;
2015 ((HTTP_VERSION_INFO*)buffer)->dwMinorVersion = 1;
2016 *size = sizeof(HTTP_VERSION_INFO);
2018 return ERROR_SUCCESS;
2020 case INTERNET_OPTION_CONNECTED_STATE:
2021 if (warn) {
2022 FIXME("INTERNET_OPTION_CONNECTED_STATE: semi-stub\n");
2023 warn = FALSE;
2025 if (*size < sizeof(ULONG))
2026 return ERROR_INSUFFICIENT_BUFFER;
2028 *(ULONG*)buffer = INTERNET_STATE_CONNECTED;
2029 *size = sizeof(ULONG);
2031 return ERROR_SUCCESS;
2033 case INTERNET_OPTION_PROXY: {
2034 appinfo_t ai;
2035 BOOL ret;
2037 TRACE("Getting global proxy info\n");
2038 memset(&ai, 0, sizeof(appinfo_t));
2039 INTERNET_ConfigureProxy(&ai);
2041 ret = APPINFO_QueryOption(&ai.hdr, INTERNET_OPTION_PROXY, buffer, size, unicode); /* FIXME */
2042 APPINFO_Destroy(&ai.hdr);
2043 return ret;
2046 case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2047 TRACE("INTERNET_OPTION_MAX_CONNS_PER_SERVER\n");
2049 if (*size < sizeof(ULONG))
2050 return ERROR_INSUFFICIENT_BUFFER;
2052 *(ULONG*)buffer = 2;
2053 *size = sizeof(ULONG);
2055 return ERROR_SUCCESS;
2057 case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2058 TRACE("INTERNET_OPTION_MAX_CONNS_1_0_SERVER\n");
2060 if (*size < sizeof(ULONG))
2061 return ERROR_INSUFFICIENT_BUFFER;
2063 *(ULONG*)size = 4;
2064 *size = sizeof(ULONG);
2066 return ERROR_SUCCESS;
2068 case INTERNET_OPTION_SECURITY_FLAGS:
2069 FIXME("INTERNET_OPTION_SECURITY_FLAGS: Stub\n");
2070 return ERROR_SUCCESS;
2072 case INTERNET_OPTION_VERSION: {
2073 static const INTERNET_VERSION_INFO info = { 1, 2 };
2075 TRACE("INTERNET_OPTION_VERSION\n");
2077 if (*size < sizeof(INTERNET_VERSION_INFO))
2078 return ERROR_INSUFFICIENT_BUFFER;
2080 memcpy(buffer, &info, sizeof(info));
2081 *size = sizeof(info);
2083 return ERROR_SUCCESS;
2086 case INTERNET_OPTION_PER_CONNECTION_OPTION: {
2087 INTERNET_PER_CONN_OPTION_LISTW *con = buffer;
2088 INTERNET_PER_CONN_OPTION_LISTA *conA = buffer;
2089 DWORD res = ERROR_SUCCESS, i;
2090 appinfo_t ai;
2092 TRACE("Getting global proxy info\n");
2093 memset(&ai, 0, sizeof(appinfo_t));
2094 INTERNET_ConfigureProxy(&ai);
2096 FIXME("INTERNET_OPTION_PER_CONNECTION_OPTION stub\n");
2098 if (*size < sizeof(INTERNET_PER_CONN_OPTION_LISTW)) {
2099 APPINFO_Destroy(&ai.hdr);
2100 return ERROR_INSUFFICIENT_BUFFER;
2103 for (i = 0; i < con->dwOptionCount; i++) {
2104 INTERNET_PER_CONN_OPTIONW *option = con->pOptions + i;
2105 INTERNET_PER_CONN_OPTIONA *optionA = conA->pOptions + i;
2107 switch (option->dwOption) {
2108 case INTERNET_PER_CONN_FLAGS:
2109 option->Value.dwValue = ai.dwAccessType;
2110 break;
2112 case INTERNET_PER_CONN_PROXY_SERVER:
2113 if (unicode)
2114 option->Value.pszValue = heap_strdupW(ai.lpszProxy);
2115 else
2116 optionA->Value.pszValue = heap_strdupWtoA(ai.lpszProxy);
2117 break;
2119 case INTERNET_PER_CONN_PROXY_BYPASS:
2120 if (unicode)
2121 option->Value.pszValue = heap_strdupW(ai.lpszProxyBypass);
2122 else
2123 optionA->Value.pszValue = heap_strdupWtoA(ai.lpszProxyBypass);
2124 break;
2126 case INTERNET_PER_CONN_AUTOCONFIG_URL:
2127 case INTERNET_PER_CONN_AUTODISCOVERY_FLAGS:
2128 case INTERNET_PER_CONN_AUTOCONFIG_SECONDARY_URL:
2129 case INTERNET_PER_CONN_AUTOCONFIG_RELOAD_DELAY_MINS:
2130 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_TIME:
2131 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_URL:
2132 FIXME("Unhandled dwOption %d\n", option->dwOption);
2133 memset(&option->Value, 0, sizeof(option->Value));
2134 break;
2136 default:
2137 FIXME("Unknown dwOption %d\n", option->dwOption);
2138 res = ERROR_INVALID_PARAMETER;
2139 break;
2142 APPINFO_Destroy(&ai.hdr);
2144 return res;
2148 FIXME("Stub for %d\n", option);
2149 return ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2152 /***********************************************************************
2153 * InternetQueryOptionW (WININET.@)
2155 * Queries an options on the specified handle
2157 * RETURNS
2158 * TRUE on success
2159 * FALSE on failure
2162 BOOL WINAPI InternetQueryOptionW(HINTERNET hInternet, DWORD dwOption,
2163 LPVOID lpBuffer, LPDWORD lpdwBufferLength)
2165 object_header_t *hdr;
2166 DWORD res = ERROR_INVALID_HANDLE;
2168 TRACE("%p %d %p %p\n", hInternet, dwOption, lpBuffer, lpdwBufferLength);
2170 if(hInternet) {
2171 hdr = WININET_GetObject(hInternet);
2172 if (hdr) {
2173 res = hdr->vtbl->QueryOption(hdr, dwOption, lpBuffer, lpdwBufferLength, TRUE);
2174 WININET_Release(hdr);
2176 }else {
2177 res = INET_QueryOption(dwOption, lpBuffer, lpdwBufferLength, TRUE);
2180 if(res != ERROR_SUCCESS)
2181 SetLastError(res);
2182 return res == ERROR_SUCCESS;
2185 /***********************************************************************
2186 * InternetQueryOptionA (WININET.@)
2188 * Queries an options on the specified handle
2190 * RETURNS
2191 * TRUE on success
2192 * FALSE on failure
2195 BOOL WINAPI InternetQueryOptionA(HINTERNET hInternet, DWORD dwOption,
2196 LPVOID lpBuffer, LPDWORD lpdwBufferLength)
2198 object_header_t *hdr;
2199 DWORD res = ERROR_INVALID_HANDLE;
2201 TRACE("%p %d %p %p\n", hInternet, dwOption, lpBuffer, lpdwBufferLength);
2203 if(hInternet) {
2204 hdr = WININET_GetObject(hInternet);
2205 if (hdr) {
2206 res = hdr->vtbl->QueryOption(hdr, dwOption, lpBuffer, lpdwBufferLength, FALSE);
2207 WININET_Release(hdr);
2209 }else {
2210 res = INET_QueryOption(dwOption, lpBuffer, lpdwBufferLength, FALSE);
2213 if(res != ERROR_SUCCESS)
2214 SetLastError(res);
2215 return res == ERROR_SUCCESS;
2219 /***********************************************************************
2220 * InternetSetOptionW (WININET.@)
2222 * Sets an options on the specified handle
2224 * RETURNS
2225 * TRUE on success
2226 * FALSE on failure
2229 BOOL WINAPI InternetSetOptionW(HINTERNET hInternet, DWORD dwOption,
2230 LPVOID lpBuffer, DWORD dwBufferLength)
2232 object_header_t *lpwhh;
2233 BOOL ret = TRUE;
2235 TRACE("(%p %d %p %d)\n", hInternet, dwOption, lpBuffer, dwBufferLength);
2237 lpwhh = (object_header_t*) WININET_GetObject( hInternet );
2238 if(lpwhh && lpwhh->vtbl->SetOption) {
2239 DWORD res;
2241 res = lpwhh->vtbl->SetOption(lpwhh, dwOption, lpBuffer, dwBufferLength);
2242 if(res != ERROR_INTERNET_INVALID_OPTION) {
2243 WININET_Release( lpwhh );
2245 if(res != ERROR_SUCCESS)
2246 SetLastError(res);
2248 return res == ERROR_SUCCESS;
2252 switch (dwOption)
2254 case INTERNET_OPTION_CALLBACK:
2256 if (!lpwhh)
2258 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2259 return FALSE;
2261 WININET_Release(lpwhh);
2262 INTERNET_SetLastError(ERROR_INTERNET_OPTION_NOT_SETTABLE);
2263 return FALSE;
2265 case INTERNET_OPTION_HTTP_VERSION:
2267 HTTP_VERSION_INFO* pVersion=(HTTP_VERSION_INFO*)lpBuffer;
2268 FIXME("Option INTERNET_OPTION_HTTP_VERSION(%d,%d): STUB\n",pVersion->dwMajorVersion,pVersion->dwMinorVersion);
2270 break;
2271 case INTERNET_OPTION_ERROR_MASK:
2273 ULONG flags = *(ULONG *)lpBuffer;
2274 FIXME("Option INTERNET_OPTION_ERROR_MASK(%d): STUB\n", flags);
2276 break;
2277 case INTERNET_OPTION_CODEPAGE:
2279 ULONG codepage = *(ULONG *)lpBuffer;
2280 FIXME("Option INTERNET_OPTION_CODEPAGE (%d): STUB\n", codepage);
2282 break;
2283 case INTERNET_OPTION_REQUEST_PRIORITY:
2285 ULONG priority = *(ULONG *)lpBuffer;
2286 FIXME("Option INTERNET_OPTION_REQUEST_PRIORITY (%d): STUB\n", priority);
2288 break;
2289 case INTERNET_OPTION_CONNECT_TIMEOUT:
2291 ULONG connecttimeout = *(ULONG *)lpBuffer;
2292 FIXME("Option INTERNET_OPTION_CONNECT_TIMEOUT (%d): STUB\n", connecttimeout);
2294 break;
2295 case INTERNET_OPTION_DATA_RECEIVE_TIMEOUT:
2297 ULONG receivetimeout = *(ULONG *)lpBuffer;
2298 FIXME("Option INTERNET_OPTION_DATA_RECEIVE_TIMEOUT (%d): STUB\n", receivetimeout);
2300 break;
2301 case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2303 ULONG conns = *(ULONG *)lpBuffer;
2304 FIXME("Option INTERNET_OPTION_MAX_CONNS_PER_SERVER (%d): STUB\n", conns);
2306 break;
2307 case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2309 ULONG conns = *(ULONG *)lpBuffer;
2310 FIXME("Option INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER (%d): STUB\n", conns);
2312 break;
2313 case INTERNET_OPTION_RESET_URLCACHE_SESSION:
2314 FIXME("Option INTERNET_OPTION_RESET_URLCACHE_SESSION: STUB\n");
2315 break;
2316 case INTERNET_OPTION_END_BROWSER_SESSION:
2317 FIXME("Option INTERNET_OPTION_END_BROWSER_SESSION: STUB\n");
2318 break;
2319 case INTERNET_OPTION_CONNECTED_STATE:
2320 FIXME("Option INTERNET_OPTION_CONNECTED_STATE: STUB\n");
2321 break;
2322 case INTERNET_OPTION_DISABLE_PASSPORT_AUTH:
2323 TRACE("Option INTERNET_OPTION_DISABLE_PASSPORT_AUTH: harmless stub, since not enabled\n");
2324 break;
2325 case INTERNET_OPTION_SEND_TIMEOUT:
2326 case INTERNET_OPTION_RECEIVE_TIMEOUT:
2328 ULONG timeout = *(ULONG *)lpBuffer;
2329 FIXME("INTERNET_OPTION_SEND/RECEIVE_TIMEOUT %d\n", timeout);
2330 break;
2332 case INTERNET_OPTION_CONNECT_RETRIES:
2334 ULONG retries = *(ULONG *)lpBuffer;
2335 FIXME("INTERNET_OPTION_CONNECT_RETRIES %d\n", retries);
2336 break;
2338 case INTERNET_OPTION_CONTEXT_VALUE:
2339 FIXME("Option INTERNET_OPTION_CONTEXT_VALUE; STUB\n");
2340 break;
2341 case INTERNET_OPTION_SECURITY_FLAGS:
2342 FIXME("Option INTERNET_OPTION_SECURITY_FLAGS; STUB\n");
2343 break;
2344 case INTERNET_OPTION_DISABLE_AUTODIAL:
2345 FIXME("Option INTERNET_OPTION_DISABLE_AUTODIAL; STUB\n");
2346 break;
2347 case INTERNET_OPTION_HTTP_DECODING:
2348 FIXME("INTERNET_OPTION_HTTP_DECODING; STUB\n");
2349 INTERNET_SetLastError(ERROR_INTERNET_INVALID_OPTION);
2350 ret = FALSE;
2351 break;
2352 case INTERNET_OPTION_COOKIES_3RD_PARTY:
2353 FIXME("INTERNET_OPTION_COOKIES_3RD_PARTY; STUB\n");
2354 INTERNET_SetLastError(ERROR_INTERNET_INVALID_OPTION);
2355 ret = FALSE;
2356 break;
2357 case INTERNET_OPTION_SEND_UTF8_SERVERNAME_TO_PROXY:
2358 FIXME("INTERNET_OPTION_SEND_UTF8_SERVERNAME_TO_PROXY; STUB\n");
2359 INTERNET_SetLastError(ERROR_INTERNET_INVALID_OPTION);
2360 ret = FALSE;
2361 break;
2362 case INTERNET_OPTION_CODEPAGE_PATH:
2363 FIXME("INTERNET_OPTION_CODEPAGE_PATH; STUB\n");
2364 INTERNET_SetLastError(ERROR_INTERNET_INVALID_OPTION);
2365 ret = FALSE;
2366 break;
2367 case INTERNET_OPTION_CODEPAGE_EXTRA:
2368 FIXME("INTERNET_OPTION_CODEPAGE_EXTRA; STUB\n");
2369 INTERNET_SetLastError(ERROR_INTERNET_INVALID_OPTION);
2370 ret = FALSE;
2371 break;
2372 case INTERNET_OPTION_IDN:
2373 FIXME("INTERNET_OPTION_IDN; STUB\n");
2374 INTERNET_SetLastError(ERROR_INTERNET_INVALID_OPTION);
2375 ret = FALSE;
2376 break;
2377 default:
2378 FIXME("Option %d STUB\n",dwOption);
2379 INTERNET_SetLastError(ERROR_INTERNET_INVALID_OPTION);
2380 ret = FALSE;
2381 break;
2384 if(lpwhh)
2385 WININET_Release( lpwhh );
2387 return ret;
2391 /***********************************************************************
2392 * InternetSetOptionA (WININET.@)
2394 * Sets an options on the specified handle.
2396 * RETURNS
2397 * TRUE on success
2398 * FALSE on failure
2401 BOOL WINAPI InternetSetOptionA(HINTERNET hInternet, DWORD dwOption,
2402 LPVOID lpBuffer, DWORD dwBufferLength)
2404 LPVOID wbuffer;
2405 DWORD wlen;
2406 BOOL r;
2408 switch( dwOption )
2410 case INTERNET_OPTION_CALLBACK:
2412 object_header_t *lpwh;
2414 if (!(lpwh = WININET_GetObject(hInternet)))
2416 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2417 return FALSE;
2419 WININET_Release(lpwh);
2420 INTERNET_SetLastError(ERROR_INTERNET_OPTION_NOT_SETTABLE);
2421 return FALSE;
2423 case INTERNET_OPTION_PROXY:
2425 LPINTERNET_PROXY_INFOA pi = (LPINTERNET_PROXY_INFOA) lpBuffer;
2426 LPINTERNET_PROXY_INFOW piw;
2427 DWORD proxlen, prbylen;
2428 LPWSTR prox, prby;
2430 proxlen = MultiByteToWideChar( CP_ACP, 0, pi->lpszProxy, -1, NULL, 0);
2431 prbylen= MultiByteToWideChar( CP_ACP, 0, pi->lpszProxyBypass, -1, NULL, 0);
2432 wlen = sizeof(*piw) + proxlen + prbylen;
2433 wbuffer = HeapAlloc( GetProcessHeap(), 0, wlen*sizeof(WCHAR) );
2434 piw = (LPINTERNET_PROXY_INFOW) wbuffer;
2435 piw->dwAccessType = pi->dwAccessType;
2436 prox = (LPWSTR) &piw[1];
2437 prby = &prox[proxlen+1];
2438 MultiByteToWideChar( CP_ACP, 0, pi->lpszProxy, -1, prox, proxlen);
2439 MultiByteToWideChar( CP_ACP, 0, pi->lpszProxyBypass, -1, prby, prbylen);
2440 piw->lpszProxy = prox;
2441 piw->lpszProxyBypass = prby;
2443 break;
2444 case INTERNET_OPTION_USER_AGENT:
2445 case INTERNET_OPTION_USERNAME:
2446 case INTERNET_OPTION_PASSWORD:
2447 wlen = MultiByteToWideChar( CP_ACP, 0, lpBuffer, dwBufferLength,
2448 NULL, 0 );
2449 wbuffer = HeapAlloc( GetProcessHeap(), 0, wlen*sizeof(WCHAR) );
2450 MultiByteToWideChar( CP_ACP, 0, lpBuffer, dwBufferLength,
2451 wbuffer, wlen );
2452 break;
2453 default:
2454 wbuffer = lpBuffer;
2455 wlen = dwBufferLength;
2458 r = InternetSetOptionW(hInternet,dwOption, wbuffer, wlen);
2460 if( lpBuffer != wbuffer )
2461 HeapFree( GetProcessHeap(), 0, wbuffer );
2463 return r;
2467 /***********************************************************************
2468 * InternetSetOptionExA (WININET.@)
2470 BOOL WINAPI InternetSetOptionExA(HINTERNET hInternet, DWORD dwOption,
2471 LPVOID lpBuffer, DWORD dwBufferLength, DWORD dwFlags)
2473 FIXME("Flags %08x ignored\n", dwFlags);
2474 return InternetSetOptionA( hInternet, dwOption, lpBuffer, dwBufferLength );
2477 /***********************************************************************
2478 * InternetSetOptionExW (WININET.@)
2480 BOOL WINAPI InternetSetOptionExW(HINTERNET hInternet, DWORD dwOption,
2481 LPVOID lpBuffer, DWORD dwBufferLength, DWORD dwFlags)
2483 FIXME("Flags %08x ignored\n", dwFlags);
2484 if( dwFlags & ~ISO_VALID_FLAGS )
2486 INTERNET_SetLastError( ERROR_INVALID_PARAMETER );
2487 return FALSE;
2489 return InternetSetOptionW( hInternet, dwOption, lpBuffer, dwBufferLength );
2492 static const WCHAR WININET_wkday[7][4] =
2493 { { 'S','u','n', 0 }, { 'M','o','n', 0 }, { 'T','u','e', 0 }, { 'W','e','d', 0 },
2494 { 'T','h','u', 0 }, { 'F','r','i', 0 }, { 'S','a','t', 0 } };
2495 static const WCHAR WININET_month[12][4] =
2496 { { 'J','a','n', 0 }, { 'F','e','b', 0 }, { 'M','a','r', 0 }, { 'A','p','r', 0 },
2497 { 'M','a','y', 0 }, { 'J','u','n', 0 }, { 'J','u','l', 0 }, { 'A','u','g', 0 },
2498 { 'S','e','p', 0 }, { 'O','c','t', 0 }, { 'N','o','v', 0 }, { 'D','e','c', 0 } };
2500 /***********************************************************************
2501 * InternetTimeFromSystemTimeA (WININET.@)
2503 BOOL WINAPI InternetTimeFromSystemTimeA( const SYSTEMTIME* time, DWORD format, LPSTR string, DWORD size )
2505 BOOL ret;
2506 WCHAR stringW[INTERNET_RFC1123_BUFSIZE];
2508 TRACE( "%p 0x%08x %p 0x%08x\n", time, format, string, size );
2510 if (!time || !string || format != INTERNET_RFC1123_FORMAT)
2512 SetLastError(ERROR_INVALID_PARAMETER);
2513 return FALSE;
2516 if (size < INTERNET_RFC1123_BUFSIZE * sizeof(*string))
2518 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2519 return FALSE;
2522 ret = InternetTimeFromSystemTimeW( time, format, stringW, sizeof(stringW) );
2523 if (ret) WideCharToMultiByte( CP_ACP, 0, stringW, -1, string, size, NULL, NULL );
2525 return ret;
2528 /***********************************************************************
2529 * InternetTimeFromSystemTimeW (WININET.@)
2531 BOOL WINAPI InternetTimeFromSystemTimeW( const SYSTEMTIME* time, DWORD format, LPWSTR string, DWORD size )
2533 static const WCHAR date[] =
2534 { '%','s',',',' ','%','0','2','d',' ','%','s',' ','%','4','d',' ','%','0',
2535 '2','d',':','%','0','2','d',':','%','0','2','d',' ','G','M','T', 0 };
2537 TRACE( "%p 0x%08x %p 0x%08x\n", time, format, string, size );
2539 if (!time || !string || format != INTERNET_RFC1123_FORMAT)
2541 SetLastError(ERROR_INVALID_PARAMETER);
2542 return FALSE;
2545 if (size < INTERNET_RFC1123_BUFSIZE * sizeof(*string))
2547 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2548 return FALSE;
2551 sprintfW( string, date,
2552 WININET_wkday[time->wDayOfWeek],
2553 time->wDay,
2554 WININET_month[time->wMonth - 1],
2555 time->wYear,
2556 time->wHour,
2557 time->wMinute,
2558 time->wSecond );
2560 return TRUE;
2563 /***********************************************************************
2564 * InternetTimeToSystemTimeA (WININET.@)
2566 BOOL WINAPI InternetTimeToSystemTimeA( LPCSTR string, SYSTEMTIME* time, DWORD reserved )
2568 BOOL ret = FALSE;
2569 WCHAR *stringW;
2571 TRACE( "%s %p 0x%08x\n", debugstr_a(string), time, reserved );
2573 stringW = heap_strdupAtoW(string);
2574 if (stringW)
2576 ret = InternetTimeToSystemTimeW( stringW, time, reserved );
2577 HeapFree( GetProcessHeap(), 0, stringW );
2579 return ret;
2582 /***********************************************************************
2583 * InternetTimeToSystemTimeW (WININET.@)
2585 BOOL WINAPI InternetTimeToSystemTimeW( LPCWSTR string, SYSTEMTIME* time, DWORD reserved )
2587 unsigned int i;
2588 const WCHAR *s = string;
2589 WCHAR *end;
2591 TRACE( "%s %p 0x%08x\n", debugstr_w(string), time, reserved );
2593 if (!string || !time) return FALSE;
2595 /* Windows does this too */
2596 GetSystemTime( time );
2598 /* Convert an RFC1123 time such as 'Fri, 07 Jan 2005 12:06:35 GMT' into
2599 * a SYSTEMTIME structure.
2602 while (*s && !isalphaW( *s )) s++;
2603 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
2604 time->wDayOfWeek = 7;
2606 for (i = 0; i < 7; i++)
2608 if (toupperW( WININET_wkday[i][0] ) == toupperW( s[0] ) &&
2609 toupperW( WININET_wkday[i][1] ) == toupperW( s[1] ) &&
2610 toupperW( WININET_wkday[i][2] ) == toupperW( s[2] ) )
2612 time->wDayOfWeek = i;
2613 break;
2617 if (time->wDayOfWeek > 6) return TRUE;
2618 while (*s && !isdigitW( *s )) s++;
2619 time->wDay = strtolW( s, &end, 10 );
2620 s = end;
2622 while (*s && !isalphaW( *s )) s++;
2623 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
2624 time->wMonth = 0;
2626 for (i = 0; i < 12; i++)
2628 if (toupperW( WININET_month[i][0]) == toupperW( s[0] ) &&
2629 toupperW( WININET_month[i][1]) == toupperW( s[1] ) &&
2630 toupperW( WININET_month[i][2]) == toupperW( s[2] ) )
2632 time->wMonth = i + 1;
2633 break;
2636 if (time->wMonth == 0) return TRUE;
2638 while (*s && !isdigitW( *s )) s++;
2639 if (*s == '\0') return TRUE;
2640 time->wYear = strtolW( s, &end, 10 );
2641 s = end;
2643 while (*s && !isdigitW( *s )) s++;
2644 if (*s == '\0') return TRUE;
2645 time->wHour = strtolW( s, &end, 10 );
2646 s = end;
2648 while (*s && !isdigitW( *s )) s++;
2649 if (*s == '\0') return TRUE;
2650 time->wMinute = strtolW( s, &end, 10 );
2651 s = end;
2653 while (*s && !isdigitW( *s )) s++;
2654 if (*s == '\0') return TRUE;
2655 time->wSecond = strtolW( s, &end, 10 );
2656 s = end;
2658 time->wMilliseconds = 0;
2659 return TRUE;
2662 /***********************************************************************
2663 * InternetCheckConnectionW (WININET.@)
2665 * Pings a requested host to check internet connection
2667 * RETURNS
2668 * TRUE on success and FALSE on failure. If a failure then
2669 * ERROR_NOT_CONNECTED is placed into GetLastError
2672 BOOL WINAPI InternetCheckConnectionW( LPCWSTR lpszUrl, DWORD dwFlags, DWORD dwReserved )
2675 * this is a kludge which runs the resident ping program and reads the output.
2677 * Anyone have a better idea?
2680 BOOL rc = FALSE;
2681 static const CHAR ping[] = "ping -c 1 ";
2682 static const CHAR redirect[] = " >/dev/null 2>/dev/null";
2683 CHAR *command = NULL;
2684 WCHAR hostW[1024];
2685 DWORD len;
2686 INTERNET_PORT port;
2687 int status = -1;
2689 FIXME("\n");
2692 * Crack or set the Address
2694 if (lpszUrl == NULL)
2697 * According to the doc we are supposed to use the ip for the next
2698 * server in the WnInet internal server database. I have
2699 * no idea what that is or how to get it.
2701 * So someone needs to implement this.
2703 FIXME("Unimplemented with URL of NULL\n");
2704 return TRUE;
2706 else
2708 URL_COMPONENTSW components;
2710 ZeroMemory(&components,sizeof(URL_COMPONENTSW));
2711 components.lpszHostName = (LPWSTR)hostW;
2712 components.dwHostNameLength = 1024;
2714 if (!InternetCrackUrlW(lpszUrl,0,0,&components))
2715 goto End;
2717 TRACE("host name : %s\n",debugstr_w(components.lpszHostName));
2718 port = components.nPort;
2719 TRACE("port: %d\n", port);
2722 if (dwFlags & FLAG_ICC_FORCE_CONNECTION)
2724 struct sockaddr_storage saddr;
2725 socklen_t sa_len = sizeof(saddr);
2726 int fd;
2728 if (!GetAddress(hostW, port, (struct sockaddr *)&saddr, &sa_len))
2729 goto End;
2730 fd = socket(saddr.ss_family, SOCK_STREAM, 0);
2731 if (fd != -1)
2733 if (connect(fd, (struct sockaddr *)&saddr, sa_len) == 0)
2734 rc = TRUE;
2735 close(fd);
2738 else
2741 * Build our ping command
2743 len = WideCharToMultiByte(CP_UNIXCP, 0, hostW, -1, NULL, 0, NULL, NULL);
2744 command = HeapAlloc( GetProcessHeap(), 0, strlen(ping)+len+strlen(redirect) );
2745 strcpy(command,ping);
2746 WideCharToMultiByte(CP_UNIXCP, 0, hostW, -1, command+strlen(ping), len, NULL, NULL);
2747 strcat(command,redirect);
2749 TRACE("Ping command is : %s\n",command);
2751 status = system(command);
2753 TRACE("Ping returned a code of %i\n",status);
2755 /* Ping return code of 0 indicates success */
2756 if (status == 0)
2757 rc = TRUE;
2760 End:
2762 HeapFree( GetProcessHeap(), 0, command );
2763 if (rc == FALSE)
2764 INTERNET_SetLastError(ERROR_NOT_CONNECTED);
2766 return rc;
2770 /***********************************************************************
2771 * InternetCheckConnectionA (WININET.@)
2773 * Pings a requested host to check internet connection
2775 * RETURNS
2776 * TRUE on success and FALSE on failure. If a failure then
2777 * ERROR_NOT_CONNECTED is placed into GetLastError
2780 BOOL WINAPI InternetCheckConnectionA(LPCSTR lpszUrl, DWORD dwFlags, DWORD dwReserved)
2782 WCHAR *url = NULL;
2783 BOOL rc;
2785 if(lpszUrl) {
2786 url = heap_strdupAtoW(lpszUrl);
2787 if(!url)
2788 return FALSE;
2791 rc = InternetCheckConnectionW(url, dwFlags, dwReserved);
2793 HeapFree(GetProcessHeap(), 0, url);
2794 return rc;
2798 /**********************************************************
2799 * INTERNET_InternetOpenUrlW (internal)
2801 * Opens an URL
2803 * RETURNS
2804 * handle of connection or NULL on failure
2806 static HINTERNET INTERNET_InternetOpenUrlW(appinfo_t *hIC, LPCWSTR lpszUrl,
2807 LPCWSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext)
2809 URL_COMPONENTSW urlComponents;
2810 WCHAR protocol[32], hostName[MAXHOSTNAME], userName[1024];
2811 WCHAR password[1024], path[2048], extra[1024];
2812 HINTERNET client = NULL, client1 = NULL;
2814 TRACE("(%p, %s, %s, %08x, %08x, %08lx)\n", hIC, debugstr_w(lpszUrl), debugstr_w(lpszHeaders),
2815 dwHeadersLength, dwFlags, dwContext);
2817 urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
2818 urlComponents.lpszScheme = protocol;
2819 urlComponents.dwSchemeLength = 32;
2820 urlComponents.lpszHostName = hostName;
2821 urlComponents.dwHostNameLength = MAXHOSTNAME;
2822 urlComponents.lpszUserName = userName;
2823 urlComponents.dwUserNameLength = 1024;
2824 urlComponents.lpszPassword = password;
2825 urlComponents.dwPasswordLength = 1024;
2826 urlComponents.lpszUrlPath = path;
2827 urlComponents.dwUrlPathLength = 2048;
2828 urlComponents.lpszExtraInfo = extra;
2829 urlComponents.dwExtraInfoLength = 1024;
2830 if(!InternetCrackUrlW(lpszUrl, strlenW(lpszUrl), 0, &urlComponents))
2831 return NULL;
2832 switch(urlComponents.nScheme) {
2833 case INTERNET_SCHEME_FTP:
2834 if(urlComponents.nPort == 0)
2835 urlComponents.nPort = INTERNET_DEFAULT_FTP_PORT;
2836 client = FTP_Connect(hIC, hostName, urlComponents.nPort,
2837 userName, password, dwFlags, dwContext, INET_OPENURL);
2838 if(client == NULL)
2839 break;
2840 client1 = FtpOpenFileW(client, path, GENERIC_READ, dwFlags, dwContext);
2841 if(client1 == NULL) {
2842 InternetCloseHandle(client);
2843 break;
2845 break;
2847 case INTERNET_SCHEME_HTTP:
2848 case INTERNET_SCHEME_HTTPS: {
2849 static const WCHAR szStars[] = { '*','/','*', 0 };
2850 LPCWSTR accept[2] = { szStars, NULL };
2851 if(urlComponents.nPort == 0) {
2852 if(urlComponents.nScheme == INTERNET_SCHEME_HTTP)
2853 urlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
2854 else
2855 urlComponents.nPort = INTERNET_DEFAULT_HTTPS_PORT;
2857 if (urlComponents.nScheme == INTERNET_SCHEME_HTTPS) dwFlags |= INTERNET_FLAG_SECURE;
2859 /* FIXME: should use pointers, not handles, as handles are not thread-safe */
2860 client = HTTP_Connect(hIC, hostName, urlComponents.nPort,
2861 userName, password, dwFlags, dwContext, INET_OPENURL);
2862 if(client == NULL)
2863 break;
2865 if (urlComponents.dwExtraInfoLength) {
2866 WCHAR *path_extra;
2867 DWORD len = urlComponents.dwUrlPathLength + urlComponents.dwExtraInfoLength + 1;
2869 if (!(path_extra = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR))))
2871 InternetCloseHandle(client);
2872 break;
2874 strcpyW(path_extra, urlComponents.lpszUrlPath);
2875 strcatW(path_extra, urlComponents.lpszExtraInfo);
2876 client1 = HttpOpenRequestW(client, NULL, path_extra, NULL, NULL, accept, dwFlags, dwContext);
2877 HeapFree(GetProcessHeap(), 0, path_extra);
2879 else
2880 client1 = HttpOpenRequestW(client, NULL, path, NULL, NULL, accept, dwFlags, dwContext);
2882 if(client1 == NULL) {
2883 InternetCloseHandle(client);
2884 break;
2886 HttpAddRequestHeadersW(client1, lpszHeaders, dwHeadersLength, HTTP_ADDREQ_FLAG_ADD);
2887 if (!HttpSendRequestW(client1, NULL, 0, NULL, 0) &&
2888 GetLastError() != ERROR_IO_PENDING) {
2889 InternetCloseHandle(client1);
2890 client1 = NULL;
2891 break;
2894 case INTERNET_SCHEME_GOPHER:
2895 /* gopher doesn't seem to be implemented in wine, but it's supposed
2896 * to be supported by InternetOpenUrlA. */
2897 default:
2898 INTERNET_SetLastError(ERROR_INTERNET_UNRECOGNIZED_SCHEME);
2899 break;
2902 TRACE(" %p <--\n", client1);
2904 return client1;
2907 /**********************************************************
2908 * InternetOpenUrlW (WININET.@)
2910 * Opens an URL
2912 * RETURNS
2913 * handle of connection or NULL on failure
2915 static void AsyncInternetOpenUrlProc(WORKREQUEST *workRequest)
2917 struct WORKREQ_INTERNETOPENURLW const *req = &workRequest->u.InternetOpenUrlW;
2918 appinfo_t *hIC = (appinfo_t*) workRequest->hdr;
2920 TRACE("%p\n", hIC);
2922 INTERNET_InternetOpenUrlW(hIC, req->lpszUrl,
2923 req->lpszHeaders, req->dwHeadersLength, req->dwFlags, req->dwContext);
2924 HeapFree(GetProcessHeap(), 0, req->lpszUrl);
2925 HeapFree(GetProcessHeap(), 0, req->lpszHeaders);
2928 HINTERNET WINAPI InternetOpenUrlW(HINTERNET hInternet, LPCWSTR lpszUrl,
2929 LPCWSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext)
2931 HINTERNET ret = NULL;
2932 appinfo_t *hIC = NULL;
2934 if (TRACE_ON(wininet)) {
2935 TRACE("(%p, %s, %s, %08x, %08x, %08lx)\n", hInternet, debugstr_w(lpszUrl), debugstr_w(lpszHeaders),
2936 dwHeadersLength, dwFlags, dwContext);
2937 TRACE(" flags :");
2938 dump_INTERNET_FLAGS(dwFlags);
2941 if (!lpszUrl)
2943 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
2944 goto lend;
2947 hIC = (appinfo_t*)WININET_GetObject( hInternet );
2948 if (NULL == hIC || hIC->hdr.htype != WH_HINIT) {
2949 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2950 goto lend;
2953 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC) {
2954 WORKREQUEST workRequest;
2955 struct WORKREQ_INTERNETOPENURLW *req;
2957 workRequest.asyncproc = AsyncInternetOpenUrlProc;
2958 workRequest.hdr = WININET_AddRef( &hIC->hdr );
2959 req = &workRequest.u.InternetOpenUrlW;
2960 req->lpszUrl = heap_strdupW(lpszUrl);
2961 req->lpszHeaders = heap_strdupW(lpszHeaders);
2962 req->dwHeadersLength = dwHeadersLength;
2963 req->dwFlags = dwFlags;
2964 req->dwContext = dwContext;
2966 INTERNET_AsyncCall(&workRequest);
2968 * This is from windows.
2970 INTERNET_SetLastError(ERROR_IO_PENDING);
2971 } else {
2972 ret = INTERNET_InternetOpenUrlW(hIC, lpszUrl, lpszHeaders, dwHeadersLength, dwFlags, dwContext);
2975 lend:
2976 if( hIC )
2977 WININET_Release( &hIC->hdr );
2978 TRACE(" %p <--\n", ret);
2980 return ret;
2983 /**********************************************************
2984 * InternetOpenUrlA (WININET.@)
2986 * Opens an URL
2988 * RETURNS
2989 * handle of connection or NULL on failure
2991 HINTERNET WINAPI InternetOpenUrlA(HINTERNET hInternet, LPCSTR lpszUrl,
2992 LPCSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext)
2994 HINTERNET rc = NULL;
2995 DWORD lenHeaders = 0;
2996 LPWSTR szUrl = NULL;
2997 LPWSTR szHeaders = NULL;
2999 TRACE("\n");
3001 if(lpszUrl) {
3002 szUrl = heap_strdupAtoW(lpszUrl);
3003 if(!szUrl)
3004 return NULL;
3007 if(lpszHeaders) {
3008 lenHeaders = MultiByteToWideChar(CP_ACP, 0, lpszHeaders, dwHeadersLength, NULL, 0 );
3009 szHeaders = HeapAlloc(GetProcessHeap(), 0, lenHeaders*sizeof(WCHAR));
3010 if(!szHeaders) {
3011 HeapFree(GetProcessHeap(), 0, szUrl);
3012 return NULL;
3014 MultiByteToWideChar(CP_ACP, 0, lpszHeaders, dwHeadersLength, szHeaders, lenHeaders);
3017 rc = InternetOpenUrlW(hInternet, szUrl, szHeaders,
3018 lenHeaders, dwFlags, dwContext);
3020 HeapFree(GetProcessHeap(), 0, szUrl);
3021 HeapFree(GetProcessHeap(), 0, szHeaders);
3023 return rc;
3027 static LPWITHREADERROR INTERNET_AllocThreadError(void)
3029 LPWITHREADERROR lpwite = HeapAlloc(GetProcessHeap(), 0, sizeof(*lpwite));
3031 if (lpwite)
3033 lpwite->dwError = 0;
3034 lpwite->response[0] = '\0';
3037 if (!TlsSetValue(g_dwTlsErrIndex, lpwite))
3039 HeapFree(GetProcessHeap(), 0, lpwite);
3040 return NULL;
3043 return lpwite;
3047 /***********************************************************************
3048 * INTERNET_SetLastError (internal)
3050 * Set last thread specific error
3052 * RETURNS
3055 void INTERNET_SetLastError(DWORD dwError)
3057 LPWITHREADERROR lpwite = TlsGetValue(g_dwTlsErrIndex);
3059 if (!lpwite)
3060 lpwite = INTERNET_AllocThreadError();
3062 SetLastError(dwError);
3063 if(lpwite)
3064 lpwite->dwError = dwError;
3068 /***********************************************************************
3069 * INTERNET_GetLastError (internal)
3071 * Get last thread specific error
3073 * RETURNS
3076 DWORD INTERNET_GetLastError(void)
3078 LPWITHREADERROR lpwite = TlsGetValue(g_dwTlsErrIndex);
3079 if (!lpwite) return 0;
3080 /* TlsGetValue clears last error, so set it again here */
3081 SetLastError(lpwite->dwError);
3082 return lpwite->dwError;
3086 /***********************************************************************
3087 * INTERNET_WorkerThreadFunc (internal)
3089 * Worker thread execution function
3091 * RETURNS
3094 static DWORD CALLBACK INTERNET_WorkerThreadFunc(LPVOID lpvParam)
3096 LPWORKREQUEST lpRequest = lpvParam;
3097 WORKREQUEST workRequest;
3099 TRACE("\n");
3101 workRequest = *lpRequest;
3102 HeapFree(GetProcessHeap(), 0, lpRequest);
3104 workRequest.asyncproc(&workRequest);
3106 WININET_Release( workRequest.hdr );
3107 return TRUE;
3111 /***********************************************************************
3112 * INTERNET_AsyncCall (internal)
3114 * Retrieves work request from queue
3116 * RETURNS
3119 BOOL INTERNET_AsyncCall(LPWORKREQUEST lpWorkRequest)
3121 BOOL bSuccess;
3122 LPWORKREQUEST lpNewRequest;
3124 TRACE("\n");
3126 lpNewRequest = HeapAlloc(GetProcessHeap(), 0, sizeof(WORKREQUEST));
3127 if (!lpNewRequest)
3128 return FALSE;
3130 *lpNewRequest = *lpWorkRequest;
3132 bSuccess = QueueUserWorkItem(INTERNET_WorkerThreadFunc, lpNewRequest, WT_EXECUTELONGFUNCTION);
3133 if (!bSuccess)
3135 HeapFree(GetProcessHeap(), 0, lpNewRequest);
3136 INTERNET_SetLastError(ERROR_INTERNET_ASYNC_THREAD_FAILED);
3139 return bSuccess;
3143 /***********************************************************************
3144 * INTERNET_GetResponseBuffer (internal)
3146 * RETURNS
3149 LPSTR INTERNET_GetResponseBuffer(void)
3151 LPWITHREADERROR lpwite = TlsGetValue(g_dwTlsErrIndex);
3152 if (!lpwite)
3153 lpwite = INTERNET_AllocThreadError();
3154 TRACE("\n");
3155 return lpwite->response;
3158 /***********************************************************************
3159 * INTERNET_GetNextLine (internal)
3161 * Parse next line in directory string listing
3163 * RETURNS
3164 * Pointer to beginning of next line
3165 * NULL on failure
3169 LPSTR INTERNET_GetNextLine(INT nSocket, LPDWORD dwLen)
3171 struct pollfd pfd;
3172 BOOL bSuccess = FALSE;
3173 INT nRecv = 0;
3174 LPSTR lpszBuffer = INTERNET_GetResponseBuffer();
3176 TRACE("\n");
3178 pfd.fd = nSocket;
3179 pfd.events = POLLIN;
3181 while (nRecv < MAX_REPLY_LEN)
3183 if (poll(&pfd,1, RESPONSE_TIMEOUT * 1000) > 0)
3185 if (recv(nSocket, &lpszBuffer[nRecv], 1, 0) <= 0)
3187 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
3188 goto lend;
3191 if (lpszBuffer[nRecv] == '\n')
3193 bSuccess = TRUE;
3194 break;
3196 if (lpszBuffer[nRecv] != '\r')
3197 nRecv++;
3199 else
3201 INTERNET_SetLastError(ERROR_INTERNET_TIMEOUT);
3202 goto lend;
3206 lend:
3207 if (bSuccess)
3209 lpszBuffer[nRecv] = '\0';
3210 *dwLen = nRecv - 1;
3211 TRACE(":%d %s\n", nRecv, lpszBuffer);
3212 return lpszBuffer;
3214 else
3216 return NULL;
3220 /**********************************************************
3221 * InternetQueryDataAvailable (WININET.@)
3223 * Determines how much data is available to be read.
3225 * RETURNS
3226 * TRUE on success, FALSE if an error occurred. If
3227 * INTERNET_FLAG_ASYNC was specified in InternetOpen, and
3228 * no data is presently available, FALSE is returned with
3229 * the last error ERROR_IO_PENDING; a callback with status
3230 * INTERNET_STATUS_REQUEST_COMPLETE will be sent when more
3231 * data is available.
3233 BOOL WINAPI InternetQueryDataAvailable( HINTERNET hFile,
3234 LPDWORD lpdwNumberOfBytesAvailble,
3235 DWORD dwFlags, DWORD_PTR dwContext)
3237 object_header_t *hdr;
3238 DWORD res;
3240 TRACE("(%p %p %x %lx)\n", hFile, lpdwNumberOfBytesAvailble, dwFlags, dwContext);
3242 hdr = WININET_GetObject( hFile );
3243 if (!hdr) {
3244 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
3245 return FALSE;
3248 if(hdr->vtbl->QueryDataAvailable) {
3249 res = hdr->vtbl->QueryDataAvailable(hdr, lpdwNumberOfBytesAvailble, dwFlags, dwContext);
3250 }else {
3251 WARN("wrong handle\n");
3252 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
3255 WININET_Release(hdr);
3257 if(res != ERROR_SUCCESS)
3258 SetLastError(res);
3259 return res == ERROR_SUCCESS;
3263 /***********************************************************************
3264 * InternetLockRequestFile (WININET.@)
3266 BOOL WINAPI InternetLockRequestFile( HINTERNET hInternet, HANDLE
3267 *lphLockReqHandle)
3269 FIXME("STUB\n");
3270 return FALSE;
3273 BOOL WINAPI InternetUnlockRequestFile( HANDLE hLockHandle)
3275 FIXME("STUB\n");
3276 return FALSE;
3280 /***********************************************************************
3281 * InternetAutodial (WININET.@)
3283 * On windows this function is supposed to dial the default internet
3284 * connection. We don't want to have Wine dial out to the internet so
3285 * we return TRUE by default. It might be nice to check if we are connected.
3287 * RETURNS
3288 * TRUE on success
3289 * FALSE on failure
3292 BOOL WINAPI InternetAutodial(DWORD dwFlags, HWND hwndParent)
3294 FIXME("STUB\n");
3296 /* Tell that we are connected to the internet. */
3297 return TRUE;
3300 /***********************************************************************
3301 * InternetAutodialHangup (WININET.@)
3303 * Hangs up a connection made with InternetAutodial
3305 * PARAM
3306 * dwReserved
3307 * RETURNS
3308 * TRUE on success
3309 * FALSE on failure
3312 BOOL WINAPI InternetAutodialHangup(DWORD dwReserved)
3314 FIXME("STUB\n");
3316 /* we didn't dial, we don't disconnect */
3317 return TRUE;
3320 /***********************************************************************
3321 * InternetCombineUrlA (WININET.@)
3323 * Combine a base URL with a relative URL
3325 * RETURNS
3326 * TRUE on success
3327 * FALSE on failure
3331 BOOL WINAPI InternetCombineUrlA(LPCSTR lpszBaseUrl, LPCSTR lpszRelativeUrl,
3332 LPSTR lpszBuffer, LPDWORD lpdwBufferLength,
3333 DWORD dwFlags)
3335 HRESULT hr=S_OK;
3337 TRACE("(%s, %s, %p, %p, 0x%08x)\n", debugstr_a(lpszBaseUrl), debugstr_a(lpszRelativeUrl), lpszBuffer, lpdwBufferLength, dwFlags);
3339 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
3340 dwFlags ^= ICU_NO_ENCODE;
3341 hr=UrlCombineA(lpszBaseUrl,lpszRelativeUrl,lpszBuffer,lpdwBufferLength,dwFlags);
3343 return (hr==S_OK);
3346 /***********************************************************************
3347 * InternetCombineUrlW (WININET.@)
3349 * Combine a base URL with a relative URL
3351 * RETURNS
3352 * TRUE on success
3353 * FALSE on failure
3357 BOOL WINAPI InternetCombineUrlW(LPCWSTR lpszBaseUrl, LPCWSTR lpszRelativeUrl,
3358 LPWSTR lpszBuffer, LPDWORD lpdwBufferLength,
3359 DWORD dwFlags)
3361 HRESULT hr=S_OK;
3363 TRACE("(%s, %s, %p, %p, 0x%08x)\n", debugstr_w(lpszBaseUrl), debugstr_w(lpszRelativeUrl), lpszBuffer, lpdwBufferLength, dwFlags);
3365 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
3366 dwFlags ^= ICU_NO_ENCODE;
3367 hr=UrlCombineW(lpszBaseUrl,lpszRelativeUrl,lpszBuffer,lpdwBufferLength,dwFlags);
3369 return (hr==S_OK);
3372 /* max port num is 65535 => 5 digits */
3373 #define MAX_WORD_DIGITS 5
3375 #define URL_GET_COMP_LENGTH(url, component) ((url)->dw##component##Length ? \
3376 (url)->dw##component##Length : strlenW((url)->lpsz##component))
3377 #define URL_GET_COMP_LENGTHA(url, component) ((url)->dw##component##Length ? \
3378 (url)->dw##component##Length : strlen((url)->lpsz##component))
3380 static BOOL url_uses_default_port(INTERNET_SCHEME nScheme, INTERNET_PORT nPort)
3382 if ((nScheme == INTERNET_SCHEME_HTTP) &&
3383 (nPort == INTERNET_DEFAULT_HTTP_PORT))
3384 return TRUE;
3385 if ((nScheme == INTERNET_SCHEME_HTTPS) &&
3386 (nPort == INTERNET_DEFAULT_HTTPS_PORT))
3387 return TRUE;
3388 if ((nScheme == INTERNET_SCHEME_FTP) &&
3389 (nPort == INTERNET_DEFAULT_FTP_PORT))
3390 return TRUE;
3391 if ((nScheme == INTERNET_SCHEME_GOPHER) &&
3392 (nPort == INTERNET_DEFAULT_GOPHER_PORT))
3393 return TRUE;
3395 if (nPort == INTERNET_INVALID_PORT_NUMBER)
3396 return TRUE;
3398 return FALSE;
3401 /* opaque urls do not fit into the standard url hierarchy and don't have
3402 * two following slashes */
3403 static inline BOOL scheme_is_opaque(INTERNET_SCHEME nScheme)
3405 return (nScheme != INTERNET_SCHEME_FTP) &&
3406 (nScheme != INTERNET_SCHEME_GOPHER) &&
3407 (nScheme != INTERNET_SCHEME_HTTP) &&
3408 (nScheme != INTERNET_SCHEME_HTTPS) &&
3409 (nScheme != INTERNET_SCHEME_FILE);
3412 static LPCWSTR INTERNET_GetSchemeString(INTERNET_SCHEME scheme)
3414 int index;
3415 if (scheme < INTERNET_SCHEME_FIRST)
3416 return NULL;
3417 index = scheme - INTERNET_SCHEME_FIRST;
3418 if (index >= sizeof(url_schemes)/sizeof(url_schemes[0]))
3419 return NULL;
3420 return (LPCWSTR)url_schemes[index];
3423 /* we can calculate using ansi strings because we're just
3424 * calculating string length, not size
3426 static BOOL calc_url_length(LPURL_COMPONENTSW lpUrlComponents,
3427 LPDWORD lpdwUrlLength)
3429 INTERNET_SCHEME nScheme;
3431 *lpdwUrlLength = 0;
3433 if (lpUrlComponents->lpszScheme)
3435 DWORD dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, Scheme);
3436 *lpdwUrlLength += dwLen;
3437 nScheme = GetInternetSchemeW(lpUrlComponents->lpszScheme, dwLen);
3439 else
3441 LPCWSTR scheme;
3443 nScheme = lpUrlComponents->nScheme;
3445 if (nScheme == INTERNET_SCHEME_DEFAULT)
3446 nScheme = INTERNET_SCHEME_HTTP;
3447 scheme = INTERNET_GetSchemeString(nScheme);
3448 *lpdwUrlLength += strlenW(scheme);
3451 (*lpdwUrlLength)++; /* ':' */
3452 if (!scheme_is_opaque(nScheme) || lpUrlComponents->lpszHostName)
3453 *lpdwUrlLength += strlen("//");
3455 if (lpUrlComponents->lpszUserName)
3457 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, UserName);
3458 *lpdwUrlLength += strlen("@");
3460 else
3462 if (lpUrlComponents->lpszPassword)
3464 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
3465 return FALSE;
3469 if (lpUrlComponents->lpszPassword)
3471 *lpdwUrlLength += strlen(":");
3472 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, Password);
3475 if (lpUrlComponents->lpszHostName)
3477 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, HostName);
3479 if (!url_uses_default_port(nScheme, lpUrlComponents->nPort))
3481 char szPort[MAX_WORD_DIGITS+1];
3483 sprintf(szPort, "%d", lpUrlComponents->nPort);
3484 *lpdwUrlLength += strlen(szPort);
3485 *lpdwUrlLength += strlen(":");
3488 if (lpUrlComponents->lpszUrlPath && *lpUrlComponents->lpszUrlPath != '/')
3489 (*lpdwUrlLength)++; /* '/' */
3492 if (lpUrlComponents->lpszUrlPath)
3493 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, UrlPath);
3495 if (lpUrlComponents->lpszExtraInfo)
3496 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, ExtraInfo);
3498 return TRUE;
3501 static void convert_urlcomp_atow(LPURL_COMPONENTSA lpUrlComponents, LPURL_COMPONENTSW urlCompW)
3503 INT len;
3505 ZeroMemory(urlCompW, sizeof(URL_COMPONENTSW));
3507 urlCompW->dwStructSize = sizeof(URL_COMPONENTSW);
3508 urlCompW->dwSchemeLength = lpUrlComponents->dwSchemeLength;
3509 urlCompW->nScheme = lpUrlComponents->nScheme;
3510 urlCompW->dwHostNameLength = lpUrlComponents->dwHostNameLength;
3511 urlCompW->nPort = lpUrlComponents->nPort;
3512 urlCompW->dwUserNameLength = lpUrlComponents->dwUserNameLength;
3513 urlCompW->dwPasswordLength = lpUrlComponents->dwPasswordLength;
3514 urlCompW->dwUrlPathLength = lpUrlComponents->dwUrlPathLength;
3515 urlCompW->dwExtraInfoLength = lpUrlComponents->dwExtraInfoLength;
3517 if (lpUrlComponents->lpszScheme)
3519 len = URL_GET_COMP_LENGTHA(lpUrlComponents, Scheme) + 1;
3520 urlCompW->lpszScheme = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3521 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszScheme,
3522 -1, urlCompW->lpszScheme, len);
3525 if (lpUrlComponents->lpszHostName)
3527 len = URL_GET_COMP_LENGTHA(lpUrlComponents, HostName) + 1;
3528 urlCompW->lpszHostName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3529 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszHostName,
3530 -1, urlCompW->lpszHostName, len);
3533 if (lpUrlComponents->lpszUserName)
3535 len = URL_GET_COMP_LENGTHA(lpUrlComponents, UserName) + 1;
3536 urlCompW->lpszUserName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3537 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszUserName,
3538 -1, urlCompW->lpszUserName, len);
3541 if (lpUrlComponents->lpszPassword)
3543 len = URL_GET_COMP_LENGTHA(lpUrlComponents, Password) + 1;
3544 urlCompW->lpszPassword = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3545 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszPassword,
3546 -1, urlCompW->lpszPassword, len);
3549 if (lpUrlComponents->lpszUrlPath)
3551 len = URL_GET_COMP_LENGTHA(lpUrlComponents, UrlPath) + 1;
3552 urlCompW->lpszUrlPath = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3553 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszUrlPath,
3554 -1, urlCompW->lpszUrlPath, len);
3557 if (lpUrlComponents->lpszExtraInfo)
3559 len = URL_GET_COMP_LENGTHA(lpUrlComponents, ExtraInfo) + 1;
3560 urlCompW->lpszExtraInfo = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3561 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszExtraInfo,
3562 -1, urlCompW->lpszExtraInfo, len);
3566 /***********************************************************************
3567 * InternetCreateUrlA (WININET.@)
3569 * See InternetCreateUrlW.
3571 BOOL WINAPI InternetCreateUrlA(LPURL_COMPONENTSA lpUrlComponents, DWORD dwFlags,
3572 LPSTR lpszUrl, LPDWORD lpdwUrlLength)
3574 BOOL ret;
3575 LPWSTR urlW = NULL;
3576 URL_COMPONENTSW urlCompW;
3578 TRACE("(%p,%d,%p,%p)\n", lpUrlComponents, dwFlags, lpszUrl, lpdwUrlLength);
3580 if (!lpUrlComponents || lpUrlComponents->dwStructSize != sizeof(URL_COMPONENTSW) || !lpdwUrlLength)
3582 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
3583 return FALSE;
3586 convert_urlcomp_atow(lpUrlComponents, &urlCompW);
3588 if (lpszUrl)
3589 urlW = HeapAlloc(GetProcessHeap(), 0, *lpdwUrlLength * sizeof(WCHAR));
3591 ret = InternetCreateUrlW(&urlCompW, dwFlags, urlW, lpdwUrlLength);
3593 if (!ret && (GetLastError() == ERROR_INSUFFICIENT_BUFFER))
3594 *lpdwUrlLength /= sizeof(WCHAR);
3596 /* on success, lpdwUrlLength points to the size of urlW in WCHARS
3597 * minus one, so add one to leave room for NULL terminator
3599 if (ret)
3600 WideCharToMultiByte(CP_ACP, 0, urlW, -1, lpszUrl, *lpdwUrlLength + 1, NULL, NULL);
3602 HeapFree(GetProcessHeap(), 0, urlCompW.lpszScheme);
3603 HeapFree(GetProcessHeap(), 0, urlCompW.lpszHostName);
3604 HeapFree(GetProcessHeap(), 0, urlCompW.lpszUserName);
3605 HeapFree(GetProcessHeap(), 0, urlCompW.lpszPassword);
3606 HeapFree(GetProcessHeap(), 0, urlCompW.lpszUrlPath);
3607 HeapFree(GetProcessHeap(), 0, urlCompW.lpszExtraInfo);
3608 HeapFree(GetProcessHeap(), 0, urlW);
3610 return ret;
3613 /***********************************************************************
3614 * InternetCreateUrlW (WININET.@)
3616 * Creates a URL from its component parts.
3618 * PARAMS
3619 * lpUrlComponents [I] URL Components.
3620 * dwFlags [I] Flags. See notes.
3621 * lpszUrl [I] Buffer in which to store the created URL.
3622 * lpdwUrlLength [I/O] On input, the length of the buffer pointed to by
3623 * lpszUrl in characters. On output, the number of bytes
3624 * required to store the URL including terminator.
3626 * NOTES
3628 * The dwFlags parameter can be zero or more of the following:
3629 *|ICU_ESCAPE - Generates escape sequences for unsafe characters in the path and extra info of the URL.
3631 * RETURNS
3632 * TRUE on success
3633 * FALSE on failure
3636 BOOL WINAPI InternetCreateUrlW(LPURL_COMPONENTSW lpUrlComponents, DWORD dwFlags,
3637 LPWSTR lpszUrl, LPDWORD lpdwUrlLength)
3639 DWORD dwLen;
3640 INTERNET_SCHEME nScheme;
3642 static const WCHAR slashSlashW[] = {'/','/'};
3643 static const WCHAR percentD[] = {'%','d',0};
3645 TRACE("(%p,%d,%p,%p)\n", lpUrlComponents, dwFlags, lpszUrl, lpdwUrlLength);
3647 if (!lpUrlComponents || lpUrlComponents->dwStructSize != sizeof(URL_COMPONENTSW) || !lpdwUrlLength)
3649 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
3650 return FALSE;
3653 if (!calc_url_length(lpUrlComponents, &dwLen))
3654 return FALSE;
3656 if (!lpszUrl || *lpdwUrlLength < dwLen)
3658 *lpdwUrlLength = (dwLen + 1) * sizeof(WCHAR);
3659 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
3660 return FALSE;
3663 *lpdwUrlLength = dwLen;
3664 lpszUrl[0] = 0x00;
3666 dwLen = 0;
3668 if (lpUrlComponents->lpszScheme)
3670 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, Scheme);
3671 memcpy(lpszUrl, lpUrlComponents->lpszScheme, dwLen * sizeof(WCHAR));
3672 lpszUrl += dwLen;
3674 nScheme = GetInternetSchemeW(lpUrlComponents->lpszScheme, dwLen);
3676 else
3678 LPCWSTR scheme;
3679 nScheme = lpUrlComponents->nScheme;
3681 if (nScheme == INTERNET_SCHEME_DEFAULT)
3682 nScheme = INTERNET_SCHEME_HTTP;
3684 scheme = INTERNET_GetSchemeString(nScheme);
3685 dwLen = strlenW(scheme);
3686 memcpy(lpszUrl, scheme, dwLen * sizeof(WCHAR));
3687 lpszUrl += dwLen;
3690 /* all schemes are followed by at least a colon */
3691 *lpszUrl = ':';
3692 lpszUrl++;
3694 if (!scheme_is_opaque(nScheme) || lpUrlComponents->lpszHostName)
3696 memcpy(lpszUrl, slashSlashW, sizeof(slashSlashW));
3697 lpszUrl += sizeof(slashSlashW)/sizeof(slashSlashW[0]);
3700 if (lpUrlComponents->lpszUserName)
3702 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, UserName);
3703 memcpy(lpszUrl, lpUrlComponents->lpszUserName, dwLen * sizeof(WCHAR));
3704 lpszUrl += dwLen;
3706 if (lpUrlComponents->lpszPassword)
3708 *lpszUrl = ':';
3709 lpszUrl++;
3711 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, Password);
3712 memcpy(lpszUrl, lpUrlComponents->lpszPassword, dwLen * sizeof(WCHAR));
3713 lpszUrl += dwLen;
3716 *lpszUrl = '@';
3717 lpszUrl++;
3720 if (lpUrlComponents->lpszHostName)
3722 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, HostName);
3723 memcpy(lpszUrl, lpUrlComponents->lpszHostName, dwLen * sizeof(WCHAR));
3724 lpszUrl += dwLen;
3726 if (!url_uses_default_port(nScheme, lpUrlComponents->nPort))
3728 WCHAR szPort[MAX_WORD_DIGITS+1];
3730 sprintfW(szPort, percentD, lpUrlComponents->nPort);
3731 *lpszUrl = ':';
3732 lpszUrl++;
3733 dwLen = strlenW(szPort);
3734 memcpy(lpszUrl, szPort, dwLen * sizeof(WCHAR));
3735 lpszUrl += dwLen;
3738 /* add slash between hostname and path if necessary */
3739 if (lpUrlComponents->lpszUrlPath && *lpUrlComponents->lpszUrlPath != '/')
3741 *lpszUrl = '/';
3742 lpszUrl++;
3746 if (lpUrlComponents->lpszUrlPath)
3748 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, UrlPath);
3749 memcpy(lpszUrl, lpUrlComponents->lpszUrlPath, dwLen * sizeof(WCHAR));
3750 lpszUrl += dwLen;
3753 if (lpUrlComponents->lpszExtraInfo)
3755 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, ExtraInfo);
3756 memcpy(lpszUrl, lpUrlComponents->lpszExtraInfo, dwLen * sizeof(WCHAR));
3757 lpszUrl += dwLen;
3760 *lpszUrl = '\0';
3762 return TRUE;
3765 /***********************************************************************
3766 * InternetConfirmZoneCrossingA (WININET.@)
3769 DWORD WINAPI InternetConfirmZoneCrossingA( HWND hWnd, LPSTR szUrlPrev, LPSTR szUrlNew, BOOL bPost )
3771 FIXME("(%p, %s, %s, %x) stub\n", hWnd, debugstr_a(szUrlPrev), debugstr_a(szUrlNew), bPost);
3772 return ERROR_SUCCESS;
3775 /***********************************************************************
3776 * InternetConfirmZoneCrossingW (WININET.@)
3779 DWORD WINAPI InternetConfirmZoneCrossingW( HWND hWnd, LPWSTR szUrlPrev, LPWSTR szUrlNew, BOOL bPost )
3781 FIXME("(%p, %s, %s, %x) stub\n", hWnd, debugstr_w(szUrlPrev), debugstr_w(szUrlNew), bPost);
3782 return ERROR_SUCCESS;
3785 static DWORD zone_preference = 3;
3787 /***********************************************************************
3788 * PrivacySetZonePreferenceW (WININET.@)
3790 DWORD WINAPI PrivacySetZonePreferenceW( DWORD zone, DWORD type, DWORD template, LPCWSTR preference )
3792 FIXME( "%x %x %x %s: stub\n", zone, type, template, debugstr_w(preference) );
3794 zone_preference = template;
3795 return 0;
3798 /***********************************************************************
3799 * PrivacyGetZonePreferenceW (WININET.@)
3801 DWORD WINAPI PrivacyGetZonePreferenceW( DWORD zone, DWORD type, LPDWORD template,
3802 LPWSTR preference, LPDWORD length )
3804 FIXME( "%x %x %p %p %p: stub\n", zone, type, template, preference, length );
3806 if (template) *template = zone_preference;
3807 return 0;
3810 DWORD WINAPI InternetDialA( HWND hwndParent, LPSTR lpszConnectoid, DWORD dwFlags,
3811 DWORD_PTR* lpdwConnection, DWORD dwReserved )
3813 FIXME("(%p, %p, 0x%08x, %p, 0x%08x) stub\n", hwndParent, lpszConnectoid, dwFlags,
3814 lpdwConnection, dwReserved);
3815 return ERROR_SUCCESS;
3818 DWORD WINAPI InternetDialW( HWND hwndParent, LPWSTR lpszConnectoid, DWORD dwFlags,
3819 DWORD_PTR* lpdwConnection, DWORD dwReserved )
3821 FIXME("(%p, %p, 0x%08x, %p, 0x%08x) stub\n", hwndParent, lpszConnectoid, dwFlags,
3822 lpdwConnection, dwReserved);
3823 return ERROR_SUCCESS;
3826 BOOL WINAPI InternetGoOnlineA( LPSTR lpszURL, HWND hwndParent, DWORD dwReserved )
3828 FIXME("(%s, %p, 0x%08x) stub\n", debugstr_a(lpszURL), hwndParent, dwReserved);
3829 return TRUE;
3832 BOOL WINAPI InternetGoOnlineW( LPWSTR lpszURL, HWND hwndParent, DWORD dwReserved )
3834 FIXME("(%s, %p, 0x%08x) stub\n", debugstr_w(lpszURL), hwndParent, dwReserved);
3835 return TRUE;
3838 DWORD WINAPI InternetHangUp( DWORD_PTR dwConnection, DWORD dwReserved )
3840 FIXME("(0x%08lx, 0x%08x) stub\n", dwConnection, dwReserved);
3841 return ERROR_SUCCESS;
3844 BOOL WINAPI CreateMD5SSOHash( PWSTR pszChallengeInfo, PWSTR pwszRealm, PWSTR pwszTarget,
3845 PBYTE pbHexHash )
3847 FIXME("(%s, %s, %s, %p) stub\n", debugstr_w(pszChallengeInfo), debugstr_w(pwszRealm),
3848 debugstr_w(pwszTarget), pbHexHash);
3849 return FALSE;
3852 BOOL WINAPI ResumeSuspendedDownload( HINTERNET hInternet, DWORD dwError )
3854 FIXME("(%p, 0x%08x) stub\n", hInternet, dwError);
3855 return FALSE;
3858 BOOL WINAPI InternetQueryFortezzaStatus(DWORD *a, DWORD_PTR b)
3860 FIXME("(%p, %08lx) stub\n", a, b);
3861 return 0;