public static void RedirectToErrorPage(HttpServletResponse res, Exception e, string path, bool devMode) { string st = devMode ? ErrorPage.ToStackTrace(e, 1024 * 3) : "See logs for stack trace"; // spec: min 4KB res.SetStatus(res.ScFound); Cookie cookie = new Cookie(StatusCookie, 500.ToString()); cookie.SetPath(path); res.AddCookie(cookie); cookie = new Cookie(ErrorCookie, st); cookie.SetPath(path); res.AddCookie(cookie); res.SetHeader("Location", path); }
public static void RemoveCookie(HttpServletResponse res, string name, string path ) { Log.Debug("removing cookie {} on {}", name, path); Cookie c = new Cookie(name, string.Empty); c.SetMaxAge(0); c.SetPath(path); res.AddCookie(c); }
/// <summary>Warn the user that the link may not be safe!</summary> /// <param name="resp">the http response</param> /// <param name="link">the link to point to</param> /// <param name="user">the user that owns the link.</param> /// <exception cref="System.IO.IOException">on any error.</exception> private static void WarnUserPage(HttpServletResponse resp, string link, string user , ApplicationId id) { //Set the cookie when we warn which overrides the query parameter //This is so that if a user passes in the approved query parameter without //having first visited this page then this page will still be displayed resp.AddCookie(MakeCheckCookie(id, false)); resp.SetContentType(MimeType.Html); WebAppProxyServlet.Page p = new WebAppProxyServlet.Page(resp.GetWriter()); p.Html().H1("WARNING: The following page may not be safe!").H3().("click ").A(link , "here").(" to continue to an Application Master web interface owned by ", user ).().(); }
/// <summary>Download link and have it be the response.</summary> /// <param name="req">the http request</param> /// <param name="resp">the http response</param> /// <param name="link">the link to download</param> /// <param name="c">the cookie to set if any</param> /// <exception cref="System.IO.IOException">on any error.</exception> private static void ProxyLink(HttpServletRequest req, HttpServletResponse resp, URI link, Cookie c, string proxyHost) { DefaultHttpClient client = new DefaultHttpClient(); client.GetParams().SetParameter(ClientPNames.CookiePolicy, CookiePolicy.BrowserCompatibility ).SetBooleanParameter(ClientPNames.AllowCircularRedirects, true); // Make sure we send the request from the proxy address in the config // since that is what the AM filter checks against. IP aliasing or // similar could cause issues otherwise. IPAddress localAddress = Sharpen.Extensions.GetAddressByName(proxyHost); if (Log.IsDebugEnabled()) { Log.Debug("local InetAddress for proxy host: {}", localAddress); } client.GetParams().SetParameter(ConnRoutePNames.LocalAddress, localAddress); HttpGet httpGet = new HttpGet(link); Enumeration <string> names = req.GetHeaderNames(); while (names.MoveNext()) { string name = names.Current; if (passThroughHeaders.Contains(name)) { string value = req.GetHeader(name); if (Log.IsDebugEnabled()) { Log.Debug("REQ HEADER: {} : {}", name, value); } httpGet.SetHeader(name, value); } } string user = req.GetRemoteUser(); if (user != null && !user.IsEmpty()) { httpGet.SetHeader("Cookie", ProxyUserCookieName + "=" + URLEncoder.Encode(user, "ASCII" )); } OutputStream @out = resp.GetOutputStream(); try { HttpResponse httpResp = client.Execute(httpGet); resp.SetStatus(httpResp.GetStatusLine().GetStatusCode()); foreach (Header header in httpResp.GetAllHeaders()) { resp.SetHeader(header.GetName(), header.GetValue()); } if (c != null) { resp.AddCookie(c); } InputStream @in = httpResp.GetEntity().GetContent(); if (@in != null) { IOUtils.CopyBytes(@in, @out, 4096, true); } } finally { httpGet.ReleaseConnection(); } }