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

📄 uri.java

📁 这是linux下ssl vpn的实现程序
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
      // 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("/../")) > 0) {
        tempString = path.substring(0, path.indexOf("/../"));
        segIndex = tempString.lastIndexOf('/');

        if (segIndex != -1) {
          if (!tempString.substring(segIndex++).equals("..")) {
            path = path.substring(0, segIndex).concat(path.substring(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)
   *
   * @throws 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 (userinfo, host and port) for this URI from a URI string spec.
   *
   * @param p_uriSpec the URI specification (cannot be null)
   *
   * @throws MalformedURIException if p_uriSpec violates syntax rules
   */
  private void initializeAuthority(String p_uriSpec) throws
      MalformedURIException {

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

    // userinfo is everything up @
    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 ':'
    String host = null;

    start = index;

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

      if (testChar == ':') {
        break;
      }

      index++;
    }

    host = p_uriSpec.substring(start, index);

    int port = -1;

    if (host.length() > 0) {

      // port
      if (testChar == ':') {
        index++;

        start = index;

        while (index < end) {
          index++;
        }

        String portStr = p_uriSpec.substring(start, index);

        if (portStr.length() > 0) {
          for (int i = 0; i < portStr.length(); i++) {
            if (!isDigit(portStr.charAt(i))) {
              throw new MalformedURIException(portStr +
                  " is invalid. Port should only contain digits!");
            }
          }

          try {
            port = Integer.parseInt(portStr);
          }
          catch (NumberFormatException nfe) {

            // can't happen
          }
        }
      }
    }

    setHost(host);
    setPort(port);
    setUserinfo(userinfo);
  }

  /**
   * Initialize the path for this URI from a URI string spec.
   *
   * @param p_uriSpec the URI specification (cannot be null)
   *
   * @throws MalformedURIException if p_uriSpec violates syntax rules
   */
  private void initializePath(String p_uriSpec) throws MalformedURIException {

    if (p_uriSpec == null) {
      throw new MalformedURIException(
          "Cannot initialize path from null string!");
    }

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

    // path - everything up to query string or fragment
    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(
              "Path contains invalid escape sequence!");
        }
      }
      else
      if (!isReservedCharacter(testChar) && !isUnreservedCharacter(testChar)) {
        if ('\\' != testChar) {
          throw new MalformedURIException("Path 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!");
          }
        }
        else
        if (!isReservedCharacter(testChar) && !isUnreservedCharacter(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!");
          }
        }
        else
        if (!isReservedCharacter(testChar) && !isUnreservedCharacter(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();

    if (m_userinfo != null || m_host != null || m_port != -1) {
      schemespec.append("//");
    }

    if (m_userinfo != null) {
      schemespec.append(m_userinfo);
      schemespec.append('@');
    }

    if (m_host != null) {
      schemespec.append(m_host);
    }

    if (m_port != -1) {
      schemespec.append(':');
      schemespec.append(m_port);
    }

    if (m_path != null) {
      schemespec.append( (m_path));
    }

    if (m_queryString != null) {
      schemespec.append('?');
      schemespec.append(m_queryString);
    }

    if (m_fragment != null) {
      schemespec.append('#');
      schemespec.append(m_fragment);
    }

    return schemespec.toString();
  }

  /**
   * Get the userinfo for this URI.
   *
   * @return the userinfo for this URI (null if not specified).
   */
  public String getUserinfo() {
    return m_userinfo;
  }

  /**
   * Get the host for this URI.
   *
   * @return the host for this URI (null if not specified).
   */
  public String getHost() {
    return m_host;
  }

  /**
   * Get the port for this URI.
   *
   * @return the port for this URI (-1 if not specified).
   */
  public int getPort() {
    return m_port;
  }

  /**
   * Get the path for this URI (optionally with the query string and fragment).
   *
   * @param p_includeQueryString if true (and query string is not null), then a "?" followed by the query string will be appended
   * @param p_includeFragment if true (and fragment is not null), then a "#" followed by the fragment will be appended
   *
       * @return the path for this URI possibly including the query string and fragment
   */
  public String getPath(boolean p_includeQueryString, boolean p_includeFragment) {

    StringBuffer pathString = new StringBuffer(m_path);

    if (p_includeQueryString && m_queryString != null) {
      pathString.append('?');
      pathString.append(m_queryString);
    }

    if (p_includeFragment && m_fragment != null) {
      pathString.append('#');
      pathString.append(m_fragment);
    }

    return pathString.toString();
  }

  /**
   * Get the path for this URI. Note that the value returned is the path only and does not include the query string or fragment.
   *
   * @return the path for this URI.
   */
  public String getPath() {
    return m_path;
  }

  /**
   * Get the query string for this URI.
   *
   * @return the query string for this URI. Null is returned if there was no "?" in the URI spec, empty string if there was a "?"
   *         but no query string following it.
   */
  public String getQueryString() {
    return m_queryString;
  }

  /**
   * Get the fragment for this URI.
   *
   * @return the fragment for this URI. Null is returned if there was no "#" in the URI spec, empty string if there was a "#" but
   *         no fragment following it.
   */
  public String getFragment() {
    return m_fragment;
  }

  /**
   * Set the scheme for this URI. The scheme is converted to lowercase before it is set.
   *
   * @param p_scheme the scheme for this URI (cannot be null)
   *
   * @throws MalformedURIException if p_scheme is not a conformant scheme name
   */
  public void setScheme(String p_scheme) throws MalformedURIException {

    if (p_scheme == null) {
      throw new MalformedURIException("Cannot set scheme from null string!");
    }

    if (!isConformantSchemeName(p_scheme)) {
      throw new MalformedURIException("The scheme is not conformant.");
    }

    m_scheme = p_scheme.toLowerCase();
  }

  /**
   * Set the userinfo for this URI. If a non-null value is passed in and the host value is null, then an exception is thrown.
   *
   * @param p_userinfo the userinfo for this URI
   *
   * @throws MalformedURIException if p_userinfo contains invalid characters
   */
  public void setUserinfo(String p_userinfo) throws MalformedURIException {

    if (p_userinfo == null) {
      m_userinfo = null;
    }
    else {
      if (m_host == null) {
        throw new MalformedURIException(
            "Userinfo cannot be set when host is null!");
      }

      // userinfo can contain alphanumerics, mark characters, escaped
      // and ';',':','&','=','+','$',','
      int index = 0;
      int end = p_userinfo.length();
      char testChar = '\0';

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

        if (testChar == '%') {
          if (index + 2 >= end || !isHex(p_userinfo.charAt(index + 1)) ||
              !isHex(p_userinfo.charAt(index + 2))) {
            throw new MalformedURIException(
                "Userinfo contains invalid escape sequence!");
          }
        }
        else
        if (!isUnreservedCharacter(testChar) &&
            USERINFO_CHARACTERS.indexOf(testChar) == -1) {
          throw new MalformedURIException(
              "Userinfo contains invalid character:" + testChar);
        }

        index++;
      }
    }

    m_userinfo = p_userinfo;
  }

  /**
   * Set the host for this URI. If null is passed in, the userinfo field is also set to null and the port is set to -1.
   *
   * @param p_host the host for this URI
   *
   * @throws MalformedURIException if p_host is not a valid IP address or DNS hostname.

⌨️ 快捷键说明

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