HTTPProtocolFactory.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System;
  2. using System.IO;
  3. namespace BestHTTP.Connections
  4. {
  5. public enum SupportedProtocols
  6. {
  7. Unknown,
  8. HTTP,
  9. #if !BESTHTTP_DISABLE_WEBSOCKET
  10. WebSocket,
  11. #endif
  12. #if !BESTHTTP_DISABLE_SERVERSENT_EVENTS
  13. ServerSentEvents
  14. #endif
  15. }
  16. public static class HTTPProtocolFactory
  17. {
  18. public const string W3C_HTTP1 = "http/1.1";
  19. #if (!UNITY_WEBGL || UNITY_EDITOR) && !BESTHTTP_DISABLE_ALTERNATE_SSL && !BESTHTTP_DISABLE_HTTP2
  20. public const string W3C_HTTP2 = "h2";
  21. #endif
  22. public static HTTPResponse Get(SupportedProtocols protocol, HTTPRequest request, Stream stream, bool isStreamed, bool isFromCache)
  23. {
  24. switch (protocol)
  25. {
  26. #if !BESTHTTP_DISABLE_WEBSOCKET && (!UNITY_WEBGL || UNITY_EDITOR)
  27. case SupportedProtocols.WebSocket: return new WebSocket.WebSocketResponse(request, stream, isStreamed, isFromCache);
  28. #endif
  29. default: return new HTTPResponse(request, stream, isStreamed, isFromCache);
  30. }
  31. }
  32. public static SupportedProtocols GetProtocolFromUri(Uri uri)
  33. {
  34. if (uri == null || uri.Scheme == null)
  35. throw new Exception("Malformed URI in GetProtocolFromUri");
  36. string scheme = uri.Scheme.ToLowerInvariant();
  37. switch (scheme)
  38. {
  39. #if !BESTHTTP_DISABLE_WEBSOCKET
  40. case "ws":
  41. case "wss":
  42. return SupportedProtocols.WebSocket;
  43. #endif
  44. default:
  45. return SupportedProtocols.HTTP;
  46. }
  47. }
  48. public static bool IsSecureProtocol(Uri uri)
  49. {
  50. if (uri == null || uri.Scheme == null)
  51. throw new Exception("Malformed URI in IsSecureProtocol");
  52. string scheme = uri.Scheme.ToLowerInvariant();
  53. switch (scheme)
  54. {
  55. // http
  56. case "https":
  57. #if !BESTHTTP_DISABLE_WEBSOCKET
  58. // WebSocket
  59. case "wss":
  60. #endif
  61. return true;
  62. }
  63. return false;
  64. }
  65. }
  66. }