httpmanager.java
来自「很棒的web服务器源代码」· Java 代码 · 共 1,120 行 · 第 1/3 页
JAVA
1,120 行
// HttpManager.java// $Id: HttpManager.java,v 1.84 2004/02/18 12:25:11 ylafon Exp $// (c) COPYRIGHT MIT and INRIA, 1996.// Please first read the full copyright statement in file COPYRIGHT.htmlpackage org.w3c.www.protocol.http ;import java.util.Enumeration;import java.util.Hashtable;import java.util.Properties;import java.net.URL;import java.io.InputStream;import java.io.PrintStream;import org.w3c.www.mime.MimeHeaderHolder;import org.w3c.www.mime.MimeParser;import org.w3c.www.mime.MimeParserFactory;import org.w3c.util.LRUList;import org.w3c.util.ObservableProperties;import org.w3c.util.PropertyMonitoring;import org.w3c.util.SyncLRUList;class ManagerDescription { HttpManager manager = null; Properties properties = null; final HttpManager getManager() { return manager; } final boolean sameProperties(Properties props) { if (props.size() != properties.size()) return false; Enumeration enum = props.propertyNames(); while (enum.hasMoreElements()) { String name = (String) enum.nextElement(); String prop = properties.getProperty(name); if ((prop == null) || (! prop.equals(props.getProperty(name)))) return false; } return true; } ManagerDescription(HttpManager manager, Properties props) { this.manager = manager; this.properties = (Properties)props.clone(); }}class ReplyFactory implements MimeParserFactory { public MimeHeaderHolder createHeaderHolder(MimeParser parser) { return new Reply(parser); }}/** * The client side HTTP request manager. * This class is the user interface (along with the other public classes of * this package) for the W3C client side library implementing HTTP. * A typicall request is launched though the following sequence: * <pre> * HttpManager manager = HttpManager.getManager() ; * Request request = manager.createRequest() ; * request.setMethod(HTTP.GET) ; * request.setURL(new URL("http://www.w3.org/pub/WWW/")); * Reply reply = manager.runRequest(request) ; * // Get the reply input stream that contains the actual data: * InputStream in = reply.getInputStream() ; * ... * </pre> */public class HttpManager implements PropertyMonitoring { private static final boolean debug = false; private static final String DEFAULT_SERVER_CLASS = "org.w3c.www.protocol.http.HttpBasicServer"; /** * The name of the property indicating the class of HttpServer to use. */ public static final String SERVER_CLASS_P = "org.w3c.www.protocol.http.server"; /** * The name of the property containing the ProprequestFilter to launch. */ public static final String FILTERS_PROP_P = "org.w3c.www.protocol.http.filters"; /** * The maximum number of simultaneous connectionlrus. */ public static final String CONN_MAX_P = "org.w3c.www.protocol.http.connections.max"; /** * The SO_TIMEOUT of the client socket. */ public static final String TIMEOUT_P = "org.w3c.www.protocol.http.connections.timeout"; /** * The connection timeout of the client socket. */ public static final String CONN_TIMEOUT_P ="org.w3c.www.protocol.http.connections.connTimeout"; /** * Header properties - The allowed drift for getting cached resources. */ public static final String MAX_STALE_P = "org.w3c.www.protocol.http.cacheControl.maxStale"; /** * Header properties - The minium freshness required on cached resources. */ public static final String MIN_FRESH_P = "org.w3c.www.protocol.http.cacheControl.minFresh"; /** * Header properties - Set the only if cached flag on requests. */ public static final String ONLY_IF_CACHED_P= "org.w3c.www.protocol.http.cacheControl.onlyIfCached"; /** * Header properties - Set the user agent. */ public static final String USER_AGENT_P = "org.w3c.www.protocol.http.userAgent"; /** * Header properties - Set the accept header. */ public static final String ACCEPT_P = "org.w3c.www.protocol.http.accept"; /** * Header properties - Set the accept language. */ public static final String ACCEPT_LANGUAGE_P = "org.w3c.www.protocol.http.acceptLanguage"; /** * Header properties - Set the accept encodings. */ public static final String ACCEPT_ENCODING_P = "org.w3c.www.protocol.http.acceptEncoding"; /** * Header properties - are we parsing answers in a lenient way? */ public static final String LENIENT_P = "org.w3c.www.protocol.http.lenient"; /** * Header properties - should we reuse a connection for POST? */ public static final String KEEPBODY_P = "org.w3c.www.protocol.http.keepbody"; /** * Header properties - Should we use a proxy ? */ public static final String PROXY_SET_P = "proxySet"; /** * Header properties - What is the proxy host name. */ public static final String PROXY_HOST_P = "proxyHost"; /** * Header properties - What is the proxy port number. */ public static final String PROXY_PORT_P = "proxyPort"; /** * The default value for the <code>Accept</code> header. */ public static final String DEFAULT_ACCEPT = "*/*"; /** * The default value for the <code>User-Agent</code> header. */ public static final String DEFAULT_USER_AGENT = "Jigsaw/2.2.4"; /** * This array keeps track of all the created managers. * A new manager (kind of HTTP client side context) is created for each * diffferent set of properties. */ private static ManagerDescription managers[] = new ManagerDescription[4]; /** * The class to instantiate to create new HttpServer instances. */ protected Class serverclass = null; /** * The properties we initialized from. */ ObservableProperties props = null; /** * The server this manager knows about, indexed by FQDN of target servers. */ protected Hashtable servers = null; /** * The template request (the request we will clone to create new requests) */ protected Request template = null ; /** * The LRU list of connections. */ protected LRUList connectionsLru = null; /** * The filter engine attached to this manager. */ FilterEngine filteng = null; protected int timeout = 300000; protected int conn_timeout = 1000; protected int conn_count = 0; protected int conn_max = 5; protected boolean lenient = true; protected boolean keepbody = false; protected Hashtable _tmp_servers = null; // synced during creation /** * Update the proxy configuration to match current properties setting. * @return A boolean, <strong>true</strong> if change was done, * <strong>false</strong> otherwise. */ protected boolean updateProxy() { boolean set = props.getBoolean(PROXY_SET_P, false); if ( set ) { // Wow using a proxy now ! String host = props.getString(PROXY_HOST_P, null); int port = props.getInteger(PROXY_PORT_P, -1); URL proxy = null; try { proxy = new URL("http", host, port, "/"); } catch (Exception ex) { return false; } // Now if a proxy... if (( proxy != null ) && (proxy.getHost() != null)) template.setProxy(proxy); } else { template.setProxy(null); } return true; } /** * Get this manager properties. * @return An ObservableProperties instance. */ public final ObservableProperties getProperties() { return props; } /** * PropertyMonitoring implementation - Update properties on the fly ! * @param name The name of the property that has changed. * @return A boolean, <strong>true</strong> if change is accepted, * <strong>false</strong> otherwise. */ public boolean propertyChanged(String name) { Request tpl = template; if ( name.equals(FILTERS_PROP_P) ) { // FIXME return true; // return false; } else if ( name.equals(TIMEOUT_P) ) { setTimeout(props.getInteger(TIMEOUT_P, timeout)); return true; } else if ( name.equals(CONN_TIMEOUT_P) ) { setConnTimeout(props.getInteger(CONN_TIMEOUT_P, conn_timeout)); return true; } else if ( name.equals(CONN_MAX_P) ) { setMaxConnections(props.getInteger(CONN_MAX_P, conn_max)); return true; } else if ( name.equals(MAX_STALE_P) ) { int ival = props.getInteger(MAX_STALE_P, -1); if ( ival >= 0 ) tpl.setMaxStale(ival); return true; } else if ( name.equals(MIN_FRESH_P) ) { int ival = props.getInteger(MIN_FRESH_P, -1); if ( ival >= 0 ) tpl.setMinFresh(ival); return true; } else if ( name.equals(LENIENT_P) ) { lenient = props.getBoolean(LENIENT_P, lenient); return true; } else if ( name.equals(KEEPBODY_P) ) { keepbody = props.getBoolean(KEEPBODY_P, keepbody); return true; } if ( name.equals(ONLY_IF_CACHED_P) ) { tpl.setOnlyIfCached(props.getBoolean(ONLY_IF_CACHED_P, false)); return true; } else if ( name.equals(USER_AGENT_P) ) { tpl.setValue("user-agent" , props.getString(USER_AGENT_P , DEFAULT_USER_AGENT)); return true; } else if ( name.equals(ACCEPT_P) ) { tpl.setValue("accept" , props.getString(ACCEPT_P, DEFAULT_ACCEPT)); return true; } else if ( name.equals(ACCEPT_LANGUAGE_P) ) { String sval = props.getString(ACCEPT_LANGUAGE_P, null); if ( sval != null ) tpl.setValue("accept-language", sval); return true; } else if ( name.equals(ACCEPT_ENCODING_P) ) { String sval = props.getString(ACCEPT_ENCODING_P, null); if ( sval != null ) tpl.setValue("accept-encoding", sval); return true; } else if ( name.equals(PROXY_SET_P) || name.equals(PROXY_HOST_P) || name.equals(PROXY_PORT_P) ) { return updateProxy(); } else { return true; } } /** * Allow the manager to interact with the user if needed. * This will, for example, allow prompting for paswords, etc. * @param onoff Turn interaction on or off. */ public void setAllowUserInteraction(boolean onoff) { template.setAllowUserInteraction(onoff); } protected static synchronized HttpManager getManager(Class managerclass, Properties p) { // Does such a manager exists already ? for (int i = 0 ; i < managers.length ; i++) { if ( managers[i] == null ) continue; if ( managers[i].sameProperties(p) ) return managers[i].getManager(); } // Get the props we will initialize from: ObservableProperties props = null; if ( p instanceof ObservableProperties ) props = (ObservableProperties) p; else props = new ObservableProperties(p); // Create a new manager for this set of properties: HttpManager manager = null;; try { Object o = managerclass.newInstance(); if (o instanceof HttpManager) { manager = (HttpManager) o; } else { // default value manager = new HttpManager(); } } catch (Exception ex) { ex.printStackTrace(); manager = new HttpManager(); } manager.props = props; // Initialize this new manager filters: String filters[] = props.getStringArray(FILTERS_PROP_P, null); if ( filters != null ) { for (int i = 0 ; i < filters.length ; i++) { try {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?