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 #ifndef NET_SPDY_SPDY_SESSION_H_ | 5 #ifndef NET_SPDY_SPDY_SESSION_H_ |
6 #define NET_SPDY_SPDY_SESSION_H_ | 6 #define NET_SPDY_SPDY_SESSION_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <list> | 10 #include <list> |
11 #include <map> | 11 #include <map> |
12 #include <queue> | 12 #include <queue> |
13 #include <string> | 13 #include <string> |
14 | 14 |
15 #include "base/gtest_prod_util.h" | 15 #include "base/gtest_prod_util.h" |
16 #include "base/memory/linked_ptr.h" | 16 #include "base/memory/linked_ptr.h" |
17 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
18 #include "base/memory/weak_ptr.h" | 18 #include "base/memory/weak_ptr.h" |
19 #include "net/base/io_buffer.h" | 19 #include "net/base/io_buffer.h" |
20 #include "net/base/load_states.h" | 20 #include "net/base/load_states.h" |
21 #include "net/base/net_errors.h" | 21 #include "net/base/net_errors.h" |
22 #include "net/base/request_priority.h" | 22 #include "net/base/request_priority.h" |
| 23 #include "net/base/ssl_client_cert_type.h" |
23 #include "net/base/ssl_config_service.h" | 24 #include "net/base/ssl_config_service.h" |
24 #include "net/base/upload_data_stream.h" | 25 #include "net/base/upload_data_stream.h" |
25 #include "net/socket/client_socket_handle.h" | 26 #include "net/socket/client_socket_handle.h" |
26 #include "net/socket/ssl_client_socket.h" | 27 #include "net/socket/ssl_client_socket.h" |
27 #include "net/socket/stream_socket.h" | 28 #include "net/socket/stream_socket.h" |
28 #include "net/spdy/buffered_spdy_framer.h" | 29 #include "net/spdy/buffered_spdy_framer.h" |
29 #include "net/spdy/spdy_credential_state.h" | 30 #include "net/spdy/spdy_credential_state.h" |
30 #include "net/spdy/spdy_io_buffer.h" | 31 #include "net/spdy/spdy_io_buffer.h" |
31 #include "net/spdy/spdy_protocol.h" | 32 #include "net/spdy/spdy_protocol.h" |
32 #include "net/spdy/spdy_session_pool.h" | 33 #include "net/spdy/spdy_session_pool.h" |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 SpdyProtocolErrorDetails_SpdyErrors_mismatch); | 88 SpdyProtocolErrorDetails_SpdyErrors_mismatch); |
88 | 89 |
89 COMPILE_ASSERT(PROTOCOL_ERROR_UNEXPECTED_PING == | 90 COMPILE_ASSERT(PROTOCOL_ERROR_UNEXPECTED_PING == |
90 static_cast<SpdyProtocolErrorDetails>(NUM_STATUS_CODES + | 91 static_cast<SpdyProtocolErrorDetails>(NUM_STATUS_CODES + |
91 STATUS_CODE_INVALID), | 92 STATUS_CODE_INVALID), |
92 SpdyProtocolErrorDetails_SpdyErrors_mismatch); | 93 SpdyProtocolErrorDetails_SpdyErrors_mismatch); |
93 | 94 |
94 class NET_EXPORT SpdySession : public base::RefCounted<SpdySession>, | 95 class NET_EXPORT SpdySession : public base::RefCounted<SpdySession>, |
95 public BufferedSpdyFramerVisitorInterface { | 96 public BufferedSpdyFramerVisitorInterface { |
96 public: | 97 public: |
| 98 // Defines an interface for producing SpdyIOBuffers. |
| 99 class NET_EXPORT_PRIVATE SpdyIOBufferProducer { |
| 100 public: |
| 101 SpdyIOBufferProducer() {} |
| 102 |
| 103 // Returns a newly created SpdyIOBuffer, owned by the caller, or NULL |
| 104 // if not buffer is ready to be produced. |
| 105 virtual SpdyIOBuffer* ProduceNextBuffer(SpdySession* session) = 0; |
| 106 |
| 107 virtual RequestPriority GetPriority() const = 0; |
| 108 |
| 109 virtual ~SpdyIOBufferProducer() {} |
| 110 |
| 111 protected: |
| 112 // Activates |spdy_stream| in |spdy_session|. |
| 113 static void ActivateStream(SpdySession* spdy_session, |
| 114 SpdyStream* spdy_stream); |
| 115 |
| 116 static SpdyIOBuffer* CreateIOBuffer(SpdyFrame* frame, |
| 117 RequestPriority priority, |
| 118 SpdyStream* spdy_stream); |
| 119 |
| 120 private: |
| 121 DISALLOW_COPY_AND_ASSIGN(SpdyIOBufferProducer); |
| 122 }; |
| 123 |
97 // Create a new SpdySession. | 124 // Create a new SpdySession. |
98 // |host_port_proxy_pair| is the host/port that this session connects to, and | 125 // |host_port_proxy_pair| is the host/port that this session connects to, and |
99 // the proxy configuration settings that it's using. | 126 // the proxy configuration settings that it's using. |
100 // |spdy_session_pool| is the SpdySessionPool that owns us. Its lifetime must | 127 // |spdy_session_pool| is the SpdySessionPool that owns us. Its lifetime must |
101 // strictly be greater than |this|. | 128 // strictly be greater than |this|. |
102 // |session| is the HttpNetworkSession. |net_log| is the NetLog that we log | 129 // |session| is the HttpNetworkSession. |net_log| is the NetLog that we log |
103 // network events to. | 130 // network events to. |
104 SpdySession(const HostPortProxyPair& host_port_proxy_pair, | 131 SpdySession(const HostPortProxyPair& host_port_proxy_pair, |
105 SpdySessionPool* spdy_session_pool, | 132 SpdySessionPool* spdy_session_pool, |
106 HttpServerProperties* http_server_properties, | 133 HttpServerProperties* http_server_properties, |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 // If the session is un-authenticated, then this call always returns true. | 177 // If the session is un-authenticated, then this call always returns true. |
151 // For SSL-based sessions, verifies that the server certificate in use by | 178 // For SSL-based sessions, verifies that the server certificate in use by |
152 // this session provides authentication for the domain and no client | 179 // this session provides authentication for the domain and no client |
153 // certificate was sent to the original server during the SSL handshake. | 180 // certificate was sent to the original server during the SSL handshake. |
154 // NOTE: This function can have false negatives on some platforms. | 181 // NOTE: This function can have false negatives on some platforms. |
155 // TODO(wtc): rename this function and the Net.SpdyIPPoolDomainMatch | 182 // TODO(wtc): rename this function and the Net.SpdyIPPoolDomainMatch |
156 // histogram because this function does more than verifying domain | 183 // histogram because this function does more than verifying domain |
157 // authentication now. | 184 // authentication now. |
158 bool VerifyDomainAuthentication(const std::string& domain); | 185 bool VerifyDomainAuthentication(const std::string& domain); |
159 | 186 |
| 187 // Records that |stream| has a write available from |producer|. |
| 188 // |producer| will be owned by this SpdySession. |
| 189 void SetStreamHasWriteAvailable(SpdyStream* stream, |
| 190 SpdyIOBufferProducer* producer); |
| 191 |
160 // Send the SYN frame for |stream_id|. This also sends PING message to check | 192 // Send the SYN frame for |stream_id|. This also sends PING message to check |
161 // the status of the connection. | 193 // the status of the connection. |
162 int WriteSynStream( | 194 SpdySynStreamControlFrame* CreateSynStream( |
163 SpdyStreamId stream_id, | 195 SpdyStreamId stream_id, |
164 RequestPriority priority, | 196 RequestPriority priority, |
165 uint8 credential_slot, | 197 uint8 credential_slot, |
166 SpdyControlFlags flags, | 198 SpdyControlFlags flags, |
167 const linked_ptr<SpdyHeaderBlock>& headers); | 199 const linked_ptr<SpdyHeaderBlock>& headers); |
168 | 200 |
169 // Write a CREDENTIAL frame to the session. | 201 // Write a CREDENTIAL frame to the session. |
170 int WriteCredentialFrame(const std::string& origin, | 202 SpdyCredentialControlFrame* CreateCredentialFrame(const std::string& origin, |
171 SSLClientCertType type, | 203 SSLClientCertType type, |
172 const std::string& key, | 204 const std::string& key, |
173 const std::string& cert, | 205 const std::string& cert, |
174 RequestPriority priority); | 206 RequestPriority priority); |
175 | 207 |
176 // Write a data frame to the stream. | 208 // Write a data frame to the stream. |
177 // Used to create and queue a data frame for the given stream. | 209 // Used to create and queue a data frame for the given stream. |
178 int WriteStreamData(SpdyStreamId stream_id, net::IOBuffer* data, | 210 SpdyDataFrame* CreateDataFrame(SpdyStreamId stream_id, |
179 int len, | 211 net::IOBuffer* data, int len, |
180 SpdyDataFlags flags); | 212 SpdyDataFlags flags); |
181 | 213 |
182 // Close a stream. | 214 // Close a stream. |
183 void CloseStream(SpdyStreamId stream_id, int status); | 215 void CloseStream(SpdyStreamId stream_id, int status); |
184 | 216 |
| 217 // Close a stream that has been created but is not yet active. |
| 218 void CloseCreatedStream(SpdyStream* stream, int status); |
| 219 |
185 // Reset a stream by sending a RST_STREAM frame with given status code. | 220 // Reset a stream by sending a RST_STREAM frame with given status code. |
186 // Also closes the stream. Was not piggybacked to CloseStream since not | 221 // Also closes the stream. Was not piggybacked to CloseStream since not |
187 // all of the calls to CloseStream necessitate sending a RST_STREAM. | 222 // all of the calls to CloseStream necessitate sending a RST_STREAM. |
188 void ResetStream(SpdyStreamId stream_id, | 223 void ResetStream(SpdyStreamId stream_id, |
189 SpdyStatusCodes status, | 224 SpdyStatusCodes status, |
190 const std::string& description); | 225 const std::string& description); |
191 | 226 |
192 // Check if a stream is active. | 227 // Check if a stream is active. |
193 bool IsStreamActive(SpdyStreamId stream_id) const; | 228 bool IsStreamActive(SpdyStreamId stream_id) const; |
194 | 229 |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 bool WasEverUsed() const { | 300 bool WasEverUsed() const { |
266 return connection_->socket()->WasEverUsed(); | 301 return connection_->socket()->WasEverUsed(); |
267 } | 302 } |
268 | 303 |
269 void set_spdy_session_pool(SpdySessionPool* pool) { | 304 void set_spdy_session_pool(SpdySessionPool* pool) { |
270 spdy_session_pool_ = NULL; | 305 spdy_session_pool_ = NULL; |
271 } | 306 } |
272 | 307 |
273 // Returns true if session is not currently active | 308 // Returns true if session is not currently active |
274 bool is_active() const { | 309 bool is_active() const { |
275 return !active_streams_.empty(); | 310 return !active_streams_.empty() || !created_streams_.empty(); |
276 } | 311 } |
277 | 312 |
278 // Access to the number of active and pending streams. These are primarily | 313 // Access to the number of active and pending streams. These are primarily |
279 // available for testing and diagnostics. | 314 // available for testing and diagnostics. |
280 size_t num_active_streams() const { return active_streams_.size(); } | 315 size_t num_active_streams() const { return active_streams_.size(); } |
281 size_t num_unclaimed_pushed_streams() const { | 316 size_t num_unclaimed_pushed_streams() const { |
282 return unclaimed_pushed_streams_.size(); | 317 return unclaimed_pushed_streams_.size(); |
283 } | 318 } |
| 319 size_t num_created_streams() const { return created_streams_.size(); } |
284 | 320 |
285 // Returns true if flow control is enabled for the session. | 321 // Returns true if flow control is enabled for the session. |
286 bool is_flow_control_enabled() const { | 322 bool is_flow_control_enabled() const { |
287 return flow_control_; | 323 return flow_control_; |
288 } | 324 } |
289 | 325 |
290 // Returns the current |initial_send_window_size_|. | 326 // Returns the current |initial_send_window_size_|. |
291 int32 initial_send_window_size() const { | 327 int32 initial_send_window_size() const { |
292 return initial_send_window_size_; | 328 return initial_send_window_size_; |
293 } | 329 } |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
349 RequestPriority priority; | 385 RequestPriority priority; |
350 scoped_refptr<SpdyStream>* spdy_stream; | 386 scoped_refptr<SpdyStream>* spdy_stream; |
351 const BoundNetLog* stream_net_log; | 387 const BoundNetLog* stream_net_log; |
352 CompletionCallback callback; | 388 CompletionCallback callback; |
353 }; | 389 }; |
354 typedef std::queue<PendingCreateStream, std::list< PendingCreateStream> > | 390 typedef std::queue<PendingCreateStream, std::list< PendingCreateStream> > |
355 PendingCreateStreamQueue; | 391 PendingCreateStreamQueue; |
356 typedef std::map<int, scoped_refptr<SpdyStream> > ActiveStreamMap; | 392 typedef std::map<int, scoped_refptr<SpdyStream> > ActiveStreamMap; |
357 // Only HTTP push a stream. | 393 // Only HTTP push a stream. |
358 typedef std::map<std::string, scoped_refptr<SpdyStream> > PushedStreamMap; | 394 typedef std::map<std::string, scoped_refptr<SpdyStream> > PushedStreamMap; |
359 typedef std::priority_queue<SpdyIOBuffer> OutputQueue; | 395 typedef std::set<scoped_refptr<SpdyStream> > CreatedStreamSet; |
| 396 typedef std::map<SpdyIOBufferProducer*, SpdyStream*> StreamProducerMap; |
| 397 class SpdyIOBufferProducerCompare { |
| 398 public: |
| 399 bool operator() (const SpdyIOBufferProducer* lhs, |
| 400 const SpdyIOBufferProducer* rhs) const { |
| 401 return lhs->GetPriority() < rhs->GetPriority(); |
| 402 } |
| 403 }; |
| 404 typedef std::priority_queue<SpdyIOBufferProducer*, |
| 405 std::vector<SpdyIOBufferProducer*>, |
| 406 SpdyIOBufferProducerCompare> WriteQueue; |
360 | 407 |
361 struct CallbackResultPair { | 408 struct CallbackResultPair { |
362 CallbackResultPair(const CompletionCallback& callback_in, int result_in) | 409 CallbackResultPair(const CompletionCallback& callback_in, int result_in) |
363 : callback(callback_in), result(result_in) {} | 410 : callback(callback_in), result(result_in) {} |
364 ~CallbackResultPair(); | 411 ~CallbackResultPair(); |
365 | 412 |
366 CompletionCallback callback; | 413 CompletionCallback callback; |
367 int result; | 414 int result; |
368 }; | 415 }; |
369 | 416 |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
427 // Write current data to the socket. | 474 // Write current data to the socket. |
428 void WriteSocketLater(); | 475 void WriteSocketLater(); |
429 void WriteSocket(); | 476 void WriteSocket(); |
430 | 477 |
431 // Get a new stream id. | 478 // Get a new stream id. |
432 int GetNewStreamId(); | 479 int GetNewStreamId(); |
433 | 480 |
434 // Queue a frame for sending. | 481 // Queue a frame for sending. |
435 // |frame| is the frame to send. | 482 // |frame| is the frame to send. |
436 // |priority| is the priority for insertion into the queue. | 483 // |priority| is the priority for insertion into the queue. |
437 // |stream| is the stream which this IO is associated with (or NULL). | 484 void QueueFrame(SpdyFrame* frame, RequestPriority priority); |
438 void QueueFrame(SpdyFrame* frame, RequestPriority priority, | |
439 SpdyStream* stream); | |
440 | 485 |
441 // Track active streams in the active stream list. | 486 // Track active streams in the active stream list. |
442 void ActivateStream(SpdyStream* stream); | 487 void ActivateStream(SpdyStream* stream); |
443 void DeleteStream(SpdyStreamId id, int status); | 488 void DeleteStream(SpdyStreamId id, int status); |
444 | 489 |
445 // Removes this session from the session pool. | 490 // Removes this session from the session pool. |
446 void RemoveFromPool(); | 491 void RemoveFromPool(); |
447 | 492 |
448 // Check if we have a pending pushed-stream for this url | 493 // Check if we have a pending pushed-stream for this url |
449 // Returns the stream if found (and returns it from the pending | 494 // Returns the stream if found (and returns it from the pending |
450 // list), returns NULL otherwise. | 495 // list), returns NULL otherwise. |
451 scoped_refptr<SpdyStream> GetActivePushStream(const std::string& url); | 496 scoped_refptr<SpdyStream> GetActivePushStream(const std::string& url); |
452 | 497 |
453 // Calls OnResponseReceived(). | 498 // Calls OnResponseReceived(). |
454 // Returns true if successful. | 499 // Returns true if successful. |
455 bool Respond(const SpdyHeaderBlock& headers, | 500 bool Respond(const SpdyHeaderBlock& headers, |
456 const scoped_refptr<SpdyStream> stream); | 501 const scoped_refptr<SpdyStream> stream); |
457 | 502 |
458 void RecordPingRTTHistogram(base::TimeDelta duration); | 503 void RecordPingRTTHistogram(base::TimeDelta duration); |
459 void RecordHistograms(); | 504 void RecordHistograms(); |
460 void RecordProtocolErrorHistogram(SpdyProtocolErrorDetails details); | 505 void RecordProtocolErrorHistogram(SpdyProtocolErrorDetails details); |
461 | 506 |
462 // Closes all streams. Used as part of shutdown. | 507 // Closes all streams. Used as part of shutdown. |
463 void CloseAllStreams(net::Error status); | 508 void CloseAllStreams(net::Error status); |
464 | 509 |
| 510 void LogAbandonedStream(const scoped_refptr<SpdyStream>& stream, |
| 511 net::Error status); |
| 512 |
465 // Invokes a user callback for stream creation. We provide this method so it | 513 // Invokes a user callback for stream creation. We provide this method so it |
466 // can be deferred to the MessageLoop, so we avoid re-entrancy problems. | 514 // can be deferred to the MessageLoop, so we avoid re-entrancy problems. |
467 void InvokeUserStreamCreationCallback(scoped_refptr<SpdyStream>* stream); | 515 void InvokeUserStreamCreationCallback(scoped_refptr<SpdyStream>* stream); |
468 | 516 |
469 // BufferedSpdyFramerVisitorInterface: | 517 // BufferedSpdyFramerVisitorInterface: |
470 virtual void OnError(SpdyFramer::SpdyError error_code) OVERRIDE; | 518 virtual void OnError(SpdyFramer::SpdyError error_code) OVERRIDE; |
471 virtual void OnStreamError(SpdyStreamId stream_id, | 519 virtual void OnStreamError(SpdyStreamId stream_id, |
472 const std::string& description) OVERRIDE; | 520 const std::string& description) OVERRIDE; |
473 virtual void OnRstStream( | 521 virtual void OnRstStream( |
474 const SpdyRstStreamControlFrame& frame) OVERRIDE; | 522 const SpdyRstStreamControlFrame& frame) OVERRIDE; |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
559 // the server to start pushing the stream]) or there are still network events | 607 // the server to start pushing the stream]) or there are still network events |
560 // incoming even though the consumer has already gone away (cancellation). | 608 // incoming even though the consumer has already gone away (cancellation). |
561 // TODO(willchan): Perhaps we should separate out cancelled streams and move | 609 // TODO(willchan): Perhaps we should separate out cancelled streams and move |
562 // them into a separate ActiveStreamMap, and not deliver network events to | 610 // them into a separate ActiveStreamMap, and not deliver network events to |
563 // them? | 611 // them? |
564 ActiveStreamMap active_streams_; | 612 ActiveStreamMap active_streams_; |
565 // Map of all the streams that have already started to be pushed by the | 613 // Map of all the streams that have already started to be pushed by the |
566 // server, but do not have consumers yet. | 614 // server, but do not have consumers yet. |
567 PushedStreamMap unclaimed_pushed_streams_; | 615 PushedStreamMap unclaimed_pushed_streams_; |
568 | 616 |
569 // As we gather data to be sent, we put it into the output queue. | 617 // Set of all created streams but that have not yet sent any frames. |
570 OutputQueue queue_; | 618 CreatedStreamSet created_streams_; |
| 619 |
| 620 // As streams have data to be sent, we put them into the write queue. |
| 621 WriteQueue write_queue_; |
| 622 |
| 623 // Mapping from SpdyIOBufferProducers to their corresponding SpdyStream |
| 624 // so that when a stream is destroyed, we can remove the corresponding |
| 625 // producer from |write_queue_|. |
| 626 StreamProducerMap stream_producers_; |
571 | 627 |
572 // The packet we are currently sending. | 628 // The packet we are currently sending. |
573 bool write_pending_; // Will be true when a write is in progress. | 629 bool write_pending_; // Will be true when a write is in progress. |
574 SpdyIOBuffer in_flight_write_; // This is the write buffer in progress. | 630 SpdyIOBuffer in_flight_write_; // This is the write buffer in progress. |
575 | 631 |
576 // Flag if we have a pending message scheduled for WriteSocket. | 632 // Flag if we have a pending message scheduled for WriteSocket. |
577 bool delayed_write_pending_; | 633 bool delayed_write_pending_; |
578 | 634 |
579 // Flag if we're using an SSL connection for this SpdySession. | 635 // Flag if we're using an SSL connection for this SpdySession. |
580 bool is_secure_; | 636 bool is_secure_; |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
666 base::TimeDelta hung_interval_; | 722 base::TimeDelta hung_interval_; |
667 | 723 |
668 // This SPDY proxy is allowed to push resources from origins that are | 724 // This SPDY proxy is allowed to push resources from origins that are |
669 // different from those of their associated streams. | 725 // different from those of their associated streams. |
670 HostPortPair trusted_spdy_proxy_; | 726 HostPortPair trusted_spdy_proxy_; |
671 }; | 727 }; |
672 | 728 |
673 } // namespace net | 729 } // namespace net |
674 | 730 |
675 #endif // NET_SPDY_SPDY_SESSION_H_ | 731 #endif // NET_SPDY_SPDY_SESSION_H_ |
OLD | NEW |