Exemplo n.º 1
0
 /// <summary>
 /// chunked エンコーディングをデコード
 /// </summary>
 /// <param name="source">対象のストリーム</param>
 /// <returns>デコードされたストリーム</returns>
 private static Stream Dechunk(this Stream source)
 {
     using (source)
         using (var c = new ChunkedBodyParser(true, int.MaxValue, true))
         {
             source.Position = 0;
             int b;
             while (-1 != (b = source.ReadByte()))
             {
                 c.WriteByte((byte)b);
             }
             return(new MemoryStream(c.Body));
         }
 }
Exemplo n.º 2
0
        public static SazHttpRequest Parse(byte[] source)
        {
            var strings = Encoding.ASCII.GetString(source);
            var lines   = strings.Split(new[] { "\r\n" }, StringSplitOptions.None);

            var requestLine = SazHttpRequestLine.Parse(lines[0] + "\r\n");

            var headersSource = string.Join("\r\n", lines.Skip(1).TakeWhile(x => !string.IsNullOrEmpty(x))) + "\r\n\r\n";
            var headers       = SazHttpHeaders.Parse(headersSource);

            var bodySkipCount = strings.Split(new[] { "\r\n\r\n" }, StringSplitOptions.None).First().Length + 4;
            var body          = source.Skip(bodySkipCount).ToArray();

            if (headers.HasHeader("Transfer-Encoding") && headers.GetValues("Transfer-Encoding").Contains("chunked"))
            {
                var parser = new ChunkedBodyParser(true, int.MaxValue, false);
                foreach (var b in body)
                {
                    parser.WriteByte(b);
                }
                return(new SazHttpRequest(requestLine, headers, parser.Body, SazHttpHeaders.Parse(parser.Trailers)));
            }
            return(new SazHttpRequest(requestLine, headers, body, SazHttpHeaders.Parse("")));
        }