Example #1
0
File: MSN.cs Project: SayHalou/ospy
        protected void SBParseMSG(PacketStream stream, TransactionNode payloadNode, int payloadLength)
        {
            List<PacketSlice> slices = new List<PacketSlice>(2);

            string content = stream.PeekStringUTF8(payloadLength);

            TransactionNode headersNode = new TransactionNode(payloadNode, "Headers");

            int pos = content.IndexOf("\r\n\r\n");
            string headers = content.Substring(0, pos);
            string[] lines = headers.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string line in lines)
            {
                stream.ReadBytes(StaticUtils.GetUTF8ByteCount(line), slices);

                string[] tokens = line.Split(new char[] { ':' }, 2);
                tokens[1] = tokens[1].TrimStart(new char[] { ' ' });

                headersNode.AddField(tokens[0], tokens[1], "Message header field.", slices);

                // Skip CRLF
                stream.ReadBytes(2);
            }

            // Skip extra CRLF
            stream.ReadBytes(2);

            int bodyLength = payloadLength - StaticUtils.GetUTF8ByteCount(headers) - 4;

            if (bodyLength > 0)
            {
                TransactionNode bodyNode = new TransactionNode(payloadNode, "Body");

                string contentType = (string)headersNode.Fields["Content-Type"];
                contentType = contentType.Split(new char[] { ';' }, 2)[0];

                if (contentType == "application/x-msnmsgrp2p")
                {
                    ReadNextP2PMessageChunk(stream, bodyNode);

                    UInt32 appID = stream.ReadU32BE(slices);
                    bodyNode.AddField("AppID", appID, "Application ID.", slices);
                }
                else if (contentType == "text/x-msmsgsinvite")
                {
                    string bodyStr = stream.ReadStringUTF8(bodyLength, slices);

                    bodyNode.AddTextField("Body", bodyStr, "Invite body.", slices);
                }
                else
                {
                    string bodyStr = stream.ReadStringUTF8(bodyLength, slices);

                    bodyNode.AddField("Body", bodyStr, "Body.", slices);
                }
            }
        }