/// <summary>Resolves the requester hostname and delegates the request to the chain.</summary> /// <remarks> /// Resolves the requester hostname and delegates the request to the chain. /// <p> /// The requester hostname is available via the /// <see cref="Get()"/> /// method. /// </remarks> /// <param name="request">servlet request.</param> /// <param name="response">servlet response.</param> /// <param name="chain">filter chain.</param> /// <exception cref="System.IO.IOException">thrown if an IO error occurrs.</exception> /// <exception cref="Javax.Servlet.ServletException">thrown if a servet error occurrs. /// </exception> public virtual void DoFilter(ServletRequest request, ServletResponse response, FilterChain chain) { try { string hostname; try { string address = request.GetRemoteAddr(); if (address != null) { hostname = Sharpen.Extensions.GetAddressByName(address).ToString(); } else { log.Warn("Request remote address is NULL"); hostname = "???"; } } catch (UnknownHostException ex) { log.Warn("Request remote address could not be resolved, {0}", ex.ToString(), ex); hostname = "???"; } HostnameTl.Set(hostname); chain.DoFilter(request, response); } finally { HostnameTl.Remove(); } }
/// <summary>Sets the slf4j <code>MDC</code> and delegates the request to the chain.</summary> /// <param name="request">servlet request.</param> /// <param name="response">servlet response.</param> /// <param name="chain">filter chain.</param> /// <exception cref="System.IO.IOException">thrown if an IO error occurrs.</exception> /// <exception cref="Javax.Servlet.ServletException">thrown if a servet error occurrs. /// </exception> public virtual void DoFilter(ServletRequest request, ServletResponse response, FilterChain chain) { try { MDC.Clear(); string hostname = HostnameFilter.Get(); if (hostname != null) { MDC.Put("hostname", HostnameFilter.Get()); } Principal principal = ((HttpServletRequest)request).GetUserPrincipal(); string user = (principal != null) ? principal.GetName() : null; if (user != null) { MDC.Put("user", user); } MDC.Put("method", ((HttpServletRequest)request).GetMethod()); MDC.Put("path", ((HttpServletRequest)request).GetPathInfo()); chain.DoFilter(request, response); } finally { MDC.Clear(); } }
static void Main(string[] args) { /* * PurchaseRequest request = new PurchaseRequest { Amount = 20000 }; * Handler zuzhang = new ZuZhang("张三"); * Handler zongjian = new ZongJian("李四"); * Handler zongjingli = new ZongJingLi("王五"); * * zuzhang.Next(zongjian); * zongjian.Next(zongjingli); * * zuzhang.HandleRequest(request); */ Request request = new Request(); Response response = new Response(); var chain = new FilterChain(); chain.Add(new ActionExecutingFilter()).Add(new ActionExecutedFilter()).Add(new ExceptionFilter()); chain.DoFilter(request, response); Console.WriteLine(request.Header); Console.WriteLine(response.Data); Console.ReadKey(); }
/// <summary> /// Enforces the content-type to be application/octet-stream for /// POST and PUT requests. /// </summary> /// <param name="request">servlet request.</param> /// <param name="response">servlet response.</param> /// <param name="chain">filter chain.</param> /// <exception cref="System.IO.IOException">thrown if an IO error occurrs.</exception> /// <exception cref="Javax.Servlet.ServletException">thrown if a servet error occurrs. /// </exception> public virtual void DoFilter(ServletRequest request, ServletResponse response, FilterChain chain) { bool contentTypeOK = true; HttpServletRequest httpReq = (HttpServletRequest)request; HttpServletResponse httpRes = (HttpServletResponse)response; string method = httpReq.GetMethod(); if (method.Equals("PUT") || method.Equals("POST")) { string op = httpReq.GetParameter(HttpFSFileSystem.OpParam); if (op != null && UploadOperations.Contains(StringUtils.ToUpperCase(op))) { if (Sharpen.Runtime.EqualsIgnoreCase("true", httpReq.GetParameter(HttpFSParametersProvider.DataParam .Name))) { string contentType = httpReq.GetContentType(); contentTypeOK = Sharpen.Runtime.EqualsIgnoreCase(HttpFSFileSystem.UploadContentType , contentType); } } } if (contentTypeOK) { chain.DoFilter(httpReq, httpRes); } else { httpRes.SendError(HttpServletResponse.ScBadRequest, "Data upload requests must have content-type set to '" + HttpFSFileSystem.UploadContentType + "'"); } }
//Empty /// <exception cref="System.IO.IOException"/> /// <exception cref="Javax.Servlet.ServletException"/> public virtual void DoFilter(ServletRequest req, ServletResponse resp, FilterChain chain) { ProxyUtils.RejectNonHttpRequests(req); HttpServletRequest httpReq = (HttpServletRequest)req; HttpServletResponse httpResp = (HttpServletResponse)resp; if (Log.IsDebugEnabled()) { Log.Debug("Remote address for request is: {}", httpReq.GetRemoteAddr()); } if (!GetProxyAddresses().Contains(httpReq.GetRemoteAddr())) { string redirectUrl = FindRedirectUrl(); string target = redirectUrl + httpReq.GetRequestURI(); ProxyUtils.SendRedirect(httpReq, httpResp, target); return; } string user = null; if (httpReq.GetCookies() != null) { foreach (Cookie c in httpReq.GetCookies()) { if (WebAppProxyServlet.ProxyUserCookieName.Equals(c.GetName())) { user = c.GetValue(); break; } } } if (user == null) { if (Log.IsDebugEnabled()) { Log.Debug("Could not find " + WebAppProxyServlet.ProxyUserCookieName + " cookie, so user will not be set" ); } chain.DoFilter(req, resp); } else { AmIpPrincipal principal = new AmIpPrincipal(user); ServletRequest requestWrapper = new AmIpServletRequestWrapper(httpReq, principal); chain.DoFilter(requestWrapper, resp); } }
// NOTHING /// <exception cref="System.IO.IOException"/> /// <exception cref="Javax.Servlet.ServletException"/> public virtual void DoFilter(ServletRequest request, ServletResponse response, FilterChain chain) { HttpServletRequest httpRequest = (HttpServletRequest)request; // if the user is already authenticated, don't override it if (httpRequest.GetRemoteUser() != null) { chain.DoFilter(request, response); } else { HttpServletRequestWrapper wrapper = new _HttpServletRequestWrapper_99(this, httpRequest ); chain.DoFilter(wrapper, response); } }
/// <exception cref="System.IO.IOException"/> /// <exception cref="Javax.Servlet.ServletException"/> public virtual void DoFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) { string userName = request.GetParameter("user.name"); ServletRequest requestModified = new _HttpServletRequestWrapper_253(userName, (HttpServletRequest )request); filterChain.DoFilter(requestModified, response); }
/// <exception cref="System.IO.IOException"/> /// <exception cref="Javax.Servlet.ServletException"/> public virtual void DoFilter(ServletRequest request, ServletResponse response, FilterChain chain) { HttpServletResponse resp = (HttpServletResponse)response; bool isHttps = "https".Equals(request.GetScheme()); AuthenticationFilter.CreateAuthCookie(resp, "token", null, null, -1, isHttps); chain.DoFilter(request, resp); }
private void RequestHandler(object sender, EventArgs e) { HttpApplication httpApplication = (HttpApplication)sender; if (httpApplication.Request.RawUrl.IndexOf("/") > 0) { FilterChain chain = new FilterChain(new HttpUrlFilter()); chain.DoFilter(httpApplication.Request, httpApplication.Response); httpApplication.Context.RemapHandler(handler); } }
/// <exception cref="System.IO.IOException"/> /// <exception cref="Javax.Servlet.ServletException"/> public virtual void DoFilter(ServletRequest request, ServletResponse response, FilterChain chain) { if (filterConfig == null) { return; } uri = ((HttpServletRequest)request).GetRequestURI(); Log.Info("filtering " + uri); chain.DoFilter(request, response); }
/// <exception cref="System.IO.IOException"/> /// <exception cref="Javax.Servlet.ServletException"/> public void DoFilter(ServletRequest request, ServletResponse response, FilterChain chain) { if (authorized) { chain.DoFilter(request, response); } else { ((HttpServletResponse)response).SendError(HttpServletResponse.ScForbidden); } }
/// <exception cref="System.IO.IOException"/> /// <exception cref="Javax.Servlet.ServletException"/> public virtual void DoFilter(ServletRequest req, ServletResponse res, FilterChain chain) { HttpServletResponse httpRes = (HttpServletResponse)res; httpRes.SetHeader("Cache-Control", "no-cache"); long now = Runtime.CurrentTimeMillis(); httpRes.AddDateHeader("Expires", now); httpRes.AddDateHeader("Date", now); httpRes.AddHeader("Pragma", "no-cache"); chain.DoFilter(req, res); }
/// <exception cref="System.IO.IOException"/> /// <exception cref="Javax.Servlet.ServletException"/> public virtual void DoFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) { if (!Log.IsDebugEnabled()) { filterChain.DoFilter(request, response); } else { RequestLoggerFilter.XHttpServletRequest xRequest = new RequestLoggerFilter.XHttpServletRequest ((HttpServletRequest)request); RequestLoggerFilter.XHttpServletResponse xResponse = new RequestLoggerFilter.XHttpServletResponse ((HttpServletResponse)response); try { Log.Debug(xRequest.GetResquestInfo().ToString()); filterChain.DoFilter(xRequest, xResponse); } finally { Log.Debug(xResponse.GetResponseInfo().ToString()); } } }
/// <exception cref="System.IO.IOException"/> /// <exception cref="Javax.Servlet.ServletException"/> public override void DoFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) { HttpServletRequest httpRequest = ToLowerCase((HttpServletRequest)request); string tokenString = httpRequest.GetParameter(DelegationParam.Name); if (tokenString != null) { //Token is present in the url, therefore token will be used for //authentication, bypass kerberos authentication. filterChain.DoFilter(httpRequest, response); return; } base.DoFilter(httpRequest, response, filterChain); }
/// <summary> /// 过滤站点html /// </summary> /// <param name="html"></param> /// <param name="filter"></param> /// <returns></returns> private static string FilterHtml(string html, string filter) { if (filter == null || filter == "") { return(html); } FilterChain chain = LoadFilter(filter); if (chain.Count() > 0) { html = chain.DoFilter(html); } return(html); }
/// <summary> /// It delegates the incoming request to the <code>FilterChain</code>, and /// at its completion (in a finally block) releases the filesystem instance /// back to the /// <see cref="Org.Apache.Hadoop.Lib.Service.FileSystemAccess"/> /// service. /// </summary> /// <param name="servletRequest">servlet request.</param> /// <param name="servletResponse">servlet response.</param> /// <param name="filterChain">filter chain.</param> /// <exception cref="System.IO.IOException">thrown if an IO error occurrs.</exception> /// <exception cref="Javax.Servlet.ServletException">thrown if a servet error occurrs. /// </exception> public virtual void DoFilter(ServletRequest servletRequest, ServletResponse servletResponse , FilterChain filterChain) { try { filterChain.DoFilter(servletRequest, servletResponse); } finally { FileSystem fs = FileSystemTl.Get(); if (fs != null) { FileSystemTl.Remove(); GetFileSystemAccess().ReleaseFileSystem(fs); } } }
/// <summary> /// 取图片分页 /// </summary> public static IEnumerable <PageModel> GetImagePage(SexSpiders sex, string url) { string html = sex.ImgType != null && sex.ImgType.Contains("ajax") ? GetJSContent(url, sex.PageEncode) : GetHtmlContent(url, sex.PageEncode, sex.Domain); //过滤站点 html = FilterHtml(html, sex.SiteFilter); html = ReplaceHtml(html, sex.SiteReplace); FilterChain chain = LoadFilter(sex.PageFilter); html = chain.DoFilter(html); //分页的时候取当前页 string _domain = url.Substring(0, url.LastIndexOf('/') + 1); var parser = new HtmlParser(); var _document = parser.Parse(html); var content = _document.QuerySelectorAll(sex.PageDiv); foreach (var item in content) { //分页时点击类型没有内容要注释,此处与获取多分页时冲突 if (!Regex.IsMatch(item.InnerHtml, @"^\d*$")) { continue; } string _link = item.GetAttribute("href"); if (_link == null || _link == "#" || _link.Contains("javascript")) { continue; } _link = GetLink(_link, _domain, sex.Domain); yield return(new PageModel { PageUrl = _link }); } }
/// <summary> /// 取图片页面 /// </summary> public static IEnumerable <ImageModel> GetListImage(SexSpiders sex, string url) { string html = sex.ImgType != null && sex.ImgType.Contains("ajax") ? GetJSContent(url, sex.PageEncode) : GetHtmlContent(url, sex.PageEncode, sex.Domain); //过滤站点 html = FilterHtml(html, sex.SiteFilter); html = ReplaceHtml(html, sex.SiteReplace); FilterChain chain = LoadFilter(sex.ImageFilter); var parser = new HtmlParser(); var _document = parser.Parse(html); var content = _document.QuerySelectorAll(sex.ImageDiv); foreach (var item in content) { string link = ""; if (chain.Count() > 0) { link = chain.DoFilter(item.OuterHtml); } else { link = item.GetAttribute("src") ?? item.GetAttribute("href"); } if (String.IsNullOrEmpty(link)) { continue; } string _image = GetLink(link, sex.Domain); yield return(new ImageModel { ImageUrl = _image, ImageDomain = sex.Domain }); } }
/// <exception cref="System.IO.IOException"/> /// <exception cref="Javax.Servlet.ServletException"/> public virtual void DoFilter(ServletRequest request, ServletResponse response, FilterChain chain) { try { DataTl.Remove(); UserGroupInformation ugi = HttpUserGroupInformation.Get(); string method = ((HttpServletRequest)request).GetMethod(); StringBuilder requestURL = ((HttpServletRequest)request).GetRequestURL(); string queryString = ((HttpServletRequest)request).GetQueryString(); if (queryString != null) { requestURL.Append("?").Append(queryString); } DataTl.Set(new KMSMDCFilter.Data(ugi, method, requestURL)); chain.DoFilter(request, response); } finally { DataTl.Remove(); } }
public void DoFilter(HttpRequest request, HttpResponse response, FilterChain chain) { //request.RequestContext.HttpContext.ApplicationInstance.Context.RemapHandler chain.DoFilter(request, response); }
/// <summary>Delegates call to the servlet filter chain.</summary> /// <remarks> /// Delegates call to the servlet filter chain. Sub-classes my override this /// method to perform pre and post tasks. /// </remarks> /// <exception cref="System.IO.IOException"/> /// <exception cref="Javax.Servlet.ServletException"/> protected internal virtual void DoFilter(FilterChain filterChain, HttpServletRequest request, HttpServletResponse response) { filterChain.DoFilter(request, response); }
/// <summary> /// 取站点列表 /// </summary> public static IEnumerable <ListModel> GetSiteList(SexSpiders sex) { string html = sex.DocType != null && sex.DocType.Contains("ajax") ? GetJSContent(sex.SiteLink, sex.PageEncode) : GetHtmlContent(sex.SiteLink, sex.PageEncode, sex.Domain); //过滤站点 html = FilterHtml(html, sex.SiteFilter); html = ReplaceHtml(html, sex.SiteReplace); FilterChain chain = LoadFilter(sex.ListFilter); if (sex.DocType != null && sex.DocType.Contains("json")) { string[] root = Regex.Split(sex.ListDiv, "\\|\\|"); var jObject = Newtonsoft.Json.Linq.JObject.Parse(html); var jToken = jObject[root[0]]; string[] m = root[0].Split('&'); switch (m.Length) { case 2: jToken = jObject[m[0]][m[1]]; break; case 3: jToken = jObject[m[0]][m[1]][m[2]]; break; } foreach (var item in jToken) { string[] child = root[1].Split('&'); yield return(new ListModel { Thumb = item.Value <string>(child[2]), Title = System.Net.WebUtility.HtmlDecode(item.Value <string>(child[0])), Link = GetLink(item.Value <string>(child[1]), sex.Domain), Domain = sex.Domain, LastStart = item.Value <string>(child[3]) }); } } else { var parser = new HtmlParser(); var _document = parser.Parse(html); if (!string.IsNullOrWhiteSpace(sex.MainDiv)) { var main = _document.QuerySelectorAll(sex.MainDiv); foreach (var m in main) { var ctx = parser.Parse(m.InnerHtml); var item = ctx.QuerySelector(sex.ListDiv); if (item == null) { continue; } string _title = chain.DoFilter(item.InnerHtml); string _link = GetLink(item.GetAttribute("href"), sex.Domain); if (String.IsNullOrEmpty(_title)) { continue; } var thumb = ctx.QuerySelector(sex.ThumbDiv); var imgtext = thumb == null ? "" : thumb.OuterHtml; yield return(new ListModel { Thumb = GetThumb(imgtext, sex.Domain), Title = System.Net.WebUtility.HtmlDecode(_title), Link = _link, Domain = sex.Domain }); } } else { var content = _document.QuerySelectorAll(sex.ListDiv); foreach (var item in content) { string _title = chain.DoFilter(item.InnerHtml); string _link = GetLink(item.GetAttribute("href"), sex.Domain); if (String.IsNullOrEmpty(_title)) { continue; } yield return(new ListModel { Thumb = GetThumb(item.InnerHtml, sex.Domain), Title = System.Net.WebUtility.HtmlDecode(_title), Link = _link, Domain = sex.Domain }); } } } }
/// <exception cref="System.IO.IOException"/> /// <exception cref="Javax.Servlet.ServletException"/> public virtual void DoFilter(ServletRequest req, ServletResponse res, FilterChain chain) { DoCrossFilter((HttpServletRequest)req, (HttpServletResponse)res); chain.DoFilter(req, res); }