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