| OLD | NEW | 
|---|
| (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_STREAM_SOURCE_H_ | 
|  | 6 #define NET_FILTER_GZIP_STREAM_SOURCE_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_stream_source.h" | 
|  | 15 #include "net/filter/gzip_header.h" | 
|  | 16 | 
|  | 17 typedef struct z_stream_s z_stream; | 
|  | 18 | 
|  | 19 namespace net { | 
|  | 20 | 
|  | 21 class NET_EXPORT_PRIVATE GzipStreamSource : public FilterStreamSource { | 
|  | 22  public: | 
|  | 23   enum GzipStreamSourceMode { | 
|  | 24     GZIP_STREAM_SOURCE_DEFLATE, | 
|  | 25     GZIP_STREAM_SOURCE_GZIP, | 
|  | 26     // Same as GZIP_STREAM_SOURCE_GZIP, but allow fallback to plain text. | 
|  | 27     GZIP_STREAM_SOURCE_GZIP_WITH_FALLBACK, | 
|  | 28   }; | 
|  | 29 | 
|  | 30   ~GzipStreamSource() override; | 
|  | 31 | 
|  | 32   // Creates a GzipStreamSource. Return nullptr if initialization fails. | 
|  | 33   static std::unique_ptr<GzipStreamSource> Create( | 
|  | 34       std::unique_ptr<StreamSource> previous, | 
|  | 35       GzipStreamSourceMode mode); | 
|  | 36 | 
|  | 37  private: | 
|  | 38   GzipStreamSource(std::unique_ptr<StreamSource> previous, | 
|  | 39                    GzipStreamSourceMode mode); | 
|  | 40   bool Init(); | 
|  | 41   // StreamSource implementation | 
|  | 42   std::string GetTypeAsString() const override; | 
|  | 43   int FilterData(IOBuffer* output_buffer, | 
|  | 44                  size_t output_buffer_size, | 
|  | 45                  DrainableIOBuffer* input_buffer) override; | 
|  | 46 | 
|  | 47   // Synchronous decompressor. This function consumes bytes from |input_buffer| | 
|  | 48   // and decompresses them into |output_buffer| until it consumes all of | 
|  | 49   // |input_buffer|. This decompressor will decompress a zlib stream (either | 
|  | 50   // gzip or deflate) until the zlib reaches EOF, then will pass any further | 
|  | 51   // input through untouched. | 
|  | 52   int Decompress(IOBuffer* output_buffer, | 
|  | 53                  size_t output_buffer_size, | 
|  | 54                  DrainableIOBuffer* input_buffer); | 
|  | 55 | 
|  | 56   // Returns how many bytes are drained from |input_buffer|. | 
|  | 57   size_t Passthrough(char* output_buffer, | 
|  | 58                      size_t output_buffer_size, | 
|  | 59                      DrainableIOBuffer* input_buffer); | 
|  | 60 | 
|  | 61   // Inserts a zlib header to the data stream before calling zlib inflate. | 
|  | 62   // This is used to work around server bugs. | 
|  | 63   // The function returns true on success and false otherwise. | 
|  | 64   bool InsertZlibHeader(); | 
|  | 65 | 
|  | 66   // Returns whether an invalid GZip header has been observed. This method | 
|  | 67   // returns true as soon as an invalid GZip header is observed; it returns | 
|  | 68   // false if a complete, valid header has been observed, or not enough bytes | 
|  | 69   // have been seen to be sure. This method pulls its input from |buffer_|. | 
|  | 70   bool IsGzipHeaderInvalid(DrainableIOBuffer* input_buffer); | 
|  | 71 | 
|  | 72   // Returns whether this stream looks like it could be plain text (ie, not | 
|  | 73   // actually gzipped). Right now this uses an extremely simple heuristic; see | 
|  | 74   // the source for details. This method checks the input in |buffer_|, but does | 
|  | 75   // not drain from it. | 
|  | 76   bool ShouldFallbackToPlain(DrainableIOBuffer* input_buffer); | 
|  | 77 | 
|  | 78   // Skips gzip footer bytes if necessary. Might drain input from |buffer_| if | 
|  | 79   // there are still footer bytes to read. | 
|  | 80   void SkipGzipFooterIfNeeded(DrainableIOBuffer* input_buffer); | 
|  | 81 | 
|  | 82   scoped_refptr<IOBuffer> pending_read_buffer_; | 
|  | 83 | 
|  | 84   // Indicates the type of content decoding the stream source is performing. | 
|  | 85   // This variable is set only once by Init. | 
|  | 86   GzipStreamSourceMode mode_; | 
|  | 87   // The control block of zlib which actually does the decoding. | 
|  | 88   // This data structure is initialized by Init and updated only by | 
|  | 89   // Decompress, with InsertZlibHeader being the exception as a workaround. | 
|  | 90   std::unique_ptr<z_stream> zlib_stream_; | 
|  | 91   // Whether end of stream has been reached. | 
|  | 92   bool zlib_eof_; | 
|  | 93   // A flag used by Decompress to record whether we've successfully added | 
|  | 94   // a zlib header to this stream. | 
|  | 95   bool zlib_header_added_; | 
|  | 96   // Used to parse the gzip header in gzip stream. | 
|  | 97   // It is used when the decoding mode is GZIP_STREAM_SOURCE_GZIP. | 
|  | 98   GZipHeader gzip_header_; | 
|  | 99   // Whether gzip header should be checked for validity. | 
|  | 100   bool should_check_gzip_header_; | 
|  | 101   // Tracks how many bytes of gzip footer are yet to be filtered. | 
|  | 102   size_t gzip_footer_bytes_left_; | 
|  | 103 | 
|  | 104   DISALLOW_COPY_AND_ASSIGN(GzipStreamSource); | 
|  | 105 }; | 
|  | 106 | 
|  | 107 }  // namespace net | 
|  | 108 | 
|  | 109 #endif  // NET_FILTER_GZIP_STREAM_SOURCE_H__ | 
| OLD | NEW | 
|---|