uri.java

来自「开源的axis2框架的源码。用于开发WEBSERVER」· Java 代码 · 共 1,619 行 · 第 1/5 页

JAVA
1,619
字号
        char testChar = '\0';
        String userinfo = null;

        // userinfo is everything up to @
        if (p_uriSpec.indexOf('@', start) != -1) {
            while (index < end) {
                testChar = p_uriSpec.charAt(index);
                if (testChar == '@') {
                    break;
                }
                index++;
            }
            userinfo = p_uriSpec.substring(start, index);
            index++;
        }

        // host is everything up to last ':', or up to
        // and including ']' if followed by ':'.
        String host = null;
        start = index;
        boolean hasPort = false;
        if (index < end) {
            if (p_uriSpec.charAt(start) == '[') {
                int bracketIndex = p_uriSpec.indexOf(']', start);
                index = (bracketIndex != -1) ? bracketIndex : end;
                if (index + 1 < end && p_uriSpec.charAt(index + 1) == ':') {
                    ++index;
                    hasPort = true;
                } else {
                    index = end;
                }
            } else {
                int colonIndex = p_uriSpec.lastIndexOf(':', end);
                index = (colonIndex > start) ? colonIndex : end;
                hasPort = (index != end);
            }
        }
        host = p_uriSpec.substring(start, index);
        int port = -1;
        if (host.length() > 0) {
            // port
            if (hasPort) {
                index++;
                start = index;
                while (index < end) {
                    index++;
                }
                String portStr = p_uriSpec.substring(start, index);
                if (portStr.length() > 0) {
                    // REVISIT: Remove this code.
                    /** for (int i = 0; i < portStr.length(); i++) {
                     if (!isDigit(portStr.charAt(i))) {
                     throw new MalformedURIException(
                     portStr +
                     " is invalid. Port should only contain digits!");
                     }
                     }**/
                    // REVISIT: Remove this code.
                    // Store port value as string instead of integer.
                    try {
                        port = Integer.parseInt(portStr);
                        if (port == -1) --port;
                    }
                    catch (NumberFormatException nfe) {
                        port = -2;
                    }
                }
            }
        }

        if (isValidServerBasedAuthority(host, port, userinfo)) {
            m_host = host;
            m_port = port;
            m_userinfo = userinfo;
            return true;
        }
        // Note: Registry based authority is being removed from a
        // new spec for URI which would obsolete RFC 2396. If the
        // spec is added to XML errata, processing of reg_name
        // needs to be removed. - mrglavas.
        else if (isValidRegistryBasedAuthority(p_uriSpec)) {
            m_regAuthority = p_uriSpec;
            return true;
        }
        return false;
    }

    /**
     * Determines whether the components host, port, and user info are valid as a server authority.
     *
     * @param host     the host component of authority
     * @param port     the port number component of authority
     * @param userinfo the user info component of authority
     * @return true if the given host, port, and userinfo compose a valid server authority
     */
    private boolean isValidServerBasedAuthority(String host, int port, String userinfo) {

        // Check if the host is well formed.
        if (!isWellFormedAddress(host)) {
            return false;
        }

        // Check that port is well formed if it exists.
        // REVISIT: There's no restriction on port value ranges, but
        // perform the same check as in setPort to be consistent. Pass
        // in a string to this method instead of an integer.
        if (port < -1 || port > 65535) {
            return false;
        }

        // Check that userinfo is well formed if it exists.
        if (userinfo != null) {
            // Userinfo can contain alphanumerics, mark characters, escaped
            // and ';',':','&','=','+','$',','
            int index = 0;
            int end = userinfo.length();
            char testChar = '\0';
            while (index < end) {
                testChar = userinfo.charAt(index);
                if (testChar == '%') {
                    if (index + 2 >= end ||
                            !isHex(userinfo.charAt(index + 1)) ||
                            !isHex(userinfo.charAt(index + 2))) {
                        return false;
                    }
                    index += 2;
                } else if (!isUserinfoCharacter(testChar)) {
                    return false;
                }
                ++index;
            }
        }
        return true;
    }

    /**
     * Determines whether the given string is a registry based authority.
     *
     * @param authority the authority component of a URI
     * @return true if the given string is a registry based authority
     */
    private boolean isValidRegistryBasedAuthority(String authority) {
        int index = 0;
        int end = authority.length();
        char testChar;

        while (index < end) {
            testChar = authority.charAt(index);

            // check for valid escape sequence
            if (testChar == '%') {
                if (index + 2 >= end ||
                        !isHex(authority.charAt(index + 1)) ||
                        !isHex(authority.charAt(index + 2))) {
                    return false;
                }
                index += 2;
            }
            // can check against path characters because the set
            // is the same except for '/' which we've already excluded.
            else if (!isPathCharacter(testChar)) {
                return false;
            }
            ++index;
        }
        return true;
    }

    /**
     * Initialize the path for this URI from a URI string spec.
     *
     * @param p_uriSpec     the URI specification (cannot be null)
     * @param p_nStartIndex the index to begin scanning from
     * @throws MalformedURIException if p_uriSpec violates syntax rules
     */
    private void initializePath(String p_uriSpec, int p_nStartIndex)
            throws MalformedURIException {
        if (p_uriSpec == null) {
            throw new MalformedURIException(
                    "Cannot initialize path from null string!");
        }

        int index = p_nStartIndex;
        int start = p_nStartIndex;
        int end = p_uriSpec.length();
        char testChar = '\0';

        // path - everything up to query string or fragment
        if (start < end) {
            // RFC 2732 only allows '[' and ']' to appear in the opaque part.
            if (getScheme() == null || p_uriSpec.charAt(start) == '/') {

                // Scan path.
                // abs_path = "/"  path_segments
                // rel_path = rel_segment [ abs_path ]
                while (index < end) {
                    testChar = p_uriSpec.charAt(index);

                    // check for valid escape sequence
                    if (testChar == '%') {
                        if (index + 2 >= end ||
                                !isHex(p_uriSpec.charAt(index + 1)) ||
                                !isHex(p_uriSpec.charAt(index + 2))) {
                            throw new MalformedURIException(
                                    "Path contains invalid escape sequence!");
                        }
                        index += 2;
                    }
                    // Path segments cannot contain '[' or ']' since pchar
                    // production was not changed by RFC 2732.
                    else if (!isPathCharacter(testChar)) {
                        if (testChar == '?' || testChar == '#') {
                            break;
                        }
                        throw new MalformedURIException(
                                "Path contains invalid character: " + testChar);
                    }
                    ++index;
                }
            } else {

                // Scan opaque part.
                // opaque_part = uric_no_slash *uric
                while (index < end) {
                    testChar = p_uriSpec.charAt(index);

                    if (testChar == '?' || testChar == '#') {
                        break;
                    }

                    // check for valid escape sequence
                    if (testChar == '%') {
                        if (index + 2 >= end ||
                                !isHex(p_uriSpec.charAt(index + 1)) ||
                                !isHex(p_uriSpec.charAt(index + 2))) {
                            throw new MalformedURIException(
                                    "Opaque part contains invalid escape sequence!");
                        }
                        index += 2;
                    }
                    // If the scheme specific part is opaque, it can contain '['
                    // and ']'. uric_no_slash wasn't modified by RFC 2732, which
                    // I've interpreted as an error in the spec, since the
                    // production should be equivalent to (uric - '/'), and uric
                    // contains '[' and ']'. - mrglavas
                    else if (!isURICharacter(testChar)) {
                        throw new MalformedURIException(
                                "Opaque part contains invalid character: " + testChar);
                    }
                    ++index;
                }
            }
        }
        m_path = p_uriSpec.substring(start, index);

        // query - starts with ? and up to fragment or end
        if (testChar == '?') {
            index++;
            start = index;
            while (index < end) {
                testChar = p_uriSpec.charAt(index);
                if (testChar == '#') {
                    break;
                }
                if (testChar == '%') {
                    if (index + 2 >= end ||
                            !isHex(p_uriSpec.charAt(index + 1)) ||
                            !isHex(p_uriSpec.charAt(index + 2))) {
                        throw new MalformedURIException(
                                "Query string contains invalid escape sequence!");
                    }
                    index += 2;
                } else if (!isURICharacter(testChar)) {
                    throw new MalformedURIException(
                            "Query string contains invalid character: " + testChar);
                }
                index++;
            }
            m_queryString = p_uriSpec.substring(start, index);
        }

        // fragment - starts with #
        if (testChar == '#') {
            index++;
            start = index;
            while (index < end) {
                testChar = p_uriSpec.charAt(index);

                if (testChar == '%') {
                    if (index + 2 >= end ||
                            !isHex(p_uriSpec.charAt(index + 1)) ||
                            !isHex(p_uriSpec.charAt(index + 2))) {
                        throw new MalformedURIException(
                                "Fragment contains invalid escape sequence!");
                    }
                    index += 2;
                } else if (!isURICharacter(testChar)) {
                    throw new MalformedURIException(
                            "Fragment contains invalid character: " + testChar);
                }
                index++;
            }
            m_fragment = p_uriSpec.substring(start, index);
        }
    }

    /**
     * Get the scheme for this URI.
     *
     * @return the scheme for this URI
     */
    public String getScheme() {
        return m_scheme;
    }

    /**
     * Get the scheme-specific part for this URI (everything following the scheme and the first
     * colon). See RFC 2396 Section 5.2 for spec.
     *
     * @return the scheme-specific part for this URI
     */
    public String getSchemeSpecificPart() {
        StringBuffer schemespec = new StringBuffer();

⌨️ 快捷键说明

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