Esempio n. 1
0
  /**
   * 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);
  }
Esempio n. 2
0
  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]);
        }
      }
    }
  }
Esempio n. 3
0
  /**
   * 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);
    }
  }
Esempio n. 4
0
 /**
  * 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());
   }
 }
  /** Invoked by the HTTPClient. */
  public void responsePhase3Handler(Response resp, RoRequest req)
      throws IOException, ModuleException {
    String ce = resp.getHeader("Content-Encoding");
    if (ce == null || req.getMethod().equals("HEAD") || resp.getStatusCode() == 206) return;

    Vector pce;
    try {
      pce = Util.parseHeader(ce);
    } catch (ParseException pe) {
      throw new ModuleException(pe.toString());
    }

    if (pce.size() == 0) return;

    String encoding = ((HttpHeaderElement) pce.firstElement()).getName();
    if (encoding.equalsIgnoreCase("gzip") || encoding.equalsIgnoreCase("x-gzip")) {
      Log.write(Log.MODS, "CEM:   pushing gzip-input-stream");

      resp.inp_stream = new GZIPInputStream(resp.inp_stream);
      pce.removeElementAt(pce.size() - 1);
      resp.deleteHeader("Content-length");
    } else if (encoding.equalsIgnoreCase("deflate")) {
      Log.write(Log.MODS, "CEM:   pushing inflater-input-stream");

      resp.inp_stream = new InflaterInputStream(resp.inp_stream);
      pce.removeElementAt(pce.size() - 1);
      resp.deleteHeader("Content-length");
    } else if (encoding.equalsIgnoreCase("compress") || encoding.equalsIgnoreCase("x-compress")) {
      Log.write(Log.MODS, "CEM:   pushing uncompress-input-stream");

      resp.inp_stream = new UncompressInputStream(resp.inp_stream);
      pce.removeElementAt(pce.size() - 1);
      resp.deleteHeader("Content-length");
    } else if (encoding.equalsIgnoreCase("identity")) {
      Log.write(Log.MODS, "CEM:   ignoring 'identity' token");
      pce.removeElementAt(pce.size() - 1);
    } else {
      Log.write(Log.MODS, "CEM:   Unknown content encoding '" + encoding + "'");
    }

    if (pce.size() > 0) resp.setHeader("Content-Encoding", Util.assembleHeader(pce));
    else resp.deleteHeader("Content-Encoding");
  }