HTTPProtocolFactory.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using System;
  2. using System.IO;
  3. namespace BestHTTP
  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 static HTTPResponse Get(SupportedProtocols protocol, HTTPRequest request, Stream stream, bool isStreamed, bool isFromCache)
  19. {
  20. switch (protocol)
  21. {
  22. #if !BESTHTTP_DISABLE_WEBSOCKET && (!UNITY_WEBGL || UNITY_EDITOR)
  23. case SupportedProtocols.WebSocket: return new WebSocket.WebSocketResponse(request, stream, isStreamed, isFromCache);
  24. #endif
  25. #if !BESTHTTP_DISABLE_SERVERSENT_EVENTS && (!UNITY_WEBGL || UNITY_EDITOR)
  26. case SupportedProtocols.ServerSentEvents: return new ServerSentEvents.EventSourceResponse(request, stream, isStreamed, isFromCache);
  27. #endif
  28. default: return new HTTPResponse(request, new Extensions.ReadOnlyBufferedStream(stream), isStreamed, isFromCache);
  29. }
  30. }
  31. public static SupportedProtocols GetProtocolFromUri(Uri uri)
  32. {
  33. if (uri == null || uri.Scheme == null)
  34. throw new Exception("Malformed URI in GetProtocolFromUri");
  35. string scheme = uri.Scheme.ToLowerInvariant();
  36. switch (scheme)
  37. {
  38. #if !BESTHTTP_DISABLE_WEBSOCKET
  39. case "ws":
  40. case "wss":
  41. return SupportedProtocols.WebSocket;
  42. #endif
  43. default:
  44. return SupportedProtocols.HTTP;
  45. }
  46. }
  47. public static bool IsSecureProtocol(Uri uri)
  48. {
  49. if (uri == null || uri.Scheme == null)
  50. throw new Exception("Malformed URI in IsSecureProtocol");
  51. string scheme = uri.Scheme.ToLowerInvariant();
  52. switch (scheme)
  53. {
  54. // http
  55. case "https":
  56. #if !BESTHTTP_DISABLE_WEBSOCKET
  57. // WebSocket
  58. case "wss":
  59. #endif
  60. return true;
  61. }
  62. return false;
  63. }
  64. }
  65. }