示例#1
0
        public async Task includeFileWithCache(ScriptScopeContext scope, string virtualPath, object options)
        {
            var scopedParams = scope.AssertOptions(nameof(includeUrl), options);
            var expireIn     = scopedParams.TryGetValue("expireInSecs", out object value)
                ? TimeSpan.FromSeconds(value.ConvertTo <int>())
                : (TimeSpan)scope.Context.Args[ScriptConstants.DefaultFileCacheExpiry];

            var cacheKey = CreateCacheKey("file:" + scope.PageResult.VirtualPath + ">" + virtualPath, scopedParams);

            if (Context.ExpiringCache.TryGetValue(cacheKey, out Tuple <DateTime, object> cacheEntry))
            {
                if (cacheEntry.Item1 > DateTime.UtcNow && cacheEntry.Item2 is byte[] bytes)
                {
                    await scope.OutputStream.WriteAsync(bytes);

                    return;
                }
            }

            var file = ResolveFile(nameof(includeFileWithCache), scope, virtualPath);
            var ms   = MemoryStreamFactory.GetStream();

            using (ms)
            {
                using (var reader = file.OpenRead())
                {
                    await reader.CopyToAsync(ms);
                }

                ms.Position = 0;
                var bytes = ms.ToArray();
                Context.ExpiringCache[cacheKey] = Tuple.Create(DateTime.UtcNow.Add(expireIn), (object)bytes);
                await scope.OutputStream.WriteAsync(bytes);
            }
        }
示例#2
0
        public async Task includeUrl(ScriptScopeContext scope, string url, object options)
        {
            var scopedParams = scope.AssertOptions(nameof(includeUrl), options);

            var webReq   = (HttpWebRequest)WebRequest.Create(url);
            var dataType = scopedParams.TryGetValue("dataType", out object value)
                ? ConvertDataTypeToContentType((string)value)
                : null;

            if (scopedParams.TryGetValue("method", out value))
            {
                webReq.Method = (string)value;
            }
            if (scopedParams.TryGetValue("contentType", out value) || dataType != null)
            {
                webReq.ContentType = (string)value ?? dataType;
            }
            if (scopedParams.TryGetValue("accept", out value) || dataType != null)
            {
                webReq.Accept = (string)value ?? dataType;
            }
            if (scopedParams.TryGetValue("userAgent", out value))
            {
                PclExport.Instance.SetUserAgent(webReq, (string)value);
            }

            if (scopedParams.TryRemove("data", out object data))
            {
                if (webReq.Method == null)
                {
                    webReq.Method = HttpMethods.Post;
                }

                if (webReq.ContentType == null)
                {
                    webReq.ContentType = MimeTypes.FormUrlEncoded;
                }

                var body = ConvertDataToString(data, webReq.ContentType);
                using (var stream = await webReq.GetRequestStreamAsync())
                {
                    await stream.WriteAsync(body);
                }
            }

            using (var webRes = await webReq.GetResponseAsync())
                using (var stream = webRes.GetResponseStream())
                {
                    await stream.CopyToAsync(scope.OutputStream);
                }
        }
示例#3
0
        public async Task includeUrlWithCache(ScriptScopeContext scope, string url, object options)
        {
            var scopedParams = scope.AssertOptions(nameof(includeUrl), options);
            var expireIn     = scopedParams.TryGetValue("expireInSecs", out object value)
                ? TimeSpan.FromSeconds(value.ConvertTo <int>())
                : (TimeSpan)scope.Context.Args[ScriptConstants.DefaultUrlCacheExpiry];

            var cacheKey = CreateCacheKey("url:" + url, scopedParams);

            if (Context.ExpiringCache.TryGetValue(cacheKey, out Tuple <DateTime, object> cacheEntry))
            {
                if (cacheEntry.Item1 > DateTime.UtcNow && cacheEntry.Item2 is byte[] bytes)
                {
                    await scope.OutputStream.WriteAsync(bytes);

                    return;
                }
            }

            var dataType = scopedParams.TryGetValue("dataType", out value)
                ? ConvertDataTypeToContentType((string)value)
                : null;

            if (scopedParams.TryGetValue("method", out value) && !((string)value).EqualsIgnoreCase("GET"))
            {
                throw new NotSupportedException($"Only GET requests can be used in {nameof(includeUrlWithCache)} filters in page '{scope.Page.VirtualPath}'");
            }
            if (scopedParams.TryGetValue("data", out value))
            {
                throw new NotSupportedException($"'data' is not supported in {nameof(includeUrlWithCache)} filters in page '{scope.Page.VirtualPath}'");
            }

            var ms = MemoryStreamFactory.GetStream();

            using (ms)
            {
                var captureScope = scope.ScopeWithStream(ms);
                await includeUrl(captureScope, url, options);

                ms.Position = 0;
                var expireAt = DateTime.UtcNow.Add(expireIn);

                var bytes = ms.ToArray();
                Context.ExpiringCache[cacheKey] = cacheEntry = Tuple.Create(expireAt, (object)bytes);
                await scope.OutputStream.WriteAsync(bytes);
            }
        }
示例#4
0
        public static Dictionary <string, object> GetParamsWithItemBindingOnly(this ScriptScopeContext scope, string filterName, SharpPage page, object scopedParams, out string itemBinding)
        {
            var pageParams = scope.AssertOptions(filterName, scopedParams);

            itemBinding = pageParams.TryGetValue("it", out object bindingName) && bindingName is string binding
                ? binding
                : "it";

            if (bindingName != null && !(bindingName is string))
            {
                throw new NotSupportedException($"'it' option in filter '{filterName}' should contain the name to bind to but contained a '{bindingName.GetType().Name}' instead");
            }

            // page vars take precedence
            if (page != null && page.Args.TryGetValue("it", out object pageBinding))
            {
                itemBinding = (string)pageBinding;
            }

            return(pageParams);
        }