| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <iostream> | 5 #include <iostream> |
| 6 #include <string> |
| 6 | 7 |
| 7 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/macros.h" |
| 10 #include "base/memory/ptr_util.h" |
| 11 #include "base/strings/string_util.h" |
| 8 #include "net/base/io_buffer.h" | 12 #include "net/base/io_buffer.h" |
| 9 #include "net/filter/filter.h" | 13 #include "net/base/test_completion_callback.h" |
| 10 #include "net/filter/mock_filter_context.h" | 14 #include "net/filter/brotli_source_stream.h" |
| 11 | 15 #include "net/filter/filter_source_stream.h" |
| 12 using net::Filter; | 16 #include "net/filter/gzip_source_stream.h" |
| 17 #include "net/filter/sdch_source_stream.h" |
| 18 #include "net/filter/source_stream.h" |
| 13 | 19 |
| 14 namespace { | 20 namespace { |
| 15 | 21 |
| 22 const int kBufferLen = 4096; |
| 23 |
| 24 // TODO(xunjielI): refactor to share logic with url_request_http_job.cc |
| 25 const char kDeflate[] = "deflate"; |
| 26 const char kGZip[] = "gzip"; |
| 27 const char kSdch[] = "sdch"; |
| 28 const char kXGZip[] = "x-gzip"; |
| 29 const char kBrotli[] = "br"; |
| 30 |
| 16 // Print the command line help. | 31 // Print the command line help. |
| 17 void PrintHelp(const char* command_line_name) { | 32 void PrintHelp(const char* command_line_name) { |
| 18 std::cout << command_line_name << " content_encoding [content_encoding]..." | 33 std::cout << command_line_name << " content_encoding [content_encoding]..." |
| 19 << std::endl | 34 << std::endl |
| 20 << std::endl; | 35 << std::endl; |
| 21 std::cout << "Decodes the stdin into the stdout using an content_encoding " | 36 std::cout << "Decodes the stdin into the stdout using an content_encoding " |
| 22 << "list given in arguments. This list is expected to be the " | 37 << "list given in arguments. This list is expected to be the " |
| 23 << "Content-Encoding HTTP response header's value split by ','." | 38 << "Content-Encoding HTTP response header's value split by ','." |
| 24 << std::endl; | 39 << std::endl; |
| 25 } | 40 } |
| 26 | 41 |
| 42 class StdinSourceStream : public net::SourceStream { |
| 43 public: |
| 44 StdinSourceStream() |
| 45 : net::SourceStream(net::SourceStream::TYPE_NONE), |
| 46 raw_read_buffer_(new net::IOBufferWithSize(kBufferLen)) {} |
| 47 ~StdinSourceStream() override {} |
| 48 |
| 49 // SourceStream implementation |
| 50 int Read(net::IOBuffer* dest_buffer, |
| 51 size_t buffer_size, |
| 52 const net::CompletionCallback& callback) override { |
| 53 scoped_refptr<net::IOBuffer> pre_filter_buf = |
| 54 new net::IOBufferWithSize(kBufferLen); |
| 55 if (std::cin.eof()) { |
| 56 return net::OK; |
| 57 } |
| 58 if (std::cin) { |
| 59 std::cin.read(dest_buffer->data(), buffer_size); |
| 60 return std::cin.gcount(); |
| 61 } |
| 62 return net::ERR_FAILED; |
| 63 } |
| 64 |
| 65 std::string OrderedTypeStringList() const override { return ""; } |
| 66 |
| 67 private: |
| 68 scoped_refptr<net::IOBuffer> raw_read_buffer_; |
| 69 DISALLOW_COPY_AND_ASSIGN(StdinSourceStream); |
| 70 }; |
| 71 |
| 27 } // namespace | 72 } // namespace |
| 28 | 73 |
| 29 int main(int argc, char* argv[]) { | 74 int main(int argc, char* argv[]) { |
| 30 base::CommandLine::Init(argc, argv); | 75 base::CommandLine::Init(argc, argv); |
| 31 const base::CommandLine& command_line = | 76 const base::CommandLine& command_line = |
| 32 *base::CommandLine::ForCurrentProcess(); | 77 *base::CommandLine::ForCurrentProcess(); |
| 33 | 78 |
| 34 std::vector<std::string> content_encodings = command_line.GetArgs(); | 79 std::vector<std::string> content_encodings = command_line.GetArgs(); |
| 35 if (content_encodings.size() == 0) { | 80 if (content_encodings.size() == 0) { |
| 36 PrintHelp(argv[0]); | 81 PrintHelp(argv[0]); |
| 37 return 1; | 82 return 1; |
| 38 } | 83 } |
| 39 | 84 |
| 40 std::vector<Filter::FilterType> filter_types; | 85 std::vector<net::SourceStream::SourceType> types; |
| 41 for (const auto& content_encoding : content_encodings) { | 86 for (const auto& content_encoding : content_encodings) { |
| 42 Filter::FilterType filter_type = | 87 if (base::LowerCaseEqualsASCII(content_encoding, kBrotli)) { |
| 43 Filter::ConvertEncodingToType(content_encoding); | 88 types.push_back(net::SourceStream::TYPE_BROTLI); |
| 44 if (filter_type == Filter::FILTER_TYPE_UNSUPPORTED) { | 89 } else if (base::LowerCaseEqualsASCII(content_encoding, kDeflate)) { |
| 45 std::cerr << "Unsupported decoder '" << content_encoding << "'." | 90 types.push_back(net::SourceStream::TYPE_DEFLATE); |
| 46 << std::endl; | 91 } else if (base::LowerCaseEqualsASCII(content_encoding, kGZip) || |
| 92 base::LowerCaseEqualsASCII(content_encoding, kXGZip)) { |
| 93 types.push_back(net::SourceStream::TYPE_GZIP); |
| 94 } else if (base::LowerCaseEqualsASCII(content_encoding, kSdch)) { |
| 95 types.push_back(net::SourceStream::TYPE_SDCH); |
| 96 } |
| 97 } |
| 98 |
| 99 // Keep a raw ptr to the front of the filter chain. |
| 100 StdinSourceStream* stdin_source_stream = new StdinSourceStream(); |
| 101 std::unique_ptr<net::SourceStream> previous( |
| 102 base::WrapUnique(stdin_source_stream)); |
| 103 for (std::vector<net::SourceStream::SourceType>::reverse_iterator riter = |
| 104 types.rbegin(); |
| 105 riter != types.rend(); ++riter) { |
| 106 std::unique_ptr<net::FilterSourceStream> next = nullptr; |
| 107 net::SourceStream::SourceType type = *riter; |
| 108 switch (type) { |
| 109 case net::SourceStream::TYPE_BROTLI: |
| 110 next = CreateBrotliSourceStream(std::move(previous)); |
| 111 break; |
| 112 case net::SourceStream::TYPE_SDCH: { |
| 113 next.reset(new net::SdchSourceStream(std::move(previous), nullptr)); |
| 114 break; |
| 115 } |
| 116 case net::SourceStream::TYPE_GZIP: |
| 117 next = net::GzipSourceStream::Create( |
| 118 std::move(previous), |
| 119 net::GzipSourceStream::GZIP_SOURCE_STREAM_GZIP); |
| 120 break; |
| 121 case net::SourceStream::TYPE_DEFLATE: |
| 122 next = net::GzipSourceStream::Create( |
| 123 std::move(previous), |
| 124 net::GzipSourceStream::GZIP_SOURCE_STREAM_DEFLATE); |
| 125 break; |
| 126 case net::SourceStream::TYPE_GZIP_FALLBACK: |
| 127 next = net::GzipSourceStream::Create( |
| 128 std::move(previous), |
| 129 net::GzipSourceStream::GZIP_SOURCE_STREAM_GZIP_WITH_FALLBACK); |
| 130 break; |
| 131 default: |
| 132 std::cerr << "Unsupported decoder '" << type << "'." << std::endl; |
| 133 NOTREACHED(); |
| 134 return 1; |
| 135 } |
| 136 if (next == nullptr) { |
| 137 std::cerr << "Couldn't create the decoder." << std::endl; |
| 47 return 1; | 138 return 1; |
| 48 } | 139 } |
| 49 filter_types.push_back(filter_type); | 140 previous = std::move(next); |
| 50 } | 141 } |
| 51 | 142 |
| 52 net::MockFilterContext filter_context; | 143 if (!previous) { |
| 53 std::unique_ptr<Filter> filter(Filter::Factory(filter_types, filter_context)); | |
| 54 if (!filter) { | |
| 55 std::cerr << "Couldn't create the decoder." << std::endl; | 144 std::cerr << "Couldn't create the decoder." << std::endl; |
| 56 return 1; | 145 return 1; |
| 57 } | 146 } |
| 58 | 147 |
| 59 net::IOBuffer* pre_filter_buf = filter->stream_buffer(); | 148 while (true) { |
| 60 int pre_filter_buf_len = filter->stream_buffer_size(); | 149 scoped_refptr<net::IOBuffer> read_buffer = |
| 61 while (std::cin) { | 150 new net::IOBufferWithSize(kBufferLen); |
| 62 std::cin.read(pre_filter_buf->data(), pre_filter_buf_len); | 151 net::TestCompletionCallback callback; |
| 63 int pre_filter_data_len = std::cin.gcount(); | 152 int bytes_read = |
| 64 filter->FlushStreamBuffer(pre_filter_data_len); | 153 previous->Read(read_buffer.get(), kBufferLen, callback.callback()); |
| 154 if (bytes_read == net::ERR_IO_PENDING) |
| 155 bytes_read = callback.WaitForResult(); |
| 65 | 156 |
| 66 while (true) { | 157 if (bytes_read > net::OK) { |
| 67 const int kPostFilterBufLen = 4096; | 158 std::cout.write(read_buffer->data(), bytes_read); |
| 68 char post_filter_buf[kPostFilterBufLen]; | 159 } else if (bytes_read < net::OK) { |
| 69 int post_filter_data_len = kPostFilterBufLen; | 160 std::cerr << "Couldn't decode stdin." << std::endl; |
| 70 Filter::FilterStatus filter_status = | 161 return 1; |
| 71 filter->ReadData(post_filter_buf, &post_filter_data_len); | 162 } else if (bytes_read == net::OK) { |
| 72 std::cout.write(post_filter_buf, post_filter_data_len); | 163 return 0; |
| 73 if (filter_status == Filter::FILTER_ERROR) { | |
| 74 std::cerr << "Couldn't decode stdin." << std::endl; | |
| 75 return 1; | |
| 76 } else if (filter_status != Filter::FILTER_OK) { | |
| 77 break; | |
| 78 } | |
| 79 } | 164 } |
| 80 } | 165 } |
| 81 | 166 |
| 82 return 0; | 167 return 0; |
| 83 } | 168 } |
| OLD | NEW |