public byte[] GenerateC2SRequest() { string lHost = mUri.DnsSafeHost; //string lPath = mUri.PathAndQuery; string lPath = mUri.AbsolutePath + "?" + mUri.Query; mOrigin = "http://" + lHost; if ("".Equals(lPath)) { lPath = "/"; } string lHandshake = "GET " + lPath + " HTTP/1.1 \r\n" + "Host: " + lHost + "\r\n" + "Upgrade: WebSocket\r\n" + "Connection: Upgrade\r\n" + "Sec-WebSocket-Key: " + mHybiKey + "\r\n" + "Origin: " + mOrigin + "\r\n" + "Sec-WebSocket-Protocol: " + mProtocol + "\r\n" + "Sec-WebSocket-Version: " + mVersion + "\r\n"; return(WebSocketConvert.StringToBytes(lHandshake, WebSocketTypeEncoding.UTF8)); }
private static string CalcHybiSecKeyAccept(string aKey) { aKey = aKey + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; string lAccept = null; //SHA1 lSHA1 = new SHA1CryptoServiceProvider(); var sha1 = new SHA1Managed(); byte[] lBufSource = WebSocketConvert.StringToBytes(aKey, WebSocketTypeEncoding.UTF8); try { //byte[] lBufTarget = lSHA1.ComputeHash(lBufSource); byte[] lBufTarget = sha1.ComputeHash(lBufSource); lAccept = Convert.ToBase64String(lBufTarget); } catch (Exception lEx) { throw new WebSocketException("Error converting to Base64" + lEx.Message); } return(lAccept); }
public void ReadResponseFromBuffer(byte[] aBuff) { int lHeaderComplete = 0; int lLines = 0; int lSizeBuff = aBuff.Length; byte lRT = 0; byte lNL = 0; byte[] lBuff = new byte[0]; while (lHeaderComplete < lSizeBuff) { lRT = lNL; lNL = aBuff[lHeaderComplete]; if (lRT.Equals(0x0D) && lNL.Equals(0x0A)) { string lLine = WebSocketConvert.BytesToString(lBuff, WebSocketTypeEncoding.UTF8); if (lLines > 0) { char[] c = { ':' }; string[] lKeyVal = lLine.Split(c); if (lKeyVal.Count() == 2) { mResponseFields.Add(lKeyVal[0].Trim(), lKeyVal[1].Trim()); } } else { mFirstLineResponse = lLine; } lBuff = new byte[0]; lLines++; } else if (!lNL.Equals(0x0A) && !lNL.Equals(0x0D)) { Write(ref lBuff, lNL); } lHeaderComplete++; } }
public void SetASCII(string aString) { mByteArray = WebSocketConvert.StringToBytes(aString, WebSocketTypeEncoding.ASCII); }
public string GetASCII() { return(WebSocketConvert.BytesToString(mByteArray, WebSocketTypeEncoding.ASCII)); }
public string GetUTF8() { return(WebSocketConvert.BytesToString(mByteArray, WebSocketTypeEncoding.UTF8)); }
public string GetString(WebSocketTypeEncoding aEncoding) { return(WebSocketConvert.BytesToString(mByteArray, aEncoding)); }
public string GetString() { return(WebSocketConvert.BytesToString(mByteArray, WebSocketTypeEncoding.Default)); }
public void SetUTF8(string aString) { mByteArray = WebSocketConvert.StringToBytes(aString, WebSocketTypeEncoding.UTF8); }
public void SetString(string aString, WebSocketTypeEncoding aEncoding) { mByteArray = WebSocketConvert.StringToBytes(aString, aEncoding); }