AccessInfo.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. namespace IFramework.Net.WebSocket
  2. {
  3. internal class AccessInfo : BaseInfo
  4. {
  5. /// <summary>
  6. /// 连接主机
  7. /// </summary>
  8. public string Host { get; set; }
  9. /// <summary>
  10. /// 连接源
  11. /// </summary>
  12. public string Origin { get; set; }
  13. /// <summary>
  14. /// 安全扩展
  15. /// </summary>
  16. public string SecWebSocketExtensions { get; set; }
  17. /// <summary>
  18. /// 安全密钥
  19. /// </summary>
  20. public string SecWebSocketKey { get; set; }
  21. /// <summary>
  22. /// 安全版本
  23. /// </summary>
  24. public string SecWebSocketVersion { get; set; }
  25. public override string ToString()
  26. {
  27. if (string.IsNullOrEmpty(HttpProto))
  28. HttpProto = "GET / HTTP/1.1";
  29. if (string.IsNullOrEmpty(SecWebSocketVersion))
  30. SecWebSocketVersion = "13";
  31. return string.Format("{0}{1}{2}{3}{4}{5}{6}",
  32. HttpProto + NewLine,
  33. "Host" + SplitChars + Host + NewLine,
  34. "Connection" + SplitChars + Connection + NewLine,
  35. "Upgrade" + SplitChars + Upgrade + NewLine,
  36. "Origin" + SplitChars + Origin + NewLine,
  37. "Sec-WebSocket-Version" + SplitChars + SecWebSocketVersion + NewLine,
  38. "Sec-WebSocket-Key" + SplitChars + SecWebSocketKey + NewLine + NewLine);
  39. }
  40. public bool IsHandShaked()
  41. {
  42. return string.IsNullOrEmpty(SecWebSocketKey) == false;
  43. }
  44. }
  45. internal class BaseInfo
  46. {
  47. /// <summary>
  48. /// http连接协议
  49. /// </summary>
  50. public string HttpProto { get; set; }
  51. /// <summary>
  52. /// 连接类型
  53. /// </summary>
  54. public string Connection { get; set; }
  55. /// <summary>
  56. /// 连接方式
  57. /// </summary>
  58. public string Upgrade { get; set; }
  59. internal const string NewLine = "\r\n";
  60. internal const string SplitChars = ": ";
  61. public BaseInfo()
  62. {
  63. Upgrade = "websocket";
  64. Connection = "Upgrade";
  65. }
  66. }
  67. }