/** * Update the permanent redirection list. * * @param the original request * @param the new location */ private static void update_perm_redir_list(RoRequest req, URI new_loc) { HTTPConnection con = req.getConnection(); URI cur_loc = null; try { cur_loc = new URI( new URI(con.getProtocol(), con.getHost(), con.getPort(), null), req.getRequestURI()); } catch (ParseException pe) { if (LOG.isTraceEnabled()) { LOG.trace("An exception occurred: " + pe.getMessage()); } } if (cur_loc != null && !cur_loc.equals(new_loc)) { Hashtable perm_redir_list = Util.getList(perm_redir_cntxt_list, con.getContext()); perm_redir_list.put(cur_loc, new_loc); } }
/** Invoked by the HTTPClient. */ public int requestHandler(Request req, Response[] resp) { HTTPConnection con = req.getConnection(); URI new_loc, cur_loc; // check for retries HttpOutputStream out = req.getStream(); if (out != null && deferred_redir_list.get(out) != null) { copyFrom((RedirectionModule) deferred_redir_list.remove(out)); req.copyFrom(saved_req); if (new_con) return REQ_NEWCON_RST; else return REQ_RESTART; } // handle permanent redirections try { cur_loc = new URI( new URI(con.getProtocol(), con.getHost(), con.getPort(), null), req.getRequestURI()); } catch (ParseException pe) { throw new Error("HTTPClient Internal Error: unexpected exception '" + pe + "'", pe); } // handle permanent redirections Hashtable perm_redir_list = Util.getList(perm_redir_cntxt_list, req.getConnection().getContext()); if ((new_loc = (URI) perm_redir_list.get(cur_loc)) != null) { /* * copy query if present in old url but not in new url. This isn't * strictly conforming, but some scripts fail to properly propagate the * query string to the Location header. Unfortunately it looks like we're * f****d either way: some scripts fail if you don't propagate the query * string, some fail if you do... God, don't you just love it when people * can't read a spec? Anway, since we can't get it right for all scripts * we opt to follow the spec. String nres = new_loc.getPathAndQuery(), * oquery = Util.getQuery(req.getRequestURI()), nquery = * Util.getQuery(nres); if (nquery == null && oquery != null) nres += "?" * + oquery; */ String nres = new_loc.getPathAndQuery(); req.setRequestURI(nres); try { lastURI = new URI(new_loc, nres); } catch (ParseException pe) { if (LOG.isTraceEnabled()) { LOG.trace("An exception occurred: " + pe.getMessage()); } } if (LOG.isDebugEnabled()) LOG.debug( "Matched request in permanent redirection list - redoing request to " + lastURI.toExternalForm()); if (!con.isCompatibleWith(new_loc)) { try { con = new HTTPConnection(new_loc); } catch (ProtocolNotSuppException e) { throw new Error("HTTPClient Internal Error: unexpected " + "exception '" + e + "'", e); } con.setContext(req.getConnection().getContext()); req.setConnection(con); return REQ_NEWCON_RST; } else { return REQ_RESTART; } } return REQ_CONTINUE; }