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

Unified Diff: net/filter/gzip_source_stream.h

Issue 2334773002: Add net::GzipSourceStream (Closed)
Patch Set: Addressed Randy's comments and synced to r419441 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..23b3c15076153f32ddb34fd54ac015fc898e6306
--- /dev/null
+++ b/net/filter/gzip_source_stream.h
@@ -0,0 +1,138 @@
+// 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:
+ ~GzipSourceStream() override;
+
+ // Creates a GzipSourceStream. Return nullptr if initialization fails.
+ static std::unique_ptr<GzipSourceStream> Create(
+ std::unique_ptr<SourceStream> previous,
+ SourceStream::SourceType type);
+
+ private:
+ enum InputState {
+ // Starts processing the input stream. Checks whether the stream is valid
+ // and whether a fallback to plain data is needed.
+ STATE_START,
+ // Gzip header of the input stream is being processed.
+ STATE_GZIP_HEADER,
+ // The input stream is being decoded.
+ STATE_COMPRESSED_BODY,
+ // The input stream is being passed through undecoded.
+ STATE_UNCOMPRESSED_BODY,
+ };
+
+ GzipSourceStream(std::unique_ptr<SourceStream> previous,
+ SourceStream::SourceType type);
+
+ // Returns true if initialization is successful, false otherwise.
+ // For instance, this method returns false if there is not enough memory or
+ // if there is a version mismatch.
+ bool Init();
+
+ // SourceStream implementation
+ std::string GetTypeAsString() const override;
+ int FilterData(IOBuffer* output_buffer,
+ int output_buffer_size,
+ IOBuffer* input_buffer,
+ int input_buffer_size,
+ int* consumed_bytes,
+ bool upstream_end_reached) 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
Randy Smith (Not in Mondays) 2016/09/21 20:57:06 Presumably it will also stop when it's filled |*ou
xunjieli 2016/09/22 17:20:49 Done. Added to the doc.
+ // gzip or deflate) until the zlib reaches EOF, then will pass any further
+ // input through untouched.
+ int Decompress(IOBuffer* output_buffer,
+ int output_buffer_size,
+ char* input_buffer,
+ int input_buffer_size,
+ int* consumed_bytes);
+
+ // Passes through as many bytes as possible from from |input_buffer| to
+ // |output_buffer|. It might additionally drains footer bytes from the
+ // |input_buffer| without writing the footer bytes to |output_buffer|. The
Randy Smith (Not in Mondays) 2016/09/21 20:57:06 I don't find this documentation complete; can this
xunjieli 2016/09/22 17:20:49 Done. I have inlined this method. After applying y
+ // total number of consumed bytes from |input_buffer| will be written to
+ // |consumed_bytes|.
+ size_t Passthrough(char* output_buffer,
+ int output_buffer_size,
+ char* input_buffer,
+ int input_buffer_size,
+ int* consumed_bytes);
+
+ // 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.
Randy Smith (Not in Mondays) 2016/09/21 20:57:06 nit, suggestion: I don't think the comments needs
xunjieli 2016/09/22 17:20:49 Done.
+ 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 can be called multiple times with
+ // multiple |input_buffer|. The number of consumed bytes will be written to
+ // |consumed_bytes|.
+ bool IsGzipHeaderInvalid(char* input_buffer,
+ int input_buffer_size,
+ int* consumed_bytes);
Randy Smith (Not in Mondays) 2016/09/21 20:57:06 I don't see this function in the source file anymo
xunjieli 2016/09/22 17:20:49 Done.
+
+ // 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 first byte of the stream.
+ bool ShouldFallbackToPlain(char first_byte);
+
+ // Returns number of footer bytes to skip.
+ size_t NumGzipFooterBytesToSkip(int input_buffer_size);
+
+ // 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_;
+
+ // 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_;
+
+ // Tracks how many bytes of gzip footer are yet to be filtered.
+ size_t gzip_footer_bytes_left_;
+
+ // Tracks the state of the input stream.
+ InputState input_state_;
+
+ 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