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 | 7 |
8 #include <deque> | 8 #include <deque> |
9 #include <list> | |
10 #include <map> | 9 #include <map> |
11 #include <queue> | |
12 #include <set> | 10 #include <set> |
13 #include <string> | 11 #include <string> |
14 | 12 |
15 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
16 #include "base/gtest_prod_util.h" | 14 #include "base/gtest_prod_util.h" |
17 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
| 16 #include "base/memory/scoped_ptr.h" |
18 #include "base/memory/weak_ptr.h" | 17 #include "base/memory/weak_ptr.h" |
19 #include "base/time.h" | 18 #include "base/time.h" |
20 #include "net/base/io_buffer.h" | 19 #include "net/base/io_buffer.h" |
21 #include "net/base/load_states.h" | 20 #include "net/base/load_states.h" |
22 #include "net/base/net_errors.h" | 21 #include "net/base/net_errors.h" |
| 22 #include "net/base/net_export.h" |
23 #include "net/base/request_priority.h" | 23 #include "net/base/request_priority.h" |
24 #include "net/socket/client_socket_handle.h" | 24 #include "net/socket/client_socket_handle.h" |
25 #include "net/socket/ssl_client_socket.h" | 25 #include "net/socket/ssl_client_socket.h" |
26 #include "net/socket/stream_socket.h" | 26 #include "net/socket/stream_socket.h" |
27 #include "net/spdy/buffered_spdy_framer.h" | 27 #include "net/spdy/buffered_spdy_framer.h" |
28 #include "net/spdy/spdy_credential_state.h" | 28 #include "net/spdy/spdy_credential_state.h" |
29 #include "net/spdy/spdy_header_block.h" | 29 #include "net/spdy/spdy_header_block.h" |
30 #include "net/spdy/spdy_io_buffer.h" | 30 #include "net/spdy/spdy_io_buffer.h" |
31 #include "net/spdy/spdy_protocol.h" | 31 #include "net/spdy/spdy_protocol.h" |
32 #include "net/spdy/spdy_session_pool.h" | 32 #include "net/spdy/spdy_session_pool.h" |
| 33 #include "net/spdy/spdy_write_queue.h" |
33 #include "net/ssl/ssl_client_cert_type.h" | 34 #include "net/ssl/ssl_client_cert_type.h" |
34 #include "net/ssl/ssl_config_service.h" | 35 #include "net/ssl/ssl_config_service.h" |
35 | 36 |
36 namespace net { | 37 namespace net { |
37 | 38 |
38 // This is somewhat arbitrary and not really fixed, but it will always work | 39 // This is somewhat arbitrary and not really fixed, but it will always work |
39 // reasonably with ethernet. Chop the world into 2-packet chunks. This is | 40 // reasonably with ethernet. Chop the world into 2-packet chunks. This is |
40 // somewhat arbitrary, but is reasonably small and ensures that we elicit | 41 // somewhat arbitrary, but is reasonably small and ensures that we elicit |
41 // ACKs quickly from TCP (because TCP tries to only ACK every other packet). | 42 // ACKs quickly from TCP (because TCP tries to only ACK every other packet). |
42 const int kMss = 1430; | 43 const int kMss = 1430; |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 // TODO(akalin): Use base::TickClock when it becomes available. | 171 // TODO(akalin): Use base::TickClock when it becomes available. |
171 typedef base::TimeTicks (*TimeFunc)(void); | 172 typedef base::TimeTicks (*TimeFunc)(void); |
172 | 173 |
173 // How we handle flow control (version-dependent). | 174 // How we handle flow control (version-dependent). |
174 enum FlowControlState { | 175 enum FlowControlState { |
175 FLOW_CONTROL_NONE, | 176 FLOW_CONTROL_NONE, |
176 FLOW_CONTROL_STREAM, | 177 FLOW_CONTROL_STREAM, |
177 FLOW_CONTROL_STREAM_AND_SESSION | 178 FLOW_CONTROL_STREAM_AND_SESSION |
178 }; | 179 }; |
179 | 180 |
180 // Defines an interface for producing SpdyIOBuffers. | |
181 class NET_EXPORT_PRIVATE SpdyIOBufferProducer { | |
182 public: | |
183 SpdyIOBufferProducer() {} | |
184 | |
185 // Returns a newly created SpdyIOBuffer, owned by the caller, or NULL | |
186 // if not buffer is ready to be produced. | |
187 virtual SpdyIOBuffer* ProduceNextBuffer(SpdySession* session) = 0; | |
188 | |
189 virtual RequestPriority GetPriority() const = 0; | |
190 | |
191 virtual ~SpdyIOBufferProducer() {} | |
192 | |
193 protected: | |
194 // Activates |spdy_stream| in |spdy_session|. | |
195 static void ActivateStream(SpdySession* spdy_session, | |
196 SpdyStream* spdy_stream); | |
197 | |
198 static SpdyIOBuffer* CreateIOBuffer(SpdyFrame* frame, | |
199 RequestPriority priority, | |
200 SpdyStream* spdy_stream); | |
201 }; | |
202 | |
203 // Create a new SpdySession. | 181 // Create a new SpdySession. |
204 // |host_port_proxy_pair| is the host/port that this session connects to, and | 182 // |host_port_proxy_pair| is the host/port that this session connects to, and |
205 // the proxy configuration settings that it's using. | 183 // the proxy configuration settings that it's using. |
206 // |spdy_session_pool| is the SpdySessionPool that owns us. Its lifetime must | 184 // |spdy_session_pool| is the SpdySessionPool that owns us. Its lifetime must |
207 // strictly be greater than |this|. | 185 // strictly be greater than |this|. |
208 // |session| is the HttpNetworkSession. |net_log| is the NetLog that we log | 186 // |session| is the HttpNetworkSession. |net_log| is the NetLog that we log |
209 // network events to. | 187 // network events to. |
210 SpdySession(const HostPortProxyPair& host_port_proxy_pair, | 188 SpdySession(const HostPortProxyPair& host_port_proxy_pair, |
211 SpdySessionPool* spdy_session_pool, | 189 SpdySessionPool* spdy_session_pool, |
212 HttpServerProperties* http_server_properties, | 190 HttpServerProperties* http_server_properties, |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 // For SSL-based sessions, verifies that the server certificate in use by | 233 // For SSL-based sessions, verifies that the server certificate in use by |
256 // this session provides authentication for the domain and no client | 234 // this session provides authentication for the domain and no client |
257 // certificate or channel ID was sent to the original server during the SSL | 235 // certificate or channel ID was sent to the original server during the SSL |
258 // handshake. NOTE: This function can have false negatives on some | 236 // handshake. NOTE: This function can have false negatives on some |
259 // platforms. | 237 // platforms. |
260 // TODO(wtc): rename this function and the Net.SpdyIPPoolDomainMatch | 238 // TODO(wtc): rename this function and the Net.SpdyIPPoolDomainMatch |
261 // histogram because this function does more than verifying domain | 239 // histogram because this function does more than verifying domain |
262 // authentication now. | 240 // authentication now. |
263 bool VerifyDomainAuthentication(const std::string& domain); | 241 bool VerifyDomainAuthentication(const std::string& domain); |
264 | 242 |
265 // Records that |stream| has a write available from |producer|. | 243 // Pushes the given producer into the write queue for |
266 // |producer| will be owned by this SpdySession. | 244 // |stream|. |stream| is guaranteed to be activated before the |
267 void SetStreamHasWriteAvailable(SpdyStream* stream, | 245 // producer is used to produce its frame. |
268 SpdyIOBufferProducer* producer); | 246 void EnqueueStreamWrite(SpdyStream* stream, |
| 247 scoped_ptr<SpdyFrameProducer> producer); |
269 | 248 |
270 // Send the SYN frame for |stream_id|. This also sends PING message to check | 249 // Creates and returns a SYN frame for |stream_id|. |
271 // the status of the connection. | 250 scoped_ptr<SpdyFrame> CreateSynStream( |
272 SpdyFrame* CreateSynStream( | |
273 SpdyStreamId stream_id, | 251 SpdyStreamId stream_id, |
274 RequestPriority priority, | 252 RequestPriority priority, |
275 uint8 credential_slot, | 253 uint8 credential_slot, |
276 SpdyControlFlags flags, | 254 SpdyControlFlags flags, |
277 const SpdyHeaderBlock& headers); | 255 const SpdyHeaderBlock& headers); |
278 | 256 |
279 // Write a CREDENTIAL frame to the session. | 257 // Tries to create a CREDENTIAL frame. If successful, fills in |
280 SpdyFrame* CreateCredentialFrame(const std::string& origin, | 258 // |credential_frame| and returns OK. Returns the error (guaranteed |
281 SSLClientCertType type, | 259 // to not be ERR_IO_PENDING) otherwise. |
282 const std::string& key, | 260 int CreateCredentialFrame(const std::string& origin, |
283 const std::string& cert, | 261 SSLClientCertType type, |
284 RequestPriority priority); | 262 const std::string& key, |
| 263 const std::string& cert, |
| 264 RequestPriority priority, |
| 265 scoped_ptr<SpdyFrame>* credential_frame); |
285 | 266 |
286 // Write a HEADERS frame to the stream. | 267 // Creates and returns a HEADERS frame. |
287 SpdyFrame* CreateHeadersFrame(SpdyStreamId stream_id, | 268 scoped_ptr<SpdyFrame> CreateHeadersFrame(SpdyStreamId stream_id, |
288 const SpdyHeaderBlock& headers, | 269 const SpdyHeaderBlock& headers, |
289 SpdyControlFlags flags); | 270 SpdyControlFlags flags); |
290 | 271 |
291 // Write a data frame to the stream. | 272 // Creates and returns a data frame. May return NULL if stalled by |
292 // Used to create and queue a data frame for the given stream. | 273 // flow control. |
293 SpdyFrame* CreateDataFrame(SpdyStreamId stream_id, | 274 scoped_ptr<SpdyFrame> CreateDataFrame(SpdyStreamId stream_id, |
294 net::IOBuffer* data, int len, | 275 net::IOBuffer* data, int len, |
295 SpdyDataFlags flags); | 276 SpdyDataFlags flags); |
296 | 277 |
297 // Close a stream. | 278 // Close a stream. |
298 void CloseStream(SpdyStreamId stream_id, int status); | 279 void CloseStream(SpdyStreamId stream_id, int status); |
299 | 280 |
300 // Close a stream that has been created but is not yet active. | 281 // Close a stream that has been created but is not yet active. |
301 void CloseCreatedStream(SpdyStream* stream, int status); | 282 void CloseCreatedStream(SpdyStream* stream, int status); |
302 | 283 |
303 // Reset a stream by sending a RST_STREAM frame with given status code. | 284 // Reset a stream by sending a RST_STREAM frame with given status code. |
304 // Also closes the stream. Was not piggybacked to CloseStream since not | 285 // Also closes the stream. Was not piggybacked to CloseStream since not |
305 // all of the calls to CloseStream necessitate sending a RST_STREAM. | 286 // all of the calls to CloseStream necessitate sending a RST_STREAM. |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
468 FRIEND_TEST_ALL_PREFIXES(SpdySessionSpdy3Test, SessionFlowControlEndToEnd31); | 449 FRIEND_TEST_ALL_PREFIXES(SpdySessionSpdy3Test, SessionFlowControlEndToEnd31); |
469 | 450 |
470 typedef std::deque<SpdyStreamRequest*> PendingStreamRequestQueue; | 451 typedef std::deque<SpdyStreamRequest*> PendingStreamRequestQueue; |
471 typedef std::set<SpdyStreamRequest*> PendingStreamRequestCompletionSet; | 452 typedef std::set<SpdyStreamRequest*> PendingStreamRequestCompletionSet; |
472 | 453 |
473 typedef std::map<int, scoped_refptr<SpdyStream> > ActiveStreamMap; | 454 typedef std::map<int, scoped_refptr<SpdyStream> > ActiveStreamMap; |
474 typedef std::map<std::string, | 455 typedef std::map<std::string, |
475 std::pair<scoped_refptr<SpdyStream>, base::TimeTicks> > PushedStreamMap; | 456 std::pair<scoped_refptr<SpdyStream>, base::TimeTicks> > PushedStreamMap; |
476 | 457 |
477 typedef std::set<scoped_refptr<SpdyStream> > CreatedStreamSet; | 458 typedef std::set<scoped_refptr<SpdyStream> > CreatedStreamSet; |
478 typedef std::map<SpdyIOBufferProducer*, SpdyStream*> StreamProducerMap; | |
479 | |
480 class SpdyIOBufferProducerCompare { | |
481 public: | |
482 bool operator() (const SpdyIOBufferProducer* lhs, | |
483 const SpdyIOBufferProducer* rhs) const { | |
484 return lhs->GetPriority() < rhs->GetPriority(); | |
485 } | |
486 }; | |
487 | |
488 typedef std::priority_queue<SpdyIOBufferProducer*, | |
489 std::vector<SpdyIOBufferProducer*>, | |
490 SpdyIOBufferProducerCompare> WriteQueue; | |
491 | 459 |
492 enum State { | 460 enum State { |
493 STATE_IDLE, | 461 STATE_IDLE, |
494 STATE_CONNECTING, | 462 STATE_CONNECTING, |
495 STATE_DO_READ, | 463 STATE_DO_READ, |
496 STATE_DO_READ_COMPLETE, | 464 STATE_DO_READ_COMPLETE, |
497 STATE_CLOSED | 465 STATE_CLOSED |
498 }; | 466 }; |
499 | 467 |
500 virtual ~SpdySession(); | 468 virtual ~SpdySession(); |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
575 // haven't received any data in |kHungInterval| time period. | 543 // haven't received any data in |kHungInterval| time period. |
576 void CheckPingStatus(base::TimeTicks last_check_time); | 544 void CheckPingStatus(base::TimeTicks last_check_time); |
577 | 545 |
578 // Write current data to the socket. | 546 // Write current data to the socket. |
579 void WriteSocketLater(); | 547 void WriteSocketLater(); |
580 void WriteSocket(); | 548 void WriteSocket(); |
581 | 549 |
582 // Get a new stream id. | 550 // Get a new stream id. |
583 int GetNewStreamId(); | 551 int GetNewStreamId(); |
584 | 552 |
585 // Queue a frame for sending. | 553 // Pushes the given frame with the given priority into the write |
586 // |frame| is the frame to send. | 554 // queue for the session. |
587 // |priority| is the priority for insertion into the queue. | 555 void EnqueueSessionWrite(RequestPriority priority, |
588 void QueueFrame(SpdyFrame* frame, RequestPriority priority); | 556 scoped_ptr<SpdyFrame> frame); |
| 557 |
| 558 // Puts |producer| associated with |stream| onto the write queue |
| 559 // with the given priority. |
| 560 void EnqueueWrite(RequestPriority priority, |
| 561 scoped_ptr<SpdyFrameProducer> producer, |
| 562 const scoped_refptr<SpdyStream>& stream); |
589 | 563 |
590 // Track active streams in the active stream list. | 564 // Track active streams in the active stream list. |
591 void ActivateStream(SpdyStream* stream); | 565 void ActivateStream(SpdyStream* stream); |
592 void DeleteStream(SpdyStreamId id, int status); | 566 void DeleteStream(SpdyStreamId id, int status); |
593 | 567 |
594 // Removes this session from the session pool. | 568 // Removes this session from the session pool. |
595 void RemoveFromPool(); | 569 void RemoveFromPool(); |
596 | 570 |
597 // Check if we have a pending pushed-stream for this url | 571 // Check if we have a pending pushed-stream for this url |
598 // Returns the stream if found (and returns it from the pending | 572 // Returns the stream if found (and returns it from the pending |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
761 // them? | 735 // them? |
762 ActiveStreamMap active_streams_; | 736 ActiveStreamMap active_streams_; |
763 | 737 |
764 // Map of all the streams that have already started to be pushed by the | 738 // Map of all the streams that have already started to be pushed by the |
765 // server, but do not have consumers yet. | 739 // server, but do not have consumers yet. |
766 PushedStreamMap unclaimed_pushed_streams_; | 740 PushedStreamMap unclaimed_pushed_streams_; |
767 | 741 |
768 // Set of all created streams but that have not yet sent any frames. | 742 // Set of all created streams but that have not yet sent any frames. |
769 CreatedStreamSet created_streams_; | 743 CreatedStreamSet created_streams_; |
770 | 744 |
771 // As streams have data to be sent, we put them into the write queue. | 745 // The write queue. |
772 WriteQueue write_queue_; | 746 SpdyWriteQueue write_queue_; |
773 | |
774 // Mapping from SpdyIOBufferProducers to their corresponding SpdyStream | |
775 // so that when a stream is destroyed, we can remove the corresponding | |
776 // producer from |write_queue_|. | |
777 StreamProducerMap stream_producers_; | |
778 | 747 |
779 // The packet we are currently sending. | 748 // The packet we are currently sending. |
780 bool write_pending_; // Will be true when a write is in progress. | 749 bool write_pending_; // Will be true when a write is in progress. |
781 SpdyIOBuffer in_flight_write_; // This is the write buffer in progress. | 750 SpdyIOBuffer in_flight_write_; // This is the write buffer in progress. |
782 | 751 |
783 // Flag if we have a pending message scheduled for WriteSocket. | 752 // Flag if we have a pending message scheduled for WriteSocket. |
784 bool delayed_write_pending_; | 753 bool delayed_write_pending_; |
785 | 754 |
786 // Flag if we're using an SSL connection for this SpdySession. | 755 // Flag if we're using an SSL connection for this SpdySession. |
787 bool is_secure_; | 756 bool is_secure_; |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
903 // This SPDY proxy is allowed to push resources from origins that are | 872 // This SPDY proxy is allowed to push resources from origins that are |
904 // different from those of their associated streams. | 873 // different from those of their associated streams. |
905 HostPortPair trusted_spdy_proxy_; | 874 HostPortPair trusted_spdy_proxy_; |
906 | 875 |
907 TimeFunc time_func_; | 876 TimeFunc time_func_; |
908 }; | 877 }; |
909 | 878 |
910 } // namespace net | 879 } // namespace net |
911 | 880 |
912 #endif // NET_SPDY_SPDY_SESSION_H_ | 881 #endif // NET_SPDY_SPDY_SESSION_H_ |
OLD | NEW |