mshtml: Added IHTMLDocument2::get_location implementation.
[wine/wine64.git] / dlls / wininet / internet.c
blobbe2fa9ce93f5cc06b0640a194174e040fac90bcd
1 /*
2 * Wininet
4 * Copyright 1999 Corel Corporation
5 * Copyright 2002 CodeWeavers Inc.
6 * Copyright 2002 Jaco Greeff
7 * Copyright 2002 TransGaming Technologies Inc.
8 * Copyright 2004 Mike McCormack for CodeWeavers
10 * Ulrich Czekalla
11 * Aric Stewart
12 * David Hammerton
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
29 #include "config.h"
30 #include "wine/port.h"
32 #define MAXHOSTNAME 100 /* from http.c */
34 #include <string.h>
35 #include <stdarg.h>
36 #include <stdio.h>
37 #include <sys/types.h>
38 #ifdef HAVE_SYS_SOCKET_H
39 # include <sys/socket.h>
40 #endif
41 #ifdef HAVE_POLL_H
42 #include <poll.h>
43 #endif
44 #ifdef HAVE_SYS_POLL_H
45 # include <sys/poll.h>
46 #endif
47 #ifdef HAVE_SYS_TIME_H
48 # include <sys/time.h>
49 #endif
50 #include <stdlib.h>
51 #include <ctype.h>
52 #ifdef HAVE_UNISTD_H
53 # include <unistd.h>
54 #endif
55 #include <assert.h>
57 #include "windef.h"
58 #include "winbase.h"
59 #include "winreg.h"
60 #include "winuser.h"
61 #include "wininet.h"
62 #include "winineti.h"
63 #include "winnls.h"
64 #include "wine/debug.h"
65 #include "winerror.h"
66 #define NO_SHLWAPI_STREAM
67 #include "shlwapi.h"
69 #include "wine/exception.h"
71 #include "internet.h"
72 #include "resource.h"
74 #include "wine/unicode.h"
76 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
78 #define RESPONSE_TIMEOUT 30
80 typedef struct
82 DWORD dwError;
83 CHAR response[MAX_REPLY_LEN];
84 } WITHREADERROR, *LPWITHREADERROR;
86 HINTERNET WINAPI INTERNET_InternetOpenUrlW(LPWININETAPPINFOW hIC, LPCWSTR lpszUrl,
87 LPCWSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext);
89 static DWORD g_dwTlsErrIndex = TLS_OUT_OF_INDEXES;
90 static HMODULE WININET_hModule;
92 #define HANDLE_CHUNK_SIZE 0x10
94 static CRITICAL_SECTION WININET_cs;
95 static CRITICAL_SECTION_DEBUG WININET_cs_debug =
97 0, 0, &WININET_cs,
98 { &WININET_cs_debug.ProcessLocksList, &WININET_cs_debug.ProcessLocksList },
99 0, 0, { (DWORD_PTR)(__FILE__ ": WININET_cs") }
101 static CRITICAL_SECTION WININET_cs = { &WININET_cs_debug, -1, 0, 0, 0, 0 };
103 static LPWININETHANDLEHEADER *WININET_Handles;
104 static UINT WININET_dwNextHandle;
105 static UINT WININET_dwMaxHandles;
107 HINTERNET WININET_AllocHandle( LPWININETHANDLEHEADER info )
109 LPWININETHANDLEHEADER *p;
110 UINT handle = 0, num;
112 list_init( &info->children );
114 EnterCriticalSection( &WININET_cs );
115 if( !WININET_dwMaxHandles )
117 num = HANDLE_CHUNK_SIZE;
118 p = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
119 sizeof (UINT)* num);
120 if( !p )
121 goto end;
122 WININET_Handles = p;
123 WININET_dwMaxHandles = num;
125 if( WININET_dwMaxHandles == WININET_dwNextHandle )
127 num = WININET_dwMaxHandles + HANDLE_CHUNK_SIZE;
128 p = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
129 WININET_Handles, sizeof (UINT)* num);
130 if( !p )
131 goto end;
132 WININET_Handles = p;
133 WININET_dwMaxHandles = num;
136 handle = WININET_dwNextHandle;
137 if( WININET_Handles[handle] )
138 ERR("handle isn't free but should be\n");
139 WININET_Handles[handle] = WININET_AddRef( info );
141 while( WININET_Handles[WININET_dwNextHandle] &&
142 (WININET_dwNextHandle < WININET_dwMaxHandles ) )
143 WININET_dwNextHandle++;
145 end:
146 LeaveCriticalSection( &WININET_cs );
148 return info->hInternet = (HINTERNET) (handle+1);
151 LPWININETHANDLEHEADER WININET_AddRef( LPWININETHANDLEHEADER info )
153 ULONG refs = InterlockedIncrement(&info->refs);
154 TRACE("%p -> refcount = %d\n", info, refs );
155 return info;
158 LPWININETHANDLEHEADER WININET_GetObject( HINTERNET hinternet )
160 LPWININETHANDLEHEADER info = NULL;
161 UINT handle = (UINT) hinternet;
163 EnterCriticalSection( &WININET_cs );
165 if( (handle > 0) && ( handle <= WININET_dwMaxHandles ) &&
166 WININET_Handles[handle-1] )
167 info = WININET_AddRef( WININET_Handles[handle-1] );
169 LeaveCriticalSection( &WININET_cs );
171 TRACE("handle %d -> %p\n", handle, info);
173 return info;
176 BOOL WININET_Release( LPWININETHANDLEHEADER info )
178 ULONG refs = InterlockedDecrement(&info->refs);
179 TRACE( "object %p refcount = %d\n", info, refs );
180 if( !refs )
182 if ( info->vtbl->CloseConnection )
184 TRACE( "closing connection %p\n", info);
185 info->vtbl->CloseConnection( info );
187 INTERNET_SendCallback(info, info->dwContext,
188 INTERNET_STATUS_HANDLE_CLOSING, &info->hInternet,
189 sizeof(HINTERNET));
190 TRACE( "destroying object %p\n", info);
191 if ( info->htype != WH_HINIT )
192 list_remove( &info->entry );
193 info->vtbl->Destroy( info );
195 return TRUE;
198 BOOL WININET_FreeHandle( HINTERNET hinternet )
200 BOOL ret = FALSE;
201 UINT handle = (UINT) hinternet;
202 LPWININETHANDLEHEADER info = NULL, child, next;
204 EnterCriticalSection( &WININET_cs );
206 if( (handle > 0) && ( handle <= WININET_dwMaxHandles ) )
208 handle--;
209 if( WININET_Handles[handle] )
211 info = WININET_Handles[handle];
212 TRACE( "destroying handle %d for object %p\n", handle+1, info);
213 WININET_Handles[handle] = NULL;
214 ret = TRUE;
218 LeaveCriticalSection( &WININET_cs );
220 /* As on native when the equivalent of WININET_Release is called, the handle
221 * is already invalid, but if a new handle is created at this time it does
222 * not yet get assigned the freed handle number */
223 if( info )
225 /* Free all children as native does */
226 LIST_FOR_EACH_ENTRY_SAFE( child, next, &info->children, WININETHANDLEHEADER, entry )
228 TRACE( "freeing child handle %d for parent handle %d\n",
229 (UINT)child->hInternet, handle+1);
230 WININET_FreeHandle( child->hInternet );
232 WININET_Release( info );
235 EnterCriticalSection( &WININET_cs );
237 if( WININET_dwNextHandle > handle && !WININET_Handles[handle] )
238 WININET_dwNextHandle = handle;
240 LeaveCriticalSection( &WININET_cs );
242 return ret;
245 /***********************************************************************
246 * DllMain [Internal] Initializes the internal 'WININET.DLL'.
248 * PARAMS
249 * hinstDLL [I] handle to the DLL's instance
250 * fdwReason [I]
251 * lpvReserved [I] reserved, must be NULL
253 * RETURNS
254 * Success: TRUE
255 * Failure: FALSE
258 BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
260 TRACE("%p,%x,%p\n", hinstDLL, fdwReason, lpvReserved);
262 switch (fdwReason) {
263 case DLL_PROCESS_ATTACH:
265 g_dwTlsErrIndex = TlsAlloc();
267 if (g_dwTlsErrIndex == TLS_OUT_OF_INDEXES)
268 return FALSE;
270 URLCacheContainers_CreateDefaults();
272 WININET_hModule = hinstDLL;
274 case DLL_THREAD_ATTACH:
275 break;
277 case DLL_THREAD_DETACH:
278 if (g_dwTlsErrIndex != TLS_OUT_OF_INDEXES)
280 LPVOID lpwite = TlsGetValue(g_dwTlsErrIndex);
281 HeapFree(GetProcessHeap(), 0, lpwite);
283 break;
285 case DLL_PROCESS_DETACH:
287 URLCacheContainers_DeleteAll();
289 if (g_dwTlsErrIndex != TLS_OUT_OF_INDEXES)
291 HeapFree(GetProcessHeap(), 0, TlsGetValue(g_dwTlsErrIndex));
292 TlsFree(g_dwTlsErrIndex);
294 break;
297 return TRUE;
301 /***********************************************************************
302 * InternetInitializeAutoProxyDll (WININET.@)
304 * Setup the internal proxy
306 * PARAMETERS
307 * dwReserved
309 * RETURNS
310 * FALSE on failure
313 BOOL WINAPI InternetInitializeAutoProxyDll(DWORD dwReserved)
315 FIXME("STUB\n");
316 INTERNET_SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
317 return FALSE;
320 /***********************************************************************
321 * DetectAutoProxyUrl (WININET.@)
323 * Auto detect the proxy url
325 * RETURNS
326 * FALSE on failure
329 BOOL WINAPI DetectAutoProxyUrl(LPSTR lpszAutoProxyUrl,
330 DWORD dwAutoProxyUrlLength, DWORD dwDetectFlags)
332 FIXME("STUB\n");
333 INTERNET_SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
334 return FALSE;
338 /***********************************************************************
339 * INTERNET_ConfigureProxyFromReg
341 * FIXME:
342 * The proxy may be specified in the form 'http=proxy.my.org'
343 * Presumably that means there can be ftp=ftpproxy.my.org too.
345 static BOOL INTERNET_ConfigureProxyFromReg( LPWININETAPPINFOW lpwai )
347 HKEY key;
348 DWORD r, keytype, len, enabled;
349 LPCSTR lpszInternetSettings =
350 "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings";
351 static const WCHAR szProxyServer[] = { 'P','r','o','x','y','S','e','r','v','e','r', 0 };
353 r = RegOpenKeyA(HKEY_CURRENT_USER, lpszInternetSettings, &key);
354 if ( r != ERROR_SUCCESS )
355 return FALSE;
357 len = sizeof enabled;
358 r = RegQueryValueExA( key, "ProxyEnable", NULL, &keytype,
359 (BYTE*)&enabled, &len);
360 if( (r == ERROR_SUCCESS) && enabled )
362 TRACE("Proxy is enabled.\n");
364 /* figure out how much memory the proxy setting takes */
365 r = RegQueryValueExW( key, szProxyServer, NULL, &keytype,
366 NULL, &len);
367 if( (r == ERROR_SUCCESS) && len && (keytype == REG_SZ) )
369 LPWSTR szProxy, p;
370 static const WCHAR szHttp[] = {'h','t','t','p','=',0};
372 szProxy=HeapAlloc( GetProcessHeap(), 0, len );
373 RegQueryValueExW( key, szProxyServer, NULL, &keytype,
374 (BYTE*)szProxy, &len);
376 /* find the http proxy, and strip away everything else */
377 p = strstrW( szProxy, szHttp );
378 if( p )
380 p += lstrlenW(szHttp);
381 lstrcpyW( szProxy, p );
383 p = strchrW( szProxy, ' ' );
384 if( p )
385 *p = 0;
387 lpwai->dwAccessType = INTERNET_OPEN_TYPE_PROXY;
388 lpwai->lpszProxy = szProxy;
390 TRACE("http proxy = %s\n", debugstr_w(lpwai->lpszProxy));
392 else
393 ERR("Couldn't read proxy server settings.\n");
395 else
396 TRACE("Proxy is not enabled.\n");
397 RegCloseKey(key);
399 return enabled;
402 /***********************************************************************
403 * dump_INTERNET_FLAGS
405 * Helper function to TRACE the internet flags.
407 * RETURNS
408 * None
411 static void dump_INTERNET_FLAGS(DWORD dwFlags)
413 #define FE(x) { x, #x }
414 static const wininet_flag_info flag[] = {
415 FE(INTERNET_FLAG_RELOAD),
416 FE(INTERNET_FLAG_RAW_DATA),
417 FE(INTERNET_FLAG_EXISTING_CONNECT),
418 FE(INTERNET_FLAG_ASYNC),
419 FE(INTERNET_FLAG_PASSIVE),
420 FE(INTERNET_FLAG_NO_CACHE_WRITE),
421 FE(INTERNET_FLAG_MAKE_PERSISTENT),
422 FE(INTERNET_FLAG_FROM_CACHE),
423 FE(INTERNET_FLAG_SECURE),
424 FE(INTERNET_FLAG_KEEP_CONNECTION),
425 FE(INTERNET_FLAG_NO_AUTO_REDIRECT),
426 FE(INTERNET_FLAG_READ_PREFETCH),
427 FE(INTERNET_FLAG_NO_COOKIES),
428 FE(INTERNET_FLAG_NO_AUTH),
429 FE(INTERNET_FLAG_CACHE_IF_NET_FAIL),
430 FE(INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP),
431 FE(INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTPS),
432 FE(INTERNET_FLAG_IGNORE_CERT_DATE_INVALID),
433 FE(INTERNET_FLAG_IGNORE_CERT_CN_INVALID),
434 FE(INTERNET_FLAG_RESYNCHRONIZE),
435 FE(INTERNET_FLAG_HYPERLINK),
436 FE(INTERNET_FLAG_NO_UI),
437 FE(INTERNET_FLAG_PRAGMA_NOCACHE),
438 FE(INTERNET_FLAG_CACHE_ASYNC),
439 FE(INTERNET_FLAG_FORMS_SUBMIT),
440 FE(INTERNET_FLAG_NEED_FILE),
441 FE(INTERNET_FLAG_TRANSFER_ASCII),
442 FE(INTERNET_FLAG_TRANSFER_BINARY)
444 #undef FE
445 int i;
447 for (i = 0; i < (sizeof(flag) / sizeof(flag[0])); i++) {
448 if (flag[i].val & dwFlags) {
449 TRACE(" %s", flag[i].name);
450 dwFlags &= ~flag[i].val;
453 if (dwFlags)
454 TRACE(" Unknown flags (%08x)\n", dwFlags);
455 else
456 TRACE("\n");
459 /***********************************************************************
460 * INTERNET_CloseHandle (internal)
462 * Close internet handle
465 static VOID APPINFO_Destroy(WININETHANDLEHEADER *hdr)
467 LPWININETAPPINFOW lpwai = (LPWININETAPPINFOW) hdr;
469 TRACE("%p\n",lpwai);
471 HeapFree(GetProcessHeap(), 0, lpwai->lpszAgent);
472 HeapFree(GetProcessHeap(), 0, lpwai->lpszProxy);
473 HeapFree(GetProcessHeap(), 0, lpwai->lpszProxyBypass);
474 HeapFree(GetProcessHeap(), 0, lpwai->lpszProxyUsername);
475 HeapFree(GetProcessHeap(), 0, lpwai->lpszProxyPassword);
476 HeapFree(GetProcessHeap(), 0, lpwai);
479 static DWORD APPINFO_QueryOption(WININETHANDLEHEADER *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
481 switch(option) {
482 case INTERNET_OPTION_HANDLE_TYPE:
483 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
485 if (*size < sizeof(ULONG))
486 return ERROR_INSUFFICIENT_BUFFER;
488 *size = sizeof(DWORD);
489 *(DWORD*)buffer = INTERNET_HANDLE_TYPE_INTERNET;
490 return ERROR_SUCCESS;
493 FIXME("Not implemented option %d\n", option);
494 return ERROR_INTERNET_INVALID_OPTION;
497 static const HANDLEHEADERVtbl APPINFOVtbl = {
498 APPINFO_Destroy,
499 NULL,
500 APPINFO_QueryOption,
501 NULL,
502 NULL,
503 NULL,
504 NULL,
505 NULL,
506 NULL
510 /***********************************************************************
511 * InternetOpenW (WININET.@)
513 * Per-application initialization of wininet
515 * RETURNS
516 * HINTERNET on success
517 * NULL on failure
520 HINTERNET WINAPI InternetOpenW(LPCWSTR lpszAgent, DWORD dwAccessType,
521 LPCWSTR lpszProxy, LPCWSTR lpszProxyBypass, DWORD dwFlags)
523 LPWININETAPPINFOW lpwai = NULL;
524 HINTERNET handle = NULL;
526 if (TRACE_ON(wininet)) {
527 #define FE(x) { x, #x }
528 static const wininet_flag_info access_type[] = {
529 FE(INTERNET_OPEN_TYPE_PRECONFIG),
530 FE(INTERNET_OPEN_TYPE_DIRECT),
531 FE(INTERNET_OPEN_TYPE_PROXY),
532 FE(INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY)
534 #undef FE
535 DWORD i;
536 const char *access_type_str = "Unknown";
538 TRACE("(%s, %i, %s, %s, %i)\n", debugstr_w(lpszAgent), dwAccessType,
539 debugstr_w(lpszProxy), debugstr_w(lpszProxyBypass), dwFlags);
540 for (i = 0; i < (sizeof(access_type) / sizeof(access_type[0])); i++) {
541 if (access_type[i].val == dwAccessType) {
542 access_type_str = access_type[i].name;
543 break;
546 TRACE(" access type : %s\n", access_type_str);
547 TRACE(" flags :");
548 dump_INTERNET_FLAGS(dwFlags);
551 /* Clear any error information */
552 INTERNET_SetLastError(0);
554 lpwai = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETAPPINFOW));
555 if (NULL == lpwai)
557 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
558 goto lend;
561 lpwai->hdr.htype = WH_HINIT;
562 lpwai->hdr.vtbl = &APPINFOVtbl;
563 lpwai->hdr.dwFlags = dwFlags;
564 lpwai->hdr.refs = 1;
565 lpwai->dwAccessType = dwAccessType;
566 lpwai->lpszProxyUsername = NULL;
567 lpwai->lpszProxyPassword = NULL;
569 handle = WININET_AllocHandle( &lpwai->hdr );
570 if( !handle )
572 HeapFree( GetProcessHeap(), 0, lpwai );
573 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
574 goto lend;
577 if (NULL != lpszAgent)
579 lpwai->lpszAgent = HeapAlloc( GetProcessHeap(),0,
580 (strlenW(lpszAgent)+1)*sizeof(WCHAR));
581 if (lpwai->lpszAgent)
582 lstrcpyW( lpwai->lpszAgent, lpszAgent );
584 if(dwAccessType == INTERNET_OPEN_TYPE_PRECONFIG)
585 INTERNET_ConfigureProxyFromReg( lpwai );
586 else if (NULL != lpszProxy)
588 lpwai->lpszProxy = HeapAlloc( GetProcessHeap(), 0,
589 (strlenW(lpszProxy)+1)*sizeof(WCHAR));
590 if (lpwai->lpszProxy)
591 lstrcpyW( lpwai->lpszProxy, lpszProxy );
594 if (NULL != lpszProxyBypass)
596 lpwai->lpszProxyBypass = HeapAlloc( GetProcessHeap(), 0,
597 (strlenW(lpszProxyBypass)+1)*sizeof(WCHAR));
598 if (lpwai->lpszProxyBypass)
599 lstrcpyW( lpwai->lpszProxyBypass, lpszProxyBypass );
602 lend:
603 if( lpwai )
604 WININET_Release( &lpwai->hdr );
606 TRACE("returning %p\n", lpwai);
608 return handle;
612 /***********************************************************************
613 * InternetOpenA (WININET.@)
615 * Per-application initialization of wininet
617 * RETURNS
618 * HINTERNET on success
619 * NULL on failure
622 HINTERNET WINAPI InternetOpenA(LPCSTR lpszAgent, DWORD dwAccessType,
623 LPCSTR lpszProxy, LPCSTR lpszProxyBypass, DWORD dwFlags)
625 HINTERNET rc = NULL;
626 INT len;
627 WCHAR *szAgent = NULL, *szProxy = NULL, *szBypass = NULL;
629 TRACE("(%s, 0x%08x, %s, %s, 0x%08x)\n", debugstr_a(lpszAgent),
630 dwAccessType, debugstr_a(lpszProxy), debugstr_a(lpszProxyBypass), dwFlags);
632 if( lpszAgent )
634 len = MultiByteToWideChar(CP_ACP, 0, lpszAgent, -1, NULL, 0);
635 szAgent = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
636 MultiByteToWideChar(CP_ACP, 0, lpszAgent, -1, szAgent, len);
639 if( lpszProxy )
641 len = MultiByteToWideChar(CP_ACP, 0, lpszProxy, -1, NULL, 0);
642 szProxy = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
643 MultiByteToWideChar(CP_ACP, 0, lpszProxy, -1, szProxy, len);
646 if( lpszProxyBypass )
648 len = MultiByteToWideChar(CP_ACP, 0, lpszProxyBypass, -1, NULL, 0);
649 szBypass = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
650 MultiByteToWideChar(CP_ACP, 0, lpszProxyBypass, -1, szBypass, len);
653 rc = InternetOpenW(szAgent, dwAccessType, szProxy, szBypass, dwFlags);
655 HeapFree(GetProcessHeap(), 0, szAgent);
656 HeapFree(GetProcessHeap(), 0, szProxy);
657 HeapFree(GetProcessHeap(), 0, szBypass);
659 return rc;
662 /***********************************************************************
663 * InternetGetLastResponseInfoA (WININET.@)
665 * Return last wininet error description on the calling thread
667 * RETURNS
668 * TRUE on success of writing to buffer
669 * FALSE on failure
672 BOOL WINAPI InternetGetLastResponseInfoA(LPDWORD lpdwError,
673 LPSTR lpszBuffer, LPDWORD lpdwBufferLength)
675 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
677 TRACE("\n");
679 if (lpwite)
681 *lpdwError = lpwite->dwError;
682 if (lpwite->dwError)
684 memcpy(lpszBuffer, lpwite->response, *lpdwBufferLength);
685 *lpdwBufferLength = strlen(lpszBuffer);
687 else
688 *lpdwBufferLength = 0;
690 else
692 *lpdwError = 0;
693 *lpdwBufferLength = 0;
696 return TRUE;
699 /***********************************************************************
700 * InternetGetLastResponseInfoW (WININET.@)
702 * Return last wininet error description on the calling thread
704 * RETURNS
705 * TRUE on success of writing to buffer
706 * FALSE on failure
709 BOOL WINAPI InternetGetLastResponseInfoW(LPDWORD lpdwError,
710 LPWSTR lpszBuffer, LPDWORD lpdwBufferLength)
712 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
714 TRACE("\n");
716 if (lpwite)
718 *lpdwError = lpwite->dwError;
719 if (lpwite->dwError)
721 memcpy(lpszBuffer, lpwite->response, *lpdwBufferLength);
722 *lpdwBufferLength = lstrlenW(lpszBuffer);
724 else
725 *lpdwBufferLength = 0;
727 else
729 *lpdwError = 0;
730 *lpdwBufferLength = 0;
733 return TRUE;
736 /***********************************************************************
737 * InternetGetConnectedState (WININET.@)
739 * Return connected state
741 * RETURNS
742 * TRUE if connected
743 * if lpdwStatus is not null, return the status (off line,
744 * modem, lan...) in it.
745 * FALSE if not connected
747 BOOL WINAPI InternetGetConnectedState(LPDWORD lpdwStatus, DWORD dwReserved)
749 TRACE("(%p, 0x%08x)\n", lpdwStatus, dwReserved);
751 if (lpdwStatus) {
752 FIXME("always returning LAN connection.\n");
753 *lpdwStatus = INTERNET_CONNECTION_LAN;
755 return TRUE;
759 /***********************************************************************
760 * InternetGetConnectedStateExW (WININET.@)
762 * Return connected state
764 * PARAMS
766 * lpdwStatus [O] Flags specifying the status of the internet connection.
767 * lpszConnectionName [O] Pointer to buffer to receive the friendly name of the internet connection.
768 * dwNameLen [I] Size of the buffer, in characters.
769 * dwReserved [I] Reserved. Must be set to 0.
771 * RETURNS
772 * TRUE if connected
773 * if lpdwStatus is not null, return the status (off line,
774 * modem, lan...) in it.
775 * FALSE if not connected
777 * NOTES
778 * If the system has no available network connections, an empty string is
779 * stored in lpszConnectionName. If there is a LAN connection, a localized
780 * "LAN Connection" string is stored. Presumably, if only a dial-up
781 * connection is available then the name of the dial-up connection is
782 * returned. Why any application, other than the "Internet Settings" CPL,
783 * would want to use this function instead of the simpler InternetGetConnectedStateW
784 * function is beyond me.
786 BOOL WINAPI InternetGetConnectedStateExW(LPDWORD lpdwStatus, LPWSTR lpszConnectionName,
787 DWORD dwNameLen, DWORD dwReserved)
789 TRACE("(%p, %p, %d, 0x%08x)\n", lpdwStatus, lpszConnectionName, dwNameLen, dwReserved);
791 /* Must be zero */
792 if(dwReserved)
793 return FALSE;
795 if (lpdwStatus) {
796 FIXME("always returning LAN connection.\n");
797 *lpdwStatus = INTERNET_CONNECTION_LAN;
799 return LoadStringW(WININET_hModule, IDS_LANCONNECTION, lpszConnectionName, dwNameLen);
803 /***********************************************************************
804 * InternetGetConnectedStateExA (WININET.@)
806 BOOL WINAPI InternetGetConnectedStateExA(LPDWORD lpdwStatus, LPSTR lpszConnectionName,
807 DWORD dwNameLen, DWORD dwReserved)
809 LPWSTR lpwszConnectionName = NULL;
810 BOOL rc;
812 TRACE("(%p, %p, %d, 0x%08x)\n", lpdwStatus, lpszConnectionName, dwNameLen, dwReserved);
814 if (lpszConnectionName && dwNameLen > 0)
815 lpwszConnectionName= HeapAlloc(GetProcessHeap(), 0, dwNameLen * sizeof(WCHAR));
817 rc = InternetGetConnectedStateExW(lpdwStatus,lpwszConnectionName, dwNameLen,
818 dwReserved);
819 if (rc && lpwszConnectionName)
821 WideCharToMultiByte(CP_ACP,0,lpwszConnectionName,-1,lpszConnectionName,
822 dwNameLen, NULL, NULL);
824 HeapFree(GetProcessHeap(),0,lpwszConnectionName);
827 return rc;
831 /***********************************************************************
832 * InternetConnectW (WININET.@)
834 * Open a ftp, gopher or http session
836 * RETURNS
837 * HINTERNET a session handle on success
838 * NULL on failure
841 HINTERNET WINAPI InternetConnectW(HINTERNET hInternet,
842 LPCWSTR lpszServerName, INTERNET_PORT nServerPort,
843 LPCWSTR lpszUserName, LPCWSTR lpszPassword,
844 DWORD dwService, DWORD dwFlags, DWORD_PTR dwContext)
846 LPWININETAPPINFOW hIC;
847 HINTERNET rc = NULL;
849 TRACE("(%p, %s, %i, %s, %s, %i, %i, %lx)\n", hInternet, debugstr_w(lpszServerName),
850 nServerPort, debugstr_w(lpszUserName), debugstr_w(lpszPassword),
851 dwService, dwFlags, dwContext);
853 if (!lpszServerName)
855 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
856 return NULL;
859 /* Clear any error information */
860 INTERNET_SetLastError(0);
861 hIC = (LPWININETAPPINFOW) WININET_GetObject( hInternet );
862 if ( (hIC == NULL) || (hIC->hdr.htype != WH_HINIT) )
864 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
865 goto lend;
868 switch (dwService)
870 case INTERNET_SERVICE_FTP:
871 rc = FTP_Connect(hIC, lpszServerName, nServerPort,
872 lpszUserName, lpszPassword, dwFlags, dwContext, 0);
873 break;
875 case INTERNET_SERVICE_HTTP:
876 rc = HTTP_Connect(hIC, lpszServerName, nServerPort,
877 lpszUserName, lpszPassword, dwFlags, dwContext, 0);
878 break;
880 case INTERNET_SERVICE_GOPHER:
881 default:
882 break;
884 lend:
885 if( hIC )
886 WININET_Release( &hIC->hdr );
888 TRACE("returning %p\n", rc);
889 return rc;
893 /***********************************************************************
894 * InternetConnectA (WININET.@)
896 * Open a ftp, gopher or http session
898 * RETURNS
899 * HINTERNET a session handle on success
900 * NULL on failure
903 HINTERNET WINAPI InternetConnectA(HINTERNET hInternet,
904 LPCSTR lpszServerName, INTERNET_PORT nServerPort,
905 LPCSTR lpszUserName, LPCSTR lpszPassword,
906 DWORD dwService, DWORD dwFlags, DWORD_PTR dwContext)
908 HINTERNET rc = NULL;
909 INT len = 0;
910 LPWSTR szServerName = NULL;
911 LPWSTR szUserName = NULL;
912 LPWSTR szPassword = NULL;
914 if (lpszServerName)
916 len = MultiByteToWideChar(CP_ACP, 0, lpszServerName, -1, NULL, 0);
917 szServerName = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
918 MultiByteToWideChar(CP_ACP, 0, lpszServerName, -1, szServerName, len);
920 if (lpszUserName)
922 len = MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, NULL, 0);
923 szUserName = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
924 MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, szUserName, len);
926 if (lpszPassword)
928 len = MultiByteToWideChar(CP_ACP, 0, lpszPassword, -1, NULL, 0);
929 szPassword = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
930 MultiByteToWideChar(CP_ACP, 0, lpszPassword, -1, szPassword, len);
934 rc = InternetConnectW(hInternet, szServerName, nServerPort,
935 szUserName, szPassword, dwService, dwFlags, dwContext);
937 HeapFree(GetProcessHeap(), 0, szServerName);
938 HeapFree(GetProcessHeap(), 0, szUserName);
939 HeapFree(GetProcessHeap(), 0, szPassword);
940 return rc;
944 /***********************************************************************
945 * InternetFindNextFileA (WININET.@)
947 * Continues a file search from a previous call to FindFirstFile
949 * RETURNS
950 * TRUE on success
951 * FALSE on failure
954 BOOL WINAPI InternetFindNextFileA(HINTERNET hFind, LPVOID lpvFindData)
956 BOOL ret;
957 WIN32_FIND_DATAW fd;
959 ret = InternetFindNextFileW(hFind, lpvFindData?&fd:NULL);
960 if(lpvFindData)
961 WININET_find_data_WtoA(&fd, (LPWIN32_FIND_DATAA)lpvFindData);
962 return ret;
965 /***********************************************************************
966 * InternetFindNextFileW (WININET.@)
968 * Continues a file search from a previous call to FindFirstFile
970 * RETURNS
971 * TRUE on success
972 * FALSE on failure
975 BOOL WINAPI InternetFindNextFileW(HINTERNET hFind, LPVOID lpvFindData)
977 WININETHANDLEHEADER *hdr;
978 DWORD res;
980 TRACE("\n");
982 hdr = WININET_GetObject(hFind);
983 if(!hdr) {
984 WARN("Invalid handle\n");
985 SetLastError(ERROR_INVALID_HANDLE);
986 return FALSE;
989 if(hdr->vtbl->FindNextFileW) {
990 res = hdr->vtbl->FindNextFileW(hdr, lpvFindData);
991 }else {
992 WARN("Handle doesn't support NextFile\n");
993 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
996 WININET_Release(hdr);
998 if(res != ERROR_SUCCESS)
999 SetLastError(res);
1000 return res == ERROR_SUCCESS;
1003 /***********************************************************************
1004 * InternetCloseHandle (WININET.@)
1006 * Generic close handle function
1008 * RETURNS
1009 * TRUE on success
1010 * FALSE on failure
1013 BOOL WINAPI InternetCloseHandle(HINTERNET hInternet)
1015 LPWININETHANDLEHEADER lpwh;
1017 TRACE("%p\n",hInternet);
1019 lpwh = WININET_GetObject( hInternet );
1020 if (NULL == lpwh)
1022 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
1023 return FALSE;
1026 WININET_Release( lpwh );
1027 WININET_FreeHandle( hInternet );
1029 return TRUE;
1033 /***********************************************************************
1034 * ConvertUrlComponentValue (Internal)
1036 * Helper function for InternetCrackUrlW
1039 static void ConvertUrlComponentValue(LPSTR* lppszComponent, LPDWORD dwComponentLen,
1040 LPWSTR lpwszComponent, DWORD dwwComponentLen,
1041 LPCSTR lpszStart, LPCWSTR lpwszStart)
1043 TRACE("%p %d %p %d %p %p\n", lppszComponent, *dwComponentLen, lpwszComponent, dwwComponentLen, lpszStart, lpwszStart);
1044 if (*dwComponentLen != 0)
1046 DWORD nASCIILength=WideCharToMultiByte(CP_ACP,0,lpwszComponent,dwwComponentLen,NULL,0,NULL,NULL);
1047 if (*lppszComponent == NULL)
1049 int nASCIIOffset=WideCharToMultiByte(CP_ACP,0,lpwszStart,lpwszComponent-lpwszStart,NULL,0,NULL,NULL);
1050 if (lpwszComponent)
1051 *lppszComponent = (LPSTR)lpszStart+nASCIIOffset;
1052 else
1053 *lppszComponent = NULL;
1054 *dwComponentLen = nASCIILength;
1056 else
1058 DWORD ncpylen = min((*dwComponentLen)-1, nASCIILength);
1059 WideCharToMultiByte(CP_ACP,0,lpwszComponent,dwwComponentLen,*lppszComponent,ncpylen+1,NULL,NULL);
1060 (*lppszComponent)[ncpylen]=0;
1061 *dwComponentLen = ncpylen;
1067 /***********************************************************************
1068 * InternetCrackUrlA (WININET.@)
1070 * See InternetCrackUrlW.
1072 BOOL WINAPI InternetCrackUrlA(LPCSTR lpszUrl, DWORD dwUrlLength, DWORD dwFlags,
1073 LPURL_COMPONENTSA lpUrlComponents)
1075 DWORD nLength;
1076 URL_COMPONENTSW UCW;
1077 WCHAR* lpwszUrl;
1079 TRACE("(%s %u %x %p)\n", debugstr_a(lpszUrl), dwUrlLength, dwFlags, lpUrlComponents);
1081 if (!lpszUrl || !*lpszUrl || !lpUrlComponents ||
1082 lpUrlComponents->dwStructSize != sizeof(URL_COMPONENTSA))
1084 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1085 return FALSE;
1088 if(dwUrlLength<=0)
1089 dwUrlLength=-1;
1090 nLength=MultiByteToWideChar(CP_ACP,0,lpszUrl,dwUrlLength,NULL,0);
1092 /* if dwUrlLength=-1 then nLength includes null but length to
1093 InternetCrackUrlW should not include it */
1094 if (dwUrlLength == -1) nLength--;
1096 lpwszUrl=HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WCHAR)*nLength);
1097 MultiByteToWideChar(CP_ACP,0,lpszUrl,dwUrlLength,lpwszUrl,nLength);
1099 memset(&UCW,0,sizeof(UCW));
1100 UCW.dwStructSize = sizeof(URL_COMPONENTSW);
1101 if(lpUrlComponents->dwHostNameLength!=0)
1102 UCW.dwHostNameLength= lpUrlComponents->dwHostNameLength;
1103 if(lpUrlComponents->dwUserNameLength!=0)
1104 UCW.dwUserNameLength=lpUrlComponents->dwUserNameLength;
1105 if(lpUrlComponents->dwPasswordLength!=0)
1106 UCW.dwPasswordLength=lpUrlComponents->dwPasswordLength;
1107 if(lpUrlComponents->dwUrlPathLength!=0)
1108 UCW.dwUrlPathLength=lpUrlComponents->dwUrlPathLength;
1109 if(lpUrlComponents->dwSchemeLength!=0)
1110 UCW.dwSchemeLength=lpUrlComponents->dwSchemeLength;
1111 if(lpUrlComponents->dwExtraInfoLength!=0)
1112 UCW.dwExtraInfoLength=lpUrlComponents->dwExtraInfoLength;
1113 if(!InternetCrackUrlW(lpwszUrl,nLength,dwFlags,&UCW))
1115 HeapFree(GetProcessHeap(), 0, lpwszUrl);
1116 return FALSE;
1119 ConvertUrlComponentValue(&lpUrlComponents->lpszHostName, &lpUrlComponents->dwHostNameLength,
1120 UCW.lpszHostName, UCW.dwHostNameLength,
1121 lpszUrl, lpwszUrl);
1122 ConvertUrlComponentValue(&lpUrlComponents->lpszUserName, &lpUrlComponents->dwUserNameLength,
1123 UCW.lpszUserName, UCW.dwUserNameLength,
1124 lpszUrl, lpwszUrl);
1125 ConvertUrlComponentValue(&lpUrlComponents->lpszPassword, &lpUrlComponents->dwPasswordLength,
1126 UCW.lpszPassword, UCW.dwPasswordLength,
1127 lpszUrl, lpwszUrl);
1128 ConvertUrlComponentValue(&lpUrlComponents->lpszUrlPath, &lpUrlComponents->dwUrlPathLength,
1129 UCW.lpszUrlPath, UCW.dwUrlPathLength,
1130 lpszUrl, lpwszUrl);
1131 ConvertUrlComponentValue(&lpUrlComponents->lpszScheme, &lpUrlComponents->dwSchemeLength,
1132 UCW.lpszScheme, UCW.dwSchemeLength,
1133 lpszUrl, lpwszUrl);
1134 ConvertUrlComponentValue(&lpUrlComponents->lpszExtraInfo, &lpUrlComponents->dwExtraInfoLength,
1135 UCW.lpszExtraInfo, UCW.dwExtraInfoLength,
1136 lpszUrl, lpwszUrl);
1137 lpUrlComponents->nScheme=UCW.nScheme;
1138 lpUrlComponents->nPort=UCW.nPort;
1139 HeapFree(GetProcessHeap(), 0, lpwszUrl);
1141 TRACE("%s: scheme(%s) host(%s) path(%s) extra(%s)\n", lpszUrl,
1142 debugstr_an(lpUrlComponents->lpszScheme,lpUrlComponents->dwSchemeLength),
1143 debugstr_an(lpUrlComponents->lpszHostName,lpUrlComponents->dwHostNameLength),
1144 debugstr_an(lpUrlComponents->lpszUrlPath,lpUrlComponents->dwUrlPathLength),
1145 debugstr_an(lpUrlComponents->lpszExtraInfo,lpUrlComponents->dwExtraInfoLength));
1147 return TRUE;
1150 static const WCHAR url_schemes[][7] =
1152 {'f','t','p',0},
1153 {'g','o','p','h','e','r',0},
1154 {'h','t','t','p',0},
1155 {'h','t','t','p','s',0},
1156 {'f','i','l','e',0},
1157 {'n','e','w','s',0},
1158 {'m','a','i','l','t','o',0},
1159 {'r','e','s',0},
1162 /***********************************************************************
1163 * GetInternetSchemeW (internal)
1165 * Get scheme of url
1167 * RETURNS
1168 * scheme on success
1169 * INTERNET_SCHEME_UNKNOWN on failure
1172 static INTERNET_SCHEME GetInternetSchemeW(LPCWSTR lpszScheme, DWORD nMaxCmp)
1174 int i;
1176 TRACE("%s %d\n",debugstr_wn(lpszScheme, nMaxCmp), nMaxCmp);
1178 if(lpszScheme==NULL)
1179 return INTERNET_SCHEME_UNKNOWN;
1181 for (i = 0; i < sizeof(url_schemes)/sizeof(url_schemes[0]); i++)
1182 if (!strncmpW(lpszScheme, url_schemes[i], nMaxCmp))
1183 return INTERNET_SCHEME_FIRST + i;
1185 return INTERNET_SCHEME_UNKNOWN;
1188 /***********************************************************************
1189 * SetUrlComponentValueW (Internal)
1191 * Helper function for InternetCrackUrlW
1193 * PARAMS
1194 * lppszComponent [O] Holds the returned string
1195 * dwComponentLen [I] Holds the size of lppszComponent
1196 * [O] Holds the length of the string in lppszComponent without '\0'
1197 * lpszStart [I] Holds the string to copy from
1198 * len [I] Holds the length of lpszStart without '\0'
1200 * RETURNS
1201 * TRUE on success
1202 * FALSE on failure
1205 static BOOL SetUrlComponentValueW(LPWSTR* lppszComponent, LPDWORD dwComponentLen, LPCWSTR lpszStart, DWORD len)
1207 TRACE("%s (%d)\n", debugstr_wn(lpszStart,len), len);
1209 if ( (*dwComponentLen == 0) && (*lppszComponent == NULL) )
1210 return FALSE;
1212 if (*dwComponentLen != 0 || *lppszComponent == NULL)
1214 if (*lppszComponent == NULL)
1216 *lppszComponent = (LPWSTR)lpszStart;
1217 *dwComponentLen = len;
1219 else
1221 DWORD ncpylen = min((*dwComponentLen)-1, len);
1222 memcpy(*lppszComponent, lpszStart, ncpylen*sizeof(WCHAR));
1223 (*lppszComponent)[ncpylen] = '\0';
1224 *dwComponentLen = ncpylen;
1228 return TRUE;
1231 /***********************************************************************
1232 * InternetCrackUrlW (WININET.@)
1234 * Break up URL into its components
1236 * RETURNS
1237 * TRUE on success
1238 * FALSE on failure
1240 BOOL WINAPI InternetCrackUrlW(LPCWSTR lpszUrl_orig, DWORD dwUrlLength_orig, DWORD dwFlags,
1241 LPURL_COMPONENTSW lpUC)
1244 * RFC 1808
1245 * <protocol>:[//<net_loc>][/path][;<params>][?<query>][#<fragment>]
1248 LPCWSTR lpszParam = NULL;
1249 BOOL bIsAbsolute = FALSE;
1250 LPCWSTR lpszap, lpszUrl = lpszUrl_orig;
1251 LPCWSTR lpszcp = NULL;
1252 LPWSTR lpszUrl_decode = NULL;
1253 DWORD dwUrlLength = dwUrlLength_orig;
1254 const WCHAR lpszSeparators[3]={';','?',0};
1255 const WCHAR lpszSlash[2]={'/',0};
1257 TRACE("(%s %u %x %p)\n", debugstr_w(lpszUrl), dwUrlLength, dwFlags, lpUC);
1259 if (!lpszUrl_orig || !*lpszUrl_orig || !lpUC)
1261 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1262 return FALSE;
1264 if (!dwUrlLength) dwUrlLength = strlenW(lpszUrl);
1266 if (dwFlags & ICU_DECODE)
1268 lpszUrl_decode=HeapAlloc( GetProcessHeap(), 0, dwUrlLength * sizeof (WCHAR) );
1269 if( InternetCanonicalizeUrlW(lpszUrl_orig, lpszUrl_decode, &dwUrlLength, dwFlags))
1271 lpszUrl = lpszUrl_decode;
1274 lpszap = lpszUrl;
1276 /* Determine if the URI is absolute. */
1277 while (*lpszap != '\0')
1279 if (isalnumW(*lpszap))
1281 lpszap++;
1282 continue;
1284 if ((*lpszap == ':') && (lpszap - lpszUrl >= 2))
1286 bIsAbsolute = TRUE;
1287 lpszcp = lpszap;
1289 else
1291 lpszcp = lpszUrl; /* Relative url */
1294 break;
1297 lpUC->nScheme = INTERNET_SCHEME_UNKNOWN;
1298 lpUC->nPort = INTERNET_INVALID_PORT_NUMBER;
1300 /* Parse <params> */
1301 lpszParam = strpbrkW(lpszap, lpszSeparators);
1302 SetUrlComponentValueW(&lpUC->lpszExtraInfo, &lpUC->dwExtraInfoLength,
1303 lpszParam, lpszParam ? dwUrlLength-(lpszParam-lpszUrl) : 0);
1305 if (bIsAbsolute) /* Parse <protocol>:[//<net_loc>] */
1307 LPCWSTR lpszNetLoc;
1309 /* Get scheme first. */
1310 lpUC->nScheme = GetInternetSchemeW(lpszUrl, lpszcp - lpszUrl);
1311 SetUrlComponentValueW(&lpUC->lpszScheme, &lpUC->dwSchemeLength,
1312 lpszUrl, lpszcp - lpszUrl);
1314 /* Eat ':' in protocol. */
1315 lpszcp++;
1317 /* double slash indicates the net_loc portion is present */
1318 if ((lpszcp[0] == '/') && (lpszcp[1] == '/'))
1320 lpszcp += 2;
1322 lpszNetLoc = strpbrkW(lpszcp, lpszSlash);
1323 if (lpszParam)
1325 if (lpszNetLoc)
1326 lpszNetLoc = min(lpszNetLoc, lpszParam);
1327 else
1328 lpszNetLoc = lpszParam;
1330 else if (!lpszNetLoc)
1331 lpszNetLoc = lpszcp + dwUrlLength-(lpszcp-lpszUrl);
1333 /* Parse net-loc */
1334 if (lpszNetLoc)
1336 LPCWSTR lpszHost;
1337 LPCWSTR lpszPort;
1339 /* [<user>[<:password>]@]<host>[:<port>] */
1340 /* First find the user and password if they exist */
1342 lpszHost = strchrW(lpszcp, '@');
1343 if (lpszHost == NULL || lpszHost > lpszNetLoc)
1345 /* username and password not specified. */
1346 SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength, NULL, 0);
1347 SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength, NULL, 0);
1349 else /* Parse out username and password */
1351 LPCWSTR lpszUser = lpszcp;
1352 LPCWSTR lpszPasswd = lpszHost;
1354 while (lpszcp < lpszHost)
1356 if (*lpszcp == ':')
1357 lpszPasswd = lpszcp;
1359 lpszcp++;
1362 SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength,
1363 lpszUser, lpszPasswd - lpszUser);
1365 if (lpszPasswd != lpszHost)
1366 lpszPasswd++;
1367 SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength,
1368 lpszPasswd == lpszHost ? NULL : lpszPasswd,
1369 lpszHost - lpszPasswd);
1371 lpszcp++; /* Advance to beginning of host */
1374 /* Parse <host><:port> */
1376 lpszHost = lpszcp;
1377 lpszPort = lpszNetLoc;
1379 /* special case for res:// URLs: there is no port here, so the host is the
1380 entire string up to the first '/' */
1381 if(lpUC->nScheme==INTERNET_SCHEME_RES)
1383 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength,
1384 lpszHost, lpszPort - lpszHost);
1385 lpszcp=lpszNetLoc;
1387 else
1389 while (lpszcp < lpszNetLoc)
1391 if (*lpszcp == ':')
1392 lpszPort = lpszcp;
1394 lpszcp++;
1397 /* If the scheme is "file" and the host is just one letter, it's not a host */
1398 if(lpUC->nScheme==INTERNET_SCHEME_FILE && (lpszPort-lpszHost)==1)
1400 lpszcp=lpszHost;
1401 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength,
1402 NULL, 0);
1404 else
1406 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength,
1407 lpszHost, lpszPort - lpszHost);
1408 if (lpszPort != lpszNetLoc)
1409 lpUC->nPort = atoiW(++lpszPort);
1410 else switch (lpUC->nScheme)
1412 case INTERNET_SCHEME_HTTP:
1413 lpUC->nPort = INTERNET_DEFAULT_HTTP_PORT;
1414 break;
1415 case INTERNET_SCHEME_HTTPS:
1416 lpUC->nPort = INTERNET_DEFAULT_HTTPS_PORT;
1417 break;
1418 case INTERNET_SCHEME_FTP:
1419 lpUC->nPort = INTERNET_DEFAULT_FTP_PORT;
1420 break;
1421 case INTERNET_SCHEME_GOPHER:
1422 lpUC->nPort = INTERNET_DEFAULT_GOPHER_PORT;
1423 break;
1424 default:
1425 break;
1431 else
1433 SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength, NULL, 0);
1434 SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength, NULL, 0);
1435 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength, NULL, 0);
1438 else
1440 SetUrlComponentValueW(&lpUC->lpszScheme, &lpUC->dwSchemeLength, NULL, 0);
1441 SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength, NULL, 0);
1442 SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength, NULL, 0);
1443 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength, NULL, 0);
1446 /* Here lpszcp points to:
1448 * <protocol>:[//<net_loc>][/path][;<params>][?<query>][#<fragment>]
1449 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1451 if (lpszcp != 0 && *lpszcp != '\0' && (!lpszParam || lpszcp < lpszParam))
1453 INT len;
1455 /* Only truncate the parameter list if it's already been saved
1456 * in lpUC->lpszExtraInfo.
1458 if (lpszParam && lpUC->dwExtraInfoLength && lpUC->lpszExtraInfo)
1459 len = lpszParam - lpszcp;
1460 else
1462 /* Leave the parameter list in lpszUrlPath. Strip off any trailing
1463 * newlines if necessary.
1465 LPWSTR lpsznewline = strchrW(lpszcp, '\n');
1466 if (lpsznewline != NULL)
1467 len = lpsznewline - lpszcp;
1468 else
1469 len = dwUrlLength-(lpszcp-lpszUrl);
1471 SetUrlComponentValueW(&lpUC->lpszUrlPath, &lpUC->dwUrlPathLength,
1472 lpszcp, len);
1474 else
1476 lpUC->dwUrlPathLength = 0;
1479 TRACE("%s: scheme(%s) host(%s) path(%s) extra(%s)\n", debugstr_wn(lpszUrl,dwUrlLength),
1480 debugstr_wn(lpUC->lpszScheme,lpUC->dwSchemeLength),
1481 debugstr_wn(lpUC->lpszHostName,lpUC->dwHostNameLength),
1482 debugstr_wn(lpUC->lpszUrlPath,lpUC->dwUrlPathLength),
1483 debugstr_wn(lpUC->lpszExtraInfo,lpUC->dwExtraInfoLength));
1485 HeapFree(GetProcessHeap(), 0, lpszUrl_decode );
1486 return TRUE;
1489 /***********************************************************************
1490 * InternetAttemptConnect (WININET.@)
1492 * Attempt to make a connection to the internet
1494 * RETURNS
1495 * ERROR_SUCCESS on success
1496 * Error value on failure
1499 DWORD WINAPI InternetAttemptConnect(DWORD dwReserved)
1501 FIXME("Stub\n");
1502 return ERROR_SUCCESS;
1506 /***********************************************************************
1507 * InternetCanonicalizeUrlA (WININET.@)
1509 * Escape unsafe characters and spaces
1511 * RETURNS
1512 * TRUE on success
1513 * FALSE on failure
1516 BOOL WINAPI InternetCanonicalizeUrlA(LPCSTR lpszUrl, LPSTR lpszBuffer,
1517 LPDWORD lpdwBufferLength, DWORD dwFlags)
1519 HRESULT hr;
1520 DWORD dwURLFlags = URL_WININET_COMPATIBILITY | URL_ESCAPE_UNSAFE;
1522 TRACE("(%s, %p, %p, 0x%08x) bufferlength: %d\n", debugstr_a(lpszUrl), lpszBuffer,
1523 lpdwBufferLength, lpdwBufferLength ? *lpdwBufferLength : -1, dwFlags);
1525 if(dwFlags & ICU_DECODE)
1527 dwURLFlags |= URL_UNESCAPE;
1528 dwFlags &= ~ICU_DECODE;
1531 if(dwFlags & ICU_ESCAPE)
1533 dwURLFlags |= URL_UNESCAPE;
1534 dwFlags &= ~ICU_ESCAPE;
1537 if(dwFlags & ICU_BROWSER_MODE)
1539 dwURLFlags |= URL_BROWSER_MODE;
1540 dwFlags &= ~ICU_BROWSER_MODE;
1543 if(dwFlags & ICU_NO_ENCODE)
1545 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
1546 dwURLFlags ^= URL_ESCAPE_UNSAFE;
1547 dwFlags &= ~ICU_NO_ENCODE;
1550 if (dwFlags) FIXME("Unhandled flags 0x%08x\n", dwFlags);
1552 hr = UrlCanonicalizeA(lpszUrl, lpszBuffer, lpdwBufferLength, dwURLFlags);
1553 if (hr == E_POINTER) SetLastError(ERROR_INSUFFICIENT_BUFFER);
1554 if (hr == E_INVALIDARG) SetLastError(ERROR_INVALID_PARAMETER);
1556 return (hr == S_OK) ? TRUE : FALSE;
1559 /***********************************************************************
1560 * InternetCanonicalizeUrlW (WININET.@)
1562 * Escape unsafe characters and spaces
1564 * RETURNS
1565 * TRUE on success
1566 * FALSE on failure
1569 BOOL WINAPI InternetCanonicalizeUrlW(LPCWSTR lpszUrl, LPWSTR lpszBuffer,
1570 LPDWORD lpdwBufferLength, DWORD dwFlags)
1572 HRESULT hr;
1573 DWORD dwURLFlags = URL_WININET_COMPATIBILITY | URL_ESCAPE_UNSAFE;
1575 TRACE("(%s, %p, %p, 0x%08x) bufferlength: %d\n", debugstr_w(lpszUrl), lpszBuffer,
1576 lpdwBufferLength, lpdwBufferLength ? *lpdwBufferLength : -1, dwFlags);
1578 if(dwFlags & ICU_DECODE)
1580 dwURLFlags |= URL_UNESCAPE;
1581 dwFlags &= ~ICU_DECODE;
1584 if(dwFlags & ICU_ESCAPE)
1586 dwURLFlags |= URL_UNESCAPE;
1587 dwFlags &= ~ICU_ESCAPE;
1590 if(dwFlags & ICU_BROWSER_MODE)
1592 dwURLFlags |= URL_BROWSER_MODE;
1593 dwFlags &= ~ICU_BROWSER_MODE;
1596 if(dwFlags & ICU_NO_ENCODE)
1598 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
1599 dwURLFlags ^= URL_ESCAPE_UNSAFE;
1600 dwFlags &= ~ICU_NO_ENCODE;
1603 if (dwFlags) FIXME("Unhandled flags 0x%08x\n", dwFlags);
1605 hr = UrlCanonicalizeW(lpszUrl, lpszBuffer, lpdwBufferLength, dwURLFlags);
1606 if (hr == E_POINTER) SetLastError(ERROR_INSUFFICIENT_BUFFER);
1607 if (hr == E_INVALIDARG) SetLastError(ERROR_INVALID_PARAMETER);
1609 return (hr == S_OK) ? TRUE : FALSE;
1612 /* #################################################### */
1614 static INTERNET_STATUS_CALLBACK set_status_callback(
1615 LPWININETHANDLEHEADER lpwh, INTERNET_STATUS_CALLBACK callback, BOOL unicode)
1617 INTERNET_STATUS_CALLBACK ret;
1619 if (unicode) lpwh->dwInternalFlags |= INET_CALLBACKW;
1620 else lpwh->dwInternalFlags &= ~INET_CALLBACKW;
1622 ret = lpwh->lpfnStatusCB;
1623 lpwh->lpfnStatusCB = callback;
1625 return ret;
1628 /***********************************************************************
1629 * InternetSetStatusCallbackA (WININET.@)
1631 * Sets up a callback function which is called as progress is made
1632 * during an operation.
1634 * RETURNS
1635 * Previous callback or NULL on success
1636 * INTERNET_INVALID_STATUS_CALLBACK on failure
1639 INTERNET_STATUS_CALLBACK WINAPI InternetSetStatusCallbackA(
1640 HINTERNET hInternet ,INTERNET_STATUS_CALLBACK lpfnIntCB)
1642 INTERNET_STATUS_CALLBACK retVal;
1643 LPWININETHANDLEHEADER lpwh;
1645 TRACE("0x%08x\n", (ULONG)hInternet);
1647 if (!(lpwh = WININET_GetObject(hInternet)))
1648 return INTERNET_INVALID_STATUS_CALLBACK;
1650 retVal = set_status_callback(lpwh, lpfnIntCB, FALSE);
1652 WININET_Release( lpwh );
1653 return retVal;
1656 /***********************************************************************
1657 * InternetSetStatusCallbackW (WININET.@)
1659 * Sets up a callback function which is called as progress is made
1660 * during an operation.
1662 * RETURNS
1663 * Previous callback or NULL on success
1664 * INTERNET_INVALID_STATUS_CALLBACK on failure
1667 INTERNET_STATUS_CALLBACK WINAPI InternetSetStatusCallbackW(
1668 HINTERNET hInternet ,INTERNET_STATUS_CALLBACK lpfnIntCB)
1670 INTERNET_STATUS_CALLBACK retVal;
1671 LPWININETHANDLEHEADER lpwh;
1673 TRACE("0x%08x\n", (ULONG)hInternet);
1675 if (!(lpwh = WININET_GetObject(hInternet)))
1676 return INTERNET_INVALID_STATUS_CALLBACK;
1678 retVal = set_status_callback(lpwh, lpfnIntCB, TRUE);
1680 WININET_Release( lpwh );
1681 return retVal;
1684 /***********************************************************************
1685 * InternetSetFilePointer (WININET.@)
1687 DWORD WINAPI InternetSetFilePointer(HINTERNET hFile, LONG lDistanceToMove,
1688 PVOID pReserved, DWORD dwMoveContext, DWORD_PTR dwContext)
1690 FIXME("stub\n");
1691 return FALSE;
1694 /***********************************************************************
1695 * InternetWriteFile (WININET.@)
1697 * Write data to an open internet file
1699 * RETURNS
1700 * TRUE on success
1701 * FALSE on failure
1704 BOOL WINAPI InternetWriteFile(HINTERNET hFile, LPCVOID lpBuffer,
1705 DWORD dwNumOfBytesToWrite, LPDWORD lpdwNumOfBytesWritten)
1707 LPWININETHANDLEHEADER lpwh;
1708 BOOL retval = FALSE;
1710 TRACE("(%p %p %d %p)\n", hFile, lpBuffer, dwNumOfBytesToWrite, lpdwNumOfBytesWritten);
1712 lpwh = WININET_GetObject( hFile );
1713 if (!lpwh) {
1714 WARN("Invalid handle\n");
1715 SetLastError(ERROR_INVALID_HANDLE);
1716 return FALSE;
1719 if(lpwh->vtbl->WriteFile) {
1720 retval = lpwh->vtbl->WriteFile(lpwh, lpBuffer, dwNumOfBytesToWrite, lpdwNumOfBytesWritten);
1721 }else {
1722 WARN("No Writefile method.\n");
1723 SetLastError(ERROR_INVALID_HANDLE);
1724 retval = FALSE;
1727 WININET_Release( lpwh );
1729 return retval;
1733 /***********************************************************************
1734 * InternetReadFile (WININET.@)
1736 * Read data from an open internet file
1738 * RETURNS
1739 * TRUE on success
1740 * FALSE on failure
1743 BOOL WINAPI InternetReadFile(HINTERNET hFile, LPVOID lpBuffer,
1744 DWORD dwNumOfBytesToRead, LPDWORD pdwNumOfBytesRead)
1746 LPWININETHANDLEHEADER hdr;
1747 DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
1749 TRACE("%p %p %d %p\n", hFile, lpBuffer, dwNumOfBytesToRead, pdwNumOfBytesRead);
1751 hdr = WININET_GetObject(hFile);
1752 if (!hdr) {
1753 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
1754 return FALSE;
1757 if(hdr->vtbl->ReadFile)
1758 res = hdr->vtbl->ReadFile(hdr, lpBuffer, dwNumOfBytesToRead, pdwNumOfBytesRead);
1760 WININET_Release(hdr);
1762 TRACE("-- %s (%u) (bytes read: %d)\n", res == ERROR_SUCCESS ? "TRUE": "FALSE", res,
1763 pdwNumOfBytesRead ? *pdwNumOfBytesRead : -1);
1765 if(res != ERROR_SUCCESS)
1766 SetLastError(res);
1767 return res == ERROR_SUCCESS;
1770 /***********************************************************************
1771 * InternetReadFileExA (WININET.@)
1773 * Read data from an open internet file
1775 * PARAMS
1776 * hFile [I] Handle returned by InternetOpenUrl or HttpOpenRequest.
1777 * lpBuffersOut [I/O] Buffer.
1778 * dwFlags [I] Flags. See notes.
1779 * dwContext [I] Context for callbacks.
1781 * RETURNS
1782 * TRUE on success
1783 * FALSE on failure
1785 * NOTES
1786 * The parameter dwFlags include zero or more of the following flags:
1787 *|IRF_ASYNC - Makes the call asynchronous.
1788 *|IRF_SYNC - Makes the call synchronous.
1789 *|IRF_USE_CONTEXT - Forces dwContext to be used.
1790 *|IRF_NO_WAIT - Don't block if the data is not available, just return what is available.
1792 * However, in testing IRF_USE_CONTEXT seems to have no effect - dwContext isn't used.
1794 * SEE
1795 * InternetOpenUrlA(), HttpOpenRequestA()
1797 BOOL WINAPI InternetReadFileExA(HINTERNET hFile, LPINTERNET_BUFFERSA lpBuffersOut,
1798 DWORD dwFlags, DWORD_PTR dwContext)
1800 LPWININETHANDLEHEADER hdr;
1801 DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
1803 TRACE("(%p %p 0x%x 0x%lx)\n", hFile, lpBuffersOut, dwFlags, dwContext);
1805 hdr = WININET_GetObject(hFile);
1806 if (!hdr) {
1807 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
1808 return FALSE;
1811 if(hdr->vtbl->ReadFileExA)
1812 res = hdr->vtbl->ReadFileExA(hdr, lpBuffersOut, dwFlags, dwContext);
1814 WININET_Release(hdr);
1816 TRACE("-- %s (%u, bytes read: %d)\n", res == ERROR_SUCCESS ? "TRUE": "FALSE",
1817 res, lpBuffersOut->dwBufferLength);
1819 if(res != ERROR_SUCCESS)
1820 SetLastError(res);
1821 return res == ERROR_SUCCESS;
1824 /***********************************************************************
1825 * InternetReadFileExW (WININET.@)
1827 * Read data from an open internet file.
1829 * PARAMS
1830 * hFile [I] Handle returned by InternetOpenUrl() or HttpOpenRequest().
1831 * lpBuffersOut [I/O] Buffer.
1832 * dwFlags [I] Flags.
1833 * dwContext [I] Context for callbacks.
1835 * RETURNS
1836 * FALSE, last error is set to ERROR_CALL_NOT_IMPLEMENTED
1838 * NOTES
1839 * Not implemented in Wine or native either (as of IE6 SP2).
1842 BOOL WINAPI InternetReadFileExW(HINTERNET hFile, LPINTERNET_BUFFERSW lpBuffer,
1843 DWORD dwFlags, DWORD_PTR dwContext)
1845 ERR("(%p, %p, 0x%x, 0x%lx): not implemented in native\n", hFile, lpBuffer, dwFlags, dwContext);
1847 INTERNET_SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1848 return FALSE;
1851 /***********************************************************************
1852 * INET_QueryOptionHelper (internal)
1854 static BOOL INET_QueryOptionHelper(BOOL bIsUnicode, HINTERNET hInternet, DWORD dwOption,
1855 LPVOID lpBuffer, LPDWORD lpdwBufferLength)
1857 LPWININETHANDLEHEADER lpwhh;
1858 BOOL bSuccess = FALSE;
1860 TRACE("(%p, 0x%08x, %p, %p)\n", hInternet, dwOption, lpBuffer, lpdwBufferLength);
1862 lpwhh = WININET_GetObject( hInternet );
1864 switch (dwOption)
1866 case INTERNET_OPTION_REQUEST_FLAGS:
1868 ULONG flags = 4;
1869 TRACE("INTERNET_OPTION_REQUEST_FLAGS: %d\n", flags);
1870 if (*lpdwBufferLength < sizeof(ULONG))
1871 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1872 else
1874 memcpy(lpBuffer, &flags, sizeof(ULONG));
1875 bSuccess = TRUE;
1877 *lpdwBufferLength = sizeof(ULONG);
1878 break;
1881 case INTERNET_OPTION_USER_AGENT:
1882 FIXME("INTERNET_OPTION_USER_AGENT\n");
1883 break;
1885 case INTERNET_OPTION_HTTP_VERSION:
1887 if (*lpdwBufferLength < sizeof(HTTP_VERSION_INFO))
1888 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1889 else
1892 * Presently hardcoded to 1.1
1894 ((HTTP_VERSION_INFO*)lpBuffer)->dwMajorVersion = 1;
1895 ((HTTP_VERSION_INFO*)lpBuffer)->dwMinorVersion = 1;
1896 bSuccess = TRUE;
1898 *lpdwBufferLength = sizeof(HTTP_VERSION_INFO);
1899 break;
1901 case INTERNET_OPTION_CONNECTED_STATE:
1903 DWORD *pdwConnectedState = (DWORD *)lpBuffer;
1904 FIXME("INTERNET_OPTION_CONNECTED_STATE: semi-stub\n");
1906 if (*lpdwBufferLength < sizeof(*pdwConnectedState))
1907 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1908 else
1910 *pdwConnectedState = INTERNET_STATE_CONNECTED;
1911 bSuccess = TRUE;
1913 *lpdwBufferLength = sizeof(*pdwConnectedState);
1914 break;
1916 case INTERNET_OPTION_PROXY:
1918 LPWININETAPPINFOW lpwai = (LPWININETAPPINFOW)lpwhh;
1919 WININETAPPINFOW wai;
1921 if (lpwai == NULL)
1923 TRACE("Getting global proxy info\n");
1924 memset(&wai, 0, sizeof(WININETAPPINFOW));
1925 INTERNET_ConfigureProxyFromReg( &wai );
1926 lpwai = &wai;
1929 if (bIsUnicode)
1931 INTERNET_PROXY_INFOW *pPI = (INTERNET_PROXY_INFOW *)lpBuffer;
1932 DWORD proxyBytesRequired = 0, proxyBypassBytesRequired = 0;
1934 if (lpwai->lpszProxy)
1935 proxyBytesRequired = (lstrlenW(lpwai->lpszProxy) + 1) *
1936 sizeof(WCHAR);
1937 if (lpwai->lpszProxyBypass)
1938 proxyBypassBytesRequired =
1939 (lstrlenW(lpwai->lpszProxyBypass) + 1) * sizeof(WCHAR);
1940 if (*lpdwBufferLength < sizeof(INTERNET_PROXY_INFOW) +
1941 proxyBytesRequired + proxyBypassBytesRequired)
1942 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1943 else
1945 LPWSTR proxy = (LPWSTR)((LPBYTE)lpBuffer +
1946 sizeof(INTERNET_PROXY_INFOW));
1947 LPWSTR proxy_bypass = (LPWSTR)((LPBYTE)lpBuffer +
1948 sizeof(INTERNET_PROXY_INFOW) +
1949 proxyBytesRequired);
1951 pPI->dwAccessType = lpwai->dwAccessType;
1952 pPI->lpszProxy = NULL;
1953 pPI->lpszProxyBypass = NULL;
1954 if (lpwai->lpszProxy)
1956 lstrcpyW(proxy, lpwai->lpszProxy);
1957 pPI->lpszProxy = proxy;
1960 if (lpwai->lpszProxyBypass)
1962 lstrcpyW(proxy_bypass, lpwai->lpszProxyBypass);
1963 pPI->lpszProxyBypass = proxy_bypass;
1965 bSuccess = TRUE;
1967 *lpdwBufferLength = sizeof(INTERNET_PROXY_INFOW) +
1968 proxyBytesRequired + proxyBypassBytesRequired;
1970 else
1972 INTERNET_PROXY_INFOA *pPI = (INTERNET_PROXY_INFOA *)lpBuffer;
1973 DWORD proxyBytesRequired = 0, proxyBypassBytesRequired = 0;
1975 if (lpwai->lpszProxy)
1976 proxyBytesRequired = WideCharToMultiByte(CP_ACP, 0,
1977 lpwai->lpszProxy, -1, NULL, 0, NULL, NULL);
1978 if (lpwai->lpszProxyBypass)
1979 proxyBypassBytesRequired = WideCharToMultiByte(CP_ACP, 0,
1980 lpwai->lpszProxyBypass, -1, NULL, 0, NULL, NULL);
1981 if (*lpdwBufferLength < sizeof(INTERNET_PROXY_INFOA) +
1982 proxyBytesRequired + proxyBypassBytesRequired)
1983 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1984 else
1986 LPSTR proxy = (LPSTR)((LPBYTE)lpBuffer +
1987 sizeof(INTERNET_PROXY_INFOA));
1988 LPSTR proxy_bypass = (LPSTR)((LPBYTE)lpBuffer +
1989 sizeof(INTERNET_PROXY_INFOA) +
1990 proxyBytesRequired);
1992 pPI->dwAccessType = lpwai->dwAccessType;
1993 pPI->lpszProxy = NULL;
1994 pPI->lpszProxyBypass = NULL;
1995 if (lpwai->lpszProxy)
1997 WideCharToMultiByte(CP_ACP, 0, lpwai->lpszProxy, -1,
1998 proxy, proxyBytesRequired, NULL, NULL);
1999 pPI->lpszProxy = proxy;
2002 if (lpwai->lpszProxyBypass)
2004 WideCharToMultiByte(CP_ACP, 0, lpwai->lpszProxyBypass,
2005 -1, proxy_bypass, proxyBypassBytesRequired,
2006 NULL, NULL);
2007 pPI->lpszProxyBypass = proxy_bypass;
2009 bSuccess = TRUE;
2011 *lpdwBufferLength = sizeof(INTERNET_PROXY_INFOA) +
2012 proxyBytesRequired + proxyBypassBytesRequired;
2014 break;
2016 case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2018 ULONG conn = 2;
2019 TRACE("INTERNET_OPTION_MAX_CONNS_PER_SERVER: %d\n", conn);
2020 if (*lpdwBufferLength < sizeof(ULONG))
2021 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2022 else
2024 memcpy(lpBuffer, &conn, sizeof(ULONG));
2025 bSuccess = TRUE;
2027 *lpdwBufferLength = sizeof(ULONG);
2028 break;
2030 case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2032 ULONG conn = 4;
2033 TRACE("INTERNET_OPTION_MAX_CONNS_1_0_SERVER: %d\n", conn);
2034 if (*lpdwBufferLength < sizeof(ULONG))
2035 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2036 else
2038 memcpy(lpBuffer, &conn, sizeof(ULONG));
2039 bSuccess = TRUE;
2041 *lpdwBufferLength = sizeof(ULONG);
2042 break;
2044 case INTERNET_OPTION_SECURITY_FLAGS:
2045 FIXME("INTERNET_OPTION_SECURITY_FLAGS: Stub\n");
2046 bSuccess = TRUE;
2047 break;
2049 case INTERNET_OPTION_VERSION:
2051 TRACE("INTERNET_OPTION_VERSION\n");
2052 if (*lpdwBufferLength < sizeof(INTERNET_VERSION_INFO))
2053 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2054 else
2056 static const INTERNET_VERSION_INFO info = { 1, 2 };
2057 memcpy(lpBuffer, &info, sizeof(info));
2058 *lpdwBufferLength = sizeof(info);
2059 bSuccess = TRUE;
2061 break;
2063 case INTERNET_OPTION_PER_CONNECTION_OPTION:
2064 FIXME("INTERNET_OPTION_PER_CONNECTION_OPTION stub\n");
2065 if (*lpdwBufferLength < sizeof(INTERNET_PER_CONN_OPTION_LISTW))
2066 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2067 else
2069 INTERNET_PER_CONN_OPTION_LISTW *con = lpBuffer;
2070 int x;
2071 bSuccess = TRUE;
2072 for (x = 0; x < con->dwOptionCount; ++x)
2074 INTERNET_PER_CONN_OPTIONW *option = con->pOptions + x;
2075 switch (option->dwOption)
2077 case INTERNET_PER_CONN_FLAGS:
2078 option->Value.dwValue = PROXY_TYPE_DIRECT;
2079 break;
2081 case INTERNET_PER_CONN_PROXY_SERVER:
2082 case INTERNET_PER_CONN_PROXY_BYPASS:
2083 case INTERNET_PER_CONN_AUTOCONFIG_URL:
2084 case INTERNET_PER_CONN_AUTODISCOVERY_FLAGS:
2085 case INTERNET_PER_CONN_AUTOCONFIG_SECONDARY_URL:
2086 case INTERNET_PER_CONN_AUTOCONFIG_RELOAD_DELAY_MINS:
2087 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_TIME:
2088 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_URL:
2089 FIXME("Unhandled dwOption %d\n", option->dwOption);
2090 option->Value.dwValue = 0;
2091 bSuccess = FALSE;
2092 break;
2094 default:
2095 FIXME("Unknown dwOption %d\n", option->dwOption);
2096 bSuccess = FALSE;
2097 break;
2100 if (!bSuccess)
2101 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
2103 break;
2104 case 66:
2105 FIXME("66\n");
2106 bSuccess = TRUE;
2107 break;
2108 default: {
2109 if(lpwhh) {
2110 DWORD res;
2112 res = lpwhh->vtbl->QueryOption(lpwhh, dwOption, lpBuffer, lpdwBufferLength, bIsUnicode);
2113 if(res == ERROR_SUCCESS)
2114 bSuccess = TRUE;
2115 else
2116 SetLastError(res);
2117 }else {
2118 FIXME("Stub! %d\n", dwOption);
2119 break;
2123 if (lpwhh)
2124 WININET_Release( lpwhh );
2126 return bSuccess;
2129 /***********************************************************************
2130 * InternetQueryOptionW (WININET.@)
2132 * Queries an options on the specified handle
2134 * RETURNS
2135 * TRUE on success
2136 * FALSE on failure
2139 BOOL WINAPI InternetQueryOptionW(HINTERNET hInternet, DWORD dwOption,
2140 LPVOID lpBuffer, LPDWORD lpdwBufferLength)
2142 return INET_QueryOptionHelper(TRUE, hInternet, dwOption, lpBuffer, lpdwBufferLength);
2145 /***********************************************************************
2146 * InternetQueryOptionA (WININET.@)
2148 * Queries an options on the specified handle
2150 * RETURNS
2151 * TRUE on success
2152 * FALSE on failure
2155 BOOL WINAPI InternetQueryOptionA(HINTERNET hInternet, DWORD dwOption,
2156 LPVOID lpBuffer, LPDWORD lpdwBufferLength)
2158 return INET_QueryOptionHelper(FALSE, hInternet, dwOption, lpBuffer, lpdwBufferLength);
2162 /***********************************************************************
2163 * InternetSetOptionW (WININET.@)
2165 * Sets an options on the specified handle
2167 * RETURNS
2168 * TRUE on success
2169 * FALSE on failure
2172 BOOL WINAPI InternetSetOptionW(HINTERNET hInternet, DWORD dwOption,
2173 LPVOID lpBuffer, DWORD dwBufferLength)
2175 LPWININETHANDLEHEADER lpwhh;
2176 BOOL ret = TRUE;
2178 TRACE("(%p %d %p %d)\n", hInternet, dwOption, lpBuffer, dwBufferLength);
2180 lpwhh = (LPWININETHANDLEHEADER) WININET_GetObject( hInternet );
2181 if(lpwhh && lpwhh->vtbl->SetOption) {
2182 DWORD res;
2184 res = lpwhh->vtbl->SetOption(lpwhh, dwOption, lpBuffer, dwBufferLength);
2185 if(res != ERROR_INTERNET_INVALID_OPTION) {
2186 WININET_Release( lpwhh );
2188 if(res != ERROR_SUCCESS)
2189 SetLastError(res);
2191 return res == ERROR_SUCCESS;
2195 switch (dwOption)
2197 case INTERNET_OPTION_CALLBACK:
2199 INTERNET_STATUS_CALLBACK callback = *(INTERNET_STATUS_CALLBACK *)lpBuffer;
2200 ret = (set_status_callback(lpwhh, callback, TRUE) != INTERNET_INVALID_STATUS_CALLBACK);
2201 break;
2203 case INTERNET_OPTION_HTTP_VERSION:
2205 HTTP_VERSION_INFO* pVersion=(HTTP_VERSION_INFO*)lpBuffer;
2206 FIXME("Option INTERNET_OPTION_HTTP_VERSION(%d,%d): STUB\n",pVersion->dwMajorVersion,pVersion->dwMinorVersion);
2208 break;
2209 case INTERNET_OPTION_ERROR_MASK:
2211 unsigned long flags=*(unsigned long*)lpBuffer;
2212 FIXME("Option INTERNET_OPTION_ERROR_MASK(%ld): STUB\n",flags);
2214 break;
2215 case INTERNET_OPTION_CODEPAGE:
2217 unsigned long codepage=*(unsigned long*)lpBuffer;
2218 FIXME("Option INTERNET_OPTION_CODEPAGE (%ld): STUB\n",codepage);
2220 break;
2221 case INTERNET_OPTION_REQUEST_PRIORITY:
2223 unsigned long priority=*(unsigned long*)lpBuffer;
2224 FIXME("Option INTERNET_OPTION_REQUEST_PRIORITY (%ld): STUB\n",priority);
2226 break;
2227 case INTERNET_OPTION_CONNECT_TIMEOUT:
2229 unsigned long connecttimeout=*(unsigned long*)lpBuffer;
2230 FIXME("Option INTERNET_OPTION_CONNECT_TIMEOUT (%ld): STUB\n",connecttimeout);
2232 break;
2233 case INTERNET_OPTION_DATA_RECEIVE_TIMEOUT:
2235 unsigned long receivetimeout=*(unsigned long*)lpBuffer;
2236 FIXME("Option INTERNET_OPTION_DATA_RECEIVE_TIMEOUT (%ld): STUB\n",receivetimeout);
2238 break;
2239 case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2241 unsigned long conns=*(unsigned long*)lpBuffer;
2242 FIXME("Option INTERNET_OPTION_MAX_CONNS_PER_SERVER (%ld): STUB\n",conns);
2244 break;
2245 case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2247 unsigned long conns=*(unsigned long*)lpBuffer;
2248 FIXME("Option INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER (%ld): STUB\n",conns);
2250 break;
2251 case INTERNET_OPTION_RESET_URLCACHE_SESSION:
2252 FIXME("Option INTERNET_OPTION_RESET_URLCACHE_SESSION: STUB\n");
2253 break;
2254 case INTERNET_OPTION_END_BROWSER_SESSION:
2255 FIXME("Option INTERNET_OPTION_END_BROWSER_SESSION: STUB\n");
2256 break;
2257 case INTERNET_OPTION_CONNECTED_STATE:
2258 FIXME("Option INTERNET_OPTION_CONNECTED_STATE: STUB\n");
2259 break;
2260 case INTERNET_OPTION_DISABLE_PASSPORT_AUTH:
2261 TRACE("Option INTERNET_OPTION_DISABLE_PASSPORT_AUTH: harmless stub, since not enabled\n");
2262 break;
2263 case INTERNET_OPTION_SEND_TIMEOUT:
2264 case INTERNET_OPTION_RECEIVE_TIMEOUT:
2265 FIXME("INTERNET_OPTION_SEND/RECEIVE_TIMEOUT\n");
2266 break;
2267 case INTERNET_OPTION_CONNECT_RETRIES:
2268 FIXME("Option INTERNET_OPTION_CONNECT_RETRIES: STUB\n");
2269 break;
2270 case INTERNET_OPTION_CONTEXT_VALUE:
2271 FIXME("Option INTERNET_OPTION_CONTEXT_VALUE; STUB\n");
2272 break;
2273 case INTERNET_OPTION_SECURITY_FLAGS:
2274 FIXME("Option INTERNET_OPTION_SECURITY_FLAGS; STUB\n");
2275 break;
2276 case 86:
2277 FIXME("86\n");
2278 break;
2279 default:
2280 FIXME("Option %d STUB\n",dwOption);
2281 INTERNET_SetLastError(ERROR_INTERNET_INVALID_OPTION);
2282 ret = FALSE;
2283 break;
2286 if(lpwhh)
2287 WININET_Release( lpwhh );
2289 return ret;
2293 /***********************************************************************
2294 * InternetSetOptionA (WININET.@)
2296 * Sets an options on the specified handle.
2298 * RETURNS
2299 * TRUE on success
2300 * FALSE on failure
2303 BOOL WINAPI InternetSetOptionA(HINTERNET hInternet, DWORD dwOption,
2304 LPVOID lpBuffer, DWORD dwBufferLength)
2306 LPVOID wbuffer;
2307 DWORD wlen;
2308 BOOL r;
2310 switch( dwOption )
2312 case INTERNET_OPTION_CALLBACK:
2314 LPWININETHANDLEHEADER lpwh;
2315 INTERNET_STATUS_CALLBACK callback = *(INTERNET_STATUS_CALLBACK *)lpBuffer;
2317 if (!(lpwh = WININET_GetObject(hInternet))) return FALSE;
2318 r = (set_status_callback(lpwh, callback, FALSE) != INTERNET_INVALID_STATUS_CALLBACK);
2319 WININET_Release(lpwh);
2320 return r;
2322 case INTERNET_OPTION_PROXY:
2324 LPINTERNET_PROXY_INFOA pi = (LPINTERNET_PROXY_INFOA) lpBuffer;
2325 LPINTERNET_PROXY_INFOW piw;
2326 DWORD proxlen, prbylen;
2327 LPWSTR prox, prby;
2329 proxlen = MultiByteToWideChar( CP_ACP, 0, pi->lpszProxy, -1, NULL, 0);
2330 prbylen= MultiByteToWideChar( CP_ACP, 0, pi->lpszProxyBypass, -1, NULL, 0);
2331 wlen = sizeof(*piw) + proxlen + prbylen;
2332 wbuffer = HeapAlloc( GetProcessHeap(), 0, wlen*sizeof(WCHAR) );
2333 piw = (LPINTERNET_PROXY_INFOW) wbuffer;
2334 piw->dwAccessType = pi->dwAccessType;
2335 prox = (LPWSTR) &piw[1];
2336 prby = &prox[proxlen+1];
2337 MultiByteToWideChar( CP_ACP, 0, pi->lpszProxy, -1, prox, proxlen);
2338 MultiByteToWideChar( CP_ACP, 0, pi->lpszProxyBypass, -1, prby, prbylen);
2339 piw->lpszProxy = prox;
2340 piw->lpszProxyBypass = prby;
2342 break;
2343 case INTERNET_OPTION_USER_AGENT:
2344 case INTERNET_OPTION_USERNAME:
2345 case INTERNET_OPTION_PASSWORD:
2346 wlen = MultiByteToWideChar( CP_ACP, 0, lpBuffer, dwBufferLength,
2347 NULL, 0 );
2348 wbuffer = HeapAlloc( GetProcessHeap(), 0, wlen*sizeof(WCHAR) );
2349 MultiByteToWideChar( CP_ACP, 0, lpBuffer, dwBufferLength,
2350 wbuffer, wlen );
2351 break;
2352 default:
2353 wbuffer = lpBuffer;
2354 wlen = dwBufferLength;
2357 r = InternetSetOptionW(hInternet,dwOption, wbuffer, wlen);
2359 if( lpBuffer != wbuffer )
2360 HeapFree( GetProcessHeap(), 0, wbuffer );
2362 return r;
2366 /***********************************************************************
2367 * InternetSetOptionExA (WININET.@)
2369 BOOL WINAPI InternetSetOptionExA(HINTERNET hInternet, DWORD dwOption,
2370 LPVOID lpBuffer, DWORD dwBufferLength, DWORD dwFlags)
2372 FIXME("Flags %08x ignored\n", dwFlags);
2373 return InternetSetOptionA( hInternet, dwOption, lpBuffer, dwBufferLength );
2376 /***********************************************************************
2377 * InternetSetOptionExW (WININET.@)
2379 BOOL WINAPI InternetSetOptionExW(HINTERNET hInternet, DWORD dwOption,
2380 LPVOID lpBuffer, DWORD dwBufferLength, DWORD dwFlags)
2382 FIXME("Flags %08x ignored\n", dwFlags);
2383 if( dwFlags & ~ISO_VALID_FLAGS )
2385 INTERNET_SetLastError( ERROR_INVALID_PARAMETER );
2386 return FALSE;
2388 return InternetSetOptionW( hInternet, dwOption, lpBuffer, dwBufferLength );
2391 static const WCHAR WININET_wkday[7][4] =
2392 { { 'S','u','n', 0 }, { 'M','o','n', 0 }, { 'T','u','e', 0 }, { 'W','e','d', 0 },
2393 { 'T','h','u', 0 }, { 'F','r','i', 0 }, { 'S','a','t', 0 } };
2394 static const WCHAR WININET_month[12][4] =
2395 { { 'J','a','n', 0 }, { 'F','e','b', 0 }, { 'M','a','r', 0 }, { 'A','p','r', 0 },
2396 { 'M','a','y', 0 }, { 'J','u','n', 0 }, { 'J','u','l', 0 }, { 'A','u','g', 0 },
2397 { 'S','e','p', 0 }, { 'O','c','t', 0 }, { 'N','o','v', 0 }, { 'D','e','c', 0 } };
2399 /***********************************************************************
2400 * InternetTimeFromSystemTimeA (WININET.@)
2402 BOOL WINAPI InternetTimeFromSystemTimeA( const SYSTEMTIME* time, DWORD format, LPSTR string, DWORD size )
2404 BOOL ret;
2405 WCHAR stringW[INTERNET_RFC1123_BUFSIZE];
2407 TRACE( "%p 0x%08x %p 0x%08x\n", time, format, string, size );
2409 ret = InternetTimeFromSystemTimeW( time, format, stringW, sizeof(stringW) );
2410 if (ret) WideCharToMultiByte( CP_ACP, 0, stringW, -1, string, size, NULL, NULL );
2412 return ret;
2415 /***********************************************************************
2416 * InternetTimeFromSystemTimeW (WININET.@)
2418 BOOL WINAPI InternetTimeFromSystemTimeW( const SYSTEMTIME* time, DWORD format, LPWSTR string, DWORD size )
2420 static const WCHAR date[] =
2421 { '%','s',',',' ','%','0','2','d',' ','%','s',' ','%','4','d',' ','%','0',
2422 '2','d',':','%','0','2','d',':','%','0','2','d',' ','G','M','T', 0 };
2424 TRACE( "%p 0x%08x %p 0x%08x\n", time, format, string, size );
2426 if (!time || !string) return FALSE;
2428 if (format != INTERNET_RFC1123_FORMAT || size < INTERNET_RFC1123_BUFSIZE * sizeof(WCHAR))
2429 return FALSE;
2431 sprintfW( string, date,
2432 WININET_wkday[time->wDayOfWeek],
2433 time->wDay,
2434 WININET_month[time->wMonth - 1],
2435 time->wYear,
2436 time->wHour,
2437 time->wMinute,
2438 time->wSecond );
2440 return TRUE;
2443 /***********************************************************************
2444 * InternetTimeToSystemTimeA (WININET.@)
2446 BOOL WINAPI InternetTimeToSystemTimeA( LPCSTR string, SYSTEMTIME* time, DWORD reserved )
2448 BOOL ret = FALSE;
2449 WCHAR *stringW;
2450 int len;
2452 TRACE( "%s %p 0x%08x\n", debugstr_a(string), time, reserved );
2454 len = MultiByteToWideChar( CP_ACP, 0, string, -1, NULL, 0 );
2455 stringW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
2457 if (stringW)
2459 MultiByteToWideChar( CP_ACP, 0, string, -1, stringW, len );
2460 ret = InternetTimeToSystemTimeW( stringW, time, reserved );
2461 HeapFree( GetProcessHeap(), 0, stringW );
2463 return ret;
2466 /***********************************************************************
2467 * InternetTimeToSystemTimeW (WININET.@)
2469 BOOL WINAPI InternetTimeToSystemTimeW( LPCWSTR string, SYSTEMTIME* time, DWORD reserved )
2471 unsigned int i;
2472 const WCHAR *s = string;
2473 WCHAR *end;
2475 TRACE( "%s %p 0x%08x\n", debugstr_w(string), time, reserved );
2477 if (!string || !time) return FALSE;
2479 /* Windows does this too */
2480 GetSystemTime( time );
2482 /* Convert an RFC1123 time such as 'Fri, 07 Jan 2005 12:06:35 GMT' into
2483 * a SYSTEMTIME structure.
2486 while (*s && !isalphaW( *s )) s++;
2487 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
2488 time->wDayOfWeek = 7;
2490 for (i = 0; i < 7; i++)
2492 if (toupperW( WININET_wkday[i][0] ) == toupperW( s[0] ) &&
2493 toupperW( WININET_wkday[i][1] ) == toupperW( s[1] ) &&
2494 toupperW( WININET_wkday[i][2] ) == toupperW( s[2] ) )
2496 time->wDayOfWeek = i;
2497 break;
2501 if (time->wDayOfWeek > 6) return TRUE;
2502 while (*s && !isdigitW( *s )) s++;
2503 time->wDay = strtolW( s, &end, 10 );
2504 s = end;
2506 while (*s && !isalphaW( *s )) s++;
2507 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
2508 time->wMonth = 0;
2510 for (i = 0; i < 12; i++)
2512 if (toupperW( WININET_month[i][0]) == toupperW( s[0] ) &&
2513 toupperW( WININET_month[i][1]) == toupperW( s[1] ) &&
2514 toupperW( WININET_month[i][2]) == toupperW( s[2] ) )
2516 time->wMonth = i + 1;
2517 break;
2520 if (time->wMonth == 0) return TRUE;
2522 while (*s && !isdigitW( *s )) s++;
2523 if (*s == '\0') return TRUE;
2524 time->wYear = strtolW( s, &end, 10 );
2525 s = end;
2527 while (*s && !isdigitW( *s )) s++;
2528 if (*s == '\0') return TRUE;
2529 time->wHour = strtolW( s, &end, 10 );
2530 s = end;
2532 while (*s && !isdigitW( *s )) s++;
2533 if (*s == '\0') return TRUE;
2534 time->wMinute = strtolW( s, &end, 10 );
2535 s = end;
2537 while (*s && !isdigitW( *s )) s++;
2538 if (*s == '\0') return TRUE;
2539 time->wSecond = strtolW( s, &end, 10 );
2540 s = end;
2542 time->wMilliseconds = 0;
2543 return TRUE;
2546 /***********************************************************************
2547 * InternetCheckConnectionW (WININET.@)
2549 * Pings a requested host to check internet connection
2551 * RETURNS
2552 * TRUE on success and FALSE on failure. If a failure then
2553 * ERROR_NOT_CONNECTED is placed into GetLastError
2556 BOOL WINAPI InternetCheckConnectionW( LPCWSTR lpszUrl, DWORD dwFlags, DWORD dwReserved )
2559 * this is a kludge which runs the resident ping program and reads the output.
2561 * Anyone have a better idea?
2564 BOOL rc = FALSE;
2565 static const CHAR ping[] = "ping -c 1 ";
2566 static const CHAR redirect[] = " >/dev/null 2>/dev/null";
2567 CHAR *command = NULL;
2568 WCHAR hostW[1024];
2569 DWORD len;
2570 INTERNET_PORT port;
2571 int status = -1;
2573 FIXME("\n");
2576 * Crack or set the Address
2578 if (lpszUrl == NULL)
2581 * According to the doc we are supposed to use the ip for the next
2582 * server in the WnInet internal server database. I have
2583 * no idea what that is or how to get it.
2585 * So someone needs to implement this.
2587 FIXME("Unimplemented with URL of NULL\n");
2588 return TRUE;
2590 else
2592 URL_COMPONENTSW components;
2594 ZeroMemory(&components,sizeof(URL_COMPONENTSW));
2595 components.lpszHostName = (LPWSTR)&hostW;
2596 components.dwHostNameLength = 1024;
2598 if (!InternetCrackUrlW(lpszUrl,0,0,&components))
2599 goto End;
2601 TRACE("host name : %s\n",debugstr_w(components.lpszHostName));
2602 port = components.nPort;
2603 TRACE("port: %d\n", port);
2606 if (dwFlags & FLAG_ICC_FORCE_CONNECTION)
2608 struct sockaddr_in sin;
2609 int fd;
2611 if (!GetAddress(hostW, port, &sin))
2612 goto End;
2613 fd = socket(sin.sin_family, SOCK_STREAM, 0);
2614 if (fd != -1)
2616 if (connect(fd, (struct sockaddr *)&sin, sizeof(sin)) == 0)
2617 rc = TRUE;
2618 close(fd);
2621 else
2624 * Build our ping command
2626 len = WideCharToMultiByte(CP_UNIXCP, 0, hostW, -1, NULL, 0, NULL, NULL);
2627 command = HeapAlloc( GetProcessHeap(), 0, strlen(ping)+len+strlen(redirect) );
2628 strcpy(command,ping);
2629 WideCharToMultiByte(CP_UNIXCP, 0, hostW, -1, command+strlen(ping), len, NULL, NULL);
2630 strcat(command,redirect);
2632 TRACE("Ping command is : %s\n",command);
2634 status = system(command);
2636 TRACE("Ping returned a code of %i\n",status);
2638 /* Ping return code of 0 indicates success */
2639 if (status == 0)
2640 rc = TRUE;
2643 End:
2645 HeapFree( GetProcessHeap(), 0, command );
2646 if (rc == FALSE)
2647 INTERNET_SetLastError(ERROR_NOT_CONNECTED);
2649 return rc;
2653 /***********************************************************************
2654 * InternetCheckConnectionA (WININET.@)
2656 * Pings a requested host to check internet connection
2658 * RETURNS
2659 * TRUE on success and FALSE on failure. If a failure then
2660 * ERROR_NOT_CONNECTED is placed into GetLastError
2663 BOOL WINAPI InternetCheckConnectionA(LPCSTR lpszUrl, DWORD dwFlags, DWORD dwReserved)
2665 WCHAR *szUrl;
2666 INT len;
2667 BOOL rc;
2669 len = MultiByteToWideChar(CP_ACP, 0, lpszUrl, -1, NULL, 0);
2670 if (!(szUrl = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR))))
2671 return FALSE;
2672 MultiByteToWideChar(CP_ACP, 0, lpszUrl, -1, szUrl, len);
2673 rc = InternetCheckConnectionW(szUrl, dwFlags, dwReserved);
2674 HeapFree(GetProcessHeap(), 0, szUrl);
2676 return rc;
2680 /**********************************************************
2681 * INTERNET_InternetOpenUrlW (internal)
2683 * Opens an URL
2685 * RETURNS
2686 * handle of connection or NULL on failure
2688 HINTERNET WINAPI INTERNET_InternetOpenUrlW(LPWININETAPPINFOW hIC, LPCWSTR lpszUrl,
2689 LPCWSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext)
2691 URL_COMPONENTSW urlComponents;
2692 WCHAR protocol[32], hostName[MAXHOSTNAME], userName[1024];
2693 WCHAR password[1024], path[2048], extra[1024];
2694 HINTERNET client = NULL, client1 = NULL;
2696 TRACE("(%p, %s, %s, %08x, %08x, %08lx)\n", hIC, debugstr_w(lpszUrl), debugstr_w(lpszHeaders),
2697 dwHeadersLength, dwFlags, dwContext);
2699 urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
2700 urlComponents.lpszScheme = protocol;
2701 urlComponents.dwSchemeLength = 32;
2702 urlComponents.lpszHostName = hostName;
2703 urlComponents.dwHostNameLength = MAXHOSTNAME;
2704 urlComponents.lpszUserName = userName;
2705 urlComponents.dwUserNameLength = 1024;
2706 urlComponents.lpszPassword = password;
2707 urlComponents.dwPasswordLength = 1024;
2708 urlComponents.lpszUrlPath = path;
2709 urlComponents.dwUrlPathLength = 2048;
2710 urlComponents.lpszExtraInfo = extra;
2711 urlComponents.dwExtraInfoLength = 1024;
2712 if(!InternetCrackUrlW(lpszUrl, strlenW(lpszUrl), 0, &urlComponents))
2713 return NULL;
2714 switch(urlComponents.nScheme) {
2715 case INTERNET_SCHEME_FTP:
2716 if(urlComponents.nPort == 0)
2717 urlComponents.nPort = INTERNET_DEFAULT_FTP_PORT;
2718 client = FTP_Connect(hIC, hostName, urlComponents.nPort,
2719 userName, password, dwFlags, dwContext, INET_OPENURL);
2720 if(client == NULL)
2721 break;
2722 client1 = FtpOpenFileW(client, path, GENERIC_READ, dwFlags, dwContext);
2723 if(client1 == NULL) {
2724 InternetCloseHandle(client);
2725 break;
2727 break;
2729 case INTERNET_SCHEME_HTTP:
2730 case INTERNET_SCHEME_HTTPS: {
2731 static const WCHAR szStars[] = { '*','/','*', 0 };
2732 LPCWSTR accept[2] = { szStars, NULL };
2733 if(urlComponents.nPort == 0) {
2734 if(urlComponents.nScheme == INTERNET_SCHEME_HTTP)
2735 urlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
2736 else
2737 urlComponents.nPort = INTERNET_DEFAULT_HTTPS_PORT;
2739 /* FIXME: should use pointers, not handles, as handles are not thread-safe */
2740 client = HTTP_Connect(hIC, hostName, urlComponents.nPort,
2741 userName, password, dwFlags, dwContext, INET_OPENURL);
2742 if(client == NULL)
2743 break;
2745 if (urlComponents.dwExtraInfoLength) {
2746 WCHAR *path_extra;
2747 DWORD len = urlComponents.dwUrlPathLength + urlComponents.dwExtraInfoLength + 1;
2749 if (!(path_extra = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR))))
2751 InternetCloseHandle(client);
2752 break;
2754 strcpyW(path_extra, urlComponents.lpszUrlPath);
2755 strcatW(path_extra, urlComponents.lpszExtraInfo);
2756 client1 = HttpOpenRequestW(client, NULL, path_extra, NULL, NULL, accept, dwFlags, dwContext);
2757 HeapFree(GetProcessHeap(), 0, path_extra);
2759 else
2760 client1 = HttpOpenRequestW(client, NULL, path, NULL, NULL, accept, dwFlags, dwContext);
2762 if(client1 == NULL) {
2763 InternetCloseHandle(client);
2764 break;
2766 HttpAddRequestHeadersW(client1, lpszHeaders, dwHeadersLength, HTTP_ADDREQ_FLAG_ADD);
2767 if (!HttpSendRequestW(client1, NULL, 0, NULL, 0) &&
2768 GetLastError() != ERROR_IO_PENDING) {
2769 InternetCloseHandle(client1);
2770 client1 = NULL;
2771 break;
2774 case INTERNET_SCHEME_GOPHER:
2775 /* gopher doesn't seem to be implemented in wine, but it's supposed
2776 * to be supported by InternetOpenUrlA. */
2777 default:
2778 INTERNET_SetLastError(ERROR_INTERNET_UNRECOGNIZED_SCHEME);
2779 break;
2782 TRACE(" %p <--\n", client1);
2784 return client1;
2787 /**********************************************************
2788 * InternetOpenUrlW (WININET.@)
2790 * Opens an URL
2792 * RETURNS
2793 * handle of connection or NULL on failure
2795 static void AsyncInternetOpenUrlProc(WORKREQUEST *workRequest)
2797 struct WORKREQ_INTERNETOPENURLW const *req = &workRequest->u.InternetOpenUrlW;
2798 LPWININETAPPINFOW hIC = (LPWININETAPPINFOW) workRequest->hdr;
2800 TRACE("%p\n", hIC);
2802 INTERNET_InternetOpenUrlW(hIC, req->lpszUrl,
2803 req->lpszHeaders, req->dwHeadersLength, req->dwFlags, req->dwContext);
2804 HeapFree(GetProcessHeap(), 0, req->lpszUrl);
2805 HeapFree(GetProcessHeap(), 0, req->lpszHeaders);
2808 HINTERNET WINAPI InternetOpenUrlW(HINTERNET hInternet, LPCWSTR lpszUrl,
2809 LPCWSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext)
2811 HINTERNET ret = NULL;
2812 LPWININETAPPINFOW hIC = NULL;
2814 if (TRACE_ON(wininet)) {
2815 TRACE("(%p, %s, %s, %08x, %08x, %08lx)\n", hInternet, debugstr_w(lpszUrl), debugstr_w(lpszHeaders),
2816 dwHeadersLength, dwFlags, dwContext);
2817 TRACE(" flags :");
2818 dump_INTERNET_FLAGS(dwFlags);
2821 if (!lpszUrl)
2823 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
2824 goto lend;
2827 hIC = (LPWININETAPPINFOW) WININET_GetObject( hInternet );
2828 if (NULL == hIC || hIC->hdr.htype != WH_HINIT) {
2829 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2830 goto lend;
2833 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC) {
2834 WORKREQUEST workRequest;
2835 struct WORKREQ_INTERNETOPENURLW *req;
2837 workRequest.asyncproc = AsyncInternetOpenUrlProc;
2838 workRequest.hdr = WININET_AddRef( &hIC->hdr );
2839 req = &workRequest.u.InternetOpenUrlW;
2840 req->lpszUrl = WININET_strdupW(lpszUrl);
2841 if (lpszHeaders)
2842 req->lpszHeaders = WININET_strdupW(lpszHeaders);
2843 else
2844 req->lpszHeaders = 0;
2845 req->dwHeadersLength = dwHeadersLength;
2846 req->dwFlags = dwFlags;
2847 req->dwContext = dwContext;
2849 INTERNET_AsyncCall(&workRequest);
2851 * This is from windows.
2853 INTERNET_SetLastError(ERROR_IO_PENDING);
2854 } else {
2855 ret = INTERNET_InternetOpenUrlW(hIC, lpszUrl, lpszHeaders, dwHeadersLength, dwFlags, dwContext);
2858 lend:
2859 if( hIC )
2860 WININET_Release( &hIC->hdr );
2861 TRACE(" %p <--\n", ret);
2863 return ret;
2866 /**********************************************************
2867 * InternetOpenUrlA (WININET.@)
2869 * Opens an URL
2871 * RETURNS
2872 * handle of connection or NULL on failure
2874 HINTERNET WINAPI InternetOpenUrlA(HINTERNET hInternet, LPCSTR lpszUrl,
2875 LPCSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext)
2877 HINTERNET rc = NULL;
2879 INT lenUrl;
2880 INT lenHeaders = 0;
2881 LPWSTR szUrl = NULL;
2882 LPWSTR szHeaders = NULL;
2884 TRACE("\n");
2886 if(lpszUrl) {
2887 lenUrl = MultiByteToWideChar(CP_ACP, 0, lpszUrl, -1, NULL, 0 );
2888 szUrl = HeapAlloc(GetProcessHeap(), 0, lenUrl*sizeof(WCHAR));
2889 if(!szUrl)
2890 return NULL;
2891 MultiByteToWideChar(CP_ACP, 0, lpszUrl, -1, szUrl, lenUrl);
2894 if(lpszHeaders) {
2895 lenHeaders = MultiByteToWideChar(CP_ACP, 0, lpszHeaders, dwHeadersLength, NULL, 0 );
2896 szHeaders = HeapAlloc(GetProcessHeap(), 0, lenHeaders*sizeof(WCHAR));
2897 if(!szHeaders) {
2898 HeapFree(GetProcessHeap(), 0, szUrl);
2899 return NULL;
2901 MultiByteToWideChar(CP_ACP, 0, lpszHeaders, dwHeadersLength, szHeaders, lenHeaders);
2904 rc = InternetOpenUrlW(hInternet, szUrl, szHeaders,
2905 lenHeaders, dwFlags, dwContext);
2907 HeapFree(GetProcessHeap(), 0, szUrl);
2908 HeapFree(GetProcessHeap(), 0, szHeaders);
2910 return rc;
2914 static LPWITHREADERROR INTERNET_AllocThreadError(void)
2916 LPWITHREADERROR lpwite = HeapAlloc(GetProcessHeap(), 0, sizeof(*lpwite));
2918 if (lpwite)
2920 lpwite->dwError = 0;
2921 lpwite->response[0] = '\0';
2924 if (!TlsSetValue(g_dwTlsErrIndex, lpwite))
2926 HeapFree(GetProcessHeap(), 0, lpwite);
2927 return NULL;
2930 return lpwite;
2934 /***********************************************************************
2935 * INTERNET_SetLastError (internal)
2937 * Set last thread specific error
2939 * RETURNS
2942 void INTERNET_SetLastError(DWORD dwError)
2944 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
2946 if (!lpwite)
2947 lpwite = INTERNET_AllocThreadError();
2949 SetLastError(dwError);
2950 if(lpwite)
2951 lpwite->dwError = dwError;
2955 /***********************************************************************
2956 * INTERNET_GetLastError (internal)
2958 * Get last thread specific error
2960 * RETURNS
2963 DWORD INTERNET_GetLastError(void)
2965 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
2966 if (!lpwite) return 0;
2967 /* TlsGetValue clears last error, so set it again here */
2968 SetLastError(lpwite->dwError);
2969 return lpwite->dwError;
2973 /***********************************************************************
2974 * INTERNET_WorkerThreadFunc (internal)
2976 * Worker thread execution function
2978 * RETURNS
2981 static DWORD CALLBACK INTERNET_WorkerThreadFunc(LPVOID lpvParam)
2983 LPWORKREQUEST lpRequest = lpvParam;
2984 WORKREQUEST workRequest;
2986 TRACE("\n");
2988 workRequest = *lpRequest;
2989 HeapFree(GetProcessHeap(), 0, lpRequest);
2991 workRequest.asyncproc(&workRequest);
2993 WININET_Release( workRequest.hdr );
2994 return TRUE;
2998 /***********************************************************************
2999 * INTERNET_AsyncCall (internal)
3001 * Retrieves work request from queue
3003 * RETURNS
3006 BOOL INTERNET_AsyncCall(LPWORKREQUEST lpWorkRequest)
3008 BOOL bSuccess;
3009 LPWORKREQUEST lpNewRequest;
3011 TRACE("\n");
3013 lpNewRequest = HeapAlloc(GetProcessHeap(), 0, sizeof(WORKREQUEST));
3014 if (!lpNewRequest)
3015 return FALSE;
3017 *lpNewRequest = *lpWorkRequest;
3019 bSuccess = QueueUserWorkItem(INTERNET_WorkerThreadFunc, lpNewRequest, WT_EXECUTELONGFUNCTION);
3020 if (!bSuccess)
3022 HeapFree(GetProcessHeap(), 0, lpNewRequest);
3023 INTERNET_SetLastError(ERROR_INTERNET_ASYNC_THREAD_FAILED);
3026 return bSuccess;
3030 /***********************************************************************
3031 * INTERNET_GetResponseBuffer (internal)
3033 * RETURNS
3036 LPSTR INTERNET_GetResponseBuffer(void)
3038 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
3039 if (!lpwite)
3040 lpwite = INTERNET_AllocThreadError();
3041 TRACE("\n");
3042 return lpwite->response;
3045 /***********************************************************************
3046 * INTERNET_GetNextLine (internal)
3048 * Parse next line in directory string listing
3050 * RETURNS
3051 * Pointer to beginning of next line
3052 * NULL on failure
3056 LPSTR INTERNET_GetNextLine(INT nSocket, LPDWORD dwLen)
3058 struct pollfd pfd;
3059 BOOL bSuccess = FALSE;
3060 INT nRecv = 0;
3061 LPSTR lpszBuffer = INTERNET_GetResponseBuffer();
3063 TRACE("\n");
3065 pfd.fd = nSocket;
3066 pfd.events = POLLIN;
3068 while (nRecv < MAX_REPLY_LEN)
3070 if (poll(&pfd,1, RESPONSE_TIMEOUT * 1000) > 0)
3072 if (recv(nSocket, &lpszBuffer[nRecv], 1, 0) <= 0)
3074 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
3075 goto lend;
3078 if (lpszBuffer[nRecv] == '\n')
3080 bSuccess = TRUE;
3081 break;
3083 if (lpszBuffer[nRecv] != '\r')
3084 nRecv++;
3086 else
3088 INTERNET_SetLastError(ERROR_INTERNET_TIMEOUT);
3089 goto lend;
3093 lend:
3094 if (bSuccess)
3096 lpszBuffer[nRecv] = '\0';
3097 *dwLen = nRecv - 1;
3098 TRACE(":%d %s\n", nRecv, lpszBuffer);
3099 return lpszBuffer;
3101 else
3103 return NULL;
3107 /**********************************************************
3108 * InternetQueryDataAvailable (WININET.@)
3110 * Determines how much data is available to be read.
3112 * RETURNS
3113 * TRUE on success, FALSE if an error occurred. If
3114 * INTERNET_FLAG_ASYNC was specified in InternetOpen, and
3115 * no data is presently available, FALSE is returned with
3116 * the last error ERROR_IO_PENDING; a callback with status
3117 * INTERNET_STATUS_REQUEST_COMPLETE will be sent when more
3118 * data is available.
3120 BOOL WINAPI InternetQueryDataAvailable( HINTERNET hFile,
3121 LPDWORD lpdwNumberOfBytesAvailble,
3122 DWORD dwFlags, DWORD_PTR dwContext)
3124 WININETHANDLEHEADER *hdr;
3125 DWORD res;
3127 TRACE("(%p %p %x %lx)\n", hFile, lpdwNumberOfBytesAvailble, dwFlags, dwContext);
3129 hdr = WININET_GetObject( hFile );
3130 if (!hdr) {
3131 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
3132 return FALSE;
3135 if(hdr->vtbl->QueryDataAvailable) {
3136 res = hdr->vtbl->QueryDataAvailable(hdr, lpdwNumberOfBytesAvailble, dwFlags, dwContext);
3137 }else {
3138 WARN("wrong handle\n");
3139 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
3142 WININET_Release(hdr);
3144 if(res != ERROR_SUCCESS)
3145 SetLastError(res);
3146 return res == ERROR_SUCCESS;
3150 /***********************************************************************
3151 * InternetLockRequestFile (WININET.@)
3153 BOOL WINAPI InternetLockRequestFile( HINTERNET hInternet, HANDLE
3154 *lphLockReqHandle)
3156 FIXME("STUB\n");
3157 return FALSE;
3160 BOOL WINAPI InternetUnlockRequestFile( HANDLE hLockHandle)
3162 FIXME("STUB\n");
3163 return FALSE;
3167 /***********************************************************************
3168 * InternetAutodial (WININET.@)
3170 * On windows this function is supposed to dial the default internet
3171 * connection. We don't want to have Wine dial out to the internet so
3172 * we return TRUE by default. It might be nice to check if we are connected.
3174 * RETURNS
3175 * TRUE on success
3176 * FALSE on failure
3179 BOOL WINAPI InternetAutodial(DWORD dwFlags, HWND hwndParent)
3181 FIXME("STUB\n");
3183 /* Tell that we are connected to the internet. */
3184 return TRUE;
3187 /***********************************************************************
3188 * InternetAutodialHangup (WININET.@)
3190 * Hangs up a connection made with InternetAutodial
3192 * PARAM
3193 * dwReserved
3194 * RETURNS
3195 * TRUE on success
3196 * FALSE on failure
3199 BOOL WINAPI InternetAutodialHangup(DWORD dwReserved)
3201 FIXME("STUB\n");
3203 /* we didn't dial, we don't disconnect */
3204 return TRUE;
3207 /***********************************************************************
3208 * InternetCombineUrlA (WININET.@)
3210 * Combine a base URL with a relative URL
3212 * RETURNS
3213 * TRUE on success
3214 * FALSE on failure
3218 BOOL WINAPI InternetCombineUrlA(LPCSTR lpszBaseUrl, LPCSTR lpszRelativeUrl,
3219 LPSTR lpszBuffer, LPDWORD lpdwBufferLength,
3220 DWORD dwFlags)
3222 HRESULT hr=S_OK;
3224 TRACE("(%s, %s, %p, %p, 0x%08x)\n", debugstr_a(lpszBaseUrl), debugstr_a(lpszRelativeUrl), lpszBuffer, lpdwBufferLength, dwFlags);
3226 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
3227 dwFlags ^= ICU_NO_ENCODE;
3228 hr=UrlCombineA(lpszBaseUrl,lpszRelativeUrl,lpszBuffer,lpdwBufferLength,dwFlags);
3230 return (hr==S_OK);
3233 /***********************************************************************
3234 * InternetCombineUrlW (WININET.@)
3236 * Combine a base URL with a relative URL
3238 * RETURNS
3239 * TRUE on success
3240 * FALSE on failure
3244 BOOL WINAPI InternetCombineUrlW(LPCWSTR lpszBaseUrl, LPCWSTR lpszRelativeUrl,
3245 LPWSTR lpszBuffer, LPDWORD lpdwBufferLength,
3246 DWORD dwFlags)
3248 HRESULT hr=S_OK;
3250 TRACE("(%s, %s, %p, %p, 0x%08x)\n", debugstr_w(lpszBaseUrl), debugstr_w(lpszRelativeUrl), lpszBuffer, lpdwBufferLength, dwFlags);
3252 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
3253 dwFlags ^= ICU_NO_ENCODE;
3254 hr=UrlCombineW(lpszBaseUrl,lpszRelativeUrl,lpszBuffer,lpdwBufferLength,dwFlags);
3256 return (hr==S_OK);
3259 /* max port num is 65535 => 5 digits */
3260 #define MAX_WORD_DIGITS 5
3262 #define URL_GET_COMP_LENGTH(url, component) ((url)->dw##component##Length ? \
3263 (url)->dw##component##Length : strlenW((url)->lpsz##component))
3264 #define URL_GET_COMP_LENGTHA(url, component) ((url)->dw##component##Length ? \
3265 (url)->dw##component##Length : strlen((url)->lpsz##component))
3267 static BOOL url_uses_default_port(INTERNET_SCHEME nScheme, INTERNET_PORT nPort)
3269 if ((nScheme == INTERNET_SCHEME_HTTP) &&
3270 (nPort == INTERNET_DEFAULT_HTTP_PORT))
3271 return TRUE;
3272 if ((nScheme == INTERNET_SCHEME_HTTPS) &&
3273 (nPort == INTERNET_DEFAULT_HTTPS_PORT))
3274 return TRUE;
3275 if ((nScheme == INTERNET_SCHEME_FTP) &&
3276 (nPort == INTERNET_DEFAULT_FTP_PORT))
3277 return TRUE;
3278 if ((nScheme == INTERNET_SCHEME_GOPHER) &&
3279 (nPort == INTERNET_DEFAULT_GOPHER_PORT))
3280 return TRUE;
3282 if (nPort == INTERNET_INVALID_PORT_NUMBER)
3283 return TRUE;
3285 return FALSE;
3288 /* opaque urls do not fit into the standard url hierarchy and don't have
3289 * two following slashes */
3290 static inline BOOL scheme_is_opaque(INTERNET_SCHEME nScheme)
3292 return (nScheme != INTERNET_SCHEME_FTP) &&
3293 (nScheme != INTERNET_SCHEME_GOPHER) &&
3294 (nScheme != INTERNET_SCHEME_HTTP) &&
3295 (nScheme != INTERNET_SCHEME_HTTPS) &&
3296 (nScheme != INTERNET_SCHEME_FILE);
3299 static LPCWSTR INTERNET_GetSchemeString(INTERNET_SCHEME scheme)
3301 int index;
3302 if (scheme < INTERNET_SCHEME_FIRST)
3303 return NULL;
3304 index = scheme - INTERNET_SCHEME_FIRST;
3305 if (index >= sizeof(url_schemes)/sizeof(url_schemes[0]))
3306 return NULL;
3307 return (LPCWSTR)&url_schemes[index];
3310 /* we can calculate using ansi strings because we're just
3311 * calculating string length, not size
3313 static BOOL calc_url_length(LPURL_COMPONENTSW lpUrlComponents,
3314 LPDWORD lpdwUrlLength)
3316 INTERNET_SCHEME nScheme;
3318 *lpdwUrlLength = 0;
3320 if (lpUrlComponents->lpszScheme)
3322 DWORD dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, Scheme);
3323 *lpdwUrlLength += dwLen;
3324 nScheme = GetInternetSchemeW(lpUrlComponents->lpszScheme, dwLen);
3326 else
3328 LPCWSTR scheme;
3330 nScheme = lpUrlComponents->nScheme;
3332 if (nScheme == INTERNET_SCHEME_DEFAULT)
3333 nScheme = INTERNET_SCHEME_HTTP;
3334 scheme = INTERNET_GetSchemeString(nScheme);
3335 *lpdwUrlLength += strlenW(scheme);
3338 (*lpdwUrlLength)++; /* ':' */
3339 if (!scheme_is_opaque(nScheme) || lpUrlComponents->lpszHostName)
3340 *lpdwUrlLength += strlen("//");
3342 if (lpUrlComponents->lpszUserName)
3344 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, UserName);
3345 *lpdwUrlLength += strlen("@");
3347 else
3349 if (lpUrlComponents->lpszPassword)
3351 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
3352 return FALSE;
3356 if (lpUrlComponents->lpszPassword)
3358 *lpdwUrlLength += strlen(":");
3359 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, Password);
3362 if (lpUrlComponents->lpszHostName)
3364 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, HostName);
3366 if (!url_uses_default_port(nScheme, lpUrlComponents->nPort))
3368 char szPort[MAX_WORD_DIGITS+1];
3370 sprintf(szPort, "%d", lpUrlComponents->nPort);
3371 *lpdwUrlLength += strlen(szPort);
3372 *lpdwUrlLength += strlen(":");
3375 if (lpUrlComponents->lpszUrlPath && *lpUrlComponents->lpszUrlPath != '/')
3376 (*lpdwUrlLength)++; /* '/' */
3379 if (lpUrlComponents->lpszUrlPath)
3380 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, UrlPath);
3382 return TRUE;
3385 static void convert_urlcomp_atow(LPURL_COMPONENTSA lpUrlComponents, LPURL_COMPONENTSW urlCompW)
3387 INT len;
3389 ZeroMemory(urlCompW, sizeof(URL_COMPONENTSW));
3391 urlCompW->dwStructSize = sizeof(URL_COMPONENTSW);
3392 urlCompW->dwSchemeLength = lpUrlComponents->dwSchemeLength;
3393 urlCompW->nScheme = lpUrlComponents->nScheme;
3394 urlCompW->dwHostNameLength = lpUrlComponents->dwHostNameLength;
3395 urlCompW->nPort = lpUrlComponents->nPort;
3396 urlCompW->dwUserNameLength = lpUrlComponents->dwUserNameLength;
3397 urlCompW->dwPasswordLength = lpUrlComponents->dwPasswordLength;
3398 urlCompW->dwUrlPathLength = lpUrlComponents->dwUrlPathLength;
3399 urlCompW->dwExtraInfoLength = lpUrlComponents->dwExtraInfoLength;
3401 if (lpUrlComponents->lpszScheme)
3403 len = URL_GET_COMP_LENGTHA(lpUrlComponents, Scheme) + 1;
3404 urlCompW->lpszScheme = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3405 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszScheme,
3406 -1, urlCompW->lpszScheme, len);
3409 if (lpUrlComponents->lpszHostName)
3411 len = URL_GET_COMP_LENGTHA(lpUrlComponents, HostName) + 1;
3412 urlCompW->lpszHostName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3413 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszHostName,
3414 -1, urlCompW->lpszHostName, len);
3417 if (lpUrlComponents->lpszUserName)
3419 len = URL_GET_COMP_LENGTHA(lpUrlComponents, UserName) + 1;
3420 urlCompW->lpszUserName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3421 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszUserName,
3422 -1, urlCompW->lpszUserName, len);
3425 if (lpUrlComponents->lpszPassword)
3427 len = URL_GET_COMP_LENGTHA(lpUrlComponents, Password) + 1;
3428 urlCompW->lpszPassword = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3429 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszPassword,
3430 -1, urlCompW->lpszPassword, len);
3433 if (lpUrlComponents->lpszUrlPath)
3435 len = URL_GET_COMP_LENGTHA(lpUrlComponents, UrlPath) + 1;
3436 urlCompW->lpszUrlPath = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3437 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszUrlPath,
3438 -1, urlCompW->lpszUrlPath, len);
3441 if (lpUrlComponents->lpszExtraInfo)
3443 len = URL_GET_COMP_LENGTHA(lpUrlComponents, ExtraInfo) + 1;
3444 urlCompW->lpszExtraInfo = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3445 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszExtraInfo,
3446 -1, urlCompW->lpszExtraInfo, len);
3450 /***********************************************************************
3451 * InternetCreateUrlA (WININET.@)
3453 * See InternetCreateUrlW.
3455 BOOL WINAPI InternetCreateUrlA(LPURL_COMPONENTSA lpUrlComponents, DWORD dwFlags,
3456 LPSTR lpszUrl, LPDWORD lpdwUrlLength)
3458 BOOL ret;
3459 LPWSTR urlW = NULL;
3460 URL_COMPONENTSW urlCompW;
3462 TRACE("(%p,%d,%p,%p)\n", lpUrlComponents, dwFlags, lpszUrl, lpdwUrlLength);
3464 if (!lpUrlComponents || lpUrlComponents->dwStructSize != sizeof(URL_COMPONENTSW) || !lpdwUrlLength)
3466 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
3467 return FALSE;
3470 convert_urlcomp_atow(lpUrlComponents, &urlCompW);
3472 if (lpszUrl)
3473 urlW = HeapAlloc(GetProcessHeap(), 0, *lpdwUrlLength * sizeof(WCHAR));
3475 ret = InternetCreateUrlW(&urlCompW, dwFlags, urlW, lpdwUrlLength);
3477 if (!ret && (GetLastError() == ERROR_INSUFFICIENT_BUFFER))
3478 *lpdwUrlLength /= sizeof(WCHAR);
3480 /* on success, lpdwUrlLength points to the size of urlW in WCHARS
3481 * minus one, so add one to leave room for NULL terminator
3483 if (ret)
3484 WideCharToMultiByte(CP_ACP, 0, urlW, -1, lpszUrl, *lpdwUrlLength + 1, NULL, NULL);
3486 HeapFree(GetProcessHeap(), 0, urlCompW.lpszScheme);
3487 HeapFree(GetProcessHeap(), 0, urlCompW.lpszHostName);
3488 HeapFree(GetProcessHeap(), 0, urlCompW.lpszUserName);
3489 HeapFree(GetProcessHeap(), 0, urlCompW.lpszPassword);
3490 HeapFree(GetProcessHeap(), 0, urlCompW.lpszUrlPath);
3491 HeapFree(GetProcessHeap(), 0, urlCompW.lpszExtraInfo);
3492 HeapFree(GetProcessHeap(), 0, urlW);
3494 return ret;
3497 /***********************************************************************
3498 * InternetCreateUrlW (WININET.@)
3500 * Creates a URL from its component parts.
3502 * PARAMS
3503 * lpUrlComponents [I] URL Components.
3504 * dwFlags [I] Flags. See notes.
3505 * lpszUrl [I] Buffer in which to store the created URL.
3506 * lpdwUrlLength [I/O] On input, the length of the buffer pointed to by
3507 * lpszUrl in characters. On output, the number of bytes
3508 * required to store the URL including terminator.
3510 * NOTES
3512 * The dwFlags parameter can be zero or more of the following:
3513 *|ICU_ESCAPE - Generates escape sequences for unsafe characters in the path and extra info of the URL.
3515 * RETURNS
3516 * TRUE on success
3517 * FALSE on failure
3520 BOOL WINAPI InternetCreateUrlW(LPURL_COMPONENTSW lpUrlComponents, DWORD dwFlags,
3521 LPWSTR lpszUrl, LPDWORD lpdwUrlLength)
3523 DWORD dwLen;
3524 INTERNET_SCHEME nScheme;
3526 static const WCHAR slashSlashW[] = {'/','/'};
3527 static const WCHAR percentD[] = {'%','d',0};
3529 TRACE("(%p,%d,%p,%p)\n", lpUrlComponents, dwFlags, lpszUrl, lpdwUrlLength);
3531 if (!lpUrlComponents || lpUrlComponents->dwStructSize != sizeof(URL_COMPONENTSW) || !lpdwUrlLength)
3533 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
3534 return FALSE;
3537 if (!calc_url_length(lpUrlComponents, &dwLen))
3538 return FALSE;
3540 if (!lpszUrl || *lpdwUrlLength < dwLen)
3542 *lpdwUrlLength = (dwLen + 1) * sizeof(WCHAR);
3543 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
3544 return FALSE;
3547 *lpdwUrlLength = dwLen;
3548 lpszUrl[0] = 0x00;
3550 dwLen = 0;
3552 if (lpUrlComponents->lpszScheme)
3554 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, Scheme);
3555 memcpy(lpszUrl, lpUrlComponents->lpszScheme, dwLen * sizeof(WCHAR));
3556 lpszUrl += dwLen;
3558 nScheme = GetInternetSchemeW(lpUrlComponents->lpszScheme, dwLen);
3560 else
3562 LPCWSTR scheme;
3563 nScheme = lpUrlComponents->nScheme;
3565 if (nScheme == INTERNET_SCHEME_DEFAULT)
3566 nScheme = INTERNET_SCHEME_HTTP;
3568 scheme = INTERNET_GetSchemeString(nScheme);
3569 dwLen = strlenW(scheme);
3570 memcpy(lpszUrl, scheme, dwLen * sizeof(WCHAR));
3571 lpszUrl += dwLen;
3574 /* all schemes are followed by at least a colon */
3575 *lpszUrl = ':';
3576 lpszUrl++;
3578 if (!scheme_is_opaque(nScheme) || lpUrlComponents->lpszHostName)
3580 memcpy(lpszUrl, slashSlashW, sizeof(slashSlashW));
3581 lpszUrl += sizeof(slashSlashW)/sizeof(slashSlashW[0]);
3584 if (lpUrlComponents->lpszUserName)
3586 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, UserName);
3587 memcpy(lpszUrl, lpUrlComponents->lpszUserName, dwLen * sizeof(WCHAR));
3588 lpszUrl += dwLen;
3590 if (lpUrlComponents->lpszPassword)
3592 *lpszUrl = ':';
3593 lpszUrl++;
3595 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, Password);
3596 memcpy(lpszUrl, lpUrlComponents->lpszPassword, dwLen * sizeof(WCHAR));
3597 lpszUrl += dwLen;
3600 *lpszUrl = '@';
3601 lpszUrl++;
3604 if (lpUrlComponents->lpszHostName)
3606 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, HostName);
3607 memcpy(lpszUrl, lpUrlComponents->lpszHostName, dwLen * sizeof(WCHAR));
3608 lpszUrl += dwLen;
3610 if (!url_uses_default_port(nScheme, lpUrlComponents->nPort))
3612 WCHAR szPort[MAX_WORD_DIGITS+1];
3614 sprintfW(szPort, percentD, lpUrlComponents->nPort);
3615 *lpszUrl = ':';
3616 lpszUrl++;
3617 dwLen = strlenW(szPort);
3618 memcpy(lpszUrl, szPort, dwLen * sizeof(WCHAR));
3619 lpszUrl += dwLen;
3622 /* add slash between hostname and path if necessary */
3623 if (lpUrlComponents->lpszUrlPath && *lpUrlComponents->lpszUrlPath != '/')
3625 *lpszUrl = '/';
3626 lpszUrl++;
3631 if (lpUrlComponents->lpszUrlPath)
3633 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, UrlPath);
3634 memcpy(lpszUrl, lpUrlComponents->lpszUrlPath, dwLen * sizeof(WCHAR));
3635 lpszUrl += dwLen;
3638 *lpszUrl = '\0';
3640 return TRUE;
3643 /***********************************************************************
3644 * InternetConfirmZoneCrossingA (WININET.@)
3647 DWORD WINAPI InternetConfirmZoneCrossingA( HWND hWnd, LPSTR szUrlPrev, LPSTR szUrlNew, BOOL bPost )
3649 FIXME("(%p, %s, %s, %x) stub\n", hWnd, debugstr_a(szUrlPrev), debugstr_a(szUrlNew), bPost);
3650 return ERROR_SUCCESS;
3653 /***********************************************************************
3654 * InternetConfirmZoneCrossingW (WININET.@)
3657 DWORD WINAPI InternetConfirmZoneCrossingW( HWND hWnd, LPWSTR szUrlPrev, LPWSTR szUrlNew, BOOL bPost )
3659 FIXME("(%p, %s, %s, %x) stub\n", hWnd, debugstr_w(szUrlPrev), debugstr_w(szUrlNew), bPost);
3660 return ERROR_SUCCESS;
3663 DWORD WINAPI InternetDialA( HWND hwndParent, LPSTR lpszConnectoid, DWORD dwFlags,
3664 DWORD_PTR* lpdwConnection, DWORD dwReserved )
3666 FIXME("(%p, %p, 0x%08x, %p, 0x%08x) stub\n", hwndParent, lpszConnectoid, dwFlags,
3667 lpdwConnection, dwReserved);
3668 return ERROR_SUCCESS;
3671 DWORD WINAPI InternetDialW( HWND hwndParent, LPWSTR lpszConnectoid, DWORD dwFlags,
3672 DWORD_PTR* lpdwConnection, DWORD dwReserved )
3674 FIXME("(%p, %p, 0x%08x, %p, 0x%08x) stub\n", hwndParent, lpszConnectoid, dwFlags,
3675 lpdwConnection, dwReserved);
3676 return ERROR_SUCCESS;
3679 BOOL WINAPI InternetGoOnlineA( LPSTR lpszURL, HWND hwndParent, DWORD dwReserved )
3681 FIXME("(%s, %p, 0x%08x) stub\n", debugstr_a(lpszURL), hwndParent, dwReserved);
3682 return TRUE;
3685 BOOL WINAPI InternetGoOnlineW( LPWSTR lpszURL, HWND hwndParent, DWORD dwReserved )
3687 FIXME("(%s, %p, 0x%08x) stub\n", debugstr_w(lpszURL), hwndParent, dwReserved);
3688 return TRUE;
3691 DWORD WINAPI InternetHangUp( DWORD_PTR dwConnection, DWORD dwReserved )
3693 FIXME("(0x%08lx, 0x%08x) stub\n", dwConnection, dwReserved);
3694 return ERROR_SUCCESS;
3697 BOOL WINAPI CreateMD5SSOHash( PWSTR pszChallengeInfo, PWSTR pwszRealm, PWSTR pwszTarget,
3698 PBYTE pbHexHash )
3700 FIXME("(%s, %s, %s, %p) stub\n", debugstr_w(pszChallengeInfo), debugstr_w(pwszRealm),
3701 debugstr_w(pwszTarget), pbHexHash);
3702 return FALSE;
3705 BOOL WINAPI ResumeSuspendedDownload( HINTERNET hInternet, DWORD dwError )
3707 FIXME("(%p, 0x%08x) stub\n", hInternet, dwError);
3708 return FALSE;
3711 BOOL WINAPI InternetQueryFortezzaStatus(DWORD *a, DWORD_PTR b)
3713 FIXME("(%p, %08lx) stub\n", a, b);
3714 return 0;