jsproxy: Allow multiple calls to InternetInitializeAutoProxyDll.
[wine.git] / dlls / jsproxy / main.c
blobe277a0b768e69f6ec0279c8c32c3ed57517ce439
1 /*
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
19 #include "config.h"
20 #include "wine/port.h"
22 #include <stdarg.h>
23 #include <sys/types.h>
24 #ifdef HAVE_SYS_SOCKET_H
25 # include <sys/socket.h>
26 #endif
27 #ifdef HAVE_NETINET_IN_H
28 # include <netinet/in.h>
29 #endif
30 #ifdef HAVE_NETDB_H
31 # include <netdb.h>
32 #endif
33 #if defined(__MINGW32__) || defined (_MSC_VER)
34 # include <ws2tcpip.h>
35 #else
36 # define closesocket close
37 # define ioctlsocket ioctl
38 #endif
40 #include "windef.h"
41 #include "winbase.h"
42 #ifndef __MINGW32__
43 #define USE_WS_PREFIX
44 #endif
45 #include "winsock2.h"
46 #include "ws2ipdef.h"
47 #include "winnls.h"
48 #include "wininet.h"
49 #define COBJMACROS
50 #include "ole2.h"
51 #include "dispex.h"
52 #include "activscp.h"
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 =
63 0, 0, &cs_jsproxy,
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 /******************************************************************
73 * DllMain (jsproxy.@)
75 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
77 switch (reason)
79 case DLL_PROCESS_ATTACH:
80 instance = hinst;
81 DisableThreadLibraryCalls( hinst );
82 break;
84 case DLL_PROCESS_DETACH:
85 break;
87 return TRUE;
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 )
102 WCHAR *dst = NULL;
103 if (src)
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 );
109 dst[dst_len] = 0;
112 return dst;
115 static inline char *strdupWA( const WCHAR *src )
117 char *dst = NULL;
118 if (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 );
123 return dst;
126 static struct pac_script
128 WCHAR *text;
129 } 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 );
145 return TRUE;
148 static WCHAR *load_script( const char *filename )
150 HANDLE handle;
151 DWORD size, bytes_read;
152 char *buffer;
153 int len;
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 );
166 script[len] = 0;
168 done:
169 CloseHandle( handle );
170 heap_free( buffer );
171 return script;
174 /******************************************************************
175 * InternetInitializeAutoProxyDll (jsproxy.@)
177 BOOL WINAPI JSPROXY_InternetInitializeAutoProxyDll( DWORD version, LPSTR tmpfile, LPSTR mime,
178 AutoProxyHelperFunctions *callbacks,
179 LPAUTO_PROXY_SCRIPT_BUFFER buffer )
181 BOOL ret = FALSE;
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)
191 heap_free( global_script->text );
192 if( (global_script->text = strdupAW( buffer->lpszScriptBuffer, buffer->dwScriptBufferSize ))) ret = TRUE;
194 else
196 heap_free( global_script->text );
197 if ((global_script->text = load_script( tmpfile ))) ret = TRUE;
200 LeaveCriticalSection( &cs_jsproxy );
201 return ret;
204 static HRESULT WINAPI dispex_QueryInterface(
205 IDispatchEx *iface, REFIID riid, void **ppv )
207 *ppv = NULL;
209 if (IsEqualGUID( riid, &IID_IUnknown ) ||
210 IsEqualGUID( riid, &IID_IDispatch ) ||
211 IsEqualGUID( riid, &IID_IDispatchEx ))
212 *ppv = iface;
213 else
214 return E_NOINTERFACE;
216 return S_OK;
219 static ULONG WINAPI dispex_AddRef(
220 IDispatchEx *iface )
222 return 2;
225 static ULONG WINAPI dispex_Release(
226 IDispatchEx *iface )
228 return 1;
231 static HRESULT WINAPI dispex_GetTypeInfoCount(
232 IDispatchEx *iface, UINT *info )
234 return E_NOTIMPL;
237 static HRESULT WINAPI dispex_GetTypeInfo(
238 IDispatchEx *iface, UINT info, LCID lcid, ITypeInfo **type_info )
240 return E_NOTIMPL;
243 static HRESULT WINAPI dispex_GetIDsOfNames(
244 IDispatchEx *iface, REFIID riid, LPOLESTR *names, UINT count, LCID lcid, DISPID *id )
246 return E_NOTIMPL;
249 static HRESULT WINAPI dispex_Invoke(
250 IDispatchEx *iface, DISPID member, REFIID riid, LCID lcid, WORD flags,
251 DISPPARAMS *params, VARIANT *result, EXCEPINFO *excep, UINT *err )
253 return E_NOTIMPL;
256 static HRESULT WINAPI dispex_DeleteMemberByName(
257 IDispatchEx *iface, BSTR name, DWORD flags )
259 return E_NOTIMPL;
262 static HRESULT WINAPI dispex_DeleteMemberByDispID(
263 IDispatchEx *iface, DISPID id )
265 return E_NOTIMPL;
268 static HRESULT WINAPI dispex_GetMemberProperties(
269 IDispatchEx *iface, DISPID id, DWORD flags_fetch, DWORD *flags )
271 return E_NOTIMPL;
274 static HRESULT WINAPI dispex_GetMemberName(
275 IDispatchEx *iface, DISPID id, BSTR *name )
277 return E_NOTIMPL;
280 static HRESULT WINAPI dispex_GetNextDispID(
281 IDispatchEx *iface, DWORD flags, DISPID id, DISPID *next )
283 return E_NOTIMPL;
286 static HRESULT WINAPI dispex_GetNameSpaceParent(
287 IDispatchEx *iface, IUnknown **unk )
289 return E_NOTIMPL;
292 #define DISPID_GLOBAL_DNSRESOLVE 0x1000
294 static HRESULT WINAPI dispex_GetDispID(
295 IDispatchEx *iface, BSTR name, DWORD flags, DISPID *id )
297 if (!strcmpW( name, dns_resolveW ))
299 *id = DISPID_GLOBAL_DNSRESOLVE;
300 return S_OK;
302 return DISP_E_UNKNOWNNAME;
305 static char *get_computer_name( COMPUTER_NAME_FORMAT format )
307 char *ret;
308 DWORD size = 0;
310 GetComputerNameExA( format, NULL, &size );
311 if (GetLastError() != ERROR_MORE_DATA) return NULL;
312 if (!(ret = heap_alloc( size ))) return NULL;
313 if (!GetComputerNameExA( format, ret, &size ))
315 heap_free( ret );
316 return NULL;
318 return ret;
321 static void printf_addr( const WCHAR *fmt, WCHAR *buf, struct sockaddr_in *addr )
323 sprintfW( buf, fmt,
324 (unsigned int)(ntohl( addr->sin_addr.s_addr ) >> 24 & 0xff),
325 (unsigned int)(ntohl( addr->sin_addr.s_addr ) >> 16 & 0xff),
326 (unsigned int)(ntohl( addr->sin_addr.s_addr ) >> 8 & 0xff),
327 (unsigned int)(ntohl( addr->sin_addr.s_addr ) & 0xff) );
330 static HRESULT dns_resolve( const WCHAR *hostname, VARIANT *result )
332 #ifdef HAVE_GETADDRINFO
333 static const WCHAR fmtW[] = {'%','u','.','%','u','.','%','u','.','%','u',0};
334 WCHAR addr[16];
335 struct addrinfo *ai, *elem;
336 char *hostnameA;
337 int res;
339 if (hostname[0])
340 hostnameA = strdupWA( hostname );
341 else
342 hostnameA = get_computer_name( ComputerNamePhysicalDnsFullyQualified );
344 if (!hostnameA) return E_OUTOFMEMORY;
345 res = getaddrinfo( hostnameA, NULL, NULL, &ai );
346 heap_free( hostnameA );
347 if (res) return S_FALSE;
349 elem = ai;
350 while (elem && elem->ai_family != AF_INET) elem = elem->ai_next;
351 if (!elem)
353 freeaddrinfo( ai );
354 return S_FALSE;
356 printf_addr( fmtW, addr, (struct sockaddr_in *)elem->ai_addr );
357 freeaddrinfo( ai );
358 V_VT( result ) = VT_BSTR;
359 V_BSTR( result ) = SysAllocString( addr );
360 return S_OK;
361 #else
362 FIXME("getaddrinfo not found at build time\n");
363 return S_FALSE;
364 #endif
367 static HRESULT WINAPI dispex_InvokeEx(
368 IDispatchEx *iface, DISPID id, LCID lcid, WORD flags, DISPPARAMS *params,
369 VARIANT *result, EXCEPINFO *exep, IServiceProvider *caller )
371 if (id == DISPID_GLOBAL_DNSRESOLVE)
373 if (params->cArgs != 1) return DISP_E_BADPARAMCOUNT;
374 if (V_VT(&params->rgvarg[0]) != VT_BSTR) return DISP_E_BADVARTYPE;
375 return dns_resolve( V_BSTR(&params->rgvarg[0]), result );
377 return DISP_E_MEMBERNOTFOUND;
380 static const IDispatchExVtbl dispex_vtbl =
382 dispex_QueryInterface,
383 dispex_AddRef,
384 dispex_Release,
385 dispex_GetTypeInfoCount,
386 dispex_GetTypeInfo,
387 dispex_GetIDsOfNames,
388 dispex_Invoke,
389 dispex_GetDispID,
390 dispex_InvokeEx,
391 dispex_DeleteMemberByName,
392 dispex_DeleteMemberByDispID,
393 dispex_GetMemberProperties,
394 dispex_GetMemberName,
395 dispex_GetNextDispID,
396 dispex_GetNameSpaceParent
399 static IDispatchEx global_dispex = { &dispex_vtbl };
401 static HRESULT WINAPI site_QueryInterface(
402 IActiveScriptSite *iface, REFIID riid, void **ppv )
404 *ppv = NULL;
406 if (IsEqualGUID( &IID_IUnknown, riid ))
407 *ppv = iface;
408 else if (IsEqualGUID( &IID_IActiveScriptSite, riid ))
409 *ppv = iface;
410 else
411 return E_NOINTERFACE;
413 IUnknown_AddRef( (IUnknown *)*ppv );
414 return S_OK;
417 static ULONG WINAPI site_AddRef(
418 IActiveScriptSite *iface )
420 return 2;
423 static ULONG WINAPI site_Release(
424 IActiveScriptSite *iface )
426 return 1;
429 static HRESULT WINAPI site_GetLCID(
430 IActiveScriptSite *iface, LCID *lcid )
432 return E_NOTIMPL;
435 static HRESULT WINAPI site_GetItemInfo(
436 IActiveScriptSite *iface, LPCOLESTR name, DWORD mask,
437 IUnknown **item, ITypeInfo **type_info )
439 if (!strcmpW( name, global_funcsW ) && mask == SCRIPTINFO_IUNKNOWN)
441 *item = (IUnknown *)&global_dispex;
442 return S_OK;
444 return E_NOTIMPL;
447 static HRESULT WINAPI site_GetDocVersionString(
448 IActiveScriptSite *iface, BSTR *version )
450 return E_NOTIMPL;
453 static HRESULT WINAPI site_OnScriptTerminate(
454 IActiveScriptSite *iface, const VARIANT *result, const EXCEPINFO *info )
456 return E_NOTIMPL;
459 static HRESULT WINAPI site_OnStateChange(
460 IActiveScriptSite *iface, SCRIPTSTATE state )
462 return E_NOTIMPL;
465 static HRESULT WINAPI site_OnScriptError(
466 IActiveScriptSite *iface, IActiveScriptError *error )
468 return E_NOTIMPL;
471 static HRESULT WINAPI site_OnEnterScript(
472 IActiveScriptSite *iface )
474 return E_NOTIMPL;
477 static HRESULT WINAPI site_OnLeaveScript(
478 IActiveScriptSite *iface )
480 return E_NOTIMPL;
483 static const IActiveScriptSiteVtbl site_vtbl =
485 site_QueryInterface,
486 site_AddRef,
487 site_Release,
488 site_GetLCID,
489 site_GetItemInfo,
490 site_GetDocVersionString,
491 site_OnScriptTerminate,
492 site_OnStateChange,
493 site_OnScriptError,
494 site_OnEnterScript,
495 site_OnLeaveScript
498 static IActiveScriptSite script_site = { &site_vtbl };
500 static BSTR include_pac_utils( const WCHAR *script )
502 static const WCHAR pacjsW[] = {'p','a','c','.','j','s',0};
503 HMODULE hmod = GetModuleHandleA( "jsproxy.dll" );
504 HRSRC rsrc;
505 DWORD size;
506 const char *data;
507 BSTR ret;
508 int len;
510 if (!(rsrc = FindResourceW( hmod, pacjsW, (LPCWSTR)40 ))) return NULL;
511 size = SizeofResource( hmod, rsrc );
512 data = LoadResource( hmod, rsrc );
514 len = MultiByteToWideChar( CP_ACP, 0, data, size, NULL, 0 );
515 if (!(ret = SysAllocStringLen( NULL, len + strlenW( script ) + 1 ))) return NULL;
516 MultiByteToWideChar( CP_ACP, 0, data, size, ret, len );
517 strcpyW( ret + len, script );
518 return ret;
521 #ifdef _WIN64
522 #define IActiveScriptParse_Release IActiveScriptParse64_Release
523 #define IActiveScriptParse_InitNew IActiveScriptParse64_InitNew
524 #define IActiveScriptParse_ParseScriptText IActiveScriptParse64_ParseScriptText
525 #else
526 #define IActiveScriptParse_Release IActiveScriptParse32_Release
527 #define IActiveScriptParse_InitNew IActiveScriptParse32_InitNew
528 #define IActiveScriptParse_ParseScriptText IActiveScriptParse32_ParseScriptText
529 #endif
531 static BOOL run_script( const WCHAR *script, const WCHAR *url, const WCHAR *hostname, char **result_str, DWORD *result_len )
533 static const WCHAR jscriptW[] = {'J','S','c','r','i','p','t',0};
534 static const WCHAR findproxyW[] = {'F','i','n','d','P','r','o','x','y','F','o','r','U','R','L',0};
535 IActiveScriptParse *parser = NULL;
536 IActiveScript *engine = NULL;
537 IDispatch *dispatch = NULL;
538 BOOL ret = FALSE;
539 CLSID clsid;
540 DISPID dispid;
541 BSTR func = NULL, full_script = NULL;
542 VARIANT args[2], retval;
543 DISPPARAMS params;
544 HRESULT hr, init;
546 init = CoInitialize( NULL );
547 hr = CLSIDFromProgID( jscriptW, &clsid );
548 if (hr != S_OK) goto done;
550 hr = CoCreateInstance( &clsid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
551 &IID_IActiveScript, (void **)&engine );
552 if (hr != S_OK) goto done;
554 hr = IActiveScript_QueryInterface( engine, &IID_IActiveScriptParse, (void **)&parser );
555 if (hr != S_OK) goto done;
557 hr = IActiveScriptParse_InitNew( parser );
558 if (hr != S_OK) goto done;
560 hr = IActiveScript_SetScriptSite( engine, &script_site );
561 if (hr != S_OK) goto done;
563 hr = IActiveScript_AddNamedItem( engine, global_funcsW, SCRIPTITEM_GLOBALMEMBERS );
564 if (hr != S_OK) goto done;
566 if (!(full_script = include_pac_utils( script ))) goto done;
568 hr = IActiveScriptParse_ParseScriptText( parser, full_script, NULL, NULL, NULL, 0, 0, 0, NULL, NULL );
569 if (hr != S_OK) goto done;
571 hr = IActiveScript_SetScriptState( engine, SCRIPTSTATE_STARTED );
572 if (hr != S_OK) goto done;
574 hr = IActiveScript_GetScriptDispatch( engine, NULL, &dispatch );
575 if (hr != S_OK) goto done;
577 if (!(func = SysAllocString( findproxyW ))) goto done;
578 hr = IDispatch_GetIDsOfNames( dispatch, &IID_NULL, &func, 1, LOCALE_SYSTEM_DEFAULT, &dispid );
579 if (hr != S_OK) goto done;
581 V_VT( &args[0] ) = VT_BSTR;
582 V_BSTR( &args[0] ) = SysAllocString( hostname );
583 V_VT( &args[1] ) = VT_BSTR;
584 V_BSTR( &args[1] ) = SysAllocString( url );
586 params.rgvarg = args;
587 params.rgdispidNamedArgs = NULL;
588 params.cArgs = 2;
589 params.cNamedArgs = 0;
590 hr = IDispatch_Invoke( dispatch, dispid, &IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD,
591 &params, &retval, NULL, NULL );
592 VariantClear( &args[0] );
593 VariantClear( &args[1] );
594 if (hr != S_OK)
596 WARN("script failed 0x%08x\n", hr);
597 goto done;
599 if ((*result_str = strdupWA( V_BSTR( &retval ) )))
601 TRACE( "result: %s\n", debugstr_a(*result_str) );
602 *result_len = strlen( *result_str ) + 1;
603 ret = TRUE;
605 VariantClear( &retval );
607 done:
608 SysFreeString( full_script );
609 SysFreeString( func );
610 if (dispatch) IDispatch_Release( dispatch );
611 if (parser) IActiveScriptParse_Release( parser );
612 if (engine) IActiveScript_Release( engine );
613 if (SUCCEEDED( init )) CoUninitialize();
614 return ret;
617 /******************************************************************
618 * InternetGetProxyInfo (jsproxy.@)
620 BOOL WINAPI InternetGetProxyInfo( LPCSTR url, DWORD len_url, LPCSTR hostname, DWORD len_hostname, LPSTR *proxy,
621 LPDWORD len_proxy )
623 WCHAR *urlW = NULL, *hostnameW = NULL;
624 BOOL ret = FALSE;
626 TRACE( "%s, %u, %s, %u, %p, %p\n", url, len_url, hostname, len_hostname, proxy, len_proxy );
628 EnterCriticalSection( &cs_jsproxy );
630 if (!global_script->text) goto done;
631 if (!(urlW = strdupAW( url, len_url ))) goto done;
632 if (hostname && !(hostnameW = strdupAW( hostname, len_hostname ))) goto done;
634 TRACE( "%s\n", debugstr_w(global_script->text) );
635 ret = run_script( global_script->text, urlW, hostnameW, proxy, len_proxy );
637 done:
638 heap_free( hostnameW );
639 heap_free( urlW );
640 LeaveCriticalSection( &cs_jsproxy );
641 return ret;