/// <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="directoryPath"></param> /// <param name="filepath"></param> /// <returns></returns> public static bool FileIsInDirectory(string directoryPath, string filepath) { if (StringUtil.StartsWith(filepath, '~')) { filepath = ResolvePath(filepath); } DirectoryInfo dir = null; FileInfo file = null; try { file = new FileInfo(filepath); dir = new DirectoryInfo(directoryPath); } catch { return(false); } if (file.Directory.FullName == dir.FullName) { return(true); } return(false); }
////private static Regex ipRegex = new Regex(@"^(\d+\.*)+$"); //public static string GetMainDomain(string domain, bool forCookieDomain) //{ // string mainDomain = null; // string[] splitDomain = domain.Split('.'); // if (splitDomain.Length == 4 && ValidateUtil.IsIPAddress(domain)) //如果是ip地址 // mainDomain = string.Empty; // else if (splitDomain.Length == 2) // mainDomain = string.Empty; // else if (splitDomain.Length > 2) // { // switch (splitDomain[splitDomain.Length - 1]) // { // case "cn": // foreach (string part in Domains_CN) // { // if (splitDomain[splitDomain.Length - 2] == part) // { // mainDomain = string.Concat(splitDomain[splitDomain.Length - 3], ".", splitDomain[splitDomain.Length - 2], ".", splitDomain[splitDomain.Length - 1]); // break; // } // } // break; // case "ru": // foreach (string part in Domains_RU) // { // if (splitDomain[splitDomain.Length - 2] == part) // { // mainDomain = string.Concat(splitDomain[splitDomain.Length - 3], ".", splitDomain[splitDomain.Length - 2], ".", splitDomain[splitDomain.Length - 1]); // break; // } // } // break; // default: // break; // } // if (string.IsNullOrEmpty(mainDomain)) // mainDomain = string.Concat(splitDomain[splitDomain.Length - 2], ".", splitDomain[splitDomain.Length - 1]); // } // else // mainDomain = string.Empty; // if (forCookieDomain) // { // if (mainDomain.Length != 0) // mainDomain = "." + mainDomain; // } // else // { // if (mainDomain.Length == 0) // mainDomain = domain; // } // return mainDomain; //} public static string ResolveUrl(string relativeUrl) { if (relativeUrl != null && StringUtil.StartsWith(relativeUrl, "~/")) { return(JoinUrl(Globals.AppRoot, relativeUrl.Substring(1))); } return(relativeUrl); }
public static string GetIpArea(string ip) { if (string.IsNullOrEmpty(ip)) { return(string.Empty); } else if (ip == "0.0.0.0") { return("(未获得IP)"); } else if (ip == "127.0.0.1") { return("服务器"); } else if (StringUtil.StartsWith(ip, "192.168.")) { return("本地网络"); } else if (StringUtil.StartsWith(ip, "10.")) { return("本地网络"); } else if (ip == "::1") { return("服务器"); } else if (ip.Contains(":")) { return("(不支持ipv6)"); } try { IPLocation location = IPWry.Instance.SearchIPLocation(ip); if (location != null) { return(location.country + location.area); } } catch { return("(未知地址)"); } return(string.Empty); }
private static string Join(JoinType joinType, params string[] paths) { if (joinType == JoinType.Path) { return(IOUtil.JoinPath(paths)); } else if (joinType == JoinType.Url) { return(UrlUtil.JoinUrl(paths)); } else if (joinType == JoinType.RelativeUrl) { string temp = UrlUtil.JoinUrl(paths); return(StringUtil.StartsWith(temp, '/') ? "~" + temp : "~/" + temp); } return(string.Empty); }
public static string MapPath(string virtualPath) { string result = HostingEnvironment.MapPath(virtualPath); if (result == null) { if (StringUtil.StartsWith(virtualPath, "~/")) { result = IOUtil.JoinPath(Globals.ApplicationPath, virtualPath.Substring(2)); } else { throw new Exception("无法映射路径:" + virtualPath); } } return(result); }
/// <summary> /// 检查指定的Url是否是应用程序内的地址 /// </summary> /// <param name="url"></param> /// <returns></returns> public static bool IsUrlInApp(string url) { if (string.IsNullOrEmpty(url)) { return(false); } //如果有提交ReturnUrl且是本程序内的安全地址,则返回true if ( StringUtil.StartsWith(url, Globals.AppRoot + "/") || StringUtil.StartsWith(url, Globals.FullAppRoot + "/") || url == Globals.AppRoot || url == Globals.FullAppRoot ) { return(true); } return(false); }
private static string Join(JoinType joinType, string str1, string str2, string str3) { if (joinType == JoinType.Path) { return(IOUtil.JoinPath(str1, str2, str3)); } else if (joinType == JoinType.Url) { return(UrlUtil.JoinUrl(str1, str2, str3)); } else if (joinType == JoinType.RelativeUrl) { if (StringUtil.StartsWith(str1, '/')) { return(UrlUtil.JoinUrl("~", str1, str2, str3)); } else { return(UrlUtil.JoinUrl("~/", str1, str2, str3)); } } return(string.Empty); }
private static bool IsRelativePath(string path) { return(path != null && (StringUtil.StartsWith(path, "~/") || StringUtil.StartsWith(path, "~\\"))); }
/// <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); }
internal virtual string ToString(bool urlencoded, IDictionary excludeKeys) { int count = this.Count; if (count == 0) { return(string.Empty); } StringBuilder builder = new StringBuilder(); bool flag = (excludeKeys != null) && (excludeKeys["__VIEWSTATE"] != null); for (int i = 0; i < count; i++) { string key = this.GetKey(i); if (((!flag || (key == null)) || !StringUtil.StartsWith(key, "__VIEWSTATE")) && (((excludeKeys == null) || (key == null)) || (excludeKeys[key] == null))) { string str3; if (urlencoded) { key = HttpUtility.UrlEncode(key); } string str2 = !string.IsNullOrEmpty(key) ? (key + "=") : string.Empty; ArrayList list = (ArrayList)base.BaseGet(i); int num3 = (list != null) ? list.Count : 0; if (builder.Length > 0) { builder.Append('&'); } if (num3 == 1) { builder.Append(str2); str3 = (string)list[0]; if (urlencoded) { str3 = HttpUtility.UrlEncode(str3); } builder.Append(str3); } else if (num3 == 0) { builder.Append(str2); } else { for (int j = 0; j < num3; j++) { if (j > 0) { builder.Append('&'); } builder.Append(str2); str3 = (string)list[j]; if (urlencoded) { str3 = HttpUtility.UrlEncode(str3); } builder.Append(str3); } } } } return(builder.ToString()); }
//private static readonly string HtmlFormat = "<img src=\"{1}\" emoticon=\"{0}\" />"; public static string ParseToHtml(int userID, string content, bool parseUserEmoticon, bool parseDefaultEmoticon) { Dictionary <string, string> parsedShortcats = new Dictionary <string, string>(); StringBuilder builder = new StringBuilder(content); #if !Passport if (parseUserEmoticon) { foreach (IEmoticonBase emote in EmoticonBO.Instance.GetEmoticons(userID)) { if (parsedShortcats.ContainsKey(emote.Shortcut)) { continue; } string result = emote.ImageSrc; if (StringUtil.StartsWith(result, '~')) { result = string.Concat("<img src=\"{$root}", result.Remove(0, 1), "\" emoticon=\"", emote.Shortcut, "\" alt=\"\" />"); } else { result = string.Concat("<img src=\"", result.Remove(0, 1), "\" emoticon=\"", emote.Shortcut, "\" alt=\"\" />"); } builder.Replace(emote.Shortcut, result); parsedShortcats.Add(emote.Shortcut, string.Empty); } } if (parseDefaultEmoticon) { foreach (EmoticonGroupBase group in AllSettings.Current.DefaultEmotSettings.AvailableGroups) { foreach (IEmoticonBase emote in (group as DefaultEmoticonGroup).Emoticons) { string result = emote.ImageSrc; if (parsedShortcats.ContainsKey(emote.Shortcut)) { continue; } if (StringUtil.StartsWith(result, '~')) { result = string.Concat("<img src=\"{$root}", result.Remove(0, 1), "\" emoticon=\"", emote.Shortcut, "\" alt=\"\" />"); } else { result = string.Concat("<img src=\"", result.Remove(0, 1), "\" emoticon=\"", emote.Shortcut, "\" alt=\"\" />"); } builder.Replace(emote.Shortcut, result); parsedShortcats.Add(emote.Shortcut, string.Empty); } } } #endif return(builder.ToString()); }