summaryrefslogtreecommitdiffstats
path: root/net/tools/content_decoder_tool/content_decoder_tool.cc
blob: 89646ebb6131941f4030d63ef2e9bc0604343dca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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(const char* command_line_name) {
  std::cout << command_line_name << " content_encoding [content_encoding]..."
            << std::endl
            << std::endl;
  std::cout << "Decodes the stdin into the stdout using an content_encoding "
            << "list given in arguments. This list is expected to be the "
            << "Content-Encoding HTTP response header's value split 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> content_encodings;
#if defined(OS_WIN)
  base::CommandLine::StringVector wide_args = command_line.GetArgs();
  for (const auto& arg : wide_args)
    content_encodings.push_back(base::WideToUTF8(arg));
#else
  content_encodings = command_line.GetArgs();
#endif

  if (content_encodings.size() == 0) {
    PrintHelp(argv[0]);
    return 1;
  }

  std::vector<Filter::FilterType> filter_types;
  for (const auto& content_encoding : content_encodings) {
    Filter::FilterType filter_type =
        Filter::ConvertEncodingToType(content_encoding);
    if (filter_type == Filter::FILTER_TYPE_UNSUPPORTED) {
      std::cerr << "Unsupported decoder '" << content_encoding << "'."
                << std::endl;
      return 1;
    }
    filter_types.push_back(filter_type);
  }

  net::MockFilterContext filter_context;
  scoped_ptr<Filter> filter(Filter::Factory(filter_types, filter_context));
  if (!filter) {
    std::cerr << "Couldn't create the decoder." << std::endl;
    return 1;
  }

  net::IOBuffer* pre_filter_buf = filter->stream_buffer();
  int pre_filter_buf_len = filter->stream_buffer_size();
  while (std::cin) {
    std::cin.read(pre_filter_buf->data(), pre_filter_buf_len);
    int pre_filter_data_len = std::cin.gcount();
    filter->FlushStreamBuffer(pre_filter_data_len);

    while (true) {
      int kPostFilterBufLen = 4096;
      char post_filter_buf[kPostFilterBufLen];
      int post_filter_data_len = kPostFilterBufLen;
      Filter::FilterStatus filter_status =
          filter->ReadData(post_filter_buf, &post_filter_data_len);
      std::cout.write(post_filter_buf, post_filter_data_len);
      if (filter_status == Filter::FILTER_ERROR) {
        std::cerr << "Couldn't decode stdin." << std::endl;
        return 1;
      } else if (filter_status != Filter::FILTER_OK) {
        break;
      }
    }
  }

  return 0;
}