Chromium Code Reviews| Index: net/server/http_connection.cc |
| diff --git a/net/server/http_connection.cc b/net/server/http_connection.cc |
| index d433012cd651c7b7462972c3835cb818a9a78d8d..d984f0793bd66fa6e022e4ce23d977a571d20fd9 100644 |
| --- a/net/server/http_connection.cc |
| +++ b/net/server/http_connection.cc |
| @@ -4,44 +4,167 @@ |
| #include "net/server/http_connection.h" |
| -#include "net/server/http_server.h" |
| -#include "net/server/http_server_response_info.h" |
| #include "net/server/web_socket.h" |
| -#include "net/socket/stream_listen_socket.h" |
| +#include "net/socket/stream_socket.h" |
| namespace net { |
| -int HttpConnection::last_id_ = 0; |
| +HttpConnection::ReadIOBuffer::ReadIOBuffer() |
| + : base_(new GrowableIOBuffer()), |
| + capacity_limit_(kDefaultCapacityLimit) { |
| + SetCapacity(kInitialBufSize); |
| +} |
| -void HttpConnection::Send(const std::string& data) { |
| - if (!socket_.get()) |
| - return; |
| - socket_->Send(data); |
| +HttpConnection::ReadIOBuffer::~ReadIOBuffer() { |
| + data_ = NULL; // base_ owns data_. |
|
mmenke
2014/08/08 18:35:43
nit: Not needed.
byungchul
2014/08/12 21:36:53
IOBuffer::~IOBuffer deletes data_ if it is not nul
|
| +} |
| + |
| +int HttpConnection::ReadIOBuffer::GetCapacity() const { |
| + return base_->capacity(); |
| +} |
| + |
| +void HttpConnection::ReadIOBuffer::SetCapacity(int capacity) { |
| + base_->SetCapacity(capacity); |
| + data_ = base_->data(); |
| +} |
| + |
| +bool HttpConnection::ReadIOBuffer::IncreaseCapacity() { |
| + if (GetCapacity() >= capacity_limit_) { |
| + LOG(ERROR) << "Too large read data is pending: capacity=" << GetCapacity() |
| + << ", capacity_limit=" << capacity_limit_ |
| + << ", read=" << GetSize(); |
| + return false; |
| + } |
| + |
| + int new_capacity = GetCapacity() * kCapacityIncreaseFactor; |
| + if (new_capacity > capacity_limit_) { |
| + new_capacity = capacity_limit_; |
| + } |
|
mmenke
2014/08/08 18:35:43
nit: There's a general preference in net/ not to
byungchul
2014/08/12 21:36:53
Done.
|
| + 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; |
| + } |
| + // 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::PendingWriteIOBuffer::PendingWriteIOBuffer() |
| + : total_size_(0), |
| + total_size_limit_(kDefaultTotalSizeLimit) { |
| +} |
| + |
| +HttpConnection::PendingWriteIOBuffer::~PendingWriteIOBuffer() { |
| + data_ = NULL; // pending_data_ owns data_. |
| } |
| -void HttpConnection::Send(const char* bytes, int len) { |
| - if (!socket_.get()) |
| +bool HttpConnection::PendingWriteIOBuffer::IsEmpty() const { |
| + return pending_data_.empty(); |
| +} |
| + |
| +bool HttpConnection::PendingWriteIOBuffer::Append(const std::string& data) { |
| + if (data.empty()) { |
| + return true; |
| + } |
| + |
| + if (total_size_ + static_cast<int>(data.size()) > total_size_limit_) { |
| + LOG(ERROR) << "Too large write data is pending: size=" |
| + << total_size_ + data.size() |
| + << ", size_limit=" << total_size_limit_; |
| + return false; |
| + } |
| + |
| + pending_data_.push(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::PendingWriteIOBuffer::DidConsume(int size) { |
| + DCHECK_GE(total_size_, size); |
| + DCHECK_GE(GetSizeToWrite(), size); |
| + if (size == 0) { |
| return; |
| - socket_->Send(bytes, len); |
| + } |
| + |
| + if (size < GetSizeToWrite()) { |
| + data_ += size; |
| + } else { // size == GetSizeToWrite(). Updates data_ to next pending data. |
| + pending_data_.pop(); |
| + data_ = IsEmpty() ? NULL : const_cast<char*>(pending_data_.front().data()); |
| + } |
| + total_size_ -= size; |
| } |
| -void HttpConnection::Send(const HttpServerResponseInfo& response) { |
| - Send(response.Serialize()); |
| +int HttpConnection::PendingWriteIOBuffer::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(HttpServer* server, |
| - scoped_ptr<StreamListenSocket> sock) |
| - : server_(server), |
| - socket_(sock.Pass()) { |
| - id_ = last_id_++; |
| +HttpConnection::HttpConnection(int id, |
| + scoped_ptr<StreamSocket> socket) |
| + : id_(id), |
| + socket_(socket.Pass()), |
| + read_buf_(new ReadIOBuffer()), |
| + write_buf_(new PendingWriteIOBuffer()) { |
| } |
| HttpConnection::~HttpConnection() { |
| - server_->delegate_->OnClose(id_); |
| } |
| -void HttpConnection::Shift(int num_bytes) { |
| - recv_data_ = recv_data_.substr(num_bytes); |
| +void HttpConnection::SetWebSocket(scoped_ptr<WebSocket> web_socket) { |
| + DCHECK(!web_socket_); |
| + web_socket_ = web_socket.Pass(); |
| } |
| } // namespace net |