OLD | NEW |
---|---|
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/http/http_stream_parser.h" | 5 #include "net/http/http_stream_parser.h" |
6 | 6 |
7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
9 #include "net/base/auth.h" | 9 #include "net/base/auth.h" |
10 #include "net/base/io_buffer.h" | 10 #include "net/base/io_buffer.h" |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
60 NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST_HEADERS, | 60 NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST_HEADERS, |
61 make_scoped_refptr(new NetLogHttpRequestParameter( | 61 make_scoped_refptr(new NetLogHttpRequestParameter( |
62 request_line, headers))); | 62 request_line, headers))); |
63 } | 63 } |
64 response_ = response; | 64 response_ = response; |
65 std::string request = request_line + headers.ToString(); | 65 std::string request = request_line + headers.ToString(); |
66 scoped_refptr<StringIOBuffer> headers_io_buf(new StringIOBuffer(request)); | 66 scoped_refptr<StringIOBuffer> headers_io_buf(new StringIOBuffer(request)); |
67 request_headers_ = new DrainableIOBuffer(headers_io_buf, | 67 request_headers_ = new DrainableIOBuffer(headers_io_buf, |
68 headers_io_buf->size()); | 68 headers_io_buf->size()); |
69 request_body_.reset(request_body); | 69 request_body_.reset(request_body); |
70 if (request_body_.get() && request_body_->is_chunked()) | |
wtc
2011/01/20 00:29:47
Nit: request_body_.get() => request_body_ != NULL
| |
71 request_body_->set_chunk_callback(this); | |
70 | 72 |
71 io_state_ = STATE_SENDING_HEADERS; | 73 io_state_ = STATE_SENDING_HEADERS; |
72 int result = DoLoop(OK); | 74 int result = DoLoop(OK); |
73 if (result == ERR_IO_PENDING) | 75 if (result == ERR_IO_PENDING) |
74 user_callback_ = callback; | 76 user_callback_ = callback; |
75 | 77 |
76 return result > 0 ? OK : result; | 78 return result > 0 ? OK : result; |
77 } | 79 } |
78 | 80 |
79 int HttpStreamParser::ReadResponseHeaders(CompletionCallback* callback) { | 81 int HttpStreamParser::ReadResponseHeaders(CompletionCallback* callback) { |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
136 | 138 |
137 // The client callback can do anything, including destroying this class, | 139 // The client callback can do anything, including destroying this class, |
138 // so any pending callback must be issued after everything else is done. | 140 // so any pending callback must be issued after everything else is done. |
139 if (result != ERR_IO_PENDING && user_callback_) { | 141 if (result != ERR_IO_PENDING && user_callback_) { |
140 CompletionCallback* c = user_callback_; | 142 CompletionCallback* c = user_callback_; |
141 user_callback_ = NULL; | 143 user_callback_ = NULL; |
142 c->Run(result); | 144 c->Run(result); |
143 } | 145 } |
144 } | 146 } |
145 | 147 |
148 void HttpStreamParser::OnChunkAvailable() { | |
149 // This method may get called in any state so check before processing the new | |
wtc
2011/01/20 00:29:47
If you add the DCHECK vandebo suggested, please up
| |
150 // data. If we were still initializing or sending headers, we will | |
151 // automatically start reading the chunks once we get into STATE_SENDING_BODY | |
152 // so nothing to do here. | |
vandebo (ex-Chrome)
2011/01/18 21:51:17
nit: You could DCHECK(io_state_ == STATE_SENDING_
| |
153 if (io_state_ == STATE_SENDING_BODY) | |
154 OnIOComplete(0); | |
155 } | |
156 | |
146 int HttpStreamParser::DoLoop(int result) { | 157 int HttpStreamParser::DoLoop(int result) { |
147 bool can_do_more = true; | 158 bool can_do_more = true; |
148 do { | 159 do { |
149 switch (io_state_) { | 160 switch (io_state_) { |
150 case STATE_SENDING_HEADERS: | 161 case STATE_SENDING_HEADERS: |
151 if (result < 0) | 162 if (result < 0) |
152 can_do_more = false; | 163 can_do_more = false; |
153 else | 164 else |
154 result = DoSendHeaders(result); | 165 result = DoSendHeaders(result); |
155 break; | 166 break; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
202 if (bytes_remaining > 0) { | 213 if (bytes_remaining > 0) { |
203 // Record our best estimate of the 'request time' as the time when we send | 214 // Record our best estimate of the 'request time' as the time when we send |
204 // out the first bytes of the request headers. | 215 // out the first bytes of the request headers. |
205 if (bytes_remaining == request_headers_->size()) { | 216 if (bytes_remaining == request_headers_->size()) { |
206 response_->request_time = base::Time::Now(); | 217 response_->request_time = base::Time::Now(); |
207 | 218 |
208 // We'll record the count of uncoalesced packets IFF coalescing will help, | 219 // We'll record the count of uncoalesced packets IFF coalescing will help, |
209 // and otherwise we'll use an enum to tell why it won't help. | 220 // and otherwise we'll use an enum to tell why it won't help. |
210 enum COALESCE_POTENTIAL { | 221 enum COALESCE_POTENTIAL { |
211 NO_ADVANTAGE = 0, // Coalescing won't reduce packet count. | 222 NO_ADVANTAGE = 0, // Coalescing won't reduce packet count. |
212 HEADER_ONLY = 1, // There is only a header packet (can't coalesce). | 223 HEADER_ONLY = 1, // There is only a header packet (can't coalesce). |
wtc
2011/01/20 00:29:47
In light of your change to line 227, the HEADER_ON
| |
213 COALESCE_POTENTIAL_MAX = 30 // Various cases of coalasced savings. | 224 COALESCE_POTENTIAL_MAX = 30 // Various cases of coalasced savings. |
214 }; | 225 }; |
215 size_t coalesce = HEADER_ONLY; | 226 size_t coalesce = HEADER_ONLY; |
216 if (request_body_ != NULL) { | 227 if (request_body_ != NULL && !request_body_->is_chunked()) { |
217 const size_t kBytesPerPacket = 1430; | 228 const size_t kBytesPerPacket = 1430; |
218 uint64 body_packets = (request_body_->size() + kBytesPerPacket - 1) / | 229 uint64 body_packets = (request_body_->size() + kBytesPerPacket - 1) / |
219 kBytesPerPacket; | 230 kBytesPerPacket; |
220 uint64 header_packets = (bytes_remaining + kBytesPerPacket - 1) / | 231 uint64 header_packets = (bytes_remaining + kBytesPerPacket - 1) / |
221 kBytesPerPacket; | 232 kBytesPerPacket; |
222 uint64 coalesced_packets = (request_body_->size() + bytes_remaining + | 233 uint64 coalesced_packets = (request_body_->size() + bytes_remaining + |
223 kBytesPerPacket - 1) / kBytesPerPacket; | 234 kBytesPerPacket - 1) / kBytesPerPacket; |
224 if (coalesced_packets < header_packets + body_packets) { | 235 if (coalesced_packets < header_packets + body_packets) { |
225 if (coalesced_packets > COALESCE_POTENTIAL_MAX) | 236 if (coalesced_packets > COALESCE_POTENTIAL_MAX) |
226 coalesce = COALESCE_POTENTIAL_MAX; | 237 coalesce = COALESCE_POTENTIAL_MAX; |
227 else | 238 else |
228 coalesce = static_cast<size_t>(header_packets + body_packets); | 239 coalesce = static_cast<size_t>(header_packets + body_packets); |
229 } else { | 240 } else { |
230 coalesce = NO_ADVANTAGE; | 241 coalesce = NO_ADVANTAGE; |
231 } | 242 } |
232 } | 243 } |
233 UMA_HISTOGRAM_ENUMERATION("Net.CoalescePotential", coalesce, | 244 UMA_HISTOGRAM_ENUMERATION("Net.CoalescePotential", coalesce, |
234 COALESCE_POTENTIAL_MAX); | 245 COALESCE_POTENTIAL_MAX); |
235 } | 246 } |
236 result = connection_->socket()->Write(request_headers_, | 247 result = connection_->socket()->Write(request_headers_, |
237 bytes_remaining, | 248 bytes_remaining, |
238 &io_callback_); | 249 &io_callback_); |
239 } else if (request_body_ != NULL && request_body_->size()) { | 250 } else if (request_body_ != NULL && |
251 (request_body_->is_chunked() || request_body_->size())) { | |
240 io_state_ = STATE_SENDING_BODY; | 252 io_state_ = STATE_SENDING_BODY; |
241 result = OK; | 253 result = OK; |
242 } else { | 254 } else { |
243 io_state_ = STATE_REQUEST_SENT; | 255 io_state_ = STATE_REQUEST_SENT; |
244 } | 256 } |
245 return result; | 257 return result; |
246 } | 258 } |
247 | 259 |
248 int HttpStreamParser::DoSendBody(int result) { | 260 int HttpStreamParser::DoSendBody(int result) { |
249 if (result > 0) | 261 if (result > 0) |
250 request_body_->DidConsume(result); | 262 request_body_->DidConsume(result); |
251 | 263 |
252 if (!request_body_->eof()) { | 264 if (!request_body_->eof()) { |
253 int buf_len = static_cast<int>(request_body_->buf_len()); | 265 int buf_len = static_cast<int>(request_body_->buf_len()); |
254 result = connection_->socket()->Write(request_body_->buf(), buf_len, | 266 if (buf_len) { |
255 &io_callback_); | 267 result = connection_->socket()->Write(request_body_->buf(), buf_len, |
268 &io_callback_); | |
269 } else { | |
270 // More POST data is to come hence wait for the callback. | |
271 result = ERR_IO_PENDING; | |
272 } | |
256 } else { | 273 } else { |
257 io_state_ = STATE_REQUEST_SENT; | 274 io_state_ = STATE_REQUEST_SENT; |
258 } | 275 } |
259 return result; | 276 return result; |
260 } | 277 } |
261 | 278 |
262 int HttpStreamParser::DoReadHeaders() { | 279 int HttpStreamParser::DoReadHeaders() { |
263 io_state_ = STATE_READ_HEADERS_COMPLETE; | 280 io_state_ = STATE_READ_HEADERS_COMPLETE; |
264 | 281 |
265 // Grow the read buffer if necessary. | 282 // Grow the read buffer if necessary. |
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
630 void HttpStreamParser::GetSSLCertRequestInfo( | 647 void HttpStreamParser::GetSSLCertRequestInfo( |
631 SSLCertRequestInfo* cert_request_info) { | 648 SSLCertRequestInfo* cert_request_info) { |
632 if (request_->url.SchemeIs("https") && connection_->socket()) { | 649 if (request_->url.SchemeIs("https") && connection_->socket()) { |
633 SSLClientSocket* ssl_socket = | 650 SSLClientSocket* ssl_socket = |
634 static_cast<SSLClientSocket*>(connection_->socket()); | 651 static_cast<SSLClientSocket*>(connection_->socket()); |
635 ssl_socket->GetSSLCertRequestInfo(cert_request_info); | 652 ssl_socket->GetSSLCertRequestInfo(cert_request_info); |
636 } | 653 } |
637 } | 654 } |
638 | 655 |
639 } // namespace net | 656 } // namespace net |
OLD | NEW |