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
|
// Copyright (c) 2012 The Chromium 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 "content/browser/loader/certificate_resource_handler.h"
#include "base/strings/string_util.h"
#include "content/browser/loader/resource_request_info_impl.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/common/resource_response.h"
#include "net/base/io_buffer.h"
#include "net/base/mime_sniffer.h"
#include "net/base/mime_util.h"
#include "net/http/http_response_headers.h"
#include "net/url_request/redirect_info.h"
#include "net/url_request/url_request.h"
#include "net/url_request/url_request_status.h"
namespace content {
CertificateResourceHandler::CertificateResourceHandler(
net::URLRequest* request)
: ResourceHandler(request),
content_length_(0),
read_buffer_(NULL),
resource_buffer_(NULL),
cert_type_(net::CERTIFICATE_MIME_TYPE_UNKNOWN) {
}
CertificateResourceHandler::~CertificateResourceHandler() {
}
bool CertificateResourceHandler::OnUploadProgress(uint64 position,
uint64 size) {
return true;
}
bool CertificateResourceHandler::OnRequestRedirected(
const net::RedirectInfo& redirect_info,
ResourceResponse* resp,
bool* defer) {
return true;
}
bool CertificateResourceHandler::OnResponseStarted(ResourceResponse* resp,
bool* defer) {
cert_type_ = net::GetCertificateMimeTypeForMimeType(resp->head.mime_type);
return cert_type_ != net::CERTIFICATE_MIME_TYPE_UNKNOWN;
}
bool CertificateResourceHandler::OnWillStart(const GURL& url, bool* defer) {
return true;
}
bool CertificateResourceHandler::OnBeforeNetworkStart(const GURL& url,
bool* defer) {
return true;
}
bool CertificateResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf,
int* buf_size,
int min_size) {
static const int kReadBufSize = 32768;
// TODO(gauravsh): Should we use 'min_size' here?
DCHECK(buf && buf_size);
if (!read_buffer_.get()) {
read_buffer_ = new net::IOBuffer(kReadBufSize);
}
*buf = read_buffer_.get();
*buf_size = kReadBufSize;
return true;
}
bool CertificateResourceHandler::OnReadCompleted(int bytes_read, bool* defer) {
if (!bytes_read)
return true;
// We have more data to read.
DCHECK(read_buffer_.get());
content_length_ += bytes_read;
// Release the ownership of the buffer, and store a reference
// to it. A new one will be allocated in OnWillRead().
scoped_refptr<net::IOBuffer> buffer;
read_buffer_.swap(buffer);
// TODO(gauravsh): Should this be handled by a separate thread?
buffer_.push_back(std::make_pair(buffer, bytes_read));
return true;
}
void CertificateResourceHandler::OnResponseCompleted(
const net::URLRequestStatus& urs,
const std::string& sec_info,
bool* defer) {
if (urs.status() != net::URLRequestStatus::SUCCESS)
return;
AssembleResource();
const void* content_bytes = NULL;
if (resource_buffer_.get())
content_bytes = resource_buffer_->data();
// Note that it's up to the browser to verify that the certificate
// data is well-formed.
const ResourceRequestInfo* info = GetRequestInfo();
GetContentClient()->browser()->AddCertificate(
cert_type_, content_bytes, content_length_,
info->GetChildID(), info->GetRenderFrameID());
}
void CertificateResourceHandler::AssembleResource() {
// 0-length IOBuffers are not allowed.
if (content_length_ == 0) {
resource_buffer_ = NULL;
return;
}
// Create the new buffer.
resource_buffer_ = new net::IOBuffer(content_length_);
// Copy the data into it.
size_t bytes_copied = 0;
for (size_t i = 0; i < buffer_.size(); ++i) {
net::IOBuffer* data = buffer_[i].first.get();
size_t data_len = buffer_[i].second;
DCHECK(data != NULL);
DCHECK_LE(bytes_copied + data_len, content_length_);
memcpy(resource_buffer_->data() + bytes_copied, data->data(), data_len);
bytes_copied += data_len;
}
DCHECK_EQ(content_length_, bytes_copied);
}
void CertificateResourceHandler::OnDataDownloaded(int bytes_downloaded) {
NOTREACHED();
}
} // namespace content
|