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)); } }
/** * 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."); } } } }
/** * 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; } }