| 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 9787 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 9798 data_writes2, arraysize(data_writes2)); | 9798 data_writes2, arraysize(data_writes2)); |
| 9799 StaticSocketDataProvider* data[] = { &data1, &data2 }; | 9799 StaticSocketDataProvider* data[] = { &data1, &data2 }; |
| 9800 | 9800 |
| 9801 SimpleGetHelperResult out = SimpleGetHelperForData(data, arraysize(data)); | 9801 SimpleGetHelperResult out = SimpleGetHelperForData(data, arraysize(data)); |
| 9802 | 9802 |
| 9803 EXPECT_EQ(OK, out.rv); | 9803 EXPECT_EQ(OK, out.rv); |
| 9804 EXPECT_EQ("HTTP/1.0 200 OK", out.status_line); | 9804 EXPECT_EQ("HTTP/1.0 200 OK", out.status_line); |
| 9805 EXPECT_EQ("hello world", out.response_data); | 9805 EXPECT_EQ("hello world", out.response_data); |
| 9806 } | 9806 } |
| 9807 | 9807 |
| 9808 TEST_F(HttpNetworkTransactionSpdy3Test, DoNotUseSpdySessionForHttp) { |
| 9809 const std::string https_url = "https://www.google.com/"; |
| 9810 const std::string httpUrl = "http://www.google.com:443/"; |
| 9811 |
| 9812 // SPDY GET for HTTPS URL |
| 9813 scoped_ptr<SpdyFrame> req1(ConstructSpdyGet(https_url.c_str(), |
| 9814 false, 1, LOWEST)); |
| 9815 |
| 9816 MockWrite writes1[] = { |
| 9817 CreateMockWrite(*req1, 0), |
| 9818 }; |
| 9819 |
| 9820 scoped_ptr<SpdyFrame> resp1(ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 9821 scoped_ptr<SpdyFrame> body1(ConstructSpdyBodyFrame(1, true)); |
| 9822 MockRead reads1[] = { |
| 9823 CreateMockRead(*resp1, 1), |
| 9824 CreateMockRead(*body1, 2), |
| 9825 MockRead(ASYNC, ERR_IO_PENDING, 3) |
| 9826 }; |
| 9827 |
| 9828 scoped_ptr<DelayedSocketData> data1( |
| 9829 new DelayedSocketData(1, reads1, arraysize(reads1), |
| 9830 writes1, arraysize(writes1))); |
| 9831 MockConnect connect_data1(ASYNC, OK); |
| 9832 data1->set_connect_data(connect_data1); |
| 9833 |
| 9834 // HTTP GET for the HTTP URL |
| 9835 MockWrite writes2[] = { |
| 9836 MockWrite(ASYNC, 4, |
| 9837 "GET / HTTP/1.1\r\n" |
| 9838 "Host: www.google.com:443\r\n" |
| 9839 "Connection: keep-alive\r\n\r\n"), |
| 9840 }; |
| 9841 |
| 9842 MockRead reads2[] = { |
| 9843 MockRead(ASYNC, 5, "HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), |
| 9844 MockRead(ASYNC, 6, "hello"), |
| 9845 MockRead(ASYNC, 7, OK), |
| 9846 }; |
| 9847 |
| 9848 scoped_ptr<DelayedSocketData> data2( |
| 9849 new DelayedSocketData(1, reads2, arraysize(reads2), |
| 9850 writes2, arraysize(writes2))); |
| 9851 |
| 9852 SessionDependencies session_deps; |
| 9853 SSLSocketDataProvider ssl(ASYNC, OK); |
| 9854 ssl.SetNextProto(kProtoSPDY3); |
| 9855 session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 9856 session_deps.socket_factory.AddSocketDataProvider(data1.get()); |
| 9857 session_deps.socket_factory.AddSocketDataProvider(data2.get()); |
| 9858 |
| 9859 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 9860 |
| 9861 // Start the first transaction to set up the SpdySession |
| 9862 HttpRequestInfo request1; |
| 9863 request1.method = "GET"; |
| 9864 request1.url = GURL(https_url); |
| 9865 request1.priority = LOWEST; |
| 9866 request1.load_flags = 0; |
| 9867 HttpNetworkTransaction trans1(session); |
| 9868 TestCompletionCallback callback1; |
| 9869 EXPECT_EQ(ERR_IO_PENDING, |
| 9870 trans1.Start(&request1, callback1.callback(), BoundNetLog())); |
| 9871 MessageLoop::current()->RunAllPending(); |
| 9872 |
| 9873 EXPECT_EQ(OK, callback1.WaitForResult()); |
| 9874 EXPECT_TRUE(trans1.GetResponseInfo()->was_fetched_via_spdy); |
| 9875 |
| 9876 // Now, start the HTTP request |
| 9877 HttpRequestInfo request2; |
| 9878 request2.method = "GET"; |
| 9879 request2.url = GURL(httpUrl); |
| 9880 request2.priority = MEDIUM; |
| 9881 request2.load_flags = 0; |
| 9882 HttpNetworkTransaction trans2(session); |
| 9883 TestCompletionCallback callback2; |
| 9884 EXPECT_EQ(ERR_IO_PENDING, |
| 9885 trans2.Start(&request2, callback2.callback(), BoundNetLog())); |
| 9886 MessageLoop::current()->RunAllPending(); |
| 9887 |
| 9888 EXPECT_EQ(OK, callback2.WaitForResult()); |
| 9889 EXPECT_FALSE(trans2.GetResponseInfo()->was_fetched_via_spdy); |
| 9890 } |
| 9891 |
| 9892 TEST_F(HttpNetworkTransactionSpdy3Test, DoNotUseSpdySessionForHttpOverTunnel) { |
| 9893 const std::string https_url = "https://www.google.com/"; |
| 9894 const std::string httpUrl = "http://www.google.com:443/"; |
| 9895 |
| 9896 // SPDY GET for HTTPS URL (through CONNECT tunnel) |
| 9897 scoped_ptr<SpdyFrame> connect(ConstructSpdyConnect(NULL, 0, 1)); |
| 9898 scoped_ptr<SpdyFrame> req1(ConstructSpdyGet(https_url.c_str(), |
| 9899 false, 1, LOWEST)); |
| 9900 |
| 9901 scoped_ptr<SpdyFrame> wrapped_req1(ConstructWrappedSpdyFrame(req1, 1)); |
| 9902 // SPDY GET for HTTP URL (through the proxy, but not the tunnel) |
| 9903 scoped_ptr<SpdyFrame> req2(ConstructSpdyGet(httpUrl.c_str(), |
| 9904 false, 3, MEDIUM)); |
| 9905 |
| 9906 MockWrite writes1[] = { |
| 9907 CreateMockWrite(*connect, 0), |
| 9908 CreateMockWrite(*wrapped_req1, 2), |
| 9909 CreateMockWrite(*req2, 5), |
| 9910 }; |
| 9911 |
| 9912 scoped_ptr<SpdyFrame> conn_resp(ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 9913 scoped_ptr<SpdyFrame> resp1(ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 9914 scoped_ptr<SpdyFrame> body1(ConstructSpdyBodyFrame(1, true)); |
| 9915 scoped_ptr<SpdyFrame> wrapped_resp1(ConstructWrappedSpdyFrame(resp1, 1)); |
| 9916 scoped_ptr<SpdyFrame> wrapped_body1(ConstructWrappedSpdyFrame(body1, 1)); |
| 9917 scoped_ptr<SpdyFrame> resp2(ConstructSpdyGetSynReply(NULL, 0, 3)); |
| 9918 scoped_ptr<SpdyFrame> body2(ConstructSpdyBodyFrame(3, true)); |
| 9919 MockRead reads1[] = { |
| 9920 CreateMockRead(*conn_resp, 1), |
| 9921 CreateMockRead(*wrapped_resp1, 3), |
| 9922 CreateMockRead(*wrapped_body1, 4), |
| 9923 CreateMockRead(*resp2, 6), |
| 9924 CreateMockRead(*body2, 7), |
| 9925 MockRead(ASYNC, ERR_IO_PENDING, 8) |
| 9926 }; |
| 9927 |
| 9928 scoped_refptr<DeterministicSocketData> data1( |
| 9929 new DeterministicSocketData(reads1, arraysize(reads1), |
| 9930 writes1, arraysize(writes1))); |
| 9931 MockConnect connect_data1(ASYNC, OK); |
| 9932 data1->set_connect_data(connect_data1); |
| 9933 |
| 9934 SpdySessionDependencies session_deps(ProxyService::CreateFixed( |
| 9935 "https://proxy:70")); |
| 9936 SSLSocketDataProvider ssl1(ASYNC, OK); // to the proxy |
| 9937 ssl1.SetNextProto(kProtoSPDY3); |
| 9938 session_deps.deterministic_socket_factory->AddSSLSocketDataProvider(&ssl1); |
| 9939 SSLSocketDataProvider ssl2(ASYNC, OK); // to the server |
| 9940 ssl2.SetNextProto(kProtoSPDY3); |
| 9941 session_deps.deterministic_socket_factory->AddSSLSocketDataProvider(&ssl2); |
| 9942 session_deps.deterministic_socket_factory->AddSocketDataProvider(data1.get()); |
| 9943 |
| 9944 scoped_refptr<HttpNetworkSession> session( |
| 9945 SpdySessionDependencies::SpdyCreateSessionDeterministic(&session_deps)); |
| 9946 |
| 9947 // Start the first transaction to set up the SpdySession |
| 9948 HttpRequestInfo request1; |
| 9949 request1.method = "GET"; |
| 9950 request1.url = GURL(https_url); |
| 9951 request1.priority = LOWEST; |
| 9952 request1.load_flags = 0; |
| 9953 HttpNetworkTransaction trans1(session); |
| 9954 TestCompletionCallback callback1; |
| 9955 EXPECT_EQ(ERR_IO_PENDING, |
| 9956 trans1.Start(&request1, callback1.callback(), BoundNetLog())); |
| 9957 MessageLoop::current()->RunAllPending(); |
| 9958 data1->RunFor(4); |
| 9959 |
| 9960 EXPECT_EQ(OK, callback1.WaitForResult()); |
| 9961 EXPECT_TRUE(trans1.GetResponseInfo()->was_fetched_via_spdy); |
| 9962 |
| 9963 // Now, start the HTTP request |
| 9964 HttpRequestInfo request2; |
| 9965 request2.method = "GET"; |
| 9966 request2.url = GURL(httpUrl); |
| 9967 request2.priority = MEDIUM; |
| 9968 request2.load_flags = 0; |
| 9969 HttpNetworkTransaction trans2(session); |
| 9970 TestCompletionCallback callback2; |
| 9971 EXPECT_EQ(ERR_IO_PENDING, |
| 9972 trans2.Start(&request2, callback2.callback(), BoundNetLog())); |
| 9973 MessageLoop::current()->RunAllPending(); |
| 9974 data1->RunFor(3); |
| 9975 |
| 9976 EXPECT_EQ(OK, callback2.WaitForResult()); |
| 9977 EXPECT_TRUE(trans2.GetResponseInfo()->was_fetched_via_spdy); |
| 9978 } |
| 9979 |
| 9980 TEST_F(HttpNetworkTransactionSpdy3Test, UseSpdySessionForHttpWhenForced) { |
| 9981 HttpStreamFactory::set_force_spdy_always(true); |
| 9982 const std::string https_url = "https://www.google.com/"; |
| 9983 const std::string http_url = "http://www.google.com:443/"; |
| 9984 |
| 9985 // SPDY GET for HTTPS URL |
| 9986 scoped_ptr<SpdyFrame> req1(ConstructSpdyGet(https_url.c_str(), |
| 9987 false, 1, LOWEST)); |
| 9988 // SPDY GET for the HTTP URL |
| 9989 scoped_ptr<SpdyFrame> req2(ConstructSpdyGet(http_url.c_str(), |
| 9990 false, 3, MEDIUM)); |
| 9991 |
| 9992 MockWrite writes[] = { |
| 9993 CreateMockWrite(*req1, 1), |
| 9994 CreateMockWrite(*req2, 4), |
| 9995 }; |
| 9996 |
| 9997 scoped_ptr<SpdyFrame> resp1(ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 9998 scoped_ptr<SpdyFrame> body1(ConstructSpdyBodyFrame(1, true)); |
| 9999 scoped_ptr<SpdyFrame> resp2(ConstructSpdyGetSynReply(NULL, 0, 3)); |
| 10000 scoped_ptr<SpdyFrame> body2(ConstructSpdyBodyFrame(3, true)); |
| 10001 MockRead reads[] = { |
| 10002 CreateMockRead(*resp1, 2), |
| 10003 CreateMockRead(*body1, 3), |
| 10004 CreateMockRead(*resp2, 5), |
| 10005 CreateMockRead(*body2, 6), |
| 10006 MockRead(ASYNC, ERR_IO_PENDING, 7) |
| 10007 }; |
| 10008 |
| 10009 scoped_ptr<OrderedSocketData> data( |
| 10010 new OrderedSocketData(reads, arraysize(reads), |
| 10011 writes, arraysize(writes))); |
| 10012 |
| 10013 SessionDependencies session_deps; |
| 10014 SSLSocketDataProvider ssl(ASYNC, OK); |
| 10015 ssl.SetNextProto(kProtoSPDY3); |
| 10016 session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 10017 session_deps.socket_factory.AddSocketDataProvider(data.get()); |
| 10018 |
| 10019 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 10020 |
| 10021 // Start the first transaction to set up the SpdySession |
| 10022 HttpRequestInfo request1; |
| 10023 request1.method = "GET"; |
| 10024 request1.url = GURL(https_url); |
| 10025 request1.priority = LOWEST; |
| 10026 request1.load_flags = 0; |
| 10027 HttpNetworkTransaction trans1(session); |
| 10028 TestCompletionCallback callback1; |
| 10029 EXPECT_EQ(ERR_IO_PENDING, |
| 10030 trans1.Start(&request1, callback1.callback(), BoundNetLog())); |
| 10031 MessageLoop::current()->RunAllPending(); |
| 10032 |
| 10033 EXPECT_EQ(OK, callback1.WaitForResult()); |
| 10034 EXPECT_TRUE(trans1.GetResponseInfo()->was_fetched_via_spdy); |
| 10035 |
| 10036 // Now, start the HTTP request |
| 10037 HttpRequestInfo request2; |
| 10038 request2.method = "GET"; |
| 10039 request2.url = GURL(http_url); |
| 10040 request2.priority = MEDIUM; |
| 10041 request2.load_flags = 0; |
| 10042 HttpNetworkTransaction trans2(session); |
| 10043 TestCompletionCallback callback2; |
| 10044 EXPECT_EQ(ERR_IO_PENDING, |
| 10045 trans2.Start(&request2, callback2.callback(), BoundNetLog())); |
| 10046 MessageLoop::current()->RunAllPending(); |
| 10047 |
| 10048 EXPECT_EQ(OK, callback2.WaitForResult()); |
| 10049 EXPECT_TRUE(trans2.GetResponseInfo()->was_fetched_via_spdy); |
| 10050 } |
| 10051 |
| 9808 } // namespace net | 10052 } // namespace net |
| OLD | NEW |