File: content_decoder_tool.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (117 lines) | stat: -rw-r--r-- 3,770 bytes parent folder | download | duplicates (6)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/tools/content_decoder_tool/content_decoder_tool.h"

#include <memory>
#include <utility>

#include "base/containers/adapters.h"
#include "base/logging.h"
#include "base/strings/string_util.h"
#include "net/base/completion_once_callback.h"
#include "net/base/io_buffer.h"
#include "net/base/test_completion_callback.h"
#include "net/filter/brotli_source_stream.h"
#include "net/filter/gzip_source_stream.h"
#include "net/filter/source_stream.h"

namespace net {

namespace {

const int kBufferLen = 4096;

const char kDeflate[] = "deflate";
const char kGZip[] = "gzip";
const char kXGZip[] = "x-gzip";
const char kBrotli[] = "br";

class StdinSourceStream : public SourceStream {
 public:
  explicit StdinSourceStream(std::istream* input_stream)
      : SourceStream(SourceStreamType::kNone), input_stream_(input_stream) {}

  StdinSourceStream(const StdinSourceStream&) = delete;
  StdinSourceStream& operator=(const StdinSourceStream&) = delete;

  ~StdinSourceStream() override = default;

  // SourceStream implementation.
  int Read(IOBuffer* dest_buffer,
           int buffer_size,
           CompletionOnceCallback callback) override {
    if (input_stream_->eof())
      return OK;
    if (input_stream_) {
      input_stream_->read(dest_buffer->data(), buffer_size);
      int bytes = input_stream_->gcount();
      return bytes;
    }
    return ERR_FAILED;
  }

  std::string Description() const override { return ""; }

  bool MayHaveMoreBytes() const override { return true; }

 private:
  std::istream* input_stream_;
};

}  // namespace

// static
bool ContentDecoderToolProcessInput(std::vector<std::string> content_encodings,
                                    std::istream* input_stream,
                                    std::ostream* output_stream) {
  std::unique_ptr<SourceStream> upstream(
      std::make_unique<StdinSourceStream>(input_stream));
  for (const auto& content_encoding : base::Reversed(content_encodings)) {
    std::unique_ptr<SourceStream> downstream;
    if (base::EqualsCaseInsensitiveASCII(content_encoding, kBrotli)) {
      downstream = CreateBrotliSourceStream(std::move(upstream));
    } else if (base::EqualsCaseInsensitiveASCII(content_encoding, kDeflate)) {
      downstream = GzipSourceStream::Create(std::move(upstream),
                                            SourceStreamType::kDeflate);
    } else if (base::EqualsCaseInsensitiveASCII(content_encoding, kGZip) ||
               base::EqualsCaseInsensitiveASCII(content_encoding, kXGZip)) {
      downstream = GzipSourceStream::Create(std::move(upstream),
                                            SourceStreamType::kGzip);
    } else {
      LOG(ERROR) << "Unsupported decoder '" << content_encoding << "'.";
      return false;
    }
    if (downstream == nullptr) {
      LOG(ERROR) << "Couldn't create the decoder.";
      return false;
    }
    upstream = std::move(downstream);
  }
  if (!upstream) {
    LOG(ERROR) << "Couldn't create the decoder.";
    return false;
  }
  scoped_refptr<IOBuffer> read_buffer =
      base::MakeRefCounted<IOBufferWithSize>(kBufferLen);
  while (true) {
    TestCompletionCallback callback;
    int bytes_read =
        upstream->Read(read_buffer.get(), kBufferLen, callback.callback());
    if (bytes_read == ERR_IO_PENDING)
      bytes_read = callback.WaitForResult();

    if (bytes_read < 0) {
      LOG(ERROR) << "Couldn't decode stdin.";
      return false;
    }
    output_stream->write(read_buffer->data(), bytes_read);
    // If EOF is read, break out the while loop.
    if (bytes_read == 0)
      break;
  }
  return true;
}

}  // namespace net