Permalink
Browse files

Fix Link header non-ASCII characters issue (#2903)

  • Loading branch information...
sergeykonkin authored and khellang committed Jun 1, 2018
1 parent 68e0b9f commit de458a9b42db6478e0c2bb8adef0f9fa342a2674
@@ -9,6 +9,7 @@
using Nancy.Conventions;
using Nancy.Extensions;
+ using Nancy.Helpers;
/// <summary>
/// The default implementation for a response negotiator.
@@ -82,7 +83,7 @@ private static bool TryCastResultToResponse(dynamic routeResult, out Response re
if (routeResult is Response)
{
response = (Response)routeResult;
- return true;
+ return true;
}
var methods = responseType.GetMethods(BindingFlags.Public | BindingFlags.Static);
@@ -364,7 +365,7 @@ private void AddLinkHeader(IEnumerable<CompatibleHeader> compatibleHeaders, Resp
/// <returns>The link header.</returns>
protected virtual string CreateLinkHeader(Url requestUrl, IEnumerable<KeyValuePair<string, MediaRange>> linkProcessors, string existingLinkHeader)
{
- var fileName = Path.GetFileNameWithoutExtension(requestUrl.Path);
+ var fileName = HttpUtility.UrlEncode(Path.GetFileNameWithoutExtension(requestUrl.Path));
var baseUrl = string.Concat(requestUrl.BasePath, "/", fileName);
var linkBuilder = new HttpLinkBuilder();
@@ -0,0 +1,43 @@
+namespace Nancy.Tests.Unit.Responses.Negotiation
+{
+ using System;
+ using System.Collections.Generic;
+ using System.Linq;
+
+ using Nancy.Responses.Negotiation;
+
+ using Xunit;
+
+ public class DefaultResponseNegotiatorFixture
+ {
+ [Fact]
+ public void Should_encode_URL_in_Link_response_header()
+ {
+ // Given
+ var negotiator = new DefaultResponseNegotiatorWrapper();
+ var requestUrl = new Uri("http://localhost:80/george-å");
+ var range = new MediaRange("application/vnd.nancy");
+ var linkProcessor = new KeyValuePair<string, MediaRange>("nancy", range);
+
+ // When
+ var linkHeader = negotiator.CreateLinkHeaderWrapper(requestUrl, new[] { linkProcessor }, null);
+
+ // Then
+ Assert.DoesNotContain("å", linkHeader, StringComparison.InvariantCultureIgnoreCase);
+ Assert.Contains("%C3%A5", linkHeader, StringComparison.InvariantCultureIgnoreCase);
+ }
+
+ private class DefaultResponseNegotiatorWrapper : DefaultResponseNegotiator
+ {
+ public DefaultResponseNegotiatorWrapper()
+ : base(Enumerable.Empty<IResponseProcessor>(), null)
+ {
+ }
+
+ public string CreateLinkHeaderWrapper(Url requestUrl, IEnumerable<KeyValuePair<string, MediaRange>> linkProcessors, string existingLinkHeader)
+ {
+ return base.CreateLinkHeader(requestUrl, linkProcessors, existingLinkHeader);
+ }
+ }
+ }
+}

0 comments on commit de458a9

Please sign in to comment.