⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rfc2965spec.java

📁 Light in the box 抓取程序。 使用HttpClient
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        LOG.trace("enter RFC2965.match("                  + "String, int, String, boolean, Cookie");        if (cookie == null) {            throw new IllegalArgumentException("Cookie may not be null");        }        if (cookie instanceof Cookie2) {            // check if cookie has expired            if (cookie.isPersistent() && cookie.isExpired()) {                return false;            }            CookieOrigin origin = new CookieOrigin(getEffectiveHost(host), port, path, secure);             for (Iterator i = getAttribHandlerIterator(); i.hasNext(); ) {                CookieAttributeHandler handler = (CookieAttributeHandler) i.next();                if (!handler.match(cookie, origin)) {                    return false;                }            }            return true;        } else {            // old-style cookies are matched according to the old rules            return this.rfc2109.match(host, port, path, secure, cookie);        }    }    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));        }    }        /**     * 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);        }    }    /**     * Create a RFC 2965 compliant <tt>"Cookie"</tt> header value containing all     * {@link org.apache.commons.httpclient.Cookie}s suitable for     * sending in a <tt>"Cookie"</tt> header     * @param cookies an array of {@link org.apache.commons.httpclient.Cookie}s to be formatted     * @return a string suitable for sending in a Cookie header.     */    public String formatCookies(final Cookie[] cookies) {        LOG.trace("enter RFC2965Spec.formatCookieHeader(Cookie[])");        if (cookies == null) {            throw new IllegalArgumentException("Cookies may not be null");        }        // check if cookies array contains a set-cookie (old style) cookie        boolean hasOldStyleCookie = false;        int version = -1;        for (int i = 0; i < cookies.length; i++) {            Cookie cookie = cookies[i];            if (!(cookie instanceof Cookie2)) {                hasOldStyleCookie = true;                break;            }            if (cookie.getVersion() > version) {                version = cookie.getVersion();            }        }        if (version < 0) {            version = 0;        }        if (hasOldStyleCookie || version < 1) {            // delegate old-style cookie formatting to rfc2109Spec            return this.rfc2109.formatCookies(cookies);        }        // Arrange cookies by path        Arrays.sort(cookies, PATH_COMPOARATOR);                final StringBuffer buffer = new StringBuffer();        // format cookie version        this.formatter.format(buffer, new NameValuePair("$Version", Integer.toString(version)));        for (int i = 0; i < cookies.length; i++) {            buffer.append("; ");            Cookie2 cookie = (Cookie2) cookies[i];            // format cookie attributes            doFormatCookie2(cookie, buffer);        }        return buffer.toString();    }    /**     * Retrieves valid Port attribute value for the given ports array.     * e.g. "8000,8001,8002"     *     * @param ports int array of ports     */    private String createPortAttribute(int[] ports) {        StringBuffer portValue = new StringBuffer();        for (int i = 0, len = ports.length; i < len; i++) {            if (i > 0) {                portValue.append(",");            }            portValue.append(ports[i]);        }        return portValue.toString();    }    /**     * Parses the given Port attribute value (e.g. "8000,8001,8002")     * into an array of ports.     *     * @param portValue port attribute value     * @return parsed array of ports     * @throws MalformedCookieException if there is a problem in     *          parsing due to invalid portValue.     */    private int[] parsePortAttribute(final String portValue)            throws MalformedCookieException {        StringTokenizer st = new StringTokenizer(portValue, ",");        int[] ports = new int[st.countTokens()];        try {            int i = 0;            while(st.hasMoreTokens()) {                ports[i] = Integer.parseInt(st.nextToken().trim());                if (ports[i] < 0) {                  throw new MalformedCookieException ("Invalid Port attribute.");                }                ++i;            }        } catch (NumberFormatException e) {            throw new MalformedCookieException ("Invalid Port "                                                + "attribute: " + e.getMessage());        }        return ports;    }    /**     * Gets 'effective host name' as defined in RFC 2965.     * <p>     * If a host name contains no dots, the effective host name is     * that name with the string .local appended to it.  Otherwise     * the effective host name is the same as the host name.  Note     * that all effective host names contain at least one dot.     *     * @param host host name where cookie is received from or being sent to.     * @return     */    private static String getEffectiveHost(final String host) {        String effectiveHost = host.toLowerCase();        if (host.indexOf('.') < 0) {            effectiveHost += ".local";        }        return effectiveHost;    }    /**     * Performs domain-match as defined by the RFC2965.     * <p>     * Host A's name domain-matches host B's if     * <ol>     *   <ul>their host name strings string-compare equal; or</ul>     *   <ul>A is a HDN string and has the form NB, where N is a non-empty     *       name string, B has the form .B', and B' is a HDN string.  (So,     *       x.y.com domain-matches .Y.com but not Y.com.)</ul>     * </ol>     *     * @param host host name where cookie is received from or being sent to.     * @param domain The cookie domain attribute.     * @return true if the specified host matches the given domain.     */    public boolean domainMatch(String host, String domain) {        boolean match = host.equals(domain)                        || (domain.startsWith(".") && host.endsWith(domain));        return match;    }    /**     * Returns <tt>true</tt> if the given port exists in the given     * ports list.     *     * @param port port of host where cookie was received from or being sent to.     * @param ports port list     * @return true returns <tt>true</tt> if the given port exists in     *         the given ports list; <tt>false</tt> otherwise.     */    private boolean portMatch(int port, int[] ports) {        boolean portInList = false;        for (int i = 0, len = ports.length; i < len; i++) {            if (port == ports[i]) {                portInList = true;                break;            }        }        return portInList;    }    /**     * <tt>"Path"</tt> attribute handler for RFC 2965 cookie spec.     */    private class Cookie2PathAttributeHandler            implements CookieAttributeHandler {        /**         * Parse cookie path attribute.         */        public void parse(final Cookie cookie, final String path)                throws MalformedCookieException {            if (cookie == null) {                throw new IllegalArgumentException("Cookie may not be null");            }            if (path == null) {                throw new MalformedCookieException(                        "Missing value for path attribute");            }            if (path.trim().equals("")) {                throw new MalformedCookieException(                        "Blank value for path attribute");            }            cookie.setPath(path);            cookie.setPathAttributeSpecified(true);        }        /**         * Validate cookie path attribute. The value for the Path attribute must be a         * prefix of the request-URI (case-sensitive matching).         */        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");            }            String path = origin.getPath();            if (path == null) {                throw new IllegalArgumentException(                        "Path of origin host may not be null.");            }            if (cookie.getPath() == null) {                throw new MalformedCookieException("Invalid cookie state: " +                                                   "path attribute is null.");            }            if (path.trim().equals("")) {                path = PATH_DELIM;            }            if (!pathMatch(path, cookie.getPath())) {                throw new MalformedCookieException(                        "Illegal path attribute \"" + cookie.getPath()                        + "\". Path of origin: \"" + path + "\"");            }        }        /**         * Match cookie path attribute. The value for the Path attribute must be a         * prefix of the request-URI (case-sensitive matching).         */        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");            }            String path = origin.getPath();            if (cookie.getPath() == null) {                LOG.warn("Invalid cookie state: path attribute is null.");                return false;            }            if (path.trim().equals("")) {                path = PATH_DELIM;            }            if (!pathMatch(path, cookie.getPath())) {                return false;            }            return true;        }    }    /**     * <tt>"Domain"</tt> cookie attribute handler for RFC 2965 cookie spec.     */    private class Cookie2DomainAttributeHandler            implements CookieAttributeHandler {        /**         * Parse cookie domain attribute.         */        public void parse(final Cookie cookie, String domain)                throws MalformedCookieException {            if (cookie == null) {                throw new IllegalArgumentException("Cookie may not be null");            }            if (domain == null) {                throw new MalformedCookieException(                        "Missing value for domain attribute");            }            if (domain.trim().equals("")) {                throw new MalformedCookieException(                        "Blank value for domain attribute");            }            domain = domain.toLowerCase();            if (!domain.startsWith(".")) {                // Per RFC 2965 section 3.2.2                // "... If an explicitly specified value does not start with                // a dot, the user agent supplies a leading dot ..."                // That effectively implies that the domain attribute                 // MAY NOT be an IP address of a host name                domain = "." + domain;            }            cookie.setDomain(domain);            cookie.setDomainAttributeSpecified(true);        }        /**         * Validate cookie domain attribute.         */        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");            }            String host = origin.getHost().toLowerCase();            if (cookie.getDomain() == null) {                throw new MalformedCookieException("Invalid cookie state: " +                                                   "domain not specified");            }            String cookieDomain = cookie.getDomain().toLowerCase();            if (cookie.isDomainAttributeSpecified()) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -