Esempio n. 1
0
        /// <summary>
        /// Loads the data for the request asynchronously.
        /// </summary>
        /// <param name="request">The issued request.</param>
        /// <returns>The active download.</returns>
        public virtual IDownload DownloadAsync(ResourceRequest request)
        {
            var data = new Request
            {
                Address = request.Target,
                Method = HttpMethod.Get
            };

            data.Headers[HeaderNames.Referer] = request.Source.Owner.DocumentUri;
            return DownloadAsync(data, request.Source);
        }
Esempio n. 2
0
        /// <summary>
        /// Loads the data for the request asynchronously.
        /// </summary>
        /// <param name="request">The issued request.</param>
        /// <param name="cancel">The cancellation token.</param>
        /// <returns>The task creating the response.</returns>
        public virtual Task<IResponse> LoadAsync(ResourceRequest request, CancellationToken cancel)
        {
            var events = _document.Context.Configuration.Events;
            var data = new Request
            {
                Address = request.Target,
                Method = HttpMethod.Get
            };

            data.Headers[HeaderNames.Referer] = request.Source.Owner.DocumentUri;
            return _filter(data) ? _requesters.LoadAsync(data, events, cancel) : TaskEx.FromResult(default(IResponse));
        }
        public async Task DownloadFtpRfcViaFtpRequester()
        {
            var url = "ftp://ftp.funet.fi/pub/standards/RFC/rfc959.txt";
            var requester = new FtpRequester();
            var request = new Request { Address = Url.Create(url) };

            var response = await requester.RequestAsync(request, CancellationToken.None);
            Assert.IsNotNull(response);

            var content = await new StreamReader(response.Content).ReadToEndAsync();
            Assert.AreEqual(147316, content.Length);
        }
Esempio n. 4
0
        /// <summary>
        /// Starts downloading the request.
        /// </summary>
        /// <param name="request">The request data.</param>
        /// <param name="originator">The request's originator.</param>
        /// <returns>The active download.</returns>
        protected virtual IDownload DownloadAsync(Request request, INode originator)
        {
            var cancel = new CancellationTokenSource();

            if (_filter(request))
            {
                var task = LoadAsync(request, cancel.Token);
                var download = new Download(task, cancel, request.Address, originator);
                Add(download);
                task.ContinueWith(m => Remove(download));
                return download;
            }

            return new Download(TaskEx.FromResult(default(IResponse)), cancel, request.Address, originator);
        }
Esempio n. 5
0
        /// <summary>
        /// Loads the data for the request asynchronously.
        /// </summary>
        /// <param name="request">The issued request.</param>
        /// <returns>The active download.</returns>
        public virtual IDownload DownloadAsync(DocumentRequest request)
        {
            var data = new Request
            {
                Address = request.Target,
                Content = request.Body,
                Method = request.Method
            };

            foreach (var header in request.Headers)
            {
                data.Headers[header.Key] = header.Value;
            }

            return DownloadAsync(data, request.Source);
        }
Esempio n. 6
0
        public async Task DataGetRequestForPlainText()
        {
            var http = new DataRequester();
            var request = new Request { Address = new Url("data:,Hello+there"), Method = HttpMethod.Get };

            using (var response = await http.RequestAsync(request, CancellationToken.None))
            {
                Assert.IsNotNull(response);
                Assert.AreEqual(200, (int)response.StatusCode);
                Assert.IsTrue(response.Content.CanRead);
                Assert.IsTrue(response.Headers.Count > 0);
                Assert.AreEqual("text/plain", response.Headers[HeaderNames.ContentType]);

                var content = new StreamReader(response.Content);
                Assert.AreEqual("Hello there", content.ReadToEnd());
            }
        }
Esempio n. 7
0
        public async Task DataGetRequestForMiddleImageBase64Encoded()
        {
            var http = new DataRequester();
            var request = new Request();
            var content = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAABlBMVEUAAAD///+l2Z/dAAAAM0lEQVR4nGP4/5/h/1+G/58ZDrAz3D/McH8yw83NDDeNGe4Ug9C9zwz3gVLMDA/A6P9/AFGGFyjOXZtQAAAAAElFTkSuQmCC";
            request.Address = new Url(content);
            request.Method = HttpMethod.Get;

            using (var response = await http.RequestAsync(request, CancellationToken.None))
            {
                Assert.IsNotNull(response);
                Assert.AreEqual(200, (int)response.StatusCode);
                Assert.IsTrue(response.Content.CanRead);
                Assert.IsTrue(response.Headers.Count > 0);
                Assert.AreEqual("image/png", response.Headers[HeaderNames.ContentType]);
            }
        }
Esempio n. 8
0
        public async Task StatusCode403OfHttpGetRequest()
        {
            if (Helper.IsNetworkAvailable())
            {
                var http = new HttpRequester();
                var request = new Request();
                request.Address = new Url("http://httpbin.org/status/403");
                request.Method = HttpMethod.Get;

                using (var response = await http.RequestAsync(request, CancellationToken.None))
                {
                    Assert.IsNotNull(response);
                    Assert.AreEqual(403, (int)response.StatusCode);
                    Assert.IsTrue(response.Content.CanRead);
                    Assert.IsTrue(response.Headers.Count > 0);
                }
            }
        }
Esempio n. 9
0
        public async Task InvokeAboutUnsetUrlShouldNotFire()
        {
            var about = new AboutRequester();
            var requests = new List<IRequest>();
            var req = new Request
            {
                Address = Url.Create("about://bookmarks"),
                Method = HttpMethod.Get
            };

            about.SetRoute("settings", (request, cancel) =>
            {
                requests.Add(request);
                return Task.FromResult(default(IResponse));
            });

            var response = await about.RequestAsync(req, CancellationToken.None);
            Assert.IsNull(response);
            Assert.AreEqual(0, requests.Count);
        }
Esempio n. 10
0
        /// <summary>
        /// Loads the data for the request asynchronously.
        /// </summary>
        /// <param name="request">The issued request.</param>
        /// <param name="cancel">The cancellation token.</param>
        /// <returns>The task creating the response.</returns>
        public virtual Task<IResponse> LoadAsync(DocumentRequest request, CancellationToken cancel)
        {
            var events = _context.Configuration.Events;
            var data = new Request
            {
                Address = request.Target,
                Content = request.Body,
                Method = request.Method
            };

            foreach (var header in request.Headers)
                data.Headers[header.Key] = header.Value;

            var cookie = _context.Configuration.GetCookie(request.Target.Origin);

            if (cookie != null)
                data.Headers[HeaderNames.Cookie] = cookie;

            return _filter(data) ? _requesters.LoadAsync(data, events, cancel) : TaskEx.FromResult(default(IResponse));
        }
Esempio n. 11
0
        public async Task DataGetRequestForHtmlWithUtf8Base64Encoded()
        {
            var http = new DataRequester();
            var request = new Request();
            var source = "<!DOCTYPE html><html lang='en'><head><title>Embedded Window</title></head><body><h1>42</h1></body></html>";
            var content = Convert.ToBase64String(TextEncoding.Utf8.GetBytes(source));
            request.Address = new Url("data:text/html;charset=utf-8;base64," + content);
            request.Method = HttpMethod.Get;

            using (var response = await http.RequestAsync(request, CancellationToken.None))
            {
                Assert.IsNotNull(response);
                Assert.AreEqual(200, (int)response.StatusCode);
                Assert.IsTrue(response.Content.CanRead);
                Assert.IsTrue(response.Headers.Count > 0);
                Assert.AreEqual("text/html;charset=utf-8", response.Headers[HeaderNames.ContentType]);

                var reader = new StreamReader(response.Content);
                Assert.AreEqual(source, reader.ReadToEnd());
            }
        }
Esempio n. 12
0
        /// <summary>
        /// Loads the data for the request asynchronously.
        /// </summary>
        /// <param name="request">The issued request.</param>
        /// <returns>The active download.</returns>
        public virtual IDownload DownloadAsync(ResourceRequest request)
        {
            var data = new Request
            {
                Address = request.Target,
                Method = HttpMethod.Get,
                Headers = new Dictionary<String, String>
                {
                    { HeaderNames.Referer, request.Source.Owner.DocumentUri }
                }
            };

            var cookie = GetCookie(request.Target);

            if (cookie != null)
            {
                data.Headers[HeaderNames.Cookie] = cookie;
            }

            return DownloadAsync(data, request.Source);
        }
Esempio n. 13
0
        public async Task SimpleHttpGetRequest()
        {
            if (Helper.IsNetworkAvailable())
            {
                var http = new HttpRequester();
                var request = new Request();
                request.Address = new Url("http://httpbin.org/robots.txt");
                request.Method = HttpMethod.Get;

                using (var response = await http.RequestAsync(request, CancellationToken.None))
                {
                    Assert.IsNotNull(response);
                    Assert.AreEqual(200, (int)response.StatusCode);
                    Assert.IsTrue(response.Content.CanRead);
                    Assert.IsTrue(response.Headers.Count > 0);

                    var content = new StreamReader(response.Content);
                    Assert.AreEqual("User-agent: *\nDisallow: /deny\n", content.ReadToEnd());
                }
            }
        }
Esempio n. 14
0
        /// <summary>
        /// Loads the data for the request asynchronously.
        /// </summary>
        /// <param name="request">The issued request.</param>
        /// <returns>The active download.</returns>
        public virtual IDownload DownloadAsync(DocumentRequest request)
        {
            var data = new Request
            {
                Address = request.Target,
                Content = request.Body,
                Method = request.Method
            };

            foreach (var header in request.Headers)
            {
                data.Headers[header.Key] = header.Value;
            }

            var cookie = GetCookie(request.Target);

            if (cookie != null)
            {
                data.Headers[HeaderNames.Cookie] = cookie;
            }

            return DownloadAsync(data, request.Source);
        }
Esempio n. 15
0
        public async Task InvokeAboutUrlWithQueryShouldFire()
        {
            var about = new AboutRequester();
            var requests = new List<IRequest>();
            var req = new Request
            {
                Address = Url.Create("about://cache?device=memory"),
                Method = HttpMethod.Get
            };

            about.SetRoute("cache", (request, cancel) =>
            {
                requests.Add(request);
                return Task.FromResult(default(IResponse));
            });

            var response = await about.RequestAsync(req, CancellationToken.None);
            Assert.IsNull(response);
            Assert.AreEqual(1, requests.Count);
            Assert.AreEqual(req.Address.Data, requests[0].Address.Data);
        }
Esempio n. 16
0
        public async Task DataGetRequestForSmallImageBase64Encoded()
        {
            var http = new DataRequester();
            var request = new Request();
            var content = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==";
            request.Address = new Url(content);
            request.Method = HttpMethod.Get;

            using (var response = await http.RequestAsync(request, CancellationToken.None))
            {
                Assert.IsNotNull(response);
                Assert.AreEqual(200, (int)response.StatusCode);
                Assert.IsTrue(response.Content.CanRead);
                Assert.IsTrue(response.Headers.Count > 0);
                Assert.AreEqual("image/png", response.Headers[HeaderNames.ContentType]);
            }
        }
Esempio n. 17
0
        /// <summary>
        /// Loads the given URI by using an asynchronous request.
        /// </summary>
        /// <param name="request">The data of the request to send.</param>
        /// <param name="cancel">The cancellation token to use..</param>
        /// <returns>
        /// The task which will eventually return the response.
        /// </returns>
        protected async Task<IResponse> LoadAsync(Request request, CancellationToken cancel)
        {
            foreach (var requester in _requesters)
            {
                if (requester.SupportsProtocol(request.Address.Scheme))
                {
                    var events = _configuration.Events;
                    var evt = new RequestStartEvent(requester, request);

                    if (events != null)
                    {
                        events.Publish(evt);
                    }

                    var result = await requester.RequestAsync(request, cancel).ConfigureAwait(false);
                    evt.FireEnd();
                    return result;
                }
            }

            return default(IResponse);
        }
Esempio n. 18
0
        public async Task RequestUserAgentString()
        {
            if (Helper.IsNetworkAvailable())
            {
                var agent = "MyAgent";
                var http = new HttpRequester(agent);
                var request = new Request();
                request.Address = new Url("http://httpbin.org/user-agent");
                request.Method = HttpMethod.Get;

                using (var response = await http.RequestAsync(request, CancellationToken.None))
                {
                    Assert.IsNotNull(response);
                    Assert.AreEqual(200, (int)response.StatusCode);
                    Assert.IsTrue(response.Content.CanRead);
                    Assert.IsTrue(response.Headers.Count > 0);

                    var stream = new StreamReader(response.Content);
                    Assert.IsNotNull(stream);

                    var content = stream.ReadToEnd();
                    Assert.IsTrue(content.Length > 0);
                    Assert.AreEqual("{\n  \"user-agent\": \"" + agent + "\"\n}\n", content);
                }
            }
        }
Esempio n. 19
0
        public async Task MethodNotAllowedOnHttpPut()
        {
            if (Helper.IsNetworkAvailable())
            {
                var http = new HttpRequester();
                var request = new Request();
                request.Address = new Url("http://httpbin.org/get");
                request.Method = HttpMethod.Put;
                request.Content = Helper.StreamFromString("Should be ignored");

                using (var response = await http.RequestAsync(request, CancellationToken.None))
                {
                    Assert.IsNotNull(response);
                    Assert.AreEqual(405, (int)response.StatusCode);
                    Assert.IsTrue(response.Content.CanRead);
                    Assert.IsTrue(response.Headers.Count > 0);
                }
            }
        }
Esempio n. 20
0
        public async Task SimpleHttpPutRequest()
        {
            if (Helper.IsNetworkAvailable())
            {
                var http = new HttpRequester();
                var request = new Request();
                request.Address = new Url("http://httpbin.org/put");
                request.Method = HttpMethod.Put;
                request.Content = Helper.StreamFromString("PUT THIS THING BACK");

                using (var response = await http.RequestAsync(request, CancellationToken.None))
                {
                    Assert.IsNotNull(response);
                    Assert.AreEqual(200, (int)response.StatusCode);
                    Assert.IsTrue(response.Content.CanRead);
                    Assert.IsTrue(response.Headers.Count > 0);

                    var stream = new StreamReader(response.Content);
                    Assert.IsNotNull(stream);

                    var content = stream.ReadToEnd();
                    Assert.IsTrue(content.Length > 0);
                    Assert.IsTrue(content.Contains("\"data\": \"PUT THIS THING BACK\""));
                }
            }
        }
Esempio n. 21
0
        public async Task SimpleHttpDeleteRequest()
        {
            if (Helper.IsNetworkAvailable())
            {
                var http = new HttpRequester();
                var request = new Request
                {
                    Address = new Url("http://httpbin.org/delete"),
                    Method = HttpMethod.Delete,
                    Content = Helper.StreamFromString("Should be ignored")
                };

                using (var response = await http.RequestAsync(request, CancellationToken.None))
                {
                    Assert.IsNotNull(response);
                    Assert.AreEqual(200, (int)response.StatusCode);
                    Assert.IsTrue(response.Content.CanRead);
                    Assert.IsTrue(response.Headers.Count > 0);
                }
            }
        }