1 28 29 package com.caucho.server.connection; 30 31 import com.caucho.log.Log; 32 import com.caucho.util.CharCursor; 33 import com.caucho.util.HashMapImpl; 34 import com.caucho.util.StringCharCursor; 35 import com.caucho.vfs.ByteToChar; 36 37 import java.io.IOException ; 38 import java.io.InputStream ; 39 import java.io.UnsupportedEncodingException ; 40 import java.util.logging.Level ; 41 import java.util.logging.Logger ; 42 43 46 public class Form { 47 static final Logger log = Log.open(Form.class); 48 49 private final ByteToChar _converter = ByteToChar.create(); 50 51 58 public void parseQueryString(HashMapImpl<String ,String []> table, 59 String query, 60 String javaEncoding, 61 boolean isTop) 62 throws IOException 63 { 64 CharCursor is = new StringCharCursor(query); 65 66 ByteToChar converter = _converter; 67 try { 68 converter.setEncoding(javaEncoding); 69 } catch (UnsupportedEncodingException e) { 70 log.log(Level.FINE, e.toString(), e); 71 } 72 73 int ch = is.current(); 74 while (ch != is.DONE) { 75 for (; Character.isWhitespace((char) ch) || ch == '&'; ch = is.next()) { 76 } 77 78 converter.clear(); 79 for (; ch != is.DONE && ch != '=' && ch != '&'; ch = is.next()) 80 readChar(converter, is, ch, isTop); 81 82 String key = converter.getConvertedString(); 83 84 converter.clear(); 85 if (ch == '=') 86 ch = is.next(); 87 for (; ch != is.DONE && ch != '&'; ch = is.next()) 88 readChar(converter, is, ch, isTop); 89 90 String value = converter.getConvertedString(); 91 92 if (log.isLoggable(Level.FINE)) 93 log.fine("query: " + key + "=" + value); 94 95 String []oldValue = table.get(key); 96 97 if (key == null || key.equals("")) { 98 } 99 else if (oldValue == null) 100 table.put(key, new String [] { value }); 101 else { 102 String []newValue = new String [oldValue.length + 1]; 103 System.arraycopy(oldValue, 0, newValue, 0, oldValue.length); 104 newValue[oldValue.length] = value; 105 table.put(key, newValue); 106 } 107 } 108 } 109 110 118 private static void readChar(ByteToChar converter, CharCursor is, 119 int ch, boolean isTop) 120 throws IOException 121 { 122 if (ch == '+') 123 converter.addByte(' '); 124 else if (ch == '%') { 125 int ch1 = is.next(); 126 127 if (ch1 == 'u') { 128 ch1 = is.next(); 129 int ch2 = is.next(); 130 int ch3 = is.next(); 131 int ch4 = is.next(); 132 133 converter.addChar((char) ((toHex(ch1) << 12) + 134 (toHex(ch2) << 8) + 135 (toHex(ch3) << 4) + 136 (toHex(ch4)))); 137 } 138 else { 139 int ch2 = is.next(); 140 141 converter.addByte(((toHex(ch1) << 4) + toHex(ch2))); 142 } 143 } 144 else if (isTop) 145 converter.addByte((byte) ch); 146 else 147 converter.addChar((char) ch); 148 } 149 150 157 void parsePostData(HashMapImpl<String ,String []> table, InputStream is, 158 String javaEncoding) 159 throws IOException 160 { 161 ByteToChar converter = _converter; 162 try { 163 converter.setEncoding(javaEncoding); 164 } catch (UnsupportedEncodingException e) { 165 log.log(Level.FINE, e.toString(), e); 166 } 167 168 int ch = is.read(); 169 while (ch >= 0) { 170 for (; Character.isWhitespace((char) ch) || ch == '&'; ch = is.read()) { 171 } 172 173 converter.clear(); 174 for (; 175 ch >= 0 && ch != '=' && ch != '&' && 176 ! Character.isWhitespace((char) ch); 177 ch = is.read()) { 178 readChar(converter, is, ch); 179 } 180 181 String key = converter.getConvertedString(); 182 183 for (; Character.isWhitespace((char) ch); ch = is.read()) { 184 } 185 186 converter.clear(); 187 if (ch == '=') { 188 ch = is.read(); 189 for (; Character.isWhitespace((char) ch); ch = is.read()) { 190 } 191 } 192 193 for (; ch >= 0 && ch != '&'; ch = is.read()) 194 readChar(converter, is, ch); 195 196 String value = converter.getConvertedString(); 197 198 202 203 String []oldValue = table.get(key); 204 205 if (key == null || key.equals("")) { 206 } 207 else if (oldValue == null) 208 table.put(key, new String [] { value }); 209 else { 210 String []newValue = new String [oldValue.length + 1]; 211 System.arraycopy(oldValue, 0, newValue, 0, oldValue.length); 212 newValue[oldValue.length] = value; 213 table.put(key, newValue); 214 } 215 } 216 } 217 218 226 private static void readChar(ByteToChar converter, InputStream is, int ch) 227 throws IOException 228 { 229 if (ch == '+') 230 converter.addByte(' '); 231 else if (ch == '%') { 232 int ch1 = is.read(); 233 234 if (ch1 == 'u') { 235 ch1 = is.read(); 236 int ch2 = is.read(); 237 int ch3 = is.read(); 238 int ch4 = is.read(); 239 240 converter.addChar((char) ((toHex(ch1) << 12) + 241 (toHex(ch2) << 8) + 242 (toHex(ch3) << 4) + 243 (toHex(ch4)))); 244 } 245 else { 246 int ch2 = is.read(); 247 248 converter.addByte(((toHex(ch1) << 4) + toHex(ch2))); 249 } 250 } 251 else 252 converter.addByte(ch); 253 } 254 255 258 private static int toHex(int ch) 259 { 260 if (ch >= '0' && ch <= '9') 261 return ch - '0'; 262 else if (ch >= 'a' && ch <= 'f') 263 return ch - 'a' + 10; 264 else if (ch >= 'A' && ch <= 'F') 265 return ch - 'A' + 10; 266 else 267 return -1; 268 } 269 } 270 | Popular Tags |