⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 httpbasefilter.java

📁 一款Java实现的HTTP代理服务器
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	boolean mayKeepAlive = true;    	String requestVersion = header.getHTTPVersion ().toUpperCase ();	if (requestVersion.equals ("HTTP/1.1")) {	    String host = header.getHeader ("Host");	    if (host == null) {		Exception exe = 		    new Exception ("No host header set in HTTP/1.1 request");		HttpHeader ret = 		    con.getHttpGenerator ().get400 (exe);		return ret;	    }	    maychunk = true;    	    String closeit = header.getHeader ("Proxy-Connection");	    if (closeit == null)		closeit = header.getHeader ("Connection");	    mayKeepAlive = (closeit == null 			    || !closeit.equalsIgnoreCase ("close"));	} else {	    header.setHTTPVersion ("HTTP/1.1");	    maychunk = false;	    // stupid netscape to not follow the standards, 	    // only "Connection" should be used...	    String keepalive = header.getHeader ("Proxy-Connection");	    mayKeepAlive = (keepalive != null 			    && keepalive.equalsIgnoreCase ("Keep-Alive"));	    if (!mayKeepAlive) {		keepalive = header.getHeader ("Connection");		mayKeepAlive = (keepalive != null 				&& keepalive.equalsIgnoreCase ("Keep-Alive"));	    }		}		boolean useCached = true;	boolean cacheAllowed = true;	// damn how many system that use cookies with id's	/*         System.out.println ("auth: " + header.getHeader ("authorization") +                             ", cookie:" + header.getHeader ("cookie") +                             ", Pragma: " + header.getHeader ("Pragma") +                             ", Cache: " + header.getHeader ("Cache-Control"));	*/	//String cached = header.getHeader ("Pragma");	List<String> ccs = header.getHeaders ("Cache-Control");	for (String cached : ccs) {	    cached = cached.trim ();	    if (cached.equals ("no-store")) {		useCached = false;		cacheAllowed = false;	    } else if (cached.equals ("no-cache")) {		useCached = false;	    } else if (cached.equals ("no-transform")) {		useCached = false;     // cache is transformed.		cacheAllowed = false;  // dont store, no point.		con.setMayFilter (false);	    }	}	ccs = header.getHeaders ("Pragma");	for (String cached : ccs) {	    cached = cached.trim ();	    if (cached.equals ("no-cache")) {		useCached = false;	    }	}		String method = header.getMethod ().trim ();	if (!method.equals ("GET") && !method.equals ("HEAD")) {	    useCached = false;	    cacheAllowed = false;	    //mayKeepAlive = false;	} else if (method.equals ("HEAD")) {	    maychunk = false;	}	con.setChunking (maychunk);	String mf = header.getHeader ("Max-Forwards");	if (mf != null) {	    HttpHeader ret = checkMaxForwards (con, header, mf);	    if (ret != null) {		return ret;	    }	}	String auths = header.getHeader ("authorization");	if (auths != null) {	    useCached = false;     // dont use cached files, 	    cacheAllowed = false;  // and dont cache it.	} else if (cookieId) {	    String cookie = header.getHeader ("cookie");	    String lccookie = null;	    if (cookie != null &&     // cookie-passwords suck.		(((lccookie = cookie.toLowerCase ()).indexOf ("password") >= 0)		 || (lccookie.indexOf ("id") >= 0))) {		useCached = false;     // dont use cached files, 		cacheAllowed = false;  // and dont cache it.	    }	}	con.setMayUseCache (useCached);	con.setMayCache (cacheAllowed);	con.setKeepalive (mayKeepAlive);		String requri = header.getRequestURI ();	if (requri.toLowerCase ().startsWith (NOPROXY)) 	    requri = handleNoProxyRequest (requri, header, con);       			HttpHeader headerr = handleURLSetup (requri, header, con);	if (headerr != null) 	    return headerr;		removeConnectionTokens (header);	int rsize = removes.size ();	for (int i = 0; i < rsize; i++) {	    String r = removes.get (i);	    header.removeHeader (r);	}		HttpProxy proxy = con.getProxy ();	if (proxy.isProxyConnected ()) {	    String auth = proxy.getProxyAuthString ();	    // it should look like this (using RabbIT:RabbIT):	    // Proxy-authorization: Basic UmFiYklUOlJhYmJJVA==	    header.setHeader ("Proxy-authorization", 			      "Basic " + Coder.uuencode (auth));	}	// try to use keepalive backwards.	// This is not needed since it is a HTTP/1.1 request.	// header.setHeader ("Connection", "Keep-Alive");	return null;    }    private boolean checkCacheControl (String cachecontrol) {	String[] caches = cachecontrol.split (",");	for (String cached : caches) {	    cached = cached.trim ();	    if (cached.equals ("no-store"))		return false;	    if (cached.equals ("private")) 		return false;	}	return true;    }    /** test if a socket/header combination is valid or return a new HttpHeader.     * @param socket the SocketChannel that made the request.     * @param header the actual response made.     * @param con the Connection handling the request.     * @return null if everything is fine or a HttpHeader      *         describing the error (like a 403).     */    public HttpHeader doHttpOutFiltering (SocketChannel socket, 					  HttpHeader header, Connection con) {	boolean useCache = true;	//String cached = header.getHeader ("Pragma");	List<String> ccs = header.getHeaders ("Cache-Control");	for (String cached : ccs) {	    if (cached != null) 		useCache &= checkCacheControl (cached);	}		String status = header.getStatusCode ().trim ();	if (!(status.equals ("200") || status.equals ("206") 	      || status.equals ("304"))) {	    con.setKeepalive (false);    	    useCache = false;	}	String age = header.getHeader ("Age");	long secs = 0;	if (age == null)	    age = "0";	try {	    secs = Long.parseLong (age);	} catch (NumberFormatException e) {	    // ignore, we already have a warning for this..	}	if (secs > 60 * 60 * 24) 	    header.setHeader ("Warning", "113 RabbIT \"Heuristic expiration\"");		header.setResponseHTTPVersion ("HTTP/1.1");	con.setMayCache (useCache);	/** Dont filter compressed pages... 	    someone else has already thought about speed... */	String ce = header.getHeader ("Content-Encoding");	if (ce != null &&	    (ce.equalsIgnoreCase ("gzip") 	     || (ce.equalsIgnoreCase ("compress"))))	    con.setMayFilter (false);	/** Try to make sure that IE can handle NTLM authentication. */	/** This does not work. */	/*	List ls = header.getHeaders ("WWW-Authenticate");	for (Iterator i = ls.iterator (); i.hasNext (); ) {	    String s = (String)i.next ();	    if (s.indexOf ("Negotiate") != -1 || 		s.indexOf ("NTLM") != -1) {		con.setMayFilter (false);		con.setChunking (false);	    }	}	*/		removeConnectionTokens (header);	for (String r : removes)	    header.removeHeader (r);		String d = header.getHeader ("Date");	if (d == null) {	    // ok, maybe we should check if there is an Age set	    // otherwise we can do like this.	    header.setHeader ("Date", 			      HttpDateParser.getDateString (new Date ()));	}		String cl = header.getHeader ("Content-Length");	if (cl == null) 	    if (!con.getChunking ())		con.setKeepalive (false);	return null;    }    /** Setup this class with the given properties.     * @param properties the new configuration of this class.     */    public void setup (Logger logger, SProperties properties) {	removes.clear ();	String rs = properties.getProperty ("remove", "");	String[] sts = rs.split (",");	for (String r : sts)	    removes.add (r.trim ());	String userFile = properties.getProperty ("userfile", "conf/users");	userHandler.setFile (userFile, logger);	String cid = properties.getProperty ("cookieid", "false");	cookieId = cid.equals ("true");    }        /** Check if a given url is a public URL of the Proxy.     * @param url the URL to check.     * @return true if this url has public access, false otherwise.     */    public boolean isPublic (URL url) {	String file = url.getFile ();	if (file.startsWith ("/FileSender/public/"))	    return true;	return false;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -