Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(224)

Unified Diff: net/filter/gzip_stream_source.h

Issue 1662763002: [ON HOLD] Implement pull-based design for content decoding (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address comments Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: net/filter/gzip_stream_source.h
diff --git a/net/filter/gzip_stream_source.h b/net/filter/gzip_stream_source.h
new file mode 100644
index 0000000000000000000000000000000000000000..3afb3336010dfe159086098f32cbe84bd639f228
--- /dev/null
+++ b/net/filter/gzip_stream_source.h
@@ -0,0 +1,103 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef NET_FILTER_GZIP_STREAM_SOURCE_H_
+#define NET_FILTER_GZIP_STREAM_SOURCE_H_
+
+#include <memory>
+
+#include "net/filter/filter_stream_source.h"
+#include "net/filter/gzip_header.h"
+
+typedef struct z_stream_s z_stream;
+
+namespace net {
+
+class BlockBuffer;
+
+class GzipStreamSource : public FilterStreamSource {
+ public:
+ enum GzipStreamSourceMode {
+ GZIP_STREAM_SOURCE_DEFLATE,
+ GZIP_STREAM_SOURCE_GZIP,
+ // Same as GZIP_STREAM_SOURCE_GZIP, but allow fallback to plain text.
+ GZIP_STREAM_SOURCE_GZIP_WITH_FALLBACK,
+ };
+
+ explicit GzipStreamSource(std::unique_ptr<StreamSource> previous,
+ GzipStreamSourceMode mode);
+ ~GzipStreamSource() override;
+
+ bool Init() override;
+
+ private:
+ enum GzipStreamState {
+ GZIP_STREAM_ERROR,
+ GZIP_STREAM_MORE_INPUT,
+ GZIP_STREAM_MORE_OUTPUT_SPACE,
+ };
+
+ // StreamSource implementation
+ Error ReadInternal(IOBuffer* dest_buffer,
+ size_t buffer_size,
+ size_t* bytes_read) override;
+
+ GzipStreamState Decompress(IOBuffer* dest_buffer,
+ size_t buffer_size,
+ size_t* bytes_read);
+
+ GzipStreamState Passthrough(IOBuffer* dest_buffer,
+ size_t buffer_size,
+ size_t* bytes_read);
+
+ void OnReadComplete(const OnReadCompleteCallback& callback,
+ IOBuffer* dest_buffer,
+ size_t dest_buffer_size,
+ Error error,
+ size_t bytes_read);
+
+ bool InsertZlibHeader();
+
+ // Returns whether an invalid GZip header has been observed. This method
+ // returns true as soon as an invalid GZip header is observed; it returns
+ // false if a complete, valid header has been observed, or not enough bytes
+ // have been seen to be sure. This method pulls its input from |buffer_|.
+ bool IsGzipHeaderInvalid();
+
+ // Returns whether this stream looks like it could be plain text (ie, not
+ // actually gzipped). Right now this uses an extremely simple heuristic; see
+ // the source for details. This method checks the input in |buffer_|, but does
+ // not drain from it.
+ bool ShouldFallbackToPlain();
+
+ // Skips gzip footer bytes if necessary. Might drain input from |buffer_| if
+ // there are still footer bytes to read.
+ void SkipGzipFooterIfNeeded();
+
+ scoped_refptr<IOBuffer> pending_read_buffer_;
+
+ // Indicates the type of content decoding the stream source is performing.
+ // This variable is set only once by Init.
+ GzipStreamSourceMode mode_;
+ // The control block of zlib which actually does the decoding.
+ // This data structure is initialized by Init and updated only by
+ // Decompress, with InsertZlibHeader being the exception as a workaround.
+ std::unique_ptr<z_stream> zlib_stream_;
+ // Whether end of stream has been reached.
+ bool zlib_eof_;
+ // A flag used by Decompress to record whether we've successfully added
+ // a zlib header to this stream.
+ bool zlib_header_added_;
+ // Used to parse the gzip header in gzip stream.
+ // It is used when the decoding mode is GZIP_STREAM_SOURCE_GZIP.
+ GZipHeader gzip_header_;
+ // Whether gzip header should be checked for validity.
+ bool should_check_gzip_header_;
+ // Tracks how many bytes of gzip footer are yet to be filtered.
+ size_t gzip_footer_bytes_left_;
+};
+
+} // namespace net
+
+#endif // NET_FILTER_GZIP_STREAM_SOURCE_H__

Powered by Google App Engine
This is Rietveld 408576698