1 19 20 package com.sslexplorer.replacementproxy; 21 22 import java.net.MalformedURLException ; 23 import java.net.URL ; 24 import java.util.Date ; 25 import java.util.Enumeration ; 26 import java.util.Hashtable ; 27 import java.util.Iterator ; 28 import java.util.StringTokenizer ; 29 import java.util.Vector ; 30 31 import javax.servlet.http.HttpSession ; 32 33 import org.apache.commons.logging.Log; 34 import org.apache.commons.logging.LogFactory; 35 36 import com.sslexplorer.boot.HttpConstants; 37 import com.sslexplorer.boot.PropertyList; 38 import com.sslexplorer.boot.RequestHandlerRequest; 39 import com.sslexplorer.boot.Util; 40 import com.sslexplorer.core.CoreEvent; 41 import com.sslexplorer.core.CoreServlet; 42 import com.sslexplorer.core.RequestParameterMap; 43 import com.sslexplorer.core.stringreplacement.VariableReplacement; 44 import com.sslexplorer.policyframework.LaunchSession; 45 import com.sslexplorer.policyframework.ResourceAccessEvent; 46 import com.sslexplorer.security.SessionInfo; 47 import com.sslexplorer.webforwards.ReplacementProxyWebForward; 48 import com.sslexplorer.webforwards.WebForwardEventConstants; 49 50 51 52 61 public class RequestProcessor { 62 final static Log log = LogFactory.getLog(RequestProcessor.class); 63 64 private ContentCache cache; 65 private boolean getFromCache; 66 private ReplacementProxyWebForward webForward; 67 private RequestParameterMap requestParameters ; 68 private RequestHandlerRequest request; 69 private HeaderMap headerMap; 70 private LaunchSession launchSession; 71 private URL requestBaseURL; 72 73 81 public RequestProcessor(ContentCache cache, 82 int maxAge, 83 RequestHandlerRequest request, 84 LaunchSession launchSession) throws MalformedURLException { 85 this.cache = cache; 86 this.request = request; 87 this.launchSession = launchSession; 88 this.webForward = (ReplacementProxyWebForward)launchSession.getResource(); 89 90 93 StringBuffer buf = new StringBuffer (); 94 buf.append(request.isSecure() ? "https" : "http"); 95 buf.append("://"); 96 buf.append(request.getHost()); 97 if( request.getPort() > 0 && ( ( request.isSecure() && request.getPort() != 443 ) || 98 ( !request.isSecure() && request.getPort() != 80 ) ) ) { 99 buf.append(":"); 100 buf.append(request.getPort()); 101 } 102 requestBaseURL = new URL (buf.toString()); 103 } 104 105 111 public URL getRequestBaseURL() { 112 return requestBaseURL; 113 } 114 115 121 public String getLaunchId() { 122 return launchSession.getId(); 123 } 124 125 public LaunchSession getLaunchSession() { 126 return launchSession; 127 } 128 129 134 public String getMethod() { 135 return request.getMethod(); 136 } 137 138 143 public RequestHandlerRequest getRequest() { 144 return request; 145 } 146 147 152 public SessionInfo getSessionInfo() { 153 return launchSession.getSession(); 154 } 155 156 161 public RequestParameterMap getRequestParameters() { 162 return requestParameters; 163 } 164 165 169 public boolean isGetFromCache() { 170 return getFromCache; 171 } 172 173 179 public String getUriEncoded() { 180 String uriEncoded = Util.isNullOrTrimmedBlank(requestParameters.getProxiedURLBase().getFile()) ? "/" : requestParameters.getProxiedURLBase().getFile(); 181 if (log.isDebugEnabled()) 182 log.debug("Returning URI " + uriEncoded); 183 return uriEncoded; 184 } 185 186 public void processRequest() throws Exception { 187 188 headerMap = new HeaderMap(); 190 for(Enumeration e = request.getFieldNames(); e.hasMoreElements(); ) { 191 String n = (String )e.nextElement(); 192 for(Enumeration e2 = request.getFieldValues(n); e2.hasMoreElements(); ) { 193 String v = (String )e2.nextElement(); 194 headerMap.putHeader(n, v); 195 } 196 } 197 198 requestParameters = new RequestParameterMap(request); 200 201 VariableReplacement r = new VariableReplacement(); 202 r.setRequest(request); 203 r.setSession(launchSession.getSession()); 204 r.setPolicy(launchSession.getPolicy()); 205 String actualURL = r.replace(webForward.getDestinationURL()); 206 207 if (requestParameters.getProxiedURL() == null) { 208 throw new Exception ("No sslex_url parameter provided."); 209 } 210 211 if (log.isDebugEnabled()) 212 log.debug("Proxying [" + request.getMethod() + "] " + requestParameters.getProxiedURL()); 213 214 URL proxiedURLBase = requestParameters.getProxiedURLBase(); 215 if (log.isDebugEnabled()) 216 log.debug("Proxied URL base " + proxiedURLBase.toExternalForm()); 217 218 CoreServlet.getServlet().fireCoreEvent(new ResourceAccessEvent(this, 219 WebForwardEventConstants.WEB_FORWARD_RESOURCE_LOADED, webForward, launchSession.getPolicy(), launchSession.getSession(), CoreEvent.STATE_SUCCESSFUL). 220 addAttribute(WebForwardEventConstants.EVENT_ATTR_WEB_FORWARD_URL, proxiedURLBase.toExternalForm())); 221 222 PropertyList restrictTo = webForward.getRestrictToHosts(); 224 if(!restrictTo.isEmpty()) { 225 boolean found = proxiedURLBase.getHost().equals(new URL (actualURL).getHost()); 226 for(Iterator i = restrictTo.iterator(); !found && i.hasNext(); ) { 227 String host = (String )i.next(); 228 if(proxiedURLBase.getHost().matches(Util.parseSimplePatternToRegExp(host))) { 229 found = true; 230 } 231 } 232 if(!found) { 233 throw new Exception ("This resource (" + proxiedURLBase.toExternalForm() + ") is restricted to a list of target hosts. This host is not in the list."); 234 } 235 236 } 237 238 240 getFromCache = false; 241 Date expiryDate = null; 242 if (cache != null && HttpConstants.METHOD_GET.equals(request.getMethod()) && cache.contains(requestParameters.getProxiedURL())) { 243 getFromCache = true; 244 245 String cacheControl = request.getField(HttpConstants.HDR_PRAGMA); 247 if (cacheControl != null && cacheControl.equalsIgnoreCase("no-cache")) { 248 getFromCache = false; 249 } else { 250 String ifModifiedSince = request.getField(HttpConstants.HDR_IF_MODIFIED_SINCE); 251 if (ifModifiedSince != null) { 252 try { 253 getFromCache = false; 255 } catch (Exception e) { 256 } 257 } 258 } 259 260 if (getFromCache) { 262 cacheControl = request.getField(HttpConstants.HDR_CACHE_CONTROL); 263 if (cacheControl != null) { 264 StringTokenizer tok = new StringTokenizer (cacheControl, ";"); 265 while (tok.hasMoreTokens()) { 266 String t = tok.nextToken().trim(); 267 String tl = t.toLowerCase(); 268 if (t.startsWith("no-cache") || t.startsWith("no-store")) { 269 getFromCache = false; 270 } else if (tl.startsWith("max-age")) { 271 expiryDate = new Date (); 272 try { 273 expiryDate.setTime(expiryDate.getTime() - (Integer.parseInt(Util.valueOfNameValuePair(tl)))); 274 } catch (Exception e) { 275 } 276 } 277 } 278 } 279 } 280 281 if(getFromCache) { 283 CacheingOutputStream cos = (CacheingOutputStream) cache.retrieve(requestParameters.getProxiedURL()); 284 if(expiryDate == null || ( expiryDate != null && cos.getCachedDate().after(expiryDate) ) ) { 285 } 287 else { 288 if (log.isDebugEnabled()) 289 log.debug("Page expired"); 290 getFromCache = false; 291 } 292 } 293 else { 294 if (log.isDebugEnabled()) 295 log.debug("Not using cached page."); 296 } 297 } 298 } 299 300 public ReplacementProxyWebForward getWebForward() { 301 return webForward; 302 } 303 304 307 public String getRequestMethod() { 308 return request.getMethod(); 309 } 310 311 314 public HttpSession getSession() { 315 return getSessionInfo().getHttpSession(); 316 } 317 318 322 public String getHeader(String hdr) { 323 Enumeration e = headerMap.getHeaders(hdr); 324 return e == null ? null : (String )e.nextElement(); 325 } 326 327 330 public Enumeration getHeaderNames() { 331 return headerMap.keys(); 332 } 333 334 338 public Enumeration getHeaders(String hdr) { 339 return headerMap.getHeaders(hdr); 340 } 341 342 class HeaderMap extends Hashtable { 343 344 private static final long serialVersionUID = -6768313767635812871L; 345 346 HeaderMap() { 347 348 } 349 350 void putHeader(String name, String value) { 351 Vector l = (Vector )get(name); 352 if(l == null) { 353 l = new Vector (); 354 put(name, l); 355 } 356 l.addElement(value); 357 } 358 359 void setHeader(String name, String value) { 360 Vector l = new Vector (); 361 l.addElement(value); 362 put(name, l); 363 } 364 365 Enumeration getHeaders(String hdr) { 366 Vector l = (Vector )get(hdr); 367 return l == null ? null : l.elements(); 368 369 } 370 } 371 } 372 | Popular Tags |