public void parse(final Cookie cookie, final String commenturl)
     throws MalformedCookieException {
   if (cookie instanceof Cookie2) {
     Cookie2 cookie2 = (Cookie2) cookie;
     cookie2.setDiscard(true);
   }
 }
 /** validate cookie version attribute. Version attribute is REQUIRED. */
 public void validate(final Cookie cookie, final CookieOrigin origin)
     throws MalformedCookieException {
   if (cookie == null) {
     throw new IllegalArgumentException("Cookie may not be null");
   }
   if (cookie instanceof Cookie2) {
     Cookie2 cookie2 = (Cookie2) cookie;
     if (!cookie2.isVersionAttributeSpecified()) {
       throw new MalformedCookieException("Violates RFC 2965. Version attribute is required.");
     }
   }
 }
示例#3
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]);
        }
      }
    }
  }
 /** Parse cookie port attribute. */
 public void parse(final Cookie cookie, final String portValue) throws MalformedCookieException {
   if (cookie == null) {
     throw new IllegalArgumentException("Cookie may not be null");
   }
   if (cookie instanceof Cookie2) {
     Cookie2 cookie2 = (Cookie2) cookie;
     if ((portValue == null) || (portValue.trim().equals(""))) {
       // If the Port attribute is present but has no value, the
       // cookie can only be sent to the request-port.
       // Since the default port list contains only request-port, we don't
       // need to do anything here.
       cookie2.setPortAttributeBlank(true);
     } else {
       int[] ports = parsePortAttribute(portValue);
       cookie2.setPorts(ports);
     }
     cookie2.setPortAttributeSpecified(true);
   }
 }
  /**
   * Return a string suitable for sending in a <tt>"Cookie"</tt> header as defined in RFC 2965
   *
   * @param cookie a {@link org.apache.commons.httpclient.Cookie} to be formatted as string
   * @return a string suitable for sending in a <tt>"Cookie"</tt> header.
   */
  public String formatCookie(final Cookie cookie) {
    LOG.trace("enter RFC2965Spec.formatCookie(Cookie)");

    if (cookie == null) {
      throw new IllegalArgumentException("Cookie may not be null");
    }
    if (cookie instanceof Cookie2) {
      Cookie2 cookie2 = (Cookie2) cookie;
      int version = cookie2.getVersion();
      final StringBuffer buffer = new StringBuffer();
      this.formatter.format(buffer, new NameValuePair("$Version", Integer.toString(version)));
      buffer.append("; ");
      doFormatCookie2(cookie2, buffer);
      return buffer.toString();
    } else {
      // old-style cookies are formatted according to the old rules
      return this.rfc2109.formatCookie(cookie);
    }
  }
 /**
  * Validate cookie port attribute. If the Port attribute was specified in header, the request
  * port must be in cookie's port list.
  */
 public void validate(final Cookie cookie, final CookieOrigin origin)
     throws MalformedCookieException {
   if (cookie == null) {
     throw new IllegalArgumentException("Cookie may not be null");
   }
   if (origin == null) {
     throw new IllegalArgumentException("Cookie origin may not be null");
   }
   if (cookie instanceof Cookie2) {
     Cookie2 cookie2 = (Cookie2) cookie;
     int port = origin.getPort();
     if (cookie2.isPortAttributeSpecified()) {
       if (!portMatch(port, cookie2.getPorts())) {
         throw new MalformedCookieException(
             "Port attribute violates RFC 2965: "
                 + "Request port not found in cookie's port list.");
       }
     }
   }
 }
 /** Parse cookie version attribute. */
 public void parse(final Cookie cookie, final String value) throws MalformedCookieException {
   if (cookie == null) {
     throw new IllegalArgumentException("Cookie may not be null");
   }
   if (cookie instanceof Cookie2) {
     Cookie2 cookie2 = (Cookie2) cookie;
     if (value == null) {
       throw new MalformedCookieException("Missing value for version attribute");
     }
     int version = -1;
     try {
       version = Integer.parseInt(value);
     } catch (NumberFormatException e) {
       version = -1;
     }
     if (version < 0) {
       throw new MalformedCookieException("Invalid cookie version.");
     }
     cookie2.setVersion(version);
     cookie2.setVersionAttributeSpecified(true);
   }
 }
 /**
  * Match cookie port attribute. If the Port attribute is not specified in header, the cookie can
  * be sent to any port. Otherwise, the request port must be in the cookie's port list.
  */
 public boolean match(final Cookie cookie, final CookieOrigin origin) {
   if (cookie == null) {
     throw new IllegalArgumentException("Cookie may not be null");
   }
   if (origin == null) {
     throw new IllegalArgumentException("Cookie origin may not be null");
   }
   if (cookie instanceof Cookie2) {
     Cookie2 cookie2 = (Cookie2) cookie;
     int port = origin.getPort();
     if (cookie2.isPortAttributeSpecified()) {
       if (cookie2.getPorts() == null) {
         LOG.warn("Invalid cookie state: port not specified");
         return false;
       }
       if (!portMatch(port, cookie2.getPorts())) {
         return false;
       }
     }
     return true;
   } else {
     return false;
   }
 }
 private void doFormatCookie2(final Cookie2 cookie, final StringBuffer buffer) {
   String name = cookie.getName();
   String value = cookie.getValue();
   if (value == null) {
     value = "";
   }
   this.formatter.format(buffer, new NameValuePair(name, value));
   // format domain attribute
   if (cookie.getDomain() != null && cookie.isDomainAttributeSpecified()) {
     buffer.append("; ");
     this.formatter.format(buffer, new NameValuePair("$Domain", cookie.getDomain()));
   }
   // format path attribute
   if ((cookie.getPath() != null) && (cookie.isPathAttributeSpecified())) {
     buffer.append("; ");
     this.formatter.format(buffer, new NameValuePair("$Path", cookie.getPath()));
   }
   // format port attribute
   if (cookie.isPortAttributeSpecified()) {
     String portValue = "";
     if (!cookie.isPortAttributeBlank()) {
       portValue = createPortAttribute(cookie.getPorts());
     }
     buffer.append("; ");
     this.formatter.format(buffer, new NameValuePair("$Port", portValue));
   }
 }
示例#10
0
  /**
   * the method called by the DefaultCookiePolicyHandler.
   *
   * @return true if the cookie should be accepted
   */
  public synchronized boolean accept(Cookie cookie, DefaultCookiePolicyHandler h, String server) {
    // set the new values

    name_value_label.setText(cookie.getName() + "=" + cookie.getValue());
    domain_value.setText(cookie.getDomain());
    path_value.setText(cookie.getPath());
    if (cookie.expires() == null) expires_value.setText("never");
    else expires_value.setText(cookie.expires().toString());
    int pos = 2;
    if (cookie.isSecure()) add(secure_note, constr, pos++);
    if (cookie.discard()) add(discard_note, constr, pos++);

    if (cookie instanceof Cookie2) {
      Cookie2 cookie2 = (Cookie2) cookie;

      // set ports list
      if (cookie2.getPorts() != null) {
        ((GridLayout) left_panel.getLayout()).setRows(5);
        left_panel.add(ports_label, 2);
        ((GridLayout) right_panel.getLayout()).setRows(5);
        int[] ports = cookie2.getPorts();
        StringBuffer plist = new StringBuffer();
        plist.append(ports[0]);
        for (int idx = 1; idx < ports.length; idx++) {
          plist.append(", ");
          plist.append(ports[idx]);
        }
        ports_value.setText(plist.toString());
        right_panel.add(ports_value, 2);
      }

      // set comment url
      if (cookie2.getCommentURL() != null) {
        c_url_note.setText("For more info on this cookie see: " + cookie2.getCommentURL());
        add(c_url_note, constr, pos++);
      }

      // set comment
      if (cookie2.getComment() != null) {
        comment_value.setText(cookie2.getComment());
        add(comment_label, constr, pos++);
        add(comment_value, constr, pos++);
      }
    }

    // invalidate all labels, so that new values are displayed correctly

    name_value_label.invalidate();
    domain_value.invalidate();
    ports_value.invalidate();
    path_value.invalidate();
    expires_value.invalidate();
    left_panel.invalidate();
    right_panel.invalidate();
    secure_note.invalidate();
    discard_note.invalidate();
    c_url_note.invalidate();
    comment_value.invalidate();
    invalidate();

    // set default domain test

    domain.setText(cookie.getDomain());

    // display

    setResizable(true);
    pack();
    setResizable(false);
    setLocation(
        (screen.width - getPreferredSize().width) / 2,
        (int) ((screen.height - getPreferredSize().height) / 2 * .7));
    setVisible(true);
    default_focus.requestFocus();

    // wait for user input

    try {
      wait();
    } catch (InterruptedException e) {
    }

    setVisible(false);

    // reset popup

    remove(secure_note);
    remove(discard_note);
    left_panel.remove(ports_label);
    ((GridLayout) left_panel.getLayout()).setRows(4);
    right_panel.remove(ports_value);
    ((GridLayout) right_panel.getLayout()).setRows(4);
    remove(c_url_note);
    remove(comment_label);
    remove(comment_value);

    // handle accept/reject domain buttons

    if (accept_domain) {
      String dom = domain.getText().trim().toLowerCase();

      if (accept) h.addAcceptDomain(dom);
      else h.addRejectDomain(dom);
    }

    return accept;
  }