Пример #1
0
        public override Task ProcessRequestAsync(HttpContextBase context)
        {
            string url = context.Request.QueryString["url"];

            Uri uri;

            if (String.IsNullOrEmpty(url) ||
                !ImageContentProvider.IsValidImagePath(url) ||
                !Uri.TryCreate(url, UriKind.Absolute, out uri))
            {
                context.Response.StatusCode = 404;
                return(TaskAsyncHelper.Empty);
            }

            // TODO: Add caching
            var request     = (HttpWebRequest)WebRequest.Create(url);
            var requestTask = Task.Factory.FromAsync((cb, state) => request.BeginGetResponse(cb, state),
                                                     ar => (HttpWebResponse)request.EndGetResponse(ar), null);

            return(requestTask.Then(response =>
            {
                context.Response.ContentType = response.ContentType;
                context.Response.StatusCode = (int)response.StatusCode;
                context.Response.StatusDescription = response.StatusDescription;

                using (response)
                {
                    using (Stream stream = response.GetResponseStream())
                    {
                        // TODO: Make this async
                        stream.CopyTo(context.Response.OutputStream);
                    }
                }
            }));
        }
Пример #2
0
        public async Task Invoke(IDictionary <string, object> env)
        {
            var httpRequest  = new Gate.Request(env);
            var httpResponse = new OwinResponse(env);

            string url;
            Uri    uri;

            if (!httpRequest.Query.TryGetValue("url", out url) ||
                String.IsNullOrEmpty(url) ||
                !Uri.TryCreate(url, UriKind.Absolute, out uri) ||
                !ImageContentProvider.IsValidImagePath(uri) ||
                !IsAuthenticated(env))
            {
                httpResponse.StatusCode = 404;
                return;
            }

            try
            {
                var request = (HttpWebRequest)WebRequest.Create(url);
                request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.Default);
                var response = (HttpWebResponse)await request.GetResponseAsync();

                if (!ImageContentProvider.IsValidContentType(response.ContentType) &&
                    response.ContentLength > _settings.ProxyImageMaxSizeBytes)
                {
                    httpResponse.StatusCode = 404;
                    return;
                }

                httpResponse.SetHeader("Content-Type", response.ContentType);
                httpResponse.StatusCode = (int)response.StatusCode;

                using (response)
                {
                    using (Stream stream = response.GetResponseStream())
                    {
                        await stream.CopyToAsync(httpResponse.Body);
                    }
                }
            }
            catch
            {
                httpResponse.StatusCode = 404;
            }
        }
Пример #3
0
        public async Task Invoke(IDictionary <string, object> env)
        {
            var httpRequest = new OwinRequest(env);

            if (!httpRequest.Path.StartsWith(_path))
            {
                await _next(env);

                return;
            }

            var httpResponse = new OwinResponse(env);

            var qs = new QueryStringCollection(httpRequest.QueryString);

            string url = qs["url"];

            Uri uri;

            if (String.IsNullOrEmpty(url) ||
                !ImageContentProvider.IsValidImagePath(url) ||
                !Uri.TryCreate(url, UriKind.Absolute, out uri))
            {
                httpResponse.StatusCode = 404;
                await TaskAsyncHelper.Empty;
                return;
            }

            var request = (HttpWebRequest)WebRequest.Create(url);

            request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.Default);
            var response = (HttpWebResponse)await request.GetResponseAsync();

            httpResponse.SetHeader("ContentType", response.ContentType);
            httpResponse.StatusCode = (int)response.StatusCode;

            using (response)
            {
                using (Stream stream = response.GetResponseStream())
                {
                    await stream.CopyToAsync(httpResponse.Body);
                }
            }
        }
Пример #4
0
        public override Task ProcessRequestAsync(HttpContextBase context)
        {
            string url = context.Request.QueryString["url"];

            Uri uri;

            if (String.IsNullOrEmpty(url) ||
                !ImageContentProvider.IsValidImagePath(url) ||
                !Uri.TryCreate(url, UriKind.Absolute, out uri))
            {
                context.Response.StatusCode = 404;
                return(TaskAsyncHelper.Empty);
            }

            // Since we only handle requests for imgur and other random images, just cached based on the url
            // context.Response.Cache.SetCacheability(HttpCacheability.Public);
            // context.Response.Cache.SetMaxAge(TimeSpan.MaxValue);
            // context.Response.Cache.SetLastModified(DateTime.Now);

            var request = (HttpWebRequest)WebRequest.Create(url);

            request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.Default);
            var requestTask = Task.Factory.FromAsync((cb, state) => request.BeginGetResponse(cb, state),
                                                     ar => (HttpWebResponse)request.EndGetResponse(ar), null);

            return(requestTask.Then(response =>
            {
                context.Response.ContentType = response.ContentType;
                context.Response.StatusCode = (int)response.StatusCode;
                context.Response.StatusDescription = response.StatusDescription;

                using (response)
                {
                    using (Stream stream = response.GetResponseStream())
                    {
                        // TODO: Make this async
                        stream.CopyTo(context.Response.OutputStream);
                    }
                }
            }));
        }