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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/server/http_connection.h"
#include <utility>
#include "base/logging.h"
#include "net/server/web_socket.h"
#include "net/socket/stream_socket.h"
namespace net {
HttpConnection::ReadIOBuffer::ReadIOBuffer()
: base_(base::MakeRefCounted<GrowableIOBuffer>()) {
SetCapacity(kInitialBufSize);
}
HttpConnection::ReadIOBuffer::~ReadIOBuffer() {
data_ = nullptr; // base_ owns data_.
}
int HttpConnection::ReadIOBuffer::GetCapacity() const {
return base_->capacity();
}
void HttpConnection::ReadIOBuffer::SetCapacity(int capacity) {
DCHECK_LE(GetSize(), capacity);
data_ = nullptr;
base_->SetCapacity(capacity);
data_ = base_->data();
}
bool HttpConnection::ReadIOBuffer::IncreaseCapacity() {
if (GetCapacity() >= max_buffer_size_) {
LOG(ERROR) << "Too large read data is pending: capacity=" << GetCapacity()
<< ", max_buffer_size=" << max_buffer_size_
<< ", read=" << GetSize();
return false;
}
int new_capacity = GetCapacity() * kCapacityIncreaseFactor;
if (new_capacity > max_buffer_size_)
new_capacity = max_buffer_size_;
SetCapacity(new_capacity);
return true;
}
char* HttpConnection::ReadIOBuffer::StartOfBuffer() const {
return base_->StartOfBuffer();
}
int HttpConnection::ReadIOBuffer::GetSize() const {
return base_->offset();
}
void HttpConnection::ReadIOBuffer::DidRead(int bytes) {
DCHECK_GE(RemainingCapacity(), bytes);
base_->set_offset(base_->offset() + bytes);
data_ = base_->data();
}
int HttpConnection::ReadIOBuffer::RemainingCapacity() const {
return base_->RemainingCapacity();
}
void HttpConnection::ReadIOBuffer::DidConsume(int bytes) {
int previous_size = GetSize();
int unconsumed_size = previous_size - bytes;
DCHECK_LE(0, unconsumed_size);
if (unconsumed_size > 0) {
// Move unconsumed data to the start of buffer.
memmove(StartOfBuffer(), StartOfBuffer() + bytes, unconsumed_size);
}
base_->set_offset(unconsumed_size);
data_ = base_->data();
// If capacity is too big, reduce it.
if (GetCapacity() > kMinimumBufSize &&
GetCapacity() > previous_size * kCapacityIncreaseFactor) {
int new_capacity = GetCapacity() / kCapacityIncreaseFactor;
if (new_capacity < kMinimumBufSize)
new_capacity = kMinimumBufSize;
// this avoids the pointer to dangle until `SetCapacity` gets called.
data_ = nullptr;
// realloc() within GrowableIOBuffer::SetCapacity() could move data even
// when size is reduced. If unconsumed_size == 0, i.e. no data exists in
// the buffer, free internal buffer first to guarantee no data move.
if (!unconsumed_size)
base_->SetCapacity(0);
SetCapacity(new_capacity);
}
}
HttpConnection::QueuedWriteIOBuffer::QueuedWriteIOBuffer() = default;
HttpConnection::QueuedWriteIOBuffer::~QueuedWriteIOBuffer() {
data_ = nullptr; // pending_data_ owns data_.
}
bool HttpConnection::QueuedWriteIOBuffer::IsEmpty() const {
return pending_data_.empty();
}
bool HttpConnection::QueuedWriteIOBuffer::Append(const std::string& data) {
if (data.empty())
return true;
if (total_size_ + static_cast<int>(data.size()) > max_buffer_size_) {
LOG(ERROR) << "Too large write data is pending: size="
<< total_size_ + data.size()
<< ", max_buffer_size=" << max_buffer_size_;
return false;
}
pending_data_.push(std::make_unique<std::string>(data));
total_size_ += data.size();
// If new data is the first pending data, updates data_.
if (pending_data_.size() == 1)
data_ = const_cast<char*>(pending_data_.front()->data());
return true;
}
void HttpConnection::QueuedWriteIOBuffer::DidConsume(int size) {
DCHECK_GE(total_size_, size);
DCHECK_GE(GetSizeToWrite(), size);
if (size == 0)
return;
if (size < GetSizeToWrite()) {
data_ += size;
} else { // size == GetSizeToWrite(). Updates data_ to next pending data.
data_ = nullptr;
pending_data_.pop();
data_ =
IsEmpty() ? nullptr : const_cast<char*>(pending_data_.front()->data());
}
total_size_ -= size;
}
int HttpConnection::QueuedWriteIOBuffer::GetSizeToWrite() const {
if (IsEmpty()) {
DCHECK_EQ(0, total_size_);
return 0;
}
DCHECK_GE(data_, pending_data_.front()->data());
int consumed = static_cast<int>(data_ - pending_data_.front()->data());
DCHECK_GT(static_cast<int>(pending_data_.front()->size()), consumed);
return pending_data_.front()->size() - consumed;
}
HttpConnection::HttpConnection(int id, std::unique_ptr<StreamSocket> socket)
: id_(id),
socket_(std::move(socket)),
read_buf_(base::MakeRefCounted<ReadIOBuffer>()),
write_buf_(base::MakeRefCounted<QueuedWriteIOBuffer>()) {}
HttpConnection::~HttpConnection() = default;
void HttpConnection::SetWebSocket(std::unique_ptr<WebSocket> web_socket) {
DCHECK(!web_socket_);
web_socket_ = std::move(web_socket);
}
} // namespace net
|