internal long Parse(byte[] data) { if (WSMode) { // now parse WS protocol WebsocketPacket ws = new WebsocketPacket(); var pSize = ws.Parse(data, 0, (uint)data.Length); if (pSize > 0) { WSRequest = ws; return(0); } else { return(pSize); } } else { var rp = new HTTPRequestPacket(); var pSize = rp.Parse(data, 0, (uint)data.Length); if (pSize > 0) { Request = rp; return(0); } else { return(pSize); } } }
public static bool Upgrade(HTTPRequestPacket request, HTTPResponsePacket response) { if (IsWebsocketRequest(request)) { string magicString = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; string ret = request.Headers["Sec-WebSocket-Key"] + magicString; // Compute the SHA1 hash SHA1 sha = SHA1.Create(); byte[] sha1Hash = sha.ComputeHash(Encoding.UTF8.GetBytes(ret)); response.Headers["Upgrade"] = request.Headers["Upgrade"]; response.Headers["Connection"] = request.Headers["Connection"];// "Upgrade"; response.Headers["Sec-WebSocket-Accept"] = Convert.ToBase64String(sha1Hash); if (request.Headers.ContainsKey("Sec-WebSocket-Protocol")) { response.Headers["Sec-WebSocket-Protocol"] = request.Headers["Sec-WebSocket-Protocol"]; } response.Number = HTTPResponsePacket.ResponseCode.Switching; response.Text = "Switching Protocols"; return(true); } return(false); }
public static bool IsWebsocketRequest(HTTPRequestPacket request) { if (request.Headers.ContainsKey("connection") && request.Headers["connection"].ToLower().Contains("upgrade") && request.Headers.ContainsKey("upgrade") && request.Headers["upgrade"].ToLower() == "websocket" && request.Headers.ContainsKey("Sec-WebSocket-Version") && request.Headers["Sec-WebSocket-Version"] == "13" && request.Headers.ContainsKey("Sec-WebSocket-Key")) //&& Request.Headers.ContainsKey("Sec-WebSocket-Protocol")) { return(true); } else { return(false); } }