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

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

Issue 1767653002: Implement content_decoder_tool.cc to decode offline any resources. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressing comments I have miss in the previous revision. Created 4 years, 9 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
« no previous file with comments | « net/net.gyp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 splitted by `,`."
gavinp 2016/03/22 14:56:58 Nit: "split" not "splitted" and ' not `.
gabadie 2016/03/22 16:20:42 Done.
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) {
gavinp 2016/03/22 14:56:58 Nit: braces not needed.
gabadie 2016/03/22 16:20:43 Done.
38 content_encodings.push_back(base::WideToUTF8(arg));
39 }
40 #else
41 content_encodings = command_line.GetArgs();
42 #endif
43
44 if (content_encodings.size() == 0) {
45 PrintHelp(argv[0]);
46 return 1;
47 }
48
49 std::vector<Filter::FilterType> filter_types;
50 for (const auto& content_encoding : content_encodings) {
51 Filter::FilterType filter_type =
52 Filter::ConvertEncodingToType(content_encoding);
53 if (filter_type == Filter::FILTER_TYPE_UNSUPPORTED) {
54 std::cerr << "Unsupported decoder `" << content_encoding << "`."
gavinp 2016/03/22 14:56:58 nit: I think using ` here is a bit odd. Would ' ma
gabadie 2016/03/22 16:20:43 Done.
55 << std::endl;
56 return 1;
57 }
58 filter_types.push_back(filter_type);
59 }
60
61 net::MockFilterContext filter_context;
62 scoped_ptr<Filter> filter(Filter::Factory(filter_types, filter_context));
63 if (!filter) {
64 std::cerr << "Couldn't create the decoder." << std::endl;
65 return 1;
66 }
67
68 net::IOBuffer* pre_filter_buf = filter->stream_buffer();
69 int pre_filter_buf_len = filter->stream_buffer_size();
70 while (std::cin) {
71 std::cin.read(pre_filter_buf->data(), pre_filter_buf_len);
72 int pre_filter_data_len = std::cin.gcount();
73 filter->FlushStreamBuffer(pre_filter_data_len);
74
75 while (true) {
76 int kPostFilterBufLen = 4096;
77 char post_filter_buf[kPostFilterBufLen];
78 int post_filter_data_len = kPostFilterBufLen;
79 Filter::FilterStatus filter_status =
80 filter->ReadData(post_filter_buf, &post_filter_data_len);
81 std::cout.write(post_filter_buf, post_filter_data_len);
82 if (filter_status == Filter::FILTER_ERROR) {
83 std::cerr << "Couldn't decode stdin." << std::endl;
84 return 1;
85 } else if (filter_status != Filter::FILTER_OK) {
86 break;
87 }
88 }
89 }
90
91 return 0;
92 }
OLDNEW
« no previous file with comments | « net/net.gyp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698