Index: net/tools/contentdecodertool/contentdecodertool.cc |
diff --git a/net/tools/contentdecodertool/contentdecodertool.cc b/net/tools/contentdecodertool/contentdecodertool.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..eee4b702035ca6a72c1094fb7e72b29815c70b8d |
--- /dev/null |
+++ b/net/tools/contentdecodertool/contentdecodertool.cc |
@@ -0,0 +1,91 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include <iostream> |
+ |
+#include "base/command_line.h" |
+#include "net/base/io_buffer.h" |
+#include "net/filter/filter.h" |
+#include "net/filter/mock_filter_context.h" |
+ |
+using net::Filter; |
+ |
+namespace { |
+ |
+// Print the command line help. |
+void PrintHelp() { |
+ 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.
|
+ << std::endl; |
+ std::cout << "Decodes the stdin into the stdout using an encoding_type list " |
+ << "given in arguments. This list is expected to be the Content-" |
+ << "Encoding HTTP response header's value splitted by `,`." |
+ << std::endl; |
+} |
+ |
+} // namespace |
+ |
+int main(int argc, char* argv[]) { |
+ base::CommandLine::Init(argc, argv); |
+ const base::CommandLine& command_line = |
+ *base::CommandLine::ForCurrentProcess(); |
+ |
+ std::vector<std::string> encoding_type_names; |
+#if defined(OS_WIN) |
+ base::CommandLine::StringVector wide_args = command_line.GetArgs(); |
+ for (const auto& arg : wide_args) { |
+ encoding_type_names.push_back(base::WideToUTF8(arg)); |
+ } |
+#else |
+ encoding_type_names = command_line.GetArgs(); |
+#endif |
+ |
+ if (encoding_type_names.size() == 0) { |
+ PrintHelp(); |
+ return 1; |
+ } |
+ |
+ std::vector<Filter::FilterType> encoding_types; |
+ for (const auto& encoding_type_name : encoding_type_names) { |
+ 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.
|
+ Filter::ConvertEncodingToType(encoding_type_name); |
+ if (encode_type == Filter::FILTER_TYPE_UNSUPPORTED) { |
+ std::cerr << "Unsupported decoder `" << encoding_type_name << "`." |
+ << std::endl; |
+ return 1; |
+ } |
+ encoding_types.push_back(encode_type); |
+ } |
+ |
+ net::MockFilterContext filter_context; |
+ scoped_ptr<Filter> filter(Filter::Factory(encoding_types, filter_context)); |
+ if (!filter) { |
+ std::cerr << "Couldn't create the decoder." << std::endl; |
+ return 1; |
+ } |
+ |
+ net::IOBuffer* encode_buffer = filter->stream_buffer(); |
+ 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.
|
+ while (std::cin) { |
+ std::cin.read(encode_buffer->data(), encode_buffer_size); |
+ int encode_read_size = std::cin.gcount(); |
+ filter->FlushStreamBuffer(encode_read_size); |
+ |
+ while (true) { |
+ int kDecodeBufferSize = 4096; |
+ char decode_buffer[kDecodeBufferSize]; |
+ int decode_size = kDecodeBufferSize; |
+ Filter::FilterStatus filter_status = |
+ filter->ReadData(decode_buffer, &decode_size); |
+ std::cout.write(decode_buffer, decode_size); |
+ if (filter_status == Filter::FILTER_ERROR) { |
+ 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.
|
+ return 1; |
+ } else if (filter_status != Filter::FILTER_OK) { |
+ break; |
+ } |
+ } |
+ } |
+ |
+ return 0; |
+} |