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

Side by Side Diff: net/filter/gzip_source_stream.h

Issue 2334773002: Add net::GzipSourceStream (Closed)
Patch Set: Address Randy's comments Created 4 years, 2 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 | « no previous file | 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 class IOBuffer;
22
23 // GZipSourceStream applies gzip and deflate content encoding/decoding to a data
24 // stream. As specified by HTTP 1.1, with gzip encoding the content is
25 // wrapped with a gzip header, and with deflate encoding the content is in
26 // a raw, headerless DEFLATE stream.
27 //
28 // Internally GZipSourceStream uses zlib inflate to do decoding.
29 //
30 class NET_EXPORT_PRIVATE GzipSourceStream : public FilterSourceStream {
31 public:
32 ~GzipSourceStream() override;
33
34 // Creates a GzipSourceStream. Return nullptr if initialization fails.
35 static std::unique_ptr<GzipSourceStream> Create(
36 std::unique_ptr<SourceStream> previous,
37 SourceStream::SourceType type);
38
39 private:
40 enum InputState {
41 // Starts processing the input stream. Checks whether the stream is valid
42 // and whether a fallback to plain data is needed.
43 STATE_START,
44 // Gzip header of the input stream is being processed.
45 STATE_GZIP_HEADER,
46 // The input stream is being decoded.
47 STATE_COMPRESSED_BODY,
48 // Gzip footer of the input stream is being processed.
49 STATE_GZIP_FOOTER,
50 // The input stream is being passed through undecoded.
51 STATE_UNCOMPRESSED_BODY,
52 };
53
54 GzipSourceStream(std::unique_ptr<SourceStream> previous,
55 SourceStream::SourceType type);
56
57 // Returns true if initialization is successful, false otherwise.
58 // For instance, this method returns false if there is not enough memory or
59 // if there is a version mismatch.
60 bool Init();
61
62 // SourceStream implementation
63 std::string GetTypeAsString() const override;
64 int FilterData(IOBuffer* output_buffer,
65 int output_buffer_size,
66 IOBuffer* input_buffer,
67 int input_buffer_size,
68 int* consumed_bytes,
69 bool upstream_end_reached) override;
70
71 // Inserts a zlib header to the data stream before calling zlib inflate.
72 // This is used to work around server bugs. The function returns true on
73 // success.
74 bool InsertZlibHeader();
75
76 // Returns whether this stream looks like it could be plain text (ie, not
77 // actually gzipped). Right now this uses an extremely simple heuristic; see
78 // the source for details. This method checks the first byte of the stream.
79 bool ShouldFallbackToPlain(char first_byte);
80
81 // The control block of zlib which actually does the decoding.
82 // This data structure is initialized by Init and updated only by
83 // FilterData(), with InsertZlibHeader() being the exception as a workaround.
84 std::unique_ptr<z_stream> zlib_stream_;
85
86 // A flag used by FilterData() to record whether we've successfully added
87 // a zlib header to this stream.
88 bool zlib_header_added_;
89
90 // Used to parse the gzip header in gzip stream.
91 // It is used when the decoding mode is GZIP_SOURCE_STREAM_GZIP.
92 GZipHeader gzip_header_;
93
94 // Tracks how many bytes of gzip footer are yet to be filtered.
95 size_t gzip_footer_bytes_left_;
96
97 // Tracks the state of the input stream.
98 InputState input_state_;
99
100 DISALLOW_COPY_AND_ASSIGN(GzipSourceStream);
101 };
102
103 } // namespace net
104
105 #endif // NET_FILTER_GZIP_SOURCE_STREAM_H__
OLDNEW
« no previous file with comments | « no previous file | net/filter/gzip_source_stream.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698