summaryrefslogtreecommitdiff
path: root/chromium/extensions/browser/api/web_request/upload_data_presenter.cc
blob: 017234ad63828f5b3a8f85d6e29e73a8f07df4aa (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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright (c) 2012 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 "extensions/browser/api/web_request/upload_data_presenter.h"

#include <utility>

#include "base/containers/span.h"
#include "base/files/file_path.h"
#include "base/memory/ptr_util.h"
#include "base/strings/string_util.h"
#include "base/values.h"
#include "extensions/browser/api/web_request/form_data_parser.h"
#include "extensions/browser/api/web_request/web_request_api_constants.h"
#include "net/base/upload_file_element_reader.h"

using base::DictionaryValue;
using base::ListValue;
using base::Value;

namespace keys = extension_web_request_api_constants;

namespace {

// Takes |dictionary| of <string, list of strings> pairs, and gets the list
// for |key|, creating it if necessary.
base::Value* GetOrCreateList(base::DictionaryValue* dictionary,
                             const std::string& key) {
  base::Value* list = dictionary->FindKeyOfType(key, base::Value::Type::LIST);
  if (list)
    return list;
  return dictionary->SetKey(key, base::Value(base::Value::Type::LIST));
}

}  // namespace

namespace extensions {

namespace subtle {

void AppendKeyValuePair(const char* key,
                        std::unique_ptr<base::Value> value,
                        base::ListValue* list) {
  std::unique_ptr<base::DictionaryValue> dictionary(new base::DictionaryValue);
  dictionary->SetKey(key, base::Value::FromUniquePtrValue(std::move(value)));
  list->Append(std::move(dictionary));
}

}  // namespace subtle

UploadDataPresenter::~UploadDataPresenter() {}

RawDataPresenter::RawDataPresenter()
  : success_(true),
    list_(new base::ListValue) {
}
RawDataPresenter::~RawDataPresenter() {}

void RawDataPresenter::FeedBytes(base::StringPiece bytes) {
  if (!success_)
    return;

  FeedNextBytes(bytes.data(), bytes.size());
}

void RawDataPresenter::FeedFile(const base::FilePath& path) {
  if (!success_)
    return;

  FeedNextFile(path.AsUTF8Unsafe());
}

bool RawDataPresenter::Succeeded() {
  return success_;
}

std::unique_ptr<base::Value> RawDataPresenter::Result() {
  if (!success_)
    return nullptr;

  return std::move(list_);
}

void RawDataPresenter::FeedNextBytes(const char* bytes, size_t size) {
  subtle::AppendKeyValuePair(keys::kRequestBodyRawBytesKey,
                             base::Value::ToUniquePtrValue(base::Value(
                                 base::as_bytes(base::make_span(bytes, size)))),
                             list_.get());
}

void RawDataPresenter::FeedNextFile(const std::string& filename) {
  // Insert the file path instead of the contents, which may be too large.
  subtle::AppendKeyValuePair(keys::kRequestBodyRawFileKey,
                             std::make_unique<base::Value>(filename),
                             list_.get());
}

ParsedDataPresenter::ParsedDataPresenter(
    const net::HttpRequestHeaders& request_headers)
    : parser_(FormDataParser::Create(request_headers)),
      success_(parser_ != nullptr),
      dictionary_(success_ ? new base::DictionaryValue() : nullptr) {}

ParsedDataPresenter::~ParsedDataPresenter() {}

void ParsedDataPresenter::FeedBytes(base::StringPiece bytes) {
  if (!success_)
    return;

  if (!parser_->SetSource(bytes)) {
    Abort();
    return;
  }

  FormDataParser::Result result;
  while (parser_->GetNextNameValue(&result)) {
    base::Value* list = GetOrCreateList(dictionary_.get(), result.name());
    list->Append(result.take_value());
  }
}

void ParsedDataPresenter::FeedFile(const base::FilePath& path) {}

bool ParsedDataPresenter::Succeeded() {
  if (success_ && !parser_->AllDataReadOK())
    Abort();
  return success_;
}

std::unique_ptr<base::Value> ParsedDataPresenter::Result() {
  if (!success_)
    return nullptr;

  return std::move(dictionary_);
}

// static
std::unique_ptr<ParsedDataPresenter> ParsedDataPresenter::CreateForTests() {
  return base::WrapUnique(
      new ParsedDataPresenter("application/x-www-form-urlencoded"));
}

ParsedDataPresenter::ParsedDataPresenter(const std::string& form_type)
  : parser_(FormDataParser::CreateFromContentTypeHeader(&form_type)),
    success_(parser_.get() != NULL),
    dictionary_(success_ ? new base::DictionaryValue() : NULL) {
}

void ParsedDataPresenter::Abort() {
  success_ = false;
  dictionary_.reset();
  parser_.reset();
}

}  // namespace extensions