2019-05-15 14:56:09 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* (c) Copyright Ascensio System Limited 2010-2018
|
|
|
|
*
|
|
|
|
* This program is freeware. You can redistribute it and/or modify it under the terms of the GNU
|
|
|
|
* General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html).
|
|
|
|
* In accordance with Section 7(a) of the GNU GPL its Section 15 shall be amended to the effect that
|
|
|
|
* Ascensio System SIA expressly excludes the warranty of non-infringement of any third-party rights.
|
|
|
|
*
|
|
|
|
* THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. For more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html
|
|
|
|
*
|
|
|
|
* You can contact Ascensio System SIA by email at sales@onlyoffice.com
|
|
|
|
*
|
|
|
|
* The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display
|
|
|
|
* Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3.
|
|
|
|
*
|
|
|
|
* Pursuant to Section 7 § 3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains
|
|
|
|
* relevant author attributions when distributing the software. If the display of the logo in its graphic
|
|
|
|
* form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE"
|
|
|
|
* in every copy of the program you distribute.
|
|
|
|
* Pursuant to Section 7 § 3(e) we decline to grant you any rights under trademark law for use of our trademarks.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
using System.Globalization;
|
2019-08-15 12:04:42 +00:00
|
|
|
using System.Reflection;
|
2020-08-27 14:01:37 +00:00
|
|
|
|
2019-05-15 14:56:09 +00:00
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
using Microsoft.AspNetCore.Http.Extensions;
|
2019-08-15 12:04:42 +00:00
|
|
|
using Microsoft.AspNetCore.Http.Features;
|
2021-05-17 11:35:00 +00:00
|
|
|
using Microsoft.Net.Http.Headers;
|
2020-08-27 14:01:37 +00:00
|
|
|
|
2019-05-15 14:56:09 +00:00
|
|
|
using HttpContext = Microsoft.AspNetCore.Http.HttpContext;
|
|
|
|
|
|
|
|
namespace System.Web
|
|
|
|
{
|
|
|
|
public static class HttpRequestExtensions
|
|
|
|
{
|
|
|
|
public static readonly string UrlRewriterHeader = "X-REWRITER-URL";
|
|
|
|
|
|
|
|
|
|
|
|
public static Uri GetUrlRewriter(this HttpRequest request)
|
|
|
|
{
|
2019-08-08 09:26:58 +00:00
|
|
|
return request != null ? GetUrlRewriter(request.Headers, request) : null;
|
2019-06-25 07:34:27 +00:00
|
|
|
}
|
|
|
|
public static Uri Url(this HttpRequest request)
|
|
|
|
{
|
2019-08-15 12:04:42 +00:00
|
|
|
return request != null ? new Uri(request.GetDisplayUrl()) : null;
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*public static Uri GetUrlRewriter(this HttpRequestBase request)
|
|
|
|
{
|
|
|
|
return request != null ? GetUrlRewriter(request.Headers, request.Url) : null;
|
|
|
|
}*/
|
|
|
|
|
2019-08-08 09:26:58 +00:00
|
|
|
public static Uri GetUrlRewriter(IHeaderDictionary headers, HttpRequest request)
|
2019-05-15 14:56:09 +00:00
|
|
|
{
|
2019-08-28 09:15:33 +00:00
|
|
|
if (headers != null)
|
|
|
|
{
|
|
|
|
var h = headers[UrlRewriterHeader];
|
|
|
|
var rewriterUri = !string.IsNullOrEmpty(h) ? ParseRewriterUrl(h) : null;
|
|
|
|
if (request != null && rewriterUri != null)
|
2019-05-15 14:56:09 +00:00
|
|
|
{
|
2019-08-28 09:15:33 +00:00
|
|
|
var result = new UriBuilder()
|
2019-05-15 14:56:09 +00:00
|
|
|
{
|
|
|
|
Scheme = rewriterUri.Scheme,
|
|
|
|
Host = rewriterUri.Host,
|
|
|
|
Port = rewriterUri.Port
|
2019-08-28 09:15:33 +00:00
|
|
|
};
|
|
|
|
result.Query = request.QueryString.Value;
|
|
|
|
result.Path = request.Path.Value;
|
2019-05-15 14:56:09 +00:00
|
|
|
return result.Uri;
|
|
|
|
}
|
2019-08-28 09:15:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (request != null && request.Query != null)
|
|
|
|
{
|
|
|
|
var h = request.Query[UrlRewriterHeader];
|
|
|
|
var rewriterUri = !string.IsNullOrEmpty(h) ? ParseRewriterUrl(h) : null;
|
2019-05-15 14:56:09 +00:00
|
|
|
if (rewriterUri != null)
|
|
|
|
{
|
2019-08-28 09:15:33 +00:00
|
|
|
var result = new UriBuilder()
|
2019-05-15 14:56:09 +00:00
|
|
|
{
|
|
|
|
Scheme = rewriterUri.Scheme,
|
|
|
|
Host = rewriterUri.Host,
|
|
|
|
Port = rewriterUri.Port
|
2019-08-28 09:15:33 +00:00
|
|
|
};
|
|
|
|
result.Query = request.QueryString.Value;
|
|
|
|
result.Path = request.Path.Value;
|
|
|
|
|
2019-05-15 14:56:09 +00:00
|
|
|
return result.Uri;
|
|
|
|
}
|
2019-08-28 09:15:33 +00:00
|
|
|
}
|
2019-05-15 14:56:09 +00:00
|
|
|
|
2019-08-08 09:26:58 +00:00
|
|
|
return request.Url();
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static Uri PushRewritenUri(this HttpContext context)
|
|
|
|
{
|
|
|
|
return context != null ? PushRewritenUri(context, GetUrlRewriter(context.Request)) : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Uri PushRewritenUri(this HttpContext context, Uri rewrittenUri)
|
|
|
|
{
|
|
|
|
Uri oldUri = null;
|
|
|
|
if (context != null)
|
|
|
|
{
|
|
|
|
var request = context.Request;
|
|
|
|
|
|
|
|
var url = new Uri(request.GetDisplayUrl());
|
|
|
|
|
|
|
|
if (url != rewrittenUri)
|
|
|
|
{
|
|
|
|
var requestUri = url;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
//Push it
|
|
|
|
request.Headers.SetCommaSeparatedValues("HTTPS", rewrittenUri.Scheme.Equals("https", StringComparison.OrdinalIgnoreCase) ? "on" : "off");
|
|
|
|
request.Headers.SetCommaSeparatedValues("SERVER_NAME", rewrittenUri.Host);
|
|
|
|
request.Headers.SetCommaSeparatedValues("SERVER_PORT",
|
|
|
|
rewrittenUri.Port.ToString(CultureInfo.InvariantCulture));
|
|
|
|
|
|
|
|
if (rewrittenUri.IsDefaultPort)
|
|
|
|
{
|
|
|
|
request.Headers.SetCommaSeparatedValues("HTTP_HOST",
|
|
|
|
rewrittenUri.Host);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
request.Headers.SetCommaSeparatedValues("HTTP_HOST",
|
|
|
|
rewrittenUri.Host + ":" + requestUri.Port);
|
|
|
|
}
|
|
|
|
//Hack:
|
2019-08-15 12:04:42 +00:00
|
|
|
typeof(HttpRequest).InvokeMember("_url",
|
2019-05-15 14:56:09 +00:00
|
|
|
BindingFlags.NonPublic | BindingFlags.SetField |
|
|
|
|
BindingFlags.Instance,
|
|
|
|
null, request,
|
|
|
|
new object[] { null });
|
|
|
|
oldUri = requestUri;
|
|
|
|
context.Items["oldUri"] = oldUri;
|
|
|
|
|
|
|
|
}
|
|
|
|
catch (Exception)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return oldUri;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Uri PopRewritenUri(this HttpContext context)
|
|
|
|
{
|
|
|
|
if (context != null && context.Items["oldUri"] != null)
|
|
|
|
{
|
|
|
|
var rewriteTo = context.Items["oldUri"] as Uri;
|
|
|
|
if (rewriteTo != null)
|
|
|
|
{
|
|
|
|
return PushRewritenUri(context, rewriteTo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static bool DesktopApp(this HttpRequest request)
|
2021-05-17 11:35:00 +00:00
|
|
|
{
|
|
|
|
return request != null
|
|
|
|
&& (!string.IsNullOrEmpty(request.Query["desktop"])
|
|
|
|
|| !string.IsNullOrEmpty(request.Headers[HeaderNames.UserAgent]) && request.Headers[HeaderNames.UserAgent].ToString().Contains("AscDesktopEditor"));
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static bool SailfishApp(this HttpRequest request)
|
|
|
|
{
|
|
|
|
return request != null
|
|
|
|
&& (!string.IsNullOrEmpty(request.Headers["sailfish"])
|
2021-05-17 11:35:00 +00:00
|
|
|
|| !string.IsNullOrEmpty(request.Headers[HeaderNames.UserAgent]) && request.Headers[HeaderNames.UserAgent].ToString().Contains("SailfishOS"));
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static Uri ParseRewriterUrl(string s)
|
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(s))
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const StringComparison cmp = StringComparison.OrdinalIgnoreCase;
|
2022-01-21 13:46:30 +00:00
|
|
|
if (0 < s.Length && s.StartsWith('0'))
|
2019-05-15 14:56:09 +00:00
|
|
|
{
|
|
|
|
s = Uri.UriSchemeHttp + s.Substring(1);
|
|
|
|
}
|
|
|
|
else if (3 < s.Length && s.StartsWith("OFF", cmp))
|
|
|
|
{
|
|
|
|
s = Uri.UriSchemeHttp + s.Substring(3);
|
|
|
|
}
|
2022-01-21 13:46:30 +00:00
|
|
|
else if (0 < s.Length && s.StartsWith('1'))
|
2019-05-15 14:56:09 +00:00
|
|
|
{
|
|
|
|
s = Uri.UriSchemeHttps + s.Substring(1);
|
|
|
|
}
|
|
|
|
else if (2 < s.Length && s.StartsWith("ON", cmp))
|
|
|
|
{
|
|
|
|
s = Uri.UriSchemeHttps + s.Substring(2);
|
|
|
|
}
|
|
|
|
else if (s.StartsWith(Uri.UriSchemeHttp + "%3A%2F%2F", cmp) || s.StartsWith(Uri.UriSchemeHttps + "%3A%2F%2F", cmp))
|
|
|
|
{
|
|
|
|
s = HttpUtility.UrlDecode(s);
|
|
|
|
}
|
|
|
|
|
2019-08-15 13:05:50 +00:00
|
|
|
Uri.TryCreate(s, UriKind.Absolute, out var result);
|
2019-05-15 14:56:09 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static string GetUserHostAddress(this HttpRequest request)
|
|
|
|
{
|
|
|
|
return request.HttpContext.Features.Get<IHttpConnectionFeature>()?.RemoteIpAddress.ToString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|