1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "net/server/http_connection.h"
6
7 #include <utility>
8
9 #include "base/logging.h"
10 #include "net/server/web_socket.h"
11 #include "net/socket/stream_socket.h"
12
13 namespace net {
14
ReadIOBuffer()15 HttpConnection::ReadIOBuffer::ReadIOBuffer()
16 : base_(base::MakeRefCounted<GrowableIOBuffer>()) {
17 SetCapacity(kInitialBufSize);
18 }
19
~ReadIOBuffer()20 HttpConnection::ReadIOBuffer::~ReadIOBuffer() {
21 data_ = nullptr; // Avoid dangling ptr when `base_` is destroyed.
22 }
23
GetCapacity() const24 int HttpConnection::ReadIOBuffer::GetCapacity() const {
25 return base_->capacity();
26 }
27
SetCapacity(int capacity)28 void HttpConnection::ReadIOBuffer::SetCapacity(int capacity) {
29 DCHECK_LE(GetSize(), capacity);
30 data_ = nullptr;
31 base_->SetCapacity(capacity);
32 data_ = base_->data();
33 }
34
IncreaseCapacity()35 bool HttpConnection::ReadIOBuffer::IncreaseCapacity() {
36 if (GetCapacity() >= max_buffer_size_) {
37 LOG(ERROR) << "Too large read data is pending: capacity=" << GetCapacity()
38 << ", max_buffer_size=" << max_buffer_size_
39 << ", read=" << GetSize();
40 return false;
41 }
42
43 int new_capacity = GetCapacity() * kCapacityIncreaseFactor;
44 if (new_capacity > max_buffer_size_)
45 new_capacity = max_buffer_size_;
46 SetCapacity(new_capacity);
47 return true;
48 }
49
StartOfBuffer() const50 char* HttpConnection::ReadIOBuffer::StartOfBuffer() const {
51 return base_->StartOfBuffer();
52 }
53
GetSize() const54 int HttpConnection::ReadIOBuffer::GetSize() const {
55 return base_->offset();
56 }
57
DidRead(int bytes)58 void HttpConnection::ReadIOBuffer::DidRead(int bytes) {
59 DCHECK_GE(RemainingCapacity(), bytes);
60 base_->set_offset(base_->offset() + bytes);
61 data_ = base_->data();
62 }
63
RemainingCapacity() const64 int HttpConnection::ReadIOBuffer::RemainingCapacity() const {
65 return base_->RemainingCapacity();
66 }
67
DidConsume(int bytes)68 void HttpConnection::ReadIOBuffer::DidConsume(int bytes) {
69 int previous_size = GetSize();
70 int unconsumed_size = previous_size - bytes;
71 DCHECK_LE(0, unconsumed_size);
72 if (unconsumed_size > 0) {
73 // Move unconsumed data to the start of buffer.
74 memmove(StartOfBuffer(), StartOfBuffer() + bytes, unconsumed_size);
75 }
76 base_->set_offset(unconsumed_size);
77 data_ = base_->data();
78
79 // If capacity is too big, reduce it.
80 if (GetCapacity() > kMinimumBufSize &&
81 GetCapacity() > previous_size * kCapacityIncreaseFactor) {
82 int new_capacity = GetCapacity() / kCapacityIncreaseFactor;
83 if (new_capacity < kMinimumBufSize)
84 new_capacity = kMinimumBufSize;
85 // this avoids the pointer to dangle until `SetCapacity` gets called.
86 data_ = nullptr;
87 // realloc() within GrowableIOBuffer::SetCapacity() could move data even
88 // when size is reduced. If unconsumed_size == 0, i.e. no data exists in
89 // the buffer, free internal buffer first to guarantee no data move.
90 if (!unconsumed_size)
91 base_->SetCapacity(0);
92 SetCapacity(new_capacity);
93 }
94 }
95
96 HttpConnection::QueuedWriteIOBuffer::QueuedWriteIOBuffer() = default;
97
~QueuedWriteIOBuffer()98 HttpConnection::QueuedWriteIOBuffer::~QueuedWriteIOBuffer() {
99 data_ = nullptr; // pending_data_ owns data_.
100 }
101
IsEmpty() const102 bool HttpConnection::QueuedWriteIOBuffer::IsEmpty() const {
103 return pending_data_.empty();
104 }
105
Append(const std::string & data)106 bool HttpConnection::QueuedWriteIOBuffer::Append(const std::string& data) {
107 if (data.empty())
108 return true;
109
110 if (total_size_ + static_cast<int>(data.size()) > max_buffer_size_) {
111 LOG(ERROR) << "Too large write data is pending: size="
112 << total_size_ + data.size()
113 << ", max_buffer_size=" << max_buffer_size_;
114 return false;
115 }
116
117 pending_data_.push(std::make_unique<std::string>(data));
118 total_size_ += data.size();
119
120 // If new data is the first pending data, updates data_.
121 if (pending_data_.size() == 1)
122 data_ = const_cast<char*>(pending_data_.front()->data());
123 return true;
124 }
125
DidConsume(int size)126 void HttpConnection::QueuedWriteIOBuffer::DidConsume(int size) {
127 DCHECK_GE(total_size_, size);
128 DCHECK_GE(GetSizeToWrite(), size);
129 if (size == 0)
130 return;
131
132 if (size < GetSizeToWrite()) {
133 data_ += size;
134 } else { // size == GetSizeToWrite(). Updates data_ to next pending data.
135 data_ = nullptr;
136 pending_data_.pop();
137 data_ =
138 IsEmpty() ? nullptr : const_cast<char*>(pending_data_.front()->data());
139 }
140 total_size_ -= size;
141 }
142
GetSizeToWrite() const143 int HttpConnection::QueuedWriteIOBuffer::GetSizeToWrite() const {
144 if (IsEmpty()) {
145 DCHECK_EQ(0, total_size_);
146 return 0;
147 }
148 DCHECK_GE(data_, pending_data_.front()->data());
149 int consumed = static_cast<int>(data_ - pending_data_.front()->data());
150 DCHECK_GT(static_cast<int>(pending_data_.front()->size()), consumed);
151 return pending_data_.front()->size() - consumed;
152 }
153
HttpConnection(int id,std::unique_ptr<StreamSocket> socket)154 HttpConnection::HttpConnection(int id, std::unique_ptr<StreamSocket> socket)
155 : id_(id),
156 socket_(std::move(socket)),
157 read_buf_(base::MakeRefCounted<ReadIOBuffer>()),
158 write_buf_(base::MakeRefCounted<QueuedWriteIOBuffer>()) {}
159
160 HttpConnection::~HttpConnection() = default;
161
SetWebSocket(std::unique_ptr<WebSocket> web_socket)162 void HttpConnection::SetWebSocket(std::unique_ptr<WebSocket> web_socket) {
163 DCHECK(!web_socket_);
164 web_socket_ = std::move(web_socket);
165 }
166
167 } // namespace net
168