private object[] decodeDecryptLoop(object[] messageArray) { if (this.CIPHER_KEY.Length > 0) { List<object> receivedMsg = new List<object>(); foreach (object item in messageArray) { PubnubCrypto aes = new PubnubCrypto(this.CIPHER_KEY); string decryptMsg = aes.decrypt(item.ToString()); receivedMsg.Add(decryptMsg); } return receivedMsg.ToArray(); } else { return messageArray; } }
/// <summary> /// Gets the result by wrapping the json response based on the request /// </summary> /// <param name="type"></param> /// <param name="jsonString"></param> /// <param name="url_components"></param> /// <returns></returns> private List<object> WrapResultBasedOnResponseType(ResponseType type, string jsonString, List<string> url_components, bool reconnect) { List<object> result = new List<object>(); string channelName = getChannelName(url_components, type); JavaScriptSerializer jS = new JavaScriptSerializer(); //mod for monomac /*result = jS.Deserialize<List<object>>(jsonString) as List<object>; if (result != null && result.Count > 0 && result[0] is object[]) { result[0] = decodeMsg((object[])result[0], type); }*/ //mod for monomac result = (List<object>)jS.Deserialize<List<object>>(jsonString); var resultOccupancy = jS.DeserializeObject(jsonString.ToString()); if ((result.Count != 0) && (type != ResponseType.DetailedHistory)) { if (result[0] is object[]) { foreach (object message in (object[])result[0]) { //this.ReturnMessage = message; result[0] = message; } } } switch (type) { case ResponseType.Publish: result.Add(channelName); _publishMsg.AddOrUpdate(channelName, result, (key, oldValue) => result); break; case ResponseType.History: if (this.CIPHER_KEY.Length > 0) { List<object> historyDecrypted = new List<object>(); PubnubCrypto aes = new PubnubCrypto(this.CIPHER_KEY); foreach (object message in result) { //mod for monomac //historyDecrypted.Add(aes.decrypt(message.ToString())); string strDecrypted = aes.decrypt(message.ToString()); string strDecoded = jS.Deserialize<string>(strDecrypted); historyDecrypted.Add(strDecoded); } History = historyDecrypted; } else { History = result; } break; case ResponseType.DetailedHistory: //mod for monomac if (this.CIPHER_KEY.Length > 0) { List<object> historyDecrypted = new List<object>(); PubnubCrypto aes = new PubnubCrypto(this.CIPHER_KEY); var myObjectArray = (from item in result select item as object).ToArray(); IEnumerable enumerable = myObjectArray[0] as IEnumerable; if (enumerable != null) { foreach(object element in enumerable) { string strDecrypted = aes.decrypt(element.ToString()); string strDecoded = jS.Deserialize<string>(strDecrypted); historyDecrypted.Add(strDecoded); } } result = historyDecrypted; } else { //DetailedHistory = (object[])(result[0]); List<object> historyDecrypted = new List<object>(); var myObjectArray = (from item in result select item as object).ToArray(); IEnumerable enumerable = myObjectArray[0] as IEnumerable; if (enumerable != null) { foreach(object element in enumerable) { historyDecrypted.Add(element.ToString()); } } result = historyDecrypted; } result.Add(channelName); break; case ResponseType.Here_Now: //mod for monomac //var resultOccupancy = jS.DeserializeObject(jsonString); Dictionary<string, object> dic = (Dictionary<string, object>)resultOccupancy; result = new List<object>(); result.Add(dic); result.Add(channelName); break; case ResponseType.Time: Time = result; break; case ResponseType.Subscribe: result.Add(channelName); _subscribeMsg.AddOrUpdate(channelName, result, (key, oldValue) => { if (reconnect) { List<object> oldResult = oldValue as List<object>; if (oldResult != null) { result[1] = oldResult[1]; } return result; } else { return result; } }); break; case ResponseType.Presence: result.Add(channelName); _presenceMsg.AddOrUpdate(channelName, result, (key, oldValue) => { if (reconnect) { List<object> oldResult = oldValue as List<object>; if (oldResult != null) { result[1] = oldResult[1]; } return result; } else { return result; } }); break; default: break; };//switch stmt end return result; }
/** * Http Get Request * * @param List<string> request of URL directories. * @return List<object> from JSON response. */ private bool _request(List<string> url_components, ResponseType type) { List<object> result = new List<object>(); StringBuilder url = new StringBuilder(); // Add Origin To The Request url.Append(this.ORIGIN); // Generate URL with UTF-8 Encoding foreach (string url_bit in url_components) { url.Append("/"); //url.Append(_encodeURIcomponent(url_bit)); url.Append(url_bit); } if (type == ResponseType.Presence || type == ResponseType.Subscribe) { url.Append("?uuid="); url.Append(this.sessionUUID); } if (type == ResponseType.DetailedHistory) url.Append(parameters); // Temporary fail if string too long if (url.Length > this.LIMIT) { result.Add(0); result.Add("Message Too Long."); // return result; } Uri requestUri = new Uri(url.ToString()); // Force canonical path and query string paq = requestUri.PathAndQuery; /*FieldInfo flagsFieldInfo = typeof(Uri).GetField("m_Flags", BindingFlags.Instance | BindingFlags.NonPublic); ulong flags = (ulong)flagsFieldInfo.GetValue(requestUri); flags &= ~((ulong)0x30); // Flags.PathNotCanonical|Flags.QueryNotCanonical flagsFieldInfo.SetValue(requestUri, flags);*/ // Create Request HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri); try { // Make request with the following inline Asynchronous callback IAsyncResult asyncResult = request.BeginGetResponse(new AsyncCallback((asynchronousResult) => { HttpWebRequest aRequest = (HttpWebRequest)asynchronousResult.AsyncState; HttpWebResponse aResponse = (HttpWebResponse)aRequest.EndGetResponse(asynchronousResult); using (StreamReader streamReader = new StreamReader(aResponse.GetResponseStream())) { // Deserialize the result object jsonString = streamReader.ReadToEnd(); JavaScriptSerializer jS = new JavaScriptSerializer(); //deserialize //object objDecrypted = jS.Deserialize<object>(jsonString.ToString()); //result = objDecrypted.ToString(); result = (List<object>)jS.Deserialize<List<object>>(jsonString.ToString()); var resultOccupancy = jS.DeserializeObject(jsonString.ToString()); if ((result.Count != 0) && (type != ResponseType.DetailedHistory)) { if (result[0] is object[]) { foreach (object message in (object[])result[0]) { this.ReturnMessage = message; } } } switch (type) { case ResponseType.Publish: result.Add(url_components[4]); Publish = result; break; case ResponseType.History: if (this.CIPHER_KEY.Length > 0) { List<object> historyDecrypted = new List<object>(); PubnubCrypto aes = new PubnubCrypto(this.CIPHER_KEY); foreach (object message in result) { string strDecrypted = aes.decrypt(message.ToString()); string strDecoded = jS.Deserialize<string>(strDecrypted); historyDecrypted.Add(strDecoded); } History = historyDecrypted; } else { History = result; } break; case ResponseType.DetailedHistory: if (this.CIPHER_KEY.Length > 0) { List<object> historyDecrypted = new List<object>(); PubnubCrypto aes = new PubnubCrypto(this.CIPHER_KEY); var myObjectArray = (from item in result select item as object).ToArray(); IEnumerable enumerable = myObjectArray[0] as IEnumerable; if (enumerable != null) { foreach(object element in enumerable) { string strDecrypted = aes.decrypt(element.ToString()); string strDecoded = jS.Deserialize<string>(strDecrypted); historyDecrypted.Add(strDecoded); } } DetailedHistory = historyDecrypted; } else { //DetailedHistory = (object[])(result[0]); List<object> historyDecrypted = new List<object>(); var myObjectArray = (from item in result select item as object).ToArray(); IEnumerable enumerable = myObjectArray[0] as IEnumerable; if (enumerable != null) { foreach(object element in enumerable) { historyDecrypted.Add(element.ToString()); } } DetailedHistory = historyDecrypted; } break; case ResponseType.Here_Now: Dictionary<string, object> dic = (Dictionary<string, object>)resultOccupancy; List<object> presented = new List<object>(); presented.Add(dic); Here_Now = (List<object>)presented; break; case ResponseType.Time: Time = result; break; case ResponseType.Subscribe: result.Add(url_components[2]); Subscribe = result; break; case ResponseType.Presence: result.Add(url_components[2]); Presence = result; break; default: break; } } }), request ); //asyncResult.AsyncWaitHandle.WaitOne(); return true; } catch (System.Exception ex) { Console.WriteLine(ex.ToString()); return false; } }