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 <sys/types.h>
33 #include "wine/debug.h"
34 #include "wine/heap.h"
36 static HINSTANCE instance
;
38 WINE_DEFAULT_DEBUG_CHANNEL(jsproxy
);
40 static CRITICAL_SECTION cs_jsproxy
;
41 static CRITICAL_SECTION_DEBUG critsect_debug
=
44 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
45 0, 0, { (DWORD_PTR
)(__FILE__
": cs_jsproxy") }
47 static CRITICAL_SECTION cs_jsproxy
= { &critsect_debug
, -1, 0, 0, 0, 0 };
49 /******************************************************************
52 BOOL WINAPI
DllMain( HINSTANCE hinst
, DWORD reason
, LPVOID reserved
)
56 case DLL_PROCESS_ATTACH
:
58 DisableThreadLibraryCalls( hinst
);
61 case DLL_PROCESS_DETACH
:
67 static inline WCHAR
*strdupAW( const char *src
, int len
)
72 int dst_len
= MultiByteToWideChar( CP_ACP
, 0, src
, len
, NULL
, 0 );
73 if ((dst
= heap_alloc( (dst_len
+ 1) * sizeof(WCHAR
) )))
75 len
= MultiByteToWideChar( CP_ACP
, 0, src
, len
, dst
, dst_len
);
82 static inline char *strdupWA( const WCHAR
*src
)
87 int len
= WideCharToMultiByte( CP_ACP
, 0, src
, -1, NULL
, 0, NULL
, NULL
);
88 if ((dst
= heap_alloc( len
))) WideCharToMultiByte( CP_ACP
, 0, src
, -1, dst
, len
, NULL
, NULL
);
93 static struct pac_script
97 static struct pac_script
*global_script
= &pac_script
;
99 /******************************************************************
100 * InternetDeInitializeAutoProxyDll (jsproxy.@)
102 BOOL WINAPI
InternetDeInitializeAutoProxyDll( LPSTR mime
, DWORD reserved
)
104 TRACE( "%s, %u\n", debugstr_a(mime
), reserved
);
106 EnterCriticalSection( &cs_jsproxy
);
108 heap_free( global_script
->text
);
109 global_script
->text
= NULL
;
111 LeaveCriticalSection( &cs_jsproxy
);
115 static WCHAR
*load_script( const char *filename
)
118 DWORD size
, bytes_read
;
121 WCHAR
*script
= NULL
;
123 handle
= CreateFileA( filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0 );
124 if (handle
== INVALID_HANDLE_VALUE
) return NULL
;
126 size
= GetFileSize( handle
, NULL
);
127 if (!(buffer
= heap_alloc( size
))) goto done
;
128 if (!ReadFile( handle
, buffer
, size
, &bytes_read
, NULL
) || bytes_read
!= size
) goto done
;
130 len
= MultiByteToWideChar( CP_ACP
, 0, buffer
, size
, NULL
, 0 );
131 if (!(script
= heap_alloc( (len
+ 1) * sizeof(WCHAR
) ))) goto done
;
132 MultiByteToWideChar( CP_ACP
, 0, buffer
, size
, script
, len
);
136 CloseHandle( handle
);
141 /******************************************************************
142 * InternetInitializeAutoProxyDll (jsproxy.@)
144 BOOL WINAPI
JSPROXY_InternetInitializeAutoProxyDll( DWORD version
, LPSTR tmpfile
, LPSTR mime
,
145 AutoProxyHelperFunctions
*callbacks
,
146 LPAUTO_PROXY_SCRIPT_BUFFER buffer
)
150 TRACE( "%u, %s, %s, %p, %p\n", version
, debugstr_a(tmpfile
), debugstr_a(mime
), callbacks
, buffer
);
152 if (callbacks
) FIXME( "callbacks not supported\n" );
154 EnterCriticalSection( &cs_jsproxy
);
156 if (buffer
&& buffer
->dwStructSize
== sizeof(*buffer
) && buffer
->lpszScriptBuffer
)
158 if (!buffer
->dwScriptBufferSize
)
160 SetLastError( ERROR_INVALID_PARAMETER
);
161 LeaveCriticalSection( &cs_jsproxy
);
164 heap_free( global_script
->text
);
165 if ((global_script
->text
= strdupAW( buffer
->lpszScriptBuffer
,
166 buffer
->dwScriptBufferSize
))) ret
= TRUE
;
170 heap_free( global_script
->text
);
171 if ((global_script
->text
= load_script( tmpfile
))) ret
= TRUE
;
174 LeaveCriticalSection( &cs_jsproxy
);
178 static HRESULT WINAPI
dispex_QueryInterface(
179 IDispatchEx
*iface
, REFIID riid
, void **ppv
)
183 if (IsEqualGUID( riid
, &IID_IUnknown
) ||
184 IsEqualGUID( riid
, &IID_IDispatch
) ||
185 IsEqualGUID( riid
, &IID_IDispatchEx
))
188 return E_NOINTERFACE
;
193 static ULONG WINAPI
dispex_AddRef(
199 static ULONG WINAPI
dispex_Release(
205 static HRESULT WINAPI
dispex_GetTypeInfoCount(
206 IDispatchEx
*iface
, UINT
*info
)
211 static HRESULT WINAPI
dispex_GetTypeInfo(
212 IDispatchEx
*iface
, UINT info
, LCID lcid
, ITypeInfo
**type_info
)
217 static HRESULT WINAPI
dispex_GetIDsOfNames(
218 IDispatchEx
*iface
, REFIID riid
, LPOLESTR
*names
, UINT count
, LCID lcid
, DISPID
*id
)
223 static HRESULT WINAPI
dispex_Invoke(
224 IDispatchEx
*iface
, DISPID member
, REFIID riid
, LCID lcid
, WORD flags
,
225 DISPPARAMS
*params
, VARIANT
*result
, EXCEPINFO
*excep
, UINT
*err
)
230 static HRESULT WINAPI
dispex_DeleteMemberByName(
231 IDispatchEx
*iface
, BSTR name
, DWORD flags
)
236 static HRESULT WINAPI
dispex_DeleteMemberByDispID(
237 IDispatchEx
*iface
, DISPID id
)
242 static HRESULT WINAPI
dispex_GetMemberProperties(
243 IDispatchEx
*iface
, DISPID id
, DWORD flags_fetch
, DWORD
*flags
)
248 static HRESULT WINAPI
dispex_GetMemberName(
249 IDispatchEx
*iface
, DISPID id
, BSTR
*name
)
254 static HRESULT WINAPI
dispex_GetNextDispID(
255 IDispatchEx
*iface
, DWORD flags
, DISPID id
, DISPID
*next
)
260 static HRESULT WINAPI
dispex_GetNameSpaceParent(
261 IDispatchEx
*iface
, IUnknown
**unk
)
266 #define DISPID_GLOBAL_DNSRESOLVE 0x1000
268 static HRESULT WINAPI
dispex_GetDispID(
269 IDispatchEx
*iface
, BSTR name
, DWORD flags
, DISPID
*id
)
271 if (!lstrcmpW( name
, L
"dns_resolve" ))
273 *id
= DISPID_GLOBAL_DNSRESOLVE
;
276 return DISP_E_UNKNOWNNAME
;
279 static char *get_computer_name( COMPUTER_NAME_FORMAT format
)
284 GetComputerNameExA( format
, NULL
, &size
);
285 if (GetLastError() != ERROR_MORE_DATA
) return NULL
;
286 if (!(ret
= heap_alloc( size
))) return NULL
;
287 if (!GetComputerNameExA( format
, ret
, &size
))
295 static void printf_addr( const WCHAR
*fmt
, WCHAR
*buf
, SIZE_T size
, struct sockaddr_in
*addr
)
297 swprintf( buf
, size
, fmt
,
298 (unsigned int)(ntohl( addr
->sin_addr
.s_addr
) >> 24 & 0xff),
299 (unsigned int)(ntohl( addr
->sin_addr
.s_addr
) >> 16 & 0xff),
300 (unsigned int)(ntohl( addr
->sin_addr
.s_addr
) >> 8 & 0xff),
301 (unsigned int)(ntohl( addr
->sin_addr
.s_addr
) & 0xff) );
304 static HRESULT
dns_resolve( const WCHAR
*hostname
, VARIANT
*result
)
307 struct addrinfo
*ai
, *elem
;
312 hostnameA
= strdupWA( hostname
);
314 hostnameA
= get_computer_name( ComputerNamePhysicalDnsFullyQualified
);
316 if (!hostnameA
) return E_OUTOFMEMORY
;
317 res
= getaddrinfo( hostnameA
, NULL
, NULL
, &ai
);
318 heap_free( hostnameA
);
319 if (res
) return S_FALSE
;
322 while (elem
&& elem
->ai_family
!= AF_INET
) elem
= elem
->ai_next
;
328 printf_addr( L
"%u.%u.%u.%u", addr
, ARRAY_SIZE(addr
), (struct sockaddr_in
*)elem
->ai_addr
);
330 V_VT( result
) = VT_BSTR
;
331 V_BSTR( result
) = SysAllocString( addr
);
335 static HRESULT WINAPI
dispex_InvokeEx(
336 IDispatchEx
*iface
, DISPID id
, LCID lcid
, WORD flags
, DISPPARAMS
*params
,
337 VARIANT
*result
, EXCEPINFO
*exep
, IServiceProvider
*caller
)
339 if (id
== DISPID_GLOBAL_DNSRESOLVE
)
341 if (params
->cArgs
!= 1) return DISP_E_BADPARAMCOUNT
;
342 if (V_VT(¶ms
->rgvarg
[0]) != VT_BSTR
) return DISP_E_BADVARTYPE
;
343 return dns_resolve( V_BSTR(¶ms
->rgvarg
[0]), result
);
345 return DISP_E_MEMBERNOTFOUND
;
348 static const IDispatchExVtbl dispex_vtbl
=
350 dispex_QueryInterface
,
353 dispex_GetTypeInfoCount
,
355 dispex_GetIDsOfNames
,
359 dispex_DeleteMemberByName
,
360 dispex_DeleteMemberByDispID
,
361 dispex_GetMemberProperties
,
362 dispex_GetMemberName
,
363 dispex_GetNextDispID
,
364 dispex_GetNameSpaceParent
367 static IDispatchEx global_dispex
= { &dispex_vtbl
};
369 static HRESULT WINAPI
site_QueryInterface(
370 IActiveScriptSite
*iface
, REFIID riid
, void **ppv
)
374 if (IsEqualGUID( &IID_IUnknown
, riid
))
376 else if (IsEqualGUID( &IID_IActiveScriptSite
, riid
))
379 return E_NOINTERFACE
;
381 IUnknown_AddRef( (IUnknown
*)*ppv
);
385 static ULONG WINAPI
site_AddRef(
386 IActiveScriptSite
*iface
)
391 static ULONG WINAPI
site_Release(
392 IActiveScriptSite
*iface
)
397 static HRESULT WINAPI
site_GetLCID(
398 IActiveScriptSite
*iface
, LCID
*lcid
)
403 static HRESULT WINAPI
site_GetItemInfo(
404 IActiveScriptSite
*iface
, LPCOLESTR name
, DWORD mask
,
405 IUnknown
**item
, ITypeInfo
**type_info
)
407 if (!lstrcmpW( name
, L
"global_funcs" ) && mask
== SCRIPTINFO_IUNKNOWN
)
409 *item
= (IUnknown
*)&global_dispex
;
415 static HRESULT WINAPI
site_GetDocVersionString(
416 IActiveScriptSite
*iface
, BSTR
*version
)
421 static HRESULT WINAPI
site_OnScriptTerminate(
422 IActiveScriptSite
*iface
, const VARIANT
*result
, const EXCEPINFO
*info
)
427 static HRESULT WINAPI
site_OnStateChange(
428 IActiveScriptSite
*iface
, SCRIPTSTATE state
)
433 static HRESULT WINAPI
site_OnScriptError(
434 IActiveScriptSite
*iface
, IActiveScriptError
*error
)
439 static HRESULT WINAPI
site_OnEnterScript(
440 IActiveScriptSite
*iface
)
445 static HRESULT WINAPI
site_OnLeaveScript(
446 IActiveScriptSite
*iface
)
451 static const IActiveScriptSiteVtbl site_vtbl
=
458 site_GetDocVersionString
,
459 site_OnScriptTerminate
,
466 static IActiveScriptSite script_site
= { &site_vtbl
};
468 static BSTR
include_pac_utils( const WCHAR
*script
)
470 HMODULE hmod
= GetModuleHandleA( "jsproxy.dll" );
477 if (!(rsrc
= FindResourceW( hmod
, L
"pac.js", (LPCWSTR
)40 ))) return NULL
;
478 size
= SizeofResource( hmod
, rsrc
);
479 data
= LoadResource( hmod
, rsrc
);
481 len
= MultiByteToWideChar( CP_ACP
, 0, data
, size
, NULL
, 0 );
482 if (!(ret
= SysAllocStringLen( NULL
, len
+ lstrlenW( script
) + 1 ))) return NULL
;
483 MultiByteToWideChar( CP_ACP
, 0, data
, size
, ret
, len
);
484 lstrcpyW( ret
+ len
, script
);
489 #define IActiveScriptParse_Release IActiveScriptParse64_Release
490 #define IActiveScriptParse_InitNew IActiveScriptParse64_InitNew
491 #define IActiveScriptParse_ParseScriptText IActiveScriptParse64_ParseScriptText
493 #define IActiveScriptParse_Release IActiveScriptParse32_Release
494 #define IActiveScriptParse_InitNew IActiveScriptParse32_InitNew
495 #define IActiveScriptParse_ParseScriptText IActiveScriptParse32_ParseScriptText
498 static BOOL
run_script( const WCHAR
*script
, const WCHAR
*url
, const WCHAR
*hostname
, char **result_str
, DWORD
*result_len
)
500 IActiveScriptParse
*parser
= NULL
;
501 IActiveScript
*engine
= NULL
;
502 IDispatch
*dispatch
= NULL
;
506 BSTR func
= NULL
, full_script
= NULL
;
507 VARIANT args
[2], retval
;
511 init
= CoInitialize( NULL
);
512 hr
= CLSIDFromProgID( L
"JScript", &clsid
);
513 if (hr
!= S_OK
) goto done
;
515 hr
= CoCreateInstance( &clsid
, NULL
, CLSCTX_INPROC_SERVER
|CLSCTX_INPROC_HANDLER
,
516 &IID_IActiveScript
, (void **)&engine
);
517 if (hr
!= S_OK
) goto done
;
519 hr
= IActiveScript_QueryInterface( engine
, &IID_IActiveScriptParse
, (void **)&parser
);
520 if (hr
!= S_OK
) goto done
;
522 hr
= IActiveScriptParse_InitNew( parser
);
523 if (hr
!= S_OK
) goto done
;
525 hr
= IActiveScript_SetScriptSite( engine
, &script_site
);
526 if (hr
!= S_OK
) goto done
;
528 hr
= IActiveScript_AddNamedItem( engine
, L
"global_funcs", SCRIPTITEM_GLOBALMEMBERS
);
529 if (hr
!= S_OK
) goto done
;
531 if (!(full_script
= include_pac_utils( script
))) goto done
;
533 hr
= IActiveScriptParse_ParseScriptText( parser
, full_script
, NULL
, NULL
, NULL
, 0, 0, 0, NULL
, NULL
);
534 if (hr
!= S_OK
) goto done
;
536 hr
= IActiveScript_SetScriptState( engine
, SCRIPTSTATE_STARTED
);
537 if (hr
!= S_OK
) goto done
;
539 hr
= IActiveScript_GetScriptDispatch( engine
, NULL
, &dispatch
);
540 if (hr
!= S_OK
) goto done
;
542 if (!(func
= SysAllocString( L
"FindProxyForURL" ))) goto done
;
543 hr
= IDispatch_GetIDsOfNames( dispatch
, &IID_NULL
, &func
, 1, LOCALE_SYSTEM_DEFAULT
, &dispid
);
544 if (hr
!= S_OK
) goto done
;
546 V_VT( &args
[0] ) = VT_BSTR
;
547 V_BSTR( &args
[0] ) = SysAllocString( hostname
);
548 V_VT( &args
[1] ) = VT_BSTR
;
549 V_BSTR( &args
[1] ) = SysAllocString( url
);
551 params
.rgvarg
= args
;
552 params
.rgdispidNamedArgs
= NULL
;
554 params
.cNamedArgs
= 0;
555 hr
= IDispatch_Invoke( dispatch
, dispid
, &IID_NULL
, LOCALE_SYSTEM_DEFAULT
, DISPATCH_METHOD
,
556 ¶ms
, &retval
, NULL
, NULL
);
557 VariantClear( &args
[0] );
558 VariantClear( &args
[1] );
561 WARN("script failed 0x%08x\n", hr
);
564 if ((*result_str
= strdupWA( V_BSTR( &retval
) )))
566 TRACE( "result: %s\n", debugstr_a(*result_str
) );
567 *result_len
= strlen( *result_str
) + 1;
570 VariantClear( &retval
);
573 SysFreeString( full_script
);
574 SysFreeString( func
);
575 if (dispatch
) IDispatch_Release( dispatch
);
576 if (parser
) IActiveScriptParse_Release( parser
);
577 if (engine
) IActiveScript_Release( engine
);
578 if (SUCCEEDED( init
)) CoUninitialize();
582 /******************************************************************
583 * InternetGetProxyInfo (jsproxy.@)
585 BOOL WINAPI
InternetGetProxyInfo( LPCSTR url
, DWORD len_url
, LPCSTR hostname
, DWORD len_hostname
, LPSTR
*proxy
,
588 WCHAR
*urlW
= NULL
, *hostnameW
= NULL
;
591 TRACE( "%s, %u, %s, %u, %p, %p\n", debugstr_a(url
), len_url
, hostname
, len_hostname
, proxy
, len_proxy
);
593 EnterCriticalSection( &cs_jsproxy
);
595 if (!global_script
->text
)
597 SetLastError( ERROR_CAN_NOT_COMPLETE
);
600 if (hostname
&& len_hostname
< strlen( hostname
))
602 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
605 if (!(urlW
= strdupAW( url
, -1 ))) goto done
;
606 if (hostname
&& !(hostnameW
= strdupAW( hostname
, -1 ))) goto done
;
608 TRACE( "%s\n", debugstr_w(global_script
->text
) );
609 ret
= run_script( global_script
->text
, urlW
, hostnameW
, proxy
, len_proxy
);
612 heap_free( hostnameW
);
614 LeaveCriticalSection( &cs_jsproxy
);