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 capacity_limit_(kDefaultCapacityLimit) { |
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_. |
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
| |
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() >= capacity_limit_) { | |
33 LOG(ERROR) << "Too large read data is pending: capacity=" << GetCapacity() | |
34 << ", capacity_limit=" << capacity_limit_ | |
35 << ", read=" << GetSize(); | |
36 return false; | |
37 } | |
38 | |
39 int new_capacity = GetCapacity() * kCapacityIncreaseFactor; | |
40 if (new_capacity > capacity_limit_) { | |
41 new_capacity = capacity_limit_; | |
42 } | |
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.
| |
43 SetCapacity(new_capacity); | |
44 return true; | |
45 } | |
46 | |
47 char* HttpConnection::ReadIOBuffer::StartOfBuffer() const { | |
48 return base_->StartOfBuffer(); | |
49 } | |
50 | |
51 int HttpConnection::ReadIOBuffer::GetSize() const { | |
52 return base_->offset(); | |
53 } | |
54 | |
55 void HttpConnection::ReadIOBuffer::DidRead(int bytes) { | |
56 DCHECK_GE(RemainingCapacity(), bytes); | |
57 base_->set_offset(base_->offset() + bytes); | |
58 data_ = base_->data(); | |
59 } | |
60 | |
61 int HttpConnection::ReadIOBuffer::RemainingCapacity() const { | |
62 return base_->RemainingCapacity(); | |
63 } | |
64 | |
65 void HttpConnection::ReadIOBuffer::DidConsume(int bytes) { | |
66 int previous_size = GetSize(); | |
67 int unconsumed_size = previous_size - bytes; | |
68 DCHECK_LE(0, unconsumed_size); | |
69 if (unconsumed_size > 0) { | |
70 // Move unconsumed data to the start of buffer. | |
71 memmove(StartOfBuffer(), StartOfBuffer() + bytes, unconsumed_size); | |
72 } | |
73 base_->set_offset(unconsumed_size); | |
74 data_ = base_->data(); | |
75 | |
76 // If capacity is too big, reduce it. | |
77 if (GetCapacity() > kMinimumBufSize | |
78 && GetCapacity() > previous_size * kCapacityIncreaseFactor) { | |
79 int new_capacity = GetCapacity() / kCapacityIncreaseFactor; | |
80 if (new_capacity < kMinimumBufSize) { | |
81 new_capacity = kMinimumBufSize; | |
82 } | |
83 // realloc() within GrowableIOBuffer::SetCapacity() could move data even | |
84 // when size is reduced. If unconsumed_size == 0, i.e. no data exists in | |
85 // the buffer, free internal buffer first to guarantee no data move. | |
86 if (!unconsumed_size) | |
87 base_->SetCapacity(0); | |
88 SetCapacity(new_capacity); | |
89 } | |
90 } | |
91 | |
92 HttpConnection::PendingWriteIOBuffer::PendingWriteIOBuffer() | |
93 : total_size_(0), | |
94 total_size_limit_(kDefaultTotalSizeLimit) { | |
95 } | |
96 | |
97 HttpConnection::PendingWriteIOBuffer::~PendingWriteIOBuffer() { | |
98 data_ = NULL; // pending_data_ owns data_. | |
99 } | |
100 | |
101 bool HttpConnection::PendingWriteIOBuffer::IsEmpty() const { | |
102 return pending_data_.empty(); | |
103 } | |
104 | |
105 bool HttpConnection::PendingWriteIOBuffer::Append(const std::string& data) { | |
106 if (data.empty()) { | |
107 return true; | |
108 } | |
109 | |
110 if (total_size_ + static_cast<int>(data.size()) > total_size_limit_) { | |
111 LOG(ERROR) << "Too large write data is pending: size=" | |
112 << total_size_ + data.size() | |
113 << ", size_limit=" << total_size_limit_; | |
114 return false; | |
115 } | |
116 | |
117 pending_data_.push(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 } | |
124 return true; | |
125 } | |
126 | |
127 void HttpConnection::PendingWriteIOBuffer::DidConsume(int size) { | |
128 DCHECK_GE(total_size_, size); | |
129 DCHECK_GE(GetSizeToWrite(), size); | |
130 if (size == 0) { | |
131 return; | |
132 } | |
133 | |
134 if (size < GetSizeToWrite()) { | |
135 data_ += size; | |
136 } else { // size == GetSizeToWrite(). Updates data_ to next pending data. | |
137 pending_data_.pop(); | |
138 data_ = IsEmpty() ? NULL : const_cast<char*>(pending_data_.front().data()); | |
139 } | |
140 total_size_ -= size; | |
141 } | |
142 | |
143 int HttpConnection::PendingWriteIOBuffer::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 | |
154 HttpConnection::HttpConnection(int id, | |
155 scoped_ptr<StreamSocket> socket) | |
156 : id_(id), | |
157 socket_(socket.Pass()), | |
158 read_buf_(new ReadIOBuffer()), | |
159 write_buf_(new PendingWriteIOBuffer()) { | |
37 } | 160 } |
38 | 161 |
39 HttpConnection::~HttpConnection() { | 162 HttpConnection::~HttpConnection() { |
40 server_->delegate_->OnClose(id_); | |
41 } | 163 } |
42 | 164 |
43 void HttpConnection::Shift(int num_bytes) { | 165 void HttpConnection::SetWebSocket(scoped_ptr<WebSocket> web_socket) { |
44 recv_data_ = recv_data_.substr(num_bytes); | 166 DCHECK(!web_socket_); |
167 web_socket_ = web_socket.Pass(); | |
45 } | 168 } |
46 | 169 |
47 } // namespace net | 170 } // namespace net |
OLD | NEW |