Exemple #1
0
        public static Http2HeadersRequest ParseHeaders(Http2Frame frame, ArraySegment <byte> data)
        {
            if (frame.type == Http2FrameType.Headers)
            {
                throw new InvalidOperationException("frame.type != Headers");
            }
            if (frame.length > data.Count)
            {
                return(null);
            }

            var req = new Http2HeadersRequest();

            for (int i = 0; i < frame.length; i++)
            {
                var ch = data.ElementAt(i);

                // 1 0 -> Indexed_HasNothing
                if ((ch & 0x80) != 0)
                {
                    req.hpackCommands.Add(HPackCommandFactory.Indexed(ch ^ 0x80));
                }
                // 0 1 -> Indexed_HasValue
                else if ((ch & 0x40) != 0)
                {
                    var length = data.ElementAt(i + 1);
                    var huff   = (length & 0x80) != 0;

                    length &= 0x7F;

                    var payload = new byte[length];

                    Console.WriteLine("hUFF : " + huff.ToString());
                    Console.WriteLine("LEN : " + length.ToString());

                    Buffer.BlockCopy(data.Array, data.Offset + i + 2, payload, 0, length);

                    if (huff)
                    {
                        payload = Huffman.Convert.Decode(payload);
                    }

                    Console.WriteLine(Encoding.UTF8.GetString(payload));

                    req.hpackCommands.Add(
                        HPackCommandFactory.UpdateValue(ch ^ 0x40, Encoding.UTF8.GetString(payload)));

                    i += 1 + length;
                }
            }

            return(req);
        }
Exemple #2
0
        public Http2HeadersResponse BuildResponse(HttpResponse response)
        {
            var headers = new Http2HeadersResponse();

            if (ResponseCode.OK == response.code)
            {
                headers.Add(HPackCommandFactory.Indexed(PredefinedIndexes.Status1));
            }
            else if (ResponseCode.BadRequest == response.code)
            {
                headers.Add(HPackCommandFactory.Indexed(PredefinedIndexes.Status400));
            }
            else if (ResponseCode.NotFound == response.code)
            {
                headers.Add(HPackCommandFactory.Indexed(PredefinedIndexes.Status404));
            }
            else if (ResponseCode.InternalServerError == response.code)
            {
                headers.Add(HPackCommandFactory.Indexed(PredefinedIndexes.Status500));
            }
            else
            {
                headers.Add(HPackCommandFactory.UpdateValue(PredefinedIndexes.Status1, ((int)response.code).ToString()));
            }

            foreach (var header in response.headers)
            {
                // 이미 캐싱된 키
                if (tableByKey.ContainsKey(header.Key))
                {
                    var item = tableByKey[header.Key];

                    // 값도 캐싱됨
                    if (item.strValue == header.Value)
                    {
                        headers.Add(HPackCommandFactory.Indexed(item.index));
                    }
                    else
                    {
                        headers.Add(HPackCommandFactory.UseIndexedKey(item.index, header.Value));
                    }
                }
                else
                {
                    headers.Add(HPackCommandFactory.NotIndexing(header.Key, header.Value));
                }
            }

            return(headers);
        }