2 * Copyright 2014 Hans Leidekker for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "wine/port.h"
23 #include <sys/types.h>
24 #ifdef HAVE_SYS_SOCKET_H
25 # include <sys/socket.h>
27 #ifdef HAVE_NETINET_IN_H
28 # include <netinet/in.h>
33 #if defined(__MINGW32__) || defined (_MSC_VER)
34 # include <ws2tcpip.h>
36 # define closesocket close
37 # define ioctlsocket ioctl
53 #include "wine/debug.h"
54 #include "wine/unicode.h"
56 static HINSTANCE instance
;
58 WINE_DEFAULT_DEBUG_CHANNEL(jsproxy
);
60 static CRITICAL_SECTION cs_jsproxy
;
61 static CRITICAL_SECTION_DEBUG critsect_debug
=
64 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
65 0, 0, { (DWORD_PTR
)(__FILE__
": cs_jsproxy") }
67 static CRITICAL_SECTION cs_jsproxy
= { &critsect_debug
, -1, 0, 0, 0, 0 };
69 static const WCHAR global_funcsW
[] = {'g','l','o','b','a','l','_','f','u','n','c','s',0};
70 static const WCHAR dns_resolveW
[] = {'d','n','s','_','r','e','s','o','l','v','e',0};
72 /******************************************************************
75 BOOL WINAPI
DllMain( HINSTANCE hinst
, DWORD reason
, LPVOID reserved
)
79 case DLL_PROCESS_ATTACH
:
81 DisableThreadLibraryCalls( hinst
);
84 case DLL_PROCESS_DETACH
:
90 static inline void *heap_alloc( SIZE_T size
)
92 return HeapAlloc( GetProcessHeap(), 0, size
);
95 static inline BOOL
heap_free( LPVOID mem
)
97 return HeapFree( GetProcessHeap(), 0, mem
);
100 static inline WCHAR
*strdupAW( const char *src
, DWORD len
)
105 int dst_len
= MultiByteToWideChar( CP_ACP
, 0, src
, len
, NULL
, 0 );
106 if ((dst
= heap_alloc( (dst_len
+ 1) * sizeof(WCHAR
) )))
108 len
= MultiByteToWideChar( CP_ACP
, 0, src
, len
, dst
, dst_len
);
115 static inline char *strdupWA( const WCHAR
*src
)
120 int len
= WideCharToMultiByte( CP_ACP
, 0, src
, -1, NULL
, 0, NULL
, NULL
);
121 if ((dst
= heap_alloc( len
))) WideCharToMultiByte( CP_ACP
, 0, src
, -1, dst
, len
, NULL
, NULL
);
126 static struct pac_script
130 static struct pac_script
*global_script
= &pac_script
;
132 /******************************************************************
133 * InternetDeInitializeAutoProxyDll (jsproxy.@)
135 BOOL WINAPI
InternetDeInitializeAutoProxyDll( LPSTR mime
, DWORD reserved
)
137 TRACE( "%s, %u\n", debugstr_a(mime
), reserved
);
139 EnterCriticalSection( &cs_jsproxy
);
141 heap_free( global_script
->text
);
142 global_script
->text
= NULL
;
144 LeaveCriticalSection( &cs_jsproxy
);
148 static WCHAR
*load_script( const char *filename
)
151 DWORD size
, bytes_read
;
154 WCHAR
*script
= NULL
;
156 handle
= CreateFileA( filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0 );
157 if (handle
== INVALID_HANDLE_VALUE
) return NULL
;
159 size
= GetFileSize( handle
, NULL
);
160 if (!(buffer
= heap_alloc( size
))) goto done
;
161 if (!ReadFile( handle
, buffer
, size
, &bytes_read
, NULL
) || bytes_read
!= size
) goto done
;
163 len
= MultiByteToWideChar( CP_ACP
, 0, buffer
, size
, NULL
, 0 );
164 if (!(script
= heap_alloc( (len
+ 1) * sizeof(WCHAR
) ))) goto done
;
165 MultiByteToWideChar( CP_ACP
, 0, buffer
, size
, script
, len
);
169 CloseHandle( handle
);
174 /******************************************************************
175 * InternetInitializeAutoProxyDll (jsproxy.@)
177 BOOL WINAPI
JSPROXY_InternetInitializeAutoProxyDll( DWORD version
, LPSTR tmpfile
, LPSTR mime
,
178 AutoProxyHelperFunctions
*callbacks
,
179 LPAUTO_PROXY_SCRIPT_BUFFER buffer
)
183 TRACE( "%u, %s, %s, %p, %p\n", version
, debugstr_a(tmpfile
), debugstr_a(mime
), callbacks
, buffer
);
185 if (callbacks
) FIXME( "callbacks not supported\n" );
187 EnterCriticalSection( &cs_jsproxy
);
189 if (global_script
->text
)
191 LeaveCriticalSection( &cs_jsproxy
);
194 if (buffer
&& buffer
->dwStructSize
== sizeof(*buffer
) && buffer
->lpszScriptBuffer
&&
195 (global_script
->text
= strdupAW( buffer
->lpszScriptBuffer
, buffer
->dwScriptBufferSize
))) ret
= TRUE
;
196 else if ((global_script
->text
= load_script( tmpfile
))) ret
= TRUE
;
198 LeaveCriticalSection( &cs_jsproxy
);
202 static HRESULT WINAPI
dispex_QueryInterface(
203 IDispatchEx
*iface
, REFIID riid
, void **ppv
)
207 if (IsEqualGUID( riid
, &IID_IUnknown
) ||
208 IsEqualGUID( riid
, &IID_IDispatch
) ||
209 IsEqualGUID( riid
, &IID_IDispatchEx
))
212 return E_NOINTERFACE
;
217 static ULONG WINAPI
dispex_AddRef(
223 static ULONG WINAPI
dispex_Release(
229 static HRESULT WINAPI
dispex_GetTypeInfoCount(
230 IDispatchEx
*iface
, UINT
*info
)
235 static HRESULT WINAPI
dispex_GetTypeInfo(
236 IDispatchEx
*iface
, UINT info
, LCID lcid
, ITypeInfo
**type_info
)
241 static HRESULT WINAPI
dispex_GetIDsOfNames(
242 IDispatchEx
*iface
, REFIID riid
, LPOLESTR
*names
, UINT count
, LCID lcid
, DISPID
*id
)
247 static HRESULT WINAPI
dispex_Invoke(
248 IDispatchEx
*iface
, DISPID member
, REFIID riid
, LCID lcid
, WORD flags
,
249 DISPPARAMS
*params
, VARIANT
*result
, EXCEPINFO
*excep
, UINT
*err
)
254 static HRESULT WINAPI
dispex_DeleteMemberByName(
255 IDispatchEx
*iface
, BSTR name
, DWORD flags
)
260 static HRESULT WINAPI
dispex_DeleteMemberByDispID(
261 IDispatchEx
*iface
, DISPID id
)
266 static HRESULT WINAPI
dispex_GetMemberProperties(
267 IDispatchEx
*iface
, DISPID id
, DWORD flags_fetch
, DWORD
*flags
)
272 static HRESULT WINAPI
dispex_GetMemberName(
273 IDispatchEx
*iface
, DISPID id
, BSTR
*name
)
278 static HRESULT WINAPI
dispex_GetNextDispID(
279 IDispatchEx
*iface
, DWORD flags
, DISPID id
, DISPID
*next
)
284 static HRESULT WINAPI
dispex_GetNameSpaceParent(
285 IDispatchEx
*iface
, IUnknown
**unk
)
290 #define DISPID_GLOBAL_DNSRESOLVE 0x1000
292 static HRESULT WINAPI
dispex_GetDispID(
293 IDispatchEx
*iface
, BSTR name
, DWORD flags
, DISPID
*id
)
295 if (!strcmpW( name
, dns_resolveW
))
297 *id
= DISPID_GLOBAL_DNSRESOLVE
;
300 return DISP_E_UNKNOWNNAME
;
303 static char *get_computer_name( COMPUTER_NAME_FORMAT format
)
308 GetComputerNameExA( format
, NULL
, &size
);
309 if (GetLastError() != ERROR_MORE_DATA
) return NULL
;
310 if (!(ret
= heap_alloc( size
))) return NULL
;
311 if (!GetComputerNameExA( format
, ret
, &size
))
319 static void printf_addr( const WCHAR
*fmt
, WCHAR
*buf
, struct sockaddr_in
*addr
)
322 (unsigned int)(ntohl( addr
->sin_addr
.s_addr
) >> 24 & 0xff),
323 (unsigned int)(ntohl( addr
->sin_addr
.s_addr
) >> 16 & 0xff),
324 (unsigned int)(ntohl( addr
->sin_addr
.s_addr
) >> 8 & 0xff),
325 (unsigned int)(ntohl( addr
->sin_addr
.s_addr
) & 0xff) );
328 static HRESULT
dns_resolve( const WCHAR
*hostname
, VARIANT
*result
)
330 #ifdef HAVE_GETADDRINFO
331 static const WCHAR fmtW
[] = {'%','u','.','%','u','.','%','u','.','%','u',0};
333 struct addrinfo
*ai
, *elem
;
338 hostnameA
= strdupWA( hostname
);
340 hostnameA
= get_computer_name( ComputerNamePhysicalDnsFullyQualified
);
342 if (!hostnameA
) return E_OUTOFMEMORY
;
343 res
= getaddrinfo( hostnameA
, NULL
, NULL
, &ai
);
344 heap_free( hostnameA
);
345 if (res
) return S_FALSE
;
348 while (elem
&& elem
->ai_family
!= AF_INET
) elem
= elem
->ai_next
;
354 printf_addr( fmtW
, addr
, (struct sockaddr_in
*)elem
->ai_addr
);
356 V_VT( result
) = VT_BSTR
;
357 V_BSTR( result
) = SysAllocString( addr
);
360 FIXME("getaddrinfo not found at build time\n");
365 static HRESULT WINAPI
dispex_InvokeEx(
366 IDispatchEx
*iface
, DISPID id
, LCID lcid
, WORD flags
, DISPPARAMS
*params
,
367 VARIANT
*result
, EXCEPINFO
*exep
, IServiceProvider
*caller
)
369 if (id
== DISPID_GLOBAL_DNSRESOLVE
)
371 if (params
->cArgs
!= 1) return DISP_E_BADPARAMCOUNT
;
372 if (V_VT(¶ms
->rgvarg
[0]) != VT_BSTR
) return DISP_E_BADVARTYPE
;
373 return dns_resolve( V_BSTR(¶ms
->rgvarg
[0]), result
);
375 return DISP_E_MEMBERNOTFOUND
;
378 static const IDispatchExVtbl dispex_vtbl
=
380 dispex_QueryInterface
,
383 dispex_GetTypeInfoCount
,
385 dispex_GetIDsOfNames
,
389 dispex_DeleteMemberByName
,
390 dispex_DeleteMemberByDispID
,
391 dispex_GetMemberProperties
,
392 dispex_GetMemberName
,
393 dispex_GetNextDispID
,
394 dispex_GetNameSpaceParent
397 static IDispatchEx global_dispex
= { &dispex_vtbl
};
399 static HRESULT WINAPI
site_QueryInterface(
400 IActiveScriptSite
*iface
, REFIID riid
, void **ppv
)
404 if (IsEqualGUID( &IID_IUnknown
, riid
))
406 else if (IsEqualGUID( &IID_IActiveScriptSite
, riid
))
409 return E_NOINTERFACE
;
411 IUnknown_AddRef( (IUnknown
*)*ppv
);
415 static ULONG WINAPI
site_AddRef(
416 IActiveScriptSite
*iface
)
421 static ULONG WINAPI
site_Release(
422 IActiveScriptSite
*iface
)
427 static HRESULT WINAPI
site_GetLCID(
428 IActiveScriptSite
*iface
, LCID
*lcid
)
433 static HRESULT WINAPI
site_GetItemInfo(
434 IActiveScriptSite
*iface
, LPCOLESTR name
, DWORD mask
,
435 IUnknown
**item
, ITypeInfo
**type_info
)
437 if (!strcmpW( name
, global_funcsW
) && mask
== SCRIPTINFO_IUNKNOWN
)
439 *item
= (IUnknown
*)&global_dispex
;
445 static HRESULT WINAPI
site_GetDocVersionString(
446 IActiveScriptSite
*iface
, BSTR
*version
)
451 static HRESULT WINAPI
site_OnScriptTerminate(
452 IActiveScriptSite
*iface
, const VARIANT
*result
, const EXCEPINFO
*info
)
457 static HRESULT WINAPI
site_OnStateChange(
458 IActiveScriptSite
*iface
, SCRIPTSTATE state
)
463 static HRESULT WINAPI
site_OnScriptError(
464 IActiveScriptSite
*iface
, IActiveScriptError
*error
)
469 static HRESULT WINAPI
site_OnEnterScript(
470 IActiveScriptSite
*iface
)
475 static HRESULT WINAPI
site_OnLeaveScript(
476 IActiveScriptSite
*iface
)
481 static const IActiveScriptSiteVtbl site_vtbl
=
488 site_GetDocVersionString
,
489 site_OnScriptTerminate
,
496 static IActiveScriptSite script_site
= { &site_vtbl
};
498 static BSTR
include_pac_utils( const WCHAR
*script
)
500 static const WCHAR pacjsW
[] = {'p','a','c','.','j','s',0};
501 HMODULE hmod
= GetModuleHandleA( "jsproxy.dll" );
508 if (!(rsrc
= FindResourceW( hmod
, pacjsW
, (LPCWSTR
)40 ))) return NULL
;
509 size
= SizeofResource( hmod
, rsrc
);
510 data
= LoadResource( hmod
, rsrc
);
512 len
= MultiByteToWideChar( CP_ACP
, 0, data
, size
, NULL
, 0 );
513 if (!(ret
= SysAllocStringLen( NULL
, len
+ strlenW( script
) + 1 ))) return NULL
;
514 MultiByteToWideChar( CP_ACP
, 0, data
, size
, ret
, len
);
515 strcpyW( ret
+ len
, script
);
520 #define IActiveScriptParse_Release IActiveScriptParse64_Release
521 #define IActiveScriptParse_InitNew IActiveScriptParse64_InitNew
522 #define IActiveScriptParse_ParseScriptText IActiveScriptParse64_ParseScriptText
524 #define IActiveScriptParse_Release IActiveScriptParse32_Release
525 #define IActiveScriptParse_InitNew IActiveScriptParse32_InitNew
526 #define IActiveScriptParse_ParseScriptText IActiveScriptParse32_ParseScriptText
529 static BOOL
run_script( const WCHAR
*script
, const WCHAR
*url
, const WCHAR
*hostname
, char **result_str
, DWORD
*result_len
)
531 static const WCHAR jscriptW
[] = {'J','S','c','r','i','p','t',0};
532 static const WCHAR findproxyW
[] = {'F','i','n','d','P','r','o','x','y','F','o','r','U','R','L',0};
533 IActiveScriptParse
*parser
= NULL
;
534 IActiveScript
*engine
= NULL
;
535 IDispatch
*dispatch
= NULL
;
539 BSTR func
= NULL
, full_script
= NULL
;
540 VARIANT args
[2], retval
;
544 init
= CoInitialize( NULL
);
545 hr
= CLSIDFromProgID( jscriptW
, &clsid
);
546 if (hr
!= S_OK
) goto done
;
548 hr
= CoCreateInstance( &clsid
, NULL
, CLSCTX_INPROC_SERVER
|CLSCTX_INPROC_HANDLER
,
549 &IID_IActiveScript
, (void **)&engine
);
550 if (hr
!= S_OK
) goto done
;
552 hr
= IActiveScript_QueryInterface( engine
, &IID_IActiveScriptParse
, (void **)&parser
);
553 if (hr
!= S_OK
) goto done
;
555 hr
= IActiveScriptParse_InitNew( parser
);
556 if (hr
!= S_OK
) goto done
;
558 hr
= IActiveScript_SetScriptSite( engine
, &script_site
);
559 if (hr
!= S_OK
) goto done
;
561 hr
= IActiveScript_AddNamedItem( engine
, global_funcsW
, SCRIPTITEM_GLOBALMEMBERS
);
562 if (hr
!= S_OK
) goto done
;
564 if (!(full_script
= include_pac_utils( script
))) goto done
;
566 hr
= IActiveScriptParse_ParseScriptText( parser
, full_script
, NULL
, NULL
, NULL
, 0, 0, 0, NULL
, NULL
);
567 if (hr
!= S_OK
) goto done
;
569 hr
= IActiveScript_SetScriptState( engine
, SCRIPTSTATE_STARTED
);
570 if (hr
!= S_OK
) goto done
;
572 hr
= IActiveScript_GetScriptDispatch( engine
, NULL
, &dispatch
);
573 if (hr
!= S_OK
) goto done
;
575 if (!(func
= SysAllocString( findproxyW
))) goto done
;
576 hr
= IDispatch_GetIDsOfNames( dispatch
, &IID_NULL
, &func
, 1, LOCALE_SYSTEM_DEFAULT
, &dispid
);
577 if (hr
!= S_OK
) goto done
;
579 V_VT( &args
[0] ) = VT_BSTR
;
580 V_BSTR( &args
[0] ) = SysAllocString( hostname
);
581 V_VT( &args
[1] ) = VT_BSTR
;
582 V_BSTR( &args
[1] ) = SysAllocString( url
);
584 params
.rgvarg
= args
;
585 params
.rgdispidNamedArgs
= NULL
;
587 params
.cNamedArgs
= 0;
588 hr
= IDispatch_Invoke( dispatch
, dispid
, &IID_NULL
, LOCALE_SYSTEM_DEFAULT
, DISPATCH_METHOD
,
589 ¶ms
, &retval
, NULL
, NULL
);
590 VariantClear( &args
[0] );
591 VariantClear( &args
[1] );
594 WARN("script failed 0x%08x\n", hr
);
597 if ((*result_str
= strdupWA( V_BSTR( &retval
) )))
599 TRACE( "result: %s\n", debugstr_a(*result_str
) );
600 *result_len
= strlen( *result_str
) + 1;
603 VariantClear( &retval
);
606 SysFreeString( full_script
);
607 SysFreeString( func
);
608 if (dispatch
) IDispatch_Release( dispatch
);
609 if (parser
) IActiveScriptParse_Release( parser
);
610 if (engine
) IActiveScript_Release( engine
);
611 if (SUCCEEDED( init
)) CoUninitialize();
615 /******************************************************************
616 * InternetGetProxyInfo (jsproxy.@)
618 BOOL WINAPI
InternetGetProxyInfo( LPCSTR url
, DWORD len_url
, LPCSTR hostname
, DWORD len_hostname
, LPSTR
*proxy
,
621 WCHAR
*urlW
= NULL
, *hostnameW
= NULL
;
624 TRACE( "%s, %u, %s, %u, %p, %p\n", url
, len_url
, hostname
, len_hostname
, proxy
, len_proxy
);
626 EnterCriticalSection( &cs_jsproxy
);
628 if (!global_script
->text
) goto done
;
629 if (!(urlW
= strdupAW( url
, len_url
))) goto done
;
630 if (hostname
&& !(hostnameW
= strdupAW( hostname
, len_hostname
))) goto done
;
632 TRACE( "%s\n", debugstr_w(global_script
->text
) );
633 ret
= run_script( global_script
->text
, urlW
, hostnameW
, proxy
, len_proxy
);
636 heap_free( hostnameW
);
638 LeaveCriticalSection( &cs_jsproxy
);