aboutsummaryrefslogtreecommitdiffstats
path: root/src/IOL.Helpers/HttpRequestHelpers.cs
blob: 60dbd909d1f944f69be362e6a029cc35fcd5c94d (plain) (blame)
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
using Microsoft.AspNetCore.Http;

namespace IOL.Helpers
{
	public static class HttpRequestHelpers
	{
		/// <summary>
		/// Get's the scheme and host (scheme://host) value of the current HttpRequest
		/// </summary>
		/// <param name="request">HttpRequest to retrieve value from</param>
		/// <param name="ignoreForwared">Ignore header values like X-Forwarded-Host|Proto</param>
		/// <returns></returns>
		public static string GetRequestHost(this HttpRequest request, bool ignoreForwared = false) {
			if (!ignoreForwared) {
				var forwardedHostHeader = request.Headers["X-Forwarded-Host"].ToString();
				var forwardedProtoHeader = request.Headers["X-Forwarded-Proto"].ToString();
				if (forwardedHostHeader.HasValue()) {
					return (forwardedProtoHeader ?? "https") + "://" + forwardedHostHeader;
				}
			}

			return request.Scheme + "://" + request.Host;
		}
	}
}