-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextFile.txt
103 lines (83 loc) · 3.93 KB
/
TextFile.txt
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
/// <summary>
/// Gets store location
/// </summary>
/// <param name="useSsl">Whether to get SSL secured URL; pass null to determine automatically</param>
/// <returns>Store location</returns>
public virtual string GetStoreLocation(bool? useSsl = null)
{
var storeLocation = string.Empty;
//get store host
var storeHost = GetStoreHost(useSsl ?? IsCurrentConnectionSecured());
if (!string.IsNullOrEmpty(storeHost))
{
//add application path base if exists
storeLocation = IsRequestAvailable() ? $"{storeHost.TrimEnd('/')}{_httpContextAccessor.HttpContext.Request.PathBase}" : storeHost;
}
//if host is empty (it is possible only when HttpContext is not available), use URL of a store entity configured in admin area
if (string.IsNullOrEmpty(storeHost) && DataSettingsHelper.DatabaseIsInstalled())
{
var currentStore = _httpContextAccessor.HttpContext.RequestServices.GetRequiredService<IStoreContext>().CurrentStore;
if (currentStore != null)
storeLocation = !currentStore.SslEnabled ? currentStore.Url : currentStore.SecureUrl;
else
throw new Exception("Current store cannot be loaded");
}
//ensure that URL is ended with slash
storeLocation = $"{storeLocation.TrimEnd('/')}/";
return storeLocation;
}
/// <summary>
/// Gets store host location
/// </summary>
/// <param name="useSsl">Whether to get SSL secured URL</param>
/// <returns>Store host location</returns>
public virtual string GetStoreHost(bool useSsl)
{
if (!IsRequestAvailable())
return string.Empty;
//try to get host from the request HOST header
var hostHeader = _httpContextAccessor.HttpContext.Request.Headers[HeaderNames.Host];
if (StringValues.IsNullOrEmpty(hostHeader))
return string.Empty;
//add scheme to the URL
var storeHost = $"{(useSsl ? Uri.UriSchemeHttps : Uri.UriSchemeHttp)}://{hostHeader.FirstOrDefault()}";
//ensure that host is ended with slash
storeHost = $"{storeHost.TrimEnd('/')}/";
return storeHost;
}
/// <summary>
/// Gets a value indicating whether current connection is secured
/// </summary>
/// <returns>true - secured, false - not secured</returns>
public virtual bool IsCurrentConnectionSecured()
{
if (!IsRequestAvailable())
return false;
//check whether hosting uses a load balancer
//use HTTP_CLUSTER_HTTPS?
if (_hostingConfig.UseHttpClusterHttps)
return _httpContextAccessor.HttpContext.Request.Headers["HTTP_CLUSTER_HTTPS"].ToString().Equals("on", StringComparison.OrdinalIgnoreCase);
//use HTTP_X_FORWARDED_PROTO?
if (_hostingConfig.UseHttpXForwardedProto)
return _httpContextAccessor.HttpContext.Request.Headers["X-Forwarded-Proto"].ToString().Equals("https", StringComparison.OrdinalIgnoreCase);
return _httpContextAccessor.HttpContext.Request.IsHttps;
}
/// <summary>
/// Check whether current HTTP request is available
/// </summary>
/// <returns>True if available; otherwise false</returns>
protected virtual bool IsRequestAvailable()
{
if (_httpContextAccessor == null || _httpContextAccessor.HttpContext == null)
return false;
try
{
if (_httpContextAccessor.HttpContext.Request == null)
return false;
}
catch (Exception)
{
return false;
}
return true;
}