entry : attribmap.entrySet()) {
michael@0: NameValuePair attrib = entry.getValue();
michael@0: String s = attrib.getName().toLowerCase(Locale.ENGLISH);
michael@0:
michael@0: cookie.setAttribute(s, attrib.getValue());
michael@0:
michael@0: CookieAttributeHandler handler = findAttribHandler(s);
michael@0: if (handler != null) {
michael@0: handler.parse(cookie, attrib.getValue());
michael@0: }
michael@0: }
michael@0: cookies.add(cookie);
michael@0: }
michael@0: return cookies;
michael@0: }
michael@0:
michael@0: @Override
michael@0: public void validate(final Cookie cookie, CookieOrigin origin)
michael@0: throws MalformedCookieException {
michael@0: if (cookie == null) {
michael@0: throw new IllegalArgumentException("Cookie may not be null");
michael@0: }
michael@0: if (origin == null) {
michael@0: throw new IllegalArgumentException("Cookie origin may not be null");
michael@0: }
michael@0: origin = adjustEffectiveHost(origin);
michael@0: super.validate(cookie, origin);
michael@0: }
michael@0:
michael@0: @Override
michael@0: public boolean match(final Cookie cookie, CookieOrigin origin) {
michael@0: if (cookie == null) {
michael@0: throw new IllegalArgumentException("Cookie may not be null");
michael@0: }
michael@0: if (origin == null) {
michael@0: throw new IllegalArgumentException("Cookie origin may not be null");
michael@0: }
michael@0: origin = adjustEffectiveHost(origin);
michael@0: return super.match(cookie, origin);
michael@0: }
michael@0:
michael@0: /**
michael@0: * Adds valid Port attribute value, e.g. "8000,8001,8002"
michael@0: */
michael@0: @Override
michael@0: protected void formatCookieAsVer(final CharArrayBuffer buffer,
michael@0: final Cookie cookie, int version) {
michael@0: super.formatCookieAsVer(buffer, cookie, version);
michael@0: // format port attribute
michael@0: if (cookie instanceof ClientCookie) {
michael@0: // Test if the port attribute as set by the origin server is not blank
michael@0: String s = ((ClientCookie) cookie).getAttribute(ClientCookie.PORT_ATTR);
michael@0: if (s != null) {
michael@0: buffer.append("; $Port");
michael@0: buffer.append("=\"");
michael@0: if (s.trim().length() > 0) {
michael@0: int[] ports = cookie.getPorts();
michael@0: if (ports != null) {
michael@0: for (int i = 0, len = ports.length; i < len; i++) {
michael@0: if (i > 0) {
michael@0: buffer.append(",");
michael@0: }
michael@0: buffer.append(Integer.toString(ports[i]));
michael@0: }
michael@0: }
michael@0: }
michael@0: buffer.append("\"");
michael@0: }
michael@0: }
michael@0: }
michael@0:
michael@0: /**
michael@0: * Set 'effective host name' as defined in RFC 2965.
michael@0: *
michael@0: * If a host name contains no dots, the effective host name is
michael@0: * that name with the string .local appended to it. Otherwise
michael@0: * the effective host name is the same as the host name. Note
michael@0: * that all effective host names contain at least one dot.
michael@0: *
michael@0: * @param origin origin where cookie is received from or being sent to.
michael@0: * @return
michael@0: */
michael@0: private static CookieOrigin adjustEffectiveHost(final CookieOrigin origin) {
michael@0: String host = origin.getHost();
michael@0:
michael@0: // Test if the host name appears to be a fully qualified DNS name,
michael@0: // IPv4 address or IPv6 address
michael@0: boolean isLocalHost = true;
michael@0: for (int i = 0; i < host.length(); i++) {
michael@0: char ch = host.charAt(i);
michael@0: if (ch == '.' || ch == ':') {
michael@0: isLocalHost = false;
michael@0: break;
michael@0: }
michael@0: }
michael@0: if (isLocalHost) {
michael@0: host += ".local";
michael@0: return new CookieOrigin(
michael@0: host,
michael@0: origin.getPort(),
michael@0: origin.getPath(),
michael@0: origin.isSecure());
michael@0: } else {
michael@0: return origin;
michael@0: }
michael@0: }
michael@0:
michael@0: @Override
michael@0: public int getVersion() {
michael@0: return 1;
michael@0: }
michael@0:
michael@0: @Override
michael@0: public Header getVersionHeader() {
michael@0: CharArrayBuffer buffer = new CharArrayBuffer(40);
michael@0: buffer.append(SM.COOKIE2);
michael@0: buffer.append(": ");
michael@0: buffer.append("$Version=");
michael@0: buffer.append(Integer.toString(getVersion()));
michael@0: return new BufferedHeader(buffer);
michael@0: }
michael@0:
michael@0: @Override
michael@0: public String toString() {
michael@0: return "rfc2965";
michael@0: }
michael@0:
michael@0: }
michael@0: