2011-02-09 23:04:35 +01:00
|
|
|
// Copyright (c) 2011 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.
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
#include "libcef/response_impl.h"
|
|
|
|
#include "libcef/http_header_utils.h"
|
2011-02-09 23:04:35 +01:00
|
|
|
|
|
|
|
#include "base/logging.h"
|
2011-04-21 18:46:16 +02:00
|
|
|
#include "base/stringprintf.h"
|
2011-05-26 20:11:56 +02:00
|
|
|
#include "net/http/http_response_headers.h"
|
2013-02-26 00:44:37 +01:00
|
|
|
#include "third_party/WebKit/Source/Platform/chromium/public/WebString.h"
|
|
|
|
#include "third_party/WebKit/Source/Platform/chromium/public/WebURLResponse.h"
|
2011-02-09 23:04:35 +01:00
|
|
|
|
2011-04-21 18:46:16 +02:00
|
|
|
CefResponseImpl::CefResponseImpl()
|
2012-01-10 00:46:23 +01:00
|
|
|
: status_code_(0) {
|
2011-04-21 18:46:16 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
CefResponseImpl::CefResponseImpl(const WebKit::WebURLResponse &response) {
|
2011-02-09 23:04:35 +01:00
|
|
|
DCHECK(!response.isNull());
|
|
|
|
|
|
|
|
WebKit::WebString str;
|
|
|
|
status_code_ = response.httpStatusCode();
|
|
|
|
str = response.httpStatusText();
|
|
|
|
status_text_ = CefString(str);
|
2011-04-21 18:46:16 +02:00
|
|
|
str = response.mimeType();
|
|
|
|
mime_type_ = CefString(str);
|
2011-02-09 23:04:35 +01:00
|
|
|
|
|
|
|
HttpHeaderUtils::HeaderVisitor visitor(&header_map_);
|
|
|
|
response.visitHTTPHeaderFields(&visitor);
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
int CefResponseImpl::GetStatus() {
|
2011-04-21 18:46:16 +02:00
|
|
|
AutoLock lock_scope(this);
|
2011-02-09 23:04:35 +01:00
|
|
|
return status_code_;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
void CefResponseImpl::SetStatus(int status) {
|
2011-04-21 18:46:16 +02:00
|
|
|
AutoLock lock_scope(this);
|
|
|
|
status_code_ = status;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
CefString CefResponseImpl::GetStatusText() {
|
2011-04-21 18:46:16 +02:00
|
|
|
AutoLock lock_scope(this);
|
2011-02-09 23:04:35 +01:00
|
|
|
return status_text_;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
void CefResponseImpl::SetStatusText(const CefString& statusText) {
|
2011-04-21 18:46:16 +02:00
|
|
|
AutoLock lock_scope(this);
|
|
|
|
status_text_ = statusText;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
CefString CefResponseImpl::GetMimeType() {
|
2011-04-21 18:46:16 +02:00
|
|
|
AutoLock lock_scope(this);
|
|
|
|
return mime_type_;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
void CefResponseImpl::SetMimeType(const CefString& mimeType) {
|
2011-04-21 18:46:16 +02:00
|
|
|
AutoLock lock_scope(this);
|
|
|
|
mime_type_ = mimeType;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
CefString CefResponseImpl::GetHeader(const CefString& name) {
|
2011-04-21 18:46:16 +02:00
|
|
|
AutoLock lock_scope(this);
|
|
|
|
|
2011-02-09 23:04:35 +01:00
|
|
|
CefString value;
|
|
|
|
|
|
|
|
HeaderMap::const_iterator it = header_map_.find(name);
|
|
|
|
if (it != header_map_.end())
|
|
|
|
value = it->second;
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
void CefResponseImpl::GetHeaderMap(HeaderMap& map) {
|
2011-04-21 18:46:16 +02:00
|
|
|
AutoLock lock_scope(this);
|
2011-02-09 23:04:35 +01:00
|
|
|
map = header_map_;
|
|
|
|
}
|
2011-04-21 18:46:16 +02:00
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
void CefResponseImpl::SetHeaderMap(const HeaderMap& headerMap) {
|
2011-04-21 18:46:16 +02:00
|
|
|
AutoLock lock_scope(this);
|
|
|
|
header_map_ = headerMap;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
net::HttpResponseHeaders* CefResponseImpl::GetResponseHeaders() {
|
2011-04-21 18:46:16 +02:00
|
|
|
AutoLock lock_scope(this);
|
|
|
|
|
2011-05-26 20:11:56 +02:00
|
|
|
std::string response;
|
2011-04-21 18:46:16 +02:00
|
|
|
std::string status_text;
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
if (status_text_.empty())
|
2011-04-21 18:46:16 +02:00
|
|
|
status_text = (status_code_ == 200)?"OK":"ERROR";
|
|
|
|
else
|
|
|
|
status_text = status_text_;
|
|
|
|
|
2011-05-26 20:11:56 +02:00
|
|
|
base::SStringPrintf(&response, "HTTP/1.1 %d %s", status_code_,
|
2011-04-21 18:46:16 +02:00
|
|
|
status_text.c_str());
|
2011-05-26 20:11:56 +02:00
|
|
|
if (header_map_.size() > 0) {
|
2012-01-10 00:46:23 +01:00
|
|
|
for (HeaderMap::const_iterator header = header_map_.begin();
|
2011-05-26 20:11:56 +02:00
|
|
|
header != header_map_.end();
|
|
|
|
++header) {
|
|
|
|
const CefString& key = header->first;
|
|
|
|
const CefString& value = header->second;
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
if (!key.empty()) {
|
2011-05-26 20:11:56 +02:00
|
|
|
// 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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new net::HttpResponseHeaders(response);
|
2011-04-21 18:46:16 +02:00
|
|
|
}
|