1 17 18 package javax.servlet.http; 19 20 import javax.servlet.ServletInputStream ; 21 import java.util.Hashtable ; 22 import java.util.ResourceBundle ; 23 import java.util.StringTokenizer ; 24 import java.io.IOException ; 25 26 33 34 35 public class HttpUtils { 36 37 private static final String LSTRING_FILE = 38 "javax.servlet.http.LocalStrings"; 39 private static ResourceBundle lStrings = 40 ResourceBundle.getBundle(LSTRING_FILE); 41 42 43 44 48 49 public HttpUtils() {} 50 51 52 53 54 55 86 87 static public Hashtable parseQueryString(String s) { 88 89 String valArray[] = null; 90 91 if (s == null) { 92 throw new IllegalArgumentException (); 93 } 94 Hashtable ht = new Hashtable (); 95 StringBuffer sb = new StringBuffer (); 96 StringTokenizer st = new StringTokenizer (s, "&"); 97 while (st.hasMoreTokens()) { 98 String pair = (String )st.nextToken(); 99 int pos = pair.indexOf('='); 100 if (pos == -1) { 101 throw new IllegalArgumentException (); 104 } 105 String key = parseName(pair.substring(0, pos), sb); 106 String val = parseName(pair.substring(pos+1, pair.length()), sb); 107 if (ht.containsKey(key)) { 108 String oldVals[] = (String []) ht.get(key); 109 valArray = new String [oldVals.length + 1]; 110 for (int i = 0; i < oldVals.length; i++) 111 valArray[i] = oldVals[i]; 112 valArray[oldVals.length] = val; 113 } else { 114 valArray = new String [1]; 115 valArray[0] = val; 116 } 117 ht.put(key, valArray); 118 } 119 return ht; 120 } 121 122 123 124 125 164 165 166 static public Hashtable parsePostData(int len, 167 ServletInputStream in) 168 { 169 172 if (len <=0) 173 return new Hashtable (); 175 if (in == null) { 176 throw new IllegalArgumentException (); 177 } 178 179 byte[] postedBytes = new byte [len]; 183 try { 184 int offset = 0; 185 186 do { 187 int inputLen = in.read (postedBytes, offset, len - offset); 188 if (inputLen <= 0) { 189 String msg = lStrings.getString("err.io.short_read"); 190 throw new IllegalArgumentException (msg); 191 } 192 offset += inputLen; 193 } while ((len - offset) > 0); 194 195 } catch (IOException e) { 196 throw new IllegalArgumentException (e.getMessage()); 197 } 198 199 204 try { 205 String postedBody = new String (postedBytes, 0, len, "8859_1"); 206 return parseQueryString(postedBody); 207 } catch (java.io.UnsupportedEncodingException e) { 208 throw new IllegalArgumentException (e.getMessage()); 211 } 212 } 213 214 215 216 217 220 221 static private String parseName(String s, StringBuffer sb) { 222 sb.setLength(0); 223 for (int i = 0; i < s.length(); i++) { 224 char c = s.charAt(i); 225 switch (c) { 226 case '+': 227 sb.append(' '); 228 break; 229 case '%': 230 try { 231 sb.append((char) Integer.parseInt(s.substring(i+1, i+3), 232 16)); 233 i += 2; 234 } catch (NumberFormatException e) { 235 throw new IllegalArgumentException (); 238 } catch (StringIndexOutOfBoundsException e) { 239 String rest = s.substring(i); 240 sb.append(rest); 241 if (rest.length()==2) 242 i++; 243 } 244 245 break; 246 default: 247 sb.append(c); 248 break; 249 } 250 } 251 return sb.toString(); 252 } 253 254 255 256 257 279 280 public static StringBuffer getRequestURL (HttpServletRequest req) { 281 StringBuffer url = new StringBuffer (); 282 String scheme = req.getScheme (); 283 int port = req.getServerPort (); 284 String urlPath = req.getRequestURI(); 285 286 289 url.append (scheme); url.append ("://"); 291 url.append (req.getServerName ()); 292 if ((scheme.equals ("http") && port != 80) 293 || (scheme.equals ("https") && port != 443)) { 294 url.append (':'); 295 url.append (req.getServerPort ()); 296 } 297 url.append(urlPath); 302 return url; 303 } 304 } 305 306 307 308 | Popular Tags |