Esempio n. 1
0
        private static void InterruptCallback(TimerThread.Timer timer, int timeNoticed, object context)
        {
            AutoWebProxyScriptWrapper pThis = (AutoWebProxyScriptWrapper)context;

            GlobalLog.Print("AutoWebProxyScriptWrapper#" + ValidationHelper.HashString(pThis) + "::InterruptCallback()");

            if (!object.ReferenceEquals(timer, pThis.activeTimer))
            {
                GlobalLog.Print("AutoWebProxyScriptWrapper#" + ValidationHelper.HashString(pThis) + "::InterruptCallback() Spurious - returning.");
                return;
            }

            EXCEPINFO exceptionInfo;

            try
            {
                pThis.jscript.InterruptScriptThread(pThis.interruptThreadId, out exceptionInfo, 0);
            }
            catch (Exception ex)
            {
                if (NclUtilities.IsFatal(ex))
                {
                    throw;
                }
                GlobalLog.Print("AutoWebProxyScriptWrapper#" + ValidationHelper.HashString(pThis) + "::InterruptCallback() InterruptScriptThread() threw:" + ValidationHelper.ToString(ex));
            }
            catch {
                GlobalLog.Print("AutoWebProxyScriptWrapper#" + ValidationHelper.HashString(pThis) + "::InterruptCallback() InterruptScriptThread() threw: Non-CLS Compliant Exception");
            }
        }
        void UpdateScriptInstance(AutoWebProxyScriptWrapper newScriptInstance)
        {
            GlobalLog.Print("AutoWebProxyScriptEngine#" + ValidationHelper.HashString(this) + "::UpdateScriptInstance() updating scriptInstance#" + ValidationHelper.HashString(scriptInstance) + " to newScriptInstance#" + ValidationHelper.HashString(newScriptInstance));

            if (scriptInstance == newScriptInstance)
            {
                return;
            }

            if (scriptInstance != null)
            {
                GlobalLog.Print("AutoWebProxyScriptEngine#" + ValidationHelper.HashString(this) + "::UpdateScriptInstance() Closing engine.");
                scriptInstance.Close();
            }
            scriptInstance = newScriptInstance;
        }
        // Downloads and compiles the script from a given Uri.
        // This code can be called by config for a downloaded control, we need to assert.
        // This code is called holding the lock.
        private AutoWebProxyState DownloadAndCompile(Uri location)
        {
            GlobalLog.Print("NetWebProxyFinder#" + ValidationHelper.HashString(this) + "::DownloadAndCompile() location:" + ValidationHelper.ToString(location));
            AutoWebProxyState newState = AutoWebProxyState.DownloadFailure;
            WebResponse       response = null;

            TimerThread.Timer         timer             = null;
            AutoWebProxyScriptWrapper newScriptInstance = null;

            // Can't assert this in declarative form (DCR?). This Assert() is needed to be able to create the request to download the proxy script.
            ExceptionHelper.WebPermissionUnrestricted.Assert();
            try
            {
                lock (lockObject)
                {
                    if (aborted)
                    {
                        throw new WebException(NetRes.GetWebStatusString("net_requestaborted",
                                                                         WebExceptionStatus.RequestCanceled), WebExceptionStatus.RequestCanceled);
                    }

                    request = WebRequest.Create(location);
                }

                request.Timeout             = Timeout.Infinite;
                request.CachePolicy         = new RequestCachePolicy(RequestCacheLevel.Default);
                request.ConnectionGroupName = "__WebProxyScript";

                // We have an opportunity here, if caching is disabled AppDomain-wide, to override it with a
                // custom, trivial cache-provider to get a similar semantic.
                //
                // We also want to have a backup caching key in the case when IE has locked an expired script response
                //
                if (request.CacheProtocol != null)
                {
                    GlobalLog.Print("NetWebProxyFinder#" + ValidationHelper.HashString(this) + "::DownloadAndCompile() Using backup caching.");
                    request.CacheProtocol = new RequestCacheProtocol(backupCache, request.CacheProtocol.Validator);
                }

                HttpWebRequest httpWebRequest = request as HttpWebRequest;
                if (httpWebRequest != null)
                {
                    httpWebRequest.Accept    = "*/*";
                    httpWebRequest.UserAgent = this.GetType().FullName + "/" + Environment.Version;
                    httpWebRequest.KeepAlive = false;
                    httpWebRequest.Pipelined = false;
                    httpWebRequest.InternalConnectionGroup = true;
                }
                else
                {
                    FtpWebRequest ftpWebRequest = request as FtpWebRequest;
                    if (ftpWebRequest != null)
                    {
                        ftpWebRequest.KeepAlive = false;
                    }
                }

                // Use no proxy, default cache - initiate the download.
                request.Proxy       = null;
                request.Credentials = Engine.Credentials;

                // Use our own timeout timer so that it can encompass the whole request, not just the headers.
                if (timerQueue == null)
                {
                    timerQueue = TimerThread.GetOrCreateQueue(SettingsSectionInternal.Section.DownloadTimeout);
                }
                timer    = timerQueue.CreateTimer(timerCallback, request);
                response = request.GetResponse();

                // Check Last Modified.
                DateTime        lastModified = DateTime.MinValue;
                HttpWebResponse httpResponse = response as HttpWebResponse;
                if (httpResponse != null)
                {
                    lastModified = httpResponse.LastModified;
                }
                else
                {
                    FtpWebResponse ftpResponse = response as FtpWebResponse;
                    if (ftpResponse != null)
                    {
                        lastModified = ftpResponse.LastModified;
                    }
                }
                GlobalLog.Print("NetWebProxyFinder#" + ValidationHelper.HashString(this) + "::DownloadAndCompile() lastModified:" + lastModified.ToString() + " (script):" + (scriptInstance == null ? "(null)" : scriptInstance.LastModified.ToString()));
                if (scriptInstance != null && lastModified != DateTime.MinValue && scriptInstance.LastModified == lastModified)
                {
                    newScriptInstance = scriptInstance;
                    newState          = AutoWebProxyState.Completed;
                }
                else
                {
                    string scriptBody   = null;
                    byte[] scriptBuffer = null;
                    using (Stream responseStream = response.GetResponseStream())
                    {
                        SingleItemRequestCache.ReadOnlyStream ros = responseStream as SingleItemRequestCache.ReadOnlyStream;
                        if (ros != null)
                        {
                            scriptBuffer = ros.Buffer;
                        }
                        if (scriptInstance != null && scriptBuffer != null && scriptBuffer == scriptInstance.Buffer)
                        {
                            scriptInstance.LastModified = lastModified;
                            newScriptInstance           = scriptInstance;
                            newState = AutoWebProxyState.Completed;
                            GlobalLog.Print("NetWebProxyFinder#" + ValidationHelper.HashString(this) + "::DownloadAndCompile() Buffer matched - reusing Engine.");
                        }
                        else
                        {
                            using (StreamReader streamReader = new StreamReader(responseStream))
                            {
                                scriptBody = streamReader.ReadToEnd();
                            }
                        }
                    }

                    WebResponse tempResponse = response;
                    response = null;
                    tempResponse.Close();
                    timer.Cancel();
                    timer = null;

                    if (newState != AutoWebProxyState.Completed)
                    {
                        GlobalLog.Print("NetWebProxyFinder#" + ValidationHelper.HashString(this) + "::DownloadAndCompile() IsFromCache:" + tempResponse.IsFromCache.ToString() + " scriptInstance:" + ValidationHelper.HashString(scriptInstance));
                        if (scriptInstance != null && scriptBody == scriptInstance.ScriptBody)
                        {
                            GlobalLog.Print("NetWebProxyFinder#" + ValidationHelper.HashString(this) + "::DownloadAndCompile() Script matched - using existing Engine.");
                            scriptInstance.LastModified = lastModified;
                            if (scriptBuffer != null)
                            {
                                scriptInstance.Buffer = scriptBuffer;
                            }
                            newScriptInstance = scriptInstance;
                            newState          = AutoWebProxyState.Completed;
                        }
                        else
                        {
                            GlobalLog.Print("NetWebProxyFinder#" + ValidationHelper.HashString(this) + "::DownloadAndCompile() Creating AutoWebProxyScriptWrapper.");
                            newScriptInstance = new AutoWebProxyScriptWrapper();
                            newScriptInstance.LastModified = lastModified;

                            if (newScriptInstance.Compile(location, scriptBody, scriptBuffer))
                            {
                                newState = AutoWebProxyState.Completed;
                            }
                            else
                            {
                                newState = AutoWebProxyState.CompilationFailure;
                            }
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                if (Logging.On)
                {
                    Logging.PrintWarning(Logging.Web, SR.GetString(SR.net_log_proxy_script_download_compile_error, exception));
                }
                GlobalLog.Print("NetWebProxyFinder#" + ValidationHelper.HashString(this) + "::DownloadAndCompile() Download() threw:" + ValidationHelper.ToString(exception));
            }
            finally
            {
                if (timer != null)
                {
                    timer.Cancel();
                }

                //
                try
                {
                    if (response != null)
                    {
                        response.Close();
                    }
                }
                finally
                {
                    WebPermission.RevertAssert();

                    // The request is not needed anymore. Set it to null, so if Abort() gets called,
                    // after this point, it will result in a no-op.
                    request = null;
                }
            }

            if ((newState == AutoWebProxyState.Completed) && (scriptInstance != newScriptInstance))
            {
                if (scriptInstance != null)
                {
                    scriptInstance.Close();
                }

                scriptInstance = newScriptInstance;
            }

            GlobalLog.Print("NetWebProxyFinder#" + ValidationHelper.HashString(this) + "::DownloadAndCompile() retuning newState:" + ValidationHelper.ToString(newState));
            return(newState);
        }
        // Ensures that (if state is AutoWebProxyState.CompilationSuccess) there is an engine available to execute script.
        // Figures out the script location (might discover if needed).
        // Calls DownloadAndCompile().
        private void EnsureEngineAvailable()
        {
            GlobalLog.Enter("NetWebProxyFinder#" + ValidationHelper.HashString(this) + "::EnsureEngineAvailable");

            if (State == AutoWebProxyState.Uninitialized || engineScriptLocation == null)
            {
#if !FEATURE_PAL
                if (Engine.AutomaticallyDetectSettings)
                {
                    GlobalLog.Print("NetWebProxyFinder#" + ValidationHelper.HashString(this) + "::EnsureEngineAvailable() Attempting auto-detection.");
                    DetectScriptLocation();
                    if (scriptLocation != null)
                    {
                        //
                        // Successfully detected or user has flipped the automaticallyDetectSettings bit.
                        // Attempt a non conclusive DownloadAndCompile() so we can fallback
                        //
                        GlobalLog.Print("NetWebProxyFinder#" + ValidationHelper.HashString(this) + "::EnsureEngineAvailable() discovered:" + ValidationHelper.ToString(scriptLocation) + " engineScriptLocation:" + ValidationHelper.ToString(engineScriptLocation));
                        if (scriptLocation.Equals(engineScriptLocation))
                        {
                            State = AutoWebProxyState.Completed;
                            GlobalLog.Leave("NetWebProxyFinder#" + ValidationHelper.HashString(this) + "::EnsureEngineAvailable", ValidationHelper.ToString(State));
                            return;
                        }
                        AutoWebProxyState newState = DownloadAndCompile(scriptLocation);
                        if (newState == AutoWebProxyState.Completed)
                        {
                            State = AutoWebProxyState.Completed;
                            engineScriptLocation = scriptLocation;
                            GlobalLog.Leave("NetWebProxyFinder#" + ValidationHelper.HashString(this) + "::EnsureEngineAvailable", ValidationHelper.ToString(State));
                            return;
                        }
                    }
                }
#endif // !FEATURE_PAL

                // Either Auto-Detect wasn't enabled or something failed with it.  Try the manual script location.
                if ((Engine.AutomaticConfigurationScript != null) && !aborted)
                {
                    GlobalLog.Print("NetWebProxyFinder#" + ValidationHelper.HashString(this) + "::EnsureEngineAvailable() using automaticConfigurationScript:" + ValidationHelper.ToString(Engine.AutomaticConfigurationScript) + " engineScriptLocation:" + ValidationHelper.ToString(engineScriptLocation));
                    if (Engine.AutomaticConfigurationScript.Equals(engineScriptLocation))
                    {
                        State = AutoWebProxyState.Completed;
                        GlobalLog.Leave("NetWebProxyFinder#" + ValidationHelper.HashString(this) + "::EnsureEngineAvailable", ValidationHelper.ToString(State));
                        return;
                    }
                    State = DownloadAndCompile(Engine.AutomaticConfigurationScript);
                    if (State == AutoWebProxyState.Completed)
                    {
                        engineScriptLocation = Engine.AutomaticConfigurationScript;
                        GlobalLog.Leave("NetWebProxyFinder#" + ValidationHelper.HashString(this) + "::EnsureEngineAvailable", ValidationHelper.ToString(State));
                        return;
                    }
                }
            }
            else
            {
                // We always want to call DownloadAndCompile to check the expiration.
                GlobalLog.Print("NetWebProxyFinder#" + ValidationHelper.HashString(this) + "::EnsureEngineAvailable() State:" + State + " engineScriptLocation:" + ValidationHelper.ToString(engineScriptLocation));
                State = DownloadAndCompile(engineScriptLocation);
                if (State == AutoWebProxyState.Completed)
                {
                    GlobalLog.Leave("NetWebProxyFinder#" + ValidationHelper.HashString(this) + "::EnsureEngineAvailable", ValidationHelper.ToString(State));
                    return;
                }

                // There's still an opportunity to fail over to the automaticConfigurationScript.
                if (!engineScriptLocation.Equals(Engine.AutomaticConfigurationScript) && !aborted)
                {
                    GlobalLog.Print("NetWebProxyFinder#" + ValidationHelper.HashString(this) + "::EnsureEngineAvailable() Update failed.  Falling back to automaticConfigurationScript:" + ValidationHelper.ToString(Engine.AutomaticConfigurationScript));
                    State = DownloadAndCompile(Engine.AutomaticConfigurationScript);
                    if (State == AutoWebProxyState.Completed)
                    {
                        engineScriptLocation = Engine.AutomaticConfigurationScript;
                        GlobalLog.Leave("NetWebProxyFinder#" + ValidationHelper.HashString(this) + "::EnsureEngineAvailable", ValidationHelper.ToString(State));
                        return;
                    }
                }
            }

            // Everything failed.  Set this instance to mostly-dead.  It will wake up again if there's a reg/connectoid change.
            GlobalLog.Print("NetWebProxyFinder#" + ValidationHelper.HashString(this) + "::EnsureEngineAvailable() All failed.");
            State = AutoWebProxyState.DiscoveryFailure;

            if (scriptInstance != null)
            {
                scriptInstance.Close();
                scriptInstance = null;
            }

            engineScriptLocation = null;

            GlobalLog.Leave("NetWebProxyFinder#" + ValidationHelper.HashString(this) + "::EnsureEngineAvailable", ValidationHelper.ToString(State));
        }
        /// <devdoc>
        ///     <para>
        ///         Downloads and compiles the script from a given Uri.
        ///         This code can be called by config for a downloaded control, we need to assert.
        ///         This code is called holding the lock.
        ///     </para>
        /// </devdoc>
        private AutoWebProxyState DownloadAndCompile(Uri location, out AutoWebProxyScriptWrapper newScriptInstance, ref int syncStatus)
        {
            GlobalLog.Print("AutoWebProxyScriptEngine#" + ValidationHelper.HashString(this) + "::DownloadAndCompile() location:" + ValidationHelper.ToString(location));
            AutoWebProxyState newState = AutoWebProxyState.DownloadFailure;
            WebResponse response = null;
            TimerThread.Timer timer = null;
            newScriptInstance = null;

            // Can't assert this in declarative form (DCR?). This Assert() is needed to be able to create the request to download the proxy script.
            ExceptionHelper.WebPermissionUnrestricted.Assert();
            try {
                // here we have a reentrance issue due to config load.
                WebRequest request = WebRequest.Create(location);
                request.Timeout = Timeout.Infinite;
                request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.Default);
                request.ConnectionGroupName = "__WebProxyScript";
                
                // We have an opportunity here, if caching is disabled AppDomain-wide, to override it with a
                // custom, trivial cache-provider to get a similar semantic.
                //
                // We also want to have a backup caching key in the case when IE has locked an expired script response
                //
                if (request.CacheProtocol != null)
                {
                    GlobalLog.Print("AutoWebProxyScriptEngine#" + ValidationHelper.HashString(this) + "::DownloadAndCompile() Using backup caching.");
                    request.CacheProtocol = new RequestCacheProtocol(backupCache, request.CacheProtocol.Validator);
                }

                HttpWebRequest httpWebRequest = request as HttpWebRequest;
                if (httpWebRequest!=null)
                {
                    httpWebRequest.Accept = "*/*";
                    httpWebRequest.UserAgent = this.GetType().FullName + "/" + Environment.Version;
                    httpWebRequest.KeepAlive = false;
                    httpWebRequest.Pipelined = false;
                    httpWebRequest.InternalConnectionGroup = true;
                }
                else
                {
                    FtpWebRequest ftpWebRequest = request as FtpWebRequest;
                    if (ftpWebRequest!=null)
                    {
                        ftpWebRequest.KeepAlive = false;
                    }
                }

                // Use no proxy, default cache - initiate the download.
                request.Proxy = null;
                request.Credentials = webProxy.Credentials;

                // Set this up with the abortable lock to abort this too.
                LockRequest(request, ref syncStatus);
                if (syncStatus != SyncStatus.RequestOwner)
                {
                    throw new WebException(NetRes.GetWebStatusString("net_requestaborted", WebExceptionStatus.RequestCanceled), WebExceptionStatus.RequestCanceled);
                }

                // Use our own timeout timer so that it can encompass the whole request, not just the headers.
                if (s_TimerQueue == null)
                {
                    s_TimerQueue = TimerThread.GetOrCreateQueue(SettingsSectionInternal.Section.DownloadTimeout);
                }
                timer = s_TimerQueue.CreateTimer(s_TimerCallback, request);
                response = request.GetResponse();

                // Check Last Modified.
                DateTime lastModified = DateTime.MinValue;
                HttpWebResponse httpResponse = response as HttpWebResponse;
                if (httpResponse != null)
                {
                    lastModified = httpResponse.LastModified;
                }
                else
                {
                    FtpWebResponse ftpResponse = response as FtpWebResponse;
                    if (ftpResponse != null)
                    {
                        lastModified = ftpResponse.LastModified;
                    }
                }
                GlobalLog.Print("AutoWebProxyScriptEngine#" + ValidationHelper.HashString(this) + "::DownloadAndCompile() lastModified:" + lastModified.ToString() + " (script):" + (scriptInstance == null ? "(null)" : scriptInstance.LastModified.ToString()));
                if (scriptInstance != null && lastModified != DateTime.MinValue && scriptInstance.LastModified == lastModified)
                {
                    newScriptInstance = scriptInstance;
                    newState = AutoWebProxyState.CompilationSuccess;
                }
                else
                {
                    string scriptBody = null;
                    byte[] scriptBuffer = null;
                    using (Stream responseStream = response.GetResponseStream())
                    {
                        SingleItemRequestCache.ReadOnlyStream ros = responseStream as SingleItemRequestCache.ReadOnlyStream;
                        if (ros != null)
                        {
                            scriptBuffer = ros.Buffer;
                        }
                        if (scriptInstance != null && scriptBuffer != null && scriptBuffer == scriptInstance.Buffer)
                        {
                            scriptInstance.LastModified = lastModified;
                            newScriptInstance = scriptInstance;
                            newState = AutoWebProxyState.CompilationSuccess;
                            GlobalLog.Print("AutoWebProxyScriptEngine#" + ValidationHelper.HashString(this) + "::DownloadAndCompile() Buffer matched - reusing engine.");
                        }
                        else
                        {
                            using (StreamReader streamReader = new StreamReader(responseStream))
                            {
                                scriptBody = streamReader.ReadToEnd();
                            }
                        }
                    }

                    WebResponse tempResponse = response;
                    response = null;
                    tempResponse.Close();
                    timer.Cancel();
                    timer = null;

                    if (newState != AutoWebProxyState.CompilationSuccess)
                    {
                        newState = AutoWebProxyState.DownloadSuccess;

                        GlobalLog.Print("AutoWebProxyScriptEngine#" + ValidationHelper.HashString(this) + "::DownloadAndCompile() IsFromCache:" + tempResponse.IsFromCache.ToString() + " scriptInstance:" + ValidationHelper.HashString(scriptInstance));
                        if (scriptInstance != null && scriptBody == scriptInstance.ScriptBody)
                        {
                            GlobalLog.Print("AutoWebProxyScriptEngine#" + ValidationHelper.HashString(this) + "::DownloadAndCompile() Script matched - using existing engine.");
                            scriptInstance.LastModified = lastModified;
                            if (scriptBuffer != null)
                            {
                                scriptInstance.Buffer = scriptBuffer;
                            }
                            newScriptInstance = scriptInstance;
                            newState = AutoWebProxyState.CompilationSuccess;
                        }
                        else
                        {
                            GlobalLog.Print("AutoWebProxyScriptEngine#" + ValidationHelper.HashString(this) + "::DownloadAndCompile() Creating AutoWebProxyScriptWrapper.");
                            newScriptInstance = new AutoWebProxyScriptWrapper();
                            newScriptInstance.LastModified = lastModified;
                            newState = newScriptInstance.Compile(location, scriptBody, scriptBuffer);
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                if (NclUtilities.IsFatal(exception)) throw;
                if(Logging.On)Logging.PrintWarning(Logging.Web, SR.GetString(SR.net_log_proxy_script_download_compile_error, exception));
                GlobalLog.Print("AutoWebProxyScriptEngine#" + ValidationHelper.HashString(this) + "::DownloadAndCompile() Download() threw:" + ValidationHelper.ToString(exception));
            }
            finally
            {
                if (timer != null)
                {
                    timer.Cancel();
                }

                //                                                                                 
                try
                {
                    if (response != null)
                    {
                        response.Close();
                    }
                }
                finally
                {
                    WebPermission.RevertAssert();
                }
            }
            if (newState!=AutoWebProxyState.CompilationSuccess) {
                newScriptInstance = null;
            }
            GlobalLog.Print("AutoWebProxyScriptEngine#" + ValidationHelper.HashString(this) + "::DownloadAndCompile() retuning newState:" + ValidationHelper.ToString(newState));
            return newState;
        }
        void UpdateScriptInstance(AutoWebProxyScriptWrapper newScriptInstance) {
            GlobalLog.Print("AutoWebProxyScriptEngine#" + ValidationHelper.HashString(this) + "::UpdateScriptInstance() updating scriptInstance#" + ValidationHelper.HashString(scriptInstance) + " to newScriptInstance#" + ValidationHelper.HashString(newScriptInstance));

            if (scriptInstance == newScriptInstance)
            {
                return;
            }

            if (scriptInstance!=null) {
                GlobalLog.Print("AutoWebProxyScriptEngine#" + ValidationHelper.HashString(this) + "::UpdateScriptInstance() Closing engine.");
                scriptInstance.Close();
            }
            scriptInstance = newScriptInstance;
        }
        // Ensures that (if state is AutoWebProxyState.CompilationSuccess) there is an engine available to execute script.
        // Figures out the script location (might discover if needed).
        // Calls DownloadAndCompile().
        private void EnsureEngineAvailable()
        {
            GlobalLog.Enter("NetWebProxyFinder#" + ValidationHelper.HashString(this) + "::EnsureEngineAvailable");

            if (State == AutoWebProxyState.Uninitialized || engineScriptLocation == null)
            {
#if !FEATURE_PAL
                if (Engine.AutomaticallyDetectSettings)
                {
                    GlobalLog.Print("NetWebProxyFinder#" + ValidationHelper.HashString(this) + "::EnsureEngineAvailable() Attempting auto-detection.");
                    DetectScriptLocation();
                    if (scriptLocation != null)
                    {
                        //
                        // Successfully detected or user has flipped the automaticallyDetectSettings bit.
                        // Attempt a non conclusive DownloadAndCompile() so we can fallback
                        //
                        GlobalLog.Print("NetWebProxyFinder#" + ValidationHelper.HashString(this) + "::EnsureEngineAvailable() discovered:" + ValidationHelper.ToString(scriptLocation) + " engineScriptLocation:" + ValidationHelper.ToString(engineScriptLocation));
                        if (scriptLocation.Equals(engineScriptLocation))
                        {
                            State = AutoWebProxyState.Completed;
                            GlobalLog.Leave("NetWebProxyFinder#" + ValidationHelper.HashString(this) + "::EnsureEngineAvailable", ValidationHelper.ToString(State));
                            return;
                        }
                        AutoWebProxyState newState = DownloadAndCompile(scriptLocation);
                        if (newState == AutoWebProxyState.Completed)
                        {
                            State = AutoWebProxyState.Completed;
                            engineScriptLocation = scriptLocation;
                            GlobalLog.Leave("NetWebProxyFinder#" + ValidationHelper.HashString(this) + "::EnsureEngineAvailable", ValidationHelper.ToString(State));
                            return;
                        }
                    }
                }
#endif // !FEATURE_PAL

                // Either Auto-Detect wasn't enabled or something failed with it.  Try the manual script location.
                if ((Engine.AutomaticConfigurationScript != null) && !aborted)
                {
                    GlobalLog.Print("NetWebProxyFinder#" + ValidationHelper.HashString(this) + "::EnsureEngineAvailable() using automaticConfigurationScript:" + ValidationHelper.ToString(Engine.AutomaticConfigurationScript) + " engineScriptLocation:" + ValidationHelper.ToString(engineScriptLocation));
                    if (Engine.AutomaticConfigurationScript.Equals(engineScriptLocation))
                    {
                        State = AutoWebProxyState.Completed;
                        GlobalLog.Leave("NetWebProxyFinder#" + ValidationHelper.HashString(this) + "::EnsureEngineAvailable", ValidationHelper.ToString(State));
                        return;
                    }
                    State = DownloadAndCompile(Engine.AutomaticConfigurationScript);
                    if (State == AutoWebProxyState.Completed)
                    {
                        engineScriptLocation = Engine.AutomaticConfigurationScript;
                        GlobalLog.Leave("NetWebProxyFinder#" + ValidationHelper.HashString(this) + "::EnsureEngineAvailable", ValidationHelper.ToString(State));
                        return;
                    }
                }
            }
            else
            {
                // We always want to call DownloadAndCompile to check the expiration.
                GlobalLog.Print("NetWebProxyFinder#" + ValidationHelper.HashString(this) + "::EnsureEngineAvailable() State:" + State + " engineScriptLocation:" + ValidationHelper.ToString(engineScriptLocation));
                State = DownloadAndCompile(engineScriptLocation);
                if (State == AutoWebProxyState.Completed)
                {
                    GlobalLog.Leave("NetWebProxyFinder#" + ValidationHelper.HashString(this) + "::EnsureEngineAvailable", ValidationHelper.ToString(State));
                    return;
                }

                // There's still an opportunity to fail over to the automaticConfigurationScript.
                if (!engineScriptLocation.Equals(Engine.AutomaticConfigurationScript) && !aborted)
                {
                    GlobalLog.Print("NetWebProxyFinder#" + ValidationHelper.HashString(this) + "::EnsureEngineAvailable() Update failed.  Falling back to automaticConfigurationScript:" + ValidationHelper.ToString(Engine.AutomaticConfigurationScript));
                    State = DownloadAndCompile(Engine.AutomaticConfigurationScript);
                    if (State == AutoWebProxyState.Completed)
                    {
                        engineScriptLocation = Engine.AutomaticConfigurationScript;
                        GlobalLog.Leave("NetWebProxyFinder#" + ValidationHelper.HashString(this) + "::EnsureEngineAvailable", ValidationHelper.ToString(State));
                        return;
                    }
                }
            }

            // Everything failed.  Set this instance to mostly-dead.  It will wake up again if there's a reg/connectoid change.
            GlobalLog.Print("NetWebProxyFinder#" + ValidationHelper.HashString(this) + "::EnsureEngineAvailable() All failed.");
            State = AutoWebProxyState.DiscoveryFailure;

            if (scriptInstance != null)
            {
                scriptInstance.Close();
                scriptInstance = null;
            }

            engineScriptLocation = null;

            GlobalLog.Leave("NetWebProxyFinder#" + ValidationHelper.HashString(this) + "::EnsureEngineAvailable", ValidationHelper.ToString(State));
        }
Esempio n. 8
0
        private BaseWebProxyFinder.AutoWebProxyState DownloadAndCompile(Uri location)
        {
            BaseWebProxyFinder.AutoWebProxyState downloadFailure = BaseWebProxyFinder.AutoWebProxyState.DownloadFailure;
            WebResponse response = null;

            TimerThread.Timer         timer          = null;
            AutoWebProxyScriptWrapper scriptInstance = null;

            ExceptionHelper.WebPermissionUnrestricted.Assert();
            try
            {
                lock (this.lockObject)
                {
                    if (this.aborted)
                    {
                        throw new WebException(NetRes.GetWebStatusString("net_requestaborted", WebExceptionStatus.RequestCanceled), WebExceptionStatus.RequestCanceled);
                    }
                    this.request = WebRequest.Create(location);
                }
                this.request.Timeout             = -1;
                this.request.CachePolicy         = new RequestCachePolicy(RequestCacheLevel.Default);
                this.request.ConnectionGroupName = "__WebProxyScript";
                if (this.request.CacheProtocol != null)
                {
                    this.request.CacheProtocol = new RequestCacheProtocol(this.backupCache, this.request.CacheProtocol.Validator);
                }
                HttpWebRequest request = this.request as HttpWebRequest;
                if (request != null)
                {
                    request.Accept    = "*/*";
                    request.UserAgent = base.GetType().FullName + "/" + Environment.Version;
                    request.KeepAlive = false;
                    request.Pipelined = false;
                    request.InternalConnectionGroup = true;
                }
                else
                {
                    FtpWebRequest request2 = this.request as FtpWebRequest;
                    if (request2 != null)
                    {
                        request2.KeepAlive = false;
                    }
                }
                this.request.Proxy       = null;
                this.request.Credentials = base.Engine.Credentials;
                if (timerQueue == null)
                {
                    timerQueue = TimerThread.GetOrCreateQueue(SettingsSectionInternal.Section.DownloadTimeout);
                }
                timer    = timerQueue.CreateTimer(timerCallback, this.request);
                response = this.request.GetResponse();
                DateTime        minValue  = DateTime.MinValue;
                HttpWebResponse response2 = response as HttpWebResponse;
                if (response2 != null)
                {
                    minValue = response2.LastModified;
                }
                else
                {
                    FtpWebResponse response3 = response as FtpWebResponse;
                    if (response3 != null)
                    {
                        minValue = response3.LastModified;
                    }
                }
                if (((this.scriptInstance != null) && (minValue != DateTime.MinValue)) && (this.scriptInstance.LastModified == minValue))
                {
                    scriptInstance  = this.scriptInstance;
                    downloadFailure = BaseWebProxyFinder.AutoWebProxyState.Completed;
                }
                else
                {
                    string scriptBody = null;
                    byte[] buffer     = null;
                    using (Stream stream = response.GetResponseStream())
                    {
                        SingleItemRequestCache.ReadOnlyStream stream2 = stream as SingleItemRequestCache.ReadOnlyStream;
                        if (stream2 != null)
                        {
                            buffer = stream2.Buffer;
                        }
                        if (((this.scriptInstance != null) && (buffer != null)) && (buffer == this.scriptInstance.Buffer))
                        {
                            this.scriptInstance.LastModified = minValue;
                            scriptInstance  = this.scriptInstance;
                            downloadFailure = BaseWebProxyFinder.AutoWebProxyState.Completed;
                        }
                        else
                        {
                            using (StreamReader reader = new StreamReader(stream))
                            {
                                scriptBody = reader.ReadToEnd();
                            }
                        }
                    }
                    WebResponse response4 = response;
                    response = null;
                    response4.Close();
                    timer.Cancel();
                    timer = null;
                    if (downloadFailure != BaseWebProxyFinder.AutoWebProxyState.Completed)
                    {
                        if ((this.scriptInstance != null) && (scriptBody == this.scriptInstance.ScriptBody))
                        {
                            this.scriptInstance.LastModified = minValue;
                            if (buffer != null)
                            {
                                this.scriptInstance.Buffer = buffer;
                            }
                            scriptInstance  = this.scriptInstance;
                            downloadFailure = BaseWebProxyFinder.AutoWebProxyState.Completed;
                        }
                        else
                        {
                            scriptInstance = new AutoWebProxyScriptWrapper {
                                LastModified = minValue
                            };
                            if (scriptInstance.Compile(location, scriptBody, buffer))
                            {
                                downloadFailure = BaseWebProxyFinder.AutoWebProxyState.Completed;
                            }
                            else
                            {
                                downloadFailure = BaseWebProxyFinder.AutoWebProxyState.CompilationFailure;
                            }
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                if (Logging.On)
                {
                    Logging.PrintWarning(Logging.Web, SR.GetString("net_log_proxy_script_download_compile_error", new object[] { exception }));
                }
            }
            finally
            {
                if (timer != null)
                {
                    timer.Cancel();
                }
                try
                {
                    if (response != null)
                    {
                        response.Close();
                    }
                }
                finally
                {
                    CodeAccessPermission.RevertAssert();
                    this.request = null;
                }
            }
            if ((downloadFailure == BaseWebProxyFinder.AutoWebProxyState.Completed) && (this.scriptInstance != scriptInstance))
            {
                if (this.scriptInstance != null)
                {
                    this.scriptInstance.Close();
                }
                this.scriptInstance = scriptInstance;
            }
            return(downloadFailure);
        }
Esempio n. 9
0
 private void EnsureEngineAvailable()
 {
     if ((base.State == BaseWebProxyFinder.AutoWebProxyState.Uninitialized) || (this.engineScriptLocation == null))
     {
         if (base.Engine.AutomaticallyDetectSettings)
         {
             this.DetectScriptLocation();
             if (this.scriptLocation != null)
             {
                 if (this.scriptLocation.Equals(this.engineScriptLocation))
                 {
                     base.State = BaseWebProxyFinder.AutoWebProxyState.Completed;
                     return;
                 }
                 if (this.DownloadAndCompile(this.scriptLocation) == BaseWebProxyFinder.AutoWebProxyState.Completed)
                 {
                     base.State = BaseWebProxyFinder.AutoWebProxyState.Completed;
                     this.engineScriptLocation = this.scriptLocation;
                     return;
                 }
             }
         }
         if ((base.Engine.AutomaticConfigurationScript != null) && !this.aborted)
         {
             if (base.Engine.AutomaticConfigurationScript.Equals(this.engineScriptLocation))
             {
                 base.State = BaseWebProxyFinder.AutoWebProxyState.Completed;
                 return;
             }
             base.State = this.DownloadAndCompile(base.Engine.AutomaticConfigurationScript);
             if (base.State == BaseWebProxyFinder.AutoWebProxyState.Completed)
             {
                 this.engineScriptLocation = base.Engine.AutomaticConfigurationScript;
                 return;
             }
         }
     }
     else
     {
         base.State = this.DownloadAndCompile(this.engineScriptLocation);
         if (base.State == BaseWebProxyFinder.AutoWebProxyState.Completed)
         {
             return;
         }
         if (!this.engineScriptLocation.Equals(base.Engine.AutomaticConfigurationScript) && !this.aborted)
         {
             base.State = this.DownloadAndCompile(base.Engine.AutomaticConfigurationScript);
             if (base.State == BaseWebProxyFinder.AutoWebProxyState.Completed)
             {
                 this.engineScriptLocation = base.Engine.AutomaticConfigurationScript;
                 return;
             }
         }
     }
     base.State = BaseWebProxyFinder.AutoWebProxyState.DiscoveryFailure;
     if (this.scriptInstance != null)
     {
         this.scriptInstance.Close();
         this.scriptInstance = null;
     }
     this.engineScriptLocation = null;
 }
 private BaseWebProxyFinder.AutoWebProxyState DownloadAndCompile(Uri location)
 {
     BaseWebProxyFinder.AutoWebProxyState downloadFailure = BaseWebProxyFinder.AutoWebProxyState.DownloadFailure;
     WebResponse response = null;
     TimerThread.Timer timer = null;
     AutoWebProxyScriptWrapper scriptInstance = null;
     ExceptionHelper.WebPermissionUnrestricted.Assert();
     try
     {
         lock (this.lockObject)
         {
             if (this.aborted)
             {
                 throw new WebException(NetRes.GetWebStatusString("net_requestaborted", WebExceptionStatus.RequestCanceled), WebExceptionStatus.RequestCanceled);
             }
             this.request = WebRequest.Create(location);
         }
         this.request.Timeout = -1;
         this.request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.Default);
         this.request.ConnectionGroupName = "__WebProxyScript";
         if (this.request.CacheProtocol != null)
         {
             this.request.CacheProtocol = new RequestCacheProtocol(this.backupCache, this.request.CacheProtocol.Validator);
         }
         HttpWebRequest request = this.request as HttpWebRequest;
         if (request != null)
         {
             request.Accept = "*/*";
             request.UserAgent = base.GetType().FullName + "/" + Environment.Version;
             request.KeepAlive = false;
             request.Pipelined = false;
             request.InternalConnectionGroup = true;
         }
         else
         {
             FtpWebRequest request2 = this.request as FtpWebRequest;
             if (request2 != null)
             {
                 request2.KeepAlive = false;
             }
         }
         this.request.Proxy = null;
         this.request.Credentials = base.Engine.Credentials;
         if (timerQueue == null)
         {
             timerQueue = TimerThread.GetOrCreateQueue(SettingsSectionInternal.Section.DownloadTimeout);
         }
         timer = timerQueue.CreateTimer(timerCallback, this.request);
         response = this.request.GetResponse();
         DateTime minValue = DateTime.MinValue;
         HttpWebResponse response2 = response as HttpWebResponse;
         if (response2 != null)
         {
             minValue = response2.LastModified;
         }
         else
         {
             FtpWebResponse response3 = response as FtpWebResponse;
             if (response3 != null)
             {
                 minValue = response3.LastModified;
             }
         }
         if (((this.scriptInstance != null) && (minValue != DateTime.MinValue)) && (this.scriptInstance.LastModified == minValue))
         {
             scriptInstance = this.scriptInstance;
             downloadFailure = BaseWebProxyFinder.AutoWebProxyState.Completed;
         }
         else
         {
             string scriptBody = null;
             byte[] buffer = null;
             using (Stream stream = response.GetResponseStream())
             {
                 SingleItemRequestCache.ReadOnlyStream stream2 = stream as SingleItemRequestCache.ReadOnlyStream;
                 if (stream2 != null)
                 {
                     buffer = stream2.Buffer;
                 }
                 if (((this.scriptInstance != null) && (buffer != null)) && (buffer == this.scriptInstance.Buffer))
                 {
                     this.scriptInstance.LastModified = minValue;
                     scriptInstance = this.scriptInstance;
                     downloadFailure = BaseWebProxyFinder.AutoWebProxyState.Completed;
                 }
                 else
                 {
                     using (StreamReader reader = new StreamReader(stream))
                     {
                         scriptBody = reader.ReadToEnd();
                     }
                 }
             }
             WebResponse response4 = response;
             response = null;
             response4.Close();
             timer.Cancel();
             timer = null;
             if (downloadFailure != BaseWebProxyFinder.AutoWebProxyState.Completed)
             {
                 if ((this.scriptInstance != null) && (scriptBody == this.scriptInstance.ScriptBody))
                 {
                     this.scriptInstance.LastModified = minValue;
                     if (buffer != null)
                     {
                         this.scriptInstance.Buffer = buffer;
                     }
                     scriptInstance = this.scriptInstance;
                     downloadFailure = BaseWebProxyFinder.AutoWebProxyState.Completed;
                 }
                 else
                 {
                     scriptInstance = new AutoWebProxyScriptWrapper {
                         LastModified = minValue
                     };
                     if (scriptInstance.Compile(location, scriptBody, buffer))
                     {
                         downloadFailure = BaseWebProxyFinder.AutoWebProxyState.Completed;
                     }
                     else
                     {
                         downloadFailure = BaseWebProxyFinder.AutoWebProxyState.CompilationFailure;
                     }
                 }
             }
         }
     }
     catch (Exception exception)
     {
         if (Logging.On)
         {
             Logging.PrintWarning(Logging.Web, SR.GetString("net_log_proxy_script_download_compile_error", new object[] { exception }));
         }
     }
     finally
     {
         if (timer != null)
         {
             timer.Cancel();
         }
         try
         {
             if (response != null)
             {
                 response.Close();
             }
         }
         finally
         {
             CodeAccessPermission.RevertAssert();
             this.request = null;
         }
     }
     if ((downloadFailure == BaseWebProxyFinder.AutoWebProxyState.Completed) && (this.scriptInstance != scriptInstance))
     {
         if (this.scriptInstance != null)
         {
             this.scriptInstance.Close();
         }
         this.scriptInstance = scriptInstance;
     }
     return downloadFailure;
 }
 private void EnsureEngineAvailable()
 {
     if ((base.State == BaseWebProxyFinder.AutoWebProxyState.Uninitialized) || (this.engineScriptLocation == null))
     {
         if (base.Engine.AutomaticallyDetectSettings)
         {
             this.DetectScriptLocation();
             if (this.scriptLocation != null)
             {
                 if (this.scriptLocation.Equals(this.engineScriptLocation))
                 {
                     base.State = BaseWebProxyFinder.AutoWebProxyState.Completed;
                     return;
                 }
                 if (this.DownloadAndCompile(this.scriptLocation) == BaseWebProxyFinder.AutoWebProxyState.Completed)
                 {
                     base.State = BaseWebProxyFinder.AutoWebProxyState.Completed;
                     this.engineScriptLocation = this.scriptLocation;
                     return;
                 }
             }
         }
         if ((base.Engine.AutomaticConfigurationScript != null) && !this.aborted)
         {
             if (base.Engine.AutomaticConfigurationScript.Equals(this.engineScriptLocation))
             {
                 base.State = BaseWebProxyFinder.AutoWebProxyState.Completed;
                 return;
             }
             base.State = this.DownloadAndCompile(base.Engine.AutomaticConfigurationScript);
             if (base.State == BaseWebProxyFinder.AutoWebProxyState.Completed)
             {
                 this.engineScriptLocation = base.Engine.AutomaticConfigurationScript;
                 return;
             }
         }
     }
     else
     {
         base.State = this.DownloadAndCompile(this.engineScriptLocation);
         if (base.State == BaseWebProxyFinder.AutoWebProxyState.Completed)
         {
             return;
         }
         if (!this.engineScriptLocation.Equals(base.Engine.AutomaticConfigurationScript) && !this.aborted)
         {
             base.State = this.DownloadAndCompile(base.Engine.AutomaticConfigurationScript);
             if (base.State == BaseWebProxyFinder.AutoWebProxyState.Completed)
             {
                 this.engineScriptLocation = base.Engine.AutomaticConfigurationScript;
                 return;
             }
         }
     }
     base.State = BaseWebProxyFinder.AutoWebProxyState.DiscoveryFailure;
     if (this.scriptInstance != null)
     {
         this.scriptInstance.Close();
         this.scriptInstance = null;
     }
     this.engineScriptLocation = null;
 }