urlmodule.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 961 行 · 第 1/2 页

JAVA
961
字号
    StringValue sb = env.createUnicodeBuilder();    ArrayValueImpl value = new ArrayValueImpl();    // XXX: php/1i04.qa contradicts:    // value.put("path", "");    ParseUrlState state = ParseUrlState.INIT;    StringValue user = null;    for (; i < length; i++) {      char ch = str.charAt(i);      switch (ch) {      case ':':        if (state == ParseUrlState.INIT) {          value.put(env.createString("scheme"), sb);          sb = env.createUnicodeBuilder();          if (length <= i + 1 || str.charAt(i + 1) != '/') {            state = ParseUrlState.PATH;          }          else if (length <= i + 2 || str.charAt(i + 2) != '/') {            state = ParseUrlState.PATH;          }          else if (length <= i + 3 || str.charAt(i + 3) != '/') {            i += 2;            state = ParseUrlState.USER;          }          else {            // file:///foo            i += 2;            state = ParseUrlState.PATH;          }        }        else if (state == ParseUrlState.USER) {          user = sb;          sb = env.createUnicodeBuilder();          state = ParseUrlState.PASS;        }        else if (state == ParseUrlState.HOST) {          value.put(env.createString("host"), sb);          sb = env.createUnicodeBuilder();          state = ParseUrlState.PORT;        }        else          sb.append(ch);        break;      case '@':        if (state == ParseUrlState.USER) {          value.put(env.createString("user"), sb);          sb = env.createUnicodeBuilder();          state = ParseUrlState.HOST;        }        else if (state == ParseUrlState.PASS) {          value.put(env.createString("user"), user);          value.put(env.createString("pass"), sb);          sb = env.createUnicodeBuilder();          state = ParseUrlState.HOST;        }        else          sb.append(ch);        break;      case '/':        if (state == ParseUrlState.USER || state == ParseUrlState.HOST) {          value.put(env.createString("host"), sb);          sb = env.createUnicodeBuilder();          state = ParseUrlState.PATH;          sb.append(ch);        }        else if (state == ParseUrlState.PASS) {          value.put(env.createString("host"), user);          value.put(env.createString("port"), new LongValue(sb.toLong()));          sb = env.createUnicodeBuilder();          state = ParseUrlState.PATH;          sb.append(ch);        }        else if (state == ParseUrlState.PORT) {          value.put(env.createString("port"), new LongValue(sb.toLong()));          sb = env.createUnicodeBuilder();          state = ParseUrlState.PATH;          sb.append(ch);        }        else          sb.append(ch);        break;      case '?':        if (state == ParseUrlState.USER || state == ParseUrlState.HOST) {          value.put(env.createString("host"), sb);          sb = env.createUnicodeBuilder();          state = ParseUrlState.QUERY;        }        else if (state == ParseUrlState.PASS) {          value.put(env.createString("host"), user);          value.put(env.createString("port"), new LongValue(sb.toLong()));          sb = env.createUnicodeBuilder();          state = ParseUrlState.QUERY;        }        else if (state == ParseUrlState.PORT) {          value.put(env.createString("port"), new LongValue(sb.toLong()));          sb = env.createUnicodeBuilder();          state = ParseUrlState.QUERY;        }        else if (state == ParseUrlState.PATH) {          if (sb.length() > 0)            value.put(env.createString("path"), sb);          sb = env.createUnicodeBuilder();          state = ParseUrlState.QUERY;        }        else          sb.append(ch);        break;      case '#':        if (state == ParseUrlState.USER || state == ParseUrlState.HOST) {          value.put(env.createString("host"), sb);          sb = env.createUnicodeBuilder();          state = ParseUrlState.FRAGMENT;        }        else if (state == ParseUrlState.PASS) {          value.put(env.createString("host"), user);          value.put(env.createString("port"), new LongValue(sb.toLong()));          sb = env.createUnicodeBuilder();          state = ParseUrlState.FRAGMENT;        }        else if (state == ParseUrlState.PORT) {          value.put(env.createString("port"), new LongValue(sb.toLong()));          sb = env.createUnicodeBuilder();          state = ParseUrlState.FRAGMENT;        }        else if (state == ParseUrlState.PATH) {          if (sb.length() > 0)            value.put(env.createString("path"), sb);          sb = env.createUnicodeBuilder();          state = ParseUrlState.FRAGMENT;        }        else if (state == ParseUrlState.QUERY) {          if (sb.length() > 0)            value.put(env.createString("query"), sb);          sb = env.createUnicodeBuilder();          state = ParseUrlState.FRAGMENT;        }        else          sb.append(ch);        break;      default:        sb.append((char) ch);        break;      }    }    if (sb.length() == 0) {    }    else if (state == ParseUrlState.USER	     || state == ParseUrlState.HOST)      value.put(env.createString("host"), sb);    else if (state == ParseUrlState.PASS) {      value.put(env.createString("host"), user);      value.put(env.createString("port"), new LongValue(sb.toLong()));    }    else if (state == ParseUrlState.PORT) {      value.put(env.createString("port"), new LongValue(sb.toLong()));    }    else if (state == ParseUrlState.QUERY)      value.put(env.createString("query"), sb);    else if (state == ParseUrlState.FRAGMENT)      value.put(env.createString("fragment"), sb);    else      value.put(env.createString("path"), sb);    return value;  }  /**   * Returns the decoded string.   */  public static String rawurldecode(String s)  {    if (s == null)      return "";    int len = s.length();    StringBuilder sb = new StringBuilder();    for (int i = 0; i < len; i++) {      char ch = s.charAt(i);      if (ch == '%' && i + 2 < len) {        int d1 = s.charAt(i + 1);        int d2 = s.charAt(i + 2);        int v = 0;        if ('0' <= d1 && d1 <= '9')          v = 16 * (d1 - '0');        else if ('a' <= d1 && d1 <= 'f')          v = 16 * (d1 - 'a' + 10);        else if ('A' <= d1 && d1 <= 'F')          v = 16 * (d1 - 'A' + 10);        else {          sb.append('%');          continue;        }        if ('0' <= d2 && d2 <= '9')          v += (d2 - '0');        else if ('a' <= d2 && d2 <= 'f')          v += (d2 - 'a' + 10);        else if ('A' <= d2 && d2 <= 'F')          v += (d2 - 'A' + 10);        else {          sb.append('%');          continue;        }        i += 2;        sb.append((char) v);      }      else        sb.append(ch);    }    return sb.toString();  }  /**   * Encodes the url   */  public static String rawurlencode(String str)  {    if (str == null)      return "";        StringBuilder sb = new StringBuilder();    for (int i = 0; i < str.length(); i++) {      char ch = str.charAt(i);      if ('a' <= ch && ch <= 'z' ||          'A' <= ch && ch <= 'Z' ||          '0' <= ch && ch <= '9' ||          ch == '-' || ch == '_' || ch == '.') {        sb.append(ch);      }      else {        sb.append('%');        sb.append(toHexDigit(ch >> 4));        sb.append(toHexDigit(ch));      }    }    return sb.toString();  }  enum ParseUrlState {    INIT, USER, PASS, HOST, PORT, PATH, QUERY, FRAGMENT  };  /**   * Gets the magic quotes value.   */  public static StringValue urlencode(StringValue str)  {    StringValue sb = str.createStringBuilder();    urlencode(sb, str);    return sb;  }  /**   * Gets the magic quotes value.   */  private static void urlencode(StringValue sb, StringValue str)  {    int len = str.length();    for (int i = 0; i < len; i++) {      char ch = str.charAt(i);      if ('a' <= ch && ch <= 'z')        sb.append(ch);      else if ('A' <= ch && ch <= 'Z')        sb.append(ch);      else if ('0' <= ch && ch <= '9')        sb.append(ch);      else if (ch == '-' || ch == '_' || ch == '.')        sb.append(ch);      else if (ch == ' ')        sb.append('+');      else {        sb.append('%');        sb.append(toHexDigit(ch / 16));        sb.append(toHexDigit(ch));      }    }  }  /**   * Returns the decoded string.   */  public static String urldecode(String s)  {    if (s == null)      return "";      int len = s.length();    StringBuilder sb = new StringBuilder();    for (int i = 0; i < len; i++) {      char ch = s.charAt(i);      if (ch == '%' && i + 2 < len) {        int d1 = s.charAt(i + 1);        int d2 = s.charAt(i + 2);        int v = 0;        if ('0' <= d1 && d1 <= '9')          v = 16 * (d1 - '0');        else if ('a' <= d1 && d1 <= 'f')          v = 16 * (d1 - 'a' + 10);        else if ('A' <= d1 && d1 <= 'F')          v = 16 * (d1 - 'A' + 10);        else {          sb.append('%');          continue;        }        if ('0' <= d2 && d2 <= '9')          v += (d2 - '0');        else if ('a' <= d2 && d2 <= 'f')          v += (d2 - 'a' + 10);        else if ('A' <= d2 && d2 <= 'F')          v += (d2 - 'A' + 10);        else {          sb.append('%');          continue;        }        i += 2;        sb.append((char) v);      }      else if (ch == '+')        sb.append(' ');      else        sb.append(ch);    }    return sb.toString();  }  private static String getNextTag(BinaryInput input)    throws IOException  {    StringBuilder tag = new StringBuilder();    for (int ch = 0; ! input.isEOF() && ch != '<'; ch = input.read()) {}    while (! input.isEOF()) {      int ch = input.read();      if (Character.isWhitespace(ch))        break;      tag.append((char) ch);    }    return tag.toString();  }  /**   * Finds the next attribute in the stream and return the key and value   * as an array.   */  private static String [] getNextAttribute(BinaryInput input)    throws IOException  {    int ch;    consumeWhiteSpace(input);    StringBuilder attribute = new StringBuilder();    while (! input.isEOF()) {      ch = input.read();      if (isValidAttributeCharacter(ch))        attribute.append((char) ch);      else {        input.unread();        break;      }    }    if (attribute.length() == 0)      return null;    consumeWhiteSpace(input);    if (input.isEOF())      return new String[] { attribute.toString() };    ch = input.read();    if (ch != '=') {      input.unread();      return new String[] { attribute.toString() };    }    consumeWhiteSpace(input);    // check for quoting    int quote = ' ';    boolean quoted = false;    if (input.isEOF())      return new String[] { attribute.toString() };    ch = input.read();    if (ch == '"' || ch == '\'') {      quoted = true;      quote = ch;    } else      input.unread();    StringBuilder value = new StringBuilder();    while (! input.isEOF()) {      ch = input.read();      // mimics PHP behavior      if ((quoted && ch == quote) ||          (! quoted && Character.isWhitespace(ch)) || ch == '>')        break;      value.append((char) ch);    }    return new String[] { attribute.toString(), value.toString() };  }  private static void consumeWhiteSpace(BinaryInput input)    throws IOException  {    int ch = 0;    while (! input.isEOF() && Character.isWhitespace(ch = input.read())) {}    if (! Character.isWhitespace(ch))      input.unread();  }  private static boolean isValidAttributeCharacter(int ch)  {    return Character.isLetterOrDigit(ch) ||           (ch == '-') || (ch == '.') || (ch == '_') || (ch == ':');  }  private static char toHexDigit(int d)  {    d = d & 0xf;    if (d < 10)      return (char) ('0' + d);    else      return (char) ('A' + d - 10);  }}

⌨️ 快捷键说明

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