日期:2014-05-20 浏览次数:21096 次
using System; using System.Collections.Generic; using System.Text; using System.Collections; using System.Collections.Specialized; using System.Web; using System.IO; using XXX.Lib; namespace XXX { // Url辅助类:对Url进行初步的解析 public class UrlHelper { const int MAX_URI_LENGTH = 512; string _scriptName = string.Empty; CommandResult _parseResult = CommandResult.Success; NameValueCollection _parameters = new NameValueCollection(); char[] _uriInvalidChar = new char[] { '/', '\\' }; char[] _pathInvalidChar = new char[] { '/', '\\', ':', '*', '?', '\"', '<', '>', '|' }; public Uri _uri = null; public string ScriptName { get { return _scriptName; } } public NameValueCollection Parameters { get { return _parameters; } } public CommandResult ParseResult { get { return _parseResult; } } public UrlHelper(Uri originalUri) { _uri = ReplaceUri(originalUri); if (IsUriLengthError()) { return; } if (CheckPathAndQuery()) { ParsePathAndQuery(); } } private Uri ReplaceUri(Uri originalUri) { string uriStr = ReplaceUriWithUrlModel(originalUri); if (uriStr == "") { return originalUri; } else { return new Uri(uriStr); } } private static string ReplaceUriWithUrlModel(Uri originalUri) { string[] splitStr = null; try { foreach (var item in File.ReadAllLines(Hisan.Lib.SystemLib.GetAppRootDir() + "UrlModel.ini")) { splitStr = item.Split(new string[] { "->" }, StringSplitOptions.RemoveEmptyEntries); if (splitStr == null || splitStr.Length != 2) { continue; } if (splitStr[0].Trim() == originalUri.PathAndQuery.Substring(1)) { return originalUri.AbsoluteUri.Replace(splitStr[0].Trim(), splitStr[1].Trim()); } } } catch { } return ""; } private bool IsUriLengthError() { if (_uri == null || _uri.ToString().Length > MAX_URI_LENGTH) { _parseResult = CommandResult.UrlTooLong; return true; } return false; } private bool CheckPathAndQuery() { string pathAndQuery = _uri.PathAndQuery.Substring(1); if (IsUrlInvalidChar(pathAndQuery)) { return false; } if (pathAndQuery.IndexOfAny(_uriInvalidChar) >= 0) { _parseResult = CommandResult.UrlInvalidChar; return false; } else if (pathAndQuery.Length == 0) { _parseResult = CommandResult.NoExistsMethod; return false; } string[] splitPathAndQuery = new string[] { }; if (IsFileNameInvalidChar(pathAndQuery, splitPathAndQuery)) { return false; } return true; } private bool IsFileNameInvalidChar(string pathAndQuery, string[] splitPathAndQuery) { splitPathAndQuery = pathAndQuery.Split(new char[] { '?' }, StringSplitOptions.RemoveEmptyEntries); if (splitPathAndQuery[0].IndexOfAny(_pathInvalidChar) >= 0) { _parseResult = CommandResult.FileNameInvalidChar; return true; } return false; } private bool IsUrlInvalidChar(string pathAndQuery) { if (pathAndQuery.IndexOfAny(_uriInvalidChar) >= 0) { _parseResult = CommandResult.UrlInvalidChar; return true; } return false; } private void ParsePathAndQuery() { string[] splitPathAndQuery = _uri.PathAndQuery.Substring(1).Split(new char[] { '?' }, StringSplitOptions.RemoveEmptyEntries); SetScriptNameAndParameters(splitPathAndQuery); } private void SetScriptNameAndParameters(string[] splitPathAndQuery) { _scriptName = splitPathAndQuery[0]; if (splitPathAndQuery.Length > 1) { _parameters = HttpUtility.ParseQueryString(splitPathAndQuery[1], Encoding.UTF8); } } } }