RequestHandshake.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #region MIT License
  2. /**
  3. * RequestHandshake.cs
  4. *
  5. * The MIT License
  6. *
  7. * Copyright (c) 2012 sta.blockhead
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. #endregion
  28. using System;
  29. using System.Collections.Specialized;
  30. using System.Text;
  31. using WebSocketSharp.Net;
  32. namespace WebSocketSharp
  33. {
  34. public class RequestHandshake : Handshake
  35. {
  36. #region Private Field
  37. private NameValueCollection _queryString;
  38. #endregion
  39. #region Private Constructor
  40. private RequestHandshake()
  41. {
  42. }
  43. #endregion
  44. #region Public Constructor
  45. public RequestHandshake(string uriString)
  46. {
  47. HttpMethod = "GET";
  48. RequestUri = Ext.ToUri(uriString);
  49. AddHeader("Upgrade", "websocket");
  50. AddHeader("Connection", "Upgrade");
  51. }
  52. #endregion
  53. #region Properties
  54. public string HttpMethod { get; private set; }
  55. public bool IsWebSocketRequest
  56. {
  57. get
  58. {
  59. if (HttpMethod != "GET")
  60. return false;
  61. if (ProtocolVersion != HttpVersion.Version11)
  62. return false;
  63. if (!HeaderExists("Upgrade", "websocket"))
  64. return false;
  65. if (!HeaderExists("Connection", "Upgrade"))
  66. return false;
  67. if (!HeaderExists("Host"))
  68. return false;
  69. if (!HeaderExists("Sec-WebSocket-Key"))
  70. return false;
  71. if (!HeaderExists("Sec-WebSocket-Version"))
  72. return false;
  73. return true;
  74. }
  75. }
  76. public NameValueCollection QueryString
  77. {
  78. get
  79. {
  80. if (_queryString == null)
  81. {
  82. _queryString = new NameValueCollection();
  83. var i = RawUrl.IndexOf('?');
  84. if (i > 0)
  85. {
  86. var query = RawUrl.Substring(i + 1);
  87. var components = query.Split('&');
  88. foreach (var c in components)
  89. {
  90. var nv = Ext.GetNameAndValue(c, "=");
  91. if (nv.Key != null)
  92. {
  93. var name = Ext.UrlDecode(nv.Key);
  94. var val = Ext.UrlDecode(nv.Value);
  95. _queryString.Add(name, val);
  96. }
  97. }
  98. }
  99. }
  100. return _queryString;
  101. }
  102. }
  103. public string RawUrl
  104. {
  105. get
  106. {
  107. if (RequestUri.IsAbsoluteUri)
  108. return RequestUri.PathAndQuery;
  109. return RequestUri.OriginalString;
  110. }
  111. }
  112. public Uri RequestUri { get; private set; }
  113. #endregion
  114. #region Public Static Methods
  115. public static RequestHandshake Parse(WebSocketContext context)
  116. {
  117. return new RequestHandshake
  118. {
  119. Headers = context.Headers,
  120. HttpMethod = "GET",
  121. RequestUri = context.RequestUri,
  122. ProtocolVersion = HttpVersion.Version11
  123. };
  124. }
  125. public static RequestHandshake Parse(string[] request)
  126. {
  127. var requestLine = request[0].Split(' ');
  128. if (requestLine.Length != 3)
  129. {
  130. var msg = "Invalid HTTP Request-Line: " + request[0];
  131. throw new ArgumentException(msg, "request");
  132. }
  133. var headers = new WebHeaderCollection();
  134. for (int i = 1; i < request.Length; i++)
  135. headers.Add(request[i]);
  136. return new RequestHandshake
  137. {
  138. Headers = headers,
  139. HttpMethod = requestLine[0],
  140. RequestUri = Ext.ToUri(requestLine[1]),
  141. ProtocolVersion = new Version(requestLine[2].Substring(5))
  142. };
  143. }
  144. #endregion
  145. #region Public Method
  146. public override string ToString()
  147. {
  148. var buffer = new StringBuilder();
  149. buffer.AppendFormat("{0} {1} HTTP/{2}{3}", HttpMethod, RawUrl, ProtocolVersion, _crlf);
  150. foreach (string key in Headers.AllKeys)
  151. buffer.AppendFormat("{0}: {1}{2}", key, Headers[key], _crlf);
  152. buffer.Append(_crlf);
  153. return buffer.ToString();
  154. }
  155. #endregion
  156. }
  157. }