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 #include <iostream> | |
6 | |
7 #include "base/command_line.h" | |
8 #include "net/base/io_buffer.h" | |
9 #include "net/filter/filter.h" | |
10 #include "net/filter/mock_filter_context.h" | |
11 | |
12 using net::Filter; | |
13 | |
14 namespace { | |
15 | |
16 // Print the command line help. | |
17 void PrintHelp(const char* command_line_name) { | |
18 std::cout << command_line_name << " content_encoding [content_encoding]..." | |
19 << std::endl | |
20 << std::endl; | |
21 std::cout << "Decodes the stdin into the stdout using an content_encoding " | |
22 << "list given in arguments. This list is expected to be the " | |
23 << "Content-Encoding HTTP response header's value split by ','." | |
24 << std::endl; | |
25 } | |
26 | |
27 } // namespace | |
28 | |
29 int main(int argc, char* argv[]) { | |
30 base::CommandLine::Init(argc, argv); | |
31 const base::CommandLine& command_line = | |
32 *base::CommandLine::ForCurrentProcess(); | |
33 | |
34 std::vector<std::string> content_encodings; | |
35 #if defined(OS_WIN) | |
36 base::CommandLine::StringVector wide_args = command_line.GetArgs(); | |
37 for (const auto& arg : wide_args) | |
38 content_encodings.push_back(base::WideToUTF8(arg)); | |
39 #else | |
40 content_encodings = command_line.GetArgs(); | |
41 #endif | |
42 | |
43 if (content_encodings.size() == 0) { | |
44 PrintHelp(argv[0]); | |
45 return 1; | |
46 } | |
47 | |
48 std::vector<Filter::FilterType> filter_types; | |
49 for (const auto& content_encoding : content_encodings) { | |
50 Filter::FilterType filter_type = | |
51 Filter::ConvertEncodingToType(content_encoding); | |
52 if (filter_type == Filter::FILTER_TYPE_UNSUPPORTED) { | |
53 std::cerr << "Unsupported decoder '" << content_encoding << "'." | |
54 << std::endl; | |
55 return 1; | |
56 } | |
57 filter_types.push_back(filter_type); | |
58 } | |
59 | |
60 net::MockFilterContext filter_context; | |
61 scoped_ptr<Filter> filter(Filter::Factory(filter_types, filter_context)); | |
62 if (!filter) { | |
63 std::cerr << "Couldn't create the decoder." << std::endl; | |
64 return 1; | |
65 } | |
66 | |
67 net::IOBuffer* pre_filter_buf = filter->stream_buffer(); | |
68 int pre_filter_buf_len = filter->stream_buffer_size(); | |
69 while (std::cin) { | |
70 std::cin.read(pre_filter_buf->data(), pre_filter_buf_len); | |
71 int pre_filter_data_len = std::cin.gcount(); | |
72 filter->FlushStreamBuffer(pre_filter_data_len); | |
73 | |
74 while (true) { | |
75 int kPostFilterBufLen = 4096; | |
76 char post_filter_buf[kPostFilterBufLen]; | |
77 int post_filter_data_len = kPostFilterBufLen; | |
78 Filter::FilterStatus filter_status = | |
79 filter->ReadData(post_filter_buf, &post_filter_data_len); | |
80 std::cout.write(post_filter_buf, post_filter_data_len); | |
81 if (filter_status == Filter::FILTER_ERROR) { | |
82 std::cerr << "Couldn't decode stdin." << std::endl; | |
83 return 1; | |
84 } else if (filter_status != Filter::FILTER_OK) { | |
85 break; | |
86 } | |
87 } | |
88 } | |
89 | |
90 return 0; | |
91 } | |
OLD | NEW |