1 2 24 25 26 27 28 package com.lutris.http; 29 import java.io.IOException ; 30 import java.io.InputStream ; 31 import java.net.URLDecoder ; 32 import java.util.Hashtable ; 33 import java.util.StringTokenizer ; 34 35 import com.lutris.appserver.server.httpPresentation.HttpPresentationException; 36 import com.lutris.appserver.server.httpPresentation.HttpPresentationRequest; 37 38 41 public class HttpUtils { 42 54 private static 55 void AddKey(Hashtable table, String key, String value) 56 { 57 Object obj = table.get(key); 58 if (obj == null) { 59 table.put(key, value); 61 } else { 62 if (obj instanceof String ) { 63 String [] newarray = new String [2]; 65 newarray[0] = obj.toString(); 66 newarray[1] = value; 67 table.put(key, newarray); 68 newarray = null; 69 } else if (obj instanceof String []) { 70 String [] oldarray = (String []) obj; 72 String [] newarray = new String [oldarray.length +1]; 73 for (int i=0; i<oldarray.length; i++) { 74 newarray[i] = oldarray[i]; 75 } 76 newarray[oldarray.length] = value; 77 oldarray = null; 78 table.put(key, newarray); 79 newarray = null; 80 } else { 81 throw new Error ("Internal Error - " 84 + "Unexpected type in QueryString Hashtable."); 85 } 86 } 87 } 88 89 97 public static 98 String trimQueryString(String s) 99 { 100 int pos = s.indexOf("?"); 101 if (pos >= 0) return s.substring(pos+1); 102 return s; 103 } 104 105 119 public static 120 Hashtable parseQueryString(String s) 121 { 122 String qs = s.replace('?', '&'); 123 StringTokenizer tok = new StringTokenizer (qs, "&", false); 124 Hashtable table = new Hashtable (); 125 String pair, key, value; 126 while (tok.hasMoreTokens()) { 127 pair = tok.nextToken(); 128 int i = pair.indexOf("="); 129 if (i >= 0) { 130 key = URLDecoder.decode(pair.substring(0, i)); 131 value = URLDecoder.decode(pair.substring(i+1)); 132 AddKey(table, key, value); 133 } else { 134 int j = pair.indexOf(","); 135 if (j >= 0) { 136 String xstr = pair.substring(0, j).trim(); 138 xstr = URLDecoder.decode(xstr); 139 String ystr = pair.substring(j+1).trim(); 140 ystr = URLDecoder.decode(ystr); 141 try { 142 int xcoord = Integer.parseInt(xstr, 10); 143 int ycoord = Integer.parseInt(ystr, 10); 144 AddKey(table, "_imap_x", "" + xcoord); 146 AddKey(table, "_imap_y", "" + ycoord); 147 } catch (Throwable t) { 148 AddKey(table, "_garbage", URLDecoder.decode(pair)); 149 } 150 } else { 151 AddKey(table, "_garbage", URLDecoder.decode(pair)); 152 } 153 } 154 155 } 156 return table; 157 } 158 159 173 public static 174 Hashtable parsePostData(int len, InputStream in) 175 throws IllegalArgumentException , IOException 176 { 177 byte[] bytes = new byte[len]; 178 int n, count=0; 179 boolean eof=false; 180 while ((count<len)&&((n = in.read(bytes, count, len - count)) >0)) 181 count += n; 182 char[] chars = new char[count]; 183 for (n=0; n<count; n++) chars[n] = (char)((((int)bytes[n])+0x100)&0xff); 186 bytes = null; 187 return parseQueryString(new String (chars)); 188 } 189 190 202 public static 203 StringBuffer getRequestURL(HttpPresentationRequest req) 204 throws HttpPresentationException 205 { 206 StringBuffer buf = new StringBuffer (); 207 buf.append("http://"); 208 buf.append(req.getServerName()); 209 int port = req.getServerPort(); 210 if (port != 80) buf.append(":" + port); 211 buf.append(req.getRequestURI()); 212 return buf; 213 } 214 } 215 216 | Popular Tags |