public override Task LoadAsync(IConfiguration configuration, IResourceLoader loader)
        {
            var link = Link;
            var request = link.CreateRequestFor(Url);
            var download = loader.DownloadAsync(request);
            SetDownload(download);

            return link.ProcessResponse(download, response =>
            {
                var type = link.Type ?? MimeTypeNames.Css;
                var engine = configuration.GetStyleEngine(type);

                if (engine != null)
                {
                    var options = new StyleOptions
                    {
                        Element = link,
                        IsDisabled = link.IsDisabled,
                        IsAlternate = link.RelationList.Contains(Keywords.Alternate),
                        Configuration = configuration
                    };

                    _sheet = engine.ParseStylesheet(response, options);
                    _sheet.Media.MediaText = link.Media ?? String.Empty;
                }
            });
        }
Esempio n. 2
0
        public override Task LoadAsync(IConfiguration configuration, IResourceLoader loader)
        {
            var link     = Link;
            var request  = link.CreateRequestFor(Url);
            var download = loader.DownloadAsync(request);

            SetDownload(download);

            return(link.ProcessResponse(download, response =>
            {
                var type = link.Type ?? MimeTypeNames.Css;
                var engine = configuration.GetStyleEngine(type);

                if (engine != null)
                {
                    var options = new StyleOptions
                    {
                        Element = link,
                        IsDisabled = link.IsDisabled,
                        IsAlternate = link.RelationList.Contains(Keywords.Alternate),
                        Configuration = configuration
                    };

                    _sheet = engine.ParseStylesheet(response, options);
                    _sheet.Media.MediaText = link.Media ?? String.Empty;
                }
            }));
        }
        /// <summary>
        /// See http://www.w3.org/TR/html-imports/#dfn-import-request.
        /// </summary>
        public override async Task LoadAsync(IConfiguration configuration, IResourceLoader loader)
        {
            var link = Link;
            var document = link.Owner;
            var list = ImportLists.GetOrCreateValue(document);
            var location = Url;
            var request = link.CreateRequestFor(location);
            var item = new ImportEntry 
            { 
                Relation = this,
                IsCycle = CheckCycle(document, location)
            };
            _isasync = link.HasAttribute(AttributeNames.Async);
            list.Add(item);
            
            if (!item.IsCycle)
            {
                var nestedStatus = new TaskCompletionSource<Boolean>();
                var download = loader.DownloadAsync(request);
                SetDownload(download);

                await link.ProcessResponse(download, async response =>
                {
                    var context = new BrowsingContext(document.Context, Sandboxes.None);
                    var options = new CreateDocumentOptions(response, configuration)
                    {
                        ImportAncestor = document
                    };
                    _import = await context.OpenAsync(options, CancellationToken.None).ConfigureAwait(false);
                    nestedStatus.SetResult(true);
                }).ConfigureAwait(false);
                await nestedStatus.Task.ConfigureAwait(false);
            }
        }
Esempio n. 4
0
        static IDownload FetchWithCors(this IResourceLoader loader, Url url, ResourceRequest request, CorsSetting setting, OriginBehavior behavior)
        {
            var download = loader.DownloadAsync(new ResourceRequest(request.Source, url)
            {
                Origin = request.Origin,
                IsManualRedirectDesired = true
            });

            return(download.Wrap(response =>
            {
                if (response.IsRedirected())
                {
                    url.Href = response.Headers.GetOrDefault(HeaderNames.Location, url.Href);

                    if (request.Origin.Is(url.Origin))
                    {
                        return loader.FetchWithCors(new ResourceRequest(request.Source, url)
                        {
                            IsCookieBlocked = request.IsCookieBlocked,
                            IsSameOriginForced = request.IsSameOriginForced,
                            Origin = request.Origin
                        }, setting, behavior);
                    }

                    return loader.FetchWithCors(url, request, setting, behavior);
                }
                else
                {
                    return download;
                }
            }));
        }
        protected void StartDownload(ResourceRequest request)
        {
            if (_download != null && !_download.IsCompleted)
            {
                _download.Cancel();
            }

            _download = _loader.DownloadAsync(request);
        }
Esempio n. 6
0
        static IDownload FetchWithoutCors(this IResourceLoader loader, ResourceRequest request, OriginBehavior behavior)
        {
            if (behavior == OriginBehavior.Fail)
            {
                throw new DomException(DomError.Network);
            }

            return(loader.DownloadAsync(request));
        }
Esempio n. 7
0
        public virtual Task ProcessAsync(ResourceRequest request)
        {
            if (IsDifferentToCurrentDownloadUrl(request.Target))
            {
                CancelDownload();
                Download = _loader.DownloadAsync(request);
                return(FinishDownloadAsync());
            }

            return(null);
        }
Esempio n. 8
0
        private static IDownload FetchFromDifferentOrigin(this IResourceLoader loader, CorsRequest cors)
        {
            var request = cors.Request;

            request.IsCredentialOmitted = cors.IsAnonymous();
            var download = loader.DownloadAsync(request);

            return(download.Wrap(response =>
            {
                if (response?.StatusCode != HttpStatusCode.OK)
                {
                    response?.Dispose();
                    throw new DomException(DomError.Network);
                }

                return cors.CheckIntegrity(download);
            }));
        }
Esempio n. 9
0
        static IDownload FetchWithCors(this IResourceLoader loader, ResourceRequest request, CorsSetting setting)
        {
            request.IsCredentialOmitted = setting == CorsSetting.Anonymous;
            var download = loader.DownloadAsync(request);

            return(download.Wrap(response =>
            {
                if (response != null && response.StatusCode == HttpStatusCode.OK)
                {
                    return download;
                }
                else if (response != null)
                {
                    response.Dispose();
                }

                throw new DomException(DomError.Network);
            }));
        }
Esempio n. 10
0
        private static IDownload FetchFromSameOrigin(this IResourceLoader loader, Url url, CorsRequest cors)
        {
            var request  = cors.Request;
            var download = loader.DownloadAsync(new ResourceRequest(request.Source, url)
            {
                Origin = request.Origin,
                IsManualRedirectDesired = true
            });

            return(download.Wrap(response =>
            {
                if (response.IsRedirected())
                {
                    url.Href = response.Headers.GetOrDefault(HeaderNames.Location, url.Href);

                    return request.Origin.Is(url.Origin) ?
                    loader.FetchWithCors(cors.RedirectTo(url)) :
                    loader.FetchFromSameOrigin(url, cors);
                }

                return cors.CheckIntegrity(download);
            }));
        }
Esempio n. 11
0
        /// <summary>
        /// See http://www.w3.org/TR/html-imports/#dfn-import-request.
        /// </summary>
        public override async Task LoadAsync(IConfiguration configuration, IResourceLoader loader)
        {
            var link     = Link;
            var document = link.Owner;
            var list     = ImportLists.GetOrCreateValue(document);
            var location = Url;
            var request  = link.CreateRequestFor(location);
            var item     = new ImportEntry
            {
                Relation = this,
                IsCycle  = CheckCycle(document, location)
            };

            _isasync = link.HasAttribute(AttributeNames.Async);
            list.Add(item);

            if (!item.IsCycle)
            {
                var nestedStatus = new TaskCompletionSource <Boolean>();
                var download     = loader.DownloadAsync(request);
                SetDownload(download);

                await link.ProcessResponse(download, async response =>
                {
                    var context = new BrowsingContext(document.Context, Sandboxes.None);
                    var options = new CreateDocumentOptions(response, configuration)
                    {
                        ImportAncestor = document
                    };
                    _import = await context.OpenAsync(options, CancellationToken.None).ConfigureAwait(false);
                    nestedStatus.SetResult(true);
                }).ConfigureAwait(false);

                await nestedStatus.Task.ConfigureAwait(false);
            }
        }
Esempio n. 12
0
        /// <summary>
        /// Performs a potentially CORS-enabled fetch from the given URI by
        /// using an asynchronous GET request. For more information see:
        /// http://www.w3.org/TR/html5/infrastructure.html#potentially-cors-enabled-fetch
        /// </summary>
        /// <param name="loader">The resource loader to use.</param>
        /// <param name="request">The request to issue.</param>
        /// <param name="setting">The cross origin settings to use.</param>
        /// <param name="behavior">
        /// The default behavior in case it is undefined.
        /// </param>
        /// <returns>
        /// The task which will eventually return the stream.
        /// </returns>
        public static async Task <IResponse> FetchWithCorsAsync(this IResourceLoader loader, ResourceRequest request, CorsSetting setting, OriginBehavior behavior)
        {
            var url = request.Target;

            if (request.Origin == url.Origin || url.Scheme == ProtocolNames.Data || url.Href == "about:blank")
            {
                while (true)
                {
                    var data = new ResourceRequest(request.Source, url)
                    {
                        Origin = request.Origin,
                        IsManualRedirectDesired = true
                    };

                    var result = await loader.DownloadAsync(data).Task.ConfigureAwait(false);

                    if (result.IsRedirected())
                    {
                        url = new Url(result.Headers.GetOrDefault(HeaderNames.Location, url.Href));

                        if (request.Origin.Is(url.Origin))
                        {
                            request = new ResourceRequest(request.Source, url)
                            {
                                IsCookieBlocked    = request.IsCookieBlocked,
                                IsSameOriginForced = request.IsSameOriginForced,
                                Origin             = request.Origin
                            };
                            return(await loader.FetchWithCorsAsync(request, setting, behavior).ConfigureAwait(false));
                        }
                    }
                    else
                    {
                        return(result);
                    }
                }
            }
            else if (setting == CorsSetting.None)
            {
                if (behavior == OriginBehavior.Fail)
                {
                    throw new DomException(DomError.Network);
                }

                return(await loader.DownloadAsync(request).Task.ConfigureAwait(false));
            }
            else if (setting == CorsSetting.Anonymous || setting == CorsSetting.UseCredentials)
            {
                request.IsCredentialOmitted = setting == CorsSetting.Anonymous;
                var result = await loader.FetchAsync(request).ConfigureAwait(false);

                if (result != null && result.StatusCode == HttpStatusCode.OK)
                {
                    return(result);
                }
                else if (result != null)
                {
                    result.Dispose();
                }
            }

            throw new DomException(DomError.Network);
        }
Esempio n. 13
0
 /// <summary>
 /// Performs a fetch from the given URI by using an asynchronous
 /// request.
 /// </summary>
 /// <param name="loader">The resource loader to use.</param>
 /// <param name="request">The request to issue.</param>
 /// <returns>
 /// The task which will eventually return the stream.
 /// </returns>
 public static Task <IResponse> FetchAsync(this IResourceLoader loader, ResourceRequest request)
 {
     return(loader != null?loader.DownloadAsync(request).Task : TaskEx.FromResult(default(IResponse)));
 }