| 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 #include "net/http/http_network_transaction.h" | 5 #include "net/http/http_network_transaction.h" |
| 6 | 6 |
| 7 #include <math.h> // ceil | 7 #include <math.h> // ceil |
| 8 #include <stdarg.h> | 8 #include <stdarg.h> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 9736 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 9747 data_writes2, arraysize(data_writes2)); | 9747 data_writes2, arraysize(data_writes2)); |
| 9748 StaticSocketDataProvider* data[] = { &data1, &data2 }; | 9748 StaticSocketDataProvider* data[] = { &data1, &data2 }; |
| 9749 | 9749 |
| 9750 SimpleGetHelperResult out = SimpleGetHelperForData(data, arraysize(data)); | 9750 SimpleGetHelperResult out = SimpleGetHelperForData(data, arraysize(data)); |
| 9751 | 9751 |
| 9752 EXPECT_EQ(OK, out.rv); | 9752 EXPECT_EQ(OK, out.rv); |
| 9753 EXPECT_EQ("HTTP/1.0 200 OK", out.status_line); | 9753 EXPECT_EQ("HTTP/1.0 200 OK", out.status_line); |
| 9754 EXPECT_EQ("hello world", out.response_data); | 9754 EXPECT_EQ("hello world", out.response_data); |
| 9755 } | 9755 } |
| 9756 | 9756 |
| 9757 TEST_F(HttpNetworkTransactionSpdy2Test, DoNotUseSpdySessionForHttp) { |
| 9758 const std::string httpsUrl = "https://www.google.com/"; |
| 9759 const std::string httpUrl = "http://www.google.com:443/"; |
| 9760 |
| 9761 // SPDY GET for HTTPS URL |
| 9762 scoped_ptr<SpdyFrame> req1(ConstructSpdyGet(httpsUrl.c_str(), |
| 9763 false, 1, LOWEST)); |
| 9764 |
| 9765 MockWrite writes1[] = { |
| 9766 CreateMockWrite(*req1, 0), |
| 9767 }; |
| 9768 |
| 9769 scoped_ptr<SpdyFrame> resp1(ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 9770 scoped_ptr<SpdyFrame> body1(ConstructSpdyBodyFrame(1, true)); |
| 9771 MockRead reads1[] = { |
| 9772 CreateMockRead(*resp1, 1), |
| 9773 CreateMockRead(*body1, 2), |
| 9774 MockRead(ASYNC, ERR_IO_PENDING, 3) |
| 9775 }; |
| 9776 |
| 9777 scoped_ptr<DelayedSocketData> data1( |
| 9778 new DelayedSocketData(1, reads1, arraysize(reads1), |
| 9779 writes1, arraysize(writes1))); |
| 9780 MockConnect connect_data1(ASYNC, OK); |
| 9781 data1->set_connect_data(connect_data1); |
| 9782 |
| 9783 // HTTP GET for the HTTP URL |
| 9784 MockWrite writes2[] = { |
| 9785 MockWrite(ASYNC, 4, |
| 9786 "GET / HTTP/1.1\r\n" |
| 9787 "Host: www.google.com:443\r\n" |
| 9788 "Connection: keep-alive\r\n\r\n"), |
| 9789 }; |
| 9790 |
| 9791 MockRead reads2[] = { |
| 9792 MockRead(ASYNC, 5, "HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), |
| 9793 MockRead(ASYNC, 6, "hello"), |
| 9794 MockRead(ASYNC, 7, OK), |
| 9795 }; |
| 9796 |
| 9797 scoped_ptr<DelayedSocketData> data2( |
| 9798 new DelayedSocketData(1, reads2, arraysize(reads2), |
| 9799 writes2, arraysize(writes2))); |
| 9800 |
| 9801 SessionDependencies session_deps; |
| 9802 SSLSocketDataProvider ssl(ASYNC, OK); |
| 9803 ssl.SetNextProto(kProtoSPDY2); |
| 9804 session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 9805 session_deps.socket_factory.AddSocketDataProvider(data1.get()); |
| 9806 session_deps.socket_factory.AddSocketDataProvider(data2.get()); |
| 9807 |
| 9808 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 9809 |
| 9810 // Start the first transaction to set up the SpdySession |
| 9811 HttpRequestInfo request1; |
| 9812 request1.method = "GET"; |
| 9813 request1.url = GURL(httpsUrl); |
| 9814 request1.priority = LOWEST; |
| 9815 request1.load_flags = 0; |
| 9816 HttpNetworkTransaction trans1(session); |
| 9817 TestCompletionCallback callback1; |
| 9818 EXPECT_EQ(ERR_IO_PENDING, |
| 9819 trans1.Start(&request1, callback1.callback(), BoundNetLog())); |
| 9820 MessageLoop::current()->RunAllPending(); |
| 9821 |
| 9822 EXPECT_EQ(OK, callback1.WaitForResult()); |
| 9823 EXPECT_TRUE(trans1.GetResponseInfo()->was_fetched_via_spdy); |
| 9824 |
| 9825 // Now, start the HTTP request |
| 9826 HttpRequestInfo request2; |
| 9827 request2.method = "GET"; |
| 9828 request2.url = GURL(httpUrl); |
| 9829 request2.priority = MEDIUM; |
| 9830 request2.load_flags = 0; |
| 9831 HttpNetworkTransaction trans2(session); |
| 9832 TestCompletionCallback callback2; |
| 9833 EXPECT_EQ(ERR_IO_PENDING, |
| 9834 trans2.Start(&request2, callback2.callback(), BoundNetLog())); |
| 9835 MessageLoop::current()->RunAllPending(); |
| 9836 |
| 9837 EXPECT_EQ(OK, callback2.WaitForResult()); |
| 9838 EXPECT_FALSE(trans2.GetResponseInfo()->was_fetched_via_spdy); |
| 9839 } |
| 9840 |
| 9841 TEST_F(HttpNetworkTransactionSpdy2Test, DoNotUseSpdySessionForHttpOverTunnel) { |
| 9842 const std::string httpsUrl = "https://www.google.com/"; |
| 9843 const std::string httpUrl = "http://www.google.com:443/"; |
| 9844 |
| 9845 // SPDY GET for HTTPS URL (through CONNECT tunnel) |
| 9846 scoped_ptr<SpdyFrame> connect(ConstructSpdyConnect(NULL, 0, 1)); |
| 9847 scoped_ptr<SpdyFrame> req1(ConstructSpdyGet(httpsUrl.c_str(), |
| 9848 false, 1, LOWEST)); |
| 9849 |
| 9850 // SPDY GET for HTTP URL (through the proxy, but not the tunnel) |
| 9851 scoped_ptr<SpdyFrame> wrapped_req1(ConstructWrappedSpdyFrame(req1, 1)); |
| 9852 const char* const headers[] = { |
| 9853 "method", "GET", |
| 9854 "url", httpUrl.c_str(), |
| 9855 "host", "www.google.com:443", |
| 9856 "scheme", "http", |
| 9857 "version", "HTTP/1.1" |
| 9858 }; |
| 9859 scoped_ptr<SpdyFrame> req2(ConstructSpdyControlFrame(NULL, 0, false, 3, |
| 9860 MEDIUM, SYN_STREAM, |
| 9861 CONTROL_FLAG_FIN, |
| 9862 headers, |
| 9863 arraysize(headers))); |
| 9864 |
| 9865 MockWrite writes1[] = { |
| 9866 CreateMockWrite(*connect, 0), |
| 9867 CreateMockWrite(*wrapped_req1, 2), |
| 9868 CreateMockWrite(*req2, 5), |
| 9869 }; |
| 9870 |
| 9871 scoped_ptr<SpdyFrame> conn_resp(ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 9872 scoped_ptr<SpdyFrame> resp1(ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 9873 scoped_ptr<SpdyFrame> body1(ConstructSpdyBodyFrame(1, true)); |
| 9874 scoped_ptr<SpdyFrame> wrapped_resp1(ConstructWrappedSpdyFrame(resp1, 1)); |
| 9875 scoped_ptr<SpdyFrame> wrapped_body1(ConstructWrappedSpdyFrame(body1, 1)); |
| 9876 scoped_ptr<SpdyFrame> resp2(ConstructSpdyGetSynReply(NULL, 0, 3)); |
| 9877 scoped_ptr<SpdyFrame> body2(ConstructSpdyBodyFrame(3, true)); |
| 9878 MockRead reads1[] = { |
| 9879 CreateMockRead(*conn_resp, 1), |
| 9880 CreateMockRead(*wrapped_resp1, 3), |
| 9881 CreateMockRead(*wrapped_body1, 4), |
| 9882 CreateMockRead(*resp2, 6), |
| 9883 CreateMockRead(*body2, 7), |
| 9884 MockRead(ASYNC, ERR_IO_PENDING, 8) |
| 9885 }; |
| 9886 |
| 9887 scoped_refptr<DeterministicSocketData> data1( |
| 9888 new DeterministicSocketData(reads1, arraysize(reads1), |
| 9889 writes1, arraysize(writes1))); |
| 9890 MockConnect connect_data1(ASYNC, OK); |
| 9891 data1->set_connect_data(connect_data1); |
| 9892 |
| 9893 SpdySessionDependencies session_deps(ProxyService::CreateFixed( |
| 9894 "https://proxy:70")); |
| 9895 SSLSocketDataProvider ssl1(ASYNC, OK); // to the proxy |
| 9896 ssl1.SetNextProto(kProtoSPDY2); |
| 9897 session_deps.deterministic_socket_factory->AddSSLSocketDataProvider(&ssl1); |
| 9898 SSLSocketDataProvider ssl2(ASYNC, OK); // to the server |
| 9899 ssl2.SetNextProto(kProtoSPDY2); |
| 9900 session_deps.deterministic_socket_factory->AddSSLSocketDataProvider(&ssl2); |
| 9901 session_deps.deterministic_socket_factory->AddSocketDataProvider(data1.get()); |
| 9902 |
| 9903 scoped_refptr<HttpNetworkSession> session( |
| 9904 SpdySessionDependencies::SpdyCreateSessionDeterministic(&session_deps)); |
| 9905 |
| 9906 // Start the first transaction to set up the SpdySession |
| 9907 HttpRequestInfo request1; |
| 9908 request1.method = "GET"; |
| 9909 request1.url = GURL(httpsUrl); |
| 9910 request1.priority = LOWEST; |
| 9911 request1.load_flags = 0; |
| 9912 HttpNetworkTransaction trans1(session); |
| 9913 TestCompletionCallback callback1; |
| 9914 EXPECT_EQ(ERR_IO_PENDING, |
| 9915 trans1.Start(&request1, callback1.callback(), BoundNetLog())); |
| 9916 MessageLoop::current()->RunAllPending(); |
| 9917 data1->RunFor(4); |
| 9918 |
| 9919 EXPECT_EQ(OK, callback1.WaitForResult()); |
| 9920 EXPECT_TRUE(trans1.GetResponseInfo()->was_fetched_via_spdy); |
| 9921 |
| 9922 // Now, start the HTTP request |
| 9923 HttpRequestInfo request2; |
| 9924 request2.method = "GET"; |
| 9925 request2.url = GURL(httpUrl); |
| 9926 request2.priority = MEDIUM; |
| 9927 request2.load_flags = 0; |
| 9928 HttpNetworkTransaction trans2(session); |
| 9929 TestCompletionCallback callback2; |
| 9930 EXPECT_EQ(ERR_IO_PENDING, |
| 9931 trans2.Start(&request2, callback2.callback(), BoundNetLog())); |
| 9932 MessageLoop::current()->RunAllPending(); |
| 9933 data1->RunFor(3); |
| 9934 |
| 9935 EXPECT_EQ(OK, callback2.WaitForResult()); |
| 9936 EXPECT_TRUE(trans2.GetResponseInfo()->was_fetched_via_spdy); |
| 9937 } |
| 9938 |
| 9939 TEST_F(HttpNetworkTransactionSpdy2Test, UseSpdySessionForHttpWhenForced) { |
| 9940 HttpStreamFactory::set_force_spdy_always(true); |
| 9941 const std::string httpsUrl = "https://www.google.com/"; |
| 9942 const std::string httpUrl = "http://www.google.com:443/"; |
| 9943 |
| 9944 // SPDY GET for HTTPS URL |
| 9945 scoped_ptr<SpdyFrame> req1(ConstructSpdyGet(httpsUrl.c_str(), |
| 9946 false, 1, LOWEST)); |
| 9947 // SPDY GET for the HTTP URL |
| 9948 scoped_ptr<SpdyFrame> req2(ConstructSpdyGet(httpUrl.c_str(), |
| 9949 false, 3, MEDIUM)); |
| 9950 |
| 9951 MockWrite writes[] = { |
| 9952 CreateMockWrite(*req1, 1), |
| 9953 CreateMockWrite(*req2, 4), |
| 9954 }; |
| 9955 |
| 9956 scoped_ptr<SpdyFrame> resp1(ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 9957 scoped_ptr<SpdyFrame> body1(ConstructSpdyBodyFrame(1, true)); |
| 9958 scoped_ptr<SpdyFrame> resp2(ConstructSpdyGetSynReply(NULL, 0, 3)); |
| 9959 scoped_ptr<SpdyFrame> body2(ConstructSpdyBodyFrame(3, true)); |
| 9960 MockRead reads[] = { |
| 9961 CreateMockRead(*resp1, 2), |
| 9962 CreateMockRead(*body1, 3), |
| 9963 CreateMockRead(*resp2, 5), |
| 9964 CreateMockRead(*body2, 6), |
| 9965 MockRead(ASYNC, ERR_IO_PENDING, 7) |
| 9966 }; |
| 9967 |
| 9968 scoped_ptr<OrderedSocketData> data( |
| 9969 new OrderedSocketData(reads, arraysize(reads), |
| 9970 writes, arraysize(writes))); |
| 9971 |
| 9972 SessionDependencies session_deps; |
| 9973 SSLSocketDataProvider ssl(ASYNC, OK); |
| 9974 ssl.SetNextProto(kProtoSPDY2); |
| 9975 session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 9976 session_deps.socket_factory.AddSocketDataProvider(data.get()); |
| 9977 |
| 9978 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 9979 |
| 9980 // Start the first transaction to set up the SpdySession |
| 9981 HttpRequestInfo request1; |
| 9982 request1.method = "GET"; |
| 9983 request1.url = GURL(httpsUrl); |
| 9984 request1.priority = LOWEST; |
| 9985 request1.load_flags = 0; |
| 9986 HttpNetworkTransaction trans1(session); |
| 9987 TestCompletionCallback callback1; |
| 9988 EXPECT_EQ(ERR_IO_PENDING, |
| 9989 trans1.Start(&request1, callback1.callback(), BoundNetLog())); |
| 9990 MessageLoop::current()->RunAllPending(); |
| 9991 |
| 9992 EXPECT_EQ(OK, callback1.WaitForResult()); |
| 9993 EXPECT_TRUE(trans1.GetResponseInfo()->was_fetched_via_spdy); |
| 9994 |
| 9995 // Now, start the HTTP request |
| 9996 HttpRequestInfo request2; |
| 9997 request2.method = "GET"; |
| 9998 request2.url = GURL(httpUrl); |
| 9999 request2.priority = MEDIUM; |
| 10000 request2.load_flags = 0; |
| 10001 HttpNetworkTransaction trans2(session); |
| 10002 TestCompletionCallback callback2; |
| 10003 EXPECT_EQ(ERR_IO_PENDING, |
| 10004 trans2.Start(&request2, callback2.callback(), BoundNetLog())); |
| 10005 MessageLoop::current()->RunAllPending(); |
| 10006 |
| 10007 EXPECT_EQ(OK, callback2.WaitForResult()); |
| 10008 EXPECT_TRUE(trans2.GetResponseInfo()->was_fetched_via_spdy); |
| 10009 } |
| 10010 |
| 9757 } // namespace net | 10011 } // namespace net |
| OLD | NEW |