Esempio n. 1
0
        /// <summary>Decompresses headers from a <see cref="SPDYFrameType.Headers"/>, <see cref="SPDYFrameType.Reply"/>, or
        /// <see cref="SPDYFrameType.Stream"/> frame.
        /// </summary>
        /// <remarks>Because the compressed stream spans all frames read in the connection, only one thread may be decompressing headers
        /// at any given time.
        /// </remarks>
        public Dictionary <string, List <string> > DecompressHeaders(byte[] data, int index, int length)
        {
            byte[] dec = decompressor.Transform(data, index, length, ZlibFlush.Sync).ToArray(); // decompress it
            int    count = SPDYFrame.ReadInt(dec, 0), i = 4;                                    // the first field is the number of headers
            var    headers = new Dictionary <string, List <string> >(count);

            while (--count >= 0)                                                   // for each header...
            {
                int    byteCount = SPDYFrame.ReadInt(dec, i);                      // read the key length
                string key       = Encoding.UTF8.GetString(dec, i + 4, byteCount); // and the key itself
                i         = i + 4 + byteCount;
                byteCount = SPDYFrame.ReadInt(dec, i);                             // now read the values length
                i        += 4;
                var values = new List <string>();
                for (int end = i + byteCount; i < end;)
                {
                    int sep = i;                     // scan for the end of the current value
                    while (sep < end && data[sep] != 0)
                    {
                        sep++;                                            // if we get to the end or see a NUL byte, stop
                    }
                    values.Add(Encoding.UTF8.GetString(dec, i, sep - i)); // add the value
                    i = sep;                                              // and move past it
                    if (sep < end)
                    {
                        i++;                               // if we stopped at a NUL byte, move past that too
                    }
                }
                headers[key] = values;
            }
            return(headers);
        }