wininet: Move InternetQueryOption(INTERNET_OPTION_DATAFILE_NAME).
[wine/wine-kai.git] / dlls / wininet / internet.c
blob89330f521090627315c386059c3e81b14c630893
1 /*
2 * Wininet
4 * Copyright 1999 Corel Corporation
5 * Copyright 2002 CodeWeavers Inc.
6 * Copyright 2002 Jaco Greeff
7 * Copyright 2002 TransGaming Technologies Inc.
8 * Copyright 2004 Mike McCormack for CodeWeavers
10 * Ulrich Czekalla
11 * Aric Stewart
12 * David Hammerton
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
29 #include "config.h"
30 #include "wine/port.h"
32 #define MAXHOSTNAME 100 /* from http.c */
34 #include <string.h>
35 #include <stdarg.h>
36 #include <stdio.h>
37 #include <sys/types.h>
38 #ifdef HAVE_SYS_SOCKET_H
39 # include <sys/socket.h>
40 #endif
41 #ifdef HAVE_SYS_TIME_H
42 # include <sys/time.h>
43 #endif
44 #include <stdlib.h>
45 #include <ctype.h>
46 #ifdef HAVE_UNISTD_H
47 # include <unistd.h>
48 #endif
49 #include <assert.h>
51 #include "windef.h"
52 #include "winbase.h"
53 #include "winreg.h"
54 #include "winuser.h"
55 #include "wininet.h"
56 #include "winineti.h"
57 #include "winnls.h"
58 #include "wine/debug.h"
59 #include "winerror.h"
60 #define NO_SHLWAPI_STREAM
61 #include "shlwapi.h"
62 #include "wincrypt.h"
64 #include "wine/exception.h"
66 #include "internet.h"
67 #include "resource.h"
69 #include "wine/unicode.h"
71 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
73 #define RESPONSE_TIMEOUT 30
75 typedef struct
77 DWORD dwError;
78 CHAR response[MAX_REPLY_LEN];
79 } WITHREADERROR, *LPWITHREADERROR;
81 HINTERNET WINAPI INTERNET_InternetOpenUrlW(LPWININETAPPINFOW hIC, LPCWSTR lpszUrl,
82 LPCWSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext);
84 static DWORD g_dwTlsErrIndex = TLS_OUT_OF_INDEXES;
85 static HMODULE WININET_hModule;
87 #define HANDLE_CHUNK_SIZE 0x10
89 static CRITICAL_SECTION WININET_cs;
90 static CRITICAL_SECTION_DEBUG WININET_cs_debug =
92 0, 0, &WININET_cs,
93 { &WININET_cs_debug.ProcessLocksList, &WININET_cs_debug.ProcessLocksList },
94 0, 0, { (DWORD_PTR)(__FILE__ ": WININET_cs") }
96 static CRITICAL_SECTION WININET_cs = { &WININET_cs_debug, -1, 0, 0, 0, 0 };
98 static LPWININETHANDLEHEADER *WININET_Handles;
99 static UINT WININET_dwNextHandle;
100 static UINT WININET_dwMaxHandles;
102 HINTERNET WININET_AllocHandle( LPWININETHANDLEHEADER info )
104 LPWININETHANDLEHEADER *p;
105 UINT handle = 0, num;
107 list_init( &info->children );
109 EnterCriticalSection( &WININET_cs );
110 if( !WININET_dwMaxHandles )
112 num = HANDLE_CHUNK_SIZE;
113 p = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
114 sizeof (UINT)* num);
115 if( !p )
116 goto end;
117 WININET_Handles = p;
118 WININET_dwMaxHandles = num;
120 if( WININET_dwMaxHandles == WININET_dwNextHandle )
122 num = WININET_dwMaxHandles + HANDLE_CHUNK_SIZE;
123 p = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
124 WININET_Handles, sizeof (UINT)* num);
125 if( !p )
126 goto end;
127 WININET_Handles = p;
128 WININET_dwMaxHandles = num;
131 handle = WININET_dwNextHandle;
132 if( WININET_Handles[handle] )
133 ERR("handle isn't free but should be\n");
134 WININET_Handles[handle] = WININET_AddRef( info );
136 while( WININET_Handles[WININET_dwNextHandle] &&
137 (WININET_dwNextHandle < WININET_dwMaxHandles ) )
138 WININET_dwNextHandle++;
140 end:
141 LeaveCriticalSection( &WININET_cs );
143 return info->hInternet = (HINTERNET) (handle+1);
146 LPWININETHANDLEHEADER WININET_AddRef( LPWININETHANDLEHEADER info )
148 ULONG refs = InterlockedIncrement(&info->refs);
149 TRACE("%p -> refcount = %d\n", info, refs );
150 return info;
153 LPWININETHANDLEHEADER WININET_GetObject( HINTERNET hinternet )
155 LPWININETHANDLEHEADER info = NULL;
156 UINT handle = (UINT) hinternet;
158 EnterCriticalSection( &WININET_cs );
160 if( (handle > 0) && ( handle <= WININET_dwMaxHandles ) &&
161 WININET_Handles[handle-1] )
162 info = WININET_AddRef( WININET_Handles[handle-1] );
164 LeaveCriticalSection( &WININET_cs );
166 TRACE("handle %d -> %p\n", handle, info);
168 return info;
171 BOOL WININET_Release( LPWININETHANDLEHEADER info )
173 ULONG refs = InterlockedDecrement(&info->refs);
174 TRACE( "object %p refcount = %d\n", info, refs );
175 if( !refs )
177 if ( info->vtbl->CloseConnection )
179 TRACE( "closing connection %p\n", info);
180 info->vtbl->CloseConnection( info );
182 INTERNET_SendCallback(info, info->dwContext,
183 INTERNET_STATUS_HANDLE_CLOSING, &info->hInternet,
184 sizeof(HINTERNET));
185 TRACE( "destroying object %p\n", info);
186 if ( info->htype != WH_HINIT )
187 list_remove( &info->entry );
188 info->vtbl->Destroy( info );
190 return TRUE;
193 BOOL WININET_FreeHandle( HINTERNET hinternet )
195 BOOL ret = FALSE;
196 UINT handle = (UINT) hinternet;
197 LPWININETHANDLEHEADER info = NULL, child, next;
199 EnterCriticalSection( &WININET_cs );
201 if( (handle > 0) && ( handle <= WININET_dwMaxHandles ) )
203 handle--;
204 if( WININET_Handles[handle] )
206 info = WININET_Handles[handle];
207 TRACE( "destroying handle %d for object %p\n", handle+1, info);
208 WININET_Handles[handle] = NULL;
209 ret = TRUE;
213 LeaveCriticalSection( &WININET_cs );
215 /* As on native when the equivalent of WININET_Release is called, the handle
216 * is already invalid, but if a new handle is created at this time it does
217 * not yet get assigned the freed handle number */
218 if( info )
220 /* Free all children as native does */
221 LIST_FOR_EACH_ENTRY_SAFE( child, next, &info->children, WININETHANDLEHEADER, entry )
223 TRACE( "freeing child handle %d for parent handle %d\n",
224 (UINT)child->hInternet, handle+1);
225 WININET_FreeHandle( child->hInternet );
227 WININET_Release( info );
230 EnterCriticalSection( &WININET_cs );
232 if( WININET_dwNextHandle > handle && !WININET_Handles[handle] )
233 WININET_dwNextHandle = handle;
235 LeaveCriticalSection( &WININET_cs );
237 return ret;
240 /***********************************************************************
241 * DllMain [Internal] Initializes the internal 'WININET.DLL'.
243 * PARAMS
244 * hinstDLL [I] handle to the DLL's instance
245 * fdwReason [I]
246 * lpvReserved [I] reserved, must be NULL
248 * RETURNS
249 * Success: TRUE
250 * Failure: FALSE
253 BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
255 TRACE("%p,%x,%p\n", hinstDLL, fdwReason, lpvReserved);
257 switch (fdwReason) {
258 case DLL_PROCESS_ATTACH:
260 g_dwTlsErrIndex = TlsAlloc();
262 if (g_dwTlsErrIndex == TLS_OUT_OF_INDEXES)
263 return FALSE;
265 URLCacheContainers_CreateDefaults();
267 WININET_hModule = hinstDLL;
269 case DLL_THREAD_ATTACH:
270 break;
272 case DLL_THREAD_DETACH:
273 if (g_dwTlsErrIndex != TLS_OUT_OF_INDEXES)
275 LPVOID lpwite = TlsGetValue(g_dwTlsErrIndex);
276 HeapFree(GetProcessHeap(), 0, lpwite);
278 break;
280 case DLL_PROCESS_DETACH:
282 URLCacheContainers_DeleteAll();
284 if (g_dwTlsErrIndex != TLS_OUT_OF_INDEXES)
286 HeapFree(GetProcessHeap(), 0, TlsGetValue(g_dwTlsErrIndex));
287 TlsFree(g_dwTlsErrIndex);
289 break;
292 return TRUE;
296 /***********************************************************************
297 * InternetInitializeAutoProxyDll (WININET.@)
299 * Setup the internal proxy
301 * PARAMETERS
302 * dwReserved
304 * RETURNS
305 * FALSE on failure
308 BOOL WINAPI InternetInitializeAutoProxyDll(DWORD dwReserved)
310 FIXME("STUB\n");
311 INTERNET_SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
312 return FALSE;
315 /***********************************************************************
316 * DetectAutoProxyUrl (WININET.@)
318 * Auto detect the proxy url
320 * RETURNS
321 * FALSE on failure
324 BOOL WINAPI DetectAutoProxyUrl(LPSTR lpszAutoProxyUrl,
325 DWORD dwAutoProxyUrlLength, DWORD dwDetectFlags)
327 FIXME("STUB\n");
328 INTERNET_SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
329 return FALSE;
333 /***********************************************************************
334 * INTERNET_ConfigureProxyFromReg
336 * FIXME:
337 * The proxy may be specified in the form 'http=proxy.my.org'
338 * Presumably that means there can be ftp=ftpproxy.my.org too.
340 static BOOL INTERNET_ConfigureProxyFromReg( LPWININETAPPINFOW lpwai )
342 HKEY key;
343 DWORD r, keytype, len, enabled;
344 LPCSTR lpszInternetSettings =
345 "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings";
346 static const WCHAR szProxyServer[] = { 'P','r','o','x','y','S','e','r','v','e','r', 0 };
348 r = RegOpenKeyA(HKEY_CURRENT_USER, lpszInternetSettings, &key);
349 if ( r != ERROR_SUCCESS )
350 return FALSE;
352 len = sizeof enabled;
353 r = RegQueryValueExA( key, "ProxyEnable", NULL, &keytype,
354 (BYTE*)&enabled, &len);
355 if( (r == ERROR_SUCCESS) && enabled )
357 TRACE("Proxy is enabled.\n");
359 /* figure out how much memory the proxy setting takes */
360 r = RegQueryValueExW( key, szProxyServer, NULL, &keytype,
361 NULL, &len);
362 if( (r == ERROR_SUCCESS) && len && (keytype == REG_SZ) )
364 LPWSTR szProxy, p;
365 static const WCHAR szHttp[] = {'h','t','t','p','=',0};
367 szProxy=HeapAlloc( GetProcessHeap(), 0, len );
368 RegQueryValueExW( key, szProxyServer, NULL, &keytype,
369 (BYTE*)szProxy, &len);
371 /* find the http proxy, and strip away everything else */
372 p = strstrW( szProxy, szHttp );
373 if( p )
375 p += lstrlenW(szHttp);
376 lstrcpyW( szProxy, p );
378 p = strchrW( szProxy, ' ' );
379 if( p )
380 *p = 0;
382 lpwai->dwAccessType = INTERNET_OPEN_TYPE_PROXY;
383 lpwai->lpszProxy = szProxy;
385 TRACE("http proxy = %s\n", debugstr_w(lpwai->lpszProxy));
387 else
388 ERR("Couldn't read proxy server settings.\n");
390 else
391 TRACE("Proxy is not enabled.\n");
392 RegCloseKey(key);
394 return enabled;
397 /***********************************************************************
398 * dump_INTERNET_FLAGS
400 * Helper function to TRACE the internet flags.
402 * RETURNS
403 * None
406 static void dump_INTERNET_FLAGS(DWORD dwFlags)
408 #define FE(x) { x, #x }
409 static const wininet_flag_info flag[] = {
410 FE(INTERNET_FLAG_RELOAD),
411 FE(INTERNET_FLAG_RAW_DATA),
412 FE(INTERNET_FLAG_EXISTING_CONNECT),
413 FE(INTERNET_FLAG_ASYNC),
414 FE(INTERNET_FLAG_PASSIVE),
415 FE(INTERNET_FLAG_NO_CACHE_WRITE),
416 FE(INTERNET_FLAG_MAKE_PERSISTENT),
417 FE(INTERNET_FLAG_FROM_CACHE),
418 FE(INTERNET_FLAG_SECURE),
419 FE(INTERNET_FLAG_KEEP_CONNECTION),
420 FE(INTERNET_FLAG_NO_AUTO_REDIRECT),
421 FE(INTERNET_FLAG_READ_PREFETCH),
422 FE(INTERNET_FLAG_NO_COOKIES),
423 FE(INTERNET_FLAG_NO_AUTH),
424 FE(INTERNET_FLAG_CACHE_IF_NET_FAIL),
425 FE(INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP),
426 FE(INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTPS),
427 FE(INTERNET_FLAG_IGNORE_CERT_DATE_INVALID),
428 FE(INTERNET_FLAG_IGNORE_CERT_CN_INVALID),
429 FE(INTERNET_FLAG_RESYNCHRONIZE),
430 FE(INTERNET_FLAG_HYPERLINK),
431 FE(INTERNET_FLAG_NO_UI),
432 FE(INTERNET_FLAG_PRAGMA_NOCACHE),
433 FE(INTERNET_FLAG_CACHE_ASYNC),
434 FE(INTERNET_FLAG_FORMS_SUBMIT),
435 FE(INTERNET_FLAG_NEED_FILE),
436 FE(INTERNET_FLAG_TRANSFER_ASCII),
437 FE(INTERNET_FLAG_TRANSFER_BINARY)
439 #undef FE
440 int i;
442 for (i = 0; i < (sizeof(flag) / sizeof(flag[0])); i++) {
443 if (flag[i].val & dwFlags) {
444 TRACE(" %s", flag[i].name);
445 dwFlags &= ~flag[i].val;
448 if (dwFlags)
449 TRACE(" Unknown flags (%08x)\n", dwFlags);
450 else
451 TRACE("\n");
454 /***********************************************************************
455 * INTERNET_CloseHandle (internal)
457 * Close internet handle
460 static VOID APPINFO_Destroy(WININETHANDLEHEADER *hdr)
462 LPWININETAPPINFOW lpwai = (LPWININETAPPINFOW) hdr;
464 TRACE("%p\n",lpwai);
466 HeapFree(GetProcessHeap(), 0, lpwai->lpszAgent);
467 HeapFree(GetProcessHeap(), 0, lpwai->lpszProxy);
468 HeapFree(GetProcessHeap(), 0, lpwai->lpszProxyBypass);
469 HeapFree(GetProcessHeap(), 0, lpwai->lpszProxyUsername);
470 HeapFree(GetProcessHeap(), 0, lpwai->lpszProxyPassword);
471 HeapFree(GetProcessHeap(), 0, lpwai);
474 static DWORD APPINFO_QueryOption(WININETHANDLEHEADER *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
476 switch(option) {
477 case INTERNET_OPTION_HANDLE_TYPE:
478 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
480 if (*size < sizeof(ULONG))
481 return ERROR_INSUFFICIENT_BUFFER;
483 *size = sizeof(DWORD);
484 *(DWORD*)buffer = INTERNET_HANDLE_TYPE_INTERNET;
485 return ERROR_SUCCESS;
488 FIXME("Not implemented option %d\n", option);
489 return ERROR_INTERNET_INVALID_OPTION;
492 static const HANDLEHEADERVtbl APPINFOVtbl = {
493 APPINFO_Destroy,
494 NULL,
495 APPINFO_QueryOption,
496 NULL,
497 NULL,
498 NULL,
499 NULL,
500 NULL,
501 NULL
505 /***********************************************************************
506 * InternetOpenW (WININET.@)
508 * Per-application initialization of wininet
510 * RETURNS
511 * HINTERNET on success
512 * NULL on failure
515 HINTERNET WINAPI InternetOpenW(LPCWSTR lpszAgent, DWORD dwAccessType,
516 LPCWSTR lpszProxy, LPCWSTR lpszProxyBypass, DWORD dwFlags)
518 LPWININETAPPINFOW lpwai = NULL;
519 HINTERNET handle = NULL;
521 if (TRACE_ON(wininet)) {
522 #define FE(x) { x, #x }
523 static const wininet_flag_info access_type[] = {
524 FE(INTERNET_OPEN_TYPE_PRECONFIG),
525 FE(INTERNET_OPEN_TYPE_DIRECT),
526 FE(INTERNET_OPEN_TYPE_PROXY),
527 FE(INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY)
529 #undef FE
530 DWORD i;
531 const char *access_type_str = "Unknown";
533 TRACE("(%s, %i, %s, %s, %i)\n", debugstr_w(lpszAgent), dwAccessType,
534 debugstr_w(lpszProxy), debugstr_w(lpszProxyBypass), dwFlags);
535 for (i = 0; i < (sizeof(access_type) / sizeof(access_type[0])); i++) {
536 if (access_type[i].val == dwAccessType) {
537 access_type_str = access_type[i].name;
538 break;
541 TRACE(" access type : %s\n", access_type_str);
542 TRACE(" flags :");
543 dump_INTERNET_FLAGS(dwFlags);
546 /* Clear any error information */
547 INTERNET_SetLastError(0);
549 lpwai = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETAPPINFOW));
550 if (NULL == lpwai)
552 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
553 goto lend;
556 lpwai->hdr.htype = WH_HINIT;
557 lpwai->hdr.vtbl = &APPINFOVtbl;
558 lpwai->hdr.dwFlags = dwFlags;
559 lpwai->hdr.refs = 1;
560 lpwai->dwAccessType = dwAccessType;
561 lpwai->lpszProxyUsername = NULL;
562 lpwai->lpszProxyPassword = NULL;
564 handle = WININET_AllocHandle( &lpwai->hdr );
565 if( !handle )
567 HeapFree( GetProcessHeap(), 0, lpwai );
568 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
569 goto lend;
572 if (NULL != lpszAgent)
574 lpwai->lpszAgent = HeapAlloc( GetProcessHeap(),0,
575 (strlenW(lpszAgent)+1)*sizeof(WCHAR));
576 if (lpwai->lpszAgent)
577 lstrcpyW( lpwai->lpszAgent, lpszAgent );
579 if(dwAccessType == INTERNET_OPEN_TYPE_PRECONFIG)
580 INTERNET_ConfigureProxyFromReg( lpwai );
581 else if (NULL != lpszProxy)
583 lpwai->lpszProxy = HeapAlloc( GetProcessHeap(), 0,
584 (strlenW(lpszProxy)+1)*sizeof(WCHAR));
585 if (lpwai->lpszProxy)
586 lstrcpyW( lpwai->lpszProxy, lpszProxy );
589 if (NULL != lpszProxyBypass)
591 lpwai->lpszProxyBypass = HeapAlloc( GetProcessHeap(), 0,
592 (strlenW(lpszProxyBypass)+1)*sizeof(WCHAR));
593 if (lpwai->lpszProxyBypass)
594 lstrcpyW( lpwai->lpszProxyBypass, lpszProxyBypass );
597 lend:
598 if( lpwai )
599 WININET_Release( &lpwai->hdr );
601 TRACE("returning %p\n", lpwai);
603 return handle;
607 /***********************************************************************
608 * InternetOpenA (WININET.@)
610 * Per-application initialization of wininet
612 * RETURNS
613 * HINTERNET on success
614 * NULL on failure
617 HINTERNET WINAPI InternetOpenA(LPCSTR lpszAgent, DWORD dwAccessType,
618 LPCSTR lpszProxy, LPCSTR lpszProxyBypass, DWORD dwFlags)
620 HINTERNET rc = NULL;
621 INT len;
622 WCHAR *szAgent = NULL, *szProxy = NULL, *szBypass = NULL;
624 TRACE("(%s, 0x%08x, %s, %s, 0x%08x)\n", debugstr_a(lpszAgent),
625 dwAccessType, debugstr_a(lpszProxy), debugstr_a(lpszProxyBypass), dwFlags);
627 if( lpszAgent )
629 len = MultiByteToWideChar(CP_ACP, 0, lpszAgent, -1, NULL, 0);
630 szAgent = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
631 MultiByteToWideChar(CP_ACP, 0, lpszAgent, -1, szAgent, len);
634 if( lpszProxy )
636 len = MultiByteToWideChar(CP_ACP, 0, lpszProxy, -1, NULL, 0);
637 szProxy = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
638 MultiByteToWideChar(CP_ACP, 0, lpszProxy, -1, szProxy, len);
641 if( lpszProxyBypass )
643 len = MultiByteToWideChar(CP_ACP, 0, lpszProxyBypass, -1, NULL, 0);
644 szBypass = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
645 MultiByteToWideChar(CP_ACP, 0, lpszProxyBypass, -1, szBypass, len);
648 rc = InternetOpenW(szAgent, dwAccessType, szProxy, szBypass, dwFlags);
650 HeapFree(GetProcessHeap(), 0, szAgent);
651 HeapFree(GetProcessHeap(), 0, szProxy);
652 HeapFree(GetProcessHeap(), 0, szBypass);
654 return rc;
657 /***********************************************************************
658 * InternetGetLastResponseInfoA (WININET.@)
660 * Return last wininet error description on the calling thread
662 * RETURNS
663 * TRUE on success of writing to buffer
664 * FALSE on failure
667 BOOL WINAPI InternetGetLastResponseInfoA(LPDWORD lpdwError,
668 LPSTR lpszBuffer, LPDWORD lpdwBufferLength)
670 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
672 TRACE("\n");
674 if (lpwite)
676 *lpdwError = lpwite->dwError;
677 if (lpwite->dwError)
679 memcpy(lpszBuffer, lpwite->response, *lpdwBufferLength);
680 *lpdwBufferLength = strlen(lpszBuffer);
682 else
683 *lpdwBufferLength = 0;
685 else
687 *lpdwError = 0;
688 *lpdwBufferLength = 0;
691 return TRUE;
694 /***********************************************************************
695 * InternetGetLastResponseInfoW (WININET.@)
697 * Return last wininet error description on the calling thread
699 * RETURNS
700 * TRUE on success of writing to buffer
701 * FALSE on failure
704 BOOL WINAPI InternetGetLastResponseInfoW(LPDWORD lpdwError,
705 LPWSTR lpszBuffer, LPDWORD lpdwBufferLength)
707 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
709 TRACE("\n");
711 if (lpwite)
713 *lpdwError = lpwite->dwError;
714 if (lpwite->dwError)
716 memcpy(lpszBuffer, lpwite->response, *lpdwBufferLength);
717 *lpdwBufferLength = lstrlenW(lpszBuffer);
719 else
720 *lpdwBufferLength = 0;
722 else
724 *lpdwError = 0;
725 *lpdwBufferLength = 0;
728 return TRUE;
731 /***********************************************************************
732 * InternetGetConnectedState (WININET.@)
734 * Return connected state
736 * RETURNS
737 * TRUE if connected
738 * if lpdwStatus is not null, return the status (off line,
739 * modem, lan...) in it.
740 * FALSE if not connected
742 BOOL WINAPI InternetGetConnectedState(LPDWORD lpdwStatus, DWORD dwReserved)
744 TRACE("(%p, 0x%08x)\n", lpdwStatus, dwReserved);
746 if (lpdwStatus) {
747 FIXME("always returning LAN connection.\n");
748 *lpdwStatus = INTERNET_CONNECTION_LAN;
750 return TRUE;
754 /***********************************************************************
755 * InternetGetConnectedStateExW (WININET.@)
757 * Return connected state
759 * PARAMS
761 * lpdwStatus [O] Flags specifying the status of the internet connection.
762 * lpszConnectionName [O] Pointer to buffer to receive the friendly name of the internet connection.
763 * dwNameLen [I] Size of the buffer, in characters.
764 * dwReserved [I] Reserved. Must be set to 0.
766 * RETURNS
767 * TRUE if connected
768 * if lpdwStatus is not null, return the status (off line,
769 * modem, lan...) in it.
770 * FALSE if not connected
772 * NOTES
773 * If the system has no available network connections, an empty string is
774 * stored in lpszConnectionName. If there is a LAN connection, a localized
775 * "LAN Connection" string is stored. Presumably, if only a dial-up
776 * connection is available then the name of the dial-up connection is
777 * returned. Why any application, other than the "Internet Settings" CPL,
778 * would want to use this function instead of the simpler InternetGetConnectedStateW
779 * function is beyond me.
781 BOOL WINAPI InternetGetConnectedStateExW(LPDWORD lpdwStatus, LPWSTR lpszConnectionName,
782 DWORD dwNameLen, DWORD dwReserved)
784 TRACE("(%p, %p, %d, 0x%08x)\n", lpdwStatus, lpszConnectionName, dwNameLen, dwReserved);
786 /* Must be zero */
787 if(dwReserved)
788 return FALSE;
790 if (lpdwStatus) {
791 FIXME("always returning LAN connection.\n");
792 *lpdwStatus = INTERNET_CONNECTION_LAN;
794 return LoadStringW(WININET_hModule, IDS_LANCONNECTION, lpszConnectionName, dwNameLen);
798 /***********************************************************************
799 * InternetGetConnectedStateExA (WININET.@)
801 BOOL WINAPI InternetGetConnectedStateExA(LPDWORD lpdwStatus, LPSTR lpszConnectionName,
802 DWORD dwNameLen, DWORD dwReserved)
804 LPWSTR lpwszConnectionName = NULL;
805 BOOL rc;
807 TRACE("(%p, %p, %d, 0x%08x)\n", lpdwStatus, lpszConnectionName, dwNameLen, dwReserved);
809 if (lpszConnectionName && dwNameLen > 0)
810 lpwszConnectionName= HeapAlloc(GetProcessHeap(), 0, dwNameLen * sizeof(WCHAR));
812 rc = InternetGetConnectedStateExW(lpdwStatus,lpwszConnectionName, dwNameLen,
813 dwReserved);
814 if (rc && lpwszConnectionName)
816 WideCharToMultiByte(CP_ACP,0,lpwszConnectionName,-1,lpszConnectionName,
817 dwNameLen, NULL, NULL);
819 HeapFree(GetProcessHeap(),0,lpwszConnectionName);
822 return rc;
826 /***********************************************************************
827 * InternetConnectW (WININET.@)
829 * Open a ftp, gopher or http session
831 * RETURNS
832 * HINTERNET a session handle on success
833 * NULL on failure
836 HINTERNET WINAPI InternetConnectW(HINTERNET hInternet,
837 LPCWSTR lpszServerName, INTERNET_PORT nServerPort,
838 LPCWSTR lpszUserName, LPCWSTR lpszPassword,
839 DWORD dwService, DWORD dwFlags, DWORD_PTR dwContext)
841 LPWININETAPPINFOW hIC;
842 HINTERNET rc = NULL;
844 TRACE("(%p, %s, %i, %s, %s, %i, %i, %lx)\n", hInternet, debugstr_w(lpszServerName),
845 nServerPort, debugstr_w(lpszUserName), debugstr_w(lpszPassword),
846 dwService, dwFlags, dwContext);
848 if (!lpszServerName)
850 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
851 return NULL;
854 /* Clear any error information */
855 INTERNET_SetLastError(0);
856 hIC = (LPWININETAPPINFOW) WININET_GetObject( hInternet );
857 if ( (hIC == NULL) || (hIC->hdr.htype != WH_HINIT) )
859 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
860 goto lend;
863 switch (dwService)
865 case INTERNET_SERVICE_FTP:
866 rc = FTP_Connect(hIC, lpszServerName, nServerPort,
867 lpszUserName, lpszPassword, dwFlags, dwContext, 0);
868 break;
870 case INTERNET_SERVICE_HTTP:
871 rc = HTTP_Connect(hIC, lpszServerName, nServerPort,
872 lpszUserName, lpszPassword, dwFlags, dwContext, 0);
873 break;
875 case INTERNET_SERVICE_GOPHER:
876 default:
877 break;
879 lend:
880 if( hIC )
881 WININET_Release( &hIC->hdr );
883 TRACE("returning %p\n", rc);
884 return rc;
888 /***********************************************************************
889 * InternetConnectA (WININET.@)
891 * Open a ftp, gopher or http session
893 * RETURNS
894 * HINTERNET a session handle on success
895 * NULL on failure
898 HINTERNET WINAPI InternetConnectA(HINTERNET hInternet,
899 LPCSTR lpszServerName, INTERNET_PORT nServerPort,
900 LPCSTR lpszUserName, LPCSTR lpszPassword,
901 DWORD dwService, DWORD dwFlags, DWORD_PTR dwContext)
903 HINTERNET rc = NULL;
904 INT len = 0;
905 LPWSTR szServerName = NULL;
906 LPWSTR szUserName = NULL;
907 LPWSTR szPassword = NULL;
909 if (lpszServerName)
911 len = MultiByteToWideChar(CP_ACP, 0, lpszServerName, -1, NULL, 0);
912 szServerName = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
913 MultiByteToWideChar(CP_ACP, 0, lpszServerName, -1, szServerName, len);
915 if (lpszUserName)
917 len = MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, NULL, 0);
918 szUserName = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
919 MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, szUserName, len);
921 if (lpszPassword)
923 len = MultiByteToWideChar(CP_ACP, 0, lpszPassword, -1, NULL, 0);
924 szPassword = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
925 MultiByteToWideChar(CP_ACP, 0, lpszPassword, -1, szPassword, len);
929 rc = InternetConnectW(hInternet, szServerName, nServerPort,
930 szUserName, szPassword, dwService, dwFlags, dwContext);
932 HeapFree(GetProcessHeap(), 0, szServerName);
933 HeapFree(GetProcessHeap(), 0, szUserName);
934 HeapFree(GetProcessHeap(), 0, szPassword);
935 return rc;
939 /***********************************************************************
940 * InternetFindNextFileA (WININET.@)
942 * Continues a file search from a previous call to FindFirstFile
944 * RETURNS
945 * TRUE on success
946 * FALSE on failure
949 BOOL WINAPI InternetFindNextFileA(HINTERNET hFind, LPVOID lpvFindData)
951 BOOL ret;
952 WIN32_FIND_DATAW fd;
954 ret = InternetFindNextFileW(hFind, lpvFindData?&fd:NULL);
955 if(lpvFindData)
956 WININET_find_data_WtoA(&fd, (LPWIN32_FIND_DATAA)lpvFindData);
957 return ret;
960 /***********************************************************************
961 * InternetFindNextFileW (WININET.@)
963 * Continues a file search from a previous call to FindFirstFile
965 * RETURNS
966 * TRUE on success
967 * FALSE on failure
970 BOOL WINAPI InternetFindNextFileW(HINTERNET hFind, LPVOID lpvFindData)
972 WININETHANDLEHEADER *hdr;
973 DWORD res;
975 TRACE("\n");
977 hdr = WININET_GetObject(hFind);
978 if(!hdr) {
979 WARN("Invalid handle\n");
980 SetLastError(ERROR_INVALID_HANDLE);
981 return FALSE;
984 if(hdr->vtbl->FindNextFileW) {
985 res = hdr->vtbl->FindNextFileW(hdr, lpvFindData);
986 }else {
987 WARN("Handle doesn't support NextFile\n");
988 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
991 WININET_Release(hdr);
993 if(res != ERROR_SUCCESS)
994 SetLastError(res);
995 return res == ERROR_SUCCESS;
998 /***********************************************************************
999 * InternetCloseHandle (WININET.@)
1001 * Generic close handle function
1003 * RETURNS
1004 * TRUE on success
1005 * FALSE on failure
1008 BOOL WINAPI InternetCloseHandle(HINTERNET hInternet)
1010 LPWININETHANDLEHEADER lpwh;
1012 TRACE("%p\n",hInternet);
1014 lpwh = WININET_GetObject( hInternet );
1015 if (NULL == lpwh)
1017 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
1018 return FALSE;
1021 WININET_Release( lpwh );
1022 WININET_FreeHandle( hInternet );
1024 return TRUE;
1028 /***********************************************************************
1029 * ConvertUrlComponentValue (Internal)
1031 * Helper function for InternetCrackUrlW
1034 static void ConvertUrlComponentValue(LPSTR* lppszComponent, LPDWORD dwComponentLen,
1035 LPWSTR lpwszComponent, DWORD dwwComponentLen,
1036 LPCSTR lpszStart, LPCWSTR lpwszStart)
1038 TRACE("%p %d %p %d %p %p\n", lppszComponent, *dwComponentLen, lpwszComponent, dwwComponentLen, lpszStart, lpwszStart);
1039 if (*dwComponentLen != 0)
1041 DWORD nASCIILength=WideCharToMultiByte(CP_ACP,0,lpwszComponent,dwwComponentLen,NULL,0,NULL,NULL);
1042 if (*lppszComponent == NULL)
1044 int nASCIIOffset=WideCharToMultiByte(CP_ACP,0,lpwszStart,lpwszComponent-lpwszStart,NULL,0,NULL,NULL);
1045 if (lpwszComponent)
1046 *lppszComponent = (LPSTR)lpszStart+nASCIIOffset;
1047 else
1048 *lppszComponent = NULL;
1049 *dwComponentLen = nASCIILength;
1051 else
1053 DWORD ncpylen = min((*dwComponentLen)-1, nASCIILength);
1054 WideCharToMultiByte(CP_ACP,0,lpwszComponent,dwwComponentLen,*lppszComponent,ncpylen+1,NULL,NULL);
1055 (*lppszComponent)[ncpylen]=0;
1056 *dwComponentLen = ncpylen;
1062 /***********************************************************************
1063 * InternetCrackUrlA (WININET.@)
1065 * See InternetCrackUrlW.
1067 BOOL WINAPI InternetCrackUrlA(LPCSTR lpszUrl, DWORD dwUrlLength, DWORD dwFlags,
1068 LPURL_COMPONENTSA lpUrlComponents)
1070 DWORD nLength;
1071 URL_COMPONENTSW UCW;
1072 WCHAR* lpwszUrl;
1074 TRACE("(%s %u %x %p)\n", debugstr_a(lpszUrl), dwUrlLength, dwFlags, lpUrlComponents);
1076 if (!lpszUrl || !*lpszUrl || !lpUrlComponents ||
1077 lpUrlComponents->dwStructSize != sizeof(URL_COMPONENTSA))
1079 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1080 return FALSE;
1083 if(dwUrlLength<=0)
1084 dwUrlLength=-1;
1085 nLength=MultiByteToWideChar(CP_ACP,0,lpszUrl,dwUrlLength,NULL,0);
1087 /* if dwUrlLength=-1 then nLength includes null but length to
1088 InternetCrackUrlW should not include it */
1089 if (dwUrlLength == -1) nLength--;
1091 lpwszUrl=HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WCHAR)*nLength);
1092 MultiByteToWideChar(CP_ACP,0,lpszUrl,dwUrlLength,lpwszUrl,nLength);
1094 memset(&UCW,0,sizeof(UCW));
1095 UCW.dwStructSize = sizeof(URL_COMPONENTSW);
1096 if(lpUrlComponents->dwHostNameLength!=0)
1097 UCW.dwHostNameLength= lpUrlComponents->dwHostNameLength;
1098 if(lpUrlComponents->dwUserNameLength!=0)
1099 UCW.dwUserNameLength=lpUrlComponents->dwUserNameLength;
1100 if(lpUrlComponents->dwPasswordLength!=0)
1101 UCW.dwPasswordLength=lpUrlComponents->dwPasswordLength;
1102 if(lpUrlComponents->dwUrlPathLength!=0)
1103 UCW.dwUrlPathLength=lpUrlComponents->dwUrlPathLength;
1104 if(lpUrlComponents->dwSchemeLength!=0)
1105 UCW.dwSchemeLength=lpUrlComponents->dwSchemeLength;
1106 if(lpUrlComponents->dwExtraInfoLength!=0)
1107 UCW.dwExtraInfoLength=lpUrlComponents->dwExtraInfoLength;
1108 if(!InternetCrackUrlW(lpwszUrl,nLength,dwFlags,&UCW))
1110 HeapFree(GetProcessHeap(), 0, lpwszUrl);
1111 return FALSE;
1114 ConvertUrlComponentValue(&lpUrlComponents->lpszHostName, &lpUrlComponents->dwHostNameLength,
1115 UCW.lpszHostName, UCW.dwHostNameLength,
1116 lpszUrl, lpwszUrl);
1117 ConvertUrlComponentValue(&lpUrlComponents->lpszUserName, &lpUrlComponents->dwUserNameLength,
1118 UCW.lpszUserName, UCW.dwUserNameLength,
1119 lpszUrl, lpwszUrl);
1120 ConvertUrlComponentValue(&lpUrlComponents->lpszPassword, &lpUrlComponents->dwPasswordLength,
1121 UCW.lpszPassword, UCW.dwPasswordLength,
1122 lpszUrl, lpwszUrl);
1123 ConvertUrlComponentValue(&lpUrlComponents->lpszUrlPath, &lpUrlComponents->dwUrlPathLength,
1124 UCW.lpszUrlPath, UCW.dwUrlPathLength,
1125 lpszUrl, lpwszUrl);
1126 ConvertUrlComponentValue(&lpUrlComponents->lpszScheme, &lpUrlComponents->dwSchemeLength,
1127 UCW.lpszScheme, UCW.dwSchemeLength,
1128 lpszUrl, lpwszUrl);
1129 ConvertUrlComponentValue(&lpUrlComponents->lpszExtraInfo, &lpUrlComponents->dwExtraInfoLength,
1130 UCW.lpszExtraInfo, UCW.dwExtraInfoLength,
1131 lpszUrl, lpwszUrl);
1132 lpUrlComponents->nScheme=UCW.nScheme;
1133 lpUrlComponents->nPort=UCW.nPort;
1134 HeapFree(GetProcessHeap(), 0, lpwszUrl);
1136 TRACE("%s: scheme(%s) host(%s) path(%s) extra(%s)\n", lpszUrl,
1137 debugstr_an(lpUrlComponents->lpszScheme,lpUrlComponents->dwSchemeLength),
1138 debugstr_an(lpUrlComponents->lpszHostName,lpUrlComponents->dwHostNameLength),
1139 debugstr_an(lpUrlComponents->lpszUrlPath,lpUrlComponents->dwUrlPathLength),
1140 debugstr_an(lpUrlComponents->lpszExtraInfo,lpUrlComponents->dwExtraInfoLength));
1142 return TRUE;
1145 static const WCHAR url_schemes[][7] =
1147 {'f','t','p',0},
1148 {'g','o','p','h','e','r',0},
1149 {'h','t','t','p',0},
1150 {'h','t','t','p','s',0},
1151 {'f','i','l','e',0},
1152 {'n','e','w','s',0},
1153 {'m','a','i','l','t','o',0},
1154 {'r','e','s',0},
1157 /***********************************************************************
1158 * GetInternetSchemeW (internal)
1160 * Get scheme of url
1162 * RETURNS
1163 * scheme on success
1164 * INTERNET_SCHEME_UNKNOWN on failure
1167 static INTERNET_SCHEME GetInternetSchemeW(LPCWSTR lpszScheme, DWORD nMaxCmp)
1169 int i;
1171 TRACE("%s %d\n",debugstr_wn(lpszScheme, nMaxCmp), nMaxCmp);
1173 if(lpszScheme==NULL)
1174 return INTERNET_SCHEME_UNKNOWN;
1176 for (i = 0; i < sizeof(url_schemes)/sizeof(url_schemes[0]); i++)
1177 if (!strncmpW(lpszScheme, url_schemes[i], nMaxCmp))
1178 return INTERNET_SCHEME_FIRST + i;
1180 return INTERNET_SCHEME_UNKNOWN;
1183 /***********************************************************************
1184 * SetUrlComponentValueW (Internal)
1186 * Helper function for InternetCrackUrlW
1188 * PARAMS
1189 * lppszComponent [O] Holds the returned string
1190 * dwComponentLen [I] Holds the size of lppszComponent
1191 * [O] Holds the length of the string in lppszComponent without '\0'
1192 * lpszStart [I] Holds the string to copy from
1193 * len [I] Holds the length of lpszStart without '\0'
1195 * RETURNS
1196 * TRUE on success
1197 * FALSE on failure
1200 static BOOL SetUrlComponentValueW(LPWSTR* lppszComponent, LPDWORD dwComponentLen, LPCWSTR lpszStart, DWORD len)
1202 TRACE("%s (%d)\n", debugstr_wn(lpszStart,len), len);
1204 if ( (*dwComponentLen == 0) && (*lppszComponent == NULL) )
1205 return FALSE;
1207 if (*dwComponentLen != 0 || *lppszComponent == NULL)
1209 if (*lppszComponent == NULL)
1211 *lppszComponent = (LPWSTR)lpszStart;
1212 *dwComponentLen = len;
1214 else
1216 DWORD ncpylen = min((*dwComponentLen)-1, len);
1217 memcpy(*lppszComponent, lpszStart, ncpylen*sizeof(WCHAR));
1218 (*lppszComponent)[ncpylen] = '\0';
1219 *dwComponentLen = ncpylen;
1223 return TRUE;
1226 /***********************************************************************
1227 * InternetCrackUrlW (WININET.@)
1229 * Break up URL into its components
1231 * RETURNS
1232 * TRUE on success
1233 * FALSE on failure
1235 BOOL WINAPI InternetCrackUrlW(LPCWSTR lpszUrl_orig, DWORD dwUrlLength_orig, DWORD dwFlags,
1236 LPURL_COMPONENTSW lpUC)
1239 * RFC 1808
1240 * <protocol>:[//<net_loc>][/path][;<params>][?<query>][#<fragment>]
1243 LPCWSTR lpszParam = NULL;
1244 BOOL bIsAbsolute = FALSE;
1245 LPCWSTR lpszap, lpszUrl = lpszUrl_orig;
1246 LPCWSTR lpszcp = NULL;
1247 LPWSTR lpszUrl_decode = NULL;
1248 DWORD dwUrlLength = dwUrlLength_orig;
1249 const WCHAR lpszSeparators[3]={';','?',0};
1250 const WCHAR lpszSlash[2]={'/',0};
1252 TRACE("(%s %u %x %p)\n", debugstr_w(lpszUrl), dwUrlLength, dwFlags, lpUC);
1254 if (!lpszUrl_orig || !*lpszUrl_orig || !lpUC)
1256 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1257 return FALSE;
1259 if (!dwUrlLength) dwUrlLength = strlenW(lpszUrl);
1261 if (dwFlags & ICU_DECODE)
1263 lpszUrl_decode=HeapAlloc( GetProcessHeap(), 0, dwUrlLength * sizeof (WCHAR) );
1264 if( InternetCanonicalizeUrlW(lpszUrl_orig, lpszUrl_decode, &dwUrlLength, dwFlags))
1266 lpszUrl = lpszUrl_decode;
1269 lpszap = lpszUrl;
1271 /* Determine if the URI is absolute. */
1272 while (*lpszap != '\0')
1274 if (isalnumW(*lpszap))
1276 lpszap++;
1277 continue;
1279 if ((*lpszap == ':') && (lpszap - lpszUrl >= 2))
1281 bIsAbsolute = TRUE;
1282 lpszcp = lpszap;
1284 else
1286 lpszcp = lpszUrl; /* Relative url */
1289 break;
1292 lpUC->nScheme = INTERNET_SCHEME_UNKNOWN;
1293 lpUC->nPort = INTERNET_INVALID_PORT_NUMBER;
1295 /* Parse <params> */
1296 lpszParam = strpbrkW(lpszap, lpszSeparators);
1297 SetUrlComponentValueW(&lpUC->lpszExtraInfo, &lpUC->dwExtraInfoLength,
1298 lpszParam, lpszParam ? dwUrlLength-(lpszParam-lpszUrl) : 0);
1300 if (bIsAbsolute) /* Parse <protocol>:[//<net_loc>] */
1302 LPCWSTR lpszNetLoc;
1304 /* Get scheme first. */
1305 lpUC->nScheme = GetInternetSchemeW(lpszUrl, lpszcp - lpszUrl);
1306 SetUrlComponentValueW(&lpUC->lpszScheme, &lpUC->dwSchemeLength,
1307 lpszUrl, lpszcp - lpszUrl);
1309 /* Eat ':' in protocol. */
1310 lpszcp++;
1312 /* double slash indicates the net_loc portion is present */
1313 if ((lpszcp[0] == '/') && (lpszcp[1] == '/'))
1315 lpszcp += 2;
1317 lpszNetLoc = strpbrkW(lpszcp, lpszSlash);
1318 if (lpszParam)
1320 if (lpszNetLoc)
1321 lpszNetLoc = min(lpszNetLoc, lpszParam);
1322 else
1323 lpszNetLoc = lpszParam;
1325 else if (!lpszNetLoc)
1326 lpszNetLoc = lpszcp + dwUrlLength-(lpszcp-lpszUrl);
1328 /* Parse net-loc */
1329 if (lpszNetLoc)
1331 LPCWSTR lpszHost;
1332 LPCWSTR lpszPort;
1334 /* [<user>[<:password>]@]<host>[:<port>] */
1335 /* First find the user and password if they exist */
1337 lpszHost = strchrW(lpszcp, '@');
1338 if (lpszHost == NULL || lpszHost > lpszNetLoc)
1340 /* username and password not specified. */
1341 SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength, NULL, 0);
1342 SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength, NULL, 0);
1344 else /* Parse out username and password */
1346 LPCWSTR lpszUser = lpszcp;
1347 LPCWSTR lpszPasswd = lpszHost;
1349 while (lpszcp < lpszHost)
1351 if (*lpszcp == ':')
1352 lpszPasswd = lpszcp;
1354 lpszcp++;
1357 SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength,
1358 lpszUser, lpszPasswd - lpszUser);
1360 if (lpszPasswd != lpszHost)
1361 lpszPasswd++;
1362 SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength,
1363 lpszPasswd == lpszHost ? NULL : lpszPasswd,
1364 lpszHost - lpszPasswd);
1366 lpszcp++; /* Advance to beginning of host */
1369 /* Parse <host><:port> */
1371 lpszHost = lpszcp;
1372 lpszPort = lpszNetLoc;
1374 /* special case for res:// URLs: there is no port here, so the host is the
1375 entire string up to the first '/' */
1376 if(lpUC->nScheme==INTERNET_SCHEME_RES)
1378 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength,
1379 lpszHost, lpszPort - lpszHost);
1380 lpszcp=lpszNetLoc;
1382 else
1384 while (lpszcp < lpszNetLoc)
1386 if (*lpszcp == ':')
1387 lpszPort = lpszcp;
1389 lpszcp++;
1392 /* If the scheme is "file" and the host is just one letter, it's not a host */
1393 if(lpUC->nScheme==INTERNET_SCHEME_FILE && (lpszPort-lpszHost)==1)
1395 lpszcp=lpszHost;
1396 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength,
1397 NULL, 0);
1399 else
1401 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength,
1402 lpszHost, lpszPort - lpszHost);
1403 if (lpszPort != lpszNetLoc)
1404 lpUC->nPort = atoiW(++lpszPort);
1405 else switch (lpUC->nScheme)
1407 case INTERNET_SCHEME_HTTP:
1408 lpUC->nPort = INTERNET_DEFAULT_HTTP_PORT;
1409 break;
1410 case INTERNET_SCHEME_HTTPS:
1411 lpUC->nPort = INTERNET_DEFAULT_HTTPS_PORT;
1412 break;
1413 case INTERNET_SCHEME_FTP:
1414 lpUC->nPort = INTERNET_DEFAULT_FTP_PORT;
1415 break;
1416 case INTERNET_SCHEME_GOPHER:
1417 lpUC->nPort = INTERNET_DEFAULT_GOPHER_PORT;
1418 break;
1419 default:
1420 break;
1426 else
1428 SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength, NULL, 0);
1429 SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength, NULL, 0);
1430 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength, NULL, 0);
1433 else
1435 SetUrlComponentValueW(&lpUC->lpszScheme, &lpUC->dwSchemeLength, NULL, 0);
1436 SetUrlComponentValueW(&lpUC->lpszUserName, &lpUC->dwUserNameLength, NULL, 0);
1437 SetUrlComponentValueW(&lpUC->lpszPassword, &lpUC->dwPasswordLength, NULL, 0);
1438 SetUrlComponentValueW(&lpUC->lpszHostName, &lpUC->dwHostNameLength, NULL, 0);
1441 /* Here lpszcp points to:
1443 * <protocol>:[//<net_loc>][/path][;<params>][?<query>][#<fragment>]
1444 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1446 if (lpszcp != 0 && *lpszcp != '\0' && (!lpszParam || lpszcp < lpszParam))
1448 INT len;
1450 /* Only truncate the parameter list if it's already been saved
1451 * in lpUC->lpszExtraInfo.
1453 if (lpszParam && lpUC->dwExtraInfoLength && lpUC->lpszExtraInfo)
1454 len = lpszParam - lpszcp;
1455 else
1457 /* Leave the parameter list in lpszUrlPath. Strip off any trailing
1458 * newlines if necessary.
1460 LPWSTR lpsznewline = strchrW(lpszcp, '\n');
1461 if (lpsznewline != NULL)
1462 len = lpsznewline - lpszcp;
1463 else
1464 len = dwUrlLength-(lpszcp-lpszUrl);
1466 SetUrlComponentValueW(&lpUC->lpszUrlPath, &lpUC->dwUrlPathLength,
1467 lpszcp, len);
1469 else
1471 lpUC->dwUrlPathLength = 0;
1474 TRACE("%s: scheme(%s) host(%s) path(%s) extra(%s)\n", debugstr_wn(lpszUrl,dwUrlLength),
1475 debugstr_wn(lpUC->lpszScheme,lpUC->dwSchemeLength),
1476 debugstr_wn(lpUC->lpszHostName,lpUC->dwHostNameLength),
1477 debugstr_wn(lpUC->lpszUrlPath,lpUC->dwUrlPathLength),
1478 debugstr_wn(lpUC->lpszExtraInfo,lpUC->dwExtraInfoLength));
1480 HeapFree(GetProcessHeap(), 0, lpszUrl_decode );
1481 return TRUE;
1484 /***********************************************************************
1485 * InternetAttemptConnect (WININET.@)
1487 * Attempt to make a connection to the internet
1489 * RETURNS
1490 * ERROR_SUCCESS on success
1491 * Error value on failure
1494 DWORD WINAPI InternetAttemptConnect(DWORD dwReserved)
1496 FIXME("Stub\n");
1497 return ERROR_SUCCESS;
1501 /***********************************************************************
1502 * InternetCanonicalizeUrlA (WININET.@)
1504 * Escape unsafe characters and spaces
1506 * RETURNS
1507 * TRUE on success
1508 * FALSE on failure
1511 BOOL WINAPI InternetCanonicalizeUrlA(LPCSTR lpszUrl, LPSTR lpszBuffer,
1512 LPDWORD lpdwBufferLength, DWORD dwFlags)
1514 HRESULT hr;
1515 DWORD dwURLFlags = URL_WININET_COMPATIBILITY | URL_ESCAPE_UNSAFE;
1517 TRACE("(%s, %p, %p, 0x%08x) bufferlength: %d\n", debugstr_a(lpszUrl), lpszBuffer,
1518 lpdwBufferLength, lpdwBufferLength ? *lpdwBufferLength : -1, dwFlags);
1520 if(dwFlags & ICU_DECODE)
1522 dwURLFlags |= URL_UNESCAPE;
1523 dwFlags &= ~ICU_DECODE;
1526 if(dwFlags & ICU_ESCAPE)
1528 dwURLFlags |= URL_UNESCAPE;
1529 dwFlags &= ~ICU_ESCAPE;
1532 if(dwFlags & ICU_BROWSER_MODE)
1534 dwURLFlags |= URL_BROWSER_MODE;
1535 dwFlags &= ~ICU_BROWSER_MODE;
1538 if(dwFlags & ICU_NO_ENCODE)
1540 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
1541 dwURLFlags ^= URL_ESCAPE_UNSAFE;
1542 dwFlags &= ~ICU_NO_ENCODE;
1545 if (dwFlags) FIXME("Unhandled flags 0x%08x\n", dwFlags);
1547 hr = UrlCanonicalizeA(lpszUrl, lpszBuffer, lpdwBufferLength, dwURLFlags);
1548 if (hr == E_POINTER) SetLastError(ERROR_INSUFFICIENT_BUFFER);
1549 if (hr == E_INVALIDARG) SetLastError(ERROR_INVALID_PARAMETER);
1551 return (hr == S_OK) ? TRUE : FALSE;
1554 /***********************************************************************
1555 * InternetCanonicalizeUrlW (WININET.@)
1557 * Escape unsafe characters and spaces
1559 * RETURNS
1560 * TRUE on success
1561 * FALSE on failure
1564 BOOL WINAPI InternetCanonicalizeUrlW(LPCWSTR lpszUrl, LPWSTR lpszBuffer,
1565 LPDWORD lpdwBufferLength, DWORD dwFlags)
1567 HRESULT hr;
1568 DWORD dwURLFlags = URL_WININET_COMPATIBILITY | URL_ESCAPE_UNSAFE;
1570 TRACE("(%s, %p, %p, 0x%08x) bufferlength: %d\n", debugstr_w(lpszUrl), lpszBuffer,
1571 lpdwBufferLength, lpdwBufferLength ? *lpdwBufferLength : -1, dwFlags);
1573 if(dwFlags & ICU_DECODE)
1575 dwURLFlags |= URL_UNESCAPE;
1576 dwFlags &= ~ICU_DECODE;
1579 if(dwFlags & ICU_ESCAPE)
1581 dwURLFlags |= URL_UNESCAPE;
1582 dwFlags &= ~ICU_ESCAPE;
1585 if(dwFlags & ICU_BROWSER_MODE)
1587 dwURLFlags |= URL_BROWSER_MODE;
1588 dwFlags &= ~ICU_BROWSER_MODE;
1591 if(dwFlags & ICU_NO_ENCODE)
1593 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
1594 dwURLFlags ^= URL_ESCAPE_UNSAFE;
1595 dwFlags &= ~ICU_NO_ENCODE;
1598 if (dwFlags) FIXME("Unhandled flags 0x%08x\n", dwFlags);
1600 hr = UrlCanonicalizeW(lpszUrl, lpszBuffer, lpdwBufferLength, dwURLFlags);
1601 if (hr == E_POINTER) SetLastError(ERROR_INSUFFICIENT_BUFFER);
1602 if (hr == E_INVALIDARG) SetLastError(ERROR_INVALID_PARAMETER);
1604 return (hr == S_OK) ? TRUE : FALSE;
1607 /* #################################################### */
1609 static INTERNET_STATUS_CALLBACK set_status_callback(
1610 LPWININETHANDLEHEADER lpwh, INTERNET_STATUS_CALLBACK callback, BOOL unicode)
1612 INTERNET_STATUS_CALLBACK ret;
1614 if (unicode) lpwh->dwInternalFlags |= INET_CALLBACKW;
1615 else lpwh->dwInternalFlags &= ~INET_CALLBACKW;
1617 ret = lpwh->lpfnStatusCB;
1618 lpwh->lpfnStatusCB = callback;
1620 return ret;
1623 /***********************************************************************
1624 * InternetSetStatusCallbackA (WININET.@)
1626 * Sets up a callback function which is called as progress is made
1627 * during an operation.
1629 * RETURNS
1630 * Previous callback or NULL on success
1631 * INTERNET_INVALID_STATUS_CALLBACK on failure
1634 INTERNET_STATUS_CALLBACK WINAPI InternetSetStatusCallbackA(
1635 HINTERNET hInternet ,INTERNET_STATUS_CALLBACK lpfnIntCB)
1637 INTERNET_STATUS_CALLBACK retVal;
1638 LPWININETHANDLEHEADER lpwh;
1640 TRACE("0x%08x\n", (ULONG)hInternet);
1642 if (!(lpwh = WININET_GetObject(hInternet)))
1643 return INTERNET_INVALID_STATUS_CALLBACK;
1645 retVal = set_status_callback(lpwh, lpfnIntCB, FALSE);
1647 WININET_Release( lpwh );
1648 return retVal;
1651 /***********************************************************************
1652 * InternetSetStatusCallbackW (WININET.@)
1654 * Sets up a callback function which is called as progress is made
1655 * during an operation.
1657 * RETURNS
1658 * Previous callback or NULL on success
1659 * INTERNET_INVALID_STATUS_CALLBACK on failure
1662 INTERNET_STATUS_CALLBACK WINAPI InternetSetStatusCallbackW(
1663 HINTERNET hInternet ,INTERNET_STATUS_CALLBACK lpfnIntCB)
1665 INTERNET_STATUS_CALLBACK retVal;
1666 LPWININETHANDLEHEADER lpwh;
1668 TRACE("0x%08x\n", (ULONG)hInternet);
1670 if (!(lpwh = WININET_GetObject(hInternet)))
1671 return INTERNET_INVALID_STATUS_CALLBACK;
1673 retVal = set_status_callback(lpwh, lpfnIntCB, TRUE);
1675 WININET_Release( lpwh );
1676 return retVal;
1679 /***********************************************************************
1680 * InternetSetFilePointer (WININET.@)
1682 DWORD WINAPI InternetSetFilePointer(HINTERNET hFile, LONG lDistanceToMove,
1683 PVOID pReserved, DWORD dwMoveContext, DWORD_PTR dwContext)
1685 FIXME("stub\n");
1686 return FALSE;
1689 /***********************************************************************
1690 * InternetWriteFile (WININET.@)
1692 * Write data to an open internet file
1694 * RETURNS
1695 * TRUE on success
1696 * FALSE on failure
1699 BOOL WINAPI InternetWriteFile(HINTERNET hFile, LPCVOID lpBuffer,
1700 DWORD dwNumOfBytesToWrite, LPDWORD lpdwNumOfBytesWritten)
1702 LPWININETHANDLEHEADER lpwh;
1703 BOOL retval = FALSE;
1705 TRACE("(%p %p %d %p)\n", hFile, lpBuffer, dwNumOfBytesToWrite, lpdwNumOfBytesWritten);
1707 lpwh = WININET_GetObject( hFile );
1709 if(lpwh && lpwh->vtbl->WriteFile) {
1710 retval = lpwh->vtbl->WriteFile(lpwh, lpBuffer, dwNumOfBytesToWrite, lpdwNumOfBytesWritten);
1711 }else {
1712 WARN("Invalid handle\n");
1713 SetLastError(ERROR_INVALID_HANDLE);
1714 retval = FALSE;
1717 WININET_Release( lpwh );
1719 return retval;
1723 /***********************************************************************
1724 * InternetReadFile (WININET.@)
1726 * Read data from an open internet file
1728 * RETURNS
1729 * TRUE on success
1730 * FALSE on failure
1733 BOOL WINAPI InternetReadFile(HINTERNET hFile, LPVOID lpBuffer,
1734 DWORD dwNumOfBytesToRead, LPDWORD pdwNumOfBytesRead)
1736 LPWININETHANDLEHEADER hdr;
1737 DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
1739 TRACE("%p %p %d %p\n", hFile, lpBuffer, dwNumOfBytesToRead, pdwNumOfBytesRead);
1741 hdr = WININET_GetObject(hFile);
1742 if (!hdr) {
1743 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
1744 return FALSE;
1747 if(hdr->vtbl->ReadFile)
1748 res = hdr->vtbl->ReadFile(hdr, lpBuffer, dwNumOfBytesToRead, pdwNumOfBytesRead);
1750 WININET_Release(hdr);
1752 TRACE("-- %s (%u) (bytes read: %d)\n", res == ERROR_SUCCESS ? "TRUE": "FALSE", res,
1753 pdwNumOfBytesRead ? *pdwNumOfBytesRead : -1);
1755 if(res != ERROR_SUCCESS)
1756 SetLastError(res);
1757 return res == ERROR_SUCCESS;
1760 /***********************************************************************
1761 * InternetReadFileExA (WININET.@)
1763 * Read data from an open internet file
1765 * PARAMS
1766 * hFile [I] Handle returned by InternetOpenUrl or HttpOpenRequest.
1767 * lpBuffersOut [I/O] Buffer.
1768 * dwFlags [I] Flags. See notes.
1769 * dwContext [I] Context for callbacks.
1771 * RETURNS
1772 * TRUE on success
1773 * FALSE on failure
1775 * NOTES
1776 * The parameter dwFlags include zero or more of the following flags:
1777 *|IRF_ASYNC - Makes the call asynchronous.
1778 *|IRF_SYNC - Makes the call synchronous.
1779 *|IRF_USE_CONTEXT - Forces dwContext to be used.
1780 *|IRF_NO_WAIT - Don't block if the data is not available, just return what is available.
1782 * However, in testing IRF_USE_CONTEXT seems to have no effect - dwContext isn't used.
1784 * SEE
1785 * InternetOpenUrlA(), HttpOpenRequestA()
1787 BOOL WINAPI InternetReadFileExA(HINTERNET hFile, LPINTERNET_BUFFERSA lpBuffersOut,
1788 DWORD dwFlags, DWORD_PTR dwContext)
1790 LPWININETHANDLEHEADER hdr;
1791 DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
1793 TRACE("(%p %p 0x%x 0x%lx)\n", hFile, lpBuffersOut, dwFlags, dwContext);
1795 hdr = WININET_GetObject(hFile);
1796 if (!hdr) {
1797 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
1798 return FALSE;
1801 if(hdr->vtbl->ReadFileExA)
1802 res = hdr->vtbl->ReadFileExA(hdr, lpBuffersOut, dwFlags, dwContext);
1804 WININET_Release(hdr);
1806 TRACE("-- %s (%u, bytes read: %d)\n", res == ERROR_SUCCESS ? "TRUE": "FALSE",
1807 res, lpBuffersOut->dwBufferLength);
1809 if(res != ERROR_SUCCESS)
1810 SetLastError(res);
1811 return res == ERROR_SUCCESS;
1814 /***********************************************************************
1815 * InternetReadFileExW (WININET.@)
1817 * Read data from an open internet file.
1819 * PARAMS
1820 * hFile [I] Handle returned by InternetOpenUrl() or HttpOpenRequest().
1821 * lpBuffersOut [I/O] Buffer.
1822 * dwFlags [I] Flags.
1823 * dwContext [I] Context for callbacks.
1825 * RETURNS
1826 * FALSE, last error is set to ERROR_CALL_NOT_IMPLEMENTED
1828 * NOTES
1829 * Not implemented in Wine or native either (as of IE6 SP2).
1832 BOOL WINAPI InternetReadFileExW(HINTERNET hFile, LPINTERNET_BUFFERSW lpBuffer,
1833 DWORD dwFlags, DWORD_PTR dwContext)
1835 ERR("(%p, %p, 0x%x, 0x%lx): not implemented in native\n", hFile, lpBuffer, dwFlags, dwContext);
1837 INTERNET_SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1838 return FALSE;
1841 /***********************************************************************
1842 * INET_QueryOptionHelper (internal)
1844 static BOOL INET_QueryOptionHelper(BOOL bIsUnicode, HINTERNET hInternet, DWORD dwOption,
1845 LPVOID lpBuffer, LPDWORD lpdwBufferLength)
1847 LPWININETHANDLEHEADER lpwhh;
1848 BOOL bSuccess = FALSE;
1850 TRACE("(%p, 0x%08x, %p, %p)\n", hInternet, dwOption, lpBuffer, lpdwBufferLength);
1852 lpwhh = (LPWININETHANDLEHEADER) WININET_GetObject( hInternet );
1854 switch (dwOption)
1856 case INTERNET_OPTION_REQUEST_FLAGS:
1858 ULONG flags = 4;
1859 TRACE("INTERNET_OPTION_REQUEST_FLAGS: %d\n", flags);
1860 if (*lpdwBufferLength < sizeof(ULONG))
1861 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1862 else
1864 memcpy(lpBuffer, &flags, sizeof(ULONG));
1865 bSuccess = TRUE;
1867 *lpdwBufferLength = sizeof(ULONG);
1868 break;
1871 case INTERNET_OPTION_USER_AGENT:
1872 FIXME("INTERNET_OPTION_USER_AGENT\n");
1873 break;
1875 case INTERNET_OPTION_HTTP_VERSION:
1877 if (*lpdwBufferLength < sizeof(HTTP_VERSION_INFO))
1878 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1879 else
1882 * Presently hardcoded to 1.1
1884 ((HTTP_VERSION_INFO*)lpBuffer)->dwMajorVersion = 1;
1885 ((HTTP_VERSION_INFO*)lpBuffer)->dwMinorVersion = 1;
1886 bSuccess = TRUE;
1888 *lpdwBufferLength = sizeof(HTTP_VERSION_INFO);
1889 break;
1891 case INTERNET_OPTION_CONNECTED_STATE:
1893 DWORD *pdwConnectedState = (DWORD *)lpBuffer;
1894 FIXME("INTERNET_OPTION_CONNECTED_STATE: semi-stub\n");
1896 if (*lpdwBufferLength < sizeof(*pdwConnectedState))
1897 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1898 else
1900 *pdwConnectedState = INTERNET_STATE_CONNECTED;
1901 bSuccess = TRUE;
1903 *lpdwBufferLength = sizeof(*pdwConnectedState);
1904 break;
1906 case INTERNET_OPTION_PROXY:
1908 LPWININETAPPINFOW lpwai = (LPWININETAPPINFOW)lpwhh;
1909 WININETAPPINFOW wai;
1911 if (lpwai == NULL)
1913 TRACE("Getting global proxy info\n");
1914 memset(&wai, 0, sizeof(WININETAPPINFOW));
1915 INTERNET_ConfigureProxyFromReg( &wai );
1916 lpwai = &wai;
1919 if (bIsUnicode)
1921 INTERNET_PROXY_INFOW *pPI = (INTERNET_PROXY_INFOW *)lpBuffer;
1922 DWORD proxyBytesRequired = 0, proxyBypassBytesRequired = 0;
1924 if (lpwai->lpszProxy)
1925 proxyBytesRequired = (lstrlenW(lpwai->lpszProxy) + 1) *
1926 sizeof(WCHAR);
1927 if (lpwai->lpszProxyBypass)
1928 proxyBypassBytesRequired =
1929 (lstrlenW(lpwai->lpszProxyBypass) + 1) * sizeof(WCHAR);
1930 if (*lpdwBufferLength < sizeof(INTERNET_PROXY_INFOW) +
1931 proxyBytesRequired + proxyBypassBytesRequired)
1932 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1933 else
1935 LPWSTR proxy = (LPWSTR)((LPBYTE)lpBuffer +
1936 sizeof(INTERNET_PROXY_INFOW));
1937 LPWSTR proxy_bypass = (LPWSTR)((LPBYTE)lpBuffer +
1938 sizeof(INTERNET_PROXY_INFOW) +
1939 proxyBytesRequired);
1941 pPI->dwAccessType = lpwai->dwAccessType;
1942 pPI->lpszProxy = NULL;
1943 pPI->lpszProxyBypass = NULL;
1944 if (lpwai->lpszProxy)
1946 lstrcpyW(proxy, lpwai->lpszProxy);
1947 pPI->lpszProxy = proxy;
1950 if (lpwai->lpszProxyBypass)
1952 lstrcpyW(proxy_bypass, lpwai->lpszProxyBypass);
1953 pPI->lpszProxyBypass = proxy_bypass;
1955 bSuccess = TRUE;
1957 *lpdwBufferLength = sizeof(INTERNET_PROXY_INFOW) +
1958 proxyBytesRequired + proxyBypassBytesRequired;
1960 else
1962 INTERNET_PROXY_INFOA *pPI = (INTERNET_PROXY_INFOA *)lpBuffer;
1963 DWORD proxyBytesRequired = 0, proxyBypassBytesRequired = 0;
1965 if (lpwai->lpszProxy)
1966 proxyBytesRequired = WideCharToMultiByte(CP_ACP, 0,
1967 lpwai->lpszProxy, -1, NULL, 0, NULL, NULL);
1968 if (lpwai->lpszProxyBypass)
1969 proxyBypassBytesRequired = WideCharToMultiByte(CP_ACP, 0,
1970 lpwai->lpszProxyBypass, -1, NULL, 0, NULL, NULL);
1971 if (*lpdwBufferLength < sizeof(INTERNET_PROXY_INFOA) +
1972 proxyBytesRequired + proxyBypassBytesRequired)
1973 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1974 else
1976 LPSTR proxy = (LPSTR)((LPBYTE)lpBuffer +
1977 sizeof(INTERNET_PROXY_INFOA));
1978 LPSTR proxy_bypass = (LPSTR)((LPBYTE)lpBuffer +
1979 sizeof(INTERNET_PROXY_INFOA) +
1980 proxyBytesRequired);
1982 pPI->dwAccessType = lpwai->dwAccessType;
1983 pPI->lpszProxy = NULL;
1984 pPI->lpszProxyBypass = NULL;
1985 if (lpwai->lpszProxy)
1987 WideCharToMultiByte(CP_ACP, 0, lpwai->lpszProxy, -1,
1988 proxy, proxyBytesRequired, NULL, NULL);
1989 pPI->lpszProxy = proxy;
1992 if (lpwai->lpszProxyBypass)
1994 WideCharToMultiByte(CP_ACP, 0, lpwai->lpszProxyBypass,
1995 -1, proxy_bypass, proxyBypassBytesRequired,
1996 NULL, NULL);
1997 pPI->lpszProxyBypass = proxy_bypass;
1999 bSuccess = TRUE;
2001 *lpdwBufferLength = sizeof(INTERNET_PROXY_INFOA) +
2002 proxyBytesRequired + proxyBypassBytesRequired;
2004 break;
2006 case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2008 ULONG conn = 2;
2009 TRACE("INTERNET_OPTION_MAX_CONNS_PER_SERVER: %d\n", conn);
2010 if (*lpdwBufferLength < sizeof(ULONG))
2011 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2012 else
2014 memcpy(lpBuffer, &conn, sizeof(ULONG));
2015 bSuccess = TRUE;
2017 *lpdwBufferLength = sizeof(ULONG);
2018 break;
2020 case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2022 ULONG conn = 4;
2023 TRACE("INTERNET_OPTION_MAX_CONNS_1_0_SERVER: %d\n", conn);
2024 if (*lpdwBufferLength < sizeof(ULONG))
2025 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2026 else
2028 memcpy(lpBuffer, &conn, sizeof(ULONG));
2029 bSuccess = TRUE;
2031 *lpdwBufferLength = sizeof(ULONG);
2032 break;
2034 case INTERNET_OPTION_SECURITY_FLAGS:
2035 FIXME("INTERNET_OPTION_SECURITY_FLAGS: Stub\n");
2036 bSuccess = TRUE;
2037 break;
2039 case INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT:
2040 if (!lpwhh)
2042 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
2043 return FALSE;
2045 if (*lpdwBufferLength < sizeof(INTERNET_CERTIFICATE_INFOW))
2047 *lpdwBufferLength = sizeof(INTERNET_CERTIFICATE_INFOW);
2048 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2050 else if (lpwhh->htype == WH_HHTTPREQ)
2052 LPWININETHTTPREQW lpwhr;
2053 PCCERT_CONTEXT context;
2055 lpwhr = (LPWININETHTTPREQW)lpwhh;
2056 context = (PCCERT_CONTEXT)NETCON_GetCert(&(lpwhr->netConnection));
2057 if (context)
2059 LPINTERNET_CERTIFICATE_INFOW info = (LPINTERNET_CERTIFICATE_INFOW)lpBuffer;
2060 DWORD strLen;
2062 memset(info,0,sizeof(INTERNET_CERTIFICATE_INFOW));
2063 info->ftExpiry = context->pCertInfo->NotAfter;
2064 info->ftStart = context->pCertInfo->NotBefore;
2065 if (bIsUnicode)
2067 strLen = CertNameToStrW(context->dwCertEncodingType,
2068 &context->pCertInfo->Subject, CERT_SIMPLE_NAME_STR,
2069 NULL, 0);
2070 info->lpszSubjectInfo = LocalAlloc(0,
2071 strLen * sizeof(WCHAR));
2072 if (info->lpszSubjectInfo)
2073 CertNameToStrW(context->dwCertEncodingType,
2074 &context->pCertInfo->Subject, CERT_SIMPLE_NAME_STR,
2075 info->lpszSubjectInfo, strLen);
2076 strLen = CertNameToStrW(context->dwCertEncodingType,
2077 &context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR,
2078 NULL, 0);
2079 info->lpszIssuerInfo = LocalAlloc(0,
2080 strLen * sizeof(WCHAR));
2081 if (info->lpszIssuerInfo)
2082 CertNameToStrW(context->dwCertEncodingType,
2083 &context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR,
2084 info->lpszIssuerInfo, strLen);
2086 else
2088 LPINTERNET_CERTIFICATE_INFOA infoA =
2089 (LPINTERNET_CERTIFICATE_INFOA)info;
2091 strLen = CertNameToStrA(context->dwCertEncodingType,
2092 &context->pCertInfo->Subject, CERT_SIMPLE_NAME_STR,
2093 NULL, 0);
2094 infoA->lpszSubjectInfo = LocalAlloc(0, strLen);
2095 if (infoA->lpszSubjectInfo)
2096 CertNameToStrA(context->dwCertEncodingType,
2097 &context->pCertInfo->Subject, CERT_SIMPLE_NAME_STR,
2098 infoA->lpszSubjectInfo, strLen);
2099 strLen = CertNameToStrA(context->dwCertEncodingType,
2100 &context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR,
2101 NULL, 0);
2102 infoA->lpszIssuerInfo = LocalAlloc(0, strLen);
2103 if (infoA->lpszIssuerInfo)
2104 CertNameToStrA(context->dwCertEncodingType,
2105 &context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR,
2106 infoA->lpszIssuerInfo, strLen);
2109 * Contrary to MSDN, these do not appear to be set.
2110 * lpszProtocolName
2111 * lpszSignatureAlgName
2112 * lpszEncryptionAlgName
2113 * dwKeySize
2115 CertFreeCertificateContext(context);
2116 bSuccess = TRUE;
2119 break;
2120 case INTERNET_OPTION_VERSION:
2122 TRACE("INTERNET_OPTION_VERSION\n");
2123 if (*lpdwBufferLength < sizeof(INTERNET_VERSION_INFO))
2124 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2125 else
2127 static const INTERNET_VERSION_INFO info = { 1, 2 };
2128 memcpy(lpBuffer, &info, sizeof(info));
2129 *lpdwBufferLength = sizeof(info);
2130 bSuccess = TRUE;
2132 break;
2134 case INTERNET_OPTION_PER_CONNECTION_OPTION:
2135 FIXME("INTERNET_OPTION_PER_CONNECTION_OPTION stub\n");
2136 if (*lpdwBufferLength < sizeof(INTERNET_PER_CONN_OPTION_LISTW))
2137 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2138 else
2140 INTERNET_PER_CONN_OPTION_LISTW *con = lpBuffer;
2141 int x;
2142 bSuccess = TRUE;
2143 for (x = 0; x < con->dwOptionCount; ++x)
2145 INTERNET_PER_CONN_OPTIONW *option = con->pOptions + x;
2146 switch (option->dwOption)
2148 case INTERNET_PER_CONN_FLAGS:
2149 option->Value.dwValue = PROXY_TYPE_DIRECT;
2150 break;
2152 case INTERNET_PER_CONN_PROXY_SERVER:
2153 case INTERNET_PER_CONN_PROXY_BYPASS:
2154 case INTERNET_PER_CONN_AUTOCONFIG_URL:
2155 case INTERNET_PER_CONN_AUTODISCOVERY_FLAGS:
2156 case INTERNET_PER_CONN_AUTOCONFIG_SECONDARY_URL:
2157 case INTERNET_PER_CONN_AUTOCONFIG_RELOAD_DELAY_MINS:
2158 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_TIME:
2159 case INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_URL:
2160 FIXME("Unhandled dwOption %d\n", option->dwOption);
2161 option->Value.dwValue = 0;
2162 bSuccess = FALSE;
2163 break;
2165 default:
2166 FIXME("Unknown dwOption %d\n", option->dwOption);
2167 bSuccess = FALSE;
2168 break;
2171 if (!bSuccess)
2172 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
2174 break;
2175 case 66:
2176 FIXME("66\n");
2177 bSuccess = TRUE;
2178 break;
2179 default: {
2180 if(lpwhh) {
2181 DWORD res;
2183 res = lpwhh->vtbl->QueryOption(lpwhh, dwOption, lpBuffer, lpdwBufferLength, bIsUnicode);
2184 if(res == ERROR_SUCCESS)
2185 bSuccess = TRUE;
2186 else
2187 SetLastError(res);
2188 }else {
2189 FIXME("Stub! %d\n", dwOption);
2190 break;
2194 if (lpwhh)
2195 WININET_Release( lpwhh );
2197 return bSuccess;
2200 /***********************************************************************
2201 * InternetQueryOptionW (WININET.@)
2203 * Queries an options on the specified handle
2205 * RETURNS
2206 * TRUE on success
2207 * FALSE on failure
2210 BOOL WINAPI InternetQueryOptionW(HINTERNET hInternet, DWORD dwOption,
2211 LPVOID lpBuffer, LPDWORD lpdwBufferLength)
2213 return INET_QueryOptionHelper(TRUE, hInternet, dwOption, lpBuffer, lpdwBufferLength);
2216 /***********************************************************************
2217 * InternetQueryOptionA (WININET.@)
2219 * Queries an options on the specified handle
2221 * RETURNS
2222 * TRUE on success
2223 * FALSE on failure
2226 BOOL WINAPI InternetQueryOptionA(HINTERNET hInternet, DWORD dwOption,
2227 LPVOID lpBuffer, LPDWORD lpdwBufferLength)
2229 return INET_QueryOptionHelper(FALSE, hInternet, dwOption, lpBuffer, lpdwBufferLength);
2233 /***********************************************************************
2234 * InternetSetOptionW (WININET.@)
2236 * Sets an options on the specified handle
2238 * RETURNS
2239 * TRUE on success
2240 * FALSE on failure
2243 BOOL WINAPI InternetSetOptionW(HINTERNET hInternet, DWORD dwOption,
2244 LPVOID lpBuffer, DWORD dwBufferLength)
2246 LPWININETHANDLEHEADER lpwhh;
2247 BOOL ret = TRUE;
2249 TRACE("(%p %d %p %d)\n", hInternet, dwOption, lpBuffer, dwBufferLength);
2251 lpwhh = (LPWININETHANDLEHEADER) WININET_GetObject( hInternet );
2252 if(lpwhh && lpwhh->vtbl->SetOption) {
2253 DWORD res;
2255 res = lpwhh->vtbl->SetOption(lpwhh, dwOption, lpBuffer, dwBufferLength);
2256 if(res != ERROR_INTERNET_INVALID_OPTION) {
2257 WININET_Release( lpwhh );
2259 if(res != ERROR_SUCCESS)
2260 SetLastError(res);
2262 return res == ERROR_SUCCESS;
2266 switch (dwOption)
2268 case INTERNET_OPTION_CALLBACK:
2270 INTERNET_STATUS_CALLBACK callback = *(INTERNET_STATUS_CALLBACK *)lpBuffer;
2271 ret = (set_status_callback(lpwhh, callback, TRUE) != INTERNET_INVALID_STATUS_CALLBACK);
2272 break;
2274 case INTERNET_OPTION_HTTP_VERSION:
2276 HTTP_VERSION_INFO* pVersion=(HTTP_VERSION_INFO*)lpBuffer;
2277 FIXME("Option INTERNET_OPTION_HTTP_VERSION(%d,%d): STUB\n",pVersion->dwMajorVersion,pVersion->dwMinorVersion);
2279 break;
2280 case INTERNET_OPTION_ERROR_MASK:
2282 unsigned long flags=*(unsigned long*)lpBuffer;
2283 FIXME("Option INTERNET_OPTION_ERROR_MASK(%ld): STUB\n",flags);
2285 break;
2286 case INTERNET_OPTION_CODEPAGE:
2288 unsigned long codepage=*(unsigned long*)lpBuffer;
2289 FIXME("Option INTERNET_OPTION_CODEPAGE (%ld): STUB\n",codepage);
2291 break;
2292 case INTERNET_OPTION_REQUEST_PRIORITY:
2294 unsigned long priority=*(unsigned long*)lpBuffer;
2295 FIXME("Option INTERNET_OPTION_REQUEST_PRIORITY (%ld): STUB\n",priority);
2297 break;
2298 case INTERNET_OPTION_CONNECT_TIMEOUT:
2300 unsigned long connecttimeout=*(unsigned long*)lpBuffer;
2301 FIXME("Option INTERNET_OPTION_CONNECT_TIMEOUT (%ld): STUB\n",connecttimeout);
2303 break;
2304 case INTERNET_OPTION_DATA_RECEIVE_TIMEOUT:
2306 unsigned long receivetimeout=*(unsigned long*)lpBuffer;
2307 FIXME("Option INTERNET_OPTION_DATA_RECEIVE_TIMEOUT (%ld): STUB\n",receivetimeout);
2309 break;
2310 case INTERNET_OPTION_MAX_CONNS_PER_SERVER:
2312 unsigned long conns=*(unsigned long*)lpBuffer;
2313 FIXME("Option INTERNET_OPTION_MAX_CONNS_PER_SERVER (%ld): STUB\n",conns);
2315 break;
2316 case INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER:
2318 unsigned long conns=*(unsigned long*)lpBuffer;
2319 FIXME("Option INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER (%ld): STUB\n",conns);
2321 break;
2322 case INTERNET_OPTION_RESET_URLCACHE_SESSION:
2323 FIXME("Option INTERNET_OPTION_RESET_URLCACHE_SESSION: STUB\n");
2324 break;
2325 case INTERNET_OPTION_END_BROWSER_SESSION:
2326 FIXME("Option INTERNET_OPTION_END_BROWSER_SESSION: STUB\n");
2327 break;
2328 case INTERNET_OPTION_CONNECTED_STATE:
2329 FIXME("Option INTERNET_OPTION_CONNECTED_STATE: STUB\n");
2330 break;
2331 case INTERNET_OPTION_DISABLE_PASSPORT_AUTH:
2332 TRACE("Option INTERNET_OPTION_DISABLE_PASSPORT_AUTH: harmless stub, since not enabled\n");
2333 break;
2334 case INTERNET_OPTION_SEND_TIMEOUT:
2335 case INTERNET_OPTION_RECEIVE_TIMEOUT:
2336 FIXME("INTERNET_OPTION_SEND/RECEIVE_TIMEOUT\n");
2337 break;
2338 case INTERNET_OPTION_CONNECT_RETRIES:
2339 FIXME("Option INTERNET_OPTION_CONNECT_RETRIES: STUB\n");
2340 break;
2341 case INTERNET_OPTION_CONTEXT_VALUE:
2342 FIXME("Option INTERNET_OPTION_CONTEXT_VALUE; STUB\n");
2343 break;
2344 case INTERNET_OPTION_SECURITY_FLAGS:
2345 FIXME("Option INTERNET_OPTION_SECURITY_FLAGS; STUB\n");
2346 break;
2347 case 86:
2348 FIXME("86\n");
2349 break;
2350 default:
2351 FIXME("Option %d STUB\n",dwOption);
2352 INTERNET_SetLastError(ERROR_INTERNET_INVALID_OPTION);
2353 ret = FALSE;
2354 break;
2357 if(lpwhh)
2358 WININET_Release( lpwhh );
2360 return ret;
2364 /***********************************************************************
2365 * InternetSetOptionA (WININET.@)
2367 * Sets an options on the specified handle.
2369 * RETURNS
2370 * TRUE on success
2371 * FALSE on failure
2374 BOOL WINAPI InternetSetOptionA(HINTERNET hInternet, DWORD dwOption,
2375 LPVOID lpBuffer, DWORD dwBufferLength)
2377 LPVOID wbuffer;
2378 DWORD wlen;
2379 BOOL r;
2381 switch( dwOption )
2383 case INTERNET_OPTION_CALLBACK:
2385 LPWININETHANDLEHEADER lpwh;
2386 INTERNET_STATUS_CALLBACK callback = *(INTERNET_STATUS_CALLBACK *)lpBuffer;
2388 if (!(lpwh = WININET_GetObject(hInternet))) return FALSE;
2389 r = (set_status_callback(lpwh, callback, FALSE) != INTERNET_INVALID_STATUS_CALLBACK);
2390 WININET_Release(lpwh);
2391 return r;
2393 case INTERNET_OPTION_PROXY:
2395 LPINTERNET_PROXY_INFOA pi = (LPINTERNET_PROXY_INFOA) lpBuffer;
2396 LPINTERNET_PROXY_INFOW piw;
2397 DWORD proxlen, prbylen;
2398 LPWSTR prox, prby;
2400 proxlen = MultiByteToWideChar( CP_ACP, 0, pi->lpszProxy, -1, NULL, 0);
2401 prbylen= MultiByteToWideChar( CP_ACP, 0, pi->lpszProxyBypass, -1, NULL, 0);
2402 wlen = sizeof(*piw) + proxlen + prbylen;
2403 wbuffer = HeapAlloc( GetProcessHeap(), 0, wlen*sizeof(WCHAR) );
2404 piw = (LPINTERNET_PROXY_INFOW) wbuffer;
2405 piw->dwAccessType = pi->dwAccessType;
2406 prox = (LPWSTR) &piw[1];
2407 prby = &prox[proxlen+1];
2408 MultiByteToWideChar( CP_ACP, 0, pi->lpszProxy, -1, prox, proxlen);
2409 MultiByteToWideChar( CP_ACP, 0, pi->lpszProxyBypass, -1, prby, prbylen);
2410 piw->lpszProxy = prox;
2411 piw->lpszProxyBypass = prby;
2413 break;
2414 case INTERNET_OPTION_USER_AGENT:
2415 case INTERNET_OPTION_USERNAME:
2416 case INTERNET_OPTION_PASSWORD:
2417 wlen = MultiByteToWideChar( CP_ACP, 0, lpBuffer, dwBufferLength,
2418 NULL, 0 );
2419 wbuffer = HeapAlloc( GetProcessHeap(), 0, wlen*sizeof(WCHAR) );
2420 MultiByteToWideChar( CP_ACP, 0, lpBuffer, dwBufferLength,
2421 wbuffer, wlen );
2422 break;
2423 default:
2424 wbuffer = lpBuffer;
2425 wlen = dwBufferLength;
2428 r = InternetSetOptionW(hInternet,dwOption, wbuffer, wlen);
2430 if( lpBuffer != wbuffer )
2431 HeapFree( GetProcessHeap(), 0, wbuffer );
2433 return r;
2437 /***********************************************************************
2438 * InternetSetOptionExA (WININET.@)
2440 BOOL WINAPI InternetSetOptionExA(HINTERNET hInternet, DWORD dwOption,
2441 LPVOID lpBuffer, DWORD dwBufferLength, DWORD dwFlags)
2443 FIXME("Flags %08x ignored\n", dwFlags);
2444 return InternetSetOptionA( hInternet, dwOption, lpBuffer, dwBufferLength );
2447 /***********************************************************************
2448 * InternetSetOptionExW (WININET.@)
2450 BOOL WINAPI InternetSetOptionExW(HINTERNET hInternet, DWORD dwOption,
2451 LPVOID lpBuffer, DWORD dwBufferLength, DWORD dwFlags)
2453 FIXME("Flags %08x ignored\n", dwFlags);
2454 if( dwFlags & ~ISO_VALID_FLAGS )
2456 INTERNET_SetLastError( ERROR_INVALID_PARAMETER );
2457 return FALSE;
2459 return InternetSetOptionW( hInternet, dwOption, lpBuffer, dwBufferLength );
2462 static const WCHAR WININET_wkday[7][4] =
2463 { { 'S','u','n', 0 }, { 'M','o','n', 0 }, { 'T','u','e', 0 }, { 'W','e','d', 0 },
2464 { 'T','h','u', 0 }, { 'F','r','i', 0 }, { 'S','a','t', 0 } };
2465 static const WCHAR WININET_month[12][4] =
2466 { { 'J','a','n', 0 }, { 'F','e','b', 0 }, { 'M','a','r', 0 }, { 'A','p','r', 0 },
2467 { 'M','a','y', 0 }, { 'J','u','n', 0 }, { 'J','u','l', 0 }, { 'A','u','g', 0 },
2468 { 'S','e','p', 0 }, { 'O','c','t', 0 }, { 'N','o','v', 0 }, { 'D','e','c', 0 } };
2470 /***********************************************************************
2471 * InternetTimeFromSystemTimeA (WININET.@)
2473 BOOL WINAPI InternetTimeFromSystemTimeA( const SYSTEMTIME* time, DWORD format, LPSTR string, DWORD size )
2475 BOOL ret;
2476 WCHAR stringW[INTERNET_RFC1123_BUFSIZE];
2478 TRACE( "%p 0x%08x %p 0x%08x\n", time, format, string, size );
2480 ret = InternetTimeFromSystemTimeW( time, format, stringW, sizeof(stringW) );
2481 if (ret) WideCharToMultiByte( CP_ACP, 0, stringW, -1, string, size, NULL, NULL );
2483 return ret;
2486 /***********************************************************************
2487 * InternetTimeFromSystemTimeW (WININET.@)
2489 BOOL WINAPI InternetTimeFromSystemTimeW( const SYSTEMTIME* time, DWORD format, LPWSTR string, DWORD size )
2491 static const WCHAR date[] =
2492 { '%','s',',',' ','%','0','2','d',' ','%','s',' ','%','4','d',' ','%','0',
2493 '2','d',':','%','0','2','d',':','%','0','2','d',' ','G','M','T', 0 };
2495 TRACE( "%p 0x%08x %p 0x%08x\n", time, format, string, size );
2497 if (!time || !string) return FALSE;
2499 if (format != INTERNET_RFC1123_FORMAT || size < INTERNET_RFC1123_BUFSIZE * sizeof(WCHAR))
2500 return FALSE;
2502 sprintfW( string, date,
2503 WININET_wkday[time->wDayOfWeek],
2504 time->wDay,
2505 WININET_month[time->wMonth - 1],
2506 time->wYear,
2507 time->wHour,
2508 time->wMinute,
2509 time->wSecond );
2511 return TRUE;
2514 /***********************************************************************
2515 * InternetTimeToSystemTimeA (WININET.@)
2517 BOOL WINAPI InternetTimeToSystemTimeA( LPCSTR string, SYSTEMTIME* time, DWORD reserved )
2519 BOOL ret = FALSE;
2520 WCHAR *stringW;
2521 int len;
2523 TRACE( "%s %p 0x%08x\n", debugstr_a(string), time, reserved );
2525 len = MultiByteToWideChar( CP_ACP, 0, string, -1, NULL, 0 );
2526 stringW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
2528 if (stringW)
2530 MultiByteToWideChar( CP_ACP, 0, string, -1, stringW, len );
2531 ret = InternetTimeToSystemTimeW( stringW, time, reserved );
2532 HeapFree( GetProcessHeap(), 0, stringW );
2534 return ret;
2537 /***********************************************************************
2538 * InternetTimeToSystemTimeW (WININET.@)
2540 BOOL WINAPI InternetTimeToSystemTimeW( LPCWSTR string, SYSTEMTIME* time, DWORD reserved )
2542 unsigned int i;
2543 const WCHAR *s = string;
2544 WCHAR *end;
2546 TRACE( "%s %p 0x%08x\n", debugstr_w(string), time, reserved );
2548 if (!string || !time) return FALSE;
2550 /* Windows does this too */
2551 GetSystemTime( time );
2553 /* Convert an RFC1123 time such as 'Fri, 07 Jan 2005 12:06:35 GMT' into
2554 * a SYSTEMTIME structure.
2557 while (*s && !isalphaW( *s )) s++;
2558 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
2559 time->wDayOfWeek = 7;
2561 for (i = 0; i < 7; i++)
2563 if (toupperW( WININET_wkday[i][0] ) == toupperW( s[0] ) &&
2564 toupperW( WININET_wkday[i][1] ) == toupperW( s[1] ) &&
2565 toupperW( WININET_wkday[i][2] ) == toupperW( s[2] ) )
2567 time->wDayOfWeek = i;
2568 break;
2572 if (time->wDayOfWeek > 6) return TRUE;
2573 while (*s && !isdigitW( *s )) s++;
2574 time->wDay = strtolW( s, &end, 10 );
2575 s = end;
2577 while (*s && !isalphaW( *s )) s++;
2578 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
2579 time->wMonth = 0;
2581 for (i = 0; i < 12; i++)
2583 if (toupperW( WININET_month[i][0]) == toupperW( s[0] ) &&
2584 toupperW( WININET_month[i][1]) == toupperW( s[1] ) &&
2585 toupperW( WININET_month[i][2]) == toupperW( s[2] ) )
2587 time->wMonth = i + 1;
2588 break;
2591 if (time->wMonth == 0) return TRUE;
2593 while (*s && !isdigitW( *s )) s++;
2594 if (*s == '\0') return TRUE;
2595 time->wYear = strtolW( s, &end, 10 );
2596 s = end;
2598 while (*s && !isdigitW( *s )) s++;
2599 if (*s == '\0') return TRUE;
2600 time->wHour = strtolW( s, &end, 10 );
2601 s = end;
2603 while (*s && !isdigitW( *s )) s++;
2604 if (*s == '\0') return TRUE;
2605 time->wMinute = strtolW( s, &end, 10 );
2606 s = end;
2608 while (*s && !isdigitW( *s )) s++;
2609 if (*s == '\0') return TRUE;
2610 time->wSecond = strtolW( s, &end, 10 );
2611 s = end;
2613 time->wMilliseconds = 0;
2614 return TRUE;
2617 /***********************************************************************
2618 * InternetCheckConnectionW (WININET.@)
2620 * Pings a requested host to check internet connection
2622 * RETURNS
2623 * TRUE on success and FALSE on failure. If a failure then
2624 * ERROR_NOT_CONNECTED is placed into GetLastError
2627 BOOL WINAPI InternetCheckConnectionW( LPCWSTR lpszUrl, DWORD dwFlags, DWORD dwReserved )
2630 * this is a kludge which runs the resident ping program and reads the output.
2632 * Anyone have a better idea?
2635 BOOL rc = FALSE;
2636 static const CHAR ping[] = "ping -c 1 ";
2637 static const CHAR redirect[] = " >/dev/null 2>/dev/null";
2638 CHAR *command = NULL;
2639 WCHAR hostW[1024];
2640 DWORD len;
2641 INTERNET_PORT port;
2642 int status = -1;
2644 FIXME("\n");
2647 * Crack or set the Address
2649 if (lpszUrl == NULL)
2652 * According to the doc we are supposed to use the ip for the next
2653 * server in the WnInet internal server database. I have
2654 * no idea what that is or how to get it.
2656 * So someone needs to implement this.
2658 FIXME("Unimplemented with URL of NULL\n");
2659 return TRUE;
2661 else
2663 URL_COMPONENTSW components;
2665 ZeroMemory(&components,sizeof(URL_COMPONENTSW));
2666 components.lpszHostName = (LPWSTR)&hostW;
2667 components.dwHostNameLength = 1024;
2669 if (!InternetCrackUrlW(lpszUrl,0,0,&components))
2670 goto End;
2672 TRACE("host name : %s\n",debugstr_w(components.lpszHostName));
2673 port = components.nPort;
2674 TRACE("port: %d\n", port);
2677 if (dwFlags & FLAG_ICC_FORCE_CONNECTION)
2679 struct sockaddr_in sin;
2680 int fd;
2682 if (!GetAddress(hostW, port, &sin))
2683 goto End;
2684 fd = socket(sin.sin_family, SOCK_STREAM, 0);
2685 if (fd != -1)
2687 if (connect(fd, (struct sockaddr *)&sin, sizeof(sin)) == 0)
2688 rc = TRUE;
2689 close(fd);
2692 else
2695 * Build our ping command
2697 len = WideCharToMultiByte(CP_UNIXCP, 0, hostW, -1, NULL, 0, NULL, NULL);
2698 command = HeapAlloc( GetProcessHeap(), 0, strlen(ping)+len+strlen(redirect) );
2699 strcpy(command,ping);
2700 WideCharToMultiByte(CP_UNIXCP, 0, hostW, -1, command+strlen(ping), len, NULL, NULL);
2701 strcat(command,redirect);
2703 TRACE("Ping command is : %s\n",command);
2705 status = system(command);
2707 TRACE("Ping returned a code of %i\n",status);
2709 /* Ping return code of 0 indicates success */
2710 if (status == 0)
2711 rc = TRUE;
2714 End:
2716 HeapFree( GetProcessHeap(), 0, command );
2717 if (rc == FALSE)
2718 INTERNET_SetLastError(ERROR_NOT_CONNECTED);
2720 return rc;
2724 /***********************************************************************
2725 * InternetCheckConnectionA (WININET.@)
2727 * Pings a requested host to check internet connection
2729 * RETURNS
2730 * TRUE on success and FALSE on failure. If a failure then
2731 * ERROR_NOT_CONNECTED is placed into GetLastError
2734 BOOL WINAPI InternetCheckConnectionA(LPCSTR lpszUrl, DWORD dwFlags, DWORD dwReserved)
2736 WCHAR *szUrl;
2737 INT len;
2738 BOOL rc;
2740 len = MultiByteToWideChar(CP_ACP, 0, lpszUrl, -1, NULL, 0);
2741 if (!(szUrl = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR))))
2742 return FALSE;
2743 MultiByteToWideChar(CP_ACP, 0, lpszUrl, -1, szUrl, len);
2744 rc = InternetCheckConnectionW(szUrl, dwFlags, dwReserved);
2745 HeapFree(GetProcessHeap(), 0, szUrl);
2747 return rc;
2751 /**********************************************************
2752 * INTERNET_InternetOpenUrlW (internal)
2754 * Opens an URL
2756 * RETURNS
2757 * handle of connection or NULL on failure
2759 HINTERNET WINAPI INTERNET_InternetOpenUrlW(LPWININETAPPINFOW hIC, LPCWSTR lpszUrl,
2760 LPCWSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext)
2762 URL_COMPONENTSW urlComponents;
2763 WCHAR protocol[32], hostName[MAXHOSTNAME], userName[1024];
2764 WCHAR password[1024], path[2048], extra[1024];
2765 HINTERNET client = NULL, client1 = NULL;
2767 TRACE("(%p, %s, %s, %08x, %08x, %08lx)\n", hIC, debugstr_w(lpszUrl), debugstr_w(lpszHeaders),
2768 dwHeadersLength, dwFlags, dwContext);
2770 urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
2771 urlComponents.lpszScheme = protocol;
2772 urlComponents.dwSchemeLength = 32;
2773 urlComponents.lpszHostName = hostName;
2774 urlComponents.dwHostNameLength = MAXHOSTNAME;
2775 urlComponents.lpszUserName = userName;
2776 urlComponents.dwUserNameLength = 1024;
2777 urlComponents.lpszPassword = password;
2778 urlComponents.dwPasswordLength = 1024;
2779 urlComponents.lpszUrlPath = path;
2780 urlComponents.dwUrlPathLength = 2048;
2781 urlComponents.lpszExtraInfo = extra;
2782 urlComponents.dwExtraInfoLength = 1024;
2783 if(!InternetCrackUrlW(lpszUrl, strlenW(lpszUrl), 0, &urlComponents))
2784 return NULL;
2785 switch(urlComponents.nScheme) {
2786 case INTERNET_SCHEME_FTP:
2787 if(urlComponents.nPort == 0)
2788 urlComponents.nPort = INTERNET_DEFAULT_FTP_PORT;
2789 client = FTP_Connect(hIC, hostName, urlComponents.nPort,
2790 userName, password, dwFlags, dwContext, INET_OPENURL);
2791 if(client == NULL)
2792 break;
2793 client1 = FtpOpenFileW(client, path, GENERIC_READ, dwFlags, dwContext);
2794 if(client1 == NULL) {
2795 InternetCloseHandle(client);
2796 break;
2798 break;
2800 case INTERNET_SCHEME_HTTP:
2801 case INTERNET_SCHEME_HTTPS: {
2802 static const WCHAR szStars[] = { '*','/','*', 0 };
2803 LPCWSTR accept[2] = { szStars, NULL };
2804 if(urlComponents.nPort == 0) {
2805 if(urlComponents.nScheme == INTERNET_SCHEME_HTTP)
2806 urlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
2807 else
2808 urlComponents.nPort = INTERNET_DEFAULT_HTTPS_PORT;
2810 /* FIXME: should use pointers, not handles, as handles are not thread-safe */
2811 client = HTTP_Connect(hIC, hostName, urlComponents.nPort,
2812 userName, password, dwFlags, dwContext, INET_OPENURL);
2813 if(client == NULL)
2814 break;
2816 if (urlComponents.dwExtraInfoLength) {
2817 WCHAR *path_extra;
2818 DWORD len = urlComponents.dwUrlPathLength + urlComponents.dwExtraInfoLength + 1;
2820 if (!(path_extra = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR))))
2822 InternetCloseHandle(client);
2823 break;
2825 strcpyW(path_extra, urlComponents.lpszUrlPath);
2826 strcatW(path_extra, urlComponents.lpszExtraInfo);
2827 client1 = HttpOpenRequestW(client, NULL, path_extra, NULL, NULL, accept, dwFlags, dwContext);
2828 HeapFree(GetProcessHeap(), 0, path_extra);
2830 else
2831 client1 = HttpOpenRequestW(client, NULL, path, NULL, NULL, accept, dwFlags, dwContext);
2833 if(client1 == NULL) {
2834 InternetCloseHandle(client);
2835 break;
2837 HttpAddRequestHeadersW(client1, lpszHeaders, dwHeadersLength, HTTP_ADDREQ_FLAG_ADD);
2838 if (!HttpSendRequestW(client1, NULL, 0, NULL, 0) &&
2839 GetLastError() != ERROR_IO_PENDING) {
2840 InternetCloseHandle(client1);
2841 client1 = NULL;
2842 break;
2845 case INTERNET_SCHEME_GOPHER:
2846 /* gopher doesn't seem to be implemented in wine, but it's supposed
2847 * to be supported by InternetOpenUrlA. */
2848 default:
2849 INTERNET_SetLastError(ERROR_INTERNET_UNRECOGNIZED_SCHEME);
2850 break;
2853 TRACE(" %p <--\n", client1);
2855 return client1;
2858 /**********************************************************
2859 * InternetOpenUrlW (WININET.@)
2861 * Opens an URL
2863 * RETURNS
2864 * handle of connection or NULL on failure
2866 static void AsyncInternetOpenUrlProc(WORKREQUEST *workRequest)
2868 struct WORKREQ_INTERNETOPENURLW const *req = &workRequest->u.InternetOpenUrlW;
2869 LPWININETAPPINFOW hIC = (LPWININETAPPINFOW) workRequest->hdr;
2871 TRACE("%p\n", hIC);
2873 INTERNET_InternetOpenUrlW(hIC, req->lpszUrl,
2874 req->lpszHeaders, req->dwHeadersLength, req->dwFlags, req->dwContext);
2875 HeapFree(GetProcessHeap(), 0, req->lpszUrl);
2876 HeapFree(GetProcessHeap(), 0, req->lpszHeaders);
2879 HINTERNET WINAPI InternetOpenUrlW(HINTERNET hInternet, LPCWSTR lpszUrl,
2880 LPCWSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext)
2882 HINTERNET ret = NULL;
2883 LPWININETAPPINFOW hIC = NULL;
2885 if (TRACE_ON(wininet)) {
2886 TRACE("(%p, %s, %s, %08x, %08x, %08lx)\n", hInternet, debugstr_w(lpszUrl), debugstr_w(lpszHeaders),
2887 dwHeadersLength, dwFlags, dwContext);
2888 TRACE(" flags :");
2889 dump_INTERNET_FLAGS(dwFlags);
2892 if (!lpszUrl)
2894 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
2895 goto lend;
2898 hIC = (LPWININETAPPINFOW) WININET_GetObject( hInternet );
2899 if (NULL == hIC || hIC->hdr.htype != WH_HINIT) {
2900 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2901 goto lend;
2904 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC) {
2905 WORKREQUEST workRequest;
2906 struct WORKREQ_INTERNETOPENURLW *req;
2908 workRequest.asyncproc = AsyncInternetOpenUrlProc;
2909 workRequest.hdr = WININET_AddRef( &hIC->hdr );
2910 req = &workRequest.u.InternetOpenUrlW;
2911 req->lpszUrl = WININET_strdupW(lpszUrl);
2912 if (lpszHeaders)
2913 req->lpszHeaders = WININET_strdupW(lpszHeaders);
2914 else
2915 req->lpszHeaders = 0;
2916 req->dwHeadersLength = dwHeadersLength;
2917 req->dwFlags = dwFlags;
2918 req->dwContext = dwContext;
2920 INTERNET_AsyncCall(&workRequest);
2922 * This is from windows.
2924 INTERNET_SetLastError(ERROR_IO_PENDING);
2925 } else {
2926 ret = INTERNET_InternetOpenUrlW(hIC, lpszUrl, lpszHeaders, dwHeadersLength, dwFlags, dwContext);
2929 lend:
2930 if( hIC )
2931 WININET_Release( &hIC->hdr );
2932 TRACE(" %p <--\n", ret);
2934 return ret;
2937 /**********************************************************
2938 * InternetOpenUrlA (WININET.@)
2940 * Opens an URL
2942 * RETURNS
2943 * handle of connection or NULL on failure
2945 HINTERNET WINAPI InternetOpenUrlA(HINTERNET hInternet, LPCSTR lpszUrl,
2946 LPCSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext)
2948 HINTERNET rc = NULL;
2950 INT lenUrl;
2951 INT lenHeaders = 0;
2952 LPWSTR szUrl = NULL;
2953 LPWSTR szHeaders = NULL;
2955 TRACE("\n");
2957 if(lpszUrl) {
2958 lenUrl = MultiByteToWideChar(CP_ACP, 0, lpszUrl, -1, NULL, 0 );
2959 szUrl = HeapAlloc(GetProcessHeap(), 0, lenUrl*sizeof(WCHAR));
2960 if(!szUrl)
2961 return NULL;
2962 MultiByteToWideChar(CP_ACP, 0, lpszUrl, -1, szUrl, lenUrl);
2965 if(lpszHeaders) {
2966 lenHeaders = MultiByteToWideChar(CP_ACP, 0, lpszHeaders, dwHeadersLength, NULL, 0 );
2967 szHeaders = HeapAlloc(GetProcessHeap(), 0, lenHeaders*sizeof(WCHAR));
2968 if(!szHeaders) {
2969 HeapFree(GetProcessHeap(), 0, szUrl);
2970 return NULL;
2972 MultiByteToWideChar(CP_ACP, 0, lpszHeaders, dwHeadersLength, szHeaders, lenHeaders);
2975 rc = InternetOpenUrlW(hInternet, szUrl, szHeaders,
2976 lenHeaders, dwFlags, dwContext);
2978 HeapFree(GetProcessHeap(), 0, szUrl);
2979 HeapFree(GetProcessHeap(), 0, szHeaders);
2981 return rc;
2985 static LPWITHREADERROR INTERNET_AllocThreadError(void)
2987 LPWITHREADERROR lpwite = HeapAlloc(GetProcessHeap(), 0, sizeof(*lpwite));
2989 if (lpwite)
2991 lpwite->dwError = 0;
2992 lpwite->response[0] = '\0';
2995 if (!TlsSetValue(g_dwTlsErrIndex, lpwite))
2997 HeapFree(GetProcessHeap(), 0, lpwite);
2998 return NULL;
3001 return lpwite;
3005 /***********************************************************************
3006 * INTERNET_SetLastError (internal)
3008 * Set last thread specific error
3010 * RETURNS
3013 void INTERNET_SetLastError(DWORD dwError)
3015 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
3017 if (!lpwite)
3018 lpwite = INTERNET_AllocThreadError();
3020 SetLastError(dwError);
3021 if(lpwite)
3022 lpwite->dwError = dwError;
3026 /***********************************************************************
3027 * INTERNET_GetLastError (internal)
3029 * Get last thread specific error
3031 * RETURNS
3034 DWORD INTERNET_GetLastError(void)
3036 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
3037 if (!lpwite) return 0;
3038 /* TlsGetValue clears last error, so set it again here */
3039 SetLastError(lpwite->dwError);
3040 return lpwite->dwError;
3044 /***********************************************************************
3045 * INTERNET_WorkerThreadFunc (internal)
3047 * Worker thread execution function
3049 * RETURNS
3052 static DWORD CALLBACK INTERNET_WorkerThreadFunc(LPVOID lpvParam)
3054 LPWORKREQUEST lpRequest = lpvParam;
3055 WORKREQUEST workRequest;
3057 TRACE("\n");
3059 memcpy(&workRequest, lpRequest, sizeof(WORKREQUEST));
3060 HeapFree(GetProcessHeap(), 0, lpRequest);
3062 workRequest.asyncproc(&workRequest);
3064 WININET_Release( workRequest.hdr );
3065 return TRUE;
3069 /***********************************************************************
3070 * INTERNET_AsyncCall (internal)
3072 * Retrieves work request from queue
3074 * RETURNS
3077 BOOL INTERNET_AsyncCall(LPWORKREQUEST lpWorkRequest)
3079 BOOL bSuccess;
3080 LPWORKREQUEST lpNewRequest;
3082 TRACE("\n");
3084 lpNewRequest = HeapAlloc(GetProcessHeap(), 0, sizeof(WORKREQUEST));
3085 if (!lpNewRequest)
3086 return FALSE;
3088 memcpy(lpNewRequest, lpWorkRequest, sizeof(WORKREQUEST));
3090 bSuccess = QueueUserWorkItem(INTERNET_WorkerThreadFunc, lpNewRequest, WT_EXECUTELONGFUNCTION);
3091 if (!bSuccess)
3093 HeapFree(GetProcessHeap(), 0, lpNewRequest);
3094 INTERNET_SetLastError(ERROR_INTERNET_ASYNC_THREAD_FAILED);
3097 return bSuccess;
3101 /***********************************************************************
3102 * INTERNET_GetResponseBuffer (internal)
3104 * RETURNS
3107 LPSTR INTERNET_GetResponseBuffer(void)
3109 LPWITHREADERROR lpwite = (LPWITHREADERROR)TlsGetValue(g_dwTlsErrIndex);
3110 if (!lpwite)
3111 lpwite = INTERNET_AllocThreadError();
3112 TRACE("\n");
3113 return lpwite->response;
3116 /***********************************************************************
3117 * INTERNET_GetNextLine (internal)
3119 * Parse next line in directory string listing
3121 * RETURNS
3122 * Pointer to beginning of next line
3123 * NULL on failure
3127 LPSTR INTERNET_GetNextLine(INT nSocket, LPDWORD dwLen)
3129 struct timeval tv;
3130 fd_set infd;
3131 BOOL bSuccess = FALSE;
3132 INT nRecv = 0;
3133 LPSTR lpszBuffer = INTERNET_GetResponseBuffer();
3135 TRACE("\n");
3137 FD_ZERO(&infd);
3138 FD_SET(nSocket, &infd);
3139 tv.tv_sec=RESPONSE_TIMEOUT;
3140 tv.tv_usec=0;
3142 while (nRecv < MAX_REPLY_LEN)
3144 if (select(nSocket+1,&infd,NULL,NULL,&tv) > 0)
3146 if (recv(nSocket, &lpszBuffer[nRecv], 1, 0) <= 0)
3148 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
3149 goto lend;
3152 if (lpszBuffer[nRecv] == '\n')
3154 bSuccess = TRUE;
3155 break;
3157 if (lpszBuffer[nRecv] != '\r')
3158 nRecv++;
3160 else
3162 INTERNET_SetLastError(ERROR_INTERNET_TIMEOUT);
3163 goto lend;
3167 lend:
3168 if (bSuccess)
3170 lpszBuffer[nRecv] = '\0';
3171 *dwLen = nRecv - 1;
3172 TRACE(":%d %s\n", nRecv, lpszBuffer);
3173 return lpszBuffer;
3175 else
3177 return NULL;
3181 /**********************************************************
3182 * InternetQueryDataAvailable (WININET.@)
3184 * Determines how much data is available to be read.
3186 * RETURNS
3187 * TRUE on success, FALSE if an error occurred. If
3188 * INTERNET_FLAG_ASYNC was specified in InternetOpen, and
3189 * no data is presently available, FALSE is returned with
3190 * the last error ERROR_IO_PENDING; a callback with status
3191 * INTERNET_STATUS_REQUEST_COMPLETE will be sent when more
3192 * data is available.
3194 BOOL WINAPI InternetQueryDataAvailable( HINTERNET hFile,
3195 LPDWORD lpdwNumberOfBytesAvailble,
3196 DWORD dwFlags, DWORD_PTR dwContext)
3198 WININETHANDLEHEADER *hdr;
3199 DWORD res;
3201 TRACE("(%p %p %x %lx)\n", hFile, lpdwNumberOfBytesAvailble, dwFlags, dwContext);
3203 hdr = WININET_GetObject( hFile );
3204 if (!hdr) {
3205 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
3206 return FALSE;
3209 if(hdr->vtbl->QueryDataAvailable) {
3210 res = hdr->vtbl->QueryDataAvailable(hdr, lpdwNumberOfBytesAvailble, dwFlags, dwContext);
3211 }else {
3212 WARN("wrong handle\n");
3213 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
3216 WININET_Release(hdr);
3218 if(res != ERROR_SUCCESS)
3219 SetLastError(res);
3220 return res == ERROR_SUCCESS;
3224 /***********************************************************************
3225 * InternetLockRequestFile (WININET.@)
3227 BOOL WINAPI InternetLockRequestFile( HINTERNET hInternet, HANDLE
3228 *lphLockReqHandle)
3230 FIXME("STUB\n");
3231 return FALSE;
3234 BOOL WINAPI InternetUnlockRequestFile( HANDLE hLockHandle)
3236 FIXME("STUB\n");
3237 return FALSE;
3241 /***********************************************************************
3242 * InternetAutodial (WININET.@)
3244 * On windows this function is supposed to dial the default internet
3245 * connection. We don't want to have Wine dial out to the internet so
3246 * we return TRUE by default. It might be nice to check if we are connected.
3248 * RETURNS
3249 * TRUE on success
3250 * FALSE on failure
3253 BOOL WINAPI InternetAutodial(DWORD dwFlags, HWND hwndParent)
3255 FIXME("STUB\n");
3257 /* Tell that we are connected to the internet. */
3258 return TRUE;
3261 /***********************************************************************
3262 * InternetAutodialHangup (WININET.@)
3264 * Hangs up a connection made with InternetAutodial
3266 * PARAM
3267 * dwReserved
3268 * RETURNS
3269 * TRUE on success
3270 * FALSE on failure
3273 BOOL WINAPI InternetAutodialHangup(DWORD dwReserved)
3275 FIXME("STUB\n");
3277 /* we didn't dial, we don't disconnect */
3278 return TRUE;
3281 /***********************************************************************
3282 * InternetCombineUrlA (WININET.@)
3284 * Combine a base URL with a relative URL
3286 * RETURNS
3287 * TRUE on success
3288 * FALSE on failure
3292 BOOL WINAPI InternetCombineUrlA(LPCSTR lpszBaseUrl, LPCSTR lpszRelativeUrl,
3293 LPSTR lpszBuffer, LPDWORD lpdwBufferLength,
3294 DWORD dwFlags)
3296 HRESULT hr=S_OK;
3298 TRACE("(%s, %s, %p, %p, 0x%08x)\n", debugstr_a(lpszBaseUrl), debugstr_a(lpszRelativeUrl), lpszBuffer, lpdwBufferLength, dwFlags);
3300 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
3301 dwFlags ^= ICU_NO_ENCODE;
3302 hr=UrlCombineA(lpszBaseUrl,lpszRelativeUrl,lpszBuffer,lpdwBufferLength,dwFlags);
3304 return (hr==S_OK);
3307 /***********************************************************************
3308 * InternetCombineUrlW (WININET.@)
3310 * Combine a base URL with a relative URL
3312 * RETURNS
3313 * TRUE on success
3314 * FALSE on failure
3318 BOOL WINAPI InternetCombineUrlW(LPCWSTR lpszBaseUrl, LPCWSTR lpszRelativeUrl,
3319 LPWSTR lpszBuffer, LPDWORD lpdwBufferLength,
3320 DWORD dwFlags)
3322 HRESULT hr=S_OK;
3324 TRACE("(%s, %s, %p, %p, 0x%08x)\n", debugstr_w(lpszBaseUrl), debugstr_w(lpszRelativeUrl), lpszBuffer, lpdwBufferLength, dwFlags);
3326 /* Flip this bit to correspond to URL_ESCAPE_UNSAFE */
3327 dwFlags ^= ICU_NO_ENCODE;
3328 hr=UrlCombineW(lpszBaseUrl,lpszRelativeUrl,lpszBuffer,lpdwBufferLength,dwFlags);
3330 return (hr==S_OK);
3333 /* max port num is 65535 => 5 digits */
3334 #define MAX_WORD_DIGITS 5
3336 #define URL_GET_COMP_LENGTH(url, component) ((url)->dw##component##Length ? \
3337 (url)->dw##component##Length : strlenW((url)->lpsz##component))
3338 #define URL_GET_COMP_LENGTHA(url, component) ((url)->dw##component##Length ? \
3339 (url)->dw##component##Length : strlen((url)->lpsz##component))
3341 static BOOL url_uses_default_port(INTERNET_SCHEME nScheme, INTERNET_PORT nPort)
3343 if ((nScheme == INTERNET_SCHEME_HTTP) &&
3344 (nPort == INTERNET_DEFAULT_HTTP_PORT))
3345 return TRUE;
3346 if ((nScheme == INTERNET_SCHEME_HTTPS) &&
3347 (nPort == INTERNET_DEFAULT_HTTPS_PORT))
3348 return TRUE;
3349 if ((nScheme == INTERNET_SCHEME_FTP) &&
3350 (nPort == INTERNET_DEFAULT_FTP_PORT))
3351 return TRUE;
3352 if ((nScheme == INTERNET_SCHEME_GOPHER) &&
3353 (nPort == INTERNET_DEFAULT_GOPHER_PORT))
3354 return TRUE;
3356 if (nPort == INTERNET_INVALID_PORT_NUMBER)
3357 return TRUE;
3359 return FALSE;
3362 /* opaque urls do not fit into the standard url hierarchy and don't have
3363 * two following slashes */
3364 static inline BOOL scheme_is_opaque(INTERNET_SCHEME nScheme)
3366 return (nScheme != INTERNET_SCHEME_FTP) &&
3367 (nScheme != INTERNET_SCHEME_GOPHER) &&
3368 (nScheme != INTERNET_SCHEME_HTTP) &&
3369 (nScheme != INTERNET_SCHEME_HTTPS) &&
3370 (nScheme != INTERNET_SCHEME_FILE);
3373 static LPCWSTR INTERNET_GetSchemeString(INTERNET_SCHEME scheme)
3375 int index;
3376 if (scheme < INTERNET_SCHEME_FIRST)
3377 return NULL;
3378 index = scheme - INTERNET_SCHEME_FIRST;
3379 if (index >= sizeof(url_schemes)/sizeof(url_schemes[0]))
3380 return NULL;
3381 return (LPCWSTR)&url_schemes[index];
3384 /* we can calculate using ansi strings because we're just
3385 * calculating string length, not size
3387 static BOOL calc_url_length(LPURL_COMPONENTSW lpUrlComponents,
3388 LPDWORD lpdwUrlLength)
3390 INTERNET_SCHEME nScheme;
3392 *lpdwUrlLength = 0;
3394 if (lpUrlComponents->lpszScheme)
3396 DWORD dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, Scheme);
3397 *lpdwUrlLength += dwLen;
3398 nScheme = GetInternetSchemeW(lpUrlComponents->lpszScheme, dwLen);
3400 else
3402 LPCWSTR scheme;
3404 nScheme = lpUrlComponents->nScheme;
3406 if (nScheme == INTERNET_SCHEME_DEFAULT)
3407 nScheme = INTERNET_SCHEME_HTTP;
3408 scheme = INTERNET_GetSchemeString(nScheme);
3409 *lpdwUrlLength += strlenW(scheme);
3412 (*lpdwUrlLength)++; /* ':' */
3413 if (!scheme_is_opaque(nScheme) || lpUrlComponents->lpszHostName)
3414 *lpdwUrlLength += strlen("//");
3416 if (lpUrlComponents->lpszUserName)
3418 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, UserName);
3419 *lpdwUrlLength += strlen("@");
3421 else
3423 if (lpUrlComponents->lpszPassword)
3425 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
3426 return FALSE;
3430 if (lpUrlComponents->lpszPassword)
3432 *lpdwUrlLength += strlen(":");
3433 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, Password);
3436 if (lpUrlComponents->lpszHostName)
3438 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, HostName);
3440 if (!url_uses_default_port(nScheme, lpUrlComponents->nPort))
3442 char szPort[MAX_WORD_DIGITS+1];
3444 sprintf(szPort, "%d", lpUrlComponents->nPort);
3445 *lpdwUrlLength += strlen(szPort);
3446 *lpdwUrlLength += strlen(":");
3449 if (lpUrlComponents->lpszUrlPath && *lpUrlComponents->lpszUrlPath != '/')
3450 (*lpdwUrlLength)++; /* '/' */
3453 if (lpUrlComponents->lpszUrlPath)
3454 *lpdwUrlLength += URL_GET_COMP_LENGTH(lpUrlComponents, UrlPath);
3456 return TRUE;
3459 static void convert_urlcomp_atow(LPURL_COMPONENTSA lpUrlComponents, LPURL_COMPONENTSW urlCompW)
3461 INT len;
3463 ZeroMemory(urlCompW, sizeof(URL_COMPONENTSW));
3465 urlCompW->dwStructSize = sizeof(URL_COMPONENTSW);
3466 urlCompW->dwSchemeLength = lpUrlComponents->dwSchemeLength;
3467 urlCompW->nScheme = lpUrlComponents->nScheme;
3468 urlCompW->dwHostNameLength = lpUrlComponents->dwHostNameLength;
3469 urlCompW->nPort = lpUrlComponents->nPort;
3470 urlCompW->dwUserNameLength = lpUrlComponents->dwUserNameLength;
3471 urlCompW->dwPasswordLength = lpUrlComponents->dwPasswordLength;
3472 urlCompW->dwUrlPathLength = lpUrlComponents->dwUrlPathLength;
3473 urlCompW->dwExtraInfoLength = lpUrlComponents->dwExtraInfoLength;
3475 if (lpUrlComponents->lpszScheme)
3477 len = URL_GET_COMP_LENGTHA(lpUrlComponents, Scheme) + 1;
3478 urlCompW->lpszScheme = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3479 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszScheme,
3480 -1, urlCompW->lpszScheme, len);
3483 if (lpUrlComponents->lpszHostName)
3485 len = URL_GET_COMP_LENGTHA(lpUrlComponents, HostName) + 1;
3486 urlCompW->lpszHostName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3487 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszHostName,
3488 -1, urlCompW->lpszHostName, len);
3491 if (lpUrlComponents->lpszUserName)
3493 len = URL_GET_COMP_LENGTHA(lpUrlComponents, UserName) + 1;
3494 urlCompW->lpszUserName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3495 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszUserName,
3496 -1, urlCompW->lpszUserName, len);
3499 if (lpUrlComponents->lpszPassword)
3501 len = URL_GET_COMP_LENGTHA(lpUrlComponents, Password) + 1;
3502 urlCompW->lpszPassword = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3503 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszPassword,
3504 -1, urlCompW->lpszPassword, len);
3507 if (lpUrlComponents->lpszUrlPath)
3509 len = URL_GET_COMP_LENGTHA(lpUrlComponents, UrlPath) + 1;
3510 urlCompW->lpszUrlPath = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3511 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszUrlPath,
3512 -1, urlCompW->lpszUrlPath, len);
3515 if (lpUrlComponents->lpszExtraInfo)
3517 len = URL_GET_COMP_LENGTHA(lpUrlComponents, ExtraInfo) + 1;
3518 urlCompW->lpszExtraInfo = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3519 MultiByteToWideChar(CP_ACP, 0, lpUrlComponents->lpszExtraInfo,
3520 -1, urlCompW->lpszExtraInfo, len);
3524 /***********************************************************************
3525 * InternetCreateUrlA (WININET.@)
3527 * See InternetCreateUrlW.
3529 BOOL WINAPI InternetCreateUrlA(LPURL_COMPONENTSA lpUrlComponents, DWORD dwFlags,
3530 LPSTR lpszUrl, LPDWORD lpdwUrlLength)
3532 BOOL ret;
3533 LPWSTR urlW = NULL;
3534 URL_COMPONENTSW urlCompW;
3536 TRACE("(%p,%d,%p,%p)\n", lpUrlComponents, dwFlags, lpszUrl, lpdwUrlLength);
3538 if (!lpUrlComponents || lpUrlComponents->dwStructSize != sizeof(URL_COMPONENTSW) || !lpdwUrlLength)
3540 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
3541 return FALSE;
3544 convert_urlcomp_atow(lpUrlComponents, &urlCompW);
3546 if (lpszUrl)
3547 urlW = HeapAlloc(GetProcessHeap(), 0, *lpdwUrlLength * sizeof(WCHAR));
3549 ret = InternetCreateUrlW(&urlCompW, dwFlags, urlW, lpdwUrlLength);
3551 if (!ret && (GetLastError() == ERROR_INSUFFICIENT_BUFFER))
3552 *lpdwUrlLength /= sizeof(WCHAR);
3554 /* on success, lpdwUrlLength points to the size of urlW in WCHARS
3555 * minus one, so add one to leave room for NULL terminator
3557 if (ret)
3558 WideCharToMultiByte(CP_ACP, 0, urlW, -1, lpszUrl, *lpdwUrlLength + 1, NULL, NULL);
3560 HeapFree(GetProcessHeap(), 0, urlCompW.lpszScheme);
3561 HeapFree(GetProcessHeap(), 0, urlCompW.lpszHostName);
3562 HeapFree(GetProcessHeap(), 0, urlCompW.lpszUserName);
3563 HeapFree(GetProcessHeap(), 0, urlCompW.lpszPassword);
3564 HeapFree(GetProcessHeap(), 0, urlCompW.lpszUrlPath);
3565 HeapFree(GetProcessHeap(), 0, urlCompW.lpszExtraInfo);
3566 HeapFree(GetProcessHeap(), 0, urlW);
3568 return ret;
3571 /***********************************************************************
3572 * InternetCreateUrlW (WININET.@)
3574 * Creates a URL from its component parts.
3576 * PARAMS
3577 * lpUrlComponents [I] URL Components.
3578 * dwFlags [I] Flags. See notes.
3579 * lpszUrl [I] Buffer in which to store the created URL.
3580 * lpdwUrlLength [I/O] On input, the length of the buffer pointed to by
3581 * lpszUrl in characters. On output, the number of bytes
3582 * required to store the URL including terminator.
3584 * NOTES
3586 * The dwFlags parameter can be zero or more of the following:
3587 *|ICU_ESCAPE - Generates escape sequences for unsafe characters in the path and extra info of the URL.
3589 * RETURNS
3590 * TRUE on success
3591 * FALSE on failure
3594 BOOL WINAPI InternetCreateUrlW(LPURL_COMPONENTSW lpUrlComponents, DWORD dwFlags,
3595 LPWSTR lpszUrl, LPDWORD lpdwUrlLength)
3597 DWORD dwLen;
3598 INTERNET_SCHEME nScheme;
3600 static const WCHAR slashSlashW[] = {'/','/'};
3601 static const WCHAR percentD[] = {'%','d',0};
3603 TRACE("(%p,%d,%p,%p)\n", lpUrlComponents, dwFlags, lpszUrl, lpdwUrlLength);
3605 if (!lpUrlComponents || lpUrlComponents->dwStructSize != sizeof(URL_COMPONENTSW) || !lpdwUrlLength)
3607 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
3608 return FALSE;
3611 if (!calc_url_length(lpUrlComponents, &dwLen))
3612 return FALSE;
3614 if (!lpszUrl || *lpdwUrlLength < dwLen)
3616 *lpdwUrlLength = (dwLen + 1) * sizeof(WCHAR);
3617 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
3618 return FALSE;
3621 *lpdwUrlLength = dwLen;
3622 lpszUrl[0] = 0x00;
3624 dwLen = 0;
3626 if (lpUrlComponents->lpszScheme)
3628 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, Scheme);
3629 memcpy(lpszUrl, lpUrlComponents->lpszScheme, dwLen * sizeof(WCHAR));
3630 lpszUrl += dwLen;
3632 nScheme = GetInternetSchemeW(lpUrlComponents->lpszScheme, dwLen);
3634 else
3636 LPCWSTR scheme;
3637 nScheme = lpUrlComponents->nScheme;
3639 if (nScheme == INTERNET_SCHEME_DEFAULT)
3640 nScheme = INTERNET_SCHEME_HTTP;
3642 scheme = INTERNET_GetSchemeString(nScheme);
3643 dwLen = strlenW(scheme);
3644 memcpy(lpszUrl, scheme, dwLen * sizeof(WCHAR));
3645 lpszUrl += dwLen;
3648 /* all schemes are followed by at least a colon */
3649 *lpszUrl = ':';
3650 lpszUrl++;
3652 if (!scheme_is_opaque(nScheme) || lpUrlComponents->lpszHostName)
3654 memcpy(lpszUrl, slashSlashW, sizeof(slashSlashW));
3655 lpszUrl += sizeof(slashSlashW)/sizeof(slashSlashW[0]);
3658 if (lpUrlComponents->lpszUserName)
3660 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, UserName);
3661 memcpy(lpszUrl, lpUrlComponents->lpszUserName, dwLen * sizeof(WCHAR));
3662 lpszUrl += dwLen;
3664 if (lpUrlComponents->lpszPassword)
3666 *lpszUrl = ':';
3667 lpszUrl++;
3669 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, Password);
3670 memcpy(lpszUrl, lpUrlComponents->lpszPassword, dwLen * sizeof(WCHAR));
3671 lpszUrl += dwLen;
3674 *lpszUrl = '@';
3675 lpszUrl++;
3678 if (lpUrlComponents->lpszHostName)
3680 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, HostName);
3681 memcpy(lpszUrl, lpUrlComponents->lpszHostName, dwLen * sizeof(WCHAR));
3682 lpszUrl += dwLen;
3684 if (!url_uses_default_port(nScheme, lpUrlComponents->nPort))
3686 WCHAR szPort[MAX_WORD_DIGITS+1];
3688 sprintfW(szPort, percentD, lpUrlComponents->nPort);
3689 *lpszUrl = ':';
3690 lpszUrl++;
3691 dwLen = strlenW(szPort);
3692 memcpy(lpszUrl, szPort, dwLen * sizeof(WCHAR));
3693 lpszUrl += dwLen;
3696 /* add slash between hostname and path if necessary */
3697 if (lpUrlComponents->lpszUrlPath && *lpUrlComponents->lpszUrlPath != '/')
3699 *lpszUrl = '/';
3700 lpszUrl++;
3705 if (lpUrlComponents->lpszUrlPath)
3707 dwLen = URL_GET_COMP_LENGTH(lpUrlComponents, UrlPath);
3708 memcpy(lpszUrl, lpUrlComponents->lpszUrlPath, dwLen * sizeof(WCHAR));
3709 lpszUrl += dwLen;
3712 *lpszUrl = '\0';
3714 return TRUE;
3717 /***********************************************************************
3718 * InternetConfirmZoneCrossingA (WININET.@)
3721 DWORD WINAPI InternetConfirmZoneCrossingA( HWND hWnd, LPSTR szUrlPrev, LPSTR szUrlNew, BOOL bPost )
3723 FIXME("(%p, %s, %s, %x) stub\n", hWnd, debugstr_a(szUrlPrev), debugstr_a(szUrlNew), bPost);
3724 return ERROR_SUCCESS;
3727 /***********************************************************************
3728 * InternetConfirmZoneCrossingW (WININET.@)
3731 DWORD WINAPI InternetConfirmZoneCrossingW( HWND hWnd, LPWSTR szUrlPrev, LPWSTR szUrlNew, BOOL bPost )
3733 FIXME("(%p, %s, %s, %x) stub\n", hWnd, debugstr_w(szUrlPrev), debugstr_w(szUrlNew), bPost);
3734 return ERROR_SUCCESS;
3737 DWORD WINAPI InternetDialA( HWND hwndParent, LPSTR lpszConnectoid, DWORD dwFlags,
3738 DWORD_PTR* lpdwConnection, DWORD dwReserved )
3740 FIXME("(%p, %p, 0x%08x, %p, 0x%08x) stub\n", hwndParent, lpszConnectoid, dwFlags,
3741 lpdwConnection, dwReserved);
3742 return ERROR_SUCCESS;
3745 DWORD WINAPI InternetDialW( HWND hwndParent, LPWSTR lpszConnectoid, DWORD dwFlags,
3746 DWORD_PTR* lpdwConnection, DWORD dwReserved )
3748 FIXME("(%p, %p, 0x%08x, %p, 0x%08x) stub\n", hwndParent, lpszConnectoid, dwFlags,
3749 lpdwConnection, dwReserved);
3750 return ERROR_SUCCESS;
3753 BOOL WINAPI InternetGoOnlineA( LPSTR lpszURL, HWND hwndParent, DWORD dwReserved )
3755 FIXME("(%s, %p, 0x%08x) stub\n", debugstr_a(lpszURL), hwndParent, dwReserved);
3756 return TRUE;
3759 BOOL WINAPI InternetGoOnlineW( LPWSTR lpszURL, HWND hwndParent, DWORD dwReserved )
3761 FIXME("(%s, %p, 0x%08x) stub\n", debugstr_w(lpszURL), hwndParent, dwReserved);
3762 return TRUE;
3765 DWORD WINAPI InternetHangUp( DWORD_PTR dwConnection, DWORD dwReserved )
3767 FIXME("(0x%08lx, 0x%08x) stub\n", dwConnection, dwReserved);
3768 return ERROR_SUCCESS;
3771 BOOL WINAPI CreateMD5SSOHash( PWSTR pszChallengeInfo, PWSTR pwszRealm, PWSTR pwszTarget,
3772 PBYTE pbHexHash )
3774 FIXME("(%s, %s, %s, %p) stub\n", debugstr_w(pszChallengeInfo), debugstr_w(pwszRealm),
3775 debugstr_w(pwszTarget), pbHexHash);
3776 return FALSE;
3779 BOOL WINAPI ResumeSuspendedDownload( HINTERNET hInternet, DWORD dwError )
3781 FIXME("(%p, 0x%08x) stub\n", hInternet, dwError);
3782 return FALSE;
3785 BOOL WINAPI InternetQueryFortezzaStatus(DWORD *a, DWORD_PTR b)
3787 FIXME("(%p, %08lx) stub\n", a, b);
3788 return 0;