/// <summary> /// 检查地址是否为本地地址(包含相对路径和绝对路径,例如:xxx://开头的都不是本地地址) /// </summary> /// <param name="url"></param> /// <returns></returns> public static bool IsLocalUrl(string url) { if (string.IsNullOrEmpty(url)) { return(false); } if (StringUtil.StartsWith(url, '/')) { return(true); } //最常见的前缀先判断,避免每次调用正则性能下降 if ( StringUtil.StartsWithIgnoreCase(url, "http://") || StringUtil.StartsWithIgnoreCase(url, "https://") || StringUtil.StartsWithIgnoreCase(url, "ftp://") ) { return(false); } if (s_LocalRegex == null) { s_LocalRegex = new Regex(@"^\w+://.*$", RegexOptions.IgnoreCase); } return(!s_LocalRegex.IsMatch(url)); }
/// <summary> /// 移除指定开头的所有缓存 /// </summary> /// <param name="startText"></param> public static void RemoveBySearch(string startText) { if (string.IsNullOrEmpty(startText)) { return; } List <string> removeKeys = new List <string>(); foreach (DictionaryEntry elem in HttpRuntime.Cache) { string cacheKey = elem.Key.ToString(); if (StringUtil.StartsWithIgnoreCase(cacheKey, startText)) { removeKeys.Add(cacheKey); } } foreach (string removeKey in removeKeys) { try { HttpRuntime.Cache.Remove(removeKey); } catch { } } }
/// <summary> /// 移除指定开头的所有缓存 /// </summary> /// <param name="startText"></param> public static void RemoveBySearch(params string[] startTexts) { if (startTexts == null || startTexts.Length == 0) { return; } List <string> removeKeys = new List <string>(); foreach (DictionaryEntry elem in HttpRuntime.Cache) { string cacheKey = elem.Key.ToString(); foreach (string startText in startTexts) { if (StringUtil.StartsWithIgnoreCase(cacheKey, startText)) { removeKeys.Add(cacheKey); } } } foreach (string removeKey in removeKeys) { try { HttpRuntime.Cache.Remove(removeKey); } catch { } } }
public static void RemoveBySearch(string keyPrefix) { if (string.IsNullOrEmpty(keyPrefix)) { return; } List <string> keys = new List <string>(); foreach (DictionaryEntry elem in HttpContext.Current.Items) { string key = elem.Key.ToString(); if (StringUtil.StartsWithIgnoreCase(key, keyPrefix)) { keys.Add(key); } } foreach (string key in keys) { try { HttpContext.Current.Items.Remove(key); } catch { } } }
public static bool IsRequestBySafeUrlRewrite() { string rawurl = RawUrl; return(StringUtil.StartsWithIgnoreCase(rawurl, Globals.AppRoot + "/default.aspx?") || StringUtil.StartsWithIgnoreCase(rawurl, Globals.AppRoot + "/index.aspx?") || StringUtil.StartsWithIgnoreCase(rawurl, Globals.AppRoot + "/?")); }
/// <summary> /// 返回指定开头的所有缓存 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="startText"></param> /// <returns></returns> public static IList <T> GetBySearch <T>(string startText) { IList <T> objects = new List <T>(); foreach (DictionaryEntry elem in HttpRuntime.Cache) { string key = elem.Key.ToString(); if (StringUtil.StartsWithIgnoreCase(key, startText)) { object value = HttpRuntime.Cache[key]; if (value != null && value is T) { objects.Add((T)value); } } } return(objects); }
public static string GetUrlInfo(string url) { string webRoot = Globals.AppRoot + "/"; string fullWebRoot = Globals.FullAppRoot + "/"; UrlFormat urlFormat = AllSettings.Current.FriendlyUrlSettings.UrlFormat; int baseUrlLength = 0;// = urlFormat == UrlFormat.Query ? 1 : 0; string urlInfo; if (StringUtil.StartsWithIgnoreCase(url, webRoot)) { baseUrlLength += webRoot.Length; } else if (StringUtil.StartsWithIgnoreCase(url, fullWebRoot)) { baseUrlLength += fullWebRoot.Length; } else { return(string.Empty); } if (urlFormat == UrlFormat.Query) { //确认是本程序内的地址以保证安全 if (UrlUtil.IsUrlInApp(url)) { if (url.IndexOf("default.aspx?", baseUrlLength) == baseUrlLength) { baseUrlLength += 13; } else if (url.IndexOf("index.aspx?", baseUrlLength) == baseUrlLength) { baseUrlLength += 11; } else { baseUrlLength += 1; } } else { baseUrlLength += 1; } } if (url.Length > baseUrlLength) { urlInfo = url.Substring(baseUrlLength); } else { urlInfo = "default"; } int queryIndex = urlInfo.IndexOf('?'); if (queryIndex != -1) { urlInfo = urlInfo.Substring(0, queryIndex); } if (urlFormat == UrlFormat.Aspx) { if (urlInfo.EndsWith(".aspx", StringComparison.OrdinalIgnoreCase)) { urlInfo = urlInfo.Substring(0, urlInfo.Length - 5); } else { return(string.Empty); } } else if (urlFormat == UrlFormat.Html) { if (urlInfo.EndsWith(".html", StringComparison.OrdinalIgnoreCase)) { urlInfo = urlInfo.Substring(0, urlInfo.Length - 5); } else { return(string.Empty); } } return(urlInfo.ToLower()); }
/// <summary> /// 获取压缩类型 /// </summary> /// <param name="schemes"></param> /// <param name="prefs"></param> /// <returns></returns> public static CompressingType GetCompressingType(HttpContext context) { string acceptedTypes = context.Request.Headers["Accept-Encoding"]; // if we couldn't find the header, bail out if (acceptedTypes == null) { return(CompressingType.None); } // the actual types could be , delimited. split 'em out. string[] schemes = acceptedTypes.Split(','); bool foundDeflate = false; bool foundGZip = false; bool foundStar = false; float deflateQuality = 0f; float gZipQuality = 0f; float starQuality = 0f; bool isAcceptableDeflate; bool isAcceptableGZip; bool isAcceptableStar; for (int i = 0; i < schemes.Length; i++) { string acceptEncodingValue = schemes[i].Trim(); if (StringUtil.StartsWithIgnoreCase(acceptEncodingValue, "deflate")) { foundDeflate = true; float newDeflateQuality = GetQuality(acceptEncodingValue); if (deflateQuality < newDeflateQuality) { deflateQuality = newDeflateQuality; } } else if (StringUtil.StartsWithIgnoreCase(acceptEncodingValue, "gzip") || StringUtil.StartsWithIgnoreCase(acceptEncodingValue, "x-gzip")) { foundGZip = true; float newGZipQuality = GetQuality(acceptEncodingValue); if (gZipQuality < newGZipQuality) { gZipQuality = newGZipQuality; } } else if (StringUtil.StartsWith(acceptEncodingValue, '*')) { foundStar = true; float newStarQuality = GetQuality(acceptEncodingValue); if (starQuality < newStarQuality) { starQuality = newStarQuality; } } } isAcceptableStar = foundStar && (starQuality > 0); isAcceptableDeflate = (foundDeflate && (deflateQuality > 0)) || (!foundDeflate && isAcceptableStar); isAcceptableGZip = (foundGZip && (gZipQuality > 0)) || (!foundGZip && isAcceptableStar); if (isAcceptableDeflate && !foundDeflate) { deflateQuality = starQuality; } if (isAcceptableGZip && !foundGZip) { gZipQuality = starQuality; } // do they support any of our compression methods? if (!(isAcceptableDeflate || isAcceptableGZip || isAcceptableStar)) { return(CompressingType.None); } // if deflate is better according to client if (isAcceptableDeflate && (!isAcceptableGZip || (deflateQuality > gZipQuality))) { return(CompressingType.Deflate); } // if gzip is better according to client if (isAcceptableGZip && (!isAcceptableDeflate || (deflateQuality < gZipQuality))) { return(CompressingType.GZip); } if (isAcceptableGZip) { return(CompressingType.GZip); } // if we're here, the client either didn't have a preference or they don't support compression if (isAcceptableDeflate) { return(CompressingType.Deflate); } if (isAcceptableDeflate || isAcceptableStar) { return(CompressingType.Deflate); } if (isAcceptableGZip) { return(CompressingType.GZip); } // return null. we couldn't find a filter. return(CompressingType.None); }