private BundleResource GetNextResource(AsyncBundlingRequestState asyncState) { if (_bundleResources.Count == 0) { asyncState.OnCompleted(); return(BundleResource.Empty); } return(_bundleResources.Dequeue()); }
private void WriteResponse(BundleResource bundleResource, AsyncBundlingRequestState asyncState) { string content = TransformContent(bundleResource, asyncState.Context); asyncState.ResourceData.Append(content).Append(Environment.NewLine); asyncState.Context.Response.Write(content); asyncState.Context.Response.Write(Environment.NewLine); }
public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData) { //Make thread barrier here if (!ProcessingEvent.WaitOne(TimeSpan.FromSeconds(90))) { //If we didn't wait finishing then redirect here again context.Response.Redirect(context.Request.GetUrlRewriter().ToString(), true); } var asyncState = new AsyncBundlingRequestState(context, cb, extraData); if (!string.IsNullOrEmpty(context.Request.Headers["If-Modified-Since"])) { //Respond 304 //Return always 304 because context.Response.StatusCode = (int)HttpStatusCode.NotModified; asyncState.IsFromCache = true; asyncState.OnCompleted(true); } else { //Try cached var cached = HttpRuntime.Cache.Get(GetKey(new HttpRequestWrapper(context.Request))) as StringBuilder; if (cached != null) { asyncState.IsFromCache = true; asyncState.ResourceData = cached; asyncState.OnCompleted(true); } else { //Beign Read _maxEmbeddableSize = ClientCapabilities.GetMaxEmbeddableImageSize(new HttpRequestWrapper(context.Request)); ProcessingEvent.Reset(); if (_bundleResources.Count(r=> r.Content.Contains("jquery"))<= 0) { var content = @"/* Copyright (c) Ascensio System SIA " + DateTime.UtcNow.Year + @". All rights reserved. http://www.teamlab.com */"; asyncState.ResourceData.Append(content).Append(Environment.NewLine); asyncState.Context.Response.Write(content); asyncState.Context.Response.Write(Environment.NewLine); } ProcessItem(_bundleResources.Dequeue(), asyncState); } } return asyncState; }
public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData) { //Make thread barrier here if (!ProcessingEvent.WaitOne(TimeSpan.FromSeconds(90))) { //If we didn't wait finishing then redirect here again context.Response.Redirect(context.Request.GetUrlRewriter().ToString(), true); } var asyncState = new AsyncBundlingRequestState(context, cb, extraData); if (!string.IsNullOrEmpty(context.Request.Headers["If-Modified-Since"])) { //Respond 304 //Return always 304 because context.Response.StatusCode = (int)HttpStatusCode.NotModified; asyncState.IsFromCache = true; asyncState.OnCompleted(true); } else { //Try cached var cached = HttpRuntime.Cache.Get(GetKey(new HttpRequestWrapper(context.Request))) as StringBuilder; if (cached != null) { asyncState.IsFromCache = true; asyncState.ResourceData = cached; asyncState.OnCompleted(true); } else { //Beign Read _maxEmbeddableSize = ClientCapabilities.GetMaxEmbeddableImageSize(new HttpRequestWrapper(context.Request)); ProcessingEvent.Reset(); if (_bundleResources.Count(r => r.Content.Contains("jquery")) <= 0) { var content = @"/* Copyright (c) Ascensio System SIA " + DateTime.UtcNow.Year + @". All rights reserved. http://www.teamlab.com */"; asyncState.ResourceData.Append(content).Append(Environment.NewLine); asyncState.Context.Response.Write(content); asyncState.Context.Response.Write(Environment.NewLine); } ProcessItem(_bundleResources.Dequeue(), asyncState); } } return(asyncState); }
private BundleResource GetNextResource(AsyncBundlingRequestState asyncState) { if (_bundleResources.Count == 0) { asyncState.OnCompleted(); return BundleResource.Empty; } return _bundleResources.Dequeue(); }
private void ProcessItem(BundleResource resource, AsyncBundlingRequestState asyncState) { if (resource == BundleResource.Empty) { return; } if (resource.Type == BundleResourceType.EmbeddedScript || resource.Type == BundleResourceType.EmbeddedStyle) { resource.ServerPath = null; WriteResponse(resource, asyncState); ProcessItem(GetNextResource(asyncState), asyncState); } else if (resource.Type == BundleResourceType.ClientScript) { try { var handler = ClientScriptBundle.GetHttpHandler(resource.ServerPath.Split('/').Last().Split('.').First()); var content = handler.GetData(asyncState.Context); asyncState.ResourceData.Append(content).Append(Environment.NewLine); asyncState.Context.Response.Write(content); asyncState.Context.Response.Write(Environment.NewLine); ProcessItem(GetNextResource(asyncState), asyncState); } catch (Exception e) { _log.Error("ClientScriptError", e); } } else { //If it's file begin read and go to IOCP thread if (File.Exists(resource.Content)) { FileStream file = File.Open(resource.Content, FileMode.Open, FileAccess.Read, FileShare.Read); var buffer = new byte[file.Length]; file.BeginRead(buffer, 0, (int)file.Length, result => { try { int readed = file.EndRead(result); var asyncObject = result.AsyncState as AsyncBundlingRequestState; HttpContext.Current = asyncState.Context; resource.Content = Encoding.UTF8.GetString(buffer, 0, readed); WriteResponse(resource, asyncState); ProcessItem(GetNextResource(asyncObject), asyncObject); } catch (Exception e) { _log.Error( string.Format( "Error while processing file:{0}", resource.Content), e); } finally { //We got wht we need in buffer. close stream file.Close(); file.Dispose(); } }, asyncState); } } }