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
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/embedder_support/android/util/web_resource_response.h"
#include <memory>
#include <string>
#include <string_view>
#include <vector>
#include "base/android/jni_android.h"
#include "base/android/jni_array.h"
#include "base/android/jni_string.h"
#include "base/containers/flat_map.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "components/embedder_support/android/util/input_stream.h"
#include "net/http/http_response_headers.h"
#include "net/url_request/url_request.h"
#include "net/url_request/url_request_job.h"
#include "third_party/jni_zero/default_conversions.h"
// Must come after all headers that specialize FromJniType() / ToJniType().
#include "components/embedder_support/android/util_jni_headers/WebResourceResponseInfo_jni.h"
using base::android::AppendJavaStringArrayToStringVector;
using base::android::ConvertJavaStringToUTF8;
using base::android::ScopedJavaLocalRef;
namespace embedder_support {
namespace {
// We include null chars to prevent collisions with any valid cookie header
// names and values.
// LINT.IfChange(MultiCookieKeys)
constexpr char kCookieMultiHeaderNameChars[] = "\0Set-Cookie-Multivalue\0";
constexpr char kCookieMultiHeaderValueSeparatorChars[] = "\0";
// LINT.ThenChange(/android_webview/support_library/boundary_interfaces/src/org/chromium/support_lib_boundary/WebViewProviderFactoryBoundaryInterface.java:MultiCookieKeys)
// std::string_literals are banned by the style guide, so this code manually
// constructs the string_view instances with explicit length to ensure they
// contain the \0 characters. Subtracting 1 to remove the compiler-inserted null
// terminator.
constexpr std::string_view kCookieMultiHeaderName(
kCookieMultiHeaderNameChars,
sizeof(kCookieMultiHeaderNameChars) - 1);
constexpr std::string_view kCookieMultiHeaderValueSeparator(
kCookieMultiHeaderValueSeparatorChars,
sizeof(kCookieMultiHeaderValueSeparatorChars) - 1);
} // namespace
WebResourceResponse::WebResourceResponse(
const base::android::JavaRef<jobject>& obj)
: java_object_(obj) {}
WebResourceResponse::~WebResourceResponse() = default;
bool WebResourceResponse::HasInputStream(JNIEnv* env) const {
return Java_WebResourceResponseInfo_hasInputStream(env, java_object_);
}
std::unique_ptr<InputStream> WebResourceResponse::GetInputStream(JNIEnv* env) {
return Java_WebResourceResponseInfo_transferStreamToNative(env, java_object_);
}
bool WebResourceResponse::GetMimeType(JNIEnv* env,
std::string* mime_type) const {
std::optional<std::string> opt_mime_type =
Java_WebResourceResponseInfo_getMimeType(env, java_object_);
if (!opt_mime_type) {
return false;
}
*mime_type = *opt_mime_type;
return true;
}
bool WebResourceResponse::GetCharset(JNIEnv* env, std::string* charset) const {
std::optional<std::string> opt_charset =
Java_WebResourceResponseInfo_getCharset(env, java_object_);
if (!opt_charset) {
return false;
}
*charset = *opt_charset;
return true;
}
bool WebResourceResponse::GetStatusInfo(JNIEnv* env,
int* status_code,
std::string* reason_phrase) const {
int status = Java_WebResourceResponseInfo_getStatusCode(env, java_object_);
std::optional<std::string> opt_reason_phrase =
Java_WebResourceResponseInfo_getReasonPhrase(env, java_object_);
if (status < 100 || status >= 600 || !opt_reason_phrase) {
return false;
}
*status_code = status;
*reason_phrase = *opt_reason_phrase;
return true;
}
bool WebResourceResponse::GetResponseHeaders(
JNIEnv* env,
net::HttpResponseHeaders* headers) const {
base::flat_map<std::string, std::string> response_headers =
Java_WebResourceResponseInfo_getResponseHeaders(env, java_object_);
bool used_multi_cookie_header = false;
bool did_modify_headers = false;
for (const auto& [name, value] : response_headers) {
if (name == kCookieMultiHeaderName) {
used_multi_cookie_header = true;
// The Set-Cookie header is special, in that it cannot be easily joined
// with comma separation. Instead, we allow the client to provide multiple
// values separated by the null character, which is not a legal value in
// header values, so we can then split it here.
//
// We do this because the API uses a simple string->string map to supply
// response header values, so we cannot cleanly send more than one value
// for each key.
std::vector<std::string_view> cookies = base::SplitStringPiece(
value, kCookieMultiHeaderValueSeparator, base::KEEP_WHITESPACE,
base::SPLIT_WANT_NONEMPTY);
for (const auto& cookie : cookies) {
headers->AddCookie(cookie);
}
did_modify_headers |= !cookies.empty();
} else {
headers->AddHeader(name, value);
did_modify_headers = true;
}
}
base::UmaHistogramBoolean(
"Android.WebView.ShouldInterceptRequest.DidIncludeMultiCookieHeader",
used_multi_cookie_header);
return did_modify_headers;
}
} // namespace embedder_support
|