push 73336d9f381967eae40f391d78198b916ed9848d
[wine/hacks.git] / dlls / wininet / internet.c
blobbe1ee164f1bb9e744cd781dd57bdd0f10fc20636
1 /*
2 * Wininet
4 * Copyright 1999 Corel Corporation
5 * Copyright 2002 CodeWeavers Inc.
6 * Copyright 2002 Jaco Greeff
7 * Copyright 2002 TransGaming Technologies Inc.
8 * Copyright 2004 Mike McCormack for CodeWeavers
10 * Ulrich Czekalla
11 * Aric Stewart
12 * David Hammerton
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
29 #include "config.h"
30 #include "wine/port.h"
32 #define MAXHOSTNAME 100 /* from http.c */
34 #include <string.h>
35 #include <stdarg.h>
36 #include <stdio.h>
37 #include <sys/types.h>
38 #ifdef HAVE_SYS_SOCKET_H
39 # include <sys/socket.h>
40 #endif
41 #ifdef HAVE_SYS_TIME_H
42 # include <sys/time.h>
43 #endif
44 #include <stdlib.h>
45 #include <ctype.h>
46 #ifdef HAVE_UNISTD_H
47 # include <unistd.h>
48 #endif
49 #include <assert.h>
51 #include "windef.h"
52 #include "winbase.h"
53 #include "winreg.h"
54 #include "winuser.h"
55 #include "wininet.h"
56 #include "winineti.h"
57 #include "winnls.h"
58 #include "wine/debug.h"
59 #include "winerror.h"
60 #define NO_SHLWAPI_STREAM
61 #include "shlwapi.h"
63 #include "wine/exception.h"
65 #include "internet.h"
66 #include "resource.h"
68 #include "wine/unicode.h"
70 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
72 #define RESPONSE_TIMEOUT 30
74 typedef struct
76 DWORD dwError;
77 CHAR response[MAX_REPLY_LEN];
78 } WITHREADERROR, *LPWITHREADERROR;
80 HINTERNET WINAPI INTERNET_InternetOpenUrlW(LPWININETAPPINFOW hIC, LPCWSTR lpszUrl,
81 LPCWSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext);
83 static DWORD g_dwTlsErrIndex = TLS_OUT_OF_INDEXES;
84 static HMODULE WININET_hModule;
86 #define HANDLE_CHUNK_SIZE 0x10
88 static CRITICAL_SECTION WININET_cs;
89 static CRITICAL_SECTION_DEBUG WININET_cs_debug =
91 0, 0, &WININET_cs,
92 { &WININET_cs_debug.ProcessLocksList, &WININET_cs_debug.ProcessLocksList },
93 0, 0, { (DWORD_PTR)(__FILE__ ": WININET_cs") }
95 static CRITICAL_SECTION WININET_cs = { &WININET_cs_debug, -1, 0, 0, 0, 0 };
97 static LPWININETHANDLEHEADER *WININET_Handles;
98 static UINT WININET_dwNextHandle;
99 static UINT WININET_dwMaxHandles;
101 HINTERNET WININET_AllocHandle( LPWININETHANDLEHEADER info )
103 LPWININETHANDLEHEADER *p;
104 UINT handle = 0, num;
106 list_init( &info->children );
108 EnterCriticalSection( &WININET_cs );
109 if( !WININET_dwMaxHandles )
111 num = HANDLE_CHUNK_SIZE;
112 p = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
113 sizeof (UINT)* num);
114 if( !p )
115 goto end;
116 WININET_Handles = p;
117 WININET_dwMaxHandles = num;
119 if( WININET_dwMaxHandles == WININET_dwNextHandle )
121 num = WININET_dwMaxHandles + HANDLE_CHUNK_SIZE;
122 p = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
123 WININET_Handles, sizeof (UINT)* num);
124 if( !p )
125 goto end;
126 WININET_Handles = p;
127 WININET_dwMaxHandles = num;
130 handle = WININET_dwNextHandle;
131 if( WININET_Handles[handle] )
132 ERR("handle isn't free but should be\n");
133 WININET_Handles[handle] = WININET_AddRef( info );
135 while( WININET_Handles[WININET_dwNextHandle] &&
136 (WININET_dwNextHandle < WININET_dwMaxHandles ) )
137 WININET_dwNextHandle++;
139 end:
140 LeaveCriticalSection( &WININET_cs );
142 return info->hInternet = (HINTERNET) (handle+1);
145 LPWININETHANDLEHEADER WININET_AddRef( LPWININETHANDLEHEADER info )
147 ULONG refs = InterlockedIncrement(&info->refs);
148 TRACE("%p -> refcount = %d\n", info, refs );
149 return info;
152 LPWININETHANDLEHEADER WININET_GetObject( HINTERNET hinternet )
154 LPWININETHANDLEHEADER info = NULL;
155 UINT handle = (UINT) hinternet;
157 EnterCriticalSection( &WININET_cs );
159 if( (handle > 0) && ( handle <= WININET_dwMaxHandles ) &&
160 WININET_Handles[handle-1] )
161 info = WININET_AddRef( WININET_Handles[handle-1] );
163 LeaveCriticalSection( &WININET_cs );
165 TRACE("handle %d -> %p\n", handle, info);
167 return info;
170 BOOL WININET_Release( LPWININETHANDLEHEADER info )
172 ULONG refs = InterlockedDecrement(&info->refs);
173 TRACE( "object %p refcount = %d\n", info, refs );
174 if( !refs )
176 if ( info->vtbl->CloseConnection )
178 TRACE( "closing connection %p\n", info);
179 info->vtbl->CloseConnection( info );
181 INTERNET_SendCallback(info, info->dwContext,
182 INTERNET_STATUS_HANDLE_CLOSING, &info->hInternet,
183 sizeof(HINTERNET));
184 TRACE( "destroying object %p\n", info);
185 if ( info->htype != WH_HINIT )
186 list_remove( &info->entry );
187 info->vtbl->Destroy( info );
189 return TRUE;
192 BOOL WININET_FreeHandle( HINTERNET hinternet )
194 BOOL ret = FALSE;
195 UINT handle = (UINT) hinternet;
196 LPWININETHANDLEHEADER info = NULL, child, next;
198 EnterCriticalSection( &WININET_cs );
200 if( (handle > 0) && ( handle <= WININET_dwMaxHandles ) )
202 handle--;
203 if( WININET_Handles[handle] )
205 info = WININET_Handles[handle];
206 TRACE( "destroying handle %d for object %p\n", handle+1, info);
207 WININET_Handles[handle] = NULL;
208 ret = TRUE;
212 LeaveCriticalSection( &WININET_cs );
214 /* As on native when the equivalent of WININET_Release is called, the handle
215 * is already invalid, but if a new handle is created at this time it does
216 * not yet get assigned the freed handle number */
217 if( info )
219 /* Free all children as native does */
220 LIST_FOR_EACH_ENTRY_SAFE( child, next, &info->children, WININETHANDLEHEADER, entry )
222 TRACE( "freeing child handle %d for parent handle %d\n",
223 (UINT)child->hInternet, handle+1);
224 WININET_FreeHandle( child->hInternet );
226 WININET_Release( info );
229 EnterCriticalSection( &WININET_cs );
231 if( WININET_dwNextHandle > handle && !WININET_Handles[handle] )
232 WININET_dwNextHandle = handle;
234 LeaveCriticalSection( &WININET_cs );
236 return ret;
239 /***********************************************************************
240 * DllMain [Internal] Initializes the internal 'WININET.DLL'.
242 * PARAMS
243 * hinstDLL [I] handle to the DLL's instance
244 * fdwReason [I]
245 * lpvReserved [I] reserved, must be NULL
247 * RETURNS
248 * Success: TRUE
249 * Failure: FALSE
252 BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
254 TRACE("%p,%x,%p\n", hinstDLL, fdwReason, lpvReserved);
256 switch (fdwReason) {
257 case DLL_PROCESS_ATTACH:
259 g_dwTlsErrIndex = TlsAlloc();
261 if (g_dwTlsErrIndex == TLS_OUT_OF_INDEXES)
262 return FALSE;
264 URLCacheContainers_CreateDefaults();
266 WININET_hModule = hinstDLL;
268 case DLL_THREAD_ATTACH:
269 break;
271 case DLL_THREAD_DETACH:
272 if (g_dwTlsErrIndex != TLS_OUT_OF_INDEXES)
274 LPVOID lpwite = TlsGetValue(g_dwTlsErrIndex);
275 HeapFree(GetProcessHeap(), 0, lpwite);
277 break;
279 case DLL_PROCESS_DETACH:
281 URLCacheContainers_DeleteAll();
283 if (g_dwTlsErrIndex != TLS_OUT_OF_INDEXES)
285 HeapFree(GetProcessHeap(), 0, TlsGetValue(g_dwTlsErrIndex));
286 TlsFree(g_dwTlsErrIndex);
288 break;
291 return TRUE;
295 /***********************************************************************
296 * InternetInitializeAutoProxyDll (WININET.@)
298 * Setup the internal proxy
300 * PARAMETERS
301 * dwReserved
303 * RETURNS
304 * FALSE on failure
307 BOOL WINAPI InternetInitializeAutoProxyDll(DWORD dwReserved)
309 FIXME("STUB\n");
310 INTERNET_SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
311 return FALSE;
314 /***********************************************************************
315 * DetectAutoProxyUrl (WININET.@)
317 * Auto detect the proxy url
319 * RETURNS
320 * FALSE on failure
323 BOOL WINAPI DetectAutoProxyUrl(LPSTR lpszAutoProxyUrl,
324 DWORD dwAutoProxyUrlLength, DWORD dwDetectFlags)
326 FIXME("STUB\n");
327 INTERNET_SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
328 return FALSE;
332 /***********************************************************************
333 * INTERNET_ConfigureProxyFromReg
335 * FIXME:
336 * The proxy may be specified in the form 'http=proxy.my.org'
337 * Presumably that means there can be ftp=ftpproxy.my.org too.
339 static BOOL INTERNET_ConfigureProxyFromReg( LPWININETAPPINFOW lpwai )
341 HKEY key;
342 DWORD r, keytype, len, enabled;
343 LPCSTR lpszInternetSettings =
344 "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings";
345 static const WCHAR szProxyServer[] = { 'P','r','o','x','y','S','e','r','v','e','r', 0 };
347 r = RegOpenKeyA(HKEY_CURRENT_USER, lpszInternetSettings, &key);
348 if ( r != ERROR_SUCCESS )
349 return FALSE;
351 len = sizeof enabled;
352 r = RegQueryValueExA( key, "ProxyEnable", NULL, &keytype,
353 (BYTE*)&enabled, &len);
354 if( (r == ERROR_SUCCESS) && enabled )
356 TRACE("Proxy is enabled.\n");
358 /* figure out how much memory the proxy setting takes */
359 r = RegQueryValueExW( key, szProxyServer, NULL, &keytype,
360 NULL, &len);
361 if( (r == ERROR_SUCCESS) && len && (keytype == REG_SZ) )
363 LPWSTR szProxy, p;
364 static const WCHAR szHttp[] = {'h','t','t','p','=',0};
366 szProxy=HeapAlloc( GetProcessHeap(), 0, len );
367 RegQueryValueExW( key, szProxyServer, NULL, &keytype,
368 (BYTE*)szProxy, &len);
370 /* find the http proxy, and strip away everything else */
371 p = strstrW( szProxy, szHttp );
372 if( p )
374 p += lstrlenW(szHttp);
375 lstrcpyW( szProxy, p );
377 p = strchrW( szProxy, ' ' );
378 if( p )
379 *p = 0;
381 lpwai->dwAccessType = INTERNET_OPEN_TYPE_PROXY;
382 lpwai->lpszProxy = szProxy;
384 TRACE("http proxy = %s\n", debugstr_w(lpwai->lpszProxy));
386 else
387 ERR("Couldn't read proxy server settings.\n");
389 else
390 TRACE("Proxy is not enabled.\n");
391 RegCloseKey(key);
393 return enabled;
396 /***********************************************************************
397 * dump_INTERNET_FLAGS
399 * Helper function to TRACE the internet flags.
401 * RETURNS
402 * None
405 static void dump_INTERNET_FLAGS(DWORD dwFlags)
407 #define FE(x) { x, #x }
408 static const wininet_flag_info flag[] = {
409 FE(INTERNET_FLAG_RELOAD),
410 FE(INTERNET_FLAG_RAW_DATA),
411 FE(INTERNET_FLAG_EXISTING_CONNECT),
412 FE(INTERNET_FLAG_ASYNC),
413 FE(INTERNET_FLAG_PASSIVE),
414 FE(INTERNET_FLAG_NO_CACHE_WRITE),
415 FE(INTERNET_FLAG_MAKE_PERSISTENT),
416 FE(INTERNET_FLAG_FROM_CACHE),
417 FE(INTERNET_FLAG_SECURE),
418 FE(INTERNET_FLAG_KEEP_CONNECTION),
419 FE(INTERNET_FLAG_NO_AUTO_REDIRECT),
420 FE(INTERNET_FLAG_READ_PREFETCH),
421 FE(INTERNET_FLAG_NO_COOKIES),
422 FE(INTERNET_FLAG_NO_AUTH),
423 FE(INTERNET_FLAG_CACHE_IF_NET_FAIL),
424 FE(INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP),
425 FE(INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTPS),
426 FE(INTERNET_FLAG_IGNORE_CERT_DATE_INVALID),
427 FE(INTERNET_FLAG_IGNORE_CERT_CN_INVALID),
428 FE(INTERNET_FLAG_RESYNCHRONIZE),
429 FE(INTERNET_FLAG_HYPERLINK),
430 FE(INTERNET_FLAG_NO_UI),
431 FE(INTERNET_FLAG_PRAGMA_NOCACHE),
432 FE(INTERNET_FLAG_CACHE_ASYNC),
433 FE(INTERNET_FLAG_FORMS_SUBMIT),
434 FE(INTERNET_FLAG_NEED_FILE),
435 FE(INTERNET_FLAG_TRANSFER_ASCII),
436 FE(INTERNET_FLAG_TRANSFER_BINARY)
438 #undef FE
439 int i;
441 for (i = 0; i < (sizeof(flag) / sizeof(flag[0])); i++) {
442 if (flag[i].val & dwFlags) {
443 TRACE(" %s", flag[i].name);
444 dwFlags &= ~flag[i].val;
447 if (dwFlags)
448 TRACE(" Unknown flags (%08x)\n", dwFlags);
449 else
450 TRACE("\n");
453 /***********************************************************************
454 * INTERNET_CloseHandle (internal)
456 * Close internet handle
459 static VOID APPINFO_Destroy(WININETHANDLEHEADER *hdr)
461 LPWININETAPPINFOW lpwai = (LPWININETAPPINFOW) hdr;
463 TRACE("%p\n",lpwai);
465 HeapFree(GetProcessHeap(), 0, lpwai->lpszAgent);
466 HeapFree(GetProcessHeap(), 0, lpwai->lpszProxy);
467 HeapFree(GetProcessHeap(), 0, lpwai->lpszProxyBypass);
468 HeapFree(GetProcessHeap(), 0, lpwai->lpszProxyUsername);
469 HeapFree(GetProcessHeap(), 0, lpwai->lpszProxyPassword);
470 HeapFree(GetProcessHeap(), 0, lpwai);
473 static DWORD APPINFO_QueryOption(WININETHANDLEHEADER *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
475 switch(option) {
476 case INTERNET_OPTION_HANDLE_TYPE:
477 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
479 if (*size < sizeof(ULONG))
480 return ERROR_INSUFFICIENT_BUFFER;
482 *size = sizeof(DWORD);
483 *(DWORD*)buffer = INTERNET_HANDLE_TYPE_INTERNET;
484 return ERROR_SUCCESS;
487 FIXME("Not implemented option %d\n", option);
488 return ERROR_INTERNET_INVALID_OPTION;
491 static const HANDLEHEADERVtbl APPINFOVtbl = {
492 APPINFO_Destroy,
493 NULL,
494 APPINFO_QueryOption,
495 NULL,
496 NULL,
497 NULL,
498 NULL,
499 NULL,
500 NULL
504 /***********************************************************************
505 * InternetOpenW (WININET.@)
507 * Per-application initialization of wininet
509 * RETURNS
510 * HINTERNET on success
511 * NULL on failure
514 HINTERNET WINAPI InternetOpenW(LPCWSTR lpszAgent, DWORD dwAccessType,
515 LPCWSTR lpszProxy, LPCWSTR lpszProxyBypass, DWORD dwFlags)
517 LPWININETAPPINFOW lpwai = NULL;
518 HINTERNET handle = NULL;
520 if (TRACE_ON(wininet)) {
521 #define FE(x) { x, #x }
522 static const wininet_flag_info access_type[] = {
523 FE(INTERNET_OPEN_TYPE_PRECONFIG),
524 FE(INTERNET_OPEN_TYPE_DIRECT),
525 FE(INTERNET_OPEN_TYPE_PROXY),
526 FE(INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY)
528 #undef FE
529 DWORD i;
530 const char *access_type_str = "Unknown";
532 TRACE("(%s, %i, %s, %s, %i)\n", debugstr_w(lpszAgent), dwAccessType,
533 debugstr_w(lpszProxy), debugstr_w(lpszProxyBypass), dwFlags);
534 for (i = 0; i < (sizeof(access_type) / sizeof(access_type[0])); i++) {
535 if (access_type[i].val == dwAccessType) {
536 access_type_str = access_type[i].name;
537 break;
540 TRACE(" access type : %s\n", access_type_str);
541 TRACE(" flags :");
542 dump_INTERNET_FLAGS(dwFlags);
545 /* Clear any error information */
546 INTERNET_SetLastError(0);
548 lpwai = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETAPPINFOW));
549 if (NULL == lpwai)
551 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
552 goto lend;
555 lpwai->hdr.htype = WH_HINIT;
556 lpwai->hdr.vtbl = &APPINFOVtbl;
557 lpwai->hdr.dwFlags = dwFlags;
558 lpwai->hdr.refs = 1;
559 lpwai->dwAccessType = dwAccessType;
560 lpwai->lpszProxyUsername = NULL;
561 lpwai->lpszProxyPassword = NULL;
563 handle = WININET_AllocHandle( &lpwai->hdr );
564 if( !handle )
566 HeapFree( GetProcessHeap(), 0, lpwai );
567 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
568 goto lend;
571 if (NULL != lpszAgent)
573 lpwai->lpszAgent = HeapAlloc( GetProcessHeap(),0,
574 (strlenW(lpszAgent)+1)*sizeof(WCHAR));
575 if (lpwai->lpszAgent)
576 lstrcpyW( lpwai->lpszAgent, lpszAgent );
578 if(dwAccessType == INTERNET_OPEN_TYPE_PRECONFIG)
579 INTERNET_ConfigureProxyFromReg( lpwai );
580 else if (NULL != lpszProxy)
582 lpwai->lpszProxy = HeapAlloc( GetProcessHeap(), 0,
583 (strlenW(lpszProxy)+1)*sizeof(WCHAR));
584 if (lpwai->lpszProxy)
585 lstrcpyW( lpwai->lpszProxy, lpszProxy );
588 if (NULL != lpszProxyBypass)
590 lpwai->lpszProxyBypass = HeapAlloc( GetProcessHeap(), 0,
591 (strlenW(lpszProxyBypass)+1)*sizeof(WCHAR));
592 if (lpwai->lpszProxyBypass)
593 lstrcpyW( lpwai->lpszProxyBypass, lpszProxyBypass );
596 lend:
597 if( lpwai )
598 WININET_Release( &lpwai->hdr );
600 TRACE("returning %p\n", lpwai);
602 return handle;
606 /***********************************************************************
607 * InternetOpenA (WININET.@)
609 * Per-application initialization of wininet
611 * RETURNS
612 * HINTERNET on success
613 * NULL on failure
616 HINTERNET WINAPI InternetOpenA(LPCSTR lpszAgent, DWORD dwAccessType,
617 LPCSTR lpszProxy, LPCSTR lpszProxyBypass, DWORD dwFlags)
619 HINTERNET rc = NULL;
620 INT len;
621 WCHAR *szAgent = NULL, *szProxy = NULL, *szBypass = NULL;
623 TRACE("(%s, 0x%08x, %s, %s, 0x%08x)\n", debugstr_a(lpszAgent),
624 dwAccessType, debugstr_a(lpszProxy), debugstr_a(lpszProxyBypass), dwFlags);
626 if( lpszAgent )
628 len = MultiByteToWideChar(CP_ACP, 0, lpszAgent, -1, NULL, 0);
629 szAgent = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
630 MultiByteToWideChar(CP_ACP, 0, lpszAgent, -1, szAgent, len);
633 if( lpszProxy )
635 len = MultiByteToWideChar(CP_ACP, 0, lpszProxy, -1, NULL, 0);
636 szProxy = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
637 MultiByteToWideChar(CP_ACP, 0, lpszProxy, -1, szProxy, len);
640 if( lpszProxyBypass )
642 len = MultiByteToWideChar(CP_ACP, 0, lpszProxyBypass, -1, NULL, 0);
643 szBypass = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
644 MultiByteToWideChar(CP_ACP, 0, lpszProxyBypass, -1, szBypass, len);
647 rc = InternetOpenW(szAgent, dwAccessType, szProxy, szBypass, dwFlags);
649 HeapFree(GetProcessHeap(), 0, szAgent);
650 HeapFree(GetProcessHeap(), 0, szProxy);
651 HeapFree(GetProcessHeap(), 0, szBypass);
653 return rc;
656 /***********************************************************************
657 * InternetGetLastResponseInfoA (WININET.@)
659 * Return last wininet error description on the calling thread
661 * RETURNS
662 * TRUE on success of writing to buffer
663 * FALSE on failure
666 BOOL WINAPI InternetGetLastResponseInfoA(LPDWORD lpdwError,
667 LPSTR lpszBuffer, LPDWORD lpdwBufferLength)
669 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
671 TRACE("\n");
673 if (lpwite)
675 *lpdwError = lpwite->dwError;
676 if (lpwite->dwError)
678 memcpy(lpszBuffer, lpwite->response, *lpdwBufferLength);
679 *lpdwBufferLength = strlen(lpszBuffer);
681 else
682 *lpdwBufferLength = 0;
684 else
686 *lpdwError = 0;
687 *lpdwBufferLength = 0;
690 return TRUE;
693 /***********************************************************************
694 * InternetGetLastResponseInfoW (WININET.@)
696 * Return last wininet error description on the calling thread
698 * RETURNS
699 * TRUE on success of writing to buffer
700 * FALSE on failure
703 BOOL WINAPI InternetGetLastResponseInfoW(LPDWORD lpdwError,
704 LPWSTR lpszBuffer, LPDWORD lpdwBufferLength)
706 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
708 TRACE("\n");
710 if (lpwite)
712 *lpdwError = lpwite->dwError;
713 if (lpwite->dwError)
715 memcpy(lpszBuffer, lpwite->response, *lpdwBufferLength);
716 *lpdwBufferLength = lstrlenW(lpszBuffer);
718 else
719 *lpdwBufferLength = 0;
721 else
723 *lpdwError = 0;
724 *lpdwBufferLength = 0;
727 return TRUE;
730 /***********************************************************************
731 * InternetGetConnectedState (WININET.@)
733 * Return connected state
735 * RETURNS
736 * TRUE if connected
737 * if lpdwStatus is not null, return the status (off line,
738 * modem, lan...) in it.
739 * FALSE if not connected
741 BOOL WINAPI InternetGetConnectedState(LPDWORD lpdwStatus, DWORD dwReserved)
743 TRACE("(%p, 0x%08x)\n", lpdwStatus, dwReserved);
745 if (lpdwStatus) {
746 FIXME("always returning LAN connection.\n");
747 *lpdwStatus = INTERNET_CONNECTION_LAN;
749 return TRUE;
753 /***********************************************************************
754 * InternetGetConnectedStateExW (WININET.@)
756 * Return connected state
758 * PARAMS
760 * lpdwStatus [O] Flags specifying the status of the internet connection.
761 * lpszConnectionName [O] Pointer to buffer to receive the friendly name of the internet connection.
762 * dwNameLen [I] Size of the buffer, in characters.
763 * dwReserved [I] Reserved. Must be set to 0.
765 * RETURNS
766 * TRUE if connected
767 * if lpdwStatus is not null, return the status (off line,
768 * modem, lan...) in it.
769 * FALSE if not connected
771 * NOTES
772 * If the system has no available network connections, an empty string is
773 * stored in lpszConnectionName. If there is a LAN connection, a localized
774 * "LAN Connection" string is stored. Presumably, if only a dial-up
775 * connection is available then the name of the dial-up connection is
776 * returned. Why any application, other than the "Internet Settings" CPL,
777 * would want to use this function instead of the simpler InternetGetConnectedStateW
778 * function is beyond me.
780 BOOL WINAPI InternetGetConnectedStateExW(LPDWORD lpdwStatus, LPWSTR lpszConnectionName,
781 DWORD dwNameLen, DWORD dwReserved)
783 TRACE("(%p, %p, %d, 0x%08x)\n", lpdwStatus, lpszConnectionName, dwNameLen, dwReserved);
785 /* Must be zero */
786 if(dwReserved)
787 return FALSE;
789 if (lpdwStatus) {
790 FIXME("always returning LAN connection.\n");
791 *lpdwStatus = INTERNET_CONNECTION_LAN;
793 return LoadStringW(WININET_hModule, IDS_LANCONNECTION, lpszConnectionName, dwNameLen);
797 /***********************************************************************
798 * InternetGetConnectedStateExA (WININET.@)
800 BOOL WINAPI InternetGetConnectedStateExA(LPDWORD lpdwStatus, LPSTR lpszConnectionName,
801 DWORD dwNameLen, DWORD dwReserved)
803 LPWSTR lpwszConnectionName = NULL;
804 BOOL rc;
806 TRACE("(%p, %p, %d, 0x%08x)\n", lpdwStatus, lpszConnectionName, dwNameLen, dwReserved);
808 if (lpszConnectionName && dwNameLen > 0)
809 lpwszConnectionName= HeapAlloc(GetProcessHeap(), 0, dwNameLen * sizeof(WCHAR));
811 rc = InternetGetConnectedStateExW(lpdwStatus,lpwszConnectionName, dwNameLen,
812 dwReserved);
813 if (rc && lpwszConnectionName)
815 WideCharToMultiByte(CP_ACP,0,lpwszConnectionName,-1,lpszConnectionName,
816 dwNameLen, NULL, NULL);
818 HeapFree(GetProcessHeap(),0,lpwszConnectionName);
821 return rc;
825 /***********************************************************************
826 * InternetConnectW (WININET.@)
828 * Open a ftp, gopher or http session
830 * RETURNS
831 * HINTERNET a session handle on success
832 * NULL on failure
835 HINTERNET WINAPI InternetConnectW(HINTERNET hInternet,
836 LPCWSTR lpszServerName, INTERNET_PORT nServerPort,
837 LPCWSTR lpszUserName, LPCWSTR lpszPassword,
838 DWORD dwService, DWORD dwFlags, DWORD_PTR dwContext)
840 LPWININETAPPINFOW hIC;
841 HINTERNET rc = NULL;
843 TRACE("(%p, %s, %i, %s, %s, %i, %i, %lx)\n", hInternet, debugstr_w(lpszServerName),
844 nServerPort, debugstr_w(lpszUserName), debugstr_w(lpszPassword),
845 dwService, dwFlags, dwContext);
847 if (!lpszServerName)
849 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
850 return NULL;
853 /* Clear any error information */
854 INTERNET_SetLastError(0);
855 hIC = (LPWININETAPPINFOW) WININET_GetObject( hInternet );
856 if ( (hIC == NULL) || (hIC->hdr.htype != WH_HINIT) )
858 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
859 goto lend;
862 switch (dwService)
864 case INTERNET_SERVICE_FTP:
865 rc = FTP_Connect(hIC, lpszServerName, nServerPort,
866 lpszUserName, lpszPassword, dwFlags, dwContext, 0);
867 break;
869 case INTERNET_SERVICE_HTTP:
870 rc = HTTP_Connect(hIC, lpszServerName, nServerPort,
871 lpszUserName, lpszPassword, dwFlags, dwContext, 0);
872 break;
874 case INTERNET_SERVICE_GOPHER:
875 default:
876 break;
878 lend:
879 if( hIC )
880 WININET_Release( &hIC->hdr );
882 TRACE("returning %p\n", rc);
883 return rc;
887 /***********************************************************************
888 * InternetConnectA (WININET.@)
890 * Open a ftp, gopher or http session
892 * RETURNS
893 * HINTERNET a session handle on success
894 * NULL on failure
897 HINTERNET WINAPI InternetConnectA(HINTERNET hInternet,
898 LPCSTR lpszServerName, INTERNET_PORT nServerPort,
899 LPCSTR lpszUserName, LPCSTR lpszPassword,
900 DWORD dwService, DWORD dwFlags, DWORD_PTR dwContext)
902 HINTERNET rc = NULL;
903 INT len = 0;
904 LPWSTR szServerName = NULL;
905 LPWSTR szUserName = NULL;
906 LPWSTR szPassword = NULL;
908 if (lpszServerName)
910 len = MultiByteToWideChar(CP_ACP, 0, lpszServerName, -1, NULL, 0);
911 szServerName = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
912 MultiByteToWideChar(CP_ACP, 0, lpszServerName, -1, szServerName, len);
914 if (lpszUserName)
916 len = MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, NULL, 0);
917 szUserName = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
918 MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, szUserName, len);
920 if (lpszPassword)
922 len = MultiByteToWideChar(CP_ACP, 0, lpszPassword, -1, NULL, 0);
923 szPassword = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
924 MultiByteToWideChar(CP_ACP, 0, lpszPassword, -1, szPassword, len);
928 rc = InternetConnectW(hInternet, szServerName, nServerPort,
929 szUserName, szPassword, dwService, dwFlags, dwContext);
931 HeapFree(GetProcessHeap(), 0, szServerName);
932 HeapFree(GetProcessHeap(), 0, szUserName);
933 HeapFree(GetProcessHeap(), 0, szPassword);
934 return rc;
938 /***********************************************************************
939 * InternetFindNextFileA (WININET.@)
941 * Continues a file search from a previous call to FindFirstFile
943 * RETURNS
944 * TRUE on success
945 * FALSE on failure
948 BOOL WINAPI InternetFindNextFileA(HINTERNET hFind, LPVOID lpvFindData)
950 BOOL ret;
951 WIN32_FIND_DATAW fd;
953 ret = InternetFindNextFileW(hFind, lpvFindData?&fd:NULL);
954 if(lpvFindData)
955 WININET_find_data_WtoA(&fd, (LPWIN32_FIND_DATAA)lpvFindData);
956 return ret;
959 /***********************************************************************
960 * InternetFindNextFileW (WININET.@)
962 * Continues a file search from a previous call to FindFirstFile
964 * RETURNS
965 * TRUE on success
966 * FALSE on failure
969 BOOL WINAPI InternetFindNextFileW(HINTERNET hFind, LPVOID lpvFindData)
971 WININETHANDLEHEADER *hdr;
972 DWORD res;
974 TRACE("\n");
976 hdr = WININET_GetObject(hFind);
977 if(!hdr) {
978 WARN("Invalid handle\n");
979 SetLastError(ERROR_INVALID_HANDLE);
980 return FALSE;
983 if(hdr->vtbl->FindNextFileW) {
984 res = hdr->vtbl->FindNextFileW(hdr, lpvFindData);
985 }else {
986 WARN("Handle doesn't support NextFile\n");
987 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
990 WININET_Release(hdr);
992 if(res != ERROR_SUCCESS)
993 SetLastError(res);
994 return res == ERROR_SUCCESS;
997 /***********************************************************************
998 * InternetCloseHandle (WININET.@)
1000 * Generic close handle function
1002 * RETURNS
1003 * TRUE on success
1004 * FALSE on failure
1007 BOOL WINAPI InternetCloseHandle(HINTERNET hInternet)
1009 LPWININETHANDLEHEADER lpwh;
1011 TRACE("%p\n",hInternet);
1013 lpwh = WININET_GetObject( hInternet );
1014 if (NULL == lpwh)
1016 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
1017 return FALSE;
1020 WININET_Release( lpwh );
1021 WININET_FreeHandle( hInternet );
1023 return TRUE;
1027 /***********************************************************************
1028 * ConvertUrlComponentValue (Internal)
1030 * Helper function for InternetCrackUrlW
1033 static void ConvertUrlComponentValue(LPSTR* lppszComponent, LPDWORD dwComponentLen,
1034 LPWSTR lpwszComponent, DWORD dwwComponentLen,
1035 LPCSTR lpszStart, LPCWSTR lpwszStart)
1037 TRACE("%p %d %p %d %p %p\n", lppszComponent, *dwComponentLen, lpwszComponent, dwwComponentLen, lpszStart, lpwszStart);
1038 if (*dwComponentLen != 0)
1040 DWORD nASCIILength=WideCharToMultiByte(CP_ACP,0,lpwszComponent,dwwComponentLen,NULL,0,NULL,NULL);
1041 if (*lppszComponent == NULL)
1043 int nASCIIOffset=WideCharToMultiByte(CP_ACP,0,lpwszStart,lpwszComponent-lpwszStart,NULL,0,NULL,NULL);
1044 if (lpwszComponent)
1045 *lppszComponent = (LPSTR)lpszStart+nASCIIOffset;
1046 else
1047 *lppszComponent = NULL;
1048 *dwComponentLen = nASCIILength;
1050 else
1052 DWORD ncpylen = min((*dwComponentLen)-1, nASCIILength);
1053 WideCharToMultiByte(CP_ACP,0,lpwszComponent,dwwComponentLen,*lppszComponent,ncpylen+1,NULL,NULL);
1054 (*lppszComponent)[ncpylen]=0;
1055 *dwComponentLen = ncpylen;
1061 /***********************************************************************
1062 * InternetCrackUrlA (WININET.@)
1064 * See InternetCrackUrlW.
1066 BOOL WINAPI InternetCrackUrlA(LPCSTR lpszUrl, DWORD dwUrlLength, DWORD dwFlags,
1067 LPURL_COMPONENTSA lpUrlComponents)
1069 DWORD nLength;
1070 URL_COMPONENTSW UCW;
1071 WCHAR* lpwszUrl;
1073 TRACE("(%s %u %x %p)\n", debugstr_a(lpszUrl), dwUrlLength, dwFlags, lpUrlComponents);
1075 if (!lpszUrl || !*lpszUrl || !lpUrlComponents ||
1076 lpUrlComponents->dwStructSize != sizeof(URL_COMPONENTSA))
1078 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1079 return FALSE;
1082 if(dwUrlLength<=0)
1083 dwUrlLength=-1;
1084 nLength=MultiByteToWideChar(CP_ACP,0,lpszUrl,dwUrlLength,NULL,0);
1086 /* if dwUrlLength=-1 then nLength includes null but length to
1087 InternetCrackUrlW should not include it */
1088 if (dwUrlLength == -1) nLength--;
1090 lpwszUrl=HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WCHAR)*nLength);
1091 MultiByteToWideChar(CP_ACP,0,lpszUrl,dwUrlLength,lpwszUrl,nLength);
1093 memset(&UCW,0,sizeof(UCW));
1094 UCW.dwStructSize = sizeof(URL_COMPONENTSW);
1095 if(lpUrlComponents->dwHostNameLength!=0)
1096 UCW.dwHostNameLength= lpUrlComponents->dwHostNameLength;
1097 if(lpUrlComponents->dwUserNameLength!=0)
1098 UCW.dwUserNameLength=lpUrlComponents->dwUserNameLength;
1099 if(lpUrlComponents->dwPasswordLength!=0)
1100 UCW.dwPasswordLength=lpUrlComponents->dwPasswordLength;
1101 if(lpUrlComponents->dwUrlPathLength!=0)
1102 UCW.dwUrlPathLength=lpUrlComponents->dwUrlPathLength;
1103 if(lpUrlComponents->dwSchemeLength!=0)
1104 UCW.dwSchemeLength=lpUrlComponents->dwSchemeLength;
1105 if(lpUrlComponents->dwExtraInfoLength!=0)
1106 UCW.dwExtraInfoLength=lpUrlComponents->dwExtraInfoLength;
1107 if(!InternetCrackUrlW(lpwszUrl,nLength,dwFlags,&UCW))
1109 HeapFree(GetProcessHeap(), 0, lpwszUrl);
1110 return FALSE;
1113 ConvertUrlComponentValue(&lpUrlComponents->lpszHostName, &lpUrlComponents->dwHostNameLength,
1114 UCW.lpszHostName, UCW.dwHostNameLength,
1115 lpszUrl, lpwszUrl);
1116 ConvertUrlComponentValue(&lpUrlComponents->lpszUserName, &lpUrlComponents->dwUserNameLength,
1117 UCW.lpszUserName, UCW.dwUserNameLength,
1118 lpszUrl, lpwszUrl);
1119 ConvertUrlComponentValue(&lpUrlComponents->lpszPassword, &lpUrlComponents->dwPasswordLength,
1120 UCW.lpszPassword, UCW.dwPasswordLength,
1121 lpszUrl, lpwszUrl);
1122 ConvertUrlComponentValue(&lpUrlComponents->lpszUrlPath, &lpUrlComponents->dwUrlPathLength,
1123 UCW.lpszUrlPath, UCW.dwUrlPathLength,
1124 lpszUrl, lpwszUrl);
1125 ConvertUrlComponentValue(&lpUrlComponents->lpszScheme, &lpUrlComponents->dwSchemeLength,
1126 UCW.lpszScheme, UCW.dwSchemeLength,
1127 lpszUrl, lpwszUrl);
1128 ConvertUrlComponentValue(&lpUrlComponents->lpszExtraInfo, &lpUrlComponents->dwExtraInfoLength,
1129 UCW.lpszExtraInfo, UCW.dwExtraInfoLength,
1130 lpszUrl, lpwszUrl);
1131 lpUrlComponents->nScheme=UCW.nScheme;
1132 lpUrlComponents->nPort=UCW.nPort;
1133 HeapFree(GetProcessHeap(), 0, lpwszUrl);
1135 TRACE("%s: scheme(%s) host(%s) path(%s) extra(%s)\n", lpszUrl,
1136 debugstr_an(lpUrlComponents->lpszScheme,lpUrlComponents->dwSchemeLength),
1137 debugstr_an(lpUrlComponents->lpszHostName,lpUrlComponents->dwHostNameLength),
1138 debugstr_an(lpUrlComponents->lpszUrlPath,lpUrlComponents->dwUrlPathLength),
1139 debugstr_an(lpUrlComponents->lpszExtraInfo,lpUrlComponents->dwExtraInfoLength));
1141 return TRUE;
1144 static const WCHAR url_schemes[][7] =
1146 {'f','t','p',0},
1147 {'g','o','p','h','e','r',0},
1148 {'h','t','t','p',0},
1149 {'h','t','t','p','s',0},
1150 {'f','i','l','e',0},
1151 {'n','e','w','s',0},
1152 {'m','a','i','l','t','o',0},
1153 {'r','e','s',0},
1156 /***********************************************************************
1157 * GetInternetSchemeW (internal)
1159 * Get scheme of url
1161 * RETURNS
1162 * scheme on success
1163 * INTERNET_SCHEME_UNKNOWN on failure
1166 static INTERNET_SCHEME GetInternetSchemeW(LPCWSTR lpszScheme, DWORD nMaxCmp)
1168 int i;
1170 TRACE("%s %d\n",debugstr_wn(lpszScheme, nMaxCmp), nMaxCmp);
1172 if(lpszScheme==NULL)
1173 return INTERNET_SCHEME_UNKNOWN;
1175 for (i = 0; i < sizeof(url_schemes)/sizeof(url_schemes[0]); i++)
1176 if (!strncmpW(lpszScheme, url_schemes[i], nMaxCmp))
1177 return INTERNET_SCHEME_FIRST + i;
1179 return INTERNET_SCHEME_UNKNOWN;
1182 /***********************************************************************
1183 * SetUrlComponentValueW (Internal)
1185 * Helper function for InternetCrackUrlW
1187 * PARAMS
1188 * lppszComponent [O] Holds the returned string
1189 * dwComponentLen [I] Holds the size of lppszComponent
1190 * [O] Holds the length of the string in lppszComponent without '\0'
1191 * lpszStart [I] Holds the string to copy from
1192 * len [I] Holds the length of lpszStart without '\0'
1194 * RETURNS
1195 * TRUE on success
1196 * FALSE on failure
1199 static BOOL SetUrlComponentValueW(LPWSTR* lppszComponent, LPDWORD dwComponentLen, LPCWSTR lpszStart, DWORD len)
1201 TRACE("%s (%d)\n", debugstr_wn(lpszStart,len), len);
1203 if ( (*dwComponentLen == 0) && (*lppszComponent == NULL) )
1204 return FALSE;
1206 if (*dwComponentLen != 0 || *lppszComponent == NULL)
1208 if (*lppszComponent == NULL)
1210 *lppszComponent = (LPWSTR)lpszStart;
1211 *dwComponentLen = len;
1213 else
1215 DWORD ncpylen = min((*dwComponentLen)-1, len);
1216 memcpy(*lppszComponent, lpszStart, ncpylen*sizeof(WCHAR));
1217 (*lppszComponent)[ncpylen] = '\0';
1218 *dwComponentLen = ncpylen;
1222 return TRUE;
1225 /***********************************************************************
1226 * InternetCrackUrlW (WININET.@)
1228 * Break up URL into its components
1230 * RETURNS
1231 * TRUE on success
1232 * FALSE on failure
1234 BOOL WINAPI InternetCrackUrlW(LPCWSTR lpszUrl_orig, DWORD dwUrlLength_orig, DWORD dwFlags,
1235 LPURL_COMPONENTSW lpUC)
1238 * RFC 1808
1239 * <protocol>:[//<net_loc>][/path][;<params>][?<query>][#<fragment>]
1242 LPCWSTR lpszParam = NULL;
1243 BOOL bIsAbsolute = FALSE;
1244 LPCWSTR lpszap, lpszUrl = lpszUrl_orig;
1245 LPCWSTR lpszcp = NULL;
1246 LPWSTR lpszUrl_decode = NULL;
1247 DWORD dwUrlLength = dwUrlLength_orig;
1248 const WCHAR lpszSeparators[3]={';','?',0};
1249 const WCHAR lpszSlash[2]={'/',0};
1251 TRACE("(%s %u %x %p)\n", debugstr_w(lpszUrl), dwUrlLength, dwFlags, lpUC);
1253 if (!lpszUrl_orig || !*lpszUrl_orig || !lpUC)
1255 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1256 return FALSE;
1258 if (!dwUrlLength) dwUrlLength = strlenW(lpszUrl);
1260 if (dwFlags & ICU_DECODE)
1262 lpszUrl_decode=HeapAlloc( GetProcessHeap(), 0, dwUrlLength * sizeof (WCHAR) );
1263 if( InternetCanonicalizeUrlW(lpszUrl_orig, lpszUrl_decode, &dwUrlLength, dwFlags))
1265 lpszUrl = lpszUrl_decode;
1268 lpszap = lpszUrl;
1270 /* Determine if the URI is absolute. */
1271 while (*lpszap != '\0')
1273 if (isalnumW(*lpszap))
1275 lpszap++;
1276 continue;
1278 if ((*lpszap == ':') && (lpszap - lpszUrl >= 2))
1280 bIsAbsolute = TRUE;
1281 lpszcp = lpszap;
1283 else
1285 lpszcp = lpszUrl; /* Relative url */
1288 break;
1291 lpUC->nScheme = INTERNET_SCHEME_UNKNOWN;
1292 lpUC->nPort = INTERNET_INVALID_PORT_NUMBER;
1294 /* Parse <params> */
1295 lpszParam = strpbrkW(lpszap, lpszSeparators);
1296 SetUrlComponentValueW(&lpUC->lpszExtraInfo, &lpUC->dwExtraInfoLength,
1297 lpszParam, lpszParam ? dwUrlLength-(lpszParam-lpszUrl) : 0);
1299 if (bIsAbsolute) /* Parse <protocol>:[//<net_loc>] */
1301 LPCWSTR lpszNetLoc;
1303 /* Get scheme first. */
1304 lpUC->nScheme = GetInternetSchemeW(lpszUrl, lpszcp - lpszUrl);
1305 SetUrlComponentValueW(&lpUC->lpszScheme, &lpUC->dwSchemeLength,
1306 lpszUrl, lpszcp - lpszUrl);
1308 /* Eat ':' in protocol. */
1309 lpszcp++;
1311 /* double slash indicates the net_loc portion is present */
1312 if ((lpszcp[0] == '/') && (lpszcp[1] == '/'))
1314 lpszcp += 2;
1316 lpszNetLoc = strpbrkW(lpszcp, lpszSlash);
1317 if (lpszParam)
1319 if (lpszNetLoc)
1320 lpszNetLoc = min(lpszNetLoc, lpszParam);
1321 else
1322 lpszNetLoc = lpszParam;
1324 else if (!lpszNetLoc)
1325 lpszNetLoc = lpszcp + dwUrlLength-(lpszcp-lpszUrl);
1327 /* Parse net-loc */
1328 if (lpszNetLoc)
1330 LPCWSTR lpszHost;
1331 LPCWSTR lpszPort;
1333 /* [<user>[<:password>]@]<host>[:<port>] */
1334 /* First find the user and password if they exist */
1336 lpszHost = strchrW(lpszcp, '@');
1337 if (lpszHost == NULL || lpszHost > lpszNetLoc)
1339 /* username and password not specified. */
1340 SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength, NULL, 0);
1341 SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength, NULL, 0);
1343 else /* Parse out username and password */
1345 LPCWSTR lpszUser = lpszcp;
1346 LPCWSTR lpszPasswd = lpszHost;
1348 while (lpszcp < lpszHost)
1350 if (*lpszcp == ':')
1351 lpszPasswd = lpszcp;
1353 lpszcp++;
1356 SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength,
1357 lpszUser, lpszPasswd - lpszUser);
1359 if (lpszPasswd != lpszHost)
1360 lpszPasswd++;
1361 SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength,
1362 lpszPasswd == lpszHost ? NULL : lpszPasswd,
1363 lpszHost - lpszPasswd);
1365 lpszcp++; /* Advance to beginning of host */
1368 /* Parse <host><:port> */
1370 lpszHost = lpszcp;
1371 lpszPort = lpszNetLoc;
1373 /* special case for res:// URLs: there is no port here, so the host is the
1374 entire string up to the first '/' */
1375 if(lpUC->nScheme==INTERNET_SCHEME_RES)
1377 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength,
1378 lpszHost, lpszPort - lpszHost);
1379 lpszcp=lpszNetLoc;
1381 else
1383 while (lpszcp < lpszNetLoc)
1385 if (*lpszcp == ':')
1386 lpszPort = lpszcp;
1388 lpszcp++;
1391 /* If the scheme is "file" and the host is just one letter, it's not a host */
1392 if(lpUC->nScheme==INTERNET_SCHEME_FILE && (lpszPort-lpszHost)==1)
1394 lpszcp=lpszHost;
1395 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength,
1396 NULL, 0);
1398 else
1400 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength,
1401 lpszHost, lpszPort - lpszHost);
1402 if (lpszPort != lpszNetLoc)
1403 lpUC->nPort = atoiW(++lpszPort);
1404 else switch (lpUC->nScheme)
1406 case INTERNET_SCHEME_HTTP:
1407 lpUC->nPort = INTERNET_DEFAULT_HTTP_PORT;
1408 break;
1409 case INTERNET_SCHEME_HTTPS:
1410 lpUC->nPort = INTERNET_DEFAULT_HTTPS_PORT;
1411 break;
1412 case INTERNET_SCHEME_FTP:
1413 lpUC->nPort = INTERNET_DEFAULT_FTP_PORT;
1414 break;
1415 case INTERNET_SCHEME_GOPHER:
1416 lpUC->nPort = INTERNET_DEFAULT_GOPHER_PORT;
1417 break;
1418 default:
1419 break;
1425 else
1427 SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength, NULL, 0);
1428 SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength, NULL, 0);
1429 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength, NULL, 0);
1432 else
1434 SetUrlComponentValueW(&lpUC->lpszScheme, &lpUC->dwSchemeLength, NULL, 0);
1435 SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength, NULL, 0);
1436 SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength, NULL, 0);
1437 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength, NULL, 0);
1440 /* Here lpszcp points to:
1442 * <protocol>:[//<net_loc>][/path][;<params>][?<query>][#<fragment>]
1443 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1445 if (lpszcp != 0 && *lpszcp != '\0' && (!lpszParam || lpszcp < lpszParam))
1447 INT len;
1449 /* Only truncate the parameter list if it's already been saved
1450 * in lpUC->lpszExtraInfo.
1452 if (lpszParam && lpUC->dwExtraInfoLength && lpUC->lpszExtraInfo)
1453 len = lpszParam - lpszcp;
1454 else
1456 /* Leave the parameter list in lpszUrlPath. Strip off any trailing
1457 * newlines if necessary.
1459 LPWSTR lpsznewline = strchrW(lpszcp, '\n');
1460 if (lpsznewline != NULL)
1461 len = lpsznewline - lpszcp;
1462 else
1463 len = dwUrlLength-(lpszcp-lpszUrl);
1465 SetUrlComponentValueW(&lpUC->lpszUrlPath, &lpUC->dwUrlPathLength,
1466 lpszcp, len);
1468 else
1470 lpUC->dwUrlPathLength = 0;
1473 TRACE("%s: scheme(%s) host(%s) path(%s) extra(%s)\n", debugstr_wn(lpszUrl,dwUrlLength),
1474 debugstr_wn(lpUC->lpszScheme,lpUC->dwSchemeLength),
1475 debugstr_wn(lpUC->lpszHostName,lpUC->dwHostNameLength),
1476 debugstr_wn(lpUC->lpszUrlPath,lpUC->dwUrlPathLength),
1477 debugstr_wn(lpUC->lpszExtraInfo,lpUC->dwExtraInfoLength));
1479 HeapFree(GetProcessHeap(), 0, lpszUrl_decode );
1480 return TRUE;
1483 /***********************************************************************
1484 * InternetAttemptConnect (WININET.@)
1486 * Attempt to make a connection to the internet
1488 * RETURNS
1489 * ERROR_SUCCESS on success
1490 * Error value on failure
1493 DWORD WINAPI InternetAttemptConnect(DWORD dwReserved)
1495 FIXME("Stub\n");
1496 return ERROR_SUCCESS;
1500 /***********************************************************************
1501 * InternetCanonicalizeUrlA (WININET.@)
1503 * Escape unsafe characters and spaces
1505 * RETURNS
1506 * TRUE on success
1507 * FALSE on failure
1510 BOOL WINAPI InternetCanonicalizeUrlA(LPCSTR lpszUrl, LPSTR lpszBuffer,
1511 LPDWORD lpdwBufferLength, DWORD dwFlags)
1513 HRESULT hr;
1514 DWORD dwURLFlags = URL_WININET_COMPATIBILITY | URL_ESCAPE_UNSAFE;
1516 TRACE("(%s, %p, %p, 0x%08x) bufferlength: %d\n", debugstr_a(lpszUrl), lpszBuffer,
1517 lpdwBufferLength, lpdwBufferLength ? *lpdwBufferLength : -1, dwFlags);
1519 if(dwFlags & ICU_DECODE)
1521 dwURLFlags |= URL_UNESCAPE;
1522 dwFlags &= ~ICU_DECODE;
1525 if(dwFlags & ICU_ESCAPE)
1527 dwURLFlags |= URL_UNESCAPE;
1528 dwFlags &= ~ICU_ESCAPE;
1531 if(dwFlags & ICU_BROWSER_MODE)
1533 dwURLFlags |= URL_BROWSER_MODE;
1534 dwFlags &= ~ICU_BROWSER_MODE;
1537 if(dwFlags & ICU_NO_ENCODE)
1539 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
1540 dwURLFlags ^= URL_ESCAPE_UNSAFE;
1541 dwFlags &= ~ICU_NO_ENCODE;
1544 if (dwFlags) FIXME("Unhandled flags 0x%08x\n", dwFlags);
1546 hr = UrlCanonicalizeA(lpszUrl, lpszBuffer, lpdwBufferLength, dwURLFlags);
1547 if (hr == E_POINTER) SetLastError(ERROR_INSUFFICIENT_BUFFER);
1548 if (hr == E_INVALIDARG) SetLastError(ERROR_INVALID_PARAMETER);
1550 return (hr == S_OK) ? TRUE : FALSE;
1553 /***********************************************************************
1554 * InternetCanonicalizeUrlW (WININET.@)
1556 * Escape unsafe characters and spaces
1558 * RETURNS
1559 * TRUE on success
1560 * FALSE on failure
1563 BOOL WINAPI InternetCanonicalizeUrlW(LPCWSTR lpszUrl, LPWSTR lpszBuffer,
1564 LPDWORD lpdwBufferLength, DWORD dwFlags)
1566 HRESULT hr;
1567 DWORD dwURLFlags = URL_WININET_COMPATIBILITY | URL_ESCAPE_UNSAFE;
1569 TRACE("(%s, %p, %p, 0x%08x) bufferlength: %d\n", debugstr_w(lpszUrl), lpszBuffer,
1570 lpdwBufferLength, lpdwBufferLength ? *lpdwBufferLength : -1, dwFlags);
1572 if(dwFlags & ICU_DECODE)
1574 dwURLFlags |= URL_UNESCAPE;
1575 dwFlags &= ~ICU_DECODE;
1578 if(dwFlags & ICU_ESCAPE)
1580 dwURLFlags |= URL_UNESCAPE;
1581 dwFlags &= ~ICU_ESCAPE;
1584 if(dwFlags & ICU_BROWSER_MODE)
1586 dwURLFlags |= URL_BROWSER_MODE;
1587 dwFlags &= ~ICU_BROWSER_MODE;
1590 if(dwFlags & ICU_NO_ENCODE)
1592 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
1593 dwURLFlags ^= URL_ESCAPE_UNSAFE;
1594 dwFlags &= ~ICU_NO_ENCODE;
1597 if (dwFlags) FIXME("Unhandled flags 0x%08x\n", dwFlags);
1599 hr = UrlCanonicalizeW(lpszUrl, lpszBuffer, lpdwBufferLength, dwURLFlags);
1600 if (hr == E_POINTER) SetLastError(ERROR_INSUFFICIENT_BUFFER);
1601 if (hr == E_INVALIDARG) SetLastError(ERROR_INVALID_PARAMETER);
1603 return (hr == S_OK) ? TRUE : FALSE;
1606 /* #################################################### */
1608 static INTERNET_STATUS_CALLBACK set_status_callback(
1609 LPWININETHANDLEHEADER lpwh, INTERNET_STATUS_CALLBACK callback, BOOL unicode)
1611 INTERNET_STATUS_CALLBACK ret;
1613 if (unicode) lpwh->dwInternalFlags |= INET_CALLBACKW;
1614 else lpwh->dwInternalFlags &= ~INET_CALLBACKW;
1616 ret = lpwh->lpfnStatusCB;
1617 lpwh->lpfnStatusCB = callback;
1619 return ret;
1622 /***********************************************************************
1623 * InternetSetStatusCallbackA (WININET.@)
1625 * Sets up a callback function which is called as progress is made
1626 * during an operation.
1628 * RETURNS
1629 * Previous callback or NULL on success
1630 * INTERNET_INVALID_STATUS_CALLBACK on failure
1633 INTERNET_STATUS_CALLBACK WINAPI InternetSetStatusCallbackA(
1634 HINTERNET hInternet ,INTERNET_STATUS_CALLBACK lpfnIntCB)
1636 INTERNET_STATUS_CALLBACK retVal;
1637 LPWININETHANDLEHEADER lpwh;
1639 TRACE("0x%08x\n", (ULONG)hInternet);
1641 if (!(lpwh = WININET_GetObject(hInternet)))
1642 return INTERNET_INVALID_STATUS_CALLBACK;
1644 retVal = set_status_callback(lpwh, lpfnIntCB, FALSE);
1646 WININET_Release( lpwh );
1647 return retVal;
1650 /***********************************************************************
1651 * InternetSetStatusCallbackW (WININET.@)
1653 * Sets up a callback function which is called as progress is made
1654 * during an operation.
1656 * RETURNS
1657 * Previous callback or NULL on success
1658 * INTERNET_INVALID_STATUS_CALLBACK on failure
1661 INTERNET_STATUS_CALLBACK WINAPI InternetSetStatusCallbackW(
1662 HINTERNET hInternet ,INTERNET_STATUS_CALLBACK lpfnIntCB)
1664 INTERNET_STATUS_CALLBACK retVal;
1665 LPWININETHANDLEHEADER lpwh;
1667 TRACE("0x%08x\n", (ULONG)hInternet);
1669 if (!(lpwh = WININET_GetObject(hInternet)))
1670 return INTERNET_INVALID_STATUS_CALLBACK;
1672 retVal = set_status_callback(lpwh, lpfnIntCB, TRUE);
1674 WININET_Release( lpwh );
1675 return retVal;
1678 /***********************************************************************
1679 * InternetSetFilePointer (WININET.@)
1681 DWORD WINAPI InternetSetFilePointer(HINTERNET hFile, LONG lDistanceToMove,
1682 PVOID pReserved, DWORD dwMoveContext, DWORD_PTR dwContext)
1684 FIXME("stub\n");
1685 return FALSE;
1688 /***********************************************************************
1689 * InternetWriteFile (WININET.@)
1691 * Write data to an open internet file
1693 * RETURNS
1694 * TRUE on success
1695 * FALSE on failure
1698 BOOL WINAPI InternetWriteFile(HINTERNET hFile, LPCVOID lpBuffer,
1699 DWORD dwNumOfBytesToWrite, LPDWORD lpdwNumOfBytesWritten)
1701 LPWININETHANDLEHEADER lpwh;
1702 BOOL retval = FALSE;
1704 TRACE("(%p %p %d %p)\n", hFile, lpBuffer, dwNumOfBytesToWrite, lpdwNumOfBytesWritten);
1706 lpwh = WININET_GetObject( hFile );
1707 if (!lpwh) {
1708 WARN("Invalid handle\n");
1709 SetLastError(ERROR_INVALID_HANDLE);
1710 return FALSE;
1713 if(lpwh->vtbl->WriteFile) {
1714 retval = lpwh->vtbl->WriteFile(lpwh, lpBuffer, dwNumOfBytesToWrite, lpdwNumOfBytesWritten);
1715 }else {
1716 WARN("No Writefile method.\n");
1717 SetLastError(ERROR_INVALID_HANDLE);
1718 retval = FALSE;
1721 WININET_Release( lpwh );
1723 return retval;
1727 /***********************************************************************
1728 * InternetReadFile (WININET.@)
1730 * Read data from an open internet file
1732 * RETURNS
1733 * TRUE on success
1734 * FALSE on failure
1737 BOOL WINAPI InternetReadFile(HINTERNET hFile, LPVOID lpBuffer,
1738 DWORD dwNumOfBytesToRead, LPDWORD pdwNumOfBytesRead)
1740 LPWININETHANDLEHEADER hdr;
1741 DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
1743 TRACE("%p %p %d %p\n", hFile, lpBuffer, dwNumOfBytesToRead, pdwNumOfBytesRead);
1745 hdr = WININET_GetObject(hFile);
1746 if (!hdr) {
1747 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
1748 return FALSE;
1751 if(hdr->vtbl->ReadFile)
1752 res = hdr->vtbl->ReadFile(hdr, lpBuffer, dwNumOfBytesToRead, pdwNumOfBytesRead);
1754 WININET_Release(hdr);
1756 TRACE("-- %s (%u) (bytes read: %d)\n", res == ERROR_SUCCESS ? "TRUE": "FALSE", res,
1757 pdwNumOfBytesRead ? *pdwNumOfBytesRead : -1);
1759 if(res != ERROR_SUCCESS)
1760 SetLastError(res);
1761 return res == ERROR_SUCCESS;
1764 /***********************************************************************
1765 * InternetReadFileExA (WININET.@)
1767 * Read data from an open internet file
1769 * PARAMS
1770 * hFile [I] Handle returned by InternetOpenUrl or HttpOpenRequest.
1771 * lpBuffersOut [I/O] Buffer.
1772 * dwFlags [I] Flags. See notes.
1773 * dwContext [I] Context for callbacks.
1775 * RETURNS
1776 * TRUE on success
1777 * FALSE on failure
1779 * NOTES
1780 * The parameter dwFlags include zero or more of the following flags:
1781 *|IRF_ASYNC - Makes the call asynchronous.
1782 *|IRF_SYNC - Makes the call synchronous.
1783 *|IRF_USE_CONTEXT - Forces dwContext to be used.
1784 *|IRF_NO_WAIT - Don't block if the data is not available, just return what is available.
1786 * However, in testing IRF_USE_CONTEXT seems to have no effect - dwContext isn't used.
1788 * SEE
1789 * InternetOpenUrlA(), HttpOpenRequestA()
1791 BOOL WINAPI InternetReadFileExA(HINTERNET hFile, LPINTERNET_BUFFERSA lpBuffersOut,
1792 DWORD dwFlags, DWORD_PTR dwContext)
1794 LPWININETHANDLEHEADER hdr;
1795 DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
1797 TRACE("(%p %p 0x%x 0x%lx)\n", hFile, lpBuffersOut, dwFlags, dwContext);
1799 hdr = WININET_GetObject(hFile);
1800 if (!hdr) {
1801 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
1802 return FALSE;
1805 if(hdr->vtbl->ReadFileExA)
1806 res = hdr->vtbl->ReadFileExA(hdr, lpBuffersOut, dwFlags, dwContext);
1808 WININET_Release(hdr);
1810 TRACE("-- %s (%u, bytes read: %d)\n", res == ERROR_SUCCESS ? "TRUE": "FALSE",
1811 res, lpBuffersOut->dwBufferLength);
1813 if(res != ERROR_SUCCESS)
1814 SetLastError(res);
1815 return res == ERROR_SUCCESS;
1818 /***********************************************************************
1819 * InternetReadFileExW (WININET.@)
1821 * Read data from an open internet file.
1823 * PARAMS
1824 * hFile [I] Handle returned by InternetOpenUrl() or HttpOpenRequest().
1825 * lpBuffersOut [I/O] Buffer.
1826 * dwFlags [I] Flags.
1827 * dwContext [I] Context for callbacks.
1829 * RETURNS
1830 * FALSE, last error is set to ERROR_CALL_NOT_IMPLEMENTED
1832 * NOTES
1833 * Not implemented in Wine or native either (as of IE6 SP2).
1836 BOOL WINAPI InternetReadFileExW(HINTERNET hFile, LPINTERNET_BUFFERSW lpBuffer,
1837 DWORD dwFlags, DWORD_PTR dwContext)
1839 ERR("(%p, %p, 0x%x, 0x%lx): not implemented in native\n", hFile, lpBuffer, dwFlags, dwContext);
1841 INTERNET_SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1842 return FALSE;
1845 /***********************************************************************
1846 * INET_QueryOptionHelper (internal)
1848 static BOOL INET_QueryOptionHelper(BOOL bIsUnicode, HINTERNET hInternet, DWORD dwOption,
1849 LPVOID lpBuffer, LPDWORD lpdwBufferLength)
1851 LPWININETHANDLEHEADER lpwhh;
1852 BOOL bSuccess = FALSE;
1854 TRACE("(%p, 0x%08x, %p, %p)\n", hInternet, dwOption, lpBuffer, lpdwBufferLength);
1856 lpwhh = WININET_GetObject( hInternet );
1858 switch (dwOption)
1860 case INTERNET_OPTION_REQUEST_FLAGS:
1862 ULONG flags = 4;
1863 TRACE("INTERNET_OPTION_REQUEST_FLAGS: %d\n", flags);
1864 if (*lpdwBufferLength < sizeof(ULONG))
1865 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1866 else
1868 memcpy(lpBuffer, &flags, sizeof(ULONG));
1869 bSuccess = TRUE;
1871 *lpdwBufferLength = sizeof(ULONG);
1872 break;
1875 case INTERNET_OPTION_USER_AGENT:
1876 FIXME("INTERNET_OPTION_USER_AGENT\n");
1877 break;
1879 case INTERNET_OPTION_HTTP_VERSION:
1881 if (*lpdwBufferLength < sizeof(HTTP_VERSION_INFO))
1882 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1883 else
1886 * Presently hardcoded to 1.1
1888 ((HTTP_VERSION_INFO*)lpBuffer)->dwMajorVersion = 1;
1889 ((HTTP_VERSION_INFO*)lpBuffer)->dwMinorVersion = 1;
1890 bSuccess = TRUE;
1892 *lpdwBufferLength = sizeof(HTTP_VERSION_INFO);
1893 break;
1895 case INTERNET_OPTION_CONNECTED_STATE:
1897 DWORD *pdwConnectedState = (DWORD *)lpBuffer;
1898 FIXME("INTERNET_OPTION_CONNECTED_STATE: semi-stub\n");
1900 if (*lpdwBufferLength < sizeof(*pdwConnectedState))
1901 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1902 else
1904 *pdwConnectedState = INTERNET_STATE_CONNECTED;
1905 bSuccess = TRUE;
1907 *lpdwBufferLength = sizeof(*pdwConnectedState);
1908 break;
1910 case INTERNET_OPTION_PROXY:
1912 LPWININETAPPINFOW lpwai = (LPWININETAPPINFOW)lpwhh;
1913 WININETAPPINFOW wai;
1915 if (lpwai == NULL)
1917 TRACE("Getting global proxy info\n");
1918 memset(&wai, 0, sizeof(WININETAPPINFOW));
1919 INTERNET_ConfigureProxyFromReg( &wai );
1920 lpwai = &wai;
1923 if (bIsUnicode)
1925 INTERNET_PROXY_INFOW *pPI = (INTERNET_PROXY_INFOW *)lpBuffer;
1926 DWORD proxyBytesRequired = 0, proxyBypassBytesRequired = 0;
1928 if (lpwai->lpszProxy)
1929 proxyBytesRequired = (lstrlenW(lpwai->lpszProxy) + 1) *
1930 sizeof(WCHAR);
1931 if (lpwai->lpszProxyBypass)
1932 proxyBypassBytesRequired =
1933 (lstrlenW(lpwai->lpszProxyBypass) + 1) * sizeof(WCHAR);
1934 if (*lpdwBufferLength < sizeof(INTERNET_PROXY_INFOW) +
1935 proxyBytesRequired + proxyBypassBytesRequired)
1936 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1937 else
1939 LPWSTR proxy = (LPWSTR)((LPBYTE)lpBuffer +
1940 sizeof(INTERNET_PROXY_INFOW));
1941 LPWSTR proxy_bypass = (LPWSTR)((LPBYTE)lpBuffer +
1942 sizeof(INTERNET_PROXY_INFOW) +
1943 proxyBytesRequired);
1945 pPI->dwAccessType = lpwai->dwAccessType;
1946 pPI->lpszProxy = NULL;
1947 pPI->lpszProxyBypass = NULL;
1948 if (lpwai->lpszProxy)
1950 lstrcpyW(proxy, lpwai->lpszProxy);
1951 pPI->lpszProxy = proxy;
1954 if (lpwai->lpszProxyBypass)
1956 lstrcpyW(proxy_bypass, lpwai->lpszProxyBypass);
1957 pPI->lpszProxyBypass = proxy_bypass;
1959 bSuccess = TRUE;
1961 *lpdwBufferLength = sizeof(INTERNET_PROXY_INFOW) +
1962 proxyBytesRequired + proxyBypassBytesRequired;
1964 else
1966 INTERNET_PROXY_INFOA *pPI = (INTERNET_PROXY_INFOA *)lpBuffer;
1967 DWORD proxyBytesRequired = 0, proxyBypassBytesRequired = 0;
1969 if (lpwai->lpszProxy)
1970 proxyBytesRequired = WideCharToMultiByte(CP_ACP, 0,
1971 lpwai->lpszProxy, -1, NULL, 0, NULL, NULL);
1972 if (lpwai->lpszProxyBypass)
1973 proxyBypassBytesRequired = WideCharToMultiByte(CP_ACP, 0,
1974 lpwai->lpszProxyBypass, -1, NULL, 0, NULL, NULL);
1975 if (*lpdwBufferLength < sizeof(INTERNET_PROXY_INFOA) +
1976 proxyBytesRequired + proxyBypassBytesRequired)
1977 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1978 else
1980 LPSTR proxy = (LPSTR)((LPBYTE)lpBuffer +
1981 sizeof(INTERNET_PROXY_INFOA));
1982 LPSTR proxy_bypass = (LPSTR)((LPBYTE)lpBuffer +
1983 sizeof(INTERNET_PROXY_INFOA) +
1984 proxyBytesRequired);
1986 pPI->dwAccessType = lpwai->dwAccessType;
1987 pPI->lpszProxy = NULL;
1988 pPI->lpszProxyBypass = NULL;
1989 if (lpwai->lpszProxy)
1991 WideCharToMultiByte(CP_ACP, 0, lpwai->lpszProxy, -1,
1992 proxy, proxyBytesRequired, NULL, NULL);
1993 pPI->lpszProxy = proxy;
1996 if (lpwai->lpszProxyBypass)
1998 WideCharToMultiByte(CP_ACP, 0, lpwai->lpszProxyBypass,
1999 -1, proxy_bypass, proxyBypassBytesRequired,
2000 NULL, NULL);
2001 pPI->lpszProxyBypass = proxy_bypass;
2003 bSuccess = TRUE;
2005 *lpdwBufferLength = sizeof(INTERNET_PROXY_INFOA) +
2006 proxyBytesRequired + proxyBypassBytesRequired;
2008 break;
2010 case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2012 ULONG conn = 2;
2013 TRACE("INTERNET_OPTION_MAX_CONNS_PER_SERVER: %d\n", conn);
2014 if (*lpdwBufferLength < sizeof(ULONG))
2015 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2016 else
2018 memcpy(lpBuffer, &conn, sizeof(ULONG));
2019 bSuccess = TRUE;
2021 *lpdwBufferLength = sizeof(ULONG);
2022 break;
2024 case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2026 ULONG conn = 4;
2027 TRACE("INTERNET_OPTION_MAX_CONNS_1_0_SERVER: %d\n", conn);
2028 if (*lpdwBufferLength < sizeof(ULONG))
2029 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2030 else
2032 memcpy(lpBuffer, &conn, sizeof(ULONG));
2033 bSuccess = TRUE;
2035 *lpdwBufferLength = sizeof(ULONG);
2036 break;
2038 case INTERNET_OPTION_SECURITY_FLAGS:
2039 FIXME("INTERNET_OPTION_SECURITY_FLAGS: Stub\n");
2040 bSuccess = TRUE;
2041 break;
2043 case INTERNET_OPTION_VERSION:
2045 TRACE("INTERNET_OPTION_VERSION\n");
2046 if (*lpdwBufferLength < sizeof(INTERNET_VERSION_INFO))
2047 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2048 else
2050 static const INTERNET_VERSION_INFO info = { 1, 2 };
2051 memcpy(lpBuffer, &info, sizeof(info));
2052 *lpdwBufferLength = sizeof(info);
2053 bSuccess = TRUE;
2055 break;
2057 case INTERNET_OPTION_PER_CONNECTION_OPTION:
2058 FIXME("INTERNET_OPTION_PER_CONNECTION_OPTION stub\n");
2059 if (*lpdwBufferLength < sizeof(INTERNET_PER_CONN_OPTION_LISTW))
2060 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2061 else
2063 INTERNET_PER_CONN_OPTION_LISTW *con = lpBuffer;
2064 int x;
2065 bSuccess = TRUE;
2066 for (x = 0; x < con->dwOptionCount; ++x)
2068 INTERNET_PER_CONN_OPTIONW *option = con->pOptions + x;
2069 switch (option->dwOption)
2071 case INTERNET_PER_CONN_FLAGS:
2072 option->Value.dwValue = PROXY_TYPE_DIRECT;
2073 break;
2075 case INTERNET_PER_CONN_PROXY_SERVER:
2076 case INTERNET_PER_CONN_PROXY_BYPASS:
2077 case INTERNET_PER_CONN_AUTOCONFIG_URL:
2078 case INTERNET_PER_CONN_AUTODISCOVERY_FLAGS:
2079 case INTERNET_PER_CONN_AUTOCONFIG_SECONDARY_URL:
2080 case INTERNET_PER_CONN_AUTOCONFIG_RELOAD_DELAY_MINS:
2081 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_TIME:
2082 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_URL:
2083 FIXME("Unhandled dwOption %d\n", option->dwOption);
2084 option->Value.dwValue = 0;
2085 bSuccess = FALSE;
2086 break;
2088 default:
2089 FIXME("Unknown dwOption %d\n", option->dwOption);
2090 bSuccess = FALSE;
2091 break;
2094 if (!bSuccess)
2095 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
2097 break;
2098 case 66:
2099 FIXME("66\n");
2100 bSuccess = TRUE;
2101 break;
2102 default: {
2103 if(lpwhh) {
2104 DWORD res;
2106 res = lpwhh->vtbl->QueryOption(lpwhh, dwOption, lpBuffer, lpdwBufferLength, bIsUnicode);
2107 if(res == ERROR_SUCCESS)
2108 bSuccess = TRUE;
2109 else
2110 SetLastError(res);
2111 }else {
2112 FIXME("Stub! %d\n", dwOption);
2113 break;
2117 if (lpwhh)
2118 WININET_Release( lpwhh );
2120 return bSuccess;
2123 /***********************************************************************
2124 * InternetQueryOptionW (WININET.@)
2126 * Queries an options on the specified handle
2128 * RETURNS
2129 * TRUE on success
2130 * FALSE on failure
2133 BOOL WINAPI InternetQueryOptionW(HINTERNET hInternet, DWORD dwOption,
2134 LPVOID lpBuffer, LPDWORD lpdwBufferLength)
2136 return INET_QueryOptionHelper(TRUE, hInternet, dwOption, lpBuffer, lpdwBufferLength);
2139 /***********************************************************************
2140 * InternetQueryOptionA (WININET.@)
2142 * Queries an options on the specified handle
2144 * RETURNS
2145 * TRUE on success
2146 * FALSE on failure
2149 BOOL WINAPI InternetQueryOptionA(HINTERNET hInternet, DWORD dwOption,
2150 LPVOID lpBuffer, LPDWORD lpdwBufferLength)
2152 return INET_QueryOptionHelper(FALSE, hInternet, dwOption, lpBuffer, lpdwBufferLength);
2156 /***********************************************************************
2157 * InternetSetOptionW (WININET.@)
2159 * Sets an options on the specified handle
2161 * RETURNS
2162 * TRUE on success
2163 * FALSE on failure
2166 BOOL WINAPI InternetSetOptionW(HINTERNET hInternet, DWORD dwOption,
2167 LPVOID lpBuffer, DWORD dwBufferLength)
2169 LPWININETHANDLEHEADER lpwhh;
2170 BOOL ret = TRUE;
2172 TRACE("(%p %d %p %d)\n", hInternet, dwOption, lpBuffer, dwBufferLength);
2174 lpwhh = (LPWININETHANDLEHEADER) WININET_GetObject( hInternet );
2175 if(lpwhh && lpwhh->vtbl->SetOption) {
2176 DWORD res;
2178 res = lpwhh->vtbl->SetOption(lpwhh, dwOption, lpBuffer, dwBufferLength);
2179 if(res != ERROR_INTERNET_INVALID_OPTION) {
2180 WININET_Release( lpwhh );
2182 if(res != ERROR_SUCCESS)
2183 SetLastError(res);
2185 return res == ERROR_SUCCESS;
2189 switch (dwOption)
2191 case INTERNET_OPTION_CALLBACK:
2193 INTERNET_STATUS_CALLBACK callback = *(INTERNET_STATUS_CALLBACK *)lpBuffer;
2194 ret = (set_status_callback(lpwhh, callback, TRUE) != INTERNET_INVALID_STATUS_CALLBACK);
2195 break;
2197 case INTERNET_OPTION_HTTP_VERSION:
2199 HTTP_VERSION_INFO* pVersion=(HTTP_VERSION_INFO*)lpBuffer;
2200 FIXME("Option INTERNET_OPTION_HTTP_VERSION(%d,%d): STUB\n",pVersion->dwMajorVersion,pVersion->dwMinorVersion);
2202 break;
2203 case INTERNET_OPTION_ERROR_MASK:
2205 unsigned long flags=*(unsigned long*)lpBuffer;
2206 FIXME("Option INTERNET_OPTION_ERROR_MASK(%ld): STUB\n",flags);
2208 break;
2209 case INTERNET_OPTION_CODEPAGE:
2211 unsigned long codepage=*(unsigned long*)lpBuffer;
2212 FIXME("Option INTERNET_OPTION_CODEPAGE (%ld): STUB\n",codepage);
2214 break;
2215 case INTERNET_OPTION_REQUEST_PRIORITY:
2217 unsigned long priority=*(unsigned long*)lpBuffer;
2218 FIXME("Option INTERNET_OPTION_REQUEST_PRIORITY (%ld): STUB\n",priority);
2220 break;
2221 case INTERNET_OPTION_CONNECT_TIMEOUT:
2223 unsigned long connecttimeout=*(unsigned long*)lpBuffer;
2224 FIXME("Option INTERNET_OPTION_CONNECT_TIMEOUT (%ld): STUB\n",connecttimeout);
2226 break;
2227 case INTERNET_OPTION_DATA_RECEIVE_TIMEOUT:
2229 unsigned long receivetimeout=*(unsigned long*)lpBuffer;
2230 FIXME("Option INTERNET_OPTION_DATA_RECEIVE_TIMEOUT (%ld): STUB\n",receivetimeout);
2232 break;
2233 case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2235 unsigned long conns=*(unsigned long*)lpBuffer;
2236 FIXME("Option INTERNET_OPTION_MAX_CONNS_PER_SERVER (%ld): STUB\n",conns);
2238 break;
2239 case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2241 unsigned long conns=*(unsigned long*)lpBuffer;
2242 FIXME("Option INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER (%ld): STUB\n",conns);
2244 break;
2245 case INTERNET_OPTION_RESET_URLCACHE_SESSION:
2246 FIXME("Option INTERNET_OPTION_RESET_URLCACHE_SESSION: STUB\n");
2247 break;
2248 case INTERNET_OPTION_END_BROWSER_SESSION:
2249 FIXME("Option INTERNET_OPTION_END_BROWSER_SESSION: STUB\n");
2250 break;
2251 case INTERNET_OPTION_CONNECTED_STATE:
2252 FIXME("Option INTERNET_OPTION_CONNECTED_STATE: STUB\n");
2253 break;
2254 case INTERNET_OPTION_DISABLE_PASSPORT_AUTH:
2255 TRACE("Option INTERNET_OPTION_DISABLE_PASSPORT_AUTH: harmless stub, since not enabled\n");
2256 break;
2257 case INTERNET_OPTION_SEND_TIMEOUT:
2258 case INTERNET_OPTION_RECEIVE_TIMEOUT:
2259 FIXME("INTERNET_OPTION_SEND/RECEIVE_TIMEOUT\n");
2260 break;
2261 case INTERNET_OPTION_CONNECT_RETRIES:
2262 FIXME("Option INTERNET_OPTION_CONNECT_RETRIES: STUB\n");
2263 break;
2264 case INTERNET_OPTION_CONTEXT_VALUE:
2265 FIXME("Option INTERNET_OPTION_CONTEXT_VALUE; STUB\n");
2266 break;
2267 case INTERNET_OPTION_SECURITY_FLAGS:
2268 FIXME("Option INTERNET_OPTION_SECURITY_FLAGS; STUB\n");
2269 break;
2270 case 86:
2271 FIXME("86\n");
2272 break;
2273 default:
2274 FIXME("Option %d STUB\n",dwOption);
2275 INTERNET_SetLastError(ERROR_INTERNET_INVALID_OPTION);
2276 ret = FALSE;
2277 break;
2280 if(lpwhh)
2281 WININET_Release( lpwhh );
2283 return ret;
2287 /***********************************************************************
2288 * InternetSetOptionA (WININET.@)
2290 * Sets an options on the specified handle.
2292 * RETURNS
2293 * TRUE on success
2294 * FALSE on failure
2297 BOOL WINAPI InternetSetOptionA(HINTERNET hInternet, DWORD dwOption,
2298 LPVOID lpBuffer, DWORD dwBufferLength)
2300 LPVOID wbuffer;
2301 DWORD wlen;
2302 BOOL r;
2304 switch( dwOption )
2306 case INTERNET_OPTION_CALLBACK:
2308 LPWININETHANDLEHEADER lpwh;
2309 INTERNET_STATUS_CALLBACK callback = *(INTERNET_STATUS_CALLBACK *)lpBuffer;
2311 if (!(lpwh = WININET_GetObject(hInternet))) return FALSE;
2312 r = (set_status_callback(lpwh, callback, FALSE) != INTERNET_INVALID_STATUS_CALLBACK);
2313 WININET_Release(lpwh);
2314 return r;
2316 case INTERNET_OPTION_PROXY:
2318 LPINTERNET_PROXY_INFOA pi = (LPINTERNET_PROXY_INFOA) lpBuffer;
2319 LPINTERNET_PROXY_INFOW piw;
2320 DWORD proxlen, prbylen;
2321 LPWSTR prox, prby;
2323 proxlen = MultiByteToWideChar( CP_ACP, 0, pi->lpszProxy, -1, NULL, 0);
2324 prbylen= MultiByteToWideChar( CP_ACP, 0, pi->lpszProxyBypass, -1, NULL, 0);
2325 wlen = sizeof(*piw) + proxlen + prbylen;
2326 wbuffer = HeapAlloc( GetProcessHeap(), 0, wlen*sizeof(WCHAR) );
2327 piw = (LPINTERNET_PROXY_INFOW) wbuffer;
2328 piw->dwAccessType = pi->dwAccessType;
2329 prox = (LPWSTR) &piw[1];
2330 prby = &prox[proxlen+1];
2331 MultiByteToWideChar( CP_ACP, 0, pi->lpszProxy, -1, prox, proxlen);
2332 MultiByteToWideChar( CP_ACP, 0, pi->lpszProxyBypass, -1, prby, prbylen);
2333 piw->lpszProxy = prox;
2334 piw->lpszProxyBypass = prby;
2336 break;
2337 case INTERNET_OPTION_USER_AGENT:
2338 case INTERNET_OPTION_USERNAME:
2339 case INTERNET_OPTION_PASSWORD:
2340 wlen = MultiByteToWideChar( CP_ACP, 0, lpBuffer, dwBufferLength,
2341 NULL, 0 );
2342 wbuffer = HeapAlloc( GetProcessHeap(), 0, wlen*sizeof(WCHAR) );
2343 MultiByteToWideChar( CP_ACP, 0, lpBuffer, dwBufferLength,
2344 wbuffer, wlen );
2345 break;
2346 default:
2347 wbuffer = lpBuffer;
2348 wlen = dwBufferLength;
2351 r = InternetSetOptionW(hInternet,dwOption, wbuffer, wlen);
2353 if( lpBuffer != wbuffer )
2354 HeapFree( GetProcessHeap(), 0, wbuffer );
2356 return r;
2360 /***********************************************************************
2361 * InternetSetOptionExA (WININET.@)
2363 BOOL WINAPI InternetSetOptionExA(HINTERNET hInternet, DWORD dwOption,
2364 LPVOID lpBuffer, DWORD dwBufferLength, DWORD dwFlags)
2366 FIXME("Flags %08x ignored\n", dwFlags);
2367 return InternetSetOptionA( hInternet, dwOption, lpBuffer, dwBufferLength );
2370 /***********************************************************************
2371 * InternetSetOptionExW (WININET.@)
2373 BOOL WINAPI InternetSetOptionExW(HINTERNET hInternet, DWORD dwOption,
2374 LPVOID lpBuffer, DWORD dwBufferLength, DWORD dwFlags)
2376 FIXME("Flags %08x ignored\n", dwFlags);
2377 if( dwFlags & ~ISO_VALID_FLAGS )
2379 INTERNET_SetLastError( ERROR_INVALID_PARAMETER );
2380 return FALSE;
2382 return InternetSetOptionW( hInternet, dwOption, lpBuffer, dwBufferLength );
2385 static const WCHAR WININET_wkday[7][4] =
2386 { { 'S','u','n', 0 }, { 'M','o','n', 0 }, { 'T','u','e', 0 }, { 'W','e','d', 0 },
2387 { 'T','h','u', 0 }, { 'F','r','i', 0 }, { 'S','a','t', 0 } };
2388 static const WCHAR WININET_month[12][4] =
2389 { { 'J','a','n', 0 }, { 'F','e','b', 0 }, { 'M','a','r', 0 }, { 'A','p','r', 0 },
2390 { 'M','a','y', 0 }, { 'J','u','n', 0 }, { 'J','u','l', 0 }, { 'A','u','g', 0 },
2391 { 'S','e','p', 0 }, { 'O','c','t', 0 }, { 'N','o','v', 0 }, { 'D','e','c', 0 } };
2393 /***********************************************************************
2394 * InternetTimeFromSystemTimeA (WININET.@)
2396 BOOL WINAPI InternetTimeFromSystemTimeA( const SYSTEMTIME* time, DWORD format, LPSTR string, DWORD size )
2398 BOOL ret;
2399 WCHAR stringW[INTERNET_RFC1123_BUFSIZE];
2401 TRACE( "%p 0x%08x %p 0x%08x\n", time, format, string, size );
2403 ret = InternetTimeFromSystemTimeW( time, format, stringW, sizeof(stringW) );
2404 if (ret) WideCharToMultiByte( CP_ACP, 0, stringW, -1, string, size, NULL, NULL );
2406 return ret;
2409 /***********************************************************************
2410 * InternetTimeFromSystemTimeW (WININET.@)
2412 BOOL WINAPI InternetTimeFromSystemTimeW( const SYSTEMTIME* time, DWORD format, LPWSTR string, DWORD size )
2414 static const WCHAR date[] =
2415 { '%','s',',',' ','%','0','2','d',' ','%','s',' ','%','4','d',' ','%','0',
2416 '2','d',':','%','0','2','d',':','%','0','2','d',' ','G','M','T', 0 };
2418 TRACE( "%p 0x%08x %p 0x%08x\n", time, format, string, size );
2420 if (!time || !string) return FALSE;
2422 if (format != INTERNET_RFC1123_FORMAT || size < INTERNET_RFC1123_BUFSIZE * sizeof(WCHAR))
2423 return FALSE;
2425 sprintfW( string, date,
2426 WININET_wkday[time->wDayOfWeek],
2427 time->wDay,
2428 WININET_month[time->wMonth - 1],
2429 time->wYear,
2430 time->wHour,
2431 time->wMinute,
2432 time->wSecond );
2434 return TRUE;
2437 /***********************************************************************
2438 * InternetTimeToSystemTimeA (WININET.@)
2440 BOOL WINAPI InternetTimeToSystemTimeA( LPCSTR string, SYSTEMTIME* time, DWORD reserved )
2442 BOOL ret = FALSE;
2443 WCHAR *stringW;
2444 int len;
2446 TRACE( "%s %p 0x%08x\n", debugstr_a(string), time, reserved );
2448 len = MultiByteToWideChar( CP_ACP, 0, string, -1, NULL, 0 );
2449 stringW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
2451 if (stringW)
2453 MultiByteToWideChar( CP_ACP, 0, string, -1, stringW, len );
2454 ret = InternetTimeToSystemTimeW( stringW, time, reserved );
2455 HeapFree( GetProcessHeap(), 0, stringW );
2457 return ret;
2460 /***********************************************************************
2461 * InternetTimeToSystemTimeW (WININET.@)
2463 BOOL WINAPI InternetTimeToSystemTimeW( LPCWSTR string, SYSTEMTIME* time, DWORD reserved )
2465 unsigned int i;
2466 const WCHAR *s = string;
2467 WCHAR *end;
2469 TRACE( "%s %p 0x%08x\n", debugstr_w(string), time, reserved );
2471 if (!string || !time) return FALSE;
2473 /* Windows does this too */
2474 GetSystemTime( time );
2476 /* Convert an RFC1123 time such as 'Fri, 07 Jan 2005 12:06:35 GMT' into
2477 * a SYSTEMTIME structure.
2480 while (*s && !isalphaW( *s )) s++;
2481 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
2482 time->wDayOfWeek = 7;
2484 for (i = 0; i < 7; i++)
2486 if (toupperW( WININET_wkday[i][0] ) == toupperW( s[0] ) &&
2487 toupperW( WININET_wkday[i][1] ) == toupperW( s[1] ) &&
2488 toupperW( WININET_wkday[i][2] ) == toupperW( s[2] ) )
2490 time->wDayOfWeek = i;
2491 break;
2495 if (time->wDayOfWeek > 6) return TRUE;
2496 while (*s && !isdigitW( *s )) s++;
2497 time->wDay = strtolW( s, &end, 10 );
2498 s = end;
2500 while (*s && !isalphaW( *s )) s++;
2501 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
2502 time->wMonth = 0;
2504 for (i = 0; i < 12; i++)
2506 if (toupperW( WININET_month[i][0]) == toupperW( s[0] ) &&
2507 toupperW( WININET_month[i][1]) == toupperW( s[1] ) &&
2508 toupperW( WININET_month[i][2]) == toupperW( s[2] ) )
2510 time->wMonth = i + 1;
2511 break;
2514 if (time->wMonth == 0) return TRUE;
2516 while (*s && !isdigitW( *s )) s++;
2517 if (*s == '\0') return TRUE;
2518 time->wYear = strtolW( s, &end, 10 );
2519 s = end;
2521 while (*s && !isdigitW( *s )) s++;
2522 if (*s == '\0') return TRUE;
2523 time->wHour = strtolW( s, &end, 10 );
2524 s = end;
2526 while (*s && !isdigitW( *s )) s++;
2527 if (*s == '\0') return TRUE;
2528 time->wMinute = strtolW( s, &end, 10 );
2529 s = end;
2531 while (*s && !isdigitW( *s )) s++;
2532 if (*s == '\0') return TRUE;
2533 time->wSecond = strtolW( s, &end, 10 );
2534 s = end;
2536 time->wMilliseconds = 0;
2537 return TRUE;
2540 /***********************************************************************
2541 * InternetCheckConnectionW (WININET.@)
2543 * Pings a requested host to check internet connection
2545 * RETURNS
2546 * TRUE on success and FALSE on failure. If a failure then
2547 * ERROR_NOT_CONNECTED is placed into GetLastError
2550 BOOL WINAPI InternetCheckConnectionW( LPCWSTR lpszUrl, DWORD dwFlags, DWORD dwReserved )
2553 * this is a kludge which runs the resident ping program and reads the output.
2555 * Anyone have a better idea?
2558 BOOL rc = FALSE;
2559 static const CHAR ping[] = "ping -c 1 ";
2560 static const CHAR redirect[] = " >/dev/null 2>/dev/null";
2561 CHAR *command = NULL;
2562 WCHAR hostW[1024];
2563 DWORD len;
2564 INTERNET_PORT port;
2565 int status = -1;
2567 FIXME("\n");
2570 * Crack or set the Address
2572 if (lpszUrl == NULL)
2575 * According to the doc we are supposed to use the ip for the next
2576 * server in the WnInet internal server database. I have
2577 * no idea what that is or how to get it.
2579 * So someone needs to implement this.
2581 FIXME("Unimplemented with URL of NULL\n");
2582 return TRUE;
2584 else
2586 URL_COMPONENTSW components;
2588 ZeroMemory(&components,sizeof(URL_COMPONENTSW));
2589 components.lpszHostName = (LPWSTR)&hostW;
2590 components.dwHostNameLength = 1024;
2592 if (!InternetCrackUrlW(lpszUrl,0,0,&components))
2593 goto End;
2595 TRACE("host name : %s\n",debugstr_w(components.lpszHostName));
2596 port = components.nPort;
2597 TRACE("port: %d\n", port);
2600 if (dwFlags & FLAG_ICC_FORCE_CONNECTION)
2602 struct sockaddr_in sin;
2603 int fd;
2605 if (!GetAddress(hostW, port, &sin))
2606 goto End;
2607 fd = socket(sin.sin_family, SOCK_STREAM, 0);
2608 if (fd != -1)
2610 if (connect(fd, (struct sockaddr *)&sin, sizeof(sin)) == 0)
2611 rc = TRUE;
2612 close(fd);
2615 else
2618 * Build our ping command
2620 len = WideCharToMultiByte(CP_UNIXCP, 0, hostW, -1, NULL, 0, NULL, NULL);
2621 command = HeapAlloc( GetProcessHeap(), 0, strlen(ping)+len+strlen(redirect) );
2622 strcpy(command,ping);
2623 WideCharToMultiByte(CP_UNIXCP, 0, hostW, -1, command+strlen(ping), len, NULL, NULL);
2624 strcat(command,redirect);
2626 TRACE("Ping command is : %s\n",command);
2628 status = system(command);
2630 TRACE("Ping returned a code of %i\n",status);
2632 /* Ping return code of 0 indicates success */
2633 if (status == 0)
2634 rc = TRUE;
2637 End:
2639 HeapFree( GetProcessHeap(), 0, command );
2640 if (rc == FALSE)
2641 INTERNET_SetLastError(ERROR_NOT_CONNECTED);
2643 return rc;
2647 /***********************************************************************
2648 * InternetCheckConnectionA (WININET.@)
2650 * Pings a requested host to check internet connection
2652 * RETURNS
2653 * TRUE on success and FALSE on failure. If a failure then
2654 * ERROR_NOT_CONNECTED is placed into GetLastError
2657 BOOL WINAPI InternetCheckConnectionA(LPCSTR lpszUrl, DWORD dwFlags, DWORD dwReserved)
2659 WCHAR *szUrl;
2660 INT len;
2661 BOOL rc;
2663 len = MultiByteToWideChar(CP_ACP, 0, lpszUrl, -1, NULL, 0);
2664 if (!(szUrl = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR))))
2665 return FALSE;
2666 MultiByteToWideChar(CP_ACP, 0, lpszUrl, -1, szUrl, len);
2667 rc = InternetCheckConnectionW(szUrl, dwFlags, dwReserved);
2668 HeapFree(GetProcessHeap(), 0, szUrl);
2670 return rc;
2674 /**********************************************************
2675 * INTERNET_InternetOpenUrlW (internal)
2677 * Opens an URL
2679 * RETURNS
2680 * handle of connection or NULL on failure
2682 HINTERNET WINAPI INTERNET_InternetOpenUrlW(LPWININETAPPINFOW hIC, LPCWSTR lpszUrl,
2683 LPCWSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext)
2685 URL_COMPONENTSW urlComponents;
2686 WCHAR protocol[32], hostName[MAXHOSTNAME], userName[1024];
2687 WCHAR password[1024], path[2048], extra[1024];
2688 HINTERNET client = NULL, client1 = NULL;
2690 TRACE("(%p, %s, %s, %08x, %08x, %08lx)\n", hIC, debugstr_w(lpszUrl), debugstr_w(lpszHeaders),
2691 dwHeadersLength, dwFlags, dwContext);
2693 urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
2694 urlComponents.lpszScheme = protocol;
2695 urlComponents.dwSchemeLength = 32;
2696 urlComponents.lpszHostName = hostName;
2697 urlComponents.dwHostNameLength = MAXHOSTNAME;
2698 urlComponents.lpszUserName = userName;
2699 urlComponents.dwUserNameLength = 1024;
2700 urlComponents.lpszPassword = password;
2701 urlComponents.dwPasswordLength = 1024;
2702 urlComponents.lpszUrlPath = path;
2703 urlComponents.dwUrlPathLength = 2048;
2704 urlComponents.lpszExtraInfo = extra;
2705 urlComponents.dwExtraInfoLength = 1024;
2706 if(!InternetCrackUrlW(lpszUrl, strlenW(lpszUrl), 0, &urlComponents))
2707 return NULL;
2708 switch(urlComponents.nScheme) {
2709 case INTERNET_SCHEME_FTP:
2710 if(urlComponents.nPort == 0)
2711 urlComponents.nPort = INTERNET_DEFAULT_FTP_PORT;
2712 client = FTP_Connect(hIC, hostName, urlComponents.nPort,
2713 userName, password, dwFlags, dwContext, INET_OPENURL);
2714 if(client == NULL)
2715 break;
2716 client1 = FtpOpenFileW(client, path, GENERIC_READ, dwFlags, dwContext);
2717 if(client1 == NULL) {
2718 InternetCloseHandle(client);
2719 break;
2721 break;
2723 case INTERNET_SCHEME_HTTP:
2724 case INTERNET_SCHEME_HTTPS: {
2725 static const WCHAR szStars[] = { '*','/','*', 0 };
2726 LPCWSTR accept[2] = { szStars, NULL };
2727 if(urlComponents.nPort == 0) {
2728 if(urlComponents.nScheme == INTERNET_SCHEME_HTTP)
2729 urlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
2730 else
2731 urlComponents.nPort = INTERNET_DEFAULT_HTTPS_PORT;
2733 /* FIXME: should use pointers, not handles, as handles are not thread-safe */
2734 client = HTTP_Connect(hIC, hostName, urlComponents.nPort,
2735 userName, password, dwFlags, dwContext, INET_OPENURL);
2736 if(client == NULL)
2737 break;
2739 if (urlComponents.dwExtraInfoLength) {
2740 WCHAR *path_extra;
2741 DWORD len = urlComponents.dwUrlPathLength + urlComponents.dwExtraInfoLength + 1;
2743 if (!(path_extra = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR))))
2745 InternetCloseHandle(client);
2746 break;
2748 strcpyW(path_extra, urlComponents.lpszUrlPath);
2749 strcatW(path_extra, urlComponents.lpszExtraInfo);
2750 client1 = HttpOpenRequestW(client, NULL, path_extra, NULL, NULL, accept, dwFlags, dwContext);
2751 HeapFree(GetProcessHeap(), 0, path_extra);
2753 else
2754 client1 = HttpOpenRequestW(client, NULL, path, NULL, NULL, accept, dwFlags, dwContext);
2756 if(client1 == NULL) {
2757 InternetCloseHandle(client);
2758 break;
2760 HttpAddRequestHeadersW(client1, lpszHeaders, dwHeadersLength, HTTP_ADDREQ_FLAG_ADD);
2761 if (!HttpSendRequestW(client1, NULL, 0, NULL, 0) &&
2762 GetLastError() != ERROR_IO_PENDING) {
2763 InternetCloseHandle(client1);
2764 client1 = NULL;
2765 break;
2768 case INTERNET_SCHEME_GOPHER:
2769 /* gopher doesn't seem to be implemented in wine, but it's supposed
2770 * to be supported by InternetOpenUrlA. */
2771 default:
2772 INTERNET_SetLastError(ERROR_INTERNET_UNRECOGNIZED_SCHEME);
2773 break;
2776 TRACE(" %p <--\n", client1);
2778 return client1;
2781 /**********************************************************
2782 * InternetOpenUrlW (WININET.@)
2784 * Opens an URL
2786 * RETURNS
2787 * handle of connection or NULL on failure
2789 static void AsyncInternetOpenUrlProc(WORKREQUEST *workRequest)
2791 struct WORKREQ_INTERNETOPENURLW const *req = &workRequest->u.InternetOpenUrlW;
2792 LPWININETAPPINFOW hIC = (LPWININETAPPINFOW) workRequest->hdr;
2794 TRACE("%p\n", hIC);
2796 INTERNET_InternetOpenUrlW(hIC, req->lpszUrl,
2797 req->lpszHeaders, req->dwHeadersLength, req->dwFlags, req->dwContext);
2798 HeapFree(GetProcessHeap(), 0, req->lpszUrl);
2799 HeapFree(GetProcessHeap(), 0, req->lpszHeaders);
2802 HINTERNET WINAPI InternetOpenUrlW(HINTERNET hInternet, LPCWSTR lpszUrl,
2803 LPCWSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext)
2805 HINTERNET ret = NULL;
2806 LPWININETAPPINFOW hIC = NULL;
2808 if (TRACE_ON(wininet)) {
2809 TRACE("(%p, %s, %s, %08x, %08x, %08lx)\n", hInternet, debugstr_w(lpszUrl), debugstr_w(lpszHeaders),
2810 dwHeadersLength, dwFlags, dwContext);
2811 TRACE(" flags :");
2812 dump_INTERNET_FLAGS(dwFlags);
2815 if (!lpszUrl)
2817 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
2818 goto lend;
2821 hIC = (LPWININETAPPINFOW) WININET_GetObject( hInternet );
2822 if (NULL == hIC || hIC->hdr.htype != WH_HINIT) {
2823 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2824 goto lend;
2827 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC) {
2828 WORKREQUEST workRequest;
2829 struct WORKREQ_INTERNETOPENURLW *req;
2831 workRequest.asyncproc = AsyncInternetOpenUrlProc;
2832 workRequest.hdr = WININET_AddRef( &hIC->hdr );
2833 req = &workRequest.u.InternetOpenUrlW;
2834 req->lpszUrl = WININET_strdupW(lpszUrl);
2835 if (lpszHeaders)
2836 req->lpszHeaders = WININET_strdupW(lpszHeaders);
2837 else
2838 req->lpszHeaders = 0;
2839 req->dwHeadersLength = dwHeadersLength;
2840 req->dwFlags = dwFlags;
2841 req->dwContext = dwContext;
2843 INTERNET_AsyncCall(&workRequest);
2845 * This is from windows.
2847 INTERNET_SetLastError(ERROR_IO_PENDING);
2848 } else {
2849 ret = INTERNET_InternetOpenUrlW(hIC, lpszUrl, lpszHeaders, dwHeadersLength, dwFlags, dwContext);
2852 lend:
2853 if( hIC )
2854 WININET_Release( &hIC->hdr );
2855 TRACE(" %p <--\n", ret);
2857 return ret;
2860 /**********************************************************
2861 * InternetOpenUrlA (WININET.@)
2863 * Opens an URL
2865 * RETURNS
2866 * handle of connection or NULL on failure
2868 HINTERNET WINAPI InternetOpenUrlA(HINTERNET hInternet, LPCSTR lpszUrl,
2869 LPCSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext)
2871 HINTERNET rc = NULL;
2873 INT lenUrl;
2874 INT lenHeaders = 0;
2875 LPWSTR szUrl = NULL;
2876 LPWSTR szHeaders = NULL;
2878 TRACE("\n");
2880 if(lpszUrl) {
2881 lenUrl = MultiByteToWideChar(CP_ACP, 0, lpszUrl, -1, NULL, 0 );
2882 szUrl = HeapAlloc(GetProcessHeap(), 0, lenUrl*sizeof(WCHAR));
2883 if(!szUrl)
2884 return NULL;
2885 MultiByteToWideChar(CP_ACP, 0, lpszUrl, -1, szUrl, lenUrl);
2888 if(lpszHeaders) {
2889 lenHeaders = MultiByteToWideChar(CP_ACP, 0, lpszHeaders, dwHeadersLength, NULL, 0 );
2890 szHeaders = HeapAlloc(GetProcessHeap(), 0, lenHeaders*sizeof(WCHAR));
2891 if(!szHeaders) {
2892 HeapFree(GetProcessHeap(), 0, szUrl);
2893 return NULL;
2895 MultiByteToWideChar(CP_ACP, 0, lpszHeaders, dwHeadersLength, szHeaders, lenHeaders);
2898 rc = InternetOpenUrlW(hInternet, szUrl, szHeaders,
2899 lenHeaders, dwFlags, dwContext);
2901 HeapFree(GetProcessHeap(), 0, szUrl);
2902 HeapFree(GetProcessHeap(), 0, szHeaders);
2904 return rc;
2908 static LPWITHREADERROR INTERNET_AllocThreadError(void)
2910 LPWITHREADERROR lpwite = HeapAlloc(GetProcessHeap(), 0, sizeof(*lpwite));
2912 if (lpwite)
2914 lpwite->dwError = 0;
2915 lpwite->response[0] = '\0';
2918 if (!TlsSetValue(g_dwTlsErrIndex, lpwite))
2920 HeapFree(GetProcessHeap(), 0, lpwite);
2921 return NULL;
2924 return lpwite;
2928 /***********************************************************************
2929 * INTERNET_SetLastError (internal)
2931 * Set last thread specific error
2933 * RETURNS
2936 void INTERNET_SetLastError(DWORD dwError)
2938 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
2940 if (!lpwite)
2941 lpwite = INTERNET_AllocThreadError();
2943 SetLastError(dwError);
2944 if(lpwite)
2945 lpwite->dwError = dwError;
2949 /***********************************************************************
2950 * INTERNET_GetLastError (internal)
2952 * Get last thread specific error
2954 * RETURNS
2957 DWORD INTERNET_GetLastError(void)
2959 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
2960 if (!lpwite) return 0;
2961 /* TlsGetValue clears last error, so set it again here */
2962 SetLastError(lpwite->dwError);
2963 return lpwite->dwError;
2967 /***********************************************************************
2968 * INTERNET_WorkerThreadFunc (internal)
2970 * Worker thread execution function
2972 * RETURNS
2975 static DWORD CALLBACK INTERNET_WorkerThreadFunc(LPVOID lpvParam)
2977 LPWORKREQUEST lpRequest = lpvParam;
2978 WORKREQUEST workRequest;
2980 TRACE("\n");
2982 memcpy(&workRequest, lpRequest, sizeof(WORKREQUEST));
2983 HeapFree(GetProcessHeap(), 0, lpRequest);
2985 workRequest.asyncproc(&workRequest);
2987 WININET_Release( workRequest.hdr );
2988 return TRUE;
2992 /***********************************************************************
2993 * INTERNET_AsyncCall (internal)
2995 * Retrieves work request from queue
2997 * RETURNS
3000 BOOL INTERNET_AsyncCall(LPWORKREQUEST lpWorkRequest)
3002 BOOL bSuccess;
3003 LPWORKREQUEST lpNewRequest;
3005 TRACE("\n");
3007 lpNewRequest = HeapAlloc(GetProcessHeap(), 0, sizeof(WORKREQUEST));
3008 if (!lpNewRequest)
3009 return FALSE;
3011 memcpy(lpNewRequest, lpWorkRequest, sizeof(WORKREQUEST));
3013 bSuccess = QueueUserWorkItem(INTERNET_WorkerThreadFunc, lpNewRequest, WT_EXECUTELONGFUNCTION);
3014 if (!bSuccess)
3016 HeapFree(GetProcessHeap(), 0, lpNewRequest);
3017 INTERNET_SetLastError(ERROR_INTERNET_ASYNC_THREAD_FAILED);
3020 return bSuccess;
3024 /***********************************************************************
3025 * INTERNET_GetResponseBuffer (internal)
3027 * RETURNS
3030 LPSTR INTERNET_GetResponseBuffer(void)
3032 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
3033 if (!lpwite)
3034 lpwite = INTERNET_AllocThreadError();
3035 TRACE("\n");
3036 return lpwite->response;
3039 /***********************************************************************
3040 * INTERNET_GetNextLine (internal)
3042 * Parse next line in directory string listing
3044 * RETURNS
3045 * Pointer to beginning of next line
3046 * NULL on failure
3050 LPSTR INTERNET_GetNextLine(INT nSocket, LPDWORD dwLen)
3052 struct timeval tv;
3053 fd_set infd;
3054 BOOL bSuccess = FALSE;
3055 INT nRecv = 0;
3056 LPSTR lpszBuffer = INTERNET_GetResponseBuffer();
3058 TRACE("\n");
3060 FD_ZERO(&infd);
3061 FD_SET(nSocket, &infd);
3062 tv.tv_sec=RESPONSE_TIMEOUT;
3063 tv.tv_usec=0;
3065 while (nRecv < MAX_REPLY_LEN)
3067 if (select(nSocket+1,&infd,NULL,NULL,&tv) > 0)
3069 if (recv(nSocket, &lpszBuffer[nRecv], 1, 0) <= 0)
3071 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
3072 goto lend;
3075 if (lpszBuffer[nRecv] == '\n')
3077 bSuccess = TRUE;
3078 break;
3080 if (lpszBuffer[nRecv] != '\r')
3081 nRecv++;
3083 else
3085 INTERNET_SetLastError(ERROR_INTERNET_TIMEOUT);
3086 goto lend;
3090 lend:
3091 if (bSuccess)
3093 lpszBuffer[nRecv] = '\0';
3094 *dwLen = nRecv - 1;
3095 TRACE(":%d %s\n", nRecv, lpszBuffer);
3096 return lpszBuffer;
3098 else
3100 return NULL;
3104 /**********************************************************
3105 * InternetQueryDataAvailable (WININET.@)
3107 * Determines how much data is available to be read.
3109 * RETURNS
3110 * TRUE on success, FALSE if an error occurred. If
3111 * INTERNET_FLAG_ASYNC was specified in InternetOpen, and
3112 * no data is presently available, FALSE is returned with
3113 * the last error ERROR_IO_PENDING; a callback with status
3114 * INTERNET_STATUS_REQUEST_COMPLETE will be sent when more
3115 * data is available.
3117 BOOL WINAPI InternetQueryDataAvailable( HINTERNET hFile,
3118 LPDWORD lpdwNumberOfBytesAvailble,
3119 DWORD dwFlags, DWORD_PTR dwContext)
3121 WININETHANDLEHEADER *hdr;
3122 DWORD res;
3124 TRACE("(%p %p %x %lx)\n", hFile, lpdwNumberOfBytesAvailble, dwFlags, dwContext);
3126 hdr = WININET_GetObject( hFile );
3127 if (!hdr) {
3128 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
3129 return FALSE;
3132 if(hdr->vtbl->QueryDataAvailable) {
3133 res = hdr->vtbl->QueryDataAvailable(hdr, lpdwNumberOfBytesAvailble, dwFlags, dwContext);
3134 }else {
3135 WARN("wrong handle\n");
3136 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
3139 WININET_Release(hdr);
3141 if(res != ERROR_SUCCESS)
3142 SetLastError(res);
3143 return res == ERROR_SUCCESS;
3147 /***********************************************************************
3148 * InternetLockRequestFile (WININET.@)
3150 BOOL WINAPI InternetLockRequestFile( HINTERNET hInternet, HANDLE
3151 *lphLockReqHandle)
3153 FIXME("STUB\n");
3154 return FALSE;
3157 BOOL WINAPI InternetUnlockRequestFile( HANDLE hLockHandle)
3159 FIXME("STUB\n");
3160 return FALSE;
3164 /***********************************************************************
3165 * InternetAutodial (WININET.@)
3167 * On windows this function is supposed to dial the default internet
3168 * connection. We don't want to have Wine dial out to the internet so
3169 * we return TRUE by default. It might be nice to check if we are connected.
3171 * RETURNS
3172 * TRUE on success
3173 * FALSE on failure
3176 BOOL WINAPI InternetAutodial(DWORD dwFlags, HWND hwndParent)
3178 FIXME("STUB\n");
3180 /* Tell that we are connected to the internet. */
3181 return TRUE;
3184 /***********************************************************************
3185 * InternetAutodialHangup (WININET.@)
3187 * Hangs up a connection made with InternetAutodial
3189 * PARAM
3190 * dwReserved
3191 * RETURNS
3192 * TRUE on success
3193 * FALSE on failure
3196 BOOL WINAPI InternetAutodialHangup(DWORD dwReserved)
3198 FIXME("STUB\n");
3200 /* we didn't dial, we don't disconnect */
3201 return TRUE;
3204 /***********************************************************************
3205 * InternetCombineUrlA (WININET.@)
3207 * Combine a base URL with a relative URL
3209 * RETURNS
3210 * TRUE on success
3211 * FALSE on failure
3215 BOOL WINAPI InternetCombineUrlA(LPCSTR lpszBaseUrl, LPCSTR lpszRelativeUrl,
3216 LPSTR lpszBuffer, LPDWORD lpdwBufferLength,
3217 DWORD dwFlags)
3219 HRESULT hr=S_OK;
3221 TRACE("(%s, %s, %p, %p, 0x%08x)\n", debugstr_a(lpszBaseUrl), debugstr_a(lpszRelativeUrl), lpszBuffer, lpdwBufferLength, dwFlags);
3223 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
3224 dwFlags ^= ICU_NO_ENCODE;
3225 hr=UrlCombineA(lpszBaseUrl,lpszRelativeUrl,lpszBuffer,lpdwBufferLength,dwFlags);
3227 return (hr==S_OK);
3230 /***********************************************************************
3231 * InternetCombineUrlW (WININET.@)
3233 * Combine a base URL with a relative URL
3235 * RETURNS
3236 * TRUE on success
3237 * FALSE on failure
3241 BOOL WINAPI InternetCombineUrlW(LPCWSTR lpszBaseUrl, LPCWSTR lpszRelativeUrl,
3242 LPWSTR lpszBuffer, LPDWORD lpdwBufferLength,
3243 DWORD dwFlags)
3245 HRESULT hr=S_OK;
3247 TRACE("(%s, %s, %p, %p, 0x%08x)\n", debugstr_w(lpszBaseUrl), debugstr_w(lpszRelativeUrl), lpszBuffer, lpdwBufferLength, dwFlags);
3249 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
3250 dwFlags ^= ICU_NO_ENCODE;
3251 hr=UrlCombineW(lpszBaseUrl,lpszRelativeUrl,lpszBuffer,lpdwBufferLength,dwFlags);
3253 return (hr==S_OK);
3256 /* max port num is 65535 => 5 digits */
3257 #define MAX_WORD_DIGITS 5
3259 #define URL_GET_COMP_LENGTH(url, component) ((url)->dw##component##Length ? \
3260 (url)->dw##component##Length : strlenW((url)->lpsz##component))
3261 #define URL_GET_COMP_LENGTHA(url, component) ((url)->dw##component##Length ? \
3262 (url)->dw##component##Length : strlen((url)->lpsz##component))
3264 static BOOL url_uses_default_port(INTERNET_SCHEME nScheme, INTERNET_PORT nPort)
3266 if ((nScheme == INTERNET_SCHEME_HTTP) &&
3267 (nPort == INTERNET_DEFAULT_HTTP_PORT))
3268 return TRUE;
3269 if ((nScheme == INTERNET_SCHEME_HTTPS) &&
3270 (nPort == INTERNET_DEFAULT_HTTPS_PORT))
3271 return TRUE;
3272 if ((nScheme == INTERNET_SCHEME_FTP) &&
3273 (nPort == INTERNET_DEFAULT_FTP_PORT))
3274 return TRUE;
3275 if ((nScheme == INTERNET_SCHEME_GOPHER) &&
3276 (nPort == INTERNET_DEFAULT_GOPHER_PORT))
3277 return TRUE;
3279 if (nPort == INTERNET_INVALID_PORT_NUMBER)
3280 return TRUE;
3282 return FALSE;
3285 /* opaque urls do not fit into the standard url hierarchy and don't have
3286 * two following slashes */
3287 static inline BOOL scheme_is_opaque(INTERNET_SCHEME nScheme)
3289 return (nScheme != INTERNET_SCHEME_FTP) &&
3290 (nScheme != INTERNET_SCHEME_GOPHER) &&
3291 (nScheme != INTERNET_SCHEME_HTTP) &&
3292 (nScheme != INTERNET_SCHEME_HTTPS) &&
3293 (nScheme != INTERNET_SCHEME_FILE);
3296 static LPCWSTR INTERNET_GetSchemeString(INTERNET_SCHEME scheme)
3298 int index;
3299 if (scheme < INTERNET_SCHEME_FIRST)
3300 return NULL;
3301 index = scheme - INTERNET_SCHEME_FIRST;
3302 if (index >= sizeof(url_schemes)/sizeof(url_schemes[0]))
3303 return NULL;
3304 return (LPCWSTR)&url_schemes[index];
3307 /* we can calculate using ansi strings because we're just
3308 * calculating string length, not size
3310 static BOOL calc_url_length(LPURL_COMPONENTSW lpUrlComponents,
3311 LPDWORD lpdwUrlLength)
3313 INTERNET_SCHEME nScheme;
3315 *lpdwUrlLength = 0;
3317 if (lpUrlComponents->lpszScheme)
3319 DWORD dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, Scheme);
3320 *lpdwUrlLength += dwLen;
3321 nScheme = GetInternetSchemeW(lpUrlComponents->lpszScheme, dwLen);
3323 else
3325 LPCWSTR scheme;
3327 nScheme = lpUrlComponents->nScheme;
3329 if (nScheme == INTERNET_SCHEME_DEFAULT)
3330 nScheme = INTERNET_SCHEME_HTTP;
3331 scheme = INTERNET_GetSchemeString(nScheme);
3332 *lpdwUrlLength += strlenW(scheme);
3335 (*lpdwUrlLength)++; /* ':' */
3336 if (!scheme_is_opaque(nScheme) || lpUrlComponents->lpszHostName)
3337 *lpdwUrlLength += strlen("//");
3339 if (lpUrlComponents->lpszUserName)
3341 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, UserName);
3342 *lpdwUrlLength += strlen("@");
3344 else
3346 if (lpUrlComponents->lpszPassword)
3348 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
3349 return FALSE;
3353 if (lpUrlComponents->lpszPassword)
3355 *lpdwUrlLength += strlen(":");
3356 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, Password);
3359 if (lpUrlComponents->lpszHostName)
3361 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, HostName);
3363 if (!url_uses_default_port(nScheme, lpUrlComponents->nPort))
3365 char szPort[MAX_WORD_DIGITS+1];
3367 sprintf(szPort, "%d", lpUrlComponents->nPort);
3368 *lpdwUrlLength += strlen(szPort);
3369 *lpdwUrlLength += strlen(":");
3372 if (lpUrlComponents->lpszUrlPath && *lpUrlComponents->lpszUrlPath != '/')
3373 (*lpdwUrlLength)++; /* '/' */
3376 if (lpUrlComponents->lpszUrlPath)
3377 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, UrlPath);
3379 return TRUE;
3382 static void convert_urlcomp_atow(LPURL_COMPONENTSA lpUrlComponents, LPURL_COMPONENTSW urlCompW)
3384 INT len;
3386 ZeroMemory(urlCompW, sizeof(URL_COMPONENTSW));
3388 urlCompW->dwStructSize = sizeof(URL_COMPONENTSW);
3389 urlCompW->dwSchemeLength = lpUrlComponents->dwSchemeLength;
3390 urlCompW->nScheme = lpUrlComponents->nScheme;
3391 urlCompW->dwHostNameLength = lpUrlComponents->dwHostNameLength;
3392 urlCompW->nPort = lpUrlComponents->nPort;
3393 urlCompW->dwUserNameLength = lpUrlComponents->dwUserNameLength;
3394 urlCompW->dwPasswordLength = lpUrlComponents->dwPasswordLength;
3395 urlCompW->dwUrlPathLength = lpUrlComponents->dwUrlPathLength;
3396 urlCompW->dwExtraInfoLength = lpUrlComponents->dwExtraInfoLength;
3398 if (lpUrlComponents->lpszScheme)
3400 len = URL_GET_COMP_LENGTHA(lpUrlComponents, Scheme) + 1;
3401 urlCompW->lpszScheme = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3402 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszScheme,
3403 -1, urlCompW->lpszScheme, len);
3406 if (lpUrlComponents->lpszHostName)
3408 len = URL_GET_COMP_LENGTHA(lpUrlComponents, HostName) + 1;
3409 urlCompW->lpszHostName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3410 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszHostName,
3411 -1, urlCompW->lpszHostName, len);
3414 if (lpUrlComponents->lpszUserName)
3416 len = URL_GET_COMP_LENGTHA(lpUrlComponents, UserName) + 1;
3417 urlCompW->lpszUserName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3418 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszUserName,
3419 -1, urlCompW->lpszUserName, len);
3422 if (lpUrlComponents->lpszPassword)
3424 len = URL_GET_COMP_LENGTHA(lpUrlComponents, Password) + 1;
3425 urlCompW->lpszPassword = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3426 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszPassword,
3427 -1, urlCompW->lpszPassword, len);
3430 if (lpUrlComponents->lpszUrlPath)
3432 len = URL_GET_COMP_LENGTHA(lpUrlComponents, UrlPath) + 1;
3433 urlCompW->lpszUrlPath = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3434 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszUrlPath,
3435 -1, urlCompW->lpszUrlPath, len);
3438 if (lpUrlComponents->lpszExtraInfo)
3440 len = URL_GET_COMP_LENGTHA(lpUrlComponents, ExtraInfo) + 1;
3441 urlCompW->lpszExtraInfo = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3442 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszExtraInfo,
3443 -1, urlCompW->lpszExtraInfo, len);
3447 /***********************************************************************
3448 * InternetCreateUrlA (WININET.@)
3450 * See InternetCreateUrlW.
3452 BOOL WINAPI InternetCreateUrlA(LPURL_COMPONENTSA lpUrlComponents, DWORD dwFlags,
3453 LPSTR lpszUrl, LPDWORD lpdwUrlLength)
3455 BOOL ret;
3456 LPWSTR urlW = NULL;
3457 URL_COMPONENTSW urlCompW;
3459 TRACE("(%p,%d,%p,%p)\n", lpUrlComponents, dwFlags, lpszUrl, lpdwUrlLength);
3461 if (!lpUrlComponents || lpUrlComponents->dwStructSize != sizeof(URL_COMPONENTSW) || !lpdwUrlLength)
3463 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
3464 return FALSE;
3467 convert_urlcomp_atow(lpUrlComponents, &urlCompW);
3469 if (lpszUrl)
3470 urlW = HeapAlloc(GetProcessHeap(), 0, *lpdwUrlLength * sizeof(WCHAR));
3472 ret = InternetCreateUrlW(&urlCompW, dwFlags, urlW, lpdwUrlLength);
3474 if (!ret && (GetLastError() == ERROR_INSUFFICIENT_BUFFER))
3475 *lpdwUrlLength /= sizeof(WCHAR);
3477 /* on success, lpdwUrlLength points to the size of urlW in WCHARS
3478 * minus one, so add one to leave room for NULL terminator
3480 if (ret)
3481 WideCharToMultiByte(CP_ACP, 0, urlW, -1, lpszUrl, *lpdwUrlLength + 1, NULL, NULL);
3483 HeapFree(GetProcessHeap(), 0, urlCompW.lpszScheme);
3484 HeapFree(GetProcessHeap(), 0, urlCompW.lpszHostName);
3485 HeapFree(GetProcessHeap(), 0, urlCompW.lpszUserName);
3486 HeapFree(GetProcessHeap(), 0, urlCompW.lpszPassword);
3487 HeapFree(GetProcessHeap(), 0, urlCompW.lpszUrlPath);
3488 HeapFree(GetProcessHeap(), 0, urlCompW.lpszExtraInfo);
3489 HeapFree(GetProcessHeap(), 0, urlW);
3491 return ret;
3494 /***********************************************************************
3495 * InternetCreateUrlW (WININET.@)
3497 * Creates a URL from its component parts.
3499 * PARAMS
3500 * lpUrlComponents [I] URL Components.
3501 * dwFlags [I] Flags. See notes.
3502 * lpszUrl [I] Buffer in which to store the created URL.
3503 * lpdwUrlLength [I/O] On input, the length of the buffer pointed to by
3504 * lpszUrl in characters. On output, the number of bytes
3505 * required to store the URL including terminator.
3507 * NOTES
3509 * The dwFlags parameter can be zero or more of the following:
3510 *|ICU_ESCAPE - Generates escape sequences for unsafe characters in the path and extra info of the URL.
3512 * RETURNS
3513 * TRUE on success
3514 * FALSE on failure
3517 BOOL WINAPI InternetCreateUrlW(LPURL_COMPONENTSW lpUrlComponents, DWORD dwFlags,
3518 LPWSTR lpszUrl, LPDWORD lpdwUrlLength)
3520 DWORD dwLen;
3521 INTERNET_SCHEME nScheme;
3523 static const WCHAR slashSlashW[] = {'/','/'};
3524 static const WCHAR percentD[] = {'%','d',0};
3526 TRACE("(%p,%d,%p,%p)\n", lpUrlComponents, dwFlags, lpszUrl, lpdwUrlLength);
3528 if (!lpUrlComponents || lpUrlComponents->dwStructSize != sizeof(URL_COMPONENTSW) || !lpdwUrlLength)
3530 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
3531 return FALSE;
3534 if (!calc_url_length(lpUrlComponents, &dwLen))
3535 return FALSE;
3537 if (!lpszUrl || *lpdwUrlLength < dwLen)
3539 *lpdwUrlLength = (dwLen + 1) * sizeof(WCHAR);
3540 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
3541 return FALSE;
3544 *lpdwUrlLength = dwLen;
3545 lpszUrl[0] = 0x00;
3547 dwLen = 0;
3549 if (lpUrlComponents->lpszScheme)
3551 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, Scheme);
3552 memcpy(lpszUrl, lpUrlComponents->lpszScheme, dwLen * sizeof(WCHAR));
3553 lpszUrl += dwLen;
3555 nScheme = GetInternetSchemeW(lpUrlComponents->lpszScheme, dwLen);
3557 else
3559 LPCWSTR scheme;
3560 nScheme = lpUrlComponents->nScheme;
3562 if (nScheme == INTERNET_SCHEME_DEFAULT)
3563 nScheme = INTERNET_SCHEME_HTTP;
3565 scheme = INTERNET_GetSchemeString(nScheme);
3566 dwLen = strlenW(scheme);
3567 memcpy(lpszUrl, scheme, dwLen * sizeof(WCHAR));
3568 lpszUrl += dwLen;
3571 /* all schemes are followed by at least a colon */
3572 *lpszUrl = ':';
3573 lpszUrl++;
3575 if (!scheme_is_opaque(nScheme) || lpUrlComponents->lpszHostName)
3577 memcpy(lpszUrl, slashSlashW, sizeof(slashSlashW));
3578 lpszUrl += sizeof(slashSlashW)/sizeof(slashSlashW[0]);
3581 if (lpUrlComponents->lpszUserName)
3583 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, UserName);
3584 memcpy(lpszUrl, lpUrlComponents->lpszUserName, dwLen * sizeof(WCHAR));
3585 lpszUrl += dwLen;
3587 if (lpUrlComponents->lpszPassword)
3589 *lpszUrl = ':';
3590 lpszUrl++;
3592 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, Password);
3593 memcpy(lpszUrl, lpUrlComponents->lpszPassword, dwLen * sizeof(WCHAR));
3594 lpszUrl += dwLen;
3597 *lpszUrl = '@';
3598 lpszUrl++;
3601 if (lpUrlComponents->lpszHostName)
3603 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, HostName);
3604 memcpy(lpszUrl, lpUrlComponents->lpszHostName, dwLen * sizeof(WCHAR));
3605 lpszUrl += dwLen;
3607 if (!url_uses_default_port(nScheme, lpUrlComponents->nPort))
3609 WCHAR szPort[MAX_WORD_DIGITS+1];
3611 sprintfW(szPort, percentD, lpUrlComponents->nPort);
3612 *lpszUrl = ':';
3613 lpszUrl++;
3614 dwLen = strlenW(szPort);
3615 memcpy(lpszUrl, szPort, dwLen * sizeof(WCHAR));
3616 lpszUrl += dwLen;
3619 /* add slash between hostname and path if necessary */
3620 if (lpUrlComponents->lpszUrlPath && *lpUrlComponents->lpszUrlPath != '/')
3622 *lpszUrl = '/';
3623 lpszUrl++;
3628 if (lpUrlComponents->lpszUrlPath)
3630 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, UrlPath);
3631 memcpy(lpszUrl, lpUrlComponents->lpszUrlPath, dwLen * sizeof(WCHAR));
3632 lpszUrl += dwLen;
3635 *lpszUrl = '\0';
3637 return TRUE;
3640 /***********************************************************************
3641 * InternetConfirmZoneCrossingA (WININET.@)
3644 DWORD WINAPI InternetConfirmZoneCrossingA( HWND hWnd, LPSTR szUrlPrev, LPSTR szUrlNew, BOOL bPost )
3646 FIXME("(%p, %s, %s, %x) stub\n", hWnd, debugstr_a(szUrlPrev), debugstr_a(szUrlNew), bPost);
3647 return ERROR_SUCCESS;
3650 /***********************************************************************
3651 * InternetConfirmZoneCrossingW (WININET.@)
3654 DWORD WINAPI InternetConfirmZoneCrossingW( HWND hWnd, LPWSTR szUrlPrev, LPWSTR szUrlNew, BOOL bPost )
3656 FIXME("(%p, %s, %s, %x) stub\n", hWnd, debugstr_w(szUrlPrev), debugstr_w(szUrlNew), bPost);
3657 return ERROR_SUCCESS;
3660 DWORD WINAPI InternetDialA( HWND hwndParent, LPSTR lpszConnectoid, DWORD dwFlags,
3661 DWORD_PTR* lpdwConnection, DWORD dwReserved )
3663 FIXME("(%p, %p, 0x%08x, %p, 0x%08x) stub\n", hwndParent, lpszConnectoid, dwFlags,
3664 lpdwConnection, dwReserved);
3665 return ERROR_SUCCESS;
3668 DWORD WINAPI InternetDialW( HWND hwndParent, LPWSTR lpszConnectoid, DWORD dwFlags,
3669 DWORD_PTR* lpdwConnection, DWORD dwReserved )
3671 FIXME("(%p, %p, 0x%08x, %p, 0x%08x) stub\n", hwndParent, lpszConnectoid, dwFlags,
3672 lpdwConnection, dwReserved);
3673 return ERROR_SUCCESS;
3676 BOOL WINAPI InternetGoOnlineA( LPSTR lpszURL, HWND hwndParent, DWORD dwReserved )
3678 FIXME("(%s, %p, 0x%08x) stub\n", debugstr_a(lpszURL), hwndParent, dwReserved);
3679 return TRUE;
3682 BOOL WINAPI InternetGoOnlineW( LPWSTR lpszURL, HWND hwndParent, DWORD dwReserved )
3684 FIXME("(%s, %p, 0x%08x) stub\n", debugstr_w(lpszURL), hwndParent, dwReserved);
3685 return TRUE;
3688 DWORD WINAPI InternetHangUp( DWORD_PTR dwConnection, DWORD dwReserved )
3690 FIXME("(0x%08lx, 0x%08x) stub\n", dwConnection, dwReserved);
3691 return ERROR_SUCCESS;
3694 BOOL WINAPI CreateMD5SSOHash( PWSTR pszChallengeInfo, PWSTR pwszRealm, PWSTR pwszTarget,
3695 PBYTE pbHexHash )
3697 FIXME("(%s, %s, %s, %p) stub\n", debugstr_w(pszChallengeInfo), debugstr_w(pwszRealm),
3698 debugstr_w(pwszTarget), pbHexHash);
3699 return FALSE;
3702 BOOL WINAPI ResumeSuspendedDownload( HINTERNET hInternet, DWORD dwError )
3704 FIXME("(%p, 0x%08x) stub\n", hInternet, dwError);
3705 return FALSE;
3708 BOOL WINAPI InternetQueryFortezzaStatus(DWORD *a, DWORD_PTR b)
3710 FIXME("(%p, %08lx) stub\n", a, b);
3711 return 0;