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
, int 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 (buffer
&& buffer
->dwStructSize
== sizeof(*buffer
) && buffer
->lpszScriptBuffer
)
192 for (i
= 0; i
< buffer
->dwScriptBufferSize
; i
++)
194 if (!buffer
->lpszScriptBuffer
[i
]) break;
197 if (len
== buffer
->dwScriptBufferSize
)
199 SetLastError( ERROR_INVALID_PARAMETER
);
200 LeaveCriticalSection( &cs_jsproxy
);
203 heap_free( global_script
->text
);
204 if ((global_script
->text
= strdupAW( buffer
->lpszScriptBuffer
, len
))) ret
= TRUE
;
208 heap_free( global_script
->text
);
209 if ((global_script
->text
= load_script( tmpfile
))) ret
= TRUE
;
212 LeaveCriticalSection( &cs_jsproxy
);
216 static HRESULT WINAPI
dispex_QueryInterface(
217 IDispatchEx
*iface
, REFIID riid
, void **ppv
)
221 if (IsEqualGUID( riid
, &IID_IUnknown
) ||
222 IsEqualGUID( riid
, &IID_IDispatch
) ||
223 IsEqualGUID( riid
, &IID_IDispatchEx
))
226 return E_NOINTERFACE
;
231 static ULONG WINAPI
dispex_AddRef(
237 static ULONG WINAPI
dispex_Release(
243 static HRESULT WINAPI
dispex_GetTypeInfoCount(
244 IDispatchEx
*iface
, UINT
*info
)
249 static HRESULT WINAPI
dispex_GetTypeInfo(
250 IDispatchEx
*iface
, UINT info
, LCID lcid
, ITypeInfo
**type_info
)
255 static HRESULT WINAPI
dispex_GetIDsOfNames(
256 IDispatchEx
*iface
, REFIID riid
, LPOLESTR
*names
, UINT count
, LCID lcid
, DISPID
*id
)
261 static HRESULT WINAPI
dispex_Invoke(
262 IDispatchEx
*iface
, DISPID member
, REFIID riid
, LCID lcid
, WORD flags
,
263 DISPPARAMS
*params
, VARIANT
*result
, EXCEPINFO
*excep
, UINT
*err
)
268 static HRESULT WINAPI
dispex_DeleteMemberByName(
269 IDispatchEx
*iface
, BSTR name
, DWORD flags
)
274 static HRESULT WINAPI
dispex_DeleteMemberByDispID(
275 IDispatchEx
*iface
, DISPID id
)
280 static HRESULT WINAPI
dispex_GetMemberProperties(
281 IDispatchEx
*iface
, DISPID id
, DWORD flags_fetch
, DWORD
*flags
)
286 static HRESULT WINAPI
dispex_GetMemberName(
287 IDispatchEx
*iface
, DISPID id
, BSTR
*name
)
292 static HRESULT WINAPI
dispex_GetNextDispID(
293 IDispatchEx
*iface
, DWORD flags
, DISPID id
, DISPID
*next
)
298 static HRESULT WINAPI
dispex_GetNameSpaceParent(
299 IDispatchEx
*iface
, IUnknown
**unk
)
304 #define DISPID_GLOBAL_DNSRESOLVE 0x1000
306 static HRESULT WINAPI
dispex_GetDispID(
307 IDispatchEx
*iface
, BSTR name
, DWORD flags
, DISPID
*id
)
309 if (!strcmpW( name
, dns_resolveW
))
311 *id
= DISPID_GLOBAL_DNSRESOLVE
;
314 return DISP_E_UNKNOWNNAME
;
317 static char *get_computer_name( COMPUTER_NAME_FORMAT format
)
322 GetComputerNameExA( format
, NULL
, &size
);
323 if (GetLastError() != ERROR_MORE_DATA
) return NULL
;
324 if (!(ret
= heap_alloc( size
))) return NULL
;
325 if (!GetComputerNameExA( format
, ret
, &size
))
333 static void printf_addr( const WCHAR
*fmt
, WCHAR
*buf
, struct sockaddr_in
*addr
)
336 (unsigned int)(ntohl( addr
->sin_addr
.s_addr
) >> 24 & 0xff),
337 (unsigned int)(ntohl( addr
->sin_addr
.s_addr
) >> 16 & 0xff),
338 (unsigned int)(ntohl( addr
->sin_addr
.s_addr
) >> 8 & 0xff),
339 (unsigned int)(ntohl( addr
->sin_addr
.s_addr
) & 0xff) );
342 static HRESULT
dns_resolve( const WCHAR
*hostname
, VARIANT
*result
)
344 #ifdef HAVE_GETADDRINFO
345 static const WCHAR fmtW
[] = {'%','u','.','%','u','.','%','u','.','%','u',0};
347 struct addrinfo
*ai
, *elem
;
352 hostnameA
= strdupWA( hostname
);
354 hostnameA
= get_computer_name( ComputerNamePhysicalDnsFullyQualified
);
356 if (!hostnameA
) return E_OUTOFMEMORY
;
357 res
= getaddrinfo( hostnameA
, NULL
, NULL
, &ai
);
358 heap_free( hostnameA
);
359 if (res
) return S_FALSE
;
362 while (elem
&& elem
->ai_family
!= AF_INET
) elem
= elem
->ai_next
;
368 printf_addr( fmtW
, addr
, (struct sockaddr_in
*)elem
->ai_addr
);
370 V_VT( result
) = VT_BSTR
;
371 V_BSTR( result
) = SysAllocString( addr
);
374 FIXME("getaddrinfo not found at build time\n");
379 static HRESULT WINAPI
dispex_InvokeEx(
380 IDispatchEx
*iface
, DISPID id
, LCID lcid
, WORD flags
, DISPPARAMS
*params
,
381 VARIANT
*result
, EXCEPINFO
*exep
, IServiceProvider
*caller
)
383 if (id
== DISPID_GLOBAL_DNSRESOLVE
)
385 if (params
->cArgs
!= 1) return DISP_E_BADPARAMCOUNT
;
386 if (V_VT(¶ms
->rgvarg
[0]) != VT_BSTR
) return DISP_E_BADVARTYPE
;
387 return dns_resolve( V_BSTR(¶ms
->rgvarg
[0]), result
);
389 return DISP_E_MEMBERNOTFOUND
;
392 static const IDispatchExVtbl dispex_vtbl
=
394 dispex_QueryInterface
,
397 dispex_GetTypeInfoCount
,
399 dispex_GetIDsOfNames
,
403 dispex_DeleteMemberByName
,
404 dispex_DeleteMemberByDispID
,
405 dispex_GetMemberProperties
,
406 dispex_GetMemberName
,
407 dispex_GetNextDispID
,
408 dispex_GetNameSpaceParent
411 static IDispatchEx global_dispex
= { &dispex_vtbl
};
413 static HRESULT WINAPI
site_QueryInterface(
414 IActiveScriptSite
*iface
, REFIID riid
, void **ppv
)
418 if (IsEqualGUID( &IID_IUnknown
, riid
))
420 else if (IsEqualGUID( &IID_IActiveScriptSite
, riid
))
423 return E_NOINTERFACE
;
425 IUnknown_AddRef( (IUnknown
*)*ppv
);
429 static ULONG WINAPI
site_AddRef(
430 IActiveScriptSite
*iface
)
435 static ULONG WINAPI
site_Release(
436 IActiveScriptSite
*iface
)
441 static HRESULT WINAPI
site_GetLCID(
442 IActiveScriptSite
*iface
, LCID
*lcid
)
447 static HRESULT WINAPI
site_GetItemInfo(
448 IActiveScriptSite
*iface
, LPCOLESTR name
, DWORD mask
,
449 IUnknown
**item
, ITypeInfo
**type_info
)
451 if (!strcmpW( name
, global_funcsW
) && mask
== SCRIPTINFO_IUNKNOWN
)
453 *item
= (IUnknown
*)&global_dispex
;
459 static HRESULT WINAPI
site_GetDocVersionString(
460 IActiveScriptSite
*iface
, BSTR
*version
)
465 static HRESULT WINAPI
site_OnScriptTerminate(
466 IActiveScriptSite
*iface
, const VARIANT
*result
, const EXCEPINFO
*info
)
471 static HRESULT WINAPI
site_OnStateChange(
472 IActiveScriptSite
*iface
, SCRIPTSTATE state
)
477 static HRESULT WINAPI
site_OnScriptError(
478 IActiveScriptSite
*iface
, IActiveScriptError
*error
)
483 static HRESULT WINAPI
site_OnEnterScript(
484 IActiveScriptSite
*iface
)
489 static HRESULT WINAPI
site_OnLeaveScript(
490 IActiveScriptSite
*iface
)
495 static const IActiveScriptSiteVtbl site_vtbl
=
502 site_GetDocVersionString
,
503 site_OnScriptTerminate
,
510 static IActiveScriptSite script_site
= { &site_vtbl
};
512 static BSTR
include_pac_utils( const WCHAR
*script
)
514 static const WCHAR pacjsW
[] = {'p','a','c','.','j','s',0};
515 HMODULE hmod
= GetModuleHandleA( "jsproxy.dll" );
522 if (!(rsrc
= FindResourceW( hmod
, pacjsW
, (LPCWSTR
)40 ))) return NULL
;
523 size
= SizeofResource( hmod
, rsrc
);
524 data
= LoadResource( hmod
, rsrc
);
526 len
= MultiByteToWideChar( CP_ACP
, 0, data
, size
, NULL
, 0 );
527 if (!(ret
= SysAllocStringLen( NULL
, len
+ strlenW( script
) + 1 ))) return NULL
;
528 MultiByteToWideChar( CP_ACP
, 0, data
, size
, ret
, len
);
529 strcpyW( ret
+ len
, script
);
534 #define IActiveScriptParse_Release IActiveScriptParse64_Release
535 #define IActiveScriptParse_InitNew IActiveScriptParse64_InitNew
536 #define IActiveScriptParse_ParseScriptText IActiveScriptParse64_ParseScriptText
538 #define IActiveScriptParse_Release IActiveScriptParse32_Release
539 #define IActiveScriptParse_InitNew IActiveScriptParse32_InitNew
540 #define IActiveScriptParse_ParseScriptText IActiveScriptParse32_ParseScriptText
543 static BOOL
run_script( const WCHAR
*script
, const WCHAR
*url
, const WCHAR
*hostname
, char **result_str
, DWORD
*result_len
)
545 static const WCHAR jscriptW
[] = {'J','S','c','r','i','p','t',0};
546 static const WCHAR findproxyW
[] = {'F','i','n','d','P','r','o','x','y','F','o','r','U','R','L',0};
547 IActiveScriptParse
*parser
= NULL
;
548 IActiveScript
*engine
= NULL
;
549 IDispatch
*dispatch
= NULL
;
553 BSTR func
= NULL
, full_script
= NULL
;
554 VARIANT args
[2], retval
;
558 init
= CoInitialize( NULL
);
559 hr
= CLSIDFromProgID( jscriptW
, &clsid
);
560 if (hr
!= S_OK
) goto done
;
562 hr
= CoCreateInstance( &clsid
, NULL
, CLSCTX_INPROC_SERVER
|CLSCTX_INPROC_HANDLER
,
563 &IID_IActiveScript
, (void **)&engine
);
564 if (hr
!= S_OK
) goto done
;
566 hr
= IActiveScript_QueryInterface( engine
, &IID_IActiveScriptParse
, (void **)&parser
);
567 if (hr
!= S_OK
) goto done
;
569 hr
= IActiveScriptParse_InitNew( parser
);
570 if (hr
!= S_OK
) goto done
;
572 hr
= IActiveScript_SetScriptSite( engine
, &script_site
);
573 if (hr
!= S_OK
) goto done
;
575 hr
= IActiveScript_AddNamedItem( engine
, global_funcsW
, SCRIPTITEM_GLOBALMEMBERS
);
576 if (hr
!= S_OK
) goto done
;
578 if (!(full_script
= include_pac_utils( script
))) goto done
;
580 hr
= IActiveScriptParse_ParseScriptText( parser
, full_script
, NULL
, NULL
, NULL
, 0, 0, 0, NULL
, NULL
);
581 if (hr
!= S_OK
) goto done
;
583 hr
= IActiveScript_SetScriptState( engine
, SCRIPTSTATE_STARTED
);
584 if (hr
!= S_OK
) goto done
;
586 hr
= IActiveScript_GetScriptDispatch( engine
, NULL
, &dispatch
);
587 if (hr
!= S_OK
) goto done
;
589 if (!(func
= SysAllocString( findproxyW
))) goto done
;
590 hr
= IDispatch_GetIDsOfNames( dispatch
, &IID_NULL
, &func
, 1, LOCALE_SYSTEM_DEFAULT
, &dispid
);
591 if (hr
!= S_OK
) goto done
;
593 V_VT( &args
[0] ) = VT_BSTR
;
594 V_BSTR( &args
[0] ) = SysAllocString( hostname
);
595 V_VT( &args
[1] ) = VT_BSTR
;
596 V_BSTR( &args
[1] ) = SysAllocString( url
);
598 params
.rgvarg
= args
;
599 params
.rgdispidNamedArgs
= NULL
;
601 params
.cNamedArgs
= 0;
602 hr
= IDispatch_Invoke( dispatch
, dispid
, &IID_NULL
, LOCALE_SYSTEM_DEFAULT
, DISPATCH_METHOD
,
603 ¶ms
, &retval
, NULL
, NULL
);
604 VariantClear( &args
[0] );
605 VariantClear( &args
[1] );
608 WARN("script failed 0x%08x\n", hr
);
611 if ((*result_str
= strdupWA( V_BSTR( &retval
) )))
613 TRACE( "result: %s\n", debugstr_a(*result_str
) );
614 *result_len
= strlen( *result_str
) + 1;
617 VariantClear( &retval
);
620 SysFreeString( full_script
);
621 SysFreeString( func
);
622 if (dispatch
) IDispatch_Release( dispatch
);
623 if (parser
) IActiveScriptParse_Release( parser
);
624 if (engine
) IActiveScript_Release( engine
);
625 if (SUCCEEDED( init
)) CoUninitialize();
629 /******************************************************************
630 * InternetGetProxyInfo (jsproxy.@)
632 BOOL WINAPI
InternetGetProxyInfo( LPCSTR url
, DWORD len_url
, LPCSTR hostname
, DWORD len_hostname
, LPSTR
*proxy
,
635 WCHAR
*urlW
= NULL
, *hostnameW
= NULL
;
638 TRACE( "%s, %u, %s, %u, %p, %p\n", url
, len_url
, hostname
, len_hostname
, proxy
, len_proxy
);
640 EnterCriticalSection( &cs_jsproxy
);
642 if (!global_script
->text
)
644 SetLastError( ERROR_CAN_NOT_COMPLETE
);
647 if (hostname
&& len_hostname
< strlen( hostname
))
649 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
652 if (!(urlW
= strdupAW( url
, -1 ))) goto done
;
653 if (hostname
&& !(hostnameW
= strdupAW( hostname
, -1 ))) goto done
;
655 TRACE( "%s\n", debugstr_w(global_script
->text
) );
656 ret
= run_script( global_script
->text
, urlW
, hostnameW
, proxy
, len_proxy
);
659 heap_free( hostnameW
);
661 LeaveCriticalSection( &cs_jsproxy
);