📄 uri.java
字号:
} // if we get to this point, we need to resolve relative path // RFC 2396 5.2 #6 String path = ""; String basePath = p_base.getPath(); // 6a - get all but the last segment of the base URI path if (basePath != null && basePath.length() > 0) { int lastSlash = basePath.lastIndexOf('/'); if (lastSlash != -1) { path = basePath.substring(0, lastSlash+1); } } else if (m_path.length() > 0) { path = "/"; } // 6b - append the relative URI path path = path.concat(m_path); // 6c - remove all "./" where "." is a complete path segment int index = -1; while ((index = path.indexOf("/./")) != -1) { path = path.substring(0, index+1).concat(path.substring(index+3)); } // 6d - remove "." if path ends with "." as a complete path segment if (path.endsWith("/.")) { path = path.substring(0, path.length()-1); } // 6e - remove all "<segment>/../" where "<segment>" is a complete // path segment not equal to ".." index = 1; int segIndex = -1; String tempString = null; while ((index = path.indexOf("/../", index)) > 0) { tempString = path.substring(0, path.indexOf("/../")); segIndex = tempString.lastIndexOf('/'); if (segIndex != -1) { if (!tempString.substring(segIndex).equals("..")) { path = path.substring(0, segIndex+1).concat(path.substring(index+4)); index = segIndex; } else { index += 4; } } else { index += 4; } } // 6f - remove ending "<segment>/.." where "<segment>" is a // complete path segment if (path.endsWith("/..")) { tempString = path.substring(0, path.length()-3); segIndex = tempString.lastIndexOf('/'); if (segIndex != -1) { path = path.substring(0, segIndex+1); } } m_path = path; } /** * Initialize the scheme for this URI from a URI string spec. * * @param p_uriSpec the URI specification (cannot be null) * * @exception MalformedURIException if URI does not have a conformant * scheme */ private void initializeScheme(String p_uriSpec) throws MalformedURIException { int uriSpecLen = p_uriSpec.length(); int index = 0; String scheme = null; char testChar = '\0'; while (index < uriSpecLen) { testChar = p_uriSpec.charAt(index); if (testChar == ':' || testChar == '/' || testChar == '?' || testChar == '#') { break; } index++; } scheme = p_uriSpec.substring(0, index); if (scheme.length() == 0) { throw new MalformedURIException("No scheme found in URI."); } else { setScheme(scheme); } } /** * Initialize the authority (either server or registry based) * for this URI from a URI string spec. * * @param p_uriSpec the URI specification (cannot be null) * * @return true if the given string matched server or registry * based authority */ private boolean initializeAuthority(String p_uriSpec) { int index = 0; int start = 0; int end = p_uriSpec.length(); 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 * * @exception 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++;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -