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

📄 urlutils.java

📁 java 文件下载器。可自定义
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   UrlUtils.java

package org.gudy.azureus2.core3.util;

import com.aelitis.azureus.core.util.Java15Utils;
import com.aelitis.net.magneturi.MagnetURIHandler;
import java.io.*;
import java.net.*;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.bouncycastle.util.encoders.Base64;
import org.gudy.azureus2.core3.config.COConfigurationManager;
import org.gudy.azureus2.plugins.utils.resourcedownloader.ResourceDownloader;
import org.gudy.azureus2.plugins.utils.resourceuploader.ResourceUploader;

// Referenced classes of package org.gudy.azureus2.core3.util:
//			AESemaphore, Base32, ByteFormatter, Debug, 
//			ThreadPool, AERunnable

public class UrlUtils
{

	private static final ThreadPool connect_pool;
	private static final String prefixes[] = {
		"http://", "https://", "ftp://", "dht://", "magnet:?", "magnet://?"
	};
	private static int MAGNETURL_STARTS_AT = 3;
	private static final Object XMLescapes[] = {
		new String[] {
			"&", "&"
		}, new String[] {
			">", ">"
		}, new String[] {
			"<", "&lt;"
		}, new String[] {
			"\"", "&quot;"
		}, new String[] {
			"'", "&apos;"
		}
	};
	private static String last_headers = COConfigurationManager.getStringParameter("metasearch.web.last.headers", null);
	private static final String default_headers = "SG9zdDogbG9jYWxob3N0OjQ1MTAwClVzZXItQWdlbnQ6IE1vemlsbGEvNS4wIChXaW5kb3dzOyBVOyBXaW5kb3dzIE5UIDUuMTsgZW4tVVM7IHJ2OjEuOC4xLjE0KSBHZWNrby8yMDA4MDQwNCBGaXJlZm94LzIuMC4wLjE0CkFjY2VwdDogdGV4dC94bWwsYXBwbGljYXRpb24veG1sLGFwcGxpY2F0aW9uL3hodG1sK3htbCx0ZXh0L2h0bWw7cT0wLjksdGV4dC9wbGFpbjtxPTAuOCxpbWFnZS9wbmcsKi8qO3E9MC41CkFjY2VwdC1MYW5ndWFnZTogZW4tdXMsZW47cT0wLjUKQWNjZXB0LUVuY29kaW5nOiBnemlwLGRlZmxhdGUKQWNjZXB0LUNoYXJzZXQ6IElTTy04ODU5LTEsdXRmLTg7cT0wLjcsKjtxPTAuNwpLZWVwLUFsaXZlOiAzMDAKQ29ubmVjdGlvbjoga2VlcC1hbGl2ZQ==";

	public UrlUtils()
	{
	}

	public static boolean isURL(String sURL)
	{
		return parseTextForURL(sURL, true) != null;
	}

	public static boolean isURL(String sURL, boolean bGuess)
	{
		return parseTextForURL(sURL, true, bGuess) != null;
	}

	public static String parseTextForURL(String text, boolean accept_magnets)
	{
		return parseTextForURL(text, accept_magnets, true);
	}

	public static String parseTextForURL(String text, boolean accept_magnets, boolean guess)
	{
		if (text == null || text.length() < 5)
			return null;
		String href = parseHTMLforURL(text);
		if (href != null)
			return href;
		try
		{
			text = text.trim();
			text = URLDecoder.decode(text);
		}
		catch (Exception e) { }
		String textLower;
		try
		{
			textLower = text.toLowerCase();
		}
		catch (Throwable e)
		{
			textLower = text;
		}
		int max = accept_magnets ? prefixes.length : MAGNETURL_STARTS_AT;
		int end = -1;
		int start = textLower.length();
		String strURL = null;
		for (int i = 0; i < max; i++)
		{
			int testBegin = textLower.indexOf(prefixes[i]);
			if (testBegin < 0 || testBegin >= start)
				continue;
			end = text.indexOf("\n", testBegin + prefixes[i].length());
			String strURLTest = end < 0 ? text.substring(testBegin) : text.substring(testBegin, end - 1);
			try
			{
				URL parsedURL = new URL(strURLTest);
				strURL = parsedURL.toExternalForm();
				continue;
			}
			catch (MalformedURLException e1)
			{
				e1.printStackTrace();
			}
			if (i >= MAGNETURL_STARTS_AT)
				strURL = strURLTest;
		}

		if (strURL != null)
			return strURL;
		if ((new File(text)).exists())
			return null;
		if (accept_magnets && text.matches("^[a-fA-F0-9]{40}$"))
		{
			byte infohash[] = ByteFormatter.decodeString(text.toUpperCase());
			return (new StringBuilder()).append("magnet:?xt=urn:btih:").append(Base32.encode(infohash)).toString();
		}
		if (accept_magnets && text.matches("^[a-zA-Z2-7]{32}$"))
			return (new StringBuilder()).append("magnet:?xt=urn:btih:").append(text).toString();
		if (accept_magnets && guess)
		{
			Pattern pattern = Pattern.compile("[^a-zA-Z2-7][a-zA-Z2-7]{32}[^a-zA-Z2-7]");
			Matcher matcher = pattern.matcher(text);
			if (matcher.find())
			{
				String hash = text.substring(matcher.start() + 1, matcher.start() + 33);
				return (new StringBuilder()).append("magnet:?xt=urn:btih:").append(hash).toString();
			}
			pattern = Pattern.compile("[^a-fA-F0-9][a-fA-F0-9]{40}[^a-fA-F0-9]");
			matcher = pattern.matcher(text);
			if (matcher.find())
			{
				String hash = text.substring(matcher.start() + 1, matcher.start() + 41);
				byte infohash[] = ByteFormatter.decodeString(hash.toUpperCase());
				return (new StringBuilder()).append("magnet:?xt=urn:btih:").append(Base32.encode(infohash)).toString();
			}
		}
		return null;
	}

	public static String parseHTMLforURL(String text)
	{
		if (text == null)
			return null;
		Pattern pat = Pattern.compile("<.*a\\s++.*href=\"?([^\\'\"\\s>]++).*", 2);
		Matcher m = pat.matcher(text);
		if (m.find())
		{
			String sURL = m.group(1);
			try
			{
				sURL = URLDecoder.decode(sURL);
			}
			catch (Exception e) { }
			return sURL;
		} else
		{
			return null;
		}
	}

	public static void main(String args[])
	{
		MagnetURIHandler.getSingleton();
		byte infohash[] = ByteFormatter.decodeString("1234567890123456789012345678901234567890");
		String test[] = {
			"http://moo.com", "http%3A%2F/moo%2Ecom", "magnet:?moo", "magnet%3A%3Fxt=urn:btih:26", "magnet%3A//%3Fmooo", (new StringBuilder()).append("magnet:?xt=urn:btih:").append(Base32.encode(infohash)).toString(), "aaaaaaaaaabbbbbbbbbbccccccccccdddddddddd", "magnet:?dn=OpenOffice.org_2.0.3_Win32Intel_install.exe&xt=urn:sha1:PEMIGLKMNFI4HZ4CCHZNPKZJNMAAORKN&xt=urn:tree:tiger:JMIJVWHCQUX47YYH7O4XIBCORNU2KYKHBBC6DHA&xt=urn:ed2k:1c0804541f34b6583a383bb8f2cec682&xl=96793015&xs=http://mirror.switch.ch/ftp/mirror/OpenOffice/stable/2.0.3/OOo_2.0.3_Win32Intel_install.exe"
		};
		for (int i = 0; i < test.length; i++)
		{
			System.out.println((new StringBuilder()).append("decode: ").append(test[i]).append(" -> ").append(URLDecoder.decode(test[i])).toString());
			System.out.println((new StringBuilder()).append("isURL: ").append(test[i]).append(" -> ").append(isURL(test[i])).toString());
			System.out.println((new StringBuilder()).append("parse: ").append(test[i]).append(" -> ").append(parseTextForURL(test[i], true)).toString());
		}

	}

	public static String encode(String s)
	{
		if (s == null)
			return "";
		return URLEncoder.encode(s, "UTF-8").replaceAll("\\+", "%20");
		UnsupportedEncodingException e;
		e;
		return URLEncoder.encode(s).replaceAll("\\+", "%20");
	}

	public static String decode(String s)
	{
		if (s == null)
			return "";
		return URLDecoder.decode(s, "UTF-8");
		UnsupportedEncodingException e;
		e;
		return URLDecoder.decode(s);
	}

	public static String escapeXML(String s)
	{
		if (s == null)
			return "";
		String ret = s;
		for (int i = 0; i < XMLescapes.length; i++)
		{
			String escapeEntry[] = (String[])(String[])XMLescapes[i];
			ret = ret.replaceAll(escapeEntry[0], escapeEntry[1]);
		}

		return ret;
	}

	public static String unescapeXML(String s)
	{
		if (s == null)
			return "";
		String ret = s;
		for (int i = 0; i < XMLescapes.length; i++)
		{
			String escapeEntry[] = (String[])(String[])XMLescapes[i];
			ret = ret.replaceAll(escapeEntry[1], escapeEntry[0]);
		}

		return ret;
	}

	public static String convertIPV6Host(String host)
	{
		if (host.indexOf(':') != -1)
			return (new StringBuilder()).append("[").append(host).append("]").toString();
		else
			return host;
	}

	public static String expandIPV6Host(String host)
	{
		if (host.indexOf(':') == -1)
			break MISSING_BLOCK_LABEL_29;
		return InetAddress.getByAddress(InetAddress.getByName(host).getAddress()).getHostAddress();
		Throwable e;
		e;
		Debug.printStackTrace(e);
		return host;
	}

	public static void connectWithTimeout(URLConnection connection, long connect_timeout)
		throws IOException
	{
		connectWithTimeouts(connection, connect_timeout, -1L);
	}

	public static void connectWithTimeouts(URLConnection connection, long connect_timeout, long read_timeout)
		throws IOException
	{
		if (Java15Utils.isAvailable())

⌨️ 快捷键说明

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