OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // TODO(ukai): code is similar with http_network_transaction.cc. We should | 5 // TODO(ukai): code is similar with http_network_transaction.cc. We should |
6 // think about ways to share code, if possible. | 6 // think about ways to share code, if possible. |
7 | 7 |
8 #include "net/socket_stream/socket_stream.h" | 8 #include "net/socket_stream/socket_stream.h" |
9 | 9 |
10 #include <set> | 10 #include <set> |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 proxy_mode_(kDirectConnection), | 64 proxy_mode_(kDirectConnection), |
65 proxy_url_(url), | 65 proxy_url_(url), |
66 pac_request_(NULL), | 66 pac_request_(NULL), |
67 // Unretained() is required; without it, Bind() creates a circular | 67 // Unretained() is required; without it, Bind() creates a circular |
68 // dependency and the SocketStream object will not be freed. | 68 // dependency and the SocketStream object will not be freed. |
69 ALLOW_THIS_IN_INITIALIZER_LIST( | 69 ALLOW_THIS_IN_INITIALIZER_LIST( |
70 io_callback_(base::Bind(&SocketStream::OnIOCompleted, | 70 io_callback_(base::Bind(&SocketStream::OnIOCompleted, |
71 base::Unretained(this)))), | 71 base::Unretained(this)))), |
72 ALLOW_THIS_IN_INITIALIZER_LIST( | 72 ALLOW_THIS_IN_INITIALIZER_LIST( |
73 io_callback_old_(this, &SocketStream::OnIOCompleted)), | 73 io_callback_old_(this, &SocketStream::OnIOCompleted)), |
74 ALLOW_THIS_IN_INITIALIZER_LIST( | |
75 read_callback_old_(this, &SocketStream::OnReadCompleted)), | |
76 ALLOW_THIS_IN_INITIALIZER_LIST( | |
77 write_callback_old_(this, &SocketStream::OnWriteCompleted)), | |
78 read_buf_(NULL), | 74 read_buf_(NULL), |
79 write_buf_(NULL), | 75 write_buf_(NULL), |
80 current_write_buf_(NULL), | 76 current_write_buf_(NULL), |
81 write_buf_offset_(0), | 77 write_buf_offset_(0), |
82 write_buf_size_(0), | 78 write_buf_size_(0), |
83 closing_(false), | 79 closing_(false), |
84 server_closed_(false), | 80 server_closed_(false), |
85 metrics_(new SocketStreamMetrics(url)) { | 81 metrics_(new SocketStreamMetrics(url)) { |
86 DCHECK(MessageLoop::current()) << | 82 DCHECK(MessageLoop::current()) << |
87 "The current MessageLoop must exist"; | 83 "The current MessageLoop must exist"; |
(...skipping 573 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
661 if (result != OK) { | 657 if (result != OK) { |
662 next_state_ = STATE_CLOSE; | 658 next_state_ = STATE_CLOSE; |
663 return result; | 659 return result; |
664 } | 660 } |
665 next_state_ = STATE_TCP_CONNECT_COMPLETE; | 661 next_state_ = STATE_TCP_CONNECT_COMPLETE; |
666 DCHECK(factory_); | 662 DCHECK(factory_); |
667 socket_.reset(factory_->CreateTransportClientSocket(addresses_, | 663 socket_.reset(factory_->CreateTransportClientSocket(addresses_, |
668 net_log_.net_log(), | 664 net_log_.net_log(), |
669 net_log_.source())); | 665 net_log_.source())); |
670 metrics_->OnStartConnection(); | 666 metrics_->OnStartConnection(); |
671 return socket_->Connect(&io_callback_old_); | 667 return socket_->Connect(io_callback_); |
672 } | 668 } |
673 | 669 |
674 int SocketStream::DoTcpConnectComplete(int result) { | 670 int SocketStream::DoTcpConnectComplete(int result) { |
675 // TODO(ukai): if error occured, reconsider proxy after error. | 671 // TODO(ukai): if error occured, reconsider proxy after error. |
676 if (result != OK) { | 672 if (result != OK) { |
677 next_state_ = STATE_CLOSE; | 673 next_state_ = STATE_CLOSE; |
678 return result; | 674 return result; |
679 } | 675 } |
680 | 676 |
681 if (proxy_mode_ == kTunnelProxy) { | 677 if (proxy_mode_ == kTunnelProxy) { |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
754 GetHostAndPort(url_).c_str(), | 750 GetHostAndPort(url_).c_str(), |
755 GetHostAndOptionalPort(url_).c_str()); | 751 GetHostAndOptionalPort(url_).c_str()); |
756 if (!authorization_headers.empty()) | 752 if (!authorization_headers.empty()) |
757 tunnel_request_headers_->headers_ += authorization_headers; | 753 tunnel_request_headers_->headers_ += authorization_headers; |
758 tunnel_request_headers_->headers_ += "\r\n"; | 754 tunnel_request_headers_->headers_ += "\r\n"; |
759 } | 755 } |
760 tunnel_request_headers_->SetDataOffset(tunnel_request_headers_bytes_sent_); | 756 tunnel_request_headers_->SetDataOffset(tunnel_request_headers_bytes_sent_); |
761 int buf_len = static_cast<int>(tunnel_request_headers_->headers_.size() - | 757 int buf_len = static_cast<int>(tunnel_request_headers_->headers_.size() - |
762 tunnel_request_headers_bytes_sent_); | 758 tunnel_request_headers_bytes_sent_); |
763 DCHECK_GT(buf_len, 0); | 759 DCHECK_GT(buf_len, 0); |
764 return socket_->Write(tunnel_request_headers_, buf_len, &io_callback_old_); | 760 return socket_->Write(tunnel_request_headers_, buf_len, io_callback_); |
765 } | 761 } |
766 | 762 |
767 int SocketStream::DoWriteTunnelHeadersComplete(int result) { | 763 int SocketStream::DoWriteTunnelHeadersComplete(int result) { |
768 DCHECK_EQ(kTunnelProxy, proxy_mode_); | 764 DCHECK_EQ(kTunnelProxy, proxy_mode_); |
769 | 765 |
770 if (result < 0) { | 766 if (result < 0) { |
771 next_state_ = STATE_CLOSE; | 767 next_state_ = STATE_CLOSE; |
772 return result; | 768 return result; |
773 } | 769 } |
774 | 770 |
(...skipping 16 matching lines...) Expand all Loading... |
791 tunnel_response_headers_capacity_ = kMaxTunnelResponseHeadersSize; | 787 tunnel_response_headers_capacity_ = kMaxTunnelResponseHeadersSize; |
792 tunnel_response_headers_->Realloc(tunnel_response_headers_capacity_); | 788 tunnel_response_headers_->Realloc(tunnel_response_headers_capacity_); |
793 tunnel_response_headers_len_ = 0; | 789 tunnel_response_headers_len_ = 0; |
794 } | 790 } |
795 | 791 |
796 int buf_len = tunnel_response_headers_capacity_ - | 792 int buf_len = tunnel_response_headers_capacity_ - |
797 tunnel_response_headers_len_; | 793 tunnel_response_headers_len_; |
798 tunnel_response_headers_->SetDataOffset(tunnel_response_headers_len_); | 794 tunnel_response_headers_->SetDataOffset(tunnel_response_headers_len_); |
799 CHECK(tunnel_response_headers_->data()); | 795 CHECK(tunnel_response_headers_->data()); |
800 | 796 |
801 return socket_->Read(tunnel_response_headers_, buf_len, &io_callback_old_); | 797 return socket_->Read(tunnel_response_headers_, buf_len, io_callback_); |
802 } | 798 } |
803 | 799 |
804 int SocketStream::DoReadTunnelHeadersComplete(int result) { | 800 int SocketStream::DoReadTunnelHeadersComplete(int result) { |
805 DCHECK_EQ(kTunnelProxy, proxy_mode_); | 801 DCHECK_EQ(kTunnelProxy, proxy_mode_); |
806 | 802 |
807 if (result < 0) { | 803 if (result < 0) { |
808 next_state_ = STATE_CLOSE; | 804 next_state_ = STATE_CLOSE; |
809 return result; | 805 return result; |
810 } | 806 } |
811 | 807 |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
888 StreamSocket* s = socket_.release(); | 884 StreamSocket* s = socket_.release(); |
889 HostResolver::RequestInfo req_info(HostPortPair::FromURL(url_)); | 885 HostResolver::RequestInfo req_info(HostPortPair::FromURL(url_)); |
890 | 886 |
891 DCHECK(!proxy_info_.is_empty()); | 887 DCHECK(!proxy_info_.is_empty()); |
892 if (proxy_info_.proxy_server().scheme() == ProxyServer::SCHEME_SOCKS5) | 888 if (proxy_info_.proxy_server().scheme() == ProxyServer::SCHEME_SOCKS5) |
893 s = new SOCKS5ClientSocket(s, req_info); | 889 s = new SOCKS5ClientSocket(s, req_info); |
894 else | 890 else |
895 s = new SOCKSClientSocket(s, req_info, host_resolver_); | 891 s = new SOCKSClientSocket(s, req_info, host_resolver_); |
896 socket_.reset(s); | 892 socket_.reset(s); |
897 metrics_->OnCountConnectionType(SocketStreamMetrics::SOCKS_CONNECTION); | 893 metrics_->OnCountConnectionType(SocketStreamMetrics::SOCKS_CONNECTION); |
898 return socket_->Connect(&io_callback_old_); | 894 return socket_->Connect(io_callback_); |
899 } | 895 } |
900 | 896 |
901 int SocketStream::DoSOCKSConnectComplete(int result) { | 897 int SocketStream::DoSOCKSConnectComplete(int result) { |
902 DCHECK_EQ(kSOCKSProxy, proxy_mode_); | 898 DCHECK_EQ(kSOCKSProxy, proxy_mode_); |
903 | 899 |
904 if (result == OK) { | 900 if (result == OK) { |
905 if (is_secure()) | 901 if (is_secure()) |
906 next_state_ = STATE_SSL_CONNECT; | 902 next_state_ = STATE_SSL_CONNECT; |
907 else | 903 else |
908 result = DidEstablishConnection(); | 904 result = DidEstablishConnection(); |
(...skipping 10 matching lines...) Expand all Loading... |
919 ssl_context.origin_bound_cert_service = origin_bound_cert_service_; | 915 ssl_context.origin_bound_cert_service = origin_bound_cert_service_; |
920 // TODO(agl): look into plumbing SSLHostInfo here. | 916 // TODO(agl): look into plumbing SSLHostInfo here. |
921 socket_.reset(factory_->CreateSSLClientSocket( | 917 socket_.reset(factory_->CreateSSLClientSocket( |
922 socket_.release(), | 918 socket_.release(), |
923 proxy_info_.proxy_server().host_port_pair(), | 919 proxy_info_.proxy_server().host_port_pair(), |
924 proxy_ssl_config_, | 920 proxy_ssl_config_, |
925 NULL /* ssl_host_info */, | 921 NULL /* ssl_host_info */, |
926 ssl_context)); | 922 ssl_context)); |
927 next_state_ = STATE_SECURE_PROXY_CONNECT_COMPLETE; | 923 next_state_ = STATE_SECURE_PROXY_CONNECT_COMPLETE; |
928 metrics_->OnCountConnectionType(SocketStreamMetrics::SECURE_PROXY_CONNECTION); | 924 metrics_->OnCountConnectionType(SocketStreamMetrics::SECURE_PROXY_CONNECTION); |
929 return socket_->Connect(&io_callback_old_); | 925 return socket_->Connect(io_callback_); |
930 } | 926 } |
931 | 927 |
932 int SocketStream::DoSecureProxyConnectComplete(int result) { | 928 int SocketStream::DoSecureProxyConnectComplete(int result) { |
933 DCHECK_EQ(STATE_NONE, next_state_); | 929 DCHECK_EQ(STATE_NONE, next_state_); |
934 result = DidEstablishSSL(result, &proxy_ssl_config_); | 930 result = DidEstablishSSL(result, &proxy_ssl_config_); |
935 if (next_state_ != STATE_NONE) | 931 if (next_state_ != STATE_NONE) |
936 return result; | 932 return result; |
937 if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) | 933 if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) |
938 return HandleCertificateRequest(result); | 934 return HandleCertificateRequest(result); |
939 if (result == OK) | 935 if (result == OK) |
940 next_state_ = STATE_WRITE_TUNNEL_HEADERS; | 936 next_state_ = STATE_WRITE_TUNNEL_HEADERS; |
941 else | 937 else |
942 next_state_ = STATE_CLOSE; | 938 next_state_ = STATE_CLOSE; |
943 return result; | 939 return result; |
944 } | 940 } |
945 | 941 |
946 int SocketStream::DoSSLConnect() { | 942 int SocketStream::DoSSLConnect() { |
947 DCHECK(factory_); | 943 DCHECK(factory_); |
948 SSLClientSocketContext ssl_context; | 944 SSLClientSocketContext ssl_context; |
949 ssl_context.cert_verifier = cert_verifier_; | 945 ssl_context.cert_verifier = cert_verifier_; |
950 ssl_context.origin_bound_cert_service = origin_bound_cert_service_; | 946 ssl_context.origin_bound_cert_service = origin_bound_cert_service_; |
951 // TODO(agl): look into plumbing SSLHostInfo here. | 947 // TODO(agl): look into plumbing SSLHostInfo here. |
952 socket_.reset(factory_->CreateSSLClientSocket(socket_.release(), | 948 socket_.reset(factory_->CreateSSLClientSocket(socket_.release(), |
953 HostPortPair::FromURL(url_), | 949 HostPortPair::FromURL(url_), |
954 server_ssl_config_, | 950 server_ssl_config_, |
955 NULL /* ssl_host_info */, | 951 NULL /* ssl_host_info */, |
956 ssl_context)); | 952 ssl_context)); |
957 next_state_ = STATE_SSL_CONNECT_COMPLETE; | 953 next_state_ = STATE_SSL_CONNECT_COMPLETE; |
958 metrics_->OnCountConnectionType(SocketStreamMetrics::SSL_CONNECTION); | 954 metrics_->OnCountConnectionType(SocketStreamMetrics::SSL_CONNECTION); |
959 return socket_->Connect(&io_callback_old_); | 955 return socket_->Connect(io_callback_); |
960 } | 956 } |
961 | 957 |
962 int SocketStream::DoSSLConnectComplete(int result) { | 958 int SocketStream::DoSSLConnectComplete(int result) { |
963 DCHECK_EQ(STATE_NONE, next_state_); | 959 DCHECK_EQ(STATE_NONE, next_state_); |
964 result = DidEstablishSSL(result, &server_ssl_config_); | 960 result = DidEstablishSSL(result, &server_ssl_config_); |
965 if (next_state_ != STATE_NONE) | 961 if (next_state_ != STATE_NONE) |
966 return result; | 962 return result; |
967 // TODO(toyoshim): Upgrade to SPDY through TLS NPN extension if possible. | 963 // TODO(toyoshim): Upgrade to SPDY through TLS NPN extension if possible. |
968 // If we use HTTPS and this is the first connection to the SPDY server, | 964 // If we use HTTPS and this is the first connection to the SPDY server, |
969 // we should take care of TLS NPN extension here. | 965 // we should take care of TLS NPN extension here. |
(...skipping 24 matching lines...) Expand all Loading... |
994 return OK; | 990 return OK; |
995 } | 991 } |
996 | 992 |
997 next_state_ = STATE_READ_WRITE; | 993 next_state_ = STATE_READ_WRITE; |
998 | 994 |
999 // If server already closed the socket, we don't try to read. | 995 // If server already closed the socket, we don't try to read. |
1000 if (!server_closed_) { | 996 if (!server_closed_) { |
1001 if (!read_buf_) { | 997 if (!read_buf_) { |
1002 // No read pending and server didn't close the socket. | 998 // No read pending and server didn't close the socket. |
1003 read_buf_ = new IOBuffer(kReadBufferSize); | 999 read_buf_ = new IOBuffer(kReadBufferSize); |
1004 result = socket_->Read(read_buf_, kReadBufferSize, &read_callback_old_); | 1000 result = socket_->Read(read_buf_, kReadBufferSize, |
| 1001 base::Bind(&SocketStream::OnReadCompleted, |
| 1002 base::Unretained(this))); |
1005 if (result > 0) { | 1003 if (result > 0) { |
1006 return DidReceiveData(result); | 1004 return DidReceiveData(result); |
1007 } else if (result == 0) { | 1005 } else if (result == 0) { |
1008 // 0 indicates end-of-file, so socket was closed. | 1006 // 0 indicates end-of-file, so socket was closed. |
1009 next_state_ = STATE_CLOSE; | 1007 next_state_ = STATE_CLOSE; |
1010 server_closed_ = true; | 1008 server_closed_ = true; |
1011 return ERR_CONNECTION_CLOSED; | 1009 return ERR_CONNECTION_CLOSED; |
1012 } | 1010 } |
1013 // If read is pending, try write as well. | 1011 // If read is pending, try write as well. |
1014 // Otherwise, return the result and do next loop (to close the | 1012 // Otherwise, return the result and do next loop (to close the |
1015 // connection). | 1013 // connection). |
1016 if (result != ERR_IO_PENDING) { | 1014 if (result != ERR_IO_PENDING) { |
1017 next_state_ = STATE_CLOSE; | 1015 next_state_ = STATE_CLOSE; |
1018 server_closed_ = true; | 1016 server_closed_ = true; |
1019 return result; | 1017 return result; |
1020 } | 1018 } |
1021 } | 1019 } |
1022 // Read is pending. | 1020 // Read is pending. |
1023 DCHECK(read_buf_); | 1021 DCHECK(read_buf_); |
1024 } | 1022 } |
1025 | 1023 |
1026 if (write_buf_ && !current_write_buf_) { | 1024 if (write_buf_ && !current_write_buf_) { |
1027 // No write pending. | 1025 // No write pending. |
1028 current_write_buf_ = new DrainableIOBuffer(write_buf_, write_buf_size_); | 1026 current_write_buf_ = new DrainableIOBuffer(write_buf_, write_buf_size_); |
1029 current_write_buf_->SetOffset(write_buf_offset_); | 1027 current_write_buf_->SetOffset(write_buf_offset_); |
1030 result = socket_->Write(current_write_buf_, | 1028 result = socket_->Write(current_write_buf_, |
1031 current_write_buf_->BytesRemaining(), | 1029 current_write_buf_->BytesRemaining(), |
1032 &write_callback_old_); | 1030 base::Bind(&SocketStream::OnWriteCompleted, |
| 1031 base::Unretained(this))); |
1033 if (result > 0) { | 1032 if (result > 0) { |
1034 return DidSendData(result); | 1033 return DidSendData(result); |
1035 } | 1034 } |
1036 // If write is not pending, return the result and do next loop (to close | 1035 // If write is not pending, return the result and do next loop (to close |
1037 // the connection). | 1036 // the connection). |
1038 if (result != 0 && result != ERR_IO_PENDING) { | 1037 if (result != 0 && result != ERR_IO_PENDING) { |
1039 next_state_ = STATE_CLOSE; | 1038 next_state_ = STATE_CLOSE; |
1040 return result; | 1039 return result; |
1041 } | 1040 } |
1042 return result; | 1041 return result; |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1188 | 1187 |
1189 SSLConfigService* SocketStream::ssl_config_service() const { | 1188 SSLConfigService* SocketStream::ssl_config_service() const { |
1190 return context_->ssl_config_service(); | 1189 return context_->ssl_config_service(); |
1191 } | 1190 } |
1192 | 1191 |
1193 ProxyService* SocketStream::proxy_service() const { | 1192 ProxyService* SocketStream::proxy_service() const { |
1194 return context_->proxy_service(); | 1193 return context_->proxy_service(); |
1195 } | 1194 } |
1196 | 1195 |
1197 } // namespace net | 1196 } // namespace net |
OLD | NEW |