-
Notifications
You must be signed in to change notification settings - Fork 7
/
RequestResponseExtensions.cs
64 lines (57 loc) · 2.18 KB
/
RequestResponseExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using System;
using System.Linq;
using Nancy;
namespace Argolis.Hydra.Nancy
{
/// <summary>
/// Wires Hydra with the application
/// </summary>
public static class RequestResponseExtensions
{
private const string LinkHeaderFormat = "<{0}>; rel=\"{1}\"";
/// <summary>
/// Appends a Link header to the response.
/// </summary>
/// <param name="response">The response.</param>
/// <param name="uri">The link URI.</param>
/// <param name="relation">The link relation.</param>
/// <remarks>See https://tools.ietf.org/html/rfc5988</remarks>
public static void AppendLinkHeader(this Response response, string uri, string relation)
{
const string linkHeader = "Link";
var headerValue = string.Format(LinkHeaderFormat, uri, relation);
if (response.Headers.ContainsKey(linkHeader))
{
string current = response.Headers[linkHeader];
response.WithHeader(linkHeader, current + ", " + headerValue);
}
else
{
response.WithHeader(linkHeader, headerValue);
}
}
/// <summary>
/// Gets the API documentation URI.
/// </summary>
/// <param name="request">The request.</param>
/// <param name="documentationPath">The documentation path.</param>
/// <returns>absolute URI string</returns>
public static string GetApiDocumentationUri(this Request request, string documentationPath)
{
var uriBuilder = new UriBuilder(request.Url.SiteBase)
{
Path = request.Url.BasePath + documentationPath
};
if (request.Headers["X-Forwarded-Proto"].Contains("https", StringComparer.OrdinalIgnoreCase))
{
uriBuilder.Scheme = "https";
}
string apiDocPath = uriBuilder.Uri.ToString();
if (uriBuilder.Port == 80)
{
apiDocPath = uriBuilder.Uri.GetComponents(UriComponents.AbsoluteUri & ~UriComponents.Port, UriFormat.UriEscaped);
}
return apiDocPath;
}
}
}