HTTPProtocolFactory.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using System;
  2. using System.IO;
  3. namespace Best.HTTP.Hosts.Connections
  4. {
  5. public enum SupportedProtocols
  6. {
  7. Unknown,
  8. HTTP,
  9. WebSocket,
  10. ServerSentEvents
  11. }
  12. public static class HTTPProtocolFactory
  13. {
  14. public const string W3C_HTTP1 = "http/1.1";
  15. #if (!UNITY_WEBGL || UNITY_EDITOR) && !BESTHTTP_DISABLE_ALTERNATE_SSL
  16. public const string W3C_HTTP2 = "h2";
  17. #endif
  18. public static SupportedProtocols GetProtocolFromUri(Uri uri)
  19. {
  20. if (uri == null || uri.Scheme == null)
  21. throw new Exception("Malformed URI in GetProtocolFromUri");
  22. string scheme = uri.Scheme.ToLowerInvariant();
  23. switch (scheme)
  24. {
  25. case "ws":
  26. case "wss":
  27. return SupportedProtocols.WebSocket;
  28. default:
  29. return SupportedProtocols.HTTP;
  30. }
  31. }
  32. public static bool IsSecureProtocol(Uri uri)
  33. {
  34. if (uri == null || uri.Scheme == null)
  35. throw new Exception("Malformed URI in IsSecureProtocol");
  36. string scheme = uri.Scheme.ToLowerInvariant();
  37. switch (scheme)
  38. {
  39. // http
  40. case "https":
  41. // WebSocket
  42. case "wss":
  43. return true;
  44. }
  45. return false;
  46. }
  47. }
  48. }