push 8c61147e396035865ef2da2e6aa2d0f3b0907179
[wine/hacks.git] / dlls / wininet / internet.c
blob236e1f664533f16ff8a3d1ea6b00d2e9cc455747
1 /*
2 * Wininet
4 * Copyright 1999 Corel Corporation
5 * Copyright 2002 CodeWeavers Inc.
6 * Copyright 2002 Jaco Greeff
7 * Copyright 2002 TransGaming Technologies Inc.
8 * Copyright 2004 Mike McCormack for CodeWeavers
10 * Ulrich Czekalla
11 * Aric Stewart
12 * David Hammerton
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
29 #include "config.h"
30 #include "wine/port.h"
32 #define MAXHOSTNAME 100 /* from http.c */
34 #if defined(__MINGW32__) || defined (_MSC_VER)
35 #include <ws2tcpip.h>
36 #endif
38 #include <string.h>
39 #include <stdarg.h>
40 #include <stdio.h>
41 #include <sys/types.h>
42 #ifdef HAVE_SYS_SOCKET_H
43 # include <sys/socket.h>
44 #endif
45 #ifdef HAVE_POLL_H
46 #include <poll.h>
47 #endif
48 #ifdef HAVE_SYS_POLL_H
49 # include <sys/poll.h>
50 #endif
51 #ifdef HAVE_SYS_TIME_H
52 # include <sys/time.h>
53 #endif
54 #include <stdlib.h>
55 #include <ctype.h>
56 #ifdef HAVE_UNISTD_H
57 # include <unistd.h>
58 #endif
59 #include <assert.h>
61 #include "windef.h"
62 #include "winbase.h"
63 #include "winreg.h"
64 #include "winuser.h"
65 #include "wininet.h"
66 #include "winineti.h"
67 #include "winnls.h"
68 #include "wine/debug.h"
69 #include "winerror.h"
70 #define NO_SHLWAPI_STREAM
71 #include "shlwapi.h"
73 #include "wine/exception.h"
75 #include "internet.h"
76 #include "resource.h"
78 #include "wine/unicode.h"
80 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
82 #define RESPONSE_TIMEOUT 30
84 typedef struct
86 DWORD dwError;
87 CHAR response[MAX_REPLY_LEN];
88 } WITHREADERROR, *LPWITHREADERROR;
90 static DWORD g_dwTlsErrIndex = TLS_OUT_OF_INDEXES;
91 static HMODULE WININET_hModule;
93 #define HANDLE_CHUNK_SIZE 0x10
95 static CRITICAL_SECTION WININET_cs;
96 static CRITICAL_SECTION_DEBUG WININET_cs_debug =
98 0, 0, &WININET_cs,
99 { &WININET_cs_debug.ProcessLocksList, &WININET_cs_debug.ProcessLocksList },
100 0, 0, { (DWORD_PTR)(__FILE__ ": WININET_cs") }
102 static CRITICAL_SECTION WININET_cs = { &WININET_cs_debug, -1, 0, 0, 0, 0 };
104 static object_header_t **WININET_Handles;
105 static UINT WININET_dwNextHandle;
106 static UINT WININET_dwMaxHandles;
108 HINTERNET WININET_AllocHandle( object_header_t *info )
110 object_header_t **p;
111 UINT handle = 0, num;
113 list_init( &info->children );
115 EnterCriticalSection( &WININET_cs );
116 if( !WININET_dwMaxHandles )
118 num = HANDLE_CHUNK_SIZE;
119 p = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
120 sizeof (*WININET_Handles)* num);
121 if( !p )
122 goto end;
123 WININET_Handles = p;
124 WININET_dwMaxHandles = num;
126 if( WININET_dwMaxHandles == WININET_dwNextHandle )
128 num = WININET_dwMaxHandles + HANDLE_CHUNK_SIZE;
129 p = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
130 WININET_Handles, sizeof (*WININET_Handles)* num);
131 if( !p )
132 goto end;
133 WININET_Handles = p;
134 WININET_dwMaxHandles = num;
137 handle = WININET_dwNextHandle;
138 if( WININET_Handles[handle] )
139 ERR("handle isn't free but should be\n");
140 WININET_Handles[handle] = WININET_AddRef( info );
142 while( WININET_Handles[WININET_dwNextHandle] &&
143 (WININET_dwNextHandle < WININET_dwMaxHandles ) )
144 WININET_dwNextHandle++;
146 end:
147 LeaveCriticalSection( &WININET_cs );
149 return info->hInternet = (HINTERNET) (handle+1);
152 object_header_t *WININET_AddRef( object_header_t *info )
154 ULONG refs = InterlockedIncrement(&info->refs);
155 TRACE("%p -> refcount = %d\n", info, refs );
156 return info;
159 object_header_t *WININET_GetObject( HINTERNET hinternet )
161 object_header_t *info = NULL;
162 UINT handle = (UINT) hinternet;
164 EnterCriticalSection( &WININET_cs );
166 if( (handle > 0) && ( handle <= WININET_dwMaxHandles ) &&
167 WININET_Handles[handle-1] )
168 info = WININET_AddRef( WININET_Handles[handle-1] );
170 LeaveCriticalSection( &WININET_cs );
172 TRACE("handle %d -> %p\n", handle, info);
174 return info;
177 BOOL WININET_Release( object_header_t *info )
179 ULONG refs = InterlockedDecrement(&info->refs);
180 TRACE( "object %p refcount = %d\n", info, refs );
181 if( !refs )
183 if ( info->vtbl->CloseConnection )
185 TRACE( "closing connection %p\n", info);
186 info->vtbl->CloseConnection( info );
188 /* Don't send a callback if this is a session handle created with InternetOpenUrl */
189 if ((info->htype != WH_HHTTPSESSION && info->htype != WH_HFTPSESSION)
190 || !(info->dwInternalFlags & INET_OPENURL))
192 INTERNET_SendCallback(info, info->dwContext,
193 INTERNET_STATUS_HANDLE_CLOSING, &info->hInternet,
194 sizeof(HINTERNET));
196 TRACE( "destroying object %p\n", info);
197 if ( info->htype != WH_HINIT )
198 list_remove( &info->entry );
199 info->vtbl->Destroy( info );
201 return TRUE;
204 BOOL WININET_FreeHandle( HINTERNET hinternet )
206 BOOL ret = FALSE;
207 UINT handle = (UINT) hinternet;
208 object_header_t *info = NULL, *child, *next;
210 EnterCriticalSection( &WININET_cs );
212 if( (handle > 0) && ( handle <= WININET_dwMaxHandles ) )
214 handle--;
215 if( WININET_Handles[handle] )
217 info = WININET_Handles[handle];
218 TRACE( "destroying handle %d for object %p\n", handle+1, info);
219 WININET_Handles[handle] = NULL;
220 ret = TRUE;
224 LeaveCriticalSection( &WININET_cs );
226 /* As on native when the equivalent of WININET_Release is called, the handle
227 * is already invalid, but if a new handle is created at this time it does
228 * not yet get assigned the freed handle number */
229 if( info )
231 /* Free all children as native does */
232 LIST_FOR_EACH_ENTRY_SAFE( child, next, &info->children, object_header_t, entry )
234 TRACE( "freeing child handle %d for parent handle %d\n",
235 (UINT)child->hInternet, handle+1);
236 WININET_FreeHandle( child->hInternet );
238 WININET_Release( info );
241 EnterCriticalSection( &WININET_cs );
243 if( WININET_dwNextHandle > handle && !WININET_Handles[handle] )
244 WININET_dwNextHandle = handle;
246 LeaveCriticalSection( &WININET_cs );
248 return ret;
251 /***********************************************************************
252 * DllMain [Internal] Initializes the internal 'WININET.DLL'.
254 * PARAMS
255 * hinstDLL [I] handle to the DLL's instance
256 * fdwReason [I]
257 * lpvReserved [I] reserved, must be NULL
259 * RETURNS
260 * Success: TRUE
261 * Failure: FALSE
264 BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
266 TRACE("%p,%x,%p\n", hinstDLL, fdwReason, lpvReserved);
268 switch (fdwReason) {
269 case DLL_PROCESS_ATTACH:
271 g_dwTlsErrIndex = TlsAlloc();
273 if (g_dwTlsErrIndex == TLS_OUT_OF_INDEXES)
274 return FALSE;
276 URLCacheContainers_CreateDefaults();
278 WININET_hModule = hinstDLL;
280 case DLL_THREAD_ATTACH:
281 break;
283 case DLL_THREAD_DETACH:
284 if (g_dwTlsErrIndex != TLS_OUT_OF_INDEXES)
286 LPVOID lpwite = TlsGetValue(g_dwTlsErrIndex);
287 HeapFree(GetProcessHeap(), 0, lpwite);
289 break;
291 case DLL_PROCESS_DETACH:
293 URLCacheContainers_DeleteAll();
295 if (g_dwTlsErrIndex != TLS_OUT_OF_INDEXES)
297 HeapFree(GetProcessHeap(), 0, TlsGetValue(g_dwTlsErrIndex));
298 TlsFree(g_dwTlsErrIndex);
300 break;
303 return TRUE;
307 /***********************************************************************
308 * InternetInitializeAutoProxyDll (WININET.@)
310 * Setup the internal proxy
312 * PARAMETERS
313 * dwReserved
315 * RETURNS
316 * FALSE on failure
319 BOOL WINAPI InternetInitializeAutoProxyDll(DWORD dwReserved)
321 FIXME("STUB\n");
322 INTERNET_SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
323 return FALSE;
326 /***********************************************************************
327 * DetectAutoProxyUrl (WININET.@)
329 * Auto detect the proxy url
331 * RETURNS
332 * FALSE on failure
335 BOOL WINAPI DetectAutoProxyUrl(LPSTR lpszAutoProxyUrl,
336 DWORD dwAutoProxyUrlLength, DWORD dwDetectFlags)
338 FIXME("STUB\n");
339 INTERNET_SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
340 return FALSE;
344 /***********************************************************************
345 * INTERNET_ConfigureProxy
347 * FIXME:
348 * The proxy may be specified in the form 'http=proxy.my.org'
349 * Presumably that means there can be ftp=ftpproxy.my.org too.
351 static BOOL INTERNET_ConfigureProxy( appinfo_t *lpwai )
353 HKEY key;
354 DWORD type, len, enabled = 0;
355 LPCSTR envproxy;
356 static const WCHAR szInternetSettings[] =
357 { 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
358 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
359 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s',0 };
360 static const WCHAR szProxyServer[] = { 'P','r','o','x','y','S','e','r','v','e','r', 0 };
361 static const WCHAR szProxyEnable[] = { 'P','r','o','x','y','E','n','a','b','l','e', 0 };
363 if (RegOpenKeyW( HKEY_CURRENT_USER, szInternetSettings, &key )) return FALSE;
365 len = sizeof enabled;
366 if (RegQueryValueExW( key, szProxyEnable, NULL, &type, (BYTE *)&enabled, &len ) || type != REG_DWORD)
367 RegSetValueExW( key, szProxyEnable, 0, REG_DWORD, (BYTE *)&enabled, sizeof(REG_DWORD) );
369 if (enabled)
371 TRACE("Proxy is enabled.\n");
373 /* figure out how much memory the proxy setting takes */
374 if (!RegQueryValueExW( key, szProxyServer, NULL, &type, NULL, &len ) && len && (type == REG_SZ))
376 LPWSTR szProxy, p;
377 static const WCHAR szHttp[] = {'h','t','t','p','=',0};
379 if (!(szProxy = HeapAlloc( GetProcessHeap(), 0, len )))
381 RegCloseKey( key );
382 return FALSE;
384 RegQueryValueExW( key, szProxyServer, NULL, &type, (BYTE*)szProxy, &len );
386 /* find the http proxy, and strip away everything else */
387 p = strstrW( szProxy, szHttp );
388 if (p)
390 p += lstrlenW( szHttp );
391 lstrcpyW( szProxy, p );
393 p = strchrW( szProxy, ' ' );
394 if (p) *p = 0;
396 lpwai->dwAccessType = INTERNET_OPEN_TYPE_PROXY;
397 lpwai->lpszProxy = szProxy;
399 TRACE("http proxy = %s\n", debugstr_w(lpwai->lpszProxy));
401 else
402 ERR("Couldn't read proxy server settings from registry.\n");
404 else if ((envproxy = getenv( "http_proxy" )))
406 WCHAR *envproxyW;
408 len = MultiByteToWideChar( CP_UNIXCP, 0, envproxy, -1, NULL, 0 );
409 if (!(envproxyW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR)))) return FALSE;
410 MultiByteToWideChar( CP_UNIXCP, 0, envproxy, -1, envproxyW, len );
412 lpwai->dwAccessType = INTERNET_OPEN_TYPE_PROXY;
413 lpwai->lpszProxy = envproxyW;
415 TRACE("http proxy (from environment) = %s\n", debugstr_w(lpwai->lpszProxy));
416 enabled = 1;
418 if (!enabled)
420 TRACE("Proxy is not enabled.\n");
421 lpwai->dwAccessType = INTERNET_OPEN_TYPE_DIRECT;
423 RegCloseKey( key );
424 return (enabled > 0);
427 /***********************************************************************
428 * dump_INTERNET_FLAGS
430 * Helper function to TRACE the internet flags.
432 * RETURNS
433 * None
436 static void dump_INTERNET_FLAGS(DWORD dwFlags)
438 #define FE(x) { x, #x }
439 static const wininet_flag_info flag[] = {
440 FE(INTERNET_FLAG_RELOAD),
441 FE(INTERNET_FLAG_RAW_DATA),
442 FE(INTERNET_FLAG_EXISTING_CONNECT),
443 FE(INTERNET_FLAG_ASYNC),
444 FE(INTERNET_FLAG_PASSIVE),
445 FE(INTERNET_FLAG_NO_CACHE_WRITE),
446 FE(INTERNET_FLAG_MAKE_PERSISTENT),
447 FE(INTERNET_FLAG_FROM_CACHE),
448 FE(INTERNET_FLAG_SECURE),
449 FE(INTERNET_FLAG_KEEP_CONNECTION),
450 FE(INTERNET_FLAG_NO_AUTO_REDIRECT),
451 FE(INTERNET_FLAG_READ_PREFETCH),
452 FE(INTERNET_FLAG_NO_COOKIES),
453 FE(INTERNET_FLAG_NO_AUTH),
454 FE(INTERNET_FLAG_CACHE_IF_NET_FAIL),
455 FE(INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP),
456 FE(INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTPS),
457 FE(INTERNET_FLAG_IGNORE_CERT_DATE_INVALID),
458 FE(INTERNET_FLAG_IGNORE_CERT_CN_INVALID),
459 FE(INTERNET_FLAG_RESYNCHRONIZE),
460 FE(INTERNET_FLAG_HYPERLINK),
461 FE(INTERNET_FLAG_NO_UI),
462 FE(INTERNET_FLAG_PRAGMA_NOCACHE),
463 FE(INTERNET_FLAG_CACHE_ASYNC),
464 FE(INTERNET_FLAG_FORMS_SUBMIT),
465 FE(INTERNET_FLAG_NEED_FILE),
466 FE(INTERNET_FLAG_TRANSFER_ASCII),
467 FE(INTERNET_FLAG_TRANSFER_BINARY)
469 #undef FE
470 unsigned int i;
472 for (i = 0; i < (sizeof(flag) / sizeof(flag[0])); i++) {
473 if (flag[i].val & dwFlags) {
474 TRACE(" %s", flag[i].name);
475 dwFlags &= ~flag[i].val;
478 if (dwFlags)
479 TRACE(" Unknown flags (%08x)\n", dwFlags);
480 else
481 TRACE("\n");
484 /***********************************************************************
485 * INTERNET_CloseHandle (internal)
487 * Close internet handle
490 static VOID APPINFO_Destroy(object_header_t *hdr)
492 appinfo_t *lpwai = (appinfo_t*)hdr;
494 TRACE("%p\n",lpwai);
496 HeapFree(GetProcessHeap(), 0, lpwai->lpszAgent);
497 HeapFree(GetProcessHeap(), 0, lpwai->lpszProxy);
498 HeapFree(GetProcessHeap(), 0, lpwai->lpszProxyBypass);
499 HeapFree(GetProcessHeap(), 0, lpwai->lpszProxyUsername);
500 HeapFree(GetProcessHeap(), 0, lpwai->lpszProxyPassword);
501 HeapFree(GetProcessHeap(), 0, lpwai);
504 static DWORD APPINFO_QueryOption(object_header_t *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
506 appinfo_t *ai = (appinfo_t*)hdr;
508 switch(option) {
509 case INTERNET_OPTION_HANDLE_TYPE:
510 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
512 if (*size < sizeof(ULONG))
513 return ERROR_INSUFFICIENT_BUFFER;
515 *size = sizeof(DWORD);
516 *(DWORD*)buffer = INTERNET_HANDLE_TYPE_INTERNET;
517 return ERROR_SUCCESS;
519 case INTERNET_OPTION_USER_AGENT: {
520 DWORD bufsize;
522 TRACE("INTERNET_OPTION_USER_AGENT\n");
524 bufsize = *size;
526 if (unicode) {
527 *size = (strlenW(ai->lpszAgent) + 1) * sizeof(WCHAR);
528 if(!buffer || bufsize < *size)
529 return ERROR_INSUFFICIENT_BUFFER;
531 strcpyW(buffer, ai->lpszAgent);
532 }else {
533 *size = WideCharToMultiByte(CP_ACP, 0, ai->lpszAgent, -1, NULL, 0, NULL, NULL);
534 if(!buffer || bufsize < *size)
535 return ERROR_INSUFFICIENT_BUFFER;
537 WideCharToMultiByte(CP_ACP, 0, ai->lpszAgent, -1, buffer, *size, NULL, NULL);
540 return ERROR_SUCCESS;
543 case INTERNET_OPTION_PROXY:
544 if (unicode) {
545 INTERNET_PROXY_INFOW *pi = (INTERNET_PROXY_INFOW *)buffer;
546 DWORD proxyBytesRequired = 0, proxyBypassBytesRequired = 0;
547 LPWSTR proxy, proxy_bypass;
549 if (ai->lpszProxy)
550 proxyBytesRequired = (lstrlenW(ai->lpszProxy) + 1) * sizeof(WCHAR);
551 if (ai->lpszProxyBypass)
552 proxyBypassBytesRequired = (lstrlenW(ai->lpszProxyBypass) + 1) * sizeof(WCHAR);
553 if (*size < sizeof(INTERNET_PROXY_INFOW) + proxyBytesRequired + proxyBypassBytesRequired)
555 *size = sizeof(INTERNET_PROXY_INFOW) + proxyBytesRequired + proxyBypassBytesRequired;
556 return ERROR_INSUFFICIENT_BUFFER;
558 proxy = (LPWSTR)((LPBYTE)buffer + sizeof(INTERNET_PROXY_INFOW));
559 proxy_bypass = (LPWSTR)((LPBYTE)buffer + sizeof(INTERNET_PROXY_INFOW) + proxyBytesRequired);
561 pi->dwAccessType = ai->dwAccessType;
562 pi->lpszProxy = NULL;
563 pi->lpszProxyBypass = NULL;
564 if (ai->lpszProxy) {
565 lstrcpyW(proxy, ai->lpszProxy);
566 pi->lpszProxy = proxy;
569 if (ai->lpszProxyBypass) {
570 lstrcpyW(proxy_bypass, ai->lpszProxyBypass);
571 pi->lpszProxyBypass = proxy_bypass;
574 *size = sizeof(INTERNET_PROXY_INFOW) + proxyBytesRequired + proxyBypassBytesRequired;
575 return ERROR_SUCCESS;
576 }else {
577 INTERNET_PROXY_INFOA *pi = (INTERNET_PROXY_INFOA *)buffer;
578 DWORD proxyBytesRequired = 0, proxyBypassBytesRequired = 0;
579 LPSTR proxy, proxy_bypass;
581 if (ai->lpszProxy)
582 proxyBytesRequired = WideCharToMultiByte(CP_ACP, 0, ai->lpszProxy, -1, NULL, 0, NULL, NULL);
583 if (ai->lpszProxyBypass)
584 proxyBypassBytesRequired = WideCharToMultiByte(CP_ACP, 0, ai->lpszProxyBypass, -1,
585 NULL, 0, NULL, NULL);
586 if (*size < sizeof(INTERNET_PROXY_INFOA) + proxyBytesRequired + proxyBypassBytesRequired)
588 *size = sizeof(INTERNET_PROXY_INFOA) + proxyBytesRequired + proxyBypassBytesRequired;
589 return ERROR_INSUFFICIENT_BUFFER;
591 proxy = (LPSTR)((LPBYTE)buffer + sizeof(INTERNET_PROXY_INFOA));
592 proxy_bypass = (LPSTR)((LPBYTE)buffer + sizeof(INTERNET_PROXY_INFOA) + proxyBytesRequired);
594 pi->dwAccessType = ai->dwAccessType;
595 pi->lpszProxy = NULL;
596 pi->lpszProxyBypass = NULL;
597 if (ai->lpszProxy) {
598 WideCharToMultiByte(CP_ACP, 0, ai->lpszProxy, -1, proxy, proxyBytesRequired, NULL, NULL);
599 pi->lpszProxy = proxy;
602 if (ai->lpszProxyBypass) {
603 WideCharToMultiByte(CP_ACP, 0, ai->lpszProxyBypass, -1, proxy_bypass,
604 proxyBypassBytesRequired, NULL, NULL);
605 pi->lpszProxyBypass = proxy_bypass;
608 *size = sizeof(INTERNET_PROXY_INFOA) + proxyBytesRequired + proxyBypassBytesRequired;
609 return ERROR_SUCCESS;
613 return INET_QueryOption(option, buffer, size, unicode);
616 static const object_vtbl_t APPINFOVtbl = {
617 APPINFO_Destroy,
618 NULL,
619 APPINFO_QueryOption,
620 NULL,
621 NULL,
622 NULL,
623 NULL,
624 NULL,
625 NULL
629 /***********************************************************************
630 * InternetOpenW (WININET.@)
632 * Per-application initialization of wininet
634 * RETURNS
635 * HINTERNET on success
636 * NULL on failure
639 HINTERNET WINAPI InternetOpenW(LPCWSTR lpszAgent, DWORD dwAccessType,
640 LPCWSTR lpszProxy, LPCWSTR lpszProxyBypass, DWORD dwFlags)
642 appinfo_t *lpwai = NULL;
643 HINTERNET handle = NULL;
645 if (TRACE_ON(wininet)) {
646 #define FE(x) { x, #x }
647 static const wininet_flag_info access_type[] = {
648 FE(INTERNET_OPEN_TYPE_PRECONFIG),
649 FE(INTERNET_OPEN_TYPE_DIRECT),
650 FE(INTERNET_OPEN_TYPE_PROXY),
651 FE(INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY)
653 #undef FE
654 DWORD i;
655 const char *access_type_str = "Unknown";
657 TRACE("(%s, %i, %s, %s, %i)\n", debugstr_w(lpszAgent), dwAccessType,
658 debugstr_w(lpszProxy), debugstr_w(lpszProxyBypass), dwFlags);
659 for (i = 0; i < (sizeof(access_type) / sizeof(access_type[0])); i++) {
660 if (access_type[i].val == dwAccessType) {
661 access_type_str = access_type[i].name;
662 break;
665 TRACE(" access type : %s\n", access_type_str);
666 TRACE(" flags :");
667 dump_INTERNET_FLAGS(dwFlags);
670 /* Clear any error information */
671 INTERNET_SetLastError(0);
673 lpwai = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(appinfo_t));
674 if (NULL == lpwai)
676 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
677 goto lend;
680 lpwai->hdr.htype = WH_HINIT;
681 lpwai->hdr.vtbl = &APPINFOVtbl;
682 lpwai->hdr.dwFlags = dwFlags;
683 lpwai->hdr.refs = 1;
684 lpwai->dwAccessType = dwAccessType;
685 lpwai->lpszProxyUsername = NULL;
686 lpwai->lpszProxyPassword = NULL;
688 handle = WININET_AllocHandle( &lpwai->hdr );
689 if( !handle )
691 HeapFree( GetProcessHeap(), 0, lpwai );
692 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
693 goto lend;
696 lpwai->lpszAgent = heap_strdupW(lpszAgent);
697 if(dwAccessType == INTERNET_OPEN_TYPE_PRECONFIG)
698 INTERNET_ConfigureProxy( lpwai );
699 else
700 lpwai->lpszProxy = heap_strdupW(lpszProxy);
701 lpwai->lpszProxyBypass = heap_strdupW(lpszProxyBypass);
703 lend:
704 if( lpwai )
705 WININET_Release( &lpwai->hdr );
707 TRACE("returning %p\n", lpwai);
709 return handle;
713 /***********************************************************************
714 * InternetOpenA (WININET.@)
716 * Per-application initialization of wininet
718 * RETURNS
719 * HINTERNET on success
720 * NULL on failure
723 HINTERNET WINAPI InternetOpenA(LPCSTR lpszAgent, DWORD dwAccessType,
724 LPCSTR lpszProxy, LPCSTR lpszProxyBypass, DWORD dwFlags)
726 WCHAR *szAgent, *szProxy, *szBypass;
727 HINTERNET rc;
729 TRACE("(%s, 0x%08x, %s, %s, 0x%08x)\n", debugstr_a(lpszAgent),
730 dwAccessType, debugstr_a(lpszProxy), debugstr_a(lpszProxyBypass), dwFlags);
732 szAgent = heap_strdupAtoW(lpszAgent);
733 szProxy = heap_strdupAtoW(lpszProxy);
734 szBypass = heap_strdupAtoW(lpszProxyBypass);
736 rc = InternetOpenW(szAgent, dwAccessType, szProxy, szBypass, dwFlags);
738 HeapFree(GetProcessHeap(), 0, szAgent);
739 HeapFree(GetProcessHeap(), 0, szProxy);
740 HeapFree(GetProcessHeap(), 0, szBypass);
742 return rc;
745 /***********************************************************************
746 * InternetGetLastResponseInfoA (WININET.@)
748 * Return last wininet error description on the calling thread
750 * RETURNS
751 * TRUE on success of writing to buffer
752 * FALSE on failure
755 BOOL WINAPI InternetGetLastResponseInfoA(LPDWORD lpdwError,
756 LPSTR lpszBuffer, LPDWORD lpdwBufferLength)
758 LPWITHREADERROR lpwite = TlsGetValue(g_dwTlsErrIndex);
760 TRACE("\n");
762 if (lpwite)
764 *lpdwError = lpwite->dwError;
765 if (lpwite->dwError)
767 memcpy(lpszBuffer, lpwite->response, *lpdwBufferLength);
768 *lpdwBufferLength = strlen(lpszBuffer);
770 else
771 *lpdwBufferLength = 0;
773 else
775 *lpdwError = 0;
776 *lpdwBufferLength = 0;
779 return TRUE;
782 /***********************************************************************
783 * InternetGetLastResponseInfoW (WININET.@)
785 * Return last wininet error description on the calling thread
787 * RETURNS
788 * TRUE on success of writing to buffer
789 * FALSE on failure
792 BOOL WINAPI InternetGetLastResponseInfoW(LPDWORD lpdwError,
793 LPWSTR lpszBuffer, LPDWORD lpdwBufferLength)
795 LPWITHREADERROR lpwite = TlsGetValue(g_dwTlsErrIndex);
797 TRACE("\n");
799 if (lpwite)
801 *lpdwError = lpwite->dwError;
802 if (lpwite->dwError)
804 memcpy(lpszBuffer, lpwite->response, *lpdwBufferLength);
805 *lpdwBufferLength = lstrlenW(lpszBuffer);
807 else
808 *lpdwBufferLength = 0;
810 else
812 *lpdwError = 0;
813 *lpdwBufferLength = 0;
816 return TRUE;
819 /***********************************************************************
820 * InternetGetConnectedState (WININET.@)
822 * Return connected state
824 * RETURNS
825 * TRUE if connected
826 * if lpdwStatus is not null, return the status (off line,
827 * modem, lan...) in it.
828 * FALSE if not connected
830 BOOL WINAPI InternetGetConnectedState(LPDWORD lpdwStatus, DWORD dwReserved)
832 TRACE("(%p, 0x%08x)\n", lpdwStatus, dwReserved);
834 if (lpdwStatus) {
835 WARN("always returning LAN connection.\n");
836 *lpdwStatus = INTERNET_CONNECTION_LAN;
838 return TRUE;
842 /***********************************************************************
843 * InternetGetConnectedStateExW (WININET.@)
845 * Return connected state
847 * PARAMS
849 * lpdwStatus [O] Flags specifying the status of the internet connection.
850 * lpszConnectionName [O] Pointer to buffer to receive the friendly name of the internet connection.
851 * dwNameLen [I] Size of the buffer, in characters.
852 * dwReserved [I] Reserved. Must be set to 0.
854 * RETURNS
855 * TRUE if connected
856 * if lpdwStatus is not null, return the status (off line,
857 * modem, lan...) in it.
858 * FALSE if not connected
860 * NOTES
861 * If the system has no available network connections, an empty string is
862 * stored in lpszConnectionName. If there is a LAN connection, a localized
863 * "LAN Connection" string is stored. Presumably, if only a dial-up
864 * connection is available then the name of the dial-up connection is
865 * returned. Why any application, other than the "Internet Settings" CPL,
866 * would want to use this function instead of the simpler InternetGetConnectedStateW
867 * function is beyond me.
869 BOOL WINAPI InternetGetConnectedStateExW(LPDWORD lpdwStatus, LPWSTR lpszConnectionName,
870 DWORD dwNameLen, DWORD dwReserved)
872 TRACE("(%p, %p, %d, 0x%08x)\n", lpdwStatus, lpszConnectionName, dwNameLen, dwReserved);
874 /* Must be zero */
875 if(dwReserved)
876 return FALSE;
878 if (lpdwStatus) {
879 WARN("always returning LAN connection.\n");
880 *lpdwStatus = INTERNET_CONNECTION_LAN;
882 return LoadStringW(WININET_hModule, IDS_LANCONNECTION, lpszConnectionName, dwNameLen);
886 /***********************************************************************
887 * InternetGetConnectedStateExA (WININET.@)
889 BOOL WINAPI InternetGetConnectedStateExA(LPDWORD lpdwStatus, LPSTR lpszConnectionName,
890 DWORD dwNameLen, DWORD dwReserved)
892 LPWSTR lpwszConnectionName = NULL;
893 BOOL rc;
895 TRACE("(%p, %p, %d, 0x%08x)\n", lpdwStatus, lpszConnectionName, dwNameLen, dwReserved);
897 if (lpszConnectionName && dwNameLen > 0)
898 lpwszConnectionName= HeapAlloc(GetProcessHeap(), 0, dwNameLen * sizeof(WCHAR));
900 rc = InternetGetConnectedStateExW(lpdwStatus,lpwszConnectionName, dwNameLen,
901 dwReserved);
902 if (rc && lpwszConnectionName)
904 WideCharToMultiByte(CP_ACP,0,lpwszConnectionName,-1,lpszConnectionName,
905 dwNameLen, NULL, NULL);
907 HeapFree(GetProcessHeap(),0,lpwszConnectionName);
910 return rc;
914 /***********************************************************************
915 * InternetConnectW (WININET.@)
917 * Open a ftp, gopher or http session
919 * RETURNS
920 * HINTERNET a session handle on success
921 * NULL on failure
924 HINTERNET WINAPI InternetConnectW(HINTERNET hInternet,
925 LPCWSTR lpszServerName, INTERNET_PORT nServerPort,
926 LPCWSTR lpszUserName, LPCWSTR lpszPassword,
927 DWORD dwService, DWORD dwFlags, DWORD_PTR dwContext)
929 appinfo_t *hIC;
930 HINTERNET rc = NULL;
932 TRACE("(%p, %s, %i, %s, %s, %i, %i, %lx)\n", hInternet, debugstr_w(lpszServerName),
933 nServerPort, debugstr_w(lpszUserName), debugstr_w(lpszPassword),
934 dwService, dwFlags, dwContext);
936 if (!lpszServerName)
938 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
939 return NULL;
942 /* Clear any error information */
943 INTERNET_SetLastError(0);
944 hIC = (appinfo_t*)WININET_GetObject( hInternet );
945 if ( (hIC == NULL) || (hIC->hdr.htype != WH_HINIT) )
947 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
948 goto lend;
951 switch (dwService)
953 case INTERNET_SERVICE_FTP:
954 rc = FTP_Connect(hIC, lpszServerName, nServerPort,
955 lpszUserName, lpszPassword, dwFlags, dwContext, 0);
956 break;
958 case INTERNET_SERVICE_HTTP:
959 rc = HTTP_Connect(hIC, lpszServerName, nServerPort,
960 lpszUserName, lpszPassword, dwFlags, dwContext, 0);
961 break;
963 case INTERNET_SERVICE_GOPHER:
964 default:
965 break;
967 lend:
968 if( hIC )
969 WININET_Release( &hIC->hdr );
971 TRACE("returning %p\n", rc);
972 return rc;
976 /***********************************************************************
977 * InternetConnectA (WININET.@)
979 * Open a ftp, gopher or http session
981 * RETURNS
982 * HINTERNET a session handle on success
983 * NULL on failure
986 HINTERNET WINAPI InternetConnectA(HINTERNET hInternet,
987 LPCSTR lpszServerName, INTERNET_PORT nServerPort,
988 LPCSTR lpszUserName, LPCSTR lpszPassword,
989 DWORD dwService, DWORD dwFlags, DWORD_PTR dwContext)
991 HINTERNET rc = NULL;
992 LPWSTR szServerName;
993 LPWSTR szUserName;
994 LPWSTR szPassword;
996 szServerName = heap_strdupAtoW(lpszServerName);
997 szUserName = heap_strdupAtoW(lpszUserName);
998 szPassword = heap_strdupAtoW(lpszPassword);
1000 rc = InternetConnectW(hInternet, szServerName, nServerPort,
1001 szUserName, szPassword, dwService, dwFlags, dwContext);
1003 HeapFree(GetProcessHeap(), 0, szServerName);
1004 HeapFree(GetProcessHeap(), 0, szUserName);
1005 HeapFree(GetProcessHeap(), 0, szPassword);
1006 return rc;
1010 /***********************************************************************
1011 * InternetFindNextFileA (WININET.@)
1013 * Continues a file search from a previous call to FindFirstFile
1015 * RETURNS
1016 * TRUE on success
1017 * FALSE on failure
1020 BOOL WINAPI InternetFindNextFileA(HINTERNET hFind, LPVOID lpvFindData)
1022 BOOL ret;
1023 WIN32_FIND_DATAW fd;
1025 ret = InternetFindNextFileW(hFind, lpvFindData?&fd:NULL);
1026 if(lpvFindData)
1027 WININET_find_data_WtoA(&fd, (LPWIN32_FIND_DATAA)lpvFindData);
1028 return ret;
1031 /***********************************************************************
1032 * InternetFindNextFileW (WININET.@)
1034 * Continues a file search from a previous call to FindFirstFile
1036 * RETURNS
1037 * TRUE on success
1038 * FALSE on failure
1041 BOOL WINAPI InternetFindNextFileW(HINTERNET hFind, LPVOID lpvFindData)
1043 object_header_t *hdr;
1044 DWORD res;
1046 TRACE("\n");
1048 hdr = WININET_GetObject(hFind);
1049 if(!hdr) {
1050 WARN("Invalid handle\n");
1051 SetLastError(ERROR_INVALID_HANDLE);
1052 return FALSE;
1055 if(hdr->vtbl->FindNextFileW) {
1056 res = hdr->vtbl->FindNextFileW(hdr, lpvFindData);
1057 }else {
1058 WARN("Handle doesn't support NextFile\n");
1059 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
1062 WININET_Release(hdr);
1064 if(res != ERROR_SUCCESS)
1065 SetLastError(res);
1066 return res == ERROR_SUCCESS;
1069 /***********************************************************************
1070 * InternetCloseHandle (WININET.@)
1072 * Generic close handle function
1074 * RETURNS
1075 * TRUE on success
1076 * FALSE on failure
1079 BOOL WINAPI InternetCloseHandle(HINTERNET hInternet)
1081 object_header_t *lpwh;
1083 TRACE("%p\n",hInternet);
1085 lpwh = WININET_GetObject( hInternet );
1086 if (NULL == lpwh)
1088 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
1089 return FALSE;
1092 WININET_Release( lpwh );
1093 WININET_FreeHandle( hInternet );
1095 return TRUE;
1099 /***********************************************************************
1100 * ConvertUrlComponentValue (Internal)
1102 * Helper function for InternetCrackUrlA
1105 static void ConvertUrlComponentValue(LPSTR* lppszComponent, LPDWORD dwComponentLen,
1106 LPWSTR lpwszComponent, DWORD dwwComponentLen,
1107 LPCSTR lpszStart, LPCWSTR lpwszStart)
1109 TRACE("%p %d %p %d %p %p\n", *lppszComponent, *dwComponentLen, lpwszComponent, dwwComponentLen, lpszStart, lpwszStart);
1110 if (*dwComponentLen != 0)
1112 DWORD nASCIILength=WideCharToMultiByte(CP_ACP,0,lpwszComponent,dwwComponentLen,NULL,0,NULL,NULL);
1113 if (*lppszComponent == NULL)
1115 int nASCIIOffset=WideCharToMultiByte(CP_ACP,0,lpwszStart,lpwszComponent-lpwszStart,NULL,0,NULL,NULL);
1116 if (lpwszComponent)
1117 *lppszComponent = (LPSTR)lpszStart+nASCIIOffset;
1118 else
1119 *lppszComponent = NULL;
1120 *dwComponentLen = nASCIILength;
1122 else
1124 DWORD ncpylen = min((*dwComponentLen)-1, nASCIILength);
1125 WideCharToMultiByte(CP_ACP,0,lpwszComponent,dwwComponentLen,*lppszComponent,ncpylen+1,NULL,NULL);
1126 (*lppszComponent)[ncpylen]=0;
1127 *dwComponentLen = ncpylen;
1133 /***********************************************************************
1134 * InternetCrackUrlA (WININET.@)
1136 * See InternetCrackUrlW.
1138 BOOL WINAPI InternetCrackUrlA(LPCSTR lpszUrl, DWORD dwUrlLength, DWORD dwFlags,
1139 LPURL_COMPONENTSA lpUrlComponents)
1141 DWORD nLength;
1142 URL_COMPONENTSW UCW;
1143 BOOL ret = FALSE;
1144 WCHAR *lpwszUrl, *hostname = NULL, *username = NULL, *password = NULL, *path = NULL,
1145 *scheme = NULL, *extra = NULL;
1147 TRACE("(%s %u %x %p)\n",
1148 lpszUrl ? debugstr_an(lpszUrl, dwUrlLength ? dwUrlLength : strlen(lpszUrl)) : "(null)",
1149 dwUrlLength, dwFlags, lpUrlComponents);
1151 if (!lpszUrl || !*lpszUrl || !lpUrlComponents ||
1152 lpUrlComponents->dwStructSize != sizeof(URL_COMPONENTSA))
1154 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1155 return FALSE;
1158 if(dwUrlLength<=0)
1159 dwUrlLength=-1;
1160 nLength=MultiByteToWideChar(CP_ACP,0,lpszUrl,dwUrlLength,NULL,0);
1162 /* if dwUrlLength=-1 then nLength includes null but length to
1163 InternetCrackUrlW should not include it */
1164 if (dwUrlLength == -1) nLength--;
1166 lpwszUrl=HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WCHAR)*nLength);
1167 MultiByteToWideChar(CP_ACP,0,lpszUrl,dwUrlLength,lpwszUrl,nLength);
1169 memset(&UCW,0,sizeof(UCW));
1170 UCW.dwStructSize = sizeof(URL_COMPONENTSW);
1171 if (lpUrlComponents->dwHostNameLength)
1173 UCW.dwHostNameLength = lpUrlComponents->dwHostNameLength;
1174 if (lpUrlComponents->lpszHostName)
1176 hostname = HeapAlloc(GetProcessHeap(), 0, UCW.dwHostNameLength * sizeof(WCHAR));
1177 UCW.lpszHostName = hostname;
1180 if (lpUrlComponents->dwUserNameLength)
1182 UCW.dwUserNameLength = lpUrlComponents->dwUserNameLength;
1183 if (lpUrlComponents->lpszUserName)
1185 username = HeapAlloc(GetProcessHeap(), 0, UCW.dwUserNameLength * sizeof(WCHAR));
1186 UCW.lpszUserName = username;
1189 if (lpUrlComponents->dwPasswordLength)
1191 UCW.dwPasswordLength = lpUrlComponents->dwPasswordLength;
1192 if (lpUrlComponents->lpszPassword)
1194 password = HeapAlloc(GetProcessHeap(), 0, UCW.dwPasswordLength * sizeof(WCHAR));
1195 UCW.lpszPassword = password;
1198 if (lpUrlComponents->dwUrlPathLength)
1200 UCW.dwUrlPathLength = lpUrlComponents->dwUrlPathLength;
1201 if (lpUrlComponents->lpszUrlPath)
1203 path = HeapAlloc(GetProcessHeap(), 0, UCW.dwUrlPathLength * sizeof(WCHAR));
1204 UCW.lpszUrlPath = path;
1207 if (lpUrlComponents->dwSchemeLength)
1209 UCW.dwSchemeLength = lpUrlComponents->dwSchemeLength;
1210 if (lpUrlComponents->lpszScheme)
1212 scheme = HeapAlloc(GetProcessHeap(), 0, UCW.dwSchemeLength * sizeof(WCHAR));
1213 UCW.lpszScheme = scheme;
1216 if (lpUrlComponents->dwExtraInfoLength)
1218 UCW.dwExtraInfoLength = lpUrlComponents->dwExtraInfoLength;
1219 if (lpUrlComponents->lpszExtraInfo)
1221 extra = HeapAlloc(GetProcessHeap(), 0, UCW.dwExtraInfoLength * sizeof(WCHAR));
1222 UCW.lpszExtraInfo = extra;
1225 if ((ret = InternetCrackUrlW(lpwszUrl, nLength, dwFlags, &UCW)))
1227 ConvertUrlComponentValue(&lpUrlComponents->lpszHostName, &lpUrlComponents->dwHostNameLength,
1228 UCW.lpszHostName, UCW.dwHostNameLength, lpszUrl, lpwszUrl);
1229 ConvertUrlComponentValue(&lpUrlComponents->lpszUserName, &lpUrlComponents->dwUserNameLength,
1230 UCW.lpszUserName, UCW.dwUserNameLength, lpszUrl, lpwszUrl);
1231 ConvertUrlComponentValue(&lpUrlComponents->lpszPassword, &lpUrlComponents->dwPasswordLength,
1232 UCW.lpszPassword, UCW.dwPasswordLength, lpszUrl, lpwszUrl);
1233 ConvertUrlComponentValue(&lpUrlComponents->lpszUrlPath, &lpUrlComponents->dwUrlPathLength,
1234 UCW.lpszUrlPath, UCW.dwUrlPathLength, lpszUrl, lpwszUrl);
1235 ConvertUrlComponentValue(&lpUrlComponents->lpszScheme, &lpUrlComponents->dwSchemeLength,
1236 UCW.lpszScheme, UCW.dwSchemeLength, lpszUrl, lpwszUrl);
1237 ConvertUrlComponentValue(&lpUrlComponents->lpszExtraInfo, &lpUrlComponents->dwExtraInfoLength,
1238 UCW.lpszExtraInfo, UCW.dwExtraInfoLength, lpszUrl, lpwszUrl);
1240 lpUrlComponents->nScheme = UCW.nScheme;
1241 lpUrlComponents->nPort = UCW.nPort;
1243 TRACE("%s: scheme(%s) host(%s) path(%s) extra(%s)\n", lpszUrl,
1244 debugstr_an(lpUrlComponents->lpszScheme, lpUrlComponents->dwSchemeLength),
1245 debugstr_an(lpUrlComponents->lpszHostName, lpUrlComponents->dwHostNameLength),
1246 debugstr_an(lpUrlComponents->lpszUrlPath, lpUrlComponents->dwUrlPathLength),
1247 debugstr_an(lpUrlComponents->lpszExtraInfo, lpUrlComponents->dwExtraInfoLength));
1249 HeapFree(GetProcessHeap(), 0, lpwszUrl);
1250 HeapFree(GetProcessHeap(), 0, hostname);
1251 HeapFree(GetProcessHeap(), 0, username);
1252 HeapFree(GetProcessHeap(), 0, password);
1253 HeapFree(GetProcessHeap(), 0, path);
1254 HeapFree(GetProcessHeap(), 0, scheme);
1255 HeapFree(GetProcessHeap(), 0, extra);
1256 return ret;
1259 static const WCHAR url_schemes[][7] =
1261 {'f','t','p',0},
1262 {'g','o','p','h','e','r',0},
1263 {'h','t','t','p',0},
1264 {'h','t','t','p','s',0},
1265 {'f','i','l','e',0},
1266 {'n','e','w','s',0},
1267 {'m','a','i','l','t','o',0},
1268 {'r','e','s',0},
1271 /***********************************************************************
1272 * GetInternetSchemeW (internal)
1274 * Get scheme of url
1276 * RETURNS
1277 * scheme on success
1278 * INTERNET_SCHEME_UNKNOWN on failure
1281 static INTERNET_SCHEME GetInternetSchemeW(LPCWSTR lpszScheme, DWORD nMaxCmp)
1283 int i;
1285 TRACE("%s %d\n",debugstr_wn(lpszScheme, nMaxCmp), nMaxCmp);
1287 if(lpszScheme==NULL)
1288 return INTERNET_SCHEME_UNKNOWN;
1290 for (i = 0; i < sizeof(url_schemes)/sizeof(url_schemes[0]); i++)
1291 if (!strncmpW(lpszScheme, url_schemes[i], nMaxCmp))
1292 return INTERNET_SCHEME_FIRST + i;
1294 return INTERNET_SCHEME_UNKNOWN;
1297 /***********************************************************************
1298 * SetUrlComponentValueW (Internal)
1300 * Helper function for InternetCrackUrlW
1302 * PARAMS
1303 * lppszComponent [O] Holds the returned string
1304 * dwComponentLen [I] Holds the size of lppszComponent
1305 * [O] Holds the length of the string in lppszComponent without '\0'
1306 * lpszStart [I] Holds the string to copy from
1307 * len [I] Holds the length of lpszStart without '\0'
1309 * RETURNS
1310 * TRUE on success
1311 * FALSE on failure
1314 static BOOL SetUrlComponentValueW(LPWSTR* lppszComponent, LPDWORD dwComponentLen, LPCWSTR lpszStart, DWORD len)
1316 TRACE("%s (%d)\n", debugstr_wn(lpszStart,len), len);
1318 if ( (*dwComponentLen == 0) && (*lppszComponent == NULL) )
1319 return FALSE;
1321 if (*dwComponentLen != 0 || *lppszComponent == NULL)
1323 if (*lppszComponent == NULL)
1325 *lppszComponent = (LPWSTR)lpszStart;
1326 *dwComponentLen = len;
1328 else
1330 DWORD ncpylen = min((*dwComponentLen)-1, len);
1331 memcpy(*lppszComponent, lpszStart, ncpylen*sizeof(WCHAR));
1332 (*lppszComponent)[ncpylen] = '\0';
1333 *dwComponentLen = ncpylen;
1337 return TRUE;
1340 /***********************************************************************
1341 * InternetCrackUrlW (WININET.@)
1343 * Break up URL into its components
1345 * RETURNS
1346 * TRUE on success
1347 * FALSE on failure
1349 BOOL WINAPI InternetCrackUrlW(LPCWSTR lpszUrl_orig, DWORD dwUrlLength_orig, DWORD dwFlags,
1350 LPURL_COMPONENTSW lpUC)
1353 * RFC 1808
1354 * <protocol>:[//<net_loc>][/path][;<params>][?<query>][#<fragment>]
1357 LPCWSTR lpszParam = NULL;
1358 BOOL bIsAbsolute = FALSE;
1359 LPCWSTR lpszap, lpszUrl = lpszUrl_orig;
1360 LPCWSTR lpszcp = NULL;
1361 LPWSTR lpszUrl_decode = NULL;
1362 DWORD dwUrlLength = dwUrlLength_orig;
1364 TRACE("(%s %u %x %p)\n",
1365 lpszUrl ? debugstr_wn(lpszUrl, dwUrlLength ? dwUrlLength : strlenW(lpszUrl)) : "(null)",
1366 dwUrlLength, dwFlags, lpUC);
1368 if (!lpszUrl_orig || !*lpszUrl_orig || !lpUC)
1370 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1371 return FALSE;
1373 if (!dwUrlLength) dwUrlLength = strlenW(lpszUrl);
1375 if (dwFlags & ICU_DECODE)
1377 WCHAR *url_tmp;
1378 DWORD len = dwUrlLength + 1;
1380 if (!(url_tmp = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR))))
1382 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1383 return FALSE;
1385 memcpy(url_tmp, lpszUrl_orig, dwUrlLength * sizeof(WCHAR));
1386 url_tmp[dwUrlLength] = 0;
1387 if (!(lpszUrl_decode = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR))))
1389 HeapFree(GetProcessHeap(), 0, url_tmp);
1390 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1391 return FALSE;
1393 if (InternetCanonicalizeUrlW(url_tmp, lpszUrl_decode, &len, ICU_DECODE | ICU_NO_ENCODE))
1395 dwUrlLength = len;
1396 lpszUrl = lpszUrl_decode;
1398 HeapFree(GetProcessHeap(), 0, url_tmp);
1400 lpszap = lpszUrl;
1402 /* Determine if the URI is absolute. */
1403 while (lpszap - lpszUrl < dwUrlLength)
1405 if (isalnumW(*lpszap))
1407 lpszap++;
1408 continue;
1410 if ((*lpszap == ':') && (lpszap - lpszUrl >= 2))
1412 bIsAbsolute = TRUE;
1413 lpszcp = lpszap;
1415 else
1417 lpszcp = lpszUrl; /* Relative url */
1420 break;
1423 lpUC->nScheme = INTERNET_SCHEME_UNKNOWN;
1424 lpUC->nPort = INTERNET_INVALID_PORT_NUMBER;
1426 /* Parse <params> */
1427 if (!(lpszParam = memchrW(lpszap, ';', dwUrlLength - (lpszap - lpszUrl))))
1428 lpszParam = memchrW(lpszap, '?', dwUrlLength - (lpszap - lpszUrl));
1430 SetUrlComponentValueW(&lpUC->lpszExtraInfo, &lpUC->dwExtraInfoLength,
1431 lpszParam, lpszParam ? dwUrlLength-(lpszParam-lpszUrl) : 0);
1433 if (bIsAbsolute) /* Parse <protocol>:[//<net_loc>] */
1435 LPCWSTR lpszNetLoc;
1437 /* Get scheme first. */
1438 lpUC->nScheme = GetInternetSchemeW(lpszUrl, lpszcp - lpszUrl);
1439 SetUrlComponentValueW(&lpUC->lpszScheme, &lpUC->dwSchemeLength,
1440 lpszUrl, lpszcp - lpszUrl);
1442 /* Eat ':' in protocol. */
1443 lpszcp++;
1445 /* double slash indicates the net_loc portion is present */
1446 if ((lpszcp[0] == '/') && (lpszcp[1] == '/'))
1448 lpszcp += 2;
1450 lpszNetLoc = memchrW(lpszcp, '/', dwUrlLength - (lpszcp - lpszUrl));
1451 if (lpszParam)
1453 if (lpszNetLoc)
1454 lpszNetLoc = min(lpszNetLoc, lpszParam);
1455 else
1456 lpszNetLoc = lpszParam;
1458 else if (!lpszNetLoc)
1459 lpszNetLoc = lpszcp + dwUrlLength-(lpszcp-lpszUrl);
1461 /* Parse net-loc */
1462 if (lpszNetLoc)
1464 LPCWSTR lpszHost;
1465 LPCWSTR lpszPort;
1467 /* [<user>[<:password>]@]<host>[:<port>] */
1468 /* First find the user and password if they exist */
1470 lpszHost = memchrW(lpszcp, '@', dwUrlLength - (lpszcp - lpszUrl));
1471 if (lpszHost == NULL || lpszHost > lpszNetLoc)
1473 /* username and password not specified. */
1474 SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength, NULL, 0);
1475 SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength, NULL, 0);
1477 else /* Parse out username and password */
1479 LPCWSTR lpszUser = lpszcp;
1480 LPCWSTR lpszPasswd = lpszHost;
1482 while (lpszcp < lpszHost)
1484 if (*lpszcp == ':')
1485 lpszPasswd = lpszcp;
1487 lpszcp++;
1490 SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength,
1491 lpszUser, lpszPasswd - lpszUser);
1493 if (lpszPasswd != lpszHost)
1494 lpszPasswd++;
1495 SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength,
1496 lpszPasswd == lpszHost ? NULL : lpszPasswd,
1497 lpszHost - lpszPasswd);
1499 lpszcp++; /* Advance to beginning of host */
1502 /* Parse <host><:port> */
1504 lpszHost = lpszcp;
1505 lpszPort = lpszNetLoc;
1507 /* special case for res:// URLs: there is no port here, so the host is the
1508 entire string up to the first '/' */
1509 if(lpUC->nScheme==INTERNET_SCHEME_RES)
1511 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength,
1512 lpszHost, lpszPort - lpszHost);
1513 lpszcp=lpszNetLoc;
1515 else
1517 while (lpszcp < lpszNetLoc)
1519 if (*lpszcp == ':')
1520 lpszPort = lpszcp;
1522 lpszcp++;
1525 /* If the scheme is "file" and the host is just one letter, it's not a host */
1526 if(lpUC->nScheme==INTERNET_SCHEME_FILE && (lpszPort-lpszHost)==1)
1528 lpszcp=lpszHost;
1529 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength,
1530 NULL, 0);
1532 else
1534 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength,
1535 lpszHost, lpszPort - lpszHost);
1536 if (lpszPort != lpszNetLoc)
1537 lpUC->nPort = atoiW(++lpszPort);
1538 else switch (lpUC->nScheme)
1540 case INTERNET_SCHEME_HTTP:
1541 lpUC->nPort = INTERNET_DEFAULT_HTTP_PORT;
1542 break;
1543 case INTERNET_SCHEME_HTTPS:
1544 lpUC->nPort = INTERNET_DEFAULT_HTTPS_PORT;
1545 break;
1546 case INTERNET_SCHEME_FTP:
1547 lpUC->nPort = INTERNET_DEFAULT_FTP_PORT;
1548 break;
1549 case INTERNET_SCHEME_GOPHER:
1550 lpUC->nPort = INTERNET_DEFAULT_GOPHER_PORT;
1551 break;
1552 default:
1553 break;
1559 else
1561 SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength, NULL, 0);
1562 SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength, NULL, 0);
1563 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength, NULL, 0);
1566 else
1568 SetUrlComponentValueW(&lpUC->lpszScheme, &lpUC->dwSchemeLength, NULL, 0);
1569 SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength, NULL, 0);
1570 SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength, NULL, 0);
1571 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength, NULL, 0);
1574 /* Here lpszcp points to:
1576 * <protocol>:[//<net_loc>][/path][;<params>][?<query>][#<fragment>]
1577 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1579 if (lpszcp != 0 && lpszcp - lpszUrl < dwUrlLength && (!lpszParam || lpszcp < lpszParam))
1581 INT len;
1583 /* Only truncate the parameter list if it's already been saved
1584 * in lpUC->lpszExtraInfo.
1586 if (lpszParam && lpUC->dwExtraInfoLength && lpUC->lpszExtraInfo)
1587 len = lpszParam - lpszcp;
1588 else
1590 /* Leave the parameter list in lpszUrlPath. Strip off any trailing
1591 * newlines if necessary.
1593 LPWSTR lpsznewline = memchrW(lpszcp, '\n', dwUrlLength - (lpszcp - lpszUrl));
1594 if (lpsznewline != NULL)
1595 len = lpsznewline - lpszcp;
1596 else
1597 len = dwUrlLength-(lpszcp-lpszUrl);
1599 SetUrlComponentValueW(&lpUC->lpszUrlPath, &lpUC->dwUrlPathLength,
1600 lpszcp, len);
1602 else
1604 if (lpUC->lpszUrlPath && (lpUC->dwUrlPathLength > 0))
1605 lpUC->lpszUrlPath[0] = 0;
1606 lpUC->dwUrlPathLength = 0;
1609 TRACE("%s: scheme(%s) host(%s) path(%s) extra(%s)\n", debugstr_wn(lpszUrl,dwUrlLength),
1610 debugstr_wn(lpUC->lpszScheme,lpUC->dwSchemeLength),
1611 debugstr_wn(lpUC->lpszHostName,lpUC->dwHostNameLength),
1612 debugstr_wn(lpUC->lpszUrlPath,lpUC->dwUrlPathLength),
1613 debugstr_wn(lpUC->lpszExtraInfo,lpUC->dwExtraInfoLength));
1615 HeapFree(GetProcessHeap(), 0, lpszUrl_decode );
1616 return TRUE;
1619 /***********************************************************************
1620 * InternetAttemptConnect (WININET.@)
1622 * Attempt to make a connection to the internet
1624 * RETURNS
1625 * ERROR_SUCCESS on success
1626 * Error value on failure
1629 DWORD WINAPI InternetAttemptConnect(DWORD dwReserved)
1631 FIXME("Stub\n");
1632 return ERROR_SUCCESS;
1636 /***********************************************************************
1637 * InternetCanonicalizeUrlA (WININET.@)
1639 * Escape unsafe characters and spaces
1641 * RETURNS
1642 * TRUE on success
1643 * FALSE on failure
1646 BOOL WINAPI InternetCanonicalizeUrlA(LPCSTR lpszUrl, LPSTR lpszBuffer,
1647 LPDWORD lpdwBufferLength, DWORD dwFlags)
1649 HRESULT hr;
1650 DWORD dwURLFlags = URL_WININET_COMPATIBILITY | URL_ESCAPE_UNSAFE;
1652 TRACE("(%s, %p, %p, 0x%08x) bufferlength: %d\n", debugstr_a(lpszUrl), lpszBuffer,
1653 lpdwBufferLength, lpdwBufferLength ? *lpdwBufferLength : -1, dwFlags);
1655 if(dwFlags & ICU_DECODE)
1657 dwURLFlags |= URL_UNESCAPE;
1658 dwFlags &= ~ICU_DECODE;
1661 if(dwFlags & ICU_ESCAPE)
1663 dwURLFlags |= URL_UNESCAPE;
1664 dwFlags &= ~ICU_ESCAPE;
1667 if(dwFlags & ICU_BROWSER_MODE)
1669 dwURLFlags |= URL_BROWSER_MODE;
1670 dwFlags &= ~ICU_BROWSER_MODE;
1673 if(dwFlags & ICU_NO_ENCODE)
1675 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
1676 dwURLFlags ^= URL_ESCAPE_UNSAFE;
1677 dwFlags &= ~ICU_NO_ENCODE;
1680 if (dwFlags) FIXME("Unhandled flags 0x%08x\n", dwFlags);
1682 hr = UrlCanonicalizeA(lpszUrl, lpszBuffer, lpdwBufferLength, dwURLFlags);
1683 if (hr == E_POINTER) SetLastError(ERROR_INSUFFICIENT_BUFFER);
1684 if (hr == E_INVALIDARG) SetLastError(ERROR_INVALID_PARAMETER);
1686 return (hr == S_OK) ? TRUE : FALSE;
1689 /***********************************************************************
1690 * InternetCanonicalizeUrlW (WININET.@)
1692 * Escape unsafe characters and spaces
1694 * RETURNS
1695 * TRUE on success
1696 * FALSE on failure
1699 BOOL WINAPI InternetCanonicalizeUrlW(LPCWSTR lpszUrl, LPWSTR lpszBuffer,
1700 LPDWORD lpdwBufferLength, DWORD dwFlags)
1702 HRESULT hr;
1703 DWORD dwURLFlags = URL_WININET_COMPATIBILITY | URL_ESCAPE_UNSAFE;
1705 TRACE("(%s, %p, %p, 0x%08x) bufferlength: %d\n", debugstr_w(lpszUrl), lpszBuffer,
1706 lpdwBufferLength, dwFlags, lpdwBufferLength ? *lpdwBufferLength : -1);
1708 if(dwFlags & ICU_DECODE)
1710 dwURLFlags |= URL_UNESCAPE;
1711 dwFlags &= ~ICU_DECODE;
1714 if(dwFlags & ICU_ESCAPE)
1716 dwURLFlags |= URL_UNESCAPE;
1717 dwFlags &= ~ICU_ESCAPE;
1720 if(dwFlags & ICU_BROWSER_MODE)
1722 dwURLFlags |= URL_BROWSER_MODE;
1723 dwFlags &= ~ICU_BROWSER_MODE;
1726 if(dwFlags & ICU_NO_ENCODE)
1728 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
1729 dwURLFlags ^= URL_ESCAPE_UNSAFE;
1730 dwFlags &= ~ICU_NO_ENCODE;
1733 if (dwFlags) FIXME("Unhandled flags 0x%08x\n", dwFlags);
1735 hr = UrlCanonicalizeW(lpszUrl, lpszBuffer, lpdwBufferLength, dwURLFlags);
1736 if (hr == E_POINTER) SetLastError(ERROR_INSUFFICIENT_BUFFER);
1737 if (hr == E_INVALIDARG) SetLastError(ERROR_INVALID_PARAMETER);
1739 return (hr == S_OK) ? TRUE : FALSE;
1742 /* #################################################### */
1744 static INTERNET_STATUS_CALLBACK set_status_callback(
1745 object_header_t *lpwh, INTERNET_STATUS_CALLBACK callback, BOOL unicode)
1747 INTERNET_STATUS_CALLBACK ret;
1749 if (unicode) lpwh->dwInternalFlags |= INET_CALLBACKW;
1750 else lpwh->dwInternalFlags &= ~INET_CALLBACKW;
1752 ret = lpwh->lpfnStatusCB;
1753 lpwh->lpfnStatusCB = callback;
1755 return ret;
1758 /***********************************************************************
1759 * InternetSetStatusCallbackA (WININET.@)
1761 * Sets up a callback function which is called as progress is made
1762 * during an operation.
1764 * RETURNS
1765 * Previous callback or NULL on success
1766 * INTERNET_INVALID_STATUS_CALLBACK on failure
1769 INTERNET_STATUS_CALLBACK WINAPI InternetSetStatusCallbackA(
1770 HINTERNET hInternet ,INTERNET_STATUS_CALLBACK lpfnIntCB)
1772 INTERNET_STATUS_CALLBACK retVal;
1773 object_header_t *lpwh;
1775 TRACE("%p\n", hInternet);
1777 if (!(lpwh = WININET_GetObject(hInternet)))
1778 return INTERNET_INVALID_STATUS_CALLBACK;
1780 retVal = set_status_callback(lpwh, lpfnIntCB, FALSE);
1782 WININET_Release( lpwh );
1783 return retVal;
1786 /***********************************************************************
1787 * InternetSetStatusCallbackW (WININET.@)
1789 * Sets up a callback function which is called as progress is made
1790 * during an operation.
1792 * RETURNS
1793 * Previous callback or NULL on success
1794 * INTERNET_INVALID_STATUS_CALLBACK on failure
1797 INTERNET_STATUS_CALLBACK WINAPI InternetSetStatusCallbackW(
1798 HINTERNET hInternet ,INTERNET_STATUS_CALLBACK lpfnIntCB)
1800 INTERNET_STATUS_CALLBACK retVal;
1801 object_header_t *lpwh;
1803 TRACE("%p\n", hInternet);
1805 if (!(lpwh = WININET_GetObject(hInternet)))
1806 return INTERNET_INVALID_STATUS_CALLBACK;
1808 retVal = set_status_callback(lpwh, lpfnIntCB, TRUE);
1810 WININET_Release( lpwh );
1811 return retVal;
1814 /***********************************************************************
1815 * InternetSetFilePointer (WININET.@)
1817 DWORD WINAPI InternetSetFilePointer(HINTERNET hFile, LONG lDistanceToMove,
1818 PVOID pReserved, DWORD dwMoveContext, DWORD_PTR dwContext)
1820 FIXME("stub\n");
1821 return FALSE;
1824 /***********************************************************************
1825 * InternetWriteFile (WININET.@)
1827 * Write data to an open internet file
1829 * RETURNS
1830 * TRUE on success
1831 * FALSE on failure
1834 BOOL WINAPI InternetWriteFile(HINTERNET hFile, LPCVOID lpBuffer,
1835 DWORD dwNumOfBytesToWrite, LPDWORD lpdwNumOfBytesWritten)
1837 object_header_t *lpwh;
1838 BOOL retval = FALSE;
1840 TRACE("(%p %p %d %p)\n", hFile, lpBuffer, dwNumOfBytesToWrite, lpdwNumOfBytesWritten);
1842 lpwh = WININET_GetObject( hFile );
1843 if (!lpwh) {
1844 WARN("Invalid handle\n");
1845 SetLastError(ERROR_INVALID_HANDLE);
1846 return FALSE;
1849 if(lpwh->vtbl->WriteFile) {
1850 retval = lpwh->vtbl->WriteFile(lpwh, lpBuffer, dwNumOfBytesToWrite, lpdwNumOfBytesWritten);
1851 }else {
1852 WARN("No Writefile method.\n");
1853 SetLastError(ERROR_INVALID_HANDLE);
1854 retval = FALSE;
1857 WININET_Release( lpwh );
1859 return retval;
1863 /***********************************************************************
1864 * InternetReadFile (WININET.@)
1866 * Read data from an open internet file
1868 * RETURNS
1869 * TRUE on success
1870 * FALSE on failure
1873 BOOL WINAPI InternetReadFile(HINTERNET hFile, LPVOID lpBuffer,
1874 DWORD dwNumOfBytesToRead, LPDWORD pdwNumOfBytesRead)
1876 object_header_t *hdr;
1877 DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
1879 TRACE("%p %p %d %p\n", hFile, lpBuffer, dwNumOfBytesToRead, pdwNumOfBytesRead);
1881 hdr = WININET_GetObject(hFile);
1882 if (!hdr) {
1883 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
1884 return FALSE;
1887 if(hdr->vtbl->ReadFile)
1888 res = hdr->vtbl->ReadFile(hdr, lpBuffer, dwNumOfBytesToRead, pdwNumOfBytesRead);
1890 WININET_Release(hdr);
1892 TRACE("-- %s (%u) (bytes read: %d)\n", res == ERROR_SUCCESS ? "TRUE": "FALSE", res,
1893 pdwNumOfBytesRead ? *pdwNumOfBytesRead : -1);
1895 if(res != ERROR_SUCCESS)
1896 SetLastError(res);
1897 return res == ERROR_SUCCESS;
1900 /***********************************************************************
1901 * InternetReadFileExA (WININET.@)
1903 * Read data from an open internet file
1905 * PARAMS
1906 * hFile [I] Handle returned by InternetOpenUrl or HttpOpenRequest.
1907 * lpBuffersOut [I/O] Buffer.
1908 * dwFlags [I] Flags. See notes.
1909 * dwContext [I] Context for callbacks.
1911 * RETURNS
1912 * TRUE on success
1913 * FALSE on failure
1915 * NOTES
1916 * The parameter dwFlags include zero or more of the following flags:
1917 *|IRF_ASYNC - Makes the call asynchronous.
1918 *|IRF_SYNC - Makes the call synchronous.
1919 *|IRF_USE_CONTEXT - Forces dwContext to be used.
1920 *|IRF_NO_WAIT - Don't block if the data is not available, just return what is available.
1922 * However, in testing IRF_USE_CONTEXT seems to have no effect - dwContext isn't used.
1924 * SEE
1925 * InternetOpenUrlA(), HttpOpenRequestA()
1927 BOOL WINAPI InternetReadFileExA(HINTERNET hFile, LPINTERNET_BUFFERSA lpBuffersOut,
1928 DWORD dwFlags, DWORD_PTR dwContext)
1930 object_header_t *hdr;
1931 DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
1933 TRACE("(%p %p 0x%x 0x%lx)\n", hFile, lpBuffersOut, dwFlags, dwContext);
1935 hdr = WININET_GetObject(hFile);
1936 if (!hdr) {
1937 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
1938 return FALSE;
1941 if(hdr->vtbl->ReadFileExA)
1942 res = hdr->vtbl->ReadFileExA(hdr, lpBuffersOut, dwFlags, dwContext);
1944 WININET_Release(hdr);
1946 TRACE("-- %s (%u, bytes read: %d)\n", res == ERROR_SUCCESS ? "TRUE": "FALSE",
1947 res, lpBuffersOut->dwBufferLength);
1949 if(res != ERROR_SUCCESS)
1950 SetLastError(res);
1951 return res == ERROR_SUCCESS;
1954 /***********************************************************************
1955 * InternetReadFileExW (WININET.@)
1956 * SEE
1957 * InternetReadFileExA()
1959 BOOL WINAPI InternetReadFileExW(HINTERNET hFile, LPINTERNET_BUFFERSW lpBuffer,
1960 DWORD dwFlags, DWORD_PTR dwContext)
1962 object_header_t *hdr;
1963 DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
1965 TRACE("(%p %p 0x%x 0x%lx)\n", hFile, lpBuffer, dwFlags, dwContext);
1967 hdr = WININET_GetObject(hFile);
1968 if (!hdr) {
1969 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
1970 return FALSE;
1973 if(hdr->vtbl->ReadFileExW)
1974 res = hdr->vtbl->ReadFileExW(hdr, lpBuffer, dwFlags, dwContext);
1976 WININET_Release(hdr);
1978 TRACE("-- %s (%u, bytes read: %d)\n", res == ERROR_SUCCESS ? "TRUE": "FALSE",
1979 res, lpBuffer->dwBufferLength);
1981 if(res != ERROR_SUCCESS)
1982 SetLastError(res);
1983 return res == ERROR_SUCCESS;
1986 DWORD INET_QueryOption(DWORD option, void *buffer, DWORD *size, BOOL unicode)
1988 static BOOL warn = TRUE;
1990 switch(option) {
1991 case INTERNET_OPTION_REQUEST_FLAGS:
1992 TRACE("INTERNET_OPTION_REQUEST_FLAGS\n");
1994 if (*size < sizeof(ULONG))
1995 return ERROR_INSUFFICIENT_BUFFER;
1997 *(ULONG*)buffer = 4;
1998 *size = sizeof(ULONG);
2000 return ERROR_SUCCESS;
2002 case INTERNET_OPTION_HTTP_VERSION:
2003 if (*size < sizeof(HTTP_VERSION_INFO))
2004 return ERROR_INSUFFICIENT_BUFFER;
2007 * Presently hardcoded to 1.1
2009 ((HTTP_VERSION_INFO*)buffer)->dwMajorVersion = 1;
2010 ((HTTP_VERSION_INFO*)buffer)->dwMinorVersion = 1;
2011 *size = sizeof(HTTP_VERSION_INFO);
2013 return ERROR_SUCCESS;
2015 case INTERNET_OPTION_CONNECTED_STATE:
2016 if (warn) {
2017 FIXME("INTERNET_OPTION_CONNECTED_STATE: semi-stub\n");
2018 warn = FALSE;
2020 if (*size < sizeof(ULONG))
2021 return ERROR_INSUFFICIENT_BUFFER;
2023 *(ULONG*)buffer = INTERNET_STATE_CONNECTED;
2024 *size = sizeof(ULONG);
2026 return ERROR_SUCCESS;
2028 case INTERNET_OPTION_PROXY: {
2029 appinfo_t ai;
2031 TRACE("Getting global proxy info\n");
2032 memset(&ai, 0, sizeof(appinfo_t));
2033 INTERNET_ConfigureProxy(&ai);
2035 return APPINFO_QueryOption(&ai.hdr, INTERNET_OPTION_PROXY, buffer, size, unicode); /* FIXME */
2038 case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2039 TRACE("INTERNET_OPTION_MAX_CONNS_PER_SERVER\n");
2041 if (*size < sizeof(ULONG))
2042 return ERROR_INSUFFICIENT_BUFFER;
2044 *(ULONG*)buffer = 2;
2045 *size = sizeof(ULONG);
2047 return ERROR_SUCCESS;
2049 case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2050 TRACE("INTERNET_OPTION_MAX_CONNS_1_0_SERVER\n");
2052 if (*size < sizeof(ULONG))
2053 return ERROR_INSUFFICIENT_BUFFER;
2055 *(ULONG*)size = 4;
2056 *size = sizeof(ULONG);
2058 return ERROR_SUCCESS;
2060 case INTERNET_OPTION_SECURITY_FLAGS:
2061 FIXME("INTERNET_OPTION_SECURITY_FLAGS: Stub\n");
2062 return ERROR_SUCCESS;
2064 case INTERNET_OPTION_VERSION: {
2065 static const INTERNET_VERSION_INFO info = { 1, 2 };
2067 TRACE("INTERNET_OPTION_VERSION\n");
2069 if (*size < sizeof(INTERNET_VERSION_INFO))
2070 return ERROR_INSUFFICIENT_BUFFER;
2072 memcpy(buffer, &info, sizeof(info));
2073 *size = sizeof(info);
2075 return ERROR_SUCCESS;
2078 case INTERNET_OPTION_PER_CONNECTION_OPTION: {
2079 INTERNET_PER_CONN_OPTION_LISTW *con = buffer;
2080 DWORD res = ERROR_SUCCESS, i;
2082 FIXME("INTERNET_OPTION_PER_CONNECTION_OPTION stub\n");
2084 if (*size < sizeof(INTERNET_PER_CONN_OPTION_LISTW))
2085 return ERROR_INSUFFICIENT_BUFFER;
2087 for (i = 0; i < con->dwOptionCount; i++) {
2088 INTERNET_PER_CONN_OPTIONW *option = con->pOptions + i;
2090 switch (option->dwOption) {
2091 case INTERNET_PER_CONN_FLAGS:
2092 option->Value.dwValue = PROXY_TYPE_DIRECT;
2093 break;
2095 case INTERNET_PER_CONN_PROXY_SERVER:
2096 case INTERNET_PER_CONN_PROXY_BYPASS:
2097 case INTERNET_PER_CONN_AUTOCONFIG_URL:
2098 case INTERNET_PER_CONN_AUTODISCOVERY_FLAGS:
2099 case INTERNET_PER_CONN_AUTOCONFIG_SECONDARY_URL:
2100 case INTERNET_PER_CONN_AUTOCONFIG_RELOAD_DELAY_MINS:
2101 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_TIME:
2102 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_URL:
2103 FIXME("Unhandled dwOption %d\n", option->dwOption);
2104 memset(&option->Value, 0, sizeof(option->Value));
2105 break;
2107 default:
2108 FIXME("Unknown dwOption %d\n", option->dwOption);
2109 res = ERROR_INVALID_PARAMETER;
2110 break;
2114 return res;
2118 FIXME("Stub for %d\n", option);
2119 return ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
2122 /***********************************************************************
2123 * InternetQueryOptionW (WININET.@)
2125 * Queries an options on the specified handle
2127 * RETURNS
2128 * TRUE on success
2129 * FALSE on failure
2132 BOOL WINAPI InternetQueryOptionW(HINTERNET hInternet, DWORD dwOption,
2133 LPVOID lpBuffer, LPDWORD lpdwBufferLength)
2135 object_header_t *hdr;
2136 DWORD res = ERROR_INVALID_HANDLE;
2138 TRACE("%p %d %p %p\n", hInternet, dwOption, lpBuffer, lpdwBufferLength);
2140 if(hInternet) {
2141 hdr = WININET_GetObject(hInternet);
2142 if (hdr) {
2143 res = hdr->vtbl->QueryOption(hdr, dwOption, lpBuffer, lpdwBufferLength, TRUE);
2144 WININET_Release(hdr);
2146 }else {
2147 res = INET_QueryOption(dwOption, lpBuffer, lpdwBufferLength, TRUE);
2150 if(res != ERROR_SUCCESS)
2151 SetLastError(res);
2152 return res == ERROR_SUCCESS;
2155 /***********************************************************************
2156 * InternetQueryOptionA (WININET.@)
2158 * Queries an options on the specified handle
2160 * RETURNS
2161 * TRUE on success
2162 * FALSE on failure
2165 BOOL WINAPI InternetQueryOptionA(HINTERNET hInternet, DWORD dwOption,
2166 LPVOID lpBuffer, LPDWORD lpdwBufferLength)
2168 object_header_t *hdr;
2169 DWORD res = ERROR_INVALID_HANDLE;
2171 TRACE("%p %d %p %p\n", hInternet, dwOption, lpBuffer, lpdwBufferLength);
2173 if(hInternet) {
2174 hdr = WININET_GetObject(hInternet);
2175 if (hdr) {
2176 res = hdr->vtbl->QueryOption(hdr, dwOption, lpBuffer, lpdwBufferLength, FALSE);
2177 WININET_Release(hdr);
2179 }else {
2180 res = INET_QueryOption(dwOption, lpBuffer, lpdwBufferLength, FALSE);
2183 if(res != ERROR_SUCCESS)
2184 SetLastError(res);
2185 return res == ERROR_SUCCESS;
2189 /***********************************************************************
2190 * InternetSetOptionW (WININET.@)
2192 * Sets an options on the specified handle
2194 * RETURNS
2195 * TRUE on success
2196 * FALSE on failure
2199 BOOL WINAPI InternetSetOptionW(HINTERNET hInternet, DWORD dwOption,
2200 LPVOID lpBuffer, DWORD dwBufferLength)
2202 object_header_t *lpwhh;
2203 BOOL ret = TRUE;
2205 TRACE("(%p %d %p %d)\n", hInternet, dwOption, lpBuffer, dwBufferLength);
2207 lpwhh = (object_header_t*) WININET_GetObject( hInternet );
2208 if(lpwhh && lpwhh->vtbl->SetOption) {
2209 DWORD res;
2211 res = lpwhh->vtbl->SetOption(lpwhh, dwOption, lpBuffer, dwBufferLength);
2212 if(res != ERROR_INTERNET_INVALID_OPTION) {
2213 WININET_Release( lpwhh );
2215 if(res != ERROR_SUCCESS)
2216 SetLastError(res);
2218 return res == ERROR_SUCCESS;
2222 switch (dwOption)
2224 case INTERNET_OPTION_CALLBACK:
2226 if (!lpwhh)
2228 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2229 return FALSE;
2231 WININET_Release(lpwhh);
2232 INTERNET_SetLastError(ERROR_INTERNET_OPTION_NOT_SETTABLE);
2233 return FALSE;
2235 case INTERNET_OPTION_HTTP_VERSION:
2237 HTTP_VERSION_INFO* pVersion=(HTTP_VERSION_INFO*)lpBuffer;
2238 FIXME("Option INTERNET_OPTION_HTTP_VERSION(%d,%d): STUB\n",pVersion->dwMajorVersion,pVersion->dwMinorVersion);
2240 break;
2241 case INTERNET_OPTION_ERROR_MASK:
2243 ULONG flags = *(ULONG *)lpBuffer;
2244 FIXME("Option INTERNET_OPTION_ERROR_MASK(%d): STUB\n", flags);
2246 break;
2247 case INTERNET_OPTION_CODEPAGE:
2249 ULONG codepage = *(ULONG *)lpBuffer;
2250 FIXME("Option INTERNET_OPTION_CODEPAGE (%d): STUB\n", codepage);
2252 break;
2253 case INTERNET_OPTION_REQUEST_PRIORITY:
2255 ULONG priority = *(ULONG *)lpBuffer;
2256 FIXME("Option INTERNET_OPTION_REQUEST_PRIORITY (%d): STUB\n", priority);
2258 break;
2259 case INTERNET_OPTION_CONNECT_TIMEOUT:
2261 ULONG connecttimeout = *(ULONG *)lpBuffer;
2262 FIXME("Option INTERNET_OPTION_CONNECT_TIMEOUT (%d): STUB\n", connecttimeout);
2264 break;
2265 case INTERNET_OPTION_DATA_RECEIVE_TIMEOUT:
2267 ULONG receivetimeout = *(ULONG *)lpBuffer;
2268 FIXME("Option INTERNET_OPTION_DATA_RECEIVE_TIMEOUT (%d): STUB\n", receivetimeout);
2270 break;
2271 case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2273 ULONG conns = *(ULONG *)lpBuffer;
2274 FIXME("Option INTERNET_OPTION_MAX_CONNS_PER_SERVER (%d): STUB\n", conns);
2276 break;
2277 case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2279 ULONG conns = *(ULONG *)lpBuffer;
2280 FIXME("Option INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER (%d): STUB\n", conns);
2282 break;
2283 case INTERNET_OPTION_RESET_URLCACHE_SESSION:
2284 FIXME("Option INTERNET_OPTION_RESET_URLCACHE_SESSION: STUB\n");
2285 break;
2286 case INTERNET_OPTION_END_BROWSER_SESSION:
2287 FIXME("Option INTERNET_OPTION_END_BROWSER_SESSION: STUB\n");
2288 break;
2289 case INTERNET_OPTION_CONNECTED_STATE:
2290 FIXME("Option INTERNET_OPTION_CONNECTED_STATE: STUB\n");
2291 break;
2292 case INTERNET_OPTION_DISABLE_PASSPORT_AUTH:
2293 TRACE("Option INTERNET_OPTION_DISABLE_PASSPORT_AUTH: harmless stub, since not enabled\n");
2294 break;
2295 case INTERNET_OPTION_SEND_TIMEOUT:
2296 case INTERNET_OPTION_RECEIVE_TIMEOUT:
2298 ULONG timeout = *(ULONG *)lpBuffer;
2299 FIXME("INTERNET_OPTION_SEND/RECEIVE_TIMEOUT %d\n", timeout);
2300 break;
2302 case INTERNET_OPTION_CONNECT_RETRIES:
2304 ULONG retries = *(ULONG *)lpBuffer;
2305 FIXME("INTERNET_OPTION_CONNECT_RETRIES %d\n", retries);
2306 break;
2308 case INTERNET_OPTION_CONTEXT_VALUE:
2309 FIXME("Option INTERNET_OPTION_CONTEXT_VALUE; STUB\n");
2310 break;
2311 case INTERNET_OPTION_SECURITY_FLAGS:
2312 FIXME("Option INTERNET_OPTION_SECURITY_FLAGS; STUB\n");
2313 break;
2314 case INTERNET_OPTION_DISABLE_AUTODIAL:
2315 FIXME("Option INTERNET_OPTION_DISABLE_AUTODIAL; STUB\n");
2316 break;
2317 case INTERNET_OPTION_HTTP_DECODING:
2318 FIXME("INTERNET_OPTION_HTTP_DECODING; STUB\n");
2319 INTERNET_SetLastError(ERROR_INTERNET_INVALID_OPTION);
2320 ret = FALSE;
2321 break;
2322 case INTERNET_OPTION_COOKIES_3RD_PARTY:
2323 FIXME("INTERNET_OPTION_COOKIES_3RD_PARTY; STUB\n");
2324 INTERNET_SetLastError(ERROR_INTERNET_INVALID_OPTION);
2325 ret = FALSE;
2326 break;
2327 case INTERNET_OPTION_SEND_UTF8_SERVERNAME_TO_PROXY:
2328 FIXME("INTERNET_OPTION_SEND_UTF8_SERVERNAME_TO_PROXY; STUB\n");
2329 INTERNET_SetLastError(ERROR_INTERNET_INVALID_OPTION);
2330 ret = FALSE;
2331 break;
2332 case INTERNET_OPTION_CODEPAGE_PATH:
2333 FIXME("INTERNET_OPTION_CODEPAGE_PATH; STUB\n");
2334 INTERNET_SetLastError(ERROR_INTERNET_INVALID_OPTION);
2335 ret = FALSE;
2336 break;
2337 case INTERNET_OPTION_CODEPAGE_EXTRA:
2338 FIXME("INTERNET_OPTION_CODEPAGE_EXTRA; STUB\n");
2339 INTERNET_SetLastError(ERROR_INTERNET_INVALID_OPTION);
2340 ret = FALSE;
2341 break;
2342 case INTERNET_OPTION_IDN:
2343 FIXME("INTERNET_OPTION_IDN; STUB\n");
2344 INTERNET_SetLastError(ERROR_INTERNET_INVALID_OPTION);
2345 ret = FALSE;
2346 break;
2347 default:
2348 FIXME("Option %d STUB\n",dwOption);
2349 INTERNET_SetLastError(ERROR_INTERNET_INVALID_OPTION);
2350 ret = FALSE;
2351 break;
2354 if(lpwhh)
2355 WININET_Release( lpwhh );
2357 return ret;
2361 /***********************************************************************
2362 * InternetSetOptionA (WININET.@)
2364 * Sets an options on the specified handle.
2366 * RETURNS
2367 * TRUE on success
2368 * FALSE on failure
2371 BOOL WINAPI InternetSetOptionA(HINTERNET hInternet, DWORD dwOption,
2372 LPVOID lpBuffer, DWORD dwBufferLength)
2374 LPVOID wbuffer;
2375 DWORD wlen;
2376 BOOL r;
2378 switch( dwOption )
2380 case INTERNET_OPTION_CALLBACK:
2382 object_header_t *lpwh;
2384 if (!(lpwh = WININET_GetObject(hInternet)))
2386 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2387 return FALSE;
2389 WININET_Release(lpwh);
2390 INTERNET_SetLastError(ERROR_INTERNET_OPTION_NOT_SETTABLE);
2391 return FALSE;
2393 case INTERNET_OPTION_PROXY:
2395 LPINTERNET_PROXY_INFOA pi = (LPINTERNET_PROXY_INFOA) lpBuffer;
2396 LPINTERNET_PROXY_INFOW piw;
2397 DWORD proxlen, prbylen;
2398 LPWSTR prox, prby;
2400 proxlen = MultiByteToWideChar( CP_ACP, 0, pi->lpszProxy, -1, NULL, 0);
2401 prbylen= MultiByteToWideChar( CP_ACP, 0, pi->lpszProxyBypass, -1, NULL, 0);
2402 wlen = sizeof(*piw) + proxlen + prbylen;
2403 wbuffer = HeapAlloc( GetProcessHeap(), 0, wlen*sizeof(WCHAR) );
2404 piw = (LPINTERNET_PROXY_INFOW) wbuffer;
2405 piw->dwAccessType = pi->dwAccessType;
2406 prox = (LPWSTR) &piw[1];
2407 prby = &prox[proxlen+1];
2408 MultiByteToWideChar( CP_ACP, 0, pi->lpszProxy, -1, prox, proxlen);
2409 MultiByteToWideChar( CP_ACP, 0, pi->lpszProxyBypass, -1, prby, prbylen);
2410 piw->lpszProxy = prox;
2411 piw->lpszProxyBypass = prby;
2413 break;
2414 case INTERNET_OPTION_USER_AGENT:
2415 case INTERNET_OPTION_USERNAME:
2416 case INTERNET_OPTION_PASSWORD:
2417 wlen = MultiByteToWideChar( CP_ACP, 0, lpBuffer, dwBufferLength,
2418 NULL, 0 );
2419 wbuffer = HeapAlloc( GetProcessHeap(), 0, wlen*sizeof(WCHAR) );
2420 MultiByteToWideChar( CP_ACP, 0, lpBuffer, dwBufferLength,
2421 wbuffer, wlen );
2422 break;
2423 default:
2424 wbuffer = lpBuffer;
2425 wlen = dwBufferLength;
2428 r = InternetSetOptionW(hInternet,dwOption, wbuffer, wlen);
2430 if( lpBuffer != wbuffer )
2431 HeapFree( GetProcessHeap(), 0, wbuffer );
2433 return r;
2437 /***********************************************************************
2438 * InternetSetOptionExA (WININET.@)
2440 BOOL WINAPI InternetSetOptionExA(HINTERNET hInternet, DWORD dwOption,
2441 LPVOID lpBuffer, DWORD dwBufferLength, DWORD dwFlags)
2443 FIXME("Flags %08x ignored\n", dwFlags);
2444 return InternetSetOptionA( hInternet, dwOption, lpBuffer, dwBufferLength );
2447 /***********************************************************************
2448 * InternetSetOptionExW (WININET.@)
2450 BOOL WINAPI InternetSetOptionExW(HINTERNET hInternet, DWORD dwOption,
2451 LPVOID lpBuffer, DWORD dwBufferLength, DWORD dwFlags)
2453 FIXME("Flags %08x ignored\n", dwFlags);
2454 if( dwFlags & ~ISO_VALID_FLAGS )
2456 INTERNET_SetLastError( ERROR_INVALID_PARAMETER );
2457 return FALSE;
2459 return InternetSetOptionW( hInternet, dwOption, lpBuffer, dwBufferLength );
2462 static const WCHAR WININET_wkday[7][4] =
2463 { { 'S','u','n', 0 }, { 'M','o','n', 0 }, { 'T','u','e', 0 }, { 'W','e','d', 0 },
2464 { 'T','h','u', 0 }, { 'F','r','i', 0 }, { 'S','a','t', 0 } };
2465 static const WCHAR WININET_month[12][4] =
2466 { { 'J','a','n', 0 }, { 'F','e','b', 0 }, { 'M','a','r', 0 }, { 'A','p','r', 0 },
2467 { 'M','a','y', 0 }, { 'J','u','n', 0 }, { 'J','u','l', 0 }, { 'A','u','g', 0 },
2468 { 'S','e','p', 0 }, { 'O','c','t', 0 }, { 'N','o','v', 0 }, { 'D','e','c', 0 } };
2470 /***********************************************************************
2471 * InternetTimeFromSystemTimeA (WININET.@)
2473 BOOL WINAPI InternetTimeFromSystemTimeA( const SYSTEMTIME* time, DWORD format, LPSTR string, DWORD size )
2475 BOOL ret;
2476 WCHAR stringW[INTERNET_RFC1123_BUFSIZE];
2478 TRACE( "%p 0x%08x %p 0x%08x\n", time, format, string, size );
2480 if (!time || !string || format != INTERNET_RFC1123_FORMAT)
2482 SetLastError(ERROR_INVALID_PARAMETER);
2483 return FALSE;
2486 if (size < INTERNET_RFC1123_BUFSIZE * sizeof(*string))
2488 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2489 return FALSE;
2492 ret = InternetTimeFromSystemTimeW( time, format, stringW, sizeof(stringW) );
2493 if (ret) WideCharToMultiByte( CP_ACP, 0, stringW, -1, string, size, NULL, NULL );
2495 return ret;
2498 /***********************************************************************
2499 * InternetTimeFromSystemTimeW (WININET.@)
2501 BOOL WINAPI InternetTimeFromSystemTimeW( const SYSTEMTIME* time, DWORD format, LPWSTR string, DWORD size )
2503 static const WCHAR date[] =
2504 { '%','s',',',' ','%','0','2','d',' ','%','s',' ','%','4','d',' ','%','0',
2505 '2','d',':','%','0','2','d',':','%','0','2','d',' ','G','M','T', 0 };
2507 TRACE( "%p 0x%08x %p 0x%08x\n", time, format, string, size );
2509 if (!time || !string || format != INTERNET_RFC1123_FORMAT)
2511 SetLastError(ERROR_INVALID_PARAMETER);
2512 return FALSE;
2515 if (size < INTERNET_RFC1123_BUFSIZE * sizeof(*string))
2517 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2518 return FALSE;
2521 sprintfW( string, date,
2522 WININET_wkday[time->wDayOfWeek],
2523 time->wDay,
2524 WININET_month[time->wMonth - 1],
2525 time->wYear,
2526 time->wHour,
2527 time->wMinute,
2528 time->wSecond );
2530 return TRUE;
2533 /***********************************************************************
2534 * InternetTimeToSystemTimeA (WININET.@)
2536 BOOL WINAPI InternetTimeToSystemTimeA( LPCSTR string, SYSTEMTIME* time, DWORD reserved )
2538 BOOL ret = FALSE;
2539 WCHAR *stringW;
2541 TRACE( "%s %p 0x%08x\n", debugstr_a(string), time, reserved );
2543 stringW = heap_strdupAtoW(string);
2544 if (stringW)
2546 ret = InternetTimeToSystemTimeW( stringW, time, reserved );
2547 HeapFree( GetProcessHeap(), 0, stringW );
2549 return ret;
2552 /***********************************************************************
2553 * InternetTimeToSystemTimeW (WININET.@)
2555 BOOL WINAPI InternetTimeToSystemTimeW( LPCWSTR string, SYSTEMTIME* time, DWORD reserved )
2557 unsigned int i;
2558 const WCHAR *s = string;
2559 WCHAR *end;
2561 TRACE( "%s %p 0x%08x\n", debugstr_w(string), time, reserved );
2563 if (!string || !time) return FALSE;
2565 /* Windows does this too */
2566 GetSystemTime( time );
2568 /* Convert an RFC1123 time such as 'Fri, 07 Jan 2005 12:06:35 GMT' into
2569 * a SYSTEMTIME structure.
2572 while (*s && !isalphaW( *s )) s++;
2573 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
2574 time->wDayOfWeek = 7;
2576 for (i = 0; i < 7; i++)
2578 if (toupperW( WININET_wkday[i][0] ) == toupperW( s[0] ) &&
2579 toupperW( WININET_wkday[i][1] ) == toupperW( s[1] ) &&
2580 toupperW( WININET_wkday[i][2] ) == toupperW( s[2] ) )
2582 time->wDayOfWeek = i;
2583 break;
2587 if (time->wDayOfWeek > 6) return TRUE;
2588 while (*s && !isdigitW( *s )) s++;
2589 time->wDay = strtolW( s, &end, 10 );
2590 s = end;
2592 while (*s && !isalphaW( *s )) s++;
2593 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
2594 time->wMonth = 0;
2596 for (i = 0; i < 12; i++)
2598 if (toupperW( WININET_month[i][0]) == toupperW( s[0] ) &&
2599 toupperW( WININET_month[i][1]) == toupperW( s[1] ) &&
2600 toupperW( WININET_month[i][2]) == toupperW( s[2] ) )
2602 time->wMonth = i + 1;
2603 break;
2606 if (time->wMonth == 0) return TRUE;
2608 while (*s && !isdigitW( *s )) s++;
2609 if (*s == '\0') return TRUE;
2610 time->wYear = strtolW( s, &end, 10 );
2611 s = end;
2613 while (*s && !isdigitW( *s )) s++;
2614 if (*s == '\0') return TRUE;
2615 time->wHour = strtolW( s, &end, 10 );
2616 s = end;
2618 while (*s && !isdigitW( *s )) s++;
2619 if (*s == '\0') return TRUE;
2620 time->wMinute = strtolW( s, &end, 10 );
2621 s = end;
2623 while (*s && !isdigitW( *s )) s++;
2624 if (*s == '\0') return TRUE;
2625 time->wSecond = strtolW( s, &end, 10 );
2626 s = end;
2628 time->wMilliseconds = 0;
2629 return TRUE;
2632 /***********************************************************************
2633 * InternetCheckConnectionW (WININET.@)
2635 * Pings a requested host to check internet connection
2637 * RETURNS
2638 * TRUE on success and FALSE on failure. If a failure then
2639 * ERROR_NOT_CONNECTED is placed into GetLastError
2642 BOOL WINAPI InternetCheckConnectionW( LPCWSTR lpszUrl, DWORD dwFlags, DWORD dwReserved )
2645 * this is a kludge which runs the resident ping program and reads the output.
2647 * Anyone have a better idea?
2650 BOOL rc = FALSE;
2651 static const CHAR ping[] = "ping -c 1 ";
2652 static const CHAR redirect[] = " >/dev/null 2>/dev/null";
2653 CHAR *command = NULL;
2654 WCHAR hostW[1024];
2655 DWORD len;
2656 INTERNET_PORT port;
2657 int status = -1;
2659 FIXME("\n");
2662 * Crack or set the Address
2664 if (lpszUrl == NULL)
2667 * According to the doc we are supposed to use the ip for the next
2668 * server in the WnInet internal server database. I have
2669 * no idea what that is or how to get it.
2671 * So someone needs to implement this.
2673 FIXME("Unimplemented with URL of NULL\n");
2674 return TRUE;
2676 else
2678 URL_COMPONENTSW components;
2680 ZeroMemory(&components,sizeof(URL_COMPONENTSW));
2681 components.lpszHostName = (LPWSTR)hostW;
2682 components.dwHostNameLength = 1024;
2684 if (!InternetCrackUrlW(lpszUrl,0,0,&components))
2685 goto End;
2687 TRACE("host name : %s\n",debugstr_w(components.lpszHostName));
2688 port = components.nPort;
2689 TRACE("port: %d\n", port);
2692 if (dwFlags & FLAG_ICC_FORCE_CONNECTION)
2694 struct sockaddr_storage saddr;
2695 socklen_t sa_len = sizeof(saddr);
2696 int fd;
2698 if (!GetAddress(hostW, port, (struct sockaddr *)&saddr, &sa_len))
2699 goto End;
2700 fd = socket(saddr.ss_family, SOCK_STREAM, 0);
2701 if (fd != -1)
2703 if (connect(fd, (struct sockaddr *)&saddr, sa_len) == 0)
2704 rc = TRUE;
2705 close(fd);
2708 else
2711 * Build our ping command
2713 len = WideCharToMultiByte(CP_UNIXCP, 0, hostW, -1, NULL, 0, NULL, NULL);
2714 command = HeapAlloc( GetProcessHeap(), 0, strlen(ping)+len+strlen(redirect) );
2715 strcpy(command,ping);
2716 WideCharToMultiByte(CP_UNIXCP, 0, hostW, -1, command+strlen(ping), len, NULL, NULL);
2717 strcat(command,redirect);
2719 TRACE("Ping command is : %s\n",command);
2721 status = system(command);
2723 TRACE("Ping returned a code of %i\n",status);
2725 /* Ping return code of 0 indicates success */
2726 if (status == 0)
2727 rc = TRUE;
2730 End:
2732 HeapFree( GetProcessHeap(), 0, command );
2733 if (rc == FALSE)
2734 INTERNET_SetLastError(ERROR_NOT_CONNECTED);
2736 return rc;
2740 /***********************************************************************
2741 * InternetCheckConnectionA (WININET.@)
2743 * Pings a requested host to check internet connection
2745 * RETURNS
2746 * TRUE on success and FALSE on failure. If a failure then
2747 * ERROR_NOT_CONNECTED is placed into GetLastError
2750 BOOL WINAPI InternetCheckConnectionA(LPCSTR lpszUrl, DWORD dwFlags, DWORD dwReserved)
2752 WCHAR *url = NULL;
2753 BOOL rc;
2755 if(lpszUrl) {
2756 url = heap_strdupAtoW(lpszUrl);
2757 if(!url)
2758 return FALSE;
2761 rc = InternetCheckConnectionW(url, dwFlags, dwReserved);
2763 HeapFree(GetProcessHeap(), 0, url);
2764 return rc;
2768 /**********************************************************
2769 * INTERNET_InternetOpenUrlW (internal)
2771 * Opens an URL
2773 * RETURNS
2774 * handle of connection or NULL on failure
2776 static HINTERNET INTERNET_InternetOpenUrlW(appinfo_t *hIC, LPCWSTR lpszUrl,
2777 LPCWSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext)
2779 URL_COMPONENTSW urlComponents;
2780 WCHAR protocol[32], hostName[MAXHOSTNAME], userName[1024];
2781 WCHAR password[1024], path[2048], extra[1024];
2782 HINTERNET client = NULL, client1 = NULL;
2784 TRACE("(%p, %s, %s, %08x, %08x, %08lx)\n", hIC, debugstr_w(lpszUrl), debugstr_w(lpszHeaders),
2785 dwHeadersLength, dwFlags, dwContext);
2787 urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
2788 urlComponents.lpszScheme = protocol;
2789 urlComponents.dwSchemeLength = 32;
2790 urlComponents.lpszHostName = hostName;
2791 urlComponents.dwHostNameLength = MAXHOSTNAME;
2792 urlComponents.lpszUserName = userName;
2793 urlComponents.dwUserNameLength = 1024;
2794 urlComponents.lpszPassword = password;
2795 urlComponents.dwPasswordLength = 1024;
2796 urlComponents.lpszUrlPath = path;
2797 urlComponents.dwUrlPathLength = 2048;
2798 urlComponents.lpszExtraInfo = extra;
2799 urlComponents.dwExtraInfoLength = 1024;
2800 if(!InternetCrackUrlW(lpszUrl, strlenW(lpszUrl), 0, &urlComponents))
2801 return NULL;
2802 switch(urlComponents.nScheme) {
2803 case INTERNET_SCHEME_FTP:
2804 if(urlComponents.nPort == 0)
2805 urlComponents.nPort = INTERNET_DEFAULT_FTP_PORT;
2806 client = FTP_Connect(hIC, hostName, urlComponents.nPort,
2807 userName, password, dwFlags, dwContext, INET_OPENURL);
2808 if(client == NULL)
2809 break;
2810 client1 = FtpOpenFileW(client, path, GENERIC_READ, dwFlags, dwContext);
2811 if(client1 == NULL) {
2812 InternetCloseHandle(client);
2813 break;
2815 break;
2817 case INTERNET_SCHEME_HTTP:
2818 case INTERNET_SCHEME_HTTPS: {
2819 static const WCHAR szStars[] = { '*','/','*', 0 };
2820 LPCWSTR accept[2] = { szStars, NULL };
2821 if(urlComponents.nPort == 0) {
2822 if(urlComponents.nScheme == INTERNET_SCHEME_HTTP)
2823 urlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
2824 else
2825 urlComponents.nPort = INTERNET_DEFAULT_HTTPS_PORT;
2827 if (urlComponents.nScheme == INTERNET_SCHEME_HTTPS) dwFlags |= INTERNET_FLAG_SECURE;
2829 /* FIXME: should use pointers, not handles, as handles are not thread-safe */
2830 client = HTTP_Connect(hIC, hostName, urlComponents.nPort,
2831 userName, password, dwFlags, dwContext, INET_OPENURL);
2832 if(client == NULL)
2833 break;
2835 if (urlComponents.dwExtraInfoLength) {
2836 WCHAR *path_extra;
2837 DWORD len = urlComponents.dwUrlPathLength + urlComponents.dwExtraInfoLength + 1;
2839 if (!(path_extra = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR))))
2841 InternetCloseHandle(client);
2842 break;
2844 strcpyW(path_extra, urlComponents.lpszUrlPath);
2845 strcatW(path_extra, urlComponents.lpszExtraInfo);
2846 client1 = HttpOpenRequestW(client, NULL, path_extra, NULL, NULL, accept, dwFlags, dwContext);
2847 HeapFree(GetProcessHeap(), 0, path_extra);
2849 else
2850 client1 = HttpOpenRequestW(client, NULL, path, NULL, NULL, accept, dwFlags, dwContext);
2852 if(client1 == NULL) {
2853 InternetCloseHandle(client);
2854 break;
2856 HttpAddRequestHeadersW(client1, lpszHeaders, dwHeadersLength, HTTP_ADDREQ_FLAG_ADD);
2857 if (!HttpSendRequestW(client1, NULL, 0, NULL, 0) &&
2858 GetLastError() != ERROR_IO_PENDING) {
2859 InternetCloseHandle(client1);
2860 client1 = NULL;
2861 break;
2864 case INTERNET_SCHEME_GOPHER:
2865 /* gopher doesn't seem to be implemented in wine, but it's supposed
2866 * to be supported by InternetOpenUrlA. */
2867 default:
2868 INTERNET_SetLastError(ERROR_INTERNET_UNRECOGNIZED_SCHEME);
2869 break;
2872 TRACE(" %p <--\n", client1);
2874 return client1;
2877 /**********************************************************
2878 * InternetOpenUrlW (WININET.@)
2880 * Opens an URL
2882 * RETURNS
2883 * handle of connection or NULL on failure
2885 static void AsyncInternetOpenUrlProc(WORKREQUEST *workRequest)
2887 struct WORKREQ_INTERNETOPENURLW const *req = &workRequest->u.InternetOpenUrlW;
2888 appinfo_t *hIC = (appinfo_t*) workRequest->hdr;
2890 TRACE("%p\n", hIC);
2892 INTERNET_InternetOpenUrlW(hIC, req->lpszUrl,
2893 req->lpszHeaders, req->dwHeadersLength, req->dwFlags, req->dwContext);
2894 HeapFree(GetProcessHeap(), 0, req->lpszUrl);
2895 HeapFree(GetProcessHeap(), 0, req->lpszHeaders);
2898 HINTERNET WINAPI InternetOpenUrlW(HINTERNET hInternet, LPCWSTR lpszUrl,
2899 LPCWSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext)
2901 HINTERNET ret = NULL;
2902 appinfo_t *hIC = NULL;
2904 if (TRACE_ON(wininet)) {
2905 TRACE("(%p, %s, %s, %08x, %08x, %08lx)\n", hInternet, debugstr_w(lpszUrl), debugstr_w(lpszHeaders),
2906 dwHeadersLength, dwFlags, dwContext);
2907 TRACE(" flags :");
2908 dump_INTERNET_FLAGS(dwFlags);
2911 if (!lpszUrl)
2913 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
2914 goto lend;
2917 hIC = (appinfo_t*)WININET_GetObject( hInternet );
2918 if (NULL == hIC || hIC->hdr.htype != WH_HINIT) {
2919 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2920 goto lend;
2923 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC) {
2924 WORKREQUEST workRequest;
2925 struct WORKREQ_INTERNETOPENURLW *req;
2927 workRequest.asyncproc = AsyncInternetOpenUrlProc;
2928 workRequest.hdr = WININET_AddRef( &hIC->hdr );
2929 req = &workRequest.u.InternetOpenUrlW;
2930 req->lpszUrl = heap_strdupW(lpszUrl);
2931 req->lpszHeaders = heap_strdupW(lpszHeaders);
2932 req->dwHeadersLength = dwHeadersLength;
2933 req->dwFlags = dwFlags;
2934 req->dwContext = dwContext;
2936 INTERNET_AsyncCall(&workRequest);
2938 * This is from windows.
2940 INTERNET_SetLastError(ERROR_IO_PENDING);
2941 } else {
2942 ret = INTERNET_InternetOpenUrlW(hIC, lpszUrl, lpszHeaders, dwHeadersLength, dwFlags, dwContext);
2945 lend:
2946 if( hIC )
2947 WININET_Release( &hIC->hdr );
2948 TRACE(" %p <--\n", ret);
2950 return ret;
2953 /**********************************************************
2954 * InternetOpenUrlA (WININET.@)
2956 * Opens an URL
2958 * RETURNS
2959 * handle of connection or NULL on failure
2961 HINTERNET WINAPI InternetOpenUrlA(HINTERNET hInternet, LPCSTR lpszUrl,
2962 LPCSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext)
2964 HINTERNET rc = NULL;
2965 DWORD lenHeaders = 0;
2966 LPWSTR szUrl = NULL;
2967 LPWSTR szHeaders = NULL;
2969 TRACE("\n");
2971 if(lpszUrl) {
2972 szUrl = heap_strdupAtoW(lpszUrl);
2973 if(!szUrl)
2974 return NULL;
2977 if(lpszHeaders) {
2978 lenHeaders = MultiByteToWideChar(CP_ACP, 0, lpszHeaders, dwHeadersLength, NULL, 0 );
2979 szHeaders = HeapAlloc(GetProcessHeap(), 0, lenHeaders*sizeof(WCHAR));
2980 if(!szHeaders) {
2981 HeapFree(GetProcessHeap(), 0, szUrl);
2982 return NULL;
2984 MultiByteToWideChar(CP_ACP, 0, lpszHeaders, dwHeadersLength, szHeaders, lenHeaders);
2987 rc = InternetOpenUrlW(hInternet, szUrl, szHeaders,
2988 lenHeaders, dwFlags, dwContext);
2990 HeapFree(GetProcessHeap(), 0, szUrl);
2991 HeapFree(GetProcessHeap(), 0, szHeaders);
2993 return rc;
2997 static LPWITHREADERROR INTERNET_AllocThreadError(void)
2999 LPWITHREADERROR lpwite = HeapAlloc(GetProcessHeap(), 0, sizeof(*lpwite));
3001 if (lpwite)
3003 lpwite->dwError = 0;
3004 lpwite->response[0] = '\0';
3007 if (!TlsSetValue(g_dwTlsErrIndex, lpwite))
3009 HeapFree(GetProcessHeap(), 0, lpwite);
3010 return NULL;
3013 return lpwite;
3017 /***********************************************************************
3018 * INTERNET_SetLastError (internal)
3020 * Set last thread specific error
3022 * RETURNS
3025 void INTERNET_SetLastError(DWORD dwError)
3027 LPWITHREADERROR lpwite = TlsGetValue(g_dwTlsErrIndex);
3029 if (!lpwite)
3030 lpwite = INTERNET_AllocThreadError();
3032 SetLastError(dwError);
3033 if(lpwite)
3034 lpwite->dwError = dwError;
3038 /***********************************************************************
3039 * INTERNET_GetLastError (internal)
3041 * Get last thread specific error
3043 * RETURNS
3046 DWORD INTERNET_GetLastError(void)
3048 LPWITHREADERROR lpwite = TlsGetValue(g_dwTlsErrIndex);
3049 if (!lpwite) return 0;
3050 /* TlsGetValue clears last error, so set it again here */
3051 SetLastError(lpwite->dwError);
3052 return lpwite->dwError;
3056 /***********************************************************************
3057 * INTERNET_WorkerThreadFunc (internal)
3059 * Worker thread execution function
3061 * RETURNS
3064 static DWORD CALLBACK INTERNET_WorkerThreadFunc(LPVOID lpvParam)
3066 LPWORKREQUEST lpRequest = lpvParam;
3067 WORKREQUEST workRequest;
3069 TRACE("\n");
3071 workRequest = *lpRequest;
3072 HeapFree(GetProcessHeap(), 0, lpRequest);
3074 workRequest.asyncproc(&workRequest);
3076 WININET_Release( workRequest.hdr );
3077 return TRUE;
3081 /***********************************************************************
3082 * INTERNET_AsyncCall (internal)
3084 * Retrieves work request from queue
3086 * RETURNS
3089 BOOL INTERNET_AsyncCall(LPWORKREQUEST lpWorkRequest)
3091 BOOL bSuccess;
3092 LPWORKREQUEST lpNewRequest;
3094 TRACE("\n");
3096 lpNewRequest = HeapAlloc(GetProcessHeap(), 0, sizeof(WORKREQUEST));
3097 if (!lpNewRequest)
3098 return FALSE;
3100 *lpNewRequest = *lpWorkRequest;
3102 bSuccess = QueueUserWorkItem(INTERNET_WorkerThreadFunc, lpNewRequest, WT_EXECUTELONGFUNCTION);
3103 if (!bSuccess)
3105 HeapFree(GetProcessHeap(), 0, lpNewRequest);
3106 INTERNET_SetLastError(ERROR_INTERNET_ASYNC_THREAD_FAILED);
3109 return bSuccess;
3113 /***********************************************************************
3114 * INTERNET_GetResponseBuffer (internal)
3116 * RETURNS
3119 LPSTR INTERNET_GetResponseBuffer(void)
3121 LPWITHREADERROR lpwite = TlsGetValue(g_dwTlsErrIndex);
3122 if (!lpwite)
3123 lpwite = INTERNET_AllocThreadError();
3124 TRACE("\n");
3125 return lpwite->response;
3128 /***********************************************************************
3129 * INTERNET_GetNextLine (internal)
3131 * Parse next line in directory string listing
3133 * RETURNS
3134 * Pointer to beginning of next line
3135 * NULL on failure
3139 LPSTR INTERNET_GetNextLine(INT nSocket, LPDWORD dwLen)
3141 struct pollfd pfd;
3142 BOOL bSuccess = FALSE;
3143 INT nRecv = 0;
3144 LPSTR lpszBuffer = INTERNET_GetResponseBuffer();
3146 TRACE("\n");
3148 pfd.fd = nSocket;
3149 pfd.events = POLLIN;
3151 while (nRecv < MAX_REPLY_LEN)
3153 if (poll(&pfd,1, RESPONSE_TIMEOUT * 1000) > 0)
3155 if (recv(nSocket, &lpszBuffer[nRecv], 1, 0) <= 0)
3157 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
3158 goto lend;
3161 if (lpszBuffer[nRecv] == '\n')
3163 bSuccess = TRUE;
3164 break;
3166 if (lpszBuffer[nRecv] != '\r')
3167 nRecv++;
3169 else
3171 INTERNET_SetLastError(ERROR_INTERNET_TIMEOUT);
3172 goto lend;
3176 lend:
3177 if (bSuccess)
3179 lpszBuffer[nRecv] = '\0';
3180 *dwLen = nRecv - 1;
3181 TRACE(":%d %s\n", nRecv, lpszBuffer);
3182 return lpszBuffer;
3184 else
3186 return NULL;
3190 /**********************************************************
3191 * InternetQueryDataAvailable (WININET.@)
3193 * Determines how much data is available to be read.
3195 * RETURNS
3196 * TRUE on success, FALSE if an error occurred. If
3197 * INTERNET_FLAG_ASYNC was specified in InternetOpen, and
3198 * no data is presently available, FALSE is returned with
3199 * the last error ERROR_IO_PENDING; a callback with status
3200 * INTERNET_STATUS_REQUEST_COMPLETE will be sent when more
3201 * data is available.
3203 BOOL WINAPI InternetQueryDataAvailable( HINTERNET hFile,
3204 LPDWORD lpdwNumberOfBytesAvailble,
3205 DWORD dwFlags, DWORD_PTR dwContext)
3207 object_header_t *hdr;
3208 DWORD res;
3210 TRACE("(%p %p %x %lx)\n", hFile, lpdwNumberOfBytesAvailble, dwFlags, dwContext);
3212 hdr = WININET_GetObject( hFile );
3213 if (!hdr) {
3214 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
3215 return FALSE;
3218 if(hdr->vtbl->QueryDataAvailable) {
3219 res = hdr->vtbl->QueryDataAvailable(hdr, lpdwNumberOfBytesAvailble, dwFlags, dwContext);
3220 }else {
3221 WARN("wrong handle\n");
3222 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
3225 WININET_Release(hdr);
3227 if(res != ERROR_SUCCESS)
3228 SetLastError(res);
3229 return res == ERROR_SUCCESS;
3233 /***********************************************************************
3234 * InternetLockRequestFile (WININET.@)
3236 BOOL WINAPI InternetLockRequestFile( HINTERNET hInternet, HANDLE
3237 *lphLockReqHandle)
3239 FIXME("STUB\n");
3240 return FALSE;
3243 BOOL WINAPI InternetUnlockRequestFile( HANDLE hLockHandle)
3245 FIXME("STUB\n");
3246 return FALSE;
3250 /***********************************************************************
3251 * InternetAutodial (WININET.@)
3253 * On windows this function is supposed to dial the default internet
3254 * connection. We don't want to have Wine dial out to the internet so
3255 * we return TRUE by default. It might be nice to check if we are connected.
3257 * RETURNS
3258 * TRUE on success
3259 * FALSE on failure
3262 BOOL WINAPI InternetAutodial(DWORD dwFlags, HWND hwndParent)
3264 FIXME("STUB\n");
3266 /* Tell that we are connected to the internet. */
3267 return TRUE;
3270 /***********************************************************************
3271 * InternetAutodialHangup (WININET.@)
3273 * Hangs up a connection made with InternetAutodial
3275 * PARAM
3276 * dwReserved
3277 * RETURNS
3278 * TRUE on success
3279 * FALSE on failure
3282 BOOL WINAPI InternetAutodialHangup(DWORD dwReserved)
3284 FIXME("STUB\n");
3286 /* we didn't dial, we don't disconnect */
3287 return TRUE;
3290 /***********************************************************************
3291 * InternetCombineUrlA (WININET.@)
3293 * Combine a base URL with a relative URL
3295 * RETURNS
3296 * TRUE on success
3297 * FALSE on failure
3301 BOOL WINAPI InternetCombineUrlA(LPCSTR lpszBaseUrl, LPCSTR lpszRelativeUrl,
3302 LPSTR lpszBuffer, LPDWORD lpdwBufferLength,
3303 DWORD dwFlags)
3305 HRESULT hr=S_OK;
3307 TRACE("(%s, %s, %p, %p, 0x%08x)\n", debugstr_a(lpszBaseUrl), debugstr_a(lpszRelativeUrl), lpszBuffer, lpdwBufferLength, dwFlags);
3309 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
3310 dwFlags ^= ICU_NO_ENCODE;
3311 hr=UrlCombineA(lpszBaseUrl,lpszRelativeUrl,lpszBuffer,lpdwBufferLength,dwFlags);
3313 return (hr==S_OK);
3316 /***********************************************************************
3317 * InternetCombineUrlW (WININET.@)
3319 * Combine a base URL with a relative URL
3321 * RETURNS
3322 * TRUE on success
3323 * FALSE on failure
3327 BOOL WINAPI InternetCombineUrlW(LPCWSTR lpszBaseUrl, LPCWSTR lpszRelativeUrl,
3328 LPWSTR lpszBuffer, LPDWORD lpdwBufferLength,
3329 DWORD dwFlags)
3331 HRESULT hr=S_OK;
3333 TRACE("(%s, %s, %p, %p, 0x%08x)\n", debugstr_w(lpszBaseUrl), debugstr_w(lpszRelativeUrl), lpszBuffer, lpdwBufferLength, dwFlags);
3335 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
3336 dwFlags ^= ICU_NO_ENCODE;
3337 hr=UrlCombineW(lpszBaseUrl,lpszRelativeUrl,lpszBuffer,lpdwBufferLength,dwFlags);
3339 return (hr==S_OK);
3342 /* max port num is 65535 => 5 digits */
3343 #define MAX_WORD_DIGITS 5
3345 #define URL_GET_COMP_LENGTH(url, component) ((url)->dw##component##Length ? \
3346 (url)->dw##component##Length : strlenW((url)->lpsz##component))
3347 #define URL_GET_COMP_LENGTHA(url, component) ((url)->dw##component##Length ? \
3348 (url)->dw##component##Length : strlen((url)->lpsz##component))
3350 static BOOL url_uses_default_port(INTERNET_SCHEME nScheme, INTERNET_PORT nPort)
3352 if ((nScheme == INTERNET_SCHEME_HTTP) &&
3353 (nPort == INTERNET_DEFAULT_HTTP_PORT))
3354 return TRUE;
3355 if ((nScheme == INTERNET_SCHEME_HTTPS) &&
3356 (nPort == INTERNET_DEFAULT_HTTPS_PORT))
3357 return TRUE;
3358 if ((nScheme == INTERNET_SCHEME_FTP) &&
3359 (nPort == INTERNET_DEFAULT_FTP_PORT))
3360 return TRUE;
3361 if ((nScheme == INTERNET_SCHEME_GOPHER) &&
3362 (nPort == INTERNET_DEFAULT_GOPHER_PORT))
3363 return TRUE;
3365 if (nPort == INTERNET_INVALID_PORT_NUMBER)
3366 return TRUE;
3368 return FALSE;
3371 /* opaque urls do not fit into the standard url hierarchy and don't have
3372 * two following slashes */
3373 static inline BOOL scheme_is_opaque(INTERNET_SCHEME nScheme)
3375 return (nScheme != INTERNET_SCHEME_FTP) &&
3376 (nScheme != INTERNET_SCHEME_GOPHER) &&
3377 (nScheme != INTERNET_SCHEME_HTTP) &&
3378 (nScheme != INTERNET_SCHEME_HTTPS) &&
3379 (nScheme != INTERNET_SCHEME_FILE);
3382 static LPCWSTR INTERNET_GetSchemeString(INTERNET_SCHEME scheme)
3384 int index;
3385 if (scheme < INTERNET_SCHEME_FIRST)
3386 return NULL;
3387 index = scheme - INTERNET_SCHEME_FIRST;
3388 if (index >= sizeof(url_schemes)/sizeof(url_schemes[0]))
3389 return NULL;
3390 return (LPCWSTR)url_schemes[index];
3393 /* we can calculate using ansi strings because we're just
3394 * calculating string length, not size
3396 static BOOL calc_url_length(LPURL_COMPONENTSW lpUrlComponents,
3397 LPDWORD lpdwUrlLength)
3399 INTERNET_SCHEME nScheme;
3401 *lpdwUrlLength = 0;
3403 if (lpUrlComponents->lpszScheme)
3405 DWORD dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, Scheme);
3406 *lpdwUrlLength += dwLen;
3407 nScheme = GetInternetSchemeW(lpUrlComponents->lpszScheme, dwLen);
3409 else
3411 LPCWSTR scheme;
3413 nScheme = lpUrlComponents->nScheme;
3415 if (nScheme == INTERNET_SCHEME_DEFAULT)
3416 nScheme = INTERNET_SCHEME_HTTP;
3417 scheme = INTERNET_GetSchemeString(nScheme);
3418 *lpdwUrlLength += strlenW(scheme);
3421 (*lpdwUrlLength)++; /* ':' */
3422 if (!scheme_is_opaque(nScheme) || lpUrlComponents->lpszHostName)
3423 *lpdwUrlLength += strlen("//");
3425 if (lpUrlComponents->lpszUserName)
3427 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, UserName);
3428 *lpdwUrlLength += strlen("@");
3430 else
3432 if (lpUrlComponents->lpszPassword)
3434 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
3435 return FALSE;
3439 if (lpUrlComponents->lpszPassword)
3441 *lpdwUrlLength += strlen(":");
3442 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, Password);
3445 if (lpUrlComponents->lpszHostName)
3447 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, HostName);
3449 if (!url_uses_default_port(nScheme, lpUrlComponents->nPort))
3451 char szPort[MAX_WORD_DIGITS+1];
3453 sprintf(szPort, "%d", lpUrlComponents->nPort);
3454 *lpdwUrlLength += strlen(szPort);
3455 *lpdwUrlLength += strlen(":");
3458 if (lpUrlComponents->lpszUrlPath && *lpUrlComponents->lpszUrlPath != '/')
3459 (*lpdwUrlLength)++; /* '/' */
3462 if (lpUrlComponents->lpszUrlPath)
3463 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, UrlPath);
3465 if (lpUrlComponents->lpszExtraInfo)
3466 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, ExtraInfo);
3468 return TRUE;
3471 static void convert_urlcomp_atow(LPURL_COMPONENTSA lpUrlComponents, LPURL_COMPONENTSW urlCompW)
3473 INT len;
3475 ZeroMemory(urlCompW, sizeof(URL_COMPONENTSW));
3477 urlCompW->dwStructSize = sizeof(URL_COMPONENTSW);
3478 urlCompW->dwSchemeLength = lpUrlComponents->dwSchemeLength;
3479 urlCompW->nScheme = lpUrlComponents->nScheme;
3480 urlCompW->dwHostNameLength = lpUrlComponents->dwHostNameLength;
3481 urlCompW->nPort = lpUrlComponents->nPort;
3482 urlCompW->dwUserNameLength = lpUrlComponents->dwUserNameLength;
3483 urlCompW->dwPasswordLength = lpUrlComponents->dwPasswordLength;
3484 urlCompW->dwUrlPathLength = lpUrlComponents->dwUrlPathLength;
3485 urlCompW->dwExtraInfoLength = lpUrlComponents->dwExtraInfoLength;
3487 if (lpUrlComponents->lpszScheme)
3489 len = URL_GET_COMP_LENGTHA(lpUrlComponents, Scheme) + 1;
3490 urlCompW->lpszScheme = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3491 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszScheme,
3492 -1, urlCompW->lpszScheme, len);
3495 if (lpUrlComponents->lpszHostName)
3497 len = URL_GET_COMP_LENGTHA(lpUrlComponents, HostName) + 1;
3498 urlCompW->lpszHostName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3499 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszHostName,
3500 -1, urlCompW->lpszHostName, len);
3503 if (lpUrlComponents->lpszUserName)
3505 len = URL_GET_COMP_LENGTHA(lpUrlComponents, UserName) + 1;
3506 urlCompW->lpszUserName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3507 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszUserName,
3508 -1, urlCompW->lpszUserName, len);
3511 if (lpUrlComponents->lpszPassword)
3513 len = URL_GET_COMP_LENGTHA(lpUrlComponents, Password) + 1;
3514 urlCompW->lpszPassword = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3515 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszPassword,
3516 -1, urlCompW->lpszPassword, len);
3519 if (lpUrlComponents->lpszUrlPath)
3521 len = URL_GET_COMP_LENGTHA(lpUrlComponents, UrlPath) + 1;
3522 urlCompW->lpszUrlPath = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3523 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszUrlPath,
3524 -1, urlCompW->lpszUrlPath, len);
3527 if (lpUrlComponents->lpszExtraInfo)
3529 len = URL_GET_COMP_LENGTHA(lpUrlComponents, ExtraInfo) + 1;
3530 urlCompW->lpszExtraInfo = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3531 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszExtraInfo,
3532 -1, urlCompW->lpszExtraInfo, len);
3536 /***********************************************************************
3537 * InternetCreateUrlA (WININET.@)
3539 * See InternetCreateUrlW.
3541 BOOL WINAPI InternetCreateUrlA(LPURL_COMPONENTSA lpUrlComponents, DWORD dwFlags,
3542 LPSTR lpszUrl, LPDWORD lpdwUrlLength)
3544 BOOL ret;
3545 LPWSTR urlW = NULL;
3546 URL_COMPONENTSW urlCompW;
3548 TRACE("(%p,%d,%p,%p)\n", lpUrlComponents, dwFlags, lpszUrl, lpdwUrlLength);
3550 if (!lpUrlComponents || lpUrlComponents->dwStructSize != sizeof(URL_COMPONENTSW) || !lpdwUrlLength)
3552 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
3553 return FALSE;
3556 convert_urlcomp_atow(lpUrlComponents, &urlCompW);
3558 if (lpszUrl)
3559 urlW = HeapAlloc(GetProcessHeap(), 0, *lpdwUrlLength * sizeof(WCHAR));
3561 ret = InternetCreateUrlW(&urlCompW, dwFlags, urlW, lpdwUrlLength);
3563 if (!ret && (GetLastError() == ERROR_INSUFFICIENT_BUFFER))
3564 *lpdwUrlLength /= sizeof(WCHAR);
3566 /* on success, lpdwUrlLength points to the size of urlW in WCHARS
3567 * minus one, so add one to leave room for NULL terminator
3569 if (ret)
3570 WideCharToMultiByte(CP_ACP, 0, urlW, -1, lpszUrl, *lpdwUrlLength + 1, NULL, NULL);
3572 HeapFree(GetProcessHeap(), 0, urlCompW.lpszScheme);
3573 HeapFree(GetProcessHeap(), 0, urlCompW.lpszHostName);
3574 HeapFree(GetProcessHeap(), 0, urlCompW.lpszUserName);
3575 HeapFree(GetProcessHeap(), 0, urlCompW.lpszPassword);
3576 HeapFree(GetProcessHeap(), 0, urlCompW.lpszUrlPath);
3577 HeapFree(GetProcessHeap(), 0, urlCompW.lpszExtraInfo);
3578 HeapFree(GetProcessHeap(), 0, urlW);
3580 return ret;
3583 /***********************************************************************
3584 * InternetCreateUrlW (WININET.@)
3586 * Creates a URL from its component parts.
3588 * PARAMS
3589 * lpUrlComponents [I] URL Components.
3590 * dwFlags [I] Flags. See notes.
3591 * lpszUrl [I] Buffer in which to store the created URL.
3592 * lpdwUrlLength [I/O] On input, the length of the buffer pointed to by
3593 * lpszUrl in characters. On output, the number of bytes
3594 * required to store the URL including terminator.
3596 * NOTES
3598 * The dwFlags parameter can be zero or more of the following:
3599 *|ICU_ESCAPE - Generates escape sequences for unsafe characters in the path and extra info of the URL.
3601 * RETURNS
3602 * TRUE on success
3603 * FALSE on failure
3606 BOOL WINAPI InternetCreateUrlW(LPURL_COMPONENTSW lpUrlComponents, DWORD dwFlags,
3607 LPWSTR lpszUrl, LPDWORD lpdwUrlLength)
3609 DWORD dwLen;
3610 INTERNET_SCHEME nScheme;
3612 static const WCHAR slashSlashW[] = {'/','/'};
3613 static const WCHAR percentD[] = {'%','d',0};
3615 TRACE("(%p,%d,%p,%p)\n", lpUrlComponents, dwFlags, lpszUrl, lpdwUrlLength);
3617 if (!lpUrlComponents || lpUrlComponents->dwStructSize != sizeof(URL_COMPONENTSW) || !lpdwUrlLength)
3619 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
3620 return FALSE;
3623 if (!calc_url_length(lpUrlComponents, &dwLen))
3624 return FALSE;
3626 if (!lpszUrl || *lpdwUrlLength < dwLen)
3628 *lpdwUrlLength = (dwLen + 1) * sizeof(WCHAR);
3629 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
3630 return FALSE;
3633 *lpdwUrlLength = dwLen;
3634 lpszUrl[0] = 0x00;
3636 dwLen = 0;
3638 if (lpUrlComponents->lpszScheme)
3640 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, Scheme);
3641 memcpy(lpszUrl, lpUrlComponents->lpszScheme, dwLen * sizeof(WCHAR));
3642 lpszUrl += dwLen;
3644 nScheme = GetInternetSchemeW(lpUrlComponents->lpszScheme, dwLen);
3646 else
3648 LPCWSTR scheme;
3649 nScheme = lpUrlComponents->nScheme;
3651 if (nScheme == INTERNET_SCHEME_DEFAULT)
3652 nScheme = INTERNET_SCHEME_HTTP;
3654 scheme = INTERNET_GetSchemeString(nScheme);
3655 dwLen = strlenW(scheme);
3656 memcpy(lpszUrl, scheme, dwLen * sizeof(WCHAR));
3657 lpszUrl += dwLen;
3660 /* all schemes are followed by at least a colon */
3661 *lpszUrl = ':';
3662 lpszUrl++;
3664 if (!scheme_is_opaque(nScheme) || lpUrlComponents->lpszHostName)
3666 memcpy(lpszUrl, slashSlashW, sizeof(slashSlashW));
3667 lpszUrl += sizeof(slashSlashW)/sizeof(slashSlashW[0]);
3670 if (lpUrlComponents->lpszUserName)
3672 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, UserName);
3673 memcpy(lpszUrl, lpUrlComponents->lpszUserName, dwLen * sizeof(WCHAR));
3674 lpszUrl += dwLen;
3676 if (lpUrlComponents->lpszPassword)
3678 *lpszUrl = ':';
3679 lpszUrl++;
3681 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, Password);
3682 memcpy(lpszUrl, lpUrlComponents->lpszPassword, dwLen * sizeof(WCHAR));
3683 lpszUrl += dwLen;
3686 *lpszUrl = '@';
3687 lpszUrl++;
3690 if (lpUrlComponents->lpszHostName)
3692 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, HostName);
3693 memcpy(lpszUrl, lpUrlComponents->lpszHostName, dwLen * sizeof(WCHAR));
3694 lpszUrl += dwLen;
3696 if (!url_uses_default_port(nScheme, lpUrlComponents->nPort))
3698 WCHAR szPort[MAX_WORD_DIGITS+1];
3700 sprintfW(szPort, percentD, lpUrlComponents->nPort);
3701 *lpszUrl = ':';
3702 lpszUrl++;
3703 dwLen = strlenW(szPort);
3704 memcpy(lpszUrl, szPort, dwLen * sizeof(WCHAR));
3705 lpszUrl += dwLen;
3708 /* add slash between hostname and path if necessary */
3709 if (lpUrlComponents->lpszUrlPath && *lpUrlComponents->lpszUrlPath != '/')
3711 *lpszUrl = '/';
3712 lpszUrl++;
3716 if (lpUrlComponents->lpszUrlPath)
3718 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, UrlPath);
3719 memcpy(lpszUrl, lpUrlComponents->lpszUrlPath, dwLen * sizeof(WCHAR));
3720 lpszUrl += dwLen;
3723 if (lpUrlComponents->lpszExtraInfo)
3725 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, ExtraInfo);
3726 memcpy(lpszUrl, lpUrlComponents->lpszExtraInfo, dwLen * sizeof(WCHAR));
3727 lpszUrl += dwLen;
3730 *lpszUrl = '\0';
3732 return TRUE;
3735 /***********************************************************************
3736 * InternetConfirmZoneCrossingA (WININET.@)
3739 DWORD WINAPI InternetConfirmZoneCrossingA( HWND hWnd, LPSTR szUrlPrev, LPSTR szUrlNew, BOOL bPost )
3741 FIXME("(%p, %s, %s, %x) stub\n", hWnd, debugstr_a(szUrlPrev), debugstr_a(szUrlNew), bPost);
3742 return ERROR_SUCCESS;
3745 /***********************************************************************
3746 * InternetConfirmZoneCrossingW (WININET.@)
3749 DWORD WINAPI InternetConfirmZoneCrossingW( HWND hWnd, LPWSTR szUrlPrev, LPWSTR szUrlNew, BOOL bPost )
3751 FIXME("(%p, %s, %s, %x) stub\n", hWnd, debugstr_w(szUrlPrev), debugstr_w(szUrlNew), bPost);
3752 return ERROR_SUCCESS;
3755 static DWORD zone_preference = 3;
3757 /***********************************************************************
3758 * PrivacySetZonePreferenceW (WININET.@)
3760 DWORD WINAPI PrivacySetZonePreferenceW( DWORD zone, DWORD type, DWORD template, LPCWSTR preference )
3762 FIXME( "%x %x %x %s: stub\n", zone, type, template, debugstr_w(preference) );
3764 zone_preference = template;
3765 return 0;
3768 /***********************************************************************
3769 * PrivacyGetZonePreferenceW (WININET.@)
3771 DWORD WINAPI PrivacyGetZonePreferenceW( DWORD zone, DWORD type, LPDWORD template,
3772 LPWSTR preference, LPDWORD length )
3774 FIXME( "%x %x %p %p %p: stub\n", zone, type, template, preference, length );
3776 if (template) *template = zone_preference;
3777 return 0;
3780 DWORD WINAPI InternetDialA( HWND hwndParent, LPSTR lpszConnectoid, DWORD dwFlags,
3781 DWORD_PTR* lpdwConnection, DWORD dwReserved )
3783 FIXME("(%p, %p, 0x%08x, %p, 0x%08x) stub\n", hwndParent, lpszConnectoid, dwFlags,
3784 lpdwConnection, dwReserved);
3785 return ERROR_SUCCESS;
3788 DWORD WINAPI InternetDialW( HWND hwndParent, LPWSTR lpszConnectoid, DWORD dwFlags,
3789 DWORD_PTR* lpdwConnection, DWORD dwReserved )
3791 FIXME("(%p, %p, 0x%08x, %p, 0x%08x) stub\n", hwndParent, lpszConnectoid, dwFlags,
3792 lpdwConnection, dwReserved);
3793 return ERROR_SUCCESS;
3796 BOOL WINAPI InternetGoOnlineA( LPSTR lpszURL, HWND hwndParent, DWORD dwReserved )
3798 FIXME("(%s, %p, 0x%08x) stub\n", debugstr_a(lpszURL), hwndParent, dwReserved);
3799 return TRUE;
3802 BOOL WINAPI InternetGoOnlineW( LPWSTR lpszURL, HWND hwndParent, DWORD dwReserved )
3804 FIXME("(%s, %p, 0x%08x) stub\n", debugstr_w(lpszURL), hwndParent, dwReserved);
3805 return TRUE;
3808 DWORD WINAPI InternetHangUp( DWORD_PTR dwConnection, DWORD dwReserved )
3810 FIXME("(0x%08lx, 0x%08x) stub\n", dwConnection, dwReserved);
3811 return ERROR_SUCCESS;
3814 BOOL WINAPI CreateMD5SSOHash( PWSTR pszChallengeInfo, PWSTR pwszRealm, PWSTR pwszTarget,
3815 PBYTE pbHexHash )
3817 FIXME("(%s, %s, %s, %p) stub\n", debugstr_w(pszChallengeInfo), debugstr_w(pwszRealm),
3818 debugstr_w(pwszTarget), pbHexHash);
3819 return FALSE;
3822 BOOL WINAPI ResumeSuspendedDownload( HINTERNET hInternet, DWORD dwError )
3824 FIXME("(%p, 0x%08x) stub\n", hInternet, dwError);
3825 return FALSE;
3828 BOOL WINAPI InternetQueryFortezzaStatus(DWORD *a, DWORD_PTR b)
3830 FIXME("(%p, %08lx) stub\n", a, b);
3831 return 0;