public void setup() { _rawSample = new PushbackInputStream(HttpSample.ChunkedResponse()); var buf = new byte[74]; _rawSample.Read(buf, 0, 74); // skip HTTP headers _subject = new HttpChunkedStreamWrapper(_rawSample, TimeSpan.FromSeconds(1)); }
public void pushback_input_disposal() { var under = Substitute.For<Stream>(); var subject = new PushbackInputStream(under); subject.Dispose(); under.Received().Close(); }
static bool LooksLikeGzip([NotNull] PushbackInputStream s) { var magic = new byte[10]; var len = s.Read(magic, 0, 10); s.UnRead(magic, 0, 10); // always reset the stream. return(len == 10 && magic[0] == 0x1F && magic[1] == 0x8B); }
public void setup() { _underlying = Substitute.For<Stream>(); _underlying.Read(Arg.Any<byte[]>(), 0, 100).Returns(99); _underlying.CanSeek.Returns(true); _underlying.Seek(-100, SeekOrigin.Current).Returns(1); _underlying.Seek(10, SeekOrigin.Begin).Returns(9); _subject = new PushbackInputStream(_underlying); }
[NotNull] Stream PrepareGzip([NotNull] PushbackInputStream src) { if (LooksLikeGzip(src)) { var gz = new GZipStream(src, CompressionMode.Decompress, true); _isDone = GzipFinished(gz) ?? (() => true); return(gz); } _isDone = () => true; return(src); }
/// <summary> /// Reads synchronously until headers are complete, then /// provides the remaining data in a stream /// </summary> public HttpResponse(Stream rawResponse, TimeSpan timeout) { _rawResponse = rawResponse; Headers = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); ReadStatusLine(NextLine(_rawResponse)); foreach (var headerLine in NonBlankLines(_rawResponse)) AddHeader(headerLine); HeadersComplete = true; _rawResponse.ReadByte(); // eat one spare byte if (_rawResponse is SocketStream) ((SocketStream)_rawResponse).ResetCounts(); RawBodyStream = _rawResponse; // I am scared of this code: var buffered = new PushbackInputStream(_rawResponse); var dechunked = IsChunked() ? (Stream)new HttpChunkedStreamWrapper(buffered, timeout) : buffered; var decompressed = RestOfStreamDecompressed(dechunked); BodyReader = new HttpResponseStream(decompressed, ReportedBodyLength()) {Timeout = timeout}; }
/// <summary> /// Wrap gzip stream /// </summary> public GZipStreamWrapper(PushbackInputStream source) { _source = source; _isDone = () => false; _decompressedStream = null; }
Stream PrepareGzip(PushbackInputStream src) { if (LooksLikeGzip(src)) { var gz = new GZipStream(src, CompressionMode.Decompress, true); _isDone = GzipFinished(gz) ?? (() => true); return gz; } _isDone = () => true; return src; }
static bool LooksLikeGzip(PushbackInputStream s) { var magic = new byte[10]; var len = s.Read(magic, 0, 10); s.UnRead(magic, 0, 10); // always reset the stream. return (len == 10 && magic[0] == 0x1F && magic[1] == 0x8B); }
/// <summary> /// Wrap gzip stream /// </summary> public GZipStreamWrapper(PushbackInputStream source) { _source = source ?? throw new ArgumentNullException(nameof(source)); _isDone = () => false; _decompressedStream = null; }
Stream RestOfStreamDecompressed(Stream unchunked) { if (!Headers.ContainsKey("Content-Encoding")) return unchunked; // plain body var pushbackStream = new PushbackInputStream(unchunked); switch (Headers["Content-Encoding"]) { case "gzip": return GzipUnwrap(pushbackStream); case "deflate": return DeflateUnwrap(unchunked); // no good way to determine this default: throw new Exception("Unknown compression scheme: " + Headers["Content-Encoding"]); } }
static Stream GzipUnwrap(PushbackInputStream rawResponse) { return new GZipStreamWrapper(rawResponse); }
public void setup() { _underlying = new NonSeekable(_sample_data); _subject = new PushbackInputStream(_underlying); }