📄 requestprocessor.java
字号:
/*
* SSL-Explorer
*
* Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package com.sslexplorer.replacementproxy;
import java.net.InetAddress;
import java.net.URL;
import java.util.Date;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.StringTokenizer;
import java.util.Vector;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.sslexplorer.boot.HttpConstants;
import com.sslexplorer.boot.PropertyList;
import com.sslexplorer.boot.RequestHandlerRequest;
import com.sslexplorer.boot.Util;
import com.sslexplorer.core.CoreAttributeConstants;
import com.sslexplorer.core.CoreEvent;
import com.sslexplorer.core.CoreEventConstants;
import com.sslexplorer.core.CoreServlet;
import com.sslexplorer.policyframework.Policy;
import com.sslexplorer.policyframework.ResourceAccessEvent;
import com.sslexplorer.security.SessionInfo;
import com.sslexplorer.security.User;
import com.sslexplorer.webforwards.ReplacementProxyWebForward;
/**
* @author Brett Smith <brett@3sp.com>
*/
public class RequestProcessor {
final static Log log = LogFactory.getLog(RequestProcessor.class);
private ContentCache cache;
private User user;
private boolean getFromCache;
private boolean keepAlive;
private StringBuffer connectionPath;
private byte[] content = null;
private ReplacementProxyWebForward webForward;
private StringBuffer parms;
private RequestParameterMap requestParameters ;
private URL context;
private boolean setWebforwardCookie;
private RequestHandlerRequest request;
private HeaderMap headerMap;
private SessionInfo session;
private String uniqueTicket;
private String proxiedUrl;
public RequestProcessor(ContentCache cache,
User user,
int maxAge,
RequestHandlerRequest request,
SessionInfo session,
String uniqueTicket,
ReplacementProxyWebForward webForward,
String proxiedUrl) {
this.cache = cache;
this.user = user;
this.request = request;
this.session = session;
this.webForward = webForward;
this.uniqueTicket = uniqueTicket;
this.proxiedUrl = proxiedUrl;
}
public String getProxiedUrl() {
return proxiedUrl;
}
public String getUniqueTicket() {
return uniqueTicket;
}
public String getMethod() {
return request.getMethod();
}
public RequestHandlerRequest getRequestX() {
return request;
}
public boolean isSetWebForwardCookie() {
return setWebforwardCookie;
}
public URL getContext() {
return context;
}
public SessionInfo getSessionInfo() {
return session;
}
public RequestParameterMap getRequestParameters() {
return requestParameters;
}
public String getParametersAsFormEncodedString() {
return parms.toString();
}
public byte[] getContent() {
return content;
}
public boolean isKeepAlive() {
return keepAlive;
}
public String getConnectionPath() {
return connectionPath.toString();
}
public boolean isGetFromCache() {
return getFromCache;
}
public String getEncodedUri() {
if (log.isInfoEnabled())
log.info("Returning URI " + (context.getPath().equals("") ? "/" : context.getPath()) + (parms.length() > 0 ? "?" + parms.toString() : ""));
return (context.getPath().equals("") ? "/" : context.getPath()) + (parms.length() > 0 ? "?" + parms.toString() : "");
}
public void processRequest() throws Exception {
// Keep alive is on by default with HTTP 1.1
// LDP - Conversion to HttpClient can ignore this
/*if (request.getProtocol().endsWith("1.1")) {
keepAlive = true;
}*/
// Create our own map of headers so they can be edited
headerMap = new HeaderMap();
for(Enumeration e = request.getFieldNames(); e.hasMoreElements(); ) {
String n = (String)e.nextElement();
for(Enumeration e2 = request.getFieldValues(n); e2.hasMoreElements(); ) {
String v = (String)e2.nextElement();
headerMap.putHeader(n, v);
}
}
// Build up the parameter map
requestParameters = new RequestParameterMap(request);
//
setWebforwardCookie = false;
String destUrl = requestParameters.getDestinationURL();
// TODO there could be an issue here.
if (destUrl != null) {
// webForward = (ReplacementProxyWebForward)CoreServlet.getServlet().getSystemDatabase().getWebForward(user.getPrincipalName(), destUrl);
setWebforwardCookie = true;
} else {
Cookie[] cookies = request.getCookies();
for (int i = 0; cookies != null && i < cookies.length && webForward == null; i++) {
if (cookies[i].getName().endsWith("webForward")) {
// webForward = (ReplacementProxyWebForward)CoreServlet.getServlet().getSystemDatabase().getWebForward(user.getPrincipalName(),
// Util.urlDecode(cookies[i].getValue()));
}
}
if (webForward == null) {
throw new Exception("Could not determine the web forward. Do you have cookies enabled?");
}
}
parms = new StringBuffer();
String url = requestParameters.getProxiedURL();
if (url == null) {
if (destUrl == null) {
throw new Exception("No sslex_url parameter provided.");
} else {
url = destUrl;
}
}
if (log.isDebugEnabled())
log.debug("Proxying [" + request.getMethod() + "] " + url);
int pidx = url.indexOf("?");
if (pidx != -1) {
parms.append(url.substring(pidx + 1));
url = url.substring(0, pidx);
}
context = new URL(url);
if (log.isDebugEnabled())
log.debug("Context " + context.toExternalForm());
// The web forward may restrict access to the target URL
PropertyList restrictTo = webForward.getRestrictToHosts();
if(!restrictTo.isEmpty()) {
boolean found = context.getHost().equals(new URL(webForward.getDestinationURL()).getHost());
for(Iterator i = restrictTo.iterator(); !found && i.hasNext(); ) {
String host = (String)i.next();
if(context.getHost().matches(Util.parseSimplePatternToRegExp(host))) {
found = true;
}
}
if(!found) {
throw new Exception("This resource (" + context.toExternalForm() + ") is restricted to a list of target hosts. This host is not in the list.");
}
}
connectionPath = new StringBuffer(url);
//Util.removeMe("Context path is " + context.toExternalForm());
for (Iterator e = requestParameters.getParameterNames(); e.hasNext();) {
String n = (String) e.next();
if (!n.equals("sslex_url") && !n.equals("sslex_ticket")) {
String v = requestParameters.getParameter(n);
// for (int i = 0; i < v.length; i++) {
if (parms.length() > 0) {
parms.append('&');
}
String name = Util.urlEncode(n);
parms.append(name);
parms.append('=');
String val = Util.urlEncode(v);
parms.append(val);
// }
}
}
if (log.isDebugEnabled())
log.debug("Parms = " + parms.toString());
// Add any other request parameters that may have been supplied with a
// GET form submission
if (request.getMethod().equalsIgnoreCase("get") && parms.length() > 0) {
connectionPath.append("?");
connectionPath.append(parms);
}
// Determine if the page can be retrieved from cache
getFromCache = false;
Date expiryDate = null;
if (cache != null && HttpConstants.METHOD_GET.equals(request.getMethod()) && cache.contains(connectionPath.toString())) {
getFromCache = true;
// HTTP 1.0
String cacheControl = request.getField(HttpConstants.HDR_PRAGMA);
if (cacheControl != null && cacheControl.equalsIgnoreCase("no-cache")) {
getFromCache = false;
} else {
String ifModifiedSince = request.getField(HttpConstants.HDR_IF_MODIFIED_SINCE);
if (ifModifiedSince != null) {
try {
// Dont get from cache if
getFromCache = false;
} catch (Exception e) {
}
}
}
// HTTP 1.1
if (getFromCache) {
cacheControl = request.getField(HttpConstants.HDR_CACHE_CONTROL);
if (cacheControl != null) {
StringTokenizer tok = new StringTokenizer(cacheControl, ";");
while (tok.hasMoreTokens()) {
String t = tok.nextToken().trim();
String tl = t.toLowerCase();
if (t.startsWith("no-cache") || t.startsWith("no-store")) {
getFromCache = false;
} else if (tl.startsWith("max-age")) {
expiryDate = new Date();
try {
expiryDate.setTime(expiryDate.getTime() - (Integer.parseInt(Util.valueOfNameValuePair(tl))));
} catch (Exception e) {
}
}
}
}
}
// Check expiry
if(getFromCache) {
CacheingOutputStream cos = (CacheingOutputStream) cache.retrieve(getConnectionPath());
if(expiryDate == null || ( expiryDate != null && cos.getCachedDate().after(expiryDate) ) ) {
// Still ok
}
else {
if (log.isDebugEnabled())
log.debug("Page expired");
getFromCache = false;
}
}
else {
if (log.isDebugEnabled())
log.debug("Not using cached page.");
}
}
}
public ReplacementProxyWebForward getWebForward() {
return webForward;
}
/**
* @return
*/
public String getRequestMethod() {
return request.getMethod();
}
/**
* @return
*/
public String getRemoteAddr() {
return request.getRemoteAddr();
}
/**
* @return
*/
public HttpSession getSession() {
return session.getHttpSession();
}
/**
* @param hdr
* @return
*/
public String getHeader(String hdr) {
Enumeration e = headerMap.getHeaders(hdr);
return e == null ? null : (String)e.nextElement();
}
/**
* @return
*/
public Enumeration getHeaderNames() {
return headerMap.keys();
}
/**
* @param hdr
* @return
*/
public Enumeration getHeaders(String hdr) {
return headerMap.getHeaders(hdr);
}
/**
* @param header
*/
public void addHeader(String name, String value) {
headerMap.putHeader(name, value);
}
/**
* @param name
* @param value
*/
public void setHeader(String name, String value) {
headerMap.setHeader(name, value);
}
class HeaderMap extends Hashtable {
private static final long serialVersionUID = -6768313767635812871L;
HeaderMap() {
}
void putHeader(String name, String value) {
Vector l = (Vector)get(name);
if(l == null) {
l = new Vector();
put(name, l);
}
l.addElement(value);
}
void setHeader(String name, String value) {
Vector l = new Vector();
l.addElement(value);
put(name, l);
}
Enumeration getHeaders(String hdr) {
Vector l = (Vector)get(hdr);
return l == null ? null : l.elements();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -