1 29 30 package com.caucho.quercus.env; 31 32 import javax.servlet.http.HttpServletRequest ; 33 34 import java.io.IOException ; 35 import java.io.NotSerializableException ; 36 import java.io.ObjectInputStream ; 37 import java.io.ObjectOutputStream ; 38 import java.util.Enumeration ; 39 import java.util.Map ; 40 import java.util.Set ; 41 42 45 public class ServerArrayValue extends ArrayValueImpl { 46 private static final StringValue SERVER_NAME_V 47 = new StringValueImpl("SERVER_NAME"); 48 private static final StringValue SERVER_PORT_V 49 = new StringValueImpl("SERVER_PORT"); 50 private static final StringValue REMOTE_HOST_V 51 = new StringValueImpl("REMOTE_HOST"); 52 private static final StringValue REMOTE_ADDR_V 53 = new StringValueImpl("REMOTE_ADDR"); 54 private static final StringValue REMOTE_PORT_V 55 = new StringValueImpl("REMOTE_PORT"); 56 57 private static final StringValue DOCUMENT_ROOT_V 58 = new StringValueImpl("DOCUMENT_ROOT"); 59 60 private static final StringValue SERVER_PROTOCOL_V 61 = new StringValueImpl("SERVER_PROTOCOL"); 62 private static final StringValue REQUEST_METHOD_V 63 = new StringValueImpl("REQUEST_METHOD"); 64 private static final StringValue QUERY_STRING_V 65 = new StringValueImpl("QUERY_STRING"); 66 67 private static final StringValue REQUEST_URI_V 68 = new StringValueImpl("REQUEST_URI"); 69 private static final StringValue SCRIPT_NAME_V 70 = new StringValueImpl("SCRIPT_NAME"); 71 private static final StringValue SCRIPT_FILENAME_V 72 = new StringValueImpl("SCRIPT_FILENAME"); 73 private static final StringValue PATH_INFO_V 74 = new StringValueImpl("PATH_INFO"); 75 private static final StringValue PATH_TRANSLATED_V 76 = new StringValueImpl("PATH_TRANSLATED"); 77 78 private static final StringValue PHP_SELF_V 79 = new StringValueImpl("PHP_SELF"); 80 81 private static final StringValue HTTPS_V 82 = new StringValueImpl("HTTPS"); 83 84 private static final StringValue HTTP_HOST_V 85 = new StringValueImpl("HTTP_HOST"); 86 87 private final Env _env; 88 89 private boolean _isFilled; 90 91 public ServerArrayValue(Env env) 92 { 93 _env = env; 94 } 95 96 99 public Object toObject() 100 { 101 return null; 102 } 103 104 107 public Value put(Value key, Value value) 108 { 109 if (! _isFilled) 110 fillMap(); 111 112 return super.put(key, value); 113 } 114 115 118 public Value put(Value value) 119 { 120 if (! _isFilled) 121 fillMap(); 122 123 return super.put(value); 124 } 125 126 129 public Value get(Value key) 130 { 131 if (! _isFilled) 132 fillMap(); 133 134 return super.get(key); 135 } 136 137 140 public Var getRef(Value key) 141 { 142 if (! _isFilled) 143 fillMap(); 144 145 return super.getRef(key); 146 } 147 148 151 public Value copy() 152 { 153 if (! _isFilled) 154 fillMap(); 155 156 return new ArrayValueImpl(this); 157 } 158 159 162 public Set <Map.Entry <Value,Value>> entrySet() 163 { 164 if (! _isFilled) 165 fillMap(); 166 167 return super.entrySet(); 168 } 169 170 173 public void put(String key, String value) 174 { 175 if (! _isFilled) 176 fillMap(); 177 178 super.put(key, value); 179 } 180 181 185 public void print(Env env) 186 { 187 env.print("Array"); 188 } 189 190 193 private void fillMap() 194 { 195 if (_isFilled) 196 return; 197 198 _isFilled = true; 199 200 for (Map.Entry <String ,String > entry: System.getenv().entrySet()) { 201 super.put(new StringValueImpl(entry.getKey()), 202 new StringValueImpl(entry.getValue())); 203 } 204 205 for (Map.Entry <Value,Value> entry: 206 _env.getQuercus().getServerEnvMap().entrySet()) { 207 super.put(entry.getKey(), entry.getValue()); 208 } 209 210 HttpServletRequest request = _env.getRequest(); 211 212 if (request != null) { 213 super.put(SERVER_NAME_V, 214 new StringValueImpl(request.getServerName())); 215 216 super.put(SERVER_PORT_V, 217 new LongValue(request.getServerPort())); 218 super.put(REMOTE_HOST_V, 219 new StringValueImpl(request.getRemoteHost())); 220 super.put(REMOTE_ADDR_V, 221 new StringValueImpl(request.getRemoteAddr())); 222 super.put(REMOTE_PORT_V, 223 new LongValue(request.getRemotePort())); 224 225 super.put(SERVER_PROTOCOL_V, 226 new StringValueImpl(request.getProtocol())); 227 super.put(REQUEST_METHOD_V, 228 new StringValueImpl(request.getMethod())); 229 230 if (request.getQueryString() != null) { 231 super.put(QUERY_STRING_V, 232 new StringValueImpl(request.getQueryString())); 233 } 234 235 super.put(DOCUMENT_ROOT_V, 236 new StringValueImpl(request.getRealPath("/"))); 237 238 super.put(SCRIPT_NAME_V, 239 new StringValueImpl(request.getContextPath() + 240 request.getServletPath())); 241 242 String requestURI = request.getRequestURI(); 243 String queryString = request.getQueryString(); 244 245 if (queryString != null) 246 requestURI = requestURI + '?' + queryString; 247 248 super.put(REQUEST_URI_V, 249 new StringValueImpl(requestURI)); 250 super.put(SCRIPT_FILENAME_V, 251 new StringValueImpl(request.getRealPath(request.getServletPath()))); 252 253 if (request.getPathInfo() != null) { 254 super.put(PATH_INFO_V, 255 new StringValueImpl(request.getPathInfo())); 256 super.put(PATH_TRANSLATED_V, 257 new StringValueImpl(request.getRealPath(request.getPathInfo()))); 258 } 259 260 if (request.isSecure()) 261 super.put(HTTPS_V, new StringValueImpl("on")); 262 263 String contextPath = request.getContextPath(); 264 String servletPath = request.getServletPath(); 265 String pathInfo = request.getPathInfo(); 266 267 if (pathInfo == null) 268 super.put(PHP_SELF_V, new StringValueImpl(contextPath + servletPath)); 269 else 270 super.put(PHP_SELF_V, new StringValueImpl(contextPath + servletPath + pathInfo)); 271 272 Enumeration e = request.getHeaderNames(); 273 while (e.hasMoreElements()) { 274 String key = (String ) e.nextElement(); 275 276 String value = request.getHeader(key); 277 278 if (key.equalsIgnoreCase("Host")) { 279 super.put(HTTP_HOST_V, new StringValueImpl(value)); 280 } 281 else { 282 super.put(convertHttpKey(key), new StringValueImpl(value)); 283 } 284 } 285 } 286 } 287 288 291 private StringValue convertHttpKey(String key) 292 { 293 StringBuilder sb = new StringBuilder (); 294 295 sb.append("HTTP_"); 296 297 int len = key.length(); 298 for (int i = 0; i < len; i++) { 299 char ch = key.charAt(i); 300 301 if (Character.isLowerCase(ch)) 302 sb.append(Character.toUpperCase(ch)); 303 else if (ch == '-') 304 sb.append('_'); 305 else 306 sb.append(ch); 307 } 308 309 return new StringValueImpl(sb.toString()); 310 } 311 312 316 private Object writeReplace() 317 { 318 if (! _isFilled) 319 fillMap(); 320 321 return new ArrayValueImpl(this); 322 } 323 } 324 325 | Popular Tags |