-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathBaseUrlService.cs
More file actions
115 lines (106 loc) · 3.8 KB
/
BaseUrlService.cs
File metadata and controls
115 lines (106 loc) · 3.8 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System;
using System.Net.Http;
namespace JSONAPI.Http
{
/// <summary>
/// Default implementation of IBaseUrlService
/// </summary>
public class BaseUrlService : IBaseUrlService
{
private string _contextPath = string.Empty;
private Uri _publicOrigin;
/// <summary>
/// Default constructor
/// </summary>
public BaseUrlService() { }
/// <summary>
/// Constructor which provides a context path for the routes of JSONAPI.NET
/// </summary>
/// <param name="contextPath">context path for the routes</param>
public BaseUrlService(string contextPath)
{
CleanContextPath(contextPath);
}
/// <summary>
/// Constructor which provides a public origin host and a context path for the routes of JSONAPI.NET.
/// If only public origin is desired provide emtpy string to contextPath.
/// </summary>
/// <param name="publicOrigin">public hostname</param>
/// <param name="contextPath">context path for the routes</param>
public BaseUrlService(Uri publicOrigin, string contextPath)
{
CleanContextPath(contextPath);
this._publicOrigin = publicOrigin;
}
/// <summary>
/// Retrieve the base path to provide in responses.
/// </summary>
/// <param name="requestMessage"></param>
/// <returns></returns>
public virtual string GetBaseUrl(HttpRequestMessage requestMessage)
{
string pathAndQuery;
string absolutUri = requestMessage.RequestUri.AbsoluteUri;
if (_publicOrigin != null)
{
var publicUriBuilder = new UriBuilder(absolutUri)
{
Host = _publicOrigin.Host,
Scheme = _publicOrigin.Scheme,
Port = _publicOrigin.Port
};
absolutUri = publicUriBuilder.Uri.AbsoluteUri;
pathAndQuery = publicUriBuilder.Uri.PathAndQuery;
}
else
{
pathAndQuery = requestMessage.RequestUri.PathAndQuery;
}
pathAndQuery = RemoveFromBegin(pathAndQuery, GetContextPath());
pathAndQuery= pathAndQuery.TrimStart('/');
var baseUrl = RemoveFromEnd(absolutUri, pathAndQuery);
return baseUrl;
}
/// <summary>
/// Provides the context path to serve JSONAPI.NET without leading and trailing slash.
/// </summary>
/// <returns></returns>
public string GetContextPath()
{
return _contextPath;
}
/// <summary>
/// Makes sure thre are no slashes at the beginnig or end.
/// </summary>
/// <param name="contextPath"></param>
private void CleanContextPath(string contextPath)
{
if (!string.IsNullOrEmpty(contextPath) && !contextPath.EndsWith("/"))
{
contextPath = contextPath.TrimEnd('/');
}
if (!string.IsNullOrEmpty(contextPath) && contextPath.StartsWith("/"))
{
contextPath = contextPath.TrimStart('/');
}
_contextPath = contextPath;
}
private string RemoveFromEnd(string input, string suffix)
{
if (input.EndsWith(suffix))
{
return input.Substring(0, input.Length - suffix.Length);
}
return input;
}
private string RemoveFromBegin(string input, string prefix)
{
prefix = "/" + prefix;
if (input.StartsWith(prefix))
{
return input.Substring(prefix.Length, input.Length - prefix.Length);
}
return input;
}
}
}