wininet: Moved InternetQueryOption(INTERNET_OPTION_PROXY) implementation to vtbl.
[wine/gsoc_dplay.git] / dlls / wininet / internet.c
blobcf21944feaef257f087d668103ce2f148f0d1c7d
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 #include <string.h>
35 #include <stdarg.h>
36 #include <stdio.h>
37 #include <sys/types.h>
38 #ifdef HAVE_SYS_SOCKET_H
39 # include <sys/socket.h>
40 #endif
41 #ifdef HAVE_POLL_H
42 #include <poll.h>
43 #endif
44 #ifdef HAVE_SYS_POLL_H
45 # include <sys/poll.h>
46 #endif
47 #ifdef HAVE_SYS_TIME_H
48 # include <sys/time.h>
49 #endif
50 #include <stdlib.h>
51 #include <ctype.h>
52 #ifdef HAVE_UNISTD_H
53 # include <unistd.h>
54 #endif
55 #include <assert.h>
57 #include "windef.h"
58 #include "winbase.h"
59 #include "winreg.h"
60 #include "winuser.h"
61 #include "wininet.h"
62 #include "winineti.h"
63 #include "winnls.h"
64 #include "wine/debug.h"
65 #include "winerror.h"
66 #define NO_SHLWAPI_STREAM
67 #include "shlwapi.h"
69 #include "wine/exception.h"
71 #include "internet.h"
72 #include "resource.h"
74 #include "wine/unicode.h"
76 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
78 #define RESPONSE_TIMEOUT 30
80 typedef struct
82 DWORD dwError;
83 CHAR response[MAX_REPLY_LEN];
84 } WITHREADERROR, *LPWITHREADERROR;
86 static DWORD g_dwTlsErrIndex = TLS_OUT_OF_INDEXES;
87 static HMODULE WININET_hModule;
89 #define HANDLE_CHUNK_SIZE 0x10
91 static CRITICAL_SECTION WININET_cs;
92 static CRITICAL_SECTION_DEBUG WININET_cs_debug =
94 0, 0, &WININET_cs,
95 { &WININET_cs_debug.ProcessLocksList, &WININET_cs_debug.ProcessLocksList },
96 0, 0, { (DWORD_PTR)(__FILE__ ": WININET_cs") }
98 static CRITICAL_SECTION WININET_cs = { &WININET_cs_debug, -1, 0, 0, 0, 0 };
100 static LPWININETHANDLEHEADER *WININET_Handles;
101 static UINT WININET_dwNextHandle;
102 static UINT WININET_dwMaxHandles;
104 HINTERNET WININET_AllocHandle( LPWININETHANDLEHEADER info )
106 LPWININETHANDLEHEADER *p;
107 UINT handle = 0, num;
109 list_init( &info->children );
111 EnterCriticalSection( &WININET_cs );
112 if( !WININET_dwMaxHandles )
114 num = HANDLE_CHUNK_SIZE;
115 p = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
116 sizeof (UINT)* num);
117 if( !p )
118 goto end;
119 WININET_Handles = p;
120 WININET_dwMaxHandles = num;
122 if( WININET_dwMaxHandles == WININET_dwNextHandle )
124 num = WININET_dwMaxHandles + HANDLE_CHUNK_SIZE;
125 p = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
126 WININET_Handles, sizeof (UINT)* num);
127 if( !p )
128 goto end;
129 WININET_Handles = p;
130 WININET_dwMaxHandles = num;
133 handle = WININET_dwNextHandle;
134 if( WININET_Handles[handle] )
135 ERR("handle isn't free but should be\n");
136 WININET_Handles[handle] = WININET_AddRef( info );
138 while( WININET_Handles[WININET_dwNextHandle] &&
139 (WININET_dwNextHandle < WININET_dwMaxHandles ) )
140 WININET_dwNextHandle++;
142 end:
143 LeaveCriticalSection( &WININET_cs );
145 return info->hInternet = (HINTERNET) (handle+1);
148 LPWININETHANDLEHEADER WININET_AddRef( LPWININETHANDLEHEADER info )
150 ULONG refs = InterlockedIncrement(&info->refs);
151 TRACE("%p -> refcount = %d\n", info, refs );
152 return info;
155 LPWININETHANDLEHEADER WININET_GetObject( HINTERNET hinternet )
157 LPWININETHANDLEHEADER info = NULL;
158 UINT handle = (UINT) hinternet;
160 EnterCriticalSection( &WININET_cs );
162 if( (handle > 0) && ( handle <= WININET_dwMaxHandles ) &&
163 WININET_Handles[handle-1] )
164 info = WININET_AddRef( WININET_Handles[handle-1] );
166 LeaveCriticalSection( &WININET_cs );
168 TRACE("handle %d -> %p\n", handle, info);
170 return info;
173 BOOL WININET_Release( LPWININETHANDLEHEADER info )
175 ULONG refs = InterlockedDecrement(&info->refs);
176 TRACE( "object %p refcount = %d\n", info, refs );
177 if( !refs )
179 if ( info->vtbl->CloseConnection )
181 TRACE( "closing connection %p\n", info);
182 info->vtbl->CloseConnection( info );
184 /* Don't send a callback if this is a session handle created with InternetOpenUrl */
185 if (info->htype != WH_HHTTPSESSION || !(info->dwInternalFlags & INET_OPENURL))
187 INTERNET_SendCallback(info, info->dwContext,
188 INTERNET_STATUS_HANDLE_CLOSING, &info->hInternet,
189 sizeof(HINTERNET));
191 TRACE( "destroying object %p\n", info);
192 if ( info->htype != WH_HINIT )
193 list_remove( &info->entry );
194 info->vtbl->Destroy( info );
196 return TRUE;
199 BOOL WININET_FreeHandle( HINTERNET hinternet )
201 BOOL ret = FALSE;
202 UINT handle = (UINT) hinternet;
203 LPWININETHANDLEHEADER info = NULL, child, next;
205 EnterCriticalSection( &WININET_cs );
207 if( (handle > 0) && ( handle <= WININET_dwMaxHandles ) )
209 handle--;
210 if( WININET_Handles[handle] )
212 info = WININET_Handles[handle];
213 TRACE( "destroying handle %d for object %p\n", handle+1, info);
214 WININET_Handles[handle] = NULL;
215 ret = TRUE;
219 LeaveCriticalSection( &WININET_cs );
221 /* As on native when the equivalent of WININET_Release is called, the handle
222 * is already invalid, but if a new handle is created at this time it does
223 * not yet get assigned the freed handle number */
224 if( info )
226 /* Free all children as native does */
227 LIST_FOR_EACH_ENTRY_SAFE( child, next, &info->children, WININETHANDLEHEADER, entry )
229 TRACE( "freeing child handle %d for parent handle %d\n",
230 (UINT)child->hInternet, handle+1);
231 WININET_FreeHandle( child->hInternet );
233 WININET_Release( info );
236 EnterCriticalSection( &WININET_cs );
238 if( WININET_dwNextHandle > handle && !WININET_Handles[handle] )
239 WININET_dwNextHandle = handle;
241 LeaveCriticalSection( &WININET_cs );
243 return ret;
246 /***********************************************************************
247 * DllMain [Internal] Initializes the internal 'WININET.DLL'.
249 * PARAMS
250 * hinstDLL [I] handle to the DLL's instance
251 * fdwReason [I]
252 * lpvReserved [I] reserved, must be NULL
254 * RETURNS
255 * Success: TRUE
256 * Failure: FALSE
259 BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
261 TRACE("%p,%x,%p\n", hinstDLL, fdwReason, lpvReserved);
263 switch (fdwReason) {
264 case DLL_PROCESS_ATTACH:
266 g_dwTlsErrIndex = TlsAlloc();
268 if (g_dwTlsErrIndex == TLS_OUT_OF_INDEXES)
269 return FALSE;
271 URLCacheContainers_CreateDefaults();
273 WININET_hModule = hinstDLL;
275 case DLL_THREAD_ATTACH:
276 break;
278 case DLL_THREAD_DETACH:
279 if (g_dwTlsErrIndex != TLS_OUT_OF_INDEXES)
281 LPVOID lpwite = TlsGetValue(g_dwTlsErrIndex);
282 HeapFree(GetProcessHeap(), 0, lpwite);
284 break;
286 case DLL_PROCESS_DETACH:
288 URLCacheContainers_DeleteAll();
290 if (g_dwTlsErrIndex != TLS_OUT_OF_INDEXES)
292 HeapFree(GetProcessHeap(), 0, TlsGetValue(g_dwTlsErrIndex));
293 TlsFree(g_dwTlsErrIndex);
295 break;
298 return TRUE;
302 /***********************************************************************
303 * InternetInitializeAutoProxyDll (WININET.@)
305 * Setup the internal proxy
307 * PARAMETERS
308 * dwReserved
310 * RETURNS
311 * FALSE on failure
314 BOOL WINAPI InternetInitializeAutoProxyDll(DWORD dwReserved)
316 FIXME("STUB\n");
317 INTERNET_SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
318 return FALSE;
321 /***********************************************************************
322 * DetectAutoProxyUrl (WININET.@)
324 * Auto detect the proxy url
326 * RETURNS
327 * FALSE on failure
330 BOOL WINAPI DetectAutoProxyUrl(LPSTR lpszAutoProxyUrl,
331 DWORD dwAutoProxyUrlLength, DWORD dwDetectFlags)
333 FIXME("STUB\n");
334 INTERNET_SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
335 return FALSE;
339 /***********************************************************************
340 * INTERNET_ConfigureProxy
342 * FIXME:
343 * The proxy may be specified in the form 'http=proxy.my.org'
344 * Presumably that means there can be ftp=ftpproxy.my.org too.
346 static BOOL INTERNET_ConfigureProxy( LPWININETAPPINFOW lpwai )
348 HKEY key;
349 DWORD type, len, enabled = 0;
350 LPCSTR envproxy;
351 static const WCHAR szInternetSettings[] =
352 { 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
353 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
354 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s',0 };
355 static const WCHAR szProxyServer[] = { 'P','r','o','x','y','S','e','r','v','e','r', 0 };
356 static const WCHAR szProxyEnable[] = { 'P','r','o','x','y','E','n','a','b','l','e', 0 };
358 if (RegOpenKeyW( HKEY_CURRENT_USER, szInternetSettings, &key )) return FALSE;
360 len = sizeof enabled;
361 if (RegQueryValueExW( key, szProxyEnable, NULL, &type, (BYTE *)&enabled, &len ) || type != REG_DWORD)
362 RegSetValueExW( key, szProxyEnable, 0, REG_DWORD, (BYTE *)&enabled, sizeof(REG_DWORD) );
364 if (enabled)
366 TRACE("Proxy is enabled.\n");
368 /* figure out how much memory the proxy setting takes */
369 if (!RegQueryValueExW( key, szProxyServer, NULL, &type, NULL, &len ) && len && (type == REG_SZ))
371 LPWSTR szProxy, p;
372 static const WCHAR szHttp[] = {'h','t','t','p','=',0};
374 if (!(szProxy = HeapAlloc( GetProcessHeap(), 0, len )))
376 RegCloseKey( key );
377 return FALSE;
379 RegQueryValueExW( key, szProxyServer, NULL, &type, (BYTE*)szProxy, &len );
381 /* find the http proxy, and strip away everything else */
382 p = strstrW( szProxy, szHttp );
383 if (p)
385 p += lstrlenW( szHttp );
386 lstrcpyW( szProxy, p );
388 p = strchrW( szProxy, ' ' );
389 if (p) *p = 0;
391 lpwai->dwAccessType = INTERNET_OPEN_TYPE_PROXY;
392 lpwai->lpszProxy = szProxy;
394 TRACE("http proxy = %s\n", debugstr_w(lpwai->lpszProxy));
396 else
397 ERR("Couldn't read proxy server settings from registry.\n");
399 else if ((envproxy = getenv( "http_proxy" )))
401 WCHAR *envproxyW;
403 len = MultiByteToWideChar( CP_UNIXCP, 0, envproxy, -1, NULL, 0 );
404 if (!(envproxyW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR)))) return FALSE;
405 MultiByteToWideChar( CP_UNIXCP, 0, envproxy, -1, envproxyW, len );
407 lpwai->dwAccessType = INTERNET_OPEN_TYPE_PROXY;
408 lpwai->lpszProxy = envproxyW;
410 TRACE("http proxy (from environment) = %s\n", debugstr_w(lpwai->lpszProxy));
411 enabled = 1;
413 if (!enabled) TRACE("Proxy is not enabled.\n");
415 RegCloseKey( key );
416 return (enabled > 0);
419 /***********************************************************************
420 * dump_INTERNET_FLAGS
422 * Helper function to TRACE the internet flags.
424 * RETURNS
425 * None
428 static void dump_INTERNET_FLAGS(DWORD dwFlags)
430 #define FE(x) { x, #x }
431 static const wininet_flag_info flag[] = {
432 FE(INTERNET_FLAG_RELOAD),
433 FE(INTERNET_FLAG_RAW_DATA),
434 FE(INTERNET_FLAG_EXISTING_CONNECT),
435 FE(INTERNET_FLAG_ASYNC),
436 FE(INTERNET_FLAG_PASSIVE),
437 FE(INTERNET_FLAG_NO_CACHE_WRITE),
438 FE(INTERNET_FLAG_MAKE_PERSISTENT),
439 FE(INTERNET_FLAG_FROM_CACHE),
440 FE(INTERNET_FLAG_SECURE),
441 FE(INTERNET_FLAG_KEEP_CONNECTION),
442 FE(INTERNET_FLAG_NO_AUTO_REDIRECT),
443 FE(INTERNET_FLAG_READ_PREFETCH),
444 FE(INTERNET_FLAG_NO_COOKIES),
445 FE(INTERNET_FLAG_NO_AUTH),
446 FE(INTERNET_FLAG_CACHE_IF_NET_FAIL),
447 FE(INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP),
448 FE(INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTPS),
449 FE(INTERNET_FLAG_IGNORE_CERT_DATE_INVALID),
450 FE(INTERNET_FLAG_IGNORE_CERT_CN_INVALID),
451 FE(INTERNET_FLAG_RESYNCHRONIZE),
452 FE(INTERNET_FLAG_HYPERLINK),
453 FE(INTERNET_FLAG_NO_UI),
454 FE(INTERNET_FLAG_PRAGMA_NOCACHE),
455 FE(INTERNET_FLAG_CACHE_ASYNC),
456 FE(INTERNET_FLAG_FORMS_SUBMIT),
457 FE(INTERNET_FLAG_NEED_FILE),
458 FE(INTERNET_FLAG_TRANSFER_ASCII),
459 FE(INTERNET_FLAG_TRANSFER_BINARY)
461 #undef FE
462 int i;
464 for (i = 0; i < (sizeof(flag) / sizeof(flag[0])); i++) {
465 if (flag[i].val & dwFlags) {
466 TRACE(" %s", flag[i].name);
467 dwFlags &= ~flag[i].val;
470 if (dwFlags)
471 TRACE(" Unknown flags (%08x)\n", dwFlags);
472 else
473 TRACE("\n");
476 /***********************************************************************
477 * INTERNET_CloseHandle (internal)
479 * Close internet handle
482 static VOID APPINFO_Destroy(WININETHANDLEHEADER *hdr)
484 LPWININETAPPINFOW lpwai = (LPWININETAPPINFOW) hdr;
486 TRACE("%p\n",lpwai);
488 HeapFree(GetProcessHeap(), 0, lpwai->lpszAgent);
489 HeapFree(GetProcessHeap(), 0, lpwai->lpszProxy);
490 HeapFree(GetProcessHeap(), 0, lpwai->lpszProxyBypass);
491 HeapFree(GetProcessHeap(), 0, lpwai->lpszProxyUsername);
492 HeapFree(GetProcessHeap(), 0, lpwai->lpszProxyPassword);
493 HeapFree(GetProcessHeap(), 0, lpwai);
496 static DWORD APPINFO_QueryOption(WININETHANDLEHEADER *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
498 LPWININETAPPINFOW ai = (LPWININETAPPINFOW)hdr;
500 switch(option) {
501 case INTERNET_OPTION_HANDLE_TYPE:
502 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
504 if (*size < sizeof(ULONG))
505 return ERROR_INSUFFICIENT_BUFFER;
507 *size = sizeof(DWORD);
508 *(DWORD*)buffer = INTERNET_HANDLE_TYPE_INTERNET;
509 return ERROR_SUCCESS;
511 case INTERNET_OPTION_USER_AGENT: {
512 DWORD bufsize;
514 TRACE("INTERNET_OPTION_USER_AGENT\n");
516 bufsize = *size;
518 if (unicode) {
519 *size = (strlenW(ai->lpszAgent) + 1) * sizeof(WCHAR);
520 if(!buffer || bufsize < *size)
521 return ERROR_INSUFFICIENT_BUFFER;
523 strcpyW(buffer, ai->lpszAgent);
524 }else {
525 *size = WideCharToMultiByte(CP_ACP, 0, ai->lpszAgent, -1, NULL, 0, NULL, NULL);
526 if(!buffer || bufsize < *size)
527 return ERROR_INSUFFICIENT_BUFFER;
529 WideCharToMultiByte(CP_ACP, 0, ai->lpszAgent, -1, buffer, *size, NULL, NULL);
532 return ERROR_SUCCESS;
535 case INTERNET_OPTION_PROXY:
536 if (unicode) {
537 INTERNET_PROXY_INFOW *pi = (INTERNET_PROXY_INFOW *)buffer;
538 DWORD proxyBytesRequired = 0, proxyBypassBytesRequired = 0;
539 LPWSTR proxy, proxy_bypass;
541 if (ai->lpszProxy)
542 proxyBytesRequired = (lstrlenW(ai->lpszProxy) + 1) * sizeof(WCHAR);
543 if (ai->lpszProxyBypass)
544 proxyBypassBytesRequired = (lstrlenW(ai->lpszProxyBypass) + 1) * sizeof(WCHAR);
545 if (*size < sizeof(INTERNET_PROXY_INFOW) + proxyBytesRequired + proxyBypassBytesRequired)
546 return ERROR_INSUFFICIENT_BUFFER;
548 proxy = (LPWSTR)((LPBYTE)buffer + sizeof(INTERNET_PROXY_INFOW));
549 proxy_bypass = (LPWSTR)((LPBYTE)buffer + sizeof(INTERNET_PROXY_INFOW) + proxyBytesRequired);
551 pi->dwAccessType = ai->dwAccessType;
552 pi->lpszProxy = NULL;
553 pi->lpszProxyBypass = NULL;
554 if (ai->lpszProxy) {
555 lstrcpyW(proxy, ai->lpszProxy);
556 pi->lpszProxy = proxy;
559 if (ai->lpszProxyBypass) {
560 lstrcpyW(proxy_bypass, ai->lpszProxyBypass);
561 pi->lpszProxyBypass = proxy_bypass;
564 *size = sizeof(INTERNET_PROXY_INFOW) + proxyBytesRequired + proxyBypassBytesRequired;
565 return ERROR_SUCCESS;
566 }else {
567 INTERNET_PROXY_INFOA *pi = (INTERNET_PROXY_INFOA *)buffer;
568 DWORD proxyBytesRequired = 0, proxyBypassBytesRequired = 0;
569 LPSTR proxy, proxy_bypass;
571 if (ai->lpszProxy)
572 proxyBytesRequired = WideCharToMultiByte(CP_ACP, 0, ai->lpszProxy, -1, NULL, 0, NULL, NULL);
573 if (ai->lpszProxyBypass)
574 proxyBypassBytesRequired = WideCharToMultiByte(CP_ACP, 0, ai->lpszProxyBypass, -1,
575 NULL, 0, NULL, NULL);
576 if (*size < sizeof(INTERNET_PROXY_INFOA) + proxyBytesRequired + proxyBypassBytesRequired)
577 return ERROR_INSUFFICIENT_BUFFER;
579 proxy = (LPSTR)((LPBYTE)buffer + sizeof(INTERNET_PROXY_INFOA));
580 proxy_bypass = (LPSTR)((LPBYTE)buffer + sizeof(INTERNET_PROXY_INFOA) + proxyBytesRequired);
582 pi->dwAccessType = ai->dwAccessType;
583 pi->lpszProxy = NULL;
584 pi->lpszProxyBypass = NULL;
585 if (ai->lpszProxy) {
586 WideCharToMultiByte(CP_ACP, 0, ai->lpszProxy, -1, proxy, proxyBytesRequired, NULL, NULL);
587 pi->lpszProxy = proxy;
590 if (ai->lpszProxyBypass) {
591 WideCharToMultiByte(CP_ACP, 0, ai->lpszProxyBypass, -1, proxy_bypass,
592 proxyBypassBytesRequired, NULL, NULL);
593 pi->lpszProxyBypass = proxy_bypass;
596 *size = sizeof(INTERNET_PROXY_INFOA) + proxyBytesRequired + proxyBypassBytesRequired;
597 return ERROR_SUCCESS;
601 return INET_QueryOption(option, buffer, size, unicode);
604 static const HANDLEHEADERVtbl APPINFOVtbl = {
605 APPINFO_Destroy,
606 NULL,
607 APPINFO_QueryOption,
608 NULL,
609 NULL,
610 NULL,
611 NULL,
612 NULL,
613 NULL
617 /***********************************************************************
618 * InternetOpenW (WININET.@)
620 * Per-application initialization of wininet
622 * RETURNS
623 * HINTERNET on success
624 * NULL on failure
627 HINTERNET WINAPI InternetOpenW(LPCWSTR lpszAgent, DWORD dwAccessType,
628 LPCWSTR lpszProxy, LPCWSTR lpszProxyBypass, DWORD dwFlags)
630 LPWININETAPPINFOW lpwai = NULL;
631 HINTERNET handle = NULL;
633 if (TRACE_ON(wininet)) {
634 #define FE(x) { x, #x }
635 static const wininet_flag_info access_type[] = {
636 FE(INTERNET_OPEN_TYPE_PRECONFIG),
637 FE(INTERNET_OPEN_TYPE_DIRECT),
638 FE(INTERNET_OPEN_TYPE_PROXY),
639 FE(INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY)
641 #undef FE
642 DWORD i;
643 const char *access_type_str = "Unknown";
645 TRACE("(%s, %i, %s, %s, %i)\n", debugstr_w(lpszAgent), dwAccessType,
646 debugstr_w(lpszProxy), debugstr_w(lpszProxyBypass), dwFlags);
647 for (i = 0; i < (sizeof(access_type) / sizeof(access_type[0])); i++) {
648 if (access_type[i].val == dwAccessType) {
649 access_type_str = access_type[i].name;
650 break;
653 TRACE(" access type : %s\n", access_type_str);
654 TRACE(" flags :");
655 dump_INTERNET_FLAGS(dwFlags);
658 /* Clear any error information */
659 INTERNET_SetLastError(0);
661 lpwai = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETAPPINFOW));
662 if (NULL == lpwai)
664 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
665 goto lend;
668 lpwai->hdr.htype = WH_HINIT;
669 lpwai->hdr.vtbl = &APPINFOVtbl;
670 lpwai->hdr.dwFlags = dwFlags;
671 lpwai->hdr.refs = 1;
672 lpwai->dwAccessType = dwAccessType;
673 lpwai->lpszProxyUsername = NULL;
674 lpwai->lpszProxyPassword = NULL;
676 handle = WININET_AllocHandle( &lpwai->hdr );
677 if( !handle )
679 HeapFree( GetProcessHeap(), 0, lpwai );
680 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
681 goto lend;
684 if (NULL != lpszAgent)
686 lpwai->lpszAgent = HeapAlloc( GetProcessHeap(),0,
687 (strlenW(lpszAgent)+1)*sizeof(WCHAR));
688 if (lpwai->lpszAgent)
689 lstrcpyW( lpwai->lpszAgent, lpszAgent );
691 if(dwAccessType == INTERNET_OPEN_TYPE_PRECONFIG)
692 INTERNET_ConfigureProxy( lpwai );
693 else if (NULL != lpszProxy)
695 lpwai->lpszProxy = HeapAlloc( GetProcessHeap(), 0,
696 (strlenW(lpszProxy)+1)*sizeof(WCHAR));
697 if (lpwai->lpszProxy)
698 lstrcpyW( lpwai->lpszProxy, lpszProxy );
701 if (NULL != lpszProxyBypass)
703 lpwai->lpszProxyBypass = HeapAlloc( GetProcessHeap(), 0,
704 (strlenW(lpszProxyBypass)+1)*sizeof(WCHAR));
705 if (lpwai->lpszProxyBypass)
706 lstrcpyW( lpwai->lpszProxyBypass, lpszProxyBypass );
709 lend:
710 if( lpwai )
711 WININET_Release( &lpwai->hdr );
713 TRACE("returning %p\n", lpwai);
715 return handle;
719 /***********************************************************************
720 * InternetOpenA (WININET.@)
722 * Per-application initialization of wininet
724 * RETURNS
725 * HINTERNET on success
726 * NULL on failure
729 HINTERNET WINAPI InternetOpenA(LPCSTR lpszAgent, DWORD dwAccessType,
730 LPCSTR lpszProxy, LPCSTR lpszProxyBypass, DWORD dwFlags)
732 HINTERNET rc = NULL;
733 INT len;
734 WCHAR *szAgent = NULL, *szProxy = NULL, *szBypass = NULL;
736 TRACE("(%s, 0x%08x, %s, %s, 0x%08x)\n", debugstr_a(lpszAgent),
737 dwAccessType, debugstr_a(lpszProxy), debugstr_a(lpszProxyBypass), dwFlags);
739 if( lpszAgent )
741 len = MultiByteToWideChar(CP_ACP, 0, lpszAgent, -1, NULL, 0);
742 szAgent = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
743 MultiByteToWideChar(CP_ACP, 0, lpszAgent, -1, szAgent, len);
746 if( lpszProxy )
748 len = MultiByteToWideChar(CP_ACP, 0, lpszProxy, -1, NULL, 0);
749 szProxy = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
750 MultiByteToWideChar(CP_ACP, 0, lpszProxy, -1, szProxy, len);
753 if( lpszProxyBypass )
755 len = MultiByteToWideChar(CP_ACP, 0, lpszProxyBypass, -1, NULL, 0);
756 szBypass = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
757 MultiByteToWideChar(CP_ACP, 0, lpszProxyBypass, -1, szBypass, len);
760 rc = InternetOpenW(szAgent, dwAccessType, szProxy, szBypass, dwFlags);
762 HeapFree(GetProcessHeap(), 0, szAgent);
763 HeapFree(GetProcessHeap(), 0, szProxy);
764 HeapFree(GetProcessHeap(), 0, szBypass);
766 return rc;
769 /***********************************************************************
770 * InternetGetLastResponseInfoA (WININET.@)
772 * Return last wininet error description on the calling thread
774 * RETURNS
775 * TRUE on success of writing to buffer
776 * FALSE on failure
779 BOOL WINAPI InternetGetLastResponseInfoA(LPDWORD lpdwError,
780 LPSTR lpszBuffer, LPDWORD lpdwBufferLength)
782 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
784 TRACE("\n");
786 if (lpwite)
788 *lpdwError = lpwite->dwError;
789 if (lpwite->dwError)
791 memcpy(lpszBuffer, lpwite->response, *lpdwBufferLength);
792 *lpdwBufferLength = strlen(lpszBuffer);
794 else
795 *lpdwBufferLength = 0;
797 else
799 *lpdwError = 0;
800 *lpdwBufferLength = 0;
803 return TRUE;
806 /***********************************************************************
807 * InternetGetLastResponseInfoW (WININET.@)
809 * Return last wininet error description on the calling thread
811 * RETURNS
812 * TRUE on success of writing to buffer
813 * FALSE on failure
816 BOOL WINAPI InternetGetLastResponseInfoW(LPDWORD lpdwError,
817 LPWSTR lpszBuffer, LPDWORD lpdwBufferLength)
819 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
821 TRACE("\n");
823 if (lpwite)
825 *lpdwError = lpwite->dwError;
826 if (lpwite->dwError)
828 memcpy(lpszBuffer, lpwite->response, *lpdwBufferLength);
829 *lpdwBufferLength = lstrlenW(lpszBuffer);
831 else
832 *lpdwBufferLength = 0;
834 else
836 *lpdwError = 0;
837 *lpdwBufferLength = 0;
840 return TRUE;
843 /***********************************************************************
844 * InternetGetConnectedState (WININET.@)
846 * Return connected state
848 * RETURNS
849 * TRUE if connected
850 * if lpdwStatus is not null, return the status (off line,
851 * modem, lan...) in it.
852 * FALSE if not connected
854 BOOL WINAPI InternetGetConnectedState(LPDWORD lpdwStatus, DWORD dwReserved)
856 TRACE("(%p, 0x%08x)\n", lpdwStatus, dwReserved);
858 if (lpdwStatus) {
859 FIXME("always returning LAN connection.\n");
860 *lpdwStatus = INTERNET_CONNECTION_LAN;
862 return TRUE;
866 /***********************************************************************
867 * InternetGetConnectedStateExW (WININET.@)
869 * Return connected state
871 * PARAMS
873 * lpdwStatus [O] Flags specifying the status of the internet connection.
874 * lpszConnectionName [O] Pointer to buffer to receive the friendly name of the internet connection.
875 * dwNameLen [I] Size of the buffer, in characters.
876 * dwReserved [I] Reserved. Must be set to 0.
878 * RETURNS
879 * TRUE if connected
880 * if lpdwStatus is not null, return the status (off line,
881 * modem, lan...) in it.
882 * FALSE if not connected
884 * NOTES
885 * If the system has no available network connections, an empty string is
886 * stored in lpszConnectionName. If there is a LAN connection, a localized
887 * "LAN Connection" string is stored. Presumably, if only a dial-up
888 * connection is available then the name of the dial-up connection is
889 * returned. Why any application, other than the "Internet Settings" CPL,
890 * would want to use this function instead of the simpler InternetGetConnectedStateW
891 * function is beyond me.
893 BOOL WINAPI InternetGetConnectedStateExW(LPDWORD lpdwStatus, LPWSTR lpszConnectionName,
894 DWORD dwNameLen, DWORD dwReserved)
896 TRACE("(%p, %p, %d, 0x%08x)\n", lpdwStatus, lpszConnectionName, dwNameLen, dwReserved);
898 /* Must be zero */
899 if(dwReserved)
900 return FALSE;
902 if (lpdwStatus) {
903 FIXME("always returning LAN connection.\n");
904 *lpdwStatus = INTERNET_CONNECTION_LAN;
906 return LoadStringW(WININET_hModule, IDS_LANCONNECTION, lpszConnectionName, dwNameLen);
910 /***********************************************************************
911 * InternetGetConnectedStateExA (WININET.@)
913 BOOL WINAPI InternetGetConnectedStateExA(LPDWORD lpdwStatus, LPSTR lpszConnectionName,
914 DWORD dwNameLen, DWORD dwReserved)
916 LPWSTR lpwszConnectionName = NULL;
917 BOOL rc;
919 TRACE("(%p, %p, %d, 0x%08x)\n", lpdwStatus, lpszConnectionName, dwNameLen, dwReserved);
921 if (lpszConnectionName && dwNameLen > 0)
922 lpwszConnectionName= HeapAlloc(GetProcessHeap(), 0, dwNameLen * sizeof(WCHAR));
924 rc = InternetGetConnectedStateExW(lpdwStatus,lpwszConnectionName, dwNameLen,
925 dwReserved);
926 if (rc && lpwszConnectionName)
928 WideCharToMultiByte(CP_ACP,0,lpwszConnectionName,-1,lpszConnectionName,
929 dwNameLen, NULL, NULL);
931 HeapFree(GetProcessHeap(),0,lpwszConnectionName);
934 return rc;
938 /***********************************************************************
939 * InternetConnectW (WININET.@)
941 * Open a ftp, gopher or http session
943 * RETURNS
944 * HINTERNET a session handle on success
945 * NULL on failure
948 HINTERNET WINAPI InternetConnectW(HINTERNET hInternet,
949 LPCWSTR lpszServerName, INTERNET_PORT nServerPort,
950 LPCWSTR lpszUserName, LPCWSTR lpszPassword,
951 DWORD dwService, DWORD dwFlags, DWORD_PTR dwContext)
953 LPWININETAPPINFOW hIC;
954 HINTERNET rc = NULL;
956 TRACE("(%p, %s, %i, %s, %s, %i, %i, %lx)\n", hInternet, debugstr_w(lpszServerName),
957 nServerPort, debugstr_w(lpszUserName), debugstr_w(lpszPassword),
958 dwService, dwFlags, dwContext);
960 if (!lpszServerName)
962 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
963 return NULL;
966 /* Clear any error information */
967 INTERNET_SetLastError(0);
968 hIC = (LPWININETAPPINFOW) WININET_GetObject( hInternet );
969 if ( (hIC == NULL) || (hIC->hdr.htype != WH_HINIT) )
971 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
972 goto lend;
975 switch (dwService)
977 case INTERNET_SERVICE_FTP:
978 rc = FTP_Connect(hIC, lpszServerName, nServerPort,
979 lpszUserName, lpszPassword, dwFlags, dwContext, 0);
980 break;
982 case INTERNET_SERVICE_HTTP:
983 rc = HTTP_Connect(hIC, lpszServerName, nServerPort,
984 lpszUserName, lpszPassword, dwFlags, dwContext, 0);
985 break;
987 case INTERNET_SERVICE_GOPHER:
988 default:
989 break;
991 lend:
992 if( hIC )
993 WININET_Release( &hIC->hdr );
995 TRACE("returning %p\n", rc);
996 return rc;
1000 /***********************************************************************
1001 * InternetConnectA (WININET.@)
1003 * Open a ftp, gopher or http session
1005 * RETURNS
1006 * HINTERNET a session handle on success
1007 * NULL on failure
1010 HINTERNET WINAPI InternetConnectA(HINTERNET hInternet,
1011 LPCSTR lpszServerName, INTERNET_PORT nServerPort,
1012 LPCSTR lpszUserName, LPCSTR lpszPassword,
1013 DWORD dwService, DWORD dwFlags, DWORD_PTR dwContext)
1015 HINTERNET rc = NULL;
1016 INT len = 0;
1017 LPWSTR szServerName = NULL;
1018 LPWSTR szUserName = NULL;
1019 LPWSTR szPassword = NULL;
1021 if (lpszServerName)
1023 len = MultiByteToWideChar(CP_ACP, 0, lpszServerName, -1, NULL, 0);
1024 szServerName = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1025 MultiByteToWideChar(CP_ACP, 0, lpszServerName, -1, szServerName, len);
1027 if (lpszUserName)
1029 len = MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, NULL, 0);
1030 szUserName = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1031 MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, szUserName, len);
1033 if (lpszPassword)
1035 len = MultiByteToWideChar(CP_ACP, 0, lpszPassword, -1, NULL, 0);
1036 szPassword = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1037 MultiByteToWideChar(CP_ACP, 0, lpszPassword, -1, szPassword, len);
1041 rc = InternetConnectW(hInternet, szServerName, nServerPort,
1042 szUserName, szPassword, dwService, dwFlags, dwContext);
1044 HeapFree(GetProcessHeap(), 0, szServerName);
1045 HeapFree(GetProcessHeap(), 0, szUserName);
1046 HeapFree(GetProcessHeap(), 0, szPassword);
1047 return rc;
1051 /***********************************************************************
1052 * InternetFindNextFileA (WININET.@)
1054 * Continues a file search from a previous call to FindFirstFile
1056 * RETURNS
1057 * TRUE on success
1058 * FALSE on failure
1061 BOOL WINAPI InternetFindNextFileA(HINTERNET hFind, LPVOID lpvFindData)
1063 BOOL ret;
1064 WIN32_FIND_DATAW fd;
1066 ret = InternetFindNextFileW(hFind, lpvFindData?&fd:NULL);
1067 if(lpvFindData)
1068 WININET_find_data_WtoA(&fd, (LPWIN32_FIND_DATAA)lpvFindData);
1069 return ret;
1072 /***********************************************************************
1073 * InternetFindNextFileW (WININET.@)
1075 * Continues a file search from a previous call to FindFirstFile
1077 * RETURNS
1078 * TRUE on success
1079 * FALSE on failure
1082 BOOL WINAPI InternetFindNextFileW(HINTERNET hFind, LPVOID lpvFindData)
1084 WININETHANDLEHEADER *hdr;
1085 DWORD res;
1087 TRACE("\n");
1089 hdr = WININET_GetObject(hFind);
1090 if(!hdr) {
1091 WARN("Invalid handle\n");
1092 SetLastError(ERROR_INVALID_HANDLE);
1093 return FALSE;
1096 if(hdr->vtbl->FindNextFileW) {
1097 res = hdr->vtbl->FindNextFileW(hdr, lpvFindData);
1098 }else {
1099 WARN("Handle doesn't support NextFile\n");
1100 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
1103 WININET_Release(hdr);
1105 if(res != ERROR_SUCCESS)
1106 SetLastError(res);
1107 return res == ERROR_SUCCESS;
1110 /***********************************************************************
1111 * InternetCloseHandle (WININET.@)
1113 * Generic close handle function
1115 * RETURNS
1116 * TRUE on success
1117 * FALSE on failure
1120 BOOL WINAPI InternetCloseHandle(HINTERNET hInternet)
1122 LPWININETHANDLEHEADER lpwh;
1124 TRACE("%p\n",hInternet);
1126 lpwh = WININET_GetObject( hInternet );
1127 if (NULL == lpwh)
1129 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
1130 return FALSE;
1133 WININET_Release( lpwh );
1134 WININET_FreeHandle( hInternet );
1136 return TRUE;
1140 /***********************************************************************
1141 * ConvertUrlComponentValue (Internal)
1143 * Helper function for InternetCrackUrlA
1146 static void ConvertUrlComponentValue(LPSTR* lppszComponent, LPDWORD dwComponentLen,
1147 LPWSTR lpwszComponent, DWORD dwwComponentLen,
1148 LPCSTR lpszStart, LPCWSTR lpwszStart)
1150 TRACE("%p %d %p %d %p %p\n", *lppszComponent, *dwComponentLen, lpwszComponent, dwwComponentLen, lpszStart, lpwszStart);
1151 if (*dwComponentLen != 0)
1153 DWORD nASCIILength=WideCharToMultiByte(CP_ACP,0,lpwszComponent,dwwComponentLen,NULL,0,NULL,NULL);
1154 if (*lppszComponent == NULL)
1156 int nASCIIOffset=WideCharToMultiByte(CP_ACP,0,lpwszStart,lpwszComponent-lpwszStart,NULL,0,NULL,NULL);
1157 if (lpwszComponent)
1158 *lppszComponent = (LPSTR)lpszStart+nASCIIOffset;
1159 else
1160 *lppszComponent = NULL;
1161 *dwComponentLen = nASCIILength;
1163 else
1165 DWORD ncpylen = min((*dwComponentLen)-1, nASCIILength);
1166 WideCharToMultiByte(CP_ACP,0,lpwszComponent,dwwComponentLen,*lppszComponent,ncpylen+1,NULL,NULL);
1167 (*lppszComponent)[ncpylen]=0;
1168 *dwComponentLen = ncpylen;
1174 /***********************************************************************
1175 * InternetCrackUrlA (WININET.@)
1177 * See InternetCrackUrlW.
1179 BOOL WINAPI InternetCrackUrlA(LPCSTR lpszUrl, DWORD dwUrlLength, DWORD dwFlags,
1180 LPURL_COMPONENTSA lpUrlComponents)
1182 DWORD nLength;
1183 URL_COMPONENTSW UCW;
1184 BOOL ret = FALSE;
1185 WCHAR *lpwszUrl, *hostname = NULL, *username = NULL, *password = NULL, *path = NULL,
1186 *scheme = NULL, *extra = NULL;
1188 TRACE("(%s %u %x %p)\n",
1189 lpszUrl ? debugstr_an(lpszUrl, dwUrlLength ? dwUrlLength : strlen(lpszUrl)) : "(null)",
1190 dwUrlLength, dwFlags, lpUrlComponents);
1192 if (!lpszUrl || !*lpszUrl || !lpUrlComponents ||
1193 lpUrlComponents->dwStructSize != sizeof(URL_COMPONENTSA))
1195 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1196 return FALSE;
1199 if(dwUrlLength<=0)
1200 dwUrlLength=-1;
1201 nLength=MultiByteToWideChar(CP_ACP,0,lpszUrl,dwUrlLength,NULL,0);
1203 /* if dwUrlLength=-1 then nLength includes null but length to
1204 InternetCrackUrlW should not include it */
1205 if (dwUrlLength == -1) nLength--;
1207 lpwszUrl=HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WCHAR)*nLength);
1208 MultiByteToWideChar(CP_ACP,0,lpszUrl,dwUrlLength,lpwszUrl,nLength);
1210 memset(&UCW,0,sizeof(UCW));
1211 UCW.dwStructSize = sizeof(URL_COMPONENTSW);
1212 if (lpUrlComponents->dwHostNameLength)
1214 UCW.dwHostNameLength = lpUrlComponents->dwHostNameLength;
1215 if (lpUrlComponents->lpszHostName)
1217 hostname = HeapAlloc(GetProcessHeap(), 0, UCW.dwHostNameLength * sizeof(WCHAR));
1218 UCW.lpszHostName = hostname;
1221 if (lpUrlComponents->dwUserNameLength)
1223 UCW.dwUserNameLength = lpUrlComponents->dwUserNameLength;
1224 if (lpUrlComponents->lpszUserName)
1226 username = HeapAlloc(GetProcessHeap(), 0, UCW.dwUserNameLength * sizeof(WCHAR));
1227 UCW.lpszUserName = username;
1230 if (lpUrlComponents->dwPasswordLength)
1232 UCW.dwPasswordLength = lpUrlComponents->dwPasswordLength;
1233 if (lpUrlComponents->lpszPassword)
1235 password = HeapAlloc(GetProcessHeap(), 0, UCW.dwPasswordLength * sizeof(WCHAR));
1236 UCW.lpszPassword = password;
1239 if (lpUrlComponents->dwUrlPathLength)
1241 UCW.dwUrlPathLength = lpUrlComponents->dwUrlPathLength;
1242 if (lpUrlComponents->lpszUrlPath)
1244 path = HeapAlloc(GetProcessHeap(), 0, UCW.dwUrlPathLength * sizeof(WCHAR));
1245 UCW.lpszUrlPath = path;
1248 if (lpUrlComponents->dwSchemeLength)
1250 UCW.dwSchemeLength = lpUrlComponents->dwSchemeLength;
1251 if (lpUrlComponents->lpszScheme)
1253 scheme = HeapAlloc(GetProcessHeap(), 0, UCW.dwSchemeLength * sizeof(WCHAR));
1254 UCW.lpszScheme = scheme;
1257 if (lpUrlComponents->dwExtraInfoLength)
1259 UCW.dwExtraInfoLength = lpUrlComponents->dwExtraInfoLength;
1260 if (lpUrlComponents->lpszExtraInfo)
1262 extra = HeapAlloc(GetProcessHeap(), 0, UCW.dwExtraInfoLength * sizeof(WCHAR));
1263 UCW.lpszExtraInfo = extra;
1266 if ((ret = InternetCrackUrlW(lpwszUrl, nLength, dwFlags, &UCW)))
1268 ConvertUrlComponentValue(&lpUrlComponents->lpszHostName, &lpUrlComponents->dwHostNameLength,
1269 UCW.lpszHostName, UCW.dwHostNameLength, lpszUrl, lpwszUrl);
1270 ConvertUrlComponentValue(&lpUrlComponents->lpszUserName, &lpUrlComponents->dwUserNameLength,
1271 UCW.lpszUserName, UCW.dwUserNameLength, lpszUrl, lpwszUrl);
1272 ConvertUrlComponentValue(&lpUrlComponents->lpszPassword, &lpUrlComponents->dwPasswordLength,
1273 UCW.lpszPassword, UCW.dwPasswordLength, lpszUrl, lpwszUrl);
1274 ConvertUrlComponentValue(&lpUrlComponents->lpszUrlPath, &lpUrlComponents->dwUrlPathLength,
1275 UCW.lpszUrlPath, UCW.dwUrlPathLength, lpszUrl, lpwszUrl);
1276 ConvertUrlComponentValue(&lpUrlComponents->lpszScheme, &lpUrlComponents->dwSchemeLength,
1277 UCW.lpszScheme, UCW.dwSchemeLength, lpszUrl, lpwszUrl);
1278 ConvertUrlComponentValue(&lpUrlComponents->lpszExtraInfo, &lpUrlComponents->dwExtraInfoLength,
1279 UCW.lpszExtraInfo, UCW.dwExtraInfoLength, lpszUrl, lpwszUrl);
1281 lpUrlComponents->nScheme = UCW.nScheme;
1282 lpUrlComponents->nPort = UCW.nPort;
1284 TRACE("%s: scheme(%s) host(%s) path(%s) extra(%s)\n", lpszUrl,
1285 debugstr_an(lpUrlComponents->lpszScheme, lpUrlComponents->dwSchemeLength),
1286 debugstr_an(lpUrlComponents->lpszHostName, lpUrlComponents->dwHostNameLength),
1287 debugstr_an(lpUrlComponents->lpszUrlPath, lpUrlComponents->dwUrlPathLength),
1288 debugstr_an(lpUrlComponents->lpszExtraInfo, lpUrlComponents->dwExtraInfoLength));
1290 HeapFree(GetProcessHeap(), 0, lpwszUrl);
1291 HeapFree(GetProcessHeap(), 0, hostname);
1292 HeapFree(GetProcessHeap(), 0, username);
1293 HeapFree(GetProcessHeap(), 0, password);
1294 HeapFree(GetProcessHeap(), 0, path);
1295 HeapFree(GetProcessHeap(), 0, scheme);
1296 HeapFree(GetProcessHeap(), 0, extra);
1297 return ret;
1300 static const WCHAR url_schemes[][7] =
1302 {'f','t','p',0},
1303 {'g','o','p','h','e','r',0},
1304 {'h','t','t','p',0},
1305 {'h','t','t','p','s',0},
1306 {'f','i','l','e',0},
1307 {'n','e','w','s',0},
1308 {'m','a','i','l','t','o',0},
1309 {'r','e','s',0},
1312 /***********************************************************************
1313 * GetInternetSchemeW (internal)
1315 * Get scheme of url
1317 * RETURNS
1318 * scheme on success
1319 * INTERNET_SCHEME_UNKNOWN on failure
1322 static INTERNET_SCHEME GetInternetSchemeW(LPCWSTR lpszScheme, DWORD nMaxCmp)
1324 int i;
1326 TRACE("%s %d\n",debugstr_wn(lpszScheme, nMaxCmp), nMaxCmp);
1328 if(lpszScheme==NULL)
1329 return INTERNET_SCHEME_UNKNOWN;
1331 for (i = 0; i < sizeof(url_schemes)/sizeof(url_schemes[0]); i++)
1332 if (!strncmpW(lpszScheme, url_schemes[i], nMaxCmp))
1333 return INTERNET_SCHEME_FIRST + i;
1335 return INTERNET_SCHEME_UNKNOWN;
1338 /***********************************************************************
1339 * SetUrlComponentValueW (Internal)
1341 * Helper function for InternetCrackUrlW
1343 * PARAMS
1344 * lppszComponent [O] Holds the returned string
1345 * dwComponentLen [I] Holds the size of lppszComponent
1346 * [O] Holds the length of the string in lppszComponent without '\0'
1347 * lpszStart [I] Holds the string to copy from
1348 * len [I] Holds the length of lpszStart without '\0'
1350 * RETURNS
1351 * TRUE on success
1352 * FALSE on failure
1355 static BOOL SetUrlComponentValueW(LPWSTR* lppszComponent, LPDWORD dwComponentLen, LPCWSTR lpszStart, DWORD len)
1357 TRACE("%s (%d)\n", debugstr_wn(lpszStart,len), len);
1359 if ( (*dwComponentLen == 0) && (*lppszComponent == NULL) )
1360 return FALSE;
1362 if (*dwComponentLen != 0 || *lppszComponent == NULL)
1364 if (*lppszComponent == NULL)
1366 *lppszComponent = (LPWSTR)lpszStart;
1367 *dwComponentLen = len;
1369 else
1371 DWORD ncpylen = min((*dwComponentLen)-1, len);
1372 memcpy(*lppszComponent, lpszStart, ncpylen*sizeof(WCHAR));
1373 (*lppszComponent)[ncpylen] = '\0';
1374 *dwComponentLen = ncpylen;
1378 return TRUE;
1381 /***********************************************************************
1382 * InternetCrackUrlW (WININET.@)
1384 * Break up URL into its components
1386 * RETURNS
1387 * TRUE on success
1388 * FALSE on failure
1390 BOOL WINAPI InternetCrackUrlW(LPCWSTR lpszUrl_orig, DWORD dwUrlLength_orig, DWORD dwFlags,
1391 LPURL_COMPONENTSW lpUC)
1394 * RFC 1808
1395 * <protocol>:[//<net_loc>][/path][;<params>][?<query>][#<fragment>]
1398 LPCWSTR lpszParam = NULL;
1399 BOOL bIsAbsolute = FALSE;
1400 LPCWSTR lpszap, lpszUrl = lpszUrl_orig;
1401 LPCWSTR lpszcp = NULL;
1402 LPWSTR lpszUrl_decode = NULL;
1403 DWORD dwUrlLength = dwUrlLength_orig;
1405 TRACE("(%s %u %x %p)\n",
1406 lpszUrl ? debugstr_wn(lpszUrl, dwUrlLength ? dwUrlLength : strlenW(lpszUrl)) : "(null)",
1407 dwUrlLength, dwFlags, lpUC);
1409 if (!lpszUrl_orig || !*lpszUrl_orig || !lpUC)
1411 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1412 return FALSE;
1414 if (!dwUrlLength) dwUrlLength = strlenW(lpszUrl);
1416 if (dwFlags & ICU_DECODE)
1418 WCHAR *url_tmp;
1419 DWORD len = dwUrlLength + 1;
1421 if (!(url_tmp = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR))))
1423 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1424 return FALSE;
1426 memcpy(url_tmp, lpszUrl_orig, dwUrlLength * sizeof(WCHAR));
1427 url_tmp[dwUrlLength] = 0;
1428 if (!(lpszUrl_decode = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR))))
1430 HeapFree(GetProcessHeap(), 0, url_tmp);
1431 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1432 return FALSE;
1434 if (InternetCanonicalizeUrlW(url_tmp, lpszUrl_decode, &len, ICU_DECODE | ICU_NO_ENCODE))
1436 dwUrlLength = len;
1437 lpszUrl = lpszUrl_decode;
1439 HeapFree(GetProcessHeap(), 0, url_tmp);
1441 lpszap = lpszUrl;
1443 /* Determine if the URI is absolute. */
1444 while (lpszap - lpszUrl < dwUrlLength)
1446 if (isalnumW(*lpszap))
1448 lpszap++;
1449 continue;
1451 if ((*lpszap == ':') && (lpszap - lpszUrl >= 2))
1453 bIsAbsolute = TRUE;
1454 lpszcp = lpszap;
1456 else
1458 lpszcp = lpszUrl; /* Relative url */
1461 break;
1464 lpUC->nScheme = INTERNET_SCHEME_UNKNOWN;
1465 lpUC->nPort = INTERNET_INVALID_PORT_NUMBER;
1467 /* Parse <params> */
1468 if (!(lpszParam = memchrW(lpszap, ';', dwUrlLength - (lpszap - lpszUrl))))
1469 lpszParam = memchrW(lpszap, '?', dwUrlLength - (lpszap - lpszUrl));
1471 SetUrlComponentValueW(&lpUC->lpszExtraInfo, &lpUC->dwExtraInfoLength,
1472 lpszParam, lpszParam ? dwUrlLength-(lpszParam-lpszUrl) : 0);
1474 if (bIsAbsolute) /* Parse <protocol>:[//<net_loc>] */
1476 LPCWSTR lpszNetLoc;
1478 /* Get scheme first. */
1479 lpUC->nScheme = GetInternetSchemeW(lpszUrl, lpszcp - lpszUrl);
1480 SetUrlComponentValueW(&lpUC->lpszScheme, &lpUC->dwSchemeLength,
1481 lpszUrl, lpszcp - lpszUrl);
1483 /* Eat ':' in protocol. */
1484 lpszcp++;
1486 /* double slash indicates the net_loc portion is present */
1487 if ((lpszcp[0] == '/') && (lpszcp[1] == '/'))
1489 lpszcp += 2;
1491 lpszNetLoc = memchrW(lpszcp, '/', dwUrlLength - (lpszcp - lpszUrl));
1492 if (lpszParam)
1494 if (lpszNetLoc)
1495 lpszNetLoc = min(lpszNetLoc, lpszParam);
1496 else
1497 lpszNetLoc = lpszParam;
1499 else if (!lpszNetLoc)
1500 lpszNetLoc = lpszcp + dwUrlLength-(lpszcp-lpszUrl);
1502 /* Parse net-loc */
1503 if (lpszNetLoc)
1505 LPCWSTR lpszHost;
1506 LPCWSTR lpszPort;
1508 /* [<user>[<:password>]@]<host>[:<port>] */
1509 /* First find the user and password if they exist */
1511 lpszHost = memchrW(lpszcp, '@', dwUrlLength - (lpszcp - lpszUrl));
1512 if (lpszHost == NULL || lpszHost > lpszNetLoc)
1514 /* username and password not specified. */
1515 SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength, NULL, 0);
1516 SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength, NULL, 0);
1518 else /* Parse out username and password */
1520 LPCWSTR lpszUser = lpszcp;
1521 LPCWSTR lpszPasswd = lpszHost;
1523 while (lpszcp < lpszHost)
1525 if (*lpszcp == ':')
1526 lpszPasswd = lpszcp;
1528 lpszcp++;
1531 SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength,
1532 lpszUser, lpszPasswd - lpszUser);
1534 if (lpszPasswd != lpszHost)
1535 lpszPasswd++;
1536 SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength,
1537 lpszPasswd == lpszHost ? NULL : lpszPasswd,
1538 lpszHost - lpszPasswd);
1540 lpszcp++; /* Advance to beginning of host */
1543 /* Parse <host><:port> */
1545 lpszHost = lpszcp;
1546 lpszPort = lpszNetLoc;
1548 /* special case for res:// URLs: there is no port here, so the host is the
1549 entire string up to the first '/' */
1550 if(lpUC->nScheme==INTERNET_SCHEME_RES)
1552 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength,
1553 lpszHost, lpszPort - lpszHost);
1554 lpszcp=lpszNetLoc;
1556 else
1558 while (lpszcp < lpszNetLoc)
1560 if (*lpszcp == ':')
1561 lpszPort = lpszcp;
1563 lpszcp++;
1566 /* If the scheme is "file" and the host is just one letter, it's not a host */
1567 if(lpUC->nScheme==INTERNET_SCHEME_FILE && (lpszPort-lpszHost)==1)
1569 lpszcp=lpszHost;
1570 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength,
1571 NULL, 0);
1573 else
1575 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength,
1576 lpszHost, lpszPort - lpszHost);
1577 if (lpszPort != lpszNetLoc)
1578 lpUC->nPort = atoiW(++lpszPort);
1579 else switch (lpUC->nScheme)
1581 case INTERNET_SCHEME_HTTP:
1582 lpUC->nPort = INTERNET_DEFAULT_HTTP_PORT;
1583 break;
1584 case INTERNET_SCHEME_HTTPS:
1585 lpUC->nPort = INTERNET_DEFAULT_HTTPS_PORT;
1586 break;
1587 case INTERNET_SCHEME_FTP:
1588 lpUC->nPort = INTERNET_DEFAULT_FTP_PORT;
1589 break;
1590 case INTERNET_SCHEME_GOPHER:
1591 lpUC->nPort = INTERNET_DEFAULT_GOPHER_PORT;
1592 break;
1593 default:
1594 break;
1600 else
1602 SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength, NULL, 0);
1603 SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength, NULL, 0);
1604 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength, NULL, 0);
1607 else
1609 SetUrlComponentValueW(&lpUC->lpszScheme, &lpUC->dwSchemeLength, NULL, 0);
1610 SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength, NULL, 0);
1611 SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength, NULL, 0);
1612 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength, NULL, 0);
1615 /* Here lpszcp points to:
1617 * <protocol>:[//<net_loc>][/path][;<params>][?<query>][#<fragment>]
1618 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1620 if (lpszcp != 0 && lpszcp - lpszUrl < dwUrlLength && (!lpszParam || lpszcp < lpszParam))
1622 INT len;
1624 /* Only truncate the parameter list if it's already been saved
1625 * in lpUC->lpszExtraInfo.
1627 if (lpszParam && lpUC->dwExtraInfoLength && lpUC->lpszExtraInfo)
1628 len = lpszParam - lpszcp;
1629 else
1631 /* Leave the parameter list in lpszUrlPath. Strip off any trailing
1632 * newlines if necessary.
1634 LPWSTR lpsznewline = memchrW(lpszcp, '\n', dwUrlLength - (lpszcp - lpszUrl));
1635 if (lpsznewline != NULL)
1636 len = lpsznewline - lpszcp;
1637 else
1638 len = dwUrlLength-(lpszcp-lpszUrl);
1640 SetUrlComponentValueW(&lpUC->lpszUrlPath, &lpUC->dwUrlPathLength,
1641 lpszcp, len);
1643 else
1645 if (lpUC->lpszUrlPath && (lpUC->dwUrlPathLength > 0))
1646 lpUC->lpszUrlPath[0] = 0;
1647 lpUC->dwUrlPathLength = 0;
1650 TRACE("%s: scheme(%s) host(%s) path(%s) extra(%s)\n", debugstr_wn(lpszUrl,dwUrlLength),
1651 debugstr_wn(lpUC->lpszScheme,lpUC->dwSchemeLength),
1652 debugstr_wn(lpUC->lpszHostName,lpUC->dwHostNameLength),
1653 debugstr_wn(lpUC->lpszUrlPath,lpUC->dwUrlPathLength),
1654 debugstr_wn(lpUC->lpszExtraInfo,lpUC->dwExtraInfoLength));
1656 HeapFree(GetProcessHeap(), 0, lpszUrl_decode );
1657 return TRUE;
1660 /***********************************************************************
1661 * InternetAttemptConnect (WININET.@)
1663 * Attempt to make a connection to the internet
1665 * RETURNS
1666 * ERROR_SUCCESS on success
1667 * Error value on failure
1670 DWORD WINAPI InternetAttemptConnect(DWORD dwReserved)
1672 FIXME("Stub\n");
1673 return ERROR_SUCCESS;
1677 /***********************************************************************
1678 * InternetCanonicalizeUrlA (WININET.@)
1680 * Escape unsafe characters and spaces
1682 * RETURNS
1683 * TRUE on success
1684 * FALSE on failure
1687 BOOL WINAPI InternetCanonicalizeUrlA(LPCSTR lpszUrl, LPSTR lpszBuffer,
1688 LPDWORD lpdwBufferLength, DWORD dwFlags)
1690 HRESULT hr;
1691 DWORD dwURLFlags = URL_WININET_COMPATIBILITY | URL_ESCAPE_UNSAFE;
1693 TRACE("(%s, %p, %p, 0x%08x) bufferlength: %d\n", debugstr_a(lpszUrl), lpszBuffer,
1694 lpdwBufferLength, lpdwBufferLength ? *lpdwBufferLength : -1, dwFlags);
1696 if(dwFlags & ICU_DECODE)
1698 dwURLFlags |= URL_UNESCAPE;
1699 dwFlags &= ~ICU_DECODE;
1702 if(dwFlags & ICU_ESCAPE)
1704 dwURLFlags |= URL_UNESCAPE;
1705 dwFlags &= ~ICU_ESCAPE;
1708 if(dwFlags & ICU_BROWSER_MODE)
1710 dwURLFlags |= URL_BROWSER_MODE;
1711 dwFlags &= ~ICU_BROWSER_MODE;
1714 if(dwFlags & ICU_NO_ENCODE)
1716 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
1717 dwURLFlags ^= URL_ESCAPE_UNSAFE;
1718 dwFlags &= ~ICU_NO_ENCODE;
1721 if (dwFlags) FIXME("Unhandled flags 0x%08x\n", dwFlags);
1723 hr = UrlCanonicalizeA(lpszUrl, lpszBuffer, lpdwBufferLength, dwURLFlags);
1724 if (hr == E_POINTER) SetLastError(ERROR_INSUFFICIENT_BUFFER);
1725 if (hr == E_INVALIDARG) SetLastError(ERROR_INVALID_PARAMETER);
1727 return (hr == S_OK) ? TRUE : FALSE;
1730 /***********************************************************************
1731 * InternetCanonicalizeUrlW (WININET.@)
1733 * Escape unsafe characters and spaces
1735 * RETURNS
1736 * TRUE on success
1737 * FALSE on failure
1740 BOOL WINAPI InternetCanonicalizeUrlW(LPCWSTR lpszUrl, LPWSTR lpszBuffer,
1741 LPDWORD lpdwBufferLength, DWORD dwFlags)
1743 HRESULT hr;
1744 DWORD dwURLFlags = URL_WININET_COMPATIBILITY | URL_ESCAPE_UNSAFE;
1746 TRACE("(%s, %p, %p, 0x%08x) bufferlength: %d\n", debugstr_w(lpszUrl), lpszBuffer,
1747 lpdwBufferLength, dwFlags, lpdwBufferLength ? *lpdwBufferLength : -1);
1749 if(dwFlags & ICU_DECODE)
1751 dwURLFlags |= URL_UNESCAPE;
1752 dwFlags &= ~ICU_DECODE;
1755 if(dwFlags & ICU_ESCAPE)
1757 dwURLFlags |= URL_UNESCAPE;
1758 dwFlags &= ~ICU_ESCAPE;
1761 if(dwFlags & ICU_BROWSER_MODE)
1763 dwURLFlags |= URL_BROWSER_MODE;
1764 dwFlags &= ~ICU_BROWSER_MODE;
1767 if(dwFlags & ICU_NO_ENCODE)
1769 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
1770 dwURLFlags ^= URL_ESCAPE_UNSAFE;
1771 dwFlags &= ~ICU_NO_ENCODE;
1774 if (dwFlags) FIXME("Unhandled flags 0x%08x\n", dwFlags);
1776 hr = UrlCanonicalizeW(lpszUrl, lpszBuffer, lpdwBufferLength, dwURLFlags);
1777 if (hr == E_POINTER) SetLastError(ERROR_INSUFFICIENT_BUFFER);
1778 if (hr == E_INVALIDARG) SetLastError(ERROR_INVALID_PARAMETER);
1780 return (hr == S_OK) ? TRUE : FALSE;
1783 /* #################################################### */
1785 static INTERNET_STATUS_CALLBACK set_status_callback(
1786 LPWININETHANDLEHEADER lpwh, INTERNET_STATUS_CALLBACK callback, BOOL unicode)
1788 INTERNET_STATUS_CALLBACK ret;
1790 if (unicode) lpwh->dwInternalFlags |= INET_CALLBACKW;
1791 else lpwh->dwInternalFlags &= ~INET_CALLBACKW;
1793 ret = lpwh->lpfnStatusCB;
1794 lpwh->lpfnStatusCB = callback;
1796 return ret;
1799 /***********************************************************************
1800 * InternetSetStatusCallbackA (WININET.@)
1802 * Sets up a callback function which is called as progress is made
1803 * during an operation.
1805 * RETURNS
1806 * Previous callback or NULL on success
1807 * INTERNET_INVALID_STATUS_CALLBACK on failure
1810 INTERNET_STATUS_CALLBACK WINAPI InternetSetStatusCallbackA(
1811 HINTERNET hInternet ,INTERNET_STATUS_CALLBACK lpfnIntCB)
1813 INTERNET_STATUS_CALLBACK retVal;
1814 LPWININETHANDLEHEADER lpwh;
1816 TRACE("0x%08x\n", (ULONG)hInternet);
1818 if (!(lpwh = WININET_GetObject(hInternet)))
1819 return INTERNET_INVALID_STATUS_CALLBACK;
1821 retVal = set_status_callback(lpwh, lpfnIntCB, FALSE);
1823 WININET_Release( lpwh );
1824 return retVal;
1827 /***********************************************************************
1828 * InternetSetStatusCallbackW (WININET.@)
1830 * Sets up a callback function which is called as progress is made
1831 * during an operation.
1833 * RETURNS
1834 * Previous callback or NULL on success
1835 * INTERNET_INVALID_STATUS_CALLBACK on failure
1838 INTERNET_STATUS_CALLBACK WINAPI InternetSetStatusCallbackW(
1839 HINTERNET hInternet ,INTERNET_STATUS_CALLBACK lpfnIntCB)
1841 INTERNET_STATUS_CALLBACK retVal;
1842 LPWININETHANDLEHEADER lpwh;
1844 TRACE("0x%08x\n", (ULONG)hInternet);
1846 if (!(lpwh = WININET_GetObject(hInternet)))
1847 return INTERNET_INVALID_STATUS_CALLBACK;
1849 retVal = set_status_callback(lpwh, lpfnIntCB, TRUE);
1851 WININET_Release( lpwh );
1852 return retVal;
1855 /***********************************************************************
1856 * InternetSetFilePointer (WININET.@)
1858 DWORD WINAPI InternetSetFilePointer(HINTERNET hFile, LONG lDistanceToMove,
1859 PVOID pReserved, DWORD dwMoveContext, DWORD_PTR dwContext)
1861 FIXME("stub\n");
1862 return FALSE;
1865 /***********************************************************************
1866 * InternetWriteFile (WININET.@)
1868 * Write data to an open internet file
1870 * RETURNS
1871 * TRUE on success
1872 * FALSE on failure
1875 BOOL WINAPI InternetWriteFile(HINTERNET hFile, LPCVOID lpBuffer,
1876 DWORD dwNumOfBytesToWrite, LPDWORD lpdwNumOfBytesWritten)
1878 LPWININETHANDLEHEADER lpwh;
1879 BOOL retval = FALSE;
1881 TRACE("(%p %p %d %p)\n", hFile, lpBuffer, dwNumOfBytesToWrite, lpdwNumOfBytesWritten);
1883 lpwh = WININET_GetObject( hFile );
1884 if (!lpwh) {
1885 WARN("Invalid handle\n");
1886 SetLastError(ERROR_INVALID_HANDLE);
1887 return FALSE;
1890 if(lpwh->vtbl->WriteFile) {
1891 retval = lpwh->vtbl->WriteFile(lpwh, lpBuffer, dwNumOfBytesToWrite, lpdwNumOfBytesWritten);
1892 }else {
1893 WARN("No Writefile method.\n");
1894 SetLastError(ERROR_INVALID_HANDLE);
1895 retval = FALSE;
1898 WININET_Release( lpwh );
1900 return retval;
1904 /***********************************************************************
1905 * InternetReadFile (WININET.@)
1907 * Read data from an open internet file
1909 * RETURNS
1910 * TRUE on success
1911 * FALSE on failure
1914 BOOL WINAPI InternetReadFile(HINTERNET hFile, LPVOID lpBuffer,
1915 DWORD dwNumOfBytesToRead, LPDWORD pdwNumOfBytesRead)
1917 LPWININETHANDLEHEADER hdr;
1918 DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
1920 TRACE("%p %p %d %p\n", hFile, lpBuffer, dwNumOfBytesToRead, pdwNumOfBytesRead);
1922 hdr = WININET_GetObject(hFile);
1923 if (!hdr) {
1924 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
1925 return FALSE;
1928 if(hdr->vtbl->ReadFile)
1929 res = hdr->vtbl->ReadFile(hdr, lpBuffer, dwNumOfBytesToRead, pdwNumOfBytesRead);
1931 WININET_Release(hdr);
1933 TRACE("-- %s (%u) (bytes read: %d)\n", res == ERROR_SUCCESS ? "TRUE": "FALSE", res,
1934 pdwNumOfBytesRead ? *pdwNumOfBytesRead : -1);
1936 if(res != ERROR_SUCCESS)
1937 SetLastError(res);
1938 return res == ERROR_SUCCESS;
1941 /***********************************************************************
1942 * InternetReadFileExA (WININET.@)
1944 * Read data from an open internet file
1946 * PARAMS
1947 * hFile [I] Handle returned by InternetOpenUrl or HttpOpenRequest.
1948 * lpBuffersOut [I/O] Buffer.
1949 * dwFlags [I] Flags. See notes.
1950 * dwContext [I] Context for callbacks.
1952 * RETURNS
1953 * TRUE on success
1954 * FALSE on failure
1956 * NOTES
1957 * The parameter dwFlags include zero or more of the following flags:
1958 *|IRF_ASYNC - Makes the call asynchronous.
1959 *|IRF_SYNC - Makes the call synchronous.
1960 *|IRF_USE_CONTEXT - Forces dwContext to be used.
1961 *|IRF_NO_WAIT - Don't block if the data is not available, just return what is available.
1963 * However, in testing IRF_USE_CONTEXT seems to have no effect - dwContext isn't used.
1965 * SEE
1966 * InternetOpenUrlA(), HttpOpenRequestA()
1968 BOOL WINAPI InternetReadFileExA(HINTERNET hFile, LPINTERNET_BUFFERSA lpBuffersOut,
1969 DWORD dwFlags, DWORD_PTR dwContext)
1971 LPWININETHANDLEHEADER hdr;
1972 DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
1974 TRACE("(%p %p 0x%x 0x%lx)\n", hFile, lpBuffersOut, dwFlags, dwContext);
1976 hdr = WININET_GetObject(hFile);
1977 if (!hdr) {
1978 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
1979 return FALSE;
1982 if(hdr->vtbl->ReadFileExA)
1983 res = hdr->vtbl->ReadFileExA(hdr, lpBuffersOut, dwFlags, dwContext);
1985 WININET_Release(hdr);
1987 TRACE("-- %s (%u, bytes read: %d)\n", res == ERROR_SUCCESS ? "TRUE": "FALSE",
1988 res, lpBuffersOut->dwBufferLength);
1990 if(res != ERROR_SUCCESS)
1991 SetLastError(res);
1992 return res == ERROR_SUCCESS;
1995 /***********************************************************************
1996 * InternetReadFileExW (WININET.@)
1998 * Read data from an open internet file.
2000 * PARAMS
2001 * hFile [I] Handle returned by InternetOpenUrl() or HttpOpenRequest().
2002 * lpBuffersOut [I/O] Buffer.
2003 * dwFlags [I] Flags.
2004 * dwContext [I] Context for callbacks.
2006 * RETURNS
2007 * FALSE, last error is set to ERROR_CALL_NOT_IMPLEMENTED
2009 * NOTES
2010 * Not implemented in Wine or native either (as of IE6 SP2).
2013 BOOL WINAPI InternetReadFileExW(HINTERNET hFile, LPINTERNET_BUFFERSW lpBuffer,
2014 DWORD dwFlags, DWORD_PTR dwContext)
2016 ERR("(%p, %p, 0x%x, 0x%lx): not implemented in native\n", hFile, lpBuffer, dwFlags, dwContext);
2018 INTERNET_SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2019 return FALSE;
2022 DWORD INET_QueryOption(DWORD option, void *buffer, DWORD *size, BOOL unicode)
2024 switch(option) {
2025 case INTERNET_OPTION_REQUEST_FLAGS:
2026 TRACE("INTERNET_OPTION_REQUEST_FLAGS\n");
2028 if (*size < sizeof(ULONG))
2029 return ERROR_INSUFFICIENT_BUFFER;
2031 *(ULONG*)buffer = 4;
2032 *size = sizeof(ULONG);
2034 return ERROR_SUCCESS;
2036 case INTERNET_OPTION_HTTP_VERSION:
2037 if (*size < sizeof(HTTP_VERSION_INFO))
2038 return ERROR_INSUFFICIENT_BUFFER;
2041 * Presently hardcoded to 1.1
2043 ((HTTP_VERSION_INFO*)buffer)->dwMajorVersion = 1;
2044 ((HTTP_VERSION_INFO*)buffer)->dwMinorVersion = 1;
2045 *size = sizeof(HTTP_VERSION_INFO);
2047 return ERROR_SUCCESS;
2049 case INTERNET_OPTION_CONNECTED_STATE:
2050 FIXME("INTERNET_OPTION_CONNECTED_STATE: semi-stub\n");
2052 if (*size < sizeof(ULONG))
2053 return ERROR_INSUFFICIENT_BUFFER;
2055 *(ULONG*)buffer = INTERNET_STATE_CONNECTED;
2056 *size = sizeof(ULONG);
2058 return ERROR_SUCCESS;
2060 case INTERNET_OPTION_PROXY: {
2061 WININETAPPINFOW ai;
2063 TRACE("Getting global proxy info\n");
2064 memset(&ai, 0, sizeof(WININETAPPINFOW));
2065 INTERNET_ConfigureProxy(&ai);
2067 return APPINFO_QueryOption(&ai.hdr, INTERNET_OPTION_PROXY, buffer, size, unicode); /* FIXME */
2071 FIXME("Stub for %d\n", option);
2072 return ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2075 /***********************************************************************
2076 * INET_QueryOptionHelper (internal)
2078 static BOOL INET_QueryOptionHelper(BOOL bIsUnicode, HINTERNET hInternet, DWORD dwOption,
2079 LPVOID lpBuffer, LPDWORD lpdwBufferLength)
2081 LPWININETHANDLEHEADER lpwhh;
2082 BOOL bSuccess = FALSE;
2084 TRACE("(%p, 0x%08x, %p, %p)\n", hInternet, dwOption, lpBuffer, lpdwBufferLength);
2086 lpwhh = WININET_GetObject( hInternet );
2088 switch (dwOption)
2090 case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2092 ULONG conn = 2;
2093 TRACE("INTERNET_OPTION_MAX_CONNS_PER_SERVER: %d\n", conn);
2094 if (*lpdwBufferLength < sizeof(ULONG))
2095 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2096 else
2098 memcpy(lpBuffer, &conn, sizeof(ULONG));
2099 bSuccess = TRUE;
2101 *lpdwBufferLength = sizeof(ULONG);
2102 break;
2104 case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2106 ULONG conn = 4;
2107 TRACE("INTERNET_OPTION_MAX_CONNS_1_0_SERVER: %d\n", conn);
2108 if (*lpdwBufferLength < sizeof(ULONG))
2109 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2110 else
2112 memcpy(lpBuffer, &conn, sizeof(ULONG));
2113 bSuccess = TRUE;
2115 *lpdwBufferLength = sizeof(ULONG);
2116 break;
2118 case INTERNET_OPTION_SECURITY_FLAGS:
2119 FIXME("INTERNET_OPTION_SECURITY_FLAGS: Stub\n");
2120 bSuccess = TRUE;
2121 break;
2123 case INTERNET_OPTION_VERSION:
2125 TRACE("INTERNET_OPTION_VERSION\n");
2126 if (*lpdwBufferLength < sizeof(INTERNET_VERSION_INFO))
2127 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2128 else
2130 static const INTERNET_VERSION_INFO info = { 1, 2 };
2131 memcpy(lpBuffer, &info, sizeof(info));
2132 *lpdwBufferLength = sizeof(info);
2133 bSuccess = TRUE;
2135 break;
2137 case INTERNET_OPTION_PER_CONNECTION_OPTION:
2138 FIXME("INTERNET_OPTION_PER_CONNECTION_OPTION stub\n");
2139 if (*lpdwBufferLength < sizeof(INTERNET_PER_CONN_OPTION_LISTW))
2140 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2141 else
2143 INTERNET_PER_CONN_OPTION_LISTW *con = lpBuffer;
2144 int x;
2145 bSuccess = TRUE;
2146 for (x = 0; x < con->dwOptionCount; ++x)
2148 INTERNET_PER_CONN_OPTIONW *option = con->pOptions + x;
2149 switch (option->dwOption)
2151 case INTERNET_PER_CONN_FLAGS:
2152 option->Value.dwValue = PROXY_TYPE_DIRECT;
2153 break;
2155 case INTERNET_PER_CONN_PROXY_SERVER:
2156 case INTERNET_PER_CONN_PROXY_BYPASS:
2157 case INTERNET_PER_CONN_AUTOCONFIG_URL:
2158 case INTERNET_PER_CONN_AUTODISCOVERY_FLAGS:
2159 case INTERNET_PER_CONN_AUTOCONFIG_SECONDARY_URL:
2160 case INTERNET_PER_CONN_AUTOCONFIG_RELOAD_DELAY_MINS:
2161 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_TIME:
2162 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_URL:
2163 FIXME("Unhandled dwOption %d\n", option->dwOption);
2164 option->Value.dwValue = 0;
2165 bSuccess = FALSE;
2166 break;
2168 default:
2169 FIXME("Unknown dwOption %d\n", option->dwOption);
2170 bSuccess = FALSE;
2171 break;
2174 if (!bSuccess)
2175 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
2177 break;
2178 case 66:
2179 FIXME("66\n");
2180 bSuccess = TRUE;
2181 break;
2182 default: {
2183 DWORD res;
2185 if(lpwhh)
2186 res = lpwhh->vtbl->QueryOption(lpwhh, dwOption, lpBuffer, lpdwBufferLength, bIsUnicode);
2187 else
2188 res = INET_QueryOption(dwOption, lpBuffer, lpdwBufferLength, bIsUnicode);
2190 if(res == ERROR_SUCCESS)
2191 bSuccess = TRUE;
2192 else
2193 SetLastError(res);
2196 if (lpwhh)
2197 WININET_Release( lpwhh );
2199 return bSuccess;
2202 /***********************************************************************
2203 * InternetQueryOptionW (WININET.@)
2205 * Queries an options on the specified handle
2207 * RETURNS
2208 * TRUE on success
2209 * FALSE on failure
2212 BOOL WINAPI InternetQueryOptionW(HINTERNET hInternet, DWORD dwOption,
2213 LPVOID lpBuffer, LPDWORD lpdwBufferLength)
2215 return INET_QueryOptionHelper(TRUE, hInternet, dwOption, lpBuffer, lpdwBufferLength);
2218 /***********************************************************************
2219 * InternetQueryOptionA (WININET.@)
2221 * Queries an options on the specified handle
2223 * RETURNS
2224 * TRUE on success
2225 * FALSE on failure
2228 BOOL WINAPI InternetQueryOptionA(HINTERNET hInternet, DWORD dwOption,
2229 LPVOID lpBuffer, LPDWORD lpdwBufferLength)
2231 return INET_QueryOptionHelper(FALSE, hInternet, dwOption, lpBuffer, lpdwBufferLength);
2235 /***********************************************************************
2236 * InternetSetOptionW (WININET.@)
2238 * Sets an options on the specified handle
2240 * RETURNS
2241 * TRUE on success
2242 * FALSE on failure
2245 BOOL WINAPI InternetSetOptionW(HINTERNET hInternet, DWORD dwOption,
2246 LPVOID lpBuffer, DWORD dwBufferLength)
2248 LPWININETHANDLEHEADER lpwhh;
2249 BOOL ret = TRUE;
2251 TRACE("(%p %d %p %d)\n", hInternet, dwOption, lpBuffer, dwBufferLength);
2253 lpwhh = (LPWININETHANDLEHEADER) WININET_GetObject( hInternet );
2254 if(lpwhh && lpwhh->vtbl->SetOption) {
2255 DWORD res;
2257 res = lpwhh->vtbl->SetOption(lpwhh, dwOption, lpBuffer, dwBufferLength);
2258 if(res != ERROR_INTERNET_INVALID_OPTION) {
2259 WININET_Release( lpwhh );
2261 if(res != ERROR_SUCCESS)
2262 SetLastError(res);
2264 return res == ERROR_SUCCESS;
2268 switch (dwOption)
2270 case INTERNET_OPTION_CALLBACK:
2272 INTERNET_STATUS_CALLBACK callback = *(INTERNET_STATUS_CALLBACK *)lpBuffer;
2273 ret = (set_status_callback(lpwhh, callback, TRUE) != INTERNET_INVALID_STATUS_CALLBACK);
2274 break;
2276 case INTERNET_OPTION_HTTP_VERSION:
2278 HTTP_VERSION_INFO* pVersion=(HTTP_VERSION_INFO*)lpBuffer;
2279 FIXME("Option INTERNET_OPTION_HTTP_VERSION(%d,%d): STUB\n",pVersion->dwMajorVersion,pVersion->dwMinorVersion);
2281 break;
2282 case INTERNET_OPTION_ERROR_MASK:
2284 ULONG flags = *(ULONG *)lpBuffer;
2285 FIXME("Option INTERNET_OPTION_ERROR_MASK(%d): STUB\n", flags);
2287 break;
2288 case INTERNET_OPTION_CODEPAGE:
2290 ULONG codepage = *(ULONG *)lpBuffer;
2291 FIXME("Option INTERNET_OPTION_CODEPAGE (%d): STUB\n", codepage);
2293 break;
2294 case INTERNET_OPTION_REQUEST_PRIORITY:
2296 ULONG priority = *(ULONG *)lpBuffer;
2297 FIXME("Option INTERNET_OPTION_REQUEST_PRIORITY (%d): STUB\n", priority);
2299 break;
2300 case INTERNET_OPTION_CONNECT_TIMEOUT:
2302 ULONG connecttimeout = *(ULONG *)lpBuffer;
2303 FIXME("Option INTERNET_OPTION_CONNECT_TIMEOUT (%d): STUB\n", connecttimeout);
2305 break;
2306 case INTERNET_OPTION_DATA_RECEIVE_TIMEOUT:
2308 ULONG receivetimeout = *(ULONG *)lpBuffer;
2309 FIXME("Option INTERNET_OPTION_DATA_RECEIVE_TIMEOUT (%d): STUB\n", receivetimeout);
2311 break;
2312 case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2314 ULONG conns = *(ULONG *)lpBuffer;
2315 FIXME("Option INTERNET_OPTION_MAX_CONNS_PER_SERVER (%d): STUB\n", conns);
2317 break;
2318 case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2320 ULONG conns = *(ULONG *)lpBuffer;
2321 FIXME("Option INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER (%d): STUB\n", conns);
2323 break;
2324 case INTERNET_OPTION_RESET_URLCACHE_SESSION:
2325 FIXME("Option INTERNET_OPTION_RESET_URLCACHE_SESSION: STUB\n");
2326 break;
2327 case INTERNET_OPTION_END_BROWSER_SESSION:
2328 FIXME("Option INTERNET_OPTION_END_BROWSER_SESSION: STUB\n");
2329 break;
2330 case INTERNET_OPTION_CONNECTED_STATE:
2331 FIXME("Option INTERNET_OPTION_CONNECTED_STATE: STUB\n");
2332 break;
2333 case INTERNET_OPTION_DISABLE_PASSPORT_AUTH:
2334 TRACE("Option INTERNET_OPTION_DISABLE_PASSPORT_AUTH: harmless stub, since not enabled\n");
2335 break;
2336 case INTERNET_OPTION_SEND_TIMEOUT:
2337 case INTERNET_OPTION_RECEIVE_TIMEOUT:
2339 ULONG timeout = *(ULONG *)lpBuffer;
2340 FIXME("INTERNET_OPTION_SEND/RECEIVE_TIMEOUT %d\n", timeout);
2341 break;
2343 case INTERNET_OPTION_CONNECT_RETRIES:
2345 ULONG retries = *(ULONG *)lpBuffer;
2346 FIXME("INTERNET_OPTION_CONNECT_RETRIES %d\n", retries);
2347 break;
2349 case INTERNET_OPTION_CONTEXT_VALUE:
2350 FIXME("Option INTERNET_OPTION_CONTEXT_VALUE; STUB\n");
2351 break;
2352 case INTERNET_OPTION_SECURITY_FLAGS:
2353 FIXME("Option INTERNET_OPTION_SECURITY_FLAGS; STUB\n");
2354 break;
2355 case INTERNET_OPTION_DISABLE_AUTODIAL:
2356 FIXME("Option INTERNET_OPTION_DISABLE_AUTODIAL; STUB\n");
2357 break;
2358 case 86:
2359 FIXME("86\n");
2360 break;
2361 default:
2362 FIXME("Option %d STUB\n",dwOption);
2363 INTERNET_SetLastError(ERROR_INTERNET_INVALID_OPTION);
2364 ret = FALSE;
2365 break;
2368 if(lpwhh)
2369 WININET_Release( lpwhh );
2371 return ret;
2375 /***********************************************************************
2376 * InternetSetOptionA (WININET.@)
2378 * Sets an options on the specified handle.
2380 * RETURNS
2381 * TRUE on success
2382 * FALSE on failure
2385 BOOL WINAPI InternetSetOptionA(HINTERNET hInternet, DWORD dwOption,
2386 LPVOID lpBuffer, DWORD dwBufferLength)
2388 LPVOID wbuffer;
2389 DWORD wlen;
2390 BOOL r;
2392 switch( dwOption )
2394 case INTERNET_OPTION_CALLBACK:
2396 LPWININETHANDLEHEADER lpwh;
2397 INTERNET_STATUS_CALLBACK callback = *(INTERNET_STATUS_CALLBACK *)lpBuffer;
2399 if (!(lpwh = WININET_GetObject(hInternet))) return FALSE;
2400 r = (set_status_callback(lpwh, callback, FALSE) != INTERNET_INVALID_STATUS_CALLBACK);
2401 WININET_Release(lpwh);
2402 return r;
2404 case INTERNET_OPTION_PROXY:
2406 LPINTERNET_PROXY_INFOA pi = (LPINTERNET_PROXY_INFOA) lpBuffer;
2407 LPINTERNET_PROXY_INFOW piw;
2408 DWORD proxlen, prbylen;
2409 LPWSTR prox, prby;
2411 proxlen = MultiByteToWideChar( CP_ACP, 0, pi->lpszProxy, -1, NULL, 0);
2412 prbylen= MultiByteToWideChar( CP_ACP, 0, pi->lpszProxyBypass, -1, NULL, 0);
2413 wlen = sizeof(*piw) + proxlen + prbylen;
2414 wbuffer = HeapAlloc( GetProcessHeap(), 0, wlen*sizeof(WCHAR) );
2415 piw = (LPINTERNET_PROXY_INFOW) wbuffer;
2416 piw->dwAccessType = pi->dwAccessType;
2417 prox = (LPWSTR) &piw[1];
2418 prby = &prox[proxlen+1];
2419 MultiByteToWideChar( CP_ACP, 0, pi->lpszProxy, -1, prox, proxlen);
2420 MultiByteToWideChar( CP_ACP, 0, pi->lpszProxyBypass, -1, prby, prbylen);
2421 piw->lpszProxy = prox;
2422 piw->lpszProxyBypass = prby;
2424 break;
2425 case INTERNET_OPTION_USER_AGENT:
2426 case INTERNET_OPTION_USERNAME:
2427 case INTERNET_OPTION_PASSWORD:
2428 wlen = MultiByteToWideChar( CP_ACP, 0, lpBuffer, dwBufferLength,
2429 NULL, 0 );
2430 wbuffer = HeapAlloc( GetProcessHeap(), 0, wlen*sizeof(WCHAR) );
2431 MultiByteToWideChar( CP_ACP, 0, lpBuffer, dwBufferLength,
2432 wbuffer, wlen );
2433 break;
2434 default:
2435 wbuffer = lpBuffer;
2436 wlen = dwBufferLength;
2439 r = InternetSetOptionW(hInternet,dwOption, wbuffer, wlen);
2441 if( lpBuffer != wbuffer )
2442 HeapFree( GetProcessHeap(), 0, wbuffer );
2444 return r;
2448 /***********************************************************************
2449 * InternetSetOptionExA (WININET.@)
2451 BOOL WINAPI InternetSetOptionExA(HINTERNET hInternet, DWORD dwOption,
2452 LPVOID lpBuffer, DWORD dwBufferLength, DWORD dwFlags)
2454 FIXME("Flags %08x ignored\n", dwFlags);
2455 return InternetSetOptionA( hInternet, dwOption, lpBuffer, dwBufferLength );
2458 /***********************************************************************
2459 * InternetSetOptionExW (WININET.@)
2461 BOOL WINAPI InternetSetOptionExW(HINTERNET hInternet, DWORD dwOption,
2462 LPVOID lpBuffer, DWORD dwBufferLength, DWORD dwFlags)
2464 FIXME("Flags %08x ignored\n", dwFlags);
2465 if( dwFlags & ~ISO_VALID_FLAGS )
2467 INTERNET_SetLastError( ERROR_INVALID_PARAMETER );
2468 return FALSE;
2470 return InternetSetOptionW( hInternet, dwOption, lpBuffer, dwBufferLength );
2473 static const WCHAR WININET_wkday[7][4] =
2474 { { 'S','u','n', 0 }, { 'M','o','n', 0 }, { 'T','u','e', 0 }, { 'W','e','d', 0 },
2475 { 'T','h','u', 0 }, { 'F','r','i', 0 }, { 'S','a','t', 0 } };
2476 static const WCHAR WININET_month[12][4] =
2477 { { 'J','a','n', 0 }, { 'F','e','b', 0 }, { 'M','a','r', 0 }, { 'A','p','r', 0 },
2478 { 'M','a','y', 0 }, { 'J','u','n', 0 }, { 'J','u','l', 0 }, { 'A','u','g', 0 },
2479 { 'S','e','p', 0 }, { 'O','c','t', 0 }, { 'N','o','v', 0 }, { 'D','e','c', 0 } };
2481 /***********************************************************************
2482 * InternetTimeFromSystemTimeA (WININET.@)
2484 BOOL WINAPI InternetTimeFromSystemTimeA( const SYSTEMTIME* time, DWORD format, LPSTR string, DWORD size )
2486 BOOL ret;
2487 WCHAR stringW[INTERNET_RFC1123_BUFSIZE];
2489 TRACE( "%p 0x%08x %p 0x%08x\n", time, format, string, size );
2491 ret = InternetTimeFromSystemTimeW( time, format, stringW, sizeof(stringW) );
2492 if (ret) WideCharToMultiByte( CP_ACP, 0, stringW, -1, string, size, NULL, NULL );
2494 return ret;
2497 /***********************************************************************
2498 * InternetTimeFromSystemTimeW (WININET.@)
2500 BOOL WINAPI InternetTimeFromSystemTimeW( const SYSTEMTIME* time, DWORD format, LPWSTR string, DWORD size )
2502 static const WCHAR date[] =
2503 { '%','s',',',' ','%','0','2','d',' ','%','s',' ','%','4','d',' ','%','0',
2504 '2','d',':','%','0','2','d',':','%','0','2','d',' ','G','M','T', 0 };
2506 TRACE( "%p 0x%08x %p 0x%08x\n", time, format, string, size );
2508 if (!time || !string) return FALSE;
2510 if (format != INTERNET_RFC1123_FORMAT || size < INTERNET_RFC1123_BUFSIZE * sizeof(WCHAR))
2511 return FALSE;
2513 sprintfW( string, date,
2514 WININET_wkday[time->wDayOfWeek],
2515 time->wDay,
2516 WININET_month[time->wMonth - 1],
2517 time->wYear,
2518 time->wHour,
2519 time->wMinute,
2520 time->wSecond );
2522 return TRUE;
2525 /***********************************************************************
2526 * InternetTimeToSystemTimeA (WININET.@)
2528 BOOL WINAPI InternetTimeToSystemTimeA( LPCSTR string, SYSTEMTIME* time, DWORD reserved )
2530 BOOL ret = FALSE;
2531 WCHAR *stringW;
2532 int len;
2534 TRACE( "%s %p 0x%08x\n", debugstr_a(string), time, reserved );
2536 len = MultiByteToWideChar( CP_ACP, 0, string, -1, NULL, 0 );
2537 stringW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
2539 if (stringW)
2541 MultiByteToWideChar( CP_ACP, 0, string, -1, stringW, len );
2542 ret = InternetTimeToSystemTimeW( stringW, time, reserved );
2543 HeapFree( GetProcessHeap(), 0, stringW );
2545 return ret;
2548 /***********************************************************************
2549 * InternetTimeToSystemTimeW (WININET.@)
2551 BOOL WINAPI InternetTimeToSystemTimeW( LPCWSTR string, SYSTEMTIME* time, DWORD reserved )
2553 unsigned int i;
2554 const WCHAR *s = string;
2555 WCHAR *end;
2557 TRACE( "%s %p 0x%08x\n", debugstr_w(string), time, reserved );
2559 if (!string || !time) return FALSE;
2561 /* Windows does this too */
2562 GetSystemTime( time );
2564 /* Convert an RFC1123 time such as 'Fri, 07 Jan 2005 12:06:35 GMT' into
2565 * a SYSTEMTIME structure.
2568 while (*s && !isalphaW( *s )) s++;
2569 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
2570 time->wDayOfWeek = 7;
2572 for (i = 0; i < 7; i++)
2574 if (toupperW( WININET_wkday[i][0] ) == toupperW( s[0] ) &&
2575 toupperW( WININET_wkday[i][1] ) == toupperW( s[1] ) &&
2576 toupperW( WININET_wkday[i][2] ) == toupperW( s[2] ) )
2578 time->wDayOfWeek = i;
2579 break;
2583 if (time->wDayOfWeek > 6) return TRUE;
2584 while (*s && !isdigitW( *s )) s++;
2585 time->wDay = strtolW( s, &end, 10 );
2586 s = end;
2588 while (*s && !isalphaW( *s )) s++;
2589 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
2590 time->wMonth = 0;
2592 for (i = 0; i < 12; i++)
2594 if (toupperW( WININET_month[i][0]) == toupperW( s[0] ) &&
2595 toupperW( WININET_month[i][1]) == toupperW( s[1] ) &&
2596 toupperW( WININET_month[i][2]) == toupperW( s[2] ) )
2598 time->wMonth = i + 1;
2599 break;
2602 if (time->wMonth == 0) return TRUE;
2604 while (*s && !isdigitW( *s )) s++;
2605 if (*s == '\0') return TRUE;
2606 time->wYear = strtolW( s, &end, 10 );
2607 s = end;
2609 while (*s && !isdigitW( *s )) s++;
2610 if (*s == '\0') return TRUE;
2611 time->wHour = strtolW( s, &end, 10 );
2612 s = end;
2614 while (*s && !isdigitW( *s )) s++;
2615 if (*s == '\0') return TRUE;
2616 time->wMinute = strtolW( s, &end, 10 );
2617 s = end;
2619 while (*s && !isdigitW( *s )) s++;
2620 if (*s == '\0') return TRUE;
2621 time->wSecond = strtolW( s, &end, 10 );
2622 s = end;
2624 time->wMilliseconds = 0;
2625 return TRUE;
2628 /***********************************************************************
2629 * InternetCheckConnectionW (WININET.@)
2631 * Pings a requested host to check internet connection
2633 * RETURNS
2634 * TRUE on success and FALSE on failure. If a failure then
2635 * ERROR_NOT_CONNECTED is placed into GetLastError
2638 BOOL WINAPI InternetCheckConnectionW( LPCWSTR lpszUrl, DWORD dwFlags, DWORD dwReserved )
2641 * this is a kludge which runs the resident ping program and reads the output.
2643 * Anyone have a better idea?
2646 BOOL rc = FALSE;
2647 static const CHAR ping[] = "ping -c 1 ";
2648 static const CHAR redirect[] = " >/dev/null 2>/dev/null";
2649 CHAR *command = NULL;
2650 WCHAR hostW[1024];
2651 DWORD len;
2652 INTERNET_PORT port;
2653 int status = -1;
2655 FIXME("\n");
2658 * Crack or set the Address
2660 if (lpszUrl == NULL)
2663 * According to the doc we are supposed to use the ip for the next
2664 * server in the WnInet internal server database. I have
2665 * no idea what that is or how to get it.
2667 * So someone needs to implement this.
2669 FIXME("Unimplemented with URL of NULL\n");
2670 return TRUE;
2672 else
2674 URL_COMPONENTSW components;
2676 ZeroMemory(&components,sizeof(URL_COMPONENTSW));
2677 components.lpszHostName = (LPWSTR)hostW;
2678 components.dwHostNameLength = 1024;
2680 if (!InternetCrackUrlW(lpszUrl,0,0,&components))
2681 goto End;
2683 TRACE("host name : %s\n",debugstr_w(components.lpszHostName));
2684 port = components.nPort;
2685 TRACE("port: %d\n", port);
2688 if (dwFlags & FLAG_ICC_FORCE_CONNECTION)
2690 struct sockaddr_in sin;
2691 int fd;
2693 if (!GetAddress(hostW, port, &sin))
2694 goto End;
2695 fd = socket(sin.sin_family, SOCK_STREAM, 0);
2696 if (fd != -1)
2698 if (connect(fd, (struct sockaddr *)&sin, sizeof(sin)) == 0)
2699 rc = TRUE;
2700 close(fd);
2703 else
2706 * Build our ping command
2708 len = WideCharToMultiByte(CP_UNIXCP, 0, hostW, -1, NULL, 0, NULL, NULL);
2709 command = HeapAlloc( GetProcessHeap(), 0, strlen(ping)+len+strlen(redirect) );
2710 strcpy(command,ping);
2711 WideCharToMultiByte(CP_UNIXCP, 0, hostW, -1, command+strlen(ping), len, NULL, NULL);
2712 strcat(command,redirect);
2714 TRACE("Ping command is : %s\n",command);
2716 status = system(command);
2718 TRACE("Ping returned a code of %i\n",status);
2720 /* Ping return code of 0 indicates success */
2721 if (status == 0)
2722 rc = TRUE;
2725 End:
2727 HeapFree( GetProcessHeap(), 0, command );
2728 if (rc == FALSE)
2729 INTERNET_SetLastError(ERROR_NOT_CONNECTED);
2731 return rc;
2735 /***********************************************************************
2736 * InternetCheckConnectionA (WININET.@)
2738 * Pings a requested host to check internet connection
2740 * RETURNS
2741 * TRUE on success and FALSE on failure. If a failure then
2742 * ERROR_NOT_CONNECTED is placed into GetLastError
2745 BOOL WINAPI InternetCheckConnectionA(LPCSTR lpszUrl, DWORD dwFlags, DWORD dwReserved)
2747 WCHAR *szUrl;
2748 INT len;
2749 BOOL rc;
2751 len = MultiByteToWideChar(CP_ACP, 0, lpszUrl, -1, NULL, 0);
2752 if (!(szUrl = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR))))
2753 return FALSE;
2754 MultiByteToWideChar(CP_ACP, 0, lpszUrl, -1, szUrl, len);
2755 rc = InternetCheckConnectionW(szUrl, dwFlags, dwReserved);
2756 HeapFree(GetProcessHeap(), 0, szUrl);
2758 return rc;
2762 /**********************************************************
2763 * INTERNET_InternetOpenUrlW (internal)
2765 * Opens an URL
2767 * RETURNS
2768 * handle of connection or NULL on failure
2770 static HINTERNET INTERNET_InternetOpenUrlW(LPWININETAPPINFOW hIC, LPCWSTR lpszUrl,
2771 LPCWSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext)
2773 URL_COMPONENTSW urlComponents;
2774 WCHAR protocol[32], hostName[MAXHOSTNAME], userName[1024];
2775 WCHAR password[1024], path[2048], extra[1024];
2776 HINTERNET client = NULL, client1 = NULL;
2778 TRACE("(%p, %s, %s, %08x, %08x, %08lx)\n", hIC, debugstr_w(lpszUrl), debugstr_w(lpszHeaders),
2779 dwHeadersLength, dwFlags, dwContext);
2781 urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
2782 urlComponents.lpszScheme = protocol;
2783 urlComponents.dwSchemeLength = 32;
2784 urlComponents.lpszHostName = hostName;
2785 urlComponents.dwHostNameLength = MAXHOSTNAME;
2786 urlComponents.lpszUserName = userName;
2787 urlComponents.dwUserNameLength = 1024;
2788 urlComponents.lpszPassword = password;
2789 urlComponents.dwPasswordLength = 1024;
2790 urlComponents.lpszUrlPath = path;
2791 urlComponents.dwUrlPathLength = 2048;
2792 urlComponents.lpszExtraInfo = extra;
2793 urlComponents.dwExtraInfoLength = 1024;
2794 if(!InternetCrackUrlW(lpszUrl, strlenW(lpszUrl), 0, &urlComponents))
2795 return NULL;
2796 switch(urlComponents.nScheme) {
2797 case INTERNET_SCHEME_FTP:
2798 if(urlComponents.nPort == 0)
2799 urlComponents.nPort = INTERNET_DEFAULT_FTP_PORT;
2800 client = FTP_Connect(hIC, hostName, urlComponents.nPort,
2801 userName, password, dwFlags, dwContext, INET_OPENURL);
2802 if(client == NULL)
2803 break;
2804 client1 = FtpOpenFileW(client, path, GENERIC_READ, dwFlags, dwContext);
2805 if(client1 == NULL) {
2806 InternetCloseHandle(client);
2807 break;
2809 break;
2811 case INTERNET_SCHEME_HTTP:
2812 case INTERNET_SCHEME_HTTPS: {
2813 static const WCHAR szStars[] = { '*','/','*', 0 };
2814 LPCWSTR accept[2] = { szStars, NULL };
2815 if(urlComponents.nPort == 0) {
2816 if(urlComponents.nScheme == INTERNET_SCHEME_HTTP)
2817 urlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
2818 else
2819 urlComponents.nPort = INTERNET_DEFAULT_HTTPS_PORT;
2821 /* FIXME: should use pointers, not handles, as handles are not thread-safe */
2822 client = HTTP_Connect(hIC, hostName, urlComponents.nPort,
2823 userName, password, dwFlags, dwContext, INET_OPENURL);
2824 if(client == NULL)
2825 break;
2827 if (urlComponents.dwExtraInfoLength) {
2828 WCHAR *path_extra;
2829 DWORD len = urlComponents.dwUrlPathLength + urlComponents.dwExtraInfoLength + 1;
2831 if (!(path_extra = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR))))
2833 InternetCloseHandle(client);
2834 break;
2836 strcpyW(path_extra, urlComponents.lpszUrlPath);
2837 strcatW(path_extra, urlComponents.lpszExtraInfo);
2838 client1 = HttpOpenRequestW(client, NULL, path_extra, NULL, NULL, accept, dwFlags, dwContext);
2839 HeapFree(GetProcessHeap(), 0, path_extra);
2841 else
2842 client1 = HttpOpenRequestW(client, NULL, path, NULL, NULL, accept, dwFlags, dwContext);
2844 if(client1 == NULL) {
2845 InternetCloseHandle(client);
2846 break;
2848 HttpAddRequestHeadersW(client1, lpszHeaders, dwHeadersLength, HTTP_ADDREQ_FLAG_ADD);
2849 if (!HttpSendRequestW(client1, NULL, 0, NULL, 0) &&
2850 GetLastError() != ERROR_IO_PENDING) {
2851 InternetCloseHandle(client1);
2852 client1 = NULL;
2853 break;
2856 case INTERNET_SCHEME_GOPHER:
2857 /* gopher doesn't seem to be implemented in wine, but it's supposed
2858 * to be supported by InternetOpenUrlA. */
2859 default:
2860 INTERNET_SetLastError(ERROR_INTERNET_UNRECOGNIZED_SCHEME);
2861 break;
2864 TRACE(" %p <--\n", client1);
2866 return client1;
2869 /**********************************************************
2870 * InternetOpenUrlW (WININET.@)
2872 * Opens an URL
2874 * RETURNS
2875 * handle of connection or NULL on failure
2877 static void AsyncInternetOpenUrlProc(WORKREQUEST *workRequest)
2879 struct WORKREQ_INTERNETOPENURLW const *req = &workRequest->u.InternetOpenUrlW;
2880 LPWININETAPPINFOW hIC = (LPWININETAPPINFOW) workRequest->hdr;
2882 TRACE("%p\n", hIC);
2884 INTERNET_InternetOpenUrlW(hIC, req->lpszUrl,
2885 req->lpszHeaders, req->dwHeadersLength, req->dwFlags, req->dwContext);
2886 HeapFree(GetProcessHeap(), 0, req->lpszUrl);
2887 HeapFree(GetProcessHeap(), 0, req->lpszHeaders);
2890 HINTERNET WINAPI InternetOpenUrlW(HINTERNET hInternet, LPCWSTR lpszUrl,
2891 LPCWSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext)
2893 HINTERNET ret = NULL;
2894 LPWININETAPPINFOW hIC = NULL;
2896 if (TRACE_ON(wininet)) {
2897 TRACE("(%p, %s, %s, %08x, %08x, %08lx)\n", hInternet, debugstr_w(lpszUrl), debugstr_w(lpszHeaders),
2898 dwHeadersLength, dwFlags, dwContext);
2899 TRACE(" flags :");
2900 dump_INTERNET_FLAGS(dwFlags);
2903 if (!lpszUrl)
2905 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
2906 goto lend;
2909 hIC = (LPWININETAPPINFOW) WININET_GetObject( hInternet );
2910 if (NULL == hIC || hIC->hdr.htype != WH_HINIT) {
2911 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2912 goto lend;
2915 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC) {
2916 WORKREQUEST workRequest;
2917 struct WORKREQ_INTERNETOPENURLW *req;
2919 workRequest.asyncproc = AsyncInternetOpenUrlProc;
2920 workRequest.hdr = WININET_AddRef( &hIC->hdr );
2921 req = &workRequest.u.InternetOpenUrlW;
2922 req->lpszUrl = WININET_strdupW(lpszUrl);
2923 if (lpszHeaders)
2924 req->lpszHeaders = WININET_strdupW(lpszHeaders);
2925 else
2926 req->lpszHeaders = 0;
2927 req->dwHeadersLength = dwHeadersLength;
2928 req->dwFlags = dwFlags;
2929 req->dwContext = dwContext;
2931 INTERNET_AsyncCall(&workRequest);
2933 * This is from windows.
2935 INTERNET_SetLastError(ERROR_IO_PENDING);
2936 } else {
2937 ret = INTERNET_InternetOpenUrlW(hIC, lpszUrl, lpszHeaders, dwHeadersLength, dwFlags, dwContext);
2940 lend:
2941 if( hIC )
2942 WININET_Release( &hIC->hdr );
2943 TRACE(" %p <--\n", ret);
2945 return ret;
2948 /**********************************************************
2949 * InternetOpenUrlA (WININET.@)
2951 * Opens an URL
2953 * RETURNS
2954 * handle of connection or NULL on failure
2956 HINTERNET WINAPI InternetOpenUrlA(HINTERNET hInternet, LPCSTR lpszUrl,
2957 LPCSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext)
2959 HINTERNET rc = NULL;
2961 INT lenUrl;
2962 INT lenHeaders = 0;
2963 LPWSTR szUrl = NULL;
2964 LPWSTR szHeaders = NULL;
2966 TRACE("\n");
2968 if(lpszUrl) {
2969 lenUrl = MultiByteToWideChar(CP_ACP, 0, lpszUrl, -1, NULL, 0 );
2970 szUrl = HeapAlloc(GetProcessHeap(), 0, lenUrl*sizeof(WCHAR));
2971 if(!szUrl)
2972 return NULL;
2973 MultiByteToWideChar(CP_ACP, 0, lpszUrl, -1, szUrl, lenUrl);
2976 if(lpszHeaders) {
2977 lenHeaders = MultiByteToWideChar(CP_ACP, 0, lpszHeaders, dwHeadersLength, NULL, 0 );
2978 szHeaders = HeapAlloc(GetProcessHeap(), 0, lenHeaders*sizeof(WCHAR));
2979 if(!szHeaders) {
2980 HeapFree(GetProcessHeap(), 0, szUrl);
2981 return NULL;
2983 MultiByteToWideChar(CP_ACP, 0, lpszHeaders, dwHeadersLength, szHeaders, lenHeaders);
2986 rc = InternetOpenUrlW(hInternet, szUrl, szHeaders,
2987 lenHeaders, dwFlags, dwContext);
2989 HeapFree(GetProcessHeap(), 0, szUrl);
2990 HeapFree(GetProcessHeap(), 0, szHeaders);
2992 return rc;
2996 static LPWITHREADERROR INTERNET_AllocThreadError(void)
2998 LPWITHREADERROR lpwite = HeapAlloc(GetProcessHeap(), 0, sizeof(*lpwite));
3000 if (lpwite)
3002 lpwite->dwError = 0;
3003 lpwite->response[0] = '\0';
3006 if (!TlsSetValue(g_dwTlsErrIndex, lpwite))
3008 HeapFree(GetProcessHeap(), 0, lpwite);
3009 return NULL;
3012 return lpwite;
3016 /***********************************************************************
3017 * INTERNET_SetLastError (internal)
3019 * Set last thread specific error
3021 * RETURNS
3024 void INTERNET_SetLastError(DWORD dwError)
3026 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
3028 if (!lpwite)
3029 lpwite = INTERNET_AllocThreadError();
3031 SetLastError(dwError);
3032 if(lpwite)
3033 lpwite->dwError = dwError;
3037 /***********************************************************************
3038 * INTERNET_GetLastError (internal)
3040 * Get last thread specific error
3042 * RETURNS
3045 DWORD INTERNET_GetLastError(void)
3047 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
3048 if (!lpwite) return 0;
3049 /* TlsGetValue clears last error, so set it again here */
3050 SetLastError(lpwite->dwError);
3051 return lpwite->dwError;
3055 /***********************************************************************
3056 * INTERNET_WorkerThreadFunc (internal)
3058 * Worker thread execution function
3060 * RETURNS
3063 static DWORD CALLBACK INTERNET_WorkerThreadFunc(LPVOID lpvParam)
3065 LPWORKREQUEST lpRequest = lpvParam;
3066 WORKREQUEST workRequest;
3068 TRACE("\n");
3070 workRequest = *lpRequest;
3071 HeapFree(GetProcessHeap(), 0, lpRequest);
3073 workRequest.asyncproc(&workRequest);
3075 WININET_Release( workRequest.hdr );
3076 return TRUE;
3080 /***********************************************************************
3081 * INTERNET_AsyncCall (internal)
3083 * Retrieves work request from queue
3085 * RETURNS
3088 BOOL INTERNET_AsyncCall(LPWORKREQUEST lpWorkRequest)
3090 BOOL bSuccess;
3091 LPWORKREQUEST lpNewRequest;
3093 TRACE("\n");
3095 lpNewRequest = HeapAlloc(GetProcessHeap(), 0, sizeof(WORKREQUEST));
3096 if (!lpNewRequest)
3097 return FALSE;
3099 *lpNewRequest = *lpWorkRequest;
3101 bSuccess = QueueUserWorkItem(INTERNET_WorkerThreadFunc, lpNewRequest, WT_EXECUTELONGFUNCTION);
3102 if (!bSuccess)
3104 HeapFree(GetProcessHeap(), 0, lpNewRequest);
3105 INTERNET_SetLastError(ERROR_INTERNET_ASYNC_THREAD_FAILED);
3108 return bSuccess;
3112 /***********************************************************************
3113 * INTERNET_GetResponseBuffer (internal)
3115 * RETURNS
3118 LPSTR INTERNET_GetResponseBuffer(void)
3120 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
3121 if (!lpwite)
3122 lpwite = INTERNET_AllocThreadError();
3123 TRACE("\n");
3124 return lpwite->response;
3127 /***********************************************************************
3128 * INTERNET_GetNextLine (internal)
3130 * Parse next line in directory string listing
3132 * RETURNS
3133 * Pointer to beginning of next line
3134 * NULL on failure
3138 LPSTR INTERNET_GetNextLine(INT nSocket, LPDWORD dwLen)
3140 struct pollfd pfd;
3141 BOOL bSuccess = FALSE;
3142 INT nRecv = 0;
3143 LPSTR lpszBuffer = INTERNET_GetResponseBuffer();
3145 TRACE("\n");
3147 pfd.fd = nSocket;
3148 pfd.events = POLLIN;
3150 while (nRecv < MAX_REPLY_LEN)
3152 if (poll(&pfd,1, RESPONSE_TIMEOUT * 1000) > 0)
3154 if (recv(nSocket, &lpszBuffer[nRecv], 1, 0) <= 0)
3156 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
3157 goto lend;
3160 if (lpszBuffer[nRecv] == '\n')
3162 bSuccess = TRUE;
3163 break;
3165 if (lpszBuffer[nRecv] != '\r')
3166 nRecv++;
3168 else
3170 INTERNET_SetLastError(ERROR_INTERNET_TIMEOUT);
3171 goto lend;
3175 lend:
3176 if (bSuccess)
3178 lpszBuffer[nRecv] = '\0';
3179 *dwLen = nRecv - 1;
3180 TRACE(":%d %s\n", nRecv, lpszBuffer);
3181 return lpszBuffer;
3183 else
3185 return NULL;
3189 /**********************************************************
3190 * InternetQueryDataAvailable (WININET.@)
3192 * Determines how much data is available to be read.
3194 * RETURNS
3195 * TRUE on success, FALSE if an error occurred. If
3196 * INTERNET_FLAG_ASYNC was specified in InternetOpen, and
3197 * no data is presently available, FALSE is returned with
3198 * the last error ERROR_IO_PENDING; a callback with status
3199 * INTERNET_STATUS_REQUEST_COMPLETE will be sent when more
3200 * data is available.
3202 BOOL WINAPI InternetQueryDataAvailable( HINTERNET hFile,
3203 LPDWORD lpdwNumberOfBytesAvailble,
3204 DWORD dwFlags, DWORD_PTR dwContext)
3206 WININETHANDLEHEADER *hdr;
3207 DWORD res;
3209 TRACE("(%p %p %x %lx)\n", hFile, lpdwNumberOfBytesAvailble, dwFlags, dwContext);
3211 hdr = WININET_GetObject( hFile );
3212 if (!hdr) {
3213 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
3214 return FALSE;
3217 if(hdr->vtbl->QueryDataAvailable) {
3218 res = hdr->vtbl->QueryDataAvailable(hdr, lpdwNumberOfBytesAvailble, dwFlags, dwContext);
3219 }else {
3220 WARN("wrong handle\n");
3221 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
3224 WININET_Release(hdr);
3226 if(res != ERROR_SUCCESS)
3227 SetLastError(res);
3228 return res == ERROR_SUCCESS;
3232 /***********************************************************************
3233 * InternetLockRequestFile (WININET.@)
3235 BOOL WINAPI InternetLockRequestFile( HINTERNET hInternet, HANDLE
3236 *lphLockReqHandle)
3238 FIXME("STUB\n");
3239 return FALSE;
3242 BOOL WINAPI InternetUnlockRequestFile( HANDLE hLockHandle)
3244 FIXME("STUB\n");
3245 return FALSE;
3249 /***********************************************************************
3250 * InternetAutodial (WININET.@)
3252 * On windows this function is supposed to dial the default internet
3253 * connection. We don't want to have Wine dial out to the internet so
3254 * we return TRUE by default. It might be nice to check if we are connected.
3256 * RETURNS
3257 * TRUE on success
3258 * FALSE on failure
3261 BOOL WINAPI InternetAutodial(DWORD dwFlags, HWND hwndParent)
3263 FIXME("STUB\n");
3265 /* Tell that we are connected to the internet. */
3266 return TRUE;
3269 /***********************************************************************
3270 * InternetAutodialHangup (WININET.@)
3272 * Hangs up a connection made with InternetAutodial
3274 * PARAM
3275 * dwReserved
3276 * RETURNS
3277 * TRUE on success
3278 * FALSE on failure
3281 BOOL WINAPI InternetAutodialHangup(DWORD dwReserved)
3283 FIXME("STUB\n");
3285 /* we didn't dial, we don't disconnect */
3286 return TRUE;
3289 /***********************************************************************
3290 * InternetCombineUrlA (WININET.@)
3292 * Combine a base URL with a relative URL
3294 * RETURNS
3295 * TRUE on success
3296 * FALSE on failure
3300 BOOL WINAPI InternetCombineUrlA(LPCSTR lpszBaseUrl, LPCSTR lpszRelativeUrl,
3301 LPSTR lpszBuffer, LPDWORD lpdwBufferLength,
3302 DWORD dwFlags)
3304 HRESULT hr=S_OK;
3306 TRACE("(%s, %s, %p, %p, 0x%08x)\n", debugstr_a(lpszBaseUrl), debugstr_a(lpszRelativeUrl), lpszBuffer, lpdwBufferLength, dwFlags);
3308 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
3309 dwFlags ^= ICU_NO_ENCODE;
3310 hr=UrlCombineA(lpszBaseUrl,lpszRelativeUrl,lpszBuffer,lpdwBufferLength,dwFlags);
3312 return (hr==S_OK);
3315 /***********************************************************************
3316 * InternetCombineUrlW (WININET.@)
3318 * Combine a base URL with a relative URL
3320 * RETURNS
3321 * TRUE on success
3322 * FALSE on failure
3326 BOOL WINAPI InternetCombineUrlW(LPCWSTR lpszBaseUrl, LPCWSTR lpszRelativeUrl,
3327 LPWSTR lpszBuffer, LPDWORD lpdwBufferLength,
3328 DWORD dwFlags)
3330 HRESULT hr=S_OK;
3332 TRACE("(%s, %s, %p, %p, 0x%08x)\n", debugstr_w(lpszBaseUrl), debugstr_w(lpszRelativeUrl), lpszBuffer, lpdwBufferLength, dwFlags);
3334 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
3335 dwFlags ^= ICU_NO_ENCODE;
3336 hr=UrlCombineW(lpszBaseUrl,lpszRelativeUrl,lpszBuffer,lpdwBufferLength,dwFlags);
3338 return (hr==S_OK);
3341 /* max port num is 65535 => 5 digits */
3342 #define MAX_WORD_DIGITS 5
3344 #define URL_GET_COMP_LENGTH(url, component) ((url)->dw##component##Length ? \
3345 (url)->dw##component##Length : strlenW((url)->lpsz##component))
3346 #define URL_GET_COMP_LENGTHA(url, component) ((url)->dw##component##Length ? \
3347 (url)->dw##component##Length : strlen((url)->lpsz##component))
3349 static BOOL url_uses_default_port(INTERNET_SCHEME nScheme, INTERNET_PORT nPort)
3351 if ((nScheme == INTERNET_SCHEME_HTTP) &&
3352 (nPort == INTERNET_DEFAULT_HTTP_PORT))
3353 return TRUE;
3354 if ((nScheme == INTERNET_SCHEME_HTTPS) &&
3355 (nPort == INTERNET_DEFAULT_HTTPS_PORT))
3356 return TRUE;
3357 if ((nScheme == INTERNET_SCHEME_FTP) &&
3358 (nPort == INTERNET_DEFAULT_FTP_PORT))
3359 return TRUE;
3360 if ((nScheme == INTERNET_SCHEME_GOPHER) &&
3361 (nPort == INTERNET_DEFAULT_GOPHER_PORT))
3362 return TRUE;
3364 if (nPort == INTERNET_INVALID_PORT_NUMBER)
3365 return TRUE;
3367 return FALSE;
3370 /* opaque urls do not fit into the standard url hierarchy and don't have
3371 * two following slashes */
3372 static inline BOOL scheme_is_opaque(INTERNET_SCHEME nScheme)
3374 return (nScheme != INTERNET_SCHEME_FTP) &&
3375 (nScheme != INTERNET_SCHEME_GOPHER) &&
3376 (nScheme != INTERNET_SCHEME_HTTP) &&
3377 (nScheme != INTERNET_SCHEME_HTTPS) &&
3378 (nScheme != INTERNET_SCHEME_FILE);
3381 static LPCWSTR INTERNET_GetSchemeString(INTERNET_SCHEME scheme)
3383 int index;
3384 if (scheme < INTERNET_SCHEME_FIRST)
3385 return NULL;
3386 index = scheme - INTERNET_SCHEME_FIRST;
3387 if (index >= sizeof(url_schemes)/sizeof(url_schemes[0]))
3388 return NULL;
3389 return (LPCWSTR)url_schemes[index];
3392 /* we can calculate using ansi strings because we're just
3393 * calculating string length, not size
3395 static BOOL calc_url_length(LPURL_COMPONENTSW lpUrlComponents,
3396 LPDWORD lpdwUrlLength)
3398 INTERNET_SCHEME nScheme;
3400 *lpdwUrlLength = 0;
3402 if (lpUrlComponents->lpszScheme)
3404 DWORD dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, Scheme);
3405 *lpdwUrlLength += dwLen;
3406 nScheme = GetInternetSchemeW(lpUrlComponents->lpszScheme, dwLen);
3408 else
3410 LPCWSTR scheme;
3412 nScheme = lpUrlComponents->nScheme;
3414 if (nScheme == INTERNET_SCHEME_DEFAULT)
3415 nScheme = INTERNET_SCHEME_HTTP;
3416 scheme = INTERNET_GetSchemeString(nScheme);
3417 *lpdwUrlLength += strlenW(scheme);
3420 (*lpdwUrlLength)++; /* ':' */
3421 if (!scheme_is_opaque(nScheme) || lpUrlComponents->lpszHostName)
3422 *lpdwUrlLength += strlen("//");
3424 if (lpUrlComponents->lpszUserName)
3426 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, UserName);
3427 *lpdwUrlLength += strlen("@");
3429 else
3431 if (lpUrlComponents->lpszPassword)
3433 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
3434 return FALSE;
3438 if (lpUrlComponents->lpszPassword)
3440 *lpdwUrlLength += strlen(":");
3441 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, Password);
3444 if (lpUrlComponents->lpszHostName)
3446 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, HostName);
3448 if (!url_uses_default_port(nScheme, lpUrlComponents->nPort))
3450 char szPort[MAX_WORD_DIGITS+1];
3452 sprintf(szPort, "%d", lpUrlComponents->nPort);
3453 *lpdwUrlLength += strlen(szPort);
3454 *lpdwUrlLength += strlen(":");
3457 if (lpUrlComponents->lpszUrlPath && *lpUrlComponents->lpszUrlPath != '/')
3458 (*lpdwUrlLength)++; /* '/' */
3461 if (lpUrlComponents->lpszUrlPath)
3462 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, UrlPath);
3464 return TRUE;
3467 static void convert_urlcomp_atow(LPURL_COMPONENTSA lpUrlComponents, LPURL_COMPONENTSW urlCompW)
3469 INT len;
3471 ZeroMemory(urlCompW, sizeof(URL_COMPONENTSW));
3473 urlCompW->dwStructSize = sizeof(URL_COMPONENTSW);
3474 urlCompW->dwSchemeLength = lpUrlComponents->dwSchemeLength;
3475 urlCompW->nScheme = lpUrlComponents->nScheme;
3476 urlCompW->dwHostNameLength = lpUrlComponents->dwHostNameLength;
3477 urlCompW->nPort = lpUrlComponents->nPort;
3478 urlCompW->dwUserNameLength = lpUrlComponents->dwUserNameLength;
3479 urlCompW->dwPasswordLength = lpUrlComponents->dwPasswordLength;
3480 urlCompW->dwUrlPathLength = lpUrlComponents->dwUrlPathLength;
3481 urlCompW->dwExtraInfoLength = lpUrlComponents->dwExtraInfoLength;
3483 if (lpUrlComponents->lpszScheme)
3485 len = URL_GET_COMP_LENGTHA(lpUrlComponents, Scheme) + 1;
3486 urlCompW->lpszScheme = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3487 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszScheme,
3488 -1, urlCompW->lpszScheme, len);
3491 if (lpUrlComponents->lpszHostName)
3493 len = URL_GET_COMP_LENGTHA(lpUrlComponents, HostName) + 1;
3494 urlCompW->lpszHostName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3495 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszHostName,
3496 -1, urlCompW->lpszHostName, len);
3499 if (lpUrlComponents->lpszUserName)
3501 len = URL_GET_COMP_LENGTHA(lpUrlComponents, UserName) + 1;
3502 urlCompW->lpszUserName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3503 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszUserName,
3504 -1, urlCompW->lpszUserName, len);
3507 if (lpUrlComponents->lpszPassword)
3509 len = URL_GET_COMP_LENGTHA(lpUrlComponents, Password) + 1;
3510 urlCompW->lpszPassword = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3511 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszPassword,
3512 -1, urlCompW->lpszPassword, len);
3515 if (lpUrlComponents->lpszUrlPath)
3517 len = URL_GET_COMP_LENGTHA(lpUrlComponents, UrlPath) + 1;
3518 urlCompW->lpszUrlPath = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3519 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszUrlPath,
3520 -1, urlCompW->lpszUrlPath, len);
3523 if (lpUrlComponents->lpszExtraInfo)
3525 len = URL_GET_COMP_LENGTHA(lpUrlComponents, ExtraInfo) + 1;
3526 urlCompW->lpszExtraInfo = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3527 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszExtraInfo,
3528 -1, urlCompW->lpszExtraInfo, len);
3532 /***********************************************************************
3533 * InternetCreateUrlA (WININET.@)
3535 * See InternetCreateUrlW.
3537 BOOL WINAPI InternetCreateUrlA(LPURL_COMPONENTSA lpUrlComponents, DWORD dwFlags,
3538 LPSTR lpszUrl, LPDWORD lpdwUrlLength)
3540 BOOL ret;
3541 LPWSTR urlW = NULL;
3542 URL_COMPONENTSW urlCompW;
3544 TRACE("(%p,%d,%p,%p)\n", lpUrlComponents, dwFlags, lpszUrl, lpdwUrlLength);
3546 if (!lpUrlComponents || lpUrlComponents->dwStructSize != sizeof(URL_COMPONENTSW) || !lpdwUrlLength)
3548 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
3549 return FALSE;
3552 convert_urlcomp_atow(lpUrlComponents, &urlCompW);
3554 if (lpszUrl)
3555 urlW = HeapAlloc(GetProcessHeap(), 0, *lpdwUrlLength * sizeof(WCHAR));
3557 ret = InternetCreateUrlW(&urlCompW, dwFlags, urlW, lpdwUrlLength);
3559 if (!ret && (GetLastError() == ERROR_INSUFFICIENT_BUFFER))
3560 *lpdwUrlLength /= sizeof(WCHAR);
3562 /* on success, lpdwUrlLength points to the size of urlW in WCHARS
3563 * minus one, so add one to leave room for NULL terminator
3565 if (ret)
3566 WideCharToMultiByte(CP_ACP, 0, urlW, -1, lpszUrl, *lpdwUrlLength + 1, NULL, NULL);
3568 HeapFree(GetProcessHeap(), 0, urlCompW.lpszScheme);
3569 HeapFree(GetProcessHeap(), 0, urlCompW.lpszHostName);
3570 HeapFree(GetProcessHeap(), 0, urlCompW.lpszUserName);
3571 HeapFree(GetProcessHeap(), 0, urlCompW.lpszPassword);
3572 HeapFree(GetProcessHeap(), 0, urlCompW.lpszUrlPath);
3573 HeapFree(GetProcessHeap(), 0, urlCompW.lpszExtraInfo);
3574 HeapFree(GetProcessHeap(), 0, urlW);
3576 return ret;
3579 /***********************************************************************
3580 * InternetCreateUrlW (WININET.@)
3582 * Creates a URL from its component parts.
3584 * PARAMS
3585 * lpUrlComponents [I] URL Components.
3586 * dwFlags [I] Flags. See notes.
3587 * lpszUrl [I] Buffer in which to store the created URL.
3588 * lpdwUrlLength [I/O] On input, the length of the buffer pointed to by
3589 * lpszUrl in characters. On output, the number of bytes
3590 * required to store the URL including terminator.
3592 * NOTES
3594 * The dwFlags parameter can be zero or more of the following:
3595 *|ICU_ESCAPE - Generates escape sequences for unsafe characters in the path and extra info of the URL.
3597 * RETURNS
3598 * TRUE on success
3599 * FALSE on failure
3602 BOOL WINAPI InternetCreateUrlW(LPURL_COMPONENTSW lpUrlComponents, DWORD dwFlags,
3603 LPWSTR lpszUrl, LPDWORD lpdwUrlLength)
3605 DWORD dwLen;
3606 INTERNET_SCHEME nScheme;
3608 static const WCHAR slashSlashW[] = {'/','/'};
3609 static const WCHAR percentD[] = {'%','d',0};
3611 TRACE("(%p,%d,%p,%p)\n", lpUrlComponents, dwFlags, lpszUrl, lpdwUrlLength);
3613 if (!lpUrlComponents || lpUrlComponents->dwStructSize != sizeof(URL_COMPONENTSW) || !lpdwUrlLength)
3615 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
3616 return FALSE;
3619 if (!calc_url_length(lpUrlComponents, &dwLen))
3620 return FALSE;
3622 if (!lpszUrl || *lpdwUrlLength < dwLen)
3624 *lpdwUrlLength = (dwLen + 1) * sizeof(WCHAR);
3625 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
3626 return FALSE;
3629 *lpdwUrlLength = dwLen;
3630 lpszUrl[0] = 0x00;
3632 dwLen = 0;
3634 if (lpUrlComponents->lpszScheme)
3636 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, Scheme);
3637 memcpy(lpszUrl, lpUrlComponents->lpszScheme, dwLen * sizeof(WCHAR));
3638 lpszUrl += dwLen;
3640 nScheme = GetInternetSchemeW(lpUrlComponents->lpszScheme, dwLen);
3642 else
3644 LPCWSTR scheme;
3645 nScheme = lpUrlComponents->nScheme;
3647 if (nScheme == INTERNET_SCHEME_DEFAULT)
3648 nScheme = INTERNET_SCHEME_HTTP;
3650 scheme = INTERNET_GetSchemeString(nScheme);
3651 dwLen = strlenW(scheme);
3652 memcpy(lpszUrl, scheme, dwLen * sizeof(WCHAR));
3653 lpszUrl += dwLen;
3656 /* all schemes are followed by at least a colon */
3657 *lpszUrl = ':';
3658 lpszUrl++;
3660 if (!scheme_is_opaque(nScheme) || lpUrlComponents->lpszHostName)
3662 memcpy(lpszUrl, slashSlashW, sizeof(slashSlashW));
3663 lpszUrl += sizeof(slashSlashW)/sizeof(slashSlashW[0]);
3666 if (lpUrlComponents->lpszUserName)
3668 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, UserName);
3669 memcpy(lpszUrl, lpUrlComponents->lpszUserName, dwLen * sizeof(WCHAR));
3670 lpszUrl += dwLen;
3672 if (lpUrlComponents->lpszPassword)
3674 *lpszUrl = ':';
3675 lpszUrl++;
3677 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, Password);
3678 memcpy(lpszUrl, lpUrlComponents->lpszPassword, dwLen * sizeof(WCHAR));
3679 lpszUrl += dwLen;
3682 *lpszUrl = '@';
3683 lpszUrl++;
3686 if (lpUrlComponents->lpszHostName)
3688 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, HostName);
3689 memcpy(lpszUrl, lpUrlComponents->lpszHostName, dwLen * sizeof(WCHAR));
3690 lpszUrl += dwLen;
3692 if (!url_uses_default_port(nScheme, lpUrlComponents->nPort))
3694 WCHAR szPort[MAX_WORD_DIGITS+1];
3696 sprintfW(szPort, percentD, lpUrlComponents->nPort);
3697 *lpszUrl = ':';
3698 lpszUrl++;
3699 dwLen = strlenW(szPort);
3700 memcpy(lpszUrl, szPort, dwLen * sizeof(WCHAR));
3701 lpszUrl += dwLen;
3704 /* add slash between hostname and path if necessary */
3705 if (lpUrlComponents->lpszUrlPath && *lpUrlComponents->lpszUrlPath != '/')
3707 *lpszUrl = '/';
3708 lpszUrl++;
3713 if (lpUrlComponents->lpszUrlPath)
3715 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, UrlPath);
3716 memcpy(lpszUrl, lpUrlComponents->lpszUrlPath, dwLen * sizeof(WCHAR));
3717 lpszUrl += dwLen;
3720 *lpszUrl = '\0';
3722 return TRUE;
3725 /***********************************************************************
3726 * InternetConfirmZoneCrossingA (WININET.@)
3729 DWORD WINAPI InternetConfirmZoneCrossingA( HWND hWnd, LPSTR szUrlPrev, LPSTR szUrlNew, BOOL bPost )
3731 FIXME("(%p, %s, %s, %x) stub\n", hWnd, debugstr_a(szUrlPrev), debugstr_a(szUrlNew), bPost);
3732 return ERROR_SUCCESS;
3735 /***********************************************************************
3736 * InternetConfirmZoneCrossingW (WININET.@)
3739 DWORD WINAPI InternetConfirmZoneCrossingW( HWND hWnd, LPWSTR szUrlPrev, LPWSTR szUrlNew, BOOL bPost )
3741 FIXME("(%p, %s, %s, %x) stub\n", hWnd, debugstr_w(szUrlPrev), debugstr_w(szUrlNew), bPost);
3742 return ERROR_SUCCESS;
3745 DWORD WINAPI InternetDialA( HWND hwndParent, LPSTR lpszConnectoid, DWORD dwFlags,
3746 DWORD_PTR* lpdwConnection, DWORD dwReserved )
3748 FIXME("(%p, %p, 0x%08x, %p, 0x%08x) stub\n", hwndParent, lpszConnectoid, dwFlags,
3749 lpdwConnection, dwReserved);
3750 return ERROR_SUCCESS;
3753 DWORD WINAPI InternetDialW( HWND hwndParent, LPWSTR lpszConnectoid, DWORD dwFlags,
3754 DWORD_PTR* lpdwConnection, DWORD dwReserved )
3756 FIXME("(%p, %p, 0x%08x, %p, 0x%08x) stub\n", hwndParent, lpszConnectoid, dwFlags,
3757 lpdwConnection, dwReserved);
3758 return ERROR_SUCCESS;
3761 BOOL WINAPI InternetGoOnlineA( LPSTR lpszURL, HWND hwndParent, DWORD dwReserved )
3763 FIXME("(%s, %p, 0x%08x) stub\n", debugstr_a(lpszURL), hwndParent, dwReserved);
3764 return TRUE;
3767 BOOL WINAPI InternetGoOnlineW( LPWSTR lpszURL, HWND hwndParent, DWORD dwReserved )
3769 FIXME("(%s, %p, 0x%08x) stub\n", debugstr_w(lpszURL), hwndParent, dwReserved);
3770 return TRUE;
3773 DWORD WINAPI InternetHangUp( DWORD_PTR dwConnection, DWORD dwReserved )
3775 FIXME("(0x%08lx, 0x%08x) stub\n", dwConnection, dwReserved);
3776 return ERROR_SUCCESS;
3779 BOOL WINAPI CreateMD5SSOHash( PWSTR pszChallengeInfo, PWSTR pwszRealm, PWSTR pwszTarget,
3780 PBYTE pbHexHash )
3782 FIXME("(%s, %s, %s, %p) stub\n", debugstr_w(pszChallengeInfo), debugstr_w(pwszRealm),
3783 debugstr_w(pwszTarget), pbHexHash);
3784 return FALSE;
3787 BOOL WINAPI ResumeSuspendedDownload( HINTERNET hInternet, DWORD dwError )
3789 FIXME("(%p, 0x%08x) stub\n", hInternet, dwError);
3790 return FALSE;
3793 BOOL WINAPI InternetQueryFortezzaStatus(DWORD *a, DWORD_PTR b)
3795 FIXME("(%p, %08lx) stub\n", a, b);
3796 return 0;