HTTPProtocolFactory.cs 2.2 KB

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