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/next_proto.h" | 25 #include "net/socket/next_proto.h" |
26 #include "net/socket/ssl_client_socket.h" | 26 #include "net/socket/ssl_client_socket.h" |
27 #include "net/socket/stream_socket.h" | 27 #include "net/socket/stream_socket.h" |
28 #include "net/spdy/buffered_spdy_framer.h" | 28 #include "net/spdy/buffered_spdy_framer.h" |
29 #include "net/spdy/spdy_credential_state.h" | 29 #include "net/spdy/spdy_credential_state.h" |
30 #include "net/spdy/spdy_header_block.h" | 30 #include "net/spdy/spdy_header_block.h" |
31 #include "net/spdy/spdy_io_buffer.h" | 31 #include "net/spdy/spdy_io_buffer.h" |
32 #include "net/spdy/spdy_protocol.h" | 32 #include "net/spdy/spdy_protocol.h" |
33 #include "net/spdy/spdy_session_pool.h" | 33 #include "net/spdy/spdy_session_pool.h" |
| 34 #include "net/spdy/spdy_write_queue.h" |
34 #include "net/ssl/ssl_client_cert_type.h" | 35 #include "net/ssl/ssl_client_cert_type.h" |
35 #include "net/ssl/ssl_config_service.h" | 36 #include "net/ssl/ssl_config_service.h" |
36 | 37 |
37 namespace net { | 38 namespace net { |
38 | 39 |
39 // This is somewhat arbitrary and not really fixed, but it will always work | 40 // This is somewhat arbitrary and not really fixed, but it will always work |
40 // reasonably with ethernet. Chop the world into 2-packet chunks. This is | 41 // reasonably with ethernet. Chop the world into 2-packet chunks. This is |
41 // somewhat arbitrary, but is reasonably small and ensures that we elicit | 42 // somewhat arbitrary, but is reasonably small and ensures that we elicit |
42 // ACKs quickly from TCP (because TCP tries to only ACK every other packet). | 43 // ACKs quickly from TCP (because TCP tries to only ACK every other packet). |
43 const int kMss = 1430; | 44 const int kMss = 1430; |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 // TODO(akalin): Use base::TickClock when it becomes available. | 168 // TODO(akalin): Use base::TickClock when it becomes available. |
168 typedef base::TimeTicks (*TimeFunc)(void); | 169 typedef base::TimeTicks (*TimeFunc)(void); |
169 | 170 |
170 // How we handle flow control (version-dependent). | 171 // How we handle flow control (version-dependent). |
171 enum FlowControlState { | 172 enum FlowControlState { |
172 FLOW_CONTROL_NONE, | 173 FLOW_CONTROL_NONE, |
173 FLOW_CONTROL_STREAM, | 174 FLOW_CONTROL_STREAM, |
174 FLOW_CONTROL_STREAM_AND_SESSION | 175 FLOW_CONTROL_STREAM_AND_SESSION |
175 }; | 176 }; |
176 | 177 |
177 // Defines an interface for producing SpdyIOBuffers. | |
178 class NET_EXPORT_PRIVATE SpdyIOBufferProducer { | |
179 public: | |
180 SpdyIOBufferProducer() {} | |
181 | |
182 // Returns a newly created SpdyIOBuffer, owned by the caller, or NULL | |
183 // if not buffer is ready to be produced. | |
184 virtual SpdyIOBuffer* ProduceNextBuffer(SpdySession* session) = 0; | |
185 | |
186 virtual RequestPriority GetPriority() const = 0; | |
187 | |
188 virtual ~SpdyIOBufferProducer() {} | |
189 | |
190 protected: | |
191 // Activates |spdy_stream| in |spdy_session|. | |
192 static void ActivateStream(SpdySession* spdy_session, | |
193 SpdyStream* spdy_stream); | |
194 | |
195 static SpdyIOBuffer* CreateIOBuffer(SpdyFrame* frame, | |
196 RequestPriority priority, | |
197 SpdyStream* spdy_stream); | |
198 }; | |
199 | |
200 // Create a new SpdySession. | 178 // Create a new SpdySession. |
201 // |host_port_proxy_pair| is the host/port that this session connects to, and | 179 // |host_port_proxy_pair| is the host/port that this session connects to, and |
202 // the proxy configuration settings that it's using. | 180 // the proxy configuration settings that it's using. |
203 // |spdy_session_pool| is the SpdySessionPool that owns us. Its lifetime must | 181 // |spdy_session_pool| is the SpdySessionPool that owns us. Its lifetime must |
204 // strictly be greater than |this|. | 182 // strictly be greater than |this|. |
205 // |session| is the HttpNetworkSession. |net_log| is the NetLog that we log | 183 // |session| is the HttpNetworkSession. |net_log| is the NetLog that we log |
206 // network events to. | 184 // network events to. |
207 SpdySession(const HostPortProxyPair& host_port_proxy_pair, | 185 SpdySession(const HostPortProxyPair& host_port_proxy_pair, |
208 SpdySessionPool* spdy_session_pool, | 186 SpdySessionPool* spdy_session_pool, |
209 HttpServerProperties* http_server_properties, | 187 HttpServerProperties* http_server_properties, |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
252 // For SSL-based sessions, verifies that the server certificate in use by | 230 // For SSL-based sessions, verifies that the server certificate in use by |
253 // this session provides authentication for the domain and no client | 231 // this session provides authentication for the domain and no client |
254 // certificate or channel ID was sent to the original server during the SSL | 232 // certificate or channel ID was sent to the original server during the SSL |
255 // handshake. NOTE: This function can have false negatives on some | 233 // handshake. NOTE: This function can have false negatives on some |
256 // platforms. | 234 // platforms. |
257 // TODO(wtc): rename this function and the Net.SpdyIPPoolDomainMatch | 235 // TODO(wtc): rename this function and the Net.SpdyIPPoolDomainMatch |
258 // histogram because this function does more than verifying domain | 236 // histogram because this function does more than verifying domain |
259 // authentication now. | 237 // authentication now. |
260 bool VerifyDomainAuthentication(const std::string& domain); | 238 bool VerifyDomainAuthentication(const std::string& domain); |
261 | 239 |
262 // Records that |stream| has a write available from |producer|. | 240 // Pushes the given producer into the write queue for |
263 // |producer| will be owned by this SpdySession. | 241 // |stream|. |stream| is guaranteed to be activated before the |
264 void SetStreamHasWriteAvailable(SpdyStream* stream, | 242 // producer is used to produce its frame. |
265 SpdyIOBufferProducer* producer); | 243 void EnqueueStreamWrite(SpdyStream* stream, |
| 244 scoped_ptr<SpdyFrameProducer> producer); |
266 | 245 |
267 // Send the SYN frame for |stream_id|. This also sends PING message to check | 246 // Creates and returns a SYN frame for |stream_id|. |
268 // the status of the connection. | 247 scoped_ptr<SpdyFrame> CreateSynStream( |
269 SpdyFrame* CreateSynStream( | |
270 SpdyStreamId stream_id, | 248 SpdyStreamId stream_id, |
271 RequestPriority priority, | 249 RequestPriority priority, |
272 uint8 credential_slot, | 250 uint8 credential_slot, |
273 SpdyControlFlags flags, | 251 SpdyControlFlags flags, |
274 const SpdyHeaderBlock& headers); | 252 const SpdyHeaderBlock& headers); |
275 | 253 |
276 // Write a CREDENTIAL frame to the session. | 254 // Tries to create a CREDENTIAL frame. If successful, fills in |
277 SpdyFrame* CreateCredentialFrame(const std::string& origin, | 255 // |credential_frame| and returns OK. Returns the error (guaranteed |
278 SSLClientCertType type, | 256 // to not be ERR_IO_PENDING) otherwise. |
279 const std::string& key, | 257 int CreateCredentialFrame(const std::string& origin, |
280 const std::string& cert, | 258 SSLClientCertType type, |
281 RequestPriority priority); | 259 const std::string& key, |
| 260 const std::string& cert, |
| 261 RequestPriority priority, |
| 262 scoped_ptr<SpdyFrame>* credential_frame); |
282 | 263 |
283 // Write a HEADERS frame to the stream. | 264 // Creates and returns a HEADERS frame. |
284 SpdyFrame* CreateHeadersFrame(SpdyStreamId stream_id, | 265 scoped_ptr<SpdyFrame> CreateHeadersFrame(SpdyStreamId stream_id, |
285 const SpdyHeaderBlock& headers, | 266 const SpdyHeaderBlock& headers, |
286 SpdyControlFlags flags); | 267 SpdyControlFlags flags); |
287 | 268 |
288 // Write a data frame to the stream. | 269 // Creates and returns a data frame. May return NULL if stalled by |
289 // Used to create and queue a data frame for the given stream. | 270 // flow control. |
290 SpdyFrame* CreateDataFrame(SpdyStreamId stream_id, | 271 scoped_ptr<SpdyFrame> CreateDataFrame(SpdyStreamId stream_id, |
291 net::IOBuffer* data, int len, | 272 net::IOBuffer* data, |
292 SpdyDataFlags flags); | 273 int len, |
| 274 SpdyDataFlags flags); |
293 | 275 |
294 // Close a stream. | 276 // Close a stream. |
295 void CloseStream(SpdyStreamId stream_id, int status); | 277 void CloseStream(SpdyStreamId stream_id, int status); |
296 | 278 |
297 // Close a stream that has been created but is not yet active. | 279 // Close a stream that has been created but is not yet active. |
298 void CloseCreatedStream(SpdyStream* stream, int status); | 280 void CloseCreatedStream(SpdyStream* stream, int status); |
299 | 281 |
300 // Reset a stream by sending a RST_STREAM frame with given status code. | 282 // Reset a stream by sending a RST_STREAM frame with given status code. |
301 // Also closes the stream. Was not piggybacked to CloseStream since not | 283 // Also closes the stream. Was not piggybacked to CloseStream since not |
302 // all of the calls to CloseStream necessitate sending a RST_STREAM. | 284 // all of the calls to CloseStream necessitate sending a RST_STREAM. |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
466 FRIEND_TEST_ALL_PREFIXES(SpdySessionSpdy3Test, SessionFlowControlEndToEnd31); | 448 FRIEND_TEST_ALL_PREFIXES(SpdySessionSpdy3Test, SessionFlowControlEndToEnd31); |
467 | 449 |
468 typedef std::deque<SpdyStreamRequest*> PendingStreamRequestQueue; | 450 typedef std::deque<SpdyStreamRequest*> PendingStreamRequestQueue; |
469 typedef std::set<SpdyStreamRequest*> PendingStreamRequestCompletionSet; | 451 typedef std::set<SpdyStreamRequest*> PendingStreamRequestCompletionSet; |
470 | 452 |
471 typedef std::map<int, scoped_refptr<SpdyStream> > ActiveStreamMap; | 453 typedef std::map<int, scoped_refptr<SpdyStream> > ActiveStreamMap; |
472 typedef std::map<std::string, | 454 typedef std::map<std::string, |
473 std::pair<scoped_refptr<SpdyStream>, base::TimeTicks> > PushedStreamMap; | 455 std::pair<scoped_refptr<SpdyStream>, base::TimeTicks> > PushedStreamMap; |
474 | 456 |
475 typedef std::set<scoped_refptr<SpdyStream> > CreatedStreamSet; | 457 typedef std::set<scoped_refptr<SpdyStream> > CreatedStreamSet; |
476 typedef std::map<SpdyIOBufferProducer*, SpdyStream*> StreamProducerMap; | |
477 | |
478 class SpdyIOBufferProducerCompare { | |
479 public: | |
480 bool operator() (const SpdyIOBufferProducer* lhs, | |
481 const SpdyIOBufferProducer* rhs) const { | |
482 return lhs->GetPriority() < rhs->GetPriority(); | |
483 } | |
484 }; | |
485 | |
486 typedef std::priority_queue<SpdyIOBufferProducer*, | |
487 std::vector<SpdyIOBufferProducer*>, | |
488 SpdyIOBufferProducerCompare> WriteQueue; | |
489 | 458 |
490 enum State { | 459 enum State { |
491 IDLE, | 460 IDLE, |
492 CONNECTING, | 461 CONNECTING, |
493 CONNECTED, | 462 CONNECTED, |
494 CLOSED | 463 CLOSED |
495 }; | 464 }; |
496 | 465 |
497 virtual ~SpdySession(); | 466 virtual ~SpdySession(); |
498 | 467 |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
561 // Returns OK on success, or an error on failure. | 530 // Returns OK on success, or an error on failure. |
562 net::Error ReadSocket(); | 531 net::Error ReadSocket(); |
563 | 532 |
564 // Write current data to the socket. | 533 // Write current data to the socket. |
565 void WriteSocketLater(); | 534 void WriteSocketLater(); |
566 void WriteSocket(); | 535 void WriteSocket(); |
567 | 536 |
568 // Get a new stream id. | 537 // Get a new stream id. |
569 int GetNewStreamId(); | 538 int GetNewStreamId(); |
570 | 539 |
571 // Queue a frame for sending. | 540 // Pushes the given frame with the given priority into the write |
572 // |frame| is the frame to send. | 541 // queue for the session. |
573 // |priority| is the priority for insertion into the queue. | 542 void EnqueueSessionWrite(RequestPriority priority, |
574 void QueueFrame(SpdyFrame* frame, RequestPriority priority); | 543 scoped_ptr<SpdyFrame> frame); |
| 544 |
| 545 // Puts |producer| associated with |stream| onto the write queue |
| 546 // with the given priority. |
| 547 void EnqueueWrite(RequestPriority priority, |
| 548 scoped_ptr<SpdyFrameProducer> producer, |
| 549 const scoped_refptr<SpdyStream>& stream); |
575 | 550 |
576 // Track active streams in the active stream list. | 551 // Track active streams in the active stream list. |
577 void ActivateStream(SpdyStream* stream); | 552 void ActivateStream(SpdyStream* stream); |
578 void DeleteStream(SpdyStreamId id, int status); | 553 void DeleteStream(SpdyStreamId id, int status); |
579 | 554 |
580 // Removes this session from the session pool. | 555 // Removes this session from the session pool. |
581 void RemoveFromPool(); | 556 void RemoveFromPool(); |
582 | 557 |
583 // Check if we have a pending pushed-stream for this url | 558 // Check if we have a pending pushed-stream for this url |
584 // Returns the stream if found (and returns it from the pending | 559 // Returns the stream if found (and returns it from the pending |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
748 // them? | 723 // them? |
749 ActiveStreamMap active_streams_; | 724 ActiveStreamMap active_streams_; |
750 | 725 |
751 // Map of all the streams that have already started to be pushed by the | 726 // Map of all the streams that have already started to be pushed by the |
752 // server, but do not have consumers yet. | 727 // server, but do not have consumers yet. |
753 PushedStreamMap unclaimed_pushed_streams_; | 728 PushedStreamMap unclaimed_pushed_streams_; |
754 | 729 |
755 // Set of all created streams but that have not yet sent any frames. | 730 // Set of all created streams but that have not yet sent any frames. |
756 CreatedStreamSet created_streams_; | 731 CreatedStreamSet created_streams_; |
757 | 732 |
758 // As streams have data to be sent, we put them into the write queue. | 733 // The write queue. |
759 WriteQueue write_queue_; | 734 SpdyWriteQueue write_queue_; |
760 | |
761 // Mapping from SpdyIOBufferProducers to their corresponding SpdyStream | |
762 // so that when a stream is destroyed, we can remove the corresponding | |
763 // producer from |write_queue_|. | |
764 StreamProducerMap stream_producers_; | |
765 | 735 |
766 // The packet we are currently sending. | 736 // The packet we are currently sending. |
767 bool write_pending_; // Will be true when a write is in progress. | 737 bool write_pending_; // Will be true when a write is in progress. |
768 SpdyIOBuffer in_flight_write_; // This is the write buffer in progress. | 738 SpdyIOBuffer in_flight_write_; // This is the write buffer in progress. |
769 | 739 |
770 // Flag if we have a pending message scheduled for WriteSocket. | 740 // Flag if we have a pending message scheduled for WriteSocket. |
771 bool delayed_write_pending_; | 741 bool delayed_write_pending_; |
772 | 742 |
773 // Flag if we're using an SSL connection for this SpdySession. | 743 // Flag if we're using an SSL connection for this SpdySession. |
774 bool is_secure_; | 744 bool is_secure_; |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
882 // This SPDY proxy is allowed to push resources from origins that are | 852 // This SPDY proxy is allowed to push resources from origins that are |
883 // different from those of their associated streams. | 853 // different from those of their associated streams. |
884 HostPortPair trusted_spdy_proxy_; | 854 HostPortPair trusted_spdy_proxy_; |
885 | 855 |
886 TimeFunc time_func_; | 856 TimeFunc time_func_; |
887 }; | 857 }; |
888 | 858 |
889 } // namespace net | 859 } // namespace net |
890 | 860 |
891 #endif // NET_SPDY_SPDY_SESSION_H_ | 861 #endif // NET_SPDY_SPDY_SESSION_H_ |
OLD | NEW |