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

Side by Side Diff: net/tools/content_decoder_tool/content_decoder_tool.cc

Issue 1662763002: [ON HOLD] Implement pull-based design for content decoding (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix compile on mac Created 4 years, 5 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
OLDNEW
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 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/macros.h"
9 #include "base/memory/ptr_util.h"
10 #include "base/strings/string_util.h"
8 #include "net/base/io_buffer.h" 11 #include "net/base/io_buffer.h"
9 #include "net/filter/filter.h" 12 #include "net/base/test_completion_callback.h"
10 #include "net/filter/mock_filter_context.h" 13 #include "net/filter/brotli_stream_source.h"
11 14 #include "net/filter/filter_stream_source.h"
12 using net::Filter; 15 #include "net/filter/gzip_stream_source.h"
16 #include "net/filter/sdch_stream_source.h"
17 #include "net/filter/stream_source.h"
13 18
14 namespace { 19 namespace {
15 20
21 const int kBufferLen = 4096;
22
23 // TODO(xunjielI): refactor to share logic with url_request_http_job.cc
24 const char kDeflate[] = "deflate";
25 const char kGZip[] = "gzip";
26 const char kSdch[] = "sdch";
27 const char kXGZip[] = "x-gzip";
28 const char kBrotli[] = "br";
29
16 // Print the command line help. 30 // Print the command line help.
17 void PrintHelp(const char* command_line_name) { 31 void PrintHelp(const char* command_line_name) {
18 std::cout << command_line_name << " content_encoding [content_encoding]..." 32 std::cout << command_line_name << " content_encoding [content_encoding]..."
19 << std::endl 33 << std::endl
20 << std::endl; 34 << std::endl;
21 std::cout << "Decodes the stdin into the stdout using an content_encoding " 35 std::cout << "Decodes the stdin into the stdout using an content_encoding "
22 << "list given in arguments. This list is expected to be the " 36 << "list given in arguments. This list is expected to be the "
23 << "Content-Encoding HTTP response header's value split by ','." 37 << "Content-Encoding HTTP response header's value split by ','."
24 << std::endl; 38 << std::endl;
25 } 39 }
26 40
41 class StdinStreamSource : public net::StreamSource {
42 public:
43 StdinStreamSource()
44 : net::StreamSource(net::StreamSource::TYPE_NONE),
45 raw_read_buffer_(new net::IOBufferWithSize(kBufferLen)) {}
46 ~StdinStreamSource() override {}
47
48 // StreamSource implementation
49 int Read(net::IOBuffer* dest_buffer,
50 size_t buffer_size,
51 const net::CompletionCallback& callback) override {
52 scoped_refptr<net::IOBuffer> pre_filter_buf =
53 new net::IOBufferWithSize(kBufferLen);
54 if (std::cin.eof()) {
55 return net::OK;
56 }
57 if (std::cin) {
58 std::cin.read(dest_buffer->data(), buffer_size);
59 return std::cin.gcount();
60 }
61 return net::ERR_FAILED;
62 }
63
64 private:
65 scoped_refptr<net::IOBuffer> raw_read_buffer_;
66 DISALLOW_COPY_AND_ASSIGN(StdinStreamSource);
67 };
68
27 } // namespace 69 } // namespace
28 70
29 int main(int argc, char* argv[]) { 71 int main(int argc, char* argv[]) {
30 base::CommandLine::Init(argc, argv); 72 base::CommandLine::Init(argc, argv);
31 const base::CommandLine& command_line = 73 const base::CommandLine& command_line =
32 *base::CommandLine::ForCurrentProcess(); 74 *base::CommandLine::ForCurrentProcess();
33 75
34 std::vector<std::string> content_encodings = command_line.GetArgs(); 76 std::vector<std::string> content_encodings = command_line.GetArgs();
35 if (content_encodings.size() == 0) { 77 if (content_encodings.size() == 0) {
36 PrintHelp(argv[0]); 78 PrintHelp(argv[0]);
37 return 1; 79 return 1;
38 } 80 }
39 81
40 std::vector<Filter::FilterType> filter_types; 82 std::vector<net::StreamSource::SourceType> types;
41 for (const auto& content_encoding : content_encodings) { 83 for (const auto& content_encoding : content_encodings) {
42 Filter::FilterType filter_type = 84 if (base::LowerCaseEqualsASCII(content_encoding, kBrotli)) {
43 Filter::ConvertEncodingToType(content_encoding); 85 types.push_back(net::StreamSource::TYPE_BROTLI);
44 if (filter_type == Filter::FILTER_TYPE_UNSUPPORTED) { 86 } else if (base::LowerCaseEqualsASCII(content_encoding, kDeflate)) {
45 std::cerr << "Unsupported decoder '" << content_encoding << "'." 87 types.push_back(net::StreamSource::TYPE_DEFLATE);
46 << std::endl; 88 } else if (base::LowerCaseEqualsASCII(content_encoding, kGZip) ||
89 base::LowerCaseEqualsASCII(content_encoding, kXGZip)) {
90 types.push_back(net::StreamSource::TYPE_GZIP);
91 } else if (base::LowerCaseEqualsASCII(content_encoding, kSdch)) {
92 types.push_back(net::StreamSource::TYPE_SDCH);
93 }
94 }
95
96 // Keep a raw ptr to the front of the filter chain.
97 StdinStreamSource* stdin_stream_source = new StdinStreamSource();
98 std::unique_ptr<net::StreamSource> previous(
99 base::WrapUnique(stdin_stream_source));
100 for (std::vector<net::StreamSource::SourceType>::reverse_iterator riter =
101 types.rbegin();
102 riter != types.rend(); ++riter) {
103 std::unique_ptr<net::FilterStreamSource> next = nullptr;
104 net::StreamSource::SourceType type = *riter;
105 switch (type) {
106 case net::StreamSource::TYPE_BROTLI:
107 next = CreateBrotliStreamSource(std::move(previous));
108 break;
109 case net::StreamSource::TYPE_SDCH: {
110 next.reset(new net::SdchStreamSource(std::move(previous), nullptr));
111 break;
112 }
113 case net::StreamSource::TYPE_GZIP:
114 next.reset(new net::GzipStreamSource(
115 std::move(previous),
116 net::GzipStreamSource::GZIP_STREAM_SOURCE_GZIP));
117 break;
118 case net::StreamSource::TYPE_DEFLATE:
119 next.reset(new net::GzipStreamSource(
120 std::move(previous),
121 net::GzipStreamSource::GZIP_STREAM_SOURCE_DEFLATE));
122 break;
123 case net::StreamSource::TYPE_GZIP_FALLBACK:
124 next.reset(new net::GzipStreamSource(
125 std::move(previous),
126 net::GzipStreamSource::GZIP_STREAM_SOURCE_GZIP_WITH_FALLBACK));
127 break;
128 default:
129 std::cerr << "Unsupported decoder '" << type << "'." << std::endl;
130 NOTREACHED();
131 return 1;
132 }
133 if (next == nullptr || !next->Init()) {
134 std::cerr << "Couldn't create the decoder." << std::endl;
47 return 1; 135 return 1;
48 } 136 }
49 filter_types.push_back(filter_type); 137 previous = std::move(next);
50 } 138 }
51 139
52 net::MockFilterContext filter_context; 140 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; 141 std::cerr << "Couldn't create the decoder." << std::endl;
56 return 1; 142 return 1;
57 } 143 }
58 144
59 net::IOBuffer* pre_filter_buf = filter->stream_buffer(); 145 while (true) {
60 int pre_filter_buf_len = filter->stream_buffer_size(); 146 scoped_refptr<net::IOBuffer> read_buffer =
61 while (std::cin) { 147 new net::IOBufferWithSize(kBufferLen);
62 std::cin.read(pre_filter_buf->data(), pre_filter_buf_len); 148 net::TestCompletionCallback callback;
63 int pre_filter_data_len = std::cin.gcount(); 149 int bytes_read =
64 filter->FlushStreamBuffer(pre_filter_data_len); 150 previous->Read(read_buffer.get(), kBufferLen, callback.callback());
151 if (bytes_read == net::ERR_IO_PENDING)
152 bytes_read = callback.WaitForResult();
65 153
66 while (true) { 154 if (bytes_read > net::OK) {
67 const int kPostFilterBufLen = 4096; 155 std::cout.write(read_buffer->data(), bytes_read);
68 char post_filter_buf[kPostFilterBufLen]; 156 } else if (bytes_read < net::OK) {
69 int post_filter_data_len = kPostFilterBufLen; 157 std::cerr << "Couldn't decode stdin." << std::endl;
70 Filter::FilterStatus filter_status = 158 return 1;
71 filter->ReadData(post_filter_buf, &post_filter_data_len); 159 } else if (bytes_read == net::OK) {
72 std::cout.write(post_filter_buf, post_filter_data_len); 160 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 } 161 }
80 } 162 }
81 163
82 return 0; 164 return 0;
83 } 165 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698