Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(88)

Side by Side Diff: net/server/http_connection.cc

Issue 296053012: Replace StreamListenSocket with StreamSocket in HttpServer. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Don't export HttpServer which is built in a static lib Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/server/http_connection.h" 5 #include "net/server/http_connection.h"
6 6
7 #include "net/server/http_server.h"
8 #include "net/server/http_server_response_info.h"
9 #include "net/server/web_socket.h" 7 #include "net/server/web_socket.h"
10 #include "net/socket/stream_listen_socket.h" 8 #include "net/socket/stream_socket.h"
11 9
12 namespace net { 10 namespace net {
13 11
14 int HttpConnection::last_id_ = 0; 12 HttpConnection::ReadIOBuffer::ReadIOBuffer()
15 13 : base_(new GrowableIOBuffer()),
16 void HttpConnection::Send(const std::string& data) { 14 capacity_limit_(kDefaultCapacityLimit) {
17 if (!socket_.get()) 15 base_->SetCapacity(kInitialBufSize);
18 return; 16 data_ = base_->StartOfBuffer();
19 socket_->Send(data);
20 } 17 }
21 18
22 void HttpConnection::Send(const char* bytes, int len) { 19 HttpConnection::ReadIOBuffer::~ReadIOBuffer() {
23 if (!socket_.get()) 20 data_ = NULL; // base_ owns data_.
24 return;
25 socket_->Send(bytes, len);
26 } 21 }
27 22
28 void HttpConnection::Send(const HttpServerResponseInfo& response) { 23 IOBuffer* HttpConnection::ReadIOBuffer::GetUnusedIOBuffer() const {
mmenke 2014/06/11 16:05:49 I think this is a very confusing name and concept.
mmenke 2014/06/11 16:09:04 Ignore this suggestion - I think we should think a
byungchul 2014/06/11 17:30:28 Okay, I am re-thinking about how to manage the rea
mmenke 2014/06/11 17:53:27 I'm not concerned with reducing data copy for the
byungchul 2014/08/05 07:06:18 Changed read buffer similar to GrowableIOBuffer. C
29 Send(response.Serialize()); 24 return base_.get();
30 } 25 }
31 26
32 HttpConnection::HttpConnection(HttpServer* server, 27 int HttpConnection::ReadIOBuffer::GetUnusedCapacity() const {
mmenke 2014/06/11 16:05:49 Suggest keeping this as GetRemainingCapacity, for
byungchul 2014/08/05 07:06:18 Done.
33 scoped_ptr<StreamListenSocket> sock) 28 return base_->RemainingCapacity();
34 : server_(server), 29 }
35 socket_(sock.Pass()) { 30
36 id_ = last_id_++; 31 int HttpConnection::ReadIOBuffer::GetCapacity() const {
32 return base_->capacity();
33 }
34
35 void HttpConnection::ReadIOBuffer::SetCapacity(int capacity) {
36 DCHECK(data_);
37 int consumed_size = static_cast<int>(data_ - base_->StartOfBuffer());
38 base_->SetCapacity(capacity);
39 data_ = base_->StartOfBuffer() + consumed_size;
40 }
41
42 bool HttpConnection::ReadIOBuffer::IncreaseCapacity() {
43 if (GetCapacity() >= capacity_limit_) {
44 LOG(ERROR) << "Too large read data is pending: capacity=" << GetCapacity()
mmenke 2014/06/11 16:05:49 DLOG, to avoid bloating up binaries.
byungchul 2014/08/05 07:06:18 Http server closes the connection for this error.
45 << ", capacity_limit=" << capacity_limit_
46 << ", consumed=" << base_->offset();
47 return false;
48 }
49
50 int new_capacity = GetCapacity() * kCapacityIncreaseFactor;
mmenke 2014/06/11 16:05:49 Seems a little weird to me that we never make an e
byungchul 2014/06/11 17:30:28 Yes, your suggestion would be helpful though this
byungchul 2014/08/05 07:06:18 Changed to move data on DidConsume() call.
51 if (new_capacity > capacity_limit_) {
52 new_capacity = capacity_limit_;
53 }
54 SetCapacity(new_capacity);
55 return true;
56 }
57
58 int HttpConnection::ReadIOBuffer::GetUnconsumedSize() const {
59 DCHECK_GE(base_->data(), data_);
60 return base_->data() - data_;
61 }
62
63 void HttpConnection::ReadIOBuffer::DidRead(int bytes) {
64 DCHECK_LE(bytes, GetUnusedCapacity());
65 base_->set_offset(base_->offset() + bytes);
66 }
67
68 void HttpConnection::ReadIOBuffer::DidConsume(int bytes) {
69 DCHECK_LE(bytes, GetUnconsumedSize());
70 if (bytes < GetUnconsumedSize()) {
71 data_ += bytes;
72 return;
73 }
74 // Since bytes == GetUnconsumedSize() here, base_->offset() is the size of
75 // consumed data, and all data has been consumed. No need to keep consumed
76 // data any more because no data will be moved.
77 // If capacity is too big, reduce it.
78 if (GetCapacity() > kMinimumBufSize
79 && GetCapacity() > base_->offset() * kCapacityIncreaseFactor) {
80 int new_capacity = GetCapacity() / kCapacityIncreaseFactor;
81 if (new_capacity < kMinimumBufSize) {
82 new_capacity = kMinimumBufSize;
83 }
84 // realloc() within GrowableIOBuffer::SetCapacity() could move data even
85 // when size is reduced. Free internal data first to guarantee no data move.
86 base_->SetCapacity(0);
87 base_->SetCapacity(new_capacity);
88 } else {
89 base_->set_offset(0);
90 }
91 data_ = base_->StartOfBuffer();
92 }
93
94 HttpConnection::PendingWriteIOBuffer::PendingWriteIOBuffer()
95 : total_size_(0),
96 total_size_limit_(kDefaultTotalSizeLimit) {
97 }
98
99 HttpConnection::PendingWriteIOBuffer::~PendingWriteIOBuffer() {
100 data_ = NULL; // pending_data_ owns data_.
101 }
102
103 bool HttpConnection::PendingWriteIOBuffer::IsEmpty() const {
104 return pending_data_.empty();
105 }
106
107 bool HttpConnection::PendingWriteIOBuffer::Append(const std::string& data) {
108 if (data.empty()) {
109 return true;
110 }
111
112 if (total_size_ + static_cast<int>(data.size()) > total_size_limit_) {
113 LOG(ERROR) << "Too large write data is pending: size="
114 << total_size_ + data.size()
115 << ", size_limit=" << total_size_limit_;
116 return false;
117 }
118
119 pending_data_.push(data);
120 total_size_ += data.size();
121
122 // If new data is the first pending data, updates data_.
123 if (pending_data_.size() == 1) {
124 data_ = const_cast<char*>(pending_data_.front().data());
125 }
126 return true;
127 }
128
129 void HttpConnection::PendingWriteIOBuffer::DidConsume(int size) {
130 DCHECK_LE(size, total_size_);
131 DCHECK_LE(size, GetSizeToWrite());
132 if (size == 0) {
133 return;
134 }
135
136 if (size < GetSizeToWrite()) {
137 data_ += size;
138 } else { // size == GetSizeToWrite(). Updates data_ to next pending data.
139 pending_data_.pop();
140 data_ = IsEmpty() ? NULL : const_cast<char*>(pending_data_.front().data());
141 }
142 total_size_ -= size;
143 }
144
145 int HttpConnection::PendingWriteIOBuffer::GetSizeToWrite() const {
146 if (IsEmpty()) {
147 DCHECK_EQ(total_size_, 0);
mmenke 2014/06/11 16:05:49 nit: Expected goes before actual.
byungchul 2014/08/05 07:06:18 Done.
148 return 0;
149 }
150 DCHECK_GE(data_, pending_data_.front().data());
151 int consumed = static_cast<int>(data_ - pending_data_.front().data());
152 DCHECK_GT(static_cast<int>(pending_data_.front().size()), consumed);
153 return pending_data_.front().size() - consumed;
154 }
155
156 HttpConnection::HttpConnection(int id,
157 scoped_ptr<StreamSocket> socket)
158 : id_(id),
159 socket_(socket.Pass()),
160 read_buf_(new ReadIOBuffer()),
161 write_buf_(new PendingWriteIOBuffer()) {
37 } 162 }
38 163
39 HttpConnection::~HttpConnection() { 164 HttpConnection::~HttpConnection() {
40 server_->delegate_->OnClose(id_);
41 } 165 }
42 166
43 void HttpConnection::Shift(int num_bytes) { 167 void HttpConnection::SetWebSocket(scoped_ptr<WebSocket> web_socket) {
44 recv_data_ = recv_data_.substr(num_bytes); 168 DCHECK(!web_socket_);
169 web_socket_ = web_socket.Pass();
45 } 170 }
46 171
47 } // namespace net 172 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698