2012-04-03 03:34:16 +02:00
|
|
|
// Copyright (c) 2012 The Chromium Embedded Framework Authors. All rights
|
|
|
|
// reserved. Use of this source code is governed by a BSD-style license that
|
|
|
|
// can be found in the LICENSE file.
|
|
|
|
|
2024-04-30 17:45:07 +02:00
|
|
|
#include "cef/libcef/common/response_impl.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2012-06-19 18:29:49 +02:00
|
|
|
#include <string>
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
#include "base/logging.h"
|
2024-05-11 17:48:38 +02:00
|
|
|
#include "base/memory/raw_ptr.h"
|
2019-04-24 04:50:25 +02:00
|
|
|
#include "base/strings/string_util.h"
|
2024-04-30 17:45:07 +02:00
|
|
|
#include "cef/libcef/common/net/http_header_utils.h"
|
|
|
|
#include "cef/libcef/common/net_service/net_service_util.h"
|
2012-06-19 18:29:49 +02:00
|
|
|
#include "net/http/http_request_headers.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
#include "net/http/http_response_headers.h"
|
2018-04-19 17:44:42 +02:00
|
|
|
#include "third_party/blink/public/platform/web_http_header_visitor.h"
|
|
|
|
#include "third_party/blink/public/platform/web_string.h"
|
|
|
|
#include "third_party/blink/public/platform/web_url.h"
|
|
|
|
#include "third_party/blink/public/platform/web_url_response.h"
|
2012-06-19 18:29:49 +02:00
|
|
|
|
2023-05-08 17:07:57 +02:00
|
|
|
#define CHECK_READONLY_RETURN_VOID() \
|
|
|
|
if (read_only_) { \
|
|
|
|
DCHECK(false) << "object is read only"; \
|
|
|
|
return; \
|
2012-06-19 18:29:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// CefResponse ----------------------------------------------------------------
|
|
|
|
|
|
|
|
// static
|
|
|
|
CefRefPtr<CefResponse> CefResponse::Create() {
|
|
|
|
CefRefPtr<CefResponse> response(new CefResponseImpl());
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
// CefResponseImpl ------------------------------------------------------------
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2024-01-20 23:48:57 +01:00
|
|
|
CefResponseImpl::CefResponseImpl() = default;
|
2012-06-19 18:29:49 +02:00
|
|
|
|
|
|
|
bool CefResponseImpl::IsReadOnly() {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2012-06-19 18:29:49 +02:00
|
|
|
return read_only_;
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2016-05-11 20:20:07 +02:00
|
|
|
cef_errorcode_t CefResponseImpl::GetError() {
|
|
|
|
base::AutoLock lock_scope(lock_);
|
|
|
|
return error_code_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefResponseImpl::SetError(cef_errorcode_t error) {
|
|
|
|
base::AutoLock lock_scope(lock_);
|
|
|
|
CHECK_READONLY_RETURN_VOID();
|
|
|
|
error_code_ = error;
|
|
|
|
}
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
int CefResponseImpl::GetStatus() {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2012-04-03 03:34:16 +02:00
|
|
|
return status_code_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefResponseImpl::SetStatus(int status) {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2012-06-19 18:29:49 +02:00
|
|
|
CHECK_READONLY_RETURN_VOID();
|
2012-04-03 03:34:16 +02:00
|
|
|
status_code_ = status;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefString CefResponseImpl::GetStatusText() {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2012-04-03 03:34:16 +02:00
|
|
|
return status_text_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefResponseImpl::SetStatusText(const CefString& statusText) {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2012-06-19 18:29:49 +02:00
|
|
|
CHECK_READONLY_RETURN_VOID();
|
2012-04-03 03:34:16 +02:00
|
|
|
status_text_ = statusText;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefString CefResponseImpl::GetMimeType() {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2012-04-03 03:34:16 +02:00
|
|
|
return mime_type_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefResponseImpl::SetMimeType(const CefString& mimeType) {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2012-06-19 18:29:49 +02:00
|
|
|
CHECK_READONLY_RETURN_VOID();
|
2012-04-03 03:34:16 +02:00
|
|
|
mime_type_ = mimeType;
|
|
|
|
}
|
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
CefString CefResponseImpl::GetCharset() {
|
|
|
|
base::AutoLock lock_scope(lock_);
|
|
|
|
return charset_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefResponseImpl::SetCharset(const CefString& charset) {
|
|
|
|
base::AutoLock lock_scope(lock_);
|
|
|
|
CHECK_READONLY_RETURN_VOID();
|
|
|
|
charset_ = charset;
|
|
|
|
}
|
|
|
|
|
2019-10-14 13:32:38 +02:00
|
|
|
CefString CefResponseImpl::GetHeaderByName(const CefString& name) {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2019-10-14 13:32:38 +02:00
|
|
|
std::string nameLower = name;
|
|
|
|
HttpHeaderUtils::MakeASCIILower(&nameLower);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2019-10-14 13:32:38 +02:00
|
|
|
auto it = HttpHeaderUtils::FindHeaderInMap(nameLower, header_map_);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (it != header_map_.end()) {
|
2019-10-14 13:32:38 +02:00
|
|
|
return it->second;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2019-10-14 13:32:38 +02:00
|
|
|
return CefString();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefResponseImpl::SetHeaderByName(const CefString& name,
|
|
|
|
const CefString& value,
|
|
|
|
bool overwrite) {
|
|
|
|
base::AutoLock lock_scope(lock_);
|
|
|
|
CHECK_READONLY_RETURN_VOID();
|
|
|
|
|
|
|
|
std::string nameLower = name;
|
|
|
|
HttpHeaderUtils::MakeASCIILower(&nameLower);
|
|
|
|
|
|
|
|
// There may be multiple values, so remove any first.
|
|
|
|
for (auto it = header_map_.begin(); it != header_map_.end();) {
|
|
|
|
if (base::EqualsCaseInsensitiveASCII(it->first.ToString(), nameLower)) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!overwrite) {
|
2019-10-14 13:32:38 +02:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-10-14 13:32:38 +02:00
|
|
|
it = header_map_.erase(it);
|
|
|
|
} else {
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
header_map_.insert(std::make_pair(name, value));
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2018-04-10 19:04:57 +02:00
|
|
|
CefString CefResponseImpl::GetURL() {
|
|
|
|
base::AutoLock lock_scope(lock_);
|
|
|
|
return url_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefResponseImpl::SetURL(const CefString& url) {
|
|
|
|
base::AutoLock lock_scope(lock_);
|
|
|
|
CHECK_READONLY_RETURN_VOID();
|
|
|
|
url_ = url;
|
|
|
|
}
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
void CefResponseImpl::GetHeaderMap(HeaderMap& map) {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2012-04-03 03:34:16 +02:00
|
|
|
map = header_map_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefResponseImpl::SetHeaderMap(const HeaderMap& headerMap) {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2012-06-19 18:29:49 +02:00
|
|
|
CHECK_READONLY_RETURN_VOID();
|
2012-04-03 03:34:16 +02:00
|
|
|
header_map_ = headerMap;
|
|
|
|
}
|
|
|
|
|
2020-03-04 01:29:39 +01:00
|
|
|
scoped_refptr<net::HttpResponseHeaders> CefResponseImpl::GetResponseHeaders() {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
std::string mime_type = mime_type_;
|
2023-01-02 23:59:03 +01:00
|
|
|
if (mime_type.empty()) {
|
2019-04-24 04:50:25 +02:00
|
|
|
mime_type = "text/html";
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
std::multimap<std::string, std::string> extra_headers;
|
2023-01-02 23:59:03 +01:00
|
|
|
for (const auto& pair : header_map_) {
|
2019-04-24 04:50:25 +02:00
|
|
|
extra_headers.insert(std::make_pair(pair.first, pair.second));
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-06-19 18:29:49 +02:00
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
return net_service::MakeResponseHeaders(
|
|
|
|
status_code_, status_text_, mime_type, charset_, -1, extra_headers,
|
|
|
|
true /* allow_existing_header_override */);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
2012-06-19 18:29:49 +02:00
|
|
|
|
|
|
|
void CefResponseImpl::SetResponseHeaders(
|
|
|
|
const net::HttpResponseHeaders& headers) {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2015-03-11 19:44:11 +01:00
|
|
|
CHECK_READONLY_RETURN_VOID();
|
2012-06-19 18:29:49 +02:00
|
|
|
|
2018-06-19 00:08:20 +02:00
|
|
|
header_map_.clear();
|
2012-06-19 18:29:49 +02:00
|
|
|
|
2016-02-05 01:49:19 +01:00
|
|
|
size_t iter = 0;
|
2012-06-19 18:29:49 +02:00
|
|
|
std::string name, value;
|
2023-01-02 23:59:03 +01:00
|
|
|
while (headers.EnumerateHeaderLines(&iter, &name, &value)) {
|
2012-06-19 18:29:49 +02:00
|
|
|
header_map_.insert(std::make_pair(name, value));
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-06-19 18:29:49 +02:00
|
|
|
|
|
|
|
status_code_ = headers.response_code();
|
|
|
|
status_text_ = headers.GetStatusText();
|
|
|
|
|
2019-05-11 00:14:48 +02:00
|
|
|
if (headers.IsRedirect(nullptr)) {
|
|
|
|
// Don't report Content-Type header values for redirects.
|
|
|
|
mime_type_.clear();
|
|
|
|
charset_.clear();
|
|
|
|
} else {
|
|
|
|
std::string mime_type, charset;
|
|
|
|
headers.GetMimeTypeAndCharset(&mime_type, &charset);
|
|
|
|
mime_type_ = mime_type;
|
|
|
|
charset_ = charset;
|
|
|
|
}
|
2012-06-19 18:29:49 +02:00
|
|
|
}
|
|
|
|
|
2013-11-08 22:28:56 +01:00
|
|
|
void CefResponseImpl::Set(const blink::WebURLResponse& response) {
|
2017-04-20 21:28:17 +02:00
|
|
|
DCHECK(!response.IsNull());
|
2012-06-19 18:29:49 +02:00
|
|
|
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2012-06-19 18:29:49 +02:00
|
|
|
CHECK_READONLY_RETURN_VOID();
|
|
|
|
|
2013-11-08 22:28:56 +01:00
|
|
|
blink::WebString str;
|
2017-04-20 21:28:17 +02:00
|
|
|
status_code_ = response.HttpStatusCode();
|
|
|
|
str = response.HttpStatusText();
|
|
|
|
status_text_ = str.Utf16();
|
|
|
|
str = response.MimeType();
|
|
|
|
mime_type_ = str.Utf16();
|
2019-01-17 10:56:52 +01:00
|
|
|
str = response.CurrentRequestUrl().GetString();
|
2018-04-10 19:04:57 +02:00
|
|
|
url_ = str.Utf16();
|
2012-06-19 18:29:49 +02:00
|
|
|
|
2013-11-08 22:28:56 +01:00
|
|
|
class HeaderVisitor : public blink::WebHTTPHeaderVisitor {
|
2012-06-19 18:29:49 +02:00
|
|
|
public:
|
|
|
|
explicit HeaderVisitor(HeaderMap* map) : map_(map) {}
|
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
void VisitHeader(const blink::WebString& name,
|
2014-11-12 20:25:15 +01:00
|
|
|
const blink::WebString& value) override {
|
2017-04-20 21:28:17 +02:00
|
|
|
map_->insert(std::make_pair(name.Utf16(), value.Utf16()));
|
2012-06-19 18:29:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2024-05-11 17:48:38 +02:00
|
|
|
raw_ptr<HeaderMap> map_;
|
2012-06-19 18:29:49 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
HeaderVisitor visitor(&header_map_);
|
2019-06-05 16:15:45 +02:00
|
|
|
response.VisitHttpHeaderFields(&visitor);
|
2012-06-19 18:29:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefResponseImpl::SetReadOnly(bool read_only) {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2012-06-19 18:29:49 +02:00
|
|
|
read_only_ = read_only;
|
|
|
|
}
|