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.
|
|
|
|
|
|
|
|
#include "libcef/common/response_impl.h"
|
|
|
|
|
2012-06-19 18:29:49 +02:00
|
|
|
#include <string>
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
#include "base/logging.h"
|
2013-06-22 04:06:32 +02:00
|
|
|
#include "base/strings/stringprintf.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"
|
2015-03-11 19:44:11 +01:00
|
|
|
#include "net/url_request/url_request.h"
|
2013-06-22 04:06:32 +02:00
|
|
|
#include "third_party/WebKit/public/platform/WebHTTPHeaderVisitor.h"
|
|
|
|
#include "third_party/WebKit/public/platform/WebString.h"
|
|
|
|
#include "third_party/WebKit/public/platform/WebURLResponse.h"
|
2012-06-19 18:29:49 +02:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
#define CHECK_READONLY_RETURN_VOID() \
|
|
|
|
if (read_only_) { \
|
2012-06-19 18:29:49 +02:00
|
|
|
NOTREACHED() << "object is read only"; \
|
2017-05-17 11:29:28 +02:00
|
|
|
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
|
|
|
|
|
|
|
CefResponseImpl::CefResponseImpl()
|
2017-05-17 11:29:28 +02:00
|
|
|
: error_code_(ERR_NONE), status_code_(0), read_only_(false) {}
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefString CefResponseImpl::GetHeader(const CefString& name) {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
CefString value;
|
|
|
|
|
|
|
|
HeaderMap::const_iterator it = header_map_.find(name);
|
|
|
|
if (it != header_map_.end())
|
|
|
|
value = it->second;
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
std::string response;
|
|
|
|
std::string status_text;
|
2012-06-19 18:29:49 +02:00
|
|
|
bool has_content_type_header = false;
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2012-06-19 18:29:49 +02:00
|
|
|
if (!status_text_.empty())
|
2012-04-03 03:34:16 +02:00
|
|
|
status_text = status_text_;
|
2012-06-19 18:29:49 +02:00
|
|
|
else
|
2017-05-17 11:29:28 +02:00
|
|
|
status_text = (status_code_ == 200) ? "OK" : "ERROR";
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
base::SStringPrintf(&response, "HTTP/1.1 %d %s", status_code_,
|
|
|
|
status_text.c_str());
|
|
|
|
if (header_map_.size() > 0) {
|
|
|
|
for (HeaderMap::const_iterator header = header_map_.begin();
|
2017-05-17 11:29:28 +02:00
|
|
|
header != header_map_.end(); ++header) {
|
2012-04-03 03:34:16 +02:00
|
|
|
const CefString& key = header->first;
|
|
|
|
const CefString& value = header->second;
|
|
|
|
|
|
|
|
if (!key.empty()) {
|
|
|
|
// Delimit with "\0" as required by net::HttpResponseHeaders.
|
|
|
|
std::string key_str(key);
|
|
|
|
std::string value_str(value);
|
|
|
|
base::StringAppendF(&response, "%c%s: %s", '\0', key_str.c_str(),
|
|
|
|
value_str.c_str());
|
2012-06-19 18:29:49 +02:00
|
|
|
|
|
|
|
if (!has_content_type_header &&
|
|
|
|
key_str == net::HttpRequestHeaders::kContentType) {
|
|
|
|
has_content_type_header = true;
|
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-19 18:29:49 +02:00
|
|
|
if (!has_content_type_header) {
|
|
|
|
std::string mime_type;
|
|
|
|
if (!mime_type_.empty())
|
|
|
|
mime_type = mime_type_;
|
|
|
|
else
|
|
|
|
mime_type = "text/html";
|
|
|
|
|
|
|
|
base::StringAppendF(&response, "%c%s: %s", '\0',
|
2017-05-17 11:29:28 +02:00
|
|
|
net::HttpRequestHeaders::kContentType,
|
|
|
|
mime_type.c_str());
|
2012-06-19 18:29:49 +02:00
|
|
|
}
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
return new net::HttpResponseHeaders(response);
|
|
|
|
}
|
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
|
|
|
|
|
|
|
header_map_.empty();
|
|
|
|
|
2016-02-05 01:49:19 +01:00
|
|
|
size_t iter = 0;
|
2012-06-19 18:29:49 +02:00
|
|
|
std::string name, value;
|
|
|
|
while (headers.EnumerateHeaderLines(&iter, &name, &value))
|
|
|
|
header_map_.insert(std::make_pair(name, value));
|
|
|
|
|
|
|
|
status_code_ = headers.response_code();
|
|
|
|
status_text_ = headers.GetStatusText();
|
|
|
|
|
|
|
|
std::string mime_type;
|
|
|
|
if (headers.GetMimeType(&mime_type))
|
|
|
|
mime_type_ = mime_type;
|
2015-03-11 19:44:11 +01:00
|
|
|
else
|
|
|
|
mime_type_.clear();
|
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();
|
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:
|
|
|
|
HeaderMap* map_;
|
|
|
|
};
|
|
|
|
|
|
|
|
HeaderVisitor visitor(&header_map_);
|
2017-04-20 21:28:17 +02:00
|
|
|
response.VisitHTTPHeaderFields(&visitor);
|
2012-06-19 18:29:49 +02:00
|
|
|
}
|
|
|
|
|
2015-03-11 19:44:11 +01:00
|
|
|
void CefResponseImpl::Set(const net::URLRequest* request) {
|
|
|
|
DCHECK(request);
|
|
|
|
|
|
|
|
const net::HttpResponseHeaders* headers = request->response_headers();
|
|
|
|
if (headers)
|
|
|
|
SetResponseHeaders(*headers);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|