public void Connected(ClientHandshake handshake) { ((IWebSocket)webSocket).Connected(handshake); }
private ClientHandshake ParseClientHandshake(ArraySegment <byte> byteShake) { // the "grammar" of the handshake var pattern = @"^(?<connect>[^\s]+)\s(?<path>[^\s]+)\sHTTP\/1\.1\r\n" + // request line @"((?<field_name>[^:\r\n]+):\s(?<field_value>[^\r\n]+)\r\n)+"; // unordered set of fields (name-chars colon space any-chars cr lf) // subtract the challenge bytes from the handshake var handshake = new ClientHandshake(); ArraySegment <byte> challenge = new ArraySegment <byte>(byteShake.Array, byteShake.Count - 8, 8); // -8 : eight byte challenge handshake.ChallengeBytes = challenge; // get the rest of the handshake var utf8_handshake = Encoding.UTF8.GetString(byteShake.Array, 0, byteShake.Count - 8); // match the handshake against the "grammar" var regex = new Regex(pattern, RegexOptions.IgnoreCase); var match = regex.Match(utf8_handshake); var fields = match.Groups; // save the request path handshake.ResourcePath = fields["path"].Value; // run through every match and save them in the handshake object for (int i = 0; i < fields["field_name"].Captures.Count; i++) { var name = fields["field_name"].Captures[i].ToString(); var value = fields["field_value"].Captures[i].ToString(); switch (name.ToLower()) { case "sec-websocket-version": handshake.Version = int.Parse(value); break; case "sec-websocket-key": handshake.Key1 = value; break; case "sec-websocket-key1": handshake.Key1 = value; break; case "sec-websocket-key2": handshake.Key2 = value; break; case "sec-websocket-protocol": handshake.SubProtocol = value; break; case "sec-websocket-origin": handshake.Origin = value; break; case "origin": handshake.Origin = value; break; case "host": handshake.Host = value; break; case "cookie": // create and fill a cookie collection from the data in the handshake handshake.Cookies = new HttpCookieCollection(); var cookies = value.Split(';'); foreach (var item in cookies) { // the name if before the '=' char var c_name = item.Remove(item.IndexOf('=')); // the value is after var c_value = item.Substring(item.IndexOf('=') + 1); // put the cookie in the collection (this also parses the sub-values and such) handshake.Cookies.Add(new HttpCookie(c_name.TrimStart(), c_value)); } break; default: // some field that we don't know about if (handshake.AdditionalFields == null) { handshake.AdditionalFields = new Dictionary <string, string>(); } handshake.AdditionalFields[name] = value; break; } } return(handshake); }
public abstract void Connected(ClientHandshake handshake);