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 static const WCHAR global_funcsW
[] = {'g','l','o','b','a','l','_','f','u','n','c','s',0};
50 static const WCHAR dns_resolveW
[] = {'d','n','s','_','r','e','s','o','l','v','e',0};
52 /******************************************************************
55 BOOL WINAPI
DllMain( HINSTANCE hinst
, DWORD reason
, LPVOID reserved
)
59 case DLL_PROCESS_ATTACH
:
61 DisableThreadLibraryCalls( hinst
);
64 case DLL_PROCESS_DETACH
:
70 static inline WCHAR
*strdupAW( const char *src
, int len
)
75 int dst_len
= MultiByteToWideChar( CP_ACP
, 0, src
, len
, NULL
, 0 );
76 if ((dst
= heap_alloc( (dst_len
+ 1) * sizeof(WCHAR
) )))
78 len
= MultiByteToWideChar( CP_ACP
, 0, src
, len
, dst
, dst_len
);
85 static inline char *strdupWA( const WCHAR
*src
)
90 int len
= WideCharToMultiByte( CP_ACP
, 0, src
, -1, NULL
, 0, NULL
, NULL
);
91 if ((dst
= heap_alloc( len
))) WideCharToMultiByte( CP_ACP
, 0, src
, -1, dst
, len
, NULL
, NULL
);
96 static struct pac_script
100 static struct pac_script
*global_script
= &pac_script
;
102 /******************************************************************
103 * InternetDeInitializeAutoProxyDll (jsproxy.@)
105 BOOL WINAPI
InternetDeInitializeAutoProxyDll( LPSTR mime
, DWORD reserved
)
107 TRACE( "%s, %u\n", debugstr_a(mime
), reserved
);
109 EnterCriticalSection( &cs_jsproxy
);
111 heap_free( global_script
->text
);
112 global_script
->text
= NULL
;
114 LeaveCriticalSection( &cs_jsproxy
);
118 static WCHAR
*load_script( const char *filename
)
121 DWORD size
, bytes_read
;
124 WCHAR
*script
= NULL
;
126 handle
= CreateFileA( filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0 );
127 if (handle
== INVALID_HANDLE_VALUE
) return NULL
;
129 size
= GetFileSize( handle
, NULL
);
130 if (!(buffer
= heap_alloc( size
))) goto done
;
131 if (!ReadFile( handle
, buffer
, size
, &bytes_read
, NULL
) || bytes_read
!= size
) goto done
;
133 len
= MultiByteToWideChar( CP_ACP
, 0, buffer
, size
, NULL
, 0 );
134 if (!(script
= heap_alloc( (len
+ 1) * sizeof(WCHAR
) ))) goto done
;
135 MultiByteToWideChar( CP_ACP
, 0, buffer
, size
, script
, len
);
139 CloseHandle( handle
);
144 /******************************************************************
145 * InternetInitializeAutoProxyDll (jsproxy.@)
147 BOOL WINAPI
JSPROXY_InternetInitializeAutoProxyDll( DWORD version
, LPSTR tmpfile
, LPSTR mime
,
148 AutoProxyHelperFunctions
*callbacks
,
149 LPAUTO_PROXY_SCRIPT_BUFFER buffer
)
153 TRACE( "%u, %s, %s, %p, %p\n", version
, debugstr_a(tmpfile
), debugstr_a(mime
), callbacks
, buffer
);
155 if (callbacks
) FIXME( "callbacks not supported\n" );
157 EnterCriticalSection( &cs_jsproxy
);
159 if (buffer
&& buffer
->dwStructSize
== sizeof(*buffer
) && buffer
->lpszScriptBuffer
)
161 if (!buffer
->dwScriptBufferSize
)
163 SetLastError( ERROR_INVALID_PARAMETER
);
164 LeaveCriticalSection( &cs_jsproxy
);
167 heap_free( global_script
->text
);
168 if ((global_script
->text
= strdupAW( buffer
->lpszScriptBuffer
,
169 buffer
->dwScriptBufferSize
))) ret
= TRUE
;
173 heap_free( global_script
->text
);
174 if ((global_script
->text
= load_script( tmpfile
))) ret
= TRUE
;
177 LeaveCriticalSection( &cs_jsproxy
);
181 static HRESULT WINAPI
dispex_QueryInterface(
182 IDispatchEx
*iface
, REFIID riid
, void **ppv
)
186 if (IsEqualGUID( riid
, &IID_IUnknown
) ||
187 IsEqualGUID( riid
, &IID_IDispatch
) ||
188 IsEqualGUID( riid
, &IID_IDispatchEx
))
191 return E_NOINTERFACE
;
196 static ULONG WINAPI
dispex_AddRef(
202 static ULONG WINAPI
dispex_Release(
208 static HRESULT WINAPI
dispex_GetTypeInfoCount(
209 IDispatchEx
*iface
, UINT
*info
)
214 static HRESULT WINAPI
dispex_GetTypeInfo(
215 IDispatchEx
*iface
, UINT info
, LCID lcid
, ITypeInfo
**type_info
)
220 static HRESULT WINAPI
dispex_GetIDsOfNames(
221 IDispatchEx
*iface
, REFIID riid
, LPOLESTR
*names
, UINT count
, LCID lcid
, DISPID
*id
)
226 static HRESULT WINAPI
dispex_Invoke(
227 IDispatchEx
*iface
, DISPID member
, REFIID riid
, LCID lcid
, WORD flags
,
228 DISPPARAMS
*params
, VARIANT
*result
, EXCEPINFO
*excep
, UINT
*err
)
233 static HRESULT WINAPI
dispex_DeleteMemberByName(
234 IDispatchEx
*iface
, BSTR name
, DWORD flags
)
239 static HRESULT WINAPI
dispex_DeleteMemberByDispID(
240 IDispatchEx
*iface
, DISPID id
)
245 static HRESULT WINAPI
dispex_GetMemberProperties(
246 IDispatchEx
*iface
, DISPID id
, DWORD flags_fetch
, DWORD
*flags
)
251 static HRESULT WINAPI
dispex_GetMemberName(
252 IDispatchEx
*iface
, DISPID id
, BSTR
*name
)
257 static HRESULT WINAPI
dispex_GetNextDispID(
258 IDispatchEx
*iface
, DWORD flags
, DISPID id
, DISPID
*next
)
263 static HRESULT WINAPI
dispex_GetNameSpaceParent(
264 IDispatchEx
*iface
, IUnknown
**unk
)
269 #define DISPID_GLOBAL_DNSRESOLVE 0x1000
271 static HRESULT WINAPI
dispex_GetDispID(
272 IDispatchEx
*iface
, BSTR name
, DWORD flags
, DISPID
*id
)
274 if (!lstrcmpW( name
, dns_resolveW
))
276 *id
= DISPID_GLOBAL_DNSRESOLVE
;
279 return DISP_E_UNKNOWNNAME
;
282 static char *get_computer_name( COMPUTER_NAME_FORMAT format
)
287 GetComputerNameExA( format
, NULL
, &size
);
288 if (GetLastError() != ERROR_MORE_DATA
) return NULL
;
289 if (!(ret
= heap_alloc( size
))) return NULL
;
290 if (!GetComputerNameExA( format
, ret
, &size
))
298 static void printf_addr( const WCHAR
*fmt
, WCHAR
*buf
, SIZE_T size
, struct sockaddr_in
*addr
)
300 swprintf( buf
, size
, fmt
,
301 (unsigned int)(ntohl( addr
->sin_addr
.s_addr
) >> 24 & 0xff),
302 (unsigned int)(ntohl( addr
->sin_addr
.s_addr
) >> 16 & 0xff),
303 (unsigned int)(ntohl( addr
->sin_addr
.s_addr
) >> 8 & 0xff),
304 (unsigned int)(ntohl( addr
->sin_addr
.s_addr
) & 0xff) );
307 static HRESULT
dns_resolve( const WCHAR
*hostname
, VARIANT
*result
)
309 static const WCHAR fmtW
[] = {'%','u','.','%','u','.','%','u','.','%','u',0};
311 struct addrinfo
*ai
, *elem
;
316 hostnameA
= strdupWA( hostname
);
318 hostnameA
= get_computer_name( ComputerNamePhysicalDnsFullyQualified
);
320 if (!hostnameA
) return E_OUTOFMEMORY
;
321 res
= getaddrinfo( hostnameA
, NULL
, NULL
, &ai
);
322 heap_free( hostnameA
);
323 if (res
) return S_FALSE
;
326 while (elem
&& elem
->ai_family
!= AF_INET
) elem
= elem
->ai_next
;
332 printf_addr( fmtW
, addr
, ARRAY_SIZE(addr
), (struct sockaddr_in
*)elem
->ai_addr
);
334 V_VT( result
) = VT_BSTR
;
335 V_BSTR( result
) = SysAllocString( addr
);
339 static HRESULT WINAPI
dispex_InvokeEx(
340 IDispatchEx
*iface
, DISPID id
, LCID lcid
, WORD flags
, DISPPARAMS
*params
,
341 VARIANT
*result
, EXCEPINFO
*exep
, IServiceProvider
*caller
)
343 if (id
== DISPID_GLOBAL_DNSRESOLVE
)
345 if (params
->cArgs
!= 1) return DISP_E_BADPARAMCOUNT
;
346 if (V_VT(¶ms
->rgvarg
[0]) != VT_BSTR
) return DISP_E_BADVARTYPE
;
347 return dns_resolve( V_BSTR(¶ms
->rgvarg
[0]), result
);
349 return DISP_E_MEMBERNOTFOUND
;
352 static const IDispatchExVtbl dispex_vtbl
=
354 dispex_QueryInterface
,
357 dispex_GetTypeInfoCount
,
359 dispex_GetIDsOfNames
,
363 dispex_DeleteMemberByName
,
364 dispex_DeleteMemberByDispID
,
365 dispex_GetMemberProperties
,
366 dispex_GetMemberName
,
367 dispex_GetNextDispID
,
368 dispex_GetNameSpaceParent
371 static IDispatchEx global_dispex
= { &dispex_vtbl
};
373 static HRESULT WINAPI
site_QueryInterface(
374 IActiveScriptSite
*iface
, REFIID riid
, void **ppv
)
378 if (IsEqualGUID( &IID_IUnknown
, riid
))
380 else if (IsEqualGUID( &IID_IActiveScriptSite
, riid
))
383 return E_NOINTERFACE
;
385 IUnknown_AddRef( (IUnknown
*)*ppv
);
389 static ULONG WINAPI
site_AddRef(
390 IActiveScriptSite
*iface
)
395 static ULONG WINAPI
site_Release(
396 IActiveScriptSite
*iface
)
401 static HRESULT WINAPI
site_GetLCID(
402 IActiveScriptSite
*iface
, LCID
*lcid
)
407 static HRESULT WINAPI
site_GetItemInfo(
408 IActiveScriptSite
*iface
, LPCOLESTR name
, DWORD mask
,
409 IUnknown
**item
, ITypeInfo
**type_info
)
411 if (!lstrcmpW( name
, global_funcsW
) && mask
== SCRIPTINFO_IUNKNOWN
)
413 *item
= (IUnknown
*)&global_dispex
;
419 static HRESULT WINAPI
site_GetDocVersionString(
420 IActiveScriptSite
*iface
, BSTR
*version
)
425 static HRESULT WINAPI
site_OnScriptTerminate(
426 IActiveScriptSite
*iface
, const VARIANT
*result
, const EXCEPINFO
*info
)
431 static HRESULT WINAPI
site_OnStateChange(
432 IActiveScriptSite
*iface
, SCRIPTSTATE state
)
437 static HRESULT WINAPI
site_OnScriptError(
438 IActiveScriptSite
*iface
, IActiveScriptError
*error
)
443 static HRESULT WINAPI
site_OnEnterScript(
444 IActiveScriptSite
*iface
)
449 static HRESULT WINAPI
site_OnLeaveScript(
450 IActiveScriptSite
*iface
)
455 static const IActiveScriptSiteVtbl site_vtbl
=
462 site_GetDocVersionString
,
463 site_OnScriptTerminate
,
470 static IActiveScriptSite script_site
= { &site_vtbl
};
472 static BSTR
include_pac_utils( const WCHAR
*script
)
474 static const WCHAR pacjsW
[] = {'p','a','c','.','j','s',0};
475 HMODULE hmod
= GetModuleHandleA( "jsproxy.dll" );
482 if (!(rsrc
= FindResourceW( hmod
, pacjsW
, (LPCWSTR
)40 ))) return NULL
;
483 size
= SizeofResource( hmod
, rsrc
);
484 data
= LoadResource( hmod
, rsrc
);
486 len
= MultiByteToWideChar( CP_ACP
, 0, data
, size
, NULL
, 0 );
487 if (!(ret
= SysAllocStringLen( NULL
, len
+ lstrlenW( script
) + 1 ))) return NULL
;
488 MultiByteToWideChar( CP_ACP
, 0, data
, size
, ret
, len
);
489 lstrcpyW( ret
+ len
, script
);
494 #define IActiveScriptParse_Release IActiveScriptParse64_Release
495 #define IActiveScriptParse_InitNew IActiveScriptParse64_InitNew
496 #define IActiveScriptParse_ParseScriptText IActiveScriptParse64_ParseScriptText
498 #define IActiveScriptParse_Release IActiveScriptParse32_Release
499 #define IActiveScriptParse_InitNew IActiveScriptParse32_InitNew
500 #define IActiveScriptParse_ParseScriptText IActiveScriptParse32_ParseScriptText
503 static BOOL
run_script( const WCHAR
*script
, const WCHAR
*url
, const WCHAR
*hostname
, char **result_str
, DWORD
*result_len
)
505 static const WCHAR jscriptW
[] = {'J','S','c','r','i','p','t',0};
506 static const WCHAR findproxyW
[] = {'F','i','n','d','P','r','o','x','y','F','o','r','U','R','L',0};
507 IActiveScriptParse
*parser
= NULL
;
508 IActiveScript
*engine
= NULL
;
509 IDispatch
*dispatch
= NULL
;
513 BSTR func
= NULL
, full_script
= NULL
;
514 VARIANT args
[2], retval
;
518 init
= CoInitialize( NULL
);
519 hr
= CLSIDFromProgID( jscriptW
, &clsid
);
520 if (hr
!= S_OK
) goto done
;
522 hr
= CoCreateInstance( &clsid
, NULL
, CLSCTX_INPROC_SERVER
|CLSCTX_INPROC_HANDLER
,
523 &IID_IActiveScript
, (void **)&engine
);
524 if (hr
!= S_OK
) goto done
;
526 hr
= IActiveScript_QueryInterface( engine
, &IID_IActiveScriptParse
, (void **)&parser
);
527 if (hr
!= S_OK
) goto done
;
529 hr
= IActiveScriptParse_InitNew( parser
);
530 if (hr
!= S_OK
) goto done
;
532 hr
= IActiveScript_SetScriptSite( engine
, &script_site
);
533 if (hr
!= S_OK
) goto done
;
535 hr
= IActiveScript_AddNamedItem( engine
, global_funcsW
, SCRIPTITEM_GLOBALMEMBERS
);
536 if (hr
!= S_OK
) goto done
;
538 if (!(full_script
= include_pac_utils( script
))) goto done
;
540 hr
= IActiveScriptParse_ParseScriptText( parser
, full_script
, NULL
, NULL
, NULL
, 0, 0, 0, NULL
, NULL
);
541 if (hr
!= S_OK
) goto done
;
543 hr
= IActiveScript_SetScriptState( engine
, SCRIPTSTATE_STARTED
);
544 if (hr
!= S_OK
) goto done
;
546 hr
= IActiveScript_GetScriptDispatch( engine
, NULL
, &dispatch
);
547 if (hr
!= S_OK
) goto done
;
549 if (!(func
= SysAllocString( findproxyW
))) goto done
;
550 hr
= IDispatch_GetIDsOfNames( dispatch
, &IID_NULL
, &func
, 1, LOCALE_SYSTEM_DEFAULT
, &dispid
);
551 if (hr
!= S_OK
) goto done
;
553 V_VT( &args
[0] ) = VT_BSTR
;
554 V_BSTR( &args
[0] ) = SysAllocString( hostname
);
555 V_VT( &args
[1] ) = VT_BSTR
;
556 V_BSTR( &args
[1] ) = SysAllocString( url
);
558 params
.rgvarg
= args
;
559 params
.rgdispidNamedArgs
= NULL
;
561 params
.cNamedArgs
= 0;
562 hr
= IDispatch_Invoke( dispatch
, dispid
, &IID_NULL
, LOCALE_SYSTEM_DEFAULT
, DISPATCH_METHOD
,
563 ¶ms
, &retval
, NULL
, NULL
);
564 VariantClear( &args
[0] );
565 VariantClear( &args
[1] );
568 WARN("script failed 0x%08x\n", hr
);
571 if ((*result_str
= strdupWA( V_BSTR( &retval
) )))
573 TRACE( "result: %s\n", debugstr_a(*result_str
) );
574 *result_len
= strlen( *result_str
) + 1;
577 VariantClear( &retval
);
580 SysFreeString( full_script
);
581 SysFreeString( func
);
582 if (dispatch
) IDispatch_Release( dispatch
);
583 if (parser
) IActiveScriptParse_Release( parser
);
584 if (engine
) IActiveScript_Release( engine
);
585 if (SUCCEEDED( init
)) CoUninitialize();
589 /******************************************************************
590 * InternetGetProxyInfo (jsproxy.@)
592 BOOL WINAPI
InternetGetProxyInfo( LPCSTR url
, DWORD len_url
, LPCSTR hostname
, DWORD len_hostname
, LPSTR
*proxy
,
595 WCHAR
*urlW
= NULL
, *hostnameW
= NULL
;
598 TRACE( "%s, %u, %s, %u, %p, %p\n", debugstr_a(url
), len_url
, hostname
, len_hostname
, proxy
, len_proxy
);
600 EnterCriticalSection( &cs_jsproxy
);
602 if (!global_script
->text
)
604 SetLastError( ERROR_CAN_NOT_COMPLETE
);
607 if (hostname
&& len_hostname
< strlen( hostname
))
609 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
612 if (!(urlW
= strdupAW( url
, -1 ))) goto done
;
613 if (hostname
&& !(hostnameW
= strdupAW( hostname
, -1 ))) goto done
;
615 TRACE( "%s\n", debugstr_w(global_script
->text
) );
616 ret
= run_script( global_script
->text
, urlW
, hostnameW
, proxy
, len_proxy
);
619 heap_free( hostnameW
);
621 LeaveCriticalSection( &cs_jsproxy
);