public bool Match(string url, ref RouteMatchResult result, QueryString queryString) { var items = mMatchRoute; for (int i = 0; i < items.Length; i++) { UrlRoute urlRoute = items[i]; Dictionary <string, string> ps = new Dictionary <string, string>(); if (urlRoute.Match(url, ps)) { if (ps.Count > 0) { foreach (var item in ps) { queryString.Add(item.Key, item.Value); } } result.Ext = urlRoute.ReExt; result.RewriteUrl = urlRoute.GetRewriteUrl(ps); result.RewriteUrlLower = HttpParse.CharToLower(result.RewriteUrl); return(true); } } return(false); }
public UrlRoute Match(string url, ref RouteMatchResult result, Dictionary <string, string> parameters, string ext, HttpRequest request) { var items = mMatchRoute; for (int i = 0; i < items.Length; i++) { UrlRoute urlRoute = items[i]; if (string.Compare(urlRoute.Ext, ext, true) == 0) { if (urlRoute.Prefix != null) { var prefixValue = urlRoute.Prefix.GetPrefix(request); if (string.Compare(prefixValue, urlRoute.Prefix.Value, true) != 0) { continue; } } if (urlRoute.Match(url, parameters)) { result.Ext = urlRoute.ReExt; result.RewriteUrl = urlRoute.GetRewriteUrl(parameters); return(urlRoute); } } } return(null); }
public bool Match(string url, ref RouteMatchResult result, QueryString queryString) { for (int i = 0; i < Routes.Count; i++) { UrlRoute urlRoute = Routes[i]; if (urlRoute.Match(url, queryString)) { result.Ext = urlRoute.Ext; result.RewriteUrl = urlRoute.Rewrite; result.RewriteUrlLower = urlRoute.Rewrite; return(true); } } return(false); }
public bool Match(HttpRequest request, ref RouteMatchResult result, QueryString queryString) { RouteGroup rg = null; if (mRoutes.TryGetValue(request.Path.GetHashCode(), out rg)) { if (string.Compare(rg.Ext, request.Ext, true) == 0) { if (rg.Match(request.BaseUrl, ref result, queryString)) { return(true); } } } return(false); }
private void LoadMethod(PipeStream stream) { if (mState == LoadedState.None) { string data; if (!stream.TryReadWith(HeaderTypeFactory.LINE_BYTES, out data)) { return; } HttpParse.AnalyzeRequestLine(data, this); HttpParse.ReadHttpVersionNumber(HttpVersion, mQueryString, this); int len = HttpParse.ReadUrlQueryString(Url, mQueryString, this); if (len > 0) { HttpParse.ReadUrlPathAndExt(Url.AsSpan().Slice(0, len), mQueryString, this, this.Server.ServerConfig); } else { HttpParse.ReadUrlPathAndExt(Url.AsSpan(), mQueryString, this, this.Server.ServerConfig); } RouteMatchResult routeMatchResult = new RouteMatchResult(); if (Server.UrlRewrite.Match(this, ref routeMatchResult, mQueryString)) { this.IsRewrite = true; this.SourceUrl = this.Url; if (Server.EnableLog(EventArgs.LogType.Info)) { Server.BaseServer.Log(EventArgs.LogType.Info, Session, "request rewrite {0} to {1}", Url, routeMatchResult.RewriteUrl); } Url = routeMatchResult.RewriteUrl; if (Server.ServerConfig.UrlIgnoreCase) { BaseUrl = routeMatchResult.RewriteUrlLower; } else { BaseUrl = routeMatchResult.RewriteUrl; } Ext = routeMatchResult.Ext; } mState = LoadedState.Method; } }
private void LoadMethod(PipeStream stream) { if (mState == LoadedState.None) { Span <char> data; if (!stream.ReadLine(out data)) { return; } HttpParse.AnalyzeRequestLine(data, this); HttpParse.ReadHttpVersionNumber(HttpVersion, mQueryString, this); int len = HttpParse.ReadUrlQueryString(Url, mQueryString, this); if (len > 0) { HttpParse.ReadUrlPathAndExt(Url.AsSpan().Slice(0, len), mQueryString, this, this.Server.Options); } else { HttpParse.ReadUrlPathAndExt(Url.AsSpan(), mQueryString, this, this.Server.Options); } //UrlCode = BaseUrl.GetHashCode() << 16 | BaseUrl.Length; RouteMatchResult routeMatchResult = new RouteMatchResult(); if (Server.UrlRewrite.Count > 0 && Server.UrlRewrite.Match(this, ref routeMatchResult, mQueryString)) { this.IsRewrite = true; this.SourceUrl = this.Url; if (Server.EnableLog(EventArgs.LogType.Info)) { Server.BaseServer.Log(EventArgs.LogType.Info, Session, $"HTTP {ID} request rewrite {Url} to {routeMatchResult.RewriteUrl}"); } Url = routeMatchResult.RewriteUrl; if (Server.Options.UrlIgnoreCase) { BaseUrl = routeMatchResult.RewriteUrlLower; } else { BaseUrl = routeMatchResult.RewriteUrl; } Ext = routeMatchResult.Ext; } mState = LoadedState.Method; } }
public bool Match(HttpRequest request, out RouteMatchResult result, QueryString queryString) { result = null; RouteGroup rg = null; UrlRoute urlRoute = null; string key = $"{request.GetHostBase()}{request.BaseUrl}"; if (mRouteCached.TryGetValue(key, out UrlRouteAgent cached)) { cached.ActiveTime = TimeWatch.GetTotalSeconds(); if (cached.Route == null && cached.Version == this.mVersion) { if (request.Server.EnableLog(EventArgs.LogType.Info)) { request.Server.Log(EventArgs.LogType.Info, $"HTTP {request.ID} {request.RemoteIPAddress} {request.Method} {key} rewrite cached miss"); } return(false); } if (cached.Route != null && cached.Version == this.mVersion) { if (cached.Parameters.Count > 0) { foreach (var item in cached.Parameters) { queryString.Add(item.Key, item.Value); } } result = cached.MatchResult; if (request.Server.EnableLog(EventArgs.LogType.Info)) { request.Server.Log(EventArgs.LogType.Info, $"HTTP {request.ID} {request.RemoteIPAddress} {request.Method} {key} rewrite cached hit"); } return(true); } } bool iscached = true; Dictionary <string, string> parameters = new Dictionary <string, string>(); result = new RouteMatchResult(); if (mRoutes.TryGetValue(request.Path, out rg)) { urlRoute = rg.Match(request.BaseUrl, ref result, parameters, request.Ext, request); } if (urlRoute == null) { RouteGroup[] rgs = mMatchRoutes; if (rgs != null) { for (int i = 0; i < rgs.Length; i++) { rg = rgs[i]; if (rg.PathLevel == request.PathLevel) { iscached = rg.Cached; urlRoute = rg.Match(request.BaseUrl, ref result, parameters, request.Ext, request); if (urlRoute != null) { if (urlRoute.Prefix != null && urlRoute.Prefix.Type == UrlPrefix.PrefixType.Host) { iscached = true; } break; } } } } } if (urlRoute != null) { result.HasQueryString = urlRoute.HasQueryString; } if (urlRoute != null && urlRoute.Prefix?.Type == UrlPrefix.PrefixType.Param) { return(urlRoute != null); } var agent = new UrlRouteAgent { Route = urlRoute, Version = this.mVersion, MatchResult = result, Parameters = parameters }; if (parameters.Count > 0) { foreach (var ps in parameters) { queryString.Add(ps.Key, ps.Value); } } if (iscached) { UrlRouteAgent exits = (UrlRouteAgent)mRouteCached.ExistOrAdd(key, agent); if (request.Server.EnableLog(EventArgs.LogType.Info)) { request.Server.Log(EventArgs.LogType.Info, $"HTTP {request.ID} {request.RemoteIPAddress} {request.Method} {key} rewrite save to cached"); } if (exits != null) { exits.Route = urlRoute; exits.Version = mVersion; exits.MatchResult = result; exits.Parameters = parameters; } } return(urlRoute != null); }
private void LoadMethod(PipeStream stream) { if (mState == LoadedState.None) { Span <char> data; if (!stream.ReadLine(out data)) { return; } HttpParse.AnalyzeRequestLine(data, this); HttpParse.ReadHttpVersionNumber(HttpVersion, mQueryString, this); int len = HttpParse.ReadUrlQueryString(Url, mQueryString, this); if (len > 0) { HttpParse.ReadUrlPathAndExt(Url.AsSpan().Slice(0, len), mQueryString, this, this.Server.Options); } else { HttpParse.ReadUrlPathAndExt(Url.AsSpan(), mQueryString, this, this.Server.Options); } RouteMatchResult routeMatchResult = new RouteMatchResult(); this.IsRewrite = false; if (Server.UrlRewrite.Count > 0 && Server.UrlRewrite.Match(this, ref routeMatchResult, mQueryString)) { this.IsRewrite = true; this.SourceUrl = Url; this.SourceBaseUrl = BaseUrl; this.SourcePath = Path; if (Server.Options.UrlIgnoreCase) { Url = routeMatchResult.RewriteUrlLower; } else { Url = routeMatchResult.RewriteUrl; } if (Server.Options.AgentRewrite) { if (len > 0 && this.SourceUrl.Length > len + 1) { if (Url.IndexOf('?') > 0) { Url += "&"; } else { Url += "?"; } Url += new string(SourceUrl.AsSpan().Slice(len + 1)); } } len = HttpParse.ReadUrlQueryString(Url, null, this); if (len > 0) { HttpParse.ReadUrlPathAndExt(Url.AsSpan().Slice(0, len), mQueryString, this, this.Server.Options); } else { HttpParse.ReadUrlPathAndExt(Url.AsSpan(), mQueryString, this, this.Server.Options); } if (Server.EnableLog(EventArgs.LogType.Info)) { Server.BaseServer.Log(EventArgs.LogType.Info, Session, $"HTTP {ID} {((IPEndPoint)Session.RemoteEndPoint).Address} request {SourceUrl} rewrite to {Url}"); } } mState = LoadedState.Method; } }