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

Side by Side Diff: net/filter/gzip_source_stream.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: rebased Created 4 years, 4 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 unified diff | Download patch
« no previous file with comments | « net/filter/gzip_filter_unittest.cc ('k') | net/filter/gzip_source_stream.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef NET_FILTER_GZIP_SOURCE_STREAM_H_
6 #define NET_FILTER_GZIP_SOURCE_STREAM_H_
7
8 #include <memory>
9
10 #include "base/macros.h"
11 #include "base/memory/ref_counted.h"
12 #include "net/base/io_buffer.h"
13 #include "net/base/net_export.h"
14 #include "net/filter/filter_source_stream.h"
15 #include "net/filter/gzip_header.h"
16
17 typedef struct z_stream_s z_stream;
18
19 namespace net {
20
21 // GZipSourceStream applies gzip and deflate content encoding/decoding to a data
22 // stream. As specified by HTTP 1.1, with gzip encoding the content is
23 // wrapped with a gzip header, and with deflate encoding the content is in
24 // a raw, headerless DEFLATE stream.
25 //
26 // Internally GZipSourceStream uses zlib inflate to do decoding.
27 //
28 class NET_EXPORT_PRIVATE GzipSourceStream : public FilterSourceStream {
29 public:
30 enum GzipSourceStreamMode {
31 GZIP_SOURCE_STREAM_DEFLATE,
32 GZIP_SOURCE_STREAM_GZIP,
33 // Same as GZIP_SOURCE_STREAM_GZIP, but allow fallback to plain text.
34 GZIP_SOURCE_STREAM_GZIP_WITH_FALLBACK,
35 };
36
37 ~GzipSourceStream() override;
38
39 // Creates a GzipSourceStream. Return nullptr if initialization fails.
40 static std::unique_ptr<GzipSourceStream> Create(
41 std::unique_ptr<SourceStream> previous,
42 GzipSourceStreamMode mode);
43
44 private:
45 GzipSourceStream(std::unique_ptr<SourceStream> previous,
46 GzipSourceStreamMode mode);
47 bool Init();
48 // SourceStream implementation
49 std::string GetTypeAsString() const override;
50 int FilterData(IOBuffer* output_buffer,
51 size_t output_buffer_size,
52 DrainableIOBuffer* input_buffer) override;
53
54 // Synchronous decompressor. This function consumes bytes from |input_buffer|
55 // and decompresses them into |output_buffer| until it consumes all of
56 // |input_buffer|. This decompressor will decompress a zlib stream (either
57 // gzip or deflate) until the zlib reaches EOF, then will pass any further
58 // input through untouched.
59 int Decompress(IOBuffer* output_buffer,
60 size_t output_buffer_size,
61 DrainableIOBuffer* input_buffer);
62
63 // Returns how many bytes are drained from |input_buffer|.
64 size_t Passthrough(char* output_buffer,
65 size_t output_buffer_size,
66 DrainableIOBuffer* input_buffer);
67
68 // Inserts a zlib header to the data stream before calling zlib inflate.
69 // This is used to work around server bugs.
70 // The function returns true on success and false otherwise.
71 bool InsertZlibHeader();
72
73 // Returns whether an invalid GZip header has been observed. This method
74 // returns true as soon as an invalid GZip header is observed; it returns
75 // false if a complete, valid header has been observed, or not enough bytes
76 // have been seen to be sure. This method pulls its input from |buffer_|.
77 bool IsGzipHeaderInvalid(DrainableIOBuffer* input_buffer);
78
79 // Returns whether this stream looks like it could be plain text (ie, not
80 // actually gzipped). Right now this uses an extremely simple heuristic; see
81 // the source for details. This method checks the input in |buffer_|, but does
82 // not drain from it.
83 bool ShouldFallbackToPlain(DrainableIOBuffer* input_buffer);
84
85 // Skips gzip footer bytes if necessary. Might drain input from |buffer_| if
86 // there are still footer bytes to read.
87 void SkipGzipFooterIfNeeded(DrainableIOBuffer* input_buffer);
88
89 scoped_refptr<IOBuffer> pending_read_buffer_;
90
91 // Indicates the type of content decoding the stream source is performing.
92 // This variable is set only once by Init.
93 GzipSourceStreamMode mode_;
94 // The control block of zlib which actually does the decoding.
95 // This data structure is initialized by Init and updated only by
96 // Decompress, with InsertZlibHeader being the exception as a workaround.
97 std::unique_ptr<z_stream> zlib_stream_;
98 // Whether end of stream has been reached.
99 bool zlib_eof_;
100 // A flag used by Decompress to record whether we've successfully added
101 // a zlib header to this stream.
102 bool zlib_header_added_;
103 // Used to parse the gzip header in gzip stream.
104 // It is used when the decoding mode is GZIP_SOURCE_STREAM_GZIP.
105 GZipHeader gzip_header_;
106 // Whether gzip header should be checked for validity.
107 bool should_check_gzip_header_;
108 // Tracks how many bytes of gzip footer are yet to be filtered.
109 size_t gzip_footer_bytes_left_;
110
111 DISALLOW_COPY_AND_ASSIGN(GzipSourceStream);
112 };
113
114 } // namespace net
115
116 #endif // NET_FILTER_GZIP_SOURCE_STREAM_H__
OLDNEW
« no previous file with comments | « net/filter/gzip_filter_unittest.cc ('k') | net/filter/gzip_source_stream.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698