/** * returns whether this cookie should be accepted. First checks the stored lists of accept and * reject domains, and if it is neither accepted nor rejected by these then query the user via a * popup. * * @param cookie the cookie in question * @param req the request * @param resp the response * @return true if we accept this cookie. */ public boolean acceptCookie(Cookie cookie, RoRequest req, RoResponse resp) { String server = req.getConnection().getHost(); if (server.indexOf('.') == -1) { server += ".local"; // NOSONAR } // Check lists. Reject takes priority over accept for (int idx = 0; idx < reject_domains.length; idx++) { if (reject_domains[idx].length() == 0 || reject_domains[idx].charAt(0) == '.' && server.endsWith(reject_domains[idx]) || reject_domains[idx].charAt(0) != '.' && server.equals(reject_domains[idx])) return false; } for (int idx = 0; idx < accept_domains.length; idx++) { if (accept_domains[idx].length() == 0 || accept_domains[idx].charAt(0) == '.' && server.endsWith(accept_domains[idx]) || accept_domains[idx].charAt(0) != '.' && server.equals(accept_domains[idx])) return true; } // Ok, not in any list, so ask the user (if allowed). if (!req.allowUI()) return true; if (popup == null) popup = new BasicCookieBox(); return popup.accept(cookie, this, server); }
private void handleCookie(String set_cookie, boolean cookie2, RoRequest req, Response resp) throws ProtocolException { Cookie[] cookies; if (cookie2) cookies = Cookie2.parse(set_cookie, req); else cookies = Cookie.parse(set_cookie, req); if (LOG.isDebugEnabled()) { LOG.debug("Received and parsed " + cookies.length + " cookies:"); for (int idx = 0; idx < cookies.length; idx++) LOG.debug("Cookie " + idx + ": " + cookies[idx]); } Hashtable cookie_list = Util.getList(cookie_cntxt_list, req.getConnection().getContext()); synchronized (cookie_list) { for (int idx = 0; idx < cookies.length; idx++) { Cookie cookie = (Cookie) cookie_list.get(cookies[idx]); if (cookie != null && cookies[idx].hasExpired()) { if (LOG.isDebugEnabled()) LOG.debug("Cookie has expired and is " + "being removed: " + cookie); cookie_list.remove(cookie); // expired, so remove } else if (!cookies[idx].hasExpired()) // new or replaced { if (cookie_handler == null || cookie_handler.acceptCookie(cookies[idx], req, resp)) cookie_list.put(cookies[idx], cookies[idx]); } } } }
/** * The Location header field must be an absolute URI, but too many broken servers use relative * URIs. So, we always resolve relative to the full request URI. * * @param loc the Location header field * @param req the Request to resolve relative URI's relative to * @return an absolute URI corresponding to the Location header field * @exception ProtocolException if the Location header field is completely unparseable */ private URI resLocHdr(String loc, RoRequest req) throws ProtocolException { try { URI base = new URI( req.getConnection().getProtocol(), req.getConnection().getHost(), req.getConnection().getPort(), null); base = new URI(base, req.getRequestURI()); URI res = new URI(base, loc); if (res.getHost() == null) throw new ProtocolException( "Malformed URL in Location header: `" + loc + "' - missing host field"); return res; } catch (ParseException pe) { throw new ProtocolException( "Malformed URL in Location header: `" + loc + "' - exception was: " + pe.getMessage()); } }
/** * 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); } }