コード例 #1
0
        /// <summary>
        /// Renders the requested identicon and returns it to the client.
        /// </summary>
        /// <param name="context"><see cref="HttpContext"/> with the current request and response.</param>
        public void ProcessRequest(HttpContext context)
        {
            if (IdenticonRequest.TryParse(context.Request.Url.Query, out var request))
            {
                // The urls are permanent so it is ok to cache icons for a long time
                context.Response.Cache.SetCacheability(HttpCacheability.Public);
                context.Response.Cache.SetMaxAge(TimeSpan.FromDays(30));

                var icon = Identicon.FromHash(request.Hash, request.Size);
                icon.Style = request.Style;

                switch (request.Format)
                {
                case ExportImageFormat.Png:
                    context.Response.ContentType = "image/png";
                    icon.SaveAsPng(context.Response.OutputStream);
                    break;

                case ExportImageFormat.Svg:
                    context.Response.ContentType = "image/svg+xml";
                    icon.SaveAsSvg(context.Response.OutputStream);
                    break;

                default:
                    throw new NotSupportedException($"The image format '{request.Format}' is not supported by {nameof(IdenticonHttpHandler)}.");
                }
            }
            else
            {
                throw new HttpException(404, "Icon not found");
            }
        }
コード例 #2
0
        public static string Create(HttpResponseBase response, byte[] hash, int size, ExportImageFormat format, IdenticonStyle style)
        {
            var parameters = new IdenticonRequest
            {
                Hash   = hash,
                Size   = size,
                Style  = style,
                Format = format
            };

            // Percent-encoding not needed as IdenticonRequest generates query string safe strings.
            return(response.ApplyAppPathModifier($"~/identicon.axd?{parameters}"));
        }
コード例 #3
0
        public static string Create(HttpContext context, byte[] hash, int size, ExportImageFormat format, IdenticonStyle style)
        {
            IdenticonMiddleware.EnsureConstructed();

            var parameters = new IdenticonRequest
            {
                Hash   = hash,
                Size   = size,
                Style  = style,
                Format = format
            };

            // Percent-encoding not needed as IdenticonRequest generates query string safe strings.
            return(context.Request.PathBase.Add(PathPrefix + "/" + parameters).Value);
        }
コード例 #4
0
        /// <summary>
        /// Processes a request and determines whether it can be handled as an identicon request.
        /// </summary>
        /// <param name="context">Current context.</param>
        public Task InvokeAsync(HttpContext context)
        {
            var path = context.Request.Path;

            if (path.HasValue)
            {
                var requestString = context.Request.Path.Value.Trim('/');

                if (IdenticonRequest.TryParse(requestString, out var request))
                {
                    return(HandleRequestAsync(context, request));
                }
            }

            return(next(context));
        }
コード例 #5
0
        public void IdenticonRequest_Extended()
        {
            var url1 = new IdenticonRequest
            {
                Hash  = HashGenerator.ComputeHash("Hello", "SHA1"),
                Size  = 741,
                Style = new IdenticonStyle
                {
                    Padding             = 0.3f,
                    BackColor           = Color.Bisque,
                    ColorLightness      = Range.Create(0.25f, 0.75f),
                    GrayscaleLightness  = Range.Create(0, 1f),
                    ColorSaturation     = 0.4f,
                    GrayscaleSaturation = 0.1f,
                    Hues = new HueCollection {
                        1.5f, -0.25f, 0.8f
                    }
                }
            };

            var text = url1.ToString();

            Assert.IsTrue(IdenticonRequest.TryParse(text, out var url2));

            Assert.AreEqual(url1.Format, url2.Format);
            Assert.AreEqual(url1.Style.BackColor.ToRgba(), url2.Style.BackColor.ToRgba());
            AssertAreAlmostEqual(url1.Style.Padding, url2.Style.Padding);
            AssertAreAlmostEqual(url1.Style.ColorLightness.From, url2.Style.ColorLightness.From);
            AssertAreAlmostEqual(url1.Style.ColorLightness.To, url2.Style.ColorLightness.To);
            AssertAreAlmostEqual(url1.Style.GrayscaleLightness.From, url2.Style.GrayscaleLightness.From);
            AssertAreAlmostEqual(url1.Style.GrayscaleLightness.To, url2.Style.GrayscaleLightness.To);

#pragma warning disable 0618
            AssertAreAlmostEqual(url1.Style.ColorSaturation, url2.Style.Saturation);
#pragma warning restore 0618

            AssertAreAlmostEqual(url1.Style.ColorSaturation, url2.Style.ColorSaturation);
            AssertAreAlmostEqual(url1.Style.GrayscaleSaturation, url2.Style.GrayscaleSaturation);
            Assert.AreEqual(3, url2.Style.Hues.Count);
            AssertAreAlmostEqual(0.5f, url2.Style.Hues[0]);
            AssertAreAlmostEqual(0.75f, url2.Style.Hues[1]);
            AssertAreAlmostEqual(0.8f, url2.Style.Hues[2]);

            Assert.AreEqual(url1.Size, url2.Size);
        }
コード例 #6
0
        private async Task HandleRequestAsync(HttpContext context, IdenticonRequest request)
        {
            var icon = Identicon.FromHash(request.Hash, request.Size);

            icon.Style = request.Style;

            var    headers = context.Response.GetTypedHeaders();
            Stream data;
            string contentType;

            switch (request.Format)
            {
            case ExportImageFormat.Svg:
                data        = icon.SaveAsSvg();
                contentType = "image/svg+xml";
                break;

            default:
                data        = icon.SaveAsPng();
                contentType = "image/png";
                break;
            }

            try
            {
                context.Response.ContentType   = contentType;
                context.Response.ContentLength = data.Length;

                // The urls are permanent so it is ok to cache icons for a long time
                headers.CacheControl =
                    new CacheControlHeaderValue
                {
                    Public = true,
                    MaxAge = TimeSpan.FromDays(30)
                };

                await data.CopyToAsync(context.Response.Body);
            }
            finally
            {
                data?.Dispose();
            }
        }
コード例 #7
0
        public Task InvokeAsync(HttpContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var path = context.Request.Path;

            if (path.HasValue)
            {
                var requestString = context.Request.Path.Value.Trim('/');

                if (IdenticonRequest.TryParse(requestString, out var request))
                {
                    return(HandleRequestAsync(context, request));
                }
            }

            return(next(context));
        }
コード例 #8
0
        public void IdenticonRequest_Compact_SizeMultipleOf5()
        {
            var url1 = new IdenticonRequest
            {
                Hash = HashGenerator.ComputeHash("Hello", "SHA1"),
                Size = 65
            };

            var text = url1.ToString();

            Assert.IsTrue(IdenticonRequest.TryParse(text, out var url2));

            Assert.AreEqual(url1.Format, url2.Format);
            Assert.AreEqual(url1.Style.BackColor.ToRgba(), url2.Style.BackColor.ToRgba());
            AssertAreAlmostEqual(url1.Style.Padding, url2.Style.Padding);
            AssertAreAlmostEqual(url1.Style.ColorLightness.From, url2.Style.ColorLightness.From);
            AssertAreAlmostEqual(url1.Style.ColorLightness.To, url2.Style.ColorLightness.To);
            AssertAreAlmostEqual(url1.Style.GrayscaleLightness.From, url2.Style.GrayscaleLightness.From);
            AssertAreAlmostEqual(url1.Style.GrayscaleLightness.To, url2.Style.GrayscaleLightness.To);

            Assert.AreEqual(url1.Size, url2.Size);
        }
コード例 #9
0
        public void IdenticonRequest_DefaultStyle()
        {
            var url1 = new IdenticonRequest
            {
                Hash  = HashGenerator.ComputeHash("Hello", "SHA1"),
                Size  = 741,
                Style = new IdenticonStyle
                {
                    Padding = 0.3f
                }
            };

            try
            {
                var text = url1.ToString();
                Assert.IsTrue(IdenticonRequest.TryParse(text, out var url2));

                AssertAreAlmostEqual(0.3f, url2.Style.Padding);
                Identicon.DefaultStyle.Padding = 0.1f;
                AssertAreAlmostEqual(0.3f, url2.Style.Padding);

                Identicon.DefaultStyle.Padding = 0.3f;

                text = url1.ToString();
                Assert.IsTrue(IdenticonRequest.TryParse(text, out var url3));

                AssertAreAlmostEqual(0.3f, url3.Style.Padding);
                Identicon.DefaultStyle.Padding = 0.2f;

                Assert.IsTrue(IdenticonRequest.TryParse(text, out var url4));
                AssertAreAlmostEqual(0.2f, url4.Style.Padding);
            }
            finally
            {
                // Restore default style so that other tests are not affected
                Identicon.DefaultStyle = null;
            }
        }