Exemple #1
0
        public override RequestStatus ProcessRequest(ResourceResponse request, bool prioritized)
        {
            try
            {
                var httpRequest = WebRequest.CreateHttp(request.Uri);
                httpRequest.Accept = request.Accept;

                using (var httpResponse = httpRequest.GetResponse() as HttpWebResponse)
                {
                    if (httpResponse.StatusCode == HttpStatusCode.NotFound)
                    {
                        return(RequestStatus.NotFound);
                    }

                    if (httpResponse.StatusCode != HttpStatusCode.OK)
                    {
                        return(RequestStatus.ProtocolError);
                    }

                    var resourceStream = new ResourceStream(httpResponse);
                    request.OnResponseStream(resourceStream);
                    return(request.Status);
                }
            }
            catch (Exception exc)
            {
                request.Fault = exc;
                return(RequestStatus.Faulted);
            }
        }
Exemple #2
0
        private bool ValidateContentTypeForImage(ResourceStream stream)
        {
            switch (stream.ContentType.ToLowerInvariant())
            {
            case "image/jpg":
            case "image/jpeg":
            case "image/png":
            case "image/bmp":
            case "image/gif":
                return(true);

            default:
                return(false);
            }
        }
Exemple #3
0
 private void OnPostImageResponse(Uri uri, ResourceStream stream)
 {
     if (!ValidateContentTypeForImage(stream))
     {
         return;
     }
     try
     {
         _postImage = Image.Load <Rgba32>(stream);
     }
     catch (Exception exc)
     {
         IsPostImageFaulted = true;
         Console.WriteLine(exc);
     }
     IsPostImageDownloaded = true;
 }
        public override RequestStatus ProcessRequest(ResourceResponse request, bool prioritized)
        {
            var headers = new WebHeaderCollection();

            if (!string.IsNullOrWhiteSpace(request.Accept))
            {
                headers.Add(HttpRequestHeader.Accept, request.Accept);
            }

            string path = UriToCachePath(request.Uri);

            if (!File.Exists(path))
            {
                return(RequestStatus.NotFound);
            }

            var resourceStream = new ResourceStream(File.OpenRead(path), headers);

            request.OnResponseStream(resourceStream);
            return(request.Status);
        }