ProtocolName.cs 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Tls
  7. {
  8. /// <summary>RFC 7301 Represents a protocol name for use with ALPN.</summary>
  9. public sealed class ProtocolName
  10. {
  11. public static ProtocolName AsRawBytes(byte[] bytes)
  12. {
  13. return new ProtocolName(Arrays.Clone(bytes));
  14. }
  15. public static ProtocolName AsUtf8Encoding(String name)
  16. {
  17. return new ProtocolName(Strings.ToUtf8ByteArray(name));
  18. }
  19. public static readonly ProtocolName Http_1_1 = AsUtf8Encoding("http/1.1");
  20. public static readonly ProtocolName Spdy_1 = AsUtf8Encoding("spdy/1");
  21. public static readonly ProtocolName Spdy_2 = AsUtf8Encoding("spdy/2");
  22. public static readonly ProtocolName Spdy_3 = AsUtf8Encoding("spdy/3");
  23. public static readonly ProtocolName Stun_Turn = AsUtf8Encoding("stun.turn");
  24. public static readonly ProtocolName Stun_Nat_Discovery = AsUtf8Encoding("stun.nat-discovery");
  25. public static readonly ProtocolName Http_2_Tls = AsUtf8Encoding("h2");
  26. public static readonly ProtocolName Http_2_Tcp = AsUtf8Encoding("h2c");
  27. public static readonly ProtocolName WebRtc = AsUtf8Encoding("webrtc");
  28. public static readonly ProtocolName WebRtc_Confidential = AsUtf8Encoding("c-webrtc");
  29. public static readonly ProtocolName Ftp = AsUtf8Encoding("ftp");
  30. public static readonly ProtocolName Imap = AsUtf8Encoding("imap");
  31. public static readonly ProtocolName Pop3 = AsUtf8Encoding("pop3");
  32. public static readonly ProtocolName ManageSieve = AsUtf8Encoding("managesieve");
  33. public static readonly ProtocolName Coap = AsUtf8Encoding("coap");
  34. public static readonly ProtocolName Xmpp_Client = AsUtf8Encoding("xmpp-client");
  35. public static readonly ProtocolName Xmpp_Server = AsUtf8Encoding("xmpp-server");
  36. private readonly byte[] m_bytes;
  37. private ProtocolName(byte[] bytes)
  38. {
  39. if (bytes == null)
  40. throw new ArgumentNullException("bytes");
  41. if (bytes.Length < 1 || bytes.Length > 255)
  42. throw new ArgumentException("must have length from 1 to 255", "bytes");
  43. this.m_bytes = bytes;
  44. }
  45. public byte[] GetBytes()
  46. {
  47. return Arrays.Clone(m_bytes);
  48. }
  49. public string GetUtf8Decoding()
  50. {
  51. return Strings.FromUtf8ByteArray(m_bytes);
  52. }
  53. /// <summary>Encode this <see cref="ProtocolName"/> to a <see cref="Stream"/>.</summary>
  54. /// <param name="output">the <see cref="Stream"/> to encode to.</param>
  55. /// <exception cref="IOException"/>
  56. public void Encode(Stream output)
  57. {
  58. TlsUtilities.WriteOpaque8(m_bytes, output);
  59. }
  60. /// <summary>Parse a <see cref="ProtocolName"/> from a <see cref="Stream"/>.</summary>
  61. /// <param name="input">the <see cref="Stream"/> to parse from.</param>
  62. /// <returns>a <see cref="ProtocolName"/> object.</returns>
  63. /// <exception cref="IOException"/>
  64. public static ProtocolName Parse(Stream input)
  65. {
  66. return new ProtocolName(TlsUtilities.ReadOpaque8(input, 1));
  67. }
  68. public override bool Equals(object obj)
  69. {
  70. return obj is ProtocolName && Arrays.AreEqual(m_bytes, ((ProtocolName)obj).m_bytes);
  71. }
  72. public override int GetHashCode()
  73. {
  74. return Arrays.GetHashCode(m_bytes);
  75. }
  76. }
  77. }
  78. #pragma warning restore
  79. #endif