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
|
// Copyright 2012 The Chromium Authors
// 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 <string_view>
#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"
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::List& GetOrCreateList(base::Value::Dict& dictionary,
const std::string& key) {
base::Value::List* list = dictionary.FindList(key);
if (list) {
return *list;
}
return dictionary.Set(key, base::Value::List())->GetList();
}
} // namespace
namespace extensions {
namespace subtle {
void AppendKeyValuePair(const char* key,
base::Value value,
base::Value::List& list) {
base::Value::Dict dictionary;
dictionary.Set(key, std::move(value));
list.Append(std::move(dictionary));
}
} // namespace subtle
UploadDataPresenter::~UploadDataPresenter() = default;
RawDataPresenter::RawDataPresenter() = default;
RawDataPresenter::~RawDataPresenter() = default;
void RawDataPresenter::FeedBytes(std::string_view bytes) {
FeedNextBytes(base::as_byte_span(bytes));
}
void RawDataPresenter::FeedFile(const base::FilePath& path) {
FeedNextFile(path.AsUTF8Unsafe());
}
bool RawDataPresenter::Succeeded() {
return true;
}
std::optional<base::Value> RawDataPresenter::TakeResult() {
return base::Value(std::move(list_));
}
void RawDataPresenter::FeedNextBytes(base::span<const uint8_t> bytes) {
subtle::AppendKeyValuePair(keys::kRequestBodyRawBytesKey, base::Value(bytes),
list_);
}
void RawDataPresenter::FeedNextFile(const std::string& filename) {
// Insert the file path instead of the contents, which may be too large.
subtle::AppendKeyValuePair(keys::kRequestBodyRawFileKey,
base::Value(filename), list_);
}
ParsedDataPresenter::ParsedDataPresenter(
const net::HttpRequestHeaders& request_headers)
: parser_(FormDataParser::Create(request_headers)),
success_(parser_ != nullptr) {
if (success_) {
dictionary_.emplace();
}
}
ParsedDataPresenter::~ParsedDataPresenter() = default;
void ParsedDataPresenter::FeedBytes(std::string_view bytes) {
if (!success_) {
return;
}
if (!parser_->SetSource(bytes)) {
Abort();
return;
}
FormDataParser::Result result;
while (parser_->GetNextNameValue(&result)) {
base::Value::List& list =
GetOrCreateList(dictionary_.value(), 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::optional<base::Value> ParsedDataPresenter::TakeResult() {
if (!success_) {
return std::nullopt;
}
return base::Value(std::move(dictionary_.value()));
}
// 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() != nullptr) {
if (success_) {
dictionary_.emplace();
}
}
void ParsedDataPresenter::Abort() {
success_ = false;
dictionary_.reset();
parser_.reset();
}
} // namespace extensions
|