/// <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(); } }