OLD | NEW |
---|---|
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 max_buffer_size_(kDefaultMaxBufferSize) { |
17 if (!socket_.get()) | 15 SetCapacity(kInitialBufSize); |
18 return; | |
19 socket_->Send(data); | |
20 } | 16 } |
21 | 17 |
22 void HttpConnection::Send(const char* bytes, int len) { | 18 HttpConnection::ReadIOBuffer::~ReadIOBuffer() { |
23 if (!socket_.get()) | 19 data_ = NULL; // base_ owns data_. |
24 return; | |
25 socket_->Send(bytes, len); | |
26 } | 20 } |
27 | 21 |
28 void HttpConnection::Send(const HttpServerResponseInfo& response) { | 22 int HttpConnection::ReadIOBuffer::GetCapacity() const { |
29 Send(response.Serialize()); | 23 return base_->capacity(); |
30 } | 24 } |
31 | 25 |
32 HttpConnection::HttpConnection(HttpServer* server, | 26 void HttpConnection::ReadIOBuffer::SetCapacity(int capacity) { |
33 scoped_ptr<StreamListenSocket> sock) | 27 base_->SetCapacity(capacity); |
34 : server_(server), | 28 data_ = base_->data(); |
35 socket_(sock.Pass()) { | 29 } |
36 id_ = last_id_++; | 30 |
31 bool HttpConnection::ReadIOBuffer::IncreaseCapacity() { | |
32 if (GetCapacity() >= max_buffer_size_) { | |
33 LOG(ERROR) << "Too large read data is pending: capacity=" << GetCapacity() | |
34 << ", max_buffer_size=" << max_buffer_size_ | |
35 << ", read=" << GetSize(); | |
mmenke
2014/08/14 16:36:02
include base/logging.h
byungchul
2014/08/14 18:44:02
Done.
| |
36 return false; | |
37 } | |
38 | |
39 int new_capacity = GetCapacity() * kCapacityIncreaseFactor; | |
40 if (new_capacity > max_buffer_size_) | |
41 new_capacity = max_buffer_size_; | |
42 SetCapacity(new_capacity); | |
43 return true; | |
44 } | |
45 | |
46 char* HttpConnection::ReadIOBuffer::StartOfBuffer() const { | |
47 return base_->StartOfBuffer(); | |
48 } | |
49 | |
50 int HttpConnection::ReadIOBuffer::GetSize() const { | |
51 return base_->offset(); | |
52 } | |
53 | |
54 void HttpConnection::ReadIOBuffer::DidRead(int bytes) { | |
55 DCHECK_GE(RemainingCapacity(), bytes); | |
56 base_->set_offset(base_->offset() + bytes); | |
57 data_ = base_->data(); | |
58 } | |
59 | |
60 int HttpConnection::ReadIOBuffer::RemainingCapacity() const { | |
61 return base_->RemainingCapacity(); | |
62 } | |
63 | |
64 void HttpConnection::ReadIOBuffer::DidConsume(int bytes) { | |
65 int previous_size = GetSize(); | |
66 int unconsumed_size = previous_size - bytes; | |
67 DCHECK_LE(0, unconsumed_size); | |
68 if (unconsumed_size > 0) { | |
69 // Move unconsumed data to the start of buffer. | |
70 memmove(StartOfBuffer(), StartOfBuffer() + bytes, unconsumed_size); | |
71 } | |
72 base_->set_offset(unconsumed_size); | |
73 data_ = base_->data(); | |
74 | |
75 // If capacity is too big, reduce it. | |
76 if (GetCapacity() > kMinimumBufSize | |
77 && GetCapacity() > previous_size * kCapacityIncreaseFactor) { | |
mmenke
2014/08/14 16:36:02
nit: put && on previous line
byungchul
2014/08/14 18:44:02
Done.
| |
78 int new_capacity = GetCapacity() / kCapacityIncreaseFactor; | |
79 if (new_capacity < kMinimumBufSize) | |
80 new_capacity = kMinimumBufSize; | |
81 // realloc() within GrowableIOBuffer::SetCapacity() could move data even | |
82 // when size is reduced. If unconsumed_size == 0, i.e. no data exists in | |
83 // the buffer, free internal buffer first to guarantee no data move. | |
84 if (!unconsumed_size) | |
85 base_->SetCapacity(0); | |
86 SetCapacity(new_capacity); | |
87 } | |
88 } | |
89 | |
90 HttpConnection::PendingWriteIOBuffer::PendingWriteIOBuffer() | |
91 : total_size_(0), | |
92 max_buffer_size_(kDefaultMaxBufferSize) { | |
93 } | |
94 | |
95 HttpConnection::PendingWriteIOBuffer::~PendingWriteIOBuffer() { | |
96 data_ = NULL; // pending_data_ owns data_. | |
97 } | |
98 | |
99 bool HttpConnection::PendingWriteIOBuffer::IsEmpty() const { | |
100 return pending_data_.empty(); | |
101 } | |
102 | |
103 bool HttpConnection::PendingWriteIOBuffer::Append(const std::string& data) { | |
104 if (data.empty()) | |
105 return true; | |
106 | |
107 if (total_size_ + static_cast<int>(data.size()) > max_buffer_size_) { | |
108 LOG(ERROR) << "Too large write data is pending: size=" | |
109 << total_size_ + data.size() | |
110 << ", max_buffer_size=" << max_buffer_size_; | |
111 return false; | |
112 } | |
113 | |
114 pending_data_.push(data); | |
115 total_size_ += data.size(); | |
116 | |
117 // If new data is the first pending data, updates data_. | |
118 if (pending_data_.size() == 1) | |
119 data_ = const_cast<char*>(pending_data_.front().data()); | |
120 return true; | |
121 } | |
122 | |
123 void HttpConnection::PendingWriteIOBuffer::DidConsume(int size) { | |
124 DCHECK_GE(total_size_, size); | |
125 DCHECK_GE(GetSizeToWrite(), size); | |
126 if (size == 0) | |
127 return; | |
128 | |
129 if (size < GetSizeToWrite()) { | |
130 data_ += size; | |
131 } else { // size == GetSizeToWrite(). Updates data_ to next pending data. | |
132 pending_data_.pop(); | |
133 data_ = IsEmpty() ? NULL : const_cast<char*>(pending_data_.front().data()); | |
134 } | |
135 total_size_ -= size; | |
136 } | |
137 | |
138 int HttpConnection::PendingWriteIOBuffer::GetSizeToWrite() const { | |
139 if (IsEmpty()) { | |
140 DCHECK_EQ(0, total_size_); | |
141 return 0; | |
142 } | |
143 DCHECK_GE(data_, pending_data_.front().data()); | |
144 int consumed = static_cast<int>(data_ - pending_data_.front().data()); | |
145 DCHECK_GT(static_cast<int>(pending_data_.front().size()), consumed); | |
146 return pending_data_.front().size() - consumed; | |
147 } | |
148 | |
149 HttpConnection::HttpConnection(int id, | |
150 scoped_ptr<StreamSocket> socket) | |
mmenke
2014/08/14 16:36:02
I assume this doesn't quite fit on one line?
byungchul
2014/08/14 18:44:02
Done.
| |
151 : id_(id), | |
152 socket_(socket.Pass()), | |
153 read_buf_(new ReadIOBuffer()), | |
154 write_buf_(new PendingWriteIOBuffer()) { | |
37 } | 155 } |
38 | 156 |
39 HttpConnection::~HttpConnection() { | 157 HttpConnection::~HttpConnection() { |
40 server_->delegate_->OnClose(id_); | |
41 } | 158 } |
42 | 159 |
43 void HttpConnection::Shift(int num_bytes) { | 160 void HttpConnection::SetWebSocket(scoped_ptr<WebSocket> web_socket) { |
44 recv_data_ = recv_data_.substr(num_bytes); | 161 DCHECK(!web_socket_); |
162 web_socket_ = web_socket.Pass(); | |
45 } | 163 } |
46 | 164 |
47 } // namespace net | 165 } // namespace net |
OLD | NEW |