1 8 package mx4j.tools.adaptor.http; 9 10 15 public class HttpUtil 16 { 17 18 25 public static String getCodeMessage(int code) 26 { 27 switch (code) 28 { 29 case HttpConstants.STATUS_OKAY: 30 return "OK"; 31 case HttpConstants.STATUS_NO_CONTENT: 32 return "No Content"; 33 case HttpConstants.STATUS_MOVED_PERMANENTLY: 34 return "Moved Permanently"; 35 case HttpConstants.STATUS_MOVED_TEMPORARILY: 36 return "Moved Temporarily"; 37 case HttpConstants.STATUS_BAD_REQUEST: 38 return "Bad Request"; 39 case HttpConstants.STATUS_FORBIDDEN: 40 return "Forbidden"; 41 case HttpConstants.STATUS_NOT_FOUND: 42 return "Not Found"; 43 case HttpConstants.STATUS_NOT_ALLOWED: 44 return "Method Not Allowed"; 45 case HttpConstants.STATUS_INTERNAL_ERROR: 46 return "Internal Server Error"; 47 case HttpConstants.STATUS_AUTHENTICATE: 48 return "Authentication requested"; 49 case HttpConstants.STATUS_NOT_IMPLEMENTED: 50 return "Not Implemented"; 51 default: 52 return "Unknown Code (" + code + ")"; 53 } 54 } 55 56 57 63 public static String canonicalizePath(String path) 64 { 65 char[] chars = path.toCharArray(); 66 int length = chars.length; 67 int idx; 68 int odx = 0; 69 while ((idx = indexOf(chars, length, '/', odx)) < length - 1) 70 { 71 int ndx = indexOf(chars, length, '/', idx + 1); 72 int kill = -1; 73 if (ndx == idx + 1) 74 { 75 kill = 1; 76 } 77 else if ((ndx >= idx + 2) && (chars[idx + 1] == '.')) 78 { 79 if (ndx == idx + 2) 80 { 81 kill = 2; 82 } 83 else if ((ndx == idx + 3) && (chars[idx + 2] == '.')) 84 { 85 kill = 3; 86 while ((idx > 0) && (chars[--idx] != '/')) 87 { 88 ++kill; 89 } 90 } 91 } 92 if (kill == -1) 93 { 94 odx = ndx; 95 } 96 else if (idx + kill >= length) 97 { 98 length = odx = idx + 1; 99 } 100 else 101 { 102 length -= kill; 103 System.arraycopy(chars, idx + 1 + kill, 104 chars, idx + 1, length - idx - 1); 105 odx = idx; 106 } 107 } 108 return new String (chars, 0, length); 109 } 110 111 112 protected static int indexOf(char[] chars, int length, char chr, int from) 113 { 114 while ((from < length) && (chars[from] != chr)) 115 { 116 ++from; 117 } 118 return from; 119 } 120 121 127 public static boolean booleanVariableValue(HttpInputStream in, String variable, boolean defaultValue) 128 { 129 if (in.getVariables().containsKey(variable)) 130 { 131 String result = (String )in.getVariables().get(variable); 132 return result.equals("true") || result.equals("1"); 133 } 134 return defaultValue; 135 } 136 } 137 | Popular Tags |