123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- using System;
- using System.Text;
- namespace prBrowser
- {
- internal static class URLUtils
- {
- private class UrlDecoder
- {
- private int _bufferSize;
- private byte[] _byteBuffer;
- private char[] _charBuffer;
- private Encoding _encoding;
- private int _numBytes;
- private int _numChars;
- public bool forFilePaths = false;
- internal UrlDecoder(int bufferSize, Encoding encoding)
- {
- _bufferSize = bufferSize;
- _encoding = encoding;
- _charBuffer = new char[bufferSize];
- }
- internal void AddByte(byte b)
- {
- if (_byteBuffer == null)
- {
- _byteBuffer = new byte[_bufferSize];
- }
- _byteBuffer[_numBytes++] = b;
- }
- internal void AddChar(char ch, bool checkChar = false)
- {
- if (_numBytes > 0)
- {
- FlushBytes();
- }
- if (checkChar && forFilePaths && !ch.SupportedInFilePath())
- {
- AddChar('_');
- int num = ch;
- AddString("0x" + num.ToString("X"));
- AddChar('_');
- }
- else
- {
- _charBuffer[_numChars++] = ch;
- }
- }
- internal void AddString(string str)
- {
- if (_numBytes > 0)
- {
- FlushBytes();
- }
- foreach (char ch in str)
- {
- _charBuffer[_numChars++] = ch;
- }
- }
- public void FlushBytes(bool checkChar = false)
- {
- if (_numBytes <= 0)
- {
- return;
- }
- if (checkChar && forFilePaths)
- {
- char[] newChars = _encoding.GetChars(_byteBuffer, 0, _numBytes);
- _numBytes = 0;
- char[] array = newChars;
- foreach (char ch in array)
- {
- AddChar(ch);
- }
- }
- else
- {
- _numChars += _encoding.GetChars(_byteBuffer, 0, _numBytes, _charBuffer, _numChars);
- _numBytes = 0;
- }
- }
- internal string GetString()
- {
- if (_numBytes > 0)
- {
- FlushBytes();
- }
- if (_numChars > 0)
- {
- return new string(_charBuffer, 0, _numChars);
- }
- return string.Empty;
- }
- }
- public static string PathToURL(this string filePath, string removeBaseDir = null)
- {
- if (!filePath.CheckIfValid())
- {
- return "";
- }
- return "file:///" + filePath.Replace("\\", "/");
- }
- public static bool IsURLOfflineFile(this string url)
- {
- return url.StartsWith("file://", StringComparison.Ordinal);
- }
- public static bool IsURLLocalhost(this string url)
- {
- return url.BeginsWith("http://localhost") || url.BeginsWith("localhost");
- }
- public static string DecodeURL(this string url)
- {
- if (url == null)
- {
- return null;
- }
- int length = url.Length;
- UrlDecoder decoder = new UrlDecoder(length, Encoding.UTF8);
- for (int i = 0; i < length; i++)
- {
- char ch = url[i];
- if (ch == '+')
- {
- ch = ' ';
- }
- else if (ch == '%' && i < length - 2)
- {
- if (url[i + 1] == 'u' && i < length - 5)
- {
- int num3 = url[i + 2].HexToInt();
- int num4 = url[i + 3].HexToInt();
- int num5 = url[i + 4].HexToInt();
- int num6 = url[i + 5].HexToInt();
- if (num3 >= 0 && num4 >= 0 && num5 >= 0 && num6 >= 0)
- {
- ch = (char)((num3 << 12) | (num4 << 8) | (num5 << 4) | num6);
- i += 5;
- decoder.AddChar(ch);
- continue;
- }
- }
- else
- {
- int num7 = url[i + 1].HexToInt();
- int num8 = url[i + 2].HexToInt();
- if (num7 >= 0 && num8 >= 0)
- {
- byte b = (byte)((num7 << 4) | num8);
- i += 2;
- decoder.AddByte(b);
- continue;
- }
- }
- }
- if ((ch & 0xFF80) == 0)
- {
- decoder.AddByte((byte)ch);
- }
- else
- {
- decoder.AddChar(ch);
- }
- }
- return decoder.GetString();
- }
- public static int HexToInt(this char hex)
- {
- if (hex >= '0' && hex <= '9')
- {
- return hex - 48;
- }
- if (hex >= 'a' && hex <= 'f')
- {
- return hex - 97 + 10;
- }
- if (hex >= 'A' && hex <= 'F')
- {
- return hex - 65 + 10;
- }
- return -1;
- }
- public static string DecodeURLForFilepath(this string url)
- {
- if (url == null)
- {
- return null;
- }
- int length = url.Length;
- UrlDecoder decoder = new UrlDecoder(length * 10, Encoding.UTF8);
- decoder.forFilePaths = true;
- for (int i = 0; i < length; i++)
- {
- char ch = url[i];
- if (ch == '+')
- {
- ch = ' ';
- }
- else if (ch == '%' && i < length - 2)
- {
- if (url[i + 1] == 'u' && i < length - 5)
- {
- int num3 = url[i + 2].HexToInt();
- int num4 = url[i + 3].HexToInt();
- int num5 = url[i + 4].HexToInt();
- int num6 = url[i + 5].HexToInt();
- if (num3 >= 0 && num4 >= 0 && num5 >= 0 && num6 >= 0)
- {
- ch = (char)((num3 << 12) | (num4 << 8) | (num5 << 4) | num6);
- i += 5;
- decoder.FlushBytes();
- decoder.AddChar(ch, checkChar: true);
- continue;
- }
- }
- else
- {
- int num7 = url[i + 1].HexToInt();
- int num8 = url[i + 2].HexToInt();
- if (num7 >= 0 && num8 >= 0)
- {
- byte b = (byte)((num7 << 4) | num8);
- i += 2;
- decoder.FlushBytes();
- decoder.AddByte(b);
- if (i + 1 < length - 2 && url[i + 1] == '%')
- {
- num7 = url[i + 1].HexToInt();
- num8 = url[i + 2].HexToInt();
- if (num7 >= 0 && num8 >= 0)
- {
- b = (byte)((num7 << 4) | num8);
- i += 2;
- decoder.AddByte(b);
- decoder.FlushBytes(checkChar: true);
- }
- }
- else
- {
- decoder.FlushBytes(checkChar: true);
- }
- continue;
- }
- }
- }
- if ((ch & 0xFF80) == 0)
- {
- decoder.AddByte((byte)ch);
- }
- else
- {
- decoder.AddChar(ch);
- }
- }
- return decoder.GetString();
- }
- public static string EncodeURL(this string text)
- {
- return Uri.EscapeDataString(text);
- }
- }
- }
|