private static void BeginRequest(object sender, EventArgs e) { try { if (CookiesManager.IsMobileBlocked()) { return; } var tenant = CoreContext.TenantManager.GetCurrentTenant(false); if (tenant != null && tenant.CreatedDateTime > lastMobileDate) { CookiesManager.SetCookies(CookiesType.NoMobile, "1", true); return; } var context = ((HttpApplication)sender).Context; var url = context.Request.GetUrlRewriter(); if (!IsRequestMatchesMobile(true) || (urlRegex != null && !urlRegex.IsMatch(url.ToString()))) { return; } var mobileAddress = MobileAddress; if (mobileAddress.StartsWith("~")) { mobileAddress = VirtualPathUtility.ToAbsolute(mobileAddress); } var redirectUri = Uri.IsWellFormedUriString(mobileAddress, UriKind.Absolute) ? new Uri(mobileAddress) : new Uri(url, mobileAddress); if (redirectUri.Equals(url)) { return; } var builder = new UriBuilder(redirectUri); var abspath = url.AbsolutePath; if (!string.IsNullOrEmpty(abspath) && abspath.EndsWith("default.aspx", StringComparison.InvariantCultureIgnoreCase)) { abspath = abspath.Substring(0, abspath.Length - "default.aspx".Length); } builder.Path += abspath; builder.Query += url.Query.TrimStart('?'); redirectUri = builder.Uri; LogManager.GetLogger("ASC.Mobile.Redirect").DebugFormat("Redirecting url:'{1}' to mobile. UA={0}", context.Request.UserAgent, url); context.Response.Redirect(redirectUri.ToString(), true); } catch (ThreadAbortException) { //Don't do nothing } catch (Exception ex) { LogManager.GetLogger("ASC.Mobile.Redirect").Error("failed to redirect user to mobile", ex); } }
public static void RedirectToMobileVersionIfNeeded(string mobileAddress, HttpContext context) { try { if (string.IsNullOrEmpty(mobileAddress) || !UrlCheckRegex.IsMatch(context.Request.GetUrlRewriter().ToString()) || !IsRequestMatchesMobile(context.Request.UserAgent, true) || CookiesManager.IsMobileBlocked() || CoreContext.Configuration.YourDocsDemo) { return; } //TODO: check user status to display desktop or mobile version if (mobileAddress.StartsWith("~")) { //Resolve to current mobileAddress = VirtualPathUtility.ToAbsolute(mobileAddress); } if (mobileAddress.Contains(TenantReplacePattern)) { var tennant = CoreContext.TenantManager.GetCurrentTenant(); mobileAddress = mobileAddress.Replace(TenantReplacePattern, tennant.TenantAlias); } var redirectUri = Uri.IsWellFormedUriString(mobileAddress, UriKind.Absolute) ? new Uri(mobileAddress) : new Uri(context.Request.GetUrlRewriter(), mobileAddress); if (redirectUri.Equals(context.Request.GetUrlRewriter())) { return; } var builder = new UriBuilder(redirectUri); var abspath = context.Request.GetUrlRewriter().AbsolutePath; if (!string.IsNullOrEmpty(abspath) && abspath.EndsWith("default.aspx", StringComparison.InvariantCultureIgnoreCase)) { abspath = abspath.Substring(0, abspath.Length - "default.aspx".Length); } builder.Path += abspath; builder.Query += context.Request.GetUrlRewriter().Query.TrimStart('?'); redirectUri = builder.Uri; LogManager.GetLogger("ASC.Mobile.Redirect").DebugFormat("Redirecting url:'{1}' to mobile. UA={0}", context.Request.UserAgent, context.Request.GetUrlRewriter()); context.Response.Redirect(redirectUri.ToString(), true); } catch (ThreadAbortException) { //Don't do nothing } catch (Exception e) { //If error happens it's not so bad as you may think. We won't redirect user to mobile version. LogManager.GetLogger("ASC.Mobile.Redirect").Error("failed to redirect user to mobile", e); } }
public static void RedirectToMobileVersionIfNeeded(string mobileAddress, HttpContext context) { try { if (!string.IsNullOrEmpty(mobileAddress) && UrlCheckRegex.IsMatch(context.Request.GetUrlRewriter().ToString()) && IsUserAgentMatchesMobile(context.Request.UserAgent, RedirectRegex) && !CookiesManager.IsMobileBlocked()) { //TODO: check user status to display desktop or mobile version if (mobileAddress.StartsWith("~")) { //Resolve to current mobileAddress = VirtualPathUtility.ToAbsolute(mobileAddress); } if (mobileAddress.Contains(TenantReplacePattern)) { var tennant = CoreContext.TenantManager.GetCurrentTenant(); mobileAddress = mobileAddress.Replace(TenantReplacePattern, tennant.TenantAlias); } var redirectUri = Uri.IsWellFormedUriString(mobileAddress, UriKind.Absolute) ? new Uri(mobileAddress) : new Uri(context.Request.GetUrlRewriter(), mobileAddress); if (!redirectUri.Equals(context.Request.GetUrlRewriter())) { UriBuilder builder = new UriBuilder(redirectUri); builder.Path += context.Request.GetUrlRewriter().AbsolutePath; builder.Query += context.Request.GetUrlRewriter().Query.TrimStart('?'); redirectUri = builder.Uri; LogManager.GetLogger("ASC.Mobile.Redirect").DebugFormat("Redirecting url:'{1}' to mobile. UA={0}", context.Request.UserAgent, context.Request.GetUrlRewriter()); context.Response.Redirect(redirectUri.ToString(), true); } } } catch (ThreadAbortException) { //Don't do nothing } catch (Exception e) { //If error happens it's not so bad as you may think. We won't redirect user to mobile version. LogManager.GetLogger("ASC.Mobile.Redirect").Error("failed to redirect user to mobile", e); } }
private static void BeginRequest(object sender, EventArgs e) { //Detect mobile support on begin request var context = ((HttpApplication)sender).Context; try { var mobileAddress = ConfigurationManager.AppSettings["mobile.redirect-url"]; if (string.IsNullOrEmpty(mobileAddress) || UrlCheckRegex != null && !UrlCheckRegex.IsMatch(context.Request.GetUrlRewriter().ToString()) || !IsRequestMatchesMobile(true) || CookiesManager.IsMobileBlocked()) { return; } //TODO: check user status to display desktop or mobile version if (mobileAddress.StartsWith("~")) { //Resolve to current mobileAddress = VirtualPathUtility.ToAbsolute(mobileAddress); } const string tenantReplacePattern = "%tenant%"; if (mobileAddress.Contains(tenantReplacePattern)) { var tennant = CoreContext.TenantManager.GetCurrentTenant(); mobileAddress = mobileAddress.Replace(tenantReplacePattern, tennant.TenantAlias); } var redirectUri = Uri.IsWellFormedUriString(mobileAddress, UriKind.Absolute) ? new Uri(mobileAddress) : new Uri(context.Request.GetUrlRewriter(), mobileAddress); if (redirectUri.Equals(context.Request.GetUrlRewriter())) { return; } var builder = new UriBuilder(redirectUri); var abspath = context.Request.GetUrlRewriter().AbsolutePath; if (!string.IsNullOrEmpty(abspath) && abspath.EndsWith("default.aspx", StringComparison.InvariantCultureIgnoreCase)) { abspath = abspath.Substring(0, abspath.Length - "default.aspx".Length); } builder.Path += abspath; builder.Query += context.Request.GetUrlRewriter().Query.TrimStart('?'); redirectUri = builder.Uri; LogManager.GetLogger("ASC.Mobile.Redirect").DebugFormat("Redirecting url:'{1}' to mobile. UA={0}", context.Request.UserAgent, context.Request.GetUrlRewriter()); context.Response.Redirect(redirectUri.ToString(), true); } catch (ThreadAbortException) { //Don't do nothing } catch (Exception ex) { //If error happens it's not so bad as you may think. We won't redirect user to mobile version. LogManager.GetLogger("ASC.Mobile.Redirect").Error("failed to redirect user to mobile", ex); } }