wined3d: Remove fbo_entry->d3d_depth_stencil.
[wine.git] / dlls / jsproxy / main.c
blob0399173905b666eb0214283e6b139c3149f93749
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 (global_script->text)
191 LeaveCriticalSection( &cs_jsproxy );
192 return FALSE;
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 );
199 return ret;
202 static HRESULT WINAPI dispex_QueryInterface(
203 IDispatchEx *iface, REFIID riid, void **ppv )
205 *ppv = NULL;
207 if (IsEqualGUID( riid, &IID_IUnknown ) ||
208 IsEqualGUID( riid, &IID_IDispatch ) ||
209 IsEqualGUID( riid, &IID_IDispatchEx ))
210 *ppv = iface;
211 else
212 return E_NOINTERFACE;
214 return S_OK;
217 static ULONG WINAPI dispex_AddRef(
218 IDispatchEx *iface )
220 return 2;
223 static ULONG WINAPI dispex_Release(
224 IDispatchEx *iface )
226 return 1;
229 static HRESULT WINAPI dispex_GetTypeInfoCount(
230 IDispatchEx *iface, UINT *info )
232 return E_NOTIMPL;
235 static HRESULT WINAPI dispex_GetTypeInfo(
236 IDispatchEx *iface, UINT info, LCID lcid, ITypeInfo **type_info )
238 return E_NOTIMPL;
241 static HRESULT WINAPI dispex_GetIDsOfNames(
242 IDispatchEx *iface, REFIID riid, LPOLESTR *names, UINT count, LCID lcid, DISPID *id )
244 return E_NOTIMPL;
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 )
251 return E_NOTIMPL;
254 static HRESULT WINAPI dispex_DeleteMemberByName(
255 IDispatchEx *iface, BSTR name, DWORD flags )
257 return E_NOTIMPL;
260 static HRESULT WINAPI dispex_DeleteMemberByDispID(
261 IDispatchEx *iface, DISPID id )
263 return E_NOTIMPL;
266 static HRESULT WINAPI dispex_GetMemberProperties(
267 IDispatchEx *iface, DISPID id, DWORD flags_fetch, DWORD *flags )
269 return E_NOTIMPL;
272 static HRESULT WINAPI dispex_GetMemberName(
273 IDispatchEx *iface, DISPID id, BSTR *name )
275 return E_NOTIMPL;
278 static HRESULT WINAPI dispex_GetNextDispID(
279 IDispatchEx *iface, DWORD flags, DISPID id, DISPID *next )
281 return E_NOTIMPL;
284 static HRESULT WINAPI dispex_GetNameSpaceParent(
285 IDispatchEx *iface, IUnknown **unk )
287 return E_NOTIMPL;
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;
298 return S_OK;
300 return DISP_E_UNKNOWNNAME;
303 static char *get_computer_name( COMPUTER_NAME_FORMAT format )
305 char *ret;
306 DWORD size = 0;
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 ))
313 heap_free( ret );
314 return NULL;
316 return ret;
319 static void printf_addr( const WCHAR *fmt, WCHAR *buf, struct sockaddr_in *addr )
321 sprintfW( buf, fmt,
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};
332 WCHAR addr[16];
333 struct addrinfo *ai, *elem;
334 char *hostnameA;
335 int res;
337 if (hostname[0])
338 hostnameA = strdupWA( hostname );
339 else
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;
347 elem = ai;
348 while (elem && elem->ai_family != AF_INET) elem = elem->ai_next;
349 if (!elem)
351 freeaddrinfo( ai );
352 return S_FALSE;
354 printf_addr( fmtW, addr, (struct sockaddr_in *)elem->ai_addr );
355 freeaddrinfo( ai );
356 V_VT( result ) = VT_BSTR;
357 V_BSTR( result ) = SysAllocString( addr );
358 return S_OK;
359 #else
360 FIXME("getaddrinfo not found at build time\n");
361 return S_FALSE;
362 #endif
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(&params->rgvarg[0]) != VT_BSTR) return DISP_E_BADVARTYPE;
373 return dns_resolve( V_BSTR(&params->rgvarg[0]), result );
375 return DISP_E_MEMBERNOTFOUND;
378 static const IDispatchExVtbl dispex_vtbl =
380 dispex_QueryInterface,
381 dispex_AddRef,
382 dispex_Release,
383 dispex_GetTypeInfoCount,
384 dispex_GetTypeInfo,
385 dispex_GetIDsOfNames,
386 dispex_Invoke,
387 dispex_GetDispID,
388 dispex_InvokeEx,
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 )
402 *ppv = NULL;
404 if (IsEqualGUID( &IID_IUnknown, riid ))
405 *ppv = iface;
406 else if (IsEqualGUID( &IID_IActiveScriptSite, riid ))
407 *ppv = iface;
408 else
409 return E_NOINTERFACE;
411 IUnknown_AddRef( (IUnknown *)*ppv );
412 return S_OK;
415 static ULONG WINAPI site_AddRef(
416 IActiveScriptSite *iface )
418 return 2;
421 static ULONG WINAPI site_Release(
422 IActiveScriptSite *iface )
424 return 1;
427 static HRESULT WINAPI site_GetLCID(
428 IActiveScriptSite *iface, LCID *lcid )
430 return E_NOTIMPL;
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;
440 return S_OK;
442 return E_NOTIMPL;
445 static HRESULT WINAPI site_GetDocVersionString(
446 IActiveScriptSite *iface, BSTR *version )
448 return E_NOTIMPL;
451 static HRESULT WINAPI site_OnScriptTerminate(
452 IActiveScriptSite *iface, const VARIANT *result, const EXCEPINFO *info )
454 return E_NOTIMPL;
457 static HRESULT WINAPI site_OnStateChange(
458 IActiveScriptSite *iface, SCRIPTSTATE state )
460 return E_NOTIMPL;
463 static HRESULT WINAPI site_OnScriptError(
464 IActiveScriptSite *iface, IActiveScriptError *error )
466 return E_NOTIMPL;
469 static HRESULT WINAPI site_OnEnterScript(
470 IActiveScriptSite *iface )
472 return E_NOTIMPL;
475 static HRESULT WINAPI site_OnLeaveScript(
476 IActiveScriptSite *iface )
478 return E_NOTIMPL;
481 static const IActiveScriptSiteVtbl site_vtbl =
483 site_QueryInterface,
484 site_AddRef,
485 site_Release,
486 site_GetLCID,
487 site_GetItemInfo,
488 site_GetDocVersionString,
489 site_OnScriptTerminate,
490 site_OnStateChange,
491 site_OnScriptError,
492 site_OnEnterScript,
493 site_OnLeaveScript
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" );
502 HRSRC rsrc;
503 DWORD size;
504 const char *data;
505 BSTR ret;
506 int len;
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 );
516 return ret;
519 #ifdef _WIN64
520 #define IActiveScriptParse_Release IActiveScriptParse64_Release
521 #define IActiveScriptParse_InitNew IActiveScriptParse64_InitNew
522 #define IActiveScriptParse_ParseScriptText IActiveScriptParse64_ParseScriptText
523 #else
524 #define IActiveScriptParse_Release IActiveScriptParse32_Release
525 #define IActiveScriptParse_InitNew IActiveScriptParse32_InitNew
526 #define IActiveScriptParse_ParseScriptText IActiveScriptParse32_ParseScriptText
527 #endif
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;
536 BOOL ret = FALSE;
537 CLSID clsid;
538 DISPID dispid;
539 BSTR func = NULL, full_script = NULL;
540 VARIANT args[2], retval;
541 DISPPARAMS params;
542 HRESULT hr, init;
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;
586 params.cArgs = 2;
587 params.cNamedArgs = 0;
588 hr = IDispatch_Invoke( dispatch, dispid, &IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD,
589 &params, &retval, NULL, NULL );
590 VariantClear( &args[0] );
591 VariantClear( &args[1] );
592 if (hr != S_OK)
594 WARN("script failed 0x%08x\n", hr);
595 goto done;
597 if ((*result_str = strdupWA( V_BSTR( &retval ) )))
599 TRACE( "result: %s\n", debugstr_a(*result_str) );
600 *result_len = strlen( *result_str ) + 1;
601 ret = TRUE;
603 VariantClear( &retval );
605 done:
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();
612 return ret;
615 /******************************************************************
616 * InternetGetProxyInfo (jsproxy.@)
618 BOOL WINAPI InternetGetProxyInfo( LPCSTR url, DWORD len_url, LPCSTR hostname, DWORD len_hostname, LPSTR *proxy,
619 LPDWORD len_proxy )
621 WCHAR *urlW = NULL, *hostnameW = NULL;
622 BOOL ret = FALSE;
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 );
635 done:
636 heap_free( hostnameW );
637 heap_free( urlW );
638 LeaveCriticalSection( &cs_jsproxy );
639 return ret;