cef/libcef/common/parser_impl.cc

167 lines
5.0 KiB
C++
Raw Normal View History

// 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.
#include <sstream>
#include "base/base64.h"
#include "base/strings/escape.h"
#include "base/threading/thread_restrictions.h"
#include "cef/include/cef_parser.h"
#include "components/url_formatter/elide_url.h"
#include "net/base/mime_util.h"
#include "url/gurl.h"
bool CefResolveURL(const CefString& base_url,
const CefString& relative_url,
CefString& resolved_url) {
GURL base_gurl(base_url.ToString());
2023-01-02 23:59:03 +01:00
if (!base_gurl.is_valid()) {
return false;
2023-01-02 23:59:03 +01:00
}
GURL combined_gurl = base_gurl.Resolve(relative_url.ToString());
2023-01-02 23:59:03 +01:00
if (!combined_gurl.is_valid()) {
return false;
2023-01-02 23:59:03 +01:00
}
resolved_url = combined_gurl.spec();
return true;
}
bool CefParseURL(const CefString& url, CefURLParts& parts) {
GURL gurl(url.ToString());
2023-01-02 23:59:03 +01:00
if (!gurl.is_valid()) {
return false;
2023-01-02 23:59:03 +01:00
}
CefString(&parts.spec).FromString(gurl.spec());
CefString(&parts.scheme).FromString(gurl.scheme());
CefString(&parts.username).FromString(gurl.username());
CefString(&parts.password).FromString(gurl.password());
CefString(&parts.host).FromString(gurl.host());
CefString(&parts.origin).FromString(gurl.DeprecatedGetOriginAsURL().spec());
CefString(&parts.port).FromString(gurl.port());
CefString(&parts.path).FromString(gurl.path());
CefString(&parts.query).FromString(gurl.query());
CefString(&parts.fragment).FromString(gurl.ref());
return true;
}
bool CefCreateURL(const CefURLParts& parts, CefString& url) {
std::string spec = CefString(parts.spec.str, parts.spec.length, false);
std::string scheme = CefString(parts.scheme.str, parts.scheme.length, false);
std::string username =
CefString(parts.username.str, parts.username.length, false);
std::string password =
CefString(parts.password.str, parts.password.length, false);
std::string host = CefString(parts.host.str, parts.host.length, false);
std::string port = CefString(parts.port.str, parts.port.length, false);
std::string path = CefString(parts.path.str, parts.path.length, false);
std::string query = CefString(parts.query.str, parts.query.length, false);
std::string fragment =
CefString(parts.fragment.str, parts.fragment.length, false);
GURL gurl;
if (!spec.empty()) {
gurl = GURL(spec);
} else if (!scheme.empty() && !host.empty()) {
std::stringstream ss;
ss << scheme << "://";
if (!username.empty()) {
ss << username;
2023-01-02 23:59:03 +01:00
if (!password.empty()) {
ss << ":" << password;
2023-01-02 23:59:03 +01:00
}
ss << "@";
}
ss << host;
2023-01-02 23:59:03 +01:00
if (!port.empty()) {
ss << ":" << port;
2023-01-02 23:59:03 +01:00
}
if (!path.empty()) {
ss << path;
2023-01-02 23:59:03 +01:00
}
if (!query.empty()) {
ss << "?" << query;
2023-01-02 23:59:03 +01:00
}
if (!fragment.empty()) {
ss << "#" << fragment;
2023-01-02 23:59:03 +01:00
}
gurl = GURL(ss.str());
}
if (gurl.is_valid()) {
url = gurl.spec();
return true;
}
return false;
}
CefString CefFormatUrlForSecurityDisplay(const CefString& origin_url) {
return url_formatter::FormatUrlForSecurityDisplay(
GURL(origin_url.ToString()));
}
CefString CefGetMimeType(const CefString& extension) {
// Requests should not block on the disk! On POSIX this goes to disk.
// http://code.google.com/p/chromium/issues/detail?id=59849
base::ScopedAllowBlockingForTesting allow_blocking;
std::string mime_type;
net::GetMimeTypeFromExtension(extension, &mime_type);
return mime_type;
}
void CefGetExtensionsForMimeType(const CefString& mime_type,
std::vector<CefString>& extensions) {
using VectorType = std::vector<base::FilePath::StringType>;
VectorType ext;
net::GetExtensionsForMimeType(mime_type, &ext);
VectorType::const_iterator it = ext.begin();
2023-01-02 23:59:03 +01:00
for (; it != ext.end(); ++it) {
extensions.push_back(*it);
2023-01-02 23:59:03 +01:00
}
}
CefString CefBase64Encode(const void* data, size_t data_size) {
2023-01-02 23:59:03 +01:00
if (data_size == 0) {
return CefString();
2023-01-02 23:59:03 +01:00
}
std::string_view input(static_cast<const char*>(data), data_size);
return base::Base64Encode(input);
}
CefRefPtr<CefBinaryValue> CefBase64Decode(const CefString& data) {
2023-01-02 23:59:03 +01:00
if (data.size() == 0) {
return nullptr;
2023-01-02 23:59:03 +01:00
}
const std::string& input = data;
std::string output;
2023-01-02 23:59:03 +01:00
if (base::Base64Decode(input, &output)) {
return CefBinaryValue::Create(output.data(), output.size());
2023-01-02 23:59:03 +01:00
}
return nullptr;
}
CefString CefURIEncode(const CefString& text, bool use_plus) {
return base::EscapeQueryParamValue(text.ToString(), use_plus);
}
CefString CefURIDecode(const CefString& text,
bool convert_to_utf8,
cef_uri_unescape_rule_t unescape_rule) {
const base::UnescapeRule::Type type =
static_cast<base::UnescapeRule::Type>(unescape_rule);
2023-01-02 23:59:03 +01:00
if (convert_to_utf8) {
return base::UnescapeAndDecodeUTF8URLComponentWithAdjustments(
text.ToString(), type, nullptr);
2023-01-02 23:59:03 +01:00
} else {
return base::UnescapeURLComponent(text.ToString(), type);
2023-01-02 23:59:03 +01:00
}
}