explorer: Implement ABM_ADD and ABM_REMOVE.
[wine/multimedia.git] / dlls / wininet / internet.c
blobd06298f5cfcb1a73d6120911e87f3e6f8200d055
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)
415 TRACE("Proxy is not enabled.\n");
416 lpwai->dwAccessType = INTERNET_OPEN_TYPE_DIRECT;
418 RegCloseKey( key );
419 return (enabled > 0);
422 /***********************************************************************
423 * dump_INTERNET_FLAGS
425 * Helper function to TRACE the internet flags.
427 * RETURNS
428 * None
431 static void dump_INTERNET_FLAGS(DWORD dwFlags)
433 #define FE(x) { x, #x }
434 static const wininet_flag_info flag[] = {
435 FE(INTERNET_FLAG_RELOAD),
436 FE(INTERNET_FLAG_RAW_DATA),
437 FE(INTERNET_FLAG_EXISTING_CONNECT),
438 FE(INTERNET_FLAG_ASYNC),
439 FE(INTERNET_FLAG_PASSIVE),
440 FE(INTERNET_FLAG_NO_CACHE_WRITE),
441 FE(INTERNET_FLAG_MAKE_PERSISTENT),
442 FE(INTERNET_FLAG_FROM_CACHE),
443 FE(INTERNET_FLAG_SECURE),
444 FE(INTERNET_FLAG_KEEP_CONNECTION),
445 FE(INTERNET_FLAG_NO_AUTO_REDIRECT),
446 FE(INTERNET_FLAG_READ_PREFETCH),
447 FE(INTERNET_FLAG_NO_COOKIES),
448 FE(INTERNET_FLAG_NO_AUTH),
449 FE(INTERNET_FLAG_CACHE_IF_NET_FAIL),
450 FE(INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP),
451 FE(INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTPS),
452 FE(INTERNET_FLAG_IGNORE_CERT_DATE_INVALID),
453 FE(INTERNET_FLAG_IGNORE_CERT_CN_INVALID),
454 FE(INTERNET_FLAG_RESYNCHRONIZE),
455 FE(INTERNET_FLAG_HYPERLINK),
456 FE(INTERNET_FLAG_NO_UI),
457 FE(INTERNET_FLAG_PRAGMA_NOCACHE),
458 FE(INTERNET_FLAG_CACHE_ASYNC),
459 FE(INTERNET_FLAG_FORMS_SUBMIT),
460 FE(INTERNET_FLAG_NEED_FILE),
461 FE(INTERNET_FLAG_TRANSFER_ASCII),
462 FE(INTERNET_FLAG_TRANSFER_BINARY)
464 #undef FE
465 int i;
467 for (i = 0; i < (sizeof(flag) / sizeof(flag[0])); i++) {
468 if (flag[i].val & dwFlags) {
469 TRACE(" %s", flag[i].name);
470 dwFlags &= ~flag[i].val;
473 if (dwFlags)
474 TRACE(" Unknown flags (%08x)\n", dwFlags);
475 else
476 TRACE("\n");
479 /***********************************************************************
480 * INTERNET_CloseHandle (internal)
482 * Close internet handle
485 static VOID APPINFO_Destroy(WININETHANDLEHEADER *hdr)
487 LPWININETAPPINFOW lpwai = (LPWININETAPPINFOW) hdr;
489 TRACE("%p\n",lpwai);
491 HeapFree(GetProcessHeap(), 0, lpwai->lpszAgent);
492 HeapFree(GetProcessHeap(), 0, lpwai->lpszProxy);
493 HeapFree(GetProcessHeap(), 0, lpwai->lpszProxyBypass);
494 HeapFree(GetProcessHeap(), 0, lpwai->lpszProxyUsername);
495 HeapFree(GetProcessHeap(), 0, lpwai->lpszProxyPassword);
496 HeapFree(GetProcessHeap(), 0, lpwai);
499 static DWORD APPINFO_QueryOption(WININETHANDLEHEADER *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
501 LPWININETAPPINFOW ai = (LPWININETAPPINFOW)hdr;
503 switch(option) {
504 case INTERNET_OPTION_HANDLE_TYPE:
505 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
507 if (*size < sizeof(ULONG))
508 return ERROR_INSUFFICIENT_BUFFER;
510 *size = sizeof(DWORD);
511 *(DWORD*)buffer = INTERNET_HANDLE_TYPE_INTERNET;
512 return ERROR_SUCCESS;
514 case INTERNET_OPTION_USER_AGENT: {
515 DWORD bufsize;
517 TRACE("INTERNET_OPTION_USER_AGENT\n");
519 bufsize = *size;
521 if (unicode) {
522 *size = (strlenW(ai->lpszAgent) + 1) * sizeof(WCHAR);
523 if(!buffer || bufsize < *size)
524 return ERROR_INSUFFICIENT_BUFFER;
526 strcpyW(buffer, ai->lpszAgent);
527 }else {
528 *size = WideCharToMultiByte(CP_ACP, 0, ai->lpszAgent, -1, NULL, 0, NULL, NULL);
529 if(!buffer || bufsize < *size)
530 return ERROR_INSUFFICIENT_BUFFER;
532 WideCharToMultiByte(CP_ACP, 0, ai->lpszAgent, -1, buffer, *size, NULL, NULL);
535 return ERROR_SUCCESS;
538 case INTERNET_OPTION_PROXY:
539 if (unicode) {
540 INTERNET_PROXY_INFOW *pi = (INTERNET_PROXY_INFOW *)buffer;
541 DWORD proxyBytesRequired = 0, proxyBypassBytesRequired = 0;
542 LPWSTR proxy, proxy_bypass;
544 if (ai->lpszProxy)
545 proxyBytesRequired = (lstrlenW(ai->lpszProxy) + 1) * sizeof(WCHAR);
546 if (ai->lpszProxyBypass)
547 proxyBypassBytesRequired = (lstrlenW(ai->lpszProxyBypass) + 1) * sizeof(WCHAR);
548 if (*size < sizeof(INTERNET_PROXY_INFOW) + proxyBytesRequired + proxyBypassBytesRequired)
549 return ERROR_INSUFFICIENT_BUFFER;
551 proxy = (LPWSTR)((LPBYTE)buffer + sizeof(INTERNET_PROXY_INFOW));
552 proxy_bypass = (LPWSTR)((LPBYTE)buffer + sizeof(INTERNET_PROXY_INFOW) + proxyBytesRequired);
554 pi->dwAccessType = ai->dwAccessType;
555 pi->lpszProxy = NULL;
556 pi->lpszProxyBypass = NULL;
557 if (ai->lpszProxy) {
558 lstrcpyW(proxy, ai->lpszProxy);
559 pi->lpszProxy = proxy;
562 if (ai->lpszProxyBypass) {
563 lstrcpyW(proxy_bypass, ai->lpszProxyBypass);
564 pi->lpszProxyBypass = proxy_bypass;
567 *size = sizeof(INTERNET_PROXY_INFOW) + proxyBytesRequired + proxyBypassBytesRequired;
568 return ERROR_SUCCESS;
569 }else {
570 INTERNET_PROXY_INFOA *pi = (INTERNET_PROXY_INFOA *)buffer;
571 DWORD proxyBytesRequired = 0, proxyBypassBytesRequired = 0;
572 LPSTR proxy, proxy_bypass;
574 if (ai->lpszProxy)
575 proxyBytesRequired = WideCharToMultiByte(CP_ACP, 0, ai->lpszProxy, -1, NULL, 0, NULL, NULL);
576 if (ai->lpszProxyBypass)
577 proxyBypassBytesRequired = WideCharToMultiByte(CP_ACP, 0, ai->lpszProxyBypass, -1,
578 NULL, 0, NULL, NULL);
579 if (*size < sizeof(INTERNET_PROXY_INFOA) + proxyBytesRequired + proxyBypassBytesRequired)
580 return ERROR_INSUFFICIENT_BUFFER;
582 proxy = (LPSTR)((LPBYTE)buffer + sizeof(INTERNET_PROXY_INFOA));
583 proxy_bypass = (LPSTR)((LPBYTE)buffer + sizeof(INTERNET_PROXY_INFOA) + proxyBytesRequired);
585 pi->dwAccessType = ai->dwAccessType;
586 pi->lpszProxy = NULL;
587 pi->lpszProxyBypass = NULL;
588 if (ai->lpszProxy) {
589 WideCharToMultiByte(CP_ACP, 0, ai->lpszProxy, -1, proxy, proxyBytesRequired, NULL, NULL);
590 pi->lpszProxy = proxy;
593 if (ai->lpszProxyBypass) {
594 WideCharToMultiByte(CP_ACP, 0, ai->lpszProxyBypass, -1, proxy_bypass,
595 proxyBypassBytesRequired, NULL, NULL);
596 pi->lpszProxyBypass = proxy_bypass;
599 *size = sizeof(INTERNET_PROXY_INFOA) + proxyBytesRequired + proxyBypassBytesRequired;
600 return ERROR_SUCCESS;
604 return INET_QueryOption(option, buffer, size, unicode);
607 static const HANDLEHEADERVtbl APPINFOVtbl = {
608 APPINFO_Destroy,
609 NULL,
610 APPINFO_QueryOption,
611 NULL,
612 NULL,
613 NULL,
614 NULL,
615 NULL,
616 NULL
620 /***********************************************************************
621 * InternetOpenW (WININET.@)
623 * Per-application initialization of wininet
625 * RETURNS
626 * HINTERNET on success
627 * NULL on failure
630 HINTERNET WINAPI InternetOpenW(LPCWSTR lpszAgent, DWORD dwAccessType,
631 LPCWSTR lpszProxy, LPCWSTR lpszProxyBypass, DWORD dwFlags)
633 LPWININETAPPINFOW lpwai = NULL;
634 HINTERNET handle = NULL;
636 if (TRACE_ON(wininet)) {
637 #define FE(x) { x, #x }
638 static const wininet_flag_info access_type[] = {
639 FE(INTERNET_OPEN_TYPE_PRECONFIG),
640 FE(INTERNET_OPEN_TYPE_DIRECT),
641 FE(INTERNET_OPEN_TYPE_PROXY),
642 FE(INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY)
644 #undef FE
645 DWORD i;
646 const char *access_type_str = "Unknown";
648 TRACE("(%s, %i, %s, %s, %i)\n", debugstr_w(lpszAgent), dwAccessType,
649 debugstr_w(lpszProxy), debugstr_w(lpszProxyBypass), dwFlags);
650 for (i = 0; i < (sizeof(access_type) / sizeof(access_type[0])); i++) {
651 if (access_type[i].val == dwAccessType) {
652 access_type_str = access_type[i].name;
653 break;
656 TRACE(" access type : %s\n", access_type_str);
657 TRACE(" flags :");
658 dump_INTERNET_FLAGS(dwFlags);
661 /* Clear any error information */
662 INTERNET_SetLastError(0);
664 lpwai = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETAPPINFOW));
665 if (NULL == lpwai)
667 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
668 goto lend;
671 lpwai->hdr.htype = WH_HINIT;
672 lpwai->hdr.vtbl = &APPINFOVtbl;
673 lpwai->hdr.dwFlags = dwFlags;
674 lpwai->hdr.refs = 1;
675 lpwai->dwAccessType = dwAccessType;
676 lpwai->lpszProxyUsername = NULL;
677 lpwai->lpszProxyPassword = NULL;
679 handle = WININET_AllocHandle( &lpwai->hdr );
680 if( !handle )
682 HeapFree( GetProcessHeap(), 0, lpwai );
683 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
684 goto lend;
687 if (NULL != lpszAgent)
689 lpwai->lpszAgent = HeapAlloc( GetProcessHeap(),0,
690 (strlenW(lpszAgent)+1)*sizeof(WCHAR));
691 if (lpwai->lpszAgent)
692 lstrcpyW( lpwai->lpszAgent, lpszAgent );
694 if(dwAccessType == INTERNET_OPEN_TYPE_PRECONFIG)
695 INTERNET_ConfigureProxy( lpwai );
696 else if (NULL != lpszProxy)
698 lpwai->lpszProxy = HeapAlloc( GetProcessHeap(), 0,
699 (strlenW(lpszProxy)+1)*sizeof(WCHAR));
700 if (lpwai->lpszProxy)
701 lstrcpyW( lpwai->lpszProxy, lpszProxy );
704 if (NULL != lpszProxyBypass)
706 lpwai->lpszProxyBypass = HeapAlloc( GetProcessHeap(), 0,
707 (strlenW(lpszProxyBypass)+1)*sizeof(WCHAR));
708 if (lpwai->lpszProxyBypass)
709 lstrcpyW( lpwai->lpszProxyBypass, lpszProxyBypass );
712 lend:
713 if( lpwai )
714 WININET_Release( &lpwai->hdr );
716 TRACE("returning %p\n", lpwai);
718 return handle;
722 /***********************************************************************
723 * InternetOpenA (WININET.@)
725 * Per-application initialization of wininet
727 * RETURNS
728 * HINTERNET on success
729 * NULL on failure
732 HINTERNET WINAPI InternetOpenA(LPCSTR lpszAgent, DWORD dwAccessType,
733 LPCSTR lpszProxy, LPCSTR lpszProxyBypass, DWORD dwFlags)
735 HINTERNET rc = NULL;
736 INT len;
737 WCHAR *szAgent = NULL, *szProxy = NULL, *szBypass = NULL;
739 TRACE("(%s, 0x%08x, %s, %s, 0x%08x)\n", debugstr_a(lpszAgent),
740 dwAccessType, debugstr_a(lpszProxy), debugstr_a(lpszProxyBypass), dwFlags);
742 if( lpszAgent )
744 len = MultiByteToWideChar(CP_ACP, 0, lpszAgent, -1, NULL, 0);
745 szAgent = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
746 MultiByteToWideChar(CP_ACP, 0, lpszAgent, -1, szAgent, len);
749 if( lpszProxy )
751 len = MultiByteToWideChar(CP_ACP, 0, lpszProxy, -1, NULL, 0);
752 szProxy = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
753 MultiByteToWideChar(CP_ACP, 0, lpszProxy, -1, szProxy, len);
756 if( lpszProxyBypass )
758 len = MultiByteToWideChar(CP_ACP, 0, lpszProxyBypass, -1, NULL, 0);
759 szBypass = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
760 MultiByteToWideChar(CP_ACP, 0, lpszProxyBypass, -1, szBypass, len);
763 rc = InternetOpenW(szAgent, dwAccessType, szProxy, szBypass, dwFlags);
765 HeapFree(GetProcessHeap(), 0, szAgent);
766 HeapFree(GetProcessHeap(), 0, szProxy);
767 HeapFree(GetProcessHeap(), 0, szBypass);
769 return rc;
772 /***********************************************************************
773 * InternetGetLastResponseInfoA (WININET.@)
775 * Return last wininet error description on the calling thread
777 * RETURNS
778 * TRUE on success of writing to buffer
779 * FALSE on failure
782 BOOL WINAPI InternetGetLastResponseInfoA(LPDWORD lpdwError,
783 LPSTR lpszBuffer, LPDWORD lpdwBufferLength)
785 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
787 TRACE("\n");
789 if (lpwite)
791 *lpdwError = lpwite->dwError;
792 if (lpwite->dwError)
794 memcpy(lpszBuffer, lpwite->response, *lpdwBufferLength);
795 *lpdwBufferLength = strlen(lpszBuffer);
797 else
798 *lpdwBufferLength = 0;
800 else
802 *lpdwError = 0;
803 *lpdwBufferLength = 0;
806 return TRUE;
809 /***********************************************************************
810 * InternetGetLastResponseInfoW (WININET.@)
812 * Return last wininet error description on the calling thread
814 * RETURNS
815 * TRUE on success of writing to buffer
816 * FALSE on failure
819 BOOL WINAPI InternetGetLastResponseInfoW(LPDWORD lpdwError,
820 LPWSTR lpszBuffer, LPDWORD lpdwBufferLength)
822 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
824 TRACE("\n");
826 if (lpwite)
828 *lpdwError = lpwite->dwError;
829 if (lpwite->dwError)
831 memcpy(lpszBuffer, lpwite->response, *lpdwBufferLength);
832 *lpdwBufferLength = lstrlenW(lpszBuffer);
834 else
835 *lpdwBufferLength = 0;
837 else
839 *lpdwError = 0;
840 *lpdwBufferLength = 0;
843 return TRUE;
846 /***********************************************************************
847 * InternetGetConnectedState (WININET.@)
849 * Return connected state
851 * RETURNS
852 * TRUE if connected
853 * if lpdwStatus is not null, return the status (off line,
854 * modem, lan...) in it.
855 * FALSE if not connected
857 BOOL WINAPI InternetGetConnectedState(LPDWORD lpdwStatus, DWORD dwReserved)
859 TRACE("(%p, 0x%08x)\n", lpdwStatus, dwReserved);
861 if (lpdwStatus) {
862 FIXME("always returning LAN connection.\n");
863 *lpdwStatus = INTERNET_CONNECTION_LAN;
865 return TRUE;
869 /***********************************************************************
870 * InternetGetConnectedStateExW (WININET.@)
872 * Return connected state
874 * PARAMS
876 * lpdwStatus [O] Flags specifying the status of the internet connection.
877 * lpszConnectionName [O] Pointer to buffer to receive the friendly name of the internet connection.
878 * dwNameLen [I] Size of the buffer, in characters.
879 * dwReserved [I] Reserved. Must be set to 0.
881 * RETURNS
882 * TRUE if connected
883 * if lpdwStatus is not null, return the status (off line,
884 * modem, lan...) in it.
885 * FALSE if not connected
887 * NOTES
888 * If the system has no available network connections, an empty string is
889 * stored in lpszConnectionName. If there is a LAN connection, a localized
890 * "LAN Connection" string is stored. Presumably, if only a dial-up
891 * connection is available then the name of the dial-up connection is
892 * returned. Why any application, other than the "Internet Settings" CPL,
893 * would want to use this function instead of the simpler InternetGetConnectedStateW
894 * function is beyond me.
896 BOOL WINAPI InternetGetConnectedStateExW(LPDWORD lpdwStatus, LPWSTR lpszConnectionName,
897 DWORD dwNameLen, DWORD dwReserved)
899 TRACE("(%p, %p, %d, 0x%08x)\n", lpdwStatus, lpszConnectionName, dwNameLen, dwReserved);
901 /* Must be zero */
902 if(dwReserved)
903 return FALSE;
905 if (lpdwStatus) {
906 FIXME("always returning LAN connection.\n");
907 *lpdwStatus = INTERNET_CONNECTION_LAN;
909 return LoadStringW(WININET_hModule, IDS_LANCONNECTION, lpszConnectionName, dwNameLen);
913 /***********************************************************************
914 * InternetGetConnectedStateExA (WININET.@)
916 BOOL WINAPI InternetGetConnectedStateExA(LPDWORD lpdwStatus, LPSTR lpszConnectionName,
917 DWORD dwNameLen, DWORD dwReserved)
919 LPWSTR lpwszConnectionName = NULL;
920 BOOL rc;
922 TRACE("(%p, %p, %d, 0x%08x)\n", lpdwStatus, lpszConnectionName, dwNameLen, dwReserved);
924 if (lpszConnectionName && dwNameLen > 0)
925 lpwszConnectionName= HeapAlloc(GetProcessHeap(), 0, dwNameLen * sizeof(WCHAR));
927 rc = InternetGetConnectedStateExW(lpdwStatus,lpwszConnectionName, dwNameLen,
928 dwReserved);
929 if (rc && lpwszConnectionName)
931 WideCharToMultiByte(CP_ACP,0,lpwszConnectionName,-1,lpszConnectionName,
932 dwNameLen, NULL, NULL);
934 HeapFree(GetProcessHeap(),0,lpwszConnectionName);
937 return rc;
941 /***********************************************************************
942 * InternetConnectW (WININET.@)
944 * Open a ftp, gopher or http session
946 * RETURNS
947 * HINTERNET a session handle on success
948 * NULL on failure
951 HINTERNET WINAPI InternetConnectW(HINTERNET hInternet,
952 LPCWSTR lpszServerName, INTERNET_PORT nServerPort,
953 LPCWSTR lpszUserName, LPCWSTR lpszPassword,
954 DWORD dwService, DWORD dwFlags, DWORD_PTR dwContext)
956 LPWININETAPPINFOW hIC;
957 HINTERNET rc = NULL;
959 TRACE("(%p, %s, %i, %s, %s, %i, %i, %lx)\n", hInternet, debugstr_w(lpszServerName),
960 nServerPort, debugstr_w(lpszUserName), debugstr_w(lpszPassword),
961 dwService, dwFlags, dwContext);
963 if (!lpszServerName)
965 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
966 return NULL;
969 /* Clear any error information */
970 INTERNET_SetLastError(0);
971 hIC = (LPWININETAPPINFOW) WININET_GetObject( hInternet );
972 if ( (hIC == NULL) || (hIC->hdr.htype != WH_HINIT) )
974 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
975 goto lend;
978 switch (dwService)
980 case INTERNET_SERVICE_FTP:
981 rc = FTP_Connect(hIC, lpszServerName, nServerPort,
982 lpszUserName, lpszPassword, dwFlags, dwContext, 0);
983 break;
985 case INTERNET_SERVICE_HTTP:
986 rc = HTTP_Connect(hIC, lpszServerName, nServerPort,
987 lpszUserName, lpszPassword, dwFlags, dwContext, 0);
988 break;
990 case INTERNET_SERVICE_GOPHER:
991 default:
992 break;
994 lend:
995 if( hIC )
996 WININET_Release( &hIC->hdr );
998 TRACE("returning %p\n", rc);
999 return rc;
1003 /***********************************************************************
1004 * InternetConnectA (WININET.@)
1006 * Open a ftp, gopher or http session
1008 * RETURNS
1009 * HINTERNET a session handle on success
1010 * NULL on failure
1013 HINTERNET WINAPI InternetConnectA(HINTERNET hInternet,
1014 LPCSTR lpszServerName, INTERNET_PORT nServerPort,
1015 LPCSTR lpszUserName, LPCSTR lpszPassword,
1016 DWORD dwService, DWORD dwFlags, DWORD_PTR dwContext)
1018 HINTERNET rc = NULL;
1019 INT len = 0;
1020 LPWSTR szServerName = NULL;
1021 LPWSTR szUserName = NULL;
1022 LPWSTR szPassword = NULL;
1024 if (lpszServerName)
1026 len = MultiByteToWideChar(CP_ACP, 0, lpszServerName, -1, NULL, 0);
1027 szServerName = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1028 MultiByteToWideChar(CP_ACP, 0, lpszServerName, -1, szServerName, len);
1030 if (lpszUserName)
1032 len = MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, NULL, 0);
1033 szUserName = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1034 MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, szUserName, len);
1036 if (lpszPassword)
1038 len = MultiByteToWideChar(CP_ACP, 0, lpszPassword, -1, NULL, 0);
1039 szPassword = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1040 MultiByteToWideChar(CP_ACP, 0, lpszPassword, -1, szPassword, len);
1044 rc = InternetConnectW(hInternet, szServerName, nServerPort,
1045 szUserName, szPassword, dwService, dwFlags, dwContext);
1047 HeapFree(GetProcessHeap(), 0, szServerName);
1048 HeapFree(GetProcessHeap(), 0, szUserName);
1049 HeapFree(GetProcessHeap(), 0, szPassword);
1050 return rc;
1054 /***********************************************************************
1055 * InternetFindNextFileA (WININET.@)
1057 * Continues a file search from a previous call to FindFirstFile
1059 * RETURNS
1060 * TRUE on success
1061 * FALSE on failure
1064 BOOL WINAPI InternetFindNextFileA(HINTERNET hFind, LPVOID lpvFindData)
1066 BOOL ret;
1067 WIN32_FIND_DATAW fd;
1069 ret = InternetFindNextFileW(hFind, lpvFindData?&fd:NULL);
1070 if(lpvFindData)
1071 WININET_find_data_WtoA(&fd, (LPWIN32_FIND_DATAA)lpvFindData);
1072 return ret;
1075 /***********************************************************************
1076 * InternetFindNextFileW (WININET.@)
1078 * Continues a file search from a previous call to FindFirstFile
1080 * RETURNS
1081 * TRUE on success
1082 * FALSE on failure
1085 BOOL WINAPI InternetFindNextFileW(HINTERNET hFind, LPVOID lpvFindData)
1087 WININETHANDLEHEADER *hdr;
1088 DWORD res;
1090 TRACE("\n");
1092 hdr = WININET_GetObject(hFind);
1093 if(!hdr) {
1094 WARN("Invalid handle\n");
1095 SetLastError(ERROR_INVALID_HANDLE);
1096 return FALSE;
1099 if(hdr->vtbl->FindNextFileW) {
1100 res = hdr->vtbl->FindNextFileW(hdr, lpvFindData);
1101 }else {
1102 WARN("Handle doesn't support NextFile\n");
1103 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
1106 WININET_Release(hdr);
1108 if(res != ERROR_SUCCESS)
1109 SetLastError(res);
1110 return res == ERROR_SUCCESS;
1113 /***********************************************************************
1114 * InternetCloseHandle (WININET.@)
1116 * Generic close handle function
1118 * RETURNS
1119 * TRUE on success
1120 * FALSE on failure
1123 BOOL WINAPI InternetCloseHandle(HINTERNET hInternet)
1125 LPWININETHANDLEHEADER lpwh;
1127 TRACE("%p\n",hInternet);
1129 lpwh = WININET_GetObject( hInternet );
1130 if (NULL == lpwh)
1132 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
1133 return FALSE;
1136 WININET_Release( lpwh );
1137 WININET_FreeHandle( hInternet );
1139 return TRUE;
1143 /***********************************************************************
1144 * ConvertUrlComponentValue (Internal)
1146 * Helper function for InternetCrackUrlA
1149 static void ConvertUrlComponentValue(LPSTR* lppszComponent, LPDWORD dwComponentLen,
1150 LPWSTR lpwszComponent, DWORD dwwComponentLen,
1151 LPCSTR lpszStart, LPCWSTR lpwszStart)
1153 TRACE("%p %d %p %d %p %p\n", *lppszComponent, *dwComponentLen, lpwszComponent, dwwComponentLen, lpszStart, lpwszStart);
1154 if (*dwComponentLen != 0)
1156 DWORD nASCIILength=WideCharToMultiByte(CP_ACP,0,lpwszComponent,dwwComponentLen,NULL,0,NULL,NULL);
1157 if (*lppszComponent == NULL)
1159 int nASCIIOffset=WideCharToMultiByte(CP_ACP,0,lpwszStart,lpwszComponent-lpwszStart,NULL,0,NULL,NULL);
1160 if (lpwszComponent)
1161 *lppszComponent = (LPSTR)lpszStart+nASCIIOffset;
1162 else
1163 *lppszComponent = NULL;
1164 *dwComponentLen = nASCIILength;
1166 else
1168 DWORD ncpylen = min((*dwComponentLen)-1, nASCIILength);
1169 WideCharToMultiByte(CP_ACP,0,lpwszComponent,dwwComponentLen,*lppszComponent,ncpylen+1,NULL,NULL);
1170 (*lppszComponent)[ncpylen]=0;
1171 *dwComponentLen = ncpylen;
1177 /***********************************************************************
1178 * InternetCrackUrlA (WININET.@)
1180 * See InternetCrackUrlW.
1182 BOOL WINAPI InternetCrackUrlA(LPCSTR lpszUrl, DWORD dwUrlLength, DWORD dwFlags,
1183 LPURL_COMPONENTSA lpUrlComponents)
1185 DWORD nLength;
1186 URL_COMPONENTSW UCW;
1187 BOOL ret = FALSE;
1188 WCHAR *lpwszUrl, *hostname = NULL, *username = NULL, *password = NULL, *path = NULL,
1189 *scheme = NULL, *extra = NULL;
1191 TRACE("(%s %u %x %p)\n",
1192 lpszUrl ? debugstr_an(lpszUrl, dwUrlLength ? dwUrlLength : strlen(lpszUrl)) : "(null)",
1193 dwUrlLength, dwFlags, lpUrlComponents);
1195 if (!lpszUrl || !*lpszUrl || !lpUrlComponents ||
1196 lpUrlComponents->dwStructSize != sizeof(URL_COMPONENTSA))
1198 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1199 return FALSE;
1202 if(dwUrlLength<=0)
1203 dwUrlLength=-1;
1204 nLength=MultiByteToWideChar(CP_ACP,0,lpszUrl,dwUrlLength,NULL,0);
1206 /* if dwUrlLength=-1 then nLength includes null but length to
1207 InternetCrackUrlW should not include it */
1208 if (dwUrlLength == -1) nLength--;
1210 lpwszUrl=HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WCHAR)*nLength);
1211 MultiByteToWideChar(CP_ACP,0,lpszUrl,dwUrlLength,lpwszUrl,nLength);
1213 memset(&UCW,0,sizeof(UCW));
1214 UCW.dwStructSize = sizeof(URL_COMPONENTSW);
1215 if (lpUrlComponents->dwHostNameLength)
1217 UCW.dwHostNameLength = lpUrlComponents->dwHostNameLength;
1218 if (lpUrlComponents->lpszHostName)
1220 hostname = HeapAlloc(GetProcessHeap(), 0, UCW.dwHostNameLength * sizeof(WCHAR));
1221 UCW.lpszHostName = hostname;
1224 if (lpUrlComponents->dwUserNameLength)
1226 UCW.dwUserNameLength = lpUrlComponents->dwUserNameLength;
1227 if (lpUrlComponents->lpszUserName)
1229 username = HeapAlloc(GetProcessHeap(), 0, UCW.dwUserNameLength * sizeof(WCHAR));
1230 UCW.lpszUserName = username;
1233 if (lpUrlComponents->dwPasswordLength)
1235 UCW.dwPasswordLength = lpUrlComponents->dwPasswordLength;
1236 if (lpUrlComponents->lpszPassword)
1238 password = HeapAlloc(GetProcessHeap(), 0, UCW.dwPasswordLength * sizeof(WCHAR));
1239 UCW.lpszPassword = password;
1242 if (lpUrlComponents->dwUrlPathLength)
1244 UCW.dwUrlPathLength = lpUrlComponents->dwUrlPathLength;
1245 if (lpUrlComponents->lpszUrlPath)
1247 path = HeapAlloc(GetProcessHeap(), 0, UCW.dwUrlPathLength * sizeof(WCHAR));
1248 UCW.lpszUrlPath = path;
1251 if (lpUrlComponents->dwSchemeLength)
1253 UCW.dwSchemeLength = lpUrlComponents->dwSchemeLength;
1254 if (lpUrlComponents->lpszScheme)
1256 scheme = HeapAlloc(GetProcessHeap(), 0, UCW.dwSchemeLength * sizeof(WCHAR));
1257 UCW.lpszScheme = scheme;
1260 if (lpUrlComponents->dwExtraInfoLength)
1262 UCW.dwExtraInfoLength = lpUrlComponents->dwExtraInfoLength;
1263 if (lpUrlComponents->lpszExtraInfo)
1265 extra = HeapAlloc(GetProcessHeap(), 0, UCW.dwExtraInfoLength * sizeof(WCHAR));
1266 UCW.lpszExtraInfo = extra;
1269 if ((ret = InternetCrackUrlW(lpwszUrl, nLength, dwFlags, &UCW)))
1271 ConvertUrlComponentValue(&lpUrlComponents->lpszHostName, &lpUrlComponents->dwHostNameLength,
1272 UCW.lpszHostName, UCW.dwHostNameLength, lpszUrl, lpwszUrl);
1273 ConvertUrlComponentValue(&lpUrlComponents->lpszUserName, &lpUrlComponents->dwUserNameLength,
1274 UCW.lpszUserName, UCW.dwUserNameLength, lpszUrl, lpwszUrl);
1275 ConvertUrlComponentValue(&lpUrlComponents->lpszPassword, &lpUrlComponents->dwPasswordLength,
1276 UCW.lpszPassword, UCW.dwPasswordLength, lpszUrl, lpwszUrl);
1277 ConvertUrlComponentValue(&lpUrlComponents->lpszUrlPath, &lpUrlComponents->dwUrlPathLength,
1278 UCW.lpszUrlPath, UCW.dwUrlPathLength, lpszUrl, lpwszUrl);
1279 ConvertUrlComponentValue(&lpUrlComponents->lpszScheme, &lpUrlComponents->dwSchemeLength,
1280 UCW.lpszScheme, UCW.dwSchemeLength, lpszUrl, lpwszUrl);
1281 ConvertUrlComponentValue(&lpUrlComponents->lpszExtraInfo, &lpUrlComponents->dwExtraInfoLength,
1282 UCW.lpszExtraInfo, UCW.dwExtraInfoLength, lpszUrl, lpwszUrl);
1284 lpUrlComponents->nScheme = UCW.nScheme;
1285 lpUrlComponents->nPort = UCW.nPort;
1287 TRACE("%s: scheme(%s) host(%s) path(%s) extra(%s)\n", lpszUrl,
1288 debugstr_an(lpUrlComponents->lpszScheme, lpUrlComponents->dwSchemeLength),
1289 debugstr_an(lpUrlComponents->lpszHostName, lpUrlComponents->dwHostNameLength),
1290 debugstr_an(lpUrlComponents->lpszUrlPath, lpUrlComponents->dwUrlPathLength),
1291 debugstr_an(lpUrlComponents->lpszExtraInfo, lpUrlComponents->dwExtraInfoLength));
1293 HeapFree(GetProcessHeap(), 0, lpwszUrl);
1294 HeapFree(GetProcessHeap(), 0, hostname);
1295 HeapFree(GetProcessHeap(), 0, username);
1296 HeapFree(GetProcessHeap(), 0, password);
1297 HeapFree(GetProcessHeap(), 0, path);
1298 HeapFree(GetProcessHeap(), 0, scheme);
1299 HeapFree(GetProcessHeap(), 0, extra);
1300 return ret;
1303 static const WCHAR url_schemes[][7] =
1305 {'f','t','p',0},
1306 {'g','o','p','h','e','r',0},
1307 {'h','t','t','p',0},
1308 {'h','t','t','p','s',0},
1309 {'f','i','l','e',0},
1310 {'n','e','w','s',0},
1311 {'m','a','i','l','t','o',0},
1312 {'r','e','s',0},
1315 /***********************************************************************
1316 * GetInternetSchemeW (internal)
1318 * Get scheme of url
1320 * RETURNS
1321 * scheme on success
1322 * INTERNET_SCHEME_UNKNOWN on failure
1325 static INTERNET_SCHEME GetInternetSchemeW(LPCWSTR lpszScheme, DWORD nMaxCmp)
1327 int i;
1329 TRACE("%s %d\n",debugstr_wn(lpszScheme, nMaxCmp), nMaxCmp);
1331 if(lpszScheme==NULL)
1332 return INTERNET_SCHEME_UNKNOWN;
1334 for (i = 0; i < sizeof(url_schemes)/sizeof(url_schemes[0]); i++)
1335 if (!strncmpW(lpszScheme, url_schemes[i], nMaxCmp))
1336 return INTERNET_SCHEME_FIRST + i;
1338 return INTERNET_SCHEME_UNKNOWN;
1341 /***********************************************************************
1342 * SetUrlComponentValueW (Internal)
1344 * Helper function for InternetCrackUrlW
1346 * PARAMS
1347 * lppszComponent [O] Holds the returned string
1348 * dwComponentLen [I] Holds the size of lppszComponent
1349 * [O] Holds the length of the string in lppszComponent without '\0'
1350 * lpszStart [I] Holds the string to copy from
1351 * len [I] Holds the length of lpszStart without '\0'
1353 * RETURNS
1354 * TRUE on success
1355 * FALSE on failure
1358 static BOOL SetUrlComponentValueW(LPWSTR* lppszComponent, LPDWORD dwComponentLen, LPCWSTR lpszStart, DWORD len)
1360 TRACE("%s (%d)\n", debugstr_wn(lpszStart,len), len);
1362 if ( (*dwComponentLen == 0) && (*lppszComponent == NULL) )
1363 return FALSE;
1365 if (*dwComponentLen != 0 || *lppszComponent == NULL)
1367 if (*lppszComponent == NULL)
1369 *lppszComponent = (LPWSTR)lpszStart;
1370 *dwComponentLen = len;
1372 else
1374 DWORD ncpylen = min((*dwComponentLen)-1, len);
1375 memcpy(*lppszComponent, lpszStart, ncpylen*sizeof(WCHAR));
1376 (*lppszComponent)[ncpylen] = '\0';
1377 *dwComponentLen = ncpylen;
1381 return TRUE;
1384 /***********************************************************************
1385 * InternetCrackUrlW (WININET.@)
1387 * Break up URL into its components
1389 * RETURNS
1390 * TRUE on success
1391 * FALSE on failure
1393 BOOL WINAPI InternetCrackUrlW(LPCWSTR lpszUrl_orig, DWORD dwUrlLength_orig, DWORD dwFlags,
1394 LPURL_COMPONENTSW lpUC)
1397 * RFC 1808
1398 * <protocol>:[//<net_loc>][/path][;<params>][?<query>][#<fragment>]
1401 LPCWSTR lpszParam = NULL;
1402 BOOL bIsAbsolute = FALSE;
1403 LPCWSTR lpszap, lpszUrl = lpszUrl_orig;
1404 LPCWSTR lpszcp = NULL;
1405 LPWSTR lpszUrl_decode = NULL;
1406 DWORD dwUrlLength = dwUrlLength_orig;
1408 TRACE("(%s %u %x %p)\n",
1409 lpszUrl ? debugstr_wn(lpszUrl, dwUrlLength ? dwUrlLength : strlenW(lpszUrl)) : "(null)",
1410 dwUrlLength, dwFlags, lpUC);
1412 if (!lpszUrl_orig || !*lpszUrl_orig || !lpUC)
1414 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1415 return FALSE;
1417 if (!dwUrlLength) dwUrlLength = strlenW(lpszUrl);
1419 if (dwFlags & ICU_DECODE)
1421 WCHAR *url_tmp;
1422 DWORD len = dwUrlLength + 1;
1424 if (!(url_tmp = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR))))
1426 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1427 return FALSE;
1429 memcpy(url_tmp, lpszUrl_orig, dwUrlLength * sizeof(WCHAR));
1430 url_tmp[dwUrlLength] = 0;
1431 if (!(lpszUrl_decode = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR))))
1433 HeapFree(GetProcessHeap(), 0, url_tmp);
1434 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1435 return FALSE;
1437 if (InternetCanonicalizeUrlW(url_tmp, lpszUrl_decode, &len, ICU_DECODE | ICU_NO_ENCODE))
1439 dwUrlLength = len;
1440 lpszUrl = lpszUrl_decode;
1442 HeapFree(GetProcessHeap(), 0, url_tmp);
1444 lpszap = lpszUrl;
1446 /* Determine if the URI is absolute. */
1447 while (lpszap - lpszUrl < dwUrlLength)
1449 if (isalnumW(*lpszap))
1451 lpszap++;
1452 continue;
1454 if ((*lpszap == ':') && (lpszap - lpszUrl >= 2))
1456 bIsAbsolute = TRUE;
1457 lpszcp = lpszap;
1459 else
1461 lpszcp = lpszUrl; /* Relative url */
1464 break;
1467 lpUC->nScheme = INTERNET_SCHEME_UNKNOWN;
1468 lpUC->nPort = INTERNET_INVALID_PORT_NUMBER;
1470 /* Parse <params> */
1471 if (!(lpszParam = memchrW(lpszap, ';', dwUrlLength - (lpszap - lpszUrl))))
1472 lpszParam = memchrW(lpszap, '?', dwUrlLength - (lpszap - lpszUrl));
1474 SetUrlComponentValueW(&lpUC->lpszExtraInfo, &lpUC->dwExtraInfoLength,
1475 lpszParam, lpszParam ? dwUrlLength-(lpszParam-lpszUrl) : 0);
1477 if (bIsAbsolute) /* Parse <protocol>:[//<net_loc>] */
1479 LPCWSTR lpszNetLoc;
1481 /* Get scheme first. */
1482 lpUC->nScheme = GetInternetSchemeW(lpszUrl, lpszcp - lpszUrl);
1483 SetUrlComponentValueW(&lpUC->lpszScheme, &lpUC->dwSchemeLength,
1484 lpszUrl, lpszcp - lpszUrl);
1486 /* Eat ':' in protocol. */
1487 lpszcp++;
1489 /* double slash indicates the net_loc portion is present */
1490 if ((lpszcp[0] == '/') && (lpszcp[1] == '/'))
1492 lpszcp += 2;
1494 lpszNetLoc = memchrW(lpszcp, '/', dwUrlLength - (lpszcp - lpszUrl));
1495 if (lpszParam)
1497 if (lpszNetLoc)
1498 lpszNetLoc = min(lpszNetLoc, lpszParam);
1499 else
1500 lpszNetLoc = lpszParam;
1502 else if (!lpszNetLoc)
1503 lpszNetLoc = lpszcp + dwUrlLength-(lpszcp-lpszUrl);
1505 /* Parse net-loc */
1506 if (lpszNetLoc)
1508 LPCWSTR lpszHost;
1509 LPCWSTR lpszPort;
1511 /* [<user>[<:password>]@]<host>[:<port>] */
1512 /* First find the user and password if they exist */
1514 lpszHost = memchrW(lpszcp, '@', dwUrlLength - (lpszcp - lpszUrl));
1515 if (lpszHost == NULL || lpszHost > lpszNetLoc)
1517 /* username and password not specified. */
1518 SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength, NULL, 0);
1519 SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength, NULL, 0);
1521 else /* Parse out username and password */
1523 LPCWSTR lpszUser = lpszcp;
1524 LPCWSTR lpszPasswd = lpszHost;
1526 while (lpszcp < lpszHost)
1528 if (*lpszcp == ':')
1529 lpszPasswd = lpszcp;
1531 lpszcp++;
1534 SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength,
1535 lpszUser, lpszPasswd - lpszUser);
1537 if (lpszPasswd != lpszHost)
1538 lpszPasswd++;
1539 SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength,
1540 lpszPasswd == lpszHost ? NULL : lpszPasswd,
1541 lpszHost - lpszPasswd);
1543 lpszcp++; /* Advance to beginning of host */
1546 /* Parse <host><:port> */
1548 lpszHost = lpszcp;
1549 lpszPort = lpszNetLoc;
1551 /* special case for res:// URLs: there is no port here, so the host is the
1552 entire string up to the first '/' */
1553 if(lpUC->nScheme==INTERNET_SCHEME_RES)
1555 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength,
1556 lpszHost, lpszPort - lpszHost);
1557 lpszcp=lpszNetLoc;
1559 else
1561 while (lpszcp < lpszNetLoc)
1563 if (*lpszcp == ':')
1564 lpszPort = lpszcp;
1566 lpszcp++;
1569 /* If the scheme is "file" and the host is just one letter, it's not a host */
1570 if(lpUC->nScheme==INTERNET_SCHEME_FILE && (lpszPort-lpszHost)==1)
1572 lpszcp=lpszHost;
1573 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength,
1574 NULL, 0);
1576 else
1578 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength,
1579 lpszHost, lpszPort - lpszHost);
1580 if (lpszPort != lpszNetLoc)
1581 lpUC->nPort = atoiW(++lpszPort);
1582 else switch (lpUC->nScheme)
1584 case INTERNET_SCHEME_HTTP:
1585 lpUC->nPort = INTERNET_DEFAULT_HTTP_PORT;
1586 break;
1587 case INTERNET_SCHEME_HTTPS:
1588 lpUC->nPort = INTERNET_DEFAULT_HTTPS_PORT;
1589 break;
1590 case INTERNET_SCHEME_FTP:
1591 lpUC->nPort = INTERNET_DEFAULT_FTP_PORT;
1592 break;
1593 case INTERNET_SCHEME_GOPHER:
1594 lpUC->nPort = INTERNET_DEFAULT_GOPHER_PORT;
1595 break;
1596 default:
1597 break;
1603 else
1605 SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength, NULL, 0);
1606 SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength, NULL, 0);
1607 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength, NULL, 0);
1610 else
1612 SetUrlComponentValueW(&lpUC->lpszScheme, &lpUC->dwSchemeLength, NULL, 0);
1613 SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength, NULL, 0);
1614 SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength, NULL, 0);
1615 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength, NULL, 0);
1618 /* Here lpszcp points to:
1620 * <protocol>:[//<net_loc>][/path][;<params>][?<query>][#<fragment>]
1621 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1623 if (lpszcp != 0 && lpszcp - lpszUrl < dwUrlLength && (!lpszParam || lpszcp < lpszParam))
1625 INT len;
1627 /* Only truncate the parameter list if it's already been saved
1628 * in lpUC->lpszExtraInfo.
1630 if (lpszParam && lpUC->dwExtraInfoLength && lpUC->lpszExtraInfo)
1631 len = lpszParam - lpszcp;
1632 else
1634 /* Leave the parameter list in lpszUrlPath. Strip off any trailing
1635 * newlines if necessary.
1637 LPWSTR lpsznewline = memchrW(lpszcp, '\n', dwUrlLength - (lpszcp - lpszUrl));
1638 if (lpsznewline != NULL)
1639 len = lpsznewline - lpszcp;
1640 else
1641 len = dwUrlLength-(lpszcp-lpszUrl);
1643 SetUrlComponentValueW(&lpUC->lpszUrlPath, &lpUC->dwUrlPathLength,
1644 lpszcp, len);
1646 else
1648 if (lpUC->lpszUrlPath && (lpUC->dwUrlPathLength > 0))
1649 lpUC->lpszUrlPath[0] = 0;
1650 lpUC->dwUrlPathLength = 0;
1653 TRACE("%s: scheme(%s) host(%s) path(%s) extra(%s)\n", debugstr_wn(lpszUrl,dwUrlLength),
1654 debugstr_wn(lpUC->lpszScheme,lpUC->dwSchemeLength),
1655 debugstr_wn(lpUC->lpszHostName,lpUC->dwHostNameLength),
1656 debugstr_wn(lpUC->lpszUrlPath,lpUC->dwUrlPathLength),
1657 debugstr_wn(lpUC->lpszExtraInfo,lpUC->dwExtraInfoLength));
1659 HeapFree(GetProcessHeap(), 0, lpszUrl_decode );
1660 return TRUE;
1663 /***********************************************************************
1664 * InternetAttemptConnect (WININET.@)
1666 * Attempt to make a connection to the internet
1668 * RETURNS
1669 * ERROR_SUCCESS on success
1670 * Error value on failure
1673 DWORD WINAPI InternetAttemptConnect(DWORD dwReserved)
1675 FIXME("Stub\n");
1676 return ERROR_SUCCESS;
1680 /***********************************************************************
1681 * InternetCanonicalizeUrlA (WININET.@)
1683 * Escape unsafe characters and spaces
1685 * RETURNS
1686 * TRUE on success
1687 * FALSE on failure
1690 BOOL WINAPI InternetCanonicalizeUrlA(LPCSTR lpszUrl, LPSTR lpszBuffer,
1691 LPDWORD lpdwBufferLength, DWORD dwFlags)
1693 HRESULT hr;
1694 DWORD dwURLFlags = URL_WININET_COMPATIBILITY | URL_ESCAPE_UNSAFE;
1696 TRACE("(%s, %p, %p, 0x%08x) bufferlength: %d\n", debugstr_a(lpszUrl), lpszBuffer,
1697 lpdwBufferLength, lpdwBufferLength ? *lpdwBufferLength : -1, dwFlags);
1699 if(dwFlags & ICU_DECODE)
1701 dwURLFlags |= URL_UNESCAPE;
1702 dwFlags &= ~ICU_DECODE;
1705 if(dwFlags & ICU_ESCAPE)
1707 dwURLFlags |= URL_UNESCAPE;
1708 dwFlags &= ~ICU_ESCAPE;
1711 if(dwFlags & ICU_BROWSER_MODE)
1713 dwURLFlags |= URL_BROWSER_MODE;
1714 dwFlags &= ~ICU_BROWSER_MODE;
1717 if(dwFlags & ICU_NO_ENCODE)
1719 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
1720 dwURLFlags ^= URL_ESCAPE_UNSAFE;
1721 dwFlags &= ~ICU_NO_ENCODE;
1724 if (dwFlags) FIXME("Unhandled flags 0x%08x\n", dwFlags);
1726 hr = UrlCanonicalizeA(lpszUrl, lpszBuffer, lpdwBufferLength, dwURLFlags);
1727 if (hr == E_POINTER) SetLastError(ERROR_INSUFFICIENT_BUFFER);
1728 if (hr == E_INVALIDARG) SetLastError(ERROR_INVALID_PARAMETER);
1730 return (hr == S_OK) ? TRUE : FALSE;
1733 /***********************************************************************
1734 * InternetCanonicalizeUrlW (WININET.@)
1736 * Escape unsafe characters and spaces
1738 * RETURNS
1739 * TRUE on success
1740 * FALSE on failure
1743 BOOL WINAPI InternetCanonicalizeUrlW(LPCWSTR lpszUrl, LPWSTR lpszBuffer,
1744 LPDWORD lpdwBufferLength, DWORD dwFlags)
1746 HRESULT hr;
1747 DWORD dwURLFlags = URL_WININET_COMPATIBILITY | URL_ESCAPE_UNSAFE;
1749 TRACE("(%s, %p, %p, 0x%08x) bufferlength: %d\n", debugstr_w(lpszUrl), lpszBuffer,
1750 lpdwBufferLength, dwFlags, lpdwBufferLength ? *lpdwBufferLength : -1);
1752 if(dwFlags & ICU_DECODE)
1754 dwURLFlags |= URL_UNESCAPE;
1755 dwFlags &= ~ICU_DECODE;
1758 if(dwFlags & ICU_ESCAPE)
1760 dwURLFlags |= URL_UNESCAPE;
1761 dwFlags &= ~ICU_ESCAPE;
1764 if(dwFlags & ICU_BROWSER_MODE)
1766 dwURLFlags |= URL_BROWSER_MODE;
1767 dwFlags &= ~ICU_BROWSER_MODE;
1770 if(dwFlags & ICU_NO_ENCODE)
1772 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
1773 dwURLFlags ^= URL_ESCAPE_UNSAFE;
1774 dwFlags &= ~ICU_NO_ENCODE;
1777 if (dwFlags) FIXME("Unhandled flags 0x%08x\n", dwFlags);
1779 hr = UrlCanonicalizeW(lpszUrl, lpszBuffer, lpdwBufferLength, dwURLFlags);
1780 if (hr == E_POINTER) SetLastError(ERROR_INSUFFICIENT_BUFFER);
1781 if (hr == E_INVALIDARG) SetLastError(ERROR_INVALID_PARAMETER);
1783 return (hr == S_OK) ? TRUE : FALSE;
1786 /* #################################################### */
1788 static INTERNET_STATUS_CALLBACK set_status_callback(
1789 LPWININETHANDLEHEADER lpwh, INTERNET_STATUS_CALLBACK callback, BOOL unicode)
1791 INTERNET_STATUS_CALLBACK ret;
1793 if (unicode) lpwh->dwInternalFlags |= INET_CALLBACKW;
1794 else lpwh->dwInternalFlags &= ~INET_CALLBACKW;
1796 ret = lpwh->lpfnStatusCB;
1797 lpwh->lpfnStatusCB = callback;
1799 return ret;
1802 /***********************************************************************
1803 * InternetSetStatusCallbackA (WININET.@)
1805 * Sets up a callback function which is called as progress is made
1806 * during an operation.
1808 * RETURNS
1809 * Previous callback or NULL on success
1810 * INTERNET_INVALID_STATUS_CALLBACK on failure
1813 INTERNET_STATUS_CALLBACK WINAPI InternetSetStatusCallbackA(
1814 HINTERNET hInternet ,INTERNET_STATUS_CALLBACK lpfnIntCB)
1816 INTERNET_STATUS_CALLBACK retVal;
1817 LPWININETHANDLEHEADER lpwh;
1819 TRACE("0x%08x\n", (ULONG)hInternet);
1821 if (!(lpwh = WININET_GetObject(hInternet)))
1822 return INTERNET_INVALID_STATUS_CALLBACK;
1824 retVal = set_status_callback(lpwh, lpfnIntCB, FALSE);
1826 WININET_Release( lpwh );
1827 return retVal;
1830 /***********************************************************************
1831 * InternetSetStatusCallbackW (WININET.@)
1833 * Sets up a callback function which is called as progress is made
1834 * during an operation.
1836 * RETURNS
1837 * Previous callback or NULL on success
1838 * INTERNET_INVALID_STATUS_CALLBACK on failure
1841 INTERNET_STATUS_CALLBACK WINAPI InternetSetStatusCallbackW(
1842 HINTERNET hInternet ,INTERNET_STATUS_CALLBACK lpfnIntCB)
1844 INTERNET_STATUS_CALLBACK retVal;
1845 LPWININETHANDLEHEADER lpwh;
1847 TRACE("0x%08x\n", (ULONG)hInternet);
1849 if (!(lpwh = WININET_GetObject(hInternet)))
1850 return INTERNET_INVALID_STATUS_CALLBACK;
1852 retVal = set_status_callback(lpwh, lpfnIntCB, TRUE);
1854 WININET_Release( lpwh );
1855 return retVal;
1858 /***********************************************************************
1859 * InternetSetFilePointer (WININET.@)
1861 DWORD WINAPI InternetSetFilePointer(HINTERNET hFile, LONG lDistanceToMove,
1862 PVOID pReserved, DWORD dwMoveContext, DWORD_PTR dwContext)
1864 FIXME("stub\n");
1865 return FALSE;
1868 /***********************************************************************
1869 * InternetWriteFile (WININET.@)
1871 * Write data to an open internet file
1873 * RETURNS
1874 * TRUE on success
1875 * FALSE on failure
1878 BOOL WINAPI InternetWriteFile(HINTERNET hFile, LPCVOID lpBuffer,
1879 DWORD dwNumOfBytesToWrite, LPDWORD lpdwNumOfBytesWritten)
1881 LPWININETHANDLEHEADER lpwh;
1882 BOOL retval = FALSE;
1884 TRACE("(%p %p %d %p)\n", hFile, lpBuffer, dwNumOfBytesToWrite, lpdwNumOfBytesWritten);
1886 lpwh = WININET_GetObject( hFile );
1887 if (!lpwh) {
1888 WARN("Invalid handle\n");
1889 SetLastError(ERROR_INVALID_HANDLE);
1890 return FALSE;
1893 if(lpwh->vtbl->WriteFile) {
1894 retval = lpwh->vtbl->WriteFile(lpwh, lpBuffer, dwNumOfBytesToWrite, lpdwNumOfBytesWritten);
1895 }else {
1896 WARN("No Writefile method.\n");
1897 SetLastError(ERROR_INVALID_HANDLE);
1898 retval = FALSE;
1901 WININET_Release( lpwh );
1903 return retval;
1907 /***********************************************************************
1908 * InternetReadFile (WININET.@)
1910 * Read data from an open internet file
1912 * RETURNS
1913 * TRUE on success
1914 * FALSE on failure
1917 BOOL WINAPI InternetReadFile(HINTERNET hFile, LPVOID lpBuffer,
1918 DWORD dwNumOfBytesToRead, LPDWORD pdwNumOfBytesRead)
1920 LPWININETHANDLEHEADER hdr;
1921 DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
1923 TRACE("%p %p %d %p\n", hFile, lpBuffer, dwNumOfBytesToRead, pdwNumOfBytesRead);
1925 hdr = WININET_GetObject(hFile);
1926 if (!hdr) {
1927 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
1928 return FALSE;
1931 if(hdr->vtbl->ReadFile)
1932 res = hdr->vtbl->ReadFile(hdr, lpBuffer, dwNumOfBytesToRead, pdwNumOfBytesRead);
1934 WININET_Release(hdr);
1936 TRACE("-- %s (%u) (bytes read: %d)\n", res == ERROR_SUCCESS ? "TRUE": "FALSE", res,
1937 pdwNumOfBytesRead ? *pdwNumOfBytesRead : -1);
1939 if(res != ERROR_SUCCESS)
1940 SetLastError(res);
1941 return res == ERROR_SUCCESS;
1944 /***********************************************************************
1945 * InternetReadFileExA (WININET.@)
1947 * Read data from an open internet file
1949 * PARAMS
1950 * hFile [I] Handle returned by InternetOpenUrl or HttpOpenRequest.
1951 * lpBuffersOut [I/O] Buffer.
1952 * dwFlags [I] Flags. See notes.
1953 * dwContext [I] Context for callbacks.
1955 * RETURNS
1956 * TRUE on success
1957 * FALSE on failure
1959 * NOTES
1960 * The parameter dwFlags include zero or more of the following flags:
1961 *|IRF_ASYNC - Makes the call asynchronous.
1962 *|IRF_SYNC - Makes the call synchronous.
1963 *|IRF_USE_CONTEXT - Forces dwContext to be used.
1964 *|IRF_NO_WAIT - Don't block if the data is not available, just return what is available.
1966 * However, in testing IRF_USE_CONTEXT seems to have no effect - dwContext isn't used.
1968 * SEE
1969 * InternetOpenUrlA(), HttpOpenRequestA()
1971 BOOL WINAPI InternetReadFileExA(HINTERNET hFile, LPINTERNET_BUFFERSA lpBuffersOut,
1972 DWORD dwFlags, DWORD_PTR dwContext)
1974 LPWININETHANDLEHEADER hdr;
1975 DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
1977 TRACE("(%p %p 0x%x 0x%lx)\n", hFile, lpBuffersOut, dwFlags, dwContext);
1979 hdr = WININET_GetObject(hFile);
1980 if (!hdr) {
1981 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
1982 return FALSE;
1985 if(hdr->vtbl->ReadFileExA)
1986 res = hdr->vtbl->ReadFileExA(hdr, lpBuffersOut, dwFlags, dwContext);
1988 WININET_Release(hdr);
1990 TRACE("-- %s (%u, bytes read: %d)\n", res == ERROR_SUCCESS ? "TRUE": "FALSE",
1991 res, lpBuffersOut->dwBufferLength);
1993 if(res != ERROR_SUCCESS)
1994 SetLastError(res);
1995 return res == ERROR_SUCCESS;
1998 /***********************************************************************
1999 * InternetReadFileExW (WININET.@)
2001 * Read data from an open internet file.
2003 * PARAMS
2004 * hFile [I] Handle returned by InternetOpenUrl() or HttpOpenRequest().
2005 * lpBuffersOut [I/O] Buffer.
2006 * dwFlags [I] Flags.
2007 * dwContext [I] Context for callbacks.
2009 * RETURNS
2010 * FALSE, last error is set to ERROR_CALL_NOT_IMPLEMENTED
2012 * NOTES
2013 * Not implemented in Wine or native either (as of IE6 SP2).
2016 BOOL WINAPI InternetReadFileExW(HINTERNET hFile, LPINTERNET_BUFFERSW lpBuffer,
2017 DWORD dwFlags, DWORD_PTR dwContext)
2019 ERR("(%p, %p, 0x%x, 0x%lx): not implemented in native\n", hFile, lpBuffer, dwFlags, dwContext);
2021 INTERNET_SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2022 return FALSE;
2025 DWORD INET_QueryOption(DWORD option, void *buffer, DWORD *size, BOOL unicode)
2027 switch(option) {
2028 case INTERNET_OPTION_REQUEST_FLAGS:
2029 TRACE("INTERNET_OPTION_REQUEST_FLAGS\n");
2031 if (*size < sizeof(ULONG))
2032 return ERROR_INSUFFICIENT_BUFFER;
2034 *(ULONG*)buffer = 4;
2035 *size = sizeof(ULONG);
2037 return ERROR_SUCCESS;
2039 case INTERNET_OPTION_HTTP_VERSION:
2040 if (*size < sizeof(HTTP_VERSION_INFO))
2041 return ERROR_INSUFFICIENT_BUFFER;
2044 * Presently hardcoded to 1.1
2046 ((HTTP_VERSION_INFO*)buffer)->dwMajorVersion = 1;
2047 ((HTTP_VERSION_INFO*)buffer)->dwMinorVersion = 1;
2048 *size = sizeof(HTTP_VERSION_INFO);
2050 return ERROR_SUCCESS;
2052 case INTERNET_OPTION_CONNECTED_STATE:
2053 FIXME("INTERNET_OPTION_CONNECTED_STATE: semi-stub\n");
2055 if (*size < sizeof(ULONG))
2056 return ERROR_INSUFFICIENT_BUFFER;
2058 *(ULONG*)buffer = INTERNET_STATE_CONNECTED;
2059 *size = sizeof(ULONG);
2061 return ERROR_SUCCESS;
2063 case INTERNET_OPTION_PROXY: {
2064 WININETAPPINFOW ai;
2066 TRACE("Getting global proxy info\n");
2067 memset(&ai, 0, sizeof(WININETAPPINFOW));
2068 INTERNET_ConfigureProxy(&ai);
2070 return APPINFO_QueryOption(&ai.hdr, INTERNET_OPTION_PROXY, buffer, size, unicode); /* FIXME */
2073 case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2074 TRACE("INTERNET_OPTION_MAX_CONNS_PER_SERVER\n");
2076 if (*size < sizeof(ULONG))
2077 return ERROR_INSUFFICIENT_BUFFER;
2079 *(ULONG*)buffer = 2;
2080 *size = sizeof(ULONG);
2082 return ERROR_SUCCESS;
2084 case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2085 TRACE("INTERNET_OPTION_MAX_CONNS_1_0_SERVER\n");
2087 if (*size < sizeof(ULONG))
2088 return ERROR_INSUFFICIENT_BUFFER;
2090 *(ULONG*)size = 4;
2091 *size = sizeof(ULONG);
2093 return ERROR_SUCCESS;
2095 case INTERNET_OPTION_SECURITY_FLAGS:
2096 FIXME("INTERNET_OPTION_SECURITY_FLAGS: Stub\n");
2097 return ERROR_SUCCESS;
2099 case INTERNET_OPTION_VERSION: {
2100 static const INTERNET_VERSION_INFO info = { 1, 2 };
2102 TRACE("INTERNET_OPTION_VERSION\n");
2104 if (*size < sizeof(INTERNET_VERSION_INFO))
2105 return ERROR_INSUFFICIENT_BUFFER;
2107 memcpy(buffer, &info, sizeof(info));
2108 *size = sizeof(info);
2110 return ERROR_SUCCESS;
2113 case INTERNET_OPTION_PER_CONNECTION_OPTION: {
2114 INTERNET_PER_CONN_OPTION_LISTW *con = buffer;
2115 DWORD res = ERROR_SUCCESS, i;
2117 FIXME("INTERNET_OPTION_PER_CONNECTION_OPTION stub\n");
2119 if (*size < sizeof(INTERNET_PER_CONN_OPTION_LISTW))
2120 return ERROR_INSUFFICIENT_BUFFER;
2122 for (i = 0; i < con->dwOptionCount; i++) {
2123 INTERNET_PER_CONN_OPTIONW *option = con->pOptions + i;
2125 switch (option->dwOption) {
2126 case INTERNET_PER_CONN_FLAGS:
2127 option->Value.dwValue = PROXY_TYPE_DIRECT;
2128 break;
2130 case INTERNET_PER_CONN_PROXY_SERVER:
2131 case INTERNET_PER_CONN_PROXY_BYPASS:
2132 case INTERNET_PER_CONN_AUTOCONFIG_URL:
2133 case INTERNET_PER_CONN_AUTODISCOVERY_FLAGS:
2134 case INTERNET_PER_CONN_AUTOCONFIG_SECONDARY_URL:
2135 case INTERNET_PER_CONN_AUTOCONFIG_RELOAD_DELAY_MINS:
2136 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_TIME:
2137 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_URL:
2138 FIXME("Unhandled dwOption %d\n", option->dwOption);
2139 option->Value.dwValue = 0;
2140 res = ERROR_INVALID_PARAMETER;
2141 break;
2143 default:
2144 FIXME("Unknown dwOption %d\n", option->dwOption);
2145 res = ERROR_INVALID_PARAMETER;
2146 break;
2150 return res;
2154 FIXME("Stub for %d\n", option);
2155 return ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2158 /***********************************************************************
2159 * InternetQueryOptionW (WININET.@)
2161 * Queries an options on the specified handle
2163 * RETURNS
2164 * TRUE on success
2165 * FALSE on failure
2168 BOOL WINAPI InternetQueryOptionW(HINTERNET hInternet, DWORD dwOption,
2169 LPVOID lpBuffer, LPDWORD lpdwBufferLength)
2171 LPWININETHANDLEHEADER hdr;
2172 DWORD res = ERROR_INVALID_HANDLE;
2174 TRACE("%p %d %p %p\n", hInternet, dwOption, lpBuffer, lpdwBufferLength);
2176 if(hInternet) {
2177 hdr = WININET_GetObject(hInternet);
2178 if (hdr) {
2179 res = hdr->vtbl->QueryOption(hdr, dwOption, lpBuffer, lpdwBufferLength, TRUE);
2180 WININET_Release(hdr);
2182 }else {
2183 res = INET_QueryOption(dwOption, lpBuffer, lpdwBufferLength, TRUE);
2186 if(res != ERROR_SUCCESS)
2187 SetLastError(res);
2188 return res == ERROR_SUCCESS;
2191 /***********************************************************************
2192 * InternetQueryOptionA (WININET.@)
2194 * Queries an options on the specified handle
2196 * RETURNS
2197 * TRUE on success
2198 * FALSE on failure
2201 BOOL WINAPI InternetQueryOptionA(HINTERNET hInternet, DWORD dwOption,
2202 LPVOID lpBuffer, LPDWORD lpdwBufferLength)
2204 LPWININETHANDLEHEADER hdr;
2205 DWORD res = ERROR_INVALID_HANDLE;
2207 TRACE("%p %d %p %p\n", hInternet, dwOption, lpBuffer, lpdwBufferLength);
2209 if(hInternet) {
2210 hdr = WININET_GetObject(hInternet);
2211 if (hdr) {
2212 res = hdr->vtbl->QueryOption(hdr, dwOption, lpBuffer, lpdwBufferLength, FALSE);
2213 WININET_Release(hdr);
2215 }else {
2216 res = INET_QueryOption(dwOption, lpBuffer, lpdwBufferLength, FALSE);
2219 if(res != ERROR_SUCCESS)
2220 SetLastError(res);
2221 return res == ERROR_SUCCESS;
2225 /***********************************************************************
2226 * InternetSetOptionW (WININET.@)
2228 * Sets an options on the specified handle
2230 * RETURNS
2231 * TRUE on success
2232 * FALSE on failure
2235 BOOL WINAPI InternetSetOptionW(HINTERNET hInternet, DWORD dwOption,
2236 LPVOID lpBuffer, DWORD dwBufferLength)
2238 LPWININETHANDLEHEADER lpwhh;
2239 BOOL ret = TRUE;
2241 TRACE("(%p %d %p %d)\n", hInternet, dwOption, lpBuffer, dwBufferLength);
2243 lpwhh = (LPWININETHANDLEHEADER) WININET_GetObject( hInternet );
2244 if(lpwhh && lpwhh->vtbl->SetOption) {
2245 DWORD res;
2247 res = lpwhh->vtbl->SetOption(lpwhh, dwOption, lpBuffer, dwBufferLength);
2248 if(res != ERROR_INTERNET_INVALID_OPTION) {
2249 WININET_Release( lpwhh );
2251 if(res != ERROR_SUCCESS)
2252 SetLastError(res);
2254 return res == ERROR_SUCCESS;
2258 switch (dwOption)
2260 case INTERNET_OPTION_CALLBACK:
2262 INTERNET_STATUS_CALLBACK callback = *(INTERNET_STATUS_CALLBACK *)lpBuffer;
2263 ret = (set_status_callback(lpwhh, callback, TRUE) != INTERNET_INVALID_STATUS_CALLBACK);
2264 break;
2266 case INTERNET_OPTION_HTTP_VERSION:
2268 HTTP_VERSION_INFO* pVersion=(HTTP_VERSION_INFO*)lpBuffer;
2269 FIXME("Option INTERNET_OPTION_HTTP_VERSION(%d,%d): STUB\n",pVersion->dwMajorVersion,pVersion->dwMinorVersion);
2271 break;
2272 case INTERNET_OPTION_ERROR_MASK:
2274 ULONG flags = *(ULONG *)lpBuffer;
2275 FIXME("Option INTERNET_OPTION_ERROR_MASK(%d): STUB\n", flags);
2277 break;
2278 case INTERNET_OPTION_CODEPAGE:
2280 ULONG codepage = *(ULONG *)lpBuffer;
2281 FIXME("Option INTERNET_OPTION_CODEPAGE (%d): STUB\n", codepage);
2283 break;
2284 case INTERNET_OPTION_REQUEST_PRIORITY:
2286 ULONG priority = *(ULONG *)lpBuffer;
2287 FIXME("Option INTERNET_OPTION_REQUEST_PRIORITY (%d): STUB\n", priority);
2289 break;
2290 case INTERNET_OPTION_CONNECT_TIMEOUT:
2292 ULONG connecttimeout = *(ULONG *)lpBuffer;
2293 FIXME("Option INTERNET_OPTION_CONNECT_TIMEOUT (%d): STUB\n", connecttimeout);
2295 break;
2296 case INTERNET_OPTION_DATA_RECEIVE_TIMEOUT:
2298 ULONG receivetimeout = *(ULONG *)lpBuffer;
2299 FIXME("Option INTERNET_OPTION_DATA_RECEIVE_TIMEOUT (%d): STUB\n", receivetimeout);
2301 break;
2302 case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2304 ULONG conns = *(ULONG *)lpBuffer;
2305 FIXME("Option INTERNET_OPTION_MAX_CONNS_PER_SERVER (%d): STUB\n", conns);
2307 break;
2308 case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2310 ULONG conns = *(ULONG *)lpBuffer;
2311 FIXME("Option INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER (%d): STUB\n", conns);
2313 break;
2314 case INTERNET_OPTION_RESET_URLCACHE_SESSION:
2315 FIXME("Option INTERNET_OPTION_RESET_URLCACHE_SESSION: STUB\n");
2316 break;
2317 case INTERNET_OPTION_END_BROWSER_SESSION:
2318 FIXME("Option INTERNET_OPTION_END_BROWSER_SESSION: STUB\n");
2319 break;
2320 case INTERNET_OPTION_CONNECTED_STATE:
2321 FIXME("Option INTERNET_OPTION_CONNECTED_STATE: STUB\n");
2322 break;
2323 case INTERNET_OPTION_DISABLE_PASSPORT_AUTH:
2324 TRACE("Option INTERNET_OPTION_DISABLE_PASSPORT_AUTH: harmless stub, since not enabled\n");
2325 break;
2326 case INTERNET_OPTION_SEND_TIMEOUT:
2327 case INTERNET_OPTION_RECEIVE_TIMEOUT:
2329 ULONG timeout = *(ULONG *)lpBuffer;
2330 FIXME("INTERNET_OPTION_SEND/RECEIVE_TIMEOUT %d\n", timeout);
2331 break;
2333 case INTERNET_OPTION_CONNECT_RETRIES:
2335 ULONG retries = *(ULONG *)lpBuffer;
2336 FIXME("INTERNET_OPTION_CONNECT_RETRIES %d\n", retries);
2337 break;
2339 case INTERNET_OPTION_CONTEXT_VALUE:
2340 FIXME("Option INTERNET_OPTION_CONTEXT_VALUE; STUB\n");
2341 break;
2342 case INTERNET_OPTION_SECURITY_FLAGS:
2343 FIXME("Option INTERNET_OPTION_SECURITY_FLAGS; STUB\n");
2344 break;
2345 case INTERNET_OPTION_DISABLE_AUTODIAL:
2346 FIXME("Option INTERNET_OPTION_DISABLE_AUTODIAL; STUB\n");
2347 break;
2348 case 86:
2349 FIXME("86\n");
2350 break;
2351 default:
2352 FIXME("Option %d STUB\n",dwOption);
2353 INTERNET_SetLastError(ERROR_INTERNET_INVALID_OPTION);
2354 ret = FALSE;
2355 break;
2358 if(lpwhh)
2359 WININET_Release( lpwhh );
2361 return ret;
2365 /***********************************************************************
2366 * InternetSetOptionA (WININET.@)
2368 * Sets an options on the specified handle.
2370 * RETURNS
2371 * TRUE on success
2372 * FALSE on failure
2375 BOOL WINAPI InternetSetOptionA(HINTERNET hInternet, DWORD dwOption,
2376 LPVOID lpBuffer, DWORD dwBufferLength)
2378 LPVOID wbuffer;
2379 DWORD wlen;
2380 BOOL r;
2382 switch( dwOption )
2384 case INTERNET_OPTION_CALLBACK:
2386 LPWININETHANDLEHEADER lpwh;
2387 INTERNET_STATUS_CALLBACK callback = *(INTERNET_STATUS_CALLBACK *)lpBuffer;
2389 if (!(lpwh = WININET_GetObject(hInternet))) return FALSE;
2390 r = (set_status_callback(lpwh, callback, FALSE) != INTERNET_INVALID_STATUS_CALLBACK);
2391 WININET_Release(lpwh);
2392 return r;
2394 case INTERNET_OPTION_PROXY:
2396 LPINTERNET_PROXY_INFOA pi = (LPINTERNET_PROXY_INFOA) lpBuffer;
2397 LPINTERNET_PROXY_INFOW piw;
2398 DWORD proxlen, prbylen;
2399 LPWSTR prox, prby;
2401 proxlen = MultiByteToWideChar( CP_ACP, 0, pi->lpszProxy, -1, NULL, 0);
2402 prbylen= MultiByteToWideChar( CP_ACP, 0, pi->lpszProxyBypass, -1, NULL, 0);
2403 wlen = sizeof(*piw) + proxlen + prbylen;
2404 wbuffer = HeapAlloc( GetProcessHeap(), 0, wlen*sizeof(WCHAR) );
2405 piw = (LPINTERNET_PROXY_INFOW) wbuffer;
2406 piw->dwAccessType = pi->dwAccessType;
2407 prox = (LPWSTR) &piw[1];
2408 prby = &prox[proxlen+1];
2409 MultiByteToWideChar( CP_ACP, 0, pi->lpszProxy, -1, prox, proxlen);
2410 MultiByteToWideChar( CP_ACP, 0, pi->lpszProxyBypass, -1, prby, prbylen);
2411 piw->lpszProxy = prox;
2412 piw->lpszProxyBypass = prby;
2414 break;
2415 case INTERNET_OPTION_USER_AGENT:
2416 case INTERNET_OPTION_USERNAME:
2417 case INTERNET_OPTION_PASSWORD:
2418 wlen = MultiByteToWideChar( CP_ACP, 0, lpBuffer, dwBufferLength,
2419 NULL, 0 );
2420 wbuffer = HeapAlloc( GetProcessHeap(), 0, wlen*sizeof(WCHAR) );
2421 MultiByteToWideChar( CP_ACP, 0, lpBuffer, dwBufferLength,
2422 wbuffer, wlen );
2423 break;
2424 default:
2425 wbuffer = lpBuffer;
2426 wlen = dwBufferLength;
2429 r = InternetSetOptionW(hInternet,dwOption, wbuffer, wlen);
2431 if( lpBuffer != wbuffer )
2432 HeapFree( GetProcessHeap(), 0, wbuffer );
2434 return r;
2438 /***********************************************************************
2439 * InternetSetOptionExA (WININET.@)
2441 BOOL WINAPI InternetSetOptionExA(HINTERNET hInternet, DWORD dwOption,
2442 LPVOID lpBuffer, DWORD dwBufferLength, DWORD dwFlags)
2444 FIXME("Flags %08x ignored\n", dwFlags);
2445 return InternetSetOptionA( hInternet, dwOption, lpBuffer, dwBufferLength );
2448 /***********************************************************************
2449 * InternetSetOptionExW (WININET.@)
2451 BOOL WINAPI InternetSetOptionExW(HINTERNET hInternet, DWORD dwOption,
2452 LPVOID lpBuffer, DWORD dwBufferLength, DWORD dwFlags)
2454 FIXME("Flags %08x ignored\n", dwFlags);
2455 if( dwFlags & ~ISO_VALID_FLAGS )
2457 INTERNET_SetLastError( ERROR_INVALID_PARAMETER );
2458 return FALSE;
2460 return InternetSetOptionW( hInternet, dwOption, lpBuffer, dwBufferLength );
2463 static const WCHAR WININET_wkday[7][4] =
2464 { { 'S','u','n', 0 }, { 'M','o','n', 0 }, { 'T','u','e', 0 }, { 'W','e','d', 0 },
2465 { 'T','h','u', 0 }, { 'F','r','i', 0 }, { 'S','a','t', 0 } };
2466 static const WCHAR WININET_month[12][4] =
2467 { { 'J','a','n', 0 }, { 'F','e','b', 0 }, { 'M','a','r', 0 }, { 'A','p','r', 0 },
2468 { 'M','a','y', 0 }, { 'J','u','n', 0 }, { 'J','u','l', 0 }, { 'A','u','g', 0 },
2469 { 'S','e','p', 0 }, { 'O','c','t', 0 }, { 'N','o','v', 0 }, { 'D','e','c', 0 } };
2471 /***********************************************************************
2472 * InternetTimeFromSystemTimeA (WININET.@)
2474 BOOL WINAPI InternetTimeFromSystemTimeA( const SYSTEMTIME* time, DWORD format, LPSTR string, DWORD size )
2476 BOOL ret;
2477 WCHAR stringW[INTERNET_RFC1123_BUFSIZE];
2479 TRACE( "%p 0x%08x %p 0x%08x\n", time, format, string, size );
2481 ret = InternetTimeFromSystemTimeW( time, format, stringW, sizeof(stringW) );
2482 if (ret) WideCharToMultiByte( CP_ACP, 0, stringW, -1, string, size, NULL, NULL );
2484 return ret;
2487 /***********************************************************************
2488 * InternetTimeFromSystemTimeW (WININET.@)
2490 BOOL WINAPI InternetTimeFromSystemTimeW( const SYSTEMTIME* time, DWORD format, LPWSTR string, DWORD size )
2492 static const WCHAR date[] =
2493 { '%','s',',',' ','%','0','2','d',' ','%','s',' ','%','4','d',' ','%','0',
2494 '2','d',':','%','0','2','d',':','%','0','2','d',' ','G','M','T', 0 };
2496 TRACE( "%p 0x%08x %p 0x%08x\n", time, format, string, size );
2498 if (!time || !string) return FALSE;
2500 if (format != INTERNET_RFC1123_FORMAT || size < INTERNET_RFC1123_BUFSIZE * sizeof(WCHAR))
2501 return FALSE;
2503 sprintfW( string, date,
2504 WININET_wkday[time->wDayOfWeek],
2505 time->wDay,
2506 WININET_month[time->wMonth - 1],
2507 time->wYear,
2508 time->wHour,
2509 time->wMinute,
2510 time->wSecond );
2512 return TRUE;
2515 /***********************************************************************
2516 * InternetTimeToSystemTimeA (WININET.@)
2518 BOOL WINAPI InternetTimeToSystemTimeA( LPCSTR string, SYSTEMTIME* time, DWORD reserved )
2520 BOOL ret = FALSE;
2521 WCHAR *stringW;
2522 int len;
2524 TRACE( "%s %p 0x%08x\n", debugstr_a(string), time, reserved );
2526 len = MultiByteToWideChar( CP_ACP, 0, string, -1, NULL, 0 );
2527 stringW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
2529 if (stringW)
2531 MultiByteToWideChar( CP_ACP, 0, string, -1, stringW, len );
2532 ret = InternetTimeToSystemTimeW( stringW, time, reserved );
2533 HeapFree( GetProcessHeap(), 0, stringW );
2535 return ret;
2538 /***********************************************************************
2539 * InternetTimeToSystemTimeW (WININET.@)
2541 BOOL WINAPI InternetTimeToSystemTimeW( LPCWSTR string, SYSTEMTIME* time, DWORD reserved )
2543 unsigned int i;
2544 const WCHAR *s = string;
2545 WCHAR *end;
2547 TRACE( "%s %p 0x%08x\n", debugstr_w(string), time, reserved );
2549 if (!string || !time) return FALSE;
2551 /* Windows does this too */
2552 GetSystemTime( time );
2554 /* Convert an RFC1123 time such as 'Fri, 07 Jan 2005 12:06:35 GMT' into
2555 * a SYSTEMTIME structure.
2558 while (*s && !isalphaW( *s )) s++;
2559 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
2560 time->wDayOfWeek = 7;
2562 for (i = 0; i < 7; i++)
2564 if (toupperW( WININET_wkday[i][0] ) == toupperW( s[0] ) &&
2565 toupperW( WININET_wkday[i][1] ) == toupperW( s[1] ) &&
2566 toupperW( WININET_wkday[i][2] ) == toupperW( s[2] ) )
2568 time->wDayOfWeek = i;
2569 break;
2573 if (time->wDayOfWeek > 6) return TRUE;
2574 while (*s && !isdigitW( *s )) s++;
2575 time->wDay = strtolW( s, &end, 10 );
2576 s = end;
2578 while (*s && !isalphaW( *s )) s++;
2579 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
2580 time->wMonth = 0;
2582 for (i = 0; i < 12; i++)
2584 if (toupperW( WININET_month[i][0]) == toupperW( s[0] ) &&
2585 toupperW( WININET_month[i][1]) == toupperW( s[1] ) &&
2586 toupperW( WININET_month[i][2]) == toupperW( s[2] ) )
2588 time->wMonth = i + 1;
2589 break;
2592 if (time->wMonth == 0) return TRUE;
2594 while (*s && !isdigitW( *s )) s++;
2595 if (*s == '\0') return TRUE;
2596 time->wYear = strtolW( s, &end, 10 );
2597 s = end;
2599 while (*s && !isdigitW( *s )) s++;
2600 if (*s == '\0') return TRUE;
2601 time->wHour = strtolW( s, &end, 10 );
2602 s = end;
2604 while (*s && !isdigitW( *s )) s++;
2605 if (*s == '\0') return TRUE;
2606 time->wMinute = strtolW( s, &end, 10 );
2607 s = end;
2609 while (*s && !isdigitW( *s )) s++;
2610 if (*s == '\0') return TRUE;
2611 time->wSecond = strtolW( s, &end, 10 );
2612 s = end;
2614 time->wMilliseconds = 0;
2615 return TRUE;
2618 /***********************************************************************
2619 * InternetCheckConnectionW (WININET.@)
2621 * Pings a requested host to check internet connection
2623 * RETURNS
2624 * TRUE on success and FALSE on failure. If a failure then
2625 * ERROR_NOT_CONNECTED is placed into GetLastError
2628 BOOL WINAPI InternetCheckConnectionW( LPCWSTR lpszUrl, DWORD dwFlags, DWORD dwReserved )
2631 * this is a kludge which runs the resident ping program and reads the output.
2633 * Anyone have a better idea?
2636 BOOL rc = FALSE;
2637 static const CHAR ping[] = "ping -c 1 ";
2638 static const CHAR redirect[] = " >/dev/null 2>/dev/null";
2639 CHAR *command = NULL;
2640 WCHAR hostW[1024];
2641 DWORD len;
2642 INTERNET_PORT port;
2643 int status = -1;
2645 FIXME("\n");
2648 * Crack or set the Address
2650 if (lpszUrl == NULL)
2653 * According to the doc we are supposed to use the ip for the next
2654 * server in the WnInet internal server database. I have
2655 * no idea what that is or how to get it.
2657 * So someone needs to implement this.
2659 FIXME("Unimplemented with URL of NULL\n");
2660 return TRUE;
2662 else
2664 URL_COMPONENTSW components;
2666 ZeroMemory(&components,sizeof(URL_COMPONENTSW));
2667 components.lpszHostName = (LPWSTR)hostW;
2668 components.dwHostNameLength = 1024;
2670 if (!InternetCrackUrlW(lpszUrl,0,0,&components))
2671 goto End;
2673 TRACE("host name : %s\n",debugstr_w(components.lpszHostName));
2674 port = components.nPort;
2675 TRACE("port: %d\n", port);
2678 if (dwFlags & FLAG_ICC_FORCE_CONNECTION)
2680 struct sockaddr_in sin;
2681 int fd;
2683 if (!GetAddress(hostW, port, &sin))
2684 goto End;
2685 fd = socket(sin.sin_family, SOCK_STREAM, 0);
2686 if (fd != -1)
2688 if (connect(fd, (struct sockaddr *)&sin, sizeof(sin)) == 0)
2689 rc = TRUE;
2690 close(fd);
2693 else
2696 * Build our ping command
2698 len = WideCharToMultiByte(CP_UNIXCP, 0, hostW, -1, NULL, 0, NULL, NULL);
2699 command = HeapAlloc( GetProcessHeap(), 0, strlen(ping)+len+strlen(redirect) );
2700 strcpy(command,ping);
2701 WideCharToMultiByte(CP_UNIXCP, 0, hostW, -1, command+strlen(ping), len, NULL, NULL);
2702 strcat(command,redirect);
2704 TRACE("Ping command is : %s\n",command);
2706 status = system(command);
2708 TRACE("Ping returned a code of %i\n",status);
2710 /* Ping return code of 0 indicates success */
2711 if (status == 0)
2712 rc = TRUE;
2715 End:
2717 HeapFree( GetProcessHeap(), 0, command );
2718 if (rc == FALSE)
2719 INTERNET_SetLastError(ERROR_NOT_CONNECTED);
2721 return rc;
2725 /***********************************************************************
2726 * InternetCheckConnectionA (WININET.@)
2728 * Pings a requested host to check internet connection
2730 * RETURNS
2731 * TRUE on success and FALSE on failure. If a failure then
2732 * ERROR_NOT_CONNECTED is placed into GetLastError
2735 BOOL WINAPI InternetCheckConnectionA(LPCSTR lpszUrl, DWORD dwFlags, DWORD dwReserved)
2737 WCHAR *szUrl;
2738 INT len;
2739 BOOL rc;
2741 len = MultiByteToWideChar(CP_ACP, 0, lpszUrl, -1, NULL, 0);
2742 if (!(szUrl = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR))))
2743 return FALSE;
2744 MultiByteToWideChar(CP_ACP, 0, lpszUrl, -1, szUrl, len);
2745 rc = InternetCheckConnectionW(szUrl, dwFlags, dwReserved);
2746 HeapFree(GetProcessHeap(), 0, szUrl);
2748 return rc;
2752 /**********************************************************
2753 * INTERNET_InternetOpenUrlW (internal)
2755 * Opens an URL
2757 * RETURNS
2758 * handle of connection or NULL on failure
2760 static HINTERNET INTERNET_InternetOpenUrlW(LPWININETAPPINFOW hIC, LPCWSTR lpszUrl,
2761 LPCWSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext)
2763 URL_COMPONENTSW urlComponents;
2764 WCHAR protocol[32], hostName[MAXHOSTNAME], userName[1024];
2765 WCHAR password[1024], path[2048], extra[1024];
2766 HINTERNET client = NULL, client1 = NULL;
2768 TRACE("(%p, %s, %s, %08x, %08x, %08lx)\n", hIC, debugstr_w(lpszUrl), debugstr_w(lpszHeaders),
2769 dwHeadersLength, dwFlags, dwContext);
2771 urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
2772 urlComponents.lpszScheme = protocol;
2773 urlComponents.dwSchemeLength = 32;
2774 urlComponents.lpszHostName = hostName;
2775 urlComponents.dwHostNameLength = MAXHOSTNAME;
2776 urlComponents.lpszUserName = userName;
2777 urlComponents.dwUserNameLength = 1024;
2778 urlComponents.lpszPassword = password;
2779 urlComponents.dwPasswordLength = 1024;
2780 urlComponents.lpszUrlPath = path;
2781 urlComponents.dwUrlPathLength = 2048;
2782 urlComponents.lpszExtraInfo = extra;
2783 urlComponents.dwExtraInfoLength = 1024;
2784 if(!InternetCrackUrlW(lpszUrl, strlenW(lpszUrl), 0, &urlComponents))
2785 return NULL;
2786 switch(urlComponents.nScheme) {
2787 case INTERNET_SCHEME_FTP:
2788 if(urlComponents.nPort == 0)
2789 urlComponents.nPort = INTERNET_DEFAULT_FTP_PORT;
2790 client = FTP_Connect(hIC, hostName, urlComponents.nPort,
2791 userName, password, dwFlags, dwContext, INET_OPENURL);
2792 if(client == NULL)
2793 break;
2794 client1 = FtpOpenFileW(client, path, GENERIC_READ, dwFlags, dwContext);
2795 if(client1 == NULL) {
2796 InternetCloseHandle(client);
2797 break;
2799 break;
2801 case INTERNET_SCHEME_HTTP:
2802 case INTERNET_SCHEME_HTTPS: {
2803 static const WCHAR szStars[] = { '*','/','*', 0 };
2804 LPCWSTR accept[2] = { szStars, NULL };
2805 if(urlComponents.nPort == 0) {
2806 if(urlComponents.nScheme == INTERNET_SCHEME_HTTP)
2807 urlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
2808 else
2809 urlComponents.nPort = INTERNET_DEFAULT_HTTPS_PORT;
2811 /* FIXME: should use pointers, not handles, as handles are not thread-safe */
2812 client = HTTP_Connect(hIC, hostName, urlComponents.nPort,
2813 userName, password, dwFlags, dwContext, INET_OPENURL);
2814 if(client == NULL)
2815 break;
2817 if (urlComponents.dwExtraInfoLength) {
2818 WCHAR *path_extra;
2819 DWORD len = urlComponents.dwUrlPathLength + urlComponents.dwExtraInfoLength + 1;
2821 if (!(path_extra = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR))))
2823 InternetCloseHandle(client);
2824 break;
2826 strcpyW(path_extra, urlComponents.lpszUrlPath);
2827 strcatW(path_extra, urlComponents.lpszExtraInfo);
2828 client1 = HttpOpenRequestW(client, NULL, path_extra, NULL, NULL, accept, dwFlags, dwContext);
2829 HeapFree(GetProcessHeap(), 0, path_extra);
2831 else
2832 client1 = HttpOpenRequestW(client, NULL, path, NULL, NULL, accept, dwFlags, dwContext);
2834 if(client1 == NULL) {
2835 InternetCloseHandle(client);
2836 break;
2838 HttpAddRequestHeadersW(client1, lpszHeaders, dwHeadersLength, HTTP_ADDREQ_FLAG_ADD);
2839 if (!HttpSendRequestW(client1, NULL, 0, NULL, 0) &&
2840 GetLastError() != ERROR_IO_PENDING) {
2841 InternetCloseHandle(client1);
2842 client1 = NULL;
2843 break;
2846 case INTERNET_SCHEME_GOPHER:
2847 /* gopher doesn't seem to be implemented in wine, but it's supposed
2848 * to be supported by InternetOpenUrlA. */
2849 default:
2850 INTERNET_SetLastError(ERROR_INTERNET_UNRECOGNIZED_SCHEME);
2851 break;
2854 TRACE(" %p <--\n", client1);
2856 return client1;
2859 /**********************************************************
2860 * InternetOpenUrlW (WININET.@)
2862 * Opens an URL
2864 * RETURNS
2865 * handle of connection or NULL on failure
2867 static void AsyncInternetOpenUrlProc(WORKREQUEST *workRequest)
2869 struct WORKREQ_INTERNETOPENURLW const *req = &workRequest->u.InternetOpenUrlW;
2870 LPWININETAPPINFOW hIC = (LPWININETAPPINFOW) workRequest->hdr;
2872 TRACE("%p\n", hIC);
2874 INTERNET_InternetOpenUrlW(hIC, req->lpszUrl,
2875 req->lpszHeaders, req->dwHeadersLength, req->dwFlags, req->dwContext);
2876 HeapFree(GetProcessHeap(), 0, req->lpszUrl);
2877 HeapFree(GetProcessHeap(), 0, req->lpszHeaders);
2880 HINTERNET WINAPI InternetOpenUrlW(HINTERNET hInternet, LPCWSTR lpszUrl,
2881 LPCWSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext)
2883 HINTERNET ret = NULL;
2884 LPWININETAPPINFOW hIC = NULL;
2886 if (TRACE_ON(wininet)) {
2887 TRACE("(%p, %s, %s, %08x, %08x, %08lx)\n", hInternet, debugstr_w(lpszUrl), debugstr_w(lpszHeaders),
2888 dwHeadersLength, dwFlags, dwContext);
2889 TRACE(" flags :");
2890 dump_INTERNET_FLAGS(dwFlags);
2893 if (!lpszUrl)
2895 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
2896 goto lend;
2899 hIC = (LPWININETAPPINFOW) WININET_GetObject( hInternet );
2900 if (NULL == hIC || hIC->hdr.htype != WH_HINIT) {
2901 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2902 goto lend;
2905 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC) {
2906 WORKREQUEST workRequest;
2907 struct WORKREQ_INTERNETOPENURLW *req;
2909 workRequest.asyncproc = AsyncInternetOpenUrlProc;
2910 workRequest.hdr = WININET_AddRef( &hIC->hdr );
2911 req = &workRequest.u.InternetOpenUrlW;
2912 req->lpszUrl = WININET_strdupW(lpszUrl);
2913 if (lpszHeaders)
2914 req->lpszHeaders = WININET_strdupW(lpszHeaders);
2915 else
2916 req->lpszHeaders = 0;
2917 req->dwHeadersLength = dwHeadersLength;
2918 req->dwFlags = dwFlags;
2919 req->dwContext = dwContext;
2921 INTERNET_AsyncCall(&workRequest);
2923 * This is from windows.
2925 INTERNET_SetLastError(ERROR_IO_PENDING);
2926 } else {
2927 ret = INTERNET_InternetOpenUrlW(hIC, lpszUrl, lpszHeaders, dwHeadersLength, dwFlags, dwContext);
2930 lend:
2931 if( hIC )
2932 WININET_Release( &hIC->hdr );
2933 TRACE(" %p <--\n", ret);
2935 return ret;
2938 /**********************************************************
2939 * InternetOpenUrlA (WININET.@)
2941 * Opens an URL
2943 * RETURNS
2944 * handle of connection or NULL on failure
2946 HINTERNET WINAPI InternetOpenUrlA(HINTERNET hInternet, LPCSTR lpszUrl,
2947 LPCSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext)
2949 HINTERNET rc = NULL;
2951 INT lenUrl;
2952 INT lenHeaders = 0;
2953 LPWSTR szUrl = NULL;
2954 LPWSTR szHeaders = NULL;
2956 TRACE("\n");
2958 if(lpszUrl) {
2959 lenUrl = MultiByteToWideChar(CP_ACP, 0, lpszUrl, -1, NULL, 0 );
2960 szUrl = HeapAlloc(GetProcessHeap(), 0, lenUrl*sizeof(WCHAR));
2961 if(!szUrl)
2962 return NULL;
2963 MultiByteToWideChar(CP_ACP, 0, lpszUrl, -1, szUrl, lenUrl);
2966 if(lpszHeaders) {
2967 lenHeaders = MultiByteToWideChar(CP_ACP, 0, lpszHeaders, dwHeadersLength, NULL, 0 );
2968 szHeaders = HeapAlloc(GetProcessHeap(), 0, lenHeaders*sizeof(WCHAR));
2969 if(!szHeaders) {
2970 HeapFree(GetProcessHeap(), 0, szUrl);
2971 return NULL;
2973 MultiByteToWideChar(CP_ACP, 0, lpszHeaders, dwHeadersLength, szHeaders, lenHeaders);
2976 rc = InternetOpenUrlW(hInternet, szUrl, szHeaders,
2977 lenHeaders, dwFlags, dwContext);
2979 HeapFree(GetProcessHeap(), 0, szUrl);
2980 HeapFree(GetProcessHeap(), 0, szHeaders);
2982 return rc;
2986 static LPWITHREADERROR INTERNET_AllocThreadError(void)
2988 LPWITHREADERROR lpwite = HeapAlloc(GetProcessHeap(), 0, sizeof(*lpwite));
2990 if (lpwite)
2992 lpwite->dwError = 0;
2993 lpwite->response[0] = '\0';
2996 if (!TlsSetValue(g_dwTlsErrIndex, lpwite))
2998 HeapFree(GetProcessHeap(), 0, lpwite);
2999 return NULL;
3002 return lpwite;
3006 /***********************************************************************
3007 * INTERNET_SetLastError (internal)
3009 * Set last thread specific error
3011 * RETURNS
3014 void INTERNET_SetLastError(DWORD dwError)
3016 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
3018 if (!lpwite)
3019 lpwite = INTERNET_AllocThreadError();
3021 SetLastError(dwError);
3022 if(lpwite)
3023 lpwite->dwError = dwError;
3027 /***********************************************************************
3028 * INTERNET_GetLastError (internal)
3030 * Get last thread specific error
3032 * RETURNS
3035 DWORD INTERNET_GetLastError(void)
3037 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
3038 if (!lpwite) return 0;
3039 /* TlsGetValue clears last error, so set it again here */
3040 SetLastError(lpwite->dwError);
3041 return lpwite->dwError;
3045 /***********************************************************************
3046 * INTERNET_WorkerThreadFunc (internal)
3048 * Worker thread execution function
3050 * RETURNS
3053 static DWORD CALLBACK INTERNET_WorkerThreadFunc(LPVOID lpvParam)
3055 LPWORKREQUEST lpRequest = lpvParam;
3056 WORKREQUEST workRequest;
3058 TRACE("\n");
3060 workRequest = *lpRequest;
3061 HeapFree(GetProcessHeap(), 0, lpRequest);
3063 workRequest.asyncproc(&workRequest);
3065 WININET_Release( workRequest.hdr );
3066 return TRUE;
3070 /***********************************************************************
3071 * INTERNET_AsyncCall (internal)
3073 * Retrieves work request from queue
3075 * RETURNS
3078 BOOL INTERNET_AsyncCall(LPWORKREQUEST lpWorkRequest)
3080 BOOL bSuccess;
3081 LPWORKREQUEST lpNewRequest;
3083 TRACE("\n");
3085 lpNewRequest = HeapAlloc(GetProcessHeap(), 0, sizeof(WORKREQUEST));
3086 if (!lpNewRequest)
3087 return FALSE;
3089 *lpNewRequest = *lpWorkRequest;
3091 bSuccess = QueueUserWorkItem(INTERNET_WorkerThreadFunc, lpNewRequest, WT_EXECUTELONGFUNCTION);
3092 if (!bSuccess)
3094 HeapFree(GetProcessHeap(), 0, lpNewRequest);
3095 INTERNET_SetLastError(ERROR_INTERNET_ASYNC_THREAD_FAILED);
3098 return bSuccess;
3102 /***********************************************************************
3103 * INTERNET_GetResponseBuffer (internal)
3105 * RETURNS
3108 LPSTR INTERNET_GetResponseBuffer(void)
3110 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
3111 if (!lpwite)
3112 lpwite = INTERNET_AllocThreadError();
3113 TRACE("\n");
3114 return lpwite->response;
3117 /***********************************************************************
3118 * INTERNET_GetNextLine (internal)
3120 * Parse next line in directory string listing
3122 * RETURNS
3123 * Pointer to beginning of next line
3124 * NULL on failure
3128 LPSTR INTERNET_GetNextLine(INT nSocket, LPDWORD dwLen)
3130 struct pollfd pfd;
3131 BOOL bSuccess = FALSE;
3132 INT nRecv = 0;
3133 LPSTR lpszBuffer = INTERNET_GetResponseBuffer();
3135 TRACE("\n");
3137 pfd.fd = nSocket;
3138 pfd.events = POLLIN;
3140 while (nRecv < MAX_REPLY_LEN)
3142 if (poll(&pfd,1, RESPONSE_TIMEOUT * 1000) > 0)
3144 if (recv(nSocket, &lpszBuffer[nRecv], 1, 0) <= 0)
3146 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
3147 goto lend;
3150 if (lpszBuffer[nRecv] == '\n')
3152 bSuccess = TRUE;
3153 break;
3155 if (lpszBuffer[nRecv] != '\r')
3156 nRecv++;
3158 else
3160 INTERNET_SetLastError(ERROR_INTERNET_TIMEOUT);
3161 goto lend;
3165 lend:
3166 if (bSuccess)
3168 lpszBuffer[nRecv] = '\0';
3169 *dwLen = nRecv - 1;
3170 TRACE(":%d %s\n", nRecv, lpszBuffer);
3171 return lpszBuffer;
3173 else
3175 return NULL;
3179 /**********************************************************
3180 * InternetQueryDataAvailable (WININET.@)
3182 * Determines how much data is available to be read.
3184 * RETURNS
3185 * TRUE on success, FALSE if an error occurred. If
3186 * INTERNET_FLAG_ASYNC was specified in InternetOpen, and
3187 * no data is presently available, FALSE is returned with
3188 * the last error ERROR_IO_PENDING; a callback with status
3189 * INTERNET_STATUS_REQUEST_COMPLETE will be sent when more
3190 * data is available.
3192 BOOL WINAPI InternetQueryDataAvailable( HINTERNET hFile,
3193 LPDWORD lpdwNumberOfBytesAvailble,
3194 DWORD dwFlags, DWORD_PTR dwContext)
3196 WININETHANDLEHEADER *hdr;
3197 DWORD res;
3199 TRACE("(%p %p %x %lx)\n", hFile, lpdwNumberOfBytesAvailble, dwFlags, dwContext);
3201 hdr = WININET_GetObject( hFile );
3202 if (!hdr) {
3203 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
3204 return FALSE;
3207 if(hdr->vtbl->QueryDataAvailable) {
3208 res = hdr->vtbl->QueryDataAvailable(hdr, lpdwNumberOfBytesAvailble, dwFlags, dwContext);
3209 }else {
3210 WARN("wrong handle\n");
3211 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
3214 WININET_Release(hdr);
3216 if(res != ERROR_SUCCESS)
3217 SetLastError(res);
3218 return res == ERROR_SUCCESS;
3222 /***********************************************************************
3223 * InternetLockRequestFile (WININET.@)
3225 BOOL WINAPI InternetLockRequestFile( HINTERNET hInternet, HANDLE
3226 *lphLockReqHandle)
3228 FIXME("STUB\n");
3229 return FALSE;
3232 BOOL WINAPI InternetUnlockRequestFile( HANDLE hLockHandle)
3234 FIXME("STUB\n");
3235 return FALSE;
3239 /***********************************************************************
3240 * InternetAutodial (WININET.@)
3242 * On windows this function is supposed to dial the default internet
3243 * connection. We don't want to have Wine dial out to the internet so
3244 * we return TRUE by default. It might be nice to check if we are connected.
3246 * RETURNS
3247 * TRUE on success
3248 * FALSE on failure
3251 BOOL WINAPI InternetAutodial(DWORD dwFlags, HWND hwndParent)
3253 FIXME("STUB\n");
3255 /* Tell that we are connected to the internet. */
3256 return TRUE;
3259 /***********************************************************************
3260 * InternetAutodialHangup (WININET.@)
3262 * Hangs up a connection made with InternetAutodial
3264 * PARAM
3265 * dwReserved
3266 * RETURNS
3267 * TRUE on success
3268 * FALSE on failure
3271 BOOL WINAPI InternetAutodialHangup(DWORD dwReserved)
3273 FIXME("STUB\n");
3275 /* we didn't dial, we don't disconnect */
3276 return TRUE;
3279 /***********************************************************************
3280 * InternetCombineUrlA (WININET.@)
3282 * Combine a base URL with a relative URL
3284 * RETURNS
3285 * TRUE on success
3286 * FALSE on failure
3290 BOOL WINAPI InternetCombineUrlA(LPCSTR lpszBaseUrl, LPCSTR lpszRelativeUrl,
3291 LPSTR lpszBuffer, LPDWORD lpdwBufferLength,
3292 DWORD dwFlags)
3294 HRESULT hr=S_OK;
3296 TRACE("(%s, %s, %p, %p, 0x%08x)\n", debugstr_a(lpszBaseUrl), debugstr_a(lpszRelativeUrl), lpszBuffer, lpdwBufferLength, dwFlags);
3298 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
3299 dwFlags ^= ICU_NO_ENCODE;
3300 hr=UrlCombineA(lpszBaseUrl,lpszRelativeUrl,lpszBuffer,lpdwBufferLength,dwFlags);
3302 return (hr==S_OK);
3305 /***********************************************************************
3306 * InternetCombineUrlW (WININET.@)
3308 * Combine a base URL with a relative URL
3310 * RETURNS
3311 * TRUE on success
3312 * FALSE on failure
3316 BOOL WINAPI InternetCombineUrlW(LPCWSTR lpszBaseUrl, LPCWSTR lpszRelativeUrl,
3317 LPWSTR lpszBuffer, LPDWORD lpdwBufferLength,
3318 DWORD dwFlags)
3320 HRESULT hr=S_OK;
3322 TRACE("(%s, %s, %p, %p, 0x%08x)\n", debugstr_w(lpszBaseUrl), debugstr_w(lpszRelativeUrl), lpszBuffer, lpdwBufferLength, dwFlags);
3324 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
3325 dwFlags ^= ICU_NO_ENCODE;
3326 hr=UrlCombineW(lpszBaseUrl,lpszRelativeUrl,lpszBuffer,lpdwBufferLength,dwFlags);
3328 return (hr==S_OK);
3331 /* max port num is 65535 => 5 digits */
3332 #define MAX_WORD_DIGITS 5
3334 #define URL_GET_COMP_LENGTH(url, component) ((url)->dw##component##Length ? \
3335 (url)->dw##component##Length : strlenW((url)->lpsz##component))
3336 #define URL_GET_COMP_LENGTHA(url, component) ((url)->dw##component##Length ? \
3337 (url)->dw##component##Length : strlen((url)->lpsz##component))
3339 static BOOL url_uses_default_port(INTERNET_SCHEME nScheme, INTERNET_PORT nPort)
3341 if ((nScheme == INTERNET_SCHEME_HTTP) &&
3342 (nPort == INTERNET_DEFAULT_HTTP_PORT))
3343 return TRUE;
3344 if ((nScheme == INTERNET_SCHEME_HTTPS) &&
3345 (nPort == INTERNET_DEFAULT_HTTPS_PORT))
3346 return TRUE;
3347 if ((nScheme == INTERNET_SCHEME_FTP) &&
3348 (nPort == INTERNET_DEFAULT_FTP_PORT))
3349 return TRUE;
3350 if ((nScheme == INTERNET_SCHEME_GOPHER) &&
3351 (nPort == INTERNET_DEFAULT_GOPHER_PORT))
3352 return TRUE;
3354 if (nPort == INTERNET_INVALID_PORT_NUMBER)
3355 return TRUE;
3357 return FALSE;
3360 /* opaque urls do not fit into the standard url hierarchy and don't have
3361 * two following slashes */
3362 static inline BOOL scheme_is_opaque(INTERNET_SCHEME nScheme)
3364 return (nScheme != INTERNET_SCHEME_FTP) &&
3365 (nScheme != INTERNET_SCHEME_GOPHER) &&
3366 (nScheme != INTERNET_SCHEME_HTTP) &&
3367 (nScheme != INTERNET_SCHEME_HTTPS) &&
3368 (nScheme != INTERNET_SCHEME_FILE);
3371 static LPCWSTR INTERNET_GetSchemeString(INTERNET_SCHEME scheme)
3373 int index;
3374 if (scheme < INTERNET_SCHEME_FIRST)
3375 return NULL;
3376 index = scheme - INTERNET_SCHEME_FIRST;
3377 if (index >= sizeof(url_schemes)/sizeof(url_schemes[0]))
3378 return NULL;
3379 return (LPCWSTR)url_schemes[index];
3382 /* we can calculate using ansi strings because we're just
3383 * calculating string length, not size
3385 static BOOL calc_url_length(LPURL_COMPONENTSW lpUrlComponents,
3386 LPDWORD lpdwUrlLength)
3388 INTERNET_SCHEME nScheme;
3390 *lpdwUrlLength = 0;
3392 if (lpUrlComponents->lpszScheme)
3394 DWORD dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, Scheme);
3395 *lpdwUrlLength += dwLen;
3396 nScheme = GetInternetSchemeW(lpUrlComponents->lpszScheme, dwLen);
3398 else
3400 LPCWSTR scheme;
3402 nScheme = lpUrlComponents->nScheme;
3404 if (nScheme == INTERNET_SCHEME_DEFAULT)
3405 nScheme = INTERNET_SCHEME_HTTP;
3406 scheme = INTERNET_GetSchemeString(nScheme);
3407 *lpdwUrlLength += strlenW(scheme);
3410 (*lpdwUrlLength)++; /* ':' */
3411 if (!scheme_is_opaque(nScheme) || lpUrlComponents->lpszHostName)
3412 *lpdwUrlLength += strlen("//");
3414 if (lpUrlComponents->lpszUserName)
3416 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, UserName);
3417 *lpdwUrlLength += strlen("@");
3419 else
3421 if (lpUrlComponents->lpszPassword)
3423 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
3424 return FALSE;
3428 if (lpUrlComponents->lpszPassword)
3430 *lpdwUrlLength += strlen(":");
3431 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, Password);
3434 if (lpUrlComponents->lpszHostName)
3436 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, HostName);
3438 if (!url_uses_default_port(nScheme, lpUrlComponents->nPort))
3440 char szPort[MAX_WORD_DIGITS+1];
3442 sprintf(szPort, "%d", lpUrlComponents->nPort);
3443 *lpdwUrlLength += strlen(szPort);
3444 *lpdwUrlLength += strlen(":");
3447 if (lpUrlComponents->lpszUrlPath && *lpUrlComponents->lpszUrlPath != '/')
3448 (*lpdwUrlLength)++; /* '/' */
3451 if (lpUrlComponents->lpszUrlPath)
3452 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, UrlPath);
3454 return TRUE;
3457 static void convert_urlcomp_atow(LPURL_COMPONENTSA lpUrlComponents, LPURL_COMPONENTSW urlCompW)
3459 INT len;
3461 ZeroMemory(urlCompW, sizeof(URL_COMPONENTSW));
3463 urlCompW->dwStructSize = sizeof(URL_COMPONENTSW);
3464 urlCompW->dwSchemeLength = lpUrlComponents->dwSchemeLength;
3465 urlCompW->nScheme = lpUrlComponents->nScheme;
3466 urlCompW->dwHostNameLength = lpUrlComponents->dwHostNameLength;
3467 urlCompW->nPort = lpUrlComponents->nPort;
3468 urlCompW->dwUserNameLength = lpUrlComponents->dwUserNameLength;
3469 urlCompW->dwPasswordLength = lpUrlComponents->dwPasswordLength;
3470 urlCompW->dwUrlPathLength = lpUrlComponents->dwUrlPathLength;
3471 urlCompW->dwExtraInfoLength = lpUrlComponents->dwExtraInfoLength;
3473 if (lpUrlComponents->lpszScheme)
3475 len = URL_GET_COMP_LENGTHA(lpUrlComponents, Scheme) + 1;
3476 urlCompW->lpszScheme = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3477 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszScheme,
3478 -1, urlCompW->lpszScheme, len);
3481 if (lpUrlComponents->lpszHostName)
3483 len = URL_GET_COMP_LENGTHA(lpUrlComponents, HostName) + 1;
3484 urlCompW->lpszHostName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3485 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszHostName,
3486 -1, urlCompW->lpszHostName, len);
3489 if (lpUrlComponents->lpszUserName)
3491 len = URL_GET_COMP_LENGTHA(lpUrlComponents, UserName) + 1;
3492 urlCompW->lpszUserName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3493 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszUserName,
3494 -1, urlCompW->lpszUserName, len);
3497 if (lpUrlComponents->lpszPassword)
3499 len = URL_GET_COMP_LENGTHA(lpUrlComponents, Password) + 1;
3500 urlCompW->lpszPassword = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3501 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszPassword,
3502 -1, urlCompW->lpszPassword, len);
3505 if (lpUrlComponents->lpszUrlPath)
3507 len = URL_GET_COMP_LENGTHA(lpUrlComponents, UrlPath) + 1;
3508 urlCompW->lpszUrlPath = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3509 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszUrlPath,
3510 -1, urlCompW->lpszUrlPath, len);
3513 if (lpUrlComponents->lpszExtraInfo)
3515 len = URL_GET_COMP_LENGTHA(lpUrlComponents, ExtraInfo) + 1;
3516 urlCompW->lpszExtraInfo = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3517 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszExtraInfo,
3518 -1, urlCompW->lpszExtraInfo, len);
3522 /***********************************************************************
3523 * InternetCreateUrlA (WININET.@)
3525 * See InternetCreateUrlW.
3527 BOOL WINAPI InternetCreateUrlA(LPURL_COMPONENTSA lpUrlComponents, DWORD dwFlags,
3528 LPSTR lpszUrl, LPDWORD lpdwUrlLength)
3530 BOOL ret;
3531 LPWSTR urlW = NULL;
3532 URL_COMPONENTSW urlCompW;
3534 TRACE("(%p,%d,%p,%p)\n", lpUrlComponents, dwFlags, lpszUrl, lpdwUrlLength);
3536 if (!lpUrlComponents || lpUrlComponents->dwStructSize != sizeof(URL_COMPONENTSW) || !lpdwUrlLength)
3538 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
3539 return FALSE;
3542 convert_urlcomp_atow(lpUrlComponents, &urlCompW);
3544 if (lpszUrl)
3545 urlW = HeapAlloc(GetProcessHeap(), 0, *lpdwUrlLength * sizeof(WCHAR));
3547 ret = InternetCreateUrlW(&urlCompW, dwFlags, urlW, lpdwUrlLength);
3549 if (!ret && (GetLastError() == ERROR_INSUFFICIENT_BUFFER))
3550 *lpdwUrlLength /= sizeof(WCHAR);
3552 /* on success, lpdwUrlLength points to the size of urlW in WCHARS
3553 * minus one, so add one to leave room for NULL terminator
3555 if (ret)
3556 WideCharToMultiByte(CP_ACP, 0, urlW, -1, lpszUrl, *lpdwUrlLength + 1, NULL, NULL);
3558 HeapFree(GetProcessHeap(), 0, urlCompW.lpszScheme);
3559 HeapFree(GetProcessHeap(), 0, urlCompW.lpszHostName);
3560 HeapFree(GetProcessHeap(), 0, urlCompW.lpszUserName);
3561 HeapFree(GetProcessHeap(), 0, urlCompW.lpszPassword);
3562 HeapFree(GetProcessHeap(), 0, urlCompW.lpszUrlPath);
3563 HeapFree(GetProcessHeap(), 0, urlCompW.lpszExtraInfo);
3564 HeapFree(GetProcessHeap(), 0, urlW);
3566 return ret;
3569 /***********************************************************************
3570 * InternetCreateUrlW (WININET.@)
3572 * Creates a URL from its component parts.
3574 * PARAMS
3575 * lpUrlComponents [I] URL Components.
3576 * dwFlags [I] Flags. See notes.
3577 * lpszUrl [I] Buffer in which to store the created URL.
3578 * lpdwUrlLength [I/O] On input, the length of the buffer pointed to by
3579 * lpszUrl in characters. On output, the number of bytes
3580 * required to store the URL including terminator.
3582 * NOTES
3584 * The dwFlags parameter can be zero or more of the following:
3585 *|ICU_ESCAPE - Generates escape sequences for unsafe characters in the path and extra info of the URL.
3587 * RETURNS
3588 * TRUE on success
3589 * FALSE on failure
3592 BOOL WINAPI InternetCreateUrlW(LPURL_COMPONENTSW lpUrlComponents, DWORD dwFlags,
3593 LPWSTR lpszUrl, LPDWORD lpdwUrlLength)
3595 DWORD dwLen;
3596 INTERNET_SCHEME nScheme;
3598 static const WCHAR slashSlashW[] = {'/','/'};
3599 static const WCHAR percentD[] = {'%','d',0};
3601 TRACE("(%p,%d,%p,%p)\n", lpUrlComponents, dwFlags, lpszUrl, lpdwUrlLength);
3603 if (!lpUrlComponents || lpUrlComponents->dwStructSize != sizeof(URL_COMPONENTSW) || !lpdwUrlLength)
3605 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
3606 return FALSE;
3609 if (!calc_url_length(lpUrlComponents, &dwLen))
3610 return FALSE;
3612 if (!lpszUrl || *lpdwUrlLength < dwLen)
3614 *lpdwUrlLength = (dwLen + 1) * sizeof(WCHAR);
3615 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
3616 return FALSE;
3619 *lpdwUrlLength = dwLen;
3620 lpszUrl[0] = 0x00;
3622 dwLen = 0;
3624 if (lpUrlComponents->lpszScheme)
3626 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, Scheme);
3627 memcpy(lpszUrl, lpUrlComponents->lpszScheme, dwLen * sizeof(WCHAR));
3628 lpszUrl += dwLen;
3630 nScheme = GetInternetSchemeW(lpUrlComponents->lpszScheme, dwLen);
3632 else
3634 LPCWSTR scheme;
3635 nScheme = lpUrlComponents->nScheme;
3637 if (nScheme == INTERNET_SCHEME_DEFAULT)
3638 nScheme = INTERNET_SCHEME_HTTP;
3640 scheme = INTERNET_GetSchemeString(nScheme);
3641 dwLen = strlenW(scheme);
3642 memcpy(lpszUrl, scheme, dwLen * sizeof(WCHAR));
3643 lpszUrl += dwLen;
3646 /* all schemes are followed by at least a colon */
3647 *lpszUrl = ':';
3648 lpszUrl++;
3650 if (!scheme_is_opaque(nScheme) || lpUrlComponents->lpszHostName)
3652 memcpy(lpszUrl, slashSlashW, sizeof(slashSlashW));
3653 lpszUrl += sizeof(slashSlashW)/sizeof(slashSlashW[0]);
3656 if (lpUrlComponents->lpszUserName)
3658 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, UserName);
3659 memcpy(lpszUrl, lpUrlComponents->lpszUserName, dwLen * sizeof(WCHAR));
3660 lpszUrl += dwLen;
3662 if (lpUrlComponents->lpszPassword)
3664 *lpszUrl = ':';
3665 lpszUrl++;
3667 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, Password);
3668 memcpy(lpszUrl, lpUrlComponents->lpszPassword, dwLen * sizeof(WCHAR));
3669 lpszUrl += dwLen;
3672 *lpszUrl = '@';
3673 lpszUrl++;
3676 if (lpUrlComponents->lpszHostName)
3678 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, HostName);
3679 memcpy(lpszUrl, lpUrlComponents->lpszHostName, dwLen * sizeof(WCHAR));
3680 lpszUrl += dwLen;
3682 if (!url_uses_default_port(nScheme, lpUrlComponents->nPort))
3684 WCHAR szPort[MAX_WORD_DIGITS+1];
3686 sprintfW(szPort, percentD, lpUrlComponents->nPort);
3687 *lpszUrl = ':';
3688 lpszUrl++;
3689 dwLen = strlenW(szPort);
3690 memcpy(lpszUrl, szPort, dwLen * sizeof(WCHAR));
3691 lpszUrl += dwLen;
3694 /* add slash between hostname and path if necessary */
3695 if (lpUrlComponents->lpszUrlPath && *lpUrlComponents->lpszUrlPath != '/')
3697 *lpszUrl = '/';
3698 lpszUrl++;
3703 if (lpUrlComponents->lpszUrlPath)
3705 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, UrlPath);
3706 memcpy(lpszUrl, lpUrlComponents->lpszUrlPath, dwLen * sizeof(WCHAR));
3707 lpszUrl += dwLen;
3710 *lpszUrl = '\0';
3712 return TRUE;
3715 /***********************************************************************
3716 * InternetConfirmZoneCrossingA (WININET.@)
3719 DWORD WINAPI InternetConfirmZoneCrossingA( HWND hWnd, LPSTR szUrlPrev, LPSTR szUrlNew, BOOL bPost )
3721 FIXME("(%p, %s, %s, %x) stub\n", hWnd, debugstr_a(szUrlPrev), debugstr_a(szUrlNew), bPost);
3722 return ERROR_SUCCESS;
3725 /***********************************************************************
3726 * InternetConfirmZoneCrossingW (WININET.@)
3729 DWORD WINAPI InternetConfirmZoneCrossingW( HWND hWnd, LPWSTR szUrlPrev, LPWSTR szUrlNew, BOOL bPost )
3731 FIXME("(%p, %s, %s, %x) stub\n", hWnd, debugstr_w(szUrlPrev), debugstr_w(szUrlNew), bPost);
3732 return ERROR_SUCCESS;
3735 DWORD WINAPI InternetDialA( HWND hwndParent, LPSTR lpszConnectoid, DWORD dwFlags,
3736 DWORD_PTR* lpdwConnection, DWORD dwReserved )
3738 FIXME("(%p, %p, 0x%08x, %p, 0x%08x) stub\n", hwndParent, lpszConnectoid, dwFlags,
3739 lpdwConnection, dwReserved);
3740 return ERROR_SUCCESS;
3743 DWORD WINAPI InternetDialW( HWND hwndParent, LPWSTR lpszConnectoid, DWORD dwFlags,
3744 DWORD_PTR* lpdwConnection, DWORD dwReserved )
3746 FIXME("(%p, %p, 0x%08x, %p, 0x%08x) stub\n", hwndParent, lpszConnectoid, dwFlags,
3747 lpdwConnection, dwReserved);
3748 return ERROR_SUCCESS;
3751 BOOL WINAPI InternetGoOnlineA( LPSTR lpszURL, HWND hwndParent, DWORD dwReserved )
3753 FIXME("(%s, %p, 0x%08x) stub\n", debugstr_a(lpszURL), hwndParent, dwReserved);
3754 return TRUE;
3757 BOOL WINAPI InternetGoOnlineW( LPWSTR lpszURL, HWND hwndParent, DWORD dwReserved )
3759 FIXME("(%s, %p, 0x%08x) stub\n", debugstr_w(lpszURL), hwndParent, dwReserved);
3760 return TRUE;
3763 DWORD WINAPI InternetHangUp( DWORD_PTR dwConnection, DWORD dwReserved )
3765 FIXME("(0x%08lx, 0x%08x) stub\n", dwConnection, dwReserved);
3766 return ERROR_SUCCESS;
3769 BOOL WINAPI CreateMD5SSOHash( PWSTR pszChallengeInfo, PWSTR pwszRealm, PWSTR pwszTarget,
3770 PBYTE pbHexHash )
3772 FIXME("(%s, %s, %s, %p) stub\n", debugstr_w(pszChallengeInfo), debugstr_w(pwszRealm),
3773 debugstr_w(pwszTarget), pbHexHash);
3774 return FALSE;
3777 BOOL WINAPI ResumeSuspendedDownload( HINTERNET hInternet, DWORD dwError )
3779 FIXME("(%p, 0x%08x) stub\n", hInternet, dwError);
3780 return FALSE;
3783 BOOL WINAPI InternetQueryFortezzaStatus(DWORD *a, DWORD_PTR b)
3785 FIXME("(%p, %08lx) stub\n", a, b);
3786 return 0;