gt: 			if(split[1] != null) {
context: /* This code is part of Freenet. It is distributed under the GNU General
   * Public License, version 2 (or at your option any later version). See
   * http://www.gnu.org/ for further details of the GPL. */
  package freenet.client.filter;
  
  import java.io.UnsupportedEncodingException;
  import java.net.MalformedURLException;
  import java.net.URI;
  import java.net.URISyntaxException;
  import java.net.URLEncoder;
  import java.nio.charset.Charset;
  import java.util.HashSet;
  
  import freenet.client.filter.HTMLFilter.ParsedTag;
  import freenet.clients.http.HTTPRequestImpl;
  import freenet.clients.http.StaticToadlet;
  import freenet.keys.FreenetURI;
  import freenet.l10n.NodeL10n;
  import freenet.support.HTMLEncoder;
  import freenet.support.LogThresholdCallback;
  import freenet.support.Logger;
  import freenet.support.URIPreEncoder;
  import freenet.support.URLDecoder;
  import freenet.support.URLEncodedFormatException;
  import freenet.support.Logger.LogLevel;
  import freenet.support.api.HTTPRequest;
  
  public class GenericReadFilterCallback implements FilterCallback, URIProcessor {
  	public static final String magicHTTPEscapeString = "_CHECKED_HTTP_";
  	public static final HashSet<String> allowedProtocols;
  	
  	static {
  		allowedProtocols = new HashSet<String>();
  		allowedProtocols.add("http");
  		allowedProtocols.add("https");
  		allowedProtocols.add("ftp");
  		allowedProtocols.add("mailto");
  		allowedProtocols.add("nntp");
  		allowedProtocols.add("news");
  		allowedProtocols.add("snews");
  		allowedProtocols.add("about");
  		allowedProtocols.add("irc");
  		// file:// ?
  	}
  
  	private URI baseURI;
  	private URI strippedBaseURI;
  	private final FoundURICallback cb;
  	private final TagReplacerCallback trc;
  
          private static volatile boolean logMINOR;
  	static {
  		Logger.registerLogThresholdCallback(new LogThresholdCallback(){
  			@Override
  			public void shouldUpdate(){
  				logMINOR = Logger.shouldLog(LogLevel.MINOR, this);
  			}
  		});
  	}
  
  	public GenericReadFilterCallback(URI uri, FoundURICallback cb,TagReplacerCallback trc) {
  		this.baseURI = uri;
  		this.cb = cb;
  		this.trc=trc;
  		setStrippedURI(uri.toString());
  	}
  	
  	public GenericReadFilterCallback(FreenetURI uri, FoundURICallback cb,TagReplacerCallback trc) {
  		try {
  			this.baseURI = uri.toRelativeURI();
  			setStrippedURI(baseURI.toString());
  			this.cb = cb;
  			this.trc=trc;
  		} catch (URISyntaxException e) {
  			throw new Error(e);
  		}
  	}
  
  	private void setStrippedURI(String u) {
  		int idx = u.lastIndexOf('/');
  		if(idx > 0) {
  			u = u.substring(0, idx+1);
  			try {
  				strippedBaseURI = new URI(u);
  			} catch (URISyntaxException e) {
  				Logger.error(this, "Can't strip base URI: "+e+" parsing "+u);
  				strippedBaseURI = baseURI;
  			}
  		} else
  			strippedBaseURI = baseURI;
  	}
  
  	public String processURI(String u, String overrideType) throws CommentException {
  		return processURI(u, overrideType, false, false);
  	}
  	
  	// RFC3986
  	//  unreserved    = ALPHA / DIGIT / "-" / "." / "_" / "~"
  	protected static final String UNRESERVED = "[a-zA-Z0-9\\-\\._~]";
  	//  pct-encoded   = "%" HEXDIG HEXDIG
  	protected static final String PCT_ENCODED = "%[0-9A-Fa-f][0-9A-Fa-f]";
  	//  sub-delims    = "!" / "$" / "&" / "'" / "(" / ")"
  	//                / "*" / "+" / "," / ";" / "="
  	protected static final String SUB_DELIMS  = "[\\!\\$&'\\(\\)\\*\\+,;=]";
  	//  pchar         = unreserved / pct-encoded / sub-delims / ":" / "@"
  	protected static final String PCHAR      = "(" + UNRESERVED + "|" + PCT_ENCODED + "|" + SUB_DELIMS + "|[:@])";
  	//  fragment      = *( pchar / "/" / "?" )
  	protected static final String FRAGMENT   = "(" + PCHAR + "|\\/|\\?)*";
  
  	public String processURI(String u, String overrideType, boolean noRelative, boolean inline) throws CommentException {
  		if(u.matches("^#" + FRAGMENT + "$")) {
  			// Hack for anchors, see #710
  			return u;
  		}
  		
  		// evil hack, see #2451 and r24565,r24566
  		u = u.replaceAll(" #", " %23");
  		
  		URI uri;
  		URI resolved;
  		try {
  			if(logMINOR) Logger.minor(this, "Processing "+u);
  			uri = URIPreEncoder.encodeURI(u).normalize();
  			if(logMINOR) Logger.minor(this, "Processing "+uri);
  			if(u.startsWith("/") || u.startsWith("%2f"))
  				// Don't bother with relative URIs if it's obviously absolute.
  				// Don't allow encoded /'s, they're just too confusing (here they would get decoded and then coalesced with other slashes).
  				noRelative = true;
  			if(!noRelative)
  				resolved = baseURI.resolve(uri);
  			else
  				resolved = uri;
  			if(logMINOR) Logger.minor(this, "Resolved: "+resolved);
  		} catch (URISyntaxException e1) {
  			if(logMINOR) Logger.minor(this, "Failed to parse URI: "+e1);
  			throw new CommentException(l10n("couldNotParseURIWithError", "error", e1.getMessage()));
  		}
  		String path = uri.getPath();
  		
  		HTTPRequest req = new HTTPRequestImpl(uri, "GET");
  		if (path != null){
  			if(path.equals("/") && req.isParameterSet("newbookmark")){
  				// allow links to the root to add bookmarks
  				String bookmark_key = req.getParam("newbookmark");
  				String bookmark_desc = req.getParam("desc");
  				String bookmark_activelink = req.getParam("hasAnActivelink", "");
  
  				bookmark_key = HTMLEncoder.encode(bookmark_key);
  				bookmark_desc = HTMLEncoder.encode(bookmark_desc);
  
  				String url = "/?newbookmark="+bookmark_key+"&desc="+bookmark_desc;
  				if (!bookmark_activelink.equals("")) {
  					bookmark_activelink = HTMLEncoder.encode(bookmark_activelink);
  					url = url+"&hasAnActivelink=true";
  				}
  				return url;
  			} else if(path.startsWith(StaticToadlet.ROOT_URL)) {
  				// @see bug #2297
  				return path;
  			}
  		}
  		
  		String reason = "";
  		
  		// Try as an absolute URI
  		
  		URI origURI = uri;
  		
  		// Convert localhost uri's to relative internal ones.
  		
  		String host = uri.getHost();
  		if(host != null && (host.equals("localhost") || host.equals("127.0.0.1")) && uri.getPort() == 8888) {
  			try {
  				uri = new URI(null, null, null, -1, uri.getPath(), uri.getQuery(), uri.getFragment());
  			} catch (URISyntaxException e) {
  				Logger.error(this, "URI "+uri+" looked like localhost but could not parse", e);
  				throw new CommentException("URI looked like localhost but could not parse: "+e);
  			}
  			host = null;
  		}
  		
  		String rpath = uri.getPath();
  		if(logMINOR) Logger.minor(this, "Path: \""+path+"\" rpath: \""+rpath+"\"");
  		
  		if(host == null) {
  		
  			boolean isAbsolute = false;
  			
  			if(rpath != null) {
  				if(logMINOR) Logger.minor(this, "Resolved URI (rpath absolute): \""+rpath+"\"");
  				
  				// Valid FreenetURI?
  				try {
  					String p = rpath;
  					while(p.startsWith("/")) {
  						p = p.substring(1);
  					}
  					FreenetURI furi = new FreenetURI(p, true);
  					isAbsolute = true;
  					if(logMINOR) Logger.minor(this, "Parsed: "+furi);
  					return processURI(furi, uri, overrideType, true, inline);
  				} catch (MalformedURLException e) {
  					// Not a FreenetURI
  					if(logMINOR) Logger.minor(this, "Malformed URL (a): "+e, e);
  					if(e.getMessage() != null) {
  						reason = l10n("malformedAbsoluteURL", "error", e.getMessage());
  					} else {
  						reason = l10n("couldNotParseAbsoluteFreenetURI");
  					}
  				}
  			}
  			
  			if(!isAbsolute) {
  				
  				// Relative URI
  				
  				rpath = resolved.getPath();
  				if(rpath == null) throw new CommentException("No URI");
  				if(logMINOR) Logger.minor(this, "Resolved URI (rpath relative): "+rpath);
  				
  				// Valid FreenetURI?
  				try {
  					String p = rpath;
  					while(p.startsWith("/")) p = p.substring(1);
  					FreenetURI furi = new FreenetURI(p, true);
  					if(logMINOR) Logger.minor(this, "Parsed: "+furi);
  					return processURI(furi, uri, overrideType, noRelative, inline);
  				} catch (MalformedURLException e) {
  					if(logMINOR) Logger.minor(this, "Malformed URL (b): "+e, e);
  					if(e.getMessage() != null) {
  						reason = l10n("malformedRelativeURL", "error", e.getMessage());
  					} else {
  						reason = l10n("couldNotParseRelativeFreenetURI");
  					}
  				}
  				
  			}
  		
  		}
  		
  		uri = origURI;
  		
  		if(GenericReadFilterCallback.allowedProtocols.contains(uri.getScheme()))
  			return "/?"+GenericReadFilterCallback.magicHTTPEscapeString+ '=' +uri;
  		else {
  			if(uri.getScheme() == null) {
  				throw new CommentException(reason);
  			}
  			throw new CommentException(l10n("protocolNotEscaped", "protocol", uri.getScheme()));
  		}
  	}
  	
  	public String makeURIAbsolute(String uri) throws URISyntaxException{
  		return baseURI.resolve(URIPreEncoder.encodeURI(uri).normalize()).toASCIIString();
  	}
  
  	private static String l10n(String key, String pattern, String value) {
  		return NodeL10n.getBase().getString("GenericReadFilterCallback."+key, pattern, value);
  	}
  
  	private static String l10n(String key) {
  		return NodeL10n.getBase().getString("GenericReadFilterCallback."+key);
  	}
  
  	private String finishProcess(HTTPRequest req, String overrideType, String path, URI u, boolean noRelative) {
  		String typeOverride = req.getParam("type", null);
  		if(overrideType != null)
  			typeOverride = overrideType;
  
  		if(typeOverride != null) {
  			String[] split = HTMLFilter.splitType(typeOverride);
  