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() { | |
18 std::cout << "contentdecodertool encoding_type [encoding_type]*" << std::endl | |
pasko
2016/03/16 12:05:53
let's name it content_encoding (instead of encodin
gabadie
2016/03/16 18:09:16
Done.
pasko
2016/03/16 18:30:59
OK, the 2 out of 3 suggestions were not necessary,
gabadie
2016/03/17 18:13:17
My bad. I have just completely forgotten them.
| |
19 << std::endl; | |
20 std::cout << "Decodes the stdin into the stdout using an encoding_type list " | |
21 << "given in arguments. This list is expected to be the Content-" | |
22 << "Encoding HTTP response header's value splitted by `,`." | |
23 << std::endl; | |
24 } | |
25 | |
26 } // namespace | |
27 | |
28 int main(int argc, char* argv[]) { | |
29 base::CommandLine::Init(argc, argv); | |
30 const base::CommandLine& command_line = | |
31 *base::CommandLine::ForCurrentProcess(); | |
32 | |
33 std::vector<std::string> encoding_type_names; | |
34 #if defined(OS_WIN) | |
35 base::CommandLine::StringVector wide_args = command_line.GetArgs(); | |
36 for (const auto& arg : wide_args) { | |
37 encoding_type_names.push_back(base::WideToUTF8(arg)); | |
38 } | |
39 #else | |
40 encoding_type_names = command_line.GetArgs(); | |
41 #endif | |
42 | |
43 if (encoding_type_names.size() == 0) { | |
44 PrintHelp(); | |
45 return 1; | |
46 } | |
47 | |
48 std::vector<Filter::FilterType> encoding_types; | |
49 for (const auto& encoding_type_name : encoding_type_names) { | |
50 Filter::FilterType encode_type = | |
pasko
2016/03/16 12:05:53
I think |filter_type| suits better as a name
gabadie
2016/03/16 18:09:17
Done.
| |
51 Filter::ConvertEncodingToType(encoding_type_name); | |
52 if (encode_type == Filter::FILTER_TYPE_UNSUPPORTED) { | |
53 std::cerr << "Unsupported decoder `" << encoding_type_name << "`." | |
54 << std::endl; | |
55 return 1; | |
56 } | |
57 encoding_types.push_back(encode_type); | |
58 } | |
59 | |
60 net::MockFilterContext filter_context; | |
61 scoped_ptr<Filter> filter(Filter::Factory(encoding_types, filter_context)); | |
62 if (!filter) { | |
63 std::cerr << "Couldn't create the decoder." << std::endl; | |
64 return 1; | |
65 } | |
66 | |
67 net::IOBuffer* encode_buffer = filter->stream_buffer(); | |
68 int encode_buffer_size = filter->stream_buffer_size(); | |
pasko
2016/03/16 12:05:53
I do not understand why the word 'encode' is used.
gabadie
2016/03/16 18:09:16
Done.
| |
69 while (std::cin) { | |
70 std::cin.read(encode_buffer->data(), encode_buffer_size); | |
71 int encode_read_size = std::cin.gcount(); | |
72 filter->FlushStreamBuffer(encode_read_size); | |
73 | |
74 while (true) { | |
75 int kDecodeBufferSize = 4096; | |
76 char decode_buffer[kDecodeBufferSize]; | |
77 int decode_size = kDecodeBufferSize; | |
78 Filter::FilterStatus filter_status = | |
79 filter->ReadData(decode_buffer, &decode_size); | |
80 std::cout.write(decode_buffer, decode_size); | |
81 if (filter_status == Filter::FILTER_ERROR) { | |
82 std::cerr << "Couldn't decode stdin properly." << std::endl; | |
pasko
2016/03/16 12:05:53
nit: s/ properly//
gabadie
2016/03/16 18:09:17
Done.
| |
83 return 1; | |
84 } else if (filter_status != Filter::FILTER_OK) { | |
85 break; | |
86 } | |
87 } | |
88 } | |
89 | |
90 return 0; | |
91 } | |
OLD | NEW |