Ejemplo n.º 1
0
        public static void Exec(BrewRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            var response = new BrewResponse
            {
                ContentType = request.Headers.ContentType
            };

            //Set the current context with information from the current request and response.
            BaristaContext.Current = new BaristaContext(request, response);
            var instanceSettings = BaristaContext.Current.Request.ParseInstanceSettings();

            //If we're not executing with Per-Call instancing, create a mutex to synchronize against.
            Mutex syncRoot = null;

            if (instanceSettings.InstanceMode != BaristaInstanceMode.PerCall)
            {
                syncRoot = new Mutex(false, "Barista_ScriptEngineInstance_" + instanceSettings.InstanceName);
            }

            var webBundle = new BaristaWebBundle();
            var source    = new BaristaScriptSource(request.Code, request.CodePath);

            if (syncRoot != null)
            {
                syncRoot.WaitOne();
            }

            try
            {
                bool isNewScriptEngineInstance;
                bool errorInInitialization;

                var scriptEngineFactory = new BaristaScriptEngineFactory();
                var engine = scriptEngineFactory.GetScriptEngine(webBundle, out isNewScriptEngineInstance, out errorInInitialization);

                if (engine == null)
                {
                    throw new InvalidOperationException("Unable to obtain a script engine instance.");
                }

                if (errorInInitialization)
                {
                    return;
                }

                try
                {
                    engine.Evaluate(source);
                }
                catch (JavaScriptException ex)
                {
                    //BaristaDiagnosticsService.Local.LogException(ex, BaristaDiagnosticCategory.JavaScriptException,
                    //                                             "A JavaScript exception was thrown while evaluating script: ");
                    scriptEngineFactory.UpdateResponseWithJavaScriptExceptionDetails(engine, ex, response);
                }
                catch (Exception ex)
                {
                    //BaristaDiagnosticsService.Local.LogException(ex, BaristaDiagnosticCategory.Runtime,
                    //                                             "An internal error occured while executing script: ");
                    scriptEngineFactory.UpdateResponseWithExceptionDetails(ex, response);
                }
                finally
                {
                    //Cleanup
                    // ReSharper disable RedundantAssignment
                    engine = null;
                    // ReSharper restore RedundantAssignment

                    if (BaristaContext.Current != null)
                    {
                        BaristaContext.Current.Dispose();
                    }

                    BaristaContext.Current = null;
                }
            }
            finally
            {
                if (syncRoot != null)
                {
                    syncRoot.ReleaseMutex();
                }
            }
        }
Ejemplo n.º 2
0
        public static BrewResponse Eval(BrewRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            var response = new BrewResponse
            {
                ContentType = request.Headers.ContentType
            };

            BaristaContext.Current = new BaristaContext(request, response);
            var instanceSettings = BaristaContext.Current.Request.ParseInstanceSettings();

            Mutex syncRoot = null;

            if (instanceSettings.InstanceMode != BaristaInstanceMode.PerCall)
            {
                syncRoot = new Mutex(false, "Barista_ScriptEngineInstance_" + instanceSettings.InstanceName);
            }

            var webBundle = new BaristaWebBundle();
            var source    = new BaristaScriptSource(request.Code, request.CodePath);

            if (syncRoot != null)
            {
                syncRoot.WaitOne();
            }

            try
            {
                bool isNewScriptEngineInstance;
                bool errorInInitialization;

                var scriptEngineFactory = new BaristaScriptEngineFactory();
                var engine = scriptEngineFactory.GetScriptEngine(webBundle, out isNewScriptEngineInstance, out errorInInitialization);

                if (engine == null)
                {
                    throw new InvalidOperationException("Unable to obtain a script engine instance.");
                }

                if (errorInInitialization)
                {
                    return(response);
                }

                try
                {
                    var result = engine.Evaluate(source);

                    var isRaw = false;

                    //If the web instance has been initialized on the web bundle, use the value set via script, otherwise use defaults.
                    if (webBundle.WebInstance == null || webBundle.WebInstance.Response.AutoDetectContentType)
                    {
                        response.ContentType = BrewResponse.AutoDetectContentTypeFromResult(result, response.ContentType);

                        var arrayResult = result as Barista.Library.Base64EncodedByteArrayInstance;
                        if (arrayResult != null && arrayResult.FileName.IsNullOrWhiteSpace() == false && response.Headers != null && response.Headers.ContainsKey("Content-Disposition") == false)
                        {
                            var br         = BrowserUserAgentParser.GetDefault();
                            var clientInfo = br.Parse(request.Headers.UserAgent);

                            if (clientInfo.UserAgent.Family == "IE" && (clientInfo.UserAgent.Major == "7" || clientInfo.UserAgent.Major == "8"))
                            {
                                response.Headers.Add("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(arrayResult.FileName));
                            }
                            else if (clientInfo.UserAgent.Family == "Safari")
                            {
                                response.Headers.Add("Content-Disposition", "attachment; filename=" + arrayResult.FileName);
                            }
                            else
                            {
                                response.Headers.Add("Content-Disposition", "attachment; filename=\"" + HttpUtility.UrlEncode(arrayResult.FileName) + "\"");
                            }
                        }
                    }

                    if (webBundle.WebInstance != null)
                    {
                        isRaw = webBundle.WebInstance.Response.IsRaw;
                    }

                    response.SetContentsFromResultObject(engine, result, isRaw);
                }
                catch (JavaScriptException ex)
                {
                    //BaristaDiagnosticsService.Local.LogException(ex, BaristaDiagnosticCategory.JavaScriptException, "A JavaScript exception was thrown while evaluating script: ");
                    scriptEngineFactory.UpdateResponseWithJavaScriptExceptionDetails(engine, ex, response);
                }
                catch (Exception ex)
                {
                    //BaristaDiagnosticsService.Local.LogException(ex, BaristaDiagnosticCategory.Runtime, "An internal error occurred while evaluating script: ");
                    scriptEngineFactory.UpdateResponseWithExceptionDetails(ex, response);
                }
                finally
                {
                    //Cleanup
                    // ReSharper disable RedundantAssignment
                    engine = null;
                    // ReSharper restore RedundantAssignment

                    if (BaristaContext.Current != null)
                    {
                        BaristaContext.Current.Dispose();
                    }

                    BaristaContext.Current = null;
                }
            }
            finally
            {
                if (syncRoot != null)
                {
                    syncRoot.ReleaseMutex();
                }
            }

            return(response);
        }