예제 #1
0
        private string DoTransform <T>(string preTransform, StringBuilder urls, List <string> transformableMatches, string noCommentTransform) where T : IResourceType
        {
            var resource = RRContainer.Current.GetInstance <T>();

            RRTracer.Trace("Looking for reduction for {0}", urls);
            var transform = reductionRepository.FindReduction(urls.ToString());

            if (transform != null)
            {
                RRTracer.Trace("Reduction found for {0}", urls);
                if (uriBuilder.ParseSignature(transform) != Guid.Empty.RemoveDashes())
                {
                    var firstScript =
                        RRContainer.Current.GetInstance <JavaScriptResource>().ResourceRegex.Match(noCommentTransform);
                    var firstScriptIndex = firstScript.Success ? preTransform.IndexOf(firstScript.ToString(), StringComparison.Ordinal) : -1;
                    var insertionIdx     = (firstScript.Success && firstScriptIndex <
                                            preTransform.IndexOf(transformableMatches[0], StringComparison.Ordinal) && resource is CssResource)
                                           ? firstScriptIndex - 1
                                           : preTransform.IndexOf(transformableMatches[0], StringComparison.Ordinal) - 1;
                    preTransform = preTransform.Insert(insertionIdx + 1, resource.TransformedMarkupTag(transform));
                }
                var result = preTransform;
                foreach (var match in transformableMatches)
                {
                    var idx = result.IndexOf(match, StringComparison.Ordinal);
                    result = result.Remove(idx, match.Length);
                    if (idx == result.Length)
                    {
                        continue;
                    }
                    if (result[idx] == '\r')
                    {
                        result = result.Remove(idx, 1);
                    }
                    if (result[idx] == '\n')
                    {
                        result = result.Remove(idx, 1);
                    }
                }
                return(result);
            }
            var host = string.Empty;

            if (!string.IsNullOrEmpty(config.ContentHost))
            {
                var uri = context.Request.Url;
                if (uri != null)
                {
                    host = string.Format("{0}://{1}/", uri.Scheme, uri.Host);
                }
            }
            reducingQueue.Enqueue(new QueueItem <T> {
                Urls = urls.ToString(), Host = host
            });
            RRTracer.Trace("No reduction found for {0}. Enqueuing.", urls);
            return(preTransform);
        }
예제 #2
0
        private string DoTransform <T>(string preTransform, StringBuilder urls, List <string> transformableMatches) where T : IResourceType
        {
            var resource = RRContainer.Current.GetInstance <T>();

            RRTracer.Trace("Looking for reduction for {0}", urls);
            var transform = reductionRepository.FindReduction(urls.ToString());

            if (transform != null)
            {
                RRTracer.Trace("Reduction found for {0}", urls);
                var closeHeadIdx = (preTransform.StartsWith("<head", StringComparison.OrdinalIgnoreCase) && resource is CssResource) ? preTransform.IndexOf('>') : preTransform.IndexOf(transformableMatches[0]) - 1;
                preTransform = preTransform.Insert(closeHeadIdx + 1, resource.TransformedMarkupTag(transform));
                return(transformableMatches.Aggregate(preTransform, (current, match) => current.Remove(current.IndexOf(match), match.Length)));
            }
            reducingQueue.Enqueue(new QueueItem <T> {
                Urls = urls.ToString()
            });
            RRTracer.Trace("No reduction found for {0}. Enqueuing.", urls);
            return(preTransform);
        }
예제 #3
0
        protected void ProcessQueuedItem()
        {
            var        key          = Guid.Empty;
            IQueueItem itemToReduce = null;

            if (!ReductionRepository.HasLoadedSavedEntries)
            {
                return;
            }
            try
            {
                queueLock.EnterWriteLock();

                try
                {
                    if (Queue.Count > 0)
                    {
                        itemToReduce = Queue.Dequeue();
                    }
                }
                finally
                {
                    queueLock.ExitWriteLock();
                }

                if (itemToReduce != null &&
                    ReductionRepository.FindReduction(itemToReduce.Urls) == null)
                {
                    ItemBeingProcessed = itemToReduce;
                    key = Hasher.Hash(itemToReduce.Urls);
                    RRTracer.Trace("dequeued and processing {0} with key {1}.", itemToReduce.Urls, key);
                    string url = Store.GetUrlByKey(key, itemToReduce.ResourceType);
                    if (url != null)
                    {
                        RRTracer.Trace("found url {0} in store for key {1}", url, key);
                        ReductionRepository.AddReduction(key, url);
                    }
                    else
                    {
                        if (DictionaryOfFailures.ContainsKey(key) &&
                            DictionaryOfFailures[key].Count >= FailureThreshold)
                        {
                            RRTracer.Trace("{0} has exceeded its failure threshold and will not be processed.",
                                           itemToReduce.Urls);
                            return;
                        }

                        IReducer reducer = RRContainer
                                           .Current
                                           .GetAllInstances <IReducer>()
                                           .SingleOrDefault(x => x.SupportedResourceType == itemToReduce.ResourceType);

                        if (reducer == null)
                        {
                            RRTracer.Trace(string.Format("Failed to retrieve an RRContainer for {0}.",
                                                         itemToReduce.ResourceType));
                            return;
                        }

                        reducer.Process(key, itemToReduce.Urls, itemToReduce.Host);
                    }
                    RRTracer.Trace("dequeued and processed {0}.", itemToReduce.Urls);
                }
            }
            catch (Exception e)
            {
                string message = string.Format("There were errors reducing {0}",
                                               itemToReduce != null ? itemToReduce.Urls : "");
                var wrappedException = new ApplicationException(message, e);
                RRTracer.Trace(message);
                RRTracer.Trace(e.ToString());

                AddFailure(key, wrappedException);

                if (Registry.CaptureErrorAction != null)
                {
                    Registry.CaptureErrorAction(wrappedException);
                }
            }
            finally
            {
                ItemBeingProcessed = null;
            }
        }