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

Unified Diff: net/filter/gzip_source_stream.h

Issue 2334773002: Add net::GzipSourceStream (Closed)
Patch Set: self review sync-ed to r417929 Created 4 years, 3 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
« no previous file with comments | « no previous file | net/filter/gzip_source_stream.cc » ('j') | net/filter/gzip_source_stream.cc » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/filter/gzip_source_stream.h
diff --git a/net/filter/gzip_source_stream.h b/net/filter/gzip_source_stream.h
new file mode 100644
index 0000000000000000000000000000000000000000..628bce3df8f25295861c72f24cd5e22dd82783e7
--- /dev/null
+++ b/net/filter/gzip_source_stream.h
@@ -0,0 +1,117 @@
+// 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_SOURCE_STREAM_H_
+#define NET_FILTER_GZIP_SOURCE_STREAM_H_
+
+#include <memory>
+
+#include "base/macros.h"
+#include "base/memory/ref_counted.h"
+#include "net/base/io_buffer.h"
+#include "net/base/net_export.h"
+#include "net/filter/filter_source_stream.h"
+#include "net/filter/gzip_header.h"
+
+typedef struct z_stream_s z_stream;
+
+namespace net {
+
+class IOBuffer;
+
+// GZipSourceStream applies gzip and deflate content encoding/decoding to a data
+// stream. As specified by HTTP 1.1, with gzip encoding the content is
+// wrapped with a gzip header, and with deflate encoding the content is in
+// a raw, headerless DEFLATE stream.
+//
+// Internally GZipSourceStream uses zlib inflate to do decoding.
+//
+class NET_EXPORT_PRIVATE GzipSourceStream : public FilterSourceStream {
+ public:
+ enum GzipSourceStreamMode {
+ GZIP_SOURCE_STREAM_DEFLATE,
+ GZIP_SOURCE_STREAM_GZIP,
+ // Same as GZIP_SOURCE_STREAM_GZIP, but allow fallback to plain text.
+ GZIP_SOURCE_STREAM_GZIP_WITH_FALLBACK,
+ };
+
+ ~GzipSourceStream() override;
+
+ // Creates a GzipSourceStream. Return nullptr if initialization fails.
+ static std::unique_ptr<GzipSourceStream> Create(
+ std::unique_ptr<SourceStream> previous,
+ GzipSourceStreamMode mode);
+
+ private:
+ GzipSourceStream(std::unique_ptr<SourceStream> previous,
+ GzipSourceStreamMode mode);
+ bool Init();
Randy Smith (Not in Mondays) 2016/09/12 20:54:01 Documentation as to the return value of Init()?
xunjieli 2016/09/14 16:44:01 Done.
+
+ // SourceStream implementation
+ std::string GetTypeAsString() const override;
+ int FilterData(IOBuffer* output_buffer,
+ int output_buffer_size,
+ DrainableIOBuffer* input_buffer) override;
+
+ // Synchronous decompressor. This function consumes bytes from |input_buffer|
+ // and decompresses them into |output_buffer| until it consumes all of
+ // |input_buffer|. This decompressor will decompress a zlib stream (either
+ // gzip or deflate) until the zlib reaches EOF, then will pass any further
+ // input through untouched.
+ int Decompress(IOBuffer* output_buffer,
+ size_t output_buffer_size,
+ DrainableIOBuffer* input_buffer);
+
+ // Returns how many bytes are drained from |input_buffer|.
Randy Smith (Not in Mondays) 2016/09/12 20:54:01 I don't understand based on the comment what this
xunjieli 2016/09/14 16:44:01 Done.
Randy Smith (Not in Mondays) 2016/09/14 22:25:23 Not quite what I meant. The documentation is stil
xunjieli 2016/09/19 13:57:02 Done. It additionally drains footer bytes from the
+ size_t Passthrough(char* output_buffer,
+ size_t output_buffer_size,
+ DrainableIOBuffer* input_buffer);
+
+ // Inserts a zlib header to the data stream before calling zlib inflate.
+ // This is used to work around server bugs.
+ // The function returns true on success and false otherwise.
+ 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_|.
Randy Smith (Not in Mondays) 2016/09/12 20:54:01 I presume this can be called multiple times with m
xunjieli 2016/09/14 16:44:01 Done. Yes, I believe you are right. I will add thi
+ bool IsGzipHeaderInvalid(DrainableIOBuffer* input_buffer);
+
+ // 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(DrainableIOBuffer* input_buffer);
+
+ // Skips gzip footer bytes if necessary. Might drain input from |buffer_| if
+ // there are still footer bytes to read.
+ void SkipGzipFooterIfNeeded(DrainableIOBuffer* input_buffer);
+
+ // Indicates the type of content decoding the stream source is performing.
+ // This variable is set only once by Init.
+ GzipSourceStreamMode mode_;
Randy Smith (Not in Mondays) 2016/09/12 20:54:01 It looks to me as if this is set in the constructo
Randy Smith (Not in Mondays) 2016/09/12 20:54:01 nit, suggestion: I prefer whitespace before explan
xunjieli 2016/09/14 16:44:01 Done.
xunjieli 2016/09/14 16:44:01 Done.
+ // 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_SOURCE_STREAM_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_;
+
+ DISALLOW_COPY_AND_ASSIGN(GzipSourceStream);
+};
+
+} // namespace net
+
+#endif // NET_FILTER_GZIP_SOURCE_STREAM_H__
« no previous file with comments | « no previous file | net/filter/gzip_source_stream.cc » ('j') | net/filter/gzip_source_stream.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698